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;
1577 if( tap->expected_id ){
1578 if( tap->idcode != tap->expected_id ){
1579 LOG_ERROR("ERROR: Tap: %s - Expected id: 0x%08x, Got: 0x%08x",
1583 LOG_ERROR("ERROR: expected: mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x",
1584 EXTRACT_MFG( tap->expected_id ),
1585 EXTRACT_PART( tap->expected_id ),
1586 EXTRACT_VER( tap->expected_id ) );
1587 LOG_ERROR("ERROR: got: mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x",
1588 EXTRACT_MFG( tap->idcode ),
1589 EXTRACT_PART( tap->idcode ),
1590 EXTRACT_VER( tap->idcode ) );
1592 LOG_INFO("JTAG Tap/device matched");
1596 LOG_INFO("JTAG TAP ID: 0x%08x - Unknown - please report (A) chipname and (B) idcode to the openocd project",
1600 tap = jtag_NextEnabledTap(tap);
1605 /* see if number of discovered devices matches configuration */
1606 if (device_count != jtag_NumEnabledTaps())
1608 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match (enabled) configuration (%i), total taps: %d",
1609 device_count, jtag_NumEnabledTaps(), jtag_NumTotalTaps());
1610 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1611 return ERROR_JTAG_INIT_FAILED;
1617 int jtag_validate_chain(void)
1620 int total_ir_length = 0;
1626 total_ir_length = 0;
1628 tap = jtag_NextEnabledTap(tap);
1632 total_ir_length += tap->ir_length;
1635 total_ir_length += 2;
1636 ir_test = malloc(CEIL(total_ir_length, 8));
1637 buf_set_ones(ir_test, total_ir_length);
1640 field.num_bits = total_ir_length;
1641 field.out_value = ir_test;
1642 field.out_mask = NULL;
1643 field.in_value = ir_test;
1644 field.in_check_value = NULL;
1645 field.in_check_mask = NULL;
1646 field.in_handler = NULL;
1647 field.in_handler_priv = NULL;
1649 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1650 jtag_execute_queue();
1655 tap = jtag_NextEnabledTap(tap);
1661 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1663 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1664 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1667 return ERROR_JTAG_INIT_FAILED;
1669 chain_pos += tap->ir_length;
1672 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1674 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1675 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1678 return ERROR_JTAG_INIT_FAILED;
1688 jim_newtap_cmd( Jim_GetOptInfo *goi )
1698 const Jim_Nvp opts[] = {
1699 #define NTAP_OPT_IRLEN 0
1700 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
1701 #define NTAP_OPT_IRMASK 1
1702 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
1703 #define NTAP_OPT_IRCAPTURE 2
1704 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
1705 #define NTAP_OPT_ENABLED 3
1706 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
1707 #define NTAP_OPT_DISABLED 4
1708 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
1709 #define NTAP_OPT_EXPECTED_ID 5
1710 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
1711 { .name = NULL , .value = -1 },
1715 pTap = malloc( sizeof(jtag_tap_t) );
1716 memset( pTap, 0, sizeof(*pTap) );
1718 Jim_SetResult_sprintf( goi->interp, "no memory");
1722 // we expect CHIP + TAP + OPTIONS
1724 if( goi->argc < 3 ){
1725 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
1728 Jim_GetOpt_String( goi, &cp, NULL );
1729 pTap->chip = strdup(cp);
1731 Jim_GetOpt_String( goi, &cp, NULL );
1732 pTap->tapname = strdup(cp);
1734 // name + dot + name + null
1735 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
1737 sprintf( cp, "%s.%s", pTap->chip, pTap->tapname );
1738 pTap->dotted_name = cp;
1740 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
1741 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
1744 // default is enabled
1747 // deal with options
1748 #define NTREQ_IRLEN 1
1749 #define NTREQ_IRCAPTURE 2
1750 #define NTREQ_IRMASK 4
1752 // clear them as we find them
1753 reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
1756 e = Jim_GetOpt_Nvp( goi, opts, &n );
1758 Jim_GetOpt_NvpUnknown( goi, opts, 0 );
1761 LOG_DEBUG("Processing option: %s", n->name );
1763 case NTAP_OPT_ENABLED:
1766 case NTAP_OPT_DISABLED:
1769 case NTAP_OPT_EXPECTED_ID:
1770 e = Jim_GetOpt_Wide( goi, &w );
1771 pTap->expected_id = w;
1773 case NTAP_OPT_IRLEN:
1774 case NTAP_OPT_IRMASK:
1775 case NTAP_OPT_IRCAPTURE:
1776 e = Jim_GetOpt_Wide( goi, &w );
1778 Jim_SetResult_sprintf( goi->interp, "option: %s bad parameter", n->name );
1781 if( (w < 0) || (w > 0xffff) ){
1783 Jim_SetResult_sprintf( goi->interp, "option: %s - wacky value: %d (0x%x)",
1784 n->name, (int)(w), (int)(w));
1788 case NTAP_OPT_IRLEN:
1789 pTap->ir_length = w;
1790 reqbits &= (~(NTREQ_IRLEN));
1792 case NTAP_OPT_IRMASK:
1793 pTap->ir_capture_mask = w;
1794 reqbits &= (~(NTREQ_IRMASK));
1796 case NTAP_OPT_IRCAPTURE:
1797 pTap->ir_capture_value = w;
1798 reqbits &= (~(NTREQ_IRCAPTURE));
1801 } // switch(n->value)
1802 } // while( goi->argc )
1804 // Did we get all the options?
1807 Jim_SetResult_sprintf( goi->interp,
1808 "newtap: %s missing required parameters",
1810 // fixme: Tell user what is missing :-(
1811 // no memory leaks pelase
1812 free(((void *)(pTap->chip)));
1813 free(((void *)(pTap->tapname)));
1814 free(((void *)(pTap->dotted_name)));
1815 free(((void *)(pTap)));
1819 pTap->expected = malloc( pTap->ir_length );
1820 pTap->expected_mask = malloc( pTap->ir_length );
1821 pTap->cur_instr = malloc( pTap->ir_length );
1823 buf_set_u32( pTap->expected,
1826 pTap->ir_capture_value );
1827 buf_set_u32( pTap->expected_mask,
1830 pTap->ir_capture_mask );
1831 buf_set_ones( pTap->cur_instr,
1837 jtag_register_event_callback(jtag_reset_callback, pTap );
1839 ppTap = &(jtag_all_taps);
1840 while( (*ppTap) != NULL ){
1841 ppTap = &((*ppTap)->next_tap);
1845 static int n_taps = 0;
1846 pTap->abs_chain_position = n_taps++;
1848 LOG_DEBUG( "Created Tap: %s @ abs position %d, irlen %d, capture: 0x%x mask: 0x%x",
1849 (*ppTap)->dotted_name,
1850 (*ppTap)->abs_chain_position,
1851 (*ppTap)->ir_length,
1852 (*ppTap)->ir_capture_value,
1853 (*ppTap)->ir_capture_mask );
1861 jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
1866 struct command_context_s *context;
1870 JTAG_CMD_INIT_RESET,
1873 JTAG_CMD_TAPDISABLE,
1874 JTAG_CMD_TAPISENABLED
1877 const Jim_Nvp jtag_cmds[] = {
1878 { .name = "interface" , .value = JTAG_CMD_INTERFACE },
1879 { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
1880 { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
1881 { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
1882 { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
1883 { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
1885 { .name = NULL, .value = -1 },
1888 context = Jim_GetAssocData(interp, "context");
1889 // go past the command
1890 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
1892 e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
1894 Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
1897 Jim_SetEmptyResult( goi.interp );
1899 case JTAG_CMD_INTERFACE:
1900 // return the name of the interface
1901 // TCL code might need to know the exact type...
1902 // FUTURE: we allow this as a means to "set" the interface.
1903 if( goi.argc != 0 ){
1904 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
1907 Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
1909 case JTAG_CMD_INIT_RESET:
1910 if( goi.argc != 0 ){
1911 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
1914 e = jtag_init_reset(context);
1915 if( e != ERROR_OK ){
1916 Jim_SetResult_sprintf( goi.interp, "error: %d", e);
1920 case JTAG_CMD_NEWTAP:
1921 return jim_newtap_cmd( &goi );
1923 case JTAG_CMD_TAPISENABLED:
1924 case JTAG_CMD_TAPENABLE:
1925 case JTAG_CMD_TAPDISABLE:
1926 if( goi.argc != 1 ){
1927 Jim_SetResultString( goi.interp, "Too many parameters",-1 );
1933 t = jtag_TapByJimObj( goi.interp, goi.argv[0] );
1938 case JTAG_CMD_TAPISENABLED:
1941 case JTAG_CMD_TAPENABLE:
1945 case JTAG_CMD_TAPDISABLE:
1950 Jim_SetResult( goi.interp, Jim_NewIntObj( goi.interp, e ) );
1959 int jtag_register_commands(struct command_context_s *cmd_ctx)
1961 register_jim( cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
1963 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1964 COMMAND_CONFIG, "try to configure interface");
1965 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1966 COMMAND_ANY, "set jtag speed (if supported)");
1967 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1968 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1969 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1970 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1971 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1972 COMMAND_CONFIG, NULL);
1973 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1974 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
1975 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1976 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
1978 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1979 COMMAND_EXEC, "print current scan chain configuration");
1981 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1982 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1983 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1984 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1985 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1986 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1987 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1988 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1989 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
1991 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1992 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1996 int jtag_interface_init(struct command_context_s *cmd_ctx)
2001 if (!jtag_interface)
2003 /* nothing was previously specified by "interface" command */
2004 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
2005 return ERROR_JTAG_INVALID_INTERFACE;
2009 jtag_interface->khz(speed_khz, &jtag_speed);
2013 if (jtag_interface->init() != ERROR_OK)
2014 return ERROR_JTAG_INIT_FAILED;
2018 jtag = jtag_interface;
2022 static int jtag_init_inner(struct command_context_s *cmd_ctx)
2027 LOG_DEBUG("Init JTAG chain");
2030 tap = jtag_NextEnabledTap(NULL);
2032 LOG_ERROR("There are no enabled taps?");
2033 return ERROR_JTAG_INIT_FAILED;
2037 if ((retval=jtag_execute_queue())!=ERROR_OK)
2040 /* examine chain first, as this could discover the real chain layout */
2041 if (jtag_examine_chain() != ERROR_OK)
2043 LOG_ERROR("trying to validate configured JTAG chain anyway...");
2046 if (jtag_validate_chain() != ERROR_OK)
2048 LOG_ERROR("Could not validate JTAG chain, continuing anyway...");
2054 int jtag_init_reset(struct command_context_s *cmd_ctx)
2058 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
2061 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
2063 /* Reset can happen after a power cycle.
2065 * Ideally we would only assert TRST or run TLR before the target reset.
2067 * However w/srst_pulls_trst, trst is asserted together with the target
2068 * reset whether we want it or not.
2070 * NB! Some targets have JTAG circuitry disabled until a
2071 * trst & srst has been asserted.
2073 * NB! here we assume nsrst/ntrst delay are sufficient!
2075 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
2078 jtag_add_reset(1, 0); /* TLR or TRST */
2079 if (jtag_reset_config & RESET_HAS_SRST)
2081 jtag_add_reset(1, 1);
2082 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
2083 jtag_add_reset(0, 1);
2085 jtag_add_reset(0, 0);
2086 if ((retval = jtag_execute_queue()) != ERROR_OK)
2089 /* Check that we can communication on the JTAG chain + eventually we want to
2090 * be able to perform enumeration only after OpenOCD has started
2091 * telnet and GDB server
2093 * That would allow users to more easily perform any magic they need to before
2096 return jtag_init_inner(cmd_ctx);
2099 int jtag_init(struct command_context_s *cmd_ctx)
2102 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
2104 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
2108 return jtag_init_reset(cmd_ctx);
2111 static int default_khz(int khz, int *jtag_speed)
2113 LOG_ERROR("Translation from khz to jtag_speed not implemented");
2117 static int default_speed_div(int speed, int *khz)
2119 LOG_ERROR("Translation from jtag_speed to khz not implemented");
2123 static int default_power_dropout(int *dropout)
2125 *dropout=0; /* by default we can't detect power dropout */
2129 static int default_srst_asserted(int *srst_asserted)
2131 *srst_asserted=0; /* by default we can't detect srst asserted */
2135 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2140 /* check whether the interface is already configured */
2143 LOG_WARNING("Interface already configured, ignoring");
2147 /* interface name is a mandatory argument */
2148 if (argc < 1 || args[0][0] == '\0')
2150 return ERROR_COMMAND_SYNTAX_ERROR;
2153 for (i=0; jtag_interfaces[i]; i++)
2155 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
2157 if ((retval = jtag_interfaces[i]->register_commands(cmd_ctx)) != ERROR_OK)
2162 jtag_interface = jtag_interfaces[i];
2164 if (jtag_interface->khz == NULL)
2166 jtag_interface->khz = default_khz;
2168 if (jtag_interface->speed_div == NULL)
2170 jtag_interface->speed_div = default_speed_div;
2172 if (jtag_interface->power_dropout == NULL)
2174 jtag_interface->power_dropout = default_power_dropout;
2176 if (jtag_interface->srst_asserted == NULL)
2178 jtag_interface->srst_asserted = default_srst_asserted;
2185 /* no valid interface was found (i.e. the configuration option,
2186 * didn't match one of the compiled-in interfaces
2188 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
2189 LOG_ERROR("compiled-in jtag interfaces:");
2190 for (i = 0; jtag_interfaces[i]; i++)
2192 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
2195 return ERROR_JTAG_INVALID_INTERFACE;
2198 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2202 Jim_Obj *newargs[ 10 ];
2206 // argv[-1] = command
2207 // argv[ 0] = ir length
2208 // argv[ 1] = ir capture
2209 // argv[ 2] = ir mask
2210 // argv[ 3] = not actually used by anything but in the docs
2213 command_print( cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
2216 command_print( cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
2217 command_print( cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
2221 command_print( cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundryscan(len5)");
2222 command_print( cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
2223 command_print( cmd_ctx, "jtag newtap stm32 boundry ....., and the tap: \"stm32.boundery\"");
2224 command_print( cmd_ctx, "And then refer to the taps by the dotted name.");
2228 newargs[0] = Jim_NewStringObj( interp, "jtag", -1 );
2229 newargs[1] = Jim_NewStringObj( interp, "newtap", -1 );
2230 sprintf( buf, "chip%d", jtag_NumTotalTaps() );
2231 newargs[2] = Jim_NewStringObj( interp, buf, -1 );
2232 sprintf( buf, "tap%d", jtag_NumTotalTaps() );
2233 newargs[3] = Jim_NewStringObj( interp, buf, -1 );
2234 newargs[4] = Jim_NewStringObj( interp, "-irlen", -1 );
2235 newargs[5] = Jim_NewStringObj( interp, args[0], -1 );
2236 newargs[6] = Jim_NewStringObj( interp, "-ircapture", -1 );
2237 newargs[7] = Jim_NewStringObj( interp, args[1], -1 );
2238 newargs[8] = Jim_NewStringObj( interp, "-irmask", -1 );
2239 newargs[9] = Jim_NewStringObj( interp, args[2], -1 );
2241 command_print( cmd_ctx, "NEW COMMAND:");
2242 sprintf( buf, "%s %s %s %s %s %s %s %s %s %s",
2243 Jim_GetString( newargs[0], NULL ),
2244 Jim_GetString( newargs[1], NULL ),
2245 Jim_GetString( newargs[2], NULL ),
2246 Jim_GetString( newargs[3], NULL ),
2247 Jim_GetString( newargs[4], NULL ),
2248 Jim_GetString( newargs[5], NULL ),
2249 Jim_GetString( newargs[6], NULL ),
2250 Jim_GetString( newargs[7], NULL ),
2251 Jim_GetString( newargs[8], NULL ),
2252 Jim_GetString( newargs[9], NULL ) );
2256 e = jim_jtag_command( interp, 10, newargs );
2258 command_print( cmd_ctx, "%s", Jim_GetString( Jim_GetResult(interp), NULL ) );
2264 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2268 tap = jtag_all_taps;
2269 command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
2270 command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
2273 u32 expected, expected_mask, cur_instr;
2274 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
2275 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
2276 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
2277 command_print(cmd_ctx,
2278 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
2279 tap->abs_chain_position,
2281 tap->enabled ? 'Y' : 'n',
2288 tap = tap->next_tap;
2294 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2297 return ERROR_COMMAND_SYNTAX_ERROR;
2301 if (strcmp(args[0], "none") == 0)
2302 jtag_reset_config = RESET_NONE;
2303 else if (strcmp(args[0], "trst_only") == 0)
2304 jtag_reset_config = RESET_HAS_TRST;
2305 else if (strcmp(args[0], "srst_only") == 0)
2306 jtag_reset_config = RESET_HAS_SRST;
2307 else if (strcmp(args[0], "trst_and_srst") == 0)
2308 jtag_reset_config = RESET_TRST_AND_SRST;
2311 LOG_ERROR("(1) invalid reset_config argument (%s), defaulting to none", args[0]);
2312 jtag_reset_config = RESET_NONE;
2313 return ERROR_INVALID_ARGUMENTS;
2319 if (strcmp(args[1], "separate") == 0)
2321 /* seperate reset lines - default */
2324 if (strcmp(args[1], "srst_pulls_trst") == 0)
2325 jtag_reset_config |= RESET_SRST_PULLS_TRST;
2326 else if (strcmp(args[1], "trst_pulls_srst") == 0)
2327 jtag_reset_config |= RESET_TRST_PULLS_SRST;
2328 else if (strcmp(args[1], "combined") == 0)
2329 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
2332 LOG_ERROR("(2) invalid reset_config argument (%s), defaulting to none", args[1]);
2333 jtag_reset_config = RESET_NONE;
2334 return ERROR_INVALID_ARGUMENTS;
2341 if (strcmp(args[2], "trst_open_drain") == 0)
2342 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
2343 else if (strcmp(args[2], "trst_push_pull") == 0)
2344 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
2347 LOG_ERROR("(3) invalid reset_config argument (%s) defaulting to none", args[2] );
2348 jtag_reset_config = RESET_NONE;
2349 return ERROR_INVALID_ARGUMENTS;
2355 if (strcmp(args[3], "srst_push_pull") == 0)
2356 jtag_reset_config |= RESET_SRST_PUSH_PULL;
2357 else if (strcmp(args[3], "srst_open_drain") == 0)
2358 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
2361 LOG_ERROR("(4) invalid reset_config argument (%s), defaulting to none", args[3]);
2362 jtag_reset_config = RESET_NONE;
2363 return ERROR_INVALID_ARGUMENTS;
2370 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2374 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
2379 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
2385 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2389 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
2394 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
2400 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2402 int retval=ERROR_OK;
2406 LOG_DEBUG("handle jtag speed");
2409 cur_speed = jtag_speed = strtoul(args[0], NULL, 0);
2411 /* this command can be called during CONFIG,
2412 * in which case jtag isn't initialized */
2415 retval=jtag->speed(cur_speed);
2417 } else if (argc == 0)
2421 return ERROR_COMMAND_SYNTAX_ERROR;
2423 command_print(cmd_ctx, "jtag_speed: %d", jtag_speed);
2428 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2430 int retval=ERROR_OK;
2431 LOG_DEBUG("handle jtag khz");
2435 speed_khz = strtoul(args[0], NULL, 0);
2439 LOG_DEBUG("have interface set up");
2441 if ((retval=jtag->khz(speed_khz, &speed_div1))!=ERROR_OK)
2447 cur_speed = jtag_speed = speed_div1;
2449 retval=jtag->speed(cur_speed);
2458 return ERROR_COMMAND_SYNTAX_ERROR;
2463 if ((retval=jtag->speed_div(jtag_speed, &speed_khz))!=ERROR_OK)
2469 command_print(cmd_ctx, "RCLK - adaptive");
2472 command_print(cmd_ctx, "%d kHz", speed_khz);
2478 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2480 enum tap_state state;
2484 return ERROR_COMMAND_SYNTAX_ERROR;
2488 for (state = 0; state < 16; state++)
2490 if (strcmp(args[0], tap_state_strings[state]) == 0)
2492 jtag_add_end_state(state);
2493 jtag_execute_queue();
2497 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
2502 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2509 return ERROR_COMMAND_SYNTAX_ERROR;
2512 if (args[0][0] == '1')
2514 else if (args[0][0] == '0')
2518 return ERROR_COMMAND_SYNTAX_ERROR;
2521 if (args[1][0] == '1')
2523 else if (args[1][0] == '0')
2527 return ERROR_COMMAND_SYNTAX_ERROR;
2530 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
2531 return ERROR_JTAG_INIT_FAILED;
2533 jtag_add_reset(trst, srst);
2534 jtag_execute_queue();
2539 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2543 return ERROR_COMMAND_SYNTAX_ERROR;
2546 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
2547 jtag_execute_queue();
2553 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2556 scan_field_t *fields;
2559 if ((argc < 2) || (argc % 2))
2561 return ERROR_COMMAND_SYNTAX_ERROR;
2564 fields = malloc(sizeof(scan_field_t) * argc / 2);
2566 for (i = 0; i < argc / 2; i++)
2568 tap = jtag_TapByString( args[i*2] );
2571 command_print( cmd_ctx, "Tap: %s unknown", args[i*2] );
2574 int field_size = tap->ir_length;
2575 fields[i].tap = tap;
2576 fields[i].out_value = malloc(CEIL(field_size, 8));
2577 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
2578 fields[i].out_mask = NULL;
2579 fields[i].in_value = NULL;
2580 fields[i].in_check_mask = NULL;
2581 fields[i].in_handler = NULL;
2582 fields[i].in_handler_priv = NULL;
2585 jtag_add_ir_scan(argc / 2, fields, -1);
2586 jtag_execute_queue();
2588 for (i = 0; i < argc / 2; i++)
2589 free(fields[i].out_value);
2596 int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
2599 scan_field_t *fields;
2601 int field_count = 0;
2606 * args[2] = num_bits
2607 * args[3] = hex string
2608 * ... repeat num bits and hex string ...
2610 if ((argc < 4) || ((argc % 2)!=0))
2612 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
2616 for (i = 2; i < argc; i+=2)
2620 e = Jim_GetLong(interp, args[i], &bits);
2625 tap = jtag_TapByJimObj( interp, args[1] );
2630 num_fields=(argc-2)/2;
2631 fields = malloc(sizeof(scan_field_t) * num_fields);
2632 for (i = 2; i < argc; i+=2)
2638 Jim_GetLong(interp, args[i], &bits);
2639 str = Jim_GetString(args[i+1], &len);
2642 fields[field_count].tap = tap;
2643 fields[field_count].num_bits = bits;
2644 fields[field_count].out_value = malloc(CEIL(bits, 8));
2645 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
2646 fields[field_count].out_mask = NULL;
2647 fields[field_count].in_value = fields[field_count].out_value;
2648 fields[field_count].in_check_mask = NULL;
2649 fields[field_count].in_check_value = NULL;
2650 fields[field_count].in_handler = NULL;
2651 fields[field_count++].in_handler_priv = NULL;
2654 jtag_add_dr_scan(num_fields, fields, -1);
2655 retval = jtag_execute_queue();
2656 if (retval != ERROR_OK)
2658 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
2663 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
2664 for (i = 2; i < argc; i+=2)
2669 Jim_GetLong(interp, args[i], &bits);
2670 str = buf_to_str(fields[field_count].in_value, bits, 16);
2671 free(fields[field_count].out_value);
2673 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
2678 Jim_SetResult(interp, list);
2685 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2689 if (strcmp(args[0], "enable") == 0)
2691 jtag_verify_capture_ir = 1;
2693 else if (strcmp(args[0], "disable") == 0)
2695 jtag_verify_capture_ir = 0;
2698 return ERROR_COMMAND_SYNTAX_ERROR;
2700 } else if (argc != 0)
2702 return ERROR_COMMAND_SYNTAX_ERROR;
2705 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
2711 int jtag_power_dropout(int *dropout)
2713 return jtag->power_dropout(dropout);
2716 int jtag_srst_asserted(int *srst_asserted)
2718 return jtag->srst_asserted(srst_asserted);