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 * Copyright (C) 2009 SoftPLC Corporation *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
17 * This program is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20 * GNU General Public License for more details. *
22 * You should have received a copy of the GNU General Public License *
23 * along with this program; if not, write to the *
24 * Free Software Foundation, Inc., *
25 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
26 ***************************************************************************/
38 int jtag_flush_queue_count; /* count # of flushes for profiling / debugging purposes */
41 /* note that this is not marked as static as it must be available from outside jtag.c for those
42 that implement the jtag_xxx() minidriver layer
44 int jtag_error=ERROR_OK;
46 typedef struct cmd_queue_page_s
50 struct cmd_queue_page_s *next;
53 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
54 static cmd_queue_page_t *cmd_queue_pages = NULL;
56 char* jtag_event_strings[] =
58 "JTAG controller reset (RESET or TRST)"
61 const Jim_Nvp nvp_jtag_tap_event[] = {
62 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
63 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
65 { .name = NULL, .value = -1 }
71 #ifndef HAVE_JTAG_MINIDRIVER_H
72 struct jtag_callback_entry
74 struct jtag_callback_entry *next;
76 jtag_callback_t callback;
78 jtag_callback_data_t data1;
79 jtag_callback_data_t data2;
84 static struct jtag_callback_entry *jtag_callback_queue_head = NULL;
85 static struct jtag_callback_entry *jtag_callback_queue_tail = NULL;
89 jtag_command_t *jtag_command_queue = NULL;
90 jtag_command_t **last_comand_pointer = &jtag_command_queue;
91 static jtag_tap_t *jtag_all_taps = NULL;
93 enum reset_types jtag_reset_config = RESET_NONE;
94 tap_state_t cmd_queue_end_state = TAP_RESET;
95 tap_state_t cmd_queue_cur_state = TAP_RESET;
97 int jtag_verify_capture_ir = 1;
99 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
100 static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
101 static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
103 /* maximum number of JTAG devices expected in the chain
105 #define JTAG_MAX_CHAIN_SIZE 20
107 /* callbacks to inform high-level handlers about JTAG state changes */
108 jtag_event_callback_t *jtag_event_callbacks;
111 static int speed_khz = 0;
112 /* flag if the kHz speed was defined */
113 static int hasKHz = 0;
115 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
118 #if BUILD_ECOSBOARD == 1
119 extern jtag_interface_t zy1000_interface;
122 #if BUILD_PARPORT == 1
123 extern jtag_interface_t parport_interface;
127 extern jtag_interface_t dummy_interface;
130 #if BUILD_FT2232_FTD2XX == 1
131 extern jtag_interface_t ft2232_interface;
134 #if BUILD_FT2232_LIBFTDI == 1
135 extern jtag_interface_t ft2232_interface;
138 #if BUILD_AMTJTAGACCEL == 1
139 extern jtag_interface_t amt_jtagaccel_interface;
142 #if BUILD_EP93XX == 1
143 extern jtag_interface_t ep93xx_interface;
146 #if BUILD_AT91RM9200 == 1
147 extern jtag_interface_t at91rm9200_interface;
150 #if BUILD_GW16012 == 1
151 extern jtag_interface_t gw16012_interface;
154 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
155 extern jtag_interface_t presto_interface;
158 #if BUILD_USBPROG == 1
159 extern jtag_interface_t usbprog_interface;
163 extern jtag_interface_t jlink_interface;
166 #if BUILD_VSLLINK == 1
167 extern jtag_interface_t vsllink_interface;
171 extern jtag_interface_t rlink_interface;
174 #if BUILD_ARMJTAGEW == 1
175 extern jtag_interface_t armjtagew_interface;
178 jtag_interface_t *jtag_interfaces[] = {
179 #if BUILD_ECOSBOARD == 1
182 #if BUILD_PARPORT == 1
188 #if BUILD_FT2232_FTD2XX == 1
191 #if BUILD_FT2232_LIBFTDI == 1
194 #if BUILD_AMTJTAGACCEL == 1
195 &amt_jtagaccel_interface,
197 #if BUILD_EP93XX == 1
200 #if BUILD_AT91RM9200 == 1
201 &at91rm9200_interface,
203 #if BUILD_GW16012 == 1
206 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
209 #if BUILD_USBPROG == 1
215 #if BUILD_VSLLINK == 1
221 #if BUILD_ARMJTAGEW == 1
222 &armjtagew_interface,
227 jtag_interface_t *jtag = NULL;
230 static jtag_interface_t *jtag_interface = NULL;
233 /* forward declarations */
234 //void jtag_add_pathmove(int num_states, tap_state_t *path);
235 //void jtag_add_runtest(int num_cycles, tap_state_t endstate);
236 //void jtag_add_end_state(tap_state_t endstate);
237 //void jtag_add_sleep(u32 us);
238 //int jtag_execute_queue(void);
239 static tap_state_t tap_state_by_name(const char *name);
242 static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
243 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
244 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
245 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
246 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
247 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
248 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
250 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
252 static int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
253 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
254 static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
255 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
256 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
257 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
259 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
261 jtag_tap_t *jtag_AllTaps(void)
263 return jtag_all_taps;
266 int jtag_NumTotalTaps(void)
280 int jtag_NumEnabledTaps(void)
296 jtag_tap_t *jtag_TapByString( const char *s )
304 if( 0 == strcmp( t->dotted_name, s ) ){
310 /* backup plan is by number */
312 /* ok - is "s" a number? */
314 n = strtol( s, &cp, 0 );
315 if( (s != cp) && (*cp == 0) ){
317 t = jtag_TapByAbsPosition(n);
323 jtag_tap_t * jtag_TapByJimObj( Jim_Interp *interp, Jim_Obj *o )
328 cp = Jim_GetString( o, NULL );
333 t = jtag_TapByString( cp );
336 Jim_SetResult_sprintf(interp,"Tap: %s is unknown", cp );
341 /* returns a pointer to the n-th device in the scan chain */
342 jtag_tap_t * jtag_TapByAbsPosition( int n )
350 while( t && (n > 0)) {
357 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
359 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
361 if (callback == NULL)
363 return ERROR_INVALID_ARGUMENTS;
368 while ((*callbacks_p)->next)
369 callbacks_p = &((*callbacks_p)->next);
370 callbacks_p = &((*callbacks_p)->next);
373 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
374 (*callbacks_p)->callback = callback;
375 (*callbacks_p)->priv = priv;
376 (*callbacks_p)->next = NULL;
381 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
383 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
385 if (callback == NULL)
387 return ERROR_INVALID_ARGUMENTS;
392 jtag_event_callback_t **next = &((*callbacks_p)->next);
393 if ((*callbacks_p)->callback == callback)
396 *callbacks_p = *next;
404 int jtag_call_event_callbacks(enum jtag_event event)
406 jtag_event_callback_t *callback = jtag_event_callbacks;
408 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
412 callback->callback(event, callback->priv);
413 callback = callback->next;
419 /* returns a pointer to the pointer of the last command in queue
420 * this may be a pointer to the root pointer (jtag_command_queue)
421 * or to the next member of the last but one command
423 jtag_command_t** jtag_get_last_command_p(void)
425 /* jtag_command_t *cmd = jtag_command_queue;
431 return &jtag_command_queue;
435 return last_comand_pointer;
438 void* cmd_queue_alloc(size_t size)
440 cmd_queue_page_t **p_page = &cmd_queue_pages;
446 * We align/round the *SIZE* per below
447 * so that all pointers returned by
448 * this function are reasonably well
451 * If we did not, then an "odd-length" request would cause the
452 * *next* allocation to be at an *odd* address, and because
453 * this function has the same type of api as malloc() - we
454 * must also return pointers that have the same type of
457 * What I do not/have is a reasonable portable means
460 * The solution here, is based on these suggestions.
461 * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
464 union worse_case_align {
470 #define ALIGN_SIZE (sizeof(union worse_case_align))
472 /* The alignment process. */
473 size = (size + ALIGN_SIZE -1) & (~(ALIGN_SIZE-1));
478 while ((*p_page)->next)
479 p_page = &((*p_page)->next);
480 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
481 p_page = &((*p_page)->next);
486 *p_page = malloc(sizeof(cmd_queue_page_t));
488 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
489 (*p_page)->next = NULL;
492 offset = (*p_page)->used;
493 (*p_page)->used += size;
495 t=(u8 *)((*p_page)->address);
499 void cmd_queue_free(void)
501 cmd_queue_page_t *page = cmd_queue_pages;
505 cmd_queue_page_t *last = page;
511 cmd_queue_pages = NULL;
514 static void jtag_prelude1(void)
518 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
519 jtag_error=ERROR_JTAG_TRST_ASSERTED;
523 if (cmd_queue_end_state == TAP_RESET)
524 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
527 static void jtag_prelude(tap_state_t state)
531 if (state != TAP_INVALID)
532 jtag_add_end_state(state);
534 cmd_queue_cur_state = cmd_queue_end_state;
537 void jtag_add_ir_scan_noverify(int num_fields, scan_field_t *fields, tap_state_t state)
542 retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
543 if (retval!=ERROR_OK)
549 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, tap_state_t state)
551 /* 8 x 32 bit id's is enough for all invoations */
555 /* if we are to run a verification of the ir scan, we need to get the input back.
556 * We may have to allocate space if the caller didn't ask for the input back.
559 if (jtag_verify_capture_ir)
562 for (j = 0; j < num_fields; j++)
565 if ((fields[j].in_value==NULL)&&(fields[j].num_bits<=32))
570 fields[j].in_value=(u8 *)(id+j);
573 LOG_DEBUG("caller must provide in_value space for verify_capture_ir to work");
579 jtag_add_ir_scan_noverify(num_fields, fields, state);
581 if (jtag_verify_capture_ir)
584 for (j = 0; j < num_fields; j++)
586 jtag_tap_t *tap=fields[j].tap;
587 if (fields[j].in_value!=NULL)
589 /* we verify max 32 bit long irlens. */
590 jtag_check_value_mask(fields+j, tap->expected, tap->expected_mask);
595 fields[j].in_value=NULL;
602 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, tap_state_t state)
604 jtag_command_t **last_cmd;
611 last_cmd = jtag_get_last_command_p();
613 /* allocate memory for a new list member */
614 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
615 (*last_cmd)->next = NULL;
616 last_comand_pointer = &((*last_cmd)->next);
617 (*last_cmd)->type = JTAG_SCAN;
619 /* allocate memory for ir scan command */
620 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
621 (*last_cmd)->cmd.scan->ir_scan = 1;
622 x = jtag_NumEnabledTaps();
623 (*last_cmd)->cmd.scan->num_fields = x; /* one field per device */
624 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(x * sizeof(scan_field_t));
625 (*last_cmd)->cmd.scan->end_state = state;
632 /* do this here so it is not forgotten */
633 tap = jtag_NextEnabledTap(tap);
639 assert(nth_tap < x );
641 scan_size = tap->ir_length;
642 (*last_cmd)->cmd.scan->fields[nth_tap].tap = tap;
643 (*last_cmd)->cmd.scan->fields[nth_tap].num_bits = scan_size;
644 (*last_cmd)->cmd.scan->fields[nth_tap].in_value = NULL; /* do not collect input for tap's in bypass */
646 /* search the list */
647 for (j = 0; j < num_fields; j++)
649 if (tap == fields[j].tap)
652 (*last_cmd)->cmd.scan->fields[nth_tap].in_value = fields[j].in_value;
653 (*last_cmd)->cmd.scan->fields[nth_tap].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
662 /* if a tap isn't listed, set it to BYPASS */
663 (*last_cmd)->cmd.scan->fields[nth_tap].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
667 /* update device information */
668 buf_cpy((*last_cmd)->cmd.scan->fields[nth_tap].out_value, tap->cur_instr, scan_size);
670 assert(nth_tap == (x-1));
675 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, tap_state_t state)
681 retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
682 if (retval!=ERROR_OK)
686 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, tap_state_t state)
689 jtag_command_t **last_cmd;
691 last_cmd = jtag_get_last_command_p();
693 /* allocate memory for a new list member */
694 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
695 (*last_cmd)->next = NULL;
696 last_comand_pointer = &((*last_cmd)->next);
697 (*last_cmd)->type = JTAG_SCAN;
699 /* allocate memory for ir scan command */
700 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
701 (*last_cmd)->cmd.scan->ir_scan = 1;
702 (*last_cmd)->cmd.scan->num_fields = num_fields;
703 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
704 (*last_cmd)->cmd.scan->end_state = state;
706 for( i = 0 ; i < num_fields ; i++ ){
707 int num_bits = fields[i].num_bits;
708 int num_bytes = CEIL(fields[i].num_bits, 8);
709 (*last_cmd)->cmd.scan->fields[i].tap = fields[i].tap;
710 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
711 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
712 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
717 void jtag_add_dr_scan(int num_fields, scan_field_t *fields, tap_state_t state)
723 retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
724 if (retval!=ERROR_OK)
728 void jtag_add_dr_scan_now(int num_fields, scan_field_t *fields, tap_state_t state)
730 jtag_add_dr_scan(num_fields, fields, state);
731 jtag_execute_queue_noclear();
734 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, tap_state_t state)
738 int bypass_devices = 0;
742 jtag_command_t **last_cmd = jtag_get_last_command_p();
745 /* count devices in bypass */
749 tap = jtag_NextEnabledTap(tap);
758 /* allocate memory for a new list member */
759 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
760 last_comand_pointer = &((*last_cmd)->next);
761 (*last_cmd)->next = NULL;
762 (*last_cmd)->type = JTAG_SCAN;
764 /* allocate memory for dr scan command */
765 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
766 (*last_cmd)->cmd.scan->ir_scan = 0;
767 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
768 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
769 (*last_cmd)->cmd.scan->end_state = state;
775 tap = jtag_NextEnabledTap(tap);
780 (*last_cmd)->cmd.scan->fields[field_count].tap = tap;
782 for (j = 0; j < num_fields; j++)
784 if (tap == fields[j].tap)
787 scan_size = fields[j].num_bits;
788 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
789 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
790 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
796 #ifdef _DEBUG_JTAG_IO_
797 /* if a device isn't listed, the BYPASS register should be selected */
800 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
804 /* program the scan field to 1 bit length, and ignore it's value */
805 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
806 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
807 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
812 #ifdef _DEBUG_JTAG_IO_
813 /* if a device is listed, the BYPASS register must not be selected */
816 LOG_ERROR("BUG: scan data for a device in BYPASS");
825 void MINIDRIVER(interface_jtag_add_dr_out)(jtag_tap_t *target_tap,
829 tap_state_t end_state)
834 int bypass_devices = 0;
836 jtag_command_t **last_cmd = jtag_get_last_command_p();
839 /* count devices in bypass */
843 tap = jtag_NextEnabledTap(tap);
852 /* allocate memory for a new list member */
853 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
854 last_comand_pointer = &((*last_cmd)->next);
855 (*last_cmd)->next = NULL;
856 (*last_cmd)->type = JTAG_SCAN;
858 /* allocate memory for dr scan command */
859 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
860 (*last_cmd)->cmd.scan->ir_scan = 0;
861 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
862 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
863 (*last_cmd)->cmd.scan->end_state = end_state;
868 tap = jtag_NextEnabledTap(tap);
873 (*last_cmd)->cmd.scan->fields[field_count].tap = tap;
875 if (tap == target_tap)
878 #ifdef _DEBUG_JTAG_IO_
879 /* if a device is listed, the BYPASS register must not be selected */
882 LOG_ERROR("BUG: scan data for a device in BYPASS");
886 for (j = 0; j < num_fields; j++)
889 scan_size = num_bits[j];
890 buf_set_u32(out_value, 0, scan_size, value[j]);
891 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
892 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
893 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
898 #ifdef _DEBUG_JTAG_IO_
899 /* if a device isn't listed, the BYPASS register should be selected */
902 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
906 /* program the scan field to 1 bit length, and ignore it's value */
907 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
908 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
909 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
915 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, tap_state_t state)
921 retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
922 if (retval!=ERROR_OK)
926 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, tap_state_t state)
929 jtag_command_t **last_cmd = jtag_get_last_command_p();
931 /* allocate memory for a new list member */
932 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
933 last_comand_pointer = &((*last_cmd)->next);
934 (*last_cmd)->next = NULL;
935 (*last_cmd)->type = JTAG_SCAN;
937 /* allocate memory for scan command */
938 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
939 (*last_cmd)->cmd.scan->ir_scan = 0;
940 (*last_cmd)->cmd.scan->num_fields = num_fields;
941 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
942 (*last_cmd)->cmd.scan->end_state = state;
944 for (i = 0; i < num_fields; i++)
946 int num_bits = fields[i].num_bits;
947 int num_bytes = CEIL(fields[i].num_bits, 8);
948 (*last_cmd)->cmd.scan->fields[i].tap = fields[i].tap;
949 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
950 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
951 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
957 void jtag_add_tlr(void)
959 jtag_prelude(TAP_RESET);
962 retval=interface_jtag_add_tlr();
963 if (retval!=ERROR_OK)
967 int MINIDRIVER(interface_jtag_add_tlr)(void)
969 tap_state_t state = TAP_RESET;
970 jtag_command_t **last_cmd = jtag_get_last_command_p();
972 /* allocate memory for a new list member */
973 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
974 last_comand_pointer = &((*last_cmd)->next);
975 (*last_cmd)->next = NULL;
976 (*last_cmd)->type = JTAG_STATEMOVE;
978 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
979 (*last_cmd)->cmd.statemove->end_state = state;
984 void jtag_add_pathmove(int num_states, tap_state_t *path)
986 tap_state_t cur_state = cmd_queue_cur_state;
990 /* the last state has to be a stable state */
991 if (!tap_is_state_stable(path[num_states - 1]))
993 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
997 for (i=0; i<num_states; i++)
999 if (path[i] == TAP_RESET)
1001 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
1005 if ( tap_state_transition(cur_state, true) != path[i]
1006 && tap_state_transition(cur_state, false) != path[i])
1008 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(cur_state), tap_state_name(path[i]));
1011 cur_state = path[i];
1016 retval = interface_jtag_add_pathmove(num_states, path);
1017 cmd_queue_cur_state = path[num_states - 1];
1018 if (retval!=ERROR_OK)
1022 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, tap_state_t *path)
1024 jtag_command_t **last_cmd = jtag_get_last_command_p();
1027 /* allocate memory for a new list member */
1028 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1029 last_comand_pointer = &((*last_cmd)->next);
1030 (*last_cmd)->next = NULL;
1031 (*last_cmd)->type = JTAG_PATHMOVE;
1033 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
1034 (*last_cmd)->cmd.pathmove->num_states = num_states;
1035 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(tap_state_t) * num_states);
1037 for (i = 0; i < num_states; i++)
1038 (*last_cmd)->cmd.pathmove->path[i] = path[i];
1043 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, tap_state_t state)
1045 jtag_command_t **last_cmd = jtag_get_last_command_p();
1047 /* allocate memory for a new list member */
1048 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1049 (*last_cmd)->next = NULL;
1050 last_comand_pointer = &((*last_cmd)->next);
1051 (*last_cmd)->type = JTAG_RUNTEST;
1053 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
1054 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
1055 (*last_cmd)->cmd.runtest->end_state = state;
1060 void jtag_add_runtest(int num_cycles, tap_state_t state)
1064 jtag_prelude(state);
1066 /* executed by sw or hw fifo */
1067 retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
1068 if (retval!=ERROR_OK)
1073 int MINIDRIVER(interface_jtag_add_clocks)( int num_cycles )
1075 jtag_command_t **last_cmd = jtag_get_last_command_p();
1077 /* allocate memory for a new list member */
1078 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1079 (*last_cmd)->next = NULL;
1080 last_comand_pointer = &((*last_cmd)->next);
1081 (*last_cmd)->type = JTAG_STABLECLOCKS;
1083 (*last_cmd)->cmd.stableclocks = cmd_queue_alloc(sizeof(stableclocks_command_t));
1084 (*last_cmd)->cmd.stableclocks->num_cycles = num_cycles;
1088 void jtag_add_clocks( int num_cycles )
1092 if( !tap_is_state_stable(cmd_queue_cur_state) )
1094 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
1095 tap_state_name(cmd_queue_cur_state) );
1096 jtag_error = ERROR_JTAG_NOT_STABLE_STATE;
1100 if( num_cycles > 0 )
1104 retval = interface_jtag_add_clocks(num_cycles);
1105 if (retval != ERROR_OK)
1110 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
1112 int trst_with_tlr = 0;
1115 /* FIX!!! there are *many* different cases here. A better
1116 * approach is needed for legal combinations of transitions...
1118 if ((jtag_reset_config & RESET_HAS_SRST)&&
1119 (jtag_reset_config & RESET_HAS_TRST)&&
1120 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
1122 if (((req_tlr_or_trst&&!jtag_trst)||
1123 (!req_tlr_or_trst&&jtag_trst))&&
1124 ((req_srst&&!jtag_srst)||
1125 (!req_srst&&jtag_srst)))
1127 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
1128 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
1132 /* Make sure that jtag_reset_config allows the requested reset */
1133 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
1134 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
1136 LOG_ERROR("BUG: requested reset would assert trst");
1137 jtag_error=ERROR_FAIL;
1141 /* if TRST pulls SRST, we reset with TAP T-L-R */
1142 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
1147 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
1149 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
1150 jtag_error=ERROR_FAIL;
1154 if (req_tlr_or_trst)
1156 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
1168 jtag_srst = req_srst;
1170 retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
1171 if (retval!=ERROR_OK)
1179 LOG_DEBUG("SRST line asserted");
1183 LOG_DEBUG("SRST line released");
1184 if (jtag_nsrst_delay)
1185 jtag_add_sleep(jtag_nsrst_delay * 1000);
1190 LOG_DEBUG("JTAG reset with RESET instead of TRST");
1191 jtag_add_end_state(TAP_RESET);
1193 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1199 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1200 * and inform possible listeners about this
1202 LOG_DEBUG("TRST line asserted");
1203 cmd_queue_cur_state = TAP_RESET;
1204 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1208 if (jtag_ntrst_delay)
1209 jtag_add_sleep(jtag_ntrst_delay * 1000);
1213 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1215 jtag_command_t **last_cmd = jtag_get_last_command_p();
1217 /* allocate memory for a new list member */
1218 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1219 (*last_cmd)->next = NULL;
1220 last_comand_pointer = &((*last_cmd)->next);
1221 (*last_cmd)->type = JTAG_RESET;
1223 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1224 (*last_cmd)->cmd.reset->trst = req_trst;
1225 (*last_cmd)->cmd.reset->srst = req_srst;
1230 void jtag_add_end_state(tap_state_t state)
1232 cmd_queue_end_state = state;
1233 if ((cmd_queue_end_state == TAP_DRSHIFT)||(cmd_queue_end_state == TAP_IRSHIFT))
1235 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
1239 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1241 jtag_command_t **last_cmd = jtag_get_last_command_p();
1243 /* allocate memory for a new list member */
1244 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1245 (*last_cmd)->next = NULL;
1246 last_comand_pointer = &((*last_cmd)->next);
1247 (*last_cmd)->type = JTAG_SLEEP;
1249 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1250 (*last_cmd)->cmd.sleep->us = us;
1255 void jtag_add_sleep(u32 us)
1257 keep_alive(); /* we might be running on a very slow JTAG clk */
1258 int retval=interface_jtag_add_sleep(us);
1259 if (retval!=ERROR_OK)
1264 int jtag_scan_size(scan_command_t *cmd)
1269 /* count bits in scan command */
1270 for (i = 0; i < cmd->num_fields; i++)
1272 bit_count += cmd->fields[i].num_bits;
1278 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1283 bit_count = jtag_scan_size(cmd);
1284 *buffer = malloc(CEIL(bit_count, 8));
1288 #ifdef _DEBUG_JTAG_IO_
1289 LOG_DEBUG("%s num_fields: %i", cmd->ir_scan ? "IRSCAN" : "DRSCAN", cmd->num_fields);
1292 for (i = 0; i < cmd->num_fields; i++)
1294 if (cmd->fields[i].out_value)
1296 #ifdef _DEBUG_JTAG_IO_
1297 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : cmd->fields[i].num_bits, 16);
1299 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1300 #ifdef _DEBUG_JTAG_IO_
1301 LOG_DEBUG("fields[%i].out_value[%i]: 0x%s", i, cmd->fields[i].num_bits, char_buf);
1307 #ifdef _DEBUG_JTAG_IO_
1308 LOG_DEBUG("fields[%i].out_value[%i]: NULL", i, cmd->fields[i].num_bits);
1312 bit_count += cmd->fields[i].num_bits;
1315 #ifdef _DEBUG_JTAG_IO_
1316 //LOG_DEBUG("bit_count totalling: %i", bit_count );
1322 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1328 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1331 for (i = 0; i < cmd->num_fields; i++)
1333 /* if neither in_value nor in_handler
1334 * are specified we don't have to examine this field
1336 if (cmd->fields[i].in_value)
1338 int num_bits = cmd->fields[i].num_bits;
1339 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1341 #ifdef _DEBUG_JTAG_IO_
1342 char *char_buf = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
1343 LOG_DEBUG("fields[%i].in_value[%i]: 0x%s", i, num_bits, char_buf);
1347 if (cmd->fields[i].in_value)
1349 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1354 bit_count += cmd->fields[i].num_bits;
1360 static const char *jtag_tap_name(jtag_tap_t *tap)
1362 return (tap == NULL) ? "(unknown)" : tap->dotted_name;
1365 int jtag_check_value_inner(u8 *captured, scan_field_t *field, u8 *in_check_value, u8 *in_check_mask)
1367 int retval = ERROR_OK;
1368 int num_bits = field->num_bits;
1370 int compare_failed = 0;
1373 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
1375 compare_failed = buf_cmp(captured, in_check_value, num_bits);
1377 if (compare_failed){
1378 /* An error handler could have caught the failing check
1379 * only report a problem when there wasn't a handler, or if the handler
1380 * acknowledged the error
1382 LOG_WARNING("TAP %s:",
1383 jtag_tap_name(field->tap));
1386 char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
1387 char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
1391 char *in_check_mask_char;
1392 in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
1393 LOG_WARNING("value captured during scan didn't pass the requested check:");
1394 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
1395 captured_char, in_check_value_char, in_check_mask_char);
1396 free(in_check_mask_char);
1400 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);
1403 free(captured_char);
1404 free(in_check_value_char);
1406 retval = ERROR_JTAG_QUEUE_FAILED;
1413 void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask)
1415 assert(field->in_value != NULL);
1419 /* no checking to do */
1423 jtag_execute_queue_noclear();
1425 int retval=jtag_check_value_inner(field->in_value, field, value, mask);
1426 jtag_set_error(retval);
1431 enum scan_type jtag_scan_type(scan_command_t *cmd)
1436 for (i = 0; i < cmd->num_fields; i++)
1438 if (cmd->fields[i].in_value)
1440 if (cmd->fields[i].out_value)
1448 #ifndef HAVE_JTAG_MINIDRIVER_H
1449 /* add callback to end of queue */
1450 void jtag_add_callback3(jtag_callback_t callback, u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2)
1452 struct jtag_callback_entry *entry=cmd_queue_alloc(sizeof(struct jtag_callback_entry));
1455 entry->callback=callback;
1460 if (jtag_callback_queue_head==NULL)
1462 jtag_callback_queue_head=entry;
1463 jtag_callback_queue_tail=entry;
1466 jtag_callback_queue_tail->next=entry;
1467 jtag_callback_queue_tail=entry;
1472 static int jtag_convert_to_callback3(u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2)
1474 ((jtag_callback1_t)data1)(in);
1478 void jtag_add_callback(jtag_callback1_t callback, u8 *in)
1480 jtag_add_callback3(jtag_convert_to_callback3, in, (jtag_callback_data_t)callback, 0);
1484 #ifndef HAVE_JTAG_MINIDRIVER_H
1486 int interface_jtag_execute_queue(void)
1492 LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
1496 retval = jtag->execute_queue();
1498 if (retval == ERROR_OK)
1500 struct jtag_callback_entry *entry;
1501 for (entry=jtag_callback_queue_head; entry!=NULL; entry=entry->next)
1503 retval=entry->callback(entry->in, entry->data1, entry->data2);
1504 if (retval!=ERROR_OK)
1511 jtag_callback_queue_head = NULL;
1512 jtag_callback_queue_tail = NULL;
1514 jtag_command_queue = NULL;
1515 last_comand_pointer = &jtag_command_queue;
1521 void jtag_execute_queue_noclear(void)
1523 /* each flush can take as much as 1-2ms on high bandwidth low latency interfaces.
1524 * E.g. a JTAG over TCP/IP or USB....
1526 jtag_flush_queue_count++;
1528 int retval=interface_jtag_execute_queue();
1529 /* we keep the first error */
1530 if ((jtag_error==ERROR_OK)&&(retval!=ERROR_OK))
1536 int jtag_execute_queue(void)
1539 jtag_execute_queue_noclear();
1541 jtag_error=ERROR_OK;
1545 int jtag_reset_callback(enum jtag_event event, void *priv)
1547 jtag_tap_t *tap = priv;
1551 if (event == JTAG_TRST_ASSERTED)
1553 buf_set_ones(tap->cur_instr, tap->ir_length);
1560 void jtag_sleep(u32 us)
1562 alive_sleep(us/1000);
1565 /* Try to examine chain layout according to IEEE 1149.1 §12
1567 int jtag_examine_chain(void)
1571 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1574 int device_count = 0;
1575 u8 zero_check = 0x0;
1576 u8 one_check = 0xff;
1579 field.num_bits = sizeof(idcode_buffer) * 8;
1580 field.out_value = idcode_buffer;
1582 field.in_value = idcode_buffer;
1587 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1589 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1592 jtag_add_plain_dr_scan(1, &field, TAP_RESET);
1593 jtag_execute_queue();
1595 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1597 zero_check |= idcode_buffer[i];
1598 one_check &= idcode_buffer[i];
1601 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1602 if ((zero_check == 0x00) || (one_check == 0xff))
1604 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1605 return ERROR_JTAG_INIT_FAILED;
1608 /* point at the 1st tap */
1609 tap = jtag_NextEnabledTap(NULL);
1611 LOG_ERROR("JTAG: No taps enabled?");
1612 return ERROR_JTAG_INIT_FAILED;
1615 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1617 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1618 if ((idcode & 1) == 0)
1620 /* LSB must not be 0, this indicates a device in bypass */
1621 LOG_WARNING("Tap/Device does not have IDCODE");
1632 /* some devices, such as AVR will output all 1's instead of TDI
1633 input value at end of chain. */
1634 if ((idcode == 0x000000FF)||(idcode == 0xFFFFFFFF))
1637 /* End of chain (invalid manufacturer ID)
1639 * The JTAG examine is the very first thing that happens
1641 * A single JTAG device requires only 64 bits to be read back correctly.
1643 * The code below adds a check that the rest of the data scanned (640 bits)
1644 * are all as expected. This helps diagnose/catch problems with the JTAG chain
1646 * earlier and gives more helpful/explicit error messages.
1648 for (bit_count += 32; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;bit_count += 32)
1650 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1651 if (unexpected||((idcode != 0x000000FF)&&(idcode != 0xFFFFFFFF)))
1653 LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count, idcode);
1661 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
1662 manufacturer = EXTRACT_MFG(idcode);
1663 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
1664 part = EXTRACT_PART(idcode);
1665 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
1666 version = EXTRACT_VER(idcode);
1668 LOG_INFO("JTAG tap: %s tap/device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1669 ((tap != NULL) ? (tap->dotted_name) : "(not-named)"),
1670 idcode, manufacturer, part, version);
1676 tap->idcode = idcode;
1678 if (tap->expected_ids_cnt > 0) {
1679 /* Loop over the expected identification codes and test for a match */
1681 for (ii = 0; ii < tap->expected_ids_cnt; ii++) {
1682 if( tap->idcode == tap->expected_ids[ii] ){
1687 /* If none of the expected ids matched, log an error */
1688 if (ii == tap->expected_ids_cnt) {
1689 LOG_ERROR("JTAG tap: %s got: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1692 EXTRACT_MFG( tap->idcode ),
1693 EXTRACT_PART( tap->idcode ),
1694 EXTRACT_VER( tap->idcode ) );
1695 for (ii = 0; ii < tap->expected_ids_cnt; ii++) {
1696 LOG_ERROR("JTAG tap: %s expected %hhu of %hhu: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1699 tap->expected_ids_cnt,
1700 tap->expected_ids[ii],
1701 EXTRACT_MFG( tap->expected_ids[ii] ),
1702 EXTRACT_PART( tap->expected_ids[ii] ),
1703 EXTRACT_VER( tap->expected_ids[ii] ) );
1706 return ERROR_JTAG_INIT_FAILED;
1708 LOG_INFO("JTAG Tap/device matched");
1712 LOG_INFO("JTAG TAP ID: 0x%08x - Unknown - please report (A) chipname and (B) idcode to the openocd project",
1716 tap = jtag_NextEnabledTap(tap);
1721 /* see if number of discovered devices matches configuration */
1722 if (device_count != jtag_NumEnabledTaps())
1724 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match (enabled) configuration (%i), total taps: %d",
1725 device_count, jtag_NumEnabledTaps(), jtag_NumTotalTaps());
1726 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1727 return ERROR_JTAG_INIT_FAILED;
1733 int jtag_validate_chain(void)
1736 int total_ir_length = 0;
1742 total_ir_length = 0;
1744 tap = jtag_NextEnabledTap(tap);
1748 total_ir_length += tap->ir_length;
1751 total_ir_length += 2;
1752 ir_test = malloc(CEIL(total_ir_length, 8));
1753 buf_set_ones(ir_test, total_ir_length);
1756 field.num_bits = total_ir_length;
1757 field.out_value = ir_test;
1758 field.in_value = ir_test;
1761 jtag_add_plain_ir_scan(1, &field, TAP_RESET);
1762 jtag_execute_queue();
1768 tap = jtag_NextEnabledTap(tap);
1773 val = buf_get_u32(ir_test, chain_pos, 2);
1776 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1777 LOG_ERROR("Could not validate JTAG scan chain, IR mismatch, scan returned 0x%s. tap=%s pos=%d expected 0x1 got %0x", cbuf, jtag_tap_name(tap), chain_pos, val);
1780 return ERROR_JTAG_INIT_FAILED;
1782 chain_pos += tap->ir_length;
1785 val = buf_get_u32(ir_test, chain_pos, 2);
1788 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1789 LOG_ERROR("Could not validate end of JTAG scan chain, IR mismatch, scan returned 0x%s. pos=%d expected 0x3 got %0x", cbuf, chain_pos, val);
1792 return ERROR_JTAG_INIT_FAILED;
1800 enum jtag_tap_cfg_param {
1804 static Jim_Nvp nvp_config_opts[] = {
1805 { .name = "-event", .value = JCFG_EVENT },
1807 { .name = NULL, .value = -1 }
1810 static int jtag_tap_configure_cmd( Jim_GetOptInfo *goi, jtag_tap_t * tap)
1816 /* parse config or cget options */
1817 while (goi->argc > 0) {
1818 Jim_SetEmptyResult (goi->interp);
1820 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
1822 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
1828 if (goi->argc == 0) {
1829 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ..." );
1833 e = Jim_GetOpt_Nvp( goi, nvp_jtag_tap_event, &n );
1835 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
1839 if (goi->isconfigure) {
1840 if (goi->argc != 1) {
1841 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
1845 if (goi->argc != 0) {
1846 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
1852 jtag_tap_event_action_t *jteap;
1854 jteap = tap->event_action;
1855 /* replace existing? */
1857 if (jteap->event == (enum jtag_tap_event)n->value) {
1860 jteap = jteap->next;
1863 if (goi->isconfigure) {
1864 if (jteap == NULL) {
1866 jteap = calloc(1, sizeof (*jteap));
1868 jteap->event = n->value;
1869 Jim_GetOpt_Obj( goi, &o);
1871 Jim_DecrRefCount(interp, jteap->body);
1873 jteap->body = Jim_DuplicateObj(goi->interp, o);
1874 Jim_IncrRefCount(jteap->body);
1876 /* add to head of event list */
1877 jteap->next = tap->event_action;
1878 tap->event_action = jteap;
1879 Jim_SetEmptyResult(goi->interp);
1882 if (jteap == NULL) {
1883 Jim_SetEmptyResult(goi->interp);
1885 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
1892 } /* while (goi->argc) */
1897 static int jim_newtap_cmd( Jim_GetOptInfo *goi )
1907 const Jim_Nvp opts[] = {
1908 #define NTAP_OPT_IRLEN 0
1909 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
1910 #define NTAP_OPT_IRMASK 1
1911 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
1912 #define NTAP_OPT_IRCAPTURE 2
1913 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
1914 #define NTAP_OPT_ENABLED 3
1915 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
1916 #define NTAP_OPT_DISABLED 4
1917 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
1918 #define NTAP_OPT_EXPECTED_ID 5
1919 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
1920 { .name = NULL , .value = -1 },
1923 pTap = malloc( sizeof(jtag_tap_t) );
1924 memset( pTap, 0, sizeof(*pTap) );
1926 Jim_SetResult_sprintf( goi->interp, "no memory");
1930 * we expect CHIP + TAP + OPTIONS
1932 if( goi->argc < 3 ){
1933 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
1936 Jim_GetOpt_String( goi, &cp, NULL );
1937 pTap->chip = strdup(cp);
1939 Jim_GetOpt_String( goi, &cp, NULL );
1940 pTap->tapname = strdup(cp);
1942 /* name + dot + name + null */
1943 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
1945 sprintf( cp, "%s.%s", pTap->chip, pTap->tapname );
1946 pTap->dotted_name = cp;
1948 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
1949 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
1951 /* default is enabled */
1954 /* deal with options */
1955 #define NTREQ_IRLEN 1
1956 #define NTREQ_IRCAPTURE 2
1957 #define NTREQ_IRMASK 4
1959 /* clear them as we find them */
1960 reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
1963 e = Jim_GetOpt_Nvp( goi, opts, &n );
1965 Jim_GetOpt_NvpUnknown( goi, opts, 0 );
1968 LOG_DEBUG("Processing option: %s", n->name );
1970 case NTAP_OPT_ENABLED:
1973 case NTAP_OPT_DISABLED:
1976 case NTAP_OPT_EXPECTED_ID:
1978 u32 *new_expected_ids;
1980 e = Jim_GetOpt_Wide( goi, &w );
1982 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
1986 new_expected_ids = malloc(sizeof(u32) * (pTap->expected_ids_cnt + 1));
1987 if (new_expected_ids == NULL) {
1988 Jim_SetResult_sprintf( goi->interp, "no memory");
1992 memcpy(new_expected_ids, pTap->expected_ids, sizeof(u32) * pTap->expected_ids_cnt);
1994 new_expected_ids[pTap->expected_ids_cnt] = w;
1996 free(pTap->expected_ids);
1997 pTap->expected_ids = new_expected_ids;
1998 pTap->expected_ids_cnt++;
2001 case NTAP_OPT_IRLEN:
2002 case NTAP_OPT_IRMASK:
2003 case NTAP_OPT_IRCAPTURE:
2004 e = Jim_GetOpt_Wide( goi, &w );
2006 Jim_SetResult_sprintf( goi->interp, "option: %s bad parameter", n->name );
2009 if( (w < 0) || (w > 0xffff) ){
2011 Jim_SetResult_sprintf( goi->interp, "option: %s - wacky value: %d (0x%x)",
2012 n->name, (int)(w), (int)(w));
2016 case NTAP_OPT_IRLEN:
2017 pTap->ir_length = w;
2018 reqbits &= (~(NTREQ_IRLEN));
2020 case NTAP_OPT_IRMASK:
2021 pTap->ir_capture_mask = w;
2022 reqbits &= (~(NTREQ_IRMASK));
2024 case NTAP_OPT_IRCAPTURE:
2025 pTap->ir_capture_value = w;
2026 reqbits &= (~(NTREQ_IRCAPTURE));
2029 } /* switch(n->value) */
2030 } /* while( goi->argc ) */
2032 /* Did we get all the options? */
2035 Jim_SetResult_sprintf( goi->interp,
2036 "newtap: %s missing required parameters",
2038 /* TODO: Tell user what is missing :-( */
2039 /* no memory leaks pelase */
2040 free(((void *)(pTap->expected_ids)));
2041 free(((void *)(pTap->chip)));
2042 free(((void *)(pTap->tapname)));
2043 free(((void *)(pTap->dotted_name)));
2044 free(((void *)(pTap)));
2048 pTap->expected = malloc( pTap->ir_length );
2049 pTap->expected_mask = malloc( pTap->ir_length );
2050 pTap->cur_instr = malloc( pTap->ir_length );
2052 buf_set_u32( pTap->expected,
2055 pTap->ir_capture_value );
2056 buf_set_u32( pTap->expected_mask,
2059 pTap->ir_capture_mask );
2060 buf_set_ones( pTap->cur_instr,
2065 jtag_register_event_callback(jtag_reset_callback, pTap );
2067 ppTap = &(jtag_all_taps);
2068 while( (*ppTap) != NULL ){
2069 ppTap = &((*ppTap)->next_tap);
2073 static int n_taps = 0;
2074 pTap->abs_chain_position = n_taps++;
2076 LOG_DEBUG( "Created Tap: %s @ abs position %d, irlen %d, capture: 0x%x mask: 0x%x",
2077 (*ppTap)->dotted_name,
2078 (*ppTap)->abs_chain_position,
2079 (*ppTap)->ir_length,
2080 (*ppTap)->ir_capture_value,
2081 (*ppTap)->ir_capture_mask );
2086 static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
2092 struct command_context_s *context;
2096 JTAG_CMD_INIT_RESET,
2099 JTAG_CMD_TAPDISABLE,
2100 JTAG_CMD_TAPISENABLED,
2105 const Jim_Nvp jtag_cmds[] = {
2106 { .name = "interface" , .value = JTAG_CMD_INTERFACE },
2107 { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
2108 { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
2109 { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
2110 { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
2111 { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
2112 { .name = "configure" , .value = JTAG_CMD_CONFIGURE },
2113 { .name = "cget" , .value = JTAG_CMD_CGET },
2115 { .name = NULL, .value = -1 },
2118 context = Jim_GetAssocData(interp, "context");
2119 /* go past the command */
2120 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
2122 e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
2124 Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
2127 Jim_SetEmptyResult( goi.interp );
2129 case JTAG_CMD_INTERFACE:
2130 /* return the name of the interface */
2131 /* TCL code might need to know the exact type... */
2132 /* FUTURE: we allow this as a means to "set" the interface. */
2133 if( goi.argc != 0 ){
2134 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
2137 Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
2139 case JTAG_CMD_INIT_RESET:
2140 if( goi.argc != 0 ){
2141 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
2144 e = jtag_init_reset(context);
2145 if( e != ERROR_OK ){
2146 Jim_SetResult_sprintf( goi.interp, "error: %d", e);
2150 case JTAG_CMD_NEWTAP:
2151 return jim_newtap_cmd( &goi );
2153 case JTAG_CMD_TAPISENABLED:
2154 case JTAG_CMD_TAPENABLE:
2155 case JTAG_CMD_TAPDISABLE:
2156 if( goi.argc != 1 ){
2157 Jim_SetResultString( goi.interp, "Too many parameters",-1 );
2163 t = jtag_TapByJimObj( goi.interp, goi.argv[0] );
2168 case JTAG_CMD_TAPISENABLED:
2171 case JTAG_CMD_TAPENABLE:
2172 jtag_tap_handle_event( t, JTAG_TAP_EVENT_ENABLE);
2176 case JTAG_CMD_TAPDISABLE:
2177 jtag_tap_handle_event( t, JTAG_TAP_EVENT_DISABLE);
2182 Jim_SetResult( goi.interp, Jim_NewIntObj( goi.interp, e ) );
2189 Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ...");
2196 Jim_GetOpt_Obj(&goi, &o);
2197 t = jtag_TapByJimObj( goi.interp, o );
2202 goi.isconfigure = 0;
2203 return jtag_tap_configure_cmd( &goi, t);
2207 case JTAG_CMD_CONFIGURE:
2209 Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ?VALUE? ...");
2216 Jim_GetOpt_Obj(&goi, &o);
2217 t = jtag_TapByJimObj( goi.interp, o );
2222 goi.isconfigure = 1;
2223 return jtag_tap_configure_cmd( &goi, t);
2230 int jtag_register_commands(struct command_context_s *cmd_ctx)
2232 register_jim( cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
2234 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
2235 COMMAND_CONFIG, "try to configure interface");
2236 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
2237 COMMAND_ANY, "set jtag speed (if supported)");
2238 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
2239 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
2240 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
2241 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
2242 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
2244 "[none/trst_only/srst_only/trst_and_srst] [srst_pulls_trst/trst_pulls_srst] [combined/separate] [trst_push_pull/trst_open_drain] [srst_push_pull/srst_open_drain]");
2245 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
2246 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
2247 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
2248 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
2250 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
2251 COMMAND_EXEC, "print current scan chain configuration");
2253 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
2254 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
2255 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
2256 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
2257 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
2258 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
2259 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
2260 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
2261 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
2262 register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count, "returns number of times the JTAG queue has been flushed");
2264 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
2265 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
2269 int jtag_interface_init(struct command_context_s *cmd_ctx)
2274 if (!jtag_interface)
2276 /* nothing was previously specified by "interface" command */
2277 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
2278 return ERROR_JTAG_INVALID_INTERFACE;
2282 jtag_interface->khz(speed_khz, &jtag_speed);
2286 if (jtag_interface->init() != ERROR_OK)
2287 return ERROR_JTAG_INIT_FAILED;
2289 jtag = jtag_interface;
2293 static int jtag_init_inner(struct command_context_s *cmd_ctx)
2298 LOG_DEBUG("Init JTAG chain");
2300 tap = jtag_NextEnabledTap(NULL);
2302 LOG_ERROR("There are no enabled taps?");
2303 return ERROR_JTAG_INIT_FAILED;
2307 if ((retval=jtag_execute_queue())!=ERROR_OK)
2310 /* examine chain first, as this could discover the real chain layout */
2311 if (jtag_examine_chain() != ERROR_OK)
2313 LOG_ERROR("trying to validate configured JTAG chain anyway...");
2316 if (jtag_validate_chain() != ERROR_OK)
2318 LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
2324 int jtag_init_reset(struct command_context_s *cmd_ctx)
2328 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
2331 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
2333 /* Reset can happen after a power cycle.
2335 * Ideally we would only assert TRST or run RESET before the target reset.
2337 * However w/srst_pulls_trst, trst is asserted together with the target
2338 * reset whether we want it or not.
2340 * NB! Some targets have JTAG circuitry disabled until a
2341 * trst & srst has been asserted.
2343 * NB! here we assume nsrst/ntrst delay are sufficient!
2345 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
2348 jtag_add_reset(1, 0); /* RESET or TRST */
2349 if (jtag_reset_config & RESET_HAS_SRST)
2351 jtag_add_reset(1, 1);
2352 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
2353 jtag_add_reset(0, 1);
2355 jtag_add_reset(0, 0);
2356 if ((retval = jtag_execute_queue()) != ERROR_OK)
2359 /* Check that we can communication on the JTAG chain + eventually we want to
2360 * be able to perform enumeration only after OpenOCD has started
2361 * telnet and GDB server
2363 * That would allow users to more easily perform any magic they need to before
2366 return jtag_init_inner(cmd_ctx);
2369 int jtag_init(struct command_context_s *cmd_ctx)
2372 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
2374 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
2378 return jtag_init_reset(cmd_ctx);
2381 static int default_khz(int khz, int *jtag_speed)
2383 LOG_ERROR("Translation from khz to jtag_speed not implemented");
2387 static int default_speed_div(int speed, int *khz)
2389 LOG_ERROR("Translation from jtag_speed to khz not implemented");
2393 static int default_power_dropout(int *dropout)
2395 *dropout=0; /* by default we can't detect power dropout */
2399 static int default_srst_asserted(int *srst_asserted)
2401 *srst_asserted=0; /* by default we can't detect srst asserted */
2405 static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2410 /* check whether the interface is already configured */
2413 LOG_WARNING("Interface already configured, ignoring");
2417 /* interface name is a mandatory argument */
2418 if (argc < 1 || args[0][0] == '\0')
2420 return ERROR_COMMAND_SYNTAX_ERROR;
2423 for (i=0; jtag_interfaces[i]; i++)
2425 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
2427 if ((retval = jtag_interfaces[i]->register_commands(cmd_ctx)) != ERROR_OK)
2432 jtag_interface = jtag_interfaces[i];
2434 if (jtag_interface->khz == NULL)
2436 jtag_interface->khz = default_khz;
2438 if (jtag_interface->speed_div == NULL)
2440 jtag_interface->speed_div = default_speed_div;
2442 if (jtag_interface->power_dropout == NULL)
2444 jtag_interface->power_dropout = default_power_dropout;
2446 if (jtag_interface->srst_asserted == NULL)
2448 jtag_interface->srst_asserted = default_srst_asserted;
2455 /* no valid interface was found (i.e. the configuration option,
2456 * didn't match one of the compiled-in interfaces
2458 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
2459 LOG_ERROR("compiled-in jtag interfaces:");
2460 for (i = 0; jtag_interfaces[i]; i++)
2462 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
2465 return ERROR_JTAG_INVALID_INTERFACE;
2468 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2472 Jim_Obj *newargs[ 10 ];
2475 * argv[-1] = command
2476 * argv[ 0] = ir length
2477 * argv[ 1] = ir capture
2478 * argv[ 2] = ir mask
2479 * argv[ 3] = not actually used by anything but in the docs
2483 command_print( cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
2486 command_print( cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
2487 command_print( cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
2491 command_print( cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundaryscan(len5)");
2492 command_print( cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
2493 command_print( cmd_ctx, "jtag newtap stm32 boundary ....., and the tap: \"stm32.boundary\"");
2494 command_print( cmd_ctx, "And then refer to the taps by the dotted name.");
2496 newargs[0] = Jim_NewStringObj( interp, "jtag", -1 );
2497 newargs[1] = Jim_NewStringObj( interp, "newtap", -1 );
2498 sprintf( buf, "chip%d", jtag_NumTotalTaps() );
2499 newargs[2] = Jim_NewStringObj( interp, buf, -1 );
2500 sprintf( buf, "tap%d", jtag_NumTotalTaps() );
2501 newargs[3] = Jim_NewStringObj( interp, buf, -1 );
2502 newargs[4] = Jim_NewStringObj( interp, "-irlen", -1 );
2503 newargs[5] = Jim_NewStringObj( interp, args[0], -1 );
2504 newargs[6] = Jim_NewStringObj( interp, "-ircapture", -1 );
2505 newargs[7] = Jim_NewStringObj( interp, args[1], -1 );
2506 newargs[8] = Jim_NewStringObj( interp, "-irmask", -1 );
2507 newargs[9] = Jim_NewStringObj( interp, args[2], -1 );
2509 command_print( cmd_ctx, "NEW COMMAND:");
2510 sprintf( buf, "%s %s %s %s %s %s %s %s %s %s",
2511 Jim_GetString( newargs[0], NULL ),
2512 Jim_GetString( newargs[1], NULL ),
2513 Jim_GetString( newargs[2], NULL ),
2514 Jim_GetString( newargs[3], NULL ),
2515 Jim_GetString( newargs[4], NULL ),
2516 Jim_GetString( newargs[5], NULL ),
2517 Jim_GetString( newargs[6], NULL ),
2518 Jim_GetString( newargs[7], NULL ),
2519 Jim_GetString( newargs[8], NULL ),
2520 Jim_GetString( newargs[9], NULL ) );
2522 e = jim_jtag_command( interp, 10, newargs );
2524 command_print( cmd_ctx, "%s", Jim_GetString( Jim_GetResult(interp), NULL ) );
2529 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2533 tap = jtag_all_taps;
2534 command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
2535 command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
2538 u32 expected, expected_mask, cur_instr, ii;
2539 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
2540 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
2541 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
2543 command_print(cmd_ctx,
2544 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
2545 tap->abs_chain_position,
2547 tap->enabled ? 'Y' : 'n',
2549 (tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
2555 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
2556 command_print(cmd_ctx, " | | | | 0x%08x | | | | ",
2557 tap->expected_ids[ii]);
2560 tap = tap->next_tap;
2566 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2569 return ERROR_COMMAND_SYNTAX_ERROR;
2573 if (strcmp(args[0], "none") == 0)
2574 jtag_reset_config = RESET_NONE;
2575 else if (strcmp(args[0], "trst_only") == 0)
2576 jtag_reset_config = RESET_HAS_TRST;
2577 else if (strcmp(args[0], "srst_only") == 0)
2578 jtag_reset_config = RESET_HAS_SRST;
2579 else if (strcmp(args[0], "trst_and_srst") == 0)
2580 jtag_reset_config = RESET_TRST_AND_SRST;
2583 LOG_ERROR("(1) invalid reset_config argument (%s), defaulting to none", args[0]);
2584 jtag_reset_config = RESET_NONE;
2585 return ERROR_INVALID_ARGUMENTS;
2591 if (strcmp(args[1], "separate") == 0)
2593 /* seperate reset lines - default */
2596 if (strcmp(args[1], "srst_pulls_trst") == 0)
2597 jtag_reset_config |= RESET_SRST_PULLS_TRST;
2598 else if (strcmp(args[1], "trst_pulls_srst") == 0)
2599 jtag_reset_config |= RESET_TRST_PULLS_SRST;
2600 else if (strcmp(args[1], "combined") == 0)
2601 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
2604 LOG_ERROR("(2) invalid reset_config argument (%s), defaulting to none", args[1]);
2605 jtag_reset_config = RESET_NONE;
2606 return ERROR_INVALID_ARGUMENTS;
2613 if (strcmp(args[2], "trst_open_drain") == 0)
2614 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
2615 else if (strcmp(args[2], "trst_push_pull") == 0)
2616 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
2619 LOG_ERROR("(3) invalid reset_config argument (%s) defaulting to none", args[2] );
2620 jtag_reset_config = RESET_NONE;
2621 return ERROR_INVALID_ARGUMENTS;
2627 if (strcmp(args[3], "srst_push_pull") == 0)
2628 jtag_reset_config |= RESET_SRST_PUSH_PULL;
2629 else if (strcmp(args[3], "srst_open_drain") == 0)
2630 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
2633 LOG_ERROR("(4) invalid reset_config argument (%s), defaulting to none", args[3]);
2634 jtag_reset_config = RESET_NONE;
2635 return ERROR_INVALID_ARGUMENTS;
2642 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2646 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
2651 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
2657 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2661 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
2666 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
2672 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2674 int retval=ERROR_OK;
2678 LOG_DEBUG("handle jtag speed");
2681 cur_speed = jtag_speed = strtoul(args[0], NULL, 0);
2683 /* this command can be called during CONFIG,
2684 * in which case jtag isn't initialized */
2687 retval=jtag->speed(cur_speed);
2689 } else if (argc == 0)
2693 return ERROR_COMMAND_SYNTAX_ERROR;
2695 command_print(cmd_ctx, "jtag_speed: %d", jtag_speed);
2700 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2702 int retval=ERROR_OK;
2703 LOG_DEBUG("handle jtag khz");
2707 speed_khz = strtoul(args[0], NULL, 0);
2711 LOG_DEBUG("have interface set up");
2713 if ((retval=jtag->khz(speed_khz, &speed_div1))!=ERROR_OK)
2719 cur_speed = jtag_speed = speed_div1;
2721 retval=jtag->speed(cur_speed);
2730 return ERROR_COMMAND_SYNTAX_ERROR;
2735 if ((retval=jtag->speed_div(jtag_speed, &speed_khz))!=ERROR_OK)
2741 command_print(cmd_ctx, "RCLK - adaptive");
2744 command_print(cmd_ctx, "%d kHz", speed_khz);
2750 static int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2756 return ERROR_COMMAND_SYNTAX_ERROR;
2760 state = tap_state_by_name( args[0] );
2762 command_print( cmd_ctx, "Invalid state name: %s\n", args[0] );
2763 return ERROR_COMMAND_SYNTAX_ERROR;
2765 jtag_add_end_state(state);
2766 jtag_execute_queue();
2768 command_print(cmd_ctx, "current endstate: %s", tap_state_name(cmd_queue_end_state));
2773 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2780 return ERROR_COMMAND_SYNTAX_ERROR;
2783 if (args[0][0] == '1')
2785 else if (args[0][0] == '0')
2789 return ERROR_COMMAND_SYNTAX_ERROR;
2792 if (args[1][0] == '1')
2794 else if (args[1][0] == '0')
2798 return ERROR_COMMAND_SYNTAX_ERROR;
2801 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
2802 return ERROR_JTAG_INIT_FAILED;
2804 jtag_add_reset(trst, srst);
2805 jtag_execute_queue();
2810 static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2814 return ERROR_COMMAND_SYNTAX_ERROR;
2817 jtag_add_runtest(strtol(args[0], NULL, 0), TAP_INVALID);
2818 jtag_execute_queue();
2824 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2827 scan_field_t *fields;
2829 tap_state_t endstate;
2831 if ((argc < 2) || (argc % 2))
2833 return ERROR_COMMAND_SYNTAX_ERROR;
2836 /* optional "-endstate" */
2838 /* at the end of the arguments. */
2840 endstate = TAP_INVALID;
2842 /* have at least one pair of numbers. */
2843 /* is last pair the magic text? */
2844 if( 0 == strcmp( "-endstate", args[ argc - 2 ] ) ){
2847 cpA = args[ argc-1 ];
2848 for( endstate = 0 ; endstate < TAP_NUM_STATES ; endstate++ ){
2849 cpS = tap_state_name( endstate );
2850 if( 0 == strcmp( cpA, cpS ) ){
2854 if( endstate >= TAP_NUM_STATES ){
2855 return ERROR_COMMAND_SYNTAX_ERROR;
2857 /* found - remove the last 2 args */
2863 fields = malloc(sizeof(scan_field_t) * argc / 2);
2865 for (i = 0; i < argc / 2; i++)
2867 tap = jtag_TapByString( args[i*2] );
2870 command_print( cmd_ctx, "Tap: %s unknown", args[i*2] );
2873 int field_size = tap->ir_length;
2874 fields[i].tap = tap;
2875 fields[i].out_value = malloc(CEIL(field_size, 8));
2876 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
2877 fields[i].in_value = NULL;
2880 jtag_add_ir_scan(argc / 2, fields, TAP_INVALID);
2881 /* did we have an endstate? */
2882 if (endstate != TAP_INVALID)
2883 jtag_add_end_state(endstate);
2885 jtag_execute_queue();
2887 for (i = 0; i < argc / 2; i++)
2888 free(fields[i].out_value);
2895 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
2898 scan_field_t *fields;
2900 int field_count = 0;
2903 tap_state_t endstate;
2906 * args[2] = num_bits
2907 * args[3] = hex string
2908 * ... repeat num bits and hex string ...
2911 * args[N-2] = "-endstate"
2912 * args[N-1] = statename
2914 if ((argc < 4) || ((argc % 2)!=0))
2916 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
2920 /* assume no endstate */
2921 endstate = TAP_INVALID;
2922 /* validate arguments as numbers */
2924 for (i = 2; i < argc; i+=2)
2929 e = Jim_GetLong(interp, args[i], &bits);
2930 /* If valid - try next arg */
2935 /* Not valid.. are we at the end? */
2936 if ( ((i+2) != argc) ){
2937 /* nope, then error */
2941 /* it could be: "-endstate FOO" */
2943 /* get arg as a string. */
2944 cp = Jim_GetString( args[i], NULL );
2945 /* is it the magic? */
2946 if( 0 == strcmp( "-endstate", cp ) ){
2947 /* is the statename valid? */
2948 cp = Jim_GetString( args[i+1], NULL );
2950 /* see if it is a valid state name */
2951 endstate = tap_state_by_name(cp);
2953 /* update the error message */
2954 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp );
2956 /* valid - so clear the error */
2958 /* and remove the last 2 args */
2963 /* Still an error? */
2965 return e; /* too bad */
2967 } /* validate args */
2969 tap = jtag_TapByJimObj( interp, args[1] );
2974 num_fields=(argc-2)/2;
2975 fields = malloc(sizeof(scan_field_t) * num_fields);
2976 for (i = 2; i < argc; i+=2)
2982 Jim_GetLong(interp, args[i], &bits);
2983 str = Jim_GetString(args[i+1], &len);
2985 fields[field_count].tap = tap;
2986 fields[field_count].num_bits = bits;
2987 fields[field_count].out_value = malloc(CEIL(bits, 8));
2988 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
2989 fields[field_count].in_value = fields[field_count].out_value;
2993 jtag_add_dr_scan(num_fields, fields, TAP_INVALID);
2994 /* did we get an end state? */
2995 if (endstate != TAP_INVALID)
2996 jtag_add_end_state(endstate);
2998 retval = jtag_execute_queue();
2999 if (retval != ERROR_OK)
3001 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
3006 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
3007 for (i = 2; i < argc; i+=2)
3012 Jim_GetLong(interp, args[i], &bits);
3013 str = buf_to_str(fields[field_count].in_value, bits, 16);
3014 free(fields[field_count].out_value);
3016 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
3021 Jim_SetResult(interp, list);
3029 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
3031 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_flush_queue_count));
3037 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3041 if (strcmp(args[0], "enable") == 0)
3043 jtag_verify_capture_ir = 1;
3045 else if (strcmp(args[0], "disable") == 0)
3047 jtag_verify_capture_ir = 0;
3050 return ERROR_COMMAND_SYNTAX_ERROR;
3052 } else if (argc != 0)
3054 return ERROR_COMMAND_SYNTAX_ERROR;
3057 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
3062 int jtag_power_dropout(int *dropout)
3064 return jtag->power_dropout(dropout);
3067 int jtag_srst_asserted(int *srst_asserted)
3069 return jtag->srst_asserted(srst_asserted);
3072 void jtag_tap_handle_event( jtag_tap_t * tap, enum jtag_tap_event e)
3074 jtag_tap_event_action_t * jteap;
3077 jteap = tap->event_action;
3081 if (jteap->event == e) {
3083 LOG_DEBUG( "JTAG tap: %s event: %d (%s) action: %s\n",
3086 Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
3087 Jim_GetString(jteap->body, NULL) );
3088 if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
3089 Jim_PrintErrorMessage(interp);
3093 jteap = jteap->next;
3097 LOG_DEBUG( "event %d %s - no action",
3099 Jim_Nvp_value2name_simple( nvp_jtag_tap_event, e)->name);
3103 /*-----<Cable Helper API>---------------------------------------*/
3105 /* these Cable Helper API functions are all documented in the jtag.h header file,
3106 using a Doxygen format. And since Doxygen's configuration file "Doxyfile",
3107 is setup to prefer its docs in the header file, no documentation is here, for
3108 if it were, it would have to be doubly maintained.
3112 * @see tap_set_state() and tap_get_state() accessors.
3113 * Actual name is not important since accessors hide it.
3115 static tap_state_t state_follower = TAP_RESET;
3117 void tap_set_state_impl( tap_state_t new_state )
3119 /* this is the state we think the TAPs are in now, was cur_state */
3120 state_follower = new_state;
3123 tap_state_t tap_get_state()
3125 return state_follower;
3129 * @see tap_set_end_state() and tap_get_end_state() accessors.
3130 * Actual name is not important because accessors hide it.
3132 static tap_state_t end_state_follower = TAP_RESET;
3134 void tap_set_end_state( tap_state_t new_end_state )
3136 /* this is the state we think the TAPs will be in at completion of the
3137 current TAP operation, was end_state
3139 end_state_follower = new_end_state;
3142 tap_state_t tap_get_end_state()
3144 return end_state_follower;
3148 int tap_move_ndx( tap_state_t astate )
3150 /* given a stable state, return the index into the tms_seqs[] array within tap_get_tms_path() */
3156 case TAP_RESET: ndx = 0; break;
3157 case TAP_DRSHIFT: ndx = 2; break;
3158 case TAP_DRPAUSE: ndx = 3; break;
3159 case TAP_IDLE: ndx = 1; break;
3160 case TAP_IRSHIFT: ndx = 4; break;
3161 case TAP_IRPAUSE: ndx = 5; break;
3163 LOG_ERROR( "fatal: unstable state \"%s\" used in tap_move_ndx()", tap_state_name(astate) );
3171 /* tap_move[i][j]: tap movement command to go from state i to state j
3172 * 0: Test-Logic-Reset
3179 * DRSHIFT->DRSHIFT and IRSHIFT->IRSHIFT have to be caught in interface specific code
3186 } tms_seqs[6][6] = /* [from_state_ndx][to_state_ndx] */
3188 /* value clocked to TMS to move from one of six stable states to another.
3189 * N.B. OOCD clocks TMS from LSB first, so read these right-to-left.
3190 * N.B. These values are tightly bound to the table in tap_get_tms_path_len().
3191 * N.B. Reset only needs to be 0b11111, but in JLink an even byte of 1's is more stable.
3192 * These extra ones cause no TAP state problem, because we go into reset and stay in reset.
3196 * These macros allow us to specify TMS state transitions by bits rather than hex bytes.
3197 * Read the bits from LSBit first to MSBit last (right-to-left).
3199 #define HEX__(n) 0x##n##LU
3202 (((x) & 0x0000000FLU)?(1<<0):0) \
3203 +(((x) & 0x000000F0LU)?(1<<1):0) \
3204 +(((x) & 0x00000F00LU)?(1<<2):0) \
3205 +(((x) & 0x0000F000LU)?(1<<3):0) \
3206 +(((x) & 0x000F0000LU)?(1<<4):0) \
3207 +(((x) & 0x00F00000LU)?(1<<5):0) \
3208 +(((x) & 0x0F000000LU)?(1<<6):0) \
3209 +(((x) & 0xF0000000LU)?(1<<7):0)
3211 #define B8(bits,count) { ((u8)B8__(HEX__(bits))), (count) }
3213 #if 0 && ((BUILD_FT2232_FTD2XX==1) || (BUILD_FT2232_LIBFTDI==1) || (BUILD_JLINK==1))
3214 /* this is the table submitted by Jeff Williams on 3/30/2009 with this comment:
3216 OK, I added Peter's version of the state table, and it works OK for
3217 me on MC1322x. I've recreated the jlink portion of patch with this
3218 new state table. His changes to my state table are pretty minor in
3219 terms of total transitions, but Peter feels that his version fixes
3220 some long-standing problems.
3223 I added the bit count into the table, reduced RESET column to 7 bits from 8.
3226 state specific comments:
3227 ------------------------
3228 *->RESET tried the 5 bit reset and it gave me problems, 7 bits seems to
3229 work better on ARM9 with ft2232 driver. (Dick)
3231 RESET->DRSHIFT add 1 extra clock cycles in the RESET state before advancing.
3232 needed on ARM9 with ft2232 driver. (Dick)
3234 RESET->IRSHIFT add 1 extra clock cycles in the RESET state before advancing.
3235 needed on ARM9 with ft2232 driver. (Dick)
3239 /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */ /* from state: */
3240 { B8(1111111,7), B8(0,1), B8(00101,5), B8(01010,5), B8(001101,6), B8(010110,6) }, /* RESET */
3241 { B8(1111111,7), B8(0,1), B8(001,3), B8(0101,4), B8(0011,4), B8(01011,5) }, /* IDLE */
3242 { B8(1111111,7), B8(011,3), B8(00111,5), B8(01,2), B8(001111,6), B8(0101111,7) }, /* DRSHIFT */
3243 { B8(1111111,7), B8(011,3), B8(01,2), B8(0,1), B8(001111,6), B8(0101111,7) }, /* DRPAUSE */
3244 { B8(1111111,7), B8(011,3), B8(00111,5), B8(010111,6), B8(001111,6), B8(01,2) }, /* IRSHIFT */
3245 { B8(1111111,7), B8(011,3), B8(00111,5), B8(010111,6), B8(01,2), B8(0,1) } /* IRPAUSE */
3247 #else /* this is the old table, converted from hex and with the bit_count set to 7 for each combo, like before */
3250 /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */ /* from state: */
3251 { B8(1111111,7), B8(0000000,7), B8(0010111,7), B8(0001010,7), B8(0011011,7), B8(0010110,7) }, /* RESET */
3252 { B8(1111111,7), B8(0000000,7), B8(0100101,7), B8(0000101,7), B8(0101011,7), B8(0001011,7) }, /* IDLE */
3253 { B8(1111111,7), B8(0110001,7), B8(0000000,7), B8(0000001,7), B8(0001111,7), B8(0101111,7) }, /* DRSHIFT */
3254 { B8(1111111,7), B8(0110000,7), B8(0100000,7), B8(0010111,7), B8(0011110,7), B8(0101111,7) }, /* DRPAUSE */
3255 { B8(1111111,7), B8(0110001,7), B8(0000111,7), B8(0010111,7), B8(0000000,7), B8(0000001,7) }, /* IRSHIFT */
3256 { B8(1111111,7), B8(0110000,7), B8(0011100,7), B8(0010111,7), B8(0100000,7), B8(0101111,7) } /* IRPAUSE */
3260 #if 0 /* keeping old hex stuff for awhile, for reference */
3261 /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */
3262 { 0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16 }, /* RESET */
3263 { 0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b }, /* IDLE */
3264 { 0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f }, /* DRSHIFT */
3265 { 0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f }, /* DRPAUSE */
3266 { 0x7f, 0x31, 0x07, 0x17, 0x00, 0x01 }, /* IRSHIFT */
3267 { 0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f } /* IRPAUSE */
3272 int tap_get_tms_path( tap_state_t from, tap_state_t to )
3274 return tms_seqs[tap_move_ndx(from)][tap_move_ndx(to)].bits;
3278 int tap_get_tms_path_len( tap_state_t from, tap_state_t to )
3280 return tms_seqs[tap_move_ndx(from)][tap_move_ndx(to)].bit_count;
3284 bool tap_is_state_stable(tap_state_t astate)
3288 /* A switch() is used because it is symbol dependent
3289 (not value dependent like an array), and can also check bounds.
3308 tap_state_t tap_state_transition(tap_state_t cur_state, bool tms)
3310 tap_state_t new_state;
3312 /* A switch is used because it is symbol dependent and not value dependent
3313 like an array. Also it can check for out of range conditions.
3321 new_state = cur_state;
3326 new_state = TAP_DRSELECT;
3329 new_state = TAP_IRSELECT;
3333 new_state = TAP_DREXIT1;
3337 new_state = TAP_DRUPDATE;
3340 new_state = TAP_DREXIT2;
3343 new_state = TAP_RESET;
3347 new_state = TAP_IREXIT1;
3351 new_state = TAP_IRUPDATE;
3354 new_state = TAP_IREXIT2;
3357 LOG_ERROR( "fatal: invalid argument cur_state=%d", cur_state );
3370 new_state = TAP_IDLE;
3373 new_state = TAP_DRCAPTURE;
3378 new_state = TAP_DRSHIFT;
3382 new_state = TAP_DRPAUSE;
3385 new_state = TAP_IRCAPTURE;
3390 new_state = TAP_IRSHIFT;
3394 new_state = TAP_IRPAUSE;
3397 LOG_ERROR( "fatal: invalid argument cur_state=%d", cur_state );
3406 const char* tap_state_name(tap_state_t state)
3412 case TAP_RESET: ret = "RESET"; break;
3413 case TAP_IDLE: ret = "RUN/IDLE"; break;
3414 case TAP_DRSELECT: ret = "DRSELECT"; break;
3415 case TAP_DRCAPTURE: ret = "DRCAPTURE"; break;
3416 case TAP_DRSHIFT: ret = "DRSHIFT"; break;
3417 case TAP_DREXIT1: ret = "DREXIT1"; break;
3418 case TAP_DRPAUSE: ret = "DRPAUSE"; break;
3419 case TAP_DREXIT2: ret = "DREXIT2"; break;
3420 case TAP_DRUPDATE: ret = "DRUPDATE"; break;
3421 case TAP_IRSELECT: ret = "IRSELECT"; break;
3422 case TAP_IRCAPTURE: ret = "IRCAPTURE"; break;
3423 case TAP_IRSHIFT: ret = "IRSHIFT"; break;
3424 case TAP_IREXIT1: ret = "IREXIT1"; break;
3425 case TAP_IRPAUSE: ret = "IRPAUSE"; break;
3426 case TAP_IREXIT2: ret = "IREXIT2"; break;
3427 case TAP_IRUPDATE: ret = "IRUPDATE"; break;
3428 default: ret = "???";
3434 static tap_state_t tap_state_by_name( const char *name )
3438 for( x = 0 ; x < TAP_NUM_STATES ; x++ ){
3439 /* be nice to the human */
3440 if( 0 == strcasecmp( name, tap_state_name(x) ) ){
3448 #ifdef _DEBUG_JTAG_IO_
3450 #define JTAG_DEBUG_STATE_APPEND(buf, len, bit) \
3451 do { buf[len] = bit ? '1' : '0'; } while(0)
3452 #define JTAG_DEBUG_STATE_PRINT(a, b, astr, bstr) \
3453 DEBUG_JTAG_IO("TAP/SM: %9s -> %5s\tTMS: %s\tTDI: %s", \
3454 tap_state_name(a), tap_state_name(b), astr, bstr)
3456 tap_state_t jtag_debug_state_machine(const void *tms_buf, const void *tdi_buf,
3457 unsigned tap_bits, tap_state_t next_state)
3459 const u8 *tms_buffer;
3460 const u8 *tdi_buffer;
3465 unsigned tap_out_bits;
3469 tap_state_t last_state;
3471 // set startstate (and possibly last, if tap_bits == 0)
3472 last_state = next_state;
3473 DEBUG_JTAG_IO("TAP/SM: START state: %s", tap_state_name(next_state));
3475 tms_buffer = (const u8 *)tms_buf;
3476 tdi_buffer = (const u8 *)tdi_buf;
3478 tap_bytes = TAP_SCAN_BYTES(tap_bits);
3479 DEBUG_JTAG_IO("TAP/SM: TMS bits: %u (bytes: %u)", tap_bits, tap_bytes);
3482 for(cur_byte = 0; cur_byte < tap_bytes; cur_byte++)
3484 for(cur_bit = 0; cur_bit < 8; cur_bit++)
3486 // make sure we do not run off the end of the buffers
3487 unsigned tap_bit = cur_byte * 8 + cur_bit;
3488 if (tap_bit == tap_bits)
3491 // check and save TMS bit
3492 tap_bit = !!(tms_buffer[cur_byte] & (1 << cur_bit));
3493 JTAG_DEBUG_STATE_APPEND(tms_str, tap_out_bits, tap_bit);
3495 // use TMS bit to find the next TAP state
3496 next_state = tap_state_transition(last_state, tap_bit);
3498 // check and store TDI bit
3499 tap_bit = !!(tdi_buffer[cur_byte] & (1 << cur_bit));
3500 JTAG_DEBUG_STATE_APPEND(tdi_str, tap_out_bits, tap_bit);
3502 // increment TAP bits
3505 // Only show TDO bits on state transitions, or
3506 // after some number of bits in the same state.
3507 if ((next_state == last_state) && (tap_out_bits < 32))
3510 // terminate strings and display state transition
3511 tms_str[tap_out_bits] = tdi_str[tap_out_bits] = 0;
3512 JTAG_DEBUG_STATE_PRINT(last_state, next_state, tms_str, tdi_str);
3515 last_state = next_state;
3522 // terminate strings and display state transition
3523 tms_str[tap_out_bits] = tdi_str[tap_out_bits] = 0;
3524 JTAG_DEBUG_STATE_PRINT(last_state, next_state, tms_str, tdi_str);
3527 DEBUG_JTAG_IO("TAP/SM: FINAL state: %s", tap_state_name(next_state));
3531 #endif // _DEBUG_JTAG_IO_
3533 /*-----</Cable Helper API>--------------------------------------*/