1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
27 #include "replacements.h"
38 /* note that this is not marked as static as it must be available from outside jtag.c for those
39 that implement the jtag_xxx() minidriver layer
41 int jtag_error=ERROR_OK;
44 char* tap_state_strings[16] =
47 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
49 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
52 typedef struct cmd_queue_page_s
56 struct cmd_queue_page_s *next;
59 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
60 static cmd_queue_page_t *cmd_queue_pages = NULL;
62 /* tap_move[i][j]: tap movement command to go from state i to state j
70 * SD->SD and SI->SI have to be caught in interface specific code
74 /* TLR RTI SD PD SI PI */
75 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
76 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
77 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
78 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
79 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
80 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
83 int tap_move_map[16] = {
84 0, -1, -1, 2, -1, 3, -1, -1,
85 1, -1, -1, 4, -1, 5, -1, -1
88 tap_transition_t tap_transitions[16] =
90 {TAP_TLR, TAP_RTI}, /* TLR */
91 {TAP_SIS, TAP_CD}, /* SDS */
92 {TAP_E1D, TAP_SD}, /* CD */
93 {TAP_E1D, TAP_SD}, /* SD */
94 {TAP_UD, TAP_PD}, /* E1D */
95 {TAP_E2D, TAP_PD}, /* PD */
96 {TAP_UD, TAP_SD}, /* E2D */
97 {TAP_SDS, TAP_RTI}, /* UD */
98 {TAP_SDS, TAP_RTI}, /* RTI */
99 {TAP_TLR, TAP_CI}, /* SIS */
100 {TAP_E1I, TAP_SI}, /* CI */
101 {TAP_E1I, TAP_SI}, /* SI */
102 {TAP_UI, TAP_PI}, /* E1I */
103 {TAP_E2I, TAP_PI}, /* PI */
104 {TAP_UI, TAP_SI}, /* E2I */
105 {TAP_SDS, TAP_RTI} /* UI */
108 char* jtag_event_strings[] =
110 "JTAG controller reset (TLR or TRST)"
113 /* kludge!!!! these are just global variables that the
114 * interface use internally. They really belong
115 * inside the drivers, but we don't want to break
116 * linking the drivers!!!!
118 enum tap_state end_state = TAP_TLR;
119 enum tap_state cur_state = TAP_TLR;
123 jtag_command_t *jtag_command_queue = NULL;
124 jtag_command_t **last_comand_pointer = &jtag_command_queue;
125 jtag_device_t *jtag_devices = NULL;
126 int jtag_num_devices = 0;
127 int jtag_ir_scan_size = 0;
128 enum reset_types jtag_reset_config = RESET_NONE;
129 enum tap_state cmd_queue_end_state = TAP_TLR;
130 enum tap_state cmd_queue_cur_state = TAP_TLR;
132 int jtag_verify_capture_ir = 1;
134 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
135 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
136 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
138 /* maximum number of JTAG devices expected in the chain
140 #define JTAG_MAX_CHAIN_SIZE 20
142 /* callbacks to inform high-level handlers about JTAG state changes */
143 jtag_event_callback_t *jtag_event_callbacks;
146 static int speed_khz = 0;
147 /* flag if the kHz speed was defined */
148 static int hasKHz = 0;
150 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
153 #if BUILD_ECOSBOARD == 1
154 extern jtag_interface_t eCosBoard_interface;
157 #if BUILD_PARPORT == 1
158 extern jtag_interface_t parport_interface;
162 extern jtag_interface_t dummy_interface;
165 #if BUILD_FT2232_FTD2XX == 1
166 extern jtag_interface_t ft2232_interface;
169 #if BUILD_FT2232_LIBFTDI == 1
170 extern jtag_interface_t ft2232_interface;
173 #if BUILD_AMTJTAGACCEL == 1
174 extern jtag_interface_t amt_jtagaccel_interface;
177 #if BUILD_EP93XX == 1
178 extern jtag_interface_t ep93xx_interface;
181 #if BUILD_AT91RM9200 == 1
182 extern jtag_interface_t at91rm9200_interface;
185 #if BUILD_GW16012 == 1
186 extern jtag_interface_t gw16012_interface;
189 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
190 extern jtag_interface_t presto_interface;
193 #if BUILD_USBPROG == 1
194 extern jtag_interface_t usbprog_interface;
198 extern jtag_interface_t jlink_interface;
201 jtag_interface_t *jtag_interfaces[] = {
202 #if BUILD_ECOSBOARD == 1
203 &eCosBoard_interface,
205 #if BUILD_PARPORT == 1
211 #if BUILD_FT2232_FTD2XX == 1
214 #if BUILD_FT2232_LIBFTDI == 1
217 #if BUILD_AMTJTAGACCEL == 1
218 &amt_jtagaccel_interface,
220 #if BUILD_EP93XX == 1
223 #if BUILD_AT91RM9200 == 1
224 &at91rm9200_interface,
226 #if BUILD_GW16012 == 1
229 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
232 #if BUILD_USBPROG == 1
241 jtag_interface_t *jtag = NULL;
244 jtag_interface_t *jtag_interface = NULL;
249 /* forward declarations */
250 void jtag_add_pathmove(int num_states, enum tap_state *path);
251 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
252 void jtag_add_end_state(enum tap_state endstate);
253 void jtag_add_sleep(u32 us);
254 int jtag_execute_queue(void);
255 int jtag_cancel_queue(void);
258 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
259 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
260 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
261 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
262 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
263 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
264 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
266 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
268 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
269 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
270 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
271 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
272 int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
274 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
276 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
278 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
280 if (callback == NULL)
282 return ERROR_INVALID_ARGUMENTS;
287 while ((*callbacks_p)->next)
288 callbacks_p = &((*callbacks_p)->next);
289 callbacks_p = &((*callbacks_p)->next);
292 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
293 (*callbacks_p)->callback = callback;
294 (*callbacks_p)->priv = priv;
295 (*callbacks_p)->next = NULL;
300 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
302 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
304 if (callback == NULL)
306 return ERROR_INVALID_ARGUMENTS;
311 jtag_event_callback_t **next = &((*callbacks_p)->next);
312 if ((*callbacks_p)->callback == callback)
315 *callbacks_p = *next;
323 int jtag_call_event_callbacks(enum jtag_event event)
325 jtag_event_callback_t *callback = jtag_event_callbacks;
327 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
331 callback->callback(event, callback->priv);
332 callback = callback->next;
338 /* returns a pointer to the pointer of the last command in queue
339 * this may be a pointer to the root pointer (jtag_command_queue)
340 * or to the next member of the last but one command
342 jtag_command_t** jtag_get_last_command_p(void)
344 /* jtag_command_t *cmd = jtag_command_queue;
350 return &jtag_command_queue;
354 return last_comand_pointer;
357 /* returns a pointer to the n-th device in the scan chain */
358 jtag_device_t* jtag_get_device(int num)
360 jtag_device_t *device = jtag_devices;
367 device = device->next;
371 LOG_ERROR("jtag device number %d not defined", num);
375 void* cmd_queue_alloc(size_t size)
377 cmd_queue_page_t **p_page = &cmd_queue_pages;
383 while ((*p_page)->next)
384 p_page = &((*p_page)->next);
385 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
386 p_page = &((*p_page)->next);
391 *p_page = malloc(sizeof(cmd_queue_page_t));
393 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
394 (*p_page)->next = NULL;
397 offset = (*p_page)->used;
398 (*p_page)->used += size;
400 t=(u8 *)((*p_page)->address);
404 void cmd_queue_free()
406 cmd_queue_page_t *page = cmd_queue_pages;
410 cmd_queue_page_t *last = page;
416 cmd_queue_pages = NULL;
419 static void jtag_prelude1()
423 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
424 jtag_error=ERROR_JTAG_TRST_ASSERTED;
428 if (cmd_queue_end_state == TAP_TLR)
429 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
432 static void jtag_prelude(enum tap_state state)
437 jtag_add_end_state(state);
439 cmd_queue_cur_state = cmd_queue_end_state;
442 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
448 retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
449 if (retval!=ERROR_OK)
453 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
455 jtag_command_t **last_cmd;
456 jtag_device_t *device;
461 last_cmd = jtag_get_last_command_p();
463 /* allocate memory for a new list member */
464 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
465 (*last_cmd)->next = NULL;
466 last_comand_pointer = &((*last_cmd)->next);
467 (*last_cmd)->type = JTAG_SCAN;
469 /* allocate memory for ir scan command */
470 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
471 (*last_cmd)->cmd.scan->ir_scan = 1;
472 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
473 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
474 (*last_cmd)->cmd.scan->end_state = state;
476 for (i = 0; i < jtag_num_devices; i++)
479 device = jtag_get_device(i);
480 scan_size = device->ir_length;
481 (*last_cmd)->cmd.scan->fields[i].device = i;
482 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
483 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
484 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
486 /* search the list */
487 for (j = 0; j < num_fields; j++)
489 if (i == fields[j].device)
492 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
493 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
495 if (jtag_verify_capture_ir)
497 if (fields[j].in_handler==NULL)
499 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
502 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
503 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
504 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
505 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
516 /* if a device isn't listed, set it to BYPASS */
517 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
518 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
523 /* update device information */
524 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
530 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
536 retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
537 if (retval!=ERROR_OK)
541 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
544 jtag_command_t **last_cmd;
546 last_cmd = jtag_get_last_command_p();
548 /* allocate memory for a new list member */
549 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
550 (*last_cmd)->next = NULL;
551 last_comand_pointer = &((*last_cmd)->next);
552 (*last_cmd)->type = JTAG_SCAN;
554 /* allocate memory for ir scan command */
555 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
556 (*last_cmd)->cmd.scan->ir_scan = 1;
557 (*last_cmd)->cmd.scan->num_fields = num_fields;
558 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
559 (*last_cmd)->cmd.scan->end_state = state;
561 for (i = 0; i < num_fields; i++)
563 int num_bits = fields[i].num_bits;
564 int num_bytes = CEIL(fields[i].num_bits, 8);
565 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
566 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
567 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
568 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
569 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
570 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
571 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
572 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
573 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
578 void jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
584 retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
585 if (retval!=ERROR_OK)
589 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
592 int bypass_devices = 0;
596 jtag_command_t **last_cmd = jtag_get_last_command_p();
597 jtag_device_t *device = jtag_devices;
599 /* count devices in bypass */
604 device = device->next;
606 if (bypass_devices >= jtag_num_devices)
608 LOG_ERROR("all devices in bypass");
609 return ERROR_JTAG_DEVICE_ERROR;
612 /* allocate memory for a new list member */
613 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
614 last_comand_pointer = &((*last_cmd)->next);
615 (*last_cmd)->next = NULL;
616 (*last_cmd)->type = JTAG_SCAN;
618 /* allocate memory for dr scan command */
619 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
620 (*last_cmd)->cmd.scan->ir_scan = 0;
621 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
622 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
623 (*last_cmd)->cmd.scan->end_state = state;
625 for (i = 0; i < jtag_num_devices; i++)
628 (*last_cmd)->cmd.scan->fields[field_count].device = i;
630 for (j = 0; j < num_fields; j++)
632 if (i == fields[j].device)
635 scan_size = fields[j].num_bits;
636 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
637 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
638 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
639 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
640 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
641 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
642 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
643 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
648 #ifdef _DEBUG_JTAG_IO_
649 /* if a device isn't listed, the BYPASS register should be selected */
650 if (!jtag_get_device(i)->bypass)
652 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
656 /* program the scan field to 1 bit length, and ignore it's value */
657 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
658 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
659 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
660 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
661 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
662 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
663 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
664 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
668 #ifdef _DEBUG_JTAG_IO_
669 /* if a device is listed, the BYPASS register must not be selected */
670 if (jtag_get_device(i)->bypass)
672 LOG_ERROR("BUG: scan data for a device in BYPASS");
681 void MINIDRIVER(interface_jtag_add_dr_out)(int device_num,
685 enum tap_state end_state)
690 int bypass_devices = 0;
692 jtag_command_t **last_cmd = jtag_get_last_command_p();
693 jtag_device_t *device = jtag_devices;
694 /* count devices in bypass */
699 device = device->next;
702 /* allocate memory for a new list member */
703 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
704 last_comand_pointer = &((*last_cmd)->next);
705 (*last_cmd)->next = NULL;
706 (*last_cmd)->type = JTAG_SCAN;
708 /* allocate memory for dr scan command */
709 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
710 (*last_cmd)->cmd.scan->ir_scan = 0;
711 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
712 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
713 (*last_cmd)->cmd.scan->end_state = end_state;
715 for (i = 0; i < jtag_num_devices; i++)
717 (*last_cmd)->cmd.scan->fields[field_count].device = i;
722 #ifdef _DEBUG_JTAG_IO_
723 /* if a device is listed, the BYPASS register must not be selected */
724 if (jtag_get_device(i)->bypass)
726 LOG_ERROR("BUG: scan data for a device in BYPASS");
730 for (j = 0; j < num_fields; j++)
733 scan_size = num_bits[j];
734 buf_set_u32(out_value, 0, scan_size, value[j]);
735 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
736 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
737 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
738 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
739 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
740 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
741 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
742 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
746 #ifdef _DEBUG_JTAG_IO_
747 /* if a device isn't listed, the BYPASS register should be selected */
748 if (!jtag_get_device(i)->bypass)
750 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
754 /* program the scan field to 1 bit length, and ignore it's value */
755 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
756 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
757 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
758 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
759 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
760 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
761 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
762 (*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 (path[i] == TAP_TLR)
859 LOG_ERROR("BUG: TAP_TLR is not a valid state for pathmove sequences");
862 if ((tap_transitions[cur_state].low != path[i])&&
863 (tap_transitions[cur_state].high != path[i]))
865 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
874 retval=interface_jtag_add_pathmove(num_states, path);
875 cmd_queue_cur_state = path[num_states - 1];
876 if (retval!=ERROR_OK)
880 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
882 jtag_command_t **last_cmd = jtag_get_last_command_p();
885 /* allocate memory for a new list member */
886 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
887 last_comand_pointer = &((*last_cmd)->next);
888 (*last_cmd)->next = NULL;
889 (*last_cmd)->type = JTAG_PATHMOVE;
891 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
892 (*last_cmd)->cmd.pathmove->num_states = num_states;
893 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
895 for (i = 0; i < num_states; i++)
896 (*last_cmd)->cmd.pathmove->path[i] = path[i];
901 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
903 jtag_command_t **last_cmd = jtag_get_last_command_p();
905 /* allocate memory for a new list member */
906 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
907 (*last_cmd)->next = NULL;
908 last_comand_pointer = &((*last_cmd)->next);
909 (*last_cmd)->type = JTAG_RUNTEST;
911 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
912 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
913 (*last_cmd)->cmd.runtest->end_state = state;
918 void jtag_add_runtest(int num_cycles, enum tap_state state)
924 /* executed by sw or hw fifo */
925 retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
926 if (retval!=ERROR_OK)
930 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
932 int trst_with_tlr = 0;
935 /* FIX!!! there are *many* different cases here. A better
936 * approach is needed for legal combinations of transitions...
938 if ((jtag_reset_config & RESET_HAS_SRST)&&
939 (jtag_reset_config & RESET_HAS_TRST)&&
940 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
942 if (((req_tlr_or_trst&&!jtag_trst)||
943 (!req_tlr_or_trst&&jtag_trst))&&
944 ((req_srst&&!jtag_srst)||
945 (!req_srst&&jtag_srst)))
947 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
948 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
952 /* Make sure that jtag_reset_config allows the requested reset */
953 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
954 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
956 LOG_ERROR("BUG: requested reset would assert trst");
957 jtag_error=ERROR_FAIL;
961 /* if TRST pulls SRST, we reset with TAP T-L-R */
962 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
967 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
969 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
970 jtag_error=ERROR_FAIL;
976 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
988 jtag_srst = req_srst;
990 retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
991 if (retval!=ERROR_OK)
999 LOG_DEBUG("SRST line asserted");
1003 LOG_DEBUG("SRST line released");
1004 if (jtag_nsrst_delay)
1005 jtag_add_sleep(jtag_nsrst_delay * 1000);
1010 LOG_DEBUG("JTAG reset with TLR instead of TRST");
1011 jtag_add_end_state(TAP_TLR);
1013 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1019 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1020 * and inform possible listeners about this
1022 LOG_DEBUG("TRST line asserted");
1023 cmd_queue_cur_state = TAP_TLR;
1024 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1028 if (jtag_ntrst_delay)
1029 jtag_add_sleep(jtag_ntrst_delay * 1000);
1033 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1035 jtag_command_t **last_cmd = jtag_get_last_command_p();
1037 /* allocate memory for a new list member */
1038 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1039 (*last_cmd)->next = NULL;
1040 last_comand_pointer = &((*last_cmd)->next);
1041 (*last_cmd)->type = JTAG_RESET;
1043 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1044 (*last_cmd)->cmd.reset->trst = req_trst;
1045 (*last_cmd)->cmd.reset->srst = req_srst;
1050 void jtag_add_end_state(enum tap_state state)
1052 cmd_queue_end_state = state;
1053 if ((cmd_queue_end_state == TAP_SD)||(cmd_queue_end_state == TAP_SI))
1055 LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
1059 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1061 jtag_command_t **last_cmd = jtag_get_last_command_p();
1063 /* allocate memory for a new list member */
1064 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1065 (*last_cmd)->next = NULL;
1066 last_comand_pointer = &((*last_cmd)->next);
1067 (*last_cmd)->type = JTAG_SLEEP;
1069 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1070 (*last_cmd)->cmd.sleep->us = us;
1075 void jtag_add_sleep(u32 us)
1077 keep_alive(); /* we might be running on a very slow JTAG clk */
1078 int retval=interface_jtag_add_sleep(us);
1079 if (retval!=ERROR_OK)
1084 int jtag_scan_size(scan_command_t *cmd)
1089 /* count bits in scan command */
1090 for (i = 0; i < cmd->num_fields; i++)
1092 bit_count += cmd->fields[i].num_bits;
1098 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1103 bit_count = jtag_scan_size(cmd);
1104 *buffer = malloc(CEIL(bit_count, 8));
1108 for (i = 0; i < cmd->num_fields; i++)
1110 if (cmd->fields[i].out_value)
1112 #ifdef _DEBUG_JTAG_IO_
1113 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1115 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1116 #ifdef _DEBUG_JTAG_IO_
1117 LOG_DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1122 bit_count += cmd->fields[i].num_bits;
1128 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1134 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1137 for (i = 0; i < cmd->num_fields; i++)
1139 /* if neither in_value nor in_handler
1140 * are specified we don't have to examine this field
1142 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1144 int num_bits = cmd->fields[i].num_bits;
1145 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1147 #ifdef _DEBUG_JTAG_IO_
1150 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1151 LOG_DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1155 if (cmd->fields[i].in_value)
1157 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1159 if (cmd->fields[i].in_handler)
1161 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1163 LOG_WARNING("in_handler reported a failed check");
1164 retval = ERROR_JTAG_QUEUE_FAILED;
1169 /* no in_value specified, but a handler takes care of the scanned data */
1170 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1172 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1174 /* We're going to call the error:handler later, but if the in_handler
1175 * reported an error we report this failure upstream
1177 LOG_WARNING("in_handler reported a failed check");
1178 retval = ERROR_JTAG_QUEUE_FAILED;
1184 bit_count += cmd->fields[i].num_bits;
1190 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1192 int retval = ERROR_OK;
1193 int num_bits = field->num_bits;
1195 int compare_failed = 0;
1197 if (field->in_check_mask)
1198 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1200 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1204 /* An error handler could have caught the failing check
1205 * only report a problem when there wasn't a handler, or if the handler
1206 * acknowledged the error
1210 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1211 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1213 if (field->in_check_mask)
1215 char *in_check_mask_char;
1216 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1217 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);
1218 free(in_check_mask_char);
1222 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);
1225 free(captured_char);
1226 free(in_check_value_char);
1228 retval = ERROR_JTAG_QUEUE_FAILED;
1236 set up checking of this field using the in_handler. The values passed in must be valid until
1237 after jtag_execute() has completed.
1239 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1242 field->in_handler = jtag_check_value;
1244 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1245 field->in_handler_priv = NULL;
1246 field->in_check_value = value;
1247 field->in_check_mask = mask;
1250 enum scan_type jtag_scan_type(scan_command_t *cmd)
1255 for (i = 0; i < cmd->num_fields; i++)
1257 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1259 if (cmd->fields[i].out_value)
1266 int MINIDRIVER(interface_jtag_execute_queue)(void)
1272 LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
1276 retval = jtag->execute_queue();
1280 jtag_command_queue = NULL;
1281 last_comand_pointer = &jtag_command_queue;
1286 int jtag_execute_queue(void)
1288 int retval=interface_jtag_execute_queue();
1289 if (retval==ERROR_OK)
1293 jtag_error=ERROR_OK;
1297 int jtag_reset_callback(enum jtag_event event, void *priv)
1299 jtag_device_t *device = priv;
1303 if (event == JTAG_TRST_ASSERTED)
1305 buf_set_ones(device->cur_instr, device->ir_length);
1312 void jtag_sleep(u32 us)
1317 /* Try to examine chain layout according to IEEE 1149.1 §12
1319 int jtag_examine_chain()
1321 jtag_device_t *device = jtag_devices;
1323 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1326 int device_count = 0;
1327 u8 zero_check = 0x0;
1328 u8 one_check = 0xff;
1331 field.num_bits = sizeof(idcode_buffer) * 8;
1332 field.out_value = idcode_buffer;
1333 field.out_mask = NULL;
1334 field.in_value = idcode_buffer;
1335 field.in_check_value = NULL;
1336 field.in_check_mask = NULL;
1337 field.in_handler = NULL;
1338 field.in_handler_priv = NULL;
1340 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1342 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1345 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1346 jtag_execute_queue();
1348 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1350 zero_check |= idcode_buffer[i];
1351 one_check &= idcode_buffer[i];
1354 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1355 if ((zero_check == 0x00) || (one_check == 0xff))
1357 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1358 return ERROR_JTAG_INIT_FAILED;
1361 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1363 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1364 if ((idcode & 1) == 0)
1366 /* LSB must not be 0, this indicates a device in bypass */
1369 LOG_WARNING("Device was in bypass after TRST/TMS reset");
1379 if (idcode == 0x000000FF)
1382 /* End of chain (invalid manufacturer ID)
1384 * The JTAG examine is the very first thing that happens
1386 * A single JTAG device requires only 64 bits to be read back correctly.
1388 * The code below adds a check that the rest of the data scanned (640 bits)
1389 * are all as expected. This helps diagnose/catch problems with the JTAG chain
1391 * earlier and gives more helpful/explicit error messages.
1393 for (bit_count += 32; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;bit_count += 32)
1395 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1396 if (unexpected||(idcode != 0x000000FF))
1398 LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count, idcode);
1408 device->idcode = idcode;
1409 device = device->next;
1413 manufacturer = (idcode & 0xffe) >> 1;
1414 part = (idcode & 0xffff000) >> 12;
1415 version = (idcode & 0xf0000000) >> 28;
1417 LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1418 idcode, manufacturer, part, version);
1424 /* see if number of discovered devices matches configuration */
1425 if (device_count != jtag_num_devices)
1427 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1428 device_count, jtag_num_devices);
1429 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1430 return ERROR_JTAG_INIT_FAILED;
1436 int jtag_validate_chain()
1438 jtag_device_t *device = jtag_devices;
1439 int total_ir_length = 0;
1446 total_ir_length += device->ir_length;
1447 device = device->next;
1450 total_ir_length += 2;
1451 ir_test = malloc(CEIL(total_ir_length, 8));
1452 buf_set_ones(ir_test, total_ir_length);
1455 field.num_bits = total_ir_length;
1456 field.out_value = ir_test;
1457 field.out_mask = NULL;
1458 field.in_value = ir_test;
1459 field.in_check_value = NULL;
1460 field.in_check_mask = NULL;
1461 field.in_handler = NULL;
1462 field.in_handler_priv = NULL;
1464 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1465 jtag_execute_queue();
1467 device = jtag_devices;
1470 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1472 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1473 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1476 return ERROR_JTAG_INIT_FAILED;
1478 chain_pos += device->ir_length;
1479 device = device->next;
1482 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1484 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1485 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1488 return ERROR_JTAG_INIT_FAILED;
1496 int jtag_register_commands(struct command_context_s *cmd_ctx)
1498 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1499 COMMAND_CONFIG, NULL);
1500 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1501 COMMAND_ANY, "set jtag speed (if supported)");
1502 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1503 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1504 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1505 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1506 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1507 COMMAND_CONFIG, NULL);
1508 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1509 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
1510 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1511 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
1513 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1514 COMMAND_EXEC, "print current scan chain configuration");
1516 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1517 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1518 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1519 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1520 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1521 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1522 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1523 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1524 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
1526 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1527 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1531 int jtag_interface_init(struct command_context_s *cmd_ctx)
1536 if (!jtag_interface)
1538 /* nothing was previously specified by "interface" command */
1539 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1540 return ERROR_JTAG_INVALID_INTERFACE;
1544 jtag_interface->khz(speed_khz, &jtag_speed);
1548 if (jtag_interface->init() != ERROR_OK)
1549 return ERROR_JTAG_INIT_FAILED;
1553 jtag = jtag_interface;
1557 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1559 int validate_tries = 0;
1560 jtag_device_t *device;
1563 LOG_DEBUG("Init JTAG chain");
1565 device = jtag_devices;
1566 jtag_ir_scan_size = 0;
1567 jtag_num_devices = 0;
1568 while (device != NULL)
1570 jtag_ir_scan_size += device->ir_length;
1572 device = device->next;
1576 if ((retval=jtag_execute_queue())!=ERROR_OK)
1579 /* examine chain first, as this could discover the real chain layout */
1580 if (jtag_examine_chain() != ERROR_OK)
1582 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1585 while (jtag_validate_chain() != ERROR_OK)
1589 if (validate_tries > 5)
1591 LOG_ERROR("Could not validate JTAG chain, exit");
1592 return ERROR_JTAG_INVALID_INTERFACE;
1600 int jtag_init_reset(struct command_context_s *cmd_ctx)
1604 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1607 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
1609 /* Reset can happen after a power cycle.
1611 * Ideally we would only assert TRST or run TLR before the target reset.
1613 * However w/srst_pulls_trst, trst is asserted together with the target
1614 * reset whether we want it or not.
1616 * NB! Some targets have JTAG circuitry disabled until a
1617 * trst & srst has been asserted.
1619 * NB! here we assume nsrst/ntrst delay are sufficient!
1621 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1624 jtag_add_reset(1, 0); /* TLR or TRST */
1625 if (jtag_reset_config & RESET_HAS_SRST)
1627 jtag_add_reset(1, 1);
1628 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
1629 jtag_add_reset(0, 1);
1631 jtag_add_reset(0, 0);
1632 if ((retval = jtag_execute_queue()) != ERROR_OK)
1635 /* Check that we can communication on the JTAG chain + eventually we want to
1636 * be able to perform enumeration only after OpenOCD has started
1637 * telnet and GDB server
1639 * That would allow users to more easily perform any magic they need to before
1642 return jtag_init_inner(cmd_ctx);
1645 int jtag_init(struct command_context_s *cmd_ctx)
1648 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1650 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
1654 return jtag_init_reset(cmd_ctx);
1657 static int default_khz(int khz, int *jtag_speed)
1659 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1663 static int default_speed_div(int speed, int *khz)
1668 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1672 /* check whether the interface is already configured */
1675 LOG_WARNING("Interface already configured, ignoring");
1679 /* interface name is a mandatory argument */
1680 if (argc < 1 || args[0][0] == '\0')
1682 return ERROR_COMMAND_SYNTAX_ERROR;
1685 for (i=0; jtag_interfaces[i]; i++)
1687 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1689 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1692 jtag_interface = jtag_interfaces[i];
1694 if (jtag_interface->khz == NULL)
1696 jtag_interface->khz = default_khz;
1698 if (jtag_interface->speed_div == NULL)
1700 jtag_interface->speed_div = default_speed_div;
1706 /* no valid interface was found (i.e. the configuration option,
1707 * didn't match one of the compiled-in interfaces
1709 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
1710 LOG_ERROR("compiled-in jtag interfaces:");
1711 for (i = 0; jtag_interfaces[i]; i++)
1713 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
1716 return ERROR_JTAG_INVALID_INTERFACE;
1719 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1721 jtag_device_t **last_device_p = &jtag_devices;
1725 while ((*last_device_p)->next)
1726 last_device_p = &((*last_device_p)->next);
1727 last_device_p = &((*last_device_p)->next);
1733 *last_device_p = malloc(sizeof(jtag_device_t));
1734 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1736 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1737 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1738 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1739 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1741 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1742 (*last_device_p)->bypass = 1;
1743 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1745 (*last_device_p)->next = NULL;
1747 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1754 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1756 jtag_device_t *device = jtag_devices;
1757 int device_count = 0;
1761 u32 expected, expected_mask, cur_instr;
1762 expected = buf_get_u32(device->expected, 0, device->ir_length);
1763 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1764 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1765 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);
1766 device = device->next;
1773 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1776 return ERROR_COMMAND_SYNTAX_ERROR;
1780 if (strcmp(args[0], "none") == 0)
1781 jtag_reset_config = RESET_NONE;
1782 else if (strcmp(args[0], "trst_only") == 0)
1783 jtag_reset_config = RESET_HAS_TRST;
1784 else if (strcmp(args[0], "srst_only") == 0)
1785 jtag_reset_config = RESET_HAS_SRST;
1786 else if (strcmp(args[0], "trst_and_srst") == 0)
1787 jtag_reset_config = RESET_TRST_AND_SRST;
1790 LOG_ERROR("invalid reset_config argument, defaulting to none");
1791 jtag_reset_config = RESET_NONE;
1792 return ERROR_INVALID_ARGUMENTS;
1798 if (strcmp(args[1], "separate") == 0)
1800 /* seperate reset lines - default */
1803 if (strcmp(args[1], "srst_pulls_trst") == 0)
1804 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1805 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1806 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1807 else if (strcmp(args[1], "combined") == 0)
1808 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1811 LOG_ERROR("invalid reset_config argument, defaulting to none");
1812 jtag_reset_config = RESET_NONE;
1813 return ERROR_INVALID_ARGUMENTS;
1820 if (strcmp(args[2], "trst_open_drain") == 0)
1821 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1822 else if (strcmp(args[2], "trst_push_pull") == 0)
1823 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1826 LOG_ERROR("invalid reset_config argument, defaulting to none");
1827 jtag_reset_config = RESET_NONE;
1828 return ERROR_INVALID_ARGUMENTS;
1834 if (strcmp(args[3], "srst_push_pull") == 0)
1835 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1836 else if (strcmp(args[3], "srst_open_drain") == 0)
1837 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1840 LOG_ERROR("invalid reset_config argument, defaulting to none");
1841 jtag_reset_config = RESET_NONE;
1842 return ERROR_INVALID_ARGUMENTS;
1849 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1853 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1858 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1864 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1868 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1873 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1879 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1881 int retval=ERROR_OK;
1885 LOG_DEBUG("handle jtag speed");
1888 cur_speed = jtag_speed = strtoul(args[0], NULL, 0);
1890 /* this command can be called during CONFIG,
1891 * in which case jtag isn't initialized */
1894 retval=jtag->speed(cur_speed);
1896 } else if (argc == 0)
1900 retval=ERROR_COMMAND_SYNTAX_ERROR;
1902 command_print(cmd_ctx, "jtag_speed: %d", jtag_speed);
1907 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1909 int retval=ERROR_OK;
1910 LOG_DEBUG("handle jtag khz");
1914 speed_khz = strtoul(args[0], NULL, 0);
1918 LOG_DEBUG("have interface set up");
1920 if ((retval=jtag->khz(speed_khz, &speed_div1))!=ERROR_OK)
1926 cur_speed = jtag_speed = speed_div1;
1928 retval=jtag->speed(cur_speed);
1937 retval=ERROR_COMMAND_SYNTAX_ERROR;
1939 command_print(cmd_ctx, "jtag_khz: %d", speed_khz);
1944 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1946 enum tap_state state;
1950 return ERROR_COMMAND_SYNTAX_ERROR;
1954 for (state = 0; state < 16; state++)
1956 if (strcmp(args[0], tap_state_strings[state]) == 0)
1958 jtag_add_end_state(state);
1959 jtag_execute_queue();
1963 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
1968 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1975 return ERROR_COMMAND_SYNTAX_ERROR;
1978 if (args[0][0] == '1')
1980 else if (args[0][0] == '0')
1984 return ERROR_COMMAND_SYNTAX_ERROR;
1987 if (args[1][0] == '1')
1989 else if (args[1][0] == '0')
1993 return ERROR_COMMAND_SYNTAX_ERROR;
1996 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
1997 return ERROR_JTAG_INIT_FAILED;
1999 jtag_add_reset(trst, srst);
2000 jtag_execute_queue();
2005 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2009 return ERROR_COMMAND_SYNTAX_ERROR;
2012 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
2013 jtag_execute_queue();
2019 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2022 scan_field_t *fields;
2024 if ((argc < 2) || (argc % 2))
2026 return ERROR_COMMAND_SYNTAX_ERROR;
2029 fields = malloc(sizeof(scan_field_t) * argc / 2);
2031 for (i = 0; i < argc / 2; i++)
2033 int device = strtoul(args[i*2], NULL, 0);
2034 int field_size = jtag_get_device(device)->ir_length;
2035 fields[i].device = device;
2036 fields[i].out_value = malloc(CEIL(field_size, 8));
2037 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
2038 fields[i].out_mask = NULL;
2039 fields[i].in_value = NULL;
2040 fields[i].in_check_mask = NULL;
2041 fields[i].in_handler = NULL;
2042 fields[i].in_handler_priv = NULL;
2045 jtag_add_ir_scan(argc / 2, fields, -1);
2046 jtag_execute_queue();
2048 for (i = 0; i < argc / 2; i++)
2049 free(fields[i].out_value);
2056 int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
2059 scan_field_t *fields;
2061 int field_count = 0;
2066 * args[2] = num_bits
2067 * args[3] = hex string
2068 * ... repeat num bits and hex string ...
2070 if ((argc < 4) || ((argc % 2)!=0))
2072 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
2076 for (i = 2; i < argc; i+=2)
2080 e = Jim_GetLong(interp, args[i], &bits);
2085 e = Jim_GetLong(interp, args[1], &device);
2089 num_fields=(argc-2)/2;
2090 fields = malloc(sizeof(scan_field_t) * num_fields);
2091 for (i = 2; i < argc; i+=2)
2097 Jim_GetLong(interp, args[i], &bits);
2098 str = Jim_GetString(args[i+1], &len);
2100 fields[field_count].device = device;
2101 fields[field_count].num_bits = bits;
2102 fields[field_count].out_value = malloc(CEIL(bits, 8));
2103 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
2104 fields[field_count].out_mask = NULL;
2105 fields[field_count].in_value = fields[field_count].out_value;
2106 fields[field_count].in_check_mask = NULL;
2107 fields[field_count].in_check_value = NULL;
2108 fields[field_count].in_handler = NULL;
2109 fields[field_count++].in_handler_priv = NULL;
2112 jtag_add_dr_scan(num_fields, fields, -1);
2113 retval = jtag_execute_queue();
2114 if (retval != ERROR_OK)
2116 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2117 Jim_AppendStrings(interp, Jim_GetResult(interp), "drscan: jtag execute failed", NULL);
2122 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
2123 for (i = 2; i < argc; i+=2)
2128 Jim_GetLong(interp, args[i], &bits);
2129 str = buf_to_str(fields[field_count].in_value, bits, 16);
2130 free(fields[field_count].out_value);
2132 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
2137 Jim_SetResult(interp, list);
2144 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2148 if (strcmp(args[0], "enable") == 0)
2150 jtag_verify_capture_ir = 1;
2152 else if (strcmp(args[0], "disable") == 0)
2154 jtag_verify_capture_ir = 0;
2157 return ERROR_COMMAND_SYNTAX_ERROR;
2159 } else if (argc != 0)
2161 return ERROR_COMMAND_SYNTAX_ERROR;
2164 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");