- correctly initialize start address for XScale trace buffer decodes in fill-once...
[fw/openocd] / src / target / arm_jtag.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "arm_jtag.h"
25
26 #include "binarybuffer.h"
27 #include "log.h"
28 #include "jtag.h"
29
30 #include <stdlib.h>
31
32 #if 0
33 #define _ARM_JTAG_SCAN_N_CHECK_
34 #endif
35
36 int arm_jtag_set_instr_error_handler(u8 *in_value, void *priv)
37 {
38         ERROR("setting the new JTAG instruction failed, debugging is likely to be broken");
39         
40         return ERROR_OK;
41 }
42
43 int arm_jtag_set_instr(arm_jtag_t *jtag_info, u32 new_instr, error_handler_t *caller_error_handler)
44 {
45         jtag_device_t *device = jtag_get_device(jtag_info->chain_pos);
46         
47         if (buf_get_u32(device->cur_instr, 0, device->ir_length) != new_instr)
48         {
49                 scan_field_t field;
50         
51                 field.device = jtag_info->chain_pos;
52                 field.num_bits = device->ir_length;
53                 field.out_value = calloc(CEIL(field.num_bits, 8), 1);
54                 buf_set_u32(field.out_value, 0, field.num_bits, new_instr);
55                 field.out_mask = NULL;
56                 field.in_value = NULL;
57                 field.in_check_value = NULL;
58                 field.in_check_mask = NULL;
59                 field.in_handler = NULL;
60                 field.in_handler_priv = NULL;
61                 
62                 if (caller_error_handler)
63                 {
64                         jtag_add_ir_scan(1, &field, -1, caller_error_handler);
65                 }
66                 else
67                 {
68                         error_handler_t error_handler;
69                         error_handler.error_handler = arm_jtag_set_instr_error_handler;
70                         error_handler.error_handler_priv = NULL;
71                         jtag_add_ir_scan(1, &field, -1, &error_handler);
72                 }
73                 
74                 
75                 free(field.out_value);
76         }
77         
78         return ERROR_OK;
79 }
80
81 int arm_jtag_scann(arm_jtag_t *jtag_info, u32 new_scan_chain)
82 {
83         if(jtag_info->cur_scan_chain != new_scan_chain)
84         {
85 #ifdef _ARM_JTAG_SCAN_N_CHECK_
86                 u8 scan_n_check_value = 0x10;
87 #endif
88                 scan_field_t field;
89                 
90                 field.device = jtag_info->chain_pos;
91                 field.num_bits = jtag_info->scann_size;
92                 field.out_value = calloc(CEIL(field.num_bits, 8), 1);
93                 buf_set_u32(field.out_value, 0, field.num_bits, new_scan_chain);
94                 field.out_mask = NULL;
95                 field.in_value = NULL;
96 #ifdef _ARM_JTAG_SCAN_N_CHECK_
97                 field.in_check_value = &scan_n_check_value;
98 #else
99                 field.in_check_value = NULL;
100 #endif 
101                 field.in_check_mask = NULL;
102                 field.in_handler = NULL;
103                 field.in_handler_priv = NULL;
104                 
105                 arm_jtag_set_instr(jtag_info, jtag_info->scann_instr, NULL);
106                 jtag_add_dr_scan(1, &field, -1, NULL);
107                 
108                 jtag_info->cur_scan_chain = new_scan_chain;
109                 
110                 free(field.out_value);
111         }
112         
113         return ERROR_OK;
114 }
115
116 int arm_jtag_reset_callback(enum jtag_event event, void *priv)
117 {
118         arm_jtag_t *jtag_info = priv;
119         
120         if (event == JTAG_TRST_ASSERTED)
121         {
122                 jtag_info->cur_scan_chain = 0;
123         }
124         
125         return ERROR_OK;
126 }
127
128 int arm_jtag_setup_connection(arm_jtag_t *jtag_info)
129 {
130         jtag_info->scann_instr = 0x2;
131         jtag_info->cur_scan_chain = 0;
132         jtag_info->intest_instr = 0xc;
133
134         jtag_register_event_callback(arm_jtag_reset_callback, jtag_info);
135         
136         return ERROR_OK;
137 }
138
139 /* read JTAG buffer into host-endian u32, flipping bit-order */
140 int arm_jtag_buf_to_u32_flip(u8 *in_buf, void *priv)
141 {
142         u32 *dest = priv;
143         *dest = flip_u32(le_to_h_u32(in_buf), 32);
144         return ERROR_OK;
145 }
146
147 /* read JTAG buffer into little-endian u32, flipping bit-order */
148 int arm_jtag_buf_to_le32_flip(u8 *in_buf, void *priv)
149 {
150         h_u32_to_le(((u8*)priv), flip_u32(le_to_h_u32(in_buf), 32));
151         return ERROR_OK;
152 }
153
154 /* read JTAG buffer into little-endian u16, flipping bit-order */
155 int arm_jtag_buf_to_le16_flip(u8 *in_buf, void *priv)
156 {
157         h_u16_to_le(((u8*)priv), flip_u32(le_to_h_u32(in_buf), 32) & 0xffff);
158         return ERROR_OK;
159 }
160
161 /* read JTAG buffer into big-endian u32, flipping bit-order */
162 int arm_jtag_buf_to_be32_flip(u8 *in_buf, void *priv)
163 {
164         h_u32_to_be(((u8*)priv), flip_u32(le_to_h_u32(in_buf), 32));
165         return ERROR_OK;
166 }
167
168 /* read JTAG buffer into big-endian u16, flipping bit-order */
169 int arm_jtag_buf_to_be16_flip(u8 *in_buf, void *priv)
170 {
171         h_u16_to_be(((u8*)priv), flip_u32(le_to_h_u32(in_buf), 32) & 0xffff);
172         return ERROR_OK;
173 }
174
175 /* read JTAG buffer into u8, flipping bit-order */
176 int arm_jtag_buf_to_8_flip(u8 *in_buf, void *priv)
177 {
178         u8 *dest = priv;
179         *dest = flip_u32(le_to_h_u32(in_buf), 32) & 0xff;
180         return ERROR_OK;
181 }
182
183 /* not-flipping variants */
184 /* read JTAG buffer into host-endian u32 */
185 int arm_jtag_buf_to_u32(u8 *in_buf, void *priv)
186 {
187         u32 *dest = priv;
188         *dest = le_to_h_u32(in_buf);
189         return ERROR_OK;
190 }
191
192 /* read JTAG buffer into little-endian u32 */
193 int arm_jtag_buf_to_le32(u8 *in_buf, void *priv)
194 {
195         h_u32_to_le(((u8*)priv), le_to_h_u32(in_buf));
196         return ERROR_OK;
197 }
198
199 /* read JTAG buffer into little-endian u16 */
200 int arm_jtag_buf_to_le16(u8 *in_buf, void *priv)
201 {
202         h_u16_to_le(((u8*)priv), le_to_h_u32(in_buf) & 0xffff);
203         return ERROR_OK;
204 }
205
206 /* read JTAG buffer into big-endian u32 */
207 int arm_jtag_buf_to_be32(u8 *in_buf, void *priv)
208 {
209         h_u32_to_be(((u8*)priv), le_to_h_u32(in_buf));
210         return ERROR_OK;
211 }
212
213 /* read JTAG buffer into big-endian u16 */
214 int arm_jtag_buf_to_be16(u8 *in_buf, void *priv)
215 {
216         h_u16_to_be(((u8*)priv), le_to_h_u32(in_buf) & 0xffff);
217         return ERROR_OK;
218 }
219
220 /* read JTAG buffer into u8 */
221 int arm_jtag_buf_to_8(u8 *in_buf, void *priv)
222 {
223         u8 *dest = priv;
224         *dest = le_to_h_u32(in_buf) & 0xff;
225         return ERROR_OK;
226 }