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 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[] =
109 "JTAG controller reset(tms or TRST)"
112 enum tap_state end_state = TAP_TLR;
113 enum tap_state cur_state = TAP_TLR;
117 jtag_command_t *jtag_command_queue = NULL;
118 jtag_command_t **last_comand_pointer = &jtag_command_queue;
119 jtag_device_t *jtag_devices = NULL;
120 int jtag_num_devices = 0;
121 int jtag_ir_scan_size = 0;
122 enum reset_types jtag_reset_config = RESET_NONE;
123 enum tap_state cmd_queue_end_state = TAP_TLR;
124 enum tap_state cmd_queue_cur_state = TAP_TLR;
126 int jtag_verify_capture_ir = 1;
128 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
129 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
130 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
132 /* maximum number of JTAG devices expected in the chain
134 #define JTAG_MAX_CHAIN_SIZE 20
136 /* callbacks to inform high-level handlers about JTAG state changes */
137 jtag_event_callback_t *jtag_event_callbacks;
140 static int speed1 = 0, speed2 = 0;
141 /* flag if the kHz speed was defined */
142 static int hasKHz = 0;
144 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
147 #if BUILD_ECOSBOARD == 1
148 extern jtag_interface_t eCosBoard_interface;
151 #if BUILD_PARPORT == 1
152 extern jtag_interface_t parport_interface;
155 #if BUILD_FT2232_FTD2XX == 1
156 extern jtag_interface_t ft2232_interface;
159 #if BUILD_FT2232_LIBFTDI == 1
160 extern jtag_interface_t ft2232_interface;
163 #if BUILD_AMTJTAGACCEL == 1
164 extern jtag_interface_t amt_jtagaccel_interface;
167 #if BUILD_EP93XX == 1
168 extern jtag_interface_t ep93xx_interface;
171 #if BUILD_AT91RM9200 == 1
172 extern jtag_interface_t at91rm9200_interface;
175 #if BUILD_GW16012 == 1
176 extern jtag_interface_t gw16012_interface;
179 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
180 extern jtag_interface_t presto_interface;
183 #if BUILD_USBPROG == 1
184 extern jtag_interface_t usbprog_interface;
187 jtag_interface_t *jtag_interfaces[] = {
188 #if BUILD_ECOSBOARD == 1
189 &eCosBoard_interface,
191 #if BUILD_PARPORT == 1
194 #if BUILD_FT2232_FTD2XX == 1
197 #if BUILD_FT2232_LIBFTDI == 1
200 #if BUILD_AMTJTAGACCEL == 1
201 &amt_jtagaccel_interface,
203 #if BUILD_EP93XX == 1
206 #if BUILD_AT91RM9200 == 1
207 &at91rm9200_interface,
209 #if BUILD_GW16012 == 1
212 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
215 #if BUILD_USBPROG == 1
221 jtag_interface_t *jtag = NULL;
224 jtag_interface_t *jtag_interface = NULL;
226 int jtag_speed_post_reset = 0;
229 /* forward declarations */
230 void jtag_add_pathmove(int num_states, enum tap_state *path);
231 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
232 void jtag_add_end_state(enum tap_state endstate);
233 void jtag_add_sleep(u32 us);
234 int jtag_execute_queue(void);
235 int jtag_cancel_queue(void);
238 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
239 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
240 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
241 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
242 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
243 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
244 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
246 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
248 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
249 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
250 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
251 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
252 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
254 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
256 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
258 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
260 if (callback == NULL)
262 return ERROR_INVALID_ARGUMENTS;
267 while ((*callbacks_p)->next)
268 callbacks_p = &((*callbacks_p)->next);
269 callbacks_p = &((*callbacks_p)->next);
272 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
273 (*callbacks_p)->callback = callback;
274 (*callbacks_p)->priv = priv;
275 (*callbacks_p)->next = NULL;
280 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
282 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
284 if (callback == NULL)
286 return ERROR_INVALID_ARGUMENTS;
291 jtag_event_callback_t **next = &((*callbacks_p)->next);
292 if ((*callbacks_p)->callback == callback)
295 *callbacks_p = *next;
303 int jtag_call_event_callbacks(enum jtag_event event)
305 jtag_event_callback_t *callback = jtag_event_callbacks;
307 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
311 callback->callback(event, callback->priv);
312 callback = callback->next;
318 /* returns a pointer to the pointer of the last command in queue
319 * this may be a pointer to the root pointer (jtag_command_queue)
320 * or to the next member of the last but one command
322 jtag_command_t** jtag_get_last_command_p(void)
324 /* jtag_command_t *cmd = jtag_command_queue;
330 return &jtag_command_queue;
334 return last_comand_pointer;
337 /* returns a pointer to the n-th device in the scan chain */
338 jtag_device_t* jtag_get_device(int num)
340 jtag_device_t *device = jtag_devices;
347 device = device->next;
351 LOG_ERROR("jtag device number %d not defined", num);
355 void* cmd_queue_alloc(size_t size)
357 cmd_queue_page_t **p_page = &cmd_queue_pages;
362 while ((*p_page)->next)
363 p_page = &((*p_page)->next);
364 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
365 p_page = &((*p_page)->next);
370 *p_page = malloc(sizeof(cmd_queue_page_t));
372 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
373 (*p_page)->next = NULL;
376 offset = (*p_page)->used;
377 (*p_page)->used += size;
379 u8 *t=(u8 *)((*p_page)->address);
383 void cmd_queue_free()
385 cmd_queue_page_t *page = cmd_queue_pages;
389 cmd_queue_page_t *last = page;
395 cmd_queue_pages = NULL;
398 static void jtag_prelude1()
402 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
403 jtag_error=ERROR_JTAG_TRST_ASSERTED;
407 if (cmd_queue_end_state == TAP_TLR)
408 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
411 static void jtag_prelude(enum tap_state state)
416 jtag_add_end_state(state);
418 cmd_queue_cur_state = cmd_queue_end_state;
421 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
425 int retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
426 if (retval!=ERROR_OK)
430 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
432 jtag_command_t **last_cmd;
433 jtag_device_t *device;
438 last_cmd = jtag_get_last_command_p();
440 /* allocate memory for a new list member */
441 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
442 (*last_cmd)->next = NULL;
443 last_comand_pointer = &((*last_cmd)->next);
444 (*last_cmd)->type = JTAG_SCAN;
446 /* allocate memory for ir scan command */
447 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
448 (*last_cmd)->cmd.scan->ir_scan = 1;
449 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
450 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
451 (*last_cmd)->cmd.scan->end_state = state;
453 for (i = 0; i < jtag_num_devices; i++)
456 device = jtag_get_device(i);
457 scan_size = device->ir_length;
458 (*last_cmd)->cmd.scan->fields[i].device = i;
459 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
460 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
461 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
463 /* search the list */
464 for (j = 0; j < num_fields; j++)
466 if (i == fields[j].device)
469 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
470 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
472 if (jtag_verify_capture_ir)
474 if (fields[j].in_handler==NULL)
476 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
479 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
480 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
481 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
482 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
493 /* if a device isn't listed, set it to BYPASS */
494 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
495 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
500 /* update device information */
501 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
507 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state 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 int retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
558 if (retval!=ERROR_OK)
562 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
565 int bypass_devices = 0;
569 jtag_command_t **last_cmd = jtag_get_last_command_p();
570 jtag_device_t *device = jtag_devices;
572 /* count devices in bypass */
577 device = device->next;
580 /* allocate memory for a new list member */
581 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
582 last_comand_pointer = &((*last_cmd)->next);
583 (*last_cmd)->next = NULL;
584 (*last_cmd)->type = JTAG_SCAN;
586 /* allocate memory for dr scan command */
587 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
588 (*last_cmd)->cmd.scan->ir_scan = 0;
589 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
590 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
591 (*last_cmd)->cmd.scan->end_state = state;
593 for (i = 0; i < jtag_num_devices; i++)
596 (*last_cmd)->cmd.scan->fields[field_count].device = i;
598 for (j = 0; j < num_fields; j++)
600 if (i == fields[j].device)
603 scan_size = fields[j].num_bits;
604 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
605 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
606 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
607 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
608 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
609 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
610 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
611 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
616 #ifdef _DEBUG_JTAG_IO_
617 /* if a device isn't listed, the BYPASS register should be selected */
618 if (!jtag_get_device(i)->bypass)
620 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
624 /* program the scan field to 1 bit length, and ignore it's value */
625 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
626 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
627 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
628 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
629 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
630 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
631 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
632 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
636 #ifdef _DEBUG_JTAG_IO_
637 /* if a device is listed, the BYPASS register must not be selected */
638 if (jtag_get_device(i)->bypass)
640 LOG_ERROR("BUG: scan data for a device in BYPASS");
649 void MINIDRIVER(interface_jtag_add_dr_out)(int device_num,
653 enum tap_state end_state)
658 int bypass_devices = 0;
660 jtag_command_t **last_cmd = jtag_get_last_command_p();
661 jtag_device_t *device = jtag_devices;
662 /* count devices in bypass */
667 device = device->next;
670 /* allocate memory for a new list member */
671 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
672 last_comand_pointer = &((*last_cmd)->next);
673 (*last_cmd)->next = NULL;
674 (*last_cmd)->type = JTAG_SCAN;
676 /* allocate memory for dr scan command */
677 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
678 (*last_cmd)->cmd.scan->ir_scan = 0;
679 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
680 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
681 (*last_cmd)->cmd.scan->end_state = end_state;
683 for (i = 0; i < jtag_num_devices; i++)
685 (*last_cmd)->cmd.scan->fields[field_count].device = i;
690 #ifdef _DEBUG_JTAG_IO_
691 /* if a device is listed, the BYPASS register must not be selected */
692 if (jtag_get_device(i)->bypass)
694 LOG_ERROR("BUG: scan data for a device in BYPASS");
698 for (j = 0; j < num_fields; j++)
701 scan_size = num_bits[j];
702 buf_set_u32(out_value, 0, scan_size, value[j]);
703 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
704 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
705 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
706 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
707 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
708 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
709 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
710 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
714 #ifdef _DEBUG_JTAG_IO_
715 /* if a device isn't listed, the BYPASS register should be selected */
716 if (!jtag_get_device(i)->bypass)
718 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
722 /* program the scan field to 1 bit length, and ignore it's value */
723 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
724 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
725 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
726 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
727 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
728 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
729 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
730 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
738 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
742 int retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
743 if (retval!=ERROR_OK)
747 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
750 jtag_command_t **last_cmd = jtag_get_last_command_p();
752 /* allocate memory for a new list member */
753 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
754 last_comand_pointer = &((*last_cmd)->next);
755 (*last_cmd)->next = NULL;
756 (*last_cmd)->type = JTAG_SCAN;
758 /* allocate memory for scan command */
759 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
760 (*last_cmd)->cmd.scan->ir_scan = 0;
761 (*last_cmd)->cmd.scan->num_fields = num_fields;
762 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
763 (*last_cmd)->cmd.scan->end_state = state;
765 for (i = 0; i < num_fields; i++)
767 int num_bits = fields[i].num_bits;
768 int num_bytes = CEIL(fields[i].num_bits, 8);
769 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
770 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
771 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
772 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
773 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
774 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
775 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
776 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
777 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
785 jtag_prelude(TAP_TLR);
788 retval=interface_jtag_add_tms();
789 if (retval!=ERROR_OK)
793 int MINIDRIVER(interface_jtag_add_tms)()
795 enum tap_state state = TAP_TLR;
796 jtag_command_t **last_cmd = jtag_get_last_command_p();
798 /* allocate memory for a new list member */
799 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
800 last_comand_pointer = &((*last_cmd)->next);
801 (*last_cmd)->next = NULL;
802 (*last_cmd)->type = JTAG_STATEMOVE;
804 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
805 (*last_cmd)->cmd.statemove->end_state = state;
811 void jtag_add_pathmove(int num_states, enum tap_state *path)
813 /* the last state has to be a stable state */
814 if (tap_move_map[path[num_states - 1]] == -1)
816 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
820 enum tap_state cur_state=cmd_queue_cur_state;
822 for (i=0; i<num_states; i++)
824 if ((tap_transitions[cur_state].low != path[i])&&
825 (tap_transitions[cur_state].high != path[i]))
827 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
835 cmd_queue_cur_state = path[num_states - 1];
837 int retval=interface_jtag_add_pathmove(num_states, path);
838 if (retval!=ERROR_OK)
843 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
845 jtag_command_t **last_cmd = jtag_get_last_command_p();
848 /* allocate memory for a new list member */
849 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
850 last_comand_pointer = &((*last_cmd)->next);
851 (*last_cmd)->next = NULL;
852 (*last_cmd)->type = JTAG_PATHMOVE;
854 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
855 (*last_cmd)->cmd.pathmove->num_states = num_states;
856 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
858 for (i = 0; i < num_states; i++)
859 (*last_cmd)->cmd.pathmove->path[i] = path[i];
864 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
866 jtag_command_t **last_cmd = jtag_get_last_command_p();
868 /* allocate memory for a new list member */
869 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
870 (*last_cmd)->next = NULL;
871 last_comand_pointer = &((*last_cmd)->next);
872 (*last_cmd)->type = JTAG_RUNTEST;
874 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
875 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
876 (*last_cmd)->cmd.runtest->end_state = state;
881 void jtag_add_runtest(int num_cycles, enum tap_state state)
885 /* executed by sw or hw fifo */
886 int retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
887 if (retval!=ERROR_OK)
891 void jtag_add_reset(int req_tms_or_trst, int req_srst)
893 int trst_with_tms = 0;
896 /* Make sure that jtag_reset_config allows the requested reset */
897 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
898 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tms_or_trst))
900 LOG_ERROR("BUG: requested reset would assert trst");
901 jtag_error=ERROR_FAIL;
905 /* if TRST pulls SRST, we reset with TAP T-L-R */
906 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tms_or_trst)) && (req_srst == 0))
911 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
913 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
914 jtag_error=ERROR_FAIL;
920 if (!trst_with_tms && (jtag_reset_config & RESET_HAS_TRST))
932 jtag_srst = req_srst;
934 retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
935 if (retval!=ERROR_OK)
943 LOG_DEBUG("SRST line asserted");
947 LOG_DEBUG("SRST line released");
948 if (jtag_nsrst_delay)
949 jtag_add_sleep(jtag_nsrst_delay * 1000);
954 LOG_DEBUG("JTAG reset with tms instead of TRST");
955 jtag_add_end_state(TAP_TLR);
957 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
963 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
964 * and inform possible listeners about this
966 LOG_DEBUG("TRST line asserted");
967 cmd_queue_cur_state = TAP_TLR;
968 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
972 if (jtag_ntrst_delay)
973 jtag_add_sleep(jtag_ntrst_delay * 1000);
977 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
979 jtag_command_t **last_cmd = jtag_get_last_command_p();
981 /* allocate memory for a new list member */
982 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
983 (*last_cmd)->next = NULL;
984 last_comand_pointer = &((*last_cmd)->next);
985 (*last_cmd)->type = JTAG_RESET;
987 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
988 (*last_cmd)->cmd.reset->trst = req_trst;
989 (*last_cmd)->cmd.reset->srst = req_srst;
995 void jtag_add_end_state(enum tap_state state)
997 cmd_queue_end_state = state;
998 if ((cmd_queue_end_state == TAP_SD)||(cmd_queue_end_state == TAP_SD))
1000 LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
1004 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1006 jtag_command_t **last_cmd = jtag_get_last_command_p();
1008 /* allocate memory for a new list member */
1009 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1010 (*last_cmd)->next = NULL;
1011 last_comand_pointer = &((*last_cmd)->next);
1012 (*last_cmd)->type = JTAG_SLEEP;
1014 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1015 (*last_cmd)->cmd.sleep->us = us;
1020 void jtag_add_sleep(u32 us)
1022 int retval=interface_jtag_add_sleep(us);
1023 if (retval!=ERROR_OK)
1028 int jtag_scan_size(scan_command_t *cmd)
1033 /* count bits in scan command */
1034 for (i = 0; i < cmd->num_fields; i++)
1036 bit_count += cmd->fields[i].num_bits;
1042 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1047 bit_count = jtag_scan_size(cmd);
1048 *buffer = malloc(CEIL(bit_count, 8));
1052 for (i = 0; i < cmd->num_fields; i++)
1054 if (cmd->fields[i].out_value)
1056 #ifdef _DEBUG_JTAG_IO_
1057 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1059 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1060 #ifdef _DEBUG_JTAG_IO_
1061 LOG_DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1066 bit_count += cmd->fields[i].num_bits;
1073 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1079 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1082 for (i = 0; i < cmd->num_fields; i++)
1084 /* if neither in_value nor in_handler
1085 * are specified we don't have to examine this field
1087 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1089 int num_bits = cmd->fields[i].num_bits;
1090 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1092 #ifdef _DEBUG_JTAG_IO_
1095 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1096 LOG_DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1100 if (cmd->fields[i].in_value)
1102 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1104 if (cmd->fields[i].in_handler)
1106 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1108 LOG_WARNING("in_handler reported a failed check");
1109 retval = ERROR_JTAG_QUEUE_FAILED;
1114 /* no in_value specified, but a handler takes care of the scanned data */
1115 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1117 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1119 /* We're going to call the error:handler later, but if the in_handler
1120 * reported an error we report this failure upstream
1122 LOG_WARNING("in_handler reported a failed check");
1123 retval = ERROR_JTAG_QUEUE_FAILED;
1129 bit_count += cmd->fields[i].num_bits;
1135 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1137 int retval = ERROR_OK;
1138 int num_bits = field->num_bits;
1140 int compare_failed = 0;
1142 if (field->in_check_mask)
1143 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1145 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1149 /* An error handler could have caught the failing check
1150 * only report a problem when there wasn't a handler, or if the handler
1151 * acknowledged the error
1155 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1156 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1158 if (field->in_check_mask)
1160 char *in_check_mask_char;
1161 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1162 LOG_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);
1163 free(in_check_mask_char);
1167 LOG_WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
1170 free(captured_char);
1171 free(in_check_value_char);
1173 retval = ERROR_JTAG_QUEUE_FAILED;
1181 set up checking of this field using the in_handler. The values passed in must be valid until
1182 after jtag_execute() has completed.
1184 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1187 field->in_handler = jtag_check_value;
1189 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1190 field->in_handler_priv = NULL; /* this will be filled in at the invocation site to point to the field duplicate */
1191 field->in_check_value = value;
1192 field->in_check_mask = mask;
1195 enum scan_type jtag_scan_type(scan_command_t *cmd)
1200 for (i = 0; i < cmd->num_fields; i++)
1202 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1204 if (cmd->fields[i].out_value)
1211 int MINIDRIVER(interface_jtag_execute_queue)(void)
1215 retval = jtag->execute_queue();
1219 jtag_command_queue = NULL;
1220 last_comand_pointer = &jtag_command_queue;
1225 int jtag_execute_queue(void)
1227 int retval=interface_jtag_execute_queue();
1228 if (retval==ERROR_OK)
1232 jtag_error=ERROR_OK;
1236 int jtag_reset_callback(enum jtag_event event, void *priv)
1238 jtag_device_t *device = priv;
1242 if (event == JTAG_TRST_ASSERTED)
1244 buf_set_ones(device->cur_instr, device->ir_length);
1251 void jtag_sleep(u32 us)
1256 /* Try to examine chain layout according to IEEE 1149.1 §12
1258 int jtag_examine_chain()
1260 jtag_device_t *device = jtag_devices;
1262 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1265 int device_count = 0;
1266 u8 zero_check = 0x0;
1267 u8 one_check = 0xff;
1270 field.num_bits = sizeof(idcode_buffer) * 8;
1271 field.out_value = idcode_buffer;
1272 field.out_mask = NULL;
1273 field.in_value = idcode_buffer;
1274 field.in_check_value = NULL;
1275 field.in_check_mask = NULL;
1276 field.in_handler = NULL;
1277 field.in_handler_priv = NULL;
1279 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1281 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1284 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1285 jtag_execute_queue();
1287 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1289 zero_check |= idcode_buffer[i];
1290 one_check &= idcode_buffer[i];
1293 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1294 if ((zero_check == 0x00) || (one_check == 0xff))
1296 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1297 return ERROR_JTAG_INIT_FAILED;
1300 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1302 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1303 if ((idcode & 1) == 0)
1305 /* LSB must not be 0, this indicates a device in bypass */
1316 if (idcode == 0x000000FF)
1318 /* End of chain (invalid manufacturer ID) */
1324 device->idcode = idcode;
1325 device = device->next;
1329 manufacturer = (idcode & 0xffe) >> 1;
1330 part = (idcode & 0xffff000) >> 12;
1331 version = (idcode & 0xf0000000) >> 28;
1333 LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1334 idcode, manufacturer, part, version);
1340 /* see if number of discovered devices matches configuration */
1341 if (device_count != jtag_num_devices)
1343 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1344 device_count, jtag_num_devices);
1345 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1346 return ERROR_JTAG_INIT_FAILED;
1352 int jtag_validate_chain()
1354 jtag_device_t *device = jtag_devices;
1355 int total_ir_length = 0;
1362 total_ir_length += device->ir_length;
1363 device = device->next;
1366 total_ir_length += 2;
1367 ir_test = malloc(CEIL(total_ir_length, 8));
1368 buf_set_ones(ir_test, total_ir_length);
1371 field.num_bits = total_ir_length;
1372 field.out_value = ir_test;
1373 field.out_mask = NULL;
1374 field.in_value = ir_test;
1375 field.in_check_value = NULL;
1376 field.in_check_mask = NULL;
1377 field.in_handler = NULL;
1378 field.in_handler_priv = NULL;
1380 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1381 jtag_execute_queue();
1383 device = jtag_devices;
1386 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1388 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1389 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1392 return ERROR_JTAG_INIT_FAILED;
1394 chain_pos += device->ir_length;
1395 device = device->next;
1398 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1400 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1401 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1404 return ERROR_JTAG_INIT_FAILED;
1412 int jtag_register_commands(struct command_context_s *cmd_ctx)
1414 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1415 COMMAND_CONFIG, NULL);
1416 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1417 COMMAND_ANY, "set jtag speed (if supported) <reset speed> [<post reset speed, default value is reset speed>]");
1418 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1419 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1420 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1421 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1422 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1423 COMMAND_CONFIG, NULL);
1424 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1425 COMMAND_CONFIG, NULL);
1426 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1427 COMMAND_CONFIG, NULL);
1429 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1430 COMMAND_EXEC, "print current scan chain configuration");
1432 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1433 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1434 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1435 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1436 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1437 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1438 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1439 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1440 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1441 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1443 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1444 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1448 int jtag_interface_init(struct command_context_s *cmd_ctx)
1450 if (!jtag_interface)
1452 /* nothing was previously specified by "interface" command */
1453 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1454 return ERROR_JTAG_INVALID_INTERFACE;
1458 /*stay on "reset speed"*/
1459 if (jtag_interface->khz(speed1, &speed1) == ERROR_OK)
1460 jtag_speed = speed1;
1461 if (jtag_interface->khz(speed2, &speed2) == ERROR_OK)
1462 jtag_speed_post_reset = speed2;
1466 if (jtag_interface->init() != ERROR_OK)
1467 return ERROR_JTAG_INIT_FAILED;
1471 jtag = jtag_interface;
1475 int jtag_init(struct command_context_s *cmd_ctx)
1477 int validate_tries = 0;
1478 jtag_device_t *device;
1482 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1483 return ERROR_JTAG_INIT_FAILED;
1485 device = jtag_devices;
1486 jtag_ir_scan_size = 0;
1487 jtag_num_devices = 0;
1488 while (device != NULL)
1490 jtag_ir_scan_size += device->ir_length;
1492 device = device->next;
1496 jtag_execute_queue();
1498 /* examine chain first, as this could discover the real chain layout */
1499 if (jtag_examine_chain() != ERROR_OK)
1501 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1504 while (jtag_validate_chain() != ERROR_OK)
1508 if (validate_tries > 5)
1510 LOG_ERROR("Could not validate JTAG chain, exit");
1511 return ERROR_JTAG_INVALID_INTERFACE;
1520 static int default_khz(int khz, int *jtag_speed)
1522 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1526 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1530 /* check whether the interface is already configured */
1533 LOG_WARNING("Interface already configured, ignoring");
1537 /* interface name is a mandatory argument */
1538 if (argc < 1 || args[0][0] == '\0')
1540 return ERROR_COMMAND_SYNTAX_ERROR;
1543 for (i=0; jtag_interfaces[i]; i++)
1545 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1547 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1550 jtag_interface = jtag_interfaces[i];
1552 if (jtag_interface->khz == NULL)
1554 jtag_interface->khz = default_khz;
1560 /* no valid interface was found (i.e. the configuration option,
1561 * didn't match one of the compiled-in interfaces
1563 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
1564 LOG_ERROR("compiled-in jtag interfaces:");
1565 for (i = 0; jtag_interfaces[i]; i++)
1567 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
1570 return ERROR_JTAG_INVALID_INTERFACE;
1573 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1575 jtag_device_t **last_device_p = &jtag_devices;
1579 while ((*last_device_p)->next)
1580 last_device_p = &((*last_device_p)->next);
1581 last_device_p = &((*last_device_p)->next);
1587 *last_device_p = malloc(sizeof(jtag_device_t));
1588 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1590 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1591 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1592 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1593 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1595 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1596 (*last_device_p)->bypass = 1;
1597 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1599 (*last_device_p)->next = NULL;
1601 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1608 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1610 jtag_device_t *device = jtag_devices;
1611 int device_count = 0;
1615 u32 expected, expected_mask, cur_instr;
1616 expected = buf_get_u32(device->expected, 0, device->ir_length);
1617 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1618 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1619 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);
1620 device = device->next;
1627 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1630 return ERROR_COMMAND_SYNTAX_ERROR;
1634 if (strcmp(args[0], "none") == 0)
1635 jtag_reset_config = RESET_NONE;
1636 else if (strcmp(args[0], "trst_only") == 0)
1637 jtag_reset_config = RESET_HAS_TRST;
1638 else if (strcmp(args[0], "srst_only") == 0)
1639 jtag_reset_config = RESET_HAS_SRST;
1640 else if (strcmp(args[0], "trst_and_srst") == 0)
1641 jtag_reset_config = RESET_TRST_AND_SRST;
1644 LOG_ERROR("invalid reset_config argument, defaulting to none");
1645 jtag_reset_config = RESET_NONE;
1646 return ERROR_INVALID_ARGUMENTS;
1652 if (strcmp(args[1], "separate") == 0)
1654 /* seperate reset lines - default */
1657 if (strcmp(args[1], "srst_pulls_trst") == 0)
1658 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1659 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1660 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1661 else if (strcmp(args[1], "combined") == 0)
1662 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1665 LOG_ERROR("invalid reset_config argument, defaulting to none");
1666 jtag_reset_config = RESET_NONE;
1667 return ERROR_INVALID_ARGUMENTS;
1674 if (strcmp(args[2], "trst_open_drain") == 0)
1675 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1676 else if (strcmp(args[2], "trst_push_pull") == 0)
1677 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1680 LOG_ERROR("invalid reset_config argument, defaulting to none");
1681 jtag_reset_config = RESET_NONE;
1682 return ERROR_INVALID_ARGUMENTS;
1688 if (strcmp(args[3], "srst_push_pull") == 0)
1689 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1690 else if (strcmp(args[3], "srst_open_drain") == 0)
1691 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1694 LOG_ERROR("invalid reset_config argument, defaulting to none");
1695 jtag_reset_config = RESET_NONE;
1696 return ERROR_INVALID_ARGUMENTS;
1703 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1707 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1712 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1718 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1722 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1727 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1733 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1736 if ((argc<1) || (argc>2))
1737 return ERROR_COMMAND_SYNTAX_ERROR;
1739 LOG_DEBUG("handle jtag speed");
1742 cur_speed = jtag_speed = jtag_speed_post_reset = strtoul(args[0], NULL, 0);
1744 cur_speed = jtag_speed_post_reset = strtoul(args[1], NULL, 0);
1746 /* this command can be called during CONFIG,
1747 * in which case jtag isn't initialized */
1749 jtag->speed(cur_speed);
1754 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1756 LOG_DEBUG("handle jtag khz");
1758 if ((argc<1) || (argc>2))
1759 return ERROR_COMMAND_SYNTAX_ERROR;
1762 speed1 = speed2 = strtoul(args[0], NULL, 0);
1764 speed2 = strtoul(args[1], NULL, 0);
1769 LOG_DEBUG("have interface set up");
1770 int speed_div1, speed_div2;
1771 if (jtag->khz(speed1, &speed_div1)!=ERROR_OK)
1773 if (jtag->khz(speed2, &speed_div2)!=ERROR_OK)
1777 cur_speed = jtag_speed = jtag_speed_post_reset = speed_div1;
1779 cur_speed = jtag_speed_post_reset = speed_div2;
1781 jtag->speed(cur_speed);
1791 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1793 enum tap_state state;
1797 return ERROR_COMMAND_SYNTAX_ERROR;
1801 for (state = 0; state < 16; state++)
1803 if (strcmp(args[0], tap_state_strings[state]) == 0)
1805 jtag_add_end_state(state);
1806 jtag_execute_queue();
1810 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[end_state]);
1815 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1822 return ERROR_COMMAND_SYNTAX_ERROR;
1825 if (args[0][0] == '1')
1827 else if (args[0][0] == '0')
1831 return ERROR_COMMAND_SYNTAX_ERROR;
1834 if (args[1][0] == '1')
1836 else if (args[1][0] == '0')
1840 return ERROR_COMMAND_SYNTAX_ERROR;
1843 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1844 return ERROR_JTAG_INIT_FAILED;
1846 jtag_add_reset(trst, srst);
1847 jtag_execute_queue();
1852 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1856 return ERROR_COMMAND_SYNTAX_ERROR;
1859 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1860 jtag_execute_queue();
1867 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1870 scan_field_t *fields;
1872 if ((argc < 2) || (argc % 2))
1874 return ERROR_COMMAND_SYNTAX_ERROR;
1877 fields = malloc(sizeof(scan_field_t) * argc / 2);
1879 for (i = 0; i < argc / 2; i++)
1881 int device = strtoul(args[i*2], NULL, 0);
1882 int field_size = jtag_get_device(device)->ir_length;
1883 fields[i].device = device;
1884 fields[i].out_value = malloc(CEIL(field_size, 8));
1885 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1886 fields[i].out_mask = NULL;
1887 fields[i].in_value = NULL;
1888 fields[i].in_check_mask = NULL;
1889 fields[i].in_handler = NULL;
1890 fields[i].in_handler_priv = NULL;
1893 jtag_add_ir_scan(argc / 2, fields, -1);
1894 jtag_execute_queue();
1896 for (i = 0; i < argc / 2; i++)
1897 free(fields[i].out_value);
1904 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1906 scan_field_t *fields;
1908 int field_count = 0;
1912 if ((argc < 2) || (argc % 2))
1914 return ERROR_COMMAND_SYNTAX_ERROR;
1917 for (i = 0; i < argc; i+=2)
1919 var = get_var_by_namenum(args[i+1]);
1922 num_fields += var->num_fields;
1926 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1931 fields = malloc(sizeof(scan_field_t) * num_fields);
1933 for (i = 0; i < argc; i+=2)
1935 var = get_var_by_namenum(args[i+1]);
1937 for (j = 0; j < var->num_fields; j++)
1939 fields[field_count].device = strtol(args[i], NULL, 0);
1940 fields[field_count].num_bits = var->fields[j].num_bits;
1941 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1942 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1943 fields[field_count].out_mask = NULL;
1944 fields[field_count].in_value = fields[field_count].out_value;
1945 fields[field_count].in_check_mask = NULL;
1946 fields[field_count].in_check_value = NULL;
1947 fields[field_count].in_handler = field_le_to_host;
1948 fields[field_count++].in_handler_priv = &(var->fields[j]);
1952 jtag_add_dr_scan(num_fields, fields, -1);
1953 jtag_execute_queue();
1955 for (i = 0; i < argc / 2; i++)
1956 free(fields[i].out_value);
1963 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1967 if (strcmp(args[0], "enable") == 0)
1969 jtag_verify_capture_ir = 1;
1971 else if (strcmp(args[0], "disable") == 0)
1973 jtag_verify_capture_ir = 0;
1976 return ERROR_COMMAND_SYNTAX_ERROR;
1978 } else if (argc != 0)
1980 return ERROR_COMMAND_SYNTAX_ERROR;
1983 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");