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 typedef struct cmd_queue_page_s
48 struct cmd_queue_page_s
*next
;
51 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
52 static cmd_queue_page_t
*cmd_queue_pages
= NULL
;
54 /* tap_move[i][j]: tap movement command to go from state i to state j
62 * DRSHIFT->DRSHIFT and IRSHIFT->IRSHIFT have to be caught in interface specific code
66 /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */
67 { 0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* RESET */
68 { 0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* IDLE */
69 { 0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* DRSHIFT */
70 { 0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* DRPAUSE */
71 { 0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* IRSHIFT */
72 { 0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* IRPAUSE */
75 int tap_move_map
[16] = {
76 0, -1, -1, 2, -1, 3, -1, -1,
77 1, -1, -1, 4, -1, 5, -1, -1
80 tap_transition_t tap_transitions
[16] =
82 {TAP_RESET
, TAP_IDLE
}, /* RESET */
83 {TAP_IRSELECT
, TAP_DRCAPTURE
}, /* DRSELECT */
84 {TAP_DREXIT1
, TAP_DRSHIFT
}, /* DRCAPTURE */
85 {TAP_DREXIT1
, TAP_DRSHIFT
}, /* DRSHIFT */
86 {TAP_DRUPDATE
, TAP_DRPAUSE
}, /* DREXIT1 */
87 {TAP_DREXIT2
, TAP_DRPAUSE
}, /* DRPAUSE */
88 {TAP_DRUPDATE
, TAP_DRSHIFT
}, /* DREXIT2 */
89 {TAP_DRSELECT
, TAP_IDLE
}, /* DRUPDATE */
90 {TAP_DRSELECT
, TAP_IDLE
}, /* IDLE */
91 {TAP_RESET
, TAP_IRCAPTURE
}, /* IRSELECT */
92 {TAP_IREXIT1
, TAP_IRSHIFT
}, /* IRCAPTURE */
93 {TAP_IREXIT1
, TAP_IRSHIFT
}, /* IRSHIFT */
94 {TAP_IRUPDATE
, TAP_IRPAUSE
}, /* IREXIT1 */
95 {TAP_IREXIT2
, TAP_IRPAUSE
}, /* IRPAUSE */
96 {TAP_IRUPDATE
, TAP_IRSHIFT
}, /* IREXIT2 */
97 {TAP_DRSELECT
, TAP_IDLE
} /* IRUPDATE */
100 char* jtag_event_strings
[] =
102 "JTAG controller reset (RESET or TRST)"
105 const Jim_Nvp nvp_jtag_tap_event
[] = {
106 { .value
= JTAG_TAP_EVENT_ENABLE
, .name
= "tap-enable" },
107 { .value
= JTAG_TAP_EVENT_DISABLE
, .name
= "tap-disable" },
109 { .name
= NULL
, .value
= -1 }
112 /* kludge!!!! these are just global variables that the
113 * interface use internally. They really belong
114 * inside the drivers, but we don't want to break
115 * linking the drivers!!!!
117 enum tap_state end_state
= TAP_RESET
;
118 enum tap_state cur_state
= TAP_RESET
;
122 jtag_command_t
*jtag_command_queue
= NULL
;
123 jtag_command_t
**last_comand_pointer
= &jtag_command_queue
;
124 static jtag_tap_t
*jtag_all_taps
= NULL
;
126 enum reset_types jtag_reset_config
= RESET_NONE
;
127 enum tap_state cmd_queue_end_state
= TAP_RESET
;
128 enum tap_state cmd_queue_cur_state
= TAP_RESET
;
130 int jtag_verify_capture_ir
= 1;
132 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
133 int jtag_nsrst_delay
= 0; /* default to no nSRST delay */
134 int jtag_ntrst_delay
= 0; /* default to no nTRST delay */
136 /* maximum number of JTAG devices expected in the chain
138 #define JTAG_MAX_CHAIN_SIZE 20
140 /* callbacks to inform high-level handlers about JTAG state changes */
141 jtag_event_callback_t
*jtag_event_callbacks
;
144 static int speed_khz
= 0;
145 /* flag if the kHz speed was defined */
146 static int hasKHz
= 0;
148 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
151 #if BUILD_ECOSBOARD == 1
152 extern jtag_interface_t zy1000_interface
;
155 #if BUILD_PARPORT == 1
156 extern jtag_interface_t parport_interface
;
160 extern jtag_interface_t dummy_interface
;
163 #if BUILD_FT2232_FTD2XX == 1
164 extern jtag_interface_t ft2232_interface
;
167 #if BUILD_FT2232_LIBFTDI == 1
168 extern jtag_interface_t ft2232_interface
;
171 #if BUILD_AMTJTAGACCEL == 1
172 extern jtag_interface_t amt_jtagaccel_interface
;
175 #if BUILD_EP93XX == 1
176 extern jtag_interface_t ep93xx_interface
;
179 #if BUILD_AT91RM9200 == 1
180 extern jtag_interface_t at91rm9200_interface
;
183 #if BUILD_GW16012 == 1
184 extern jtag_interface_t gw16012_interface
;
187 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
188 extern jtag_interface_t presto_interface
;
191 #if BUILD_USBPROG == 1
192 extern jtag_interface_t usbprog_interface
;
196 extern jtag_interface_t jlink_interface
;
199 jtag_interface_t
*jtag_interfaces
[] = {
200 #if BUILD_ECOSBOARD == 1
203 #if BUILD_PARPORT == 1
209 #if BUILD_FT2232_FTD2XX == 1
212 #if BUILD_FT2232_LIBFTDI == 1
215 #if BUILD_AMTJTAGACCEL == 1
216 &amt_jtagaccel_interface
,
218 #if BUILD_EP93XX == 1
221 #if BUILD_AT91RM9200 == 1
222 &at91rm9200_interface
,
224 #if BUILD_GW16012 == 1
227 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
230 #if BUILD_USBPROG == 1
239 jtag_interface_t
*jtag
= NULL
;
242 jtag_interface_t
*jtag_interface
= NULL
;
245 /* forward declarations */
246 void jtag_add_pathmove(int num_states
, enum tap_state
*path
);
247 void jtag_add_runtest(int num_cycles
, enum tap_state endstate
);
248 void jtag_add_end_state(enum tap_state endstate
);
249 void jtag_add_sleep(u32 us
);
250 int jtag_execute_queue(void);
254 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
255 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
256 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
257 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
258 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
259 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
260 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
262 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
264 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
265 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
266 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
267 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
268 int Jim_Command_drscan(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
270 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
272 jtag_tap_t
*jtag_AllTaps(void)
274 return jtag_all_taps
;
277 int jtag_NumTotalTaps(void)
291 int jtag_NumEnabledTaps(void)
308 jtag_tap_t
*jtag_TapByString( const char *s
)
316 if( 0 == strcmp( t
->dotted_name
, s
) ){
322 // backup plan is by number
324 /* ok - is "s" a number? */
326 n
= strtol( s
, &cp
, 0 );
327 if( (s
!= cp
) && (*cp
== 0) ){
329 t
= jtag_TapByAbsPosition(n
);
335 jtag_tap_t
* jtag_TapByJimObj( Jim_Interp
*interp
, Jim_Obj
*o
)
340 cp
= Jim_GetString( o
, NULL
);
345 t
= jtag_TapByString( cp
);
348 Jim_SetResult_sprintf(interp
,"Tap: %s is unknown", cp
);
353 /* returns a pointer to the n-th device in the scan chain */
354 jtag_tap_t
* jtag_TapByAbsPosition( int n
)
362 while( t
&& (n
> 0)) {
369 int jtag_register_event_callback(int (*callback
)(enum jtag_event event
, void *priv
), void *priv
)
371 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
373 if (callback
== NULL
)
375 return ERROR_INVALID_ARGUMENTS
;
380 while ((*callbacks_p
)->next
)
381 callbacks_p
= &((*callbacks_p
)->next
);
382 callbacks_p
= &((*callbacks_p
)->next
);
385 (*callbacks_p
) = malloc(sizeof(jtag_event_callback_t
));
386 (*callbacks_p
)->callback
= callback
;
387 (*callbacks_p
)->priv
= priv
;
388 (*callbacks_p
)->next
= NULL
;
393 int jtag_unregister_event_callback(int (*callback
)(enum jtag_event event
, void *priv
))
395 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
397 if (callback
== NULL
)
399 return ERROR_INVALID_ARGUMENTS
;
404 jtag_event_callback_t
**next
= &((*callbacks_p
)->next
);
405 if ((*callbacks_p
)->callback
== callback
)
408 *callbacks_p
= *next
;
416 int jtag_call_event_callbacks(enum jtag_event event
)
418 jtag_event_callback_t
*callback
= jtag_event_callbacks
;
420 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
424 callback
->callback(event
, callback
->priv
);
425 callback
= callback
->next
;
431 /* returns a pointer to the pointer of the last command in queue
432 * this may be a pointer to the root pointer (jtag_command_queue)
433 * or to the next member of the last but one command
435 jtag_command_t
** jtag_get_last_command_p(void)
437 /* jtag_command_t *cmd = jtag_command_queue;
443 return &jtag_command_queue;
447 return last_comand_pointer
;
450 void* cmd_queue_alloc(size_t size
)
452 cmd_queue_page_t
**p_page
= &cmd_queue_pages
;
458 * We align/round the *SIZE* per below
459 * so that all pointers returned by
460 * this function are reasonably well
463 * If we did not, then an "odd-length" request would cause the
464 * *next* allocation to be at an *odd* address, and because
465 * this function has the same type of api as malloc() - we
466 * must also return pointers that have the same type of
469 * What I do not/have is a reasonable portable means
472 * The solution here, is based on these suggestions.
473 * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
476 union worse_case_align
{
482 #define ALIGN_SIZE (sizeof(union worse_case_align))
484 /* The alignment process. */
485 size
= (size
+ ALIGN_SIZE
-1) & (~(ALIGN_SIZE
-1));
490 while ((*p_page
)->next
)
491 p_page
= &((*p_page
)->next
);
492 if (CMD_QUEUE_PAGE_SIZE
- (*p_page
)->used
< size
)
493 p_page
= &((*p_page
)->next
);
498 *p_page
= malloc(sizeof(cmd_queue_page_t
));
500 (*p_page
)->address
= malloc(CMD_QUEUE_PAGE_SIZE
);
501 (*p_page
)->next
= NULL
;
504 offset
= (*p_page
)->used
;
505 (*p_page
)->used
+= size
;
507 t
=(u8
*)((*p_page
)->address
);
511 void cmd_queue_free(void)
513 cmd_queue_page_t
*page
= cmd_queue_pages
;
517 cmd_queue_page_t
*last
= page
;
523 cmd_queue_pages
= NULL
;
526 static void jtag_prelude1(void)
530 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
531 jtag_error
=ERROR_JTAG_TRST_ASSERTED
;
535 if (cmd_queue_end_state
== TAP_RESET
)
536 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
539 static void jtag_prelude(enum tap_state state
)
544 jtag_add_end_state(state
);
546 cmd_queue_cur_state
= cmd_queue_end_state
;
549 void jtag_add_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
555 retval
=interface_jtag_add_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
556 if (retval
!=ERROR_OK
)
560 int MINIDRIVER(interface_jtag_add_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
562 jtag_command_t
**last_cmd
;
570 last_cmd
= jtag_get_last_command_p();
572 /* allocate memory for a new list member */
573 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
574 (*last_cmd
)->next
= NULL
;
575 last_comand_pointer
= &((*last_cmd
)->next
);
576 (*last_cmd
)->type
= JTAG_SCAN
;
578 /* allocate memory for ir scan command */
579 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
580 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
581 x
= jtag_NumEnabledTaps();
582 (*last_cmd
)->cmd
.scan
->num_fields
= x
; /* one field per device */
583 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(x
* sizeof(scan_field_t
));
584 (*last_cmd
)->cmd
.scan
->end_state
= state
;
591 // do this here so it is not forgotten
592 tap
= jtag_NextEnabledTap(tap
);
597 scan_size
= tap
->ir_length
;
598 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].tap
= tap
;
599 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].num_bits
= scan_size
;
600 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_value
= NULL
;
601 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_handler
= NULL
; /* disable verification by default */
603 /* search the list */
604 for (j
= 0; j
< num_fields
; j
++)
606 if (tap
== fields
[j
].tap
)
609 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
610 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
612 if (jtag_verify_capture_ir
)
614 if (fields
[j
].in_handler
==NULL
)
616 jtag_set_check_value((*last_cmd
)->cmd
.scan
->fields
+nth_tap
, tap
->expected
, tap
->expected_mask
, NULL
);
619 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_handler
= fields
[j
].in_handler
;
620 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_handler_priv
= fields
[j
].in_handler_priv
;
621 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_check_value
= tap
->expected
;
622 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_check_mask
= tap
->expected_mask
;
633 /* if a tap isn't listed, set it to BYPASS */
634 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_value
= buf_set_ones(cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
635 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_mask
= NULL
;
639 /* update device information */
640 buf_cpy((*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_value
, tap
->cur_instr
, scan_size
);
646 void jtag_add_plain_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
652 retval
=interface_jtag_add_plain_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
653 if (retval
!=ERROR_OK
)
657 int MINIDRIVER(interface_jtag_add_plain_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
660 jtag_command_t
**last_cmd
;
662 last_cmd
= jtag_get_last_command_p();
664 /* allocate memory for a new list member */
665 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
666 (*last_cmd
)->next
= NULL
;
667 last_comand_pointer
= &((*last_cmd
)->next
);
668 (*last_cmd
)->type
= JTAG_SCAN
;
670 /* allocate memory for ir scan command */
671 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
672 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
673 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
674 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
675 (*last_cmd
)->cmd
.scan
->end_state
= state
;
677 for( i
= 0 ; i
< num_fields
; i
++ ){
678 int num_bits
= fields
[i
].num_bits
;
679 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
680 (*last_cmd
)->cmd
.scan
->fields
[i
].tap
= fields
[i
].tap
;
681 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
682 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
683 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
684 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
685 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
686 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
687 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= NULL
;
688 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= NULL
;
693 void jtag_add_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
699 retval
=interface_jtag_add_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
700 if (retval
!=ERROR_OK
)
704 int MINIDRIVER(interface_jtag_add_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
708 int bypass_devices
= 0;
712 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
715 /* count devices in bypass */
719 tap
= jtag_NextEnabledTap(tap
);
728 /* allocate memory for a new list member */
729 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
730 last_comand_pointer
= &((*last_cmd
)->next
);
731 (*last_cmd
)->next
= NULL
;
732 (*last_cmd
)->type
= JTAG_SCAN
;
734 /* allocate memory for dr scan command */
735 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
736 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
737 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
738 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
739 (*last_cmd
)->cmd
.scan
->end_state
= state
;
745 tap
= jtag_NextEnabledTap(tap
);
750 (*last_cmd
)->cmd
.scan
->fields
[field_count
].tap
= tap
;
752 for (j
= 0; j
< num_fields
; j
++)
754 if (tap
== fields
[j
].tap
)
757 scan_size
= fields
[j
].num_bits
;
758 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
759 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
760 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
761 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= fields
[j
].in_value
;
762 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= fields
[j
].in_check_value
;
763 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= fields
[j
].in_check_mask
;
764 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= fields
[j
].in_handler
;
765 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= fields
[j
].in_handler_priv
;
770 #ifdef _DEBUG_JTAG_IO_
771 /* if a device isn't listed, the BYPASS register should be selected */
774 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
778 /* program the scan field to 1 bit length, and ignore it's value */
779 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
780 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
781 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
782 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
783 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
784 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
785 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
786 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
790 #ifdef _DEBUG_JTAG_IO_
791 /* if a device is listed, the BYPASS register must not be selected */
794 LOG_ERROR("BUG: scan data for a device in BYPASS");
803 void MINIDRIVER(interface_jtag_add_dr_out
)(jtag_tap_t
*target_tap
,
807 enum tap_state end_state
)
812 int bypass_devices
= 0;
814 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
817 /* count devices in bypass */
821 tap
= jtag_NextEnabledTap(tap
);
830 /* allocate memory for a new list member */
831 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
832 last_comand_pointer
= &((*last_cmd
)->next
);
833 (*last_cmd
)->next
= NULL
;
834 (*last_cmd
)->type
= JTAG_SCAN
;
836 /* allocate memory for dr scan command */
837 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
838 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
839 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
840 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
841 (*last_cmd
)->cmd
.scan
->end_state
= end_state
;
846 tap
= jtag_NextEnabledTap(tap
);
851 (*last_cmd
)->cmd
.scan
->fields
[field_count
].tap
= tap
;
853 if (tap
== target_tap
)
856 #ifdef _DEBUG_JTAG_IO_
857 /* if a device is listed, the BYPASS register must not be selected */
860 LOG_ERROR("BUG: scan data for a device in BYPASS");
864 for (j
= 0; j
< num_fields
; j
++)
867 scan_size
= num_bits
[j
];
868 buf_set_u32(out_value
, 0, scan_size
, value
[j
]);
869 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
870 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
871 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
872 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
873 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
874 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
875 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
876 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
880 #ifdef _DEBUG_JTAG_IO_
881 /* if a device isn't listed, the BYPASS register should be selected */
884 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
888 /* program the scan field to 1 bit length, and ignore it's value */
889 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
890 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
891 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
892 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
893 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
894 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
895 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
896 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
901 void jtag_add_plain_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
907 retval
=interface_jtag_add_plain_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
908 if (retval
!=ERROR_OK
)
912 int MINIDRIVER(interface_jtag_add_plain_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
915 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
917 /* allocate memory for a new list member */
918 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
919 last_comand_pointer
= &((*last_cmd
)->next
);
920 (*last_cmd
)->next
= NULL
;
921 (*last_cmd
)->type
= JTAG_SCAN
;
923 /* allocate memory for scan command */
924 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
925 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
926 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
927 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
928 (*last_cmd
)->cmd
.scan
->end_state
= state
;
930 for (i
= 0; i
< num_fields
; i
++)
932 int num_bits
= fields
[i
].num_bits
;
933 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
934 (*last_cmd
)->cmd
.scan
->fields
[i
].tap
= fields
[i
].tap
;
935 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
936 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
937 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
938 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
939 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
940 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
941 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= fields
[i
].in_handler
;
942 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= fields
[i
].in_handler_priv
;
948 void jtag_add_tlr(void)
950 jtag_prelude(TAP_RESET
);
953 retval
=interface_jtag_add_tlr();
954 if (retval
!=ERROR_OK
)
958 int MINIDRIVER(interface_jtag_add_tlr
)()
960 enum tap_state state
= TAP_RESET
;
961 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
963 /* allocate memory for a new list member */
964 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
965 last_comand_pointer
= &((*last_cmd
)->next
);
966 (*last_cmd
)->next
= NULL
;
967 (*last_cmd
)->type
= JTAG_STATEMOVE
;
969 (*last_cmd
)->cmd
.statemove
= cmd_queue_alloc(sizeof(statemove_command_t
));
970 (*last_cmd
)->cmd
.statemove
->end_state
= state
;
976 void jtag_add_pathmove(int num_states
, enum tap_state
*path
)
978 enum tap_state cur_state
=cmd_queue_cur_state
;
982 /* the last state has to be a stable state */
983 if (tap_move_map
[path
[num_states
- 1]] == -1)
985 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
989 for (i
=0; i
<num_states
; i
++)
991 if (path
[i
] == TAP_RESET
)
993 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
996 if ((tap_transitions
[cur_state
].low
!= path
[i
])&&
997 (tap_transitions
[cur_state
].high
!= path
[i
]))
999 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", jtag_state_name(cur_state
), jtag_state_name(path
[i
]));
1002 cur_state
= path
[i
];
1007 retval
=interface_jtag_add_pathmove(num_states
, path
);
1008 cmd_queue_cur_state
= path
[num_states
- 1];
1009 if (retval
!=ERROR_OK
)
1013 int MINIDRIVER(interface_jtag_add_pathmove
)(int num_states
, enum tap_state
*path
)
1015 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1018 /* allocate memory for a new list member */
1019 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1020 last_comand_pointer
= &((*last_cmd
)->next
);
1021 (*last_cmd
)->next
= NULL
;
1022 (*last_cmd
)->type
= JTAG_PATHMOVE
;
1024 (*last_cmd
)->cmd
.pathmove
= cmd_queue_alloc(sizeof(pathmove_command_t
));
1025 (*last_cmd
)->cmd
.pathmove
->num_states
= num_states
;
1026 (*last_cmd
)->cmd
.pathmove
->path
= cmd_queue_alloc(sizeof(enum tap_state
) * num_states
);
1028 for (i
= 0; i
< num_states
; i
++)
1029 (*last_cmd
)->cmd
.pathmove
->path
[i
] = path
[i
];
1034 int MINIDRIVER(interface_jtag_add_runtest
)(int num_cycles
, enum tap_state state
)
1036 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1038 /* allocate memory for a new list member */
1039 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1040 (*last_cmd
)->next
= NULL
;
1041 last_comand_pointer
= &((*last_cmd
)->next
);
1042 (*last_cmd
)->type
= JTAG_RUNTEST
;
1044 (*last_cmd
)->cmd
.runtest
= cmd_queue_alloc(sizeof(runtest_command_t
));
1045 (*last_cmd
)->cmd
.runtest
->num_cycles
= num_cycles
;
1046 (*last_cmd
)->cmd
.runtest
->end_state
= state
;
1051 void jtag_add_runtest(int num_cycles
, enum tap_state state
)
1055 jtag_prelude(state
);
1057 /* executed by sw or hw fifo */
1058 retval
=interface_jtag_add_runtest(num_cycles
, cmd_queue_end_state
);
1059 if (retval
!=ERROR_OK
)
1063 void jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
1065 int trst_with_tlr
= 0;
1068 /* FIX!!! there are *many* different cases here. A better
1069 * approach is needed for legal combinations of transitions...
1071 if ((jtag_reset_config
& RESET_HAS_SRST
)&&
1072 (jtag_reset_config
& RESET_HAS_TRST
)&&
1073 ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0))
1075 if (((req_tlr_or_trst
&&!jtag_trst
)||
1076 (!req_tlr_or_trst
&&jtag_trst
))&&
1077 ((req_srst
&&!jtag_srst
)||
1078 (!req_srst
&&jtag_srst
)))
1080 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
1081 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
1085 /* Make sure that jtag_reset_config allows the requested reset */
1086 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
1087 if (((jtag_reset_config
& RESET_SRST_PULLS_TRST
) && (req_srst
== 1)) && (!req_tlr_or_trst
))
1089 LOG_ERROR("BUG: requested reset would assert trst");
1090 jtag_error
=ERROR_FAIL
;
1094 /* if TRST pulls SRST, we reset with TAP T-L-R */
1095 if (((jtag_reset_config
& RESET_TRST_PULLS_SRST
) && (req_tlr_or_trst
)) && (req_srst
== 0))
1100 if (req_srst
&& !(jtag_reset_config
& RESET_HAS_SRST
))
1102 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
1103 jtag_error
=ERROR_FAIL
;
1107 if (req_tlr_or_trst
)
1109 if (!trst_with_tlr
&& (jtag_reset_config
& RESET_HAS_TRST
))
1121 jtag_srst
= req_srst
;
1123 retval
= interface_jtag_add_reset(jtag_trst
, jtag_srst
);
1124 if (retval
!=ERROR_OK
)
1132 LOG_DEBUG("SRST line asserted");
1136 LOG_DEBUG("SRST line released");
1137 if (jtag_nsrst_delay
)
1138 jtag_add_sleep(jtag_nsrst_delay
* 1000);
1143 LOG_DEBUG("JTAG reset with RESET instead of TRST");
1144 jtag_add_end_state(TAP_RESET
);
1146 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
1152 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1153 * and inform possible listeners about this
1155 LOG_DEBUG("TRST line asserted");
1156 cmd_queue_cur_state
= TAP_RESET
;
1157 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
1161 if (jtag_ntrst_delay
)
1162 jtag_add_sleep(jtag_ntrst_delay
* 1000);
1166 int MINIDRIVER(interface_jtag_add_reset
)(int req_trst
, int req_srst
)
1168 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1170 /* allocate memory for a new list member */
1171 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1172 (*last_cmd
)->next
= NULL
;
1173 last_comand_pointer
= &((*last_cmd
)->next
);
1174 (*last_cmd
)->type
= JTAG_RESET
;
1176 (*last_cmd
)->cmd
.reset
= cmd_queue_alloc(sizeof(reset_command_t
));
1177 (*last_cmd
)->cmd
.reset
->trst
= req_trst
;
1178 (*last_cmd
)->cmd
.reset
->srst
= req_srst
;
1183 void jtag_add_end_state(enum tap_state state
)
1185 cmd_queue_end_state
= state
;
1186 if ((cmd_queue_end_state
== TAP_DRSHIFT
)||(cmd_queue_end_state
== TAP_IRSHIFT
))
1188 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
1192 int MINIDRIVER(interface_jtag_add_sleep
)(u32 us
)
1194 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1196 /* allocate memory for a new list member */
1197 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1198 (*last_cmd
)->next
= NULL
;
1199 last_comand_pointer
= &((*last_cmd
)->next
);
1200 (*last_cmd
)->type
= JTAG_SLEEP
;
1202 (*last_cmd
)->cmd
.sleep
= cmd_queue_alloc(sizeof(sleep_command_t
));
1203 (*last_cmd
)->cmd
.sleep
->us
= us
;
1208 void jtag_add_sleep(u32 us
)
1210 keep_alive(); /* we might be running on a very slow JTAG clk */
1211 int retval
=interface_jtag_add_sleep(us
);
1212 if (retval
!=ERROR_OK
)
1217 int jtag_scan_size(scan_command_t
*cmd
)
1222 /* count bits in scan command */
1223 for (i
= 0; i
< cmd
->num_fields
; i
++)
1225 bit_count
+= cmd
->fields
[i
].num_bits
;
1231 int jtag_build_buffer(scan_command_t
*cmd
, u8
**buffer
)
1236 bit_count
= jtag_scan_size(cmd
);
1237 *buffer
= malloc(CEIL(bit_count
, 8));
1241 for (i
= 0; i
< cmd
->num_fields
; i
++)
1243 if (cmd
->fields
[i
].out_value
)
1245 #ifdef _DEBUG_JTAG_IO_
1246 char* char_buf
= buf_to_str(cmd
->fields
[i
].out_value
, (cmd
->fields
[i
].num_bits
> 64) ? 64 : cmd
->fields
[i
].num_bits
, 16);
1248 buf_set_buf(cmd
->fields
[i
].out_value
, 0, *buffer
, bit_count
, cmd
->fields
[i
].num_bits
);
1249 #ifdef _DEBUG_JTAG_IO_
1250 LOG_DEBUG("fields[%i].out_value: 0x%s", i
, char_buf
);
1255 bit_count
+= cmd
->fields
[i
].num_bits
;
1261 int jtag_read_buffer(u8
*buffer
, scan_command_t
*cmd
)
1267 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1270 for (i
= 0; i
< cmd
->num_fields
; i
++)
1272 /* if neither in_value nor in_handler
1273 * are specified we don't have to examine this field
1275 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1277 int num_bits
= cmd
->fields
[i
].num_bits
;
1278 u8
*captured
= buf_set_buf(buffer
, bit_count
, malloc(CEIL(num_bits
, 8)), 0, num_bits
);
1280 #ifdef _DEBUG_JTAG_IO_
1283 char_buf
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1284 LOG_DEBUG("fields[%i].in_value: 0x%s", i
, char_buf
);
1288 if (cmd
->fields
[i
].in_value
)
1290 buf_cpy(captured
, cmd
->fields
[i
].in_value
, num_bits
);
1292 if (cmd
->fields
[i
].in_handler
)
1294 if (cmd
->fields
[i
].in_handler(cmd
->fields
[i
].in_value
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1296 LOG_WARNING("in_handler reported a failed check");
1297 retval
= ERROR_JTAG_QUEUE_FAILED
;
1302 /* no in_value specified, but a handler takes care of the scanned data */
1303 if (cmd
->fields
[i
].in_handler
&& (!cmd
->fields
[i
].in_value
))
1305 if (cmd
->fields
[i
].in_handler(captured
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1307 /* We're going to call the error:handler later, but if the in_handler
1308 * reported an error we report this failure upstream
1310 LOG_WARNING("in_handler reported a failed check");
1311 retval
= ERROR_JTAG_QUEUE_FAILED
;
1317 bit_count
+= cmd
->fields
[i
].num_bits
;
1323 int jtag_check_value(u8
*captured
, void *priv
, scan_field_t
*field
)
1325 int retval
= ERROR_OK
;
1326 int num_bits
= field
->num_bits
;
1328 int compare_failed
= 0;
1330 if (field
->in_check_mask
)
1331 compare_failed
= buf_cmp_mask(captured
, field
->in_check_value
, field
->in_check_mask
, num_bits
);
1333 compare_failed
= buf_cmp(captured
, field
->in_check_value
, num_bits
);
1335 if (compare_failed
){
1336 /* An error handler could have caught the failing check
1337 * only report a problem when there wasn't a handler, or if the handler
1338 * acknowledged the error
1340 LOG_WARNING("TAP %s:",
1341 (field
->tap
== NULL
) ? "(unknown)" : field
->tap
->dotted_name
);
1344 char *captured_char
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1345 char *in_check_value_char
= buf_to_str(field
->in_check_value
, (num_bits
> 64) ? 64 : num_bits
, 16);
1347 if (field
->in_check_mask
)
1349 char *in_check_mask_char
;
1350 in_check_mask_char
= buf_to_str(field
->in_check_mask
, (num_bits
> 64) ? 64 : num_bits
, 16);
1351 LOG_WARNING("value captured during scan didn't pass the requested check:");
1352 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
1353 captured_char
, in_check_value_char
, in_check_mask_char
);
1354 free(in_check_mask_char
);
1358 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
);
1361 free(captured_char
);
1362 free(in_check_value_char
);
1364 retval
= ERROR_JTAG_QUEUE_FAILED
;
1372 set up checking of this field using the in_handler. The values passed in must be valid until
1373 after jtag_execute() has completed.
1375 void jtag_set_check_value(scan_field_t
*field
, u8
*value
, u8
*mask
, error_handler_t
*in_error_handler
)
1378 field
->in_handler
= jtag_check_value
;
1380 field
->in_handler
= NULL
; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1381 field
->in_handler_priv
= NULL
;
1382 field
->in_check_value
= value
;
1383 field
->in_check_mask
= mask
;
1386 enum scan_type
jtag_scan_type(scan_command_t
*cmd
)
1391 for (i
= 0; i
< cmd
->num_fields
; i
++)
1393 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1395 if (cmd
->fields
[i
].out_value
)
1402 int MINIDRIVER(interface_jtag_execute_queue
)(void)
1408 LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
1412 retval
= jtag
->execute_queue();
1416 jtag_command_queue
= NULL
;
1417 last_comand_pointer
= &jtag_command_queue
;
1422 int jtag_execute_queue(void)
1424 int retval
=interface_jtag_execute_queue();
1425 if (retval
==ERROR_OK
)
1429 jtag_error
=ERROR_OK
;
1433 int jtag_reset_callback(enum jtag_event event
, void *priv
)
1435 jtag_tap_t
*tap
= priv
;
1439 if (event
== JTAG_TRST_ASSERTED
)
1441 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
1448 void jtag_sleep(u32 us
)
1450 alive_sleep(us
/1000);
1453 /* Try to examine chain layout according to IEEE 1149.1 §12
1455 int jtag_examine_chain(void)
1459 u8 idcode_buffer
[JTAG_MAX_CHAIN_SIZE
* 4];
1462 int device_count
= 0;
1463 u8 zero_check
= 0x0;
1464 u8 one_check
= 0xff;
1467 field
.num_bits
= sizeof(idcode_buffer
) * 8;
1468 field
.out_value
= idcode_buffer
;
1469 field
.out_mask
= NULL
;
1470 field
.in_value
= idcode_buffer
;
1471 field
.in_check_value
= NULL
;
1472 field
.in_check_mask
= NULL
;
1473 field
.in_handler
= NULL
;
1474 field
.in_handler_priv
= NULL
;
1476 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
; i
++)
1478 buf_set_u32(idcode_buffer
, i
* 32, 32, 0x000000FF);
1481 jtag_add_plain_dr_scan(1, &field
, TAP_RESET
);
1482 jtag_execute_queue();
1484 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
* 4; i
++)
1486 zero_check
|= idcode_buffer
[i
];
1487 one_check
&= idcode_buffer
[i
];
1490 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1491 if ((zero_check
== 0x00) || (one_check
== 0xff))
1493 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1494 return ERROR_JTAG_INIT_FAILED
;
1497 // point at the 1st tap
1498 tap
= jtag_NextEnabledTap(NULL
);
1500 LOG_ERROR("JTAG: No taps enabled?");
1501 return ERROR_JTAG_INIT_FAILED
;
1504 for (bit_count
= 0; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;)
1506 u32 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1507 if ((idcode
& 1) == 0)
1509 /* LSB must not be 0, this indicates a device in bypass */
1510 LOG_WARNING("Tap/Device does not have IDCODE");
1521 if (idcode
== 0x000000FF)
1524 /* End of chain (invalid manufacturer ID)
1526 * The JTAG examine is the very first thing that happens
1528 * A single JTAG device requires only 64 bits to be read back correctly.
1530 * The code below adds a check that the rest of the data scanned (640 bits)
1531 * are all as expected. This helps diagnose/catch problems with the JTAG chain
1533 * earlier and gives more helpful/explicit error messages.
1535 for (bit_count
+= 32; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;bit_count
+= 32)
1537 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1538 if (unexpected
||(idcode
!= 0x000000FF))
1540 LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count
, idcode
);
1548 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
1549 manufacturer
= EXTRACT_MFG(idcode
);
1550 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
1551 part
= EXTRACT_PART(idcode
);
1552 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
1553 version
= EXTRACT_VER(idcode
);
1555 LOG_INFO("JTAG tap: %s tap/device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1556 ((tap
!= NULL
) ? (tap
->dotted_name
) : "(not-named)"),
1557 idcode
, manufacturer
, part
, version
);
1563 tap
->idcode
= idcode
;
1565 if (tap
->expected_ids_cnt
> 0) {
1566 /* Loop over the expected identification codes and test for a match */
1568 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++) {
1569 if( tap
->idcode
== tap
->expected_ids
[ii
] ){
1574 /* If none of the expected ids matched, log an error */
1575 if (ii
== tap
->expected_ids_cnt
) {
1576 LOG_ERROR("JTAG tap: %s got: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1579 EXTRACT_MFG( tap
->idcode
),
1580 EXTRACT_PART( tap
->idcode
),
1581 EXTRACT_VER( tap
->idcode
) );
1582 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++) {
1583 LOG_ERROR("JTAG tap: %s expected %hhu of %hhu: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1586 tap
->expected_ids_cnt
,
1587 tap
->expected_ids
[ii
],
1588 EXTRACT_MFG( tap
->expected_ids
[ii
] ),
1589 EXTRACT_PART( tap
->expected_ids
[ii
] ),
1590 EXTRACT_VER( tap
->expected_ids
[ii
] ) );
1593 return ERROR_JTAG_INIT_FAILED
;
1595 LOG_INFO("JTAG Tap/device matched");
1599 LOG_INFO("JTAG TAP ID: 0x%08x - Unknown - please report (A) chipname and (B) idcode to the openocd project",
1603 tap
= jtag_NextEnabledTap(tap
);
1608 /* see if number of discovered devices matches configuration */
1609 if (device_count
!= jtag_NumEnabledTaps())
1611 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match (enabled) configuration (%i), total taps: %d",
1612 device_count
, jtag_NumEnabledTaps(), jtag_NumTotalTaps());
1613 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1614 return ERROR_JTAG_INIT_FAILED
;
1620 int jtag_validate_chain(void)
1623 int total_ir_length
= 0;
1629 total_ir_length
= 0;
1631 tap
= jtag_NextEnabledTap(tap
);
1635 total_ir_length
+= tap
->ir_length
;
1638 total_ir_length
+= 2;
1639 ir_test
= malloc(CEIL(total_ir_length
, 8));
1640 buf_set_ones(ir_test
, total_ir_length
);
1643 field
.num_bits
= total_ir_length
;
1644 field
.out_value
= ir_test
;
1645 field
.out_mask
= NULL
;
1646 field
.in_value
= ir_test
;
1647 field
.in_check_value
= NULL
;
1648 field
.in_check_mask
= NULL
;
1649 field
.in_handler
= NULL
;
1650 field
.in_handler_priv
= NULL
;
1652 jtag_add_plain_ir_scan(1, &field
, TAP_RESET
);
1653 jtag_execute_queue();
1658 tap
= jtag_NextEnabledTap(tap
);
1664 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x1)
1666 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1667 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1670 return ERROR_JTAG_INIT_FAILED
;
1672 chain_pos
+= tap
->ir_length
;
1675 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x3)
1677 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1678 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1681 return ERROR_JTAG_INIT_FAILED
;
1689 enum jtag_tap_cfg_param
{
1693 static Jim_Nvp nvp_config_opts
[] = {
1694 { .name
= "-event", .value
= JCFG_EVENT
},
1696 { .name
= NULL
, .value
= -1 }
1700 jtag_tap_configure_cmd( Jim_GetOptInfo
*goi
,
1707 /* parse config or cget options */
1708 while (goi
->argc
> 0) {
1709 Jim_SetEmptyResult (goi
->interp
);
1711 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
1713 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
1719 if (goi
->argc
== 0) {
1720 Jim_WrongNumArgs( goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ..." );
1724 e
= Jim_GetOpt_Nvp( goi
, nvp_jtag_tap_event
, &n
);
1726 Jim_GetOpt_NvpUnknown(goi
, nvp_jtag_tap_event
, 1);
1730 if (goi
->isconfigure
) {
1731 if (goi
->argc
!= 1) {
1732 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
1736 if (goi
->argc
!= 0) {
1737 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
1743 jtag_tap_event_action_t
*jteap
;
1745 jteap
= tap
->event_action
;
1746 /* replace existing? */
1748 if (jteap
->event
== n
->value
) {
1751 jteap
= jteap
->next
;
1754 if (goi
->isconfigure
) {
1755 if (jteap
== NULL
) {
1757 jteap
= calloc(1, sizeof (*jteap
));
1759 jteap
->event
= n
->value
;
1760 Jim_GetOpt_Obj( goi
, &o
);
1762 Jim_DecrRefCount(interp
, jteap
->body
);
1764 jteap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
1765 Jim_IncrRefCount(jteap
->body
);
1767 /* add to head of event list */
1768 jteap
->next
= tap
->event_action
;
1769 tap
->event_action
= jteap
;
1770 Jim_SetEmptyResult(goi
->interp
);
1773 if (jteap
== NULL
) {
1774 Jim_SetEmptyResult(goi
->interp
);
1776 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, jteap
->body
));
1783 } /* while (goi->argc) */
1788 static int jim_newtap_cmd( Jim_GetOptInfo
*goi
)
1798 const Jim_Nvp opts
[] = {
1799 #define NTAP_OPT_IRLEN 0
1800 { .name
= "-irlen" , .value
= NTAP_OPT_IRLEN
},
1801 #define NTAP_OPT_IRMASK 1
1802 { .name
= "-irmask" , .value
= NTAP_OPT_IRMASK
},
1803 #define NTAP_OPT_IRCAPTURE 2
1804 { .name
= "-ircapture" , .value
= NTAP_OPT_IRCAPTURE
},
1805 #define NTAP_OPT_ENABLED 3
1806 { .name
= "-enable" , .value
= NTAP_OPT_ENABLED
},
1807 #define NTAP_OPT_DISABLED 4
1808 { .name
= "-disable" , .value
= NTAP_OPT_DISABLED
},
1809 #define NTAP_OPT_EXPECTED_ID 5
1810 { .name
= "-expected-id" , .value
= NTAP_OPT_EXPECTED_ID
},
1811 { .name
= NULL
, .value
= -1 },
1814 pTap
= malloc( sizeof(jtag_tap_t
) );
1815 memset( pTap
, 0, sizeof(*pTap
) );
1817 Jim_SetResult_sprintf( goi
->interp
, "no memory");
1821 * we expect CHIP + TAP + OPTIONS
1823 if( goi
->argc
< 3 ){
1824 Jim_SetResult_sprintf(goi
->interp
, "Missing CHIP TAP OPTIONS ....");
1827 Jim_GetOpt_String( goi
, &cp
, NULL
);
1828 pTap
->chip
= strdup(cp
);
1830 Jim_GetOpt_String( goi
, &cp
, NULL
);
1831 pTap
->tapname
= strdup(cp
);
1833 /* name + dot + name + null */
1834 x
= strlen(pTap
->chip
) + 1 + strlen(pTap
->tapname
) + 1;
1836 sprintf( cp
, "%s.%s", pTap
->chip
, pTap
->tapname
);
1837 pTap
->dotted_name
= cp
;
1839 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
1840 pTap
->chip
, pTap
->tapname
, pTap
->dotted_name
, goi
->argc
);
1842 /* default is enabled */
1845 /* deal with options */
1846 #define NTREQ_IRLEN 1
1847 #define NTREQ_IRCAPTURE 2
1848 #define NTREQ_IRMASK 4
1850 /* clear them as we find them */
1851 reqbits
= (NTREQ_IRLEN
| NTREQ_IRCAPTURE
| NTREQ_IRMASK
);
1854 e
= Jim_GetOpt_Nvp( goi
, opts
, &n
);
1856 Jim_GetOpt_NvpUnknown( goi
, opts
, 0 );
1859 LOG_DEBUG("Processing option: %s", n
->name
);
1861 case NTAP_OPT_ENABLED
:
1864 case NTAP_OPT_DISABLED
:
1867 case NTAP_OPT_EXPECTED_ID
:
1869 u32
*new_expected_ids
;
1871 e
= Jim_GetOpt_Wide( goi
, &w
);
1873 Jim_SetResult_sprintf(goi
->interp
, "option: %s bad parameter", n
->name
);
1877 new_expected_ids
= malloc(sizeof(u32
) * (pTap
->expected_ids_cnt
+ 1));
1878 if (new_expected_ids
== NULL
) {
1879 Jim_SetResult_sprintf( goi
->interp
, "no memory");
1883 memcpy(new_expected_ids
, pTap
->expected_ids
, sizeof(u32
) * pTap
->expected_ids_cnt
);
1885 new_expected_ids
[pTap
->expected_ids_cnt
] = w
;
1887 free(pTap
->expected_ids
);
1888 pTap
->expected_ids
= new_expected_ids
;
1889 pTap
->expected_ids_cnt
++;
1892 case NTAP_OPT_IRLEN
:
1893 case NTAP_OPT_IRMASK
:
1894 case NTAP_OPT_IRCAPTURE
:
1895 e
= Jim_GetOpt_Wide( goi
, &w
);
1897 Jim_SetResult_sprintf( goi
->interp
, "option: %s bad parameter", n
->name
);
1900 if( (w
< 0) || (w
> 0xffff) ){
1902 Jim_SetResult_sprintf( goi
->interp
, "option: %s - wacky value: %d (0x%x)",
1903 n
->name
, (int)(w
), (int)(w
));
1907 case NTAP_OPT_IRLEN
:
1908 pTap
->ir_length
= w
;
1909 reqbits
&= (~(NTREQ_IRLEN
));
1911 case NTAP_OPT_IRMASK
:
1912 pTap
->ir_capture_mask
= w
;
1913 reqbits
&= (~(NTREQ_IRMASK
));
1915 case NTAP_OPT_IRCAPTURE
:
1916 pTap
->ir_capture_value
= w
;
1917 reqbits
&= (~(NTREQ_IRCAPTURE
));
1920 } /* switch(n->value) */
1921 } /* while( goi->argc ) */
1923 /* Did we get all the options? */
1926 Jim_SetResult_sprintf( goi
->interp
,
1927 "newtap: %s missing required parameters",
1929 /* TODO: Tell user what is missing :-( */
1930 /* no memory leaks pelase */
1931 free(((void *)(pTap
->expected_ids
)));
1932 free(((void *)(pTap
->chip
)));
1933 free(((void *)(pTap
->tapname
)));
1934 free(((void *)(pTap
->dotted_name
)));
1935 free(((void *)(pTap
)));
1939 pTap
->expected
= malloc( pTap
->ir_length
);
1940 pTap
->expected_mask
= malloc( pTap
->ir_length
);
1941 pTap
->cur_instr
= malloc( pTap
->ir_length
);
1943 buf_set_u32( pTap
->expected
,
1946 pTap
->ir_capture_value
);
1947 buf_set_u32( pTap
->expected_mask
,
1950 pTap
->ir_capture_mask
);
1951 buf_set_ones( pTap
->cur_instr
,
1956 jtag_register_event_callback(jtag_reset_callback
, pTap
);
1958 ppTap
= &(jtag_all_taps
);
1959 while( (*ppTap
) != NULL
){
1960 ppTap
= &((*ppTap
)->next_tap
);
1964 static int n_taps
= 0;
1965 pTap
->abs_chain_position
= n_taps
++;
1967 LOG_DEBUG( "Created Tap: %s @ abs position %d, irlen %d, capture: 0x%x mask: 0x%x",
1968 (*ppTap
)->dotted_name
,
1969 (*ppTap
)->abs_chain_position
,
1970 (*ppTap
)->ir_length
,
1971 (*ppTap
)->ir_capture_value
,
1972 (*ppTap
)->ir_capture_mask
);
1977 static int jim_jtag_command( Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1983 struct command_context_s
*context
;
1987 JTAG_CMD_INIT_RESET
,
1990 JTAG_CMD_TAPDISABLE
,
1991 JTAG_CMD_TAPISENABLED
,
1996 const Jim_Nvp jtag_cmds
[] = {
1997 { .name
= "interface" , .value
= JTAG_CMD_INTERFACE
},
1998 { .name
= "arp_init-reset", .value
= JTAG_CMD_INIT_RESET
},
1999 { .name
= "newtap" , .value
= JTAG_CMD_NEWTAP
},
2000 { .name
= "tapisenabled" , .value
= JTAG_CMD_TAPISENABLED
},
2001 { .name
= "tapenable" , .value
= JTAG_CMD_TAPENABLE
},
2002 { .name
= "tapdisable" , .value
= JTAG_CMD_TAPDISABLE
},
2003 { .name
= "configure" , .value
= JTAG_CMD_CONFIGURE
},
2004 { .name
= "cget" , .value
= JTAG_CMD_CGET
},
2006 { .name
= NULL
, .value
= -1 },
2009 context
= Jim_GetAssocData(interp
, "context");
2010 /* go past the command */
2011 Jim_GetOpt_Setup( &goi
, interp
, argc
-1, argv
+1 );
2013 e
= Jim_GetOpt_Nvp( &goi
, jtag_cmds
, &n
);
2015 Jim_GetOpt_NvpUnknown( &goi
, jtag_cmds
, 0 );
2018 Jim_SetEmptyResult( goi
.interp
);
2020 case JTAG_CMD_INTERFACE
:
2021 /* return the name of the interface */
2022 /* TCL code might need to know the exact type... */
2023 /* FUTURE: we allow this as a means to "set" the interface. */
2024 if( goi
.argc
!= 0 ){
2025 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
-1, "(no params)");
2028 Jim_SetResultString( goi
.interp
, jtag_interface
->name
, -1 );
2030 case JTAG_CMD_INIT_RESET
:
2031 if( goi
.argc
!= 0 ){
2032 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
-1, "(no params)");
2035 e
= jtag_init_reset(context
);
2036 if( e
!= ERROR_OK
){
2037 Jim_SetResult_sprintf( goi
.interp
, "error: %d", e
);
2041 case JTAG_CMD_NEWTAP
:
2042 return jim_newtap_cmd( &goi
);
2044 case JTAG_CMD_TAPISENABLED
:
2045 case JTAG_CMD_TAPENABLE
:
2046 case JTAG_CMD_TAPDISABLE
:
2047 if( goi
.argc
!= 1 ){
2048 Jim_SetResultString( goi
.interp
, "Too many parameters",-1 );
2054 t
= jtag_TapByJimObj( goi
.interp
, goi
.argv
[0] );
2059 case JTAG_CMD_TAPISENABLED
:
2062 case JTAG_CMD_TAPENABLE
:
2063 jtag_tap_handle_event( t
, JTAG_TAP_EVENT_ENABLE
);
2067 case JTAG_CMD_TAPDISABLE
:
2068 jtag_tap_handle_event( t
, JTAG_TAP_EVENT_DISABLE
);
2073 Jim_SetResult( goi
.interp
, Jim_NewIntObj( goi
.interp
, e
) );
2080 Jim_WrongNumArgs( goi
.interp
, 0, NULL
, "?tap-name? -option ...");
2087 Jim_GetOpt_Obj(&goi
, &o
);
2088 t
= jtag_TapByJimObj( goi
.interp
, o
);
2093 goi
.isconfigure
= 0;
2094 return jtag_tap_configure_cmd( &goi
, t
);
2098 case JTAG_CMD_CONFIGURE
:
2100 Jim_WrongNumArgs( goi
.interp
, 0, NULL
, "?tap-name? -option ?VALUE? ...");
2107 Jim_GetOpt_Obj(&goi
, &o
);
2108 t
= jtag_TapByJimObj( goi
.interp
, o
);
2113 goi
.isconfigure
= 1;
2114 return jtag_tap_configure_cmd( &goi
, t
);
2121 int jtag_register_commands(struct command_context_s
*cmd_ctx
)
2123 register_jim( cmd_ctx
, "jtag", jim_jtag_command
, "perform jtag tap actions");
2125 register_command(cmd_ctx
, NULL
, "interface", handle_interface_command
,
2126 COMMAND_CONFIG
, "try to configure interface");
2127 register_command(cmd_ctx
, NULL
, "jtag_speed", handle_jtag_speed_command
,
2128 COMMAND_ANY
, "set jtag speed (if supported)");
2129 register_command(cmd_ctx
, NULL
, "jtag_khz", handle_jtag_khz_command
,
2130 COMMAND_ANY
, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
2131 register_command(cmd_ctx
, NULL
, "jtag_device", handle_jtag_device_command
,
2132 COMMAND_CONFIG
, "jtag_device <ir_length> <ir_expected> <ir_mask>");
2133 register_command(cmd_ctx
, NULL
, "reset_config", handle_reset_config_command
,
2134 COMMAND_CONFIG
, NULL
);
2135 register_command(cmd_ctx
, NULL
, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command
,
2136 COMMAND_ANY
, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
2137 register_command(cmd_ctx
, NULL
, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command
,
2138 COMMAND_ANY
, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
2140 register_command(cmd_ctx
, NULL
, "scan_chain", handle_scan_chain_command
,
2141 COMMAND_EXEC
, "print current scan chain configuration");
2143 register_command(cmd_ctx
, NULL
, "endstate", handle_endstate_command
,
2144 COMMAND_EXEC
, "finish JTAG operations in <tap_state>");
2145 register_command(cmd_ctx
, NULL
, "jtag_reset", handle_jtag_reset_command
,
2146 COMMAND_EXEC
, "toggle reset lines <trst> <srst>");
2147 register_command(cmd_ctx
, NULL
, "runtest", handle_runtest_command
,
2148 COMMAND_EXEC
, "move to Run-Test/Idle, and execute <num_cycles>");
2149 register_command(cmd_ctx
, NULL
, "irscan", handle_irscan_command
,
2150 COMMAND_EXEC
, "execute IR scan <device> <instr> [dev2] [instr2] ...");
2151 register_jim(cmd_ctx
, "drscan", Jim_Command_drscan
, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
2153 register_command(cmd_ctx
, NULL
, "verify_ircapture", handle_verify_ircapture_command
,
2154 COMMAND_ANY
, "verify value captured during Capture-IR <enable|disable>");
2158 int jtag_interface_init(struct command_context_s
*cmd_ctx
)
2163 if (!jtag_interface
)
2165 /* nothing was previously specified by "interface" command */
2166 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
2167 return ERROR_JTAG_INVALID_INTERFACE
;
2171 jtag_interface
->khz(speed_khz
, &jtag_speed
);
2175 if (jtag_interface
->init() != ERROR_OK
)
2176 return ERROR_JTAG_INIT_FAILED
;
2178 jtag
= jtag_interface
;
2182 static int jtag_init_inner(struct command_context_s
*cmd_ctx
)
2187 LOG_DEBUG("Init JTAG chain");
2189 tap
= jtag_NextEnabledTap(NULL
);
2191 LOG_ERROR("There are no enabled taps?");
2192 return ERROR_JTAG_INIT_FAILED
;
2196 if ((retval
=jtag_execute_queue())!=ERROR_OK
)
2199 /* examine chain first, as this could discover the real chain layout */
2200 if (jtag_examine_chain() != ERROR_OK
)
2202 LOG_ERROR("trying to validate configured JTAG chain anyway...");
2205 if (jtag_validate_chain() != ERROR_OK
)
2207 LOG_ERROR("Could not validate JTAG chain, continuing anyway...");
2213 int jtag_init_reset(struct command_context_s
*cmd_ctx
)
2217 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
2220 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
2222 /* Reset can happen after a power cycle.
2224 * Ideally we would only assert TRST or run RESET before the target reset.
2226 * However w/srst_pulls_trst, trst is asserted together with the target
2227 * reset whether we want it or not.
2229 * NB! Some targets have JTAG circuitry disabled until a
2230 * trst & srst has been asserted.
2232 * NB! here we assume nsrst/ntrst delay are sufficient!
2234 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
2237 jtag_add_reset(1, 0); /* RESET or TRST */
2238 if (jtag_reset_config
& RESET_HAS_SRST
)
2240 jtag_add_reset(1, 1);
2241 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0)
2242 jtag_add_reset(0, 1);
2244 jtag_add_reset(0, 0);
2245 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
2248 /* Check that we can communication on the JTAG chain + eventually we want to
2249 * be able to perform enumeration only after OpenOCD has started
2250 * telnet and GDB server
2252 * That would allow users to more easily perform any magic they need to before
2255 return jtag_init_inner(cmd_ctx
);
2258 int jtag_init(struct command_context_s
*cmd_ctx
)
2261 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
2263 if (jtag_init_inner(cmd_ctx
)==ERROR_OK
)
2267 return jtag_init_reset(cmd_ctx
);
2270 static int default_khz(int khz
, int *jtag_speed
)
2272 LOG_ERROR("Translation from khz to jtag_speed not implemented");
2276 static int default_speed_div(int speed
, int *khz
)
2278 LOG_ERROR("Translation from jtag_speed to khz not implemented");
2282 static int default_power_dropout(int *dropout
)
2284 *dropout
=0; /* by default we can't detect power dropout */
2288 static int default_srst_asserted(int *srst_asserted
)
2290 *srst_asserted
=0; /* by default we can't detect srst asserted */
2294 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2299 /* check whether the interface is already configured */
2302 LOG_WARNING("Interface already configured, ignoring");
2306 /* interface name is a mandatory argument */
2307 if (argc
< 1 || args
[0][0] == '\0')
2309 return ERROR_COMMAND_SYNTAX_ERROR
;
2312 for (i
=0; jtag_interfaces
[i
]; i
++)
2314 if (strcmp(args
[0], jtag_interfaces
[i
]->name
) == 0)
2316 if ((retval
= jtag_interfaces
[i
]->register_commands(cmd_ctx
)) != ERROR_OK
)
2321 jtag_interface
= jtag_interfaces
[i
];
2323 if (jtag_interface
->khz
== NULL
)
2325 jtag_interface
->khz
= default_khz
;
2327 if (jtag_interface
->speed_div
== NULL
)
2329 jtag_interface
->speed_div
= default_speed_div
;
2331 if (jtag_interface
->power_dropout
== NULL
)
2333 jtag_interface
->power_dropout
= default_power_dropout
;
2335 if (jtag_interface
->srst_asserted
== NULL
)
2337 jtag_interface
->srst_asserted
= default_srst_asserted
;
2344 /* no valid interface was found (i.e. the configuration option,
2345 * didn't match one of the compiled-in interfaces
2347 LOG_ERROR("No valid jtag interface found (%s)", args
[0]);
2348 LOG_ERROR("compiled-in jtag interfaces:");
2349 for (i
= 0; jtag_interfaces
[i
]; i
++)
2351 LOG_ERROR("%i: %s", i
, jtag_interfaces
[i
]->name
);
2354 return ERROR_JTAG_INVALID_INTERFACE
;
2357 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2361 Jim_Obj
*newargs
[ 10 ];
2364 * argv[-1] = command
2365 * argv[ 0] = ir length
2366 * argv[ 1] = ir capture
2367 * argv[ 2] = ir mask
2368 * argv[ 3] = not actually used by anything but in the docs
2372 command_print( cmd_ctx
, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
2375 command_print( cmd_ctx
, "OLD SYNTAX: DEPRECATED - translating to new syntax");
2376 command_print( cmd_ctx
, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
2380 command_print( cmd_ctx
, "Example: STM32 has 2 taps, the cortexM3(len4) + boundryscan(len5)");
2381 command_print( cmd_ctx
, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
2382 command_print( cmd_ctx
, "jtag newtap stm32 boundry ....., and the tap: \"stm32.boundery\"");
2383 command_print( cmd_ctx
, "And then refer to the taps by the dotted name.");
2385 newargs
[0] = Jim_NewStringObj( interp
, "jtag", -1 );
2386 newargs
[1] = Jim_NewStringObj( interp
, "newtap", -1 );
2387 sprintf( buf
, "chip%d", jtag_NumTotalTaps() );
2388 newargs
[2] = Jim_NewStringObj( interp
, buf
, -1 );
2389 sprintf( buf
, "tap%d", jtag_NumTotalTaps() );
2390 newargs
[3] = Jim_NewStringObj( interp
, buf
, -1 );
2391 newargs
[4] = Jim_NewStringObj( interp
, "-irlen", -1 );
2392 newargs
[5] = Jim_NewStringObj( interp
, args
[0], -1 );
2393 newargs
[6] = Jim_NewStringObj( interp
, "-ircapture", -1 );
2394 newargs
[7] = Jim_NewStringObj( interp
, args
[1], -1 );
2395 newargs
[8] = Jim_NewStringObj( interp
, "-irmask", -1 );
2396 newargs
[9] = Jim_NewStringObj( interp
, args
[2], -1 );
2398 command_print( cmd_ctx
, "NEW COMMAND:");
2399 sprintf( buf
, "%s %s %s %s %s %s %s %s %s %s",
2400 Jim_GetString( newargs
[0], NULL
),
2401 Jim_GetString( newargs
[1], NULL
),
2402 Jim_GetString( newargs
[2], NULL
),
2403 Jim_GetString( newargs
[3], NULL
),
2404 Jim_GetString( newargs
[4], NULL
),
2405 Jim_GetString( newargs
[5], NULL
),
2406 Jim_GetString( newargs
[6], NULL
),
2407 Jim_GetString( newargs
[7], NULL
),
2408 Jim_GetString( newargs
[8], NULL
),
2409 Jim_GetString( newargs
[9], NULL
) );
2411 e
= jim_jtag_command( interp
, 10, newargs
);
2413 command_print( cmd_ctx
, "%s", Jim_GetString( Jim_GetResult(interp
), NULL
) );
2418 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2422 tap
= jtag_all_taps
;
2423 command_print(cmd_ctx
, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
2424 command_print(cmd_ctx
, "---|--------------------|---------|------------|------------|------|------|------|---------");
2427 u32 expected
, expected_mask
, cur_instr
, ii
;
2428 expected
= buf_get_u32(tap
->expected
, 0, tap
->ir_length
);
2429 expected_mask
= buf_get_u32(tap
->expected_mask
, 0, tap
->ir_length
);
2430 cur_instr
= buf_get_u32(tap
->cur_instr
, 0, tap
->ir_length
);
2432 command_print(cmd_ctx
,
2433 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
2434 tap
->abs_chain_position
,
2436 tap
->enabled
? 'Y' : 'n',
2438 (tap
->expected_ids_cnt
> 0 ? tap
->expected_ids
[0] : 0),
2444 for (ii
= 1; ii
< tap
->expected_ids_cnt
; ii
++) {
2445 command_print(cmd_ctx
, " | | | | 0x%08x | | | | ",
2446 tap
->expected_ids
[ii
]);
2449 tap
= tap
->next_tap
;
2455 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2458 return ERROR_COMMAND_SYNTAX_ERROR
;
2462 if (strcmp(args
[0], "none") == 0)
2463 jtag_reset_config
= RESET_NONE
;
2464 else if (strcmp(args
[0], "trst_only") == 0)
2465 jtag_reset_config
= RESET_HAS_TRST
;
2466 else if (strcmp(args
[0], "srst_only") == 0)
2467 jtag_reset_config
= RESET_HAS_SRST
;
2468 else if (strcmp(args
[0], "trst_and_srst") == 0)
2469 jtag_reset_config
= RESET_TRST_AND_SRST
;
2472 LOG_ERROR("(1) invalid reset_config argument (%s), defaulting to none", args
[0]);
2473 jtag_reset_config
= RESET_NONE
;
2474 return ERROR_INVALID_ARGUMENTS
;
2480 if (strcmp(args
[1], "separate") == 0)
2482 /* seperate reset lines - default */
2485 if (strcmp(args
[1], "srst_pulls_trst") == 0)
2486 jtag_reset_config
|= RESET_SRST_PULLS_TRST
;
2487 else if (strcmp(args
[1], "trst_pulls_srst") == 0)
2488 jtag_reset_config
|= RESET_TRST_PULLS_SRST
;
2489 else if (strcmp(args
[1], "combined") == 0)
2490 jtag_reset_config
|= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
2493 LOG_ERROR("(2) invalid reset_config argument (%s), defaulting to none", args
[1]);
2494 jtag_reset_config
= RESET_NONE
;
2495 return ERROR_INVALID_ARGUMENTS
;
2502 if (strcmp(args
[2], "trst_open_drain") == 0)
2503 jtag_reset_config
|= RESET_TRST_OPEN_DRAIN
;
2504 else if (strcmp(args
[2], "trst_push_pull") == 0)
2505 jtag_reset_config
&= ~RESET_TRST_OPEN_DRAIN
;
2508 LOG_ERROR("(3) invalid reset_config argument (%s) defaulting to none", args
[2] );
2509 jtag_reset_config
= RESET_NONE
;
2510 return ERROR_INVALID_ARGUMENTS
;
2516 if (strcmp(args
[3], "srst_push_pull") == 0)
2517 jtag_reset_config
|= RESET_SRST_PUSH_PULL
;
2518 else if (strcmp(args
[3], "srst_open_drain") == 0)
2519 jtag_reset_config
&= ~RESET_SRST_PUSH_PULL
;
2522 LOG_ERROR("(4) invalid reset_config argument (%s), defaulting to none", args
[3]);
2523 jtag_reset_config
= RESET_NONE
;
2524 return ERROR_INVALID_ARGUMENTS
;
2531 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2535 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
2540 jtag_nsrst_delay
= strtoul(args
[0], NULL
, 0);
2546 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2550 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
2555 jtag_ntrst_delay
= strtoul(args
[0], NULL
, 0);
2561 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2563 int retval
=ERROR_OK
;
2567 LOG_DEBUG("handle jtag speed");
2570 cur_speed
= jtag_speed
= strtoul(args
[0], NULL
, 0);
2572 /* this command can be called during CONFIG,
2573 * in which case jtag isn't initialized */
2576 retval
=jtag
->speed(cur_speed
);
2578 } else if (argc
== 0)
2582 return ERROR_COMMAND_SYNTAX_ERROR
;
2584 command_print(cmd_ctx
, "jtag_speed: %d", jtag_speed
);
2589 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2591 int retval
=ERROR_OK
;
2592 LOG_DEBUG("handle jtag khz");
2596 speed_khz
= strtoul(args
[0], NULL
, 0);
2600 LOG_DEBUG("have interface set up");
2602 if ((retval
=jtag
->khz(speed_khz
, &speed_div1
))!=ERROR_OK
)
2608 cur_speed
= jtag_speed
= speed_div1
;
2610 retval
=jtag
->speed(cur_speed
);
2619 return ERROR_COMMAND_SYNTAX_ERROR
;
2624 if ((retval
=jtag
->speed_div(jtag_speed
, &speed_khz
))!=ERROR_OK
)
2630 command_print(cmd_ctx
, "RCLK - adaptive");
2633 command_print(cmd_ctx
, "%d kHz", speed_khz
);
2639 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2641 enum tap_state state
;
2645 return ERROR_COMMAND_SYNTAX_ERROR
;
2649 for (state
= 0; state
< 16; state
++)
2651 if (strcmp(args
[0], jtag_state_name(state
)) == 0)
2653 jtag_add_end_state(state
);
2654 jtag_execute_queue();
2658 command_print(cmd_ctx
, "current endstate: %s", jtag_state_name(cmd_queue_end_state
));
2663 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2670 return ERROR_COMMAND_SYNTAX_ERROR
;
2673 if (args
[0][0] == '1')
2675 else if (args
[0][0] == '0')
2679 return ERROR_COMMAND_SYNTAX_ERROR
;
2682 if (args
[1][0] == '1')
2684 else if (args
[1][0] == '0')
2688 return ERROR_COMMAND_SYNTAX_ERROR
;
2691 if (jtag_interface_init(cmd_ctx
) != ERROR_OK
)
2692 return ERROR_JTAG_INIT_FAILED
;
2694 jtag_add_reset(trst
, srst
);
2695 jtag_execute_queue();
2700 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2704 return ERROR_COMMAND_SYNTAX_ERROR
;
2707 jtag_add_runtest(strtol(args
[0], NULL
, 0), -1);
2708 jtag_execute_queue();
2714 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2717 scan_field_t
*fields
;
2720 if ((argc
< 2) || (argc
% 2))
2722 return ERROR_COMMAND_SYNTAX_ERROR
;
2725 fields
= malloc(sizeof(scan_field_t
) * argc
/ 2);
2727 for (i
= 0; i
< argc
/ 2; i
++)
2729 tap
= jtag_TapByString( args
[i
*2] );
2732 command_print( cmd_ctx
, "Tap: %s unknown", args
[i
*2] );
2735 int field_size
= tap
->ir_length
;
2736 fields
[i
].tap
= tap
;
2737 fields
[i
].out_value
= malloc(CEIL(field_size
, 8));
2738 buf_set_u32(fields
[i
].out_value
, 0, field_size
, strtoul(args
[i
*2+1], NULL
, 0));
2739 fields
[i
].out_mask
= NULL
;
2740 fields
[i
].in_value
= NULL
;
2741 fields
[i
].in_check_mask
= NULL
;
2742 fields
[i
].in_handler
= NULL
;
2743 fields
[i
].in_handler_priv
= NULL
;
2746 jtag_add_ir_scan(argc
/ 2, fields
, -1);
2747 jtag_execute_queue();
2749 for (i
= 0; i
< argc
/ 2; i
++)
2750 free(fields
[i
].out_value
);
2757 int Jim_Command_drscan(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *args
)
2760 scan_field_t
*fields
;
2762 int field_count
= 0;
2767 * args[2] = num_bits
2768 * args[3] = hex string
2769 * ... repeat num bits and hex string ...
2771 if ((argc
< 4) || ((argc
% 2)!=0))
2773 Jim_WrongNumArgs(interp
, 1, args
, "wrong arguments");
2777 for (i
= 2; i
< argc
; i
+=2)
2781 e
= Jim_GetLong(interp
, args
[i
], &bits
);
2786 tap
= jtag_TapByJimObj( interp
, args
[1] );
2791 num_fields
=(argc
-2)/2;
2792 fields
= malloc(sizeof(scan_field_t
) * num_fields
);
2793 for (i
= 2; i
< argc
; i
+=2)
2799 Jim_GetLong(interp
, args
[i
], &bits
);
2800 str
= Jim_GetString(args
[i
+1], &len
);
2802 fields
[field_count
].tap
= tap
;
2803 fields
[field_count
].num_bits
= bits
;
2804 fields
[field_count
].out_value
= malloc(CEIL(bits
, 8));
2805 str_to_buf(str
, len
, fields
[field_count
].out_value
, bits
, 0);
2806 fields
[field_count
].out_mask
= NULL
;
2807 fields
[field_count
].in_value
= fields
[field_count
].out_value
;
2808 fields
[field_count
].in_check_mask
= NULL
;
2809 fields
[field_count
].in_check_value
= NULL
;
2810 fields
[field_count
].in_handler
= NULL
;
2811 fields
[field_count
++].in_handler_priv
= NULL
;
2814 jtag_add_dr_scan(num_fields
, fields
, -1);
2815 retval
= jtag_execute_queue();
2816 if (retval
!= ERROR_OK
)
2818 Jim_SetResultString(interp
, "drscan: jtag execute failed",-1);
2823 Jim_Obj
*list
= Jim_NewListObj(interp
, NULL
, 0);
2824 for (i
= 2; i
< argc
; i
+=2)
2829 Jim_GetLong(interp
, args
[i
], &bits
);
2830 str
= buf_to_str(fields
[field_count
].in_value
, bits
, 16);
2831 free(fields
[field_count
].out_value
);
2833 Jim_ListAppendElement(interp
, list
, Jim_NewStringObj(interp
, str
, strlen(str
)));
2838 Jim_SetResult(interp
, list
);
2845 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2849 if (strcmp(args
[0], "enable") == 0)
2851 jtag_verify_capture_ir
= 1;
2853 else if (strcmp(args
[0], "disable") == 0)
2855 jtag_verify_capture_ir
= 0;
2858 return ERROR_COMMAND_SYNTAX_ERROR
;
2860 } else if (argc
!= 0)
2862 return ERROR_COMMAND_SYNTAX_ERROR
;
2865 command_print(cmd_ctx
, "verify Capture-IR is %s", (jtag_verify_capture_ir
) ? "enabled": "disabled");
2870 int jtag_power_dropout(int *dropout
)
2872 return jtag
->power_dropout(dropout
);
2875 int jtag_srst_asserted(int *srst_asserted
)
2877 return jtag
->srst_asserted(srst_asserted
);
2880 void jtag_tap_handle_event( jtag_tap_t
* tap
, enum jtag_tap_event e
)
2882 jtag_tap_event_action_t
* jteap
;
2885 jteap
= tap
->event_action
;
2889 if (jteap
->event
== e
) {
2891 LOG_DEBUG( "JTAG tap: %s event: %d (%s) action: %s\n",
2894 Jim_Nvp_value2name_simple(nvp_jtag_tap_event
, e
)->name
,
2895 Jim_GetString(jteap
->body
, NULL
) );
2896 if (Jim_EvalObj(interp
, jteap
->body
) != JIM_OK
) {
2897 Jim_PrintErrorMessage(interp
);
2901 jteap
= jteap
->next
;
2905 LOG_DEBUG( "event %d %s - no action",
2907 Jim_Nvp_value2name_simple( nvp_jtag_tap_event
, e
)->name
);
2912 /* map state number to SVF state string */
2913 const char* jtag_state_name(enum tap_state state
)
2919 case TAP_RESET
: ret
= "RESET"; break;
2920 case TAP_IDLE
: ret
= "IDLE"; break;
2921 case TAP_DRSELECT
: ret
= "DRSELECT"; break;
2922 case TAP_DRCAPTURE
: ret
= "DRCAPTURE"; break;
2923 case TAP_DRSHIFT
: ret
= "DRSHIFT"; break;
2924 case TAP_DREXIT1
: ret
= "DREXIT1"; break;
2925 case TAP_DRPAUSE
: ret
= "DRPAUSE"; break;
2926 case TAP_DREXIT2
: ret
= "DREXIT2"; break;
2927 case TAP_DRUPDATE
: ret
= "DRUPDATE"; break;
2928 case TAP_IRSELECT
: ret
= "IRSELECT"; break;
2929 case TAP_IRCAPTURE
: ret
= "IRCAPTURE"; break;
2930 case TAP_IRSHIFT
: ret
= "IRSHIFT"; break;
2931 case TAP_IREXIT1
: ret
= "IREXIT1"; break;
2932 case TAP_IRPAUSE
: ret
= "IRPAUSE"; break;
2933 case TAP_IREXIT2
: ret
= "IREXIT2"; break;
2934 case TAP_IRUPDATE
: ret
= "IRUPDATE"; break;
2935 default: ret
= "???";
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)