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
;
255 /* forward declarations */
256 void jtag_add_pathmove(int num_states
, enum tap_state
*path
);
257 void jtag_add_runtest(int num_cycles
, enum tap_state endstate
);
258 void jtag_add_end_state(enum tap_state endstate
);
259 void jtag_add_sleep(u32 us
);
260 int jtag_execute_queue(void);
264 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
265 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
266 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
267 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
268 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
269 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
270 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
272 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
274 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
275 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
276 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
277 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
278 int Jim_Command_drscan(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
280 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
283 jtag_tap_t
*jtag_AllTaps(void)
285 return jtag_all_taps
;
289 jtag_NumTotalTaps(void)
304 jtag_NumEnabledTaps(void)
321 jtag_tap_t
*jtag_TapByString( const char *s
)
329 if( 0 == strcmp( t
->dotted_name
, s
) ){
335 // backup plan is by number
337 /* ok - is "s" a number? */
339 n
= strtol( s
, &cp
, 0 );
340 if( (s
!= cp
) && (*cp
== 0) ){
342 t
= jtag_TapByAbsPosition(n
);
349 jtag_TapByJimObj( Jim_Interp
*interp
, Jim_Obj
*o
)
354 cp
= Jim_GetString( o
, NULL
);
359 t
= jtag_TapByString( cp
);
362 Jim_SetResult_sprintf(interp
,"Tap: %s is unknown", cp
);
367 /* returns a pointer to the n-th device in the scan chain */
369 jtag_TapByAbsPosition( int n
)
377 while( t
&& (n
> 0)) {
385 int jtag_register_event_callback(int (*callback
)(enum jtag_event event
, void *priv
), void *priv
)
387 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
389 if (callback
== NULL
)
391 return ERROR_INVALID_ARGUMENTS
;
396 while ((*callbacks_p
)->next
)
397 callbacks_p
= &((*callbacks_p
)->next
);
398 callbacks_p
= &((*callbacks_p
)->next
);
401 (*callbacks_p
) = malloc(sizeof(jtag_event_callback_t
));
402 (*callbacks_p
)->callback
= callback
;
403 (*callbacks_p
)->priv
= priv
;
404 (*callbacks_p
)->next
= NULL
;
409 int jtag_unregister_event_callback(int (*callback
)(enum jtag_event event
, void *priv
))
411 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
413 if (callback
== NULL
)
415 return ERROR_INVALID_ARGUMENTS
;
420 jtag_event_callback_t
**next
= &((*callbacks_p
)->next
);
421 if ((*callbacks_p
)->callback
== callback
)
424 *callbacks_p
= *next
;
432 int jtag_call_event_callbacks(enum jtag_event event
)
434 jtag_event_callback_t
*callback
= jtag_event_callbacks
;
436 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
440 callback
->callback(event
, callback
->priv
);
441 callback
= callback
->next
;
447 /* returns a pointer to the pointer of the last command in queue
448 * this may be a pointer to the root pointer (jtag_command_queue)
449 * or to the next member of the last but one command
451 jtag_command_t
** jtag_get_last_command_p(void)
453 /* jtag_command_t *cmd = jtag_command_queue;
459 return &jtag_command_queue;
463 return last_comand_pointer
;
467 void* cmd_queue_alloc(size_t size
)
469 cmd_queue_page_t
**p_page
= &cmd_queue_pages
;
475 * We align/round the *SIZE* per below
476 * so that all pointers returned by
477 * this function are reasonably well
480 * If we did not, then an "odd-length" request would cause the
481 * *next* allocation to be at an *odd* address, and because
482 * this function has the same type of api as malloc() - we
483 * must also return pointers that have the same type of
486 * What I do not/have is a reasonable portable means
489 * The solution here, is based on these suggestions.
490 * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
493 union worse_case_align
{
499 #define ALIGN_SIZE (sizeof(union worse_case_align))
501 // The alignment process.
502 size
= (size
+ ALIGN_SIZE
-1) & (~(ALIGN_SIZE
-1));
508 while ((*p_page
)->next
)
509 p_page
= &((*p_page
)->next
);
510 if (CMD_QUEUE_PAGE_SIZE
- (*p_page
)->used
< size
)
511 p_page
= &((*p_page
)->next
);
516 *p_page
= malloc(sizeof(cmd_queue_page_t
));
518 (*p_page
)->address
= malloc(CMD_QUEUE_PAGE_SIZE
);
519 (*p_page
)->next
= NULL
;
522 offset
= (*p_page
)->used
;
523 (*p_page
)->used
+= size
;
525 t
=(u8
*)((*p_page
)->address
);
529 void cmd_queue_free(void)
531 cmd_queue_page_t
*page
= cmd_queue_pages
;
535 cmd_queue_page_t
*last
= page
;
541 cmd_queue_pages
= NULL
;
544 static void jtag_prelude1(void)
548 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
549 jtag_error
=ERROR_JTAG_TRST_ASSERTED
;
553 if (cmd_queue_end_state
== TAP_RESET
)
554 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
557 static void jtag_prelude(enum tap_state state
)
562 jtag_add_end_state(state
);
564 cmd_queue_cur_state
= cmd_queue_end_state
;
567 void jtag_add_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
573 retval
=interface_jtag_add_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
574 if (retval
!=ERROR_OK
)
578 int MINIDRIVER(interface_jtag_add_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
580 jtag_command_t
**last_cmd
;
588 last_cmd
= jtag_get_last_command_p();
590 /* allocate memory for a new list member */
591 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
592 (*last_cmd
)->next
= NULL
;
593 last_comand_pointer
= &((*last_cmd
)->next
);
594 (*last_cmd
)->type
= JTAG_SCAN
;
596 /* allocate memory for ir scan command */
597 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
598 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
599 x
= jtag_NumEnabledTaps();
600 (*last_cmd
)->cmd
.scan
->num_fields
= x
; /* one field per device */
601 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(x
* sizeof(scan_field_t
));
602 (*last_cmd
)->cmd
.scan
->end_state
= state
;
609 // do this here so it is not forgotten
610 tap
= jtag_NextEnabledTap(tap
);
615 scan_size
= tap
->ir_length
;
616 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].tap
= tap
;
617 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].num_bits
= scan_size
;
618 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_value
= NULL
;
619 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_handler
= NULL
; /* disable verification by default */
621 /* search the list */
622 for (j
= 0; j
< num_fields
; j
++)
624 if (tap
== fields
[j
].tap
)
627 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
628 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
630 if (jtag_verify_capture_ir
)
632 if (fields
[j
].in_handler
==NULL
)
634 jtag_set_check_value((*last_cmd
)->cmd
.scan
->fields
+nth_tap
, tap
->expected
, tap
->expected_mask
, NULL
);
637 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_handler
= fields
[j
].in_handler
;
638 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_handler_priv
= fields
[j
].in_handler_priv
;
639 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_check_value
= tap
->expected
;
640 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_check_mask
= tap
->expected_mask
;
651 /* if a tap isn't listed, set it to BYPASS */
652 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_value
= buf_set_ones(cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
653 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_mask
= NULL
;
658 /* update device information */
659 buf_cpy((*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_value
, tap
->cur_instr
, scan_size
);
665 void jtag_add_plain_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
671 retval
=interface_jtag_add_plain_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
672 if (retval
!=ERROR_OK
)
676 int MINIDRIVER(interface_jtag_add_plain_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
679 jtag_command_t
**last_cmd
;
681 last_cmd
= jtag_get_last_command_p();
683 /* allocate memory for a new list member */
684 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
685 (*last_cmd
)->next
= NULL
;
686 last_comand_pointer
= &((*last_cmd
)->next
);
687 (*last_cmd
)->type
= JTAG_SCAN
;
689 /* allocate memory for ir scan command */
690 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
691 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
692 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
693 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
694 (*last_cmd
)->cmd
.scan
->end_state
= state
;
696 for( i
= 0 ; i
< num_fields
; i
++ ){
697 int num_bits
= fields
[i
].num_bits
;
698 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
699 (*last_cmd
)->cmd
.scan
->fields
[i
].tap
= fields
[i
].tap
;
700 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
701 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
702 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
703 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
704 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
705 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
706 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= NULL
;
707 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= NULL
;
712 void jtag_add_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
718 retval
=interface_jtag_add_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
719 if (retval
!=ERROR_OK
)
723 int MINIDRIVER(interface_jtag_add_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
727 int bypass_devices
= 0;
731 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
734 /* count devices in bypass */
738 tap
= jtag_NextEnabledTap(tap
);
747 /* allocate memory for a new list member */
748 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
749 last_comand_pointer
= &((*last_cmd
)->next
);
750 (*last_cmd
)->next
= NULL
;
751 (*last_cmd
)->type
= JTAG_SCAN
;
753 /* allocate memory for dr scan command */
754 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
755 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
756 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
757 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
758 (*last_cmd
)->cmd
.scan
->end_state
= state
;
764 tap
= jtag_NextEnabledTap(tap
);
769 (*last_cmd
)->cmd
.scan
->fields
[field_count
].tap
= tap
;
771 for (j
= 0; j
< num_fields
; j
++)
773 if (tap
== fields
[j
].tap
)
776 scan_size
= fields
[j
].num_bits
;
777 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
778 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
779 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
780 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= fields
[j
].in_value
;
781 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= fields
[j
].in_check_value
;
782 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= fields
[j
].in_check_mask
;
783 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= fields
[j
].in_handler
;
784 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= fields
[j
].in_handler_priv
;
789 #ifdef _DEBUG_JTAG_IO_
790 /* if a device isn't listed, the BYPASS register should be selected */
793 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
797 /* program the scan field to 1 bit length, and ignore it's value */
798 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
799 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
800 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
801 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
802 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
803 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
804 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
805 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
809 #ifdef _DEBUG_JTAG_IO_
810 /* if a device is listed, the BYPASS register must not be selected */
813 LOG_ERROR("BUG: scan data for a device in BYPASS");
822 void MINIDRIVER(interface_jtag_add_dr_out
)(jtag_tap_t
*target_tap
,
826 enum tap_state end_state
)
831 int bypass_devices
= 0;
833 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
836 /* count devices in bypass */
840 tap
= jtag_NextEnabledTap(tap
);
849 /* allocate memory for a new list member */
850 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
851 last_comand_pointer
= &((*last_cmd
)->next
);
852 (*last_cmd
)->next
= NULL
;
853 (*last_cmd
)->type
= JTAG_SCAN
;
855 /* allocate memory for dr scan command */
856 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
857 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
858 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
859 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
860 (*last_cmd
)->cmd
.scan
->end_state
= end_state
;
865 tap
= jtag_NextEnabledTap(tap
);
870 (*last_cmd
)->cmd
.scan
->fields
[field_count
].tap
= tap
;
872 if (tap
== target_tap
)
875 #ifdef _DEBUG_JTAG_IO_
876 /* if a device is listed, the BYPASS register must not be selected */
879 LOG_ERROR("BUG: scan data for a device in BYPASS");
883 for (j
= 0; j
< num_fields
; j
++)
886 scan_size
= num_bits
[j
];
887 buf_set_u32(out_value
, 0, scan_size
, value
[j
]);
888 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
889 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
890 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
891 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
892 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
893 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
894 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
895 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
899 #ifdef _DEBUG_JTAG_IO_
900 /* if a device isn't listed, the BYPASS register should be selected */
903 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
907 /* program the scan field to 1 bit length, and ignore it's value */
908 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
909 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
910 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
911 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
912 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
913 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
914 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
915 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
920 void jtag_add_plain_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
926 retval
=interface_jtag_add_plain_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
927 if (retval
!=ERROR_OK
)
931 int MINIDRIVER(interface_jtag_add_plain_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
934 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
936 /* allocate memory for a new list member */
937 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
938 last_comand_pointer
= &((*last_cmd
)->next
);
939 (*last_cmd
)->next
= NULL
;
940 (*last_cmd
)->type
= JTAG_SCAN
;
942 /* allocate memory for scan command */
943 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
944 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
945 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
946 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
947 (*last_cmd
)->cmd
.scan
->end_state
= state
;
949 for (i
= 0; i
< num_fields
; i
++)
951 int num_bits
= fields
[i
].num_bits
;
952 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
953 (*last_cmd
)->cmd
.scan
->fields
[i
].tap
= fields
[i
].tap
;
954 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
955 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
956 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
957 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
958 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
959 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
960 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= fields
[i
].in_handler
;
961 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= fields
[i
].in_handler_priv
;
967 void jtag_add_tlr(void)
969 jtag_prelude(TAP_RESET
);
972 retval
=interface_jtag_add_tlr();
973 if (retval
!=ERROR_OK
)
977 int MINIDRIVER(interface_jtag_add_tlr
)()
979 enum tap_state state
= TAP_RESET
;
980 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
982 /* allocate memory for a new list member */
983 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
984 last_comand_pointer
= &((*last_cmd
)->next
);
985 (*last_cmd
)->next
= NULL
;
986 (*last_cmd
)->type
= JTAG_STATEMOVE
;
988 (*last_cmd
)->cmd
.statemove
= cmd_queue_alloc(sizeof(statemove_command_t
));
989 (*last_cmd
)->cmd
.statemove
->end_state
= state
;
995 void jtag_add_pathmove(int num_states
, enum tap_state
*path
)
997 enum tap_state cur_state
=cmd_queue_cur_state
;
1001 /* the last state has to be a stable state */
1002 if (tap_move_map
[path
[num_states
- 1]] == -1)
1004 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
1008 for (i
=0; i
<num_states
; i
++)
1010 if (path
[i
] == TAP_RESET
)
1012 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
1015 if ((tap_transitions
[cur_state
].low
!= path
[i
])&&
1016 (tap_transitions
[cur_state
].high
!= path
[i
]))
1018 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings
[cur_state
], tap_state_strings
[path
[i
]]);
1021 cur_state
= path
[i
];
1027 retval
=interface_jtag_add_pathmove(num_states
, path
);
1028 cmd_queue_cur_state
= path
[num_states
- 1];
1029 if (retval
!=ERROR_OK
)
1033 int MINIDRIVER(interface_jtag_add_pathmove
)(int num_states
, enum tap_state
*path
)
1035 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_comand_pointer
= &((*last_cmd
)->next
);
1041 (*last_cmd
)->next
= NULL
;
1042 (*last_cmd
)->type
= JTAG_PATHMOVE
;
1044 (*last_cmd
)->cmd
.pathmove
= cmd_queue_alloc(sizeof(pathmove_command_t
));
1045 (*last_cmd
)->cmd
.pathmove
->num_states
= num_states
;
1046 (*last_cmd
)->cmd
.pathmove
->path
= cmd_queue_alloc(sizeof(enum tap_state
) * num_states
);
1048 for (i
= 0; i
< num_states
; i
++)
1049 (*last_cmd
)->cmd
.pathmove
->path
[i
] = path
[i
];
1054 int MINIDRIVER(interface_jtag_add_runtest
)(int num_cycles
, enum tap_state state
)
1056 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1058 /* allocate memory for a new list member */
1059 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1060 (*last_cmd
)->next
= NULL
;
1061 last_comand_pointer
= &((*last_cmd
)->next
);
1062 (*last_cmd
)->type
= JTAG_RUNTEST
;
1064 (*last_cmd
)->cmd
.runtest
= cmd_queue_alloc(sizeof(runtest_command_t
));
1065 (*last_cmd
)->cmd
.runtest
->num_cycles
= num_cycles
;
1066 (*last_cmd
)->cmd
.runtest
->end_state
= state
;
1071 void jtag_add_runtest(int num_cycles
, enum tap_state state
)
1075 jtag_prelude(state
);
1077 /* executed by sw or hw fifo */
1078 retval
=interface_jtag_add_runtest(num_cycles
, cmd_queue_end_state
);
1079 if (retval
!=ERROR_OK
)
1083 void jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
1085 int trst_with_tlr
= 0;
1088 /* FIX!!! there are *many* different cases here. A better
1089 * approach is needed for legal combinations of transitions...
1091 if ((jtag_reset_config
& RESET_HAS_SRST
)&&
1092 (jtag_reset_config
& RESET_HAS_TRST
)&&
1093 ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0))
1095 if (((req_tlr_or_trst
&&!jtag_trst
)||
1096 (!req_tlr_or_trst
&&jtag_trst
))&&
1097 ((req_srst
&&!jtag_srst
)||
1098 (!req_srst
&&jtag_srst
)))
1100 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
1101 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
1105 /* Make sure that jtag_reset_config allows the requested reset */
1106 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
1107 if (((jtag_reset_config
& RESET_SRST_PULLS_TRST
) && (req_srst
== 1)) && (!req_tlr_or_trst
))
1109 LOG_ERROR("BUG: requested reset would assert trst");
1110 jtag_error
=ERROR_FAIL
;
1114 /* if TRST pulls SRST, we reset with TAP T-L-R */
1115 if (((jtag_reset_config
& RESET_TRST_PULLS_SRST
) && (req_tlr_or_trst
)) && (req_srst
== 0))
1120 if (req_srst
&& !(jtag_reset_config
& RESET_HAS_SRST
))
1122 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
1123 jtag_error
=ERROR_FAIL
;
1127 if (req_tlr_or_trst
)
1129 if (!trst_with_tlr
&& (jtag_reset_config
& RESET_HAS_TRST
))
1141 jtag_srst
= req_srst
;
1143 retval
= interface_jtag_add_reset(jtag_trst
, jtag_srst
);
1144 if (retval
!=ERROR_OK
)
1152 LOG_DEBUG("SRST line asserted");
1156 LOG_DEBUG("SRST line released");
1157 if (jtag_nsrst_delay
)
1158 jtag_add_sleep(jtag_nsrst_delay
* 1000);
1163 LOG_DEBUG("JTAG reset with RESET instead of TRST");
1164 jtag_add_end_state(TAP_RESET
);
1166 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
1172 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1173 * and inform possible listeners about this
1175 LOG_DEBUG("TRST line asserted");
1176 cmd_queue_cur_state
= TAP_RESET
;
1177 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
1181 if (jtag_ntrst_delay
)
1182 jtag_add_sleep(jtag_ntrst_delay
* 1000);
1186 int MINIDRIVER(interface_jtag_add_reset
)(int req_trst
, int req_srst
)
1188 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1190 /* allocate memory for a new list member */
1191 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1192 (*last_cmd
)->next
= NULL
;
1193 last_comand_pointer
= &((*last_cmd
)->next
);
1194 (*last_cmd
)->type
= JTAG_RESET
;
1196 (*last_cmd
)->cmd
.reset
= cmd_queue_alloc(sizeof(reset_command_t
));
1197 (*last_cmd
)->cmd
.reset
->trst
= req_trst
;
1198 (*last_cmd
)->cmd
.reset
->srst
= req_srst
;
1203 void jtag_add_end_state(enum tap_state state
)
1205 cmd_queue_end_state
= state
;
1206 if ((cmd_queue_end_state
== TAP_DRSHIFT
)||(cmd_queue_end_state
== TAP_IRSHIFT
))
1208 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
1212 int MINIDRIVER(interface_jtag_add_sleep
)(u32 us
)
1214 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1216 /* allocate memory for a new list member */
1217 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1218 (*last_cmd
)->next
= NULL
;
1219 last_comand_pointer
= &((*last_cmd
)->next
);
1220 (*last_cmd
)->type
= JTAG_SLEEP
;
1222 (*last_cmd
)->cmd
.sleep
= cmd_queue_alloc(sizeof(sleep_command_t
));
1223 (*last_cmd
)->cmd
.sleep
->us
= us
;
1228 void jtag_add_sleep(u32 us
)
1230 keep_alive(); /* we might be running on a very slow JTAG clk */
1231 int retval
=interface_jtag_add_sleep(us
);
1232 if (retval
!=ERROR_OK
)
1237 int jtag_scan_size(scan_command_t
*cmd
)
1242 /* count bits in scan command */
1243 for (i
= 0; i
< cmd
->num_fields
; i
++)
1245 bit_count
+= cmd
->fields
[i
].num_bits
;
1251 int jtag_build_buffer(scan_command_t
*cmd
, u8
**buffer
)
1256 bit_count
= jtag_scan_size(cmd
);
1257 *buffer
= malloc(CEIL(bit_count
, 8));
1261 for (i
= 0; i
< cmd
->num_fields
; i
++)
1263 if (cmd
->fields
[i
].out_value
)
1265 #ifdef _DEBUG_JTAG_IO_
1266 char* char_buf
= buf_to_str(cmd
->fields
[i
].out_value
, (cmd
->fields
[i
].num_bits
> 64) ? 64 : cmd
->fields
[i
].num_bits
, 16);
1268 buf_set_buf(cmd
->fields
[i
].out_value
, 0, *buffer
, bit_count
, cmd
->fields
[i
].num_bits
);
1269 #ifdef _DEBUG_JTAG_IO_
1270 LOG_DEBUG("fields[%i].out_value: 0x%s", i
, char_buf
);
1275 bit_count
+= cmd
->fields
[i
].num_bits
;
1281 int jtag_read_buffer(u8
*buffer
, scan_command_t
*cmd
)
1287 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1290 for (i
= 0; i
< cmd
->num_fields
; i
++)
1292 /* if neither in_value nor in_handler
1293 * are specified we don't have to examine this field
1295 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1297 int num_bits
= cmd
->fields
[i
].num_bits
;
1298 u8
*captured
= buf_set_buf(buffer
, bit_count
, malloc(CEIL(num_bits
, 8)), 0, num_bits
);
1300 #ifdef _DEBUG_JTAG_IO_
1303 char_buf
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1304 LOG_DEBUG("fields[%i].in_value: 0x%s", i
, char_buf
);
1308 if (cmd
->fields
[i
].in_value
)
1310 buf_cpy(captured
, cmd
->fields
[i
].in_value
, num_bits
);
1312 if (cmd
->fields
[i
].in_handler
)
1314 if (cmd
->fields
[i
].in_handler(cmd
->fields
[i
].in_value
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1316 LOG_WARNING("in_handler reported a failed check");
1317 retval
= ERROR_JTAG_QUEUE_FAILED
;
1322 /* no in_value specified, but a handler takes care of the scanned data */
1323 if (cmd
->fields
[i
].in_handler
&& (!cmd
->fields
[i
].in_value
))
1325 if (cmd
->fields
[i
].in_handler(captured
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1327 /* We're going to call the error:handler later, but if the in_handler
1328 * reported an error we report this failure upstream
1330 LOG_WARNING("in_handler reported a failed check");
1331 retval
= ERROR_JTAG_QUEUE_FAILED
;
1337 bit_count
+= cmd
->fields
[i
].num_bits
;
1343 int jtag_check_value(u8
*captured
, void *priv
, scan_field_t
*field
)
1345 int retval
= ERROR_OK
;
1346 int num_bits
= field
->num_bits
;
1348 int compare_failed
= 0;
1350 if (field
->in_check_mask
)
1351 compare_failed
= buf_cmp_mask(captured
, field
->in_check_value
, field
->in_check_mask
, num_bits
);
1353 compare_failed
= buf_cmp(captured
, field
->in_check_value
, num_bits
);
1355 if (compare_failed
){
1356 /* An error handler could have caught the failing check
1357 * only report a problem when there wasn't a handler, or if the handler
1358 * acknowledged the error
1360 LOG_WARNING("TAP %s:",
1361 (field
->tap
== NULL
) ? "(unknown)" : field
->tap
->dotted_name
);
1364 char *captured_char
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1365 char *in_check_value_char
= buf_to_str(field
->in_check_value
, (num_bits
> 64) ? 64 : num_bits
, 16);
1367 if (field
->in_check_mask
)
1369 char *in_check_mask_char
;
1370 in_check_mask_char
= buf_to_str(field
->in_check_mask
, (num_bits
> 64) ? 64 : num_bits
, 16);
1371 LOG_WARNING("value captured during scan didn't pass the requested check:");
1372 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
1373 captured_char
, in_check_value_char
, in_check_mask_char
);
1374 free(in_check_mask_char
);
1378 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
);
1381 free(captured_char
);
1382 free(in_check_value_char
);
1384 retval
= ERROR_JTAG_QUEUE_FAILED
;
1392 set up checking of this field using the in_handler. The values passed in must be valid until
1393 after jtag_execute() has completed.
1395 void jtag_set_check_value(scan_field_t
*field
, u8
*value
, u8
*mask
, error_handler_t
*in_error_handler
)
1398 field
->in_handler
= jtag_check_value
;
1400 field
->in_handler
= NULL
; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1401 field
->in_handler_priv
= NULL
;
1402 field
->in_check_value
= value
;
1403 field
->in_check_mask
= mask
;
1406 enum scan_type
jtag_scan_type(scan_command_t
*cmd
)
1411 for (i
= 0; i
< cmd
->num_fields
; i
++)
1413 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1415 if (cmd
->fields
[i
].out_value
)
1422 int MINIDRIVER(interface_jtag_execute_queue
)(void)
1428 LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
1432 retval
= jtag
->execute_queue();
1436 jtag_command_queue
= NULL
;
1437 last_comand_pointer
= &jtag_command_queue
;
1442 int jtag_execute_queue(void)
1444 int retval
=interface_jtag_execute_queue();
1445 if (retval
==ERROR_OK
)
1449 jtag_error
=ERROR_OK
;
1453 int jtag_reset_callback(enum jtag_event event
, void *priv
)
1455 jtag_tap_t
*tap
= priv
;
1459 if (event
== JTAG_TRST_ASSERTED
)
1461 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
1468 void jtag_sleep(u32 us
)
1470 alive_sleep(us
/1000);
1473 /* Try to examine chain layout according to IEEE 1149.1 §12
1475 int jtag_examine_chain(void)
1479 u8 idcode_buffer
[JTAG_MAX_CHAIN_SIZE
* 4];
1482 int device_count
= 0;
1483 u8 zero_check
= 0x0;
1484 u8 one_check
= 0xff;
1487 field
.num_bits
= sizeof(idcode_buffer
) * 8;
1488 field
.out_value
= idcode_buffer
;
1489 field
.out_mask
= NULL
;
1490 field
.in_value
= idcode_buffer
;
1491 field
.in_check_value
= NULL
;
1492 field
.in_check_mask
= NULL
;
1493 field
.in_handler
= NULL
;
1494 field
.in_handler_priv
= NULL
;
1496 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
; i
++)
1498 buf_set_u32(idcode_buffer
, i
* 32, 32, 0x000000FF);
1501 jtag_add_plain_dr_scan(1, &field
, TAP_RESET
);
1502 jtag_execute_queue();
1504 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
* 4; i
++)
1506 zero_check
|= idcode_buffer
[i
];
1507 one_check
&= idcode_buffer
[i
];
1510 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1511 if ((zero_check
== 0x00) || (one_check
== 0xff))
1513 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1514 return ERROR_JTAG_INIT_FAILED
;
1517 // point at the 1st tap
1518 tap
= jtag_NextEnabledTap(NULL
);
1520 LOG_ERROR("JTAG: No taps enabled?");
1521 return ERROR_JTAG_INIT_FAILED
;
1524 for (bit_count
= 0; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;)
1526 u32 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1527 if ((idcode
& 1) == 0)
1529 /* LSB must not be 0, this indicates a device in bypass */
1530 LOG_WARNING("Tap/Device does not have IDCODE");
1541 if (idcode
== 0x000000FF)
1544 /* End of chain (invalid manufacturer ID)
1546 * The JTAG examine is the very first thing that happens
1548 * A single JTAG device requires only 64 bits to be read back correctly.
1550 * The code below adds a check that the rest of the data scanned (640 bits)
1551 * are all as expected. This helps diagnose/catch problems with the JTAG chain
1553 * earlier and gives more helpful/explicit error messages.
1555 for (bit_count
+= 32; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;bit_count
+= 32)
1557 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1558 if (unexpected
||(idcode
!= 0x000000FF))
1560 LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count
, idcode
);
1568 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
1569 manufacturer
= EXTRACT_MFG(idcode
);
1570 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
1571 part
= EXTRACT_PART(idcode
);
1572 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
1573 version
= EXTRACT_VER(idcode
);
1575 LOG_INFO("JTAG tap: %s tap/device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1576 ((tap
!= NULL
) ? (tap
->dotted_name
) : "(not-named)"),
1577 idcode
, manufacturer
, part
, version
);
1583 tap
->idcode
= idcode
;
1585 if (tap
->expected_ids_cnt
> 0) {
1586 /* Loop over the expected identification codes and test for a match */
1588 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++) {
1589 if( tap
->idcode
== tap
->expected_ids
[ii
] ){
1594 /* If none of the expected ids matched, log an error */
1595 if (ii
== tap
->expected_ids_cnt
) {
1596 LOG_ERROR("JTAG tap: %s got: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1599 EXTRACT_MFG( tap
->idcode
),
1600 EXTRACT_PART( tap
->idcode
),
1601 EXTRACT_VER( tap
->idcode
) );
1602 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++) {
1603 LOG_ERROR("JTAG tap: %s expected %hhu of %hhu: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1606 tap
->expected_ids_cnt
,
1607 tap
->expected_ids
[ii
],
1608 EXTRACT_MFG( tap
->expected_ids
[ii
] ),
1609 EXTRACT_PART( tap
->expected_ids
[ii
] ),
1610 EXTRACT_VER( tap
->expected_ids
[ii
] ) );
1613 return ERROR_JTAG_INIT_FAILED
;
1615 LOG_INFO("JTAG Tap/device matched");
1619 LOG_INFO("JTAG TAP ID: 0x%08x - Unknown - please report (A) chipname and (B) idcode to the openocd project",
1623 tap
= jtag_NextEnabledTap(tap
);
1628 /* see if number of discovered devices matches configuration */
1629 if (device_count
!= jtag_NumEnabledTaps())
1631 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match (enabled) configuration (%i), total taps: %d",
1632 device_count
, jtag_NumEnabledTaps(), jtag_NumTotalTaps());
1633 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1634 return ERROR_JTAG_INIT_FAILED
;
1640 int jtag_validate_chain(void)
1643 int total_ir_length
= 0;
1649 total_ir_length
= 0;
1651 tap
= jtag_NextEnabledTap(tap
);
1655 total_ir_length
+= tap
->ir_length
;
1658 total_ir_length
+= 2;
1659 ir_test
= malloc(CEIL(total_ir_length
, 8));
1660 buf_set_ones(ir_test
, total_ir_length
);
1663 field
.num_bits
= total_ir_length
;
1664 field
.out_value
= ir_test
;
1665 field
.out_mask
= NULL
;
1666 field
.in_value
= ir_test
;
1667 field
.in_check_value
= NULL
;
1668 field
.in_check_mask
= NULL
;
1669 field
.in_handler
= NULL
;
1670 field
.in_handler_priv
= NULL
;
1672 jtag_add_plain_ir_scan(1, &field
, TAP_RESET
);
1673 jtag_execute_queue();
1678 tap
= jtag_NextEnabledTap(tap
);
1684 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x1)
1686 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1687 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1690 return ERROR_JTAG_INIT_FAILED
;
1692 chain_pos
+= tap
->ir_length
;
1695 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x3)
1697 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1698 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1701 return ERROR_JTAG_INIT_FAILED
;
1709 enum jtag_tap_cfg_param
{
1713 static Jim_Nvp nvp_config_opts
[] = {
1714 { .name
= "-event", .value
= JCFG_EVENT
},
1716 { .name
= NULL
, .value
= -1 }
1720 jtag_tap_configure_cmd( Jim_GetOptInfo
*goi
,
1727 /* parse config or cget options */
1728 while (goi
->argc
> 0) {
1729 Jim_SetEmptyResult (goi
->interp
);
1731 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
1733 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
1739 if (goi
->argc
== 0) {
1740 Jim_WrongNumArgs( goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ..." );
1744 e
= Jim_GetOpt_Nvp( goi
, nvp_jtag_tap_event
, &n
);
1746 Jim_GetOpt_NvpUnknown(goi
, nvp_jtag_tap_event
, 1);
1750 if (goi
->isconfigure
) {
1751 if (goi
->argc
!= 1) {
1752 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
1756 if (goi
->argc
!= 0) {
1757 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
1763 jtag_tap_event_action_t
*jteap
;
1765 jteap
= tap
->event_action
;
1766 /* replace existing? */
1768 if (jteap
->event
== n
->value
) {
1771 jteap
= jteap
->next
;
1774 if (goi
->isconfigure
) {
1775 if (jteap
== NULL
) {
1777 jteap
= calloc(1, sizeof (*jteap
));
1779 jteap
->event
= n
->value
;
1780 Jim_GetOpt_Obj( goi
, &o
);
1782 Jim_DecrRefCount(interp
, jteap
->body
);
1784 jteap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
1785 Jim_IncrRefCount(jteap
->body
);
1787 /* add to head of event list */
1788 jteap
->next
= tap
->event_action
;
1789 tap
->event_action
= jteap
;
1790 Jim_SetEmptyResult(goi
->interp
);
1793 if (jteap
== NULL
) {
1794 Jim_SetEmptyResult(goi
->interp
);
1796 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, jteap
->body
));
1803 } /* while (goi->argc) */
1809 jim_newtap_cmd( Jim_GetOptInfo
*goi
)
1819 const Jim_Nvp opts
[] = {
1820 #define NTAP_OPT_IRLEN 0
1821 { .name
= "-irlen" , .value
= NTAP_OPT_IRLEN
},
1822 #define NTAP_OPT_IRMASK 1
1823 { .name
= "-irmask" , .value
= NTAP_OPT_IRMASK
},
1824 #define NTAP_OPT_IRCAPTURE 2
1825 { .name
= "-ircapture" , .value
= NTAP_OPT_IRCAPTURE
},
1826 #define NTAP_OPT_ENABLED 3
1827 { .name
= "-enable" , .value
= NTAP_OPT_ENABLED
},
1828 #define NTAP_OPT_DISABLED 4
1829 { .name
= "-disable" , .value
= NTAP_OPT_DISABLED
},
1830 #define NTAP_OPT_EXPECTED_ID 5
1831 { .name
= "-expected-id" , .value
= NTAP_OPT_EXPECTED_ID
},
1832 { .name
= NULL
, .value
= -1 },
1836 pTap
= malloc( sizeof(jtag_tap_t
) );
1837 memset( pTap
, 0, sizeof(*pTap
) );
1839 Jim_SetResult_sprintf( goi
->interp
, "no memory");
1843 // we expect CHIP + TAP + OPTIONS
1845 if( goi
->argc
< 3 ){
1846 Jim_SetResult_sprintf(goi
->interp
, "Missing CHIP TAP OPTIONS ....");
1849 Jim_GetOpt_String( goi
, &cp
, NULL
);
1850 pTap
->chip
= strdup(cp
);
1852 Jim_GetOpt_String( goi
, &cp
, NULL
);
1853 pTap
->tapname
= strdup(cp
);
1855 // name + dot + name + null
1856 x
= strlen(pTap
->chip
) + 1 + strlen(pTap
->tapname
) + 1;
1858 sprintf( cp
, "%s.%s", pTap
->chip
, pTap
->tapname
);
1859 pTap
->dotted_name
= cp
;
1861 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
1862 pTap
->chip
, pTap
->tapname
, pTap
->dotted_name
, goi
->argc
);
1865 // default is enabled
1868 // deal with options
1869 #define NTREQ_IRLEN 1
1870 #define NTREQ_IRCAPTURE 2
1871 #define NTREQ_IRMASK 4
1873 // clear them as we find them
1874 reqbits
= (NTREQ_IRLEN
| NTREQ_IRCAPTURE
| NTREQ_IRMASK
);
1877 e
= Jim_GetOpt_Nvp( goi
, opts
, &n
);
1879 Jim_GetOpt_NvpUnknown( goi
, opts
, 0 );
1882 LOG_DEBUG("Processing option: %s", n
->name
);
1884 case NTAP_OPT_ENABLED
:
1887 case NTAP_OPT_DISABLED
:
1890 case NTAP_OPT_EXPECTED_ID
:
1892 u32
*new_expected_ids
;
1894 e
= Jim_GetOpt_Wide( goi
, &w
);
1896 Jim_SetResult_sprintf(goi
->interp
, "option: %s bad parameter", n
->name
);
1900 new_expected_ids
= malloc(sizeof(u32
) * (pTap
->expected_ids_cnt
+ 1));
1901 if (new_expected_ids
== NULL
) {
1902 Jim_SetResult_sprintf( goi
->interp
, "no memory");
1906 memcpy(new_expected_ids
, pTap
->expected_ids
, sizeof(u32
) * pTap
->expected_ids_cnt
);
1908 new_expected_ids
[pTap
->expected_ids_cnt
] = w
;
1910 free(pTap
->expected_ids
);
1911 pTap
->expected_ids
= new_expected_ids
;
1912 pTap
->expected_ids_cnt
++;
1915 case NTAP_OPT_IRLEN
:
1916 case NTAP_OPT_IRMASK
:
1917 case NTAP_OPT_IRCAPTURE
:
1918 e
= Jim_GetOpt_Wide( goi
, &w
);
1920 Jim_SetResult_sprintf( goi
->interp
, "option: %s bad parameter", n
->name
);
1923 if( (w
< 0) || (w
> 0xffff) ){
1925 Jim_SetResult_sprintf( goi
->interp
, "option: %s - wacky value: %d (0x%x)",
1926 n
->name
, (int)(w
), (int)(w
));
1930 case NTAP_OPT_IRLEN
:
1931 pTap
->ir_length
= w
;
1932 reqbits
&= (~(NTREQ_IRLEN
));
1934 case NTAP_OPT_IRMASK
:
1935 pTap
->ir_capture_mask
= w
;
1936 reqbits
&= (~(NTREQ_IRMASK
));
1938 case NTAP_OPT_IRCAPTURE
:
1939 pTap
->ir_capture_value
= w
;
1940 reqbits
&= (~(NTREQ_IRCAPTURE
));
1943 } // switch(n->value)
1944 } // while( goi->argc )
1946 // Did we get all the options?
1949 Jim_SetResult_sprintf( goi
->interp
,
1950 "newtap: %s missing required parameters",
1952 // fixme: Tell user what is missing :-(
1953 // no memory leaks pelase
1954 free(((void *)(pTap
->expected_ids
)));
1955 free(((void *)(pTap
->chip
)));
1956 free(((void *)(pTap
->tapname
)));
1957 free(((void *)(pTap
->dotted_name
)));
1958 free(((void *)(pTap
)));
1962 pTap
->expected
= malloc( pTap
->ir_length
);
1963 pTap
->expected_mask
= malloc( pTap
->ir_length
);
1964 pTap
->cur_instr
= malloc( pTap
->ir_length
);
1966 buf_set_u32( pTap
->expected
,
1969 pTap
->ir_capture_value
);
1970 buf_set_u32( pTap
->expected_mask
,
1973 pTap
->ir_capture_mask
);
1974 buf_set_ones( pTap
->cur_instr
,
1980 jtag_register_event_callback(jtag_reset_callback
, pTap
);
1982 ppTap
= &(jtag_all_taps
);
1983 while( (*ppTap
) != NULL
){
1984 ppTap
= &((*ppTap
)->next_tap
);
1988 static int n_taps
= 0;
1989 pTap
->abs_chain_position
= n_taps
++;
1991 LOG_DEBUG( "Created Tap: %s @ abs position %d, irlen %d, capture: 0x%x mask: 0x%x",
1992 (*ppTap
)->dotted_name
,
1993 (*ppTap
)->abs_chain_position
,
1994 (*ppTap
)->ir_length
,
1995 (*ppTap
)->ir_capture_value
,
1996 (*ppTap
)->ir_capture_mask
);
2004 jim_jtag_command( Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
2010 struct command_context_s
*context
;
2014 JTAG_CMD_INIT_RESET
,
2017 JTAG_CMD_TAPDISABLE
,
2018 JTAG_CMD_TAPISENABLED
,
2023 const Jim_Nvp jtag_cmds
[] = {
2024 { .name
= "interface" , .value
= JTAG_CMD_INTERFACE
},
2025 { .name
= "arp_init-reset", .value
= JTAG_CMD_INIT_RESET
},
2026 { .name
= "newtap" , .value
= JTAG_CMD_NEWTAP
},
2027 { .name
= "tapisenabled" , .value
= JTAG_CMD_TAPISENABLED
},
2028 { .name
= "tapenable" , .value
= JTAG_CMD_TAPENABLE
},
2029 { .name
= "tapdisable" , .value
= JTAG_CMD_TAPDISABLE
},
2030 { .name
= "configure" , .value
= JTAG_CMD_CONFIGURE
},
2031 { .name
= "cget" , .value
= JTAG_CMD_CGET
},
2033 { .name
= NULL
, .value
= -1 },
2036 context
= Jim_GetAssocData(interp
, "context");
2037 // go past the command
2038 Jim_GetOpt_Setup( &goi
, interp
, argc
-1, argv
+1 );
2040 e
= Jim_GetOpt_Nvp( &goi
, jtag_cmds
, &n
);
2042 Jim_GetOpt_NvpUnknown( &goi
, jtag_cmds
, 0 );
2045 Jim_SetEmptyResult( goi
.interp
);
2047 case JTAG_CMD_INTERFACE
:
2048 // return the name of the interface
2049 // TCL code might need to know the exact type...
2050 // FUTURE: we allow this as a means to "set" the interface.
2051 if( goi
.argc
!= 0 ){
2052 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
-1, "(no params)");
2055 Jim_SetResultString( goi
.interp
, jtag_interface
->name
, -1 );
2057 case JTAG_CMD_INIT_RESET
:
2058 if( goi
.argc
!= 0 ){
2059 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
-1, "(no params)");
2062 e
= jtag_init_reset(context
);
2063 if( e
!= ERROR_OK
){
2064 Jim_SetResult_sprintf( goi
.interp
, "error: %d", e
);
2068 case JTAG_CMD_NEWTAP
:
2069 return jim_newtap_cmd( &goi
);
2071 case JTAG_CMD_TAPISENABLED
:
2072 case JTAG_CMD_TAPENABLE
:
2073 case JTAG_CMD_TAPDISABLE
:
2074 if( goi
.argc
!= 1 ){
2075 Jim_SetResultString( goi
.interp
, "Too many parameters",-1 );
2081 t
= jtag_TapByJimObj( goi
.interp
, goi
.argv
[0] );
2086 case JTAG_CMD_TAPISENABLED
:
2089 case JTAG_CMD_TAPENABLE
:
2090 jtag_tap_handle_event( t
, JTAG_TAP_EVENT_ENABLE
);
2094 case JTAG_CMD_TAPDISABLE
:
2095 jtag_tap_handle_event( t
, JTAG_TAP_EVENT_DISABLE
);
2100 Jim_SetResult( goi
.interp
, Jim_NewIntObj( goi
.interp
, e
) );
2107 Jim_WrongNumArgs( goi
.interp
, 0, NULL
, "?tap-name? -option ...");
2114 Jim_GetOpt_Obj(&goi
, &o
);
2115 t
= jtag_TapByJimObj( goi
.interp
, o
);
2120 goi
.isconfigure
= 0;
2121 return jtag_tap_configure_cmd( &goi
, t
);
2125 case JTAG_CMD_CONFIGURE
:
2127 Jim_WrongNumArgs( goi
.interp
, 0, NULL
, "?tap-name? -option ?VALUE? ...");
2134 Jim_GetOpt_Obj(&goi
, &o
);
2135 t
= jtag_TapByJimObj( goi
.interp
, o
);
2140 goi
.isconfigure
= 1;
2141 return jtag_tap_configure_cmd( &goi
, t
);
2149 int jtag_register_commands(struct command_context_s
*cmd_ctx
)
2151 register_jim( cmd_ctx
, "jtag", jim_jtag_command
, "perform jtag tap actions");
2153 register_command(cmd_ctx
, NULL
, "interface", handle_interface_command
,
2154 COMMAND_CONFIG
, "try to configure interface");
2155 register_command(cmd_ctx
, NULL
, "jtag_speed", handle_jtag_speed_command
,
2156 COMMAND_ANY
, "set jtag speed (if supported)");
2157 register_command(cmd_ctx
, NULL
, "jtag_khz", handle_jtag_khz_command
,
2158 COMMAND_ANY
, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
2159 register_command(cmd_ctx
, NULL
, "jtag_device", handle_jtag_device_command
,
2160 COMMAND_CONFIG
, "jtag_device <ir_length> <ir_expected> <ir_mask>");
2161 register_command(cmd_ctx
, NULL
, "reset_config", handle_reset_config_command
,
2162 COMMAND_CONFIG
, NULL
);
2163 register_command(cmd_ctx
, NULL
, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command
,
2164 COMMAND_ANY
, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
2165 register_command(cmd_ctx
, NULL
, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command
,
2166 COMMAND_ANY
, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
2168 register_command(cmd_ctx
, NULL
, "scan_chain", handle_scan_chain_command
,
2169 COMMAND_EXEC
, "print current scan chain configuration");
2171 register_command(cmd_ctx
, NULL
, "endstate", handle_endstate_command
,
2172 COMMAND_EXEC
, "finish JTAG operations in <tap_state>");
2173 register_command(cmd_ctx
, NULL
, "jtag_reset", handle_jtag_reset_command
,
2174 COMMAND_EXEC
, "toggle reset lines <trst> <srst>");
2175 register_command(cmd_ctx
, NULL
, "runtest", handle_runtest_command
,
2176 COMMAND_EXEC
, "move to Run-Test/Idle, and execute <num_cycles>");
2177 register_command(cmd_ctx
, NULL
, "irscan", handle_irscan_command
,
2178 COMMAND_EXEC
, "execute IR scan <device> <instr> [dev2] [instr2] ...");
2179 register_jim(cmd_ctx
, "drscan", Jim_Command_drscan
, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
2181 register_command(cmd_ctx
, NULL
, "verify_ircapture", handle_verify_ircapture_command
,
2182 COMMAND_ANY
, "verify value captured during Capture-IR <enable|disable>");
2186 int jtag_interface_init(struct command_context_s
*cmd_ctx
)
2191 if (!jtag_interface
)
2193 /* nothing was previously specified by "interface" command */
2194 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
2195 return ERROR_JTAG_INVALID_INTERFACE
;
2199 jtag_interface
->khz(speed_khz
, &jtag_speed
);
2203 if (jtag_interface
->init() != ERROR_OK
)
2204 return ERROR_JTAG_INIT_FAILED
;
2208 jtag
= jtag_interface
;
2212 static int jtag_init_inner(struct command_context_s
*cmd_ctx
)
2217 LOG_DEBUG("Init JTAG chain");
2220 tap
= jtag_NextEnabledTap(NULL
);
2222 LOG_ERROR("There are no enabled taps?");
2223 return ERROR_JTAG_INIT_FAILED
;
2227 if ((retval
=jtag_execute_queue())!=ERROR_OK
)
2230 /* examine chain first, as this could discover the real chain layout */
2231 if (jtag_examine_chain() != ERROR_OK
)
2233 LOG_ERROR("trying to validate configured JTAG chain anyway...");
2236 if (jtag_validate_chain() != ERROR_OK
)
2238 LOG_ERROR("Could not validate JTAG chain, continuing anyway...");
2244 int jtag_init_reset(struct command_context_s
*cmd_ctx
)
2248 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
2251 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
2253 /* Reset can happen after a power cycle.
2255 * Ideally we would only assert TRST or run RESET before the target reset.
2257 * However w/srst_pulls_trst, trst is asserted together with the target
2258 * reset whether we want it or not.
2260 * NB! Some targets have JTAG circuitry disabled until a
2261 * trst & srst has been asserted.
2263 * NB! here we assume nsrst/ntrst delay are sufficient!
2265 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
2268 jtag_add_reset(1, 0); /* RESET or TRST */
2269 if (jtag_reset_config
& RESET_HAS_SRST
)
2271 jtag_add_reset(1, 1);
2272 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0)
2273 jtag_add_reset(0, 1);
2275 jtag_add_reset(0, 0);
2276 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
2279 /* Check that we can communication on the JTAG chain + eventually we want to
2280 * be able to perform enumeration only after OpenOCD has started
2281 * telnet and GDB server
2283 * That would allow users to more easily perform any magic they need to before
2286 return jtag_init_inner(cmd_ctx
);
2289 int jtag_init(struct command_context_s
*cmd_ctx
)
2292 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
2294 if (jtag_init_inner(cmd_ctx
)==ERROR_OK
)
2298 return jtag_init_reset(cmd_ctx
);
2301 static int default_khz(int khz
, int *jtag_speed
)
2303 LOG_ERROR("Translation from khz to jtag_speed not implemented");
2307 static int default_speed_div(int speed
, int *khz
)
2309 LOG_ERROR("Translation from jtag_speed to khz not implemented");
2313 static int default_power_dropout(int *dropout
)
2315 *dropout
=0; /* by default we can't detect power dropout */
2319 static int default_srst_asserted(int *srst_asserted
)
2321 *srst_asserted
=0; /* by default we can't detect srst asserted */
2325 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2330 /* check whether the interface is already configured */
2333 LOG_WARNING("Interface already configured, ignoring");
2337 /* interface name is a mandatory argument */
2338 if (argc
< 1 || args
[0][0] == '\0')
2340 return ERROR_COMMAND_SYNTAX_ERROR
;
2343 for (i
=0; jtag_interfaces
[i
]; i
++)
2345 if (strcmp(args
[0], jtag_interfaces
[i
]->name
) == 0)
2347 if ((retval
= jtag_interfaces
[i
]->register_commands(cmd_ctx
)) != ERROR_OK
)
2352 jtag_interface
= jtag_interfaces
[i
];
2354 if (jtag_interface
->khz
== NULL
)
2356 jtag_interface
->khz
= default_khz
;
2358 if (jtag_interface
->speed_div
== NULL
)
2360 jtag_interface
->speed_div
= default_speed_div
;
2362 if (jtag_interface
->power_dropout
== NULL
)
2364 jtag_interface
->power_dropout
= default_power_dropout
;
2366 if (jtag_interface
->srst_asserted
== NULL
)
2368 jtag_interface
->srst_asserted
= default_srst_asserted
;
2375 /* no valid interface was found (i.e. the configuration option,
2376 * didn't match one of the compiled-in interfaces
2378 LOG_ERROR("No valid jtag interface found (%s)", args
[0]);
2379 LOG_ERROR("compiled-in jtag interfaces:");
2380 for (i
= 0; jtag_interfaces
[i
]; i
++)
2382 LOG_ERROR("%i: %s", i
, jtag_interfaces
[i
]->name
);
2385 return ERROR_JTAG_INVALID_INTERFACE
;
2388 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2392 Jim_Obj
*newargs
[ 10 ];
2396 // argv[-1] = command
2397 // argv[ 0] = ir length
2398 // argv[ 1] = ir capture
2399 // argv[ 2] = ir mask
2400 // argv[ 3] = not actually used by anything but in the docs
2403 command_print( cmd_ctx
, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
2406 command_print( cmd_ctx
, "OLD SYNTAX: DEPRECATED - translating to new syntax");
2407 command_print( cmd_ctx
, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
2411 command_print( cmd_ctx
, "Example: STM32 has 2 taps, the cortexM3(len4) + boundryscan(len5)");
2412 command_print( cmd_ctx
, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
2413 command_print( cmd_ctx
, "jtag newtap stm32 boundry ....., and the tap: \"stm32.boundery\"");
2414 command_print( cmd_ctx
, "And then refer to the taps by the dotted name.");
2418 newargs
[0] = Jim_NewStringObj( interp
, "jtag", -1 );
2419 newargs
[1] = Jim_NewStringObj( interp
, "newtap", -1 );
2420 sprintf( buf
, "chip%d", jtag_NumTotalTaps() );
2421 newargs
[2] = Jim_NewStringObj( interp
, buf
, -1 );
2422 sprintf( buf
, "tap%d", jtag_NumTotalTaps() );
2423 newargs
[3] = Jim_NewStringObj( interp
, buf
, -1 );
2424 newargs
[4] = Jim_NewStringObj( interp
, "-irlen", -1 );
2425 newargs
[5] = Jim_NewStringObj( interp
, args
[0], -1 );
2426 newargs
[6] = Jim_NewStringObj( interp
, "-ircapture", -1 );
2427 newargs
[7] = Jim_NewStringObj( interp
, args
[1], -1 );
2428 newargs
[8] = Jim_NewStringObj( interp
, "-irmask", -1 );
2429 newargs
[9] = Jim_NewStringObj( interp
, args
[2], -1 );
2431 command_print( cmd_ctx
, "NEW COMMAND:");
2432 sprintf( buf
, "%s %s %s %s %s %s %s %s %s %s",
2433 Jim_GetString( newargs
[0], NULL
),
2434 Jim_GetString( newargs
[1], NULL
),
2435 Jim_GetString( newargs
[2], NULL
),
2436 Jim_GetString( newargs
[3], NULL
),
2437 Jim_GetString( newargs
[4], NULL
),
2438 Jim_GetString( newargs
[5], NULL
),
2439 Jim_GetString( newargs
[6], NULL
),
2440 Jim_GetString( newargs
[7], NULL
),
2441 Jim_GetString( newargs
[8], NULL
),
2442 Jim_GetString( newargs
[9], NULL
) );
2446 e
= jim_jtag_command( interp
, 10, newargs
);
2448 command_print( cmd_ctx
, "%s", Jim_GetString( Jim_GetResult(interp
), NULL
) );
2454 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2458 tap
= jtag_all_taps
;
2459 command_print(cmd_ctx
, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
2460 command_print(cmd_ctx
, "---|--------------------|---------|------------|------------|------|------|------|---------");
2463 u32 expected
, expected_mask
, cur_instr
, ii
;
2464 expected
= buf_get_u32(tap
->expected
, 0, tap
->ir_length
);
2465 expected_mask
= buf_get_u32(tap
->expected_mask
, 0, tap
->ir_length
);
2466 cur_instr
= buf_get_u32(tap
->cur_instr
, 0, tap
->ir_length
);
2468 command_print(cmd_ctx
,
2469 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
2470 tap
->abs_chain_position
,
2472 tap
->enabled
? 'Y' : 'n',
2474 (tap
->expected_ids_cnt
> 0 ? tap
->expected_ids
[0] : 0),
2480 for (ii
= 1; ii
< tap
->expected_ids_cnt
; ii
++) {
2481 command_print(cmd_ctx
, " | | | | 0x%08x | | | | ",
2482 tap
->expected_ids
[ii
]);
2485 tap
= tap
->next_tap
;
2491 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2494 return ERROR_COMMAND_SYNTAX_ERROR
;
2498 if (strcmp(args
[0], "none") == 0)
2499 jtag_reset_config
= RESET_NONE
;
2500 else if (strcmp(args
[0], "trst_only") == 0)
2501 jtag_reset_config
= RESET_HAS_TRST
;
2502 else if (strcmp(args
[0], "srst_only") == 0)
2503 jtag_reset_config
= RESET_HAS_SRST
;
2504 else if (strcmp(args
[0], "trst_and_srst") == 0)
2505 jtag_reset_config
= RESET_TRST_AND_SRST
;
2508 LOG_ERROR("(1) invalid reset_config argument (%s), defaulting to none", args
[0]);
2509 jtag_reset_config
= RESET_NONE
;
2510 return ERROR_INVALID_ARGUMENTS
;
2516 if (strcmp(args
[1], "separate") == 0)
2518 /* seperate reset lines - default */
2521 if (strcmp(args
[1], "srst_pulls_trst") == 0)
2522 jtag_reset_config
|= RESET_SRST_PULLS_TRST
;
2523 else if (strcmp(args
[1], "trst_pulls_srst") == 0)
2524 jtag_reset_config
|= RESET_TRST_PULLS_SRST
;
2525 else if (strcmp(args
[1], "combined") == 0)
2526 jtag_reset_config
|= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
2529 LOG_ERROR("(2) invalid reset_config argument (%s), defaulting to none", args
[1]);
2530 jtag_reset_config
= RESET_NONE
;
2531 return ERROR_INVALID_ARGUMENTS
;
2538 if (strcmp(args
[2], "trst_open_drain") == 0)
2539 jtag_reset_config
|= RESET_TRST_OPEN_DRAIN
;
2540 else if (strcmp(args
[2], "trst_push_pull") == 0)
2541 jtag_reset_config
&= ~RESET_TRST_OPEN_DRAIN
;
2544 LOG_ERROR("(3) invalid reset_config argument (%s) defaulting to none", args
[2] );
2545 jtag_reset_config
= RESET_NONE
;
2546 return ERROR_INVALID_ARGUMENTS
;
2552 if (strcmp(args
[3], "srst_push_pull") == 0)
2553 jtag_reset_config
|= RESET_SRST_PUSH_PULL
;
2554 else if (strcmp(args
[3], "srst_open_drain") == 0)
2555 jtag_reset_config
&= ~RESET_SRST_PUSH_PULL
;
2558 LOG_ERROR("(4) invalid reset_config argument (%s), defaulting to none", args
[3]);
2559 jtag_reset_config
= RESET_NONE
;
2560 return ERROR_INVALID_ARGUMENTS
;
2567 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2571 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
2576 jtag_nsrst_delay
= strtoul(args
[0], NULL
, 0);
2582 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2586 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
2591 jtag_ntrst_delay
= strtoul(args
[0], NULL
, 0);
2597 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2599 int retval
=ERROR_OK
;
2603 LOG_DEBUG("handle jtag speed");
2606 cur_speed
= jtag_speed
= strtoul(args
[0], NULL
, 0);
2608 /* this command can be called during CONFIG,
2609 * in which case jtag isn't initialized */
2612 retval
=jtag
->speed(cur_speed
);
2614 } else if (argc
== 0)
2618 return ERROR_COMMAND_SYNTAX_ERROR
;
2620 command_print(cmd_ctx
, "jtag_speed: %d", jtag_speed
);
2625 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2627 int retval
=ERROR_OK
;
2628 LOG_DEBUG("handle jtag khz");
2632 speed_khz
= strtoul(args
[0], NULL
, 0);
2636 LOG_DEBUG("have interface set up");
2638 if ((retval
=jtag
->khz(speed_khz
, &speed_div1
))!=ERROR_OK
)
2644 cur_speed
= jtag_speed
= speed_div1
;
2646 retval
=jtag
->speed(cur_speed
);
2655 return ERROR_COMMAND_SYNTAX_ERROR
;
2660 if ((retval
=jtag
->speed_div(jtag_speed
, &speed_khz
))!=ERROR_OK
)
2666 command_print(cmd_ctx
, "RCLK - adaptive");
2669 command_print(cmd_ctx
, "%d kHz", speed_khz
);
2675 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2677 enum tap_state state
;
2681 return ERROR_COMMAND_SYNTAX_ERROR
;
2685 for (state
= 0; state
< 16; state
++)
2687 if (strcmp(args
[0], tap_state_strings
[state
]) == 0)
2689 jtag_add_end_state(state
);
2690 jtag_execute_queue();
2694 command_print(cmd_ctx
, "current endstate: %s", tap_state_strings
[cmd_queue_end_state
]);
2699 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2706 return ERROR_COMMAND_SYNTAX_ERROR
;
2709 if (args
[0][0] == '1')
2711 else if (args
[0][0] == '0')
2715 return ERROR_COMMAND_SYNTAX_ERROR
;
2718 if (args
[1][0] == '1')
2720 else if (args
[1][0] == '0')
2724 return ERROR_COMMAND_SYNTAX_ERROR
;
2727 if (jtag_interface_init(cmd_ctx
) != ERROR_OK
)
2728 return ERROR_JTAG_INIT_FAILED
;
2730 jtag_add_reset(trst
, srst
);
2731 jtag_execute_queue();
2736 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2740 return ERROR_COMMAND_SYNTAX_ERROR
;
2743 jtag_add_runtest(strtol(args
[0], NULL
, 0), -1);
2744 jtag_execute_queue();
2750 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2753 scan_field_t
*fields
;
2756 if ((argc
< 2) || (argc
% 2))
2758 return ERROR_COMMAND_SYNTAX_ERROR
;
2761 fields
= malloc(sizeof(scan_field_t
) * argc
/ 2);
2763 for (i
= 0; i
< argc
/ 2; i
++)
2765 tap
= jtag_TapByString( args
[i
*2] );
2768 command_print( cmd_ctx
, "Tap: %s unknown", args
[i
*2] );
2771 int field_size
= tap
->ir_length
;
2772 fields
[i
].tap
= tap
;
2773 fields
[i
].out_value
= malloc(CEIL(field_size
, 8));
2774 buf_set_u32(fields
[i
].out_value
, 0, field_size
, strtoul(args
[i
*2+1], NULL
, 0));
2775 fields
[i
].out_mask
= NULL
;
2776 fields
[i
].in_value
= NULL
;
2777 fields
[i
].in_check_mask
= NULL
;
2778 fields
[i
].in_handler
= NULL
;
2779 fields
[i
].in_handler_priv
= NULL
;
2782 jtag_add_ir_scan(argc
/ 2, fields
, -1);
2783 jtag_execute_queue();
2785 for (i
= 0; i
< argc
/ 2; i
++)
2786 free(fields
[i
].out_value
);
2793 int Jim_Command_drscan(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *args
)
2796 scan_field_t
*fields
;
2798 int field_count
= 0;
2803 * args[2] = num_bits
2804 * args[3] = hex string
2805 * ... repeat num bits and hex string ...
2807 if ((argc
< 4) || ((argc
% 2)!=0))
2809 Jim_WrongNumArgs(interp
, 1, args
, "wrong arguments");
2813 for (i
= 2; i
< argc
; i
+=2)
2817 e
= Jim_GetLong(interp
, args
[i
], &bits
);
2822 tap
= jtag_TapByJimObj( interp
, args
[1] );
2827 num_fields
=(argc
-2)/2;
2828 fields
= malloc(sizeof(scan_field_t
) * num_fields
);
2829 for (i
= 2; i
< argc
; i
+=2)
2835 Jim_GetLong(interp
, args
[i
], &bits
);
2836 str
= Jim_GetString(args
[i
+1], &len
);
2839 fields
[field_count
].tap
= tap
;
2840 fields
[field_count
].num_bits
= bits
;
2841 fields
[field_count
].out_value
= malloc(CEIL(bits
, 8));
2842 str_to_buf(str
, len
, fields
[field_count
].out_value
, bits
, 0);
2843 fields
[field_count
].out_mask
= NULL
;
2844 fields
[field_count
].in_value
= fields
[field_count
].out_value
;
2845 fields
[field_count
].in_check_mask
= NULL
;
2846 fields
[field_count
].in_check_value
= NULL
;
2847 fields
[field_count
].in_handler
= NULL
;
2848 fields
[field_count
++].in_handler_priv
= NULL
;
2851 jtag_add_dr_scan(num_fields
, fields
, -1);
2852 retval
= jtag_execute_queue();
2853 if (retval
!= ERROR_OK
)
2855 Jim_SetResultString(interp
, "drscan: jtag execute failed",-1);
2860 Jim_Obj
*list
= Jim_NewListObj(interp
, NULL
, 0);
2861 for (i
= 2; i
< argc
; i
+=2)
2866 Jim_GetLong(interp
, args
[i
], &bits
);
2867 str
= buf_to_str(fields
[field_count
].in_value
, bits
, 16);
2868 free(fields
[field_count
].out_value
);
2870 Jim_ListAppendElement(interp
, list
, Jim_NewStringObj(interp
, str
, strlen(str
)));
2875 Jim_SetResult(interp
, list
);
2882 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2886 if (strcmp(args
[0], "enable") == 0)
2888 jtag_verify_capture_ir
= 1;
2890 else if (strcmp(args
[0], "disable") == 0)
2892 jtag_verify_capture_ir
= 0;
2895 return ERROR_COMMAND_SYNTAX_ERROR
;
2897 } else if (argc
!= 0)
2899 return ERROR_COMMAND_SYNTAX_ERROR
;
2902 command_print(cmd_ctx
, "verify Capture-IR is %s", (jtag_verify_capture_ir
) ? "enabled": "disabled");
2908 int jtag_power_dropout(int *dropout
)
2910 return jtag
->power_dropout(dropout
);
2913 int jtag_srst_asserted(int *srst_asserted
)
2915 return jtag
->srst_asserted(srst_asserted
);
2918 void jtag_tap_handle_event( jtag_tap_t
* tap
, enum jtag_tap_event e
)
2920 jtag_tap_event_action_t
* jteap
;
2923 jteap
= tap
->event_action
;
2927 if (jteap
->event
== e
) {
2929 LOG_DEBUG( "JTAG tap: %s event: %d (%s) action: %s\n",
2932 Jim_Nvp_value2name_simple(nvp_jtag_tap_event
, e
)->name
,
2933 Jim_GetString(jteap
->body
, NULL
) );
2934 if (Jim_EvalObj(interp
, jteap
->body
) != JIM_OK
) {
2935 Jim_PrintErrorMessage(interp
);
2939 jteap
= jteap
->next
;
2943 LOG_DEBUG( "event %d %s - no action",
2945 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)