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 /* note that this is not marked as static as it must be available from outside jtag.c for those
38 that implement the jtag_xxx() minidriver layer
40 static int jtag_error=ERROR_OK;
43 char* tap_state_strings[16] =
46 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
48 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
51 typedef struct cmd_queue_page_s
55 struct cmd_queue_page_s *next;
58 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
59 static cmd_queue_page_t *cmd_queue_pages = NULL;
61 /* tap_move[i][j]: tap movement command to go from state i to state j
69 * SD->SD and SI->SI have to be caught in interface specific code
73 /* TLR RTI SD PD SI PI */
74 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
75 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
76 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
77 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
78 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
79 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
82 int tap_move_map[16] = {
83 0, -1, -1, 2, -1, 3, -1, -1,
84 1, -1, -1, 4, -1, 5, -1, -1
87 tap_transition_t tap_transitions[16] =
89 {TAP_TLR, TAP_RTI}, /* TLR */
90 {TAP_SIS, TAP_CD}, /* SDS */
91 {TAP_E1D, TAP_SD}, /* CD */
92 {TAP_E1D, TAP_SD}, /* SD */
93 {TAP_UD, TAP_PD}, /* E1D */
94 {TAP_E2D, TAP_PD}, /* PD */
95 {TAP_UD, TAP_SD}, /* E2D */
96 {TAP_SDS, TAP_RTI}, /* UD */
97 {TAP_SDS, TAP_RTI}, /* RTI */
98 {TAP_TLR, TAP_CI}, /* SIS */
99 {TAP_E1I, TAP_SI}, /* CI */
100 {TAP_E1I, TAP_SI}, /* SI */
101 {TAP_UI, TAP_PI}, /* E1I */
102 {TAP_E2I, TAP_PI}, /* PI */
103 {TAP_UI, TAP_SI}, /* E2I */
104 {TAP_SDS, TAP_RTI} /* UI */
107 char* jtag_event_strings[] =
115 enum tap_state end_state = TAP_TLR;
116 enum tap_state cur_state = TAP_TLR;
120 jtag_command_t *jtag_command_queue = NULL;
121 jtag_command_t **last_comand_pointer = &jtag_command_queue;
122 jtag_device_t *jtag_devices = NULL;
123 int jtag_num_devices = 0;
124 int jtag_ir_scan_size = 0;
125 enum reset_types jtag_reset_config = RESET_NONE;
126 enum tap_state cmd_queue_end_state = TAP_TLR;
127 enum tap_state cmd_queue_cur_state = TAP_TLR;
129 int jtag_verify_capture_ir = 1;
131 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
132 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
133 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
135 /* maximum number of JTAG devices expected in the chain
137 #define JTAG_MAX_CHAIN_SIZE 20
139 /* callbacks to inform high-level handlers about JTAG state changes */
140 jtag_event_callback_t *jtag_event_callbacks;
142 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
144 #if BUILD_PARPORT == 1
145 extern jtag_interface_t parport_interface;
148 #if BUILD_FT2232_FTD2XX == 1
149 extern jtag_interface_t ft2232_interface;
152 #if BUILD_FT2232_LIBFTDI == 1
153 extern jtag_interface_t ft2232_interface;
156 #if BUILD_AMTJTAGACCEL == 1
157 extern jtag_interface_t amt_jtagaccel_interface;
160 #if BUILD_EP93XX == 1
161 extern jtag_interface_t ep93xx_interface;
164 #if BUILD_AT91RM9200 == 1
165 extern jtag_interface_t at91rm9200_interface;
168 #if BUILD_GW16012 == 1
169 extern jtag_interface_t gw16012_interface;
172 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
173 extern jtag_interface_t presto_interface;
176 #if BUILD_USBPROG == 1
177 extern jtag_interface_t usbprog_interface;
180 jtag_interface_t *jtag_interfaces[] = {
181 #if BUILD_PARPORT == 1
184 #if BUILD_FT2232_FTD2XX == 1
187 #if BUILD_FT2232_LIBFTDI == 1
190 #if BUILD_AMTJTAGACCEL == 1
191 &amt_jtagaccel_interface,
193 #if BUILD_EP93XX == 1
196 #if BUILD_AT91RM9200 == 1
197 &at91rm9200_interface,
199 #if BUILD_GW16012 == 1
202 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
205 #if BUILD_USBPROG == 1
211 jtag_interface_t *jtag = NULL;
214 jtag_interface_t *jtag_interface = NULL;
218 /* forward declarations */
219 void jtag_add_statemove(enum tap_state endstate);
220 void jtag_add_pathmove(int num_states, enum tap_state *path);
221 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
222 int jtag_add_reset(int trst, int srst);
223 void jtag_add_end_state(enum tap_state endstate);
224 void jtag_add_sleep(u32 us);
225 int jtag_execute_queue(void);
226 int jtag_cancel_queue(void);
229 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
230 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
231 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
232 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
233 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
234 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
236 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
238 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
239 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
240 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
241 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
242 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
243 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
245 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
247 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
249 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
251 if (callback == NULL)
253 return ERROR_INVALID_ARGUMENTS;
258 while ((*callbacks_p)->next)
259 callbacks_p = &((*callbacks_p)->next);
260 callbacks_p = &((*callbacks_p)->next);
263 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
264 (*callbacks_p)->callback = callback;
265 (*callbacks_p)->priv = priv;
266 (*callbacks_p)->next = NULL;
271 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
273 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
275 if (callback == NULL)
277 return ERROR_INVALID_ARGUMENTS;
282 jtag_event_callback_t **next = &((*callbacks_p)->next);
283 if ((*callbacks_p)->callback == callback)
286 *callbacks_p = *next;
294 int jtag_call_event_callbacks(enum jtag_event event)
296 jtag_event_callback_t *callback = jtag_event_callbacks;
298 DEBUG("jtag event: %s", jtag_event_strings[event]);
302 callback->callback(event, callback->priv);
303 callback = callback->next;
309 /* returns a pointer to the pointer of the last command in queue
310 * this may be a pointer to the root pointer (jtag_command_queue)
311 * or to the next member of the last but one command
313 jtag_command_t** jtag_get_last_command_p(void)
315 /* jtag_command_t *cmd = jtag_command_queue;
321 return &jtag_command_queue;
325 return last_comand_pointer;
328 /* returns a pointer to the n-th device in the scan chain */
329 jtag_device_t* jtag_get_device(int num)
331 jtag_device_t *device = jtag_devices;
338 device = device->next;
342 ERROR("jtag device number %d not defined", num);
346 void* cmd_queue_alloc(size_t size)
348 cmd_queue_page_t **p_page = &cmd_queue_pages;
353 while ((*p_page)->next)
354 p_page = &((*p_page)->next);
355 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
356 p_page = &((*p_page)->next);
361 *p_page = malloc(sizeof(cmd_queue_page_t));
363 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
364 (*p_page)->next = NULL;
367 offset = (*p_page)->used;
368 (*p_page)->used += size;
370 u8 *t=(u8 *)((*p_page)->address);
374 void cmd_queue_free()
376 cmd_queue_page_t *page = cmd_queue_pages;
380 cmd_queue_page_t *last = page;
386 cmd_queue_pages = NULL;
389 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
393 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
394 jtag_error=ERROR_JTAG_TRST_ASSERTED;
399 cmd_queue_end_state = state;
401 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
402 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
404 if (cmd_queue_end_state == TAP_TLR)
405 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
407 cmd_queue_cur_state = cmd_queue_end_state;
409 int retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
410 if (retval!=ERROR_OK)
414 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
416 jtag_command_t **last_cmd;
417 jtag_device_t *device;
422 last_cmd = jtag_get_last_command_p();
424 /* allocate memory for a new list member */
425 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
426 (*last_cmd)->next = NULL;
427 last_comand_pointer = &((*last_cmd)->next);
428 (*last_cmd)->type = JTAG_SCAN;
430 /* allocate memory for ir scan command */
431 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
432 (*last_cmd)->cmd.scan->ir_scan = 1;
433 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
434 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
435 (*last_cmd)->cmd.scan->end_state = state;
437 for (i = 0; i < jtag_num_devices; i++)
440 device = jtag_get_device(i);
441 scan_size = device->ir_length;
442 (*last_cmd)->cmd.scan->fields[i].device = i;
443 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
444 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
445 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
447 /* search the list */
448 for (j = 0; j < num_fields; j++)
450 if (i == fields[j].device)
453 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
454 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
456 if (jtag_verify_capture_ir)
458 if (fields[j].in_handler==NULL)
460 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
463 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
464 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
465 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
466 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
477 /* if a device isn't listed, set it to BYPASS */
478 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
479 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
484 /* update device information */
485 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
491 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
495 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
496 jtag_error=ERROR_JTAG_TRST_ASSERTED;
501 cmd_queue_end_state = state;
503 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
504 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
506 if (cmd_queue_end_state == TAP_TLR)
507 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
509 cmd_queue_cur_state = cmd_queue_end_state;
511 int retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
512 if (retval!=ERROR_OK)
516 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
519 jtag_command_t **last_cmd;
521 last_cmd = jtag_get_last_command_p();
523 /* allocate memory for a new list member */
524 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
525 (*last_cmd)->next = NULL;
526 last_comand_pointer = &((*last_cmd)->next);
527 (*last_cmd)->type = JTAG_SCAN;
529 /* allocate memory for ir scan command */
530 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
531 (*last_cmd)->cmd.scan->ir_scan = 1;
532 (*last_cmd)->cmd.scan->num_fields = num_fields;
533 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
534 (*last_cmd)->cmd.scan->end_state = state;
536 for (i = 0; i < num_fields; i++)
538 int num_bits = fields[i].num_bits;
539 int num_bytes = CEIL(fields[i].num_bits, 8);
540 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
541 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
542 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
543 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
544 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
545 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
546 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
547 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
548 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
553 void jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
557 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
558 jtag_error=ERROR_JTAG_TRST_ASSERTED;
563 cmd_queue_end_state = state;
565 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
566 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
568 if (cmd_queue_end_state == TAP_TLR)
569 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
571 cmd_queue_cur_state = cmd_queue_end_state;
573 int retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
574 if (retval!=ERROR_OK)
578 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
581 int bypass_devices = 0;
585 jtag_command_t **last_cmd = jtag_get_last_command_p();
586 jtag_device_t *device = jtag_devices;
588 /* count devices in bypass */
593 device = device->next;
596 /* allocate memory for a new list member */
597 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
598 last_comand_pointer = &((*last_cmd)->next);
599 (*last_cmd)->next = NULL;
600 (*last_cmd)->type = JTAG_SCAN;
602 /* allocate memory for dr scan command */
603 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
604 (*last_cmd)->cmd.scan->ir_scan = 0;
605 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
606 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
607 (*last_cmd)->cmd.scan->end_state = state;
609 for (i = 0; i < jtag_num_devices; i++)
612 (*last_cmd)->cmd.scan->fields[field_count].device = i;
614 for (j = 0; j < num_fields; j++)
616 if (i == fields[j].device)
619 scan_size = fields[j].num_bits;
620 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
621 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
622 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
623 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
624 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
625 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
626 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
627 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
632 #ifdef _DEBUG_JTAG_IO_
633 /* if a device isn't listed, the BYPASS register should be selected */
634 if (!jtag_get_device(i)->bypass)
636 ERROR("BUG: no scan data for a device not in BYPASS");
640 /* program the scan field to 1 bit length, and ignore it's value */
641 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
642 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
643 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
644 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
645 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
646 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
647 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
648 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
652 #ifdef _DEBUG_JTAG_IO_
653 /* if a device is listed, the BYPASS register must not be selected */
654 if (jtag_get_device(i)->bypass)
656 ERROR("BUG: scan data for a device in BYPASS");
665 void MINIDRIVER(interface_jtag_add_dr_out)(int device_num,
669 enum tap_state end_state)
674 int bypass_devices = 0;
676 jtag_command_t **last_cmd = jtag_get_last_command_p();
677 jtag_device_t *device = jtag_devices;
678 /* count devices in bypass */
683 device = device->next;
686 /* allocate memory for a new list member */
687 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
688 last_comand_pointer = &((*last_cmd)->next);
689 (*last_cmd)->next = NULL;
690 (*last_cmd)->type = JTAG_SCAN;
692 /* allocate memory for dr scan command */
693 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
694 (*last_cmd)->cmd.scan->ir_scan = 0;
695 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
696 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
697 (*last_cmd)->cmd.scan->end_state = end_state;
699 for (i = 0; i < jtag_num_devices; i++)
701 (*last_cmd)->cmd.scan->fields[field_count].device = i;
706 #ifdef _DEBUG_JTAG_IO_
707 /* if a device is listed, the BYPASS register must not be selected */
708 if (jtag_get_device(i)->bypass)
710 ERROR("BUG: scan data for a device in BYPASS");
714 for (j = 0; j < num_fields; j++)
717 scan_size = num_bits[j];
718 buf_set_u32(out_value, 0, scan_size, value[j]);
719 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
720 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
721 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
722 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
723 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
724 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
725 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
726 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
730 #ifdef _DEBUG_JTAG_IO_
731 /* if a device isn't listed, the BYPASS register should be selected */
732 if (!jtag_get_device(i)->bypass)
734 ERROR("BUG: no scan data for a device not in BYPASS");
738 /* program the scan field to 1 bit length, and ignore it's value */
739 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
740 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
741 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
742 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
743 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
744 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
745 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
746 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
754 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
758 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
759 jtag_error=ERROR_JTAG_TRST_ASSERTED;
764 cmd_queue_end_state = state;
766 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
767 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
769 if (cmd_queue_end_state == TAP_TLR)
770 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
772 cmd_queue_cur_state = cmd_queue_end_state;
774 int retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
775 if (retval!=ERROR_OK)
779 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
782 jtag_command_t **last_cmd = jtag_get_last_command_p();
784 /* allocate memory for a new list member */
785 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
786 last_comand_pointer = &((*last_cmd)->next);
787 (*last_cmd)->next = NULL;
788 (*last_cmd)->type = JTAG_SCAN;
790 /* allocate memory for scan command */
791 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
792 (*last_cmd)->cmd.scan->ir_scan = 0;
793 (*last_cmd)->cmd.scan->num_fields = num_fields;
794 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
795 (*last_cmd)->cmd.scan->end_state = state;
797 for (i = 0; i < num_fields; i++)
799 int num_bits = fields[i].num_bits;
800 int num_bytes = CEIL(fields[i].num_bits, 8);
801 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
802 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
803 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
804 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
805 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
806 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
807 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
808 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
809 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
814 void jtag_add_statemove(enum tap_state state)
818 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
819 jtag_error=ERROR_JTAG_TRST_ASSERTED;
824 cmd_queue_end_state = state;
826 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
827 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
829 if (cmd_queue_end_state == TAP_TLR)
830 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
832 cmd_queue_cur_state = cmd_queue_end_state;
835 retval=interface_jtag_add_statemove(cmd_queue_end_state);
836 if (retval!=ERROR_OK)
840 int MINIDRIVER(interface_jtag_add_statemove)(enum tap_state state)
842 jtag_command_t **last_cmd = jtag_get_last_command_p();
844 /* allocate memory for a new list member */
845 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
846 last_comand_pointer = &((*last_cmd)->next);
847 (*last_cmd)->next = NULL;
848 (*last_cmd)->type = JTAG_STATEMOVE;
850 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
851 (*last_cmd)->cmd.statemove->end_state = state;
857 void jtag_add_pathmove(int num_states, enum tap_state *path)
861 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
862 jtag_error=ERROR_JTAG_TRST_ASSERTED;
866 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
867 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
869 if (cmd_queue_end_state == TAP_TLR)
870 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
872 /* the last state has to be a stable state */
873 if (tap_move_map[path[num_states - 1]] == -1)
875 ERROR("TAP path doesn't finish in a stable state");
876 jtag_error=ERROR_JTAG_NOT_IMPLEMENTED;
880 enum tap_state cur_state=cmd_queue_cur_state;
882 for (i=0; i<num_states; i++)
884 if ((tap_transitions[cur_state].low != path[i])&&
885 (tap_transitions[cur_state].high != path[i]))
887 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
893 cmd_queue_cur_state = path[num_states - 1];
895 int retval=interface_jtag_add_pathmove(num_states, path);
896 if (retval!=ERROR_OK)
901 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
903 jtag_command_t **last_cmd = jtag_get_last_command_p();
906 /* allocate memory for a new list member */
907 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
908 last_comand_pointer = &((*last_cmd)->next);
909 (*last_cmd)->next = NULL;
910 (*last_cmd)->type = JTAG_PATHMOVE;
912 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
913 (*last_cmd)->cmd.pathmove->num_states = num_states;
914 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
916 for (i = 0; i < num_states; i++)
917 (*last_cmd)->cmd.pathmove->path[i] = path[i];
922 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
924 jtag_command_t **last_cmd = jtag_get_last_command_p();
926 /* allocate memory for a new list member */
927 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
928 (*last_cmd)->next = NULL;
929 last_comand_pointer = &((*last_cmd)->next);
930 (*last_cmd)->type = JTAG_RUNTEST;
932 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
933 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
934 (*last_cmd)->cmd.runtest->end_state = state;
939 void jtag_add_runtest(int num_cycles, enum tap_state state)
943 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
944 jtag_error=ERROR_JTAG_TRST_ASSERTED;
949 cmd_queue_end_state = state;
951 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
952 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
954 if (cmd_queue_end_state == TAP_TLR)
955 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
957 cmd_queue_cur_state = cmd_queue_end_state;
959 /* executed by sw or hw fifo */
960 int retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
961 if (retval!=ERROR_OK)
965 int jtag_add_reset(int req_trst, int req_srst)
967 int trst_with_tms = 0;
971 req_trst = jtag_trst;
974 req_srst = jtag_srst;
976 /* Make sure that jtag_reset_config allows the requested reset */
977 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
978 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
980 ERROR("requested reset would assert trst");
981 return ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
984 /* if TRST pulls SRST, we reset with TAP T-L-R */
985 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
991 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
993 ERROR("requested nSRST assertion, but the current configuration doesn't support this");
994 return ERROR_JTAG_RESET_CANT_SRST;
997 if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
1003 jtag_trst = req_trst;
1004 jtag_srst = req_srst;
1006 retval = interface_jtag_add_reset(req_trst, req_srst);
1007 if (retval!=ERROR_OK)
1015 jtag_call_event_callbacks(JTAG_SRST_ASSERTED);
1019 jtag_call_event_callbacks(JTAG_SRST_RELEASED);
1020 if (jtag_nsrst_delay)
1021 jtag_add_sleep(jtag_nsrst_delay * 1000);
1026 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1027 jtag_add_end_state(TAP_TLR);
1028 jtag_add_statemove(TAP_TLR);
1034 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1035 * and inform possible listeners about this
1037 cmd_queue_cur_state = TAP_TLR;
1038 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1042 /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
1043 * but we might want to add a delay to give the TAP time to settle
1045 if (jtag_ntrst_delay)
1046 jtag_add_sleep(jtag_ntrst_delay * 1000);
1051 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1053 jtag_command_t **last_cmd = jtag_get_last_command_p();
1055 /* allocate memory for a new list member */
1056 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1057 (*last_cmd)->next = NULL;
1058 last_comand_pointer = &((*last_cmd)->next);
1059 (*last_cmd)->type = JTAG_RESET;
1061 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1062 (*last_cmd)->cmd.reset->trst = req_trst;
1063 (*last_cmd)->cmd.reset->srst = req_srst;
1069 int MINIDRIVER(interface_jtag_add_end_state)(enum tap_state state)
1071 jtag_command_t **last_cmd = jtag_get_last_command_p();
1073 /* allocate memory for a new list member */
1074 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1075 (*last_cmd)->next = NULL;
1076 last_comand_pointer = &((*last_cmd)->next);
1077 (*last_cmd)->type = JTAG_END_STATE;
1079 (*last_cmd)->cmd.end_state = cmd_queue_alloc(sizeof(end_state_command_t));
1080 (*last_cmd)->cmd.end_state->end_state = state;
1085 void jtag_add_end_state(enum tap_state state)
1088 cmd_queue_end_state = state;
1089 int retval = interface_jtag_add_end_state(cmd_queue_end_state);
1090 if (retval!=ERROR_OK)
1094 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1096 jtag_command_t **last_cmd = jtag_get_last_command_p();
1098 /* allocate memory for a new list member */
1099 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1100 (*last_cmd)->next = NULL;
1101 last_comand_pointer = &((*last_cmd)->next);
1102 (*last_cmd)->type = JTAG_SLEEP;
1104 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1105 (*last_cmd)->cmd.sleep->us = us;
1110 void jtag_add_sleep(u32 us)
1112 int retval=interface_jtag_add_sleep(us);
1113 if (retval!=ERROR_OK)
1118 int jtag_scan_size(scan_command_t *cmd)
1123 /* count bits in scan command */
1124 for (i = 0; i < cmd->num_fields; i++)
1126 bit_count += cmd->fields[i].num_bits;
1132 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1137 bit_count = jtag_scan_size(cmd);
1138 *buffer = malloc(CEIL(bit_count, 8));
1142 for (i = 0; i < cmd->num_fields; i++)
1144 if (cmd->fields[i].out_value)
1146 #ifdef _DEBUG_JTAG_IO_
1147 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1149 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1150 #ifdef _DEBUG_JTAG_IO_
1151 DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1156 bit_count += cmd->fields[i].num_bits;
1163 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1169 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1172 for (i = 0; i < cmd->num_fields; i++)
1174 /* if neither in_value nor in_handler
1175 * are specified we don't have to examine this field
1177 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1179 int num_bits = cmd->fields[i].num_bits;
1180 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1182 #ifdef _DEBUG_JTAG_IO_
1185 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1186 DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1190 if (cmd->fields[i].in_value)
1192 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1194 if (cmd->fields[i].in_handler)
1196 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1198 WARNING("in_handler reported a failed check");
1199 retval = ERROR_JTAG_QUEUE_FAILED;
1204 /* no in_value specified, but a handler takes care of the scanned data */
1205 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1207 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1209 /* We're going to call the error:handler later, but if the in_handler
1210 * reported an error we report this failure upstream
1212 WARNING("in_handler reported a failed check");
1213 retval = ERROR_JTAG_QUEUE_FAILED;
1219 bit_count += cmd->fields[i].num_bits;
1225 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1227 int retval = ERROR_OK;
1228 int num_bits = field->num_bits;
1230 int compare_failed = 0;
1232 if (field->in_check_mask)
1233 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1235 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1239 /* An error handler could have caught the failing check
1240 * only report a problem when there wasn't a handler, or if the handler
1241 * acknowledged the error
1245 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1246 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1248 if (field->in_check_mask)
1250 char *in_check_mask_char;
1251 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1252 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);
1253 free(in_check_mask_char);
1257 WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
1260 free(captured_char);
1261 free(in_check_value_char);
1263 retval = ERROR_JTAG_QUEUE_FAILED;
1271 set up checking of this field using the in_handler. The values passed in must be valid until
1272 after jtag_execute() has completed.
1274 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1277 field->in_handler = jtag_check_value;
1279 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1280 field->in_handler_priv = NULL; /* this will be filled in at the invocation site to point to the field duplicate */
1281 field->in_check_value = value;
1282 field->in_check_mask = mask;
1285 enum scan_type jtag_scan_type(scan_command_t *cmd)
1290 for (i = 0; i < cmd->num_fields; i++)
1292 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1294 if (cmd->fields[i].out_value)
1301 int MINIDRIVER(interface_jtag_execute_queue)(void)
1305 retval = jtag->execute_queue();
1309 jtag_command_queue = NULL;
1310 last_comand_pointer = &jtag_command_queue;
1315 int jtag_execute_queue(void)
1317 int retval=interface_jtag_execute_queue();
1318 if (retval==ERROR_OK)
1322 jtag_error=ERROR_OK;
1326 int jtag_reset_callback(enum jtag_event event, void *priv)
1328 jtag_device_t *device = priv;
1332 if (event == JTAG_TRST_ASSERTED)
1334 buf_set_ones(device->cur_instr, device->ir_length);
1341 void jtag_sleep(u32 us)
1346 /* Try to examine chain layout according to IEEE 1149.1 §12
1348 int jtag_examine_chain()
1350 jtag_device_t *device = jtag_devices;
1352 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1355 int device_count = 0;
1356 u8 zero_check = 0x0;
1357 u8 one_check = 0xff;
1360 field.num_bits = sizeof(idcode_buffer) * 8;
1361 field.out_value = idcode_buffer;
1362 field.out_mask = NULL;
1363 field.in_value = idcode_buffer;
1364 field.in_check_value = NULL;
1365 field.in_check_mask = NULL;
1366 field.in_handler = NULL;
1367 field.in_handler_priv = NULL;
1369 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1371 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1374 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1375 jtag_execute_queue();
1377 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1379 zero_check |= idcode_buffer[i];
1380 one_check &= idcode_buffer[i];
1383 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1384 if ((zero_check == 0x00) || (one_check == 0xff))
1386 ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1387 return ERROR_JTAG_INIT_FAILED;
1390 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1392 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1393 if ((idcode & 1) == 0)
1395 /* LSB must not be 0, this indicates a device in bypass */
1406 if (idcode == 0x000000FF)
1408 /* End of chain (invalid manufacturer ID) */
1414 device->idcode = idcode;
1415 device = device->next;
1419 manufacturer = (idcode & 0xffe) >> 1;
1420 part = (idcode & 0xffff000) >> 12;
1421 version = (idcode & 0xf0000000) >> 28;
1423 INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1424 idcode, manufacturer, part, version);
1430 /* see if number of discovered devices matches configuration */
1431 if (device_count != jtag_num_devices)
1433 ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1434 device_count, jtag_num_devices);
1435 ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1436 return ERROR_JTAG_INIT_FAILED;
1442 int jtag_validate_chain()
1444 jtag_device_t *device = jtag_devices;
1445 int total_ir_length = 0;
1452 total_ir_length += device->ir_length;
1453 device = device->next;
1456 total_ir_length += 2;
1457 ir_test = malloc(CEIL(total_ir_length, 8));
1458 buf_set_ones(ir_test, total_ir_length);
1461 field.num_bits = total_ir_length;
1462 field.out_value = ir_test;
1463 field.out_mask = NULL;
1464 field.in_value = ir_test;
1465 field.in_check_value = NULL;
1466 field.in_check_mask = NULL;
1467 field.in_handler = NULL;
1468 field.in_handler_priv = NULL;
1470 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1471 jtag_execute_queue();
1473 device = jtag_devices;
1476 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1478 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1479 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1482 return ERROR_JTAG_INIT_FAILED;
1484 chain_pos += device->ir_length;
1485 device = device->next;
1488 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1490 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1491 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1494 return ERROR_JTAG_INIT_FAILED;
1502 int jtag_register_commands(struct command_context_s *cmd_ctx)
1504 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1505 COMMAND_CONFIG, NULL);
1506 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1507 COMMAND_ANY, "set jtag speed (if supported) <speed>");
1508 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1509 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1510 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1511 COMMAND_CONFIG, NULL);
1512 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1513 COMMAND_CONFIG, NULL);
1514 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1515 COMMAND_CONFIG, NULL);
1517 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1518 COMMAND_EXEC, "print current scan chain configuration");
1520 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1521 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1522 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1523 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1524 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1525 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1526 register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
1527 COMMAND_EXEC, "move to current endstate or [tap_state]");
1528 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1529 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1530 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1531 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1533 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1534 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1538 int jtag_interface_init(struct command_context_s *cmd_ctx)
1540 if (!jtag_interface)
1542 /* nothing was previously specified by "interface" command */
1543 ERROR("JTAG interface has to be specified, see \"interface\" command");
1544 return ERROR_JTAG_INVALID_INTERFACE;
1547 if (jtag_interface->init() != ERROR_OK)
1548 return ERROR_JTAG_INIT_FAILED;
1550 jtag = jtag_interface;
1554 int jtag_init(struct command_context_s *cmd_ctx)
1556 int validate_tries = 0;
1557 jtag_device_t *device;
1561 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1562 return ERROR_JTAG_INIT_FAILED;
1564 device = jtag_devices;
1565 jtag_ir_scan_size = 0;
1566 jtag_num_devices = 0;
1567 while (device != NULL)
1569 jtag_ir_scan_size += device->ir_length;
1571 device = device->next;
1574 jtag_add_statemove(TAP_TLR);
1575 jtag_execute_queue();
1577 /* examine chain first, as this could discover the real chain layout */
1578 if (jtag_examine_chain() != ERROR_OK)
1580 ERROR("trying to validate configured JTAG chain anyway...");
1583 while (jtag_validate_chain() != ERROR_OK)
1586 if (validate_tries > 5)
1588 ERROR("Could not validate JTAG chain, exit");
1589 return ERROR_JTAG_INVALID_INTERFACE;
1598 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1602 /* check whether the interface is already configured */
1605 WARNING("Interface already configured, ignoring");
1609 /* interface name is a mandatory argument */
1610 if (argc < 1 || args[0][0] == '\0')
1612 return ERROR_COMMAND_SYNTAX_ERROR;
1615 for (i=0; jtag_interfaces[i]; i++)
1617 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1619 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1622 jtag_interface = jtag_interfaces[i];
1627 /* no valid interface was found (i.e. the configuration option,
1628 * didn't match one of the compiled-in interfaces
1630 ERROR("No valid jtag interface found (%s)", args[0]);
1631 ERROR("compiled-in jtag interfaces:");
1632 for (i = 0; jtag_interfaces[i]; i++)
1634 ERROR("%i: %s", i, jtag_interfaces[i]->name);
1637 return ERROR_JTAG_INVALID_INTERFACE;
1640 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1642 jtag_device_t **last_device_p = &jtag_devices;
1646 while ((*last_device_p)->next)
1647 last_device_p = &((*last_device_p)->next);
1648 last_device_p = &((*last_device_p)->next);
1654 *last_device_p = malloc(sizeof(jtag_device_t));
1655 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1657 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1658 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1659 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1660 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1662 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1663 (*last_device_p)->bypass = 1;
1664 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1666 (*last_device_p)->next = NULL;
1668 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1675 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1677 jtag_device_t *device = jtag_devices;
1678 int device_count = 0;
1682 u32 expected, expected_mask, cur_instr;
1683 expected = buf_get_u32(device->expected, 0, device->ir_length);
1684 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1685 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1686 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);
1687 device = device->next;
1694 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1698 if (strcmp(args[0], "none") == 0)
1699 jtag_reset_config = RESET_NONE;
1700 else if (strcmp(args[0], "trst_only") == 0)
1701 jtag_reset_config = RESET_HAS_TRST;
1702 else if (strcmp(args[0], "srst_only") == 0)
1703 jtag_reset_config = RESET_HAS_SRST;
1704 else if (strcmp(args[0], "trst_and_srst") == 0)
1705 jtag_reset_config = RESET_TRST_AND_SRST;
1708 ERROR("invalid reset_config argument, defaulting to none");
1709 jtag_reset_config = RESET_NONE;
1710 return ERROR_INVALID_ARGUMENTS;
1716 if (strcmp(args[1], "srst_pulls_trst") == 0)
1717 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1718 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1719 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1720 else if (strcmp(args[1], "combined") == 0)
1721 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1722 else if (strcmp(args[1], "separate") == 0)
1723 jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
1726 ERROR("invalid reset_config argument, defaulting to none");
1727 jtag_reset_config = RESET_NONE;
1728 return ERROR_INVALID_ARGUMENTS;
1734 if (strcmp(args[2], "trst_open_drain") == 0)
1735 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1736 else if (strcmp(args[2], "trst_push_pull") == 0)
1737 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1740 ERROR("invalid reset_config argument, defaulting to none");
1741 jtag_reset_config = RESET_NONE;
1742 return ERROR_INVALID_ARGUMENTS;
1748 if (strcmp(args[3], "srst_push_pull") == 0)
1749 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1750 else if (strcmp(args[3], "srst_open_drain") == 0)
1751 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1754 ERROR("invalid reset_config argument, defaulting to none");
1755 jtag_reset_config = RESET_NONE;
1756 return ERROR_INVALID_ARGUMENTS;
1763 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1767 ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1772 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1778 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1782 ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1787 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1793 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1796 command_print(cmd_ctx, "jtag_speed: %i", jtag_speed);
1800 jtag_speed = strtoul(args[0], NULL, 0);
1801 /* this command can be called during CONFIG,
1802 * in which case jtag isn't initialized */
1804 jtag->speed(jtag_speed);
1810 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1812 enum tap_state state;
1816 return ERROR_COMMAND_SYNTAX_ERROR;
1820 for (state = 0; state < 16; state++)
1822 if (strcmp(args[0], tap_state_strings[state]) == 0)
1824 jtag_add_end_state(state);
1825 jtag_execute_queue();
1829 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[end_state]);
1834 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1841 return ERROR_COMMAND_SYNTAX_ERROR;
1844 if (args[0][0] == '1')
1846 else if (args[0][0] == '0')
1850 return ERROR_COMMAND_SYNTAX_ERROR;
1853 if (args[1][0] == '1')
1855 else if (args[1][0] == '0')
1859 return ERROR_COMMAND_SYNTAX_ERROR;
1862 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1863 return ERROR_JTAG_INIT_FAILED;
1865 jtag_add_reset(trst, srst);
1866 jtag_execute_queue();
1871 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1875 return ERROR_COMMAND_SYNTAX_ERROR;
1878 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1879 jtag_execute_queue();
1885 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1887 enum tap_state state;
1892 for (state = 0; state < 16; state++)
1894 if (strcmp(args[0], tap_state_strings[state]) == 0)
1901 jtag_add_statemove(state);
1902 jtag_execute_queue();
1908 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1911 scan_field_t *fields;
1913 if ((argc < 2) || (argc % 2))
1915 return ERROR_COMMAND_SYNTAX_ERROR;
1918 fields = malloc(sizeof(scan_field_t) * argc / 2);
1920 for (i = 0; i < argc / 2; i++)
1922 int device = strtoul(args[i*2], NULL, 0);
1923 int field_size = jtag_get_device(device)->ir_length;
1924 fields[i].device = device;
1925 fields[i].out_value = malloc(CEIL(field_size, 8));
1926 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1927 fields[i].out_mask = NULL;
1928 fields[i].in_value = NULL;
1929 fields[i].in_check_mask = NULL;
1930 fields[i].in_handler = NULL;
1931 fields[i].in_handler_priv = NULL;
1934 jtag_add_ir_scan(argc / 2, fields, -1);
1935 jtag_execute_queue();
1937 for (i = 0; i < argc / 2; i++)
1938 free(fields[i].out_value);
1945 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1947 scan_field_t *fields;
1949 int field_count = 0;
1953 if ((argc < 2) || (argc % 2))
1955 return ERROR_COMMAND_SYNTAX_ERROR;
1958 for (i = 0; i < argc; i+=2)
1960 var = get_var_by_namenum(args[i+1]);
1963 num_fields += var->num_fields;
1967 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1972 fields = malloc(sizeof(scan_field_t) * num_fields);
1974 for (i = 0; i < argc; i+=2)
1976 var = get_var_by_namenum(args[i+1]);
1978 for (j = 0; j < var->num_fields; j++)
1980 fields[field_count].device = strtol(args[i], NULL, 0);
1981 fields[field_count].num_bits = var->fields[j].num_bits;
1982 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1983 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1984 fields[field_count].out_mask = NULL;
1985 fields[field_count].in_value = fields[field_count].out_value;
1986 fields[field_count].in_check_mask = NULL;
1987 fields[field_count].in_check_value = NULL;
1988 fields[field_count].in_handler = field_le_to_host;
1989 fields[field_count++].in_handler_priv = &(var->fields[j]);
1993 jtag_add_dr_scan(num_fields, fields, -1);
1994 jtag_execute_queue();
1996 for (i = 0; i < argc / 2; i++)
1997 free(fields[i].out_value);
2004 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2008 if (strcmp(args[0], "enable") == 0)
2010 jtag_verify_capture_ir = 1;
2012 else if (strcmp(args[0], "disable") == 0)
2014 jtag_verify_capture_ir = 0;
2017 return ERROR_COMMAND_SYNTAX_ERROR;
2019 } else if (argc != 0)
2021 return ERROR_COMMAND_SYNTAX_ERROR;
2024 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");