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;
223 /* forward declarations */
224 void jtag_add_statemove(enum tap_state endstate);
225 void jtag_add_pathmove(int num_states, enum tap_state *path);
226 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
227 int jtag_add_reset(int trst, int srst);
228 void jtag_add_end_state(enum tap_state endstate);
229 void jtag_add_sleep(u32 us);
230 int jtag_execute_queue(void);
231 int jtag_cancel_queue(void);
234 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
235 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
236 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
237 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
238 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
239 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
241 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
243 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
244 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
245 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
246 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
247 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
248 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
250 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
252 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
254 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
256 if (callback == NULL)
258 return ERROR_INVALID_ARGUMENTS;
263 while ((*callbacks_p)->next)
264 callbacks_p = &((*callbacks_p)->next);
265 callbacks_p = &((*callbacks_p)->next);
268 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
269 (*callbacks_p)->callback = callback;
270 (*callbacks_p)->priv = priv;
271 (*callbacks_p)->next = NULL;
276 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
278 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
280 if (callback == NULL)
282 return ERROR_INVALID_ARGUMENTS;
287 jtag_event_callback_t **next = &((*callbacks_p)->next);
288 if ((*callbacks_p)->callback == callback)
291 *callbacks_p = *next;
299 int jtag_call_event_callbacks(enum jtag_event event)
301 jtag_event_callback_t *callback = jtag_event_callbacks;
303 DEBUG("jtag event: %s", jtag_event_strings[event]);
307 callback->callback(event, callback->priv);
308 callback = callback->next;
314 /* returns a pointer to the pointer of the last command in queue
315 * this may be a pointer to the root pointer (jtag_command_queue)
316 * or to the next member of the last but one command
318 jtag_command_t** jtag_get_last_command_p(void)
320 /* jtag_command_t *cmd = jtag_command_queue;
326 return &jtag_command_queue;
330 return last_comand_pointer;
333 /* returns a pointer to the n-th device in the scan chain */
334 jtag_device_t* jtag_get_device(int num)
336 jtag_device_t *device = jtag_devices;
343 device = device->next;
347 ERROR("jtag device number %d not defined", num);
351 void* cmd_queue_alloc(size_t size)
353 cmd_queue_page_t **p_page = &cmd_queue_pages;
358 while ((*p_page)->next)
359 p_page = &((*p_page)->next);
360 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
361 p_page = &((*p_page)->next);
366 *p_page = malloc(sizeof(cmd_queue_page_t));
368 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
369 (*p_page)->next = NULL;
372 offset = (*p_page)->used;
373 (*p_page)->used += size;
375 u8 *t=(u8 *)((*p_page)->address);
379 void cmd_queue_free()
381 cmd_queue_page_t *page = cmd_queue_pages;
385 cmd_queue_page_t *last = page;
391 cmd_queue_pages = NULL;
394 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
398 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
399 jtag_error=ERROR_JTAG_TRST_ASSERTED;
404 cmd_queue_end_state = state;
406 if (cmd_queue_end_state == TAP_TLR)
407 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
409 cmd_queue_cur_state = cmd_queue_end_state;
411 int retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
412 if (retval!=ERROR_OK)
416 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
418 jtag_command_t **last_cmd;
419 jtag_device_t *device;
424 last_cmd = jtag_get_last_command_p();
426 /* allocate memory for a new list member */
427 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
428 (*last_cmd)->next = NULL;
429 last_comand_pointer = &((*last_cmd)->next);
430 (*last_cmd)->type = JTAG_SCAN;
432 /* allocate memory for ir scan command */
433 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
434 (*last_cmd)->cmd.scan->ir_scan = 1;
435 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
436 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
437 (*last_cmd)->cmd.scan->end_state = state;
439 for (i = 0; i < jtag_num_devices; i++)
442 device = jtag_get_device(i);
443 scan_size = device->ir_length;
444 (*last_cmd)->cmd.scan->fields[i].device = i;
445 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
446 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
447 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
449 /* search the list */
450 for (j = 0; j < num_fields; j++)
452 if (i == fields[j].device)
455 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
456 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
458 if (jtag_verify_capture_ir)
460 if (fields[j].in_handler==NULL)
462 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
465 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
466 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
467 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
468 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
479 /* if a device isn't listed, set it to BYPASS */
480 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
481 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
486 /* update device information */
487 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
493 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
497 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
498 jtag_error=ERROR_JTAG_TRST_ASSERTED;
503 cmd_queue_end_state = state;
505 if (cmd_queue_end_state == TAP_TLR)
506 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
508 cmd_queue_cur_state = cmd_queue_end_state;
510 int retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
511 if (retval!=ERROR_OK)
515 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
518 jtag_command_t **last_cmd;
520 last_cmd = jtag_get_last_command_p();
522 /* allocate memory for a new list member */
523 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
524 (*last_cmd)->next = NULL;
525 last_comand_pointer = &((*last_cmd)->next);
526 (*last_cmd)->type = JTAG_SCAN;
528 /* allocate memory for ir scan command */
529 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
530 (*last_cmd)->cmd.scan->ir_scan = 1;
531 (*last_cmd)->cmd.scan->num_fields = num_fields;
532 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
533 (*last_cmd)->cmd.scan->end_state = state;
535 for (i = 0; i < num_fields; i++)
537 int num_bits = fields[i].num_bits;
538 int num_bytes = CEIL(fields[i].num_bits, 8);
539 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
540 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
541 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
542 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
543 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
544 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
545 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
546 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
547 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
552 void jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
556 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
557 jtag_error=ERROR_JTAG_TRST_ASSERTED;
562 cmd_queue_end_state = state;
564 if (cmd_queue_end_state == TAP_TLR)
565 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
567 cmd_queue_cur_state = cmd_queue_end_state;
569 int retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
570 if (retval!=ERROR_OK)
574 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
577 int bypass_devices = 0;
581 jtag_command_t **last_cmd = jtag_get_last_command_p();
582 jtag_device_t *device = jtag_devices;
584 /* count devices in bypass */
589 device = device->next;
592 /* allocate memory for a new list member */
593 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
594 last_comand_pointer = &((*last_cmd)->next);
595 (*last_cmd)->next = NULL;
596 (*last_cmd)->type = JTAG_SCAN;
598 /* allocate memory for dr scan command */
599 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
600 (*last_cmd)->cmd.scan->ir_scan = 0;
601 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
602 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
603 (*last_cmd)->cmd.scan->end_state = state;
605 for (i = 0; i < jtag_num_devices; i++)
608 (*last_cmd)->cmd.scan->fields[field_count].device = i;
610 for (j = 0; j < num_fields; j++)
612 if (i == fields[j].device)
615 scan_size = fields[j].num_bits;
616 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
617 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
618 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
619 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
620 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
621 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
622 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
623 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
628 #ifdef _DEBUG_JTAG_IO_
629 /* if a device isn't listed, the BYPASS register should be selected */
630 if (!jtag_get_device(i)->bypass)
632 ERROR("BUG: no scan data for a device not in BYPASS");
636 /* program the scan field to 1 bit length, and ignore it's value */
637 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
638 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
639 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
640 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
641 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
642 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
643 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
644 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
648 #ifdef _DEBUG_JTAG_IO_
649 /* if a device is listed, the BYPASS register must not be selected */
650 if (jtag_get_device(i)->bypass)
652 ERROR("BUG: scan data for a device in BYPASS");
661 void MINIDRIVER(interface_jtag_add_dr_out)(int device_num,
665 enum tap_state end_state)
670 int bypass_devices = 0;
672 jtag_command_t **last_cmd = jtag_get_last_command_p();
673 jtag_device_t *device = jtag_devices;
674 /* count devices in bypass */
679 device = device->next;
682 /* allocate memory for a new list member */
683 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
684 last_comand_pointer = &((*last_cmd)->next);
685 (*last_cmd)->next = NULL;
686 (*last_cmd)->type = JTAG_SCAN;
688 /* allocate memory for dr scan command */
689 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
690 (*last_cmd)->cmd.scan->ir_scan = 0;
691 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
692 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
693 (*last_cmd)->cmd.scan->end_state = end_state;
695 for (i = 0; i < jtag_num_devices; i++)
697 (*last_cmd)->cmd.scan->fields[field_count].device = i;
702 #ifdef _DEBUG_JTAG_IO_
703 /* if a device is listed, the BYPASS register must not be selected */
704 if (jtag_get_device(i)->bypass)
706 ERROR("BUG: scan data for a device in BYPASS");
710 for (j = 0; j < num_fields; j++)
713 scan_size = num_bits[j];
714 buf_set_u32(out_value, 0, scan_size, value[j]);
715 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
716 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
717 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
718 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
719 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
720 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
721 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
722 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
726 #ifdef _DEBUG_JTAG_IO_
727 /* if a device isn't listed, the BYPASS register should be selected */
728 if (!jtag_get_device(i)->bypass)
730 ERROR("BUG: no scan data for a device not in BYPASS");
734 /* program the scan field to 1 bit length, and ignore it's value */
735 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
736 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
737 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
738 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
739 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
740 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
741 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
742 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
750 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
754 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
755 jtag_error=ERROR_JTAG_TRST_ASSERTED;
760 cmd_queue_end_state = state;
762 if (cmd_queue_end_state == TAP_TLR)
763 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
765 cmd_queue_cur_state = cmd_queue_end_state;
767 int retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
768 if (retval!=ERROR_OK)
772 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
775 jtag_command_t **last_cmd = jtag_get_last_command_p();
777 /* allocate memory for a new list member */
778 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
779 last_comand_pointer = &((*last_cmd)->next);
780 (*last_cmd)->next = NULL;
781 (*last_cmd)->type = JTAG_SCAN;
783 /* allocate memory for scan command */
784 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
785 (*last_cmd)->cmd.scan->ir_scan = 0;
786 (*last_cmd)->cmd.scan->num_fields = num_fields;
787 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
788 (*last_cmd)->cmd.scan->end_state = state;
790 for (i = 0; i < num_fields; i++)
792 int num_bits = fields[i].num_bits;
793 int num_bytes = CEIL(fields[i].num_bits, 8);
794 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
795 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
796 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
797 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
798 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
799 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
800 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
801 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
802 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
807 void jtag_add_statemove(enum tap_state state)
811 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
812 jtag_error=ERROR_JTAG_TRST_ASSERTED;
817 cmd_queue_end_state = state;
819 if (cmd_queue_end_state == TAP_TLR)
820 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
822 cmd_queue_cur_state = cmd_queue_end_state;
825 retval=interface_jtag_add_statemove(cmd_queue_end_state);
826 if (retval!=ERROR_OK)
830 int MINIDRIVER(interface_jtag_add_statemove)(enum tap_state state)
832 jtag_command_t **last_cmd = jtag_get_last_command_p();
834 /* allocate memory for a new list member */
835 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
836 last_comand_pointer = &((*last_cmd)->next);
837 (*last_cmd)->next = NULL;
838 (*last_cmd)->type = JTAG_STATEMOVE;
840 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
841 (*last_cmd)->cmd.statemove->end_state = state;
847 void jtag_add_pathmove(int num_states, enum tap_state *path)
851 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
852 jtag_error=ERROR_JTAG_TRST_ASSERTED;
856 if (cmd_queue_end_state == TAP_TLR)
857 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
859 /* the last state has to be a stable state */
860 if (tap_move_map[path[num_states - 1]] == -1)
862 ERROR("TAP path doesn't finish in a stable state");
863 jtag_error=ERROR_JTAG_NOT_IMPLEMENTED;
867 enum tap_state cur_state=cmd_queue_cur_state;
869 for (i=0; i<num_states; i++)
871 if ((tap_transitions[cur_state].low != path[i])&&
872 (tap_transitions[cur_state].high != path[i]))
874 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
880 cmd_queue_cur_state = path[num_states - 1];
882 int retval=interface_jtag_add_pathmove(num_states, path);
883 if (retval!=ERROR_OK)
888 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
890 jtag_command_t **last_cmd = jtag_get_last_command_p();
893 /* allocate memory for a new list member */
894 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
895 last_comand_pointer = &((*last_cmd)->next);
896 (*last_cmd)->next = NULL;
897 (*last_cmd)->type = JTAG_PATHMOVE;
899 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
900 (*last_cmd)->cmd.pathmove->num_states = num_states;
901 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
903 for (i = 0; i < num_states; i++)
904 (*last_cmd)->cmd.pathmove->path[i] = path[i];
909 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
911 jtag_command_t **last_cmd = jtag_get_last_command_p();
913 /* allocate memory for a new list member */
914 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
915 (*last_cmd)->next = NULL;
916 last_comand_pointer = &((*last_cmd)->next);
917 (*last_cmd)->type = JTAG_RUNTEST;
919 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
920 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
921 (*last_cmd)->cmd.runtest->end_state = state;
926 void jtag_add_runtest(int num_cycles, enum tap_state state)
930 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
931 jtag_error=ERROR_JTAG_TRST_ASSERTED;
936 cmd_queue_end_state = state;
938 if (cmd_queue_end_state == TAP_TLR)
939 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
941 cmd_queue_cur_state = cmd_queue_end_state;
943 /* executed by sw or hw fifo */
944 int retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
945 if (retval!=ERROR_OK)
949 int jtag_add_reset(int req_trst, int req_srst)
951 int trst_with_tms = 0;
955 req_trst = jtag_trst;
958 req_srst = jtag_srst;
960 /* Make sure that jtag_reset_config allows the requested reset */
961 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
962 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
964 ERROR("requested reset would assert trst");
965 return ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
968 /* if TRST pulls SRST, we reset with TAP T-L-R */
969 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
975 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
977 ERROR("requested nSRST assertion, but the current configuration doesn't support this");
978 return ERROR_JTAG_RESET_CANT_SRST;
981 if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
987 jtag_trst = req_trst;
988 jtag_srst = req_srst;
990 retval = interface_jtag_add_reset(req_trst, req_srst);
991 if (retval!=ERROR_OK)
999 DEBUG("SRST line asserted");
1003 DEBUG("SRST line released");
1004 if (jtag_nsrst_delay)
1005 jtag_add_sleep(jtag_nsrst_delay * 1000);
1010 DEBUG("JTAG reset with tms instead of TRST");
1011 jtag_add_end_state(TAP_TLR);
1012 jtag_add_statemove(TAP_TLR);
1013 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1019 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1020 * and inform possible listeners about this
1022 DEBUG("TRST line asserted");
1023 cmd_queue_cur_state = TAP_TLR;
1024 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1028 /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
1029 * but we might want to add a delay to give the TAP time to settle
1031 DEBUG("Now in TAP_TLR - Test-Logic-Reset(either due to TRST line asserted or tms reset)");
1032 if (jtag_ntrst_delay)
1033 jtag_add_sleep(jtag_ntrst_delay * 1000);
1038 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1040 jtag_command_t **last_cmd = jtag_get_last_command_p();
1042 /* allocate memory for a new list member */
1043 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1044 (*last_cmd)->next = NULL;
1045 last_comand_pointer = &((*last_cmd)->next);
1046 (*last_cmd)->type = JTAG_RESET;
1048 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1049 (*last_cmd)->cmd.reset->trst = req_trst;
1050 (*last_cmd)->cmd.reset->srst = req_srst;
1056 void jtag_add_end_state(enum tap_state state)
1058 cmd_queue_end_state = state;
1061 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1063 jtag_command_t **last_cmd = jtag_get_last_command_p();
1065 /* allocate memory for a new list member */
1066 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1067 (*last_cmd)->next = NULL;
1068 last_comand_pointer = &((*last_cmd)->next);
1069 (*last_cmd)->type = JTAG_SLEEP;
1071 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1072 (*last_cmd)->cmd.sleep->us = us;
1077 void jtag_add_sleep(u32 us)
1079 int retval=interface_jtag_add_sleep(us);
1080 if (retval!=ERROR_OK)
1085 int jtag_scan_size(scan_command_t *cmd)
1090 /* count bits in scan command */
1091 for (i = 0; i < cmd->num_fields; i++)
1093 bit_count += cmd->fields[i].num_bits;
1099 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1104 bit_count = jtag_scan_size(cmd);
1105 *buffer = malloc(CEIL(bit_count, 8));
1109 for (i = 0; i < cmd->num_fields; i++)
1111 if (cmd->fields[i].out_value)
1113 #ifdef _DEBUG_JTAG_IO_
1114 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1116 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1117 #ifdef _DEBUG_JTAG_IO_
1118 DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1123 bit_count += cmd->fields[i].num_bits;
1130 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1136 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1139 for (i = 0; i < cmd->num_fields; i++)
1141 /* if neither in_value nor in_handler
1142 * are specified we don't have to examine this field
1144 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1146 int num_bits = cmd->fields[i].num_bits;
1147 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1149 #ifdef _DEBUG_JTAG_IO_
1152 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1153 DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1157 if (cmd->fields[i].in_value)
1159 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1161 if (cmd->fields[i].in_handler)
1163 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1165 WARNING("in_handler reported a failed check");
1166 retval = ERROR_JTAG_QUEUE_FAILED;
1171 /* no in_value specified, but a handler takes care of the scanned data */
1172 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1174 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1176 /* We're going to call the error:handler later, but if the in_handler
1177 * reported an error we report this failure upstream
1179 WARNING("in_handler reported a failed check");
1180 retval = ERROR_JTAG_QUEUE_FAILED;
1186 bit_count += cmd->fields[i].num_bits;
1192 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1194 int retval = ERROR_OK;
1195 int num_bits = field->num_bits;
1197 int compare_failed = 0;
1199 if (field->in_check_mask)
1200 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1202 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1206 /* An error handler could have caught the failing check
1207 * only report a problem when there wasn't a handler, or if the handler
1208 * acknowledged the error
1212 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1213 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1215 if (field->in_check_mask)
1217 char *in_check_mask_char;
1218 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1219 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);
1220 free(in_check_mask_char);
1224 WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
1227 free(captured_char);
1228 free(in_check_value_char);
1230 retval = ERROR_JTAG_QUEUE_FAILED;
1238 set up checking of this field using the in_handler. The values passed in must be valid until
1239 after jtag_execute() has completed.
1241 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1244 field->in_handler = jtag_check_value;
1246 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1247 field->in_handler_priv = NULL; /* this will be filled in at the invocation site to point to the field duplicate */
1248 field->in_check_value = value;
1249 field->in_check_mask = mask;
1252 enum scan_type jtag_scan_type(scan_command_t *cmd)
1257 for (i = 0; i < cmd->num_fields; i++)
1259 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1261 if (cmd->fields[i].out_value)
1268 int MINIDRIVER(interface_jtag_execute_queue)(void)
1272 retval = jtag->execute_queue();
1276 jtag_command_queue = NULL;
1277 last_comand_pointer = &jtag_command_queue;
1282 int jtag_execute_queue(void)
1284 int retval=interface_jtag_execute_queue();
1285 if (retval==ERROR_OK)
1289 jtag_error=ERROR_OK;
1293 int jtag_reset_callback(enum jtag_event event, void *priv)
1295 jtag_device_t *device = priv;
1299 if (event == JTAG_TRST_ASSERTED)
1301 buf_set_ones(device->cur_instr, device->ir_length);
1308 void jtag_sleep(u32 us)
1313 /* Try to examine chain layout according to IEEE 1149.1 §12
1315 int jtag_examine_chain()
1317 jtag_device_t *device = jtag_devices;
1319 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1322 int device_count = 0;
1323 u8 zero_check = 0x0;
1324 u8 one_check = 0xff;
1327 field.num_bits = sizeof(idcode_buffer) * 8;
1328 field.out_value = idcode_buffer;
1329 field.out_mask = NULL;
1330 field.in_value = idcode_buffer;
1331 field.in_check_value = NULL;
1332 field.in_check_mask = NULL;
1333 field.in_handler = NULL;
1334 field.in_handler_priv = NULL;
1336 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1338 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1341 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1342 jtag_execute_queue();
1344 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1346 zero_check |= idcode_buffer[i];
1347 one_check &= idcode_buffer[i];
1350 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1351 if ((zero_check == 0x00) || (one_check == 0xff))
1353 ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1354 return ERROR_JTAG_INIT_FAILED;
1357 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1359 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1360 if ((idcode & 1) == 0)
1362 /* LSB must not be 0, this indicates a device in bypass */
1373 if (idcode == 0x000000FF)
1375 /* End of chain (invalid manufacturer ID) */
1381 device->idcode = idcode;
1382 device = device->next;
1386 manufacturer = (idcode & 0xffe) >> 1;
1387 part = (idcode & 0xffff000) >> 12;
1388 version = (idcode & 0xf0000000) >> 28;
1390 INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1391 idcode, manufacturer, part, version);
1397 /* see if number of discovered devices matches configuration */
1398 if (device_count != jtag_num_devices)
1400 ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1401 device_count, jtag_num_devices);
1402 ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1403 return ERROR_JTAG_INIT_FAILED;
1409 int jtag_validate_chain()
1411 jtag_device_t *device = jtag_devices;
1412 int total_ir_length = 0;
1419 total_ir_length += device->ir_length;
1420 device = device->next;
1423 total_ir_length += 2;
1424 ir_test = malloc(CEIL(total_ir_length, 8));
1425 buf_set_ones(ir_test, total_ir_length);
1428 field.num_bits = total_ir_length;
1429 field.out_value = ir_test;
1430 field.out_mask = NULL;
1431 field.in_value = ir_test;
1432 field.in_check_value = NULL;
1433 field.in_check_mask = NULL;
1434 field.in_handler = NULL;
1435 field.in_handler_priv = NULL;
1437 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1438 jtag_execute_queue();
1440 device = jtag_devices;
1443 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1445 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1446 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1449 return ERROR_JTAG_INIT_FAILED;
1451 chain_pos += device->ir_length;
1452 device = device->next;
1455 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1457 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1458 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1461 return ERROR_JTAG_INIT_FAILED;
1469 int jtag_register_commands(struct command_context_s *cmd_ctx)
1471 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1472 COMMAND_CONFIG, NULL);
1473 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1474 COMMAND_ANY, "set jtag speed (if supported) <speed>");
1475 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1476 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1477 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1478 COMMAND_CONFIG, NULL);
1479 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1480 COMMAND_CONFIG, NULL);
1481 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1482 COMMAND_CONFIG, NULL);
1484 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1485 COMMAND_EXEC, "print current scan chain configuration");
1487 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1488 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1489 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1490 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1491 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1492 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1493 register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
1494 COMMAND_EXEC, "move to current endstate or [tap_state]");
1495 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1496 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1497 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1498 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1500 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1501 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1505 int jtag_interface_init(struct command_context_s *cmd_ctx)
1507 if (!jtag_interface)
1509 /* nothing was previously specified by "interface" command */
1510 ERROR("JTAG interface has to be specified, see \"interface\" command");
1511 return ERROR_JTAG_INVALID_INTERFACE;
1514 if (jtag_interface->init() != ERROR_OK)
1515 return ERROR_JTAG_INIT_FAILED;
1517 jtag = jtag_interface;
1521 int jtag_init(struct command_context_s *cmd_ctx)
1523 int validate_tries = 0;
1524 jtag_device_t *device;
1528 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1529 return ERROR_JTAG_INIT_FAILED;
1531 device = jtag_devices;
1532 jtag_ir_scan_size = 0;
1533 jtag_num_devices = 0;
1534 while (device != NULL)
1536 jtag_ir_scan_size += device->ir_length;
1538 device = device->next;
1541 jtag_add_statemove(TAP_TLR);
1542 jtag_execute_queue();
1544 /* examine chain first, as this could discover the real chain layout */
1545 if (jtag_examine_chain() != ERROR_OK)
1547 ERROR("trying to validate configured JTAG chain anyway...");
1550 while (jtag_validate_chain() != ERROR_OK)
1553 if (validate_tries > 5)
1555 ERROR("Could not validate JTAG chain, exit");
1556 return ERROR_JTAG_INVALID_INTERFACE;
1565 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1569 /* check whether the interface is already configured */
1572 WARNING("Interface already configured, ignoring");
1576 /* interface name is a mandatory argument */
1577 if (argc < 1 || args[0][0] == '\0')
1579 return ERROR_COMMAND_SYNTAX_ERROR;
1582 for (i=0; jtag_interfaces[i]; i++)
1584 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1586 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1589 jtag_interface = jtag_interfaces[i];
1594 /* no valid interface was found (i.e. the configuration option,
1595 * didn't match one of the compiled-in interfaces
1597 ERROR("No valid jtag interface found (%s)", args[0]);
1598 ERROR("compiled-in jtag interfaces:");
1599 for (i = 0; jtag_interfaces[i]; i++)
1601 ERROR("%i: %s", i, jtag_interfaces[i]->name);
1604 return ERROR_JTAG_INVALID_INTERFACE;
1607 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1609 jtag_device_t **last_device_p = &jtag_devices;
1613 while ((*last_device_p)->next)
1614 last_device_p = &((*last_device_p)->next);
1615 last_device_p = &((*last_device_p)->next);
1621 *last_device_p = malloc(sizeof(jtag_device_t));
1622 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1624 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1625 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1626 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1627 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1629 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1630 (*last_device_p)->bypass = 1;
1631 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1633 (*last_device_p)->next = NULL;
1635 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1642 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1644 jtag_device_t *device = jtag_devices;
1645 int device_count = 0;
1649 u32 expected, expected_mask, cur_instr;
1650 expected = buf_get_u32(device->expected, 0, device->ir_length);
1651 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1652 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1653 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);
1654 device = device->next;
1661 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1665 if (strcmp(args[0], "none") == 0)
1666 jtag_reset_config = RESET_NONE;
1667 else if (strcmp(args[0], "trst_only") == 0)
1668 jtag_reset_config = RESET_HAS_TRST;
1669 else if (strcmp(args[0], "srst_only") == 0)
1670 jtag_reset_config = RESET_HAS_SRST;
1671 else if (strcmp(args[0], "trst_and_srst") == 0)
1672 jtag_reset_config = RESET_TRST_AND_SRST;
1675 ERROR("invalid reset_config argument, defaulting to none");
1676 jtag_reset_config = RESET_NONE;
1677 return ERROR_INVALID_ARGUMENTS;
1683 if (strcmp(args[1], "srst_pulls_trst") == 0)
1684 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1685 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1686 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1687 else if (strcmp(args[1], "combined") == 0)
1688 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1689 else if (strcmp(args[1], "separate") == 0)
1690 jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
1693 ERROR("invalid reset_config argument, defaulting to none");
1694 jtag_reset_config = RESET_NONE;
1695 return ERROR_INVALID_ARGUMENTS;
1701 if (strcmp(args[2], "trst_open_drain") == 0)
1702 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1703 else if (strcmp(args[2], "trst_push_pull") == 0)
1704 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1707 ERROR("invalid reset_config argument, defaulting to none");
1708 jtag_reset_config = RESET_NONE;
1709 return ERROR_INVALID_ARGUMENTS;
1715 if (strcmp(args[3], "srst_push_pull") == 0)
1716 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1717 else if (strcmp(args[3], "srst_open_drain") == 0)
1718 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1721 ERROR("invalid reset_config argument, defaulting to none");
1722 jtag_reset_config = RESET_NONE;
1723 return ERROR_INVALID_ARGUMENTS;
1730 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1734 ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1739 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1745 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1749 ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1754 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1760 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1763 command_print(cmd_ctx, "jtag_speed: %i", jtag_speed);
1767 jtag_speed = strtoul(args[0], NULL, 0);
1768 /* this command can be called during CONFIG,
1769 * in which case jtag isn't initialized */
1771 jtag->speed(jtag_speed);
1777 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1779 enum tap_state state;
1783 return ERROR_COMMAND_SYNTAX_ERROR;
1787 for (state = 0; state < 16; state++)
1789 if (strcmp(args[0], tap_state_strings[state]) == 0)
1791 jtag_add_end_state(state);
1792 jtag_execute_queue();
1796 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[end_state]);
1801 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1808 return ERROR_COMMAND_SYNTAX_ERROR;
1811 if (args[0][0] == '1')
1813 else if (args[0][0] == '0')
1817 return ERROR_COMMAND_SYNTAX_ERROR;
1820 if (args[1][0] == '1')
1822 else if (args[1][0] == '0')
1826 return ERROR_COMMAND_SYNTAX_ERROR;
1829 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1830 return ERROR_JTAG_INIT_FAILED;
1832 jtag_add_reset(trst, srst);
1833 jtag_execute_queue();
1838 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1842 return ERROR_COMMAND_SYNTAX_ERROR;
1845 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1846 jtag_execute_queue();
1852 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1854 enum tap_state state;
1859 for (state = 0; state < 16; state++)
1861 if (strcmp(args[0], tap_state_strings[state]) == 0)
1868 jtag_add_statemove(state);
1869 jtag_execute_queue();
1875 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1878 scan_field_t *fields;
1880 if ((argc < 2) || (argc % 2))
1882 return ERROR_COMMAND_SYNTAX_ERROR;
1885 fields = malloc(sizeof(scan_field_t) * argc / 2);
1887 for (i = 0; i < argc / 2; i++)
1889 int device = strtoul(args[i*2], NULL, 0);
1890 int field_size = jtag_get_device(device)->ir_length;
1891 fields[i].device = device;
1892 fields[i].out_value = malloc(CEIL(field_size, 8));
1893 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1894 fields[i].out_mask = NULL;
1895 fields[i].in_value = NULL;
1896 fields[i].in_check_mask = NULL;
1897 fields[i].in_handler = NULL;
1898 fields[i].in_handler_priv = NULL;
1901 jtag_add_ir_scan(argc / 2, fields, -1);
1902 jtag_execute_queue();
1904 for (i = 0; i < argc / 2; i++)
1905 free(fields[i].out_value);
1912 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1914 scan_field_t *fields;
1916 int field_count = 0;
1920 if ((argc < 2) || (argc % 2))
1922 return ERROR_COMMAND_SYNTAX_ERROR;
1925 for (i = 0; i < argc; i+=2)
1927 var = get_var_by_namenum(args[i+1]);
1930 num_fields += var->num_fields;
1934 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1939 fields = malloc(sizeof(scan_field_t) * num_fields);
1941 for (i = 0; i < argc; i+=2)
1943 var = get_var_by_namenum(args[i+1]);
1945 for (j = 0; j < var->num_fields; j++)
1947 fields[field_count].device = strtol(args[i], NULL, 0);
1948 fields[field_count].num_bits = var->fields[j].num_bits;
1949 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1950 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1951 fields[field_count].out_mask = NULL;
1952 fields[field_count].in_value = fields[field_count].out_value;
1953 fields[field_count].in_check_mask = NULL;
1954 fields[field_count].in_check_value = NULL;
1955 fields[field_count].in_handler = field_le_to_host;
1956 fields[field_count++].in_handler_priv = &(var->fields[j]);
1960 jtag_add_dr_scan(num_fields, fields, -1);
1961 jtag_execute_queue();
1963 for (i = 0; i < argc / 2; i++)
1964 free(fields[i].out_value);
1971 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1975 if (strcmp(args[0], "enable") == 0)
1977 jtag_verify_capture_ir = 1;
1979 else if (strcmp(args[0], "disable") == 0)
1981 jtag_verify_capture_ir = 0;
1984 return ERROR_COMMAND_SYNTAX_ERROR;
1986 } else if (argc != 0)
1988 return ERROR_COMMAND_SYNTAX_ERROR;
1991 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");