1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "replacements.h"
30 #include "interpreter.h"
37 /* this allows JTAG devices to implement the entire jtag_xxx() layer in hw/sw */
38 #define MINIDRIVER(a) a
42 /* note that this is not marked as static as it must be available from outside jtag.c for those
43 that implement the jtag_xxx() minidriver layer
45 int jtag_error=ERROR_OK;
48 char* tap_state_strings[16] =
51 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
53 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
56 typedef struct cmd_queue_page_s
60 struct cmd_queue_page_s *next;
63 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
64 static cmd_queue_page_t *cmd_queue_pages = NULL;
66 /* tap_move[i][j]: tap movement command to go from state i to state j
74 * SD->SD and SI->SI have to be caught in interface specific code
78 /* TLR RTI SD PD SI PI */
79 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
80 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
81 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
82 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
83 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
84 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
87 int tap_move_map[16] = {
88 0, -1, -1, 2, -1, 3, -1, -1,
89 1, -1, -1, 4, -1, 5, -1, -1
92 tap_transition_t tap_transitions[16] =
94 {TAP_TLR, TAP_RTI}, /* TLR */
95 {TAP_SIS, TAP_CD}, /* SDS */
96 {TAP_E1D, TAP_SD}, /* CD */
97 {TAP_E1D, TAP_SD}, /* SD */
98 {TAP_UD, TAP_PD}, /* E1D */
99 {TAP_E2D, TAP_PD}, /* PD */
100 {TAP_UD, TAP_SD}, /* E2D */
101 {TAP_SDS, TAP_RTI}, /* UD */
102 {TAP_SDS, TAP_RTI}, /* RTI */
103 {TAP_TLR, TAP_CI}, /* SIS */
104 {TAP_E1I, TAP_SI}, /* CI */
105 {TAP_E1I, TAP_SI}, /* SI */
106 {TAP_UI, TAP_PI}, /* E1I */
107 {TAP_E2I, TAP_PI}, /* PI */
108 {TAP_UI, TAP_SI}, /* E2I */
109 {TAP_SDS, TAP_RTI} /* UI */
112 char* jtag_event_strings[] =
120 enum tap_state end_state = TAP_TLR;
121 enum tap_state cur_state = TAP_TLR;
125 jtag_command_t *jtag_command_queue = NULL;
126 jtag_command_t **last_comand_pointer = &jtag_command_queue;
127 jtag_device_t *jtag_devices = NULL;
128 int jtag_num_devices = 0;
129 int jtag_ir_scan_size = 0;
130 enum reset_types jtag_reset_config = RESET_NONE;
131 enum tap_state cmd_queue_end_state = TAP_TLR;
132 enum tap_state cmd_queue_cur_state = TAP_TLR;
134 int jtag_verify_capture_ir = 1;
136 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
137 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
138 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
140 /* maximum number of JTAG devices expected in the chain
142 #define JTAG_MAX_CHAIN_SIZE 20
144 /* callbacks to inform high-level handlers about JTAG state changes */
145 jtag_event_callback_t *jtag_event_callbacks;
147 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
149 #if BUILD_PARPORT == 1
150 extern jtag_interface_t parport_interface;
153 #if BUILD_FT2232_FTD2XX == 1
154 extern jtag_interface_t ft2232_interface;
157 #if BUILD_FT2232_LIBFTDI == 1
158 extern jtag_interface_t ft2232_interface;
161 #if BUILD_AMTJTAGACCEL == 1
162 extern jtag_interface_t amt_jtagaccel_interface;
165 #if BUILD_EP93XX == 1
166 extern jtag_interface_t ep93xx_interface;
169 #if BUILD_AT91RM9200 == 1
170 extern jtag_interface_t at91rm9200_interface;
173 #if BUILD_GW16012 == 1
174 extern jtag_interface_t gw16012_interface;
177 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
178 extern jtag_interface_t presto_interface;
181 #if BUILD_USBPROG == 1
182 extern jtag_interface_t usbprog_interface;
185 jtag_interface_t *jtag_interfaces[] = {
186 #if BUILD_PARPORT == 1
189 #if BUILD_FT2232_FTD2XX == 1
192 #if BUILD_FT2232_LIBFTDI == 1
195 #if BUILD_AMTJTAGACCEL == 1
196 &amt_jtagaccel_interface,
198 #if BUILD_EP93XX == 1
201 #if BUILD_AT91RM9200 == 1
202 &at91rm9200_interface,
204 #if BUILD_GW16012 == 1
207 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
210 #if BUILD_USBPROG == 1
216 jtag_interface_t *jtag = NULL;
219 jtag_interface_t *jtag_interface = NULL;
223 /* forward declarations */
224 int jtag_add_statemove(enum tap_state endstate);
225 int jtag_add_pathmove(int num_states, enum tap_state *path);
226 int jtag_add_runtest(int num_cycles, enum tap_state endstate);
227 int jtag_add_reset(int trst, int srst);
228 int jtag_add_end_state(enum tap_state endstate);
229 int jtag_add_sleep(u32 us);
230 int jtag_execute_queue(void);
231 int jtag_cancel_queue(void);
234 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
235 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
236 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
237 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
238 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
239 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
241 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
243 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
244 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
245 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
246 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
247 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
248 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
250 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
252 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
254 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
256 if (callback == NULL)
258 return ERROR_INVALID_ARGUMENTS;
263 while ((*callbacks_p)->next)
264 callbacks_p = &((*callbacks_p)->next);
265 callbacks_p = &((*callbacks_p)->next);
268 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
269 (*callbacks_p)->callback = callback;
270 (*callbacks_p)->priv = priv;
271 (*callbacks_p)->next = NULL;
276 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
278 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
280 if (callback == NULL)
282 return ERROR_INVALID_ARGUMENTS;
287 jtag_event_callback_t **next = &((*callbacks_p)->next);
288 if ((*callbacks_p)->callback == callback)
291 *callbacks_p = *next;
299 int jtag_call_event_callbacks(enum jtag_event event)
301 jtag_event_callback_t *callback = jtag_event_callbacks;
303 DEBUG("jtag event: %s", jtag_event_strings[event]);
307 callback->callback(event, callback->priv);
308 callback = callback->next;
314 /* returns a pointer to the pointer of the last command in queue
315 * this may be a pointer to the root pointer (jtag_command_queue)
316 * or to the next member of the last but one command
318 jtag_command_t** jtag_get_last_command_p(void)
320 /* jtag_command_t *cmd = jtag_command_queue;
326 return &jtag_command_queue;
330 return last_comand_pointer;
333 /* returns a pointer to the n-th device in the scan chain */
334 jtag_device_t* jtag_get_device(int num)
336 jtag_device_t *device = jtag_devices;
343 device = device->next;
347 ERROR("jtag device number %d not defined", num);
351 void* cmd_queue_alloc(size_t size)
353 cmd_queue_page_t **p_page = &cmd_queue_pages;
358 while ((*p_page)->next)
359 p_page = &((*p_page)->next);
360 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
361 p_page = &((*p_page)->next);
366 *p_page = malloc(sizeof(cmd_queue_page_t));
368 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
369 (*p_page)->next = NULL;
372 offset = (*p_page)->used;
373 (*p_page)->used += size;
375 u8 *t=(u8 *)((*p_page)->address);
379 void cmd_queue_free()
381 cmd_queue_page_t *page = cmd_queue_pages;
385 cmd_queue_page_t *last = page;
391 cmd_queue_pages = NULL;
394 int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
398 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
399 jtag_error=ERROR_JTAG_TRST_ASSERTED;
400 return ERROR_JTAG_TRST_ASSERTED;
404 cmd_queue_end_state = state;
406 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
407 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
409 if (cmd_queue_end_state == TAP_TLR)
410 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
412 cmd_queue_cur_state = cmd_queue_end_state;
414 int retval=interface_jtag_add_ir_scan(num_fields, fields, state);
415 if (retval!=ERROR_OK)
420 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
422 jtag_command_t **last_cmd;
423 jtag_device_t *device;
428 last_cmd = jtag_get_last_command_p();
430 /* allocate memory for a new list member */
431 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
432 (*last_cmd)->next = NULL;
433 last_comand_pointer = &((*last_cmd)->next);
434 (*last_cmd)->type = JTAG_SCAN;
436 /* allocate memory for ir scan command */
437 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
438 (*last_cmd)->cmd.scan->ir_scan = 1;
439 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
440 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
441 (*last_cmd)->cmd.scan->end_state = state;
443 for (i = 0; i < jtag_num_devices; i++)
446 device = jtag_get_device(i);
447 scan_size = device->ir_length;
448 (*last_cmd)->cmd.scan->fields[i].device = i;
449 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
450 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
451 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
453 /* search the list */
454 for (j = 0; j < num_fields; j++)
456 if (i == fields[j].device)
459 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
460 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
462 if (jtag_verify_capture_ir)
464 if (fields[j].in_handler==NULL)
466 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
469 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
470 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
471 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
472 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
483 /* if a device isn't listed, set it to BYPASS */
484 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
485 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
490 /* update device information */
491 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
497 int jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
501 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
502 return jtag_error=ERROR_JTAG_TRST_ASSERTED;
506 cmd_queue_end_state = state;
508 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
509 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
511 if (cmd_queue_end_state == TAP_TLR)
512 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
514 cmd_queue_cur_state = cmd_queue_end_state;
516 return interface_jtag_add_plain_ir_scan(num_fields, fields, state);
519 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
522 jtag_command_t **last_cmd;
524 last_cmd = jtag_get_last_command_p();
526 /* allocate memory for a new list member */
527 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
528 (*last_cmd)->next = NULL;
529 last_comand_pointer = &((*last_cmd)->next);
530 (*last_cmd)->type = JTAG_SCAN;
532 /* allocate memory for ir scan command */
533 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
534 (*last_cmd)->cmd.scan->ir_scan = 1;
535 (*last_cmd)->cmd.scan->num_fields = num_fields;
536 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
537 (*last_cmd)->cmd.scan->end_state = state;
539 for (i = 0; i < num_fields; i++)
541 int num_bits = fields[i].num_bits;
542 int num_bytes = CEIL(fields[i].num_bits, 8);
543 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
544 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
545 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
546 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
547 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
548 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
549 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
550 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
551 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
556 int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
560 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
561 return jtag_error=ERROR_JTAG_TRST_ASSERTED;
565 cmd_queue_end_state = state;
567 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
568 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
570 if (cmd_queue_end_state == TAP_TLR)
571 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
573 cmd_queue_cur_state = cmd_queue_end_state;
575 return interface_jtag_add_dr_scan(num_fields, fields, state);
578 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
581 int bypass_devices = 0;
585 jtag_command_t **last_cmd = jtag_get_last_command_p();
586 jtag_device_t *device = jtag_devices;
588 /* count devices in bypass */
593 device = device->next;
596 /* allocate memory for a new list member */
597 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
598 last_comand_pointer = &((*last_cmd)->next);
599 (*last_cmd)->next = NULL;
600 (*last_cmd)->type = JTAG_SCAN;
602 /* allocate memory for dr scan command */
603 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
604 (*last_cmd)->cmd.scan->ir_scan = 0;
605 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
606 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
607 (*last_cmd)->cmd.scan->end_state = state;
609 for (i = 0; i < jtag_num_devices; i++)
612 (*last_cmd)->cmd.scan->fields[field_count].device = i;
614 for (j = 0; j < num_fields; j++)
616 if (i == fields[j].device)
619 scan_size = fields[j].num_bits;
620 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
621 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
622 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
623 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
624 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
625 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
626 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
627 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
632 /* if a device isn't listed, the BYPASS register should be selected */
633 if (!jtag_get_device(i)->bypass)
635 ERROR("BUG: no scan data for a device not in BYPASS");
639 /* program the scan field to 1 bit length, and ignore it's value */
640 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
641 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
642 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
643 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
644 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
645 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
646 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
647 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
651 /* if a device is listed, the BYPASS register must not be selected */
652 if (jtag_get_device(i)->bypass)
654 WARNING("scan data for a device in BYPASS");
662 int jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
666 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
667 return jtag_error=ERROR_JTAG_TRST_ASSERTED;
671 cmd_queue_end_state = state;
673 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
674 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
676 if (cmd_queue_end_state == TAP_TLR)
677 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
679 cmd_queue_cur_state = cmd_queue_end_state;
681 return interface_jtag_add_plain_dr_scan(num_fields, fields, state);
684 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
687 jtag_command_t **last_cmd = jtag_get_last_command_p();
689 /* allocate memory for a new list member */
690 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
691 last_comand_pointer = &((*last_cmd)->next);
692 (*last_cmd)->next = NULL;
693 (*last_cmd)->type = JTAG_SCAN;
695 /* allocate memory for scan command */
696 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
697 (*last_cmd)->cmd.scan->ir_scan = 0;
698 (*last_cmd)->cmd.scan->num_fields = num_fields;
699 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
700 (*last_cmd)->cmd.scan->end_state = state;
702 for (i = 0; i < num_fields; i++)
704 int num_bits = fields[i].num_bits;
705 int num_bytes = CEIL(fields[i].num_bits, 8);
706 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
707 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
708 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
709 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
710 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
711 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
712 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
713 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
714 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
719 int jtag_add_statemove(enum tap_state state)
723 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
724 return jtag_error=ERROR_JTAG_TRST_ASSERTED;
728 cmd_queue_end_state = state;
730 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
731 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
733 if (cmd_queue_end_state == TAP_TLR)
734 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
736 cmd_queue_cur_state = cmd_queue_end_state;
738 return interface_jtag_add_statemove(state);
741 int MINIDRIVER(interface_jtag_add_statemove)(enum tap_state state)
743 jtag_command_t **last_cmd = jtag_get_last_command_p();
745 /* allocate memory for a new list member */
746 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
747 last_comand_pointer = &((*last_cmd)->next);
748 (*last_cmd)->next = NULL;
749 (*last_cmd)->type = JTAG_STATEMOVE;
751 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
752 (*last_cmd)->cmd.statemove->end_state = state;
758 int jtag_add_pathmove(int num_states, enum tap_state *path)
762 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
763 return jtag_error=ERROR_JTAG_TRST_ASSERTED;
766 /* the last state has to be a stable state */
767 if (tap_move_map[path[num_states - 1]] == -1)
769 ERROR("TAP path doesn't finish in a stable state");
770 return jtag_error=ERROR_JTAG_NOT_IMPLEMENTED;
773 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
774 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
776 if (cmd_queue_end_state == TAP_TLR)
777 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
779 cmd_queue_cur_state = path[num_states - 1];
781 return interface_jtag_add_pathmove(num_states, path);
785 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
787 jtag_command_t **last_cmd = jtag_get_last_command_p();
790 /* allocate memory for a new list member */
791 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
792 last_comand_pointer = &((*last_cmd)->next);
793 (*last_cmd)->next = NULL;
794 (*last_cmd)->type = JTAG_PATHMOVE;
796 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
797 (*last_cmd)->cmd.pathmove->num_states = num_states;
798 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
800 for (i = 0; i < num_states; i++)
801 (*last_cmd)->cmd.pathmove->path[i] = path[i];
806 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
808 jtag_command_t **last_cmd = jtag_get_last_command_p();
810 /* allocate memory for a new list member */
811 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
812 (*last_cmd)->next = NULL;
813 last_comand_pointer = &((*last_cmd)->next);
814 (*last_cmd)->type = JTAG_RUNTEST;
816 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
817 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
818 (*last_cmd)->cmd.runtest->end_state = state;
823 int jtag_add_runtest(int num_cycles, enum tap_state state)
827 jtag_error=ERROR_JTAG_QUEUE_FAILED;
828 WARNING("JTAG command queued, while TRST is low (TAP in reset)");
829 return ERROR_JTAG_TRST_ASSERTED;
833 cmd_queue_end_state = state;
835 if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
836 jtag_call_event_callbacks(JTAG_TRST_RELEASED);
838 if (cmd_queue_end_state == TAP_TLR)
839 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
841 cmd_queue_cur_state = cmd_queue_end_state;
843 /* executed by sw or hw fifo */
844 return interface_jtag_add_runtest(num_cycles, state);
847 int jtag_add_reset(int req_trst, int req_srst)
849 int trst_with_tms = 0;
853 req_trst = jtag_trst;
856 req_srst = jtag_srst;
858 /* Make sure that jtag_reset_config allows the requested reset */
859 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
860 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
862 return jtag_error=ERROR_JTAG_RESET_WOULD_ASSERT_TRST;
865 /* if TRST pulls SRST, we reset with TAP T-L-R */
866 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
872 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
874 ERROR("requested nSRST assertion, but the current configuration doesn't support this");
875 return jtag_error=ERROR_JTAG_RESET_CANT_SRST;
878 if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
884 jtag_trst = req_trst;
885 jtag_srst = req_srst;
887 retval = interface_jtag_add_reset(req_trst, req_srst);
888 if (retval!=ERROR_OK)
896 jtag_call_event_callbacks(JTAG_SRST_ASSERTED);
900 jtag_call_event_callbacks(JTAG_SRST_RELEASED);
901 if (jtag_nsrst_delay)
902 jtag_add_sleep(jtag_nsrst_delay * 1000);
907 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
908 jtag_add_end_state(TAP_TLR);
909 jtag_add_statemove(TAP_TLR);
916 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
917 * and inform possible listeners about this
919 cmd_queue_cur_state = TAP_TLR;
920 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
924 /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
925 * but we might want to add a delay to give the TAP time to settle
927 if (jtag_ntrst_delay)
928 jtag_add_sleep(jtag_ntrst_delay * 1000);
935 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
937 jtag_command_t **last_cmd = jtag_get_last_command_p();
939 /* allocate memory for a new list member */
940 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
941 (*last_cmd)->next = NULL;
942 last_comand_pointer = &((*last_cmd)->next);
943 (*last_cmd)->type = JTAG_RESET;
945 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
946 (*last_cmd)->cmd.reset->trst = req_trst;
947 (*last_cmd)->cmd.reset->srst = req_srst;
953 int MINIDRIVER(interface_jtag_add_end_state)(enum tap_state state)
955 jtag_command_t **last_cmd = jtag_get_last_command_p();
957 /* allocate memory for a new list member */
958 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
959 (*last_cmd)->next = NULL;
960 last_comand_pointer = &((*last_cmd)->next);
961 (*last_cmd)->type = JTAG_END_STATE;
963 (*last_cmd)->cmd.end_state = cmd_queue_alloc(sizeof(end_state_command_t));
964 (*last_cmd)->cmd.end_state->end_state = state;
969 int jtag_add_end_state(enum tap_state state)
971 int retval = interface_jtag_add_end_state(state);
973 cmd_queue_end_state = state;
977 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
979 jtag_command_t **last_cmd = jtag_get_last_command_p();
981 /* allocate memory for a new list member */
982 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
983 (*last_cmd)->next = NULL;
984 last_comand_pointer = &((*last_cmd)->next);
985 (*last_cmd)->type = JTAG_SLEEP;
987 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
988 (*last_cmd)->cmd.sleep->us = us;
993 int jtag_add_sleep(u32 us)
995 return interface_jtag_add_sleep(us);
998 int jtag_scan_size(scan_command_t *cmd)
1003 /* count bits in scan command */
1004 for (i = 0; i < cmd->num_fields; i++)
1006 bit_count += cmd->fields[i].num_bits;
1012 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1017 bit_count = jtag_scan_size(cmd);
1018 *buffer = malloc(CEIL(bit_count, 8));
1022 for (i = 0; i < cmd->num_fields; i++)
1024 if (cmd->fields[i].out_value)
1026 #ifdef _DEBUG_JTAG_IO_
1027 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1029 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1030 #ifdef _DEBUG_JTAG_IO_
1031 DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1036 bit_count += cmd->fields[i].num_bits;
1043 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1049 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1052 for (i = 0; i < cmd->num_fields; i++)
1054 /* if neither in_value nor in_handler
1055 * are specified we don't have to examine this field
1057 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1059 int num_bits = cmd->fields[i].num_bits;
1060 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1062 #ifdef _DEBUG_JTAG_IO_
1065 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1066 DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1070 if (cmd->fields[i].in_value)
1072 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1074 if (cmd->fields[i].in_handler)
1076 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1078 WARNING("in_handler reported a failed check");
1079 retval = ERROR_JTAG_QUEUE_FAILED;
1084 /* no in_value specified, but a handler takes care of the scanned data */
1085 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1087 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1089 /* We're going to call the error:handler later, but if the in_handler
1090 * reported an error we report this failure upstream
1092 WARNING("in_handler reported a failed check");
1093 retval = ERROR_JTAG_QUEUE_FAILED;
1099 bit_count += cmd->fields[i].num_bits;
1105 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1107 int retval = ERROR_OK;
1108 int num_bits = field->num_bits;
1110 int compare_failed = 0;
1112 if (field->in_check_mask)
1113 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1115 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1119 /* An error handler could have caught the failing check
1120 * only report a problem when there wasn't a handler, or if the handler
1121 * acknowledged the error
1125 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1126 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1128 if (field->in_check_mask)
1130 char *in_check_mask_char;
1131 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1132 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);
1133 free(in_check_mask_char);
1137 WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
1140 free(captured_char);
1141 free(in_check_value_char);
1143 retval = ERROR_JTAG_QUEUE_FAILED;
1151 set up checking of this field using the in_handler. The values passed in must be valid until
1152 after jtag_execute() has completed.
1154 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1157 field->in_handler = jtag_check_value;
1159 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1160 field->in_handler_priv = NULL; /* this will be filled in at the invocation site to point to the field duplicate */
1161 field->in_check_value = value;
1162 field->in_check_mask = mask;
1165 enum scan_type jtag_scan_type(scan_command_t *cmd)
1170 for (i = 0; i < cmd->num_fields; i++)
1172 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1174 if (cmd->fields[i].out_value)
1181 int MINIDRIVER(interface_jtag_execute_queue)(void)
1185 retval = jtag->execute_queue();
1189 jtag_command_queue = NULL;
1190 last_comand_pointer = &jtag_command_queue;
1192 jtag_error=ERROR_OK;
1197 int jtag_execute_queue(void)
1199 int retval=interface_jtag_execute_queue();
1200 if (retval!=ERROR_OK)
1203 jtag_error=ERROR_OK;
1207 int jtag_reset_callback(enum jtag_event event, void *priv)
1209 jtag_device_t *device = priv;
1213 if (event == JTAG_TRST_ASSERTED)
1215 buf_set_ones(device->cur_instr, device->ir_length);
1222 void jtag_sleep(u32 us)
1227 /* Try to examine chain layout according to IEEE 1149.1 §12
1229 int jtag_examine_chain()
1231 jtag_device_t *device = jtag_devices;
1233 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1236 int device_count = 0;
1237 u8 zero_check = 0x0;
1238 u8 one_check = 0xff;
1241 field.num_bits = sizeof(idcode_buffer) * 8;
1242 field.out_value = idcode_buffer;
1243 field.out_mask = NULL;
1244 field.in_value = idcode_buffer;
1245 field.in_check_value = NULL;
1246 field.in_check_mask = NULL;
1247 field.in_handler = NULL;
1248 field.in_handler_priv = NULL;
1250 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1252 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1255 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1256 jtag_execute_queue();
1258 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1260 zero_check |= idcode_buffer[i];
1261 one_check &= idcode_buffer[i];
1264 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1265 if ((zero_check == 0x00) || (one_check == 0xff))
1267 ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1268 return ERROR_JTAG_INIT_FAILED;
1271 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1273 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1274 if ((idcode & 1) == 0)
1276 /* LSB must not be 0, this indicates a device in bypass */
1287 if (idcode == 0x000000FF)
1289 /* End of chain (invalid manufacturer ID) */
1295 device->idcode = idcode;
1296 device = device->next;
1300 manufacturer = (idcode & 0xffe) >> 1;
1301 part = (idcode & 0xffff000) >> 12;
1302 version = (idcode & 0xf0000000) >> 28;
1304 INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1305 idcode, manufacturer, part, version);
1311 /* see if number of discovered devices matches configuration */
1312 if (device_count != jtag_num_devices)
1314 ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1315 device_count, jtag_num_devices);
1316 ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1317 return ERROR_JTAG_INIT_FAILED;
1323 int jtag_validate_chain()
1325 jtag_device_t *device = jtag_devices;
1326 int total_ir_length = 0;
1333 total_ir_length += device->ir_length;
1334 device = device->next;
1337 total_ir_length += 2;
1338 ir_test = malloc(CEIL(total_ir_length, 8));
1339 buf_set_ones(ir_test, total_ir_length);
1342 field.num_bits = total_ir_length;
1343 field.out_value = ir_test;
1344 field.out_mask = NULL;
1345 field.in_value = ir_test;
1346 field.in_check_value = NULL;
1347 field.in_check_mask = NULL;
1348 field.in_handler = NULL;
1349 field.in_handler_priv = NULL;
1351 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1352 jtag_execute_queue();
1354 device = jtag_devices;
1357 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1359 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1360 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1363 return ERROR_JTAG_INIT_FAILED;
1365 chain_pos += device->ir_length;
1366 device = device->next;
1369 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1371 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1372 ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1375 return ERROR_JTAG_INIT_FAILED;
1383 int jtag_register_commands(struct command_context_s *cmd_ctx)
1385 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1386 COMMAND_CONFIG, NULL);
1387 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1388 COMMAND_ANY, "set jtag speed (if supported) <speed>");
1389 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1390 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1391 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1392 COMMAND_CONFIG, NULL);
1393 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1394 COMMAND_CONFIG, NULL);
1395 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1396 COMMAND_CONFIG, NULL);
1398 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1399 COMMAND_EXEC, "print current scan chain configuration");
1401 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1402 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1403 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1404 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1405 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1406 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1407 register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
1408 COMMAND_EXEC, "move to current endstate or [tap_state]");
1409 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1410 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1411 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1412 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1414 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1415 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1419 int jtag_interface_init(struct command_context_s *cmd_ctx)
1421 if (!jtag_interface)
1423 /* nothing was previously specified by "interface" command */
1424 ERROR("JTAG interface has to be specified, see \"interface\" command");
1425 return ERROR_JTAG_INVALID_INTERFACE;
1428 if (jtag_interface->init() != ERROR_OK)
1429 return ERROR_JTAG_INIT_FAILED;
1431 jtag = jtag_interface;
1435 int jtag_init(struct command_context_s *cmd_ctx)
1437 int validate_tries = 0;
1438 jtag_device_t *device;
1442 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1443 return ERROR_JTAG_INIT_FAILED;
1445 device = jtag_devices;
1446 jtag_ir_scan_size = 0;
1447 jtag_num_devices = 0;
1448 while (device != NULL)
1450 jtag_ir_scan_size += device->ir_length;
1452 device = device->next;
1455 jtag_add_statemove(TAP_TLR);
1456 jtag_execute_queue();
1458 /* examine chain first, as this could discover the real chain layout */
1459 if (jtag_examine_chain() != ERROR_OK)
1461 ERROR("trying to validate configured JTAG chain anyway...");
1464 while (jtag_validate_chain() != ERROR_OK)
1467 if (validate_tries > 5)
1469 ERROR("Could not validate JTAG chain, exit");
1470 return ERROR_JTAG_INVALID_INTERFACE;
1478 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1482 /* check whether the interface is already configured */
1485 WARNING("Interface already configured, ignoring");
1489 /* interface name is a mandatory argument */
1490 if (argc < 1 || args[0][0] == '\0')
1492 return ERROR_COMMAND_SYNTAX_ERROR;
1495 for (i=0; jtag_interfaces[i]; i++)
1497 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1499 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1502 jtag_interface = jtag_interfaces[i];
1507 /* no valid interface was found (i.e. the configuration option,
1508 * didn't match one of the compiled-in interfaces
1510 ERROR("No valid jtag interface found (%s)", args[0]);
1511 ERROR("compiled-in jtag interfaces:");
1512 for (i = 0; jtag_interfaces[i]; i++)
1514 ERROR("%i: %s", i, jtag_interfaces[i]->name);
1517 return ERROR_JTAG_INVALID_INTERFACE;
1520 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1522 jtag_device_t **last_device_p = &jtag_devices;
1526 while ((*last_device_p)->next)
1527 last_device_p = &((*last_device_p)->next);
1528 last_device_p = &((*last_device_p)->next);
1534 *last_device_p = malloc(sizeof(jtag_device_t));
1535 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1537 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1538 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1539 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1540 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1542 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1543 (*last_device_p)->bypass = 1;
1544 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1546 (*last_device_p)->next = NULL;
1548 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1555 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1557 jtag_device_t *device = jtag_devices;
1558 int device_count = 0;
1562 u32 expected, expected_mask, cur_instr;
1563 expected = buf_get_u32(device->expected, 0, device->ir_length);
1564 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1565 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1566 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);
1567 device = device->next;
1574 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1578 if (strcmp(args[0], "none") == 0)
1579 jtag_reset_config = RESET_NONE;
1580 else if (strcmp(args[0], "trst_only") == 0)
1581 jtag_reset_config = RESET_HAS_TRST;
1582 else if (strcmp(args[0], "srst_only") == 0)
1583 jtag_reset_config = RESET_HAS_SRST;
1584 else if (strcmp(args[0], "trst_and_srst") == 0)
1585 jtag_reset_config = RESET_TRST_AND_SRST;
1588 ERROR("invalid reset_config argument, defaulting to none");
1589 jtag_reset_config = RESET_NONE;
1590 return ERROR_INVALID_ARGUMENTS;
1596 if (strcmp(args[1], "srst_pulls_trst") == 0)
1597 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1598 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1599 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1600 else if (strcmp(args[1], "combined") == 0)
1601 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1602 else if (strcmp(args[1], "separate") == 0)
1603 jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
1606 ERROR("invalid reset_config argument, defaulting to none");
1607 jtag_reset_config = RESET_NONE;
1608 return ERROR_INVALID_ARGUMENTS;
1614 if (strcmp(args[2], "trst_open_drain") == 0)
1615 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1616 else if (strcmp(args[2], "trst_push_pull") == 0)
1617 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1620 ERROR("invalid reset_config argument, defaulting to none");
1621 jtag_reset_config = RESET_NONE;
1622 return ERROR_INVALID_ARGUMENTS;
1628 if (strcmp(args[3], "srst_push_pull") == 0)
1629 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1630 else if (strcmp(args[3], "srst_open_drain") == 0)
1631 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1634 ERROR("invalid reset_config argument, defaulting to none");
1635 jtag_reset_config = RESET_NONE;
1636 return ERROR_INVALID_ARGUMENTS;
1643 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1647 ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1652 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1658 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1662 ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1667 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1673 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1676 command_print(cmd_ctx, "jtag_speed: %i", jtag_speed);
1680 /* this command can be called during CONFIG,
1681 * in which case jtag isn't initialized */
1683 jtag->speed(strtoul(args[0], NULL, 0));
1685 jtag_speed = strtoul(args[0], NULL, 0);
1691 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1693 enum tap_state state;
1697 return ERROR_COMMAND_SYNTAX_ERROR;
1701 for (state = 0; state < 16; state++)
1703 if (strcmp(args[0], tap_state_strings[state]) == 0)
1705 jtag_add_end_state(state);
1706 jtag_execute_queue();
1710 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[end_state]);
1715 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1723 return ERROR_COMMAND_SYNTAX_ERROR;
1727 if (args[0][0] == '1')
1729 else if (args[0][0] == '0')
1733 return ERROR_COMMAND_SYNTAX_ERROR;
1736 if (args[1][0] == '1')
1738 else if (args[1][0] == '0')
1742 return ERROR_COMMAND_SYNTAX_ERROR;
1745 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1746 return ERROR_JTAG_INIT_FAILED;
1748 if ((retval = jtag_add_reset(trst, srst)) != ERROR_OK)
1752 case ERROR_JTAG_RESET_WOULD_ASSERT_TRST:
1753 command_print(cmd_ctx, "requested reset would assert trst\nif this is acceptable, use jtag_reset 1 %c", args[1][0]);
1755 case ERROR_JTAG_RESET_CANT_SRST:
1756 command_print(cmd_ctx, "can't assert srst because the current reset_config doesn't support it");
1759 command_print(cmd_ctx, "unknown error");
1762 jtag_execute_queue();
1767 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1771 return ERROR_COMMAND_SYNTAX_ERROR;
1774 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1775 jtag_execute_queue();
1781 int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1783 enum tap_state state;
1788 for (state = 0; state < 16; state++)
1790 if (strcmp(args[0], tap_state_strings[state]) == 0)
1797 jtag_add_statemove(state);
1798 jtag_execute_queue();
1804 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1807 scan_field_t *fields;
1809 if ((argc < 2) || (argc % 2))
1811 return ERROR_COMMAND_SYNTAX_ERROR;
1814 fields = malloc(sizeof(scan_field_t) * argc / 2);
1816 for (i = 0; i < argc / 2; i++)
1818 int device = strtoul(args[i*2], NULL, 0);
1819 int field_size = jtag_get_device(device)->ir_length;
1820 fields[i].device = device;
1821 fields[i].out_value = malloc(CEIL(field_size, 8));
1822 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1823 fields[i].out_mask = NULL;
1824 fields[i].in_value = NULL;
1825 fields[i].in_check_mask = NULL;
1826 fields[i].in_handler = NULL;
1827 fields[i].in_handler_priv = NULL;
1830 jtag_add_ir_scan(argc / 2, fields, -1);
1831 jtag_execute_queue();
1833 for (i = 0; i < argc / 2; i++)
1834 free(fields[i].out_value);
1841 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1843 scan_field_t *fields;
1845 int field_count = 0;
1849 if ((argc < 2) || (argc % 2))
1851 return ERROR_COMMAND_SYNTAX_ERROR;
1854 for (i = 0; i < argc; i+=2)
1856 var = get_var_by_namenum(args[i+1]);
1859 num_fields += var->num_fields;
1863 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
1868 fields = malloc(sizeof(scan_field_t) * num_fields);
1870 for (i = 0; i < argc; i+=2)
1872 var = get_var_by_namenum(args[i+1]);
1874 for (j = 0; j < var->num_fields; j++)
1876 fields[field_count].device = strtol(args[i], NULL, 0);
1877 fields[field_count].num_bits = var->fields[j].num_bits;
1878 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
1879 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
1880 fields[field_count].out_mask = NULL;
1881 fields[field_count].in_value = fields[field_count].out_value;
1882 fields[field_count].in_check_mask = NULL;
1883 fields[field_count].in_check_value = NULL;
1884 fields[field_count].in_handler = field_le_to_host;
1885 fields[field_count++].in_handler_priv = &(var->fields[j]);
1889 jtag_add_dr_scan(num_fields, fields, -1);
1890 jtag_execute_queue();
1892 for (i = 0; i < argc / 2; i++)
1893 free(fields[i].out_value);
1902 int MINIDRIVER(interface_jtag_add_shift)(const enum tap_state shift_state, const enum tap_state end_state, int num_bits, u32 value)
1905 buf_set_u32(out_buf, 0, 32, flip_u32(value, 32));
1907 /* allocate memory for a new list member */
1908 jtag_command_t **last_cmd;
1909 last_cmd = jtag_get_last_command_p();
1910 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1911 last_comand_pointer = &((*last_cmd)->next);
1912 (*last_cmd)->next = NULL;
1913 (*last_cmd)->type = JTAG_SCAN;
1915 /* allocate memory for scan command */
1916 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
1917 (*last_cmd)->cmd.scan->ir_scan = (shift_state==TAP_SI);
1918 (*last_cmd)->cmd.scan->num_fields = 1;
1919 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(1 * sizeof(scan_field_t));
1920 (*last_cmd)->cmd.scan->end_state = end_state;
1922 int num_bytes = CEIL(num_bits, 8);
1924 (*last_cmd)->cmd.scan->fields[i].device = 0; /* not used by any drivers */
1925 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
1926 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(out_buf, cmd_queue_alloc(num_bytes), num_bits);
1927 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
1928 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
1929 (*last_cmd)->cmd.scan->fields[i].in_check_value = NULL;
1930 (*last_cmd)->cmd.scan->fields[i].in_check_mask = NULL;
1931 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
1932 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
1937 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1941 if (strcmp(args[0], "enable") == 0)
1943 jtag_verify_capture_ir = 1;
1945 else if (strcmp(args[0], "disable") == 0)
1947 jtag_verify_capture_ir = 0;
1950 return ERROR_COMMAND_SYNTAX_ERROR;
1952 } else if (argc != 0)
1954 return ERROR_COMMAND_SYNTAX_ERROR;
1957 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");