1 |
14 |
nyawn |
/* legacy_dbg_commands.c -- JTAG protocol bridge between GDB and OpenCores debug module.
|
2 |
|
|
Copyright(C) 2001 Marko Mlinar, markom@opencores.org
|
3 |
|
|
Code for TCP/IP copied from gdb, by Chris Ziomkowski
|
4 |
|
|
Adapted for the Advanced JTAG Bridge by Nathan Yawn, 2009
|
5 |
|
|
|
6 |
|
|
This file was part of the OpenRISC 1000 Architectural Simulator.
|
7 |
|
|
It is now also used to connect GDB to a running hardware OpenCores / OR1200
|
8 |
|
|
debug unit.
|
9 |
|
|
|
10 |
|
|
This program is free software; you can redistribute it and/or modify
|
11 |
|
|
it under the terms of the GNU General Public License as published by
|
12 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
13 |
|
|
(at your option) any later version.
|
14 |
|
|
|
15 |
|
|
This program is distributed in the hope that it will be useful,
|
16 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
|
|
GNU General Public License for more details.
|
19 |
|
|
|
20 |
|
|
You should have received a copy of the GNU General Public License
|
21 |
|
|
along with this program; if not, write to the Free Software
|
22 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
23 |
|
|
|
24 |
|
|
#include <assert.h>
|
25 |
|
|
#include <stdio.h>
|
26 |
|
|
#include <ctype.h>
|
27 |
|
|
#include <string.h>
|
28 |
|
|
#include <stdlib.h>
|
29 |
|
|
#include <unistd.h>
|
30 |
|
|
#include <stdarg.h>
|
31 |
|
|
#include <sys/stat.h>
|
32 |
|
|
#include <sys/types.h>
|
33 |
|
|
#include <netinet/in.h> // for htonl
|
34 |
|
|
|
35 |
|
|
#include "chain_commands.h"
|
36 |
|
|
#include "cable_common.h"
|
37 |
|
|
#include "errcodes.h"
|
38 |
|
|
#include "legacy_dbg_commands.h"
|
39 |
|
|
|
40 |
|
|
#define debug(...) //fprintf(stderr, __VA_ARGS__ )
|
41 |
|
|
|
42 |
|
|
#define LEGACY_CRC_POLY 0x04c11db7
|
43 |
|
|
#define DBG_CRC_SIZE 32
|
44 |
|
|
|
45 |
|
|
/* Crc of current read or written data. */
|
46 |
|
|
static int legacy_crc_r, legacy_crc_w = 0;
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
/*----------------------------------------------------------------------------------*/
|
50 |
|
|
// Helper Functions
|
51 |
|
|
|
52 |
|
|
/* Generates new crc, sending in new bit input_bit */
|
53 |
|
|
static unsigned long legacy_crc_calc(unsigned long crc, int input_bit) {
|
54 |
|
|
unsigned long d = (input_bit&1) ? 0xfffffff : 0x0000000;
|
55 |
|
|
unsigned long crc_32 = ((crc >> 31)&1) ? 0xfffffff : 0x0000000;
|
56 |
|
|
crc <<= 1;
|
57 |
|
|
return crc ^ ((d ^ crc_32) & LEGACY_CRC_POLY);
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
/* Writes bitstream. LS bit first if len < 0, MS bit first if len > 0. */
|
61 |
|
|
static void legacy_write_stream(uint32_t stream, int len, int set_last_bit) {
|
62 |
|
|
int i;
|
63 |
|
|
uint32_t err;
|
64 |
|
|
uint32_t outdata = 0;
|
65 |
|
|
uint32_t datacpy = stream;
|
66 |
|
|
|
67 |
|
|
// MSB needs to be transferred first, lower levels do LSB first. Reverse.
|
68 |
|
|
for(i = 0; i < len; i++) {
|
69 |
|
|
outdata |= stream & 0x1;
|
70 |
|
|
if(i < (len-1)) {
|
71 |
|
|
outdata <<= 1;
|
72 |
|
|
stream >>= 1;
|
73 |
|
|
}
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
// Call the lower level, in case the driver has a high-speed transfer capability.
|
77 |
|
|
// *** This always transfers LS bit first.
|
78 |
|
|
err = jtag_write_stream(&outdata, len, set_last_bit);
|
79 |
|
|
|
80 |
|
|
debug("legacy_write_stream, stream = 0x%X (0x%X), len = %d, set_last_bit = %d, ret = 0x%X\n", datacpy, outdata, len, set_last_bit, err);
|
81 |
|
|
|
82 |
|
|
if(err != APP_ERR_NONE) {
|
83 |
|
|
fprintf(stderr, "Error in legacy_write_stream: %s\n", get_err_string(err));
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
// The low level call does not compute
|
87 |
|
|
// a CRC. Do so here. Remember, CRC is only calculated using data bits.
|
88 |
|
|
if(len < 0) {
|
89 |
|
|
fprintf(stderr, "Program error: legacy debug JTAG read with negative length!\n");
|
90 |
|
|
/*
|
91 |
|
|
len = -len;
|
92 |
|
|
for(i = 0; i < len; i++) {
|
93 |
|
|
legacy_crc_w = legacy_crc_calc(legacy_crc_w, stream&1);
|
94 |
|
|
datacpy >>= 1;
|
95 |
|
|
}
|
96 |
|
|
*/
|
97 |
|
|
}
|
98 |
|
|
else {
|
99 |
|
|
for(i = len-1; i >= 0; i--) {
|
100 |
|
|
legacy_crc_w = legacy_crc_calc(legacy_crc_w, (datacpy>>i)&1);
|
101 |
|
|
}
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
/* Gets bitstream. LS bit first if len < 0, MS bit first if len > 0. */
|
107 |
|
|
static uint32_t legacy_read_stream(unsigned long stream, int len, int set_last_bit) {
|
108 |
|
|
int i;
|
109 |
|
|
uint32_t data = 0, datacpy = 0;
|
110 |
|
|
uint32_t outdata = stream;
|
111 |
|
|
uint32_t indata;
|
112 |
|
|
uint32_t err;
|
113 |
|
|
|
114 |
|
|
// *** WARNING: We assume that the input ("stream") will always be 0.
|
115 |
|
|
// If it's ever not, then we probably need to reverse the bit order (as
|
116 |
|
|
// is done in legacy_write_stream) before sending.
|
117 |
|
|
|
118 |
|
|
// Call the lower level, in case the driver has a high-speed transfer capability.
|
119 |
|
|
// This always transfers LS bit first.
|
120 |
|
|
err = jtag_read_write_stream(&outdata, &indata, len, 0, set_last_bit);
|
121 |
|
|
|
122 |
|
|
// Data comes from the legacy debug unit MSB first, so we need to
|
123 |
|
|
// reverse the bit order.
|
124 |
|
|
for(i = 0; i < len; i++) {
|
125 |
|
|
data |= indata & 0x1;
|
126 |
|
|
if(i < (len-1)) {
|
127 |
|
|
data <<= 1;
|
128 |
|
|
indata >>= 1;
|
129 |
|
|
}
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
datacpy = data;
|
133 |
|
|
|
134 |
|
|
debug("legacy_read_stream: write 0x%X, read 0x%X, len %i, set_last_bit = %d\n", outdata, data, len, set_last_bit);
|
135 |
|
|
|
136 |
|
|
if(err != APP_ERR_NONE) {
|
137 |
|
|
fprintf(stderr, "Error in legacy_read_stream: %s\n", get_err_string(err));
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
// The low level call does not compute
|
141 |
|
|
// a CRC. Do so here. Remember, CRC is only calculated using data bits.
|
142 |
|
|
if(len < 0) {
|
143 |
|
|
fprintf(stderr, "Program error: legacy debug JTAG read with negative length!\n");
|
144 |
|
|
/*
|
145 |
|
|
len = -len;
|
146 |
|
|
for(i = 0; i < len; i++) {
|
147 |
|
|
legacy_crc_w = legacy_crc_calc(legacy_crc_w, stream&1);
|
148 |
|
|
stream >>= 1;
|
149 |
|
|
legacy_crc_r = legacy_crc_calc(legacy_crc_r, datacpy&1);
|
150 |
|
|
datacpy >>= 1;
|
151 |
|
|
}
|
152 |
|
|
*/
|
153 |
|
|
}
|
154 |
|
|
else {
|
155 |
|
|
for(i = len-1; i >= 0; i--) {
|
156 |
|
|
legacy_crc_w = legacy_crc_calc(legacy_crc_w, (stream>>i)&1);
|
157 |
|
|
legacy_crc_r = legacy_crc_calc(legacy_crc_r, (datacpy>>i)&1);
|
158 |
|
|
}
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
return data;
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
//////////////////////////////////////////////////////////////////////////
|
165 |
|
|
// Actual operations on the legacy debug unit
|
166 |
|
|
|
167 |
|
|
/* Sets scan chain. */
|
168 |
|
|
int legacy_dbg_set_chain(int chain) {
|
169 |
|
|
int status, crc_generated, legacy_crc_read;
|
170 |
|
|
desired_chain = chain;
|
171 |
|
|
|
172 |
|
|
try_again:
|
173 |
|
|
if (current_chain == chain) return APP_ERR_NONE;
|
174 |
|
|
current_chain = -1;
|
175 |
|
|
debug("\nset_chain %i\n", chain);
|
176 |
|
|
tap_set_shift_dr(); /* SHIFT_DR */
|
177 |
|
|
|
178 |
|
|
/* write data, EXIT1_DR */
|
179 |
|
|
legacy_crc_w = 0xffffffff;
|
180 |
|
|
legacy_write_stream(((chain & 0xf) | (1<<DC_SIZE)), DC_SIZE + 1, 0);
|
181 |
|
|
legacy_write_stream(legacy_crc_w, DBG_CRC_SIZE, 0);
|
182 |
|
|
|
183 |
|
|
legacy_crc_r = 0xffffffff;
|
184 |
|
|
status = legacy_read_stream(0, DC_STATUS_SIZE, 0);
|
185 |
|
|
crc_generated = legacy_crc_r;
|
186 |
|
|
legacy_crc_read = legacy_read_stream(0, DBG_CRC_SIZE, 1);
|
187 |
|
|
|
188 |
|
|
debug("Status/CRC read / CRC generated: %x %x %x\n", status, legacy_crc_read, crc_generated);
|
189 |
|
|
/* CRCs must match, otherwise retry */
|
190 |
|
|
if (legacy_crc_read != crc_generated) {
|
191 |
|
|
if (retry_do()) goto try_again;
|
192 |
|
|
else return APP_ERR_CRC;
|
193 |
|
|
}
|
194 |
|
|
/* we should read expected status value, otherwise retry */
|
195 |
|
|
if (status != 0) {
|
196 |
|
|
if (retry_do()) goto try_again;
|
197 |
|
|
else return APP_ERR_BAD_PARAM;
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
/* reset retry counter */
|
201 |
|
|
retry_ok();
|
202 |
|
|
tap_exit_to_idle(); // Transition the TAP back to state IDLE
|
203 |
|
|
current_chain = chain;
|
204 |
|
|
|
205 |
|
|
debug("Successfully set chain to %i\n", current_chain);
|
206 |
|
|
return APP_ERR_NONE;
|
207 |
|
|
}
|
208 |
|
|
|
209 |
|
|
/* sends out a command with 32bit address and 16bit length, if len >= 0 */
|
210 |
|
|
int legacy_dbg_command(int type, unsigned long adr, int len) {
|
211 |
|
|
int status, crc_generated, legacy_crc_read;
|
212 |
|
|
|
213 |
|
|
try_again:
|
214 |
|
|
legacy_dbg_set_chain(desired_chain);
|
215 |
|
|
debug("\ncomm %i\n", type);
|
216 |
|
|
|
217 |
|
|
/***** WRITEx *****/
|
218 |
|
|
tap_set_shift_dr(); /* SHIFT_DR */
|
219 |
|
|
|
220 |
|
|
/* write data, EXIT1_DR */
|
221 |
|
|
legacy_crc_w = 0xffffffff;
|
222 |
|
|
legacy_write_stream(((DI_WRITE_CMD & 0xf) | (0<<DC_SIZE)), DC_SIZE + 1, 0);
|
223 |
|
|
legacy_write_stream(type, 4, 0);
|
224 |
|
|
legacy_write_stream(adr, 32, 0);
|
225 |
|
|
assert(len > 0);
|
226 |
|
|
legacy_write_stream(len - 1, 16, 0);
|
227 |
|
|
legacy_write_stream(legacy_crc_w, DBG_CRC_SIZE, 0);
|
228 |
|
|
|
229 |
|
|
legacy_crc_r = 0xffffffff;
|
230 |
|
|
status = legacy_read_stream(0, DC_STATUS_SIZE, 0);
|
231 |
|
|
crc_generated = legacy_crc_r;
|
232 |
|
|
legacy_crc_read = legacy_read_stream(0, DBG_CRC_SIZE, 1);
|
233 |
|
|
|
234 |
|
|
/* CRCs must match, otherwise retry */
|
235 |
|
|
if (legacy_crc_read != crc_generated) {
|
236 |
|
|
if (retry_do()) goto try_again;
|
237 |
|
|
else return APP_ERR_CRC;
|
238 |
|
|
}
|
239 |
|
|
/* we should read expected status value, otherwise retry */
|
240 |
|
|
if (status != 0) {
|
241 |
|
|
if (retry_do()) goto try_again;
|
242 |
|
|
else return APP_ERR_BAD_PARAM;
|
243 |
|
|
}
|
244 |
|
|
|
245 |
|
|
tap_exit_to_idle(); // Transition the TAP back to state IDLE
|
246 |
|
|
|
247 |
|
|
/* reset retry counter */
|
248 |
|
|
retry_ok();
|
249 |
|
|
return APP_ERR_NONE;
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
/* writes a ctrl reg */
|
253 |
|
|
int legacy_dbg_ctrl(int reset, int stall) {
|
254 |
|
|
int status, crc_generated, legacy_crc_read;
|
255 |
|
|
|
256 |
|
|
try_again:
|
257 |
|
|
legacy_dbg_set_chain(desired_chain);
|
258 |
|
|
debug("\nctrl\n");
|
259 |
|
|
|
260 |
|
|
/***** WRITEx *****/
|
261 |
|
|
tap_set_shift_dr(); /* SHIFT_DR */
|
262 |
|
|
|
263 |
|
|
/* write data, EXIT1_DR */
|
264 |
|
|
legacy_crc_w = 0xffffffff;
|
265 |
|
|
legacy_write_stream(((DI_WRITE_CTRL & 0xf) | (0<<DC_SIZE)), DC_SIZE + 1, 0);
|
266 |
|
|
legacy_write_stream(reset, 1, 0);
|
267 |
|
|
legacy_write_stream(stall, 1, 0);
|
268 |
|
|
legacy_write_stream(0, 32, 0); // legacy_write_stream() has a max size of 32 bits, we need 50
|
269 |
|
|
legacy_write_stream(0, 18, 0);
|
270 |
|
|
legacy_write_stream(legacy_crc_w, DBG_CRC_SIZE, 0);
|
271 |
|
|
|
272 |
|
|
legacy_crc_r = 0xffffffff;
|
273 |
|
|
status = legacy_read_stream(0, DC_STATUS_SIZE, 0);
|
274 |
|
|
crc_generated = legacy_crc_r;
|
275 |
|
|
legacy_crc_read = legacy_read_stream(0, DBG_CRC_SIZE, 1);
|
276 |
|
|
|
277 |
|
|
/* CRCs must match, otherwise retry */
|
278 |
|
|
debug("did ctrl: %x %x %x\n", status, legacy_crc_read, crc_generated);
|
279 |
|
|
if (legacy_crc_read != crc_generated) {
|
280 |
|
|
if (retry_do()) goto try_again;
|
281 |
|
|
else return APP_ERR_CRC;
|
282 |
|
|
}
|
283 |
|
|
/* we should read expected status value, otherwise retry */
|
284 |
|
|
if (status != 0) {
|
285 |
|
|
if (retry_do()) goto try_again;
|
286 |
|
|
else return APP_ERR_BAD_PARAM;
|
287 |
|
|
}
|
288 |
|
|
|
289 |
|
|
tap_exit_to_idle(); // Transition the TAP back to state IDLE
|
290 |
|
|
|
291 |
|
|
/* reset retry counter */
|
292 |
|
|
retry_ok();
|
293 |
|
|
return APP_ERR_NONE;
|
294 |
|
|
}
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
/* reads control register */
|
298 |
|
|
int legacy_dbg_ctrl_read(int *reset, int *stall) {
|
299 |
|
|
int status, crc_generated, legacy_crc_read;
|
300 |
|
|
|
301 |
|
|
try_again:
|
302 |
|
|
legacy_dbg_set_chain(desired_chain);
|
303 |
|
|
debug("\nctrl_read\n");
|
304 |
|
|
|
305 |
|
|
tap_set_shift_dr(); /* SHIFT_DR */
|
306 |
|
|
|
307 |
|
|
/* write data, EXIT1_DR */
|
308 |
|
|
legacy_crc_w = 0xffffffff;
|
309 |
|
|
legacy_write_stream(DI_READ_CTRL | (0<<DC_SIZE), DC_SIZE + 1, 0);
|
310 |
|
|
legacy_write_stream(legacy_crc_w, DBG_CRC_SIZE, 0);
|
311 |
|
|
|
312 |
|
|
legacy_crc_r = 0xffffffff;
|
313 |
|
|
*reset = legacy_read_stream(0, 1, 0);
|
314 |
|
|
*stall = legacy_read_stream(0, 1, 0);
|
315 |
|
|
legacy_read_stream(0, 32, 0); // legacy_read_stream() has a max size of 32 bits, we need 50
|
316 |
|
|
legacy_read_stream(0, 18, 0);
|
317 |
|
|
status = legacy_read_stream(0, DC_STATUS_SIZE, 0);
|
318 |
|
|
crc_generated = legacy_crc_r;
|
319 |
|
|
legacy_crc_read = legacy_read_stream(0, DBG_CRC_SIZE, 1);
|
320 |
|
|
|
321 |
|
|
/* CRCs must match, otherwise retry */
|
322 |
|
|
debug("read ctrl: %x %x %x. reset = %i, stall = %i\n", status, legacy_crc_read, crc_generated, *reset, *stall);
|
323 |
|
|
if (legacy_crc_read != crc_generated) {
|
324 |
|
|
if (retry_do()) goto try_again;
|
325 |
|
|
else return APP_ERR_CRC;
|
326 |
|
|
}
|
327 |
|
|
/* we should read expected status value, otherwise retry */
|
328 |
|
|
if (status != 0) {
|
329 |
|
|
if (retry_do()) goto try_again;
|
330 |
|
|
else return APP_ERR_BAD_PARAM;
|
331 |
|
|
}
|
332 |
|
|
|
333 |
|
|
tap_exit_to_idle(); // Transition the TAP back to state IDLE
|
334 |
|
|
|
335 |
|
|
/* reset retry counter */
|
336 |
|
|
retry_ok();
|
337 |
|
|
return APP_ERR_NONE;
|
338 |
|
|
}
|
339 |
|
|
/* issues a burst read/write */
|
340 |
|
|
int legacy_dbg_go(unsigned char *data, unsigned short len, int read) {
|
341 |
|
|
int status, crc_generated, legacy_crc_read;
|
342 |
|
|
int i;
|
343 |
|
|
|
344 |
|
|
//try_again:
|
345 |
|
|
// No point in re-trying without sending the command again first...
|
346 |
|
|
|
347 |
|
|
legacy_dbg_set_chain(desired_chain);
|
348 |
|
|
debug("\ngo len = %d\n", len);
|
349 |
|
|
|
350 |
|
|
tap_set_shift_dr(); /* SHIFT_DR */
|
351 |
|
|
|
352 |
|
|
/* write data, EXIT1_DR */
|
353 |
|
|
legacy_crc_w = 0xffffffff;
|
354 |
|
|
legacy_write_stream(DI_GO | (0<<DC_SIZE), DC_SIZE + 1, 0);
|
355 |
|
|
if (!read) {
|
356 |
|
|
/* reverse byte ordering, since we must send in big endian */
|
357 |
|
|
for (i = 0; i < len; i++)
|
358 |
|
|
legacy_write_stream(data[i], 8, 0);
|
359 |
|
|
}
|
360 |
|
|
legacy_write_stream(legacy_crc_w, DBG_CRC_SIZE, 0);
|
361 |
|
|
|
362 |
|
|
legacy_crc_r = 0xffffffff;
|
363 |
|
|
|
364 |
|
|
if (read) {
|
365 |
|
|
/* reverse byte ordering, since we must send in big endian */
|
366 |
|
|
for (i = 0; i < len; i++)
|
367 |
|
|
data[i] = legacy_read_stream(0, 8, 0);
|
368 |
|
|
}
|
369 |
|
|
status = legacy_read_stream(0, DC_STATUS_SIZE, 0);
|
370 |
|
|
crc_generated = legacy_crc_r;
|
371 |
|
|
legacy_crc_read = legacy_read_stream(0, DBG_CRC_SIZE, 1);
|
372 |
|
|
|
373 |
|
|
/* CRCs must match, otherwise retry */
|
374 |
|
|
debug("%x %x %x\n", status, legacy_crc_read, crc_generated);
|
375 |
|
|
if (legacy_crc_read != crc_generated) {
|
376 |
|
|
//if (retry_do()) goto try_again;
|
377 |
|
|
//else
|
378 |
|
|
return APP_ERR_CRC;
|
379 |
|
|
}
|
380 |
|
|
/* we should read expected status value, otherwise retry */
|
381 |
|
|
if (status != 0) {
|
382 |
|
|
//if (retry_do()) goto try_again;
|
383 |
|
|
//else
|
384 |
|
|
return status;
|
385 |
|
|
}
|
386 |
|
|
|
387 |
|
|
tap_exit_to_idle(); // Transition the TAP back to state IDLE
|
388 |
|
|
|
389 |
|
|
/* reset retry counter */
|
390 |
|
|
retry_ok();
|
391 |
|
|
return APP_ERR_NONE;
|
392 |
|
|
}
|
393 |
|
|
|