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 /* kludge!!!! these are just global variables that the
113 * interface use internally. They really belong
114 * inside the drivers, but we don't want to break
115 * linking the drivers!!!!
117 enum tap_state end_state = TAP_TLR;
118 enum tap_state cur_state = TAP_TLR;
122 jtag_command_t *jtag_command_queue = NULL;
123 jtag_command_t **last_comand_pointer = &jtag_command_queue;
124 jtag_device_t *jtag_devices = NULL;
125 int jtag_num_devices = 0;
126 int jtag_ir_scan_size = 0;
127 enum reset_types jtag_reset_config = RESET_NONE;
128 enum tap_state cmd_queue_end_state = TAP_TLR;
129 enum tap_state cmd_queue_cur_state = TAP_TLR;
131 int jtag_verify_capture_ir = 1;
133 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
134 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
135 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
137 /* maximum number of JTAG devices expected in the chain
139 #define JTAG_MAX_CHAIN_SIZE 20
141 /* callbacks to inform high-level handlers about JTAG state changes */
142 jtag_event_callback_t *jtag_event_callbacks;
145 static int speed1 = 0, speed2 = 0;
146 /* flag if the kHz speed was defined */
147 static int hasKHz = 0;
149 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
152 #if BUILD_ECOSBOARD == 1
153 extern jtag_interface_t eCosBoard_interface;
156 #if BUILD_PARPORT == 1
157 extern jtag_interface_t parport_interface;
160 #if BUILD_FT2232_FTD2XX == 1
161 extern jtag_interface_t ft2232_interface;
164 #if BUILD_FT2232_LIBFTDI == 1
165 extern jtag_interface_t ft2232_interface;
168 #if BUILD_AMTJTAGACCEL == 1
169 extern jtag_interface_t amt_jtagaccel_interface;
172 #if BUILD_EP93XX == 1
173 extern jtag_interface_t ep93xx_interface;
176 #if BUILD_AT91RM9200 == 1
177 extern jtag_interface_t at91rm9200_interface;
180 #if BUILD_GW16012 == 1
181 extern jtag_interface_t gw16012_interface;
184 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
185 extern jtag_interface_t presto_interface;
188 #if BUILD_USBPROG == 1
189 extern jtag_interface_t usbprog_interface;
192 jtag_interface_t *jtag_interfaces[] = {
193 #if BUILD_ECOSBOARD == 1
194 &eCosBoard_interface,
196 #if BUILD_PARPORT == 1
199 #if BUILD_FT2232_FTD2XX == 1
202 #if BUILD_FT2232_LIBFTDI == 1
205 #if BUILD_AMTJTAGACCEL == 1
206 &amt_jtagaccel_interface,
208 #if BUILD_EP93XX == 1
211 #if BUILD_AT91RM9200 == 1
212 &at91rm9200_interface,
214 #if BUILD_GW16012 == 1
217 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
220 #if BUILD_USBPROG == 1
226 jtag_interface_t *jtag = NULL;
229 jtag_interface_t *jtag_interface = NULL;
231 int jtag_speed_post_reset = 0;
234 /* forward declarations */
235 void jtag_add_pathmove(int num_states, enum tap_state *path);
236 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
237 void jtag_add_end_state(enum tap_state endstate);
238 void jtag_add_sleep(u32 us);
239 int jtag_execute_queue(void);
240 int jtag_cancel_queue(void);
243 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
244 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
245 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
246 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
247 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
248 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
249 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
251 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
253 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
254 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
255 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
256 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
257 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
259 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
261 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
263 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
265 if (callback == NULL)
267 return ERROR_INVALID_ARGUMENTS;
272 while ((*callbacks_p)->next)
273 callbacks_p = &((*callbacks_p)->next);
274 callbacks_p = &((*callbacks_p)->next);
277 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
278 (*callbacks_p)->callback = callback;
279 (*callbacks_p)->priv = priv;
280 (*callbacks_p)->next = NULL;
285 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
287 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
289 if (callback == NULL)
291 return ERROR_INVALID_ARGUMENTS;
296 jtag_event_callback_t **next = &((*callbacks_p)->next);
297 if ((*callbacks_p)->callback == callback)
300 *callbacks_p = *next;
308 int jtag_call_event_callbacks(enum jtag_event event)
310 jtag_event_callback_t *callback = jtag_event_callbacks;
312 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
316 callback->callback(event, callback->priv);
317 callback = callback->next;
323 /* returns a pointer to the pointer of the last command in queue
324 * this may be a pointer to the root pointer (jtag_command_queue)
325 * or to the next member of the last but one command
327 jtag_command_t** jtag_get_last_command_p(void)
329 /* jtag_command_t *cmd = jtag_command_queue;
335 return &jtag_command_queue;
339 return last_comand_pointer;
342 /* returns a pointer to the n-th device in the scan chain */
343 jtag_device_t* jtag_get_device(int num)
345 jtag_device_t *device = jtag_devices;
352 device = device->next;
356 LOG_ERROR("jtag device number %d not defined", num);
360 void* cmd_queue_alloc(size_t size)
362 cmd_queue_page_t **p_page = &cmd_queue_pages;
367 while ((*p_page)->next)
368 p_page = &((*p_page)->next);
369 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
370 p_page = &((*p_page)->next);
375 *p_page = malloc(sizeof(cmd_queue_page_t));
377 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
378 (*p_page)->next = NULL;
381 offset = (*p_page)->used;
382 (*p_page)->used += size;
384 u8 *t=(u8 *)((*p_page)->address);
388 void cmd_queue_free()
390 cmd_queue_page_t *page = cmd_queue_pages;
394 cmd_queue_page_t *last = page;
400 cmd_queue_pages = NULL;
403 static void jtag_prelude1()
407 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
408 jtag_error=ERROR_JTAG_TRST_ASSERTED;
412 if (cmd_queue_end_state == TAP_TLR)
413 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
416 static void jtag_prelude(enum tap_state state)
421 jtag_add_end_state(state);
423 cmd_queue_cur_state = cmd_queue_end_state;
426 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
430 int retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
431 if (retval!=ERROR_OK)
435 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
437 jtag_command_t **last_cmd;
438 jtag_device_t *device;
443 last_cmd = jtag_get_last_command_p();
445 /* allocate memory for a new list member */
446 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
447 (*last_cmd)->next = NULL;
448 last_comand_pointer = &((*last_cmd)->next);
449 (*last_cmd)->type = JTAG_SCAN;
451 /* allocate memory for ir scan command */
452 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
453 (*last_cmd)->cmd.scan->ir_scan = 1;
454 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
455 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
456 (*last_cmd)->cmd.scan->end_state = state;
458 for (i = 0; i < jtag_num_devices; i++)
461 device = jtag_get_device(i);
462 scan_size = device->ir_length;
463 (*last_cmd)->cmd.scan->fields[i].device = i;
464 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
465 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
466 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
468 /* search the list */
469 for (j = 0; j < num_fields; j++)
471 if (i == fields[j].device)
474 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
475 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
477 if (jtag_verify_capture_ir)
479 if (fields[j].in_handler==NULL)
481 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
484 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
485 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
486 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
487 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
498 /* if a device isn't listed, set it to BYPASS */
499 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
500 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
505 /* update device information */
506 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
512 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
516 int retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
517 if (retval!=ERROR_OK)
521 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
524 jtag_command_t **last_cmd;
526 last_cmd = jtag_get_last_command_p();
528 /* allocate memory for a new list member */
529 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
530 (*last_cmd)->next = NULL;
531 last_comand_pointer = &((*last_cmd)->next);
532 (*last_cmd)->type = JTAG_SCAN;
534 /* allocate memory for ir scan command */
535 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
536 (*last_cmd)->cmd.scan->ir_scan = 1;
537 (*last_cmd)->cmd.scan->num_fields = num_fields;
538 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
539 (*last_cmd)->cmd.scan->end_state = state;
541 for (i = 0; i < num_fields; i++)
543 int num_bits = fields[i].num_bits;
544 int num_bytes = CEIL(fields[i].num_bits, 8);
545 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
546 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
547 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
548 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
549 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
550 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
551 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
552 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
553 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
558 void jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
562 int retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
563 if (retval!=ERROR_OK)
567 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
570 int bypass_devices = 0;
574 jtag_command_t **last_cmd = jtag_get_last_command_p();
575 jtag_device_t *device = jtag_devices;
577 /* count devices in bypass */
582 device = device->next;
585 /* allocate memory for a new list member */
586 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
587 last_comand_pointer = &((*last_cmd)->next);
588 (*last_cmd)->next = NULL;
589 (*last_cmd)->type = JTAG_SCAN;
591 /* allocate memory for dr scan command */
592 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
593 (*last_cmd)->cmd.scan->ir_scan = 0;
594 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
595 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
596 (*last_cmd)->cmd.scan->end_state = state;
598 for (i = 0; i < jtag_num_devices; i++)
601 (*last_cmd)->cmd.scan->fields[field_count].device = i;
603 for (j = 0; j < num_fields; j++)
605 if (i == fields[j].device)
608 scan_size = fields[j].num_bits;
609 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
610 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
611 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
612 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
613 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
614 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
615 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
616 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
621 #ifdef _DEBUG_JTAG_IO_
622 /* if a device isn't listed, the BYPASS register should be selected */
623 if (!jtag_get_device(i)->bypass)
625 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
629 /* program the scan field to 1 bit length, and ignore it's value */
630 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
631 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
632 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
633 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
634 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
635 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
636 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
637 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
641 #ifdef _DEBUG_JTAG_IO_
642 /* if a device is listed, the BYPASS register must not be selected */
643 if (jtag_get_device(i)->bypass)
645 LOG_ERROR("BUG: scan data for a device in BYPASS");
654 void MINIDRIVER(interface_jtag_add_dr_out)(int device_num,
658 enum tap_state end_state)
663 int bypass_devices = 0;
665 jtag_command_t **last_cmd = jtag_get_last_command_p();
666 jtag_device_t *device = jtag_devices;
667 /* count devices in bypass */
672 device = device->next;
675 /* allocate memory for a new list member */
676 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
677 last_comand_pointer = &((*last_cmd)->next);
678 (*last_cmd)->next = NULL;
679 (*last_cmd)->type = JTAG_SCAN;
681 /* allocate memory for dr scan command */
682 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
683 (*last_cmd)->cmd.scan->ir_scan = 0;
684 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
685 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
686 (*last_cmd)->cmd.scan->end_state = end_state;
688 for (i = 0; i < jtag_num_devices; i++)
690 (*last_cmd)->cmd.scan->fields[field_count].device = i;
695 #ifdef _DEBUG_JTAG_IO_
696 /* if a device is listed, the BYPASS register must not be selected */
697 if (jtag_get_device(i)->bypass)
699 LOG_ERROR("BUG: scan data for a device in BYPASS");
703 for (j = 0; j < num_fields; j++)
706 scan_size = num_bits[j];
707 buf_set_u32(out_value, 0, scan_size, value[j]);
708 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
709 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
710 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
711 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
712 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
713 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
714 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
715 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
719 #ifdef _DEBUG_JTAG_IO_
720 /* if a device isn't listed, the BYPASS register should be selected */
721 if (!jtag_get_device(i)->bypass)
723 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
727 /* program the scan field to 1 bit length, and ignore it's value */
728 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
729 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
730 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
731 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
732 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
733 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
734 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
735 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
743 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
747 int retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
748 if (retval!=ERROR_OK)
752 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
755 jtag_command_t **last_cmd = jtag_get_last_command_p();
757 /* allocate memory for a new list member */
758 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
759 last_comand_pointer = &((*last_cmd)->next);
760 (*last_cmd)->next = NULL;
761 (*last_cmd)->type = JTAG_SCAN;
763 /* allocate memory for scan command */
764 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
765 (*last_cmd)->cmd.scan->ir_scan = 0;
766 (*last_cmd)->cmd.scan->num_fields = num_fields;
767 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
768 (*last_cmd)->cmd.scan->end_state = state;
770 for (i = 0; i < num_fields; i++)
772 int num_bits = fields[i].num_bits;
773 int num_bytes = CEIL(fields[i].num_bits, 8);
774 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
775 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
776 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
777 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
778 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
779 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
780 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
781 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
782 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
790 jtag_prelude(TAP_TLR);
793 retval=interface_jtag_add_tms();
794 if (retval!=ERROR_OK)
798 int MINIDRIVER(interface_jtag_add_tms)()
800 enum tap_state state = TAP_TLR;
801 jtag_command_t **last_cmd = jtag_get_last_command_p();
803 /* allocate memory for a new list member */
804 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
805 last_comand_pointer = &((*last_cmd)->next);
806 (*last_cmd)->next = NULL;
807 (*last_cmd)->type = JTAG_STATEMOVE;
809 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
810 (*last_cmd)->cmd.statemove->end_state = state;
816 void jtag_add_pathmove(int num_states, enum tap_state *path)
818 /* the last state has to be a stable state */
819 if (tap_move_map[path[num_states - 1]] == -1)
821 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
825 enum tap_state cur_state=cmd_queue_cur_state;
827 for (i=0; i<num_states; i++)
829 if ((tap_transitions[cur_state].low != path[i])&&
830 (tap_transitions[cur_state].high != path[i]))
832 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
840 cmd_queue_cur_state = path[num_states - 1];
842 int retval=interface_jtag_add_pathmove(num_states, path);
843 if (retval!=ERROR_OK)
848 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
850 jtag_command_t **last_cmd = jtag_get_last_command_p();
853 /* allocate memory for a new list member */
854 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
855 last_comand_pointer = &((*last_cmd)->next);
856 (*last_cmd)->next = NULL;
857 (*last_cmd)->type = JTAG_PATHMOVE;
859 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
860 (*last_cmd)->cmd.pathmove->num_states = num_states;
861 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
863 for (i = 0; i < num_states; i++)
864 (*last_cmd)->cmd.pathmove->path[i] = path[i];
869 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
871 jtag_command_t **last_cmd = jtag_get_last_command_p();
873 /* allocate memory for a new list member */
874 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
875 (*last_cmd)->next = NULL;
876 last_comand_pointer = &((*last_cmd)->next);
877 (*last_cmd)->type = JTAG_RUNTEST;
879 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
880 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
881 (*last_cmd)->cmd.runtest->end_state = state;
886 void jtag_add_runtest(int num_cycles, enum tap_state state)
890 /* executed by sw or hw fifo */
891 int retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
892 if (retval!=ERROR_OK)
896 void jtag_add_reset(int req_tms_or_trst, int req_srst)
898 int trst_with_tms = 0;
901 /* Make sure that jtag_reset_config allows the requested reset */
902 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
903 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tms_or_trst))
905 LOG_ERROR("BUG: requested reset would assert trst");
906 jtag_error=ERROR_FAIL;
910 /* if TRST pulls SRST, we reset with TAP T-L-R */
911 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tms_or_trst)) && (req_srst == 0))
916 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
918 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
919 jtag_error=ERROR_FAIL;
925 if (!trst_with_tms && (jtag_reset_config & RESET_HAS_TRST))
937 jtag_srst = req_srst;
939 retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
940 if (retval!=ERROR_OK)
948 LOG_DEBUG("SRST line asserted");
952 LOG_DEBUG("SRST line released");
953 if (jtag_nsrst_delay)
954 jtag_add_sleep(jtag_nsrst_delay * 1000);
959 LOG_DEBUG("JTAG reset with tms instead of TRST");
960 jtag_add_end_state(TAP_TLR);
962 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
968 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
969 * and inform possible listeners about this
971 LOG_DEBUG("TRST line asserted");
972 cmd_queue_cur_state = TAP_TLR;
973 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
977 if (jtag_ntrst_delay)
978 jtag_add_sleep(jtag_ntrst_delay * 1000);
982 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
984 jtag_command_t **last_cmd = jtag_get_last_command_p();
986 /* allocate memory for a new list member */
987 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
988 (*last_cmd)->next = NULL;
989 last_comand_pointer = &((*last_cmd)->next);
990 (*last_cmd)->type = JTAG_RESET;
992 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
993 (*last_cmd)->cmd.reset->trst = req_trst;
994 (*last_cmd)->cmd.reset->srst = req_srst;
1000 void jtag_add_end_state(enum tap_state state)
1002 cmd_queue_end_state = state;
1003 if ((cmd_queue_end_state == TAP_SD)||(cmd_queue_end_state == TAP_SD))
1005 LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
1009 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1011 jtag_command_t **last_cmd = jtag_get_last_command_p();
1013 /* allocate memory for a new list member */
1014 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1015 (*last_cmd)->next = NULL;
1016 last_comand_pointer = &((*last_cmd)->next);
1017 (*last_cmd)->type = JTAG_SLEEP;
1019 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1020 (*last_cmd)->cmd.sleep->us = us;
1025 void jtag_add_sleep(u32 us)
1027 int retval=interface_jtag_add_sleep(us);
1028 if (retval!=ERROR_OK)
1033 int jtag_scan_size(scan_command_t *cmd)
1038 /* count bits in scan command */
1039 for (i = 0; i < cmd->num_fields; i++)
1041 bit_count += cmd->fields[i].num_bits;
1047 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1052 bit_count = jtag_scan_size(cmd);
1053 *buffer = malloc(CEIL(bit_count, 8));
1057 for (i = 0; i < cmd->num_fields; i++)
1059 if (cmd->fields[i].out_value)
1061 #ifdef _DEBUG_JTAG_IO_
1062 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1064 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1065 #ifdef _DEBUG_JTAG_IO_
1066 LOG_DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1071 bit_count += cmd->fields[i].num_bits;
1078 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1084 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1087 for (i = 0; i < cmd->num_fields; i++)
1089 /* if neither in_value nor in_handler
1090 * are specified we don't have to examine this field
1092 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1094 int num_bits = cmd->fields[i].num_bits;
1095 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1097 #ifdef _DEBUG_JTAG_IO_
1100 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1101 LOG_DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1105 if (cmd->fields[i].in_value)
1107 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1109 if (cmd->fields[i].in_handler)
1111 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1113 LOG_WARNING("in_handler reported a failed check");
1114 retval = ERROR_JTAG_QUEUE_FAILED;
1119 /* no in_value specified, but a handler takes care of the scanned data */
1120 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1122 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1124 /* We're going to call the error:handler later, but if the in_handler
1125 * reported an error we report this failure upstream
1127 LOG_WARNING("in_handler reported a failed check");
1128 retval = ERROR_JTAG_QUEUE_FAILED;
1134 bit_count += cmd->fields[i].num_bits;
1140 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1142 int retval = ERROR_OK;
1143 int num_bits = field->num_bits;
1145 int compare_failed = 0;
1147 if (field->in_check_mask)
1148 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1150 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1154 /* An error handler could have caught the failing check
1155 * only report a problem when there wasn't a handler, or if the handler
1156 * acknowledged the error
1160 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1161 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1163 if (field->in_check_mask)
1165 char *in_check_mask_char;
1166 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1167 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);
1168 free(in_check_mask_char);
1172 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);
1175 free(captured_char);
1176 free(in_check_value_char);
1178 retval = ERROR_JTAG_QUEUE_FAILED;
1186 set up checking of this field using the in_handler. The values passed in must be valid until
1187 after jtag_execute() has completed.
1189 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1192 field->in_handler = jtag_check_value;
1194 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1195 field->in_handler_priv = NULL; /* this will be filled in at the invocation site to point to the field duplicate */
1196 field->in_check_value = value;
1197 field->in_check_mask = mask;
1200 enum scan_type jtag_scan_type(scan_command_t *cmd)
1205 for (i = 0; i < cmd->num_fields; i++)
1207 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1209 if (cmd->fields[i].out_value)
1216 int MINIDRIVER(interface_jtag_execute_queue)(void)
1220 retval = jtag->execute_queue();
1224 jtag_command_queue = NULL;
1225 last_comand_pointer = &jtag_command_queue;
1230 int jtag_execute_queue(void)
1232 int retval=interface_jtag_execute_queue();
1233 if (retval==ERROR_OK)
1237 jtag_error=ERROR_OK;
1241 int jtag_reset_callback(enum jtag_event event, void *priv)
1243 jtag_device_t *device = priv;
1247 if (event == JTAG_TRST_ASSERTED)
1249 buf_set_ones(device->cur_instr, device->ir_length);
1256 void jtag_sleep(u32 us)
1261 /* Try to examine chain layout according to IEEE 1149.1 §12
1263 int jtag_examine_chain()
1265 jtag_device_t *device = jtag_devices;
1267 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1270 int device_count = 0;
1271 u8 zero_check = 0x0;
1272 u8 one_check = 0xff;
1275 field.num_bits = sizeof(idcode_buffer) * 8;
1276 field.out_value = idcode_buffer;
1277 field.out_mask = NULL;
1278 field.in_value = idcode_buffer;
1279 field.in_check_value = NULL;
1280 field.in_check_mask = NULL;
1281 field.in_handler = NULL;
1282 field.in_handler_priv = NULL;
1284 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1286 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1289 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1290 jtag_execute_queue();
1292 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1294 zero_check |= idcode_buffer[i];
1295 one_check &= idcode_buffer[i];
1298 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1299 if ((zero_check == 0x00) || (one_check == 0xff))
1301 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1302 return ERROR_JTAG_INIT_FAILED;
1305 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1307 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1308 if ((idcode & 1) == 0)
1310 /* LSB must not be 0, this indicates a device in bypass */
1321 if (idcode == 0x000000FF)
1323 /* End of chain (invalid manufacturer ID) */
1329 device->idcode = idcode;
1330 device = device->next;
1334 manufacturer = (idcode & 0xffe) >> 1;
1335 part = (idcode & 0xffff000) >> 12;
1336 version = (idcode & 0xf0000000) >> 28;
1338 LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1339 idcode, manufacturer, part, version);
1345 /* see if number of discovered devices matches configuration */
1346 if (device_count != jtag_num_devices)
1348 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1349 device_count, jtag_num_devices);
1350 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1351 return ERROR_JTAG_INIT_FAILED;
1357 int jtag_validate_chain()
1359 jtag_device_t *device = jtag_devices;
1360 int total_ir_length = 0;
1367 total_ir_length += device->ir_length;
1368 device = device->next;
1371 total_ir_length += 2;
1372 ir_test = malloc(CEIL(total_ir_length, 8));
1373 buf_set_ones(ir_test, total_ir_length);
1376 field.num_bits = total_ir_length;
1377 field.out_value = ir_test;
1378 field.out_mask = NULL;
1379 field.in_value = ir_test;
1380 field.in_check_value = NULL;
1381 field.in_check_mask = NULL;
1382 field.in_handler = NULL;
1383 field.in_handler_priv = NULL;
1385 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1386 jtag_execute_queue();
1388 device = jtag_devices;
1391 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1393 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1394 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1397 return ERROR_JTAG_INIT_FAILED;
1399 chain_pos += device->ir_length;
1400 device = device->next;
1403 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1405 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1406 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1409 return ERROR_JTAG_INIT_FAILED;
1417 int jtag_register_commands(struct command_context_s *cmd_ctx)
1419 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1420 COMMAND_CONFIG, NULL);
1421 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1422 COMMAND_ANY, "set jtag speed (if supported) <reset speed> [<post reset speed, default value is reset speed>]");
1423 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1424 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1425 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1426 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1427 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1428 COMMAND_CONFIG, NULL);
1429 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1430 COMMAND_CONFIG, NULL);
1431 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1432 COMMAND_CONFIG, NULL);
1434 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1435 COMMAND_EXEC, "print current scan chain configuration");
1437 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1438 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1439 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1440 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1441 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1442 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1443 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1444 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1445 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1446 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1448 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1449 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1453 int jtag_interface_init(struct command_context_s *cmd_ctx)
1455 if (!jtag_interface)
1457 /* nothing was previously specified by "interface" command */
1458 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1459 return ERROR_JTAG_INVALID_INTERFACE;
1463 /*stay on "reset speed"*/
1464 if (jtag_interface->khz(speed1, &speed1) == ERROR_OK)
1465 jtag_speed = speed1;
1466 if (jtag_interface->khz(speed2, &speed2) == ERROR_OK)
1467 jtag_speed_post_reset = speed2;
1471 if (jtag_interface->init() != ERROR_OK)
1472 return ERROR_JTAG_INIT_FAILED;
1476 jtag = jtag_interface;
1480 int jtag_init(struct command_context_s *cmd_ctx)
1482 int validate_tries = 0;
1483 jtag_device_t *device;
1487 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1488 return ERROR_JTAG_INIT_FAILED;
1490 device = jtag_devices;
1491 jtag_ir_scan_size = 0;
1492 jtag_num_devices = 0;
1493 while (device != NULL)
1495 jtag_ir_scan_size += device->ir_length;
1497 device = device->next;
1501 jtag_execute_queue();
1503 /* examine chain first, as this could discover the real chain layout */
1504 if (jtag_examine_chain() != ERROR_OK)
1506 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1509 while (jtag_validate_chain() != ERROR_OK)
1513 if (validate_tries > 5)
1515 LOG_ERROR("Could not validate JTAG chain, exit");
1516 return ERROR_JTAG_INVALID_INTERFACE;
1525 static int default_khz(int khz, int *jtag_speed)
1527 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1531 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1535 /* check whether the interface is already configured */
1538 LOG_WARNING("Interface already configured, ignoring");
1542 /* interface name is a mandatory argument */
1543 if (argc < 1 || args[0][0] == '\0')
1545 return ERROR_COMMAND_SYNTAX_ERROR;
1548 for (i=0; jtag_interfaces[i]; i++)
1550 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1552 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1555 jtag_interface = jtag_interfaces[i];
1557 if (jtag_interface->khz == NULL)
1559 jtag_interface->khz = default_khz;
1565 /* no valid interface was found (i.e. the configuration option,
1566 * didn't match one of the compiled-in interfaces
1568 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
1569 LOG_ERROR("compiled-in jtag interfaces:");
1570 for (i = 0; jtag_interfaces[i]; i++)
1572 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
1575 return ERROR_JTAG_INVALID_INTERFACE;
1578 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1580 jtag_device_t **last_device_p = &jtag_devices;
1584 while ((*last_device_p)->next)
1585 last_device_p = &((*last_device_p)->next);
1586 last_device_p = &((*last_device_p)->next);
1592 *last_device_p = malloc(sizeof(jtag_device_t));
1593 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1595 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1596 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1597 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1598 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1600 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1601 (*last_device_p)->bypass = 1;
1602 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1604 (*last_device_p)->next = NULL;
1606 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1613 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1615 jtag_device_t *device = jtag_devices;
1616 int device_count = 0;
1620 u32 expected, expected_mask, cur_instr;
1621 expected = buf_get_u32(device->expected, 0, device->ir_length);
1622 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1623 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1624 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);
1625 device = device->next;
1632 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1635 return ERROR_COMMAND_SYNTAX_ERROR;
1639 if (strcmp(args[0], "none") == 0)
1640 jtag_reset_config = RESET_NONE;
1641 else if (strcmp(args[0], "trst_only") == 0)
1642 jtag_reset_config = RESET_HAS_TRST;
1643 else if (strcmp(args[0], "srst_only") == 0)
1644 jtag_reset_config = RESET_HAS_SRST;
1645 else if (strcmp(args[0], "trst_and_srst") == 0)
1646 jtag_reset_config = RESET_TRST_AND_SRST;
1649 LOG_ERROR("invalid reset_config argument, defaulting to none");
1650 jtag_reset_config = RESET_NONE;
1651 return ERROR_INVALID_ARGUMENTS;
1657 if (strcmp(args[1], "separate") == 0)
1659 /* seperate reset lines - default */
1662 if (strcmp(args[1], "srst_pulls_trst") == 0)
1663 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1664 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1665 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1666 else if (strcmp(args[1], "combined") == 0)
1667 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1670 LOG_ERROR("invalid reset_config argument, defaulting to none");
1671 jtag_reset_config = RESET_NONE;
1672 return ERROR_INVALID_ARGUMENTS;
1679 if (strcmp(args[2], "trst_open_drain") == 0)
1680 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1681 else if (strcmp(args[2], "trst_push_pull") == 0)
1682 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1685 LOG_ERROR("invalid reset_config argument, defaulting to none");
1686 jtag_reset_config = RESET_NONE;
1687 return ERROR_INVALID_ARGUMENTS;
1693 if (strcmp(args[3], "srst_push_pull") == 0)
1694 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1695 else if (strcmp(args[3], "srst_open_drain") == 0)
1696 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1699 LOG_ERROR("invalid reset_config argument, defaulting to none");
1700 jtag_reset_config = RESET_NONE;
1701 return ERROR_INVALID_ARGUMENTS;
1708 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1712 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1717 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1723 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1727 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1732 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1738 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1744 if ((argc<1) || (argc>2))
1745 return ERROR_COMMAND_SYNTAX_ERROR;
1747 LOG_DEBUG("handle jtag speed");
1750 cur_speed = jtag_speed = jtag_speed_post_reset = strtoul(args[0], NULL, 0);
1752 cur_speed = jtag_speed_post_reset = strtoul(args[1], NULL, 0);
1754 /* this command can be called during CONFIG,
1755 * in which case jtag isn't initialized */
1757 jtag->speed(cur_speed);
1759 command_print(cmd_ctx, "jtag_speed: %d, %d", jtag_speed, jtag_speed_post_reset);
1764 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1766 LOG_DEBUG("handle jtag khz");
1768 if ((argc<1) || (argc>2))
1769 return ERROR_COMMAND_SYNTAX_ERROR;
1772 speed1 = speed2 = strtoul(args[0], NULL, 0);
1774 speed2 = strtoul(args[1], NULL, 0);
1779 LOG_DEBUG("have interface set up");
1780 int speed_div1, speed_div2;
1781 if (jtag->khz(speed1, &speed_div1)!=ERROR_OK)
1783 if (jtag->khz(speed2, &speed_div2)!=ERROR_OK)
1787 cur_speed = jtag_speed = jtag_speed_post_reset = speed_div1;
1789 cur_speed = jtag_speed_post_reset = speed_div2;
1791 jtag->speed(cur_speed);
1801 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1803 enum tap_state state;
1807 return ERROR_COMMAND_SYNTAX_ERROR;
1811 for (state = 0; state < 16; state++)
1813 if (strcmp(args[0], tap_state_strings[state]) == 0)
1815 jtag_add_end_state(state);
1816 jtag_execute_queue();
1820 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
1825 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1832 return ERROR_COMMAND_SYNTAX_ERROR;
1835 if (args[0][0] == '1')
1837 else if (args[0][0] == '0')
1841 return ERROR_COMMAND_SYNTAX_ERROR;
1844 if (args[1][0] == '1')
1846 else if (args[1][0] == '0')
1850 return ERROR_COMMAND_SYNTAX_ERROR;
1853 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1854 return ERROR_JTAG_INIT_FAILED;
1856 jtag_add_reset(trst, srst);
1857 jtag_execute_queue();
1862 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1866 return ERROR_COMMAND_SYNTAX_ERROR;
1869 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1870 jtag_execute_queue();
1877 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1880 scan_field_t *fields;
1882 if ((argc < 2) || (argc % 2))
1884 return ERROR_COMMAND_SYNTAX_ERROR;
1887 fields = malloc(sizeof(scan_field_t) * argc / 2);
1889 for (i = 0; i < argc / 2; i++)
1891 int device = strtoul(args[i*2], NULL, 0);
1892 int field_size = jtag_get_device(device)->ir_length;
1893 fields[i].device = device;
1894 fields[i].out_value = malloc(CEIL(field_size, 8));
1895 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1896 fields[i].out_mask = NULL;
1897 fields[i].in_value = NULL;
1898 fields[i].in_check_mask = NULL;
1899 fields[i].in_handler = NULL;
1900 fields[i].in_handler_priv = NULL;
1903 jtag_add_ir_scan(argc / 2, fields, -1);
1904 jtag_execute_queue();
1906 for (i = 0; i < argc / 2; i++)
1907 free(fields[i].out_value);
1914 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1916 scan_field_t *fields;
1918 int field_count = 0;
1922 if ((argc < 2) || (argc % 2))
1924 return ERROR_COMMAND_SYNTAX_ERROR;
1927 for (i = 0; i < argc; i+=2)
1929 var = get_var_by_namenum(args[i+1]);
1932 num_fields += var->num_fields;
1936 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1941 fields = malloc(sizeof(scan_field_t) * num_fields);
1943 for (i = 0; i < argc; i+=2)
1945 var = get_var_by_namenum(args[i+1]);
1947 for (j = 0; j < var->num_fields; j++)
1949 fields[field_count].device = strtol(args[i], NULL, 0);
1950 fields[field_count].num_bits = var->fields[j].num_bits;
1951 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1952 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1953 fields[field_count].out_mask = NULL;
1954 fields[field_count].in_value = fields[field_count].out_value;
1955 fields[field_count].in_check_mask = NULL;
1956 fields[field_count].in_check_value = NULL;
1957 fields[field_count].in_handler = field_le_to_host;
1958 fields[field_count++].in_handler_priv = &(var->fields[j]);
1962 jtag_add_dr_scan(num_fields, fields, -1);
1963 jtag_execute_queue();
1965 for (i = 0; i < argc / 2; i++)
1966 free(fields[i].out_value);
1973 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1977 if (strcmp(args[0], "enable") == 0)
1979 jtag_verify_capture_ir = 1;
1981 else if (strcmp(args[0], "disable") == 0)
1983 jtag_verify_capture_ir = 0;
1986 return ERROR_COMMAND_SYNTAX_ERROR;
1988 } else if (argc != 0)
1990 return ERROR_COMMAND_SYNTAX_ERROR;
1993 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");