1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
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. *
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. *
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 ***************************************************************************/
26 #include "binarybuffer.h"
33 #define _ARM_JTAG_SCAN_N_CHECK_
36 int arm_jtag_set_instr_error_handler(u8 *in_value, void *priv)
38 ERROR("setting the new JTAG instruction failed, debugging is likely to be broken");
43 int arm_jtag_set_instr(arm_jtag_t *jtag_info, u32 new_instr, error_handler_t *caller_error_handler)
45 jtag_device_t *device = jtag_get_device(jtag_info->chain_pos);
47 if (buf_get_u32(device->cur_instr, 0, device->ir_length) != new_instr)
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;
64 if (caller_error_handler)
66 jtag_set_check_value(&field, NULL, NULL, caller_error_handler);
70 error_handler_t error_handler;
71 error_handler.error_handler = arm_jtag_set_instr_error_handler;
72 error_handler.error_handler_priv = NULL;
73 jtag_set_check_value(&field, NULL, NULL, &error_handler);
75 jtag_add_ir_scan(1, &field, -1, NULL);
78 free(field.out_value);
84 int arm_jtag_scann(arm_jtag_t *jtag_info, u32 new_scan_chain)
86 if(jtag_info->cur_scan_chain != new_scan_chain)
88 #ifdef _ARM_JTAG_SCAN_N_CHECK_
89 u8 scan_n_check_value = 1 << (jtag_info->scann_size - 1);
93 field.device = jtag_info->chain_pos;
94 field.num_bits = jtag_info->scann_size;
95 field.out_value = calloc(CEIL(field.num_bits, 8), 1);
96 buf_set_u32(field.out_value, 0, field.num_bits, new_scan_chain);
97 field.out_mask = NULL;
98 field.in_value = NULL;
99 #ifdef _ARM_JTAG_SCAN_N_CHECK_
100 jtag_set_check_value(&field, &scan_n_check_value, NULL, NULL, NULL);
102 field.in_handler = NULL;
103 field.in_handler_priv = NULL;
107 arm_jtag_set_instr(jtag_info, jtag_info->scann_instr, NULL);
108 jtag_add_dr_scan(1, &field, -1, NULL);
110 jtag_info->cur_scan_chain = new_scan_chain;
112 free(field.out_value);
118 int arm_jtag_reset_callback(enum jtag_event event, void *priv)
120 arm_jtag_t *jtag_info = priv;
122 if (event == JTAG_TRST_ASSERTED)
124 jtag_info->cur_scan_chain = 0;
130 int arm_jtag_setup_connection(arm_jtag_t *jtag_info)
132 jtag_info->scann_instr = 0x2;
133 jtag_info->cur_scan_chain = 0;
134 jtag_info->intest_instr = 0xc;
136 jtag_register_event_callback(arm_jtag_reset_callback, jtag_info);
141 /* read JTAG buffer into host-endian u32, flipping bit-order */
142 int arm_jtag_buf_to_u32_flip(u8 *in_buf, void *priv)
145 *dest = flip_u32(le_to_h_u32(in_buf), 32);
149 /* read JTAG buffer into little-endian u32, flipping bit-order */
150 int arm_jtag_buf_to_le32_flip(u8 *in_buf, void *priv)
152 h_u32_to_le(((u8*)priv), flip_u32(le_to_h_u32(in_buf), 32));
156 /* read JTAG buffer into little-endian u16, flipping bit-order */
157 int arm_jtag_buf_to_le16_flip(u8 *in_buf, void *priv)
159 h_u16_to_le(((u8*)priv), flip_u32(le_to_h_u32(in_buf), 32) & 0xffff);
163 /* read JTAG buffer into big-endian u32, flipping bit-order */
164 int arm_jtag_buf_to_be32_flip(u8 *in_buf, void *priv)
166 h_u32_to_be(((u8*)priv), flip_u32(le_to_h_u32(in_buf), 32));
170 /* read JTAG buffer into big-endian u16, flipping bit-order */
171 int arm_jtag_buf_to_be16_flip(u8 *in_buf, void *priv)
173 h_u16_to_be(((u8*)priv), flip_u32(le_to_h_u32(in_buf), 32) & 0xffff);
177 /* read JTAG buffer into u8, flipping bit-order */
178 int arm_jtag_buf_to_8_flip(u8 *in_buf, void *priv)
181 *dest = flip_u32(le_to_h_u32(in_buf), 32) & 0xff;
185 /* not-flipping variants */
186 /* read JTAG buffer into host-endian u32 */
187 int arm_jtag_buf_to_u32(u8 *in_buf, void *priv)
190 *dest = le_to_h_u32(in_buf);
194 /* read JTAG buffer into little-endian u32 */
195 int arm_jtag_buf_to_le32(u8 *in_buf, void *priv)
197 h_u32_to_le(((u8*)priv), le_to_h_u32(in_buf));
201 /* read JTAG buffer into little-endian u16 */
202 int arm_jtag_buf_to_le16(u8 *in_buf, void *priv)
204 h_u16_to_le(((u8*)priv), le_to_h_u32(in_buf) & 0xffff);
208 /* read JTAG buffer into big-endian u32 */
209 int arm_jtag_buf_to_be32(u8 *in_buf, void *priv)
211 h_u32_to_be(((u8*)priv), le_to_h_u32(in_buf));
215 /* read JTAG buffer into big-endian u16 */
216 int arm_jtag_buf_to_be16(u8 *in_buf, void *priv)
218 h_u16_to_be(((u8*)priv), le_to_h_u32(in_buf) & 0xffff);
222 /* read JTAG buffer into u8 */
223 int arm_jtag_buf_to_8(u8 *in_buf, void *priv)
226 *dest = le_to_h_u32(in_buf) & 0xff;