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"
36 char* tap_state_strings[16] =
39 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
41 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
44 typedef struct cmd_queue_page_s
48 struct cmd_queue_page_s *next;
51 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
52 static cmd_queue_page_t *cmd_queue_pages = NULL;
54 /* tap_move[i][j]: tap movement command to go from state i to state j
64 /* TLR RTI SD PD SI PI */
65 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
66 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
67 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
68 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
69 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
70 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
73 int tap_move_map[16] = {
74 0, -1, -1, 2, -1, 3, -1, -1,
75 1, -1, -1, 4, -1, 5, -1, -1
78 tap_transition_t tap_transitions[16] =
80 {TAP_TLR, TAP_RTI}, /* TLR */
81 {TAP_SIS, TAP_CD}, /* SDS */
82 {TAP_E1D, TAP_SD}, /* CD */
83 {TAP_E1D, TAP_SD}, /* SD */
84 {TAP_UD, TAP_PD}, /* E1D */
85 {TAP_E2D, TAP_PD}, /* PD */
86 {TAP_UD, TAP_SD}, /* E2D */
87 {TAP_SDS, TAP_RTI}, /* UD */
88 {TAP_SDS, TAP_RTI}, /* RTI */
89 {TAP_TLR, TAP_CI}, /* SIS */
90 {TAP_E1I, TAP_SI}, /* CI */
91 {TAP_E1I, TAP_SI}, /* SI */
92 {TAP_UI, TAP_PI}, /* E1I */
93 {TAP_E2I, TAP_PI}, /* PI */
94 {TAP_UI, TAP_SI}, /* E2I */
95 {TAP_SDS, TAP_RTI} /* UI */
98 enum tap_state end_state = TAP_TLR;
99 enum tap_state cur_state = TAP_TLR;
103 jtag_command_t *jtag_command_queue = NULL;
104 jtag_command_t **last_comand_pointer = &jtag_command_queue;
105 jtag_device_t *jtag_devices = NULL;
106 int jtag_num_devices = 0;
107 int jtag_ir_scan_size = 0;
108 enum reset_types jtag_reset_config = RESET_NONE;
109 enum tap_state cmd_queue_end_state = TAP_TLR;
110 enum tap_state cmd_queue_cur_state = TAP_TLR;
112 int jtag_verify_capture_ir = 1;
114 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
115 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
116 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
118 /* callbacks to inform high-level handlers about JTAG state changes */
119 jtag_event_callback_t *jtag_event_callbacks;
121 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
123 #if BUILD_PARPORT == 1
124 extern jtag_interface_t parport_interface;
127 #if BUILD_FT2232_FTD2XX == 1
128 extern jtag_interface_t ft2232_interface;
131 #if BUILD_FT2232_LIBFTDI == 1
132 extern jtag_interface_t ft2232_interface;
135 #if BUILD_AMTJTAGACCEL == 1
136 extern jtag_interface_t amt_jtagaccel_interface;
139 #if BUILD_EP93XX == 1
140 extern jtag_interface_t ep93xx_interface;
143 #if BUILD_AT91RM9200 == 1
144 extern jtag_interface_t at91rm9200_interface;
147 #if BUILD_GW16012 == 1
148 extern jtag_interface_t gw16012_interface;
151 jtag_interface_t *jtag_interfaces[] = {
152 #if BUILD_PARPORT == 1
155 #if BUILD_FT2232_FTD2XX == 1
158 #if BUILD_FT2232_LIBFTDI == 1
161 #if BUILD_AMTJTAGACCEL == 1
162 &amt_jtagaccel_interface,
164 #if BUILD_EP93XX == 1
167 #if BUILD_AT91RM9200 == 1
168 &at91rm9200_interface,
170 #if BUILD_GW16012 == 1
176 jtag_interface_t *jtag = NULL;
179 char* jtag_interface = NULL;
182 /* forward declarations */
185 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
186 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
187 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
188 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
189 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
190 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
192 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
194 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
195 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
196 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
197 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
198 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
199 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
201 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
203 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
205 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
207 if (callback == NULL)
209 return ERROR_INVALID_ARGUMENTS;
214 while ((*callbacks_p)->next)
215 callbacks_p = &((*callbacks_p)->next);
216 callbacks_p = &((*callbacks_p)->next);
219 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
220 (*callbacks_p)->callback = callback;
221 (*callbacks_p)->priv = priv;
222 (*callbacks_p)->next = NULL;
227 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
229 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
231 if (callback == NULL)
233 return ERROR_INVALID_ARGUMENTS;
238 jtag_event_callback_t **next = &((*callbacks_p)->next);
239 if ((*callbacks_p)->callback == callback)
242 *callbacks_p = *next;
250 int jtag_call_event_callbacks(enum jtag_event event)
252 jtag_event_callback_t *callback = jtag_event_callbacks;
254 DEBUG("jtag event: %i", event);
258 callback->callback(event, callback->priv);
259 callback = callback->next;
265 /* returns a pointer to the pointer of the last command in queue
266 * this may be a pointer to the root pointer (jtag_command_queue)
267 * or to the next member of the last but one command
269 jtag_command_t** jtag_get_last_command_p(void)
271 /* jtag_command_t *cmd = jtag_command_queue;
277 return &jtag_command_queue;
281 return last_comand_pointer;
284 /* returns a pointer to the n-th device in the scan chain */
285 jtag_device_t* jtag_get_device(int num)
287 jtag_device_t *device = jtag_devices;
294 device = device->next;
301 void* cmd_queue_alloc(size_t size)
303 cmd_queue_page_t **p_page = &cmd_queue_pages;
308 while ((*p_page)->next)
309 p_page = &((*p_page)->next);
310 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
311 p_page = &((*p_page)->next);
316 *p_page = malloc(sizeof(cmd_queue_page_t));
318 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
319 (*p_page)->next = NULL;
322 offset = (*p_page)->used;
323 (*p_page)->used += size;
325 return ((*p_page)->address) + offset;
328 void cmd_queue_free()
330 cmd_queue_page_t *page = cmd_queue_pages;
334 cmd_queue_page_t *last = page;
340 cmd_queue_pages = NULL;
343 int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
345 jtag_command_t **last_cmd;
346 jtag_device_t *device;
349 /* int changed = 0; */
353 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
354 return ERROR_JTAG_TRST_ASSERTED;
358 for (i=0; i<num_fields; i++)
360 device = jtag_get_device(fields[i].device);
363 if (buf_cmp(device->cur_instr, fields[i].out_value, device->ir_length))
368 ERROR("inexistant device specified for ir scan");
369 return ERROR_INVALID_ARGUMENTS;
377 last_cmd = jtag_get_last_command_p();
379 /* allocate memory for a new list member */
380 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
381 (*last_cmd)->next = NULL;
382 last_comand_pointer = &((*last_cmd)->next);
383 (*last_cmd)->type = JTAG_SCAN;
385 /* allocate memory for ir scan command */
386 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
387 (*last_cmd)->cmd.scan->ir_scan = 1;
388 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
389 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
390 (*last_cmd)->cmd.scan->end_state = state;
393 cmd_queue_end_state = state;
395 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
396 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
398 if (cmd_queue_end_state == TAP_TLR)
399 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
401 cmd_queue_cur_state = cmd_queue_end_state;
403 for (i=0; i < jtag_num_devices; i++)
406 device = jtag_get_device(i);
407 scan_size = device->ir_length;
408 (*last_cmd)->cmd.scan->fields[i].device = i;
409 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
410 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
411 if (jtag_verify_capture_ir)
413 (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(device->expected, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
414 (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(device->expected_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
418 (*last_cmd)->cmd.scan->fields[i].in_check_value = NULL;
419 (*last_cmd)->cmd.scan->fields[i].in_check_mask = NULL;
421 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
422 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
424 /* search the list */
425 for (j=0; j < num_fields; j++)
427 if (i == fields[j].device)
430 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
431 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
440 /* if a device isn't listed, set it to BYPASS */
441 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
442 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
447 /* update device information */
448 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
454 int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
456 jtag_command_t **last_cmd;
461 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
462 return ERROR_JTAG_TRST_ASSERTED;
465 last_cmd = jtag_get_last_command_p();
467 /* allocate memory for a new list member */
468 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
469 (*last_cmd)->next = NULL;
470 last_comand_pointer = &((*last_cmd)->next);
471 (*last_cmd)->type = JTAG_SCAN;
473 /* allocate memory for ir scan command */
474 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
475 (*last_cmd)->cmd.scan->ir_scan = 1;
476 (*last_cmd)->cmd.scan->num_fields = num_fields;
477 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
478 (*last_cmd)->cmd.scan->end_state = state;
481 cmd_queue_end_state = state;
483 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
484 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
486 if (cmd_queue_end_state == TAP_TLR)
487 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
489 cmd_queue_cur_state = cmd_queue_end_state;
491 for (i = 0; i < num_fields; i++)
493 int num_bits = fields[i].num_bits;
494 int num_bytes = CEIL(fields[i].num_bits, 8);
495 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
496 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
497 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
498 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
499 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
500 (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(fields[i].in_check_value, cmd_queue_alloc(num_bytes), num_bits);
501 (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(fields[i].in_check_mask, cmd_queue_alloc(num_bytes), num_bits);
502 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
503 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
508 int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
511 int bypass_devices = 0;
513 jtag_command_t **last_cmd = jtag_get_last_command_p();
514 jtag_device_t *device = jtag_devices;
519 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
520 return ERROR_JTAG_TRST_ASSERTED;
523 /* count devices in bypass */
528 device = device->next;
531 /* allocate memory for a new list member */
532 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
533 last_comand_pointer = &((*last_cmd)->next);
534 (*last_cmd)->next = NULL;
535 (*last_cmd)->type = JTAG_SCAN;
537 /* allocate memory for dr scan command */
538 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
539 (*last_cmd)->cmd.scan->ir_scan = 0;
540 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
541 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
542 (*last_cmd)->cmd.scan->end_state = state;
545 cmd_queue_end_state = state;
547 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
548 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
550 if (cmd_queue_end_state == TAP_TLR)
551 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
553 cmd_queue_cur_state = cmd_queue_end_state;
555 for (i=0; i < jtag_num_devices; i++)
558 (*last_cmd)->cmd.scan->fields[field_count].device = i;
560 for (j=0; j < num_fields; j++)
562 if (i == fields[j].device)
565 scan_size = fields[j].num_bits;
566 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
567 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
568 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
569 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
570 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = buf_cpy(fields[j].in_check_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
571 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = buf_cpy(fields[j].in_check_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
572 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
573 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
578 /* if a device isn't listed, the BYPASS register should be selected */
579 if (!jtag_get_device(i)->bypass)
581 ERROR("BUG: no scan data for a device not in BYPASS");
585 /* program the scan field to 1 bit length, and ignore it's value */
586 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
587 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
588 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
589 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
590 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
591 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
592 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
593 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
597 /* if a device is listed, the BYPASS register must not be selected */
598 if (jtag_get_device(i)->bypass)
600 ERROR("BUG: scan data for a device in BYPASS");
608 int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
611 jtag_command_t **last_cmd = jtag_get_last_command_p();
615 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
616 return ERROR_JTAG_TRST_ASSERTED;
619 /* allocate memory for a new list member */
620 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
621 last_comand_pointer = &((*last_cmd)->next);
622 (*last_cmd)->next = NULL;
623 (*last_cmd)->type = JTAG_SCAN;
625 /* allocate memory for scan command */
626 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
627 (*last_cmd)->cmd.scan->ir_scan = 0;
628 (*last_cmd)->cmd.scan->num_fields = num_fields;
629 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
630 (*last_cmd)->cmd.scan->end_state = state;
633 cmd_queue_end_state = state;
635 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
636 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
638 if (cmd_queue_end_state == TAP_TLR)
639 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
641 cmd_queue_cur_state = cmd_queue_end_state;
643 for (i = 0; i < num_fields; i++)
645 int num_bits = fields[i].num_bits;
646 int num_bytes = CEIL(fields[i].num_bits, 8);
647 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
648 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
649 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
650 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
651 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
652 (*last_cmd)->cmd.scan->fields[i].in_check_value = buf_cpy(fields[i].in_check_value, cmd_queue_alloc(num_bytes), num_bits);
653 (*last_cmd)->cmd.scan->fields[i].in_check_mask = buf_cpy(fields[i].in_check_mask, cmd_queue_alloc(num_bytes), num_bits);
654 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
655 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
660 int jtag_add_statemove(enum tap_state state)
662 jtag_command_t **last_cmd = jtag_get_last_command_p();
666 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
667 return ERROR_JTAG_TRST_ASSERTED;
670 /* allocate memory for a new list member */
671 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
672 last_comand_pointer = &((*last_cmd)->next);
673 (*last_cmd)->next = NULL;
674 (*last_cmd)->type = JTAG_STATEMOVE;
676 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
677 (*last_cmd)->cmd.statemove->end_state = state;
680 cmd_queue_end_state = state;
682 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
683 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
685 if (cmd_queue_end_state == TAP_TLR)
686 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
688 cmd_queue_cur_state = cmd_queue_end_state;
693 int jtag_add_pathmove(int num_states, enum tap_state *path)
695 jtag_command_t **last_cmd = jtag_get_last_command_p();
700 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
701 return ERROR_JTAG_TRST_ASSERTED;
704 /* the last state has to be a stable state */
705 if (tap_move_map[path[num_states - 1]] == -1)
707 ERROR("TAP path doesn't finish in a stable state");
708 return ERROR_JTAG_NOT_IMPLEMENTED;
711 if (jtag->support_pathmove)
713 /* allocate memory for a new list member */
714 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
715 last_comand_pointer = &((*last_cmd)->next);
716 (*last_cmd)->next = NULL;
717 (*last_cmd)->type = JTAG_RUNTEST;
719 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
720 (*last_cmd)->cmd.pathmove->num_states = num_states;
721 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
723 for (i = 0; i < num_states; i++)
724 (*last_cmd)->cmd.pathmove->path[i] = path[i];
728 /* validate the desired path, and see if it fits a default path */
733 for (i = 0; i < num_states; i++)
735 for (j = i; j < num_states; j++)
737 if (tap_move_map[path[j]] != -1)
744 if (begin - end <= 7) /* a default path spans no more than 7 states */
746 jtag_add_statemove(path[end]);
750 ERROR("encountered a TAP path that can't be fulfilled by default paths");
751 return ERROR_JTAG_NOT_IMPLEMENTED;
758 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
759 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
761 if (cmd_queue_end_state == TAP_TLR)
762 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
764 cmd_queue_cur_state = path[num_states - 1];
769 int jtag_add_runtest(int num_cycles, enum tap_state state)
771 jtag_command_t **last_cmd = jtag_get_last_command_p();
775 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
776 return ERROR_JTAG_TRST_ASSERTED;
779 /* allocate memory for a new list member */
780 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
781 (*last_cmd)->next = NULL;
782 last_comand_pointer = &((*last_cmd)->next);
783 (*last_cmd)->type = JTAG_RUNTEST;
785 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
786 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
787 (*last_cmd)->cmd.runtest->end_state = state;
790 cmd_queue_end_state = state;
792 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
793 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
795 if (cmd_queue_end_state == TAP_TLR)
796 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
798 cmd_queue_cur_state = cmd_queue_end_state;
803 int jtag_add_reset(int req_trst, int req_srst)
805 int trst_with_tms = 0;
807 jtag_command_t **last_cmd = jtag_get_last_command_p();
810 req_trst = jtag_trst;
813 req_srst = jtag_srst;
815 /* Make sure that jtag_reset_config allows the requested reset */
816 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
817 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
818 return ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
820 /* if TRST pulls SRST, we reset with TAP T-L-R */
821 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
827 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
829 ERROR("requested nSRST assertion, but the current configuration doesn't support this");
830 return ERROR_JTAG_RESET_CANT_SRST;
833 if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
839 /* allocate memory for a new list member */
840 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
841 (*last_cmd)->next = NULL;
842 last_comand_pointer = &((*last_cmd)->next);
843 (*last_cmd)->type = JTAG_RESET;
845 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
846 (*last_cmd)->cmd.reset->trst = req_trst;
847 (*last_cmd)->cmd.reset->srst = req_srst;
849 jtag_trst = req_trst;
850 jtag_srst = req_srst;
854 jtag_call_event_callbacks(JTAG_SRST_ASSERTED);
858 jtag_call_event_callbacks(JTAG_SRST_RELEASED);
859 if (jtag_nsrst_delay)
860 jtag_add_sleep(jtag_nsrst_delay * 1000);
865 last_cmd = &((*last_cmd)->next);
867 /* allocate memory for a new list member */
868 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
869 (*last_cmd)->next = NULL;
870 last_comand_pointer = &((*last_cmd)->next);
871 (*last_cmd)->type = JTAG_STATEMOVE;
873 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
874 (*last_cmd)->cmd.statemove->end_state = TAP_TLR;
876 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
877 cmd_queue_cur_state = TAP_TLR;
878 cmd_queue_end_state = TAP_TLR;
886 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
887 * and inform possible listeners about this
889 cmd_queue_cur_state = TAP_TLR;
890 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
894 /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
895 * but we might want to add a delay to give the TAP time to settle
897 if (jtag_ntrst_delay)
898 jtag_add_sleep(jtag_ntrst_delay * 1000);
905 int jtag_add_end_state(enum tap_state state)
907 jtag_command_t **last_cmd = jtag_get_last_command_p();
909 /* allocate memory for a new list member */
910 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
911 (*last_cmd)->next = NULL;
912 last_comand_pointer = &((*last_cmd)->next);
913 (*last_cmd)->type = JTAG_END_STATE;
915 (*last_cmd)->cmd.end_state = cmd_queue_alloc(sizeof(end_state_command_t));
916 (*last_cmd)->cmd.end_state->end_state = state;
919 cmd_queue_end_state = state;
924 int jtag_add_sleep(u32 us)
926 jtag_command_t **last_cmd = jtag_get_last_command_p();
928 /* allocate memory for a new list member */
929 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
930 (*last_cmd)->next = NULL;
931 last_comand_pointer = &((*last_cmd)->next);
932 (*last_cmd)->type = JTAG_SLEEP;
934 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
935 (*last_cmd)->cmd.sleep->us = us;
940 int jtag_scan_size(scan_command_t *cmd)
945 /* count bits in scan command */
946 for (i=0; i<cmd->num_fields; i++)
948 bit_count += cmd->fields[i].num_bits;
954 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
959 bit_count = jtag_scan_size(cmd);
960 *buffer = malloc(CEIL(bit_count, 8));
964 for (i = 0; i < cmd->num_fields; i++)
966 if (cmd->fields[i].out_value)
968 #ifdef _DEBUG_JTAG_IO_
969 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
971 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
972 #ifdef _DEBUG_JTAG_IO_
973 DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
978 bit_count += cmd->fields[i].num_bits;
985 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
989 int retval = ERROR_OK;
991 for (i=0; i < cmd->num_fields; i++)
993 /* if neither in_value, in_check_value nor in_handler
994 * are specified we don't have to examine this field
996 if (cmd->fields[i].in_value || cmd->fields[i].in_check_value || cmd->fields[i].in_handler)
998 int num_bits = cmd->fields[i].num_bits;
999 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1000 #ifdef _DEBUG_JTAG_IO_
1003 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1004 DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1009 if (cmd->fields[i].in_value)
1011 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1013 if (cmd->fields[i].in_handler)
1015 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv) != ERROR_OK)
1017 /* TODO: error reporting */
1018 WARNING("in_handler reported a failed check");
1019 retval = ERROR_JTAG_QUEUE_FAILED;
1024 /* no in_value specified, but a handler takes care of the scanned data */
1025 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1027 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv) != ERROR_OK)
1029 /* TODO: error reporting */
1030 WARNING("in_handler reported a failed check");
1031 retval = ERROR_JTAG_QUEUE_FAILED;
1036 if (cmd->fields[i].in_check_value)
1038 if ((cmd->fields[i].in_check_mask && buf_cmp_mask(captured, cmd->fields[i].in_check_value, cmd->fields[i].in_check_mask, num_bits))
1039 || (!cmd->fields[i].in_check_mask && buf_cmp(captured, cmd->fields[i].in_check_mask, num_bits)))
1041 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1042 char *in_check_value_char = buf_to_str(cmd->fields[i].in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1043 char *in_check_mask_char = buf_to_str(cmd->fields[i].in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1044 /* TODO: error reporting */
1045 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);
1046 retval = ERROR_JTAG_QUEUE_FAILED;
1047 free(captured_char);
1048 free(in_check_value_char);
1049 free(in_check_mask_char);
1054 bit_count += cmd->fields[i].num_bits;
1060 enum scan_type jtag_scan_type(scan_command_t *cmd)
1065 for (i=0; i < cmd->num_fields; i++)
1067 if (cmd->fields[i].in_check_value || cmd->fields[i].in_value || cmd->fields[i].in_handler)
1069 if (cmd->fields[i].out_value)
1076 int jtag_execute_queue(void)
1080 retval = jtag->execute_queue();
1084 jtag_command_queue = NULL;
1085 last_comand_pointer = &jtag_command_queue;
1090 int jtag_cancel_queue(void)
1093 jtag_command_queue = NULL;
1094 last_comand_pointer = &jtag_command_queue;
1099 int jtag_reset_callback(enum jtag_event event, void *priv)
1101 jtag_device_t *device = priv;
1105 if (event == JTAG_TRST_ASSERTED)
1107 buf_set_ones(device->cur_instr, device->ir_length);
1114 void jtag_sleep(u32 us)
1119 int jtag_validate_chain()
1121 jtag_device_t *device = jtag_devices;
1122 int total_ir_length = 0;
1129 total_ir_length += device->ir_length;
1130 device = device->next;
1133 total_ir_length += 2;
1134 ir_test = malloc(CEIL(total_ir_length, 8));
1135 buf_set_ones(ir_test, total_ir_length);
1138 field.num_bits = total_ir_length;
1139 field.out_value = ir_test;
1140 field.out_mask = NULL;
1141 field.in_value = ir_test;
1142 field.in_check_value = NULL;
1143 field.in_check_mask = NULL;
1144 field.in_handler = NULL;
1145 field.in_handler_priv = NULL;
1147 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1148 jtag_execute_queue();
1150 device = jtag_devices;
1153 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1155 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1156 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1160 chain_pos += device->ir_length;
1161 device = device->next;
1164 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1166 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1167 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1177 int jtag_register_commands(struct command_context_s *cmd_ctx)
1179 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1180 COMMAND_CONFIG, NULL);
1181 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1182 COMMAND_ANY, "set jtag speed (if supported) <speed>");
1183 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1184 COMMAND_CONFIG, NULL);
1185 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1186 COMMAND_CONFIG, NULL);
1187 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1188 COMMAND_CONFIG, NULL);
1189 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1190 COMMAND_CONFIG, NULL);
1192 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1193 COMMAND_EXEC, "print current scan chain configuration");
1195 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1196 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1197 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1198 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1199 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1200 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1201 register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
1202 COMMAND_EXEC, "move to current endstate or [tap_state]");
1203 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1204 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1205 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1206 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1208 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1209 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1213 int jtag_init(struct command_context_s *cmd_ctx)
1219 if (jtag_speed == -1)
1222 if (jtag_interface && (jtag_interface[0] != 0))
1223 /* configuration var 'jtag_interface' is set, and not empty */
1224 for (i = 0; jtag_interfaces[i]; i++)
1226 if (strcmp(jtag_interface, jtag_interfaces[i]->name) == 0)
1228 jtag_device_t *device;
1229 device = jtag_devices;
1231 if (jtag_interfaces[i]->init() != ERROR_OK)
1232 return ERROR_JTAG_INIT_FAILED;
1233 jtag = jtag_interfaces[i];
1235 jtag_ir_scan_size = 0;
1236 jtag_num_devices = 0;
1237 while (device != NULL)
1239 jtag_ir_scan_size += device->ir_length;
1241 device = device->next;
1244 jtag_add_statemove(TAP_TLR);
1245 jtag_execute_queue();
1247 jtag_validate_chain();
1253 /* no valid interface was found (i.e. the configuration option,
1254 * didn't match one of the compiled-in interfaces
1256 ERROR("No valid jtag interface found (%s)", jtag_interface);
1257 ERROR("compiled-in jtag interfaces:");
1258 for (i = 0; jtag_interfaces[i]; i++)
1260 ERROR("%i: %s", i, jtag_interfaces[i]->name);
1264 return ERROR_JTAG_INVALID_INTERFACE;
1267 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1271 /* only if the configuration var isn't overwritten from cmdline */
1272 if (!jtag_interface)
1274 if (args[0] && (args[0][0] != 0))
1276 for (i=0; jtag_interfaces[i]; i++)
1278 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1280 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1283 jtag_interface = jtag_interfaces[i]->name;
1290 /* remember the requested interface name, so we can complain about it later */
1291 jtag_interface = strdup(args[0]);
1292 DEBUG("'interface' command didn't specify a valid interface");
1298 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1300 jtag_device_t **last_device_p = &jtag_devices;
1304 while ((*last_device_p)->next)
1305 last_device_p = &((*last_device_p)->next);
1306 last_device_p = &((*last_device_p)->next);
1312 *last_device_p = malloc(sizeof(jtag_device_t));
1313 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1315 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1316 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1317 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1318 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1320 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1321 (*last_device_p)->bypass = 1;
1322 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1324 (*last_device_p)->next = NULL;
1326 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1333 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1335 jtag_device_t *device = jtag_devices;
1336 int device_count = 0;
1340 u32 expected, expected_mask, cur_instr;
1341 expected = buf_get_u32(device->expected, 0, device->ir_length);
1342 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1343 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1344 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);
1345 device = device->next;
1352 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1356 if (strcmp(args[0], "none") == 0)
1357 jtag_reset_config = RESET_NONE;
1358 else if (strcmp(args[0], "trst_only") == 0)
1359 jtag_reset_config = RESET_HAS_TRST;
1360 else if (strcmp(args[0], "srst_only") == 0)
1361 jtag_reset_config = RESET_HAS_SRST;
1362 else if (strcmp(args[0], "trst_and_srst") == 0)
1363 jtag_reset_config = RESET_TRST_AND_SRST;
1366 ERROR("invalid reset_config argument");
1373 if (strcmp(args[1], "srst_pulls_trst") == 0)
1374 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1375 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1376 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1377 else if (strcmp(args[1], "combined") == 0)
1378 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1379 else if (strcmp(args[1], "separate") == 0)
1380 jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
1383 ERROR("invalid reset_config argument");
1390 if (strcmp(args[2], "trst_open_drain") == 0)
1391 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1392 else if (strcmp(args[2], "trst_push_pull") == 0)
1393 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1396 ERROR("invalid reset_config argument");
1403 if (strcmp(args[3], "srst_push_pull") == 0)
1404 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1405 else if (strcmp(args[3], "srst_open_drain") == 0)
1406 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1409 ERROR("invalid reset_config argument");
1417 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1421 ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1426 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1432 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1436 ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1441 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1447 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1450 command_print(cmd_ctx, "jtag_speed: %i", jtag_speed);
1454 /* this command can be called during CONFIG,
1455 * in which case jtag isn't initialized */
1457 jtag->speed(strtoul(args[0], NULL, 0));
1459 jtag_speed = strtoul(args[0], NULL, 0);
1465 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1467 enum tap_state state;
1471 command_print(cmd_ctx, "usage: endstate <tap_state>");
1475 for (state = 0; state < 16; state++)
1477 if (strcmp(args[0], tap_state_strings[state]) == 0)
1479 jtag_add_end_state(state);
1480 jtag_execute_queue();
1487 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1491 char *usage = "usage: jtag_reset <trst> <srst>";
1496 command_print(cmd_ctx, usage);
1500 if (args[0][0] == '1')
1502 else if (args[0][0] == '0')
1506 command_print(cmd_ctx, usage);
1510 if (args[1][0] == '1')
1512 else if (args[1][0] == '0')
1516 command_print(cmd_ctx, usage);
1520 if ((retval = jtag_add_reset(trst, srst)) != ERROR_OK)
1524 case ERROR_JTAG_RESET_WOULD_ASSERT_TRST:
1525 command_print(cmd_ctx, "requested reset would assert trst\nif this is acceptable, use jtag_reset 1 %c", args[1][0]);
1527 case ERROR_JTAG_RESET_CANT_SRST:
1528 command_print(cmd_ctx, "can't assert srst because the current reset_config doesn't support it");
1531 command_print(cmd_ctx, "unknown error");
1534 jtag_execute_queue();
1539 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1543 command_print(cmd_ctx, "usage: runtest <num_cycles>");
1547 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1548 jtag_execute_queue();
1554 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1556 enum tap_state state;
1561 for (state = 0; state < 16; state++)
1563 if (strcmp(args[0], tap_state_strings[state]) == 0)
1570 jtag_add_statemove(state);
1571 jtag_execute_queue();
1577 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1580 scan_field_t *fields;
1582 if ((argc < 2) || (argc % 2))
1584 command_print(cmd_ctx, "usage: irscan <device> <instr> [dev2] [instr2] ...");
1588 fields = malloc(sizeof(scan_field_t) * argc / 2);
1590 for (i = 0; i < argc / 2; i++)
1592 int device = strtoul(args[i*2], NULL, 0);
1593 int field_size = jtag_get_device(device)->ir_length;
1594 fields[i].device = device;
1595 fields[i].out_value = malloc(CEIL(field_size, 8));
1596 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1597 fields[i].out_mask = NULL;
1598 fields[i].in_value = NULL;
1599 fields[i].in_check_mask = NULL;
1600 fields[i].in_handler = NULL;
1601 fields[i].in_handler_priv = NULL;
1604 jtag_add_ir_scan(argc / 2, fields, -1);
1605 jtag_execute_queue();
1607 for (i = 0; i < argc / 2; i++)
1608 free(fields[i].out_value);
1615 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1617 scan_field_t *fields;
1619 int field_count = 0;
1623 if ((argc < 2) || (argc % 2))
1625 command_print(cmd_ctx, "usage: drscan <device> <var> [dev2] [var2]");
1629 for (i = 0; i < argc; i+=2)
1631 var = get_var_by_namenum(args[i+1]);
1634 num_fields += var->num_fields;
1638 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1643 fields = malloc(sizeof(scan_field_t) * num_fields);
1645 for (i = 0; i < argc; i+=2)
1647 var = get_var_by_namenum(args[i+1]);
1649 for (j = 0; j < var->num_fields; j++)
1651 fields[field_count].device = strtol(args[i], NULL, 0);
1652 fields[field_count].num_bits = var->fields[j].num_bits;
1653 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1654 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1655 fields[field_count].out_mask = NULL;
1656 fields[field_count].in_value = fields[field_count].out_value;
1657 fields[field_count].in_check_mask = NULL;
1658 fields[field_count].in_check_value = NULL;
1659 fields[field_count].in_handler = field_le_to_host;
1660 fields[field_count++].in_handler_priv = &(var->fields[j]);
1664 jtag_add_dr_scan(num_fields, fields, -1);
1665 jtag_execute_queue();
1667 for (i = 0; i < argc / 2; i++)
1668 free(fields[i].out_value);
1675 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1679 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
1683 if (strcmp(args[0], "enable") == 0)
1685 jtag_verify_capture_ir = 1;
1687 else if (strcmp(args[0], "disable") == 0)
1689 jtag_verify_capture_ir = 0;