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;
139 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
142 #if BUILD_ECOSBOARD == 1
143 extern jtag_interface_t eCosBoard_interface;
146 #if BUILD_PARPORT == 1
147 extern jtag_interface_t parport_interface;
150 #if BUILD_FT2232_FTD2XX == 1
151 extern jtag_interface_t ft2232_interface;
154 #if BUILD_FT2232_LIBFTDI == 1
155 extern jtag_interface_t ft2232_interface;
158 #if BUILD_AMTJTAGACCEL == 1
159 extern jtag_interface_t amt_jtagaccel_interface;
162 #if BUILD_EP93XX == 1
163 extern jtag_interface_t ep93xx_interface;
166 #if BUILD_AT91RM9200 == 1
167 extern jtag_interface_t at91rm9200_interface;
170 #if BUILD_GW16012 == 1
171 extern jtag_interface_t gw16012_interface;
174 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
175 extern jtag_interface_t presto_interface;
178 #if BUILD_USBPROG == 1
179 extern jtag_interface_t usbprog_interface;
182 jtag_interface_t *jtag_interfaces[] = {
183 #if BUILD_ECOSBOARD == 1
184 &eCosBoard_interface,
186 #if BUILD_PARPORT == 1
189 #if BUILD_FT2232_FTD2XX == 1
192 #if BUILD_FT2232_LIBFTDI == 1
195 #if BUILD_AMTJTAGACCEL == 1
196 &amt_jtagaccel_interface,
198 #if BUILD_EP93XX == 1
201 #if BUILD_AT91RM9200 == 1
202 &at91rm9200_interface,
204 #if BUILD_GW16012 == 1
207 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
210 #if BUILD_USBPROG == 1
216 jtag_interface_t *jtag = NULL;
219 jtag_interface_t *jtag_interface = NULL;
221 int jtag_speed_post_reset = 0;
224 /* forward declarations */
225 void jtag_add_statemove(enum tap_state endstate);
226 void jtag_add_pathmove(int num_states, enum tap_state *path);
227 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
228 int jtag_add_reset(int trst, int srst);
229 void jtag_add_end_state(enum tap_state endstate);
230 void jtag_add_sleep(u32 us);
231 int jtag_execute_queue(void);
232 int jtag_cancel_queue(void);
235 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
236 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
237 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
238 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
239 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
240 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
241 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
243 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
245 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
246 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
247 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
248 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
249 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
250 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
252 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
254 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
256 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
258 if (callback == NULL)
260 return ERROR_INVALID_ARGUMENTS;
265 while ((*callbacks_p)->next)
266 callbacks_p = &((*callbacks_p)->next);
267 callbacks_p = &((*callbacks_p)->next);
270 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
271 (*callbacks_p)->callback = callback;
272 (*callbacks_p)->priv = priv;
273 (*callbacks_p)->next = NULL;
278 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
280 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
282 if (callback == NULL)
284 return ERROR_INVALID_ARGUMENTS;
289 jtag_event_callback_t **next = &((*callbacks_p)->next);
290 if ((*callbacks_p)->callback == callback)
293 *callbacks_p = *next;
301 int jtag_call_event_callbacks(enum jtag_event event)
303 jtag_event_callback_t *callback = jtag_event_callbacks;
305 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
309 callback->callback(event, callback->priv);
310 callback = callback->next;
316 /* returns a pointer to the pointer of the last command in queue
317 * this may be a pointer to the root pointer (jtag_command_queue)
318 * or to the next member of the last but one command
320 jtag_command_t** jtag_get_last_command_p(void)
322 /* jtag_command_t *cmd = jtag_command_queue;
328 return &jtag_command_queue;
332 return last_comand_pointer;
335 /* returns a pointer to the n-th device in the scan chain */
336 jtag_device_t* jtag_get_device(int num)
338 jtag_device_t *device = jtag_devices;
345 device = device->next;
349 LOG_ERROR("jtag device number %d not defined", num);
353 void* cmd_queue_alloc(size_t size)
355 cmd_queue_page_t **p_page = &cmd_queue_pages;
360 while ((*p_page)->next)
361 p_page = &((*p_page)->next);
362 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
363 p_page = &((*p_page)->next);
368 *p_page = malloc(sizeof(cmd_queue_page_t));
370 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
371 (*p_page)->next = NULL;
374 offset = (*p_page)->used;
375 (*p_page)->used += size;
377 u8 *t=(u8 *)((*p_page)->address);
381 void cmd_queue_free()
383 cmd_queue_page_t *page = cmd_queue_pages;
387 cmd_queue_page_t *last = page;
393 cmd_queue_pages = NULL;
396 static void jtag_prelude1()
400 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
401 jtag_error=ERROR_JTAG_TRST_ASSERTED;
405 if (cmd_queue_end_state == TAP_TLR)
406 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
409 static void jtag_prelude(enum tap_state state)
414 cmd_queue_end_state = state;
416 cmd_queue_cur_state = cmd_queue_end_state;
419 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
423 int retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
424 if (retval!=ERROR_OK)
428 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
430 jtag_command_t **last_cmd;
431 jtag_device_t *device;
436 last_cmd = jtag_get_last_command_p();
438 /* allocate memory for a new list member */
439 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
440 (*last_cmd)->next = NULL;
441 last_comand_pointer = &((*last_cmd)->next);
442 (*last_cmd)->type = JTAG_SCAN;
444 /* allocate memory for ir scan command */
445 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
446 (*last_cmd)->cmd.scan->ir_scan = 1;
447 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
448 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
449 (*last_cmd)->cmd.scan->end_state = state;
451 for (i = 0; i < jtag_num_devices; i++)
454 device = jtag_get_device(i);
455 scan_size = device->ir_length;
456 (*last_cmd)->cmd.scan->fields[i].device = i;
457 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
458 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
459 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
461 /* search the list */
462 for (j = 0; j < num_fields; j++)
464 if (i == fields[j].device)
467 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
468 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
470 if (jtag_verify_capture_ir)
472 if (fields[j].in_handler==NULL)
474 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
477 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
478 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
479 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
480 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
491 /* if a device isn't listed, set it to BYPASS */
492 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
493 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
498 /* update device information */
499 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
505 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
509 int retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
510 if (retval!=ERROR_OK)
514 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
517 jtag_command_t **last_cmd;
519 last_cmd = jtag_get_last_command_p();
521 /* allocate memory for a new list member */
522 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
523 (*last_cmd)->next = NULL;
524 last_comand_pointer = &((*last_cmd)->next);
525 (*last_cmd)->type = JTAG_SCAN;
527 /* allocate memory for ir scan command */
528 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
529 (*last_cmd)->cmd.scan->ir_scan = 1;
530 (*last_cmd)->cmd.scan->num_fields = num_fields;
531 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
532 (*last_cmd)->cmd.scan->end_state = state;
534 for (i = 0; i < num_fields; i++)
536 int num_bits = fields[i].num_bits;
537 int num_bytes = CEIL(fields[i].num_bits, 8);
538 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
539 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
540 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
541 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
542 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
543 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
544 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
545 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
546 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
551 void jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
555 int retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
556 if (retval!=ERROR_OK)
560 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
563 int bypass_devices = 0;
567 jtag_command_t **last_cmd = jtag_get_last_command_p();
568 jtag_device_t *device = jtag_devices;
570 /* count devices in bypass */
575 device = device->next;
578 /* allocate memory for a new list member */
579 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
580 last_comand_pointer = &((*last_cmd)->next);
581 (*last_cmd)->next = NULL;
582 (*last_cmd)->type = JTAG_SCAN;
584 /* allocate memory for dr scan command */
585 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
586 (*last_cmd)->cmd.scan->ir_scan = 0;
587 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
588 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
589 (*last_cmd)->cmd.scan->end_state = state;
591 for (i = 0; i < jtag_num_devices; i++)
594 (*last_cmd)->cmd.scan->fields[field_count].device = i;
596 for (j = 0; j < num_fields; j++)
598 if (i == fields[j].device)
601 scan_size = fields[j].num_bits;
602 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
603 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
604 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
605 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
606 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
607 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
608 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
609 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
614 #ifdef _DEBUG_JTAG_IO_
615 /* if a device isn't listed, the BYPASS register should be selected */
616 if (!jtag_get_device(i)->bypass)
618 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
622 /* program the scan field to 1 bit length, and ignore it's value */
623 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
624 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
625 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
626 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
627 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
628 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
629 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
630 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
634 #ifdef _DEBUG_JTAG_IO_
635 /* if a device is listed, the BYPASS register must not be selected */
636 if (jtag_get_device(i)->bypass)
638 LOG_ERROR("BUG: scan data for a device in BYPASS");
647 void MINIDRIVER(interface_jtag_add_dr_out)(int device_num,
651 enum tap_state end_state)
656 int bypass_devices = 0;
658 jtag_command_t **last_cmd = jtag_get_last_command_p();
659 jtag_device_t *device = jtag_devices;
660 /* count devices in bypass */
665 device = device->next;
668 /* allocate memory for a new list member */
669 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
670 last_comand_pointer = &((*last_cmd)->next);
671 (*last_cmd)->next = NULL;
672 (*last_cmd)->type = JTAG_SCAN;
674 /* allocate memory for dr scan command */
675 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
676 (*last_cmd)->cmd.scan->ir_scan = 0;
677 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
678 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
679 (*last_cmd)->cmd.scan->end_state = end_state;
681 for (i = 0; i < jtag_num_devices; i++)
683 (*last_cmd)->cmd.scan->fields[field_count].device = i;
688 #ifdef _DEBUG_JTAG_IO_
689 /* if a device is listed, the BYPASS register must not be selected */
690 if (jtag_get_device(i)->bypass)
692 LOG_ERROR("BUG: scan data for a device in BYPASS");
696 for (j = 0; j < num_fields; j++)
699 scan_size = num_bits[j];
700 buf_set_u32(out_value, 0, scan_size, value[j]);
701 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
702 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
703 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
704 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
705 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
706 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
707 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
708 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
712 #ifdef _DEBUG_JTAG_IO_
713 /* if a device isn't listed, the BYPASS register should be selected */
714 if (!jtag_get_device(i)->bypass)
716 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
720 /* program the scan field to 1 bit length, and ignore it's value */
721 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
722 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
723 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
724 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
725 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
726 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
727 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
728 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
736 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
740 int retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
741 if (retval!=ERROR_OK)
745 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
748 jtag_command_t **last_cmd = jtag_get_last_command_p();
750 /* allocate memory for a new list member */
751 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
752 last_comand_pointer = &((*last_cmd)->next);
753 (*last_cmd)->next = NULL;
754 (*last_cmd)->type = JTAG_SCAN;
756 /* allocate memory for scan command */
757 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
758 (*last_cmd)->cmd.scan->ir_scan = 0;
759 (*last_cmd)->cmd.scan->num_fields = num_fields;
760 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
761 (*last_cmd)->cmd.scan->end_state = state;
763 for (i = 0; i < num_fields; i++)
765 int num_bits = fields[i].num_bits;
766 int num_bytes = CEIL(fields[i].num_bits, 8);
767 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
768 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
769 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
770 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
771 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
772 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
773 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
774 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
775 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
780 void jtag_add_statemove(enum tap_state state)
785 retval=interface_jtag_add_statemove(cmd_queue_end_state);
786 if (retval!=ERROR_OK)
790 int MINIDRIVER(interface_jtag_add_statemove)(enum tap_state state)
792 jtag_command_t **last_cmd = jtag_get_last_command_p();
794 /* allocate memory for a new list member */
795 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
796 last_comand_pointer = &((*last_cmd)->next);
797 (*last_cmd)->next = NULL;
798 (*last_cmd)->type = JTAG_STATEMOVE;
800 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
801 (*last_cmd)->cmd.statemove->end_state = state;
807 void jtag_add_pathmove(int num_states, enum tap_state *path)
809 /* the last state has to be a stable state */
810 if (tap_move_map[path[num_states - 1]] == -1)
812 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
816 enum tap_state cur_state=cmd_queue_cur_state;
818 for (i=0; i<num_states; i++)
820 if ((tap_transitions[cur_state].low != path[i])&&
821 (tap_transitions[cur_state].high != path[i]))
823 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
831 cmd_queue_cur_state = path[num_states - 1];
833 int retval=interface_jtag_add_pathmove(num_states, path);
834 if (retval!=ERROR_OK)
839 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
841 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_PATHMOVE;
850 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
851 (*last_cmd)->cmd.pathmove->num_states = num_states;
852 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
854 for (i = 0; i < num_states; i++)
855 (*last_cmd)->cmd.pathmove->path[i] = path[i];
860 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
862 jtag_command_t **last_cmd = jtag_get_last_command_p();
864 /* allocate memory for a new list member */
865 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
866 (*last_cmd)->next = NULL;
867 last_comand_pointer = &((*last_cmd)->next);
868 (*last_cmd)->type = JTAG_RUNTEST;
870 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
871 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
872 (*last_cmd)->cmd.runtest->end_state = state;
877 void jtag_add_runtest(int num_cycles, enum tap_state state)
881 /* executed by sw or hw fifo */
882 int retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
883 if (retval!=ERROR_OK)
887 int jtag_add_reset(int req_trst, int req_srst)
889 int trst_with_tms = 0;
893 req_trst = jtag_trst;
896 req_srst = jtag_srst;
898 /* Make sure that jtag_reset_config allows the requested reset */
899 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
900 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
902 LOG_WARNING("requested reset would assert trst");
903 return ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
906 /* if TRST pulls SRST, we reset with TAP T-L-R */
907 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
913 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
915 LOG_WARNING("requested nSRST assertion, but the current configuration doesn't support this");
916 return ERROR_JTAG_RESET_CANT_SRST;
919 if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
925 jtag_trst = req_trst;
926 jtag_srst = req_srst;
928 retval = interface_jtag_add_reset(req_trst, req_srst);
929 if (retval!=ERROR_OK)
937 LOG_DEBUG("SRST line asserted");
941 LOG_DEBUG("SRST line released");
942 if (jtag_nsrst_delay)
943 jtag_add_sleep(jtag_nsrst_delay * 1000);
948 LOG_DEBUG("JTAG reset with tms instead of TRST");
949 jtag_add_end_state(TAP_TLR);
950 jtag_add_statemove(TAP_TLR);
951 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
957 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
958 * and inform possible listeners about this
960 LOG_DEBUG("TRST line asserted");
961 cmd_queue_cur_state = TAP_TLR;
962 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
966 /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
967 * but we might want to add a delay to give the TAP time to settle
969 LOG_DEBUG("Now in TAP_TLR - Test-Logic-Reset(either due to TRST line asserted or tms reset)");
970 if (jtag_ntrst_delay)
971 jtag_add_sleep(jtag_ntrst_delay * 1000);
976 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
978 jtag_command_t **last_cmd = jtag_get_last_command_p();
980 /* allocate memory for a new list member */
981 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
982 (*last_cmd)->next = NULL;
983 last_comand_pointer = &((*last_cmd)->next);
984 (*last_cmd)->type = JTAG_RESET;
986 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
987 (*last_cmd)->cmd.reset->trst = req_trst;
988 (*last_cmd)->cmd.reset->srst = req_srst;
994 void jtag_add_end_state(enum tap_state state)
996 cmd_queue_end_state = state;
999 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1001 jtag_command_t **last_cmd = jtag_get_last_command_p();
1003 /* allocate memory for a new list member */
1004 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1005 (*last_cmd)->next = NULL;
1006 last_comand_pointer = &((*last_cmd)->next);
1007 (*last_cmd)->type = JTAG_SLEEP;
1009 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1010 (*last_cmd)->cmd.sleep->us = us;
1015 void jtag_add_sleep(u32 us)
1017 int retval=interface_jtag_add_sleep(us);
1018 if (retval!=ERROR_OK)
1023 int jtag_scan_size(scan_command_t *cmd)
1028 /* count bits in scan command */
1029 for (i = 0; i < cmd->num_fields; i++)
1031 bit_count += cmd->fields[i].num_bits;
1037 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1042 bit_count = jtag_scan_size(cmd);
1043 *buffer = malloc(CEIL(bit_count, 8));
1047 for (i = 0; i < cmd->num_fields; i++)
1049 if (cmd->fields[i].out_value)
1051 #ifdef _DEBUG_JTAG_IO_
1052 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1054 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1055 #ifdef _DEBUG_JTAG_IO_
1056 LOG_DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1061 bit_count += cmd->fields[i].num_bits;
1068 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1074 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1077 for (i = 0; i < cmd->num_fields; i++)
1079 /* if neither in_value nor in_handler
1080 * are specified we don't have to examine this field
1082 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1084 int num_bits = cmd->fields[i].num_bits;
1085 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1087 #ifdef _DEBUG_JTAG_IO_
1090 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1091 LOG_DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1095 if (cmd->fields[i].in_value)
1097 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1099 if (cmd->fields[i].in_handler)
1101 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1103 LOG_WARNING("in_handler reported a failed check");
1104 retval = ERROR_JTAG_QUEUE_FAILED;
1109 /* no in_value specified, but a handler takes care of the scanned data */
1110 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1112 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1114 /* We're going to call the error:handler later, but if the in_handler
1115 * reported an error we report this failure upstream
1117 LOG_WARNING("in_handler reported a failed check");
1118 retval = ERROR_JTAG_QUEUE_FAILED;
1124 bit_count += cmd->fields[i].num_bits;
1130 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1132 int retval = ERROR_OK;
1133 int num_bits = field->num_bits;
1135 int compare_failed = 0;
1137 if (field->in_check_mask)
1138 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1140 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1144 /* An error handler could have caught the failing check
1145 * only report a problem when there wasn't a handler, or if the handler
1146 * acknowledged the error
1150 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1151 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1153 if (field->in_check_mask)
1155 char *in_check_mask_char;
1156 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1157 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);
1158 free(in_check_mask_char);
1162 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);
1165 free(captured_char);
1166 free(in_check_value_char);
1168 retval = ERROR_JTAG_QUEUE_FAILED;
1176 set up checking of this field using the in_handler. The values passed in must be valid until
1177 after jtag_execute() has completed.
1179 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1182 field->in_handler = jtag_check_value;
1184 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1185 field->in_handler_priv = NULL; /* this will be filled in at the invocation site to point to the field duplicate */
1186 field->in_check_value = value;
1187 field->in_check_mask = mask;
1190 enum scan_type jtag_scan_type(scan_command_t *cmd)
1195 for (i = 0; i < cmd->num_fields; i++)
1197 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1199 if (cmd->fields[i].out_value)
1206 int MINIDRIVER(interface_jtag_execute_queue)(void)
1210 retval = jtag->execute_queue();
1214 jtag_command_queue = NULL;
1215 last_comand_pointer = &jtag_command_queue;
1220 int jtag_execute_queue(void)
1222 int retval=interface_jtag_execute_queue();
1223 if (retval==ERROR_OK)
1227 jtag_error=ERROR_OK;
1231 int jtag_reset_callback(enum jtag_event event, void *priv)
1233 jtag_device_t *device = priv;
1237 if (event == JTAG_TRST_ASSERTED)
1239 buf_set_ones(device->cur_instr, device->ir_length);
1246 void jtag_sleep(u32 us)
1251 /* Try to examine chain layout according to IEEE 1149.1 §12
1253 int jtag_examine_chain()
1255 jtag_device_t *device = jtag_devices;
1257 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1260 int device_count = 0;
1261 u8 zero_check = 0x0;
1262 u8 one_check = 0xff;
1265 field.num_bits = sizeof(idcode_buffer) * 8;
1266 field.out_value = idcode_buffer;
1267 field.out_mask = NULL;
1268 field.in_value = idcode_buffer;
1269 field.in_check_value = NULL;
1270 field.in_check_mask = NULL;
1271 field.in_handler = NULL;
1272 field.in_handler_priv = NULL;
1274 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1276 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1279 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1280 jtag_execute_queue();
1282 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1284 zero_check |= idcode_buffer[i];
1285 one_check &= idcode_buffer[i];
1288 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1289 if ((zero_check == 0x00) || (one_check == 0xff))
1291 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1292 return ERROR_JTAG_INIT_FAILED;
1295 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1297 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1298 if ((idcode & 1) == 0)
1300 /* LSB must not be 0, this indicates a device in bypass */
1311 if (idcode == 0x000000FF)
1313 /* End of chain (invalid manufacturer ID) */
1319 device->idcode = idcode;
1320 device = device->next;
1324 manufacturer = (idcode & 0xffe) >> 1;
1325 part = (idcode & 0xffff000) >> 12;
1326 version = (idcode & 0xf0000000) >> 28;
1328 LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1329 idcode, manufacturer, part, version);
1335 /* see if number of discovered devices matches configuration */
1336 if (device_count != jtag_num_devices)
1338 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1339 device_count, jtag_num_devices);
1340 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1341 return ERROR_JTAG_INIT_FAILED;
1347 int jtag_validate_chain()
1349 jtag_device_t *device = jtag_devices;
1350 int total_ir_length = 0;
1357 total_ir_length += device->ir_length;
1358 device = device->next;
1361 total_ir_length += 2;
1362 ir_test = malloc(CEIL(total_ir_length, 8));
1363 buf_set_ones(ir_test, total_ir_length);
1366 field.num_bits = total_ir_length;
1367 field.out_value = ir_test;
1368 field.out_mask = NULL;
1369 field.in_value = ir_test;
1370 field.in_check_value = NULL;
1371 field.in_check_mask = NULL;
1372 field.in_handler = NULL;
1373 field.in_handler_priv = NULL;
1375 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1376 jtag_execute_queue();
1378 device = jtag_devices;
1381 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1383 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1384 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1387 return ERROR_JTAG_INIT_FAILED;
1389 chain_pos += device->ir_length;
1390 device = device->next;
1393 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1395 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1396 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1399 return ERROR_JTAG_INIT_FAILED;
1407 int jtag_register_commands(struct command_context_s *cmd_ctx)
1409 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1410 COMMAND_CONFIG, NULL);
1411 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1412 COMMAND_ANY, "set jtag speed (if supported) <reset speed> [<post reset speed, default value is reset speed>]");
1413 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1414 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1415 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1416 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1417 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1418 COMMAND_CONFIG, NULL);
1419 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1420 COMMAND_CONFIG, NULL);
1421 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1422 COMMAND_CONFIG, NULL);
1424 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1425 COMMAND_EXEC, "print current scan chain configuration");
1427 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1428 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1429 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1430 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1431 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1432 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1433 register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
1434 COMMAND_EXEC, "move to current endstate or [tap_state]");
1435 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1436 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1437 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1438 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1440 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1441 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1445 int jtag_interface_init(struct command_context_s *cmd_ctx)
1447 if (!jtag_interface)
1449 /* nothing was previously specified by "interface" command */
1450 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1451 return ERROR_JTAG_INVALID_INTERFACE;
1454 if (jtag_interface->init() != ERROR_OK)
1455 return ERROR_JTAG_INIT_FAILED;
1457 jtag = jtag_interface;
1461 int jtag_init(struct command_context_s *cmd_ctx)
1463 int validate_tries = 0;
1464 jtag_device_t *device;
1468 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1469 return ERROR_JTAG_INIT_FAILED;
1471 device = jtag_devices;
1472 jtag_ir_scan_size = 0;
1473 jtag_num_devices = 0;
1474 while (device != NULL)
1476 jtag_ir_scan_size += device->ir_length;
1478 device = device->next;
1481 jtag_add_statemove(TAP_TLR);
1482 jtag_execute_queue();
1484 /* examine chain first, as this could discover the real chain layout */
1485 if (jtag_examine_chain() != ERROR_OK)
1487 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1490 while (jtag_validate_chain() != ERROR_OK)
1493 if (validate_tries > 5)
1495 LOG_ERROR("Could not validate JTAG chain, exit");
1496 return ERROR_JTAG_INVALID_INTERFACE;
1505 static int default_khz(int khz, int *jtag_speed)
1507 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1511 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1515 /* check whether the interface is already configured */
1518 LOG_WARNING("Interface already configured, ignoring");
1522 /* interface name is a mandatory argument */
1523 if (argc < 1 || args[0][0] == '\0')
1525 return ERROR_COMMAND_SYNTAX_ERROR;
1528 for (i=0; jtag_interfaces[i]; i++)
1530 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1532 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1535 jtag_interface = jtag_interfaces[i];
1537 if (jtag_interface->khz == NULL)
1539 jtag_interface->khz = default_khz;
1545 /* no valid interface was found (i.e. the configuration option,
1546 * didn't match one of the compiled-in interfaces
1548 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
1549 LOG_ERROR("compiled-in jtag interfaces:");
1550 for (i = 0; jtag_interfaces[i]; i++)
1552 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
1555 return ERROR_JTAG_INVALID_INTERFACE;
1558 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1560 jtag_device_t **last_device_p = &jtag_devices;
1564 while ((*last_device_p)->next)
1565 last_device_p = &((*last_device_p)->next);
1566 last_device_p = &((*last_device_p)->next);
1572 *last_device_p = malloc(sizeof(jtag_device_t));
1573 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1575 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1576 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1577 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1578 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1580 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1581 (*last_device_p)->bypass = 1;
1582 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1584 (*last_device_p)->next = NULL;
1586 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1593 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1595 jtag_device_t *device = jtag_devices;
1596 int device_count = 0;
1600 u32 expected, expected_mask, cur_instr;
1601 expected = buf_get_u32(device->expected, 0, device->ir_length);
1602 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1603 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1604 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);
1605 device = device->next;
1612 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1616 if (strcmp(args[0], "none") == 0)
1617 jtag_reset_config = RESET_NONE;
1618 else if (strcmp(args[0], "trst_only") == 0)
1619 jtag_reset_config = RESET_HAS_TRST;
1620 else if (strcmp(args[0], "srst_only") == 0)
1621 jtag_reset_config = RESET_HAS_SRST;
1622 else if (strcmp(args[0], "trst_and_srst") == 0)
1623 jtag_reset_config = RESET_TRST_AND_SRST;
1626 LOG_ERROR("invalid reset_config argument, defaulting to none");
1627 jtag_reset_config = RESET_NONE;
1628 return ERROR_INVALID_ARGUMENTS;
1634 if (strcmp(args[1], "srst_pulls_trst") == 0)
1635 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1636 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1637 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1638 else if (strcmp(args[1], "combined") == 0)
1639 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1640 else if (strcmp(args[1], "separate") == 0)
1641 jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_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[2], "trst_open_drain") == 0)
1653 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1654 else if (strcmp(args[2], "trst_push_pull") == 0)
1655 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1658 LOG_ERROR("invalid reset_config argument, defaulting to none");
1659 jtag_reset_config = RESET_NONE;
1660 return ERROR_INVALID_ARGUMENTS;
1666 if (strcmp(args[3], "srst_push_pull") == 0)
1667 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1668 else if (strcmp(args[3], "srst_open_drain") == 0)
1669 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1672 LOG_ERROR("invalid reset_config argument, defaulting to none");
1673 jtag_reset_config = RESET_NONE;
1674 return ERROR_INVALID_ARGUMENTS;
1681 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1685 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1690 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1696 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1700 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1705 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1711 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1714 if ((argc<1) || (argc>2))
1715 return ERROR_COMMAND_SYNTAX_ERROR;
1718 cur_speed = jtag_speed = jtag_speed_post_reset = strtoul(args[0], NULL, 0);
1720 cur_speed = jtag_speed_post_reset = strtoul(args[1], NULL, 0);
1722 /* this command can be called during CONFIG,
1723 * in which case jtag isn't initialized */
1725 jtag->speed(cur_speed);
1730 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1734 if ((argc<1) || (argc>2))
1735 return ERROR_COMMAND_SYNTAX_ERROR;
1739 LOG_ERROR("Interface not selected yet");
1740 return ERROR_COMMAND_SYNTAX_ERROR;
1744 speed1 = strtoul(args[0], NULL, 0);
1746 speed2 = strtoul(args[1], NULL, 0);
1748 if (jtag->khz(speed1, &speed1)!=ERROR_OK)
1751 if (jtag->khz(speed2, &speed2)!=ERROR_OK)
1755 cur_speed = jtag_speed = jtag_speed_post_reset = speed1;
1758 cur_speed = jtag_speed_post_reset = speed2;
1760 jtag->speed(cur_speed);
1766 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1768 enum tap_state state;
1772 return ERROR_COMMAND_SYNTAX_ERROR;
1776 for (state = 0; state < 16; state++)
1778 if (strcmp(args[0], tap_state_strings[state]) == 0)
1780 jtag_add_end_state(state);
1781 jtag_execute_queue();
1785 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[end_state]);
1790 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1797 return ERROR_COMMAND_SYNTAX_ERROR;
1800 if (args[0][0] == '1')
1802 else if (args[0][0] == '0')
1806 return ERROR_COMMAND_SYNTAX_ERROR;
1809 if (args[1][0] == '1')
1811 else if (args[1][0] == '0')
1815 return ERROR_COMMAND_SYNTAX_ERROR;
1818 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1819 return ERROR_JTAG_INIT_FAILED;
1821 jtag_add_reset(trst, srst);
1822 jtag_execute_queue();
1827 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1831 return ERROR_COMMAND_SYNTAX_ERROR;
1834 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1835 jtag_execute_queue();
1841 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1843 enum tap_state state;
1848 for (state = 0; state < 16; state++)
1850 if (strcmp(args[0], tap_state_strings[state]) == 0)
1857 jtag_add_statemove(state);
1858 jtag_execute_queue();
1864 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1867 scan_field_t *fields;
1869 if ((argc < 2) || (argc % 2))
1871 return ERROR_COMMAND_SYNTAX_ERROR;
1874 fields = malloc(sizeof(scan_field_t) * argc / 2);
1876 for (i = 0; i < argc / 2; i++)
1878 int device = strtoul(args[i*2], NULL, 0);
1879 int field_size = jtag_get_device(device)->ir_length;
1880 fields[i].device = device;
1881 fields[i].out_value = malloc(CEIL(field_size, 8));
1882 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1883 fields[i].out_mask = NULL;
1884 fields[i].in_value = NULL;
1885 fields[i].in_check_mask = NULL;
1886 fields[i].in_handler = NULL;
1887 fields[i].in_handler_priv = NULL;
1890 jtag_add_ir_scan(argc / 2, fields, -1);
1891 jtag_execute_queue();
1893 for (i = 0; i < argc / 2; i++)
1894 free(fields[i].out_value);
1901 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1903 scan_field_t *fields;
1905 int field_count = 0;
1909 if ((argc < 2) || (argc % 2))
1911 return ERROR_COMMAND_SYNTAX_ERROR;
1914 for (i = 0; i < argc; i+=2)
1916 var = get_var_by_namenum(args[i+1]);
1919 num_fields += var->num_fields;
1923 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1928 fields = malloc(sizeof(scan_field_t) * num_fields);
1930 for (i = 0; i < argc; i+=2)
1932 var = get_var_by_namenum(args[i+1]);
1934 for (j = 0; j < var->num_fields; j++)
1936 fields[field_count].device = strtol(args[i], NULL, 0);
1937 fields[field_count].num_bits = var->fields[j].num_bits;
1938 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1939 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1940 fields[field_count].out_mask = NULL;
1941 fields[field_count].in_value = fields[field_count].out_value;
1942 fields[field_count].in_check_mask = NULL;
1943 fields[field_count].in_check_value = NULL;
1944 fields[field_count].in_handler = field_le_to_host;
1945 fields[field_count++].in_handler_priv = &(var->fields[j]);
1949 jtag_add_dr_scan(num_fields, fields, -1);
1950 jtag_execute_queue();
1952 for (i = 0; i < argc / 2; i++)
1953 free(fields[i].out_value);
1960 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1964 if (strcmp(args[0], "enable") == 0)
1966 jtag_verify_capture_ir = 1;
1968 else if (strcmp(args[0], "disable") == 0)
1970 jtag_verify_capture_ir = 0;
1973 return ERROR_COMMAND_SYNTAX_ERROR;
1975 } else if (argc != 0)
1977 return ERROR_COMMAND_SYNTAX_ERROR;
1980 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");