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 static jtag_tap_t *jtag_all_taps = NULL;
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 speed_khz = 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 zy1000_interface;
156 #if BUILD_PARPORT == 1
157 extern jtag_interface_t parport_interface;
161 extern jtag_interface_t dummy_interface;
164 #if BUILD_FT2232_FTD2XX == 1
165 extern jtag_interface_t ft2232_interface;
168 #if BUILD_FT2232_LIBFTDI == 1
169 extern jtag_interface_t ft2232_interface;
172 #if BUILD_AMTJTAGACCEL == 1
173 extern jtag_interface_t amt_jtagaccel_interface;
176 #if BUILD_EP93XX == 1
177 extern jtag_interface_t ep93xx_interface;
180 #if BUILD_AT91RM9200 == 1
181 extern jtag_interface_t at91rm9200_interface;
184 #if BUILD_GW16012 == 1
185 extern jtag_interface_t gw16012_interface;
188 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
189 extern jtag_interface_t presto_interface;
192 #if BUILD_USBPROG == 1
193 extern jtag_interface_t usbprog_interface;
197 extern jtag_interface_t jlink_interface;
200 jtag_interface_t *jtag_interfaces[] = {
201 #if BUILD_ECOSBOARD == 1
204 #if BUILD_PARPORT == 1
210 #if BUILD_FT2232_FTD2XX == 1
213 #if BUILD_FT2232_LIBFTDI == 1
216 #if BUILD_AMTJTAGACCEL == 1
217 &amt_jtagaccel_interface,
219 #if BUILD_EP93XX == 1
222 #if BUILD_AT91RM9200 == 1
223 &at91rm9200_interface,
225 #if BUILD_GW16012 == 1
228 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
231 #if BUILD_USBPROG == 1
240 jtag_interface_t *jtag = NULL;
243 jtag_interface_t *jtag_interface = NULL;
248 /* forward declarations */
249 void jtag_add_pathmove(int num_states, enum tap_state *path);
250 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
251 void jtag_add_end_state(enum tap_state endstate);
252 void jtag_add_sleep(u32 us);
253 int jtag_execute_queue(void);
257 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
258 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
259 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
260 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
261 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
262 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
263 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
265 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
267 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
268 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
269 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
270 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
271 int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
273 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
276 jtag_tap_t *jtag_AllTaps(void)
278 return jtag_all_taps;
282 jtag_NumTotalTaps(void)
297 jtag_NumEnabledTaps(void)
314 jtag_tap_t *jtag_TapByString( const char *s )
322 if( 0 == strcmp( t->dotted_name, s ) ){
328 // backup plan is by number
330 /* ok - is "s" a number? */
332 n = strtol( s, &cp, 0 );
333 if( (s != cp) && (*cp == 0) ){
335 t = jtag_TapByAbsPosition(n);
342 jtag_TapByJimObj( Jim_Interp *interp, Jim_Obj *o )
347 cp = Jim_GetString( o, NULL );
352 t = jtag_TapByString( cp );
355 Jim_SetResult_sprintf(interp,"Tap: %s is unknown", cp );
360 /* returns a pointer to the n-th device in the scan chain */
362 jtag_TapByAbsPosition( int n )
370 while( t && (n > 0)) {
378 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
380 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
382 if (callback == NULL)
384 return ERROR_INVALID_ARGUMENTS;
389 while ((*callbacks_p)->next)
390 callbacks_p = &((*callbacks_p)->next);
391 callbacks_p = &((*callbacks_p)->next);
394 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
395 (*callbacks_p)->callback = callback;
396 (*callbacks_p)->priv = priv;
397 (*callbacks_p)->next = NULL;
402 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
404 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
406 if (callback == NULL)
408 return ERROR_INVALID_ARGUMENTS;
413 jtag_event_callback_t **next = &((*callbacks_p)->next);
414 if ((*callbacks_p)->callback == callback)
417 *callbacks_p = *next;
425 int jtag_call_event_callbacks(enum jtag_event event)
427 jtag_event_callback_t *callback = jtag_event_callbacks;
429 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
433 callback->callback(event, callback->priv);
434 callback = callback->next;
440 /* returns a pointer to the pointer of the last command in queue
441 * this may be a pointer to the root pointer (jtag_command_queue)
442 * or to the next member of the last but one command
444 jtag_command_t** jtag_get_last_command_p(void)
446 /* jtag_command_t *cmd = jtag_command_queue;
452 return &jtag_command_queue;
456 return last_comand_pointer;
460 void* cmd_queue_alloc(size_t size)
462 cmd_queue_page_t **p_page = &cmd_queue_pages;
468 * We align/round the *SIZE* per below
469 * so that all pointers returned by
470 * this function are reasonably well
473 * If we did not, then an "odd-length" request would cause the
474 * *next* allocation to be at an *odd* address, and because
475 * this function has the same type of api as malloc() - we
476 * must also return pointers that have the same type of
479 * What I do not/have is a reasonable portable means
482 * The solution here, is based on these suggestions.
483 * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
486 union worse_case_align {
492 #define ALIGN_SIZE (sizeof(union worse_case_align))
494 // The alignment process.
495 size = (size + ALIGN_SIZE -1) & (~(ALIGN_SIZE-1));
501 while ((*p_page)->next)
502 p_page = &((*p_page)->next);
503 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
504 p_page = &((*p_page)->next);
509 *p_page = malloc(sizeof(cmd_queue_page_t));
511 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
512 (*p_page)->next = NULL;
515 offset = (*p_page)->used;
516 (*p_page)->used += size;
518 t=(u8 *)((*p_page)->address);
522 void cmd_queue_free(void)
524 cmd_queue_page_t *page = cmd_queue_pages;
528 cmd_queue_page_t *last = page;
534 cmd_queue_pages = NULL;
537 static void jtag_prelude1(void)
541 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
542 jtag_error=ERROR_JTAG_TRST_ASSERTED;
546 if (cmd_queue_end_state == TAP_TLR)
547 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
550 static void jtag_prelude(enum tap_state state)
555 jtag_add_end_state(state);
557 cmd_queue_cur_state = cmd_queue_end_state;
560 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
566 retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
567 if (retval!=ERROR_OK)
571 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
573 jtag_command_t **last_cmd;
581 last_cmd = jtag_get_last_command_p();
583 /* allocate memory for a new list member */
584 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
585 (*last_cmd)->next = NULL;
586 last_comand_pointer = &((*last_cmd)->next);
587 (*last_cmd)->type = JTAG_SCAN;
589 /* allocate memory for ir scan command */
590 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
591 (*last_cmd)->cmd.scan->ir_scan = 1;
592 x = jtag_NumEnabledTaps();
593 (*last_cmd)->cmd.scan->num_fields = x; /* one field per device */
594 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(x * sizeof(scan_field_t));
595 (*last_cmd)->cmd.scan->end_state = state;
602 // do this here so it is not forgotten
603 tap = jtag_NextEnabledTap(tap);
608 scan_size = tap->ir_length;
609 (*last_cmd)->cmd.scan->fields[nth_tap].tap = tap;
610 (*last_cmd)->cmd.scan->fields[nth_tap].num_bits = scan_size;
611 (*last_cmd)->cmd.scan->fields[nth_tap].in_value = NULL;
612 (*last_cmd)->cmd.scan->fields[nth_tap].in_handler = NULL; /* disable verification by default */
614 /* search the list */
615 for (j = 0; j < num_fields; j++)
617 if (tap == fields[j].tap)
620 (*last_cmd)->cmd.scan->fields[nth_tap].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
621 (*last_cmd)->cmd.scan->fields[nth_tap].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
623 if (jtag_verify_capture_ir)
625 if (fields[j].in_handler==NULL)
627 jtag_set_check_value((*last_cmd)->cmd.scan->fields+nth_tap, tap->expected, tap->expected_mask, NULL);
630 (*last_cmd)->cmd.scan->fields[nth_tap].in_handler = fields[j].in_handler;
631 (*last_cmd)->cmd.scan->fields[nth_tap].in_handler_priv = fields[j].in_handler_priv;
632 (*last_cmd)->cmd.scan->fields[nth_tap].in_check_value = tap->expected;
633 (*last_cmd)->cmd.scan->fields[nth_tap].in_check_mask = tap->expected_mask;
644 /* if a tap isn't listed, set it to BYPASS */
645 (*last_cmd)->cmd.scan->fields[nth_tap].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
646 (*last_cmd)->cmd.scan->fields[nth_tap].out_mask = NULL;
651 /* update device information */
652 buf_cpy((*last_cmd)->cmd.scan->fields[nth_tap].out_value, tap->cur_instr, scan_size);
658 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
664 retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
665 if (retval!=ERROR_OK)
669 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
672 jtag_command_t **last_cmd;
674 last_cmd = jtag_get_last_command_p();
676 /* allocate memory for a new list member */
677 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
678 (*last_cmd)->next = NULL;
679 last_comand_pointer = &((*last_cmd)->next);
680 (*last_cmd)->type = JTAG_SCAN;
682 /* allocate memory for ir scan command */
683 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
684 (*last_cmd)->cmd.scan->ir_scan = 1;
685 (*last_cmd)->cmd.scan->num_fields = num_fields;
686 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
687 (*last_cmd)->cmd.scan->end_state = state;
689 for( i = 0 ; i < num_fields ; i++ ){
690 int num_bits = fields[i].num_bits;
691 int num_bytes = CEIL(fields[i].num_bits, 8);
692 (*last_cmd)->cmd.scan->fields[i].tap = fields[i].tap;
693 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
694 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
695 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
696 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
697 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
698 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
699 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
700 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
705 void jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
711 retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
712 if (retval!=ERROR_OK)
716 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
720 int bypass_devices = 0;
724 jtag_command_t **last_cmd = jtag_get_last_command_p();
727 /* count devices in bypass */
731 tap = jtag_NextEnabledTap(tap);
740 /* allocate memory for a new list member */
741 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
742 last_comand_pointer = &((*last_cmd)->next);
743 (*last_cmd)->next = NULL;
744 (*last_cmd)->type = JTAG_SCAN;
746 /* allocate memory for dr scan command */
747 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
748 (*last_cmd)->cmd.scan->ir_scan = 0;
749 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
750 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
751 (*last_cmd)->cmd.scan->end_state = state;
757 tap = jtag_NextEnabledTap(tap);
762 (*last_cmd)->cmd.scan->fields[field_count].tap = tap;
764 for (j = 0; j < num_fields; j++)
766 if (tap == fields[j].tap)
769 scan_size = fields[j].num_bits;
770 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
771 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
772 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
773 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
774 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
775 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
776 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
777 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
782 #ifdef _DEBUG_JTAG_IO_
783 /* if a device isn't listed, the BYPASS register should be selected */
786 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
790 /* program the scan field to 1 bit length, and ignore it's value */
791 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
792 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
793 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
794 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
795 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
796 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
797 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
798 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
802 #ifdef _DEBUG_JTAG_IO_
803 /* if a device is listed, the BYPASS register must not be selected */
806 LOG_ERROR("BUG: scan data for a device in BYPASS");
815 void MINIDRIVER(interface_jtag_add_dr_out)(jtag_tap_t *target_tap,
819 enum tap_state end_state)
824 int bypass_devices = 0;
826 jtag_command_t **last_cmd = jtag_get_last_command_p();
829 /* count devices in bypass */
833 tap = jtag_NextEnabledTap(tap);
842 /* allocate memory for a new list member */
843 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
844 last_comand_pointer = &((*last_cmd)->next);
845 (*last_cmd)->next = NULL;
846 (*last_cmd)->type = JTAG_SCAN;
848 /* allocate memory for dr scan command */
849 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
850 (*last_cmd)->cmd.scan->ir_scan = 0;
851 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
852 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
853 (*last_cmd)->cmd.scan->end_state = end_state;
858 tap = jtag_NextEnabledTap(tap);
863 (*last_cmd)->cmd.scan->fields[field_count].tap = tap;
865 if (tap == target_tap)
868 #ifdef _DEBUG_JTAG_IO_
869 /* if a device is listed, the BYPASS register must not be selected */
872 LOG_ERROR("BUG: scan data for a device in BYPASS");
876 for (j = 0; j < num_fields; j++)
879 scan_size = num_bits[j];
880 buf_set_u32(out_value, 0, scan_size, value[j]);
881 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
882 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
883 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
884 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
885 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
886 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
887 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
888 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
892 #ifdef _DEBUG_JTAG_IO_
893 /* if a device isn't listed, the BYPASS register should be selected */
896 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
900 /* program the scan field to 1 bit length, and ignore it's value */
901 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
902 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
903 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
904 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
905 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
906 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
907 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
908 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
913 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
919 retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
920 if (retval!=ERROR_OK)
924 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
927 jtag_command_t **last_cmd = jtag_get_last_command_p();
929 /* allocate memory for a new list member */
930 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
931 last_comand_pointer = &((*last_cmd)->next);
932 (*last_cmd)->next = NULL;
933 (*last_cmd)->type = JTAG_SCAN;
935 /* allocate memory for scan command */
936 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
937 (*last_cmd)->cmd.scan->ir_scan = 0;
938 (*last_cmd)->cmd.scan->num_fields = num_fields;
939 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
940 (*last_cmd)->cmd.scan->end_state = state;
942 for (i = 0; i < num_fields; i++)
944 int num_bits = fields[i].num_bits;
945 int num_bytes = CEIL(fields[i].num_bits, 8);
946 (*last_cmd)->cmd.scan->fields[i].tap = fields[i].tap;
947 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
948 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
949 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
950 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
951 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
952 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
953 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
954 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
960 void jtag_add_tlr(void)
962 jtag_prelude(TAP_TLR);
965 retval=interface_jtag_add_tlr();
966 if (retval!=ERROR_OK)
970 int MINIDRIVER(interface_jtag_add_tlr)()
972 enum tap_state state = TAP_TLR;
973 jtag_command_t **last_cmd = jtag_get_last_command_p();
975 /* allocate memory for a new list member */
976 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
977 last_comand_pointer = &((*last_cmd)->next);
978 (*last_cmd)->next = NULL;
979 (*last_cmd)->type = JTAG_STATEMOVE;
981 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
982 (*last_cmd)->cmd.statemove->end_state = state;
988 void jtag_add_pathmove(int num_states, enum tap_state *path)
990 enum tap_state cur_state=cmd_queue_cur_state;
994 /* the last state has to be a stable state */
995 if (tap_move_map[path[num_states - 1]] == -1)
997 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
1001 for (i=0; i<num_states; i++)
1003 if (path[i] == TAP_TLR)
1005 LOG_ERROR("BUG: TAP_TLR is not a valid state for pathmove sequences");
1008 if ((tap_transitions[cur_state].low != path[i])&&
1009 (tap_transitions[cur_state].high != path[i]))
1011 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
1014 cur_state = path[i];
1020 retval=interface_jtag_add_pathmove(num_states, path);
1021 cmd_queue_cur_state = path[num_states - 1];
1022 if (retval!=ERROR_OK)
1026 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
1028 jtag_command_t **last_cmd = jtag_get_last_command_p();
1031 /* allocate memory for a new list member */
1032 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1033 last_comand_pointer = &((*last_cmd)->next);
1034 (*last_cmd)->next = NULL;
1035 (*last_cmd)->type = JTAG_PATHMOVE;
1037 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
1038 (*last_cmd)->cmd.pathmove->num_states = num_states;
1039 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
1041 for (i = 0; i < num_states; i++)
1042 (*last_cmd)->cmd.pathmove->path[i] = path[i];
1047 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
1049 jtag_command_t **last_cmd = jtag_get_last_command_p();
1051 /* allocate memory for a new list member */
1052 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1053 (*last_cmd)->next = NULL;
1054 last_comand_pointer = &((*last_cmd)->next);
1055 (*last_cmd)->type = JTAG_RUNTEST;
1057 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
1058 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
1059 (*last_cmd)->cmd.runtest->end_state = state;
1064 void jtag_add_runtest(int num_cycles, enum tap_state state)
1068 jtag_prelude(state);
1070 /* executed by sw or hw fifo */
1071 retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
1072 if (retval!=ERROR_OK)
1076 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
1078 int trst_with_tlr = 0;
1081 /* FIX!!! there are *many* different cases here. A better
1082 * approach is needed for legal combinations of transitions...
1084 if ((jtag_reset_config & RESET_HAS_SRST)&&
1085 (jtag_reset_config & RESET_HAS_TRST)&&
1086 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
1088 if (((req_tlr_or_trst&&!jtag_trst)||
1089 (!req_tlr_or_trst&&jtag_trst))&&
1090 ((req_srst&&!jtag_srst)||
1091 (!req_srst&&jtag_srst)))
1093 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
1094 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
1098 /* Make sure that jtag_reset_config allows the requested reset */
1099 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
1100 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
1102 LOG_ERROR("BUG: requested reset would assert trst");
1103 jtag_error=ERROR_FAIL;
1107 /* if TRST pulls SRST, we reset with TAP T-L-R */
1108 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
1113 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
1115 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
1116 jtag_error=ERROR_FAIL;
1120 if (req_tlr_or_trst)
1122 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
1134 jtag_srst = req_srst;
1136 retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
1137 if (retval!=ERROR_OK)
1145 LOG_DEBUG("SRST line asserted");
1149 LOG_DEBUG("SRST line released");
1150 if (jtag_nsrst_delay)
1151 jtag_add_sleep(jtag_nsrst_delay * 1000);
1156 LOG_DEBUG("JTAG reset with TLR instead of TRST");
1157 jtag_add_end_state(TAP_TLR);
1159 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1165 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1166 * and inform possible listeners about this
1168 LOG_DEBUG("TRST line asserted");
1169 cmd_queue_cur_state = TAP_TLR;
1170 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1174 if (jtag_ntrst_delay)
1175 jtag_add_sleep(jtag_ntrst_delay * 1000);
1179 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1181 jtag_command_t **last_cmd = jtag_get_last_command_p();
1183 /* allocate memory for a new list member */
1184 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1185 (*last_cmd)->next = NULL;
1186 last_comand_pointer = &((*last_cmd)->next);
1187 (*last_cmd)->type = JTAG_RESET;
1189 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1190 (*last_cmd)->cmd.reset->trst = req_trst;
1191 (*last_cmd)->cmd.reset->srst = req_srst;
1196 void jtag_add_end_state(enum tap_state state)
1198 cmd_queue_end_state = state;
1199 if ((cmd_queue_end_state == TAP_SD)||(cmd_queue_end_state == TAP_SI))
1201 LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
1205 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1207 jtag_command_t **last_cmd = jtag_get_last_command_p();
1209 /* allocate memory for a new list member */
1210 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1211 (*last_cmd)->next = NULL;
1212 last_comand_pointer = &((*last_cmd)->next);
1213 (*last_cmd)->type = JTAG_SLEEP;
1215 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1216 (*last_cmd)->cmd.sleep->us = us;
1221 void jtag_add_sleep(u32 us)
1223 keep_alive(); /* we might be running on a very slow JTAG clk */
1224 int retval=interface_jtag_add_sleep(us);
1225 if (retval!=ERROR_OK)
1230 int jtag_scan_size(scan_command_t *cmd)
1235 /* count bits in scan command */
1236 for (i = 0; i < cmd->num_fields; i++)
1238 bit_count += cmd->fields[i].num_bits;
1244 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1249 bit_count = jtag_scan_size(cmd);
1250 *buffer = malloc(CEIL(bit_count, 8));
1254 for (i = 0; i < cmd->num_fields; i++)
1256 if (cmd->fields[i].out_value)
1258 #ifdef _DEBUG_JTAG_IO_
1259 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1261 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1262 #ifdef _DEBUG_JTAG_IO_
1263 LOG_DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1268 bit_count += cmd->fields[i].num_bits;
1274 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1280 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1283 for (i = 0; i < cmd->num_fields; i++)
1285 /* if neither in_value nor in_handler
1286 * are specified we don't have to examine this field
1288 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1290 int num_bits = cmd->fields[i].num_bits;
1291 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1293 #ifdef _DEBUG_JTAG_IO_
1296 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1297 LOG_DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1301 if (cmd->fields[i].in_value)
1303 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1305 if (cmd->fields[i].in_handler)
1307 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1309 LOG_WARNING("in_handler reported a failed check");
1310 retval = ERROR_JTAG_QUEUE_FAILED;
1315 /* no in_value specified, but a handler takes care of the scanned data */
1316 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1318 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1320 /* We're going to call the error:handler later, but if the in_handler
1321 * reported an error we report this failure upstream
1323 LOG_WARNING("in_handler reported a failed check");
1324 retval = ERROR_JTAG_QUEUE_FAILED;
1330 bit_count += cmd->fields[i].num_bits;
1336 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1338 int retval = ERROR_OK;
1339 int num_bits = field->num_bits;
1341 int compare_failed = 0;
1343 if (field->in_check_mask)
1344 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1346 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1348 if (compare_failed){
1349 /* An error handler could have caught the failing check
1350 * only report a problem when there wasn't a handler, or if the handler
1351 * acknowledged the error
1353 LOG_WARNING("TAP %s:",
1354 (field->tap == NULL) ? "(unknown)" : field->tap->dotted_name );
1357 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1358 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1360 if (field->in_check_mask)
1362 char *in_check_mask_char;
1363 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1364 LOG_WARNING("value captured during scan didn't pass the requested check:");
1365 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
1366 captured_char, in_check_value_char, in_check_mask_char);
1367 free(in_check_mask_char);
1371 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);
1374 free(captured_char);
1375 free(in_check_value_char);
1377 retval = ERROR_JTAG_QUEUE_FAILED;
1385 set up checking of this field using the in_handler. The values passed in must be valid until
1386 after jtag_execute() has completed.
1388 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1391 field->in_handler = jtag_check_value;
1393 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1394 field->in_handler_priv = NULL;
1395 field->in_check_value = value;
1396 field->in_check_mask = mask;
1399 enum scan_type jtag_scan_type(scan_command_t *cmd)
1404 for (i = 0; i < cmd->num_fields; i++)
1406 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1408 if (cmd->fields[i].out_value)
1415 int MINIDRIVER(interface_jtag_execute_queue)(void)
1421 LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
1425 retval = jtag->execute_queue();
1429 jtag_command_queue = NULL;
1430 last_comand_pointer = &jtag_command_queue;
1435 int jtag_execute_queue(void)
1437 int retval=interface_jtag_execute_queue();
1438 if (retval==ERROR_OK)
1442 jtag_error=ERROR_OK;
1446 int jtag_reset_callback(enum jtag_event event, void *priv)
1448 jtag_tap_t *tap = priv;
1452 if (event == JTAG_TRST_ASSERTED)
1454 buf_set_ones(tap->cur_instr, tap->ir_length);
1461 void jtag_sleep(u32 us)
1463 alive_sleep(us/1000);
1466 /* Try to examine chain layout according to IEEE 1149.1 §12
1468 int jtag_examine_chain(void)
1472 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1475 int device_count = 0;
1476 u8 zero_check = 0x0;
1477 u8 one_check = 0xff;
1480 field.num_bits = sizeof(idcode_buffer) * 8;
1481 field.out_value = idcode_buffer;
1482 field.out_mask = NULL;
1483 field.in_value = idcode_buffer;
1484 field.in_check_value = NULL;
1485 field.in_check_mask = NULL;
1486 field.in_handler = NULL;
1487 field.in_handler_priv = NULL;
1489 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1491 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1494 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1495 jtag_execute_queue();
1497 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1499 zero_check |= idcode_buffer[i];
1500 one_check &= idcode_buffer[i];
1503 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1504 if ((zero_check == 0x00) || (one_check == 0xff))
1506 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1507 return ERROR_JTAG_INIT_FAILED;
1510 // point at the 1st tap
1511 tap = jtag_NextEnabledTap(NULL);
1513 LOG_ERROR("JTAG: No taps enabled?");
1514 return ERROR_JTAG_INIT_FAILED;
1517 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1519 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1520 if ((idcode & 1) == 0)
1522 /* LSB must not be 0, this indicates a device in bypass */
1523 LOG_WARNING("Tap/Device does not have IDCODE");
1534 if (idcode == 0x000000FF)
1537 /* End of chain (invalid manufacturer ID)
1539 * The JTAG examine is the very first thing that happens
1541 * A single JTAG device requires only 64 bits to be read back correctly.
1543 * The code below adds a check that the rest of the data scanned (640 bits)
1544 * are all as expected. This helps diagnose/catch problems with the JTAG chain
1546 * earlier and gives more helpful/explicit error messages.
1548 for (bit_count += 32; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;bit_count += 32)
1550 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1551 if (unexpected||(idcode != 0x000000FF))
1553 LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count, idcode);
1561 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
1562 manufacturer = EXTRACT_MFG(idcode);
1563 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
1564 part = EXTRACT_PART(idcode);
1565 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
1566 version = EXTRACT_VER(idcode);
1568 LOG_INFO("JTAG tap: %s tap/device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1569 ((tap != NULL) ? (tap->dotted_name) : "(not-named)"),
1570 idcode, manufacturer, part, version);
1576 tap->idcode = idcode;
1578 if (tap->expected_ids_cnt > 0) {
1579 /* Loop over the expected identification codes and test for a match */
1581 for (ii = 0; ii < tap->expected_ids_cnt; ii++) {
1582 if( tap->idcode == tap->expected_ids[ii] ){
1587 /* If none of the expected ids matched, log an error */
1588 if (ii == tap->expected_ids_cnt) {
1589 LOG_ERROR("JTAG tap: %s got: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1592 EXTRACT_MFG( tap->idcode ),
1593 EXTRACT_PART( tap->idcode ),
1594 EXTRACT_VER( tap->idcode ) );
1595 for (ii = 0; ii < tap->expected_ids_cnt; ii++) {
1596 LOG_ERROR("JTAG tap: %s expected %hhu of %hhu: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1599 tap->expected_ids_cnt,
1600 tap->expected_ids[ii],
1601 EXTRACT_MFG( tap->expected_ids[ii] ),
1602 EXTRACT_PART( tap->expected_ids[ii] ),
1603 EXTRACT_VER( tap->expected_ids[ii] ) );
1606 return ERROR_JTAG_INIT_FAILED;
1608 LOG_INFO("JTAG Tap/device matched");
1612 LOG_INFO("JTAG TAP ID: 0x%08x - Unknown - please report (A) chipname and (B) idcode to the openocd project",
1616 tap = jtag_NextEnabledTap(tap);
1621 /* see if number of discovered devices matches configuration */
1622 if (device_count != jtag_NumEnabledTaps())
1624 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match (enabled) configuration (%i), total taps: %d",
1625 device_count, jtag_NumEnabledTaps(), jtag_NumTotalTaps());
1626 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1627 return ERROR_JTAG_INIT_FAILED;
1633 int jtag_validate_chain(void)
1636 int total_ir_length = 0;
1642 total_ir_length = 0;
1644 tap = jtag_NextEnabledTap(tap);
1648 total_ir_length += tap->ir_length;
1651 total_ir_length += 2;
1652 ir_test = malloc(CEIL(total_ir_length, 8));
1653 buf_set_ones(ir_test, total_ir_length);
1656 field.num_bits = total_ir_length;
1657 field.out_value = ir_test;
1658 field.out_mask = NULL;
1659 field.in_value = ir_test;
1660 field.in_check_value = NULL;
1661 field.in_check_mask = NULL;
1662 field.in_handler = NULL;
1663 field.in_handler_priv = NULL;
1665 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1666 jtag_execute_queue();
1671 tap = jtag_NextEnabledTap(tap);
1677 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1679 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1680 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1683 return ERROR_JTAG_INIT_FAILED;
1685 chain_pos += tap->ir_length;
1688 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1690 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1691 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1694 return ERROR_JTAG_INIT_FAILED;
1704 jim_newtap_cmd( Jim_GetOptInfo *goi )
1714 const Jim_Nvp opts[] = {
1715 #define NTAP_OPT_IRLEN 0
1716 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
1717 #define NTAP_OPT_IRMASK 1
1718 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
1719 #define NTAP_OPT_IRCAPTURE 2
1720 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
1721 #define NTAP_OPT_ENABLED 3
1722 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
1723 #define NTAP_OPT_DISABLED 4
1724 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
1725 #define NTAP_OPT_EXPECTED_ID 5
1726 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
1727 { .name = NULL , .value = -1 },
1731 pTap = malloc( sizeof(jtag_tap_t) );
1732 memset( pTap, 0, sizeof(*pTap) );
1734 Jim_SetResult_sprintf( goi->interp, "no memory");
1738 // we expect CHIP + TAP + OPTIONS
1740 if( goi->argc < 3 ){
1741 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
1744 Jim_GetOpt_String( goi, &cp, NULL );
1745 pTap->chip = strdup(cp);
1747 Jim_GetOpt_String( goi, &cp, NULL );
1748 pTap->tapname = strdup(cp);
1750 // name + dot + name + null
1751 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
1753 sprintf( cp, "%s.%s", pTap->chip, pTap->tapname );
1754 pTap->dotted_name = cp;
1756 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
1757 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
1760 // default is enabled
1763 // deal with options
1764 #define NTREQ_IRLEN 1
1765 #define NTREQ_IRCAPTURE 2
1766 #define NTREQ_IRMASK 4
1768 // clear them as we find them
1769 reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
1772 e = Jim_GetOpt_Nvp( goi, opts, &n );
1774 Jim_GetOpt_NvpUnknown( goi, opts, 0 );
1777 LOG_DEBUG("Processing option: %s", n->name );
1779 case NTAP_OPT_ENABLED:
1782 case NTAP_OPT_DISABLED:
1785 case NTAP_OPT_EXPECTED_ID:
1787 u32 *new_expected_ids;
1789 e = Jim_GetOpt_Wide( goi, &w );
1791 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
1795 new_expected_ids = malloc(sizeof(u32) * (pTap->expected_ids_cnt + 1));
1796 if (new_expected_ids == NULL) {
1797 Jim_SetResult_sprintf( goi->interp, "no memory");
1801 memcpy(new_expected_ids, pTap->expected_ids, sizeof(u32) * pTap->expected_ids_cnt);
1803 new_expected_ids[pTap->expected_ids_cnt] = w;
1805 free(pTap->expected_ids);
1806 pTap->expected_ids = new_expected_ids;
1807 pTap->expected_ids_cnt++;
1810 case NTAP_OPT_IRLEN:
1811 case NTAP_OPT_IRMASK:
1812 case NTAP_OPT_IRCAPTURE:
1813 e = Jim_GetOpt_Wide( goi, &w );
1815 Jim_SetResult_sprintf( goi->interp, "option: %s bad parameter", n->name );
1818 if( (w < 0) || (w > 0xffff) ){
1820 Jim_SetResult_sprintf( goi->interp, "option: %s - wacky value: %d (0x%x)",
1821 n->name, (int)(w), (int)(w));
1825 case NTAP_OPT_IRLEN:
1826 pTap->ir_length = w;
1827 reqbits &= (~(NTREQ_IRLEN));
1829 case NTAP_OPT_IRMASK:
1830 pTap->ir_capture_mask = w;
1831 reqbits &= (~(NTREQ_IRMASK));
1833 case NTAP_OPT_IRCAPTURE:
1834 pTap->ir_capture_value = w;
1835 reqbits &= (~(NTREQ_IRCAPTURE));
1838 } // switch(n->value)
1839 } // while( goi->argc )
1841 // Did we get all the options?
1844 Jim_SetResult_sprintf( goi->interp,
1845 "newtap: %s missing required parameters",
1847 // fixme: Tell user what is missing :-(
1848 // no memory leaks pelase
1849 free(((void *)(pTap->expected_ids)));
1850 free(((void *)(pTap->chip)));
1851 free(((void *)(pTap->tapname)));
1852 free(((void *)(pTap->dotted_name)));
1853 free(((void *)(pTap)));
1857 pTap->expected = malloc( pTap->ir_length );
1858 pTap->expected_mask = malloc( pTap->ir_length );
1859 pTap->cur_instr = malloc( pTap->ir_length );
1861 buf_set_u32( pTap->expected,
1864 pTap->ir_capture_value );
1865 buf_set_u32( pTap->expected_mask,
1868 pTap->ir_capture_mask );
1869 buf_set_ones( pTap->cur_instr,
1875 jtag_register_event_callback(jtag_reset_callback, pTap );
1877 ppTap = &(jtag_all_taps);
1878 while( (*ppTap) != NULL ){
1879 ppTap = &((*ppTap)->next_tap);
1883 static int n_taps = 0;
1884 pTap->abs_chain_position = n_taps++;
1886 LOG_DEBUG( "Created Tap: %s @ abs position %d, irlen %d, capture: 0x%x mask: 0x%x",
1887 (*ppTap)->dotted_name,
1888 (*ppTap)->abs_chain_position,
1889 (*ppTap)->ir_length,
1890 (*ppTap)->ir_capture_value,
1891 (*ppTap)->ir_capture_mask );
1899 jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
1904 struct command_context_s *context;
1908 JTAG_CMD_INIT_RESET,
1911 JTAG_CMD_TAPDISABLE,
1912 JTAG_CMD_TAPISENABLED
1915 const Jim_Nvp jtag_cmds[] = {
1916 { .name = "interface" , .value = JTAG_CMD_INTERFACE },
1917 { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
1918 { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
1919 { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
1920 { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
1921 { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
1923 { .name = NULL, .value = -1 },
1926 context = Jim_GetAssocData(interp, "context");
1927 // go past the command
1928 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
1930 e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
1932 Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
1935 Jim_SetEmptyResult( goi.interp );
1937 case JTAG_CMD_INTERFACE:
1938 // return the name of the interface
1939 // TCL code might need to know the exact type...
1940 // FUTURE: we allow this as a means to "set" the interface.
1941 if( goi.argc != 0 ){
1942 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
1945 Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
1947 case JTAG_CMD_INIT_RESET:
1948 if( goi.argc != 0 ){
1949 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
1952 e = jtag_init_reset(context);
1953 if( e != ERROR_OK ){
1954 Jim_SetResult_sprintf( goi.interp, "error: %d", e);
1958 case JTAG_CMD_NEWTAP:
1959 return jim_newtap_cmd( &goi );
1961 case JTAG_CMD_TAPISENABLED:
1962 case JTAG_CMD_TAPENABLE:
1963 case JTAG_CMD_TAPDISABLE:
1964 if( goi.argc != 1 ){
1965 Jim_SetResultString( goi.interp, "Too many parameters",-1 );
1971 t = jtag_TapByJimObj( goi.interp, goi.argv[0] );
1976 case JTAG_CMD_TAPISENABLED:
1979 case JTAG_CMD_TAPENABLE:
1983 case JTAG_CMD_TAPDISABLE:
1988 Jim_SetResult( goi.interp, Jim_NewIntObj( goi.interp, e ) );
1997 int jtag_register_commands(struct command_context_s *cmd_ctx)
1999 register_jim( cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
2001 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
2002 COMMAND_CONFIG, "try to configure interface");
2003 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
2004 COMMAND_ANY, "set jtag speed (if supported)");
2005 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
2006 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
2007 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
2008 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
2009 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
2010 COMMAND_CONFIG, NULL);
2011 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
2012 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
2013 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
2014 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
2016 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
2017 COMMAND_EXEC, "print current scan chain configuration");
2019 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
2020 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
2021 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
2022 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
2023 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
2024 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
2025 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
2026 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
2027 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
2029 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
2030 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
2034 int jtag_interface_init(struct command_context_s *cmd_ctx)
2039 if (!jtag_interface)
2041 /* nothing was previously specified by "interface" command */
2042 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
2043 return ERROR_JTAG_INVALID_INTERFACE;
2047 jtag_interface->khz(speed_khz, &jtag_speed);
2051 if (jtag_interface->init() != ERROR_OK)
2052 return ERROR_JTAG_INIT_FAILED;
2056 jtag = jtag_interface;
2060 static int jtag_init_inner(struct command_context_s *cmd_ctx)
2065 LOG_DEBUG("Init JTAG chain");
2068 tap = jtag_NextEnabledTap(NULL);
2070 LOG_ERROR("There are no enabled taps?");
2071 return ERROR_JTAG_INIT_FAILED;
2075 if ((retval=jtag_execute_queue())!=ERROR_OK)
2078 /* examine chain first, as this could discover the real chain layout */
2079 if (jtag_examine_chain() != ERROR_OK)
2081 LOG_ERROR("trying to validate configured JTAG chain anyway...");
2084 if (jtag_validate_chain() != ERROR_OK)
2086 LOG_ERROR("Could not validate JTAG chain, continuing anyway...");
2092 int jtag_init_reset(struct command_context_s *cmd_ctx)
2096 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
2099 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
2101 /* Reset can happen after a power cycle.
2103 * Ideally we would only assert TRST or run TLR before the target reset.
2105 * However w/srst_pulls_trst, trst is asserted together with the target
2106 * reset whether we want it or not.
2108 * NB! Some targets have JTAG circuitry disabled until a
2109 * trst & srst has been asserted.
2111 * NB! here we assume nsrst/ntrst delay are sufficient!
2113 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
2116 jtag_add_reset(1, 0); /* TLR or TRST */
2117 if (jtag_reset_config & RESET_HAS_SRST)
2119 jtag_add_reset(1, 1);
2120 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
2121 jtag_add_reset(0, 1);
2123 jtag_add_reset(0, 0);
2124 if ((retval = jtag_execute_queue()) != ERROR_OK)
2127 /* Check that we can communication on the JTAG chain + eventually we want to
2128 * be able to perform enumeration only after OpenOCD has started
2129 * telnet and GDB server
2131 * That would allow users to more easily perform any magic they need to before
2134 return jtag_init_inner(cmd_ctx);
2137 int jtag_init(struct command_context_s *cmd_ctx)
2140 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
2142 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
2146 return jtag_init_reset(cmd_ctx);
2149 static int default_khz(int khz, int *jtag_speed)
2151 LOG_ERROR("Translation from khz to jtag_speed not implemented");
2155 static int default_speed_div(int speed, int *khz)
2157 LOG_ERROR("Translation from jtag_speed to khz not implemented");
2161 static int default_power_dropout(int *dropout)
2163 *dropout=0; /* by default we can't detect power dropout */
2167 static int default_srst_asserted(int *srst_asserted)
2169 *srst_asserted=0; /* by default we can't detect srst asserted */
2173 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2178 /* check whether the interface is already configured */
2181 LOG_WARNING("Interface already configured, ignoring");
2185 /* interface name is a mandatory argument */
2186 if (argc < 1 || args[0][0] == '\0')
2188 return ERROR_COMMAND_SYNTAX_ERROR;
2191 for (i=0; jtag_interfaces[i]; i++)
2193 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
2195 if ((retval = jtag_interfaces[i]->register_commands(cmd_ctx)) != ERROR_OK)
2200 jtag_interface = jtag_interfaces[i];
2202 if (jtag_interface->khz == NULL)
2204 jtag_interface->khz = default_khz;
2206 if (jtag_interface->speed_div == NULL)
2208 jtag_interface->speed_div = default_speed_div;
2210 if (jtag_interface->power_dropout == NULL)
2212 jtag_interface->power_dropout = default_power_dropout;
2214 if (jtag_interface->srst_asserted == NULL)
2216 jtag_interface->srst_asserted = default_srst_asserted;
2223 /* no valid interface was found (i.e. the configuration option,
2224 * didn't match one of the compiled-in interfaces
2226 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
2227 LOG_ERROR("compiled-in jtag interfaces:");
2228 for (i = 0; jtag_interfaces[i]; i++)
2230 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
2233 return ERROR_JTAG_INVALID_INTERFACE;
2236 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2240 Jim_Obj *newargs[ 10 ];
2244 // argv[-1] = command
2245 // argv[ 0] = ir length
2246 // argv[ 1] = ir capture
2247 // argv[ 2] = ir mask
2248 // argv[ 3] = not actually used by anything but in the docs
2251 command_print( cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
2254 command_print( cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
2255 command_print( cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
2259 command_print( cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundryscan(len5)");
2260 command_print( cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
2261 command_print( cmd_ctx, "jtag newtap stm32 boundry ....., and the tap: \"stm32.boundery\"");
2262 command_print( cmd_ctx, "And then refer to the taps by the dotted name.");
2266 newargs[0] = Jim_NewStringObj( interp, "jtag", -1 );
2267 newargs[1] = Jim_NewStringObj( interp, "newtap", -1 );
2268 sprintf( buf, "chip%d", jtag_NumTotalTaps() );
2269 newargs[2] = Jim_NewStringObj( interp, buf, -1 );
2270 sprintf( buf, "tap%d", jtag_NumTotalTaps() );
2271 newargs[3] = Jim_NewStringObj( interp, buf, -1 );
2272 newargs[4] = Jim_NewStringObj( interp, "-irlen", -1 );
2273 newargs[5] = Jim_NewStringObj( interp, args[0], -1 );
2274 newargs[6] = Jim_NewStringObj( interp, "-ircapture", -1 );
2275 newargs[7] = Jim_NewStringObj( interp, args[1], -1 );
2276 newargs[8] = Jim_NewStringObj( interp, "-irmask", -1 );
2277 newargs[9] = Jim_NewStringObj( interp, args[2], -1 );
2279 command_print( cmd_ctx, "NEW COMMAND:");
2280 sprintf( buf, "%s %s %s %s %s %s %s %s %s %s",
2281 Jim_GetString( newargs[0], NULL ),
2282 Jim_GetString( newargs[1], NULL ),
2283 Jim_GetString( newargs[2], NULL ),
2284 Jim_GetString( newargs[3], NULL ),
2285 Jim_GetString( newargs[4], NULL ),
2286 Jim_GetString( newargs[5], NULL ),
2287 Jim_GetString( newargs[6], NULL ),
2288 Jim_GetString( newargs[7], NULL ),
2289 Jim_GetString( newargs[8], NULL ),
2290 Jim_GetString( newargs[9], NULL ) );
2294 e = jim_jtag_command( interp, 10, newargs );
2296 command_print( cmd_ctx, "%s", Jim_GetString( Jim_GetResult(interp), NULL ) );
2302 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2306 tap = jtag_all_taps;
2307 command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
2308 command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
2311 u32 expected, expected_mask, cur_instr, ii;
2312 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
2313 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
2314 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
2316 command_print(cmd_ctx,
2317 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
2318 tap->abs_chain_position,
2320 tap->enabled ? 'Y' : 'n',
2322 (tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
2328 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
2329 command_print(cmd_ctx, " | | | | 0x%08x | | | | ",
2330 tap->expected_ids[ii]);
2333 tap = tap->next_tap;
2339 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2342 return ERROR_COMMAND_SYNTAX_ERROR;
2346 if (strcmp(args[0], "none") == 0)
2347 jtag_reset_config = RESET_NONE;
2348 else if (strcmp(args[0], "trst_only") == 0)
2349 jtag_reset_config = RESET_HAS_TRST;
2350 else if (strcmp(args[0], "srst_only") == 0)
2351 jtag_reset_config = RESET_HAS_SRST;
2352 else if (strcmp(args[0], "trst_and_srst") == 0)
2353 jtag_reset_config = RESET_TRST_AND_SRST;
2356 LOG_ERROR("(1) invalid reset_config argument (%s), defaulting to none", args[0]);
2357 jtag_reset_config = RESET_NONE;
2358 return ERROR_INVALID_ARGUMENTS;
2364 if (strcmp(args[1], "separate") == 0)
2366 /* seperate reset lines - default */
2369 if (strcmp(args[1], "srst_pulls_trst") == 0)
2370 jtag_reset_config |= RESET_SRST_PULLS_TRST;
2371 else if (strcmp(args[1], "trst_pulls_srst") == 0)
2372 jtag_reset_config |= RESET_TRST_PULLS_SRST;
2373 else if (strcmp(args[1], "combined") == 0)
2374 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
2377 LOG_ERROR("(2) invalid reset_config argument (%s), defaulting to none", args[1]);
2378 jtag_reset_config = RESET_NONE;
2379 return ERROR_INVALID_ARGUMENTS;
2386 if (strcmp(args[2], "trst_open_drain") == 0)
2387 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
2388 else if (strcmp(args[2], "trst_push_pull") == 0)
2389 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
2392 LOG_ERROR("(3) invalid reset_config argument (%s) defaulting to none", args[2] );
2393 jtag_reset_config = RESET_NONE;
2394 return ERROR_INVALID_ARGUMENTS;
2400 if (strcmp(args[3], "srst_push_pull") == 0)
2401 jtag_reset_config |= RESET_SRST_PUSH_PULL;
2402 else if (strcmp(args[3], "srst_open_drain") == 0)
2403 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
2406 LOG_ERROR("(4) invalid reset_config argument (%s), defaulting to none", args[3]);
2407 jtag_reset_config = RESET_NONE;
2408 return ERROR_INVALID_ARGUMENTS;
2415 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2419 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
2424 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
2430 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2434 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
2439 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
2445 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2447 int retval=ERROR_OK;
2451 LOG_DEBUG("handle jtag speed");
2454 cur_speed = jtag_speed = strtoul(args[0], NULL, 0);
2456 /* this command can be called during CONFIG,
2457 * in which case jtag isn't initialized */
2460 retval=jtag->speed(cur_speed);
2462 } else if (argc == 0)
2466 return ERROR_COMMAND_SYNTAX_ERROR;
2468 command_print(cmd_ctx, "jtag_speed: %d", jtag_speed);
2473 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2475 int retval=ERROR_OK;
2476 LOG_DEBUG("handle jtag khz");
2480 speed_khz = strtoul(args[0], NULL, 0);
2484 LOG_DEBUG("have interface set up");
2486 if ((retval=jtag->khz(speed_khz, &speed_div1))!=ERROR_OK)
2492 cur_speed = jtag_speed = speed_div1;
2494 retval=jtag->speed(cur_speed);
2503 return ERROR_COMMAND_SYNTAX_ERROR;
2508 if ((retval=jtag->speed_div(jtag_speed, &speed_khz))!=ERROR_OK)
2514 command_print(cmd_ctx, "RCLK - adaptive");
2517 command_print(cmd_ctx, "%d kHz", speed_khz);
2523 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2525 enum tap_state state;
2529 return ERROR_COMMAND_SYNTAX_ERROR;
2533 for (state = 0; state < 16; state++)
2535 if (strcmp(args[0], tap_state_strings[state]) == 0)
2537 jtag_add_end_state(state);
2538 jtag_execute_queue();
2542 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
2547 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2554 return ERROR_COMMAND_SYNTAX_ERROR;
2557 if (args[0][0] == '1')
2559 else if (args[0][0] == '0')
2563 return ERROR_COMMAND_SYNTAX_ERROR;
2566 if (args[1][0] == '1')
2568 else if (args[1][0] == '0')
2572 return ERROR_COMMAND_SYNTAX_ERROR;
2575 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
2576 return ERROR_JTAG_INIT_FAILED;
2578 jtag_add_reset(trst, srst);
2579 jtag_execute_queue();
2584 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2588 return ERROR_COMMAND_SYNTAX_ERROR;
2591 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
2592 jtag_execute_queue();
2598 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2601 scan_field_t *fields;
2604 if ((argc < 2) || (argc % 2))
2606 return ERROR_COMMAND_SYNTAX_ERROR;
2609 fields = malloc(sizeof(scan_field_t) * argc / 2);
2611 for (i = 0; i < argc / 2; i++)
2613 tap = jtag_TapByString( args[i*2] );
2616 command_print( cmd_ctx, "Tap: %s unknown", args[i*2] );
2619 int field_size = tap->ir_length;
2620 fields[i].tap = tap;
2621 fields[i].out_value = malloc(CEIL(field_size, 8));
2622 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
2623 fields[i].out_mask = NULL;
2624 fields[i].in_value = NULL;
2625 fields[i].in_check_mask = NULL;
2626 fields[i].in_handler = NULL;
2627 fields[i].in_handler_priv = NULL;
2630 jtag_add_ir_scan(argc / 2, fields, -1);
2631 jtag_execute_queue();
2633 for (i = 0; i < argc / 2; i++)
2634 free(fields[i].out_value);
2641 int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
2644 scan_field_t *fields;
2646 int field_count = 0;
2651 * args[2] = num_bits
2652 * args[3] = hex string
2653 * ... repeat num bits and hex string ...
2655 if ((argc < 4) || ((argc % 2)!=0))
2657 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
2661 for (i = 2; i < argc; i+=2)
2665 e = Jim_GetLong(interp, args[i], &bits);
2670 tap = jtag_TapByJimObj( interp, args[1] );
2675 num_fields=(argc-2)/2;
2676 fields = malloc(sizeof(scan_field_t) * num_fields);
2677 for (i = 2; i < argc; i+=2)
2683 Jim_GetLong(interp, args[i], &bits);
2684 str = Jim_GetString(args[i+1], &len);
2687 fields[field_count].tap = tap;
2688 fields[field_count].num_bits = bits;
2689 fields[field_count].out_value = malloc(CEIL(bits, 8));
2690 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
2691 fields[field_count].out_mask = NULL;
2692 fields[field_count].in_value = fields[field_count].out_value;
2693 fields[field_count].in_check_mask = NULL;
2694 fields[field_count].in_check_value = NULL;
2695 fields[field_count].in_handler = NULL;
2696 fields[field_count++].in_handler_priv = NULL;
2699 jtag_add_dr_scan(num_fields, fields, -1);
2700 retval = jtag_execute_queue();
2701 if (retval != ERROR_OK)
2703 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
2708 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
2709 for (i = 2; i < argc; i+=2)
2714 Jim_GetLong(interp, args[i], &bits);
2715 str = buf_to_str(fields[field_count].in_value, bits, 16);
2716 free(fields[field_count].out_value);
2718 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
2723 Jim_SetResult(interp, list);
2730 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2734 if (strcmp(args[0], "enable") == 0)
2736 jtag_verify_capture_ir = 1;
2738 else if (strcmp(args[0], "disable") == 0)
2740 jtag_verify_capture_ir = 0;
2743 return ERROR_COMMAND_SYNTAX_ERROR;
2745 } else if (argc != 0)
2747 return ERROR_COMMAND_SYNTAX_ERROR;
2750 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
2756 int jtag_power_dropout(int *dropout)
2758 return jtag->power_dropout(dropout);
2761 int jtag_srst_asserted(int *srst_asserted)
2763 return jtag->srst_asserted(srst_asserted);