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"
38 /* note that this is not marked as static as it must be available from outside jtag.c for those
39 that implement the jtag_xxx() minidriver layer
41 int jtag_error=ERROR_OK;
44 char* tap_state_strings[16] =
47 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
49 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
52 typedef struct cmd_queue_page_s
56 struct cmd_queue_page_s *next;
59 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
60 static cmd_queue_page_t *cmd_queue_pages = NULL;
62 /* tap_move[i][j]: tap movement command to go from state i to state j
70 * SD->SD and SI->SI have to be caught in interface specific code
74 /* TLR RTI SD PD SI PI */
75 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
76 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
77 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
78 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
79 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
80 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
83 int tap_move_map[16] = {
84 0, -1, -1, 2, -1, 3, -1, -1,
85 1, -1, -1, 4, -1, 5, -1, -1
88 tap_transition_t tap_transitions[16] =
90 {TAP_TLR, TAP_RTI}, /* TLR */
91 {TAP_SIS, TAP_CD}, /* SDS */
92 {TAP_E1D, TAP_SD}, /* CD */
93 {TAP_E1D, TAP_SD}, /* SD */
94 {TAP_UD, TAP_PD}, /* E1D */
95 {TAP_E2D, TAP_PD}, /* PD */
96 {TAP_UD, TAP_SD}, /* E2D */
97 {TAP_SDS, TAP_RTI}, /* UD */
98 {TAP_SDS, TAP_RTI}, /* RTI */
99 {TAP_TLR, TAP_CI}, /* SIS */
100 {TAP_E1I, TAP_SI}, /* CI */
101 {TAP_E1I, TAP_SI}, /* SI */
102 {TAP_UI, TAP_PI}, /* E1I */
103 {TAP_E2I, TAP_PI}, /* PI */
104 {TAP_UI, TAP_SI}, /* E2I */
105 {TAP_SDS, TAP_RTI} /* UI */
108 char* jtag_event_strings[] =
116 enum tap_state end_state = TAP_TLR;
117 enum tap_state cur_state = TAP_TLR;
121 jtag_command_t *jtag_command_queue = NULL;
122 jtag_command_t **last_comand_pointer = &jtag_command_queue;
123 jtag_device_t *jtag_devices = NULL;
124 int jtag_num_devices = 0;
125 int jtag_ir_scan_size = 0;
126 enum reset_types jtag_reset_config = RESET_NONE;
127 enum tap_state cmd_queue_end_state = TAP_TLR;
128 enum tap_state cmd_queue_cur_state = TAP_TLR;
130 int jtag_verify_capture_ir = 1;
132 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
133 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
134 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
136 /* maximum number of JTAG devices expected in the chain
138 #define JTAG_MAX_CHAIN_SIZE 20
140 /* callbacks to inform high-level handlers about JTAG state changes */
141 jtag_event_callback_t *jtag_event_callbacks;
143 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
145 #if BUILD_PARPORT == 1
146 extern jtag_interface_t parport_interface;
149 #if BUILD_FT2232_FTD2XX == 1
150 extern jtag_interface_t ft2232_interface;
153 #if BUILD_FT2232_LIBFTDI == 1
154 extern jtag_interface_t ft2232_interface;
157 #if BUILD_AMTJTAGACCEL == 1
158 extern jtag_interface_t amt_jtagaccel_interface;
161 #if BUILD_EP93XX == 1
162 extern jtag_interface_t ep93xx_interface;
165 #if BUILD_AT91RM9200 == 1
166 extern jtag_interface_t at91rm9200_interface;
169 #if BUILD_GW16012 == 1
170 extern jtag_interface_t gw16012_interface;
173 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
174 extern jtag_interface_t presto_interface;
177 #if BUILD_USBPROG == 1
178 extern jtag_interface_t usbprog_interface;
181 jtag_interface_t *jtag_interfaces[] = {
182 #if BUILD_PARPORT == 1
185 #if BUILD_FT2232_FTD2XX == 1
188 #if BUILD_FT2232_LIBFTDI == 1
191 #if BUILD_AMTJTAGACCEL == 1
192 &amt_jtagaccel_interface,
194 #if BUILD_EP93XX == 1
197 #if BUILD_AT91RM9200 == 1
198 &at91rm9200_interface,
200 #if BUILD_GW16012 == 1
203 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
206 #if BUILD_USBPROG == 1
212 jtag_interface_t *jtag = NULL;
215 jtag_interface_t *jtag_interface = NULL;
219 /* forward declarations */
220 int jtag_add_statemove(enum tap_state endstate);
221 int jtag_add_pathmove(int num_states, enum tap_state *path);
222 int jtag_add_runtest(int num_cycles, enum tap_state endstate);
223 int jtag_add_reset(int trst, int srst);
224 int jtag_add_end_state(enum tap_state endstate);
225 int jtag_add_sleep(u32 us);
226 int jtag_execute_queue(void);
227 int jtag_cancel_queue(void);
230 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
231 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
232 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
233 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
234 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
235 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
237 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
239 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
240 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
241 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
242 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
243 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
244 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
246 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
248 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
250 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
252 if (callback == NULL)
254 return ERROR_INVALID_ARGUMENTS;
259 while ((*callbacks_p)->next)
260 callbacks_p = &((*callbacks_p)->next);
261 callbacks_p = &((*callbacks_p)->next);
264 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
265 (*callbacks_p)->callback = callback;
266 (*callbacks_p)->priv = priv;
267 (*callbacks_p)->next = NULL;
272 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
274 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
276 if (callback == NULL)
278 return ERROR_INVALID_ARGUMENTS;
283 jtag_event_callback_t **next = &((*callbacks_p)->next);
284 if ((*callbacks_p)->callback == callback)
287 *callbacks_p = *next;
295 int jtag_call_event_callbacks(enum jtag_event event)
297 jtag_event_callback_t *callback = jtag_event_callbacks;
299 DEBUG("jtag event: %s", jtag_event_strings[event]);
303 callback->callback(event, callback->priv);
304 callback = callback->next;
310 /* returns a pointer to the pointer of the last command in queue
311 * this may be a pointer to the root pointer (jtag_command_queue)
312 * or to the next member of the last but one command
314 jtag_command_t** jtag_get_last_command_p(void)
316 /* jtag_command_t *cmd = jtag_command_queue;
322 return &jtag_command_queue;
326 return last_comand_pointer;
329 /* returns a pointer to the n-th device in the scan chain */
330 jtag_device_t* jtag_get_device(int num)
332 jtag_device_t *device = jtag_devices;
339 device = device->next;
343 ERROR("jtag device number %d not defined", num);
347 void* cmd_queue_alloc(size_t size)
349 cmd_queue_page_t **p_page = &cmd_queue_pages;
354 while ((*p_page)->next)
355 p_page = &((*p_page)->next);
356 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
357 p_page = &((*p_page)->next);
362 *p_page = malloc(sizeof(cmd_queue_page_t));
364 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
365 (*p_page)->next = NULL;
368 offset = (*p_page)->used;
369 (*p_page)->used += size;
371 u8 *t=(u8 *)((*p_page)->address);
375 void cmd_queue_free()
377 cmd_queue_page_t *page = cmd_queue_pages;
381 cmd_queue_page_t *last = page;
387 cmd_queue_pages = NULL;
390 int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
394 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
395 jtag_error=ERROR_JTAG_TRST_ASSERTED;
396 return ERROR_JTAG_TRST_ASSERTED;
400 cmd_queue_end_state = state;
402 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
403 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
405 if (cmd_queue_end_state == TAP_TLR)
406 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
408 cmd_queue_cur_state = cmd_queue_end_state;
410 int retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
411 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 int 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 return jtag_error=ERROR_JTAG_TRST_ASSERTED;
502 cmd_queue_end_state = state;
504 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
505 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
507 if (cmd_queue_end_state == TAP_TLR)
508 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
510 cmd_queue_cur_state = cmd_queue_end_state;
512 return interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
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 int 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 return jtag_error=ERROR_JTAG_TRST_ASSERTED;
561 cmd_queue_end_state = state;
563 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
564 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
566 if (cmd_queue_end_state == TAP_TLR)
567 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
569 cmd_queue_cur_state = cmd_queue_end_state;
571 return interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
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 int 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 return jtag_error=ERROR_JTAG_TRST_ASSERTED;
759 cmd_queue_end_state = state;
761 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
762 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
764 if (cmd_queue_end_state == TAP_TLR)
765 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
767 cmd_queue_cur_state = cmd_queue_end_state;
769 return interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
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 int jtag_add_statemove(enum tap_state state)
811 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
812 return jtag_error=ERROR_JTAG_TRST_ASSERTED;
816 cmd_queue_end_state = state;
818 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
819 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
821 if (cmd_queue_end_state == TAP_TLR)
822 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
824 cmd_queue_cur_state = cmd_queue_end_state;
826 return interface_jtag_add_statemove(cmd_queue_end_state);
829 int MINIDRIVER(interface_jtag_add_statemove)(enum tap_state state)
831 jtag_command_t **last_cmd = jtag_get_last_command_p();
833 /* allocate memory for a new list member */
834 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
835 last_comand_pointer = &((*last_cmd)->next);
836 (*last_cmd)->next = NULL;
837 (*last_cmd)->type = JTAG_STATEMOVE;
839 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
840 (*last_cmd)->cmd.statemove->end_state = state;
846 int jtag_add_pathmove(int num_states, enum tap_state *path)
850 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
851 return jtag_error=ERROR_JTAG_TRST_ASSERTED;
854 /* the last state has to be a stable state */
855 if (tap_move_map[path[num_states - 1]] == -1)
857 ERROR("TAP path doesn't finish in a stable state");
858 return jtag_error=ERROR_JTAG_NOT_IMPLEMENTED;
861 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
862 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
864 if (cmd_queue_end_state == TAP_TLR)
865 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
868 enum tap_state cur_state=cmd_queue_cur_state;
870 for (i=0; i<num_states; i++)
872 if ((tap_transitions[cur_state].low != path[i])&&
873 (tap_transitions[cur_state].high != path[i]))
875 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
881 cmd_queue_cur_state = path[num_states - 1];
883 return interface_jtag_add_pathmove(num_states, path);
887 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
889 jtag_command_t **last_cmd = jtag_get_last_command_p();
892 /* allocate memory for a new list member */
893 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
894 last_comand_pointer = &((*last_cmd)->next);
895 (*last_cmd)->next = NULL;
896 (*last_cmd)->type = JTAG_PATHMOVE;
898 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
899 (*last_cmd)->cmd.pathmove->num_states = num_states;
900 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
902 for (i = 0; i < num_states; i++)
903 (*last_cmd)->cmd.pathmove->path[i] = path[i];
908 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
910 jtag_command_t **last_cmd = jtag_get_last_command_p();
912 /* allocate memory for a new list member */
913 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
914 (*last_cmd)->next = NULL;
915 last_comand_pointer = &((*last_cmd)->next);
916 (*last_cmd)->type = JTAG_RUNTEST;
918 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
919 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
920 (*last_cmd)->cmd.runtest->end_state = state;
925 int jtag_add_runtest(int num_cycles, enum tap_state state)
929 jtag_error=ERROR_JTAG_QUEUE_FAILED;
930 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
931 return ERROR_JTAG_TRST_ASSERTED;
935 cmd_queue_end_state = state;
937 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
938 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
940 if (cmd_queue_end_state == TAP_TLR)
941 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
943 cmd_queue_cur_state = cmd_queue_end_state;
945 /* executed by sw or hw fifo */
946 return interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
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 return jtag_error=ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
967 /* if TRST pulls SRST, we reset with TAP T-L-R */
968 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
974 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
976 ERROR("requested nSRST assertion, but the current configuration doesn't support this");
977 return jtag_error=ERROR_JTAG_RESET_CANT_SRST;
980 if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
986 jtag_trst = req_trst;
987 jtag_srst = req_srst;
989 retval = interface_jtag_add_reset(req_trst, req_srst);
990 if (retval!=ERROR_OK)
998 jtag_call_event_callbacks(JTAG_SRST_ASSERTED);
1002 jtag_call_event_callbacks(JTAG_SRST_RELEASED);
1003 if (jtag_nsrst_delay)
1004 jtag_add_sleep(jtag_nsrst_delay * 1000);
1009 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1010 jtag_add_end_state(TAP_TLR);
1011 jtag_add_statemove(TAP_TLR);
1018 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1019 * and inform possible listeners about this
1021 cmd_queue_cur_state = TAP_TLR;
1022 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1026 /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
1027 * but we might want to add a delay to give the TAP time to settle
1029 if (jtag_ntrst_delay)
1030 jtag_add_sleep(jtag_ntrst_delay * 1000);
1037 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1039 jtag_command_t **last_cmd = jtag_get_last_command_p();
1041 /* allocate memory for a new list member */
1042 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1043 (*last_cmd)->next = NULL;
1044 last_comand_pointer = &((*last_cmd)->next);
1045 (*last_cmd)->type = JTAG_RESET;
1047 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1048 (*last_cmd)->cmd.reset->trst = req_trst;
1049 (*last_cmd)->cmd.reset->srst = req_srst;
1055 int MINIDRIVER(interface_jtag_add_end_state)(enum tap_state state)
1057 jtag_command_t **last_cmd = jtag_get_last_command_p();
1059 /* allocate memory for a new list member */
1060 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1061 (*last_cmd)->next = NULL;
1062 last_comand_pointer = &((*last_cmd)->next);
1063 (*last_cmd)->type = JTAG_END_STATE;
1065 (*last_cmd)->cmd.end_state = cmd_queue_alloc(sizeof(end_state_command_t));
1066 (*last_cmd)->cmd.end_state->end_state = state;
1071 int jtag_add_end_state(enum tap_state state)
1074 cmd_queue_end_state = state;
1075 int retval = interface_jtag_add_end_state(cmd_queue_end_state);
1079 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1081 jtag_command_t **last_cmd = jtag_get_last_command_p();
1083 /* allocate memory for a new list member */
1084 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1085 (*last_cmd)->next = NULL;
1086 last_comand_pointer = &((*last_cmd)->next);
1087 (*last_cmd)->type = JTAG_SLEEP;
1089 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1090 (*last_cmd)->cmd.sleep->us = us;
1095 int jtag_add_sleep(u32 us)
1097 return interface_jtag_add_sleep(us);
1100 int jtag_scan_size(scan_command_t *cmd)
1105 /* count bits in scan command */
1106 for (i = 0; i < cmd->num_fields; i++)
1108 bit_count += cmd->fields[i].num_bits;
1114 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1119 bit_count = jtag_scan_size(cmd);
1120 *buffer = malloc(CEIL(bit_count, 8));
1124 for (i = 0; i < cmd->num_fields; i++)
1126 if (cmd->fields[i].out_value)
1128 #ifdef _DEBUG_JTAG_IO_
1129 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1131 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1132 #ifdef _DEBUG_JTAG_IO_
1133 DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1138 bit_count += cmd->fields[i].num_bits;
1145 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1151 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1154 for (i = 0; i < cmd->num_fields; i++)
1156 /* if neither in_value nor in_handler
1157 * are specified we don't have to examine this field
1159 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1161 int num_bits = cmd->fields[i].num_bits;
1162 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1164 #ifdef _DEBUG_JTAG_IO_
1167 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1168 DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1172 if (cmd->fields[i].in_value)
1174 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1176 if (cmd->fields[i].in_handler)
1178 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1180 WARNING("in_handler reported a failed check");
1181 retval = ERROR_JTAG_QUEUE_FAILED;
1186 /* no in_value specified, but a handler takes care of the scanned data */
1187 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1189 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1191 /* We're going to call the error:handler later, but if the in_handler
1192 * reported an error we report this failure upstream
1194 WARNING("in_handler reported a failed check");
1195 retval = ERROR_JTAG_QUEUE_FAILED;
1201 bit_count += cmd->fields[i].num_bits;
1207 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1209 int retval = ERROR_OK;
1210 int num_bits = field->num_bits;
1212 int compare_failed = 0;
1214 if (field->in_check_mask)
1215 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1217 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1221 /* An error handler could have caught the failing check
1222 * only report a problem when there wasn't a handler, or if the handler
1223 * acknowledged the error
1227 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1228 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1230 if (field->in_check_mask)
1232 char *in_check_mask_char;
1233 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1234 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);
1235 free(in_check_mask_char);
1239 WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
1242 free(captured_char);
1243 free(in_check_value_char);
1245 retval = ERROR_JTAG_QUEUE_FAILED;
1253 set up checking of this field using the in_handler. The values passed in must be valid until
1254 after jtag_execute() has completed.
1256 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1259 field->in_handler = jtag_check_value;
1261 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1262 field->in_handler_priv = NULL; /* this will be filled in at the invocation site to point to the field duplicate */
1263 field->in_check_value = value;
1264 field->in_check_mask = mask;
1267 enum scan_type jtag_scan_type(scan_command_t *cmd)
1272 for (i = 0; i < cmd->num_fields; i++)
1274 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1276 if (cmd->fields[i].out_value)
1283 int MINIDRIVER(interface_jtag_execute_queue)(void)
1287 retval = jtag->execute_queue();
1291 jtag_command_queue = NULL;
1292 last_comand_pointer = &jtag_command_queue;
1297 int jtag_execute_queue(void)
1299 int retval=interface_jtag_execute_queue();
1300 if (retval==ERROR_OK)
1304 jtag_error=ERROR_OK;
1308 int jtag_reset_callback(enum jtag_event event, void *priv)
1310 jtag_device_t *device = priv;
1314 if (event == JTAG_TRST_ASSERTED)
1316 buf_set_ones(device->cur_instr, device->ir_length);
1323 void jtag_sleep(u32 us)
1328 /* Try to examine chain layout according to IEEE 1149.1 §12
1330 int jtag_examine_chain()
1332 jtag_device_t *device = jtag_devices;
1334 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1337 int device_count = 0;
1338 u8 zero_check = 0x0;
1339 u8 one_check = 0xff;
1342 field.num_bits = sizeof(idcode_buffer) * 8;
1343 field.out_value = idcode_buffer;
1344 field.out_mask = NULL;
1345 field.in_value = idcode_buffer;
1346 field.in_check_value = NULL;
1347 field.in_check_mask = NULL;
1348 field.in_handler = NULL;
1349 field.in_handler_priv = NULL;
1351 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1353 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1356 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1357 jtag_execute_queue();
1359 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1361 zero_check |= idcode_buffer[i];
1362 one_check &= idcode_buffer[i];
1365 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1366 if ((zero_check == 0x00) || (one_check == 0xff))
1368 ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1369 return ERROR_JTAG_INIT_FAILED;
1372 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1374 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1375 if ((idcode & 1) == 0)
1377 /* LSB must not be 0, this indicates a device in bypass */
1388 if (idcode == 0x000000FF)
1390 /* End of chain (invalid manufacturer ID) */
1396 device->idcode = idcode;
1397 device = device->next;
1401 manufacturer = (idcode & 0xffe) >> 1;
1402 part = (idcode & 0xffff000) >> 12;
1403 version = (idcode & 0xf0000000) >> 28;
1405 INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1406 idcode, manufacturer, part, version);
1412 /* see if number of discovered devices matches configuration */
1413 if (device_count != jtag_num_devices)
1415 ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1416 device_count, jtag_num_devices);
1417 ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1418 return ERROR_JTAG_INIT_FAILED;
1424 int jtag_validate_chain()
1426 jtag_device_t *device = jtag_devices;
1427 int total_ir_length = 0;
1434 total_ir_length += device->ir_length;
1435 device = device->next;
1438 total_ir_length += 2;
1439 ir_test = malloc(CEIL(total_ir_length, 8));
1440 buf_set_ones(ir_test, total_ir_length);
1443 field.num_bits = total_ir_length;
1444 field.out_value = ir_test;
1445 field.out_mask = NULL;
1446 field.in_value = ir_test;
1447 field.in_check_value = NULL;
1448 field.in_check_mask = NULL;
1449 field.in_handler = NULL;
1450 field.in_handler_priv = NULL;
1452 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1453 jtag_execute_queue();
1455 device = jtag_devices;
1458 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1460 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1461 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1464 return ERROR_JTAG_INIT_FAILED;
1466 chain_pos += device->ir_length;
1467 device = device->next;
1470 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1472 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1473 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1476 return ERROR_JTAG_INIT_FAILED;
1484 int jtag_register_commands(struct command_context_s *cmd_ctx)
1486 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1487 COMMAND_CONFIG, NULL);
1488 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1489 COMMAND_ANY, "set jtag speed (if supported) <speed>");
1490 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1491 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1492 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1493 COMMAND_CONFIG, NULL);
1494 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1495 COMMAND_CONFIG, NULL);
1496 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1497 COMMAND_CONFIG, NULL);
1499 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1500 COMMAND_EXEC, "print current scan chain configuration");
1502 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1503 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1504 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1505 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1506 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1507 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1508 register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
1509 COMMAND_EXEC, "move to current endstate or [tap_state]");
1510 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1511 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1512 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1513 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1515 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1516 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1520 int jtag_interface_init(struct command_context_s *cmd_ctx)
1522 if (!jtag_interface)
1524 /* nothing was previously specified by "interface" command */
1525 ERROR("JTAG interface has to be specified, see \"interface\" command");
1526 return ERROR_JTAG_INVALID_INTERFACE;
1529 if (jtag_interface->init() != ERROR_OK)
1530 return ERROR_JTAG_INIT_FAILED;
1532 jtag = jtag_interface;
1536 int jtag_init(struct command_context_s *cmd_ctx)
1538 int validate_tries = 0;
1539 jtag_device_t *device;
1543 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1544 return ERROR_JTAG_INIT_FAILED;
1546 device = jtag_devices;
1547 jtag_ir_scan_size = 0;
1548 jtag_num_devices = 0;
1549 while (device != NULL)
1551 jtag_ir_scan_size += device->ir_length;
1553 device = device->next;
1556 jtag_add_statemove(TAP_TLR);
1557 jtag_execute_queue();
1559 /* examine chain first, as this could discover the real chain layout */
1560 if (jtag_examine_chain() != ERROR_OK)
1562 ERROR("trying to validate configured JTAG chain anyway...");
1565 while (jtag_validate_chain() != ERROR_OK)
1568 if (validate_tries > 5)
1570 ERROR("Could not validate JTAG chain, exit");
1571 return ERROR_JTAG_INVALID_INTERFACE;
1579 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1583 /* check whether the interface is already configured */
1586 WARNING("Interface already configured, ignoring");
1590 /* interface name is a mandatory argument */
1591 if (argc < 1 || args[0][0] == '\0')
1593 return ERROR_COMMAND_SYNTAX_ERROR;
1596 for (i=0; jtag_interfaces[i]; i++)
1598 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1600 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1603 jtag_interface = jtag_interfaces[i];
1608 /* no valid interface was found (i.e. the configuration option,
1609 * didn't match one of the compiled-in interfaces
1611 ERROR("No valid jtag interface found (%s)", args[0]);
1612 ERROR("compiled-in jtag interfaces:");
1613 for (i = 0; jtag_interfaces[i]; i++)
1615 ERROR("%i: %s", i, jtag_interfaces[i]->name);
1618 return ERROR_JTAG_INVALID_INTERFACE;
1621 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1623 jtag_device_t **last_device_p = &jtag_devices;
1627 while ((*last_device_p)->next)
1628 last_device_p = &((*last_device_p)->next);
1629 last_device_p = &((*last_device_p)->next);
1635 *last_device_p = malloc(sizeof(jtag_device_t));
1636 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1638 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1639 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1640 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1641 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1643 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1644 (*last_device_p)->bypass = 1;
1645 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1647 (*last_device_p)->next = NULL;
1649 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1656 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1658 jtag_device_t *device = jtag_devices;
1659 int device_count = 0;
1663 u32 expected, expected_mask, cur_instr;
1664 expected = buf_get_u32(device->expected, 0, device->ir_length);
1665 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1666 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1667 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);
1668 device = device->next;
1675 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1679 if (strcmp(args[0], "none") == 0)
1680 jtag_reset_config = RESET_NONE;
1681 else if (strcmp(args[0], "trst_only") == 0)
1682 jtag_reset_config = RESET_HAS_TRST;
1683 else if (strcmp(args[0], "srst_only") == 0)
1684 jtag_reset_config = RESET_HAS_SRST;
1685 else if (strcmp(args[0], "trst_and_srst") == 0)
1686 jtag_reset_config = RESET_TRST_AND_SRST;
1689 ERROR("invalid reset_config argument, defaulting to none");
1690 jtag_reset_config = RESET_NONE;
1691 return ERROR_INVALID_ARGUMENTS;
1697 if (strcmp(args[1], "srst_pulls_trst") == 0)
1698 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1699 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1700 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1701 else if (strcmp(args[1], "combined") == 0)
1702 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1703 else if (strcmp(args[1], "separate") == 0)
1704 jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
1707 ERROR("invalid reset_config argument, defaulting to none");
1708 jtag_reset_config = RESET_NONE;
1709 return ERROR_INVALID_ARGUMENTS;
1715 if (strcmp(args[2], "trst_open_drain") == 0)
1716 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1717 else if (strcmp(args[2], "trst_push_pull") == 0)
1718 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1721 ERROR("invalid reset_config argument, defaulting to none");
1722 jtag_reset_config = RESET_NONE;
1723 return ERROR_INVALID_ARGUMENTS;
1729 if (strcmp(args[3], "srst_push_pull") == 0)
1730 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1731 else if (strcmp(args[3], "srst_open_drain") == 0)
1732 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1735 ERROR("invalid reset_config argument, defaulting to none");
1736 jtag_reset_config = RESET_NONE;
1737 return ERROR_INVALID_ARGUMENTS;
1744 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1748 ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1753 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1759 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1763 ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1768 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1774 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1777 command_print(cmd_ctx, "jtag_speed: %i", jtag_speed);
1781 jtag_speed = strtoul(args[0], NULL, 0);
1782 /* this command can be called during CONFIG,
1783 * in which case jtag isn't initialized */
1785 jtag->speed(jtag_speed);
1791 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1793 enum tap_state state;
1797 return ERROR_COMMAND_SYNTAX_ERROR;
1801 for (state = 0; state < 16; state++)
1803 if (strcmp(args[0], tap_state_strings[state]) == 0)
1805 jtag_add_end_state(state);
1806 jtag_execute_queue();
1810 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[end_state]);
1815 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1823 return ERROR_COMMAND_SYNTAX_ERROR;
1827 if (args[0][0] == '1')
1829 else if (args[0][0] == '0')
1833 return ERROR_COMMAND_SYNTAX_ERROR;
1836 if (args[1][0] == '1')
1838 else if (args[1][0] == '0')
1842 return ERROR_COMMAND_SYNTAX_ERROR;
1845 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1846 return ERROR_JTAG_INIT_FAILED;
1848 if ((retval = jtag_add_reset(trst, srst)) != ERROR_OK)
1852 case ERROR_JTAG_RESET_WOULD_ASSERT_TRST:
1853 command_print(cmd_ctx, "requested reset would assert trst\nif this is acceptable, use jtag_reset 1 %c", args[1][0]);
1855 case ERROR_JTAG_RESET_CANT_SRST:
1856 command_print(cmd_ctx, "can't assert srst because the current reset_config doesn't support it");
1859 command_print(cmd_ctx, "unknown error");
1862 jtag_execute_queue();
1867 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1871 return ERROR_COMMAND_SYNTAX_ERROR;
1874 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1875 jtag_execute_queue();
1881 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1883 enum tap_state state;
1888 for (state = 0; state < 16; state++)
1890 if (strcmp(args[0], tap_state_strings[state]) == 0)
1897 jtag_add_statemove(state);
1898 jtag_execute_queue();
1904 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1907 scan_field_t *fields;
1909 if ((argc < 2) || (argc % 2))
1911 return ERROR_COMMAND_SYNTAX_ERROR;
1914 fields = malloc(sizeof(scan_field_t) * argc / 2);
1916 for (i = 0; i < argc / 2; i++)
1918 int device = strtoul(args[i*2], NULL, 0);
1919 int field_size = jtag_get_device(device)->ir_length;
1920 fields[i].device = device;
1921 fields[i].out_value = malloc(CEIL(field_size, 8));
1922 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1923 fields[i].out_mask = NULL;
1924 fields[i].in_value = NULL;
1925 fields[i].in_check_mask = NULL;
1926 fields[i].in_handler = NULL;
1927 fields[i].in_handler_priv = NULL;
1930 jtag_add_ir_scan(argc / 2, fields, -1);
1931 jtag_execute_queue();
1933 for (i = 0; i < argc / 2; i++)
1934 free(fields[i].out_value);
1941 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1943 scan_field_t *fields;
1945 int field_count = 0;
1949 if ((argc < 2) || (argc % 2))
1951 return ERROR_COMMAND_SYNTAX_ERROR;
1954 for (i = 0; i < argc; i+=2)
1956 var = get_var_by_namenum(args[i+1]);
1959 num_fields += var->num_fields;
1963 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1968 fields = malloc(sizeof(scan_field_t) * num_fields);
1970 for (i = 0; i < argc; i+=2)
1972 var = get_var_by_namenum(args[i+1]);
1974 for (j = 0; j < var->num_fields; j++)
1976 fields[field_count].device = strtol(args[i], NULL, 0);
1977 fields[field_count].num_bits = var->fields[j].num_bits;
1978 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1979 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1980 fields[field_count].out_mask = NULL;
1981 fields[field_count].in_value = fields[field_count].out_value;
1982 fields[field_count].in_check_mask = NULL;
1983 fields[field_count].in_check_value = NULL;
1984 fields[field_count].in_handler = field_le_to_host;
1985 fields[field_count++].in_handler_priv = &(var->fields[j]);
1989 jtag_add_dr_scan(num_fields, fields, -1);
1990 jtag_execute_queue();
1992 for (i = 0; i < argc / 2; i++)
1993 free(fields[i].out_value);
2000 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2004 if (strcmp(args[0], "enable") == 0)
2006 jtag_verify_capture_ir = 1;
2008 else if (strcmp(args[0], "disable") == 0)
2010 jtag_verify_capture_ir = 0;
2013 return ERROR_COMMAND_SYNTAX_ERROR;
2015 } else if (argc != 0)
2017 return ERROR_COMMAND_SYNTAX_ERROR;
2020 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");