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