- added support for Intel/Marvel PXA27x (XScale) targets
[openocd.git] / src / jtag / 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 "replacements.h"
25
26 #include "jtag.h"
27
28 #include "command.h"
29 #include "log.h"
30 #include "interpreter.h"
31
32 #include "stdlib.h"
33 #include "string.h"
34 #include <unistd.h>
35
36 char* tap_state_strings[16] =
37 {
38 "tlr",
39 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
40 "rti",
41 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
42 };
43
44 typedef struct cmd_queue_page_s
45 {
46 void *address;
47 size_t used;
48 struct cmd_queue_page_s *next;
49 } cmd_queue_page_t;
50
51 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
52 static cmd_queue_page_t *cmd_queue_pages = NULL;
53
54 /* tap_move[i][j]: tap movement command to go from state i to state j
55 * 0: Test-Logic-Reset
56 * 1: Run-Test/Idle
57 * 2: Shift-DR
58 * 3: Pause-DR
59 * 4: Shift-IR
60 * 5: Pause-IR
61 *
62 * SD->SD and SI->SI have to be caught in interface specific code
63 */
64 u8 tap_move[6][6] =
65 {
66 /* TLR RTI SD PD SI PI */
67 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
68 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
69 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
70 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
71 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
72 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
73 };
74
75 int tap_move_map[16] = {
76 0, -1, -1, 2, -1, 3, -1, -1,
77 1, -1, -1, 4, -1, 5, -1, -1
78 };
79
80 tap_transition_t tap_transitions[16] =
81 {
82 {TAP_TLR, TAP_RTI}, /* TLR */
83 {TAP_SIS, TAP_CD}, /* SDS */
84 {TAP_E1D, TAP_SD}, /* CD */
85 {TAP_E1D, TAP_SD}, /* SD */
86 {TAP_UD, TAP_PD}, /* E1D */
87 {TAP_E2D, TAP_PD}, /* PD */
88 {TAP_UD, TAP_SD}, /* E2D */
89 {TAP_SDS, TAP_RTI}, /* UD */
90 {TAP_SDS, TAP_RTI}, /* RTI */
91 {TAP_TLR, TAP_CI}, /* SIS */
92 {TAP_E1I, TAP_SI}, /* CI */
93 {TAP_E1I, TAP_SI}, /* SI */
94 {TAP_UI, TAP_PI}, /* E1I */
95 {TAP_E2I, TAP_PI}, /* PI */
96 {TAP_UI, TAP_SI}, /* E2I */
97 {TAP_SDS, TAP_RTI} /* UI */
98 };
99
100 char* jtag_event_strings[] =
101 {
102 "SRST asserted",
103 "TRST asserted",
104 "SRST released",
105 "TRST released"
106 };
107
108 enum tap_state end_state = TAP_TLR;
109 enum tap_state cur_state = TAP_TLR;
110 int jtag_trst = 0;
111 int jtag_srst = 0;
112
113 jtag_command_t *jtag_command_queue = NULL;
114 jtag_command_t **last_comand_pointer = &jtag_command_queue;
115 jtag_device_t *jtag_devices = NULL;
116 int jtag_num_devices = 0;
117 int jtag_ir_scan_size = 0;
118 enum reset_types jtag_reset_config = RESET_NONE;
119 enum tap_state cmd_queue_end_state = TAP_TLR;
120 enum tap_state cmd_queue_cur_state = TAP_TLR;
121
122 int jtag_verify_capture_ir = 1;
123
124 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
125 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
126 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
127
128 /* maximum number of JTAG devices expected in the chain
129 */
130 #define JTAG_MAX_CHAIN_SIZE 20
131
132 /* callbacks to inform high-level handlers about JTAG state changes */
133 jtag_event_callback_t *jtag_event_callbacks;
134
135 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
136 */
137 #if BUILD_PARPORT == 1
138 extern jtag_interface_t parport_interface;
139 #endif
140
141 #if BUILD_FT2232_FTD2XX == 1
142 extern jtag_interface_t ft2232_interface;
143 #endif
144
145 #if BUILD_FT2232_LIBFTDI == 1
146 extern jtag_interface_t ft2232_interface;
147 #endif
148
149 #if BUILD_AMTJTAGACCEL == 1
150 extern jtag_interface_t amt_jtagaccel_interface;
151 #endif
152
153 #if BUILD_EP93XX == 1
154 extern jtag_interface_t ep93xx_interface;
155 #endif
156
157 #if BUILD_AT91RM9200 == 1
158 extern jtag_interface_t at91rm9200_interface;
159 #endif
160
161 #if BUILD_GW16012 == 1
162 extern jtag_interface_t gw16012_interface;
163 #endif
164
165 jtag_interface_t *jtag_interfaces[] = {
166 #if BUILD_PARPORT == 1
167 &parport_interface,
168 #endif
169 #if BUILD_FT2232_FTD2XX == 1
170 &ft2232_interface,
171 #endif
172 #if BUILD_FT2232_LIBFTDI == 1
173 &ft2232_interface,
174 #endif
175 #if BUILD_AMTJTAGACCEL == 1
176 &amt_jtagaccel_interface,
177 #endif
178 #if BUILD_EP93XX == 1
179 &ep93xx_interface,
180 #endif
181 #if BUILD_AT91RM9200 == 1
182 &at91rm9200_interface,
183 #endif
184 #if BUILD_GW16012 == 1
185 &gw16012_interface,
186 #endif
187 NULL,
188 };
189
190 jtag_interface_t *jtag = NULL;
191
192 /* configuration */
193 char* jtag_interface = NULL;
194 int jtag_speed = -1;
195
196
197 /* forward declarations */
198 int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state endstate, error_handler_t *error_handler);
199 int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state endstate, error_handler_t *error_handler);
200 int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state endstate, error_handler_t *error_handler);
201 int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state endstate, error_handler_t *error_handler);
202 int jtag_add_statemove(enum tap_state endstate);
203 int jtag_add_pathmove(int num_states, enum tap_state *path);
204 int jtag_add_runtest(int num_cycles, enum tap_state endstate);
205 int jtag_add_reset(int trst, int srst);
206 int jtag_add_end_state(enum tap_state endstate);
207 int jtag_add_sleep(u32 us);
208 int jtag_execute_queue(void);
209 int jtag_cancel_queue(void);
210
211 /* jtag commands */
212 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
213 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
214 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
215 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
216 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
217 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
218
219 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
220
221 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
222 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
223 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
224 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
225 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
226 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
227
228 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
229
230 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
231 {
232 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
233
234 if (callback == NULL)
235 {
236 return ERROR_INVALID_ARGUMENTS;
237 }
238
239 if (*callbacks_p)
240 {
241 while ((*callbacks_p)->next)
242 callbacks_p = &((*callbacks_p)->next);
243 callbacks_p = &((*callbacks_p)->next);
244 }
245
246 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
247 (*callbacks_p)->callback = callback;
248 (*callbacks_p)->priv = priv;
249 (*callbacks_p)->next = NULL;
250
251 return ERROR_OK;
252 }
253
254 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
255 {
256 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
257
258 if (callback == NULL)
259 {
260 return ERROR_INVALID_ARGUMENTS;
261 }
262
263 while (*callbacks_p)
264 {
265 jtag_event_callback_t **next = &((*callbacks_p)->next);
266 if ((*callbacks_p)->callback == callback)
267 {
268 free(*callbacks_p);
269 *callbacks_p = *next;
270 }
271 callbacks_p = next;
272 }
273
274 return ERROR_OK;
275 }
276
277 int jtag_call_event_callbacks(enum jtag_event event)
278 {
279 jtag_event_callback_t *callback = jtag_event_callbacks;
280
281 DEBUG("jtag event: %s", jtag_event_strings[event]);
282
283 while (callback)
284 {
285 callback->callback(event, callback->priv);
286 callback = callback->next;
287 }
288
289 return ERROR_OK;
290 }
291
292 /* returns a pointer to the pointer of the last command in queue
293 * this may be a pointer to the root pointer (jtag_command_queue)
294 * or to the next member of the last but one command
295 */
296 jtag_command_t** jtag_get_last_command_p(void)
297 {
298 /* jtag_command_t *cmd = jtag_command_queue;
299
300 if (cmd)
301 while (cmd->next)
302 cmd = cmd->next;
303 else
304 return &jtag_command_queue;
305
306 return &cmd->next;*/
307
308 return last_comand_pointer;
309 }
310
311 /* returns a pointer to the n-th device in the scan chain */
312 jtag_device_t* jtag_get_device(int num)
313 {
314 jtag_device_t *device = jtag_devices;
315 int i = 0;
316
317 while (device)
318 {
319 if (num == i)
320 return device;
321 device = device->next;
322 i++;
323 }
324
325 return NULL;
326 }
327
328 void* cmd_queue_alloc(size_t size)
329 {
330 cmd_queue_page_t **p_page = &cmd_queue_pages;
331 int offset;
332
333 if (*p_page)
334 {
335 while ((*p_page)->next)
336 p_page = &((*p_page)->next);
337 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
338 p_page = &((*p_page)->next);
339 }
340
341 if (!*p_page)
342 {
343 *p_page = malloc(sizeof(cmd_queue_page_t));
344 (*p_page)->used = 0;
345 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
346 (*p_page)->next = NULL;
347 }
348
349 offset = (*p_page)->used;
350 (*p_page)->used += size;
351
352 return ((*p_page)->address) + offset;
353 }
354
355 void cmd_queue_free()
356 {
357 cmd_queue_page_t *page = cmd_queue_pages;
358
359 while (page)
360 {
361 cmd_queue_page_t *last = page;
362 free(page->address);
363 page = page->next;
364 free(last);
365 }
366
367 cmd_queue_pages = NULL;
368 }
369
370 int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state, error_handler_t *error_handler)
371 {
372 jtag_command_t **last_cmd;
373 jtag_device_t *device;
374 int i, j;
375 int scan_size = 0;
376
377 if (jtag_trst == 1)
378 {
379 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
380 return ERROR_JTAG_TRST_ASSERTED;
381 }
382
383 last_cmd = jtag_get_last_command_p();
384
385 /* allocate memory for a new list member */
386 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
387 (*last_cmd)->next = NULL;
388 last_comand_pointer = &((*last_cmd)->next);
389 (*last_cmd)->type = JTAG_SCAN;
390
391 /* allocate memory for ir scan command */
392 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
393 (*last_cmd)->cmd.scan->ir_scan = 1;
394 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
395 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
396 (*last_cmd)->cmd.scan->end_state = state;
397 if (error_handler)
398 {
399 (*last_cmd)->cmd.scan->error_handler = cmd_queue_alloc(sizeof(error_handler_t));
400 *(*last_cmd)->cmd.scan->error_handler = *error_handler;
401 }
402 else
403 {
404 (*last_cmd)->cmd.scan->error_handler = NULL;
405 }
406
407 if (state != -1)
408 cmd_queue_end_state = state;
409
410 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
411 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
412
413 if (cmd_queue_end_state == TAP_TLR)
414 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
415
416 cmd_queue_cur_state = cmd_queue_end_state;
417
418 for (i=0; i < jtag_num_devices; i++)
419 {
420 int found = 0;
421 device = jtag_get_device(i);
422 scan_size = device->ir_length;
423 (*last_cmd)->cmd.scan->fields[i].device = i;
424 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
425 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
426 if (jtag_verify_capture_ir)
427 {
428 (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(device->expected, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
429 (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(device->expected_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
430 }
431 else
432 {
433 (*last_cmd)->cmd.scan->fields[i].in_check_value = NULL;
434 (*last_cmd)->cmd.scan->fields[i].in_check_mask = NULL;
435 }
436 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
437 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
438
439 /* search the list */
440 for (j=0; j < num_fields; j++)
441 {
442 if (i == fields[j].device)
443 {
444 found = 1;
445 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
446 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
447
448 device->bypass = 0;
449 break;
450 }
451 }
452
453 if (!found)
454 {
455 /* if a device isn't listed, set it to BYPASS */
456 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
457 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
458 device->bypass = 1;
459
460 }
461
462 /* update device information */
463 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
464 }
465
466 return ERROR_OK;
467 }
468
469 int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state, error_handler_t *error_handler)
470 {
471 jtag_command_t **last_cmd;
472 int i;
473
474 if (jtag_trst == 1)
475 {
476 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
477 return ERROR_JTAG_TRST_ASSERTED;
478 }
479
480 last_cmd = jtag_get_last_command_p();
481
482 /* allocate memory for a new list member */
483 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
484 (*last_cmd)->next = NULL;
485 last_comand_pointer = &((*last_cmd)->next);
486 (*last_cmd)->type = JTAG_SCAN;
487
488 /* allocate memory for ir scan command */
489 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
490 (*last_cmd)->cmd.scan->ir_scan = 1;
491 (*last_cmd)->cmd.scan->num_fields = num_fields;
492 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
493 (*last_cmd)->cmd.scan->end_state = state;
494 if (error_handler)
495 {
496 (*last_cmd)->cmd.scan->error_handler = cmd_queue_alloc(sizeof(error_handler_t));
497 *(*last_cmd)->cmd.scan->error_handler = *error_handler;
498 }
499 else
500 {
501 (*last_cmd)->cmd.scan->error_handler = NULL;
502 }
503
504 if (state != -1)
505 cmd_queue_end_state = state;
506
507 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
508 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
509
510 if (cmd_queue_end_state == TAP_TLR)
511 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
512
513 cmd_queue_cur_state = cmd_queue_end_state;
514
515 for (i = 0; i < num_fields; i++)
516 {
517 int num_bits = fields[i].num_bits;
518 int num_bytes = CEIL(fields[i].num_bits, 8);
519 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
520 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
521 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
522 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
523 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
524 (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(fields[i].in_check_value, cmd_queue_alloc(num_bytes), num_bits);
525 (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(fields[i].in_check_mask, cmd_queue_alloc(num_bytes), num_bits);
526 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
527 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
528 }
529 return ERROR_OK;
530 }
531
532 int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state, error_handler_t *error_handler)
533 {
534 int i, j;
535 int bypass_devices = 0;
536 int field_count = 0;
537 jtag_command_t **last_cmd = jtag_get_last_command_p();
538 jtag_device_t *device = jtag_devices;
539 int scan_size;
540
541 if (jtag_trst == 1)
542 {
543 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
544 return ERROR_JTAG_TRST_ASSERTED;
545 }
546
547 /* count devices in bypass */
548 while (device)
549 {
550 if (device->bypass)
551 bypass_devices++;
552 device = device->next;
553 }
554
555 /* allocate memory for a new list member */
556 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
557 last_comand_pointer = &((*last_cmd)->next);
558 (*last_cmd)->next = NULL;
559 (*last_cmd)->type = JTAG_SCAN;
560
561 /* allocate memory for dr scan command */
562 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
563 (*last_cmd)->cmd.scan->ir_scan = 0;
564 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
565 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
566 (*last_cmd)->cmd.scan->end_state = state;
567 if (error_handler)
568 {
569 (*last_cmd)->cmd.scan->error_handler = cmd_queue_alloc(sizeof(error_handler_t));
570 *(*last_cmd)->cmd.scan->error_handler = *error_handler;
571 }
572 else
573 {
574 (*last_cmd)->cmd.scan->error_handler = NULL;
575 }
576
577 if (state != -1)
578 cmd_queue_end_state = state;
579
580 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
581 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
582
583 if (cmd_queue_end_state == TAP_TLR)
584 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
585
586 cmd_queue_cur_state = cmd_queue_end_state;
587
588 for (i=0; i < jtag_num_devices; i++)
589 {
590 int found = 0;
591 (*last_cmd)->cmd.scan->fields[field_count].device = i;
592
593 for (j=0; j < num_fields; j++)
594 {
595 if (i == fields[j].device)
596 {
597 found = 1;
598 scan_size = fields[j].num_bits;
599 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
600 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
601 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
602 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
603 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = buf_cpy(fields[j].in_check_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
604 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = buf_cpy(fields[j].in_check_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
605 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
606 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
607 }
608 }
609 if (!found)
610 {
611 /* if a device isn't listed, the BYPASS register should be selected */
612 if (!jtag_get_device(i)->bypass)
613 {
614 ERROR("BUG: no scan data for a device not in BYPASS");
615 exit(-1);
616 }
617
618 /* program the scan field to 1 bit length, and ignore it's value */
619 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
620 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
621 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
622 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
623 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
624 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
625 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
626 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
627 }
628 else
629 {
630 /* if a device is listed, the BYPASS register must not be selected */
631 if (jtag_get_device(i)->bypass)
632 {
633 WARNING("scan data for a device in BYPASS");
634 }
635 }
636 }
637 return ERROR_OK;
638 }
639
640 int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state, error_handler_t *error_handler)
641 {
642 int i;
643 jtag_command_t **last_cmd = jtag_get_last_command_p();
644
645 if (jtag_trst == 1)
646 {
647 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
648 return ERROR_JTAG_TRST_ASSERTED;
649 }
650
651 /* allocate memory for a new list member */
652 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
653 last_comand_pointer = &((*last_cmd)->next);
654 (*last_cmd)->next = NULL;
655 (*last_cmd)->type = JTAG_SCAN;
656
657 /* allocate memory for scan command */
658 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
659 (*last_cmd)->cmd.scan->ir_scan = 0;
660 (*last_cmd)->cmd.scan->num_fields = num_fields;
661 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
662 (*last_cmd)->cmd.scan->end_state = state;
663 if (error_handler)
664 {
665 (*last_cmd)->cmd.scan->error_handler = cmd_queue_alloc(sizeof(error_handler_t));
666 *(*last_cmd)->cmd.scan->error_handler = *error_handler;
667 }
668 else
669 {
670 (*last_cmd)->cmd.scan->error_handler = NULL;
671 }
672
673 if (state != -1)
674 cmd_queue_end_state = state;
675
676 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
677 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
678
679 if (cmd_queue_end_state == TAP_TLR)
680 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
681
682 cmd_queue_cur_state = cmd_queue_end_state;
683
684 for (i = 0; i < num_fields; i++)
685 {
686 int num_bits = fields[i].num_bits;
687 int num_bytes = CEIL(fields[i].num_bits, 8);
688 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
689 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
690 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
691 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
692 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
693 (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(fields[i].in_check_value, cmd_queue_alloc(num_bytes), num_bits);
694 (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(fields[i].in_check_mask, cmd_queue_alloc(num_bytes), num_bits);
695 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
696 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
697 }
698
699 return ERROR_OK;
700 }
701 int jtag_add_statemove(enum tap_state state)
702 {
703 jtag_command_t **last_cmd = jtag_get_last_command_p();
704
705 if (jtag_trst == 1)
706 {
707 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
708 return ERROR_JTAG_TRST_ASSERTED;
709 }
710
711 /* allocate memory for a new list member */
712 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
713 last_comand_pointer = &((*last_cmd)->next);
714 (*last_cmd)->next = NULL;
715 (*last_cmd)->type = JTAG_STATEMOVE;
716
717 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
718 (*last_cmd)->cmd.statemove->end_state = state;
719
720 if (state != -1)
721 cmd_queue_end_state = state;
722
723 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
724 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
725
726 if (cmd_queue_end_state == TAP_TLR)
727 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
728
729 cmd_queue_cur_state = cmd_queue_end_state;
730
731 return ERROR_OK;
732 }
733
734 int jtag_add_pathmove(int num_states, enum tap_state *path)
735 {
736 jtag_command_t **last_cmd = jtag_get_last_command_p();
737 int i;
738
739 if (jtag_trst == 1)
740 {
741 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
742 return ERROR_JTAG_TRST_ASSERTED;
743 }
744
745 /* the last state has to be a stable state */
746 if (tap_move_map[path[num_states - 1]] == -1)
747 {
748 ERROR("TAP path doesn't finish in a stable state");
749 return ERROR_JTAG_NOT_IMPLEMENTED;
750 }
751
752 if (jtag->support_pathmove)
753 {
754 /* allocate memory for a new list member */
755 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
756 last_comand_pointer = &((*last_cmd)->next);
757 (*last_cmd)->next = NULL;
758 (*last_cmd)->type = JTAG_PATHMOVE;
759
760 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
761 (*last_cmd)->cmd.pathmove->num_states = num_states;
762 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
763
764 for (i = 0; i < num_states; i++)
765 (*last_cmd)->cmd.pathmove->path[i] = path[i];
766 }
767 else
768 {
769 /* validate the desired path, and see if it fits a default path */
770 int begin = 0;
771 int end = 0;
772 int j;
773
774 for (i = 0; i < num_states; i++)
775 {
776 for (j = i; j < num_states; j++)
777 {
778 if (tap_move_map[path[j]] != -1)
779 {
780 end = j;
781 break;
782 }
783 }
784
785 if (begin - end <= 7) /* a default path spans no more than 7 states */
786 {
787 jtag_add_statemove(path[end]);
788 }
789 else
790 {
791 ERROR("encountered a TAP path that can't be fulfilled by default paths");
792 return ERROR_JTAG_NOT_IMPLEMENTED;
793 }
794
795 i = end;
796 }
797 }
798
799 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
800 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
801
802 if (cmd_queue_end_state == TAP_TLR)
803 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
804
805 cmd_queue_cur_state = path[num_states - 1];
806
807 return ERROR_OK;
808 }
809
810 int jtag_add_runtest(int num_cycles, enum tap_state state)
811 {
812 jtag_command_t **last_cmd = jtag_get_last_command_p();
813
814 if (jtag_trst == 1)
815 {
816 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
817 return ERROR_JTAG_TRST_ASSERTED;
818 }
819
820 /* allocate memory for a new list member */
821 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
822 (*last_cmd)->next = NULL;
823 last_comand_pointer = &((*last_cmd)->next);
824 (*last_cmd)->type = JTAG_RUNTEST;
825
826 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
827 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
828 (*last_cmd)->cmd.runtest->end_state = state;
829
830 if (state != -1)
831 cmd_queue_end_state = state;
832
833 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
834 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
835
836 if (cmd_queue_end_state == TAP_TLR)
837 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
838
839 cmd_queue_cur_state = cmd_queue_end_state;
840
841 return ERROR_OK;
842 }
843
844 int jtag_add_reset(int req_trst, int req_srst)
845 {
846 int trst_with_tms = 0;
847
848 jtag_command_t **last_cmd = jtag_get_last_command_p();
849
850 if (req_trst == -1)
851 req_trst = jtag_trst;
852
853 if (req_srst == -1)
854 req_srst = jtag_srst;
855
856 /* Make sure that jtag_reset_config allows the requested reset */
857 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
858 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
859 return ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
860
861 /* if TRST pulls SRST, we reset with TAP T-L-R */
862 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
863 {
864 req_trst = 0;
865 trst_with_tms = 1;
866 }
867
868 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
869 {
870 ERROR("requested nSRST assertion, but the current configuration doesn't support this");
871 return ERROR_JTAG_RESET_CANT_SRST;
872 }
873
874 if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
875 {
876 req_trst = 0;
877 trst_with_tms = 1;
878 }
879
880 /* allocate memory for a new list member */
881 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
882 (*last_cmd)->next = NULL;
883 last_comand_pointer = &((*last_cmd)->next);
884 (*last_cmd)->type = JTAG_RESET;
885
886 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
887 (*last_cmd)->cmd.reset->trst = req_trst;
888 (*last_cmd)->cmd.reset->srst = req_srst;
889
890 jtag_trst = req_trst;
891 jtag_srst = req_srst;
892
893 if (jtag_srst)
894 {
895 jtag_call_event_callbacks(JTAG_SRST_ASSERTED);
896 }
897 else
898 {
899 jtag_call_event_callbacks(JTAG_SRST_RELEASED);
900 if (jtag_nsrst_delay)
901 jtag_add_sleep(jtag_nsrst_delay * 1000);
902 }
903
904 if (trst_with_tms)
905 {
906 last_cmd = &((*last_cmd)->next);
907
908 /* allocate memory for a new list member */
909 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
910 (*last_cmd)->next = NULL;
911 last_comand_pointer = &((*last_cmd)->next);
912 (*last_cmd)->type = JTAG_STATEMOVE;
913
914 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
915 (*last_cmd)->cmd.statemove->end_state = TAP_TLR;
916
917 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
918 cmd_queue_cur_state = TAP_TLR;
919 cmd_queue_end_state = TAP_TLR;
920
921 return ERROR_OK;
922 }
923 else
924 {
925 if (jtag_trst)
926 {
927 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
928 * and inform possible listeners about this
929 */
930 cmd_queue_cur_state = TAP_TLR;
931 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
932 }
933 else
934 {
935 /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
936 * but we might want to add a delay to give the TAP time to settle
937 */
938 if (jtag_ntrst_delay)
939 jtag_add_sleep(jtag_ntrst_delay * 1000);
940 }
941 }
942
943 return ERROR_OK;
944 }
945
946 int jtag_add_end_state(enum tap_state state)
947 {
948 jtag_command_t **last_cmd = jtag_get_last_command_p();
949
950 /* allocate memory for a new list member */
951 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
952 (*last_cmd)->next = NULL;
953 last_comand_pointer = &((*last_cmd)->next);
954 (*last_cmd)->type = JTAG_END_STATE;
955
956 (*last_cmd)->cmd.end_state = cmd_queue_alloc(sizeof(end_state_command_t));
957 (*last_cmd)->cmd.end_state->end_state = state;
958
959 if (state != -1)
960 cmd_queue_end_state = state;
961
962 return ERROR_OK;
963 }
964
965 int jtag_add_sleep(u32 us)
966 {
967 jtag_command_t **last_cmd = jtag_get_last_command_p();
968
969 /* allocate memory for a new list member */
970 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
971 (*last_cmd)->next = NULL;
972 last_comand_pointer = &((*last_cmd)->next);
973 (*last_cmd)->type = JTAG_SLEEP;
974
975 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
976 (*last_cmd)->cmd.sleep->us = us;
977
978 return ERROR_OK;
979 }
980
981 int jtag_scan_size(scan_command_t *cmd)
982 {
983 int bit_count = 0;
984 int i;
985
986 /* count bits in scan command */
987 for (i=0; i<cmd->num_fields; i++)
988 {
989 bit_count += cmd->fields[i].num_bits;
990 }
991
992 return bit_count;
993 }
994
995 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
996 {
997 int bit_count = 0;
998 int i;
999
1000 bit_count = jtag_scan_size(cmd);
1001 *buffer = malloc(CEIL(bit_count, 8));
1002
1003 bit_count = 0;
1004
1005 for (i = 0; i < cmd->num_fields; i++)
1006 {
1007 if (cmd->fields[i].out_value)
1008 {
1009 #ifdef _DEBUG_JTAG_IO_
1010 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1011 #endif
1012 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1013 #ifdef _DEBUG_JTAG_IO_
1014 DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1015 free(char_buf);
1016 #endif
1017 }
1018
1019 bit_count += cmd->fields[i].num_bits;
1020 }
1021
1022 return bit_count;
1023
1024 }
1025
1026 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1027 {
1028 int i;
1029 int bit_count = 0;
1030 int retval;
1031
1032 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1033 retval = ERROR_OK;
1034
1035 for (i=0; i < cmd->num_fields; i++)
1036 {
1037 /* if neither in_value, in_check_value nor in_handler
1038 * are specified we don't have to examine this field
1039 */
1040 if (cmd->fields[i].in_value || cmd->fields[i].in_check_value || cmd->fields[i].in_handler)
1041 {
1042 int num_bits = cmd->fields[i].num_bits;
1043 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1044
1045 #ifdef _DEBUG_JTAG_IO_
1046 char *char_buf;
1047
1048 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1049 DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1050 free(char_buf);
1051 #endif
1052
1053 if (cmd->fields[i].in_value)
1054 {
1055 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1056
1057 if (cmd->fields[i].in_handler)
1058 {
1059 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv) != ERROR_OK)
1060 {
1061 WARNING("in_handler reported a failed check");
1062 retval = ERROR_JTAG_QUEUE_FAILED;
1063 }
1064 }
1065 }
1066
1067 /* no in_value specified, but a handler takes care of the scanned data */
1068 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1069 {
1070 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv) != ERROR_OK)
1071 {
1072 /* We're going to call the error:handler later, but if the in_handler
1073 * reported an error we report this failure upstream
1074 */
1075 WARNING("in_handler reported a failed check");
1076 retval = ERROR_JTAG_QUEUE_FAILED;
1077 }
1078 }
1079
1080 if (cmd->fields[i].in_check_value)
1081 {
1082 int compare_failed = 0;
1083
1084 if (cmd->fields[i].in_check_mask)
1085 compare_failed = buf_cmp_mask(captured, cmd->fields[i].in_check_value, cmd->fields[i].in_check_mask, num_bits);
1086 else
1087 compare_failed = buf_cmp(captured, cmd->fields[i].in_check_value, num_bits);
1088
1089 if (compare_failed)
1090 {
1091 if (cmd->error_handler)
1092 {
1093 /* ask the error handler if once has been specified if this is a real problem */
1094 if (cmd->error_handler->error_handler(captured, cmd->error_handler->error_handler_priv) != ERROR_OK)
1095 retval = ERROR_JTAG_QUEUE_FAILED;
1096 else
1097 compare_failed = 0;
1098 }
1099 else
1100 {
1101 /* if there wasn't a handler specified, we report a failure */
1102 retval = ERROR_JTAG_QUEUE_FAILED;
1103 }
1104
1105 /* An error handler could have caught the failing check
1106 * only report a problem when there wasn't a handler, or if the handler
1107 * acknowledged the error
1108 */
1109 if (compare_failed)
1110 {
1111 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1112 char *in_check_value_char = buf_to_str(cmd->fields[i].in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1113
1114 if (cmd->fields[i].in_check_mask)
1115 {
1116 char *in_check_mask_char;
1117 in_check_mask_char = buf_to_str(cmd->fields[i].in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1118 WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s check_mask: 0x%s", captured_char, in_check_value_char, in_check_mask_char);
1119 free(in_check_mask_char);
1120 }
1121 else
1122 {
1123 WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
1124 }
1125
1126 free(captured_char);
1127 free(in_check_value_char);
1128 }
1129
1130 }
1131 }
1132 free(captured);
1133 }
1134 bit_count += cmd->fields[i].num_bits;
1135 }
1136
1137 return retval;
1138 }
1139
1140 enum scan_type jtag_scan_type(scan_command_t *cmd)
1141 {
1142 int i;
1143 int type = 0;
1144
1145 for (i=0; i < cmd->num_fields; i++)
1146 {
1147 if (cmd->fields[i].in_check_value || cmd->fields[i].in_value || cmd->fields[i].in_handler)
1148 type |= SCAN_IN;
1149 if (cmd->fields[i].out_value)
1150 type |= SCAN_OUT;
1151 }
1152
1153 return type;
1154 }
1155
1156 int jtag_execute_queue(void)
1157 {
1158 int retval;
1159
1160 retval = jtag->execute_queue();
1161
1162 cmd_queue_free();
1163
1164 jtag_command_queue = NULL;
1165 last_comand_pointer = &jtag_command_queue;
1166
1167 return retval;
1168 }
1169
1170 int jtag_cancel_queue(void)
1171 {
1172 cmd_queue_free();
1173 jtag_command_queue = NULL;
1174 last_comand_pointer = &jtag_command_queue;
1175
1176 return ERROR_OK;
1177 }
1178
1179 int jtag_reset_callback(enum jtag_event event, void *priv)
1180 {
1181 jtag_device_t *device = priv;
1182
1183 DEBUG("-");
1184
1185 if (event == JTAG_TRST_ASSERTED)
1186 {
1187 buf_set_ones(device->cur_instr, device->ir_length);
1188 device->bypass = 1;
1189 }
1190
1191 return ERROR_OK;
1192 }
1193
1194 void jtag_sleep(u32 us)
1195 {
1196 usleep(us);
1197 }
1198
1199 /* Try to examine chain layout according to IEEE 1149.1 §12
1200 */
1201 int jtag_examine_chain()
1202 {
1203 jtag_device_t *device = jtag_devices;
1204 scan_field_t field;
1205 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1206 int i;
1207 int bit_count;
1208 int device_count = 0;
1209 u8 zero_check = 0x0;
1210 u8 one_check = 0xff;
1211
1212 field.device = 0;
1213 field.num_bits = sizeof(idcode_buffer) * 8;
1214 field.out_value = idcode_buffer;
1215 field.out_mask = NULL;
1216 field.in_value = idcode_buffer;
1217 field.in_check_value = NULL;
1218 field.in_check_mask = NULL;
1219 field.in_handler = NULL;
1220 field.in_handler_priv = NULL;
1221
1222 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1223 {
1224 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1225 }
1226
1227 jtag_add_plain_dr_scan(1, &field, TAP_TLR, NULL);
1228 jtag_execute_queue();
1229
1230 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1231 {
1232 zero_check |= idcode_buffer[i];
1233 one_check &= idcode_buffer[i];
1234 }
1235
1236 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1237 if ((zero_check == 0x00) || (one_check == 0xff))
1238 {
1239 ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1240 exit(-1);
1241 }
1242
1243 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1244 {
1245 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1246 if ((idcode & 1) == 0)
1247 {
1248 /* LSB must not be 0, this indicates a device in bypass */
1249 device_count++;
1250
1251 bit_count += 1;
1252 }
1253 else
1254 {
1255 u32 manufacturer;
1256 u32 part;
1257 u32 version;
1258
1259 if (idcode == 0x000000FF)
1260 {
1261 /* End of chain (invalid manufacturer ID) */
1262 break;
1263 }
1264
1265 if (device)
1266 {
1267 device->idcode = idcode;
1268 device = device->next;
1269 }
1270 device_count++;
1271
1272 manufacturer = (idcode & 0xffe) >> 1;
1273 part = (idcode & 0xffff000) >> 12;
1274 version = (idcode & 0xf0000000) >> 28;
1275
1276 DEBUG("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x",
1277 idcode, manufacturer, part, version);
1278
1279 bit_count += 32;
1280 }
1281 }
1282
1283 /* see if number of discovered devices matches configuration */
1284 if (device_count != jtag_num_devices)
1285 {
1286 ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1287 device_count, jtag_num_devices);
1288 ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1289 exit(-1);
1290 }
1291
1292 return ERROR_OK;
1293 }
1294
1295 int jtag_validate_chain()
1296 {
1297 jtag_device_t *device = jtag_devices;
1298 int total_ir_length = 0;
1299 u8 *ir_test = NULL;
1300 scan_field_t field;
1301 int chain_pos = 0;
1302
1303 while (device)
1304 {
1305 total_ir_length += device->ir_length;
1306 device = device->next;
1307 }
1308
1309 total_ir_length += 2;
1310 ir_test = malloc(CEIL(total_ir_length, 8));
1311 buf_set_ones(ir_test, total_ir_length);
1312
1313 field.device = 0;
1314 field.num_bits = total_ir_length;
1315 field.out_value = ir_test;
1316 field.out_mask = NULL;
1317 field.in_value = ir_test;
1318 field.in_check_value = NULL;
1319 field.in_check_mask = NULL;
1320 field.in_handler = NULL;
1321 field.in_handler_priv = NULL;
1322
1323 jtag_add_plain_ir_scan(1, &field, TAP_TLR, NULL);
1324 jtag_execute_queue();
1325
1326 device = jtag_devices;
1327 while (device)
1328 {
1329 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1330 {
1331 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1332 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1333 free(cbuf);
1334 exit(-1);
1335 }
1336 chain_pos += device->ir_length;
1337 device = device->next;
1338 }
1339
1340 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1341 {
1342 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1343 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1344 free(cbuf);
1345 exit(-1);
1346 }
1347
1348 free(ir_test);
1349
1350 return ERROR_OK;
1351 }
1352
1353 int jtag_register_commands(struct command_context_s *cmd_ctx)
1354 {
1355 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1356 COMMAND_CONFIG, NULL);
1357 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1358 COMMAND_ANY, "set jtag speed (if supported) <speed>");
1359 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1360 COMMAND_CONFIG, NULL);
1361 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1362 COMMAND_CONFIG, NULL);
1363 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1364 COMMAND_CONFIG, NULL);
1365 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1366 COMMAND_CONFIG, NULL);
1367
1368 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1369 COMMAND_EXEC, "print current scan chain configuration");
1370
1371 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1372 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1373 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1374 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1375 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1376 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1377 register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
1378 COMMAND_EXEC, "move to current endstate or [tap_state]");
1379 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1380 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1381 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1382 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1383
1384 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1385 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1386 return ERROR_OK;
1387 }
1388
1389 int jtag_init(struct command_context_s *cmd_ctx)
1390 {
1391 int i;
1392
1393 DEBUG("-");
1394
1395 if (jtag_speed == -1)
1396 jtag_speed = 0;
1397
1398 if (jtag_interface && (jtag_interface[0] != 0))
1399 /* configuration var 'jtag_interface' is set, and not empty */
1400 for (i = 0; jtag_interfaces[i]; i++)
1401 {
1402 if (strcmp(jtag_interface, jtag_interfaces[i]->name) == 0)
1403 {
1404 jtag_device_t *device;
1405 device = jtag_devices;
1406
1407 if (jtag_interfaces[i]->init() != ERROR_OK)
1408 return ERROR_JTAG_INIT_FAILED;
1409 jtag = jtag_interfaces[i];
1410
1411 jtag_ir_scan_size = 0;
1412 jtag_num_devices = 0;
1413 while (device != NULL)
1414 {
1415 jtag_ir_scan_size += device->ir_length;
1416 jtag_num_devices++;
1417 device = device->next;
1418 }
1419
1420 jtag_add_statemove(TAP_TLR);
1421 jtag_execute_queue();
1422
1423 jtag_examine_chain();
1424
1425 jtag_validate_chain();
1426
1427 return ERROR_OK;
1428 }
1429 }
1430
1431 /* no valid interface was found (i.e. the configuration option,
1432 * didn't match one of the compiled-in interfaces
1433 */
1434 ERROR("No valid jtag interface found (%s)", jtag_interface);
1435 ERROR("compiled-in jtag interfaces:");
1436 for (i = 0; jtag_interfaces[i]; i++)
1437 {
1438 ERROR("%i: %s", i, jtag_interfaces[i]->name);
1439 }
1440
1441 jtag = NULL;
1442 return ERROR_JTAG_INVALID_INTERFACE;
1443 }
1444
1445 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1446 {
1447 int i;
1448
1449 /* only if the configuration var isn't overwritten from cmdline */
1450 if (!jtag_interface)
1451 {
1452 if (args[0] && (args[0][0] != 0))
1453 {
1454 for (i=0; jtag_interfaces[i]; i++)
1455 {
1456 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1457 {
1458 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1459 exit(-1);
1460
1461 jtag_interface = jtag_interfaces[i]->name;
1462
1463 return ERROR_OK;
1464 }
1465 }
1466 }
1467
1468 /* remember the requested interface name, so we can complain about it later */
1469 jtag_interface = strdup(args[0]);
1470 DEBUG("'interface' command didn't specify a valid interface");
1471 }
1472
1473 return ERROR_OK;
1474 }
1475
1476 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1477 {
1478 jtag_device_t **last_device_p = &jtag_devices;
1479
1480 if (*last_device_p)
1481 {
1482 while ((*last_device_p)->next)
1483 last_device_p = &((*last_device_p)->next);
1484 last_device_p = &((*last_device_p)->next);
1485 }
1486
1487 if (argc < 3)
1488 return ERROR_OK;
1489
1490 *last_device_p = malloc(sizeof(jtag_device_t));
1491 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1492
1493 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1494 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1495 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1496 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1497
1498 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1499 (*last_device_p)->bypass = 1;
1500 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1501
1502 (*last_device_p)->next = NULL;
1503
1504 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1505
1506 jtag_num_devices++;
1507
1508 return ERROR_OK;
1509 }
1510
1511 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1512 {
1513 jtag_device_t *device = jtag_devices;
1514 int device_count = 0;
1515
1516 while (device)
1517 {
1518 u32 expected, expected_mask, cur_instr;
1519 expected = buf_get_u32(device->expected, 0, device->ir_length);
1520 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1521 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1522 command_print(cmd_ctx, "%i: idcode: 0x%8.8x ir length %i, ir capture 0x%x, ir mask 0x%x, current instruction 0x%x", device_count, device->idcode, device->ir_length, expected, expected_mask, cur_instr);
1523 device = device->next;
1524 device_count++;
1525 }
1526
1527 return ERROR_OK;
1528 }
1529
1530 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1531 {
1532 if (argc >= 1)
1533 {
1534 if (strcmp(args[0], "none") == 0)
1535 jtag_reset_config = RESET_NONE;
1536 else if (strcmp(args[0], "trst_only") == 0)
1537 jtag_reset_config = RESET_HAS_TRST;
1538 else if (strcmp(args[0], "srst_only") == 0)
1539 jtag_reset_config = RESET_HAS_SRST;
1540 else if (strcmp(args[0], "trst_and_srst") == 0)
1541 jtag_reset_config = RESET_TRST_AND_SRST;
1542 else
1543 {
1544 ERROR("invalid reset_config argument");
1545 exit(-1);
1546 }
1547 }
1548
1549 if (argc >= 2)
1550 {
1551 if (strcmp(args[1], "srst_pulls_trst") == 0)
1552 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1553 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1554 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1555 else if (strcmp(args[1], "combined") == 0)
1556 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1557 else if (strcmp(args[1], "separate") == 0)
1558 jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
1559 else
1560 {
1561 ERROR("invalid reset_config argument");
1562 exit(-1);
1563 }
1564 }
1565
1566 if (argc >= 3)
1567 {
1568 if (strcmp(args[2], "trst_open_drain") == 0)
1569 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1570 else if (strcmp(args[2], "trst_push_pull") == 0)
1571 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1572 else
1573 {
1574 ERROR("invalid reset_config argument");
1575 exit(-1);
1576 }
1577 }
1578
1579 if (argc >= 4)
1580 {
1581 if (strcmp(args[3], "srst_push_pull") == 0)
1582 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1583 else if (strcmp(args[3], "srst_open_drain") == 0)
1584 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1585 else
1586 {
1587 ERROR("invalid reset_config argument");
1588 exit(-1);
1589 }
1590 }
1591
1592 return ERROR_OK;
1593 }
1594
1595 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1596 {
1597 if (argc < 1)
1598 {
1599 ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1600 exit(-1);
1601 }
1602 else
1603 {
1604 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1605 }
1606
1607 return ERROR_OK;
1608 }
1609
1610 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1611 {
1612 if (argc < 1)
1613 {
1614 ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1615 exit(-1);
1616 }
1617 else
1618 {
1619 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1620 }
1621
1622 return ERROR_OK;
1623 }
1624
1625 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1626 {
1627 if (argc == 0)
1628 command_print(cmd_ctx, "jtag_speed: %i", jtag_speed);
1629
1630 if (argc > 0)
1631 {
1632 /* this command can be called during CONFIG,
1633 * in which case jtag isn't initialized */
1634 if (jtag)
1635 jtag->speed(strtoul(args[0], NULL, 0));
1636 else
1637 jtag_speed = strtoul(args[0], NULL, 0);
1638 }
1639
1640 return ERROR_OK;
1641 }
1642
1643 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1644 {
1645 enum tap_state state;
1646
1647 if (argc < 1)
1648 {
1649 command_print(cmd_ctx, "usage: endstate <tap_state>");
1650 }
1651 else
1652 {
1653 for (state = 0; state < 16; state++)
1654 {
1655 if (strcmp(args[0], tap_state_strings[state]) == 0)
1656 {
1657 jtag_add_end_state(state);
1658 jtag_execute_queue();
1659 }
1660 }
1661 }
1662 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[end_state]);
1663
1664 return ERROR_OK;
1665 }
1666
1667 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1668 {
1669 int trst = -1;
1670 int srst = -1;
1671 char *usage = "usage: jtag_reset <trst> <srst>";
1672 int retval;
1673
1674 if (argc < 1)
1675 {
1676 command_print(cmd_ctx, usage);
1677 return ERROR_OK;
1678 }
1679
1680 if (args[0][0] == '1')
1681 trst = 1;
1682 else if (args[0][0] == '0')
1683 trst = 0;
1684 else
1685 {
1686 command_print(cmd_ctx, usage);
1687 return ERROR_OK;
1688 }
1689
1690 if (args[1][0] == '1')
1691 srst = 1;
1692 else if (args[1][0] == '0')
1693 srst = 0;
1694 else
1695 {
1696 command_print(cmd_ctx, usage);
1697 return ERROR_OK;
1698 }
1699
1700 if ((retval = jtag_add_reset(trst, srst)) != ERROR_OK)
1701 {
1702 switch (retval)
1703 {
1704 case ERROR_JTAG_RESET_WOULD_ASSERT_TRST:
1705 command_print(cmd_ctx, "requested reset would assert trst\nif this is acceptable, use jtag_reset 1 %c", args[1][0]);
1706 break;
1707 case ERROR_JTAG_RESET_CANT_SRST:
1708 command_print(cmd_ctx, "can't assert srst because the current reset_config doesn't support it");
1709 break;
1710 default:
1711 command_print(cmd_ctx, "unknown error");
1712 }
1713 }
1714 jtag_execute_queue();
1715
1716 return ERROR_OK;
1717 }
1718
1719 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1720 {
1721 if (argc < 1)
1722 {
1723 command_print(cmd_ctx, "usage: runtest <num_cycles>");
1724 return ERROR_OK;
1725 }
1726
1727 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1728 jtag_execute_queue();
1729
1730 return ERROR_OK;
1731
1732 }
1733
1734 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1735 {
1736 enum tap_state state;
1737
1738 state = -1;
1739 if (argc == 1)
1740 {
1741 for (state = 0; state < 16; state++)
1742 {
1743 if (strcmp(args[0], tap_state_strings[state]) == 0)
1744 {
1745 break;
1746 }
1747 }
1748 }
1749
1750 jtag_add_statemove(state);
1751 jtag_execute_queue();
1752
1753 return ERROR_OK;
1754
1755 }
1756
1757 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1758 {
1759 int i;
1760 scan_field_t *fields;
1761
1762 if ((argc < 2) || (argc % 2))
1763 {
1764 command_print(cmd_ctx, "usage: irscan <device> <instr> [dev2] [instr2] ...");
1765 return ERROR_OK;
1766 }
1767
1768 fields = malloc(sizeof(scan_field_t) * argc / 2);
1769
1770 for (i = 0; i < argc / 2; i++)
1771 {
1772 int device = strtoul(args[i*2], NULL, 0);
1773 int field_size = jtag_get_device(device)->ir_length;
1774 fields[i].device = device;
1775 fields[i].out_value = malloc(CEIL(field_size, 8));
1776 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1777 fields[i].out_mask = NULL;
1778 fields[i].in_value = NULL;
1779 fields[i].in_check_mask = NULL;
1780 fields[i].in_handler = NULL;
1781 fields[i].in_handler_priv = NULL;
1782 }
1783
1784 jtag_add_ir_scan(argc / 2, fields, -1, NULL);
1785 jtag_execute_queue();
1786
1787 for (i = 0; i < argc / 2; i++)
1788 free(fields[i].out_value);
1789
1790 free (fields);
1791
1792 return ERROR_OK;
1793 }
1794
1795 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1796 {
1797 scan_field_t *fields;
1798 int num_fields = 0;
1799 int field_count = 0;
1800 var_t *var;
1801 int i, j;
1802
1803 if ((argc < 2) || (argc % 2))
1804 {
1805 command_print(cmd_ctx, "usage: drscan <device> <var> [dev2] [var2]");
1806 return ERROR_OK;
1807 }
1808
1809 for (i = 0; i < argc; i+=2)
1810 {
1811 var = get_var_by_namenum(args[i+1]);
1812 if (var)
1813 {
1814 num_fields += var->num_fields;
1815 }
1816 else
1817 {
1818 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1819 return ERROR_OK;
1820 }
1821 }
1822
1823 fields = malloc(sizeof(scan_field_t) * num_fields);
1824
1825 for (i = 0; i < argc; i+=2)
1826 {
1827 var = get_var_by_namenum(args[i+1]);
1828
1829 for (j = 0; j < var->num_fields; j++)
1830 {
1831 fields[field_count].device = strtol(args[i], NULL, 0);
1832 fields[field_count].num_bits = var->fields[j].num_bits;
1833 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1834 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1835 fields[field_count].out_mask = NULL;
1836 fields[field_count].in_value = fields[field_count].out_value;
1837 fields[field_count].in_check_mask = NULL;
1838 fields[field_count].in_check_value = NULL;
1839 fields[field_count].in_handler = field_le_to_host;
1840 fields[field_count++].in_handler_priv = &(var->fields[j]);
1841 }
1842 }
1843
1844 jtag_add_dr_scan(num_fields, fields, -1, NULL);
1845 jtag_execute_queue();
1846
1847 for (i = 0; i < argc / 2; i++)
1848 free(fields[i].out_value);
1849
1850 free(fields);
1851
1852 return ERROR_OK;
1853 }
1854
1855 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1856 {
1857 if (argc == 0)
1858 {
1859 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
1860 return ERROR_OK;
1861 }
1862
1863 if (strcmp(args[0], "enable") == 0)
1864 {
1865 jtag_verify_capture_ir = 1;
1866 }
1867 else if (strcmp(args[0], "disable") == 0)
1868 {
1869 jtag_verify_capture_ir = 0;
1870 }
1871
1872 return ERROR_OK;
1873 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)