1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "replacements.h"
30 #include "interpreter.h"
37 /* note that this is not marked as static as it must be available from outside jtag.c for those
38 that implement the jtag_xxx() minidriver layer
40 int jtag_error=ERROR_OK;
43 char* tap_state_strings[16] =
46 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
48 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
51 typedef struct cmd_queue_page_s
55 struct cmd_queue_page_s *next;
58 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
59 static cmd_queue_page_t *cmd_queue_pages = NULL;
61 /* tap_move[i][j]: tap movement command to go from state i to state j
69 * SD->SD and SI->SI have to be caught in interface specific code
73 /* TLR RTI SD PD SI PI */
74 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
75 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
76 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
77 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
78 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
79 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
82 int tap_move_map[16] = {
83 0, -1, -1, 2, -1, 3, -1, -1,
84 1, -1, -1, 4, -1, 5, -1, -1
87 tap_transition_t tap_transitions[16] =
89 {TAP_TLR, TAP_RTI}, /* TLR */
90 {TAP_SIS, TAP_CD}, /* SDS */
91 {TAP_E1D, TAP_SD}, /* CD */
92 {TAP_E1D, TAP_SD}, /* SD */
93 {TAP_UD, TAP_PD}, /* E1D */
94 {TAP_E2D, TAP_PD}, /* PD */
95 {TAP_UD, TAP_SD}, /* E2D */
96 {TAP_SDS, TAP_RTI}, /* UD */
97 {TAP_SDS, TAP_RTI}, /* RTI */
98 {TAP_TLR, TAP_CI}, /* SIS */
99 {TAP_E1I, TAP_SI}, /* CI */
100 {TAP_E1I, TAP_SI}, /* SI */
101 {TAP_UI, TAP_PI}, /* E1I */
102 {TAP_E2I, TAP_PI}, /* PI */
103 {TAP_UI, TAP_SI}, /* E2I */
104 {TAP_SDS, TAP_RTI} /* UI */
107 char* jtag_event_strings[] =
109 "JTAG controller reset(tms or TRST)"
112 /* kludge!!!! these are just global variables that the
113 * interface use internally. They really belong
114 * inside the drivers, but we don't want to break
115 * linking the drivers!!!!
117 enum tap_state end_state = TAP_TLR;
118 enum tap_state cur_state = TAP_TLR;
122 jtag_command_t *jtag_command_queue = NULL;
123 jtag_command_t **last_comand_pointer = &jtag_command_queue;
124 jtag_device_t *jtag_devices = NULL;
125 int jtag_num_devices = 0;
126 int jtag_ir_scan_size = 0;
127 enum reset_types jtag_reset_config = RESET_NONE;
128 enum tap_state cmd_queue_end_state = TAP_TLR;
129 enum tap_state cmd_queue_cur_state = TAP_TLR;
131 int jtag_verify_capture_ir = 1;
133 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
134 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
135 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
137 /* maximum number of JTAG devices expected in the chain
139 #define JTAG_MAX_CHAIN_SIZE 20
141 /* callbacks to inform high-level handlers about JTAG state changes */
142 jtag_event_callback_t *jtag_event_callbacks;
145 static int speed1 = 0, speed2 = 0;
146 /* flag if the kHz speed was defined */
147 static int hasKHz = 0;
149 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
152 #if BUILD_ECOSBOARD == 1
153 extern jtag_interface_t eCosBoard_interface;
156 #if BUILD_PARPORT == 1
157 extern jtag_interface_t parport_interface;
160 #if BUILD_FT2232_FTD2XX == 1
161 extern jtag_interface_t ft2232_interface;
164 #if BUILD_FT2232_LIBFTDI == 1
165 extern jtag_interface_t ft2232_interface;
168 #if BUILD_AMTJTAGACCEL == 1
169 extern jtag_interface_t amt_jtagaccel_interface;
172 #if BUILD_EP93XX == 1
173 extern jtag_interface_t ep93xx_interface;
176 #if BUILD_AT91RM9200 == 1
177 extern jtag_interface_t at91rm9200_interface;
180 #if BUILD_GW16012 == 1
181 extern jtag_interface_t gw16012_interface;
184 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
185 extern jtag_interface_t presto_interface;
188 #if BUILD_USBPROG == 1
189 extern jtag_interface_t usbprog_interface;
192 jtag_interface_t *jtag_interfaces[] = {
193 #if BUILD_ECOSBOARD == 1
194 &eCosBoard_interface,
196 #if BUILD_PARPORT == 1
199 #if BUILD_FT2232_FTD2XX == 1
202 #if BUILD_FT2232_LIBFTDI == 1
205 #if BUILD_AMTJTAGACCEL == 1
206 &amt_jtagaccel_interface,
208 #if BUILD_EP93XX == 1
211 #if BUILD_AT91RM9200 == 1
212 &at91rm9200_interface,
214 #if BUILD_GW16012 == 1
217 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
220 #if BUILD_USBPROG == 1
226 jtag_interface_t *jtag = NULL;
229 jtag_interface_t *jtag_interface = NULL;
231 int jtag_speed_post_reset = 0;
234 /* forward declarations */
235 void jtag_add_pathmove(int num_states, enum tap_state *path);
236 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
237 void jtag_add_end_state(enum tap_state endstate);
238 void jtag_add_sleep(u32 us);
239 int jtag_execute_queue(void);
240 int jtag_cancel_queue(void);
243 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
244 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
245 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
246 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
247 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
248 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
249 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
251 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
253 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
254 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
255 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
256 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
257 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
259 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
261 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
263 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
265 if (callback == NULL)
267 return ERROR_INVALID_ARGUMENTS;
272 while ((*callbacks_p)->next)
273 callbacks_p = &((*callbacks_p)->next);
274 callbacks_p = &((*callbacks_p)->next);
277 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
278 (*callbacks_p)->callback = callback;
279 (*callbacks_p)->priv = priv;
280 (*callbacks_p)->next = NULL;
285 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
287 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
289 if (callback == NULL)
291 return ERROR_INVALID_ARGUMENTS;
296 jtag_event_callback_t **next = &((*callbacks_p)->next);
297 if ((*callbacks_p)->callback == callback)
300 *callbacks_p = *next;
308 int jtag_call_event_callbacks(enum jtag_event event)
310 jtag_event_callback_t *callback = jtag_event_callbacks;
312 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
316 callback->callback(event, callback->priv);
317 callback = callback->next;
323 /* returns a pointer to the pointer of the last command in queue
324 * this may be a pointer to the root pointer (jtag_command_queue)
325 * or to the next member of the last but one command
327 jtag_command_t** jtag_get_last_command_p(void)
329 /* jtag_command_t *cmd = jtag_command_queue;
335 return &jtag_command_queue;
339 return last_comand_pointer;
342 /* returns a pointer to the n-th device in the scan chain */
343 jtag_device_t* jtag_get_device(int num)
345 jtag_device_t *device = jtag_devices;
352 device = device->next;
356 LOG_ERROR("jtag device number %d not defined", num);
360 void* cmd_queue_alloc(size_t size)
362 cmd_queue_page_t **p_page = &cmd_queue_pages;
368 while ((*p_page)->next)
369 p_page = &((*p_page)->next);
370 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
371 p_page = &((*p_page)->next);
376 *p_page = malloc(sizeof(cmd_queue_page_t));
378 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
379 (*p_page)->next = NULL;
382 offset = (*p_page)->used;
383 (*p_page)->used += size;
385 t=(u8 *)((*p_page)->address);
389 void cmd_queue_free()
391 cmd_queue_page_t *page = cmd_queue_pages;
395 cmd_queue_page_t *last = page;
401 cmd_queue_pages = NULL;
404 static void jtag_prelude1()
408 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
409 jtag_error=ERROR_JTAG_TRST_ASSERTED;
413 if (cmd_queue_end_state == TAP_TLR)
414 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
417 static void jtag_prelude(enum tap_state state)
422 jtag_add_end_state(state);
424 cmd_queue_cur_state = cmd_queue_end_state;
427 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
433 retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
434 if (retval!=ERROR_OK)
438 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
440 jtag_command_t **last_cmd;
441 jtag_device_t *device;
446 last_cmd = jtag_get_last_command_p();
448 /* allocate memory for a new list member */
449 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
450 (*last_cmd)->next = NULL;
451 last_comand_pointer = &((*last_cmd)->next);
452 (*last_cmd)->type = JTAG_SCAN;
454 /* allocate memory for ir scan command */
455 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
456 (*last_cmd)->cmd.scan->ir_scan = 1;
457 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
458 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
459 (*last_cmd)->cmd.scan->end_state = state;
461 for (i = 0; i < jtag_num_devices; i++)
464 device = jtag_get_device(i);
465 scan_size = device->ir_length;
466 (*last_cmd)->cmd.scan->fields[i].device = i;
467 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
468 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
469 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
471 /* search the list */
472 for (j = 0; j < num_fields; j++)
474 if (i == fields[j].device)
477 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
478 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
480 if (jtag_verify_capture_ir)
482 if (fields[j].in_handler==NULL)
484 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
487 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
488 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
489 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
490 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
501 /* if a device isn't listed, set it to BYPASS */
502 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
503 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
508 /* update device information */
509 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
515 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
521 retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
522 if (retval!=ERROR_OK)
526 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
529 jtag_command_t **last_cmd;
531 last_cmd = jtag_get_last_command_p();
533 /* allocate memory for a new list member */
534 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
535 (*last_cmd)->next = NULL;
536 last_comand_pointer = &((*last_cmd)->next);
537 (*last_cmd)->type = JTAG_SCAN;
539 /* allocate memory for ir scan command */
540 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
541 (*last_cmd)->cmd.scan->ir_scan = 1;
542 (*last_cmd)->cmd.scan->num_fields = num_fields;
543 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
544 (*last_cmd)->cmd.scan->end_state = state;
546 for (i = 0; i < num_fields; i++)
548 int num_bits = fields[i].num_bits;
549 int num_bytes = CEIL(fields[i].num_bits, 8);
550 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
551 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
552 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
553 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
554 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
555 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
556 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
557 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
558 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
563 void jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
569 retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
570 if (retval!=ERROR_OK)
574 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
577 int bypass_devices = 0;
581 jtag_command_t **last_cmd = jtag_get_last_command_p();
582 jtag_device_t *device = jtag_devices;
584 /* count devices in bypass */
589 device = device->next;
592 /* allocate memory for a new list member */
593 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
594 last_comand_pointer = &((*last_cmd)->next);
595 (*last_cmd)->next = NULL;
596 (*last_cmd)->type = JTAG_SCAN;
598 /* allocate memory for dr scan command */
599 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
600 (*last_cmd)->cmd.scan->ir_scan = 0;
601 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
602 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
603 (*last_cmd)->cmd.scan->end_state = state;
605 for (i = 0; i < jtag_num_devices; i++)
608 (*last_cmd)->cmd.scan->fields[field_count].device = i;
610 for (j = 0; j < num_fields; j++)
612 if (i == fields[j].device)
615 scan_size = fields[j].num_bits;
616 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
617 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
618 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
619 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
620 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
621 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
622 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
623 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
628 #ifdef _DEBUG_JTAG_IO_
629 /* if a device isn't listed, the BYPASS register should be selected */
630 if (!jtag_get_device(i)->bypass)
632 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
636 /* program the scan field to 1 bit length, and ignore it's value */
637 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
638 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
639 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
640 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
641 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
642 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
643 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
644 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
648 #ifdef _DEBUG_JTAG_IO_
649 /* if a device is listed, the BYPASS register must not be selected */
650 if (jtag_get_device(i)->bypass)
652 LOG_ERROR("BUG: scan data for a device in BYPASS");
661 void MINIDRIVER(interface_jtag_add_dr_out)(int device_num,
665 enum tap_state end_state)
670 int bypass_devices = 0;
672 jtag_command_t **last_cmd = jtag_get_last_command_p();
673 jtag_device_t *device = jtag_devices;
674 /* count devices in bypass */
679 device = device->next;
682 /* allocate memory for a new list member */
683 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
684 last_comand_pointer = &((*last_cmd)->next);
685 (*last_cmd)->next = NULL;
686 (*last_cmd)->type = JTAG_SCAN;
688 /* allocate memory for dr scan command */
689 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
690 (*last_cmd)->cmd.scan->ir_scan = 0;
691 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
692 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
693 (*last_cmd)->cmd.scan->end_state = end_state;
695 for (i = 0; i < jtag_num_devices; i++)
697 (*last_cmd)->cmd.scan->fields[field_count].device = i;
702 #ifdef _DEBUG_JTAG_IO_
703 /* if a device is listed, the BYPASS register must not be selected */
704 if (jtag_get_device(i)->bypass)
706 LOG_ERROR("BUG: scan data for a device in BYPASS");
710 for (j = 0; j < num_fields; j++)
713 scan_size = num_bits[j];
714 buf_set_u32(out_value, 0, scan_size, value[j]);
715 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
716 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
717 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
718 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
719 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
720 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
721 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
722 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
726 #ifdef _DEBUG_JTAG_IO_
727 /* if a device isn't listed, the BYPASS register should be selected */
728 if (!jtag_get_device(i)->bypass)
730 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
734 /* program the scan field to 1 bit length, and ignore it's value */
735 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
736 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
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;
750 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
756 retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
757 if (retval!=ERROR_OK)
761 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
764 jtag_command_t **last_cmd = jtag_get_last_command_p();
766 /* allocate memory for a new list member */
767 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
768 last_comand_pointer = &((*last_cmd)->next);
769 (*last_cmd)->next = NULL;
770 (*last_cmd)->type = JTAG_SCAN;
772 /* allocate memory for scan command */
773 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
774 (*last_cmd)->cmd.scan->ir_scan = 0;
775 (*last_cmd)->cmd.scan->num_fields = num_fields;
776 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
777 (*last_cmd)->cmd.scan->end_state = state;
779 for (i = 0; i < num_fields; i++)
781 int num_bits = fields[i].num_bits;
782 int num_bytes = CEIL(fields[i].num_bits, 8);
783 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
784 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
785 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
786 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
787 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
788 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
789 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
790 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
791 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
799 jtag_prelude(TAP_TLR);
802 retval=interface_jtag_add_tlr();
803 if (retval!=ERROR_OK)
807 int MINIDRIVER(interface_jtag_add_tlr)()
809 enum tap_state state = TAP_TLR;
810 jtag_command_t **last_cmd = jtag_get_last_command_p();
812 /* allocate memory for a new list member */
813 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
814 last_comand_pointer = &((*last_cmd)->next);
815 (*last_cmd)->next = NULL;
816 (*last_cmd)->type = JTAG_STATEMOVE;
818 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
819 (*last_cmd)->cmd.statemove->end_state = state;
825 void jtag_add_pathmove(int num_states, enum tap_state *path)
827 enum tap_state cur_state=cmd_queue_cur_state;
831 /* the last state has to be a stable state */
832 if (tap_move_map[path[num_states - 1]] == -1)
834 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
838 for (i=0; i<num_states; i++)
840 if ((tap_transitions[cur_state].low != path[i])&&
841 (tap_transitions[cur_state].high != path[i]))
843 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
851 cmd_queue_cur_state = path[num_states - 1];
853 retval=interface_jtag_add_pathmove(num_states, path);
854 if (retval!=ERROR_OK)
859 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
861 jtag_command_t **last_cmd = jtag_get_last_command_p();
864 /* allocate memory for a new list member */
865 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
866 last_comand_pointer = &((*last_cmd)->next);
867 (*last_cmd)->next = NULL;
868 (*last_cmd)->type = JTAG_PATHMOVE;
870 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
871 (*last_cmd)->cmd.pathmove->num_states = num_states;
872 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
874 for (i = 0; i < num_states; i++)
875 (*last_cmd)->cmd.pathmove->path[i] = path[i];
880 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
882 jtag_command_t **last_cmd = jtag_get_last_command_p();
884 /* allocate memory for a new list member */
885 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
886 (*last_cmd)->next = NULL;
887 last_comand_pointer = &((*last_cmd)->next);
888 (*last_cmd)->type = JTAG_RUNTEST;
890 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
891 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
892 (*last_cmd)->cmd.runtest->end_state = state;
897 void jtag_add_runtest(int num_cycles, enum tap_state state)
903 /* executed by sw or hw fifo */
904 retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
905 if (retval!=ERROR_OK)
909 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
911 int trst_with_tlr = 0;
914 /* FIX!!! there are *many* different cases here. A better
915 * approach is needed for legal combinations of transitions...
917 if ((jtag_reset_config & RESET_HAS_SRST)&&
918 (jtag_reset_config & RESET_HAS_TRST)&&
919 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)&&
920 ((jtag_reset_config & RESET_TRST_PULLS_SRST)==0))
922 if (((req_tlr_or_trst&&!jtag_trst)||
923 (!req_tlr_or_trst&&jtag_trst))&&
924 ((req_srst&&!jtag_srst)||
925 (!req_srst&&jtag_srst)))
927 LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
931 /* Make sure that jtag_reset_config allows the requested reset */
932 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
933 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
935 LOG_ERROR("BUG: requested reset would assert trst");
936 jtag_error=ERROR_FAIL;
940 /* if TRST pulls SRST, we reset with TAP T-L-R */
941 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
946 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
948 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
949 jtag_error=ERROR_FAIL;
955 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
967 jtag_srst = req_srst;
969 retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
970 if (retval!=ERROR_OK)
978 LOG_DEBUG("SRST line asserted");
982 LOG_DEBUG("SRST line released");
983 if (jtag_nsrst_delay)
984 jtag_add_sleep(jtag_nsrst_delay * 1000);
989 LOG_DEBUG("JTAG reset with tms instead of TRST");
990 jtag_add_end_state(TAP_TLR);
992 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
998 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
999 * and inform possible listeners about this
1001 LOG_DEBUG("TRST line asserted");
1002 cmd_queue_cur_state = TAP_TLR;
1003 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1007 if (jtag_ntrst_delay)
1008 jtag_add_sleep(jtag_ntrst_delay * 1000);
1012 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1014 jtag_command_t **last_cmd = jtag_get_last_command_p();
1016 /* allocate memory for a new list member */
1017 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1018 (*last_cmd)->next = NULL;
1019 last_comand_pointer = &((*last_cmd)->next);
1020 (*last_cmd)->type = JTAG_RESET;
1022 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1023 (*last_cmd)->cmd.reset->trst = req_trst;
1024 (*last_cmd)->cmd.reset->srst = req_srst;
1030 void jtag_add_end_state(enum tap_state state)
1032 cmd_queue_end_state = state;
1033 if ((cmd_queue_end_state == TAP_SD)||(cmd_queue_end_state == TAP_SD))
1035 LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
1039 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1041 jtag_command_t **last_cmd = jtag_get_last_command_p();
1043 /* allocate memory for a new list member */
1044 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1045 (*last_cmd)->next = NULL;
1046 last_comand_pointer = &((*last_cmd)->next);
1047 (*last_cmd)->type = JTAG_SLEEP;
1049 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1050 (*last_cmd)->cmd.sleep->us = us;
1055 void jtag_add_sleep(u32 us)
1057 int retval=interface_jtag_add_sleep(us);
1058 if (retval!=ERROR_OK)
1063 int jtag_scan_size(scan_command_t *cmd)
1068 /* count bits in scan command */
1069 for (i = 0; i < cmd->num_fields; i++)
1071 bit_count += cmd->fields[i].num_bits;
1077 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1082 bit_count = jtag_scan_size(cmd);
1083 *buffer = malloc(CEIL(bit_count, 8));
1087 for (i = 0; i < cmd->num_fields; i++)
1089 if (cmd->fields[i].out_value)
1091 #ifdef _DEBUG_JTAG_IO_
1092 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1094 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1095 #ifdef _DEBUG_JTAG_IO_
1096 LOG_DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1101 bit_count += cmd->fields[i].num_bits;
1108 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1114 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1117 for (i = 0; i < cmd->num_fields; i++)
1119 /* if neither in_value nor in_handler
1120 * are specified we don't have to examine this field
1122 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1124 int num_bits = cmd->fields[i].num_bits;
1125 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1127 #ifdef _DEBUG_JTAG_IO_
1130 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1131 LOG_DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1135 if (cmd->fields[i].in_value)
1137 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1139 if (cmd->fields[i].in_handler)
1141 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1143 LOG_WARNING("in_handler reported a failed check");
1144 retval = ERROR_JTAG_QUEUE_FAILED;
1149 /* no in_value specified, but a handler takes care of the scanned data */
1150 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1152 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1154 /* We're going to call the error:handler later, but if the in_handler
1155 * reported an error we report this failure upstream
1157 LOG_WARNING("in_handler reported a failed check");
1158 retval = ERROR_JTAG_QUEUE_FAILED;
1164 bit_count += cmd->fields[i].num_bits;
1170 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1172 int retval = ERROR_OK;
1173 int num_bits = field->num_bits;
1175 int compare_failed = 0;
1177 if (field->in_check_mask)
1178 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1180 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1184 /* An error handler could have caught the failing check
1185 * only report a problem when there wasn't a handler, or if the handler
1186 * acknowledged the error
1190 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1191 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1193 if (field->in_check_mask)
1195 char *in_check_mask_char;
1196 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1197 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);
1198 free(in_check_mask_char);
1202 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);
1205 free(captured_char);
1206 free(in_check_value_char);
1208 retval = ERROR_JTAG_QUEUE_FAILED;
1216 set up checking of this field using the in_handler. The values passed in must be valid until
1217 after jtag_execute() has completed.
1219 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1222 field->in_handler = jtag_check_value;
1224 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1225 field->in_handler_priv = NULL; /* this will be filled in at the invocation site to point to the field duplicate */
1226 field->in_check_value = value;
1227 field->in_check_mask = mask;
1230 enum scan_type jtag_scan_type(scan_command_t *cmd)
1235 for (i = 0; i < cmd->num_fields; i++)
1237 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1239 if (cmd->fields[i].out_value)
1246 int MINIDRIVER(interface_jtag_execute_queue)(void)
1250 retval = jtag->execute_queue();
1254 jtag_command_queue = NULL;
1255 last_comand_pointer = &jtag_command_queue;
1260 int jtag_execute_queue(void)
1262 int retval=interface_jtag_execute_queue();
1263 if (retval==ERROR_OK)
1267 jtag_error=ERROR_OK;
1271 int jtag_reset_callback(enum jtag_event event, void *priv)
1273 jtag_device_t *device = priv;
1277 if (event == JTAG_TRST_ASSERTED)
1279 buf_set_ones(device->cur_instr, device->ir_length);
1286 void jtag_sleep(u32 us)
1291 int cwvx_device_num = -1, cwvx_version_num, cwvx_part_num;
1293 /* Try to examine chain layout according to IEEE 1149.1 §12
1295 int jtag_examine_chain()
1297 jtag_device_t *device = jtag_devices;
1299 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1302 int device_count = 0;
1303 u8 zero_check = 0x0;
1304 u8 one_check = 0xff;
1307 field.num_bits = sizeof(idcode_buffer) * 8;
1308 field.out_value = idcode_buffer;
1309 field.out_mask = NULL;
1310 field.in_value = idcode_buffer;
1311 field.in_check_value = NULL;
1312 field.in_check_mask = NULL;
1313 field.in_handler = NULL;
1314 field.in_handler_priv = NULL;
1316 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1318 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1321 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1322 jtag_execute_queue();
1324 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1326 zero_check |= idcode_buffer[i];
1327 one_check &= idcode_buffer[i];
1330 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1331 if ((zero_check == 0x00) || (one_check == 0xff))
1333 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1334 return ERROR_JTAG_INIT_FAILED;
1337 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1339 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1340 if ((idcode & 1) == 0)
1342 /* LSB must not be 0, this indicates a device in bypass */
1353 if (idcode == 0x000000FF)
1355 /* End of chain (invalid manufacturer ID) */
1361 device->idcode = idcode;
1362 device = device->next;
1366 manufacturer = (idcode & 0xffe) >> 1;
1367 part = (idcode & 0xffff000) >> 12;
1368 version = (idcode & 0xf0000000) >> 28;
1370 LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1371 idcode, manufacturer, part, version);
1373 /* Total hacky hack! PORGES */
1374 if (manufacturer == 0x1a2) {
1375 cwvx_device_num = device_count-1;
1376 cwvx_part_num = part;
1377 cwvx_version_num = version;
1384 /* see if number of discovered devices matches configuration */
1385 if (device_count != jtag_num_devices)
1387 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1388 device_count, jtag_num_devices);
1389 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1390 return ERROR_JTAG_INIT_FAILED;
1396 int jtag_validate_chain()
1398 jtag_device_t *device = jtag_devices;
1399 int total_ir_length = 0;
1406 total_ir_length += device->ir_length;
1407 device = device->next;
1410 total_ir_length += 2;
1411 ir_test = malloc(CEIL(total_ir_length, 8));
1412 buf_set_ones(ir_test, total_ir_length);
1415 field.num_bits = total_ir_length;
1416 field.out_value = ir_test;
1417 field.out_mask = NULL;
1418 field.in_value = ir_test;
1419 field.in_check_value = NULL;
1420 field.in_check_mask = NULL;
1421 field.in_handler = NULL;
1422 field.in_handler_priv = NULL;
1424 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1425 jtag_execute_queue();
1427 device = jtag_devices;
1430 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1432 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1433 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1436 return ERROR_JTAG_INIT_FAILED;
1438 chain_pos += device->ir_length;
1439 device = device->next;
1442 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1444 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1445 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1448 return ERROR_JTAG_INIT_FAILED;
1456 int jtag_register_commands(struct command_context_s *cmd_ctx)
1458 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1459 COMMAND_CONFIG, NULL);
1460 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1461 COMMAND_ANY, "set jtag speed (if supported) <reset speed> [<post reset speed, default value is reset speed>]");
1462 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1463 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1464 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1465 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1466 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1467 COMMAND_CONFIG, NULL);
1468 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1469 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
1470 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1471 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
1473 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1474 COMMAND_EXEC, "print current scan chain configuration");
1476 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1477 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1478 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1479 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1480 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1481 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1482 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1483 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1484 register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
1485 COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
1487 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1488 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1492 int jtag_interface_init(struct command_context_s *cmd_ctx)
1494 if (!jtag_interface)
1496 /* nothing was previously specified by "interface" command */
1497 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1498 return ERROR_JTAG_INVALID_INTERFACE;
1502 /*stay on "reset speed"*/
1503 if (jtag_interface->khz(speed1, &speed1) == ERROR_OK)
1504 jtag_speed = speed1;
1505 if (jtag_interface->khz(speed2, &speed2) == ERROR_OK)
1506 jtag_speed_post_reset = speed2;
1510 if (jtag_interface->init() != ERROR_OK)
1511 return ERROR_JTAG_INIT_FAILED;
1515 jtag = jtag_interface;
1519 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1521 int validate_tries = 0;
1522 jtag_device_t *device;
1526 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1527 return ERROR_JTAG_INIT_FAILED;
1529 device = jtag_devices;
1530 jtag_ir_scan_size = 0;
1531 jtag_num_devices = 0;
1532 while (device != NULL)
1534 jtag_ir_scan_size += device->ir_length;
1536 device = device->next;
1540 jtag_execute_queue();
1542 /* examine chain first, as this could discover the real chain layout */
1543 if (jtag_examine_chain() != ERROR_OK)
1545 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1548 while (jtag_validate_chain() != ERROR_OK)
1552 if (validate_tries > 5)
1554 LOG_ERROR("Could not validate JTAG chain, exit");
1555 return ERROR_JTAG_INVALID_INTERFACE;
1563 int jtag_init_reset(struct command_context_s *cmd_ctx)
1566 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / tms");
1568 /* Reset can happen after a power cycle.
1570 * Ideally we would only assert TRST or run tms before the target reset.
1572 * However w/srst_pulls_trst, trst is asserted together with the target
1573 * reset whether we want it or not.
1575 * NB! Some targets have JTAG circuitry disabled until a
1576 * trst & srst has been asserted.
1578 * NB! here we assume nsrst/ntrst delay are sufficient!
1580 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1583 jtag_add_reset(1, 0); /* TMS or TRST */
1584 if (jtag_reset_config & RESET_HAS_SRST)
1586 jtag_add_reset(1, 1);
1587 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
1588 jtag_add_reset(0, 1);
1590 jtag_add_reset(0, 0);
1591 if ((retval = jtag_execute_queue()) != ERROR_OK)
1594 /* Check that we can communication on the JTAG chain + eventually we want to
1595 * be able to perform enumeration only after OpenOCD has started
1596 * telnet and GDB server
1598 * That would allow users to more easily perform any magic they need to before
1601 return jtag_init_inner(cmd_ctx);
1604 int jtag_init(struct command_context_s *cmd_ctx)
1606 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
1610 return jtag_init_reset(cmd_ctx);
1614 static int default_khz(int khz, int *jtag_speed)
1616 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1620 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1624 /* check whether the interface is already configured */
1627 LOG_WARNING("Interface already configured, ignoring");
1631 /* interface name is a mandatory argument */
1632 if (argc < 1 || args[0][0] == '\0')
1634 return ERROR_COMMAND_SYNTAX_ERROR;
1637 for (i=0; jtag_interfaces[i]; i++)
1639 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1641 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1644 jtag_interface = jtag_interfaces[i];
1646 if (jtag_interface->khz == NULL)
1648 jtag_interface->khz = default_khz;
1654 /* no valid interface was found (i.e. the configuration option,
1655 * didn't match one of the compiled-in interfaces
1657 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
1658 LOG_ERROR("compiled-in jtag interfaces:");
1659 for (i = 0; jtag_interfaces[i]; i++)
1661 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
1664 return ERROR_JTAG_INVALID_INTERFACE;
1667 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1669 jtag_device_t **last_device_p = &jtag_devices;
1673 while ((*last_device_p)->next)
1674 last_device_p = &((*last_device_p)->next);
1675 last_device_p = &((*last_device_p)->next);
1681 *last_device_p = malloc(sizeof(jtag_device_t));
1682 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1684 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1685 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1686 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1687 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1689 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1690 (*last_device_p)->bypass = 1;
1691 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1693 (*last_device_p)->next = NULL;
1695 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1702 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1704 jtag_device_t *device = jtag_devices;
1705 int device_count = 0;
1709 u32 expected, expected_mask, cur_instr;
1710 expected = buf_get_u32(device->expected, 0, device->ir_length);
1711 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1712 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1713 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);
1714 device = device->next;
1721 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1724 return ERROR_COMMAND_SYNTAX_ERROR;
1728 if (strcmp(args[0], "none") == 0)
1729 jtag_reset_config = RESET_NONE;
1730 else if (strcmp(args[0], "trst_only") == 0)
1731 jtag_reset_config = RESET_HAS_TRST;
1732 else if (strcmp(args[0], "srst_only") == 0)
1733 jtag_reset_config = RESET_HAS_SRST;
1734 else if (strcmp(args[0], "trst_and_srst") == 0)
1735 jtag_reset_config = RESET_TRST_AND_SRST;
1738 LOG_ERROR("invalid reset_config argument, defaulting to none");
1739 jtag_reset_config = RESET_NONE;
1740 return ERROR_INVALID_ARGUMENTS;
1746 if (strcmp(args[1], "separate") == 0)
1748 /* seperate reset lines - default */
1751 if (strcmp(args[1], "srst_pulls_trst") == 0)
1752 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1753 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1754 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1755 else if (strcmp(args[1], "combined") == 0)
1756 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1759 LOG_ERROR("invalid reset_config argument, defaulting to none");
1760 jtag_reset_config = RESET_NONE;
1761 return ERROR_INVALID_ARGUMENTS;
1768 if (strcmp(args[2], "trst_open_drain") == 0)
1769 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1770 else if (strcmp(args[2], "trst_push_pull") == 0)
1771 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1774 LOG_ERROR("invalid reset_config argument, defaulting to none");
1775 jtag_reset_config = RESET_NONE;
1776 return ERROR_INVALID_ARGUMENTS;
1782 if (strcmp(args[3], "srst_push_pull") == 0)
1783 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1784 else if (strcmp(args[3], "srst_open_drain") == 0)
1785 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1788 LOG_ERROR("invalid reset_config argument, defaulting to none");
1789 jtag_reset_config = RESET_NONE;
1790 return ERROR_INVALID_ARGUMENTS;
1797 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1801 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1806 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1812 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1816 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1821 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1827 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1833 if ((argc<1) || (argc>2))
1834 return ERROR_COMMAND_SYNTAX_ERROR;
1836 LOG_DEBUG("handle jtag speed");
1839 cur_speed = jtag_speed = jtag_speed_post_reset = strtoul(args[0], NULL, 0);
1841 cur_speed = jtag_speed_post_reset = strtoul(args[1], NULL, 0);
1843 /* this command can be called during CONFIG,
1844 * in which case jtag isn't initialized */
1846 jtag->speed(cur_speed);
1848 command_print(cmd_ctx, "jtag_speed: %d, %d", jtag_speed, jtag_speed_post_reset);
1853 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1855 LOG_DEBUG("handle jtag khz");
1857 if ((argc<1) || (argc>2))
1858 return ERROR_COMMAND_SYNTAX_ERROR;
1861 speed1 = speed2 = strtoul(args[0], NULL, 0);
1863 speed2 = strtoul(args[1], NULL, 0);
1868 LOG_DEBUG("have interface set up");
1869 int speed_div1, speed_div2;
1870 if (jtag->khz(speed1, &speed_div1)!=ERROR_OK)
1872 if (jtag->khz(speed2, &speed_div2)!=ERROR_OK)
1876 cur_speed = jtag_speed = jtag_speed_post_reset = speed_div1;
1878 cur_speed = jtag_speed_post_reset = speed_div2;
1880 jtag->speed(cur_speed);
1890 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1892 enum tap_state state;
1896 return ERROR_COMMAND_SYNTAX_ERROR;
1900 for (state = 0; state < 16; state++)
1902 if (strcmp(args[0], tap_state_strings[state]) == 0)
1904 jtag_add_end_state(state);
1905 jtag_execute_queue();
1909 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
1914 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1921 return ERROR_COMMAND_SYNTAX_ERROR;
1924 if (args[0][0] == '1')
1926 else if (args[0][0] == '0')
1930 return ERROR_COMMAND_SYNTAX_ERROR;
1933 if (args[1][0] == '1')
1935 else if (args[1][0] == '0')
1939 return ERROR_COMMAND_SYNTAX_ERROR;
1942 if (!jtag && jtag_interface_init(cmd_ctx) != ERROR_OK)
1943 return ERROR_JTAG_INIT_FAILED;
1945 jtag_add_reset(trst, srst);
1946 jtag_execute_queue();
1951 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1955 return ERROR_COMMAND_SYNTAX_ERROR;
1958 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
1959 jtag_execute_queue();
1966 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1969 scan_field_t *fields;
1971 if ((argc < 2) || (argc % 2))
1973 return ERROR_COMMAND_SYNTAX_ERROR;
1976 fields = malloc(sizeof(scan_field_t) * argc / 2);
1978 for (i = 0; i < argc / 2; i++)
1980 int device = strtoul(args[i*2], NULL, 0);
1981 int field_size = jtag_get_device(device)->ir_length;
1982 fields[i].device = device;
1983 fields[i].out_value = malloc(CEIL(field_size, 8));
1984 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1985 fields[i].out_mask = NULL;
1986 fields[i].in_value = NULL;
1987 fields[i].in_check_mask = NULL;
1988 fields[i].in_handler = NULL;
1989 fields[i].in_handler_priv = NULL;
1992 jtag_add_ir_scan(argc / 2, fields, -1);
1993 jtag_execute_queue();
1995 for (i = 0; i < argc / 2; i++)
1996 free(fields[i].out_value);
2003 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2005 scan_field_t *fields;
2007 int field_count = 0;
2011 if ((argc < 2) || (argc % 2))
2013 return ERROR_COMMAND_SYNTAX_ERROR;
2016 for (i = 0; i < argc; i+=2)
2018 var = get_var_by_namenum(args[i+1]);
2021 num_fields += var->num_fields;
2025 command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
2030 fields = malloc(sizeof(scan_field_t) * num_fields);
2032 for (i = 0; i < argc; i+=2)
2034 var = get_var_by_namenum(args[i+1]);
2036 for (j = 0; j < var->num_fields; j++)
2038 fields[field_count].device = strtol(args[i], NULL, 0);
2039 fields[field_count].num_bits = var->fields[j].num_bits;
2040 fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
2041 buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
2042 fields[field_count].out_mask = NULL;
2043 fields[field_count].in_value = fields[field_count].out_value;
2044 fields[field_count].in_check_mask = NULL;
2045 fields[field_count].in_check_value = NULL;
2046 fields[field_count].in_handler = field_le_to_host;
2047 fields[field_count++].in_handler_priv = &(var->fields[j]);
2051 jtag_add_dr_scan(num_fields, fields, -1);
2052 jtag_execute_queue();
2054 for (i = 0; i < argc / 2; i++)
2055 free(fields[i].out_value);
2062 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2066 if (strcmp(args[0], "enable") == 0)
2068 jtag_verify_capture_ir = 1;
2070 else if (strcmp(args[0], "disable") == 0)
2072 jtag_verify_capture_ir = 0;
2075 return ERROR_COMMAND_SYNTAX_ERROR;
2077 } else if (argc != 0)
2079 return ERROR_COMMAND_SYNTAX_ERROR;
2082 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");