1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
27 #include "replacements.h"
38 /* note that this is not marked as static as it must be available from outside jtag.c for those
39 that implement the jtag_xxx() minidriver layer
41 int jtag_error
=ERROR_OK
;
44 char* tap_state_strings
[16] =
47 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
49 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
52 typedef struct cmd_queue_page_s
56 struct cmd_queue_page_s
*next
;
59 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
60 static cmd_queue_page_t
*cmd_queue_pages
= NULL
;
62 /* tap_move[i][j]: tap movement command to go from state i to state j
70 * DRSHIFT->DRSHIFT and IRSHIFT->IRSHIFT have to be caught in interface specific code
74 /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */
75 { 0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* RESET */
76 { 0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* IDLE */
77 { 0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* DRSHIFT */
78 { 0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* DRPAUSE */
79 { 0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* IRSHIFT */
80 { 0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* IRPAUSE */
83 int tap_move_map
[16] = {
84 0, -1, -1, 2, -1, 3, -1, -1,
85 1, -1, -1, 4, -1, 5, -1, -1
88 tap_transition_t tap_transitions
[16] =
90 {TAP_RESET
, TAP_IDLE
}, /* RESET */
91 {TAP_IRSELECT
, TAP_DRCAPTURE
}, /* DRSELECT */
92 {TAP_DREXIT1
, TAP_DRSHIFT
}, /* DRCAPTURE */
93 {TAP_DREXIT1
, TAP_DRSHIFT
}, /* DRSHIFT */
94 {TAP_DRUPDATE
, TAP_DRPAUSE
}, /* DREXIT1 */
95 {TAP_DREXIT2
, TAP_DRPAUSE
}, /* DRPAUSE */
96 {TAP_DRUPDATE
, TAP_DRSHIFT
}, /* DREXIT2 */
97 {TAP_DRSELECT
, TAP_IDLE
}, /* DRUPDATE */
98 {TAP_DRSELECT
, TAP_IDLE
}, /* IDLE */
99 {TAP_RESET
, TAP_IRCAPTURE
}, /* IRSELECT */
100 {TAP_IREXIT1
, TAP_IRSHIFT
}, /* IRCAPTURE */
101 {TAP_IREXIT1
, TAP_IRSHIFT
}, /* IRSHIFT */
102 {TAP_IRUPDATE
, TAP_IRPAUSE
}, /* IREXIT1 */
103 {TAP_IREXIT2
, TAP_IRPAUSE
}, /* IRPAUSE */
104 {TAP_IRUPDATE
, TAP_IRSHIFT
}, /* IREXIT2 */
105 {TAP_DRSELECT
, TAP_IDLE
} /* IRUPDATE */
108 char* jtag_event_strings
[] =
110 "JTAG controller reset (RESET or TRST)"
113 const Jim_Nvp nvp_jtag_tap_event
[] = {
114 { .value
= JTAG_TAP_EVENT_ENABLE
, .name
= "tap-enable" },
115 { .value
= JTAG_TAP_EVENT_DISABLE
, .name
= "tap-disable" },
117 { .name
= NULL
, .value
= -1 }
120 /* kludge!!!! these are just global variables that the
121 * interface use internally. They really belong
122 * inside the drivers, but we don't want to break
123 * linking the drivers!!!!
125 enum tap_state end_state
= TAP_RESET
;
126 enum tap_state cur_state
= TAP_RESET
;
130 jtag_command_t
*jtag_command_queue
= NULL
;
131 jtag_command_t
**last_comand_pointer
= &jtag_command_queue
;
132 static jtag_tap_t
*jtag_all_taps
= NULL
;
134 enum reset_types jtag_reset_config
= RESET_NONE
;
135 enum tap_state cmd_queue_end_state
= TAP_RESET
;
136 enum tap_state cmd_queue_cur_state
= TAP_RESET
;
138 int jtag_verify_capture_ir
= 1;
140 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
141 int jtag_nsrst_delay
= 0; /* default to no nSRST delay */
142 int jtag_ntrst_delay
= 0; /* default to no nTRST delay */
144 /* maximum number of JTAG devices expected in the chain
146 #define JTAG_MAX_CHAIN_SIZE 20
148 /* callbacks to inform high-level handlers about JTAG state changes */
149 jtag_event_callback_t
*jtag_event_callbacks
;
152 static int speed_khz
= 0;
153 /* flag if the kHz speed was defined */
154 static int hasKHz
= 0;
156 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
159 #if BUILD_ECOSBOARD == 1
160 extern jtag_interface_t zy1000_interface
;
163 #if BUILD_PARPORT == 1
164 extern jtag_interface_t parport_interface
;
168 extern jtag_interface_t dummy_interface
;
171 #if BUILD_FT2232_FTD2XX == 1
172 extern jtag_interface_t ft2232_interface
;
175 #if BUILD_FT2232_LIBFTDI == 1
176 extern jtag_interface_t ft2232_interface
;
179 #if BUILD_AMTJTAGACCEL == 1
180 extern jtag_interface_t amt_jtagaccel_interface
;
183 #if BUILD_EP93XX == 1
184 extern jtag_interface_t ep93xx_interface
;
187 #if BUILD_AT91RM9200 == 1
188 extern jtag_interface_t at91rm9200_interface
;
191 #if BUILD_GW16012 == 1
192 extern jtag_interface_t gw16012_interface
;
195 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
196 extern jtag_interface_t presto_interface
;
199 #if BUILD_USBPROG == 1
200 extern jtag_interface_t usbprog_interface
;
204 extern jtag_interface_t jlink_interface
;
207 jtag_interface_t
*jtag_interfaces
[] = {
208 #if BUILD_ECOSBOARD == 1
211 #if BUILD_PARPORT == 1
217 #if BUILD_FT2232_FTD2XX == 1
220 #if BUILD_FT2232_LIBFTDI == 1
223 #if BUILD_AMTJTAGACCEL == 1
224 &amt_jtagaccel_interface
,
226 #if BUILD_EP93XX == 1
229 #if BUILD_AT91RM9200 == 1
230 &at91rm9200_interface
,
232 #if BUILD_GW16012 == 1
235 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
238 #if BUILD_USBPROG == 1
247 jtag_interface_t
*jtag
= NULL
;
250 jtag_interface_t
*jtag_interface
= NULL
;
253 /* forward declarations */
254 void jtag_add_pathmove(int num_states
, enum tap_state
*path
);
255 void jtag_add_runtest(int num_cycles
, enum tap_state endstate
);
256 void jtag_add_end_state(enum tap_state endstate
);
257 void jtag_add_sleep(u32 us
);
258 int jtag_execute_queue(void);
262 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
263 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
264 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
265 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
266 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
267 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
268 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
270 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
272 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
273 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
274 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
275 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
276 int Jim_Command_drscan(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
278 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
280 jtag_tap_t
*jtag_AllTaps(void)
282 return jtag_all_taps
;
285 int jtag_NumTotalTaps(void)
299 int jtag_NumEnabledTaps(void)
316 jtag_tap_t
*jtag_TapByString( const char *s
)
324 if( 0 == strcmp( t
->dotted_name
, s
) ){
330 // backup plan is by number
332 /* ok - is "s" a number? */
334 n
= strtol( s
, &cp
, 0 );
335 if( (s
!= cp
) && (*cp
== 0) ){
337 t
= jtag_TapByAbsPosition(n
);
343 jtag_tap_t
* jtag_TapByJimObj( Jim_Interp
*interp
, Jim_Obj
*o
)
348 cp
= Jim_GetString( o
, NULL
);
353 t
= jtag_TapByString( cp
);
356 Jim_SetResult_sprintf(interp
,"Tap: %s is unknown", cp
);
361 /* returns a pointer to the n-th device in the scan chain */
362 jtag_tap_t
* jtag_TapByAbsPosition( int n
)
370 while( t
&& (n
> 0)) {
377 int jtag_register_event_callback(int (*callback
)(enum jtag_event event
, void *priv
), void *priv
)
379 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
381 if (callback
== NULL
)
383 return ERROR_INVALID_ARGUMENTS
;
388 while ((*callbacks_p
)->next
)
389 callbacks_p
= &((*callbacks_p
)->next
);
390 callbacks_p
= &((*callbacks_p
)->next
);
393 (*callbacks_p
) = malloc(sizeof(jtag_event_callback_t
));
394 (*callbacks_p
)->callback
= callback
;
395 (*callbacks_p
)->priv
= priv
;
396 (*callbacks_p
)->next
= NULL
;
401 int jtag_unregister_event_callback(int (*callback
)(enum jtag_event event
, void *priv
))
403 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
405 if (callback
== NULL
)
407 return ERROR_INVALID_ARGUMENTS
;
412 jtag_event_callback_t
**next
= &((*callbacks_p
)->next
);
413 if ((*callbacks_p
)->callback
== callback
)
416 *callbacks_p
= *next
;
424 int jtag_call_event_callbacks(enum jtag_event event
)
426 jtag_event_callback_t
*callback
= jtag_event_callbacks
;
428 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
432 callback
->callback(event
, callback
->priv
);
433 callback
= callback
->next
;
439 /* returns a pointer to the pointer of the last command in queue
440 * this may be a pointer to the root pointer (jtag_command_queue)
441 * or to the next member of the last but one command
443 jtag_command_t
** jtag_get_last_command_p(void)
445 /* jtag_command_t *cmd = jtag_command_queue;
451 return &jtag_command_queue;
455 return last_comand_pointer
;
458 void* cmd_queue_alloc(size_t size
)
460 cmd_queue_page_t
**p_page
= &cmd_queue_pages
;
466 * We align/round the *SIZE* per below
467 * so that all pointers returned by
468 * this function are reasonably well
471 * If we did not, then an "odd-length" request would cause the
472 * *next* allocation to be at an *odd* address, and because
473 * this function has the same type of api as malloc() - we
474 * must also return pointers that have the same type of
477 * What I do not/have is a reasonable portable means
480 * The solution here, is based on these suggestions.
481 * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
484 union worse_case_align
{
490 #define ALIGN_SIZE (sizeof(union worse_case_align))
492 /* The alignment process. */
493 size
= (size
+ ALIGN_SIZE
-1) & (~(ALIGN_SIZE
-1));
498 while ((*p_page
)->next
)
499 p_page
= &((*p_page
)->next
);
500 if (CMD_QUEUE_PAGE_SIZE
- (*p_page
)->used
< size
)
501 p_page
= &((*p_page
)->next
);
506 *p_page
= malloc(sizeof(cmd_queue_page_t
));
508 (*p_page
)->address
= malloc(CMD_QUEUE_PAGE_SIZE
);
509 (*p_page
)->next
= NULL
;
512 offset
= (*p_page
)->used
;
513 (*p_page
)->used
+= size
;
515 t
=(u8
*)((*p_page
)->address
);
519 void cmd_queue_free(void)
521 cmd_queue_page_t
*page
= cmd_queue_pages
;
525 cmd_queue_page_t
*last
= page
;
531 cmd_queue_pages
= NULL
;
534 static void jtag_prelude1(void)
538 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
539 jtag_error
=ERROR_JTAG_TRST_ASSERTED
;
543 if (cmd_queue_end_state
== TAP_RESET
)
544 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
547 static void jtag_prelude(enum tap_state state
)
552 jtag_add_end_state(state
);
554 cmd_queue_cur_state
= cmd_queue_end_state
;
557 void jtag_add_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
563 retval
=interface_jtag_add_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
564 if (retval
!=ERROR_OK
)
568 int MINIDRIVER(interface_jtag_add_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
570 jtag_command_t
**last_cmd
;
578 last_cmd
= jtag_get_last_command_p();
580 /* allocate memory for a new list member */
581 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
582 (*last_cmd
)->next
= NULL
;
583 last_comand_pointer
= &((*last_cmd
)->next
);
584 (*last_cmd
)->type
= JTAG_SCAN
;
586 /* allocate memory for ir scan command */
587 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
588 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
589 x
= jtag_NumEnabledTaps();
590 (*last_cmd
)->cmd
.scan
->num_fields
= x
; /* one field per device */
591 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(x
* sizeof(scan_field_t
));
592 (*last_cmd
)->cmd
.scan
->end_state
= state
;
599 // do this here so it is not forgotten
600 tap
= jtag_NextEnabledTap(tap
);
605 scan_size
= tap
->ir_length
;
606 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].tap
= tap
;
607 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].num_bits
= scan_size
;
608 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_value
= NULL
;
609 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_handler
= NULL
; /* disable verification by default */
611 /* search the list */
612 for (j
= 0; j
< num_fields
; j
++)
614 if (tap
== fields
[j
].tap
)
617 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
618 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
620 if (jtag_verify_capture_ir
)
622 if (fields
[j
].in_handler
==NULL
)
624 jtag_set_check_value((*last_cmd
)->cmd
.scan
->fields
+nth_tap
, tap
->expected
, tap
->expected_mask
, NULL
);
627 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_handler
= fields
[j
].in_handler
;
628 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_handler_priv
= fields
[j
].in_handler_priv
;
629 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_check_value
= tap
->expected
;
630 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_check_mask
= tap
->expected_mask
;
641 /* if a tap isn't listed, set it to BYPASS */
642 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_value
= buf_set_ones(cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
643 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_mask
= NULL
;
647 /* update device information */
648 buf_cpy((*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_value
, tap
->cur_instr
, scan_size
);
654 void jtag_add_plain_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
660 retval
=interface_jtag_add_plain_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
661 if (retval
!=ERROR_OK
)
665 int MINIDRIVER(interface_jtag_add_plain_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
668 jtag_command_t
**last_cmd
;
670 last_cmd
= jtag_get_last_command_p();
672 /* allocate memory for a new list member */
673 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
674 (*last_cmd
)->next
= NULL
;
675 last_comand_pointer
= &((*last_cmd
)->next
);
676 (*last_cmd
)->type
= JTAG_SCAN
;
678 /* allocate memory for ir scan command */
679 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
680 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
681 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
682 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
683 (*last_cmd
)->cmd
.scan
->end_state
= state
;
685 for( i
= 0 ; i
< num_fields
; i
++ ){
686 int num_bits
= fields
[i
].num_bits
;
687 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
688 (*last_cmd
)->cmd
.scan
->fields
[i
].tap
= fields
[i
].tap
;
689 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
690 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
691 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
692 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
693 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
694 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
695 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= NULL
;
696 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= NULL
;
701 void jtag_add_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
707 retval
=interface_jtag_add_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
708 if (retval
!=ERROR_OK
)
712 int MINIDRIVER(interface_jtag_add_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
716 int bypass_devices
= 0;
720 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
723 /* count devices in bypass */
727 tap
= jtag_NextEnabledTap(tap
);
736 /* allocate memory for a new list member */
737 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
738 last_comand_pointer
= &((*last_cmd
)->next
);
739 (*last_cmd
)->next
= NULL
;
740 (*last_cmd
)->type
= JTAG_SCAN
;
742 /* allocate memory for dr scan command */
743 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
744 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
745 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
746 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
747 (*last_cmd
)->cmd
.scan
->end_state
= state
;
753 tap
= jtag_NextEnabledTap(tap
);
758 (*last_cmd
)->cmd
.scan
->fields
[field_count
].tap
= tap
;
760 for (j
= 0; j
< num_fields
; j
++)
762 if (tap
== fields
[j
].tap
)
765 scan_size
= fields
[j
].num_bits
;
766 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
767 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
768 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
769 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= fields
[j
].in_value
;
770 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= fields
[j
].in_check_value
;
771 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= fields
[j
].in_check_mask
;
772 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= fields
[j
].in_handler
;
773 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= fields
[j
].in_handler_priv
;
778 #ifdef _DEBUG_JTAG_IO_
779 /* if a device isn't listed, the BYPASS register should be selected */
782 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
786 /* program the scan field to 1 bit length, and ignore it's value */
787 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
788 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
789 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
790 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
791 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
792 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
793 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
794 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
798 #ifdef _DEBUG_JTAG_IO_
799 /* if a device is listed, the BYPASS register must not be selected */
802 LOG_ERROR("BUG: scan data for a device in BYPASS");
811 void MINIDRIVER(interface_jtag_add_dr_out
)(jtag_tap_t
*target_tap
,
815 enum tap_state end_state
)
820 int bypass_devices
= 0;
822 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
825 /* count devices in bypass */
829 tap
= jtag_NextEnabledTap(tap
);
838 /* allocate memory for a new list member */
839 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
840 last_comand_pointer
= &((*last_cmd
)->next
);
841 (*last_cmd
)->next
= NULL
;
842 (*last_cmd
)->type
= JTAG_SCAN
;
844 /* allocate memory for dr scan command */
845 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
846 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
847 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
848 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
849 (*last_cmd
)->cmd
.scan
->end_state
= end_state
;
854 tap
= jtag_NextEnabledTap(tap
);
859 (*last_cmd
)->cmd
.scan
->fields
[field_count
].tap
= tap
;
861 if (tap
== target_tap
)
864 #ifdef _DEBUG_JTAG_IO_
865 /* if a device is listed, the BYPASS register must not be selected */
868 LOG_ERROR("BUG: scan data for a device in BYPASS");
872 for (j
= 0; j
< num_fields
; j
++)
875 scan_size
= num_bits
[j
];
876 buf_set_u32(out_value
, 0, scan_size
, value
[j
]);
877 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
878 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
879 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
880 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
881 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
882 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
883 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
884 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
888 #ifdef _DEBUG_JTAG_IO_
889 /* if a device isn't listed, the BYPASS register should be selected */
892 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
896 /* program the scan field to 1 bit length, and ignore it's value */
897 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
898 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
899 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
900 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
901 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
902 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
903 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
904 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
909 void jtag_add_plain_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
915 retval
=interface_jtag_add_plain_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
916 if (retval
!=ERROR_OK
)
920 int MINIDRIVER(interface_jtag_add_plain_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
923 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
925 /* allocate memory for a new list member */
926 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
927 last_comand_pointer
= &((*last_cmd
)->next
);
928 (*last_cmd
)->next
= NULL
;
929 (*last_cmd
)->type
= JTAG_SCAN
;
931 /* allocate memory for scan command */
932 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
933 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
934 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
935 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
936 (*last_cmd
)->cmd
.scan
->end_state
= state
;
938 for (i
= 0; i
< num_fields
; i
++)
940 int num_bits
= fields
[i
].num_bits
;
941 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
942 (*last_cmd
)->cmd
.scan
->fields
[i
].tap
= fields
[i
].tap
;
943 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
944 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
945 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
946 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
947 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
948 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
949 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= fields
[i
].in_handler
;
950 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= fields
[i
].in_handler_priv
;
956 void jtag_add_tlr(void)
958 jtag_prelude(TAP_RESET
);
961 retval
=interface_jtag_add_tlr();
962 if (retval
!=ERROR_OK
)
966 int MINIDRIVER(interface_jtag_add_tlr
)()
968 enum tap_state state
= TAP_RESET
;
969 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
971 /* allocate memory for a new list member */
972 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
973 last_comand_pointer
= &((*last_cmd
)->next
);
974 (*last_cmd
)->next
= NULL
;
975 (*last_cmd
)->type
= JTAG_STATEMOVE
;
977 (*last_cmd
)->cmd
.statemove
= cmd_queue_alloc(sizeof(statemove_command_t
));
978 (*last_cmd
)->cmd
.statemove
->end_state
= state
;
984 void jtag_add_pathmove(int num_states
, enum tap_state
*path
)
986 enum tap_state cur_state
=cmd_queue_cur_state
;
990 /* the last state has to be a stable state */
991 if (tap_move_map
[path
[num_states
- 1]] == -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");
1004 if ((tap_transitions
[cur_state
].low
!= path
[i
])&&
1005 (tap_transitions
[cur_state
].high
!= path
[i
]))
1007 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings
[cur_state
], tap_state_strings
[path
[i
]]);
1010 cur_state
= path
[i
];
1015 retval
=interface_jtag_add_pathmove(num_states
, path
);
1016 cmd_queue_cur_state
= path
[num_states
- 1];
1017 if (retval
!=ERROR_OK
)
1021 int MINIDRIVER(interface_jtag_add_pathmove
)(int num_states
, enum tap_state
*path
)
1023 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1026 /* allocate memory for a new list member */
1027 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1028 last_comand_pointer
= &((*last_cmd
)->next
);
1029 (*last_cmd
)->next
= NULL
;
1030 (*last_cmd
)->type
= JTAG_PATHMOVE
;
1032 (*last_cmd
)->cmd
.pathmove
= cmd_queue_alloc(sizeof(pathmove_command_t
));
1033 (*last_cmd
)->cmd
.pathmove
->num_states
= num_states
;
1034 (*last_cmd
)->cmd
.pathmove
->path
= cmd_queue_alloc(sizeof(enum tap_state
) * num_states
);
1036 for (i
= 0; i
< num_states
; i
++)
1037 (*last_cmd
)->cmd
.pathmove
->path
[i
] = path
[i
];
1042 int MINIDRIVER(interface_jtag_add_runtest
)(int num_cycles
, enum tap_state state
)
1044 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1046 /* allocate memory for a new list member */
1047 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1048 (*last_cmd
)->next
= NULL
;
1049 last_comand_pointer
= &((*last_cmd
)->next
);
1050 (*last_cmd
)->type
= JTAG_RUNTEST
;
1052 (*last_cmd
)->cmd
.runtest
= cmd_queue_alloc(sizeof(runtest_command_t
));
1053 (*last_cmd
)->cmd
.runtest
->num_cycles
= num_cycles
;
1054 (*last_cmd
)->cmd
.runtest
->end_state
= state
;
1059 void jtag_add_runtest(int num_cycles
, enum tap_state state
)
1063 jtag_prelude(state
);
1065 /* executed by sw or hw fifo */
1066 retval
=interface_jtag_add_runtest(num_cycles
, cmd_queue_end_state
);
1067 if (retval
!=ERROR_OK
)
1071 void jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
1073 int trst_with_tlr
= 0;
1076 /* FIX!!! there are *many* different cases here. A better
1077 * approach is needed for legal combinations of transitions...
1079 if ((jtag_reset_config
& RESET_HAS_SRST
)&&
1080 (jtag_reset_config
& RESET_HAS_TRST
)&&
1081 ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0))
1083 if (((req_tlr_or_trst
&&!jtag_trst
)||
1084 (!req_tlr_or_trst
&&jtag_trst
))&&
1085 ((req_srst
&&!jtag_srst
)||
1086 (!req_srst
&&jtag_srst
)))
1088 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
1089 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
1093 /* Make sure that jtag_reset_config allows the requested reset */
1094 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
1095 if (((jtag_reset_config
& RESET_SRST_PULLS_TRST
) && (req_srst
== 1)) && (!req_tlr_or_trst
))
1097 LOG_ERROR("BUG: requested reset would assert trst");
1098 jtag_error
=ERROR_FAIL
;
1102 /* if TRST pulls SRST, we reset with TAP T-L-R */
1103 if (((jtag_reset_config
& RESET_TRST_PULLS_SRST
) && (req_tlr_or_trst
)) && (req_srst
== 0))
1108 if (req_srst
&& !(jtag_reset_config
& RESET_HAS_SRST
))
1110 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
1111 jtag_error
=ERROR_FAIL
;
1115 if (req_tlr_or_trst
)
1117 if (!trst_with_tlr
&& (jtag_reset_config
& RESET_HAS_TRST
))
1129 jtag_srst
= req_srst
;
1131 retval
= interface_jtag_add_reset(jtag_trst
, jtag_srst
);
1132 if (retval
!=ERROR_OK
)
1140 LOG_DEBUG("SRST line asserted");
1144 LOG_DEBUG("SRST line released");
1145 if (jtag_nsrst_delay
)
1146 jtag_add_sleep(jtag_nsrst_delay
* 1000);
1151 LOG_DEBUG("JTAG reset with RESET instead of TRST");
1152 jtag_add_end_state(TAP_RESET
);
1154 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
1160 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1161 * and inform possible listeners about this
1163 LOG_DEBUG("TRST line asserted");
1164 cmd_queue_cur_state
= TAP_RESET
;
1165 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
1169 if (jtag_ntrst_delay
)
1170 jtag_add_sleep(jtag_ntrst_delay
* 1000);
1174 int MINIDRIVER(interface_jtag_add_reset
)(int req_trst
, int req_srst
)
1176 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1178 /* allocate memory for a new list member */
1179 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1180 (*last_cmd
)->next
= NULL
;
1181 last_comand_pointer
= &((*last_cmd
)->next
);
1182 (*last_cmd
)->type
= JTAG_RESET
;
1184 (*last_cmd
)->cmd
.reset
= cmd_queue_alloc(sizeof(reset_command_t
));
1185 (*last_cmd
)->cmd
.reset
->trst
= req_trst
;
1186 (*last_cmd
)->cmd
.reset
->srst
= req_srst
;
1191 void jtag_add_end_state(enum tap_state state
)
1193 cmd_queue_end_state
= state
;
1194 if ((cmd_queue_end_state
== TAP_DRSHIFT
)||(cmd_queue_end_state
== TAP_IRSHIFT
))
1196 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
1200 int MINIDRIVER(interface_jtag_add_sleep
)(u32 us
)
1202 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1204 /* allocate memory for a new list member */
1205 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1206 (*last_cmd
)->next
= NULL
;
1207 last_comand_pointer
= &((*last_cmd
)->next
);
1208 (*last_cmd
)->type
= JTAG_SLEEP
;
1210 (*last_cmd
)->cmd
.sleep
= cmd_queue_alloc(sizeof(sleep_command_t
));
1211 (*last_cmd
)->cmd
.sleep
->us
= us
;
1216 void jtag_add_sleep(u32 us
)
1218 keep_alive(); /* we might be running on a very slow JTAG clk */
1219 int retval
=interface_jtag_add_sleep(us
);
1220 if (retval
!=ERROR_OK
)
1225 int jtag_scan_size(scan_command_t
*cmd
)
1230 /* count bits in scan command */
1231 for (i
= 0; i
< cmd
->num_fields
; i
++)
1233 bit_count
+= cmd
->fields
[i
].num_bits
;
1239 int jtag_build_buffer(scan_command_t
*cmd
, u8
**buffer
)
1244 bit_count
= jtag_scan_size(cmd
);
1245 *buffer
= malloc(CEIL(bit_count
, 8));
1249 for (i
= 0; i
< cmd
->num_fields
; i
++)
1251 if (cmd
->fields
[i
].out_value
)
1253 #ifdef _DEBUG_JTAG_IO_
1254 char* char_buf
= buf_to_str(cmd
->fields
[i
].out_value
, (cmd
->fields
[i
].num_bits
> 64) ? 64 : cmd
->fields
[i
].num_bits
, 16);
1256 buf_set_buf(cmd
->fields
[i
].out_value
, 0, *buffer
, bit_count
, cmd
->fields
[i
].num_bits
);
1257 #ifdef _DEBUG_JTAG_IO_
1258 LOG_DEBUG("fields[%i].out_value: 0x%s", i
, char_buf
);
1263 bit_count
+= cmd
->fields
[i
].num_bits
;
1269 int jtag_read_buffer(u8
*buffer
, scan_command_t
*cmd
)
1275 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1278 for (i
= 0; i
< cmd
->num_fields
; i
++)
1280 /* if neither in_value nor in_handler
1281 * are specified we don't have to examine this field
1283 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1285 int num_bits
= cmd
->fields
[i
].num_bits
;
1286 u8
*captured
= buf_set_buf(buffer
, bit_count
, malloc(CEIL(num_bits
, 8)), 0, num_bits
);
1288 #ifdef _DEBUG_JTAG_IO_
1291 char_buf
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1292 LOG_DEBUG("fields[%i].in_value: 0x%s", i
, char_buf
);
1296 if (cmd
->fields
[i
].in_value
)
1298 buf_cpy(captured
, cmd
->fields
[i
].in_value
, num_bits
);
1300 if (cmd
->fields
[i
].in_handler
)
1302 if (cmd
->fields
[i
].in_handler(cmd
->fields
[i
].in_value
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1304 LOG_WARNING("in_handler reported a failed check");
1305 retval
= ERROR_JTAG_QUEUE_FAILED
;
1310 /* no in_value specified, but a handler takes care of the scanned data */
1311 if (cmd
->fields
[i
].in_handler
&& (!cmd
->fields
[i
].in_value
))
1313 if (cmd
->fields
[i
].in_handler(captured
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1315 /* We're going to call the error:handler later, but if the in_handler
1316 * reported an error we report this failure upstream
1318 LOG_WARNING("in_handler reported a failed check");
1319 retval
= ERROR_JTAG_QUEUE_FAILED
;
1325 bit_count
+= cmd
->fields
[i
].num_bits
;
1331 int jtag_check_value(u8
*captured
, void *priv
, scan_field_t
*field
)
1333 int retval
= ERROR_OK
;
1334 int num_bits
= field
->num_bits
;
1336 int compare_failed
= 0;
1338 if (field
->in_check_mask
)
1339 compare_failed
= buf_cmp_mask(captured
, field
->in_check_value
, field
->in_check_mask
, num_bits
);
1341 compare_failed
= buf_cmp(captured
, field
->in_check_value
, num_bits
);
1343 if (compare_failed
){
1344 /* An error handler could have caught the failing check
1345 * only report a problem when there wasn't a handler, or if the handler
1346 * acknowledged the error
1348 LOG_WARNING("TAP %s:",
1349 (field
->tap
== NULL
) ? "(unknown)" : field
->tap
->dotted_name
);
1352 char *captured_char
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1353 char *in_check_value_char
= buf_to_str(field
->in_check_value
, (num_bits
> 64) ? 64 : num_bits
, 16);
1355 if (field
->in_check_mask
)
1357 char *in_check_mask_char
;
1358 in_check_mask_char
= buf_to_str(field
->in_check_mask
, (num_bits
> 64) ? 64 : num_bits
, 16);
1359 LOG_WARNING("value captured during scan didn't pass the requested check:");
1360 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
1361 captured_char
, in_check_value_char
, in_check_mask_char
);
1362 free(in_check_mask_char
);
1366 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
);
1369 free(captured_char
);
1370 free(in_check_value_char
);
1372 retval
= ERROR_JTAG_QUEUE_FAILED
;
1380 set up checking of this field using the in_handler. The values passed in must be valid until
1381 after jtag_execute() has completed.
1383 void jtag_set_check_value(scan_field_t
*field
, u8
*value
, u8
*mask
, error_handler_t
*in_error_handler
)
1386 field
->in_handler
= jtag_check_value
;
1388 field
->in_handler
= NULL
; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1389 field
->in_handler_priv
= NULL
;
1390 field
->in_check_value
= value
;
1391 field
->in_check_mask
= mask
;
1394 enum scan_type
jtag_scan_type(scan_command_t
*cmd
)
1399 for (i
= 0; i
< cmd
->num_fields
; i
++)
1401 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1403 if (cmd
->fields
[i
].out_value
)
1410 int MINIDRIVER(interface_jtag_execute_queue
)(void)
1416 LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
1420 retval
= jtag
->execute_queue();
1424 jtag_command_queue
= NULL
;
1425 last_comand_pointer
= &jtag_command_queue
;
1430 int jtag_execute_queue(void)
1432 int retval
=interface_jtag_execute_queue();
1433 if (retval
==ERROR_OK
)
1437 jtag_error
=ERROR_OK
;
1441 int jtag_reset_callback(enum jtag_event event
, void *priv
)
1443 jtag_tap_t
*tap
= priv
;
1447 if (event
== JTAG_TRST_ASSERTED
)
1449 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
1456 void jtag_sleep(u32 us
)
1458 alive_sleep(us
/1000);
1461 /* Try to examine chain layout according to IEEE 1149.1 §12
1463 int jtag_examine_chain(void)
1467 u8 idcode_buffer
[JTAG_MAX_CHAIN_SIZE
* 4];
1470 int device_count
= 0;
1471 u8 zero_check
= 0x0;
1472 u8 one_check
= 0xff;
1475 field
.num_bits
= sizeof(idcode_buffer
) * 8;
1476 field
.out_value
= idcode_buffer
;
1477 field
.out_mask
= NULL
;
1478 field
.in_value
= idcode_buffer
;
1479 field
.in_check_value
= NULL
;
1480 field
.in_check_mask
= NULL
;
1481 field
.in_handler
= NULL
;
1482 field
.in_handler_priv
= NULL
;
1484 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
; i
++)
1486 buf_set_u32(idcode_buffer
, i
* 32, 32, 0x000000FF);
1489 jtag_add_plain_dr_scan(1, &field
, TAP_RESET
);
1490 jtag_execute_queue();
1492 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
* 4; i
++)
1494 zero_check
|= idcode_buffer
[i
];
1495 one_check
&= idcode_buffer
[i
];
1498 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1499 if ((zero_check
== 0x00) || (one_check
== 0xff))
1501 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1502 return ERROR_JTAG_INIT_FAILED
;
1505 // point at the 1st tap
1506 tap
= jtag_NextEnabledTap(NULL
);
1508 LOG_ERROR("JTAG: No taps enabled?");
1509 return ERROR_JTAG_INIT_FAILED
;
1512 for (bit_count
= 0; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;)
1514 u32 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1515 if ((idcode
& 1) == 0)
1517 /* LSB must not be 0, this indicates a device in bypass */
1518 LOG_WARNING("Tap/Device does not have IDCODE");
1529 if (idcode
== 0x000000FF)
1532 /* End of chain (invalid manufacturer ID)
1534 * The JTAG examine is the very first thing that happens
1536 * A single JTAG device requires only 64 bits to be read back correctly.
1538 * The code below adds a check that the rest of the data scanned (640 bits)
1539 * are all as expected. This helps diagnose/catch problems with the JTAG chain
1541 * earlier and gives more helpful/explicit error messages.
1543 for (bit_count
+= 32; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;bit_count
+= 32)
1545 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1546 if (unexpected
||(idcode
!= 0x000000FF))
1548 LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count
, idcode
);
1556 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
1557 manufacturer
= EXTRACT_MFG(idcode
);
1558 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
1559 part
= EXTRACT_PART(idcode
);
1560 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
1561 version
= EXTRACT_VER(idcode
);
1563 LOG_INFO("JTAG tap: %s tap/device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1564 ((tap
!= NULL
) ? (tap
->dotted_name
) : "(not-named)"),
1565 idcode
, manufacturer
, part
, version
);
1571 tap
->idcode
= idcode
;
1573 if (tap
->expected_ids_cnt
> 0) {
1574 /* Loop over the expected identification codes and test for a match */
1576 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++) {
1577 if( tap
->idcode
== tap
->expected_ids
[ii
] ){
1582 /* If none of the expected ids matched, log an error */
1583 if (ii
== tap
->expected_ids_cnt
) {
1584 LOG_ERROR("JTAG tap: %s got: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1587 EXTRACT_MFG( tap
->idcode
),
1588 EXTRACT_PART( tap
->idcode
),
1589 EXTRACT_VER( tap
->idcode
) );
1590 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++) {
1591 LOG_ERROR("JTAG tap: %s expected %hhu of %hhu: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1594 tap
->expected_ids_cnt
,
1595 tap
->expected_ids
[ii
],
1596 EXTRACT_MFG( tap
->expected_ids
[ii
] ),
1597 EXTRACT_PART( tap
->expected_ids
[ii
] ),
1598 EXTRACT_VER( tap
->expected_ids
[ii
] ) );
1601 return ERROR_JTAG_INIT_FAILED
;
1603 LOG_INFO("JTAG Tap/device matched");
1607 LOG_INFO("JTAG TAP ID: 0x%08x - Unknown - please report (A) chipname and (B) idcode to the openocd project",
1611 tap
= jtag_NextEnabledTap(tap
);
1616 /* see if number of discovered devices matches configuration */
1617 if (device_count
!= jtag_NumEnabledTaps())
1619 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match (enabled) configuration (%i), total taps: %d",
1620 device_count
, jtag_NumEnabledTaps(), jtag_NumTotalTaps());
1621 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1622 return ERROR_JTAG_INIT_FAILED
;
1628 int jtag_validate_chain(void)
1631 int total_ir_length
= 0;
1637 total_ir_length
= 0;
1639 tap
= jtag_NextEnabledTap(tap
);
1643 total_ir_length
+= tap
->ir_length
;
1646 total_ir_length
+= 2;
1647 ir_test
= malloc(CEIL(total_ir_length
, 8));
1648 buf_set_ones(ir_test
, total_ir_length
);
1651 field
.num_bits
= total_ir_length
;
1652 field
.out_value
= ir_test
;
1653 field
.out_mask
= NULL
;
1654 field
.in_value
= ir_test
;
1655 field
.in_check_value
= NULL
;
1656 field
.in_check_mask
= NULL
;
1657 field
.in_handler
= NULL
;
1658 field
.in_handler_priv
= NULL
;
1660 jtag_add_plain_ir_scan(1, &field
, TAP_RESET
);
1661 jtag_execute_queue();
1666 tap
= jtag_NextEnabledTap(tap
);
1672 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x1)
1674 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1675 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1678 return ERROR_JTAG_INIT_FAILED
;
1680 chain_pos
+= tap
->ir_length
;
1683 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x3)
1685 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1686 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1689 return ERROR_JTAG_INIT_FAILED
;
1697 enum jtag_tap_cfg_param
{
1701 static Jim_Nvp nvp_config_opts
[] = {
1702 { .name
= "-event", .value
= JCFG_EVENT
},
1704 { .name
= NULL
, .value
= -1 }
1708 jtag_tap_configure_cmd( Jim_GetOptInfo
*goi
,
1715 /* parse config or cget options */
1716 while (goi
->argc
> 0) {
1717 Jim_SetEmptyResult (goi
->interp
);
1719 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
1721 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
1727 if (goi
->argc
== 0) {
1728 Jim_WrongNumArgs( goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ..." );
1732 e
= Jim_GetOpt_Nvp( goi
, nvp_jtag_tap_event
, &n
);
1734 Jim_GetOpt_NvpUnknown(goi
, nvp_jtag_tap_event
, 1);
1738 if (goi
->isconfigure
) {
1739 if (goi
->argc
!= 1) {
1740 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
1744 if (goi
->argc
!= 0) {
1745 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
1751 jtag_tap_event_action_t
*jteap
;
1753 jteap
= tap
->event_action
;
1754 /* replace existing? */
1756 if (jteap
->event
== n
->value
) {
1759 jteap
= jteap
->next
;
1762 if (goi
->isconfigure
) {
1763 if (jteap
== NULL
) {
1765 jteap
= calloc(1, sizeof (*jteap
));
1767 jteap
->event
= n
->value
;
1768 Jim_GetOpt_Obj( goi
, &o
);
1770 Jim_DecrRefCount(interp
, jteap
->body
);
1772 jteap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
1773 Jim_IncrRefCount(jteap
->body
);
1775 /* add to head of event list */
1776 jteap
->next
= tap
->event_action
;
1777 tap
->event_action
= jteap
;
1778 Jim_SetEmptyResult(goi
->interp
);
1781 if (jteap
== NULL
) {
1782 Jim_SetEmptyResult(goi
->interp
);
1784 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, jteap
->body
));
1791 } /* while (goi->argc) */
1796 static int jim_newtap_cmd( Jim_GetOptInfo
*goi
)
1806 const Jim_Nvp opts
[] = {
1807 #define NTAP_OPT_IRLEN 0
1808 { .name
= "-irlen" , .value
= NTAP_OPT_IRLEN
},
1809 #define NTAP_OPT_IRMASK 1
1810 { .name
= "-irmask" , .value
= NTAP_OPT_IRMASK
},
1811 #define NTAP_OPT_IRCAPTURE 2
1812 { .name
= "-ircapture" , .value
= NTAP_OPT_IRCAPTURE
},
1813 #define NTAP_OPT_ENABLED 3
1814 { .name
= "-enable" , .value
= NTAP_OPT_ENABLED
},
1815 #define NTAP_OPT_DISABLED 4
1816 { .name
= "-disable" , .value
= NTAP_OPT_DISABLED
},
1817 #define NTAP_OPT_EXPECTED_ID 5
1818 { .name
= "-expected-id" , .value
= NTAP_OPT_EXPECTED_ID
},
1819 { .name
= NULL
, .value
= -1 },
1822 pTap
= malloc( sizeof(jtag_tap_t
) );
1823 memset( pTap
, 0, sizeof(*pTap
) );
1825 Jim_SetResult_sprintf( goi
->interp
, "no memory");
1829 * we expect CHIP + TAP + OPTIONS
1831 if( goi
->argc
< 3 ){
1832 Jim_SetResult_sprintf(goi
->interp
, "Missing CHIP TAP OPTIONS ....");
1835 Jim_GetOpt_String( goi
, &cp
, NULL
);
1836 pTap
->chip
= strdup(cp
);
1838 Jim_GetOpt_String( goi
, &cp
, NULL
);
1839 pTap
->tapname
= strdup(cp
);
1841 /* name + dot + name + null */
1842 x
= strlen(pTap
->chip
) + 1 + strlen(pTap
->tapname
) + 1;
1844 sprintf( cp
, "%s.%s", pTap
->chip
, pTap
->tapname
);
1845 pTap
->dotted_name
= cp
;
1847 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
1848 pTap
->chip
, pTap
->tapname
, pTap
->dotted_name
, goi
->argc
);
1850 /* default is enabled */
1853 /* deal with options */
1854 #define NTREQ_IRLEN 1
1855 #define NTREQ_IRCAPTURE 2
1856 #define NTREQ_IRMASK 4
1858 /* clear them as we find them */
1859 reqbits
= (NTREQ_IRLEN
| NTREQ_IRCAPTURE
| NTREQ_IRMASK
);
1862 e
= Jim_GetOpt_Nvp( goi
, opts
, &n
);
1864 Jim_GetOpt_NvpUnknown( goi
, opts
, 0 );
1867 LOG_DEBUG("Processing option: %s", n
->name
);
1869 case NTAP_OPT_ENABLED
:
1872 case NTAP_OPT_DISABLED
:
1875 case NTAP_OPT_EXPECTED_ID
:
1877 u32
*new_expected_ids
;
1879 e
= Jim_GetOpt_Wide( goi
, &w
);
1881 Jim_SetResult_sprintf(goi
->interp
, "option: %s bad parameter", n
->name
);
1885 new_expected_ids
= malloc(sizeof(u32
) * (pTap
->expected_ids_cnt
+ 1));
1886 if (new_expected_ids
== NULL
) {
1887 Jim_SetResult_sprintf( goi
->interp
, "no memory");
1891 memcpy(new_expected_ids
, pTap
->expected_ids
, sizeof(u32
) * pTap
->expected_ids_cnt
);
1893 new_expected_ids
[pTap
->expected_ids_cnt
] = w
;
1895 free(pTap
->expected_ids
);
1896 pTap
->expected_ids
= new_expected_ids
;
1897 pTap
->expected_ids_cnt
++;
1900 case NTAP_OPT_IRLEN
:
1901 case NTAP_OPT_IRMASK
:
1902 case NTAP_OPT_IRCAPTURE
:
1903 e
= Jim_GetOpt_Wide( goi
, &w
);
1905 Jim_SetResult_sprintf( goi
->interp
, "option: %s bad parameter", n
->name
);
1908 if( (w
< 0) || (w
> 0xffff) ){
1910 Jim_SetResult_sprintf( goi
->interp
, "option: %s - wacky value: %d (0x%x)",
1911 n
->name
, (int)(w
), (int)(w
));
1915 case NTAP_OPT_IRLEN
:
1916 pTap
->ir_length
= w
;
1917 reqbits
&= (~(NTREQ_IRLEN
));
1919 case NTAP_OPT_IRMASK
:
1920 pTap
->ir_capture_mask
= w
;
1921 reqbits
&= (~(NTREQ_IRMASK
));
1923 case NTAP_OPT_IRCAPTURE
:
1924 pTap
->ir_capture_value
= w
;
1925 reqbits
&= (~(NTREQ_IRCAPTURE
));
1928 } /* switch(n->value) */
1929 } /* while( goi->argc ) */
1931 /* Did we get all the options? */
1934 Jim_SetResult_sprintf( goi
->interp
,
1935 "newtap: %s missing required parameters",
1937 /* TODO: Tell user what is missing :-( */
1938 /* no memory leaks pelase */
1939 free(((void *)(pTap
->expected_ids
)));
1940 free(((void *)(pTap
->chip
)));
1941 free(((void *)(pTap
->tapname
)));
1942 free(((void *)(pTap
->dotted_name
)));
1943 free(((void *)(pTap
)));
1947 pTap
->expected
= malloc( pTap
->ir_length
);
1948 pTap
->expected_mask
= malloc( pTap
->ir_length
);
1949 pTap
->cur_instr
= malloc( pTap
->ir_length
);
1951 buf_set_u32( pTap
->expected
,
1954 pTap
->ir_capture_value
);
1955 buf_set_u32( pTap
->expected_mask
,
1958 pTap
->ir_capture_mask
);
1959 buf_set_ones( pTap
->cur_instr
,
1964 jtag_register_event_callback(jtag_reset_callback
, pTap
);
1966 ppTap
= &(jtag_all_taps
);
1967 while( (*ppTap
) != NULL
){
1968 ppTap
= &((*ppTap
)->next_tap
);
1972 static int n_taps
= 0;
1973 pTap
->abs_chain_position
= n_taps
++;
1975 LOG_DEBUG( "Created Tap: %s @ abs position %d, irlen %d, capture: 0x%x mask: 0x%x",
1976 (*ppTap
)->dotted_name
,
1977 (*ppTap
)->abs_chain_position
,
1978 (*ppTap
)->ir_length
,
1979 (*ppTap
)->ir_capture_value
,
1980 (*ppTap
)->ir_capture_mask
);
1985 static int jim_jtag_command( Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1991 struct command_context_s
*context
;
1995 JTAG_CMD_INIT_RESET
,
1998 JTAG_CMD_TAPDISABLE
,
1999 JTAG_CMD_TAPISENABLED
,
2004 const Jim_Nvp jtag_cmds
[] = {
2005 { .name
= "interface" , .value
= JTAG_CMD_INTERFACE
},
2006 { .name
= "arp_init-reset", .value
= JTAG_CMD_INIT_RESET
},
2007 { .name
= "newtap" , .value
= JTAG_CMD_NEWTAP
},
2008 { .name
= "tapisenabled" , .value
= JTAG_CMD_TAPISENABLED
},
2009 { .name
= "tapenable" , .value
= JTAG_CMD_TAPENABLE
},
2010 { .name
= "tapdisable" , .value
= JTAG_CMD_TAPDISABLE
},
2011 { .name
= "configure" , .value
= JTAG_CMD_CONFIGURE
},
2012 { .name
= "cget" , .value
= JTAG_CMD_CGET
},
2014 { .name
= NULL
, .value
= -1 },
2017 context
= Jim_GetAssocData(interp
, "context");
2018 /* go past the command */
2019 Jim_GetOpt_Setup( &goi
, interp
, argc
-1, argv
+1 );
2021 e
= Jim_GetOpt_Nvp( &goi
, jtag_cmds
, &n
);
2023 Jim_GetOpt_NvpUnknown( &goi
, jtag_cmds
, 0 );
2026 Jim_SetEmptyResult( goi
.interp
);
2028 case JTAG_CMD_INTERFACE
:
2029 /* return the name of the interface */
2030 /* TCL code might need to know the exact type... */
2031 /* FUTURE: we allow this as a means to "set" the interface. */
2032 if( goi
.argc
!= 0 ){
2033 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
-1, "(no params)");
2036 Jim_SetResultString( goi
.interp
, jtag_interface
->name
, -1 );
2038 case JTAG_CMD_INIT_RESET
:
2039 if( goi
.argc
!= 0 ){
2040 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
-1, "(no params)");
2043 e
= jtag_init_reset(context
);
2044 if( e
!= ERROR_OK
){
2045 Jim_SetResult_sprintf( goi
.interp
, "error: %d", e
);
2049 case JTAG_CMD_NEWTAP
:
2050 return jim_newtap_cmd( &goi
);
2052 case JTAG_CMD_TAPISENABLED
:
2053 case JTAG_CMD_TAPENABLE
:
2054 case JTAG_CMD_TAPDISABLE
:
2055 if( goi
.argc
!= 1 ){
2056 Jim_SetResultString( goi
.interp
, "Too many parameters",-1 );
2062 t
= jtag_TapByJimObj( goi
.interp
, goi
.argv
[0] );
2067 case JTAG_CMD_TAPISENABLED
:
2070 case JTAG_CMD_TAPENABLE
:
2071 jtag_tap_handle_event( t
, JTAG_TAP_EVENT_ENABLE
);
2075 case JTAG_CMD_TAPDISABLE
:
2076 jtag_tap_handle_event( t
, JTAG_TAP_EVENT_DISABLE
);
2081 Jim_SetResult( goi
.interp
, Jim_NewIntObj( goi
.interp
, e
) );
2088 Jim_WrongNumArgs( goi
.interp
, 0, NULL
, "?tap-name? -option ...");
2095 Jim_GetOpt_Obj(&goi
, &o
);
2096 t
= jtag_TapByJimObj( goi
.interp
, o
);
2101 goi
.isconfigure
= 0;
2102 return jtag_tap_configure_cmd( &goi
, t
);
2106 case JTAG_CMD_CONFIGURE
:
2108 Jim_WrongNumArgs( goi
.interp
, 0, NULL
, "?tap-name? -option ?VALUE? ...");
2115 Jim_GetOpt_Obj(&goi
, &o
);
2116 t
= jtag_TapByJimObj( goi
.interp
, o
);
2121 goi
.isconfigure
= 1;
2122 return jtag_tap_configure_cmd( &goi
, t
);
2129 int jtag_register_commands(struct command_context_s
*cmd_ctx
)
2131 register_jim( cmd_ctx
, "jtag", jim_jtag_command
, "perform jtag tap actions");
2133 register_command(cmd_ctx
, NULL
, "interface", handle_interface_command
,
2134 COMMAND_CONFIG
, "try to configure interface");
2135 register_command(cmd_ctx
, NULL
, "jtag_speed", handle_jtag_speed_command
,
2136 COMMAND_ANY
, "set jtag speed (if supported)");
2137 register_command(cmd_ctx
, NULL
, "jtag_khz", handle_jtag_khz_command
,
2138 COMMAND_ANY
, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
2139 register_command(cmd_ctx
, NULL
, "jtag_device", handle_jtag_device_command
,
2140 COMMAND_CONFIG
, "jtag_device <ir_length> <ir_expected> <ir_mask>");
2141 register_command(cmd_ctx
, NULL
, "reset_config", handle_reset_config_command
,
2142 COMMAND_CONFIG
, NULL
);
2143 register_command(cmd_ctx
, NULL
, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command
,
2144 COMMAND_ANY
, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
2145 register_command(cmd_ctx
, NULL
, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command
,
2146 COMMAND_ANY
, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
2148 register_command(cmd_ctx
, NULL
, "scan_chain", handle_scan_chain_command
,
2149 COMMAND_EXEC
, "print current scan chain configuration");
2151 register_command(cmd_ctx
, NULL
, "endstate", handle_endstate_command
,
2152 COMMAND_EXEC
, "finish JTAG operations in <tap_state>");
2153 register_command(cmd_ctx
, NULL
, "jtag_reset", handle_jtag_reset_command
,
2154 COMMAND_EXEC
, "toggle reset lines <trst> <srst>");
2155 register_command(cmd_ctx
, NULL
, "runtest", handle_runtest_command
,
2156 COMMAND_EXEC
, "move to Run-Test/Idle, and execute <num_cycles>");
2157 register_command(cmd_ctx
, NULL
, "irscan", handle_irscan_command
,
2158 COMMAND_EXEC
, "execute IR scan <device> <instr> [dev2] [instr2] ...");
2159 register_jim(cmd_ctx
, "drscan", Jim_Command_drscan
, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
2161 register_command(cmd_ctx
, NULL
, "verify_ircapture", handle_verify_ircapture_command
,
2162 COMMAND_ANY
, "verify value captured during Capture-IR <enable|disable>");
2166 int jtag_interface_init(struct command_context_s
*cmd_ctx
)
2171 if (!jtag_interface
)
2173 /* nothing was previously specified by "interface" command */
2174 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
2175 return ERROR_JTAG_INVALID_INTERFACE
;
2179 jtag_interface
->khz(speed_khz
, &jtag_speed
);
2183 if (jtag_interface
->init() != ERROR_OK
)
2184 return ERROR_JTAG_INIT_FAILED
;
2186 jtag
= jtag_interface
;
2190 static int jtag_init_inner(struct command_context_s
*cmd_ctx
)
2195 LOG_DEBUG("Init JTAG chain");
2197 tap
= jtag_NextEnabledTap(NULL
);
2199 LOG_ERROR("There are no enabled taps?");
2200 return ERROR_JTAG_INIT_FAILED
;
2204 if ((retval
=jtag_execute_queue())!=ERROR_OK
)
2207 /* examine chain first, as this could discover the real chain layout */
2208 if (jtag_examine_chain() != ERROR_OK
)
2210 LOG_ERROR("trying to validate configured JTAG chain anyway...");
2213 if (jtag_validate_chain() != ERROR_OK
)
2215 LOG_ERROR("Could not validate JTAG chain, continuing anyway...");
2221 int jtag_init_reset(struct command_context_s
*cmd_ctx
)
2225 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
2228 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
2230 /* Reset can happen after a power cycle.
2232 * Ideally we would only assert TRST or run RESET before the target reset.
2234 * However w/srst_pulls_trst, trst is asserted together with the target
2235 * reset whether we want it or not.
2237 * NB! Some targets have JTAG circuitry disabled until a
2238 * trst & srst has been asserted.
2240 * NB! here we assume nsrst/ntrst delay are sufficient!
2242 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
2245 jtag_add_reset(1, 0); /* RESET or TRST */
2246 if (jtag_reset_config
& RESET_HAS_SRST
)
2248 jtag_add_reset(1, 1);
2249 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0)
2250 jtag_add_reset(0, 1);
2252 jtag_add_reset(0, 0);
2253 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
2256 /* Check that we can communication on the JTAG chain + eventually we want to
2257 * be able to perform enumeration only after OpenOCD has started
2258 * telnet and GDB server
2260 * That would allow users to more easily perform any magic they need to before
2263 return jtag_init_inner(cmd_ctx
);
2266 int jtag_init(struct command_context_s
*cmd_ctx
)
2269 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
2271 if (jtag_init_inner(cmd_ctx
)==ERROR_OK
)
2275 return jtag_init_reset(cmd_ctx
);
2278 static int default_khz(int khz
, int *jtag_speed
)
2280 LOG_ERROR("Translation from khz to jtag_speed not implemented");
2284 static int default_speed_div(int speed
, int *khz
)
2286 LOG_ERROR("Translation from jtag_speed to khz not implemented");
2290 static int default_power_dropout(int *dropout
)
2292 *dropout
=0; /* by default we can't detect power dropout */
2296 static int default_srst_asserted(int *srst_asserted
)
2298 *srst_asserted
=0; /* by default we can't detect srst asserted */
2302 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2307 /* check whether the interface is already configured */
2310 LOG_WARNING("Interface already configured, ignoring");
2314 /* interface name is a mandatory argument */
2315 if (argc
< 1 || args
[0][0] == '\0')
2317 return ERROR_COMMAND_SYNTAX_ERROR
;
2320 for (i
=0; jtag_interfaces
[i
]; i
++)
2322 if (strcmp(args
[0], jtag_interfaces
[i
]->name
) == 0)
2324 if ((retval
= jtag_interfaces
[i
]->register_commands(cmd_ctx
)) != ERROR_OK
)
2329 jtag_interface
= jtag_interfaces
[i
];
2331 if (jtag_interface
->khz
== NULL
)
2333 jtag_interface
->khz
= default_khz
;
2335 if (jtag_interface
->speed_div
== NULL
)
2337 jtag_interface
->speed_div
= default_speed_div
;
2339 if (jtag_interface
->power_dropout
== NULL
)
2341 jtag_interface
->power_dropout
= default_power_dropout
;
2343 if (jtag_interface
->srst_asserted
== NULL
)
2345 jtag_interface
->srst_asserted
= default_srst_asserted
;
2352 /* no valid interface was found (i.e. the configuration option,
2353 * didn't match one of the compiled-in interfaces
2355 LOG_ERROR("No valid jtag interface found (%s)", args
[0]);
2356 LOG_ERROR("compiled-in jtag interfaces:");
2357 for (i
= 0; jtag_interfaces
[i
]; i
++)
2359 LOG_ERROR("%i: %s", i
, jtag_interfaces
[i
]->name
);
2362 return ERROR_JTAG_INVALID_INTERFACE
;
2365 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2369 Jim_Obj
*newargs
[ 10 ];
2372 * argv[-1] = command
2373 * argv[ 0] = ir length
2374 * argv[ 1] = ir capture
2375 * argv[ 2] = ir mask
2376 * argv[ 3] = not actually used by anything but in the docs
2380 command_print( cmd_ctx
, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
2383 command_print( cmd_ctx
, "OLD SYNTAX: DEPRECATED - translating to new syntax");
2384 command_print( cmd_ctx
, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
2388 command_print( cmd_ctx
, "Example: STM32 has 2 taps, the cortexM3(len4) + boundryscan(len5)");
2389 command_print( cmd_ctx
, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
2390 command_print( cmd_ctx
, "jtag newtap stm32 boundry ....., and the tap: \"stm32.boundery\"");
2391 command_print( cmd_ctx
, "And then refer to the taps by the dotted name.");
2393 newargs
[0] = Jim_NewStringObj( interp
, "jtag", -1 );
2394 newargs
[1] = Jim_NewStringObj( interp
, "newtap", -1 );
2395 sprintf( buf
, "chip%d", jtag_NumTotalTaps() );
2396 newargs
[2] = Jim_NewStringObj( interp
, buf
, -1 );
2397 sprintf( buf
, "tap%d", jtag_NumTotalTaps() );
2398 newargs
[3] = Jim_NewStringObj( interp
, buf
, -1 );
2399 newargs
[4] = Jim_NewStringObj( interp
, "-irlen", -1 );
2400 newargs
[5] = Jim_NewStringObj( interp
, args
[0], -1 );
2401 newargs
[6] = Jim_NewStringObj( interp
, "-ircapture", -1 );
2402 newargs
[7] = Jim_NewStringObj( interp
, args
[1], -1 );
2403 newargs
[8] = Jim_NewStringObj( interp
, "-irmask", -1 );
2404 newargs
[9] = Jim_NewStringObj( interp
, args
[2], -1 );
2406 command_print( cmd_ctx
, "NEW COMMAND:");
2407 sprintf( buf
, "%s %s %s %s %s %s %s %s %s %s",
2408 Jim_GetString( newargs
[0], NULL
),
2409 Jim_GetString( newargs
[1], NULL
),
2410 Jim_GetString( newargs
[2], NULL
),
2411 Jim_GetString( newargs
[3], NULL
),
2412 Jim_GetString( newargs
[4], NULL
),
2413 Jim_GetString( newargs
[5], NULL
),
2414 Jim_GetString( newargs
[6], NULL
),
2415 Jim_GetString( newargs
[7], NULL
),
2416 Jim_GetString( newargs
[8], NULL
),
2417 Jim_GetString( newargs
[9], NULL
) );
2419 e
= jim_jtag_command( interp
, 10, newargs
);
2421 command_print( cmd_ctx
, "%s", Jim_GetString( Jim_GetResult(interp
), NULL
) );
2426 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2430 tap
= jtag_all_taps
;
2431 command_print(cmd_ctx
, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
2432 command_print(cmd_ctx
, "---|--------------------|---------|------------|------------|------|------|------|---------");
2435 u32 expected
, expected_mask
, cur_instr
, ii
;
2436 expected
= buf_get_u32(tap
->expected
, 0, tap
->ir_length
);
2437 expected_mask
= buf_get_u32(tap
->expected_mask
, 0, tap
->ir_length
);
2438 cur_instr
= buf_get_u32(tap
->cur_instr
, 0, tap
->ir_length
);
2440 command_print(cmd_ctx
,
2441 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
2442 tap
->abs_chain_position
,
2444 tap
->enabled
? 'Y' : 'n',
2446 (tap
->expected_ids_cnt
> 0 ? tap
->expected_ids
[0] : 0),
2452 for (ii
= 1; ii
< tap
->expected_ids_cnt
; ii
++) {
2453 command_print(cmd_ctx
, " | | | | 0x%08x | | | | ",
2454 tap
->expected_ids
[ii
]);
2457 tap
= tap
->next_tap
;
2463 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2466 return ERROR_COMMAND_SYNTAX_ERROR
;
2470 if (strcmp(args
[0], "none") == 0)
2471 jtag_reset_config
= RESET_NONE
;
2472 else if (strcmp(args
[0], "trst_only") == 0)
2473 jtag_reset_config
= RESET_HAS_TRST
;
2474 else if (strcmp(args
[0], "srst_only") == 0)
2475 jtag_reset_config
= RESET_HAS_SRST
;
2476 else if (strcmp(args
[0], "trst_and_srst") == 0)
2477 jtag_reset_config
= RESET_TRST_AND_SRST
;
2480 LOG_ERROR("(1) invalid reset_config argument (%s), defaulting to none", args
[0]);
2481 jtag_reset_config
= RESET_NONE
;
2482 return ERROR_INVALID_ARGUMENTS
;
2488 if (strcmp(args
[1], "separate") == 0)
2490 /* seperate reset lines - default */
2493 if (strcmp(args
[1], "srst_pulls_trst") == 0)
2494 jtag_reset_config
|= RESET_SRST_PULLS_TRST
;
2495 else if (strcmp(args
[1], "trst_pulls_srst") == 0)
2496 jtag_reset_config
|= RESET_TRST_PULLS_SRST
;
2497 else if (strcmp(args
[1], "combined") == 0)
2498 jtag_reset_config
|= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
2501 LOG_ERROR("(2) invalid reset_config argument (%s), defaulting to none", args
[1]);
2502 jtag_reset_config
= RESET_NONE
;
2503 return ERROR_INVALID_ARGUMENTS
;
2510 if (strcmp(args
[2], "trst_open_drain") == 0)
2511 jtag_reset_config
|= RESET_TRST_OPEN_DRAIN
;
2512 else if (strcmp(args
[2], "trst_push_pull") == 0)
2513 jtag_reset_config
&= ~RESET_TRST_OPEN_DRAIN
;
2516 LOG_ERROR("(3) invalid reset_config argument (%s) defaulting to none", args
[2] );
2517 jtag_reset_config
= RESET_NONE
;
2518 return ERROR_INVALID_ARGUMENTS
;
2524 if (strcmp(args
[3], "srst_push_pull") == 0)
2525 jtag_reset_config
|= RESET_SRST_PUSH_PULL
;
2526 else if (strcmp(args
[3], "srst_open_drain") == 0)
2527 jtag_reset_config
&= ~RESET_SRST_PUSH_PULL
;
2530 LOG_ERROR("(4) invalid reset_config argument (%s), defaulting to none", args
[3]);
2531 jtag_reset_config
= RESET_NONE
;
2532 return ERROR_INVALID_ARGUMENTS
;
2539 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2543 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
2548 jtag_nsrst_delay
= strtoul(args
[0], NULL
, 0);
2554 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2558 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
2563 jtag_ntrst_delay
= strtoul(args
[0], NULL
, 0);
2569 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2571 int retval
=ERROR_OK
;
2575 LOG_DEBUG("handle jtag speed");
2578 cur_speed
= jtag_speed
= strtoul(args
[0], NULL
, 0);
2580 /* this command can be called during CONFIG,
2581 * in which case jtag isn't initialized */
2584 retval
=jtag
->speed(cur_speed
);
2586 } else if (argc
== 0)
2590 return ERROR_COMMAND_SYNTAX_ERROR
;
2592 command_print(cmd_ctx
, "jtag_speed: %d", jtag_speed
);
2597 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2599 int retval
=ERROR_OK
;
2600 LOG_DEBUG("handle jtag khz");
2604 speed_khz
= strtoul(args
[0], NULL
, 0);
2608 LOG_DEBUG("have interface set up");
2610 if ((retval
=jtag
->khz(speed_khz
, &speed_div1
))!=ERROR_OK
)
2616 cur_speed
= jtag_speed
= speed_div1
;
2618 retval
=jtag
->speed(cur_speed
);
2627 return ERROR_COMMAND_SYNTAX_ERROR
;
2632 if ((retval
=jtag
->speed_div(jtag_speed
, &speed_khz
))!=ERROR_OK
)
2638 command_print(cmd_ctx
, "RCLK - adaptive");
2641 command_print(cmd_ctx
, "%d kHz", speed_khz
);
2647 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2649 enum tap_state state
;
2653 return ERROR_COMMAND_SYNTAX_ERROR
;
2657 for (state
= 0; state
< 16; state
++)
2659 if (strcmp(args
[0], tap_state_strings
[state
]) == 0)
2661 jtag_add_end_state(state
);
2662 jtag_execute_queue();
2666 command_print(cmd_ctx
, "current endstate: %s", tap_state_strings
[cmd_queue_end_state
]);
2671 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2678 return ERROR_COMMAND_SYNTAX_ERROR
;
2681 if (args
[0][0] == '1')
2683 else if (args
[0][0] == '0')
2687 return ERROR_COMMAND_SYNTAX_ERROR
;
2690 if (args
[1][0] == '1')
2692 else if (args
[1][0] == '0')
2696 return ERROR_COMMAND_SYNTAX_ERROR
;
2699 if (jtag_interface_init(cmd_ctx
) != ERROR_OK
)
2700 return ERROR_JTAG_INIT_FAILED
;
2702 jtag_add_reset(trst
, srst
);
2703 jtag_execute_queue();
2708 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2712 return ERROR_COMMAND_SYNTAX_ERROR
;
2715 jtag_add_runtest(strtol(args
[0], NULL
, 0), -1);
2716 jtag_execute_queue();
2722 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2725 scan_field_t
*fields
;
2728 if ((argc
< 2) || (argc
% 2))
2730 return ERROR_COMMAND_SYNTAX_ERROR
;
2733 fields
= malloc(sizeof(scan_field_t
) * argc
/ 2);
2735 for (i
= 0; i
< argc
/ 2; i
++)
2737 tap
= jtag_TapByString( args
[i
*2] );
2740 command_print( cmd_ctx
, "Tap: %s unknown", args
[i
*2] );
2743 int field_size
= tap
->ir_length
;
2744 fields
[i
].tap
= tap
;
2745 fields
[i
].out_value
= malloc(CEIL(field_size
, 8));
2746 buf_set_u32(fields
[i
].out_value
, 0, field_size
, strtoul(args
[i
*2+1], NULL
, 0));
2747 fields
[i
].out_mask
= NULL
;
2748 fields
[i
].in_value
= NULL
;
2749 fields
[i
].in_check_mask
= NULL
;
2750 fields
[i
].in_handler
= NULL
;
2751 fields
[i
].in_handler_priv
= NULL
;
2754 jtag_add_ir_scan(argc
/ 2, fields
, -1);
2755 jtag_execute_queue();
2757 for (i
= 0; i
< argc
/ 2; i
++)
2758 free(fields
[i
].out_value
);
2765 int Jim_Command_drscan(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *args
)
2768 scan_field_t
*fields
;
2770 int field_count
= 0;
2775 * args[2] = num_bits
2776 * args[3] = hex string
2777 * ... repeat num bits and hex string ...
2779 if ((argc
< 4) || ((argc
% 2)!=0))
2781 Jim_WrongNumArgs(interp
, 1, args
, "wrong arguments");
2785 for (i
= 2; i
< argc
; i
+=2)
2789 e
= Jim_GetLong(interp
, args
[i
], &bits
);
2794 tap
= jtag_TapByJimObj( interp
, args
[1] );
2799 num_fields
=(argc
-2)/2;
2800 fields
= malloc(sizeof(scan_field_t
) * num_fields
);
2801 for (i
= 2; i
< argc
; i
+=2)
2807 Jim_GetLong(interp
, args
[i
], &bits
);
2808 str
= Jim_GetString(args
[i
+1], &len
);
2810 fields
[field_count
].tap
= tap
;
2811 fields
[field_count
].num_bits
= bits
;
2812 fields
[field_count
].out_value
= malloc(CEIL(bits
, 8));
2813 str_to_buf(str
, len
, fields
[field_count
].out_value
, bits
, 0);
2814 fields
[field_count
].out_mask
= NULL
;
2815 fields
[field_count
].in_value
= fields
[field_count
].out_value
;
2816 fields
[field_count
].in_check_mask
= NULL
;
2817 fields
[field_count
].in_check_value
= NULL
;
2818 fields
[field_count
].in_handler
= NULL
;
2819 fields
[field_count
++].in_handler_priv
= NULL
;
2822 jtag_add_dr_scan(num_fields
, fields
, -1);
2823 retval
= jtag_execute_queue();
2824 if (retval
!= ERROR_OK
)
2826 Jim_SetResultString(interp
, "drscan: jtag execute failed",-1);
2831 Jim_Obj
*list
= Jim_NewListObj(interp
, NULL
, 0);
2832 for (i
= 2; i
< argc
; i
+=2)
2837 Jim_GetLong(interp
, args
[i
], &bits
);
2838 str
= buf_to_str(fields
[field_count
].in_value
, bits
, 16);
2839 free(fields
[field_count
].out_value
);
2841 Jim_ListAppendElement(interp
, list
, Jim_NewStringObj(interp
, str
, strlen(str
)));
2846 Jim_SetResult(interp
, list
);
2853 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2857 if (strcmp(args
[0], "enable") == 0)
2859 jtag_verify_capture_ir
= 1;
2861 else if (strcmp(args
[0], "disable") == 0)
2863 jtag_verify_capture_ir
= 0;
2866 return ERROR_COMMAND_SYNTAX_ERROR
;
2868 } else if (argc
!= 0)
2870 return ERROR_COMMAND_SYNTAX_ERROR
;
2873 command_print(cmd_ctx
, "verify Capture-IR is %s", (jtag_verify_capture_ir
) ? "enabled": "disabled");
2878 int jtag_power_dropout(int *dropout
)
2880 return jtag
->power_dropout(dropout
);
2883 int jtag_srst_asserted(int *srst_asserted
)
2885 return jtag
->srst_asserted(srst_asserted
);
2888 void jtag_tap_handle_event( jtag_tap_t
* tap
, enum jtag_tap_event e
)
2890 jtag_tap_event_action_t
* jteap
;
2893 jteap
= tap
->event_action
;
2897 if (jteap
->event
== e
) {
2899 LOG_DEBUG( "JTAG tap: %s event: %d (%s) action: %s\n",
2902 Jim_Nvp_value2name_simple(nvp_jtag_tap_event
, e
)->name
,
2903 Jim_GetString(jteap
->body
, NULL
) );
2904 if (Jim_EvalObj(interp
, jteap
->body
) != JIM_OK
) {
2905 Jim_PrintErrorMessage(interp
);
2909 jteap
= jteap
->next
;
2913 LOG_DEBUG( "event %d %s - no action",
2915 Jim_Nvp_value2name_simple( nvp_jtag_tap_event
, e
)->name
);
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)