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 ***************************************************************************/
24 #include "replacements.h"
30 #include "interpreter.h"
37 /* this allows JTAG devices to implement the entire jtag_xxx() layer in hw/sw */
38 #define INTERFACE(a) a
42 /* note that this is not marked as static as it must be available from outside jtag.c for those
43 that implement the jtag_xxx() minidriver layer
45 int jtag_error=ERROR_OK;
48 char* tap_state_strings[16] =
51 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
53 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
56 typedef struct cmd_queue_page_s
60 struct cmd_queue_page_s *next;
63 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
64 static cmd_queue_page_t *cmd_queue_pages = NULL;
66 /* tap_move[i][j]: tap movement command to go from state i to state j
74 * SD->SD and SI->SI have to be caught in interface specific code
78 /* TLR RTI SD PD SI PI */
79 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
80 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
81 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
82 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
83 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
84 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
87 int tap_move_map[16] = {
88 0, -1, -1, 2, -1, 3, -1, -1,
89 1, -1, -1, 4, -1, 5, -1, -1
92 tap_transition_t tap_transitions[16] =
94 {TAP_TLR, TAP_RTI}, /* TLR */
95 {TAP_SIS, TAP_CD}, /* SDS */
96 {TAP_E1D, TAP_SD}, /* CD */
97 {TAP_E1D, TAP_SD}, /* SD */
98 {TAP_UD, TAP_PD}, /* E1D */
99 {TAP_E2D, TAP_PD}, /* PD */
100 {TAP_UD, TAP_SD}, /* E2D */
101 {TAP_SDS, TAP_RTI}, /* UD */
102 {TAP_SDS, TAP_RTI}, /* RTI */
103 {TAP_TLR, TAP_CI}, /* SIS */
104 {TAP_E1I, TAP_SI}, /* CI */
105 {TAP_E1I, TAP_SI}, /* SI */
106 {TAP_UI, TAP_PI}, /* E1I */
107 {TAP_E2I, TAP_PI}, /* PI */
108 {TAP_UI, TAP_SI}, /* E2I */
109 {TAP_SDS, TAP_RTI} /* UI */
112 char* jtag_event_strings[] =
120 enum tap_state end_state = TAP_TLR;
121 enum tap_state cur_state = TAP_TLR;
125 jtag_command_t *jtag_command_queue = NULL;
126 jtag_command_t **last_comand_pointer = &jtag_command_queue;
127 jtag_device_t *jtag_devices = NULL;
128 int jtag_num_devices = 0;
129 int jtag_ir_scan_size = 0;
130 enum reset_types jtag_reset_config = RESET_NONE;
131 enum tap_state cmd_queue_end_state = TAP_TLR;
132 enum tap_state cmd_queue_cur_state = TAP_TLR;
134 int jtag_verify_capture_ir = 1;
136 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
137 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
138 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
140 /* maximum number of JTAG devices expected in the chain
142 #define JTAG_MAX_CHAIN_SIZE 20
144 /* callbacks to inform high-level handlers about JTAG state changes */
145 jtag_event_callback_t *jtag_event_callbacks;
147 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
149 #if BUILD_PARPORT == 1
150 extern jtag_interface_t parport_interface;
153 #if BUILD_FT2232_FTD2XX == 1
154 extern jtag_interface_t ft2232_interface;
157 #if BUILD_FT2232_LIBFTDI == 1
158 extern jtag_interface_t ft2232_interface;
161 #if BUILD_AMTJTAGACCEL == 1
162 extern jtag_interface_t amt_jtagaccel_interface;
165 #if BUILD_EP93XX == 1
166 extern jtag_interface_t ep93xx_interface;
169 #if BUILD_AT91RM9200 == 1
170 extern jtag_interface_t at91rm9200_interface;
173 #if BUILD_GW16012 == 1
174 extern jtag_interface_t gw16012_interface;
177 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
178 extern jtag_interface_t presto_interface;
181 #if BUILD_USBPROG == 1
182 extern jtag_interface_t usbprog_interface;
185 jtag_interface_t *jtag_interfaces[] = {
186 #if BUILD_PARPORT == 1
189 #if BUILD_FT2232_FTD2XX == 1
192 #if BUILD_FT2232_LIBFTDI == 1
195 #if BUILD_AMTJTAGACCEL == 1
196 &amt_jtagaccel_interface,
198 #if BUILD_EP93XX == 1
201 #if BUILD_AT91RM9200 == 1
202 &at91rm9200_interface,
204 #if BUILD_GW16012 == 1
207 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
210 #if BUILD_USBPROG == 1
216 jtag_interface_t *jtag = NULL;
219 jtag_interface_t *jtag_interface = NULL;
223 /* forward declarations */
224 int jtag_add_statemove(enum tap_state endstate);
225 int jtag_add_pathmove(int num_states, enum tap_state *path);
226 int jtag_add_runtest(int num_cycles, enum tap_state endstate);
227 int jtag_add_reset(int trst, int srst);
228 int jtag_add_end_state(enum tap_state endstate);
229 int jtag_add_sleep(u32 us);
230 int jtag_execute_queue(void);
231 int jtag_cancel_queue(void);
234 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
235 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
236 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
237 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
238 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
239 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
241 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
243 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
244 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
245 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
246 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
247 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
248 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
250 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
252 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
254 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
256 if (callback == NULL)
258 return ERROR_INVALID_ARGUMENTS;
263 while ((*callbacks_p)->next)
264 callbacks_p = &((*callbacks_p)->next);
265 callbacks_p = &((*callbacks_p)->next);
268 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
269 (*callbacks_p)->callback = callback;
270 (*callbacks_p)->priv = priv;
271 (*callbacks_p)->next = NULL;
276 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
278 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
280 if (callback == NULL)
282 return ERROR_INVALID_ARGUMENTS;
287 jtag_event_callback_t **next = &((*callbacks_p)->next);
288 if ((*callbacks_p)->callback == callback)
291 *callbacks_p = *next;
299 int jtag_call_event_callbacks(enum jtag_event event)
301 jtag_event_callback_t *callback = jtag_event_callbacks;
303 DEBUG("jtag event: %s", jtag_event_strings[event]);
307 callback->callback(event, callback->priv);
308 callback = callback->next;
314 /* returns a pointer to the pointer of the last command in queue
315 * this may be a pointer to the root pointer (jtag_command_queue)
316 * or to the next member of the last but one command
318 jtag_command_t** jtag_get_last_command_p(void)
320 /* jtag_command_t *cmd = jtag_command_queue;
326 return &jtag_command_queue;
330 return last_comand_pointer;
333 /* returns a pointer to the n-th device in the scan chain */
334 jtag_device_t* jtag_get_device(int num)
336 jtag_device_t *device = jtag_devices;
343 device = device->next;
347 ERROR("jtag device number %d not defined", num);
351 void* cmd_queue_alloc(size_t size)
353 cmd_queue_page_t **p_page = &cmd_queue_pages;
358 while ((*p_page)->next)
359 p_page = &((*p_page)->next);
360 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
361 p_page = &((*p_page)->next);
366 *p_page = malloc(sizeof(cmd_queue_page_t));
368 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
369 (*p_page)->next = NULL;
372 offset = (*p_page)->used;
373 (*p_page)->used += size;
375 u8 *t=(u8 *)((*p_page)->address);
379 void cmd_queue_free()
381 cmd_queue_page_t *page = cmd_queue_pages;
385 cmd_queue_page_t *last = page;
391 cmd_queue_pages = NULL;
394 int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
398 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
399 jtag_error=ERROR_JTAG_TRST_ASSERTED;
400 return ERROR_JTAG_TRST_ASSERTED;
404 cmd_queue_end_state = state;
406 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
407 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
409 if (cmd_queue_end_state == TAP_TLR)
410 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
412 cmd_queue_cur_state = cmd_queue_end_state;
414 int retval=interface_jtag_add_ir_scan(num_fields, fields, state);
415 if (retval!=ERROR_OK)
420 int INTERFACE(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
422 jtag_command_t **last_cmd;
423 jtag_device_t *device;
428 last_cmd = jtag_get_last_command_p();
430 /* allocate memory for a new list member */
431 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
432 (*last_cmd)->next = NULL;
433 last_comand_pointer = &((*last_cmd)->next);
434 (*last_cmd)->type = JTAG_SCAN;
436 /* allocate memory for ir scan command */
437 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
438 (*last_cmd)->cmd.scan->ir_scan = 1;
439 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
440 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
441 (*last_cmd)->cmd.scan->end_state = state;
443 for (i = 0; i < jtag_num_devices; i++)
446 device = jtag_get_device(i);
447 scan_size = device->ir_length;
448 (*last_cmd)->cmd.scan->fields[i].device = i;
449 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
450 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
451 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
453 /* search the list */
454 for (j = 0; j < num_fields; j++)
456 if (i == fields[j].device)
459 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
460 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
462 if (jtag_verify_capture_ir)
464 if (fields[j].in_handler==NULL)
466 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
469 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
470 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
471 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
472 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
483 /* if a device isn't listed, set it to BYPASS */
484 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
485 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
490 /* update device information */
491 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
497 int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
499 jtag_command_t **last_cmd;
504 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
505 return ERROR_JTAG_TRST_ASSERTED;
508 last_cmd = jtag_get_last_command_p();
510 /* allocate memory for a new list member */
511 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
512 (*last_cmd)->next = NULL;
513 last_comand_pointer = &((*last_cmd)->next);
514 (*last_cmd)->type = JTAG_SCAN;
516 /* allocate memory for ir scan command */
517 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
518 (*last_cmd)->cmd.scan->ir_scan = 1;
519 (*last_cmd)->cmd.scan->num_fields = num_fields;
520 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
521 (*last_cmd)->cmd.scan->end_state = state;
524 cmd_queue_end_state = state;
526 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
527 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
529 if (cmd_queue_end_state == TAP_TLR)
530 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
532 cmd_queue_cur_state = cmd_queue_end_state;
534 for (i = 0; i < num_fields; i++)
536 int num_bits = fields[i].num_bits;
537 int num_bytes = CEIL(fields[i].num_bits, 8);
538 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
539 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
540 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
541 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
542 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
543 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
544 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
545 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
546 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
551 int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
554 int bypass_devices = 0;
556 jtag_command_t **last_cmd = jtag_get_last_command_p();
557 jtag_device_t *device = jtag_devices;
562 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
563 return ERROR_JTAG_TRST_ASSERTED;
566 /* count devices in bypass */
571 device = device->next;
574 /* allocate memory for a new list member */
575 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
576 last_comand_pointer = &((*last_cmd)->next);
577 (*last_cmd)->next = NULL;
578 (*last_cmd)->type = JTAG_SCAN;
580 /* allocate memory for dr scan command */
581 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
582 (*last_cmd)->cmd.scan->ir_scan = 0;
583 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
584 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
585 (*last_cmd)->cmd.scan->end_state = state;
588 cmd_queue_end_state = state;
590 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
591 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
593 if (cmd_queue_end_state == TAP_TLR)
594 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
596 cmd_queue_cur_state = cmd_queue_end_state;
598 for (i = 0; i < jtag_num_devices; i++)
601 (*last_cmd)->cmd.scan->fields[field_count].device = i;
603 for (j = 0; j < num_fields; j++)
605 if (i == fields[j].device)
608 scan_size = fields[j].num_bits;
609 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
610 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
611 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
612 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
613 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
614 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
615 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
616 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
621 /* if a device isn't listed, the BYPASS register should be selected */
622 if (!jtag_get_device(i)->bypass)
624 ERROR("BUG: no scan data for a device not in BYPASS");
628 /* program the scan field to 1 bit length, and ignore it's value */
629 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
630 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
631 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
632 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
633 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
634 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
635 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
636 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
640 /* if a device is listed, the BYPASS register must not be selected */
641 if (jtag_get_device(i)->bypass)
643 WARNING("scan data for a device in BYPASS");
651 int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
654 jtag_command_t **last_cmd = jtag_get_last_command_p();
658 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
659 return ERROR_JTAG_TRST_ASSERTED;
662 /* allocate memory for a new list member */
663 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
664 last_comand_pointer = &((*last_cmd)->next);
665 (*last_cmd)->next = NULL;
666 (*last_cmd)->type = JTAG_SCAN;
668 /* allocate memory for scan command */
669 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
670 (*last_cmd)->cmd.scan->ir_scan = 0;
671 (*last_cmd)->cmd.scan->num_fields = num_fields;
672 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
673 (*last_cmd)->cmd.scan->end_state = state;
676 cmd_queue_end_state = state;
678 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
679 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
681 if (cmd_queue_end_state == TAP_TLR)
682 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
684 cmd_queue_cur_state = cmd_queue_end_state;
686 for (i = 0; i < num_fields; i++)
688 int num_bits = fields[i].num_bits;
689 int num_bytes = CEIL(fields[i].num_bits, 8);
690 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
691 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
692 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
693 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
694 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
695 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
696 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
697 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
698 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
703 int jtag_add_statemove(enum tap_state state)
707 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
708 return jtag_error=ERROR_JTAG_TRST_ASSERTED;
712 cmd_queue_end_state = state;
714 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
715 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
717 if (cmd_queue_end_state == TAP_TLR)
718 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
720 cmd_queue_cur_state = cmd_queue_end_state;
722 return interface_jtag_add_statemove(state);
725 int INTERFACE(interface_jtag_add_statemove)(enum tap_state state)
727 jtag_command_t **last_cmd = jtag_get_last_command_p();
729 /* allocate memory for a new list member */
730 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
731 last_comand_pointer = &((*last_cmd)->next);
732 (*last_cmd)->next = NULL;
733 (*last_cmd)->type = JTAG_STATEMOVE;
735 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
736 (*last_cmd)->cmd.statemove->end_state = state;
742 int jtag_add_pathmove(int num_states, enum tap_state *path)
744 jtag_command_t **last_cmd = jtag_get_last_command_p();
749 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
750 return ERROR_JTAG_TRST_ASSERTED;
753 /* the last state has to be a stable state */
754 if (tap_move_map[path[num_states - 1]] == -1)
756 ERROR("TAP path doesn't finish in a stable state");
757 return ERROR_JTAG_NOT_IMPLEMENTED;
760 /* allocate memory for a new list member */
761 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
762 last_comand_pointer = &((*last_cmd)->next);
763 (*last_cmd)->next = NULL;
764 (*last_cmd)->type = JTAG_PATHMOVE;
766 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
767 (*last_cmd)->cmd.pathmove->num_states = num_states;
768 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
770 for (i = 0; i < num_states; i++)
771 (*last_cmd)->cmd.pathmove->path[i] = path[i];
773 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
774 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
776 if (cmd_queue_end_state == TAP_TLR)
777 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
779 cmd_queue_cur_state = path[num_states - 1];
784 int INTERFACE(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
786 jtag_command_t **last_cmd = jtag_get_last_command_p();
788 /* allocate memory for a new list member */
789 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
790 (*last_cmd)->next = NULL;
791 last_comand_pointer = &((*last_cmd)->next);
792 (*last_cmd)->type = JTAG_RUNTEST;
794 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
795 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
796 (*last_cmd)->cmd.runtest->end_state = state;
801 int jtag_add_runtest(int num_cycles, enum tap_state state)
805 jtag_error=ERROR_JTAG_QUEUE_FAILED;
806 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
807 return ERROR_JTAG_TRST_ASSERTED;
811 cmd_queue_end_state = state;
813 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
814 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
816 if (cmd_queue_end_state == TAP_TLR)
817 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
819 cmd_queue_cur_state = cmd_queue_end_state;
821 /* executed by sw or hw fifo */
822 return interface_jtag_add_runtest(num_cycles, state);
825 int jtag_add_reset(int req_trst, int req_srst)
827 int trst_with_tms = 0;
831 req_trst = jtag_trst;
834 req_srst = jtag_srst;
836 /* Make sure that jtag_reset_config allows the requested reset */
837 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
838 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
840 return jtag_error=ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
843 /* if TRST pulls SRST, we reset with TAP T-L-R */
844 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
850 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
852 ERROR("requested nSRST assertion, but the current configuration doesn't support this");
853 return jtag_error=ERROR_JTAG_RESET_CANT_SRST;
856 if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
862 jtag_trst = req_trst;
863 jtag_srst = req_srst;
867 jtag_call_event_callbacks(JTAG_SRST_ASSERTED);
871 jtag_call_event_callbacks(JTAG_SRST_RELEASED);
872 if (jtag_nsrst_delay)
873 jtag_add_sleep(jtag_nsrst_delay * 1000);
878 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
879 cmd_queue_cur_state = TAP_TLR;
880 cmd_queue_end_state = TAP_TLR;
888 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
889 * and inform possible listeners about this
891 cmd_queue_cur_state = TAP_TLR;
892 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
896 /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
897 * but we might want to add a delay to give the TAP time to settle
899 if (jtag_ntrst_delay)
900 jtag_add_sleep(jtag_ntrst_delay * 1000);
903 retval = interface_jtag_add_reset(req_trst, req_srst);
904 if (retval!=ERROR_OK)
909 jtag_add_statemove(TAP_TLR);
916 int INTERFACE(interface_jtag_add_reset)(int req_trst, int req_srst)
918 jtag_command_t **last_cmd = jtag_get_last_command_p();
920 /* allocate memory for a new list member */
921 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
922 (*last_cmd)->next = NULL;
923 last_comand_pointer = &((*last_cmd)->next);
924 (*last_cmd)->type = JTAG_RESET;
926 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
927 (*last_cmd)->cmd.reset->trst = req_trst;
928 (*last_cmd)->cmd.reset->srst = req_srst;
934 int jtag_add_end_state(enum tap_state state)
936 jtag_command_t **last_cmd = jtag_get_last_command_p();
938 /* allocate memory for a new list member */
939 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
940 (*last_cmd)->next = NULL;
941 last_comand_pointer = &((*last_cmd)->next);
942 (*last_cmd)->type = JTAG_END_STATE;
944 (*last_cmd)->cmd.end_state = cmd_queue_alloc(sizeof(end_state_command_t));
945 (*last_cmd)->cmd.end_state->end_state = state;
948 cmd_queue_end_state = state;
953 int jtag_add_sleep(u32 us)
955 jtag_command_t **last_cmd = jtag_get_last_command_p();
957 /* allocate memory for a new list member */
958 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
959 (*last_cmd)->next = NULL;
960 last_comand_pointer = &((*last_cmd)->next);
961 (*last_cmd)->type = JTAG_SLEEP;
963 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
964 (*last_cmd)->cmd.sleep->us = us;
969 int jtag_scan_size(scan_command_t *cmd)
974 /* count bits in scan command */
975 for (i = 0; i < cmd->num_fields; i++)
977 bit_count += cmd->fields[i].num_bits;
983 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
988 bit_count = jtag_scan_size(cmd);
989 *buffer = malloc(CEIL(bit_count, 8));
993 for (i = 0; i < cmd->num_fields; i++)
995 if (cmd->fields[i].out_value)
997 #ifdef _DEBUG_JTAG_IO_
998 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1000 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1001 #ifdef _DEBUG_JTAG_IO_
1002 DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1007 bit_count += cmd->fields[i].num_bits;
1014 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1020 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1023 for (i = 0; i < cmd->num_fields; i++)
1025 /* if neither in_value nor in_handler
1026 * are specified we don't have to examine this field
1028 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1030 int num_bits = cmd->fields[i].num_bits;
1031 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1033 #ifdef _DEBUG_JTAG_IO_
1036 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1037 DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1041 if (cmd->fields[i].in_value)
1043 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1045 if (cmd->fields[i].in_handler)
1047 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1049 WARNING("in_handler reported a failed check");
1050 retval = ERROR_JTAG_QUEUE_FAILED;
1055 /* no in_value specified, but a handler takes care of the scanned data */
1056 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1058 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1060 /* We're going to call the error:handler later, but if the in_handler
1061 * reported an error we report this failure upstream
1063 WARNING("in_handler reported a failed check");
1064 retval = ERROR_JTAG_QUEUE_FAILED;
1070 bit_count += cmd->fields[i].num_bits;
1076 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1078 int retval = ERROR_OK;
1079 int num_bits = field->num_bits;
1081 int compare_failed = 0;
1083 if (field->in_check_mask)
1084 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1086 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1090 /* An error handler could have caught the failing check
1091 * only report a problem when there wasn't a handler, or if the handler
1092 * acknowledged the error
1096 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1097 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1099 if (field->in_check_mask)
1101 char *in_check_mask_char;
1102 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1103 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);
1104 free(in_check_mask_char);
1108 WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
1111 free(captured_char);
1112 free(in_check_value_char);
1114 retval = ERROR_JTAG_QUEUE_FAILED;
1122 set up checking of this field using the in_handler. The values passed in must be valid until
1123 after jtag_execute() has completed.
1125 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1128 field->in_handler = jtag_check_value;
1130 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1131 field->in_handler_priv = NULL; /* this will be filled in at the invocation site to point to the field duplicate */
1132 field->in_check_value = value;
1133 field->in_check_mask = mask;
1136 enum scan_type jtag_scan_type(scan_command_t *cmd)
1141 for (i = 0; i < cmd->num_fields; i++)
1143 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1145 if (cmd->fields[i].out_value)
1152 int INTERFACE(interface_jtag_execute_queue)(void)
1156 retval = jtag->execute_queue();
1160 jtag_command_queue = NULL;
1161 last_comand_pointer = &jtag_command_queue;
1163 jtag_error=ERROR_OK;
1168 int jtag_execute_queue(void)
1170 int retval=interface_jtag_execute_queue();
1171 if (retval!=ERROR_OK)
1174 jtag_error=ERROR_OK;
1178 int jtag_reset_callback(enum jtag_event event, void *priv)
1180 jtag_device_t *device = priv;
1184 if (event == JTAG_TRST_ASSERTED)
1186 buf_set_ones(device->cur_instr, device->ir_length);
1193 void jtag_sleep(u32 us)
1198 /* Try to examine chain layout according to IEEE 1149.1 §12
1200 int jtag_examine_chain()
1202 jtag_device_t *device = jtag_devices;
1204 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1207 int device_count = 0;
1208 u8 zero_check = 0x0;
1209 u8 one_check = 0xff;
1212 field.num_bits = sizeof(idcode_buffer) * 8;
1213 field.out_value = idcode_buffer;
1214 field.out_mask = NULL;
1215 field.in_value = idcode_buffer;
1216 field.in_check_value = NULL;
1217 field.in_check_mask = NULL;
1218 field.in_handler = NULL;
1219 field.in_handler_priv = NULL;
1221 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1223 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1226 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1227 jtag_execute_queue();
1229 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1231 zero_check |= idcode_buffer[i];
1232 one_check &= idcode_buffer[i];
1235 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1236 if ((zero_check == 0x00) || (one_check == 0xff))
1238 ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1239 return ERROR_JTAG_INIT_FAILED;
1242 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1244 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1245 if ((idcode & 1) == 0)
1247 /* LSB must not be 0, this indicates a device in bypass */
1258 if (idcode == 0x000000FF)
1260 /* End of chain (invalid manufacturer ID) */
1266 device->idcode = idcode;
1267 device = device->next;
1271 manufacturer = (idcode & 0xffe) >> 1;
1272 part = (idcode & 0xffff000) >> 12;
1273 version = (idcode & 0xf0000000) >> 28;
1275 INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1276 idcode, manufacturer, part, version);
1282 /* see if number of discovered devices matches configuration */
1283 if (device_count != jtag_num_devices)
1285 ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1286 device_count, jtag_num_devices);
1287 ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1288 return ERROR_JTAG_INIT_FAILED;
1294 int jtag_validate_chain()
1296 jtag_device_t *device = jtag_devices;
1297 int total_ir_length = 0;
1304 total_ir_length += device->ir_length;
1305 device = device->next;
1308 total_ir_length += 2;
1309 ir_test = malloc(CEIL(total_ir_length, 8));
1310 buf_set_ones(ir_test, total_ir_length);
1313 field.num_bits = total_ir_length;
1314 field.out_value = ir_test;
1315 field.out_mask = NULL;
1316 field.in_value = ir_test;
1317 field.in_check_value = NULL;
1318 field.in_check_mask = NULL;
1319 field.in_handler = NULL;
1320 field.in_handler_priv = NULL;
1322 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1323 jtag_execute_queue();
1325 device = jtag_devices;
1328 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1330 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1331 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1334 return ERROR_JTAG_INIT_FAILED;
1336 chain_pos += device->ir_length;
1337 device = device->next;
1340 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
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);
1346 return ERROR_JTAG_INIT_FAILED;
1354 int jtag_register_commands(struct command_context_s *cmd_ctx)
1356 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1357 COMMAND_CONFIG, NULL);
1358 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1359 COMMAND_ANY, "set jtag speed (if supported) <speed>");
1360 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1361 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1362 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1363 COMMAND_CONFIG, NULL);
1364 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1365 COMMAND_CONFIG, NULL);
1366 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1367 COMMAND_CONFIG, NULL);
1369 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1370 COMMAND_EXEC, "print current scan chain configuration");
1372 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1373 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1374 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1375 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1376 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1377 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1378 register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
1379 COMMAND_EXEC, "move to current endstate or [tap_state]");
1380 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1381 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1382 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1383 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1385 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1386 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1390 int jtag_interface_init(struct command_context_s *cmd_ctx)
1392 if (!jtag_interface)
1394 /* nothing was previously specified by "interface" command */
1395 ERROR("JTAG interface has to be specified, see \"interface\" command");
1396 return ERROR_JTAG_INVALID_INTERFACE;
1399 if (jtag_interface->init() != ERROR_OK)
1400 return ERROR_JTAG_INIT_FAILED;
1402 jtag = jtag_interface;
1406 int jtag_init(struct command_context_s *cmd_ctx)
1408 int validate_tries = 0;
1409 jtag_device_t *device;
1413 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1414 return ERROR_JTAG_INIT_FAILED;
1416 device = jtag_devices;
1417 jtag_ir_scan_size = 0;
1418 jtag_num_devices = 0;
1419 while (device != NULL)
1421 jtag_ir_scan_size += device->ir_length;
1423 device = device->next;
1426 jtag_add_statemove(TAP_TLR);
1427 jtag_execute_queue();
1429 /* examine chain first, as this could discover the real chain layout */
1430 if (jtag_examine_chain() != ERROR_OK)
1432 ERROR("trying to validate configured JTAG chain anyway...");
1435 while (jtag_validate_chain() != ERROR_OK)
1438 if (validate_tries > 5)
1440 ERROR("Could not validate JTAG chain, exit");
1442 return ERROR_JTAG_INVALID_INTERFACE;
1450 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1454 /* check whether the interface is already configured */
1457 WARNING("Interface already configured, ignoring");
1461 /* interface name is a mandatory argument */
1462 if (argc < 1 || args[0][0] == '\0')
1464 return ERROR_COMMAND_SYNTAX_ERROR;
1467 for (i=0; jtag_interfaces[i]; i++)
1469 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1471 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1474 jtag_interface = jtag_interfaces[i];
1479 /* no valid interface was found (i.e. the configuration option,
1480 * didn't match one of the compiled-in interfaces
1482 ERROR("No valid jtag interface found (%s)", args[0]);
1483 ERROR("compiled-in jtag interfaces:");
1484 for (i = 0; jtag_interfaces[i]; i++)
1486 ERROR("%i: %s", i, jtag_interfaces[i]->name);
1489 return ERROR_JTAG_INVALID_INTERFACE;
1492 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1494 jtag_device_t **last_device_p = &jtag_devices;
1498 while ((*last_device_p)->next)
1499 last_device_p = &((*last_device_p)->next);
1500 last_device_p = &((*last_device_p)->next);
1506 *last_device_p = malloc(sizeof(jtag_device_t));
1507 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1509 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1510 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1511 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1512 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1514 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1515 (*last_device_p)->bypass = 1;
1516 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1518 (*last_device_p)->next = NULL;
1520 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1527 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1529 jtag_device_t *device = jtag_devices;
1530 int device_count = 0;
1534 u32 expected, expected_mask, cur_instr;
1535 expected = buf_get_u32(device->expected, 0, device->ir_length);
1536 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1537 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1538 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);
1539 device = device->next;
1546 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1550 if (strcmp(args[0], "none") == 0)
1551 jtag_reset_config = RESET_NONE;
1552 else if (strcmp(args[0], "trst_only") == 0)
1553 jtag_reset_config = RESET_HAS_TRST;
1554 else if (strcmp(args[0], "srst_only") == 0)
1555 jtag_reset_config = RESET_HAS_SRST;
1556 else if (strcmp(args[0], "trst_and_srst") == 0)
1557 jtag_reset_config = RESET_TRST_AND_SRST;
1560 ERROR("invalid reset_config argument, defaulting to none");
1561 jtag_reset_config = RESET_NONE;
1562 return ERROR_INVALID_ARGUMENTS;
1568 if (strcmp(args[1], "srst_pulls_trst") == 0)
1569 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1570 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1571 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1572 else if (strcmp(args[1], "combined") == 0)
1573 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1574 else if (strcmp(args[1], "separate") == 0)
1575 jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
1578 ERROR("invalid reset_config argument, defaulting to none");
1579 jtag_reset_config = RESET_NONE;
1580 return ERROR_INVALID_ARGUMENTS;
1586 if (strcmp(args[2], "trst_open_drain") == 0)
1587 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1588 else if (strcmp(args[2], "trst_push_pull") == 0)
1589 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1592 ERROR("invalid reset_config argument, defaulting to none");
1593 jtag_reset_config = RESET_NONE;
1594 return ERROR_INVALID_ARGUMENTS;
1600 if (strcmp(args[3], "srst_push_pull") == 0)
1601 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1602 else if (strcmp(args[3], "srst_open_drain") == 0)
1603 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1606 ERROR("invalid reset_config argument, defaulting to none");
1607 jtag_reset_config = RESET_NONE;
1608 return ERROR_INVALID_ARGUMENTS;
1615 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1619 ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1624 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1630 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1634 ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1639 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1645 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1648 command_print(cmd_ctx, "jtag_speed: %i", jtag_speed);
1652 /* this command can be called during CONFIG,
1653 * in which case jtag isn't initialized */
1655 jtag->speed(strtoul(args[0], NULL, 0));
1657 jtag_speed = strtoul(args[0], NULL, 0);
1663 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1665 enum tap_state state;
1669 return ERROR_COMMAND_SYNTAX_ERROR;
1673 for (state = 0; state < 16; state++)
1675 if (strcmp(args[0], tap_state_strings[state]) == 0)
1677 jtag_add_end_state(state);
1678 jtag_execute_queue();
1682 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[end_state]);
1687 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1695 return ERROR_COMMAND_SYNTAX_ERROR;
1699 if (args[0][0] == '1')
1701 else if (args[0][0] == '0')
1705 return ERROR_COMMAND_SYNTAX_ERROR;
1708 if (args[1][0] == '1')
1710 else if (args[1][0] == '0')
1714 return ERROR_COMMAND_SYNTAX_ERROR;
1717 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1718 return ERROR_JTAG_INIT_FAILED;
1720 if ((retval = jtag_add_reset(trst, srst)) != ERROR_OK)
1724 case ERROR_JTAG_RESET_WOULD_ASSERT_TRST:
1725 command_print(cmd_ctx, "requested reset would assert trst\nif this is acceptable, use jtag_reset 1 %c", args[1][0]);
1727 case ERROR_JTAG_RESET_CANT_SRST:
1728 command_print(cmd_ctx, "can't assert srst because the current reset_config doesn't support it");
1731 command_print(cmd_ctx, "unknown error");
1734 jtag_execute_queue();
1739 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1743 return ERROR_COMMAND_SYNTAX_ERROR;
1746 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1747 jtag_execute_queue();
1753 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1755 enum tap_state state;
1760 for (state = 0; state < 16; state++)
1762 if (strcmp(args[0], tap_state_strings[state]) == 0)
1769 jtag_add_statemove(state);
1770 jtag_execute_queue();
1776 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1779 scan_field_t *fields;
1781 if ((argc < 2) || (argc % 2))
1783 return ERROR_COMMAND_SYNTAX_ERROR;
1786 fields = malloc(sizeof(scan_field_t) * argc / 2);
1788 for (i = 0; i < argc / 2; i++)
1790 int device = strtoul(args[i*2], NULL, 0);
1791 int field_size = jtag_get_device(device)->ir_length;
1792 fields[i].device = device;
1793 fields[i].out_value = malloc(CEIL(field_size, 8));
1794 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1795 fields[i].out_mask = NULL;
1796 fields[i].in_value = NULL;
1797 fields[i].in_check_mask = NULL;
1798 fields[i].in_handler = NULL;
1799 fields[i].in_handler_priv = NULL;
1802 jtag_add_ir_scan(argc / 2, fields, -1);
1803 jtag_execute_queue();
1805 for (i = 0; i < argc / 2; i++)
1806 free(fields[i].out_value);
1813 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1815 scan_field_t *fields;
1817 int field_count = 0;
1821 if ((argc < 2) || (argc % 2))
1823 return ERROR_COMMAND_SYNTAX_ERROR;
1826 for (i = 0; i < argc; i+=2)
1828 var = get_var_by_namenum(args[i+1]);
1831 num_fields += var->num_fields;
1835 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1840 fields = malloc(sizeof(scan_field_t) * num_fields);
1842 for (i = 0; i < argc; i+=2)
1844 var = get_var_by_namenum(args[i+1]);
1846 for (j = 0; j < var->num_fields; j++)
1848 fields[field_count].device = strtol(args[i], NULL, 0);
1849 fields[field_count].num_bits = var->fields[j].num_bits;
1850 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1851 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1852 fields[field_count].out_mask = NULL;
1853 fields[field_count].in_value = fields[field_count].out_value;
1854 fields[field_count].in_check_mask = NULL;
1855 fields[field_count].in_check_value = NULL;
1856 fields[field_count].in_handler = field_le_to_host;
1857 fields[field_count++].in_handler_priv = &(var->fields[j]);
1861 jtag_add_dr_scan(num_fields, fields, -1);
1862 jtag_execute_queue();
1864 for (i = 0; i < argc / 2; i++)
1865 free(fields[i].out_value);
1872 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1876 if (strcmp(args[0], "enable") == 0)
1878 jtag_verify_capture_ir = 1;
1880 else if (strcmp(args[0], "disable") == 0)
1882 jtag_verify_capture_ir = 0;
1885 return ERROR_COMMAND_SYNTAX_ERROR;
1887 } else if (argc != 0)
1889 return ERROR_COMMAND_SYNTAX_ERROR;
1892 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");