1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "replacements.h"
30 #include "interpreter.h"
37 /* note that this is not marked as static as it must be available from outside jtag.c for those
38 that implement the jtag_xxx() minidriver layer
40 int jtag_error=ERROR_OK;
43 char* tap_state_strings[16] =
46 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
48 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
51 typedef struct cmd_queue_page_s
55 struct cmd_queue_page_s *next;
58 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
59 static cmd_queue_page_t *cmd_queue_pages = NULL;
61 /* tap_move[i][j]: tap movement command to go from state i to state j
69 * SD->SD and SI->SI have to be caught in interface specific code
73 /* TLR RTI SD PD SI PI */
74 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
75 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
76 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
77 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
78 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
79 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
82 int tap_move_map[16] = {
83 0, -1, -1, 2, -1, 3, -1, -1,
84 1, -1, -1, 4, -1, 5, -1, -1
87 tap_transition_t tap_transitions[16] =
89 {TAP_TLR, TAP_RTI}, /* TLR */
90 {TAP_SIS, TAP_CD}, /* SDS */
91 {TAP_E1D, TAP_SD}, /* CD */
92 {TAP_E1D, TAP_SD}, /* SD */
93 {TAP_UD, TAP_PD}, /* E1D */
94 {TAP_E2D, TAP_PD}, /* PD */
95 {TAP_UD, TAP_SD}, /* E2D */
96 {TAP_SDS, TAP_RTI}, /* UD */
97 {TAP_SDS, TAP_RTI}, /* RTI */
98 {TAP_TLR, TAP_CI}, /* SIS */
99 {TAP_E1I, TAP_SI}, /* CI */
100 {TAP_E1I, TAP_SI}, /* SI */
101 {TAP_UI, TAP_PI}, /* E1I */
102 {TAP_E2I, TAP_PI}, /* PI */
103 {TAP_UI, TAP_SI}, /* E2I */
104 {TAP_SDS, TAP_RTI} /* UI */
107 char* jtag_event_strings[] =
109 "JTAG controller reset(tms or TRST)"
112 /* kludge!!!! these are just global variables that the
113 * interface use internally. They really belong
114 * inside the drivers, but we don't want to break
115 * linking the drivers!!!!
117 enum tap_state end_state = TAP_TLR;
118 enum tap_state cur_state = TAP_TLR;
122 jtag_command_t *jtag_command_queue = NULL;
123 jtag_command_t **last_comand_pointer = &jtag_command_queue;
124 jtag_device_t *jtag_devices = NULL;
125 int jtag_num_devices = 0;
126 int jtag_ir_scan_size = 0;
127 enum reset_types jtag_reset_config = RESET_NONE;
128 enum tap_state cmd_queue_end_state = TAP_TLR;
129 enum tap_state cmd_queue_cur_state = TAP_TLR;
131 int jtag_verify_capture_ir = 1;
133 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
134 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
135 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
137 /* maximum number of JTAG devices expected in the chain
139 #define JTAG_MAX_CHAIN_SIZE 20
141 /* callbacks to inform high-level handlers about JTAG state changes */
142 jtag_event_callback_t *jtag_event_callbacks;
145 static int speed1 = 0, speed2 = 0;
146 /* flag if the kHz speed was defined */
147 static int hasKHz = 0;
149 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
152 #if BUILD_ECOSBOARD == 1
153 extern jtag_interface_t eCosBoard_interface;
156 #if BUILD_PARPORT == 1
157 extern jtag_interface_t parport_interface;
161 extern jtag_interface_t dummy_interface;
164 #if BUILD_FT2232_FTD2XX == 1
165 extern jtag_interface_t ft2232_interface;
168 #if BUILD_FT2232_LIBFTDI == 1
169 extern jtag_interface_t ft2232_interface;
172 #if BUILD_AMTJTAGACCEL == 1
173 extern jtag_interface_t amt_jtagaccel_interface;
176 #if BUILD_EP93XX == 1
177 extern jtag_interface_t ep93xx_interface;
180 #if BUILD_AT91RM9200 == 1
181 extern jtag_interface_t at91rm9200_interface;
184 #if BUILD_GW16012 == 1
185 extern jtag_interface_t gw16012_interface;
188 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
189 extern jtag_interface_t presto_interface;
192 #if BUILD_USBPROG == 1
193 extern jtag_interface_t usbprog_interface;
196 jtag_interface_t *jtag_interfaces[] = {
197 #if BUILD_ECOSBOARD == 1
198 &eCosBoard_interface,
200 #if BUILD_PARPORT == 1
206 #if BUILD_FT2232_FTD2XX == 1
209 #if BUILD_FT2232_LIBFTDI == 1
212 #if BUILD_AMTJTAGACCEL == 1
213 &amt_jtagaccel_interface,
215 #if BUILD_EP93XX == 1
218 #if BUILD_AT91RM9200 == 1
219 &at91rm9200_interface,
221 #if BUILD_GW16012 == 1
224 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
227 #if BUILD_USBPROG == 1
233 jtag_interface_t *jtag = NULL;
236 jtag_interface_t *jtag_interface = NULL;
238 int jtag_speed_post_reset = 0;
241 /* forward declarations */
242 void jtag_add_pathmove(int num_states, enum tap_state *path);
243 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
244 void jtag_add_end_state(enum tap_state endstate);
245 void jtag_add_sleep(u32 us);
246 int jtag_execute_queue(void);
247 int jtag_cancel_queue(void);
250 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
251 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
252 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
253 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
254 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
255 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
256 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
258 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
260 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
261 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
262 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
263 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
264 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
266 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
268 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
270 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
272 if (callback == NULL)
274 return ERROR_INVALID_ARGUMENTS;
279 while ((*callbacks_p)->next)
280 callbacks_p = &((*callbacks_p)->next);
281 callbacks_p = &((*callbacks_p)->next);
284 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
285 (*callbacks_p)->callback = callback;
286 (*callbacks_p)->priv = priv;
287 (*callbacks_p)->next = NULL;
292 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
294 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
296 if (callback == NULL)
298 return ERROR_INVALID_ARGUMENTS;
303 jtag_event_callback_t **next = &((*callbacks_p)->next);
304 if ((*callbacks_p)->callback == callback)
307 *callbacks_p = *next;
315 int jtag_call_event_callbacks(enum jtag_event event)
317 jtag_event_callback_t *callback = jtag_event_callbacks;
319 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
323 callback->callback(event, callback->priv);
324 callback = callback->next;
330 /* returns a pointer to the pointer of the last command in queue
331 * this may be a pointer to the root pointer (jtag_command_queue)
332 * or to the next member of the last but one command
334 jtag_command_t** jtag_get_last_command_p(void)
336 /* jtag_command_t *cmd = jtag_command_queue;
342 return &jtag_command_queue;
346 return last_comand_pointer;
349 /* returns a pointer to the n-th device in the scan chain */
350 jtag_device_t* jtag_get_device(int num)
352 jtag_device_t *device = jtag_devices;
359 device = device->next;
363 LOG_ERROR("jtag device number %d not defined", num);
367 void* cmd_queue_alloc(size_t size)
369 cmd_queue_page_t **p_page = &cmd_queue_pages;
375 while ((*p_page)->next)
376 p_page = &((*p_page)->next);
377 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
378 p_page = &((*p_page)->next);
383 *p_page = malloc(sizeof(cmd_queue_page_t));
385 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
386 (*p_page)->next = NULL;
389 offset = (*p_page)->used;
390 (*p_page)->used += size;
392 t=(u8 *)((*p_page)->address);
396 void cmd_queue_free()
398 cmd_queue_page_t *page = cmd_queue_pages;
402 cmd_queue_page_t *last = page;
408 cmd_queue_pages = NULL;
411 static void jtag_prelude1()
415 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
416 jtag_error=ERROR_JTAG_TRST_ASSERTED;
420 if (cmd_queue_end_state == TAP_TLR)
421 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
424 static void jtag_prelude(enum tap_state state)
429 jtag_add_end_state(state);
431 cmd_queue_cur_state = cmd_queue_end_state;
434 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
440 retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
441 if (retval!=ERROR_OK)
445 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
447 jtag_command_t **last_cmd;
448 jtag_device_t *device;
453 last_cmd = jtag_get_last_command_p();
455 /* allocate memory for a new list member */
456 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
457 (*last_cmd)->next = NULL;
458 last_comand_pointer = &((*last_cmd)->next);
459 (*last_cmd)->type = JTAG_SCAN;
461 /* allocate memory for ir scan command */
462 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
463 (*last_cmd)->cmd.scan->ir_scan = 1;
464 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
465 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
466 (*last_cmd)->cmd.scan->end_state = state;
468 for (i = 0; i < jtag_num_devices; i++)
471 device = jtag_get_device(i);
472 scan_size = device->ir_length;
473 (*last_cmd)->cmd.scan->fields[i].device = i;
474 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
475 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
476 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
478 /* search the list */
479 for (j = 0; j < num_fields; j++)
481 if (i == fields[j].device)
484 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
485 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
487 if (jtag_verify_capture_ir)
489 if (fields[j].in_handler==NULL)
491 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
494 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
495 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
496 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
497 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
508 /* if a device isn't listed, set it to BYPASS */
509 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
510 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
515 /* update device information */
516 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
522 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
528 retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
529 if (retval!=ERROR_OK)
533 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
536 jtag_command_t **last_cmd;
538 last_cmd = jtag_get_last_command_p();
540 /* allocate memory for a new list member */
541 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
542 (*last_cmd)->next = NULL;
543 last_comand_pointer = &((*last_cmd)->next);
544 (*last_cmd)->type = JTAG_SCAN;
546 /* allocate memory for ir scan command */
547 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
548 (*last_cmd)->cmd.scan->ir_scan = 1;
549 (*last_cmd)->cmd.scan->num_fields = num_fields;
550 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
551 (*last_cmd)->cmd.scan->end_state = state;
553 for (i = 0; i < num_fields; i++)
555 int num_bits = fields[i].num_bits;
556 int num_bytes = CEIL(fields[i].num_bits, 8);
557 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
558 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
559 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
560 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
561 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
562 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
563 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
564 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
565 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
570 void jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
576 retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
577 if (retval!=ERROR_OK)
581 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
584 int bypass_devices = 0;
588 jtag_command_t **last_cmd = jtag_get_last_command_p();
589 jtag_device_t *device = jtag_devices;
591 /* count devices in bypass */
596 device = device->next;
599 /* allocate memory for a new list member */
600 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
601 last_comand_pointer = &((*last_cmd)->next);
602 (*last_cmd)->next = NULL;
603 (*last_cmd)->type = JTAG_SCAN;
605 /* allocate memory for dr scan command */
606 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
607 (*last_cmd)->cmd.scan->ir_scan = 0;
608 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
609 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
610 (*last_cmd)->cmd.scan->end_state = state;
612 for (i = 0; i < jtag_num_devices; i++)
615 (*last_cmd)->cmd.scan->fields[field_count].device = i;
617 for (j = 0; j < num_fields; j++)
619 if (i == fields[j].device)
622 scan_size = fields[j].num_bits;
623 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
624 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
625 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
626 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
627 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
628 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
629 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
630 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
635 #ifdef _DEBUG_JTAG_IO_
636 /* if a device isn't listed, the BYPASS register should be selected */
637 if (!jtag_get_device(i)->bypass)
639 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
643 /* program the scan field to 1 bit length, and ignore it's value */
644 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
645 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
646 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
647 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
648 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
649 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
650 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
651 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
655 #ifdef _DEBUG_JTAG_IO_
656 /* if a device is listed, the BYPASS register must not be selected */
657 if (jtag_get_device(i)->bypass)
659 LOG_ERROR("BUG: scan data for a device in BYPASS");
668 void MINIDRIVER(interface_jtag_add_dr_out)(int device_num,
672 enum tap_state end_state)
677 int bypass_devices = 0;
679 jtag_command_t **last_cmd = jtag_get_last_command_p();
680 jtag_device_t *device = jtag_devices;
681 /* count devices in bypass */
686 device = device->next;
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 dr 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 + bypass_devices;
699 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
700 (*last_cmd)->cmd.scan->end_state = end_state;
702 for (i = 0; i < jtag_num_devices; i++)
704 (*last_cmd)->cmd.scan->fields[field_count].device = i;
709 #ifdef _DEBUG_JTAG_IO_
710 /* if a device is listed, the BYPASS register must not be selected */
711 if (jtag_get_device(i)->bypass)
713 LOG_ERROR("BUG: scan data for a device in BYPASS");
717 for (j = 0; j < num_fields; j++)
720 scan_size = num_bits[j];
721 buf_set_u32(out_value, 0, scan_size, value[j]);
722 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
723 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
724 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
725 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
726 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
727 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
728 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
729 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
733 #ifdef _DEBUG_JTAG_IO_
734 /* if a device isn't listed, the BYPASS register should be selected */
735 if (!jtag_get_device(i)->bypass)
737 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
741 /* program the scan field to 1 bit length, and ignore it's value */
742 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
743 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
744 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
745 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
746 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
747 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
748 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
749 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
757 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
763 retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
764 if (retval!=ERROR_OK)
768 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
771 jtag_command_t **last_cmd = jtag_get_last_command_p();
773 /* allocate memory for a new list member */
774 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
775 last_comand_pointer = &((*last_cmd)->next);
776 (*last_cmd)->next = NULL;
777 (*last_cmd)->type = JTAG_SCAN;
779 /* allocate memory for scan command */
780 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
781 (*last_cmd)->cmd.scan->ir_scan = 0;
782 (*last_cmd)->cmd.scan->num_fields = num_fields;
783 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
784 (*last_cmd)->cmd.scan->end_state = state;
786 for (i = 0; i < num_fields; i++)
788 int num_bits = fields[i].num_bits;
789 int num_bytes = CEIL(fields[i].num_bits, 8);
790 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
791 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
792 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
793 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
794 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
795 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
796 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
797 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
798 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
806 jtag_prelude(TAP_TLR);
809 retval=interface_jtag_add_tlr();
810 if (retval!=ERROR_OK)
814 int MINIDRIVER(interface_jtag_add_tlr)()
816 enum tap_state state = TAP_TLR;
817 jtag_command_t **last_cmd = jtag_get_last_command_p();
819 /* allocate memory for a new list member */
820 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
821 last_comand_pointer = &((*last_cmd)->next);
822 (*last_cmd)->next = NULL;
823 (*last_cmd)->type = JTAG_STATEMOVE;
825 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
826 (*last_cmd)->cmd.statemove->end_state = state;
832 void jtag_add_pathmove(int num_states, enum tap_state *path)
834 enum tap_state cur_state=cmd_queue_cur_state;
838 /* the last state has to be a stable state */
839 if (tap_move_map[path[num_states - 1]] == -1)
841 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
845 for (i=0; i<num_states; i++)
847 if ((tap_transitions[cur_state].low != path[i])&&
848 (tap_transitions[cur_state].high != path[i]))
850 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
858 cmd_queue_cur_state = path[num_states - 1];
860 retval=interface_jtag_add_pathmove(num_states, path);
861 if (retval!=ERROR_OK)
866 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
868 jtag_command_t **last_cmd = jtag_get_last_command_p();
871 /* allocate memory for a new list member */
872 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
873 last_comand_pointer = &((*last_cmd)->next);
874 (*last_cmd)->next = NULL;
875 (*last_cmd)->type = JTAG_PATHMOVE;
877 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
878 (*last_cmd)->cmd.pathmove->num_states = num_states;
879 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
881 for (i = 0; i < num_states; i++)
882 (*last_cmd)->cmd.pathmove->path[i] = path[i];
887 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
889 jtag_command_t **last_cmd = jtag_get_last_command_p();
891 /* allocate memory for a new list member */
892 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
893 (*last_cmd)->next = NULL;
894 last_comand_pointer = &((*last_cmd)->next);
895 (*last_cmd)->type = JTAG_RUNTEST;
897 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
898 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
899 (*last_cmd)->cmd.runtest->end_state = state;
904 void jtag_add_runtest(int num_cycles, enum tap_state state)
910 /* executed by sw or hw fifo */
911 retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
912 if (retval!=ERROR_OK)
916 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
918 int trst_with_tlr = 0;
921 /* FIX!!! there are *many* different cases here. A better
922 * approach is needed for legal combinations of transitions...
924 if ((jtag_reset_config & RESET_HAS_SRST)&&
925 (jtag_reset_config & RESET_HAS_TRST)&&
926 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)&&
927 ((jtag_reset_config & RESET_TRST_PULLS_SRST)==0))
929 if (((req_tlr_or_trst&&!jtag_trst)||
930 (!req_tlr_or_trst&&jtag_trst))&&
931 ((req_srst&&!jtag_srst)||
932 (!req_srst&&jtag_srst)))
934 LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
938 /* Make sure that jtag_reset_config allows the requested reset */
939 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
940 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
942 LOG_ERROR("BUG: requested reset would assert trst");
943 jtag_error=ERROR_FAIL;
947 /* if TRST pulls SRST, we reset with TAP T-L-R */
948 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
953 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
955 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
956 jtag_error=ERROR_FAIL;
962 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
974 jtag_srst = req_srst;
976 retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
977 if (retval!=ERROR_OK)
985 LOG_DEBUG("SRST line asserted");
989 LOG_DEBUG("SRST line released");
990 if (jtag_nsrst_delay)
991 jtag_add_sleep(jtag_nsrst_delay * 1000);
996 LOG_DEBUG("JTAG reset with tms instead of TRST");
997 jtag_add_end_state(TAP_TLR);
999 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1005 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1006 * and inform possible listeners about this
1008 LOG_DEBUG("TRST line asserted");
1009 cmd_queue_cur_state = TAP_TLR;
1010 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1014 if (jtag_ntrst_delay)
1015 jtag_add_sleep(jtag_ntrst_delay * 1000);
1019 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1021 jtag_command_t **last_cmd = jtag_get_last_command_p();
1023 /* allocate memory for a new list member */
1024 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1025 (*last_cmd)->next = NULL;
1026 last_comand_pointer = &((*last_cmd)->next);
1027 (*last_cmd)->type = JTAG_RESET;
1029 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1030 (*last_cmd)->cmd.reset->trst = req_trst;
1031 (*last_cmd)->cmd.reset->srst = req_srst;
1037 void jtag_add_end_state(enum tap_state state)
1039 cmd_queue_end_state = state;
1040 if ((cmd_queue_end_state == TAP_SD)||(cmd_queue_end_state == TAP_SD))
1042 LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
1046 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1048 jtag_command_t **last_cmd = jtag_get_last_command_p();
1050 /* allocate memory for a new list member */
1051 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1052 (*last_cmd)->next = NULL;
1053 last_comand_pointer = &((*last_cmd)->next);
1054 (*last_cmd)->type = JTAG_SLEEP;
1056 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1057 (*last_cmd)->cmd.sleep->us = us;
1062 void jtag_add_sleep(u32 us)
1064 int retval=interface_jtag_add_sleep(us);
1065 if (retval!=ERROR_OK)
1070 int jtag_scan_size(scan_command_t *cmd)
1075 /* count bits in scan command */
1076 for (i = 0; i < cmd->num_fields; i++)
1078 bit_count += cmd->fields[i].num_bits;
1084 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1089 bit_count = jtag_scan_size(cmd);
1090 *buffer = malloc(CEIL(bit_count, 8));
1094 for (i = 0; i < cmd->num_fields; i++)
1096 if (cmd->fields[i].out_value)
1098 #ifdef _DEBUG_JTAG_IO_
1099 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1101 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1102 #ifdef _DEBUG_JTAG_IO_
1103 LOG_DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1108 bit_count += cmd->fields[i].num_bits;
1115 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1121 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1124 for (i = 0; i < cmd->num_fields; i++)
1126 /* if neither in_value nor in_handler
1127 * are specified we don't have to examine this field
1129 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1131 int num_bits = cmd->fields[i].num_bits;
1132 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1134 #ifdef _DEBUG_JTAG_IO_
1137 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1138 LOG_DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1142 if (cmd->fields[i].in_value)
1144 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1146 if (cmd->fields[i].in_handler)
1148 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1150 LOG_WARNING("in_handler reported a failed check");
1151 retval = ERROR_JTAG_QUEUE_FAILED;
1156 /* no in_value specified, but a handler takes care of the scanned data */
1157 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1159 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1161 /* We're going to call the error:handler later, but if the in_handler
1162 * reported an error we report this failure upstream
1164 LOG_WARNING("in_handler reported a failed check");
1165 retval = ERROR_JTAG_QUEUE_FAILED;
1171 bit_count += cmd->fields[i].num_bits;
1177 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1179 int retval = ERROR_OK;
1180 int num_bits = field->num_bits;
1182 int compare_failed = 0;
1184 if (field->in_check_mask)
1185 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1187 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1191 /* An error handler could have caught the failing check
1192 * only report a problem when there wasn't a handler, or if the handler
1193 * acknowledged the error
1197 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1198 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1200 if (field->in_check_mask)
1202 char *in_check_mask_char;
1203 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1204 LOG_WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s check_mask: 0x%s", captured_char, in_check_value_char, in_check_mask_char);
1205 free(in_check_mask_char);
1209 LOG_WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
1212 free(captured_char);
1213 free(in_check_value_char);
1215 retval = ERROR_JTAG_QUEUE_FAILED;
1223 set up checking of this field using the in_handler. The values passed in must be valid until
1224 after jtag_execute() has completed.
1226 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1229 field->in_handler = jtag_check_value;
1231 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1232 field->in_handler_priv = NULL; /* this will be filled in at the invocation site to point to the field duplicate */
1233 field->in_check_value = value;
1234 field->in_check_mask = mask;
1237 enum scan_type jtag_scan_type(scan_command_t *cmd)
1242 for (i = 0; i < cmd->num_fields; i++)
1244 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1246 if (cmd->fields[i].out_value)
1253 int MINIDRIVER(interface_jtag_execute_queue)(void)
1257 retval = jtag->execute_queue();
1261 jtag_command_queue = NULL;
1262 last_comand_pointer = &jtag_command_queue;
1267 int jtag_execute_queue(void)
1269 int retval=interface_jtag_execute_queue();
1270 if (retval==ERROR_OK)
1274 jtag_error=ERROR_OK;
1278 int jtag_reset_callback(enum jtag_event event, void *priv)
1280 jtag_device_t *device = priv;
1284 if (event == JTAG_TRST_ASSERTED)
1286 buf_set_ones(device->cur_instr, device->ir_length);
1293 void jtag_sleep(u32 us)
1298 /* Try to examine chain layout according to IEEE 1149.1 §12
1300 int jtag_examine_chain()
1302 jtag_device_t *device = jtag_devices;
1304 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1307 int device_count = 0;
1308 u8 zero_check = 0x0;
1309 u8 one_check = 0xff;
1312 field.num_bits = sizeof(idcode_buffer) * 8;
1313 field.out_value = idcode_buffer;
1314 field.out_mask = NULL;
1315 field.in_value = idcode_buffer;
1316 field.in_check_value = NULL;
1317 field.in_check_mask = NULL;
1318 field.in_handler = NULL;
1319 field.in_handler_priv = NULL;
1321 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1323 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1326 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1327 jtag_execute_queue();
1329 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1331 zero_check |= idcode_buffer[i];
1332 one_check &= idcode_buffer[i];
1335 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1336 if ((zero_check == 0x00) || (one_check == 0xff))
1338 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1339 return ERROR_JTAG_INIT_FAILED;
1342 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1344 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1345 if ((idcode & 1) == 0)
1347 /* LSB must not be 0, this indicates a device in bypass */
1358 if (idcode == 0x000000FF)
1360 /* End of chain (invalid manufacturer ID) */
1366 device->idcode = idcode;
1367 device = device->next;
1371 manufacturer = (idcode & 0xffe) >> 1;
1372 part = (idcode & 0xffff000) >> 12;
1373 version = (idcode & 0xf0000000) >> 28;
1375 LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1376 idcode, manufacturer, part, version);
1382 /* see if number of discovered devices matches configuration */
1383 if (device_count != jtag_num_devices)
1385 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1386 device_count, jtag_num_devices);
1387 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1388 return ERROR_JTAG_INIT_FAILED;
1394 int jtag_validate_chain()
1396 jtag_device_t *device = jtag_devices;
1397 int total_ir_length = 0;
1404 total_ir_length += device->ir_length;
1405 device = device->next;
1408 total_ir_length += 2;
1409 ir_test = malloc(CEIL(total_ir_length, 8));
1410 buf_set_ones(ir_test, total_ir_length);
1413 field.num_bits = total_ir_length;
1414 field.out_value = ir_test;
1415 field.out_mask = NULL;
1416 field.in_value = ir_test;
1417 field.in_check_value = NULL;
1418 field.in_check_mask = NULL;
1419 field.in_handler = NULL;
1420 field.in_handler_priv = NULL;
1422 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1423 jtag_execute_queue();
1425 device = jtag_devices;
1428 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1430 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1431 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1434 return ERROR_JTAG_INIT_FAILED;
1436 chain_pos += device->ir_length;
1437 device = device->next;
1440 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1442 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1443 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1446 return ERROR_JTAG_INIT_FAILED;
1454 int jtag_register_commands(struct command_context_s *cmd_ctx)
1456 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1457 COMMAND_CONFIG, NULL);
1458 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1459 COMMAND_ANY, "set jtag speed (if supported) <reset speed> [<post reset speed, default value is reset speed>]");
1460 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1461 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1462 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1463 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1464 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1465 COMMAND_CONFIG, NULL);
1466 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1467 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
1468 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1469 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
1471 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1472 COMMAND_EXEC, "print current scan chain configuration");
1474 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1475 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1476 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1477 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1478 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1479 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1480 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1481 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1482 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1483 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1485 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1486 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1490 int jtag_interface_init(struct command_context_s *cmd_ctx)
1495 if (!jtag_interface)
1497 /* nothing was previously specified by "interface" command */
1498 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1499 return ERROR_JTAG_INVALID_INTERFACE;
1503 /*stay on "reset speed"*/
1504 if (jtag_interface->khz(speed1, &speed1) == ERROR_OK)
1505 jtag_speed = speed1;
1506 if (jtag_interface->khz(speed2, &speed2) == ERROR_OK)
1507 jtag_speed_post_reset = speed2;
1511 if (jtag_interface->init() != ERROR_OK)
1512 return ERROR_JTAG_INIT_FAILED;
1516 jtag = jtag_interface;
1520 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1522 int validate_tries = 0;
1523 jtag_device_t *device;
1526 LOG_DEBUG("Init JTAG chain");
1528 device = jtag_devices;
1529 jtag_ir_scan_size = 0;
1530 jtag_num_devices = 0;
1531 while (device != NULL)
1533 jtag_ir_scan_size += device->ir_length;
1535 device = device->next;
1539 if ((retval=jtag_execute_queue())!=ERROR_OK)
1542 /* examine chain first, as this could discover the real chain layout */
1543 if (jtag_examine_chain() != ERROR_OK)
1545 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1548 while (jtag_validate_chain() != ERROR_OK)
1552 if (validate_tries > 5)
1554 LOG_ERROR("Could not validate JTAG chain, exit");
1555 return ERROR_JTAG_INVALID_INTERFACE;
1563 int jtag_init_reset(struct command_context_s *cmd_ctx)
1567 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1570 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / tms");
1572 /* Reset can happen after a power cycle.
1574 * Ideally we would only assert TRST or run tms before the target reset.
1576 * However w/srst_pulls_trst, trst is asserted together with the target
1577 * reset whether we want it or not.
1579 * NB! Some targets have JTAG circuitry disabled until a
1580 * trst & srst has been asserted.
1582 * NB! here we assume nsrst/ntrst delay are sufficient!
1584 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1587 jtag_add_reset(1, 0); /* TMS or TRST */
1588 if (jtag_reset_config & RESET_HAS_SRST)
1590 jtag_add_reset(1, 1);
1591 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
1592 jtag_add_reset(0, 1);
1594 jtag_add_reset(0, 0);
1595 if ((retval = jtag_execute_queue()) != ERROR_OK)
1598 /* Check that we can communication on the JTAG chain + eventually we want to
1599 * be able to perform enumeration only after OpenOCD has started
1600 * telnet and GDB server
1602 * That would allow users to more easily perform any magic they need to before
1605 return jtag_init_inner(cmd_ctx);
1608 int jtag_init(struct command_context_s *cmd_ctx)
1611 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1613 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
1617 return jtag_init_reset(cmd_ctx);
1621 static int default_khz(int khz, int *jtag_speed)
1623 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1627 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1631 /* check whether the interface is already configured */
1634 LOG_WARNING("Interface already configured, ignoring");
1638 /* interface name is a mandatory argument */
1639 if (argc < 1 || args[0][0] == '\0')
1641 return ERROR_COMMAND_SYNTAX_ERROR;
1644 for (i=0; jtag_interfaces[i]; i++)
1646 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1648 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1651 jtag_interface = jtag_interfaces[i];
1653 if (jtag_interface->khz == NULL)
1655 jtag_interface->khz = default_khz;
1661 /* no valid interface was found (i.e. the configuration option,
1662 * didn't match one of the compiled-in interfaces
1664 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
1665 LOG_ERROR("compiled-in jtag interfaces:");
1666 for (i = 0; jtag_interfaces[i]; i++)
1668 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
1671 return ERROR_JTAG_INVALID_INTERFACE;
1674 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1676 jtag_device_t **last_device_p = &jtag_devices;
1680 while ((*last_device_p)->next)
1681 last_device_p = &((*last_device_p)->next);
1682 last_device_p = &((*last_device_p)->next);
1688 *last_device_p = malloc(sizeof(jtag_device_t));
1689 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1691 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1692 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1693 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1694 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1696 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1697 (*last_device_p)->bypass = 1;
1698 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1700 (*last_device_p)->next = NULL;
1702 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1709 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1711 jtag_device_t *device = jtag_devices;
1712 int device_count = 0;
1716 u32 expected, expected_mask, cur_instr;
1717 expected = buf_get_u32(device->expected, 0, device->ir_length);
1718 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1719 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1720 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);
1721 device = device->next;
1728 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1731 return ERROR_COMMAND_SYNTAX_ERROR;
1735 if (strcmp(args[0], "none") == 0)
1736 jtag_reset_config = RESET_NONE;
1737 else if (strcmp(args[0], "trst_only") == 0)
1738 jtag_reset_config = RESET_HAS_TRST;
1739 else if (strcmp(args[0], "srst_only") == 0)
1740 jtag_reset_config = RESET_HAS_SRST;
1741 else if (strcmp(args[0], "trst_and_srst") == 0)
1742 jtag_reset_config = RESET_TRST_AND_SRST;
1745 LOG_ERROR("invalid reset_config argument, defaulting to none");
1746 jtag_reset_config = RESET_NONE;
1747 return ERROR_INVALID_ARGUMENTS;
1753 if (strcmp(args[1], "separate") == 0)
1755 /* seperate reset lines - default */
1758 if (strcmp(args[1], "srst_pulls_trst") == 0)
1759 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1760 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1761 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1762 else if (strcmp(args[1], "combined") == 0)
1763 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1766 LOG_ERROR("invalid reset_config argument, defaulting to none");
1767 jtag_reset_config = RESET_NONE;
1768 return ERROR_INVALID_ARGUMENTS;
1775 if (strcmp(args[2], "trst_open_drain") == 0)
1776 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1777 else if (strcmp(args[2], "trst_push_pull") == 0)
1778 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1781 LOG_ERROR("invalid reset_config argument, defaulting to none");
1782 jtag_reset_config = RESET_NONE;
1783 return ERROR_INVALID_ARGUMENTS;
1789 if (strcmp(args[3], "srst_push_pull") == 0)
1790 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1791 else if (strcmp(args[3], "srst_open_drain") == 0)
1792 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1795 LOG_ERROR("invalid reset_config argument, defaulting to none");
1796 jtag_reset_config = RESET_NONE;
1797 return ERROR_INVALID_ARGUMENTS;
1804 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1808 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1813 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1819 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1823 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1828 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1834 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1840 if ((argc<1) || (argc>2))
1841 return ERROR_COMMAND_SYNTAX_ERROR;
1843 LOG_DEBUG("handle jtag speed");
1846 cur_speed = jtag_speed = jtag_speed_post_reset = strtoul(args[0], NULL, 0);
1848 cur_speed = jtag_speed_post_reset = strtoul(args[1], NULL, 0);
1850 /* this command can be called during CONFIG,
1851 * in which case jtag isn't initialized */
1853 jtag->speed(cur_speed);
1855 command_print(cmd_ctx, "jtag_speed: %d, %d", jtag_speed, jtag_speed_post_reset);
1860 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1862 LOG_DEBUG("handle jtag khz");
1864 if ((argc<1) || (argc>2))
1865 return ERROR_COMMAND_SYNTAX_ERROR;
1868 speed1 = speed2 = strtoul(args[0], NULL, 0);
1870 speed2 = strtoul(args[1], NULL, 0);
1875 LOG_DEBUG("have interface set up");
1876 int speed_div1, speed_div2;
1877 if (jtag->khz(speed1, &speed_div1)!=ERROR_OK)
1879 if (jtag->khz(speed2, &speed_div2)!=ERROR_OK)
1883 cur_speed = jtag_speed = jtag_speed_post_reset = speed_div1;
1885 cur_speed = jtag_speed_post_reset = speed_div2;
1887 jtag->speed(cur_speed);
1897 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1899 enum tap_state state;
1903 return ERROR_COMMAND_SYNTAX_ERROR;
1907 for (state = 0; state < 16; state++)
1909 if (strcmp(args[0], tap_state_strings[state]) == 0)
1911 jtag_add_end_state(state);
1912 jtag_execute_queue();
1916 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
1921 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1928 return ERROR_COMMAND_SYNTAX_ERROR;
1931 if (args[0][0] == '1')
1933 else if (args[0][0] == '0')
1937 return ERROR_COMMAND_SYNTAX_ERROR;
1940 if (args[1][0] == '1')
1942 else if (args[1][0] == '0')
1946 return ERROR_COMMAND_SYNTAX_ERROR;
1949 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
1950 return ERROR_JTAG_INIT_FAILED;
1952 jtag_add_reset(trst, srst);
1953 jtag_execute_queue();
1958 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1962 return ERROR_COMMAND_SYNTAX_ERROR;
1965 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1966 jtag_execute_queue();
1973 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1976 scan_field_t *fields;
1978 if ((argc < 2) || (argc % 2))
1980 return ERROR_COMMAND_SYNTAX_ERROR;
1983 fields = malloc(sizeof(scan_field_t) * argc / 2);
1985 for (i = 0; i < argc / 2; i++)
1987 int device = strtoul(args[i*2], NULL, 0);
1988 int field_size = jtag_get_device(device)->ir_length;
1989 fields[i].device = device;
1990 fields[i].out_value = malloc(CEIL(field_size, 8));
1991 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1992 fields[i].out_mask = NULL;
1993 fields[i].in_value = NULL;
1994 fields[i].in_check_mask = NULL;
1995 fields[i].in_handler = NULL;
1996 fields[i].in_handler_priv = NULL;
1999 jtag_add_ir_scan(argc / 2, fields, -1);
2000 jtag_execute_queue();
2002 for (i = 0; i < argc / 2; i++)
2003 free(fields[i].out_value);
2010 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2012 scan_field_t *fields;
2014 int field_count = 0;
2018 if ((argc < 2) || (argc % 2))
2020 return ERROR_COMMAND_SYNTAX_ERROR;
2023 for (i = 0; i < argc; i+=2)
2025 var = get_var_by_namenum(args[i+1]);
2028 num_fields += var->num_fields;
2032 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
2037 fields = malloc(sizeof(scan_field_t) * num_fields);
2039 for (i = 0; i < argc; i+=2)
2041 var = get_var_by_namenum(args[i+1]);
2043 for (j = 0; j < var->num_fields; j++)
2045 fields[field_count].device = strtol(args[i], NULL, 0);
2046 fields[field_count].num_bits = var->fields[j].num_bits;
2047 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
2048 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
2049 fields[field_count].out_mask = NULL;
2050 fields[field_count].in_value = fields[field_count].out_value;
2051 fields[field_count].in_check_mask = NULL;
2052 fields[field_count].in_check_value = NULL;
2053 fields[field_count].in_handler = field_le_to_host;
2054 fields[field_count++].in_handler_priv = &(var->fields[j]);
2058 jtag_add_dr_scan(num_fields, fields, -1);
2059 jtag_execute_queue();
2061 for (i = 0; i < argc / 2; i++)
2062 free(fields[i].out_value);
2069 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2073 if (strcmp(args[0], "enable") == 0)
2075 jtag_verify_capture_ir = 1;
2077 else if (strcmp(args[0], "disable") == 0)
2079 jtag_verify_capture_ir = 0;
2082 return ERROR_COMMAND_SYNTAX_ERROR;
2084 } else if (argc != 0)
2086 return ERROR_COMMAND_SYNTAX_ERROR;
2089 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");