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"
35 /* note that this is not marked as static as it must be available from outside jtag.c for those
36 that implement the jtag_xxx() minidriver layer
38 int jtag_error=ERROR_OK;
41 char* tap_state_strings[16] =
44 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
46 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
49 typedef struct cmd_queue_page_s
53 struct cmd_queue_page_s *next;
56 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
57 static cmd_queue_page_t *cmd_queue_pages = NULL;
59 /* tap_move[i][j]: tap movement command to go from state i to state j
67 * SD->SD and SI->SI have to be caught in interface specific code
71 /* TLR RTI SD PD SI PI */
72 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
73 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
74 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
75 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
76 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
77 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
80 int tap_move_map[16] = {
81 0, -1, -1, 2, -1, 3, -1, -1,
82 1, -1, -1, 4, -1, 5, -1, -1
85 tap_transition_t tap_transitions[16] =
87 {TAP_TLR, TAP_RTI}, /* TLR */
88 {TAP_SIS, TAP_CD}, /* SDS */
89 {TAP_E1D, TAP_SD}, /* CD */
90 {TAP_E1D, TAP_SD}, /* SD */
91 {TAP_UD, TAP_PD}, /* E1D */
92 {TAP_E2D, TAP_PD}, /* PD */
93 {TAP_UD, TAP_SD}, /* E2D */
94 {TAP_SDS, TAP_RTI}, /* UD */
95 {TAP_SDS, TAP_RTI}, /* RTI */
96 {TAP_TLR, TAP_CI}, /* SIS */
97 {TAP_E1I, TAP_SI}, /* CI */
98 {TAP_E1I, TAP_SI}, /* SI */
99 {TAP_UI, TAP_PI}, /* E1I */
100 {TAP_E2I, TAP_PI}, /* PI */
101 {TAP_UI, TAP_SI}, /* E2I */
102 {TAP_SDS, TAP_RTI} /* UI */
105 char* jtag_event_strings[] =
107 "JTAG controller reset (TLR or TRST)"
110 /* kludge!!!! these are just global variables that the
111 * interface use internally. They really belong
112 * inside the drivers, but we don't want to break
113 * linking the drivers!!!!
115 enum tap_state end_state = TAP_TLR;
116 enum tap_state cur_state = TAP_TLR;
120 jtag_command_t *jtag_command_queue = NULL;
121 jtag_command_t **last_comand_pointer = &jtag_command_queue;
122 jtag_device_t *jtag_devices = NULL;
123 int jtag_num_devices = 0;
124 int jtag_ir_scan_size = 0;
125 enum reset_types jtag_reset_config = RESET_NONE;
126 enum tap_state cmd_queue_end_state = TAP_TLR;
127 enum tap_state cmd_queue_cur_state = TAP_TLR;
129 int jtag_verify_capture_ir = 1;
131 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
132 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
133 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
135 /* maximum number of JTAG devices expected in the chain
137 #define JTAG_MAX_CHAIN_SIZE 20
139 /* callbacks to inform high-level handlers about JTAG state changes */
140 jtag_event_callback_t *jtag_event_callbacks;
143 static int speed1 = 0, speed2 = 0;
144 /* flag if the kHz speed was defined */
145 static int hasKHz = 0;
147 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
150 #if BUILD_ECOSBOARD == 1
151 extern jtag_interface_t eCosBoard_interface;
154 #if BUILD_PARPORT == 1
155 extern jtag_interface_t parport_interface;
159 extern jtag_interface_t dummy_interface;
162 #if BUILD_FT2232_FTD2XX == 1
163 extern jtag_interface_t ft2232_interface;
166 #if BUILD_FT2232_LIBFTDI == 1
167 extern jtag_interface_t ft2232_interface;
170 #if BUILD_AMTJTAGACCEL == 1
171 extern jtag_interface_t amt_jtagaccel_interface;
174 #if BUILD_EP93XX == 1
175 extern jtag_interface_t ep93xx_interface;
178 #if BUILD_AT91RM9200 == 1
179 extern jtag_interface_t at91rm9200_interface;
182 #if BUILD_GW16012 == 1
183 extern jtag_interface_t gw16012_interface;
186 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
187 extern jtag_interface_t presto_interface;
190 #if BUILD_USBPROG == 1
191 extern jtag_interface_t usbprog_interface;
195 extern jtag_interface_t jlink_interface;
198 jtag_interface_t *jtag_interfaces[] = {
199 #if BUILD_ECOSBOARD == 1
200 &eCosBoard_interface,
202 #if BUILD_PARPORT == 1
208 #if BUILD_FT2232_FTD2XX == 1
211 #if BUILD_FT2232_LIBFTDI == 1
214 #if BUILD_AMTJTAGACCEL == 1
215 &amt_jtagaccel_interface,
217 #if BUILD_EP93XX == 1
220 #if BUILD_AT91RM9200 == 1
221 &at91rm9200_interface,
223 #if BUILD_GW16012 == 1
226 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
229 #if BUILD_USBPROG == 1
238 jtag_interface_t *jtag = NULL;
241 jtag_interface_t *jtag_interface = NULL;
243 int jtag_speed_post_reset = 0;
246 /* forward declarations */
247 void jtag_add_pathmove(int num_states, enum tap_state *path);
248 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
249 void jtag_add_end_state(enum tap_state endstate);
250 void jtag_add_sleep(u32 us);
251 int jtag_execute_queue(void);
252 int jtag_cancel_queue(void);
255 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
256 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
257 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
258 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
259 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
260 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
261 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
263 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
265 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
266 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
267 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
268 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
269 int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
271 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
273 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
275 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
277 if (callback == NULL)
279 return ERROR_INVALID_ARGUMENTS;
284 while ((*callbacks_p)->next)
285 callbacks_p = &((*callbacks_p)->next);
286 callbacks_p = &((*callbacks_p)->next);
289 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
290 (*callbacks_p)->callback = callback;
291 (*callbacks_p)->priv = priv;
292 (*callbacks_p)->next = NULL;
297 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
299 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
301 if (callback == NULL)
303 return ERROR_INVALID_ARGUMENTS;
308 jtag_event_callback_t **next = &((*callbacks_p)->next);
309 if ((*callbacks_p)->callback == callback)
312 *callbacks_p = *next;
320 int jtag_call_event_callbacks(enum jtag_event event)
322 jtag_event_callback_t *callback = jtag_event_callbacks;
324 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
328 callback->callback(event, callback->priv);
329 callback = callback->next;
335 /* returns a pointer to the pointer of the last command in queue
336 * this may be a pointer to the root pointer (jtag_command_queue)
337 * or to the next member of the last but one command
339 jtag_command_t** jtag_get_last_command_p(void)
341 /* jtag_command_t *cmd = jtag_command_queue;
347 return &jtag_command_queue;
351 return last_comand_pointer;
354 /* returns a pointer to the n-th device in the scan chain */
355 jtag_device_t* jtag_get_device(int num)
357 jtag_device_t *device = jtag_devices;
364 device = device->next;
368 LOG_ERROR("jtag device number %d not defined", num);
372 void* cmd_queue_alloc(size_t size)
374 cmd_queue_page_t **p_page = &cmd_queue_pages;
380 while ((*p_page)->next)
381 p_page = &((*p_page)->next);
382 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
383 p_page = &((*p_page)->next);
388 *p_page = malloc(sizeof(cmd_queue_page_t));
390 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
391 (*p_page)->next = NULL;
394 offset = (*p_page)->used;
395 (*p_page)->used += size;
397 t=(u8 *)((*p_page)->address);
401 void cmd_queue_free()
403 cmd_queue_page_t *page = cmd_queue_pages;
407 cmd_queue_page_t *last = page;
413 cmd_queue_pages = NULL;
416 static void jtag_prelude1()
420 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
421 jtag_error=ERROR_JTAG_TRST_ASSERTED;
425 if (cmd_queue_end_state == TAP_TLR)
426 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
429 static void jtag_prelude(enum tap_state state)
434 jtag_add_end_state(state);
436 cmd_queue_cur_state = cmd_queue_end_state;
439 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
445 retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
446 if (retval!=ERROR_OK)
450 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
452 jtag_command_t **last_cmd;
453 jtag_device_t *device;
458 last_cmd = jtag_get_last_command_p();
460 /* allocate memory for a new list member */
461 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
462 (*last_cmd)->next = NULL;
463 last_comand_pointer = &((*last_cmd)->next);
464 (*last_cmd)->type = JTAG_SCAN;
466 /* allocate memory for ir scan command */
467 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
468 (*last_cmd)->cmd.scan->ir_scan = 1;
469 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
470 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
471 (*last_cmd)->cmd.scan->end_state = state;
473 for (i = 0; i < jtag_num_devices; i++)
476 device = jtag_get_device(i);
477 scan_size = device->ir_length;
478 (*last_cmd)->cmd.scan->fields[i].device = i;
479 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
480 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
481 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
483 /* search the list */
484 for (j = 0; j < num_fields; j++)
486 if (i == fields[j].device)
489 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
490 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
492 if (jtag_verify_capture_ir)
494 if (fields[j].in_handler==NULL)
496 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
499 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
500 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
501 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
502 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
513 /* if a device isn't listed, set it to BYPASS */
514 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
515 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
520 /* update device information */
521 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
527 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
533 retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
534 if (retval!=ERROR_OK)
538 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
541 jtag_command_t **last_cmd;
543 last_cmd = jtag_get_last_command_p();
545 /* allocate memory for a new list member */
546 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
547 (*last_cmd)->next = NULL;
548 last_comand_pointer = &((*last_cmd)->next);
549 (*last_cmd)->type = JTAG_SCAN;
551 /* allocate memory for ir scan command */
552 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
553 (*last_cmd)->cmd.scan->ir_scan = 1;
554 (*last_cmd)->cmd.scan->num_fields = num_fields;
555 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
556 (*last_cmd)->cmd.scan->end_state = state;
558 for (i = 0; i < num_fields; i++)
560 int num_bits = fields[i].num_bits;
561 int num_bytes = CEIL(fields[i].num_bits, 8);
562 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
563 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
564 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
565 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
566 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
567 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
568 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
569 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
570 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
575 void jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
581 retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
582 if (retval!=ERROR_OK)
586 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
589 int bypass_devices = 0;
593 jtag_command_t **last_cmd = jtag_get_last_command_p();
594 jtag_device_t *device = jtag_devices;
596 /* count devices in bypass */
601 device = device->next;
603 if (bypass_devices >= jtag_num_devices)
605 LOG_ERROR("all devices in bypass");
606 return ERROR_JTAG_DEVICE_ERROR;
609 /* allocate memory for a new list member */
610 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
611 last_comand_pointer = &((*last_cmd)->next);
612 (*last_cmd)->next = NULL;
613 (*last_cmd)->type = JTAG_SCAN;
615 /* allocate memory for dr scan command */
616 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
617 (*last_cmd)->cmd.scan->ir_scan = 0;
618 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
619 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
620 (*last_cmd)->cmd.scan->end_state = state;
622 for (i = 0; i < jtag_num_devices; i++)
625 (*last_cmd)->cmd.scan->fields[field_count].device = i;
627 for (j = 0; j < num_fields; j++)
629 if (i == fields[j].device)
632 scan_size = fields[j].num_bits;
633 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
634 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
635 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
636 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
637 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
638 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
639 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
640 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
645 #ifdef _DEBUG_JTAG_IO_
646 /* if a device isn't listed, the BYPASS register should be selected */
647 if (!jtag_get_device(i)->bypass)
649 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
653 /* program the scan field to 1 bit length, and ignore it's value */
654 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
655 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
656 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
657 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
658 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
659 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
660 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
661 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
665 #ifdef _DEBUG_JTAG_IO_
666 /* if a device is listed, the BYPASS register must not be selected */
667 if (jtag_get_device(i)->bypass)
669 LOG_ERROR("BUG: scan data for a device in BYPASS");
678 void MINIDRIVER(interface_jtag_add_dr_out)(int device_num,
682 enum tap_state end_state)
687 int bypass_devices = 0;
689 jtag_command_t **last_cmd = jtag_get_last_command_p();
690 jtag_device_t *device = jtag_devices;
691 /* count devices in bypass */
696 device = device->next;
699 /* allocate memory for a new list member */
700 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
701 last_comand_pointer = &((*last_cmd)->next);
702 (*last_cmd)->next = NULL;
703 (*last_cmd)->type = JTAG_SCAN;
705 /* allocate memory for dr scan command */
706 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
707 (*last_cmd)->cmd.scan->ir_scan = 0;
708 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
709 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
710 (*last_cmd)->cmd.scan->end_state = end_state;
712 for (i = 0; i < jtag_num_devices; i++)
714 (*last_cmd)->cmd.scan->fields[field_count].device = i;
719 #ifdef _DEBUG_JTAG_IO_
720 /* if a device is listed, the BYPASS register must not be selected */
721 if (jtag_get_device(i)->bypass)
723 LOG_ERROR("BUG: scan data for a device in BYPASS");
727 for (j = 0; j < num_fields; j++)
730 scan_size = num_bits[j];
731 buf_set_u32(out_value, 0, scan_size, value[j]);
732 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
733 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
734 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
735 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
736 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
737 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
738 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
739 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
743 #ifdef _DEBUG_JTAG_IO_
744 /* if a device isn't listed, the BYPASS register should be selected */
745 if (!jtag_get_device(i)->bypass)
747 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
751 /* program the scan field to 1 bit length, and ignore it's value */
752 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
753 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
754 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
755 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
756 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
757 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
758 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
759 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
767 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
773 retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
774 if (retval!=ERROR_OK)
778 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
781 jtag_command_t **last_cmd = jtag_get_last_command_p();
783 /* allocate memory for a new list member */
784 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
785 last_comand_pointer = &((*last_cmd)->next);
786 (*last_cmd)->next = NULL;
787 (*last_cmd)->type = JTAG_SCAN;
789 /* allocate memory for scan command */
790 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
791 (*last_cmd)->cmd.scan->ir_scan = 0;
792 (*last_cmd)->cmd.scan->num_fields = num_fields;
793 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
794 (*last_cmd)->cmd.scan->end_state = state;
796 for (i = 0; i < num_fields; i++)
798 int num_bits = fields[i].num_bits;
799 int num_bytes = CEIL(fields[i].num_bits, 8);
800 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
801 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
802 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
803 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
804 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
805 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
806 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
807 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
808 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
816 jtag_prelude(TAP_TLR);
819 retval=interface_jtag_add_tlr();
820 if (retval!=ERROR_OK)
824 int MINIDRIVER(interface_jtag_add_tlr)()
826 enum tap_state state = TAP_TLR;
827 jtag_command_t **last_cmd = jtag_get_last_command_p();
829 /* allocate memory for a new list member */
830 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
831 last_comand_pointer = &((*last_cmd)->next);
832 (*last_cmd)->next = NULL;
833 (*last_cmd)->type = JTAG_STATEMOVE;
835 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
836 (*last_cmd)->cmd.statemove->end_state = state;
842 void jtag_add_pathmove(int num_states, enum tap_state *path)
844 enum tap_state cur_state=cmd_queue_cur_state;
848 /* the last state has to be a stable state */
849 if (tap_move_map[path[num_states - 1]] == -1)
851 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
855 for (i=0; i<num_states; i++)
857 if ((tap_transitions[cur_state].low != path[i])&&
858 (tap_transitions[cur_state].high != path[i]))
860 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
868 cmd_queue_cur_state = path[num_states - 1];
870 retval=interface_jtag_add_pathmove(num_states, path);
871 if (retval!=ERROR_OK)
876 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
878 jtag_command_t **last_cmd = jtag_get_last_command_p();
881 /* allocate memory for a new list member */
882 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
883 last_comand_pointer = &((*last_cmd)->next);
884 (*last_cmd)->next = NULL;
885 (*last_cmd)->type = JTAG_PATHMOVE;
887 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
888 (*last_cmd)->cmd.pathmove->num_states = num_states;
889 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
891 for (i = 0; i < num_states; i++)
892 (*last_cmd)->cmd.pathmove->path[i] = path[i];
897 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
899 jtag_command_t **last_cmd = jtag_get_last_command_p();
901 /* allocate memory for a new list member */
902 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
903 (*last_cmd)->next = NULL;
904 last_comand_pointer = &((*last_cmd)->next);
905 (*last_cmd)->type = JTAG_RUNTEST;
907 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
908 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
909 (*last_cmd)->cmd.runtest->end_state = state;
914 void jtag_add_runtest(int num_cycles, enum tap_state state)
920 /* executed by sw or hw fifo */
921 retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
922 if (retval!=ERROR_OK)
926 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
928 int trst_with_tlr = 0;
931 /* FIX!!! there are *many* different cases here. A better
932 * approach is needed for legal combinations of transitions...
934 if ((jtag_reset_config & RESET_HAS_SRST)&&
935 (jtag_reset_config & RESET_HAS_TRST)&&
936 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
938 if (((req_tlr_or_trst&&!jtag_trst)||
939 (!req_tlr_or_trst&&jtag_trst))&&
940 ((req_srst&&!jtag_srst)||
941 (!req_srst&&jtag_srst)))
943 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
944 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
948 /* Make sure that jtag_reset_config allows the requested reset */
949 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
950 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
952 LOG_ERROR("BUG: requested reset would assert trst");
953 jtag_error=ERROR_FAIL;
957 /* if TRST pulls SRST, we reset with TAP T-L-R */
958 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
963 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
965 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
966 jtag_error=ERROR_FAIL;
972 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
984 jtag_srst = req_srst;
986 retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
987 if (retval!=ERROR_OK)
995 LOG_DEBUG("SRST line asserted");
999 LOG_DEBUG("SRST line released");
1000 if (jtag_nsrst_delay)
1001 jtag_add_sleep(jtag_nsrst_delay * 1000);
1006 LOG_DEBUG("JTAG reset with TLR instead of TRST");
1007 jtag_add_end_state(TAP_TLR);
1009 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1015 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1016 * and inform possible listeners about this
1018 LOG_DEBUG("TRST line asserted");
1019 cmd_queue_cur_state = TAP_TLR;
1020 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1024 if (jtag_ntrst_delay)
1025 jtag_add_sleep(jtag_ntrst_delay * 1000);
1029 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1031 jtag_command_t **last_cmd = jtag_get_last_command_p();
1033 /* allocate memory for a new list member */
1034 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1035 (*last_cmd)->next = NULL;
1036 last_comand_pointer = &((*last_cmd)->next);
1037 (*last_cmd)->type = JTAG_RESET;
1039 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1040 (*last_cmd)->cmd.reset->trst = req_trst;
1041 (*last_cmd)->cmd.reset->srst = req_srst;
1047 void jtag_add_end_state(enum tap_state state)
1049 cmd_queue_end_state = state;
1050 if ((cmd_queue_end_state == TAP_SD)||(cmd_queue_end_state == TAP_SD))
1052 LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
1056 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1058 jtag_command_t **last_cmd = jtag_get_last_command_p();
1060 /* allocate memory for a new list member */
1061 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1062 (*last_cmd)->next = NULL;
1063 last_comand_pointer = &((*last_cmd)->next);
1064 (*last_cmd)->type = JTAG_SLEEP;
1066 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1067 (*last_cmd)->cmd.sleep->us = us;
1072 void jtag_add_sleep(u32 us)
1074 keep_alive(); /* we might be running on a very slow JTAG clk */
1075 int retval=interface_jtag_add_sleep(us);
1076 if (retval!=ERROR_OK)
1081 int jtag_scan_size(scan_command_t *cmd)
1086 /* count bits in scan command */
1087 for (i = 0; i < cmd->num_fields; i++)
1089 bit_count += cmd->fields[i].num_bits;
1095 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1100 bit_count = jtag_scan_size(cmd);
1101 *buffer = malloc(CEIL(bit_count, 8));
1105 for (i = 0; i < cmd->num_fields; i++)
1107 if (cmd->fields[i].out_value)
1109 #ifdef _DEBUG_JTAG_IO_
1110 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1112 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1113 #ifdef _DEBUG_JTAG_IO_
1114 LOG_DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1119 bit_count += cmd->fields[i].num_bits;
1126 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1132 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1135 for (i = 0; i < cmd->num_fields; i++)
1137 /* if neither in_value nor in_handler
1138 * are specified we don't have to examine this field
1140 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1142 int num_bits = cmd->fields[i].num_bits;
1143 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1145 #ifdef _DEBUG_JTAG_IO_
1148 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1149 LOG_DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1153 if (cmd->fields[i].in_value)
1155 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1157 if (cmd->fields[i].in_handler)
1159 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1161 LOG_WARNING("in_handler reported a failed check");
1162 retval = ERROR_JTAG_QUEUE_FAILED;
1167 /* no in_value specified, but a handler takes care of the scanned data */
1168 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1170 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1172 /* We're going to call the error:handler later, but if the in_handler
1173 * reported an error we report this failure upstream
1175 LOG_WARNING("in_handler reported a failed check");
1176 retval = ERROR_JTAG_QUEUE_FAILED;
1182 bit_count += cmd->fields[i].num_bits;
1188 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1190 int retval = ERROR_OK;
1191 int num_bits = field->num_bits;
1193 int compare_failed = 0;
1195 if (field->in_check_mask)
1196 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1198 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1202 /* An error handler could have caught the failing check
1203 * only report a problem when there wasn't a handler, or if the handler
1204 * acknowledged the error
1208 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1209 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1211 if (field->in_check_mask)
1213 char *in_check_mask_char;
1214 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1215 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);
1216 free(in_check_mask_char);
1220 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);
1223 free(captured_char);
1224 free(in_check_value_char);
1226 retval = ERROR_JTAG_QUEUE_FAILED;
1234 set up checking of this field using the in_handler. The values passed in must be valid until
1235 after jtag_execute() has completed.
1237 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1240 field->in_handler = jtag_check_value;
1242 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1243 field->in_handler_priv = NULL;
1244 field->in_check_value = value;
1245 field->in_check_mask = mask;
1248 enum scan_type jtag_scan_type(scan_command_t *cmd)
1253 for (i = 0; i < cmd->num_fields; i++)
1255 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1257 if (cmd->fields[i].out_value)
1264 int MINIDRIVER(interface_jtag_execute_queue)(void)
1270 LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
1274 retval = jtag->execute_queue();
1278 jtag_command_queue = NULL;
1279 last_comand_pointer = &jtag_command_queue;
1284 int jtag_execute_queue(void)
1286 int retval=interface_jtag_execute_queue();
1287 if (retval==ERROR_OK)
1291 jtag_error=ERROR_OK;
1295 int jtag_reset_callback(enum jtag_event event, void *priv)
1297 jtag_device_t *device = priv;
1301 if (event == JTAG_TRST_ASSERTED)
1303 buf_set_ones(device->cur_instr, device->ir_length);
1310 void jtag_sleep(u32 us)
1315 /* Try to examine chain layout according to IEEE 1149.1 §12
1317 int jtag_examine_chain()
1319 jtag_device_t *device = jtag_devices;
1321 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1324 int device_count = 0;
1325 u8 zero_check = 0x0;
1326 u8 one_check = 0xff;
1329 field.num_bits = sizeof(idcode_buffer) * 8;
1330 field.out_value = idcode_buffer;
1331 field.out_mask = NULL;
1332 field.in_value = idcode_buffer;
1333 field.in_check_value = NULL;
1334 field.in_check_mask = NULL;
1335 field.in_handler = NULL;
1336 field.in_handler_priv = NULL;
1338 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1340 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1343 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1344 jtag_execute_queue();
1346 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1348 zero_check |= idcode_buffer[i];
1349 one_check &= idcode_buffer[i];
1352 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1353 if ((zero_check == 0x00) || (one_check == 0xff))
1355 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1356 return ERROR_JTAG_INIT_FAILED;
1359 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1361 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1362 if ((idcode & 1) == 0)
1364 /* LSB must not be 0, this indicates a device in bypass */
1375 if (idcode == 0x000000FF)
1377 /* End of chain (invalid manufacturer ID) */
1383 device->idcode = idcode;
1384 device = device->next;
1388 manufacturer = (idcode & 0xffe) >> 1;
1389 part = (idcode & 0xffff000) >> 12;
1390 version = (idcode & 0xf0000000) >> 28;
1392 LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1393 idcode, manufacturer, part, version);
1399 /* see if number of discovered devices matches configuration */
1400 if (device_count != jtag_num_devices)
1402 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1403 device_count, jtag_num_devices);
1404 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1405 return ERROR_JTAG_INIT_FAILED;
1411 int jtag_validate_chain()
1413 jtag_device_t *device = jtag_devices;
1414 int total_ir_length = 0;
1421 total_ir_length += device->ir_length;
1422 device = device->next;
1425 total_ir_length += 2;
1426 ir_test = malloc(CEIL(total_ir_length, 8));
1427 buf_set_ones(ir_test, total_ir_length);
1430 field.num_bits = total_ir_length;
1431 field.out_value = ir_test;
1432 field.out_mask = NULL;
1433 field.in_value = ir_test;
1434 field.in_check_value = NULL;
1435 field.in_check_mask = NULL;
1436 field.in_handler = NULL;
1437 field.in_handler_priv = NULL;
1439 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1440 jtag_execute_queue();
1442 device = jtag_devices;
1445 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1447 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1448 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1451 return ERROR_JTAG_INIT_FAILED;
1453 chain_pos += device->ir_length;
1454 device = device->next;
1457 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1459 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1460 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1463 return ERROR_JTAG_INIT_FAILED;
1471 int jtag_register_commands(struct command_context_s *cmd_ctx)
1473 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1474 COMMAND_CONFIG, NULL);
1475 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1476 COMMAND_ANY, "set jtag speed (if supported) <reset speed> [<post reset speed, default value is reset speed>]");
1477 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1478 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1479 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1480 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1481 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1482 COMMAND_CONFIG, NULL);
1483 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1484 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
1485 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1486 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
1488 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1489 COMMAND_EXEC, "print current scan chain configuration");
1491 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1492 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1493 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1494 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1495 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1496 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1497 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1498 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1499 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
1501 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1502 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1506 int jtag_interface_init(struct command_context_s *cmd_ctx)
1511 if (!jtag_interface)
1513 /* nothing was previously specified by "interface" command */
1514 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1515 return ERROR_JTAG_INVALID_INTERFACE;
1519 /*stay on "reset speed"*/
1520 jtag_interface->khz(speed1, &jtag_speed);
1521 jtag_interface->khz(speed2, &jtag_speed_post_reset);
1525 if (jtag_interface->init() != ERROR_OK)
1526 return ERROR_JTAG_INIT_FAILED;
1530 jtag = jtag_interface;
1534 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1536 int validate_tries = 0;
1537 jtag_device_t *device;
1540 LOG_DEBUG("Init JTAG chain");
1542 device = jtag_devices;
1543 jtag_ir_scan_size = 0;
1544 jtag_num_devices = 0;
1545 while (device != NULL)
1547 jtag_ir_scan_size += device->ir_length;
1549 device = device->next;
1553 if ((retval=jtag_execute_queue())!=ERROR_OK)
1556 /* examine chain first, as this could discover the real chain layout */
1557 if (jtag_examine_chain() != ERROR_OK)
1559 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1562 while (jtag_validate_chain() != ERROR_OK)
1566 if (validate_tries > 5)
1568 LOG_ERROR("Could not validate JTAG chain, exit");
1569 return ERROR_JTAG_INVALID_INTERFACE;
1577 int jtag_init_reset(struct command_context_s *cmd_ctx)
1581 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1584 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
1586 /* Reset can happen after a power cycle.
1588 * Ideally we would only assert TRST or run TLR before the target reset.
1590 * However w/srst_pulls_trst, trst is asserted together with the target
1591 * reset whether we want it or not.
1593 * NB! Some targets have JTAG circuitry disabled until a
1594 * trst & srst has been asserted.
1596 * NB! here we assume nsrst/ntrst delay are sufficient!
1598 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1601 jtag_add_reset(1, 0); /* TLR or TRST */
1602 if (jtag_reset_config & RESET_HAS_SRST)
1604 jtag_add_reset(1, 1);
1605 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
1606 jtag_add_reset(0, 1);
1608 jtag_add_reset(0, 0);
1609 if ((retval = jtag_execute_queue()) != ERROR_OK)
1612 /* Check that we can communication on the JTAG chain + eventually we want to
1613 * be able to perform enumeration only after OpenOCD has started
1614 * telnet and GDB server
1616 * That would allow users to more easily perform any magic they need to before
1619 return jtag_init_inner(cmd_ctx);
1622 int jtag_init(struct command_context_s *cmd_ctx)
1625 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1627 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
1631 return jtag_init_reset(cmd_ctx);
1635 static int default_khz(int khz, int *jtag_speed)
1637 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1641 static int default_speed_div(int speed, int *khz)
1646 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1650 /* check whether the interface is already configured */
1653 LOG_WARNING("Interface already configured, ignoring");
1657 /* interface name is a mandatory argument */
1658 if (argc < 1 || args[0][0] == '\0')
1660 return ERROR_COMMAND_SYNTAX_ERROR;
1663 for (i=0; jtag_interfaces[i]; i++)
1665 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1667 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1670 jtag_interface = jtag_interfaces[i];
1672 if (jtag_interface->khz == NULL)
1674 jtag_interface->khz = default_khz;
1676 if (jtag_interface->speed_div == NULL)
1678 jtag_interface->speed_div = default_speed_div;
1684 /* no valid interface was found (i.e. the configuration option,
1685 * didn't match one of the compiled-in interfaces
1687 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
1688 LOG_ERROR("compiled-in jtag interfaces:");
1689 for (i = 0; jtag_interfaces[i]; i++)
1691 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
1694 return ERROR_JTAG_INVALID_INTERFACE;
1697 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1699 jtag_device_t **last_device_p = &jtag_devices;
1703 while ((*last_device_p)->next)
1704 last_device_p = &((*last_device_p)->next);
1705 last_device_p = &((*last_device_p)->next);
1711 *last_device_p = malloc(sizeof(jtag_device_t));
1712 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1714 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1715 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1716 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1717 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1719 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1720 (*last_device_p)->bypass = 1;
1721 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1723 (*last_device_p)->next = NULL;
1725 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1732 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1734 jtag_device_t *device = jtag_devices;
1735 int device_count = 0;
1739 u32 expected, expected_mask, cur_instr;
1740 expected = buf_get_u32(device->expected, 0, device->ir_length);
1741 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1742 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1743 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);
1744 device = device->next;
1751 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1754 return ERROR_COMMAND_SYNTAX_ERROR;
1758 if (strcmp(args[0], "none") == 0)
1759 jtag_reset_config = RESET_NONE;
1760 else if (strcmp(args[0], "trst_only") == 0)
1761 jtag_reset_config = RESET_HAS_TRST;
1762 else if (strcmp(args[0], "srst_only") == 0)
1763 jtag_reset_config = RESET_HAS_SRST;
1764 else if (strcmp(args[0], "trst_and_srst") == 0)
1765 jtag_reset_config = RESET_TRST_AND_SRST;
1768 LOG_ERROR("invalid reset_config argument, defaulting to none");
1769 jtag_reset_config = RESET_NONE;
1770 return ERROR_INVALID_ARGUMENTS;
1776 if (strcmp(args[1], "separate") == 0)
1778 /* seperate reset lines - default */
1781 if (strcmp(args[1], "srst_pulls_trst") == 0)
1782 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1783 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1784 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1785 else if (strcmp(args[1], "combined") == 0)
1786 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1789 LOG_ERROR("invalid reset_config argument, defaulting to none");
1790 jtag_reset_config = RESET_NONE;
1791 return ERROR_INVALID_ARGUMENTS;
1798 if (strcmp(args[2], "trst_open_drain") == 0)
1799 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1800 else if (strcmp(args[2], "trst_push_pull") == 0)
1801 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1804 LOG_ERROR("invalid reset_config argument, defaulting to none");
1805 jtag_reset_config = RESET_NONE;
1806 return ERROR_INVALID_ARGUMENTS;
1812 if (strcmp(args[3], "srst_push_pull") == 0)
1813 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1814 else if (strcmp(args[3], "srst_open_drain") == 0)
1815 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1818 LOG_ERROR("invalid reset_config argument, defaulting to none");
1819 jtag_reset_config = RESET_NONE;
1820 return ERROR_INVALID_ARGUMENTS;
1827 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1831 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1836 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1842 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1846 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1851 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1857 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1863 if ((argc<1) || (argc>2))
1864 return ERROR_COMMAND_SYNTAX_ERROR;
1866 LOG_DEBUG("handle jtag speed");
1869 cur_speed = jtag_speed = jtag_speed_post_reset = strtoul(args[0], NULL, 0);
1871 cur_speed = jtag_speed_post_reset = strtoul(args[1], NULL, 0);
1873 /* this command can be called during CONFIG,
1874 * in which case jtag isn't initialized */
1877 jtag->speed_div(jtag_speed, &speed1);
1878 jtag->speed_div(jtag_speed_post_reset, &speed2);
1879 jtag->speed(cur_speed);
1882 command_print(cmd_ctx, "jtag_speed: %d, %d", jtag_speed, jtag_speed_post_reset);
1887 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1889 LOG_DEBUG("handle jtag khz");
1892 return ERROR_COMMAND_SYNTAX_ERROR;
1898 speed1 = speed2 = strtoul(args[0], NULL, 0);
1900 speed2 = strtoul(args[1], NULL, 0);
1905 LOG_DEBUG("have interface set up");
1906 int speed_div1, speed_div2;
1907 if (jtag->khz(speed1, &speed_div1)!=ERROR_OK)
1909 speed1 = speed2 = 0;
1912 if (jtag->khz(speed2, &speed_div2)!=ERROR_OK)
1914 speed1 = speed2 = 0;
1919 cur_speed = jtag_speed = jtag_speed_post_reset = speed_div1;
1921 cur_speed = jtag_speed_post_reset = speed_div2;
1923 jtag->speed(cur_speed);
1929 command_print(cmd_ctx, "jtag_khz: %d, %d", speed1, speed2);
1935 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1937 enum tap_state state;
1941 return ERROR_COMMAND_SYNTAX_ERROR;
1945 for (state = 0; state < 16; state++)
1947 if (strcmp(args[0], tap_state_strings[state]) == 0)
1949 jtag_add_end_state(state);
1950 jtag_execute_queue();
1954 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
1959 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1966 return ERROR_COMMAND_SYNTAX_ERROR;
1969 if (args[0][0] == '1')
1971 else if (args[0][0] == '0')
1975 return ERROR_COMMAND_SYNTAX_ERROR;
1978 if (args[1][0] == '1')
1980 else if (args[1][0] == '0')
1984 return ERROR_COMMAND_SYNTAX_ERROR;
1987 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
1988 return ERROR_JTAG_INIT_FAILED;
1990 jtag_add_reset(trst, srst);
1991 jtag_execute_queue();
1996 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2000 return ERROR_COMMAND_SYNTAX_ERROR;
2003 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
2004 jtag_execute_queue();
2011 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2014 scan_field_t *fields;
2016 if ((argc < 2) || (argc % 2))
2018 return ERROR_COMMAND_SYNTAX_ERROR;
2021 fields = malloc(sizeof(scan_field_t) * argc / 2);
2023 for (i = 0; i < argc / 2; i++)
2025 int device = strtoul(args[i*2], NULL, 0);
2026 int field_size = jtag_get_device(device)->ir_length;
2027 fields[i].device = device;
2028 fields[i].out_value = malloc(CEIL(field_size, 8));
2029 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
2030 fields[i].out_mask = NULL;
2031 fields[i].in_value = NULL;
2032 fields[i].in_check_mask = NULL;
2033 fields[i].in_handler = NULL;
2034 fields[i].in_handler_priv = NULL;
2037 jtag_add_ir_scan(argc / 2, fields, -1);
2038 jtag_execute_queue();
2040 for (i = 0; i < argc / 2; i++)
2041 free(fields[i].out_value);
2048 int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
2051 scan_field_t *fields;
2053 int field_count = 0;
2058 * args[2] = num_bits
2059 * args[3] = hex string
2060 * ... repeat num bits and hex string ...
2062 if ((argc < 4) || ((argc % 2)!=0))
2064 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
2068 for (i = 2; i < argc; i+=2)
2072 e = Jim_GetLong(interp, args[i], &bits);
2077 e = Jim_GetLong(interp, args[1], &device);
2081 num_fields=(argc-2)/2;
2082 fields = malloc(sizeof(scan_field_t) * num_fields);
2083 for (i = 2; i < argc; i+=2)
2089 Jim_GetLong(interp, args[i], &bits);
2090 str = Jim_GetString(args[i+1], &len);
2092 fields[field_count].device = device;
2093 fields[field_count].num_bits = bits;
2094 fields[field_count].out_value = malloc(CEIL(bits, 8));
2095 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
2096 fields[field_count].out_mask = NULL;
2097 fields[field_count].in_value = fields[field_count].out_value;
2098 fields[field_count].in_check_mask = NULL;
2099 fields[field_count].in_check_value = NULL;
2100 fields[field_count].in_handler = NULL;
2101 fields[field_count++].in_handler_priv = NULL;
2104 jtag_add_dr_scan(num_fields, fields, -1);
2105 retval = jtag_execute_queue();
2106 if (retval != ERROR_OK)
2108 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2109 Jim_AppendStrings(interp, Jim_GetResult(interp), "drscan: jtag execute failed", NULL);
2114 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
2115 for (i = 2; i < argc; i+=2)
2120 Jim_GetLong(interp, args[i], &bits);
2121 str = buf_to_str(fields[field_count].in_value, bits, 16);
2122 free(fields[field_count].out_value);
2124 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
2129 Jim_SetResult(interp, list);
2136 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2140 if (strcmp(args[0], "enable") == 0)
2142 jtag_verify_capture_ir = 1;
2144 else if (strcmp(args[0], "disable") == 0)
2146 jtag_verify_capture_ir = 0;
2149 return ERROR_COMMAND_SYNTAX_ERROR;
2151 } else if (argc != 0)
2153 return ERROR_COMMAND_SYNTAX_ERROR;
2156 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");