1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
27 #include "replacements.h"
38 /* note that this is not marked as static as it must be available from outside jtag.c for those
39 that implement the jtag_xxx() minidriver layer
41 int jtag_error
=ERROR_OK
;
44 typedef struct cmd_queue_page_s
48 struct cmd_queue_page_s
*next
;
51 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
52 static cmd_queue_page_t
*cmd_queue_pages
= NULL
;
54 /* tap_move[i][j]: tap movement command to go from state i to state j
62 * DRSHIFT->DRSHIFT and IRSHIFT->IRSHIFT have to be caught in interface specific code
66 /* RESET IDLE DRSHIFT DRPAUSE IRSHIFT IRPAUSE */
67 { 0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* RESET */
68 { 0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* IDLE */
69 { 0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* DRSHIFT */
70 { 0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* DRPAUSE */
71 { 0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* IRSHIFT */
72 { 0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* IRPAUSE */
75 int tap_move_map
[16] = {
76 0, -1, -1, 2, -1, 3, -1, -1,
77 1, -1, -1, 4, -1, 5, -1, -1
80 tap_transition_t tap_transitions
[16] =
82 {TAP_RESET
, TAP_IDLE
}, /* RESET */
83 {TAP_IRSELECT
, TAP_DRCAPTURE
}, /* DRSELECT */
84 {TAP_DREXIT1
, TAP_DRSHIFT
}, /* DRCAPTURE */
85 {TAP_DREXIT1
, TAP_DRSHIFT
}, /* DRSHIFT */
86 {TAP_DRUPDATE
, TAP_DRPAUSE
}, /* DREXIT1 */
87 {TAP_DREXIT2
, TAP_DRPAUSE
}, /* DRPAUSE */
88 {TAP_DRUPDATE
, TAP_DRSHIFT
}, /* DREXIT2 */
89 {TAP_DRSELECT
, TAP_IDLE
}, /* DRUPDATE */
90 {TAP_DRSELECT
, TAP_IDLE
}, /* IDLE */
91 {TAP_RESET
, TAP_IRCAPTURE
}, /* IRSELECT */
92 {TAP_IREXIT1
, TAP_IRSHIFT
}, /* IRCAPTURE */
93 {TAP_IREXIT1
, TAP_IRSHIFT
}, /* IRSHIFT */
94 {TAP_IRUPDATE
, TAP_IRPAUSE
}, /* IREXIT1 */
95 {TAP_IREXIT2
, TAP_IRPAUSE
}, /* IRPAUSE */
96 {TAP_IRUPDATE
, TAP_IRSHIFT
}, /* IREXIT2 */
97 {TAP_DRSELECT
, TAP_IDLE
} /* IRUPDATE */
100 char* jtag_event_strings
[] =
102 "JTAG controller reset (RESET or TRST)"
105 const Jim_Nvp nvp_jtag_tap_event
[] = {
106 { .value
= JTAG_TAP_EVENT_ENABLE
, .name
= "tap-enable" },
107 { .value
= JTAG_TAP_EVENT_DISABLE
, .name
= "tap-disable" },
109 { .name
= NULL
, .value
= -1 }
112 /* kludge!!!! these are just global variables that the
113 * interface use internally. They really belong
114 * inside the drivers, but we don't want to break
115 * linking the drivers!!!!
117 enum tap_state end_state
= TAP_RESET
;
118 enum tap_state cur_state
= TAP_RESET
;
122 jtag_command_t
*jtag_command_queue
= NULL
;
123 jtag_command_t
**last_comand_pointer
= &jtag_command_queue
;
124 static jtag_tap_t
*jtag_all_taps
= NULL
;
126 enum reset_types jtag_reset_config
= RESET_NONE
;
127 enum tap_state cmd_queue_end_state
= TAP_RESET
;
128 enum tap_state cmd_queue_cur_state
= TAP_RESET
;
130 int jtag_verify_capture_ir
= 1;
132 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
133 int jtag_nsrst_delay
= 0; /* default to no nSRST delay */
134 int jtag_ntrst_delay
= 0; /* default to no nTRST delay */
136 /* maximum number of JTAG devices expected in the chain
138 #define JTAG_MAX_CHAIN_SIZE 20
140 /* callbacks to inform high-level handlers about JTAG state changes */
141 jtag_event_callback_t
*jtag_event_callbacks
;
144 static int speed_khz
= 0;
145 /* flag if the kHz speed was defined */
146 static int hasKHz
= 0;
148 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
151 #if BUILD_ECOSBOARD == 1
152 extern jtag_interface_t zy1000_interface
;
155 #if BUILD_PARPORT == 1
156 extern jtag_interface_t parport_interface
;
160 extern jtag_interface_t dummy_interface
;
163 #if BUILD_FT2232_FTD2XX == 1
164 extern jtag_interface_t ft2232_interface
;
167 #if BUILD_FT2232_LIBFTDI == 1
168 extern jtag_interface_t ft2232_interface
;
171 #if BUILD_AMTJTAGACCEL == 1
172 extern jtag_interface_t amt_jtagaccel_interface
;
175 #if BUILD_EP93XX == 1
176 extern jtag_interface_t ep93xx_interface
;
179 #if BUILD_AT91RM9200 == 1
180 extern jtag_interface_t at91rm9200_interface
;
183 #if BUILD_GW16012 == 1
184 extern jtag_interface_t gw16012_interface
;
187 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
188 extern jtag_interface_t presto_interface
;
191 #if BUILD_USBPROG == 1
192 extern jtag_interface_t usbprog_interface
;
196 extern jtag_interface_t jlink_interface
;
200 extern jtag_interface_t rlink_interface
;
203 jtag_interface_t
*jtag_interfaces
[] = {
204 #if BUILD_ECOSBOARD == 1
207 #if BUILD_PARPORT == 1
213 #if BUILD_FT2232_FTD2XX == 1
216 #if BUILD_FT2232_LIBFTDI == 1
219 #if BUILD_AMTJTAGACCEL == 1
220 &amt_jtagaccel_interface
,
222 #if BUILD_EP93XX == 1
225 #if BUILD_AT91RM9200 == 1
226 &at91rm9200_interface
,
228 #if BUILD_GW16012 == 1
231 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
234 #if BUILD_USBPROG == 1
246 jtag_interface_t
*jtag
= NULL
;
249 jtag_interface_t
*jtag_interface
= NULL
;
252 /* forward declarations */
253 void jtag_add_pathmove(int num_states
, enum tap_state
*path
);
254 void jtag_add_runtest(int num_cycles
, enum tap_state endstate
);
255 void jtag_add_end_state(enum tap_state endstate
);
256 void jtag_add_sleep(u32 us
);
257 int jtag_execute_queue(void);
261 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
262 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
263 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
264 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
265 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
266 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
267 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
269 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
271 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
272 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
273 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
274 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
275 int Jim_Command_drscan(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
277 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
279 jtag_tap_t
*jtag_AllTaps(void)
281 return jtag_all_taps
;
284 int jtag_NumTotalTaps(void)
298 int jtag_NumEnabledTaps(void)
315 jtag_tap_t
*jtag_TapByString( const char *s
)
323 if( 0 == strcmp( t
->dotted_name
, s
) ){
329 // backup plan is by number
331 /* ok - is "s" a number? */
333 n
= strtol( s
, &cp
, 0 );
334 if( (s
!= cp
) && (*cp
== 0) ){
336 t
= jtag_TapByAbsPosition(n
);
342 jtag_tap_t
* jtag_TapByJimObj( Jim_Interp
*interp
, Jim_Obj
*o
)
347 cp
= Jim_GetString( o
, NULL
);
352 t
= jtag_TapByString( cp
);
355 Jim_SetResult_sprintf(interp
,"Tap: %s is unknown", cp
);
360 /* returns a pointer to the n-th device in the scan chain */
361 jtag_tap_t
* jtag_TapByAbsPosition( int n
)
369 while( t
&& (n
> 0)) {
376 int jtag_register_event_callback(int (*callback
)(enum jtag_event event
, void *priv
), void *priv
)
378 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
380 if (callback
== NULL
)
382 return ERROR_INVALID_ARGUMENTS
;
387 while ((*callbacks_p
)->next
)
388 callbacks_p
= &((*callbacks_p
)->next
);
389 callbacks_p
= &((*callbacks_p
)->next
);
392 (*callbacks_p
) = malloc(sizeof(jtag_event_callback_t
));
393 (*callbacks_p
)->callback
= callback
;
394 (*callbacks_p
)->priv
= priv
;
395 (*callbacks_p
)->next
= NULL
;
400 int jtag_unregister_event_callback(int (*callback
)(enum jtag_event event
, void *priv
))
402 jtag_event_callback_t
**callbacks_p
= &jtag_event_callbacks
;
404 if (callback
== NULL
)
406 return ERROR_INVALID_ARGUMENTS
;
411 jtag_event_callback_t
**next
= &((*callbacks_p
)->next
);
412 if ((*callbacks_p
)->callback
== callback
)
415 *callbacks_p
= *next
;
423 int jtag_call_event_callbacks(enum jtag_event event
)
425 jtag_event_callback_t
*callback
= jtag_event_callbacks
;
427 LOG_DEBUG("jtag event: %s", jtag_event_strings
[event
]);
431 callback
->callback(event
, callback
->priv
);
432 callback
= callback
->next
;
438 /* returns a pointer to the pointer of the last command in queue
439 * this may be a pointer to the root pointer (jtag_command_queue)
440 * or to the next member of the last but one command
442 jtag_command_t
** jtag_get_last_command_p(void)
444 /* jtag_command_t *cmd = jtag_command_queue;
450 return &jtag_command_queue;
454 return last_comand_pointer
;
457 void* cmd_queue_alloc(size_t size
)
459 cmd_queue_page_t
**p_page
= &cmd_queue_pages
;
465 * We align/round the *SIZE* per below
466 * so that all pointers returned by
467 * this function are reasonably well
470 * If we did not, then an "odd-length" request would cause the
471 * *next* allocation to be at an *odd* address, and because
472 * this function has the same type of api as malloc() - we
473 * must also return pointers that have the same type of
476 * What I do not/have is a reasonable portable means
479 * The solution here, is based on these suggestions.
480 * http://gcc.gnu.org/ml/gcc-help/2008-12/msg00041.html
483 union worse_case_align
{
489 #define ALIGN_SIZE (sizeof(union worse_case_align))
491 /* The alignment process. */
492 size
= (size
+ ALIGN_SIZE
-1) & (~(ALIGN_SIZE
-1));
497 while ((*p_page
)->next
)
498 p_page
= &((*p_page
)->next
);
499 if (CMD_QUEUE_PAGE_SIZE
- (*p_page
)->used
< size
)
500 p_page
= &((*p_page
)->next
);
505 *p_page
= malloc(sizeof(cmd_queue_page_t
));
507 (*p_page
)->address
= malloc(CMD_QUEUE_PAGE_SIZE
);
508 (*p_page
)->next
= NULL
;
511 offset
= (*p_page
)->used
;
512 (*p_page
)->used
+= size
;
514 t
=(u8
*)((*p_page
)->address
);
518 void cmd_queue_free(void)
520 cmd_queue_page_t
*page
= cmd_queue_pages
;
524 cmd_queue_page_t
*last
= page
;
530 cmd_queue_pages
= NULL
;
533 static void jtag_prelude1(void)
537 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
538 jtag_error
=ERROR_JTAG_TRST_ASSERTED
;
542 if (cmd_queue_end_state
== TAP_RESET
)
543 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
546 static void jtag_prelude(enum tap_state state
)
551 jtag_add_end_state(state
);
553 cmd_queue_cur_state
= cmd_queue_end_state
;
556 void jtag_add_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
562 retval
=interface_jtag_add_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
563 if (retval
!=ERROR_OK
)
567 int MINIDRIVER(interface_jtag_add_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
569 jtag_command_t
**last_cmd
;
577 last_cmd
= jtag_get_last_command_p();
579 /* allocate memory for a new list member */
580 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
581 (*last_cmd
)->next
= NULL
;
582 last_comand_pointer
= &((*last_cmd
)->next
);
583 (*last_cmd
)->type
= JTAG_SCAN
;
585 /* allocate memory for ir scan command */
586 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
587 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
588 x
= jtag_NumEnabledTaps();
589 (*last_cmd
)->cmd
.scan
->num_fields
= x
; /* one field per device */
590 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(x
* sizeof(scan_field_t
));
591 (*last_cmd
)->cmd
.scan
->end_state
= state
;
598 // do this here so it is not forgotten
599 tap
= jtag_NextEnabledTap(tap
);
604 scan_size
= tap
->ir_length
;
605 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].tap
= tap
;
606 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].num_bits
= scan_size
;
607 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_value
= NULL
;
608 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_handler
= NULL
; /* disable verification by default */
610 /* search the list */
611 for (j
= 0; j
< num_fields
; j
++)
613 if (tap
== fields
[j
].tap
)
616 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
617 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
619 if (jtag_verify_capture_ir
)
621 if (fields
[j
].in_handler
==NULL
)
623 jtag_set_check_value((*last_cmd
)->cmd
.scan
->fields
+nth_tap
, tap
->expected
, tap
->expected_mask
, NULL
);
626 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_handler
= fields
[j
].in_handler
;
627 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_handler_priv
= fields
[j
].in_handler_priv
;
628 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_check_value
= tap
->expected
;
629 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].in_check_mask
= tap
->expected_mask
;
640 /* if a tap isn't listed, set it to BYPASS */
641 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_value
= buf_set_ones(cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
642 (*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_mask
= NULL
;
646 /* update device information */
647 buf_cpy((*last_cmd
)->cmd
.scan
->fields
[nth_tap
].out_value
, tap
->cur_instr
, scan_size
);
653 void jtag_add_plain_ir_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
659 retval
=interface_jtag_add_plain_ir_scan(num_fields
, fields
, cmd_queue_end_state
);
660 if (retval
!=ERROR_OK
)
664 int MINIDRIVER(interface_jtag_add_plain_ir_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
667 jtag_command_t
**last_cmd
;
669 last_cmd
= jtag_get_last_command_p();
671 /* allocate memory for a new list member */
672 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
673 (*last_cmd
)->next
= NULL
;
674 last_comand_pointer
= &((*last_cmd
)->next
);
675 (*last_cmd
)->type
= JTAG_SCAN
;
677 /* allocate memory for ir scan command */
678 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
679 (*last_cmd
)->cmd
.scan
->ir_scan
= 1;
680 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
681 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
682 (*last_cmd
)->cmd
.scan
->end_state
= state
;
684 for( i
= 0 ; i
< num_fields
; i
++ ){
685 int num_bits
= fields
[i
].num_bits
;
686 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
687 (*last_cmd
)->cmd
.scan
->fields
[i
].tap
= fields
[i
].tap
;
688 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
689 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
690 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
691 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
692 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
693 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
694 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= NULL
;
695 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= NULL
;
700 void jtag_add_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
706 retval
=interface_jtag_add_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
707 if (retval
!=ERROR_OK
)
711 int MINIDRIVER(interface_jtag_add_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
715 int bypass_devices
= 0;
719 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
722 /* count devices in bypass */
726 tap
= jtag_NextEnabledTap(tap
);
735 /* allocate memory for a new list member */
736 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
737 last_comand_pointer
= &((*last_cmd
)->next
);
738 (*last_cmd
)->next
= NULL
;
739 (*last_cmd
)->type
= JTAG_SCAN
;
741 /* allocate memory for dr scan command */
742 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
743 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
744 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
745 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
746 (*last_cmd
)->cmd
.scan
->end_state
= state
;
752 tap
= jtag_NextEnabledTap(tap
);
757 (*last_cmd
)->cmd
.scan
->fields
[field_count
].tap
= tap
;
759 for (j
= 0; j
< num_fields
; j
++)
761 if (tap
== fields
[j
].tap
)
764 scan_size
= fields
[j
].num_bits
;
765 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
766 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(fields
[j
].out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
767 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= buf_cpy(fields
[j
].out_mask
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
768 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= fields
[j
].in_value
;
769 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= fields
[j
].in_check_value
;
770 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= fields
[j
].in_check_mask
;
771 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= fields
[j
].in_handler
;
772 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= fields
[j
].in_handler_priv
;
777 #ifdef _DEBUG_JTAG_IO_
778 /* if a device isn't listed, the BYPASS register should be selected */
781 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
785 /* program the scan field to 1 bit length, and ignore it's value */
786 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
787 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
788 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
789 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
790 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
791 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
792 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
793 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
797 #ifdef _DEBUG_JTAG_IO_
798 /* if a device is listed, the BYPASS register must not be selected */
801 LOG_ERROR("BUG: scan data for a device in BYPASS");
810 void MINIDRIVER(interface_jtag_add_dr_out
)(jtag_tap_t
*target_tap
,
814 enum tap_state end_state
)
819 int bypass_devices
= 0;
821 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
824 /* count devices in bypass */
828 tap
= jtag_NextEnabledTap(tap
);
837 /* allocate memory for a new list member */
838 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
839 last_comand_pointer
= &((*last_cmd
)->next
);
840 (*last_cmd
)->next
= NULL
;
841 (*last_cmd
)->type
= JTAG_SCAN
;
843 /* allocate memory for dr scan command */
844 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
845 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
846 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
+ bypass_devices
;
847 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc((num_fields
+ bypass_devices
) * sizeof(scan_field_t
));
848 (*last_cmd
)->cmd
.scan
->end_state
= end_state
;
853 tap
= jtag_NextEnabledTap(tap
);
858 (*last_cmd
)->cmd
.scan
->fields
[field_count
].tap
= tap
;
860 if (tap
== target_tap
)
863 #ifdef _DEBUG_JTAG_IO_
864 /* if a device is listed, the BYPASS register must not be selected */
867 LOG_ERROR("BUG: scan data for a device in BYPASS");
871 for (j
= 0; j
< num_fields
; j
++)
874 scan_size
= num_bits
[j
];
875 buf_set_u32(out_value
, 0, scan_size
, value
[j
]);
876 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= scan_size
;
877 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= buf_cpy(out_value
, cmd_queue_alloc(CEIL(scan_size
, 8)), scan_size
);
878 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
879 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
880 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
881 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
882 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
883 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
887 #ifdef _DEBUG_JTAG_IO_
888 /* if a device isn't listed, the BYPASS register should be selected */
891 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
895 /* program the scan field to 1 bit length, and ignore it's value */
896 (*last_cmd
)->cmd
.scan
->fields
[field_count
].num_bits
= 1;
897 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_value
= NULL
;
898 (*last_cmd
)->cmd
.scan
->fields
[field_count
].out_mask
= NULL
;
899 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_value
= NULL
;
900 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_value
= NULL
;
901 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_check_mask
= NULL
;
902 (*last_cmd
)->cmd
.scan
->fields
[field_count
].in_handler
= NULL
;
903 (*last_cmd
)->cmd
.scan
->fields
[field_count
++].in_handler_priv
= NULL
;
908 void jtag_add_plain_dr_scan(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
914 retval
=interface_jtag_add_plain_dr_scan(num_fields
, fields
, cmd_queue_end_state
);
915 if (retval
!=ERROR_OK
)
919 int MINIDRIVER(interface_jtag_add_plain_dr_scan
)(int num_fields
, scan_field_t
*fields
, enum tap_state state
)
922 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
924 /* allocate memory for a new list member */
925 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
926 last_comand_pointer
= &((*last_cmd
)->next
);
927 (*last_cmd
)->next
= NULL
;
928 (*last_cmd
)->type
= JTAG_SCAN
;
930 /* allocate memory for scan command */
931 (*last_cmd
)->cmd
.scan
= cmd_queue_alloc(sizeof(scan_command_t
));
932 (*last_cmd
)->cmd
.scan
->ir_scan
= 0;
933 (*last_cmd
)->cmd
.scan
->num_fields
= num_fields
;
934 (*last_cmd
)->cmd
.scan
->fields
= cmd_queue_alloc(num_fields
* sizeof(scan_field_t
));
935 (*last_cmd
)->cmd
.scan
->end_state
= state
;
937 for (i
= 0; i
< num_fields
; i
++)
939 int num_bits
= fields
[i
].num_bits
;
940 int num_bytes
= CEIL(fields
[i
].num_bits
, 8);
941 (*last_cmd
)->cmd
.scan
->fields
[i
].tap
= fields
[i
].tap
;
942 (*last_cmd
)->cmd
.scan
->fields
[i
].num_bits
= num_bits
;
943 (*last_cmd
)->cmd
.scan
->fields
[i
].out_value
= buf_cpy(fields
[i
].out_value
, cmd_queue_alloc(num_bytes
), num_bits
);
944 (*last_cmd
)->cmd
.scan
->fields
[i
].out_mask
= buf_cpy(fields
[i
].out_mask
, cmd_queue_alloc(num_bytes
), num_bits
);
945 (*last_cmd
)->cmd
.scan
->fields
[i
].in_value
= fields
[i
].in_value
;
946 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_value
= fields
[i
].in_check_value
;
947 (*last_cmd
)->cmd
.scan
->fields
[i
].in_check_mask
= fields
[i
].in_check_mask
;
948 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler
= fields
[i
].in_handler
;
949 (*last_cmd
)->cmd
.scan
->fields
[i
].in_handler_priv
= fields
[i
].in_handler_priv
;
955 void jtag_add_tlr(void)
957 jtag_prelude(TAP_RESET
);
960 retval
=interface_jtag_add_tlr();
961 if (retval
!=ERROR_OK
)
965 int MINIDRIVER(interface_jtag_add_tlr
)()
967 enum tap_state state
= TAP_RESET
;
968 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
970 /* allocate memory for a new list member */
971 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
972 last_comand_pointer
= &((*last_cmd
)->next
);
973 (*last_cmd
)->next
= NULL
;
974 (*last_cmd
)->type
= JTAG_STATEMOVE
;
976 (*last_cmd
)->cmd
.statemove
= cmd_queue_alloc(sizeof(statemove_command_t
));
977 (*last_cmd
)->cmd
.statemove
->end_state
= state
;
983 void jtag_add_pathmove(int num_states
, enum tap_state
*path
)
985 enum tap_state cur_state
=cmd_queue_cur_state
;
989 /* the last state has to be a stable state */
990 if (tap_move_map
[path
[num_states
- 1]] == -1)
992 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
996 for (i
=0; i
<num_states
; i
++)
998 if (path
[i
] == TAP_RESET
)
1000 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
1003 if ((tap_transitions
[cur_state
].low
!= path
[i
])&&
1004 (tap_transitions
[cur_state
].high
!= path
[i
]))
1006 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", jtag_state_name(cur_state
), jtag_state_name(path
[i
]));
1009 cur_state
= path
[i
];
1014 retval
=interface_jtag_add_pathmove(num_states
, path
);
1015 cmd_queue_cur_state
= path
[num_states
- 1];
1016 if (retval
!=ERROR_OK
)
1020 int MINIDRIVER(interface_jtag_add_pathmove
)(int num_states
, enum tap_state
*path
)
1022 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1025 /* allocate memory for a new list member */
1026 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1027 last_comand_pointer
= &((*last_cmd
)->next
);
1028 (*last_cmd
)->next
= NULL
;
1029 (*last_cmd
)->type
= JTAG_PATHMOVE
;
1031 (*last_cmd
)->cmd
.pathmove
= cmd_queue_alloc(sizeof(pathmove_command_t
));
1032 (*last_cmd
)->cmd
.pathmove
->num_states
= num_states
;
1033 (*last_cmd
)->cmd
.pathmove
->path
= cmd_queue_alloc(sizeof(enum tap_state
) * num_states
);
1035 for (i
= 0; i
< num_states
; i
++)
1036 (*last_cmd
)->cmd
.pathmove
->path
[i
] = path
[i
];
1041 int MINIDRIVER(interface_jtag_add_runtest
)(int num_cycles
, enum tap_state state
)
1043 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1045 /* allocate memory for a new list member */
1046 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1047 (*last_cmd
)->next
= NULL
;
1048 last_comand_pointer
= &((*last_cmd
)->next
);
1049 (*last_cmd
)->type
= JTAG_RUNTEST
;
1051 (*last_cmd
)->cmd
.runtest
= cmd_queue_alloc(sizeof(runtest_command_t
));
1052 (*last_cmd
)->cmd
.runtest
->num_cycles
= num_cycles
;
1053 (*last_cmd
)->cmd
.runtest
->end_state
= state
;
1058 void jtag_add_runtest(int num_cycles
, enum tap_state state
)
1062 jtag_prelude(state
);
1064 /* executed by sw or hw fifo */
1065 retval
=interface_jtag_add_runtest(num_cycles
, cmd_queue_end_state
);
1066 if (retval
!=ERROR_OK
)
1070 void jtag_add_reset(int req_tlr_or_trst
, int req_srst
)
1072 int trst_with_tlr
= 0;
1075 /* FIX!!! there are *many* different cases here. A better
1076 * approach is needed for legal combinations of transitions...
1078 if ((jtag_reset_config
& RESET_HAS_SRST
)&&
1079 (jtag_reset_config
& RESET_HAS_TRST
)&&
1080 ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0))
1082 if (((req_tlr_or_trst
&&!jtag_trst
)||
1083 (!req_tlr_or_trst
&&jtag_trst
))&&
1084 ((req_srst
&&!jtag_srst
)||
1085 (!req_srst
&&jtag_srst
)))
1087 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
1088 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
1092 /* Make sure that jtag_reset_config allows the requested reset */
1093 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
1094 if (((jtag_reset_config
& RESET_SRST_PULLS_TRST
) && (req_srst
== 1)) && (!req_tlr_or_trst
))
1096 LOG_ERROR("BUG: requested reset would assert trst");
1097 jtag_error
=ERROR_FAIL
;
1101 /* if TRST pulls SRST, we reset with TAP T-L-R */
1102 if (((jtag_reset_config
& RESET_TRST_PULLS_SRST
) && (req_tlr_or_trst
)) && (req_srst
== 0))
1107 if (req_srst
&& !(jtag_reset_config
& RESET_HAS_SRST
))
1109 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
1110 jtag_error
=ERROR_FAIL
;
1114 if (req_tlr_or_trst
)
1116 if (!trst_with_tlr
&& (jtag_reset_config
& RESET_HAS_TRST
))
1128 jtag_srst
= req_srst
;
1130 retval
= interface_jtag_add_reset(jtag_trst
, jtag_srst
);
1131 if (retval
!=ERROR_OK
)
1139 LOG_DEBUG("SRST line asserted");
1143 LOG_DEBUG("SRST line released");
1144 if (jtag_nsrst_delay
)
1145 jtag_add_sleep(jtag_nsrst_delay
* 1000);
1150 LOG_DEBUG("JTAG reset with RESET instead of TRST");
1151 jtag_add_end_state(TAP_RESET
);
1153 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
1159 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1160 * and inform possible listeners about this
1162 LOG_DEBUG("TRST line asserted");
1163 cmd_queue_cur_state
= TAP_RESET
;
1164 jtag_call_event_callbacks(JTAG_TRST_ASSERTED
);
1168 if (jtag_ntrst_delay
)
1169 jtag_add_sleep(jtag_ntrst_delay
* 1000);
1173 int MINIDRIVER(interface_jtag_add_reset
)(int req_trst
, int req_srst
)
1175 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1177 /* allocate memory for a new list member */
1178 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1179 (*last_cmd
)->next
= NULL
;
1180 last_comand_pointer
= &((*last_cmd
)->next
);
1181 (*last_cmd
)->type
= JTAG_RESET
;
1183 (*last_cmd
)->cmd
.reset
= cmd_queue_alloc(sizeof(reset_command_t
));
1184 (*last_cmd
)->cmd
.reset
->trst
= req_trst
;
1185 (*last_cmd
)->cmd
.reset
->srst
= req_srst
;
1190 void jtag_add_end_state(enum tap_state state
)
1192 cmd_queue_end_state
= state
;
1193 if ((cmd_queue_end_state
== TAP_DRSHIFT
)||(cmd_queue_end_state
== TAP_IRSHIFT
))
1195 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
1199 int MINIDRIVER(interface_jtag_add_sleep
)(u32 us
)
1201 jtag_command_t
**last_cmd
= jtag_get_last_command_p();
1203 /* allocate memory for a new list member */
1204 *last_cmd
= cmd_queue_alloc(sizeof(jtag_command_t
));
1205 (*last_cmd
)->next
= NULL
;
1206 last_comand_pointer
= &((*last_cmd
)->next
);
1207 (*last_cmd
)->type
= JTAG_SLEEP
;
1209 (*last_cmd
)->cmd
.sleep
= cmd_queue_alloc(sizeof(sleep_command_t
));
1210 (*last_cmd
)->cmd
.sleep
->us
= us
;
1215 void jtag_add_sleep(u32 us
)
1217 keep_alive(); /* we might be running on a very slow JTAG clk */
1218 int retval
=interface_jtag_add_sleep(us
);
1219 if (retval
!=ERROR_OK
)
1224 int jtag_scan_size(scan_command_t
*cmd
)
1229 /* count bits in scan command */
1230 for (i
= 0; i
< cmd
->num_fields
; i
++)
1232 bit_count
+= cmd
->fields
[i
].num_bits
;
1238 int jtag_build_buffer(scan_command_t
*cmd
, u8
**buffer
)
1243 bit_count
= jtag_scan_size(cmd
);
1244 *buffer
= malloc(CEIL(bit_count
, 8));
1248 for (i
= 0; i
< cmd
->num_fields
; i
++)
1250 if (cmd
->fields
[i
].out_value
)
1252 #ifdef _DEBUG_JTAG_IO_
1253 char* char_buf
= buf_to_str(cmd
->fields
[i
].out_value
, (cmd
->fields
[i
].num_bits
> 64) ? 64 : cmd
->fields
[i
].num_bits
, 16);
1255 buf_set_buf(cmd
->fields
[i
].out_value
, 0, *buffer
, bit_count
, cmd
->fields
[i
].num_bits
);
1256 #ifdef _DEBUG_JTAG_IO_
1257 LOG_DEBUG("fields[%i].out_value: 0x%s", i
, char_buf
);
1262 bit_count
+= cmd
->fields
[i
].num_bits
;
1268 int jtag_read_buffer(u8
*buffer
, scan_command_t
*cmd
)
1274 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1277 for (i
= 0; i
< cmd
->num_fields
; i
++)
1279 /* if neither in_value nor in_handler
1280 * are specified we don't have to examine this field
1282 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1284 int num_bits
= cmd
->fields
[i
].num_bits
;
1285 u8
*captured
= buf_set_buf(buffer
, bit_count
, malloc(CEIL(num_bits
, 8)), 0, num_bits
);
1287 #ifdef _DEBUG_JTAG_IO_
1290 char_buf
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1291 LOG_DEBUG("fields[%i].in_value: 0x%s", i
, char_buf
);
1295 if (cmd
->fields
[i
].in_value
)
1297 buf_cpy(captured
, cmd
->fields
[i
].in_value
, num_bits
);
1299 if (cmd
->fields
[i
].in_handler
)
1301 if (cmd
->fields
[i
].in_handler(cmd
->fields
[i
].in_value
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1303 LOG_WARNING("in_handler: with \"in_value\", mismatch in %s", cmd
->ir_scan
? "SIR" : "SDR" );
1304 retval
= ERROR_JTAG_QUEUE_FAILED
;
1309 /* no in_value specified, but a handler takes care of the scanned data */
1310 if (cmd
->fields
[i
].in_handler
&& (!cmd
->fields
[i
].in_value
))
1312 if (cmd
->fields
[i
].in_handler(captured
, cmd
->fields
[i
].in_handler_priv
, cmd
->fields
+i
) != ERROR_OK
)
1314 /* We're going to call the error:handler later, but if the in_handler
1315 * reported an error we report this failure upstream
1317 LOG_WARNING("in_handler: w/o \"in_value\", mismatch in %s", cmd
->ir_scan
? "SIR" : "SDR" );
1318 retval
= ERROR_JTAG_QUEUE_FAILED
;
1324 bit_count
+= cmd
->fields
[i
].num_bits
;
1330 int jtag_check_value(u8
*captured
, void *priv
, scan_field_t
*field
)
1332 int retval
= ERROR_OK
;
1333 int num_bits
= field
->num_bits
;
1335 int compare_failed
= 0;
1337 if (field
->in_check_mask
)
1338 compare_failed
= buf_cmp_mask(captured
, field
->in_check_value
, field
->in_check_mask
, num_bits
);
1340 compare_failed
= buf_cmp(captured
, field
->in_check_value
, num_bits
);
1342 if (compare_failed
){
1343 /* An error handler could have caught the failing check
1344 * only report a problem when there wasn't a handler, or if the handler
1345 * acknowledged the error
1347 LOG_WARNING("TAP %s:",
1348 (field
->tap
== NULL
) ? "(unknown)" : field
->tap
->dotted_name
);
1351 char *captured_char
= buf_to_str(captured
, (num_bits
> 64) ? 64 : num_bits
, 16);
1352 char *in_check_value_char
= buf_to_str(field
->in_check_value
, (num_bits
> 64) ? 64 : num_bits
, 16);
1354 if (field
->in_check_mask
)
1356 char *in_check_mask_char
;
1357 in_check_mask_char
= buf_to_str(field
->in_check_mask
, (num_bits
> 64) ? 64 : num_bits
, 16);
1358 LOG_WARNING("value captured during scan didn't pass the requested check:");
1359 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
1360 captured_char
, in_check_value_char
, in_check_mask_char
);
1361 free(in_check_mask_char
);
1365 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
);
1368 free(captured_char
);
1369 free(in_check_value_char
);
1371 retval
= ERROR_JTAG_QUEUE_FAILED
;
1379 set up checking of this field using the in_handler. The values passed in must be valid until
1380 after jtag_execute() has completed.
1382 void jtag_set_check_value(scan_field_t
*field
, u8
*value
, u8
*mask
, error_handler_t
*in_error_handler
)
1385 field
->in_handler
= jtag_check_value
;
1387 field
->in_handler
= NULL
; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1388 field
->in_handler_priv
= NULL
;
1389 field
->in_check_value
= value
;
1390 field
->in_check_mask
= mask
;
1393 enum scan_type
jtag_scan_type(scan_command_t
*cmd
)
1398 for (i
= 0; i
< cmd
->num_fields
; i
++)
1400 if (cmd
->fields
[i
].in_value
|| cmd
->fields
[i
].in_handler
)
1402 if (cmd
->fields
[i
].out_value
)
1409 int MINIDRIVER(interface_jtag_execute_queue
)(void)
1415 LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
1419 retval
= jtag
->execute_queue();
1423 jtag_command_queue
= NULL
;
1424 last_comand_pointer
= &jtag_command_queue
;
1429 int jtag_execute_queue(void)
1431 int retval
=interface_jtag_execute_queue();
1432 if (retval
==ERROR_OK
)
1436 jtag_error
=ERROR_OK
;
1440 int jtag_reset_callback(enum jtag_event event
, void *priv
)
1442 jtag_tap_t
*tap
= priv
;
1446 if (event
== JTAG_TRST_ASSERTED
)
1448 buf_set_ones(tap
->cur_instr
, tap
->ir_length
);
1455 void jtag_sleep(u32 us
)
1457 alive_sleep(us
/1000);
1460 /* Try to examine chain layout according to IEEE 1149.1 §12
1462 int jtag_examine_chain(void)
1466 u8 idcode_buffer
[JTAG_MAX_CHAIN_SIZE
* 4];
1469 int device_count
= 0;
1470 u8 zero_check
= 0x0;
1471 u8 one_check
= 0xff;
1474 field
.num_bits
= sizeof(idcode_buffer
) * 8;
1475 field
.out_value
= idcode_buffer
;
1476 field
.out_mask
= NULL
;
1477 field
.in_value
= idcode_buffer
;
1478 field
.in_check_value
= NULL
;
1479 field
.in_check_mask
= NULL
;
1480 field
.in_handler
= NULL
;
1481 field
.in_handler_priv
= NULL
;
1483 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
; i
++)
1485 buf_set_u32(idcode_buffer
, i
* 32, 32, 0x000000FF);
1488 jtag_add_plain_dr_scan(1, &field
, TAP_RESET
);
1489 jtag_execute_queue();
1491 for (i
= 0; i
< JTAG_MAX_CHAIN_SIZE
* 4; i
++)
1493 zero_check
|= idcode_buffer
[i
];
1494 one_check
&= idcode_buffer
[i
];
1497 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1498 if ((zero_check
== 0x00) || (one_check
== 0xff))
1500 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1501 return ERROR_JTAG_INIT_FAILED
;
1504 // point at the 1st tap
1505 tap
= jtag_NextEnabledTap(NULL
);
1507 LOG_ERROR("JTAG: No taps enabled?");
1508 return ERROR_JTAG_INIT_FAILED
;
1511 for (bit_count
= 0; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;)
1513 u32 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1514 if ((idcode
& 1) == 0)
1516 /* LSB must not be 0, this indicates a device in bypass */
1517 LOG_WARNING("Tap/Device does not have IDCODE");
1528 if (idcode
== 0x000000FF)
1531 /* End of chain (invalid manufacturer ID)
1533 * The JTAG examine is the very first thing that happens
1535 * A single JTAG device requires only 64 bits to be read back correctly.
1537 * The code below adds a check that the rest of the data scanned (640 bits)
1538 * are all as expected. This helps diagnose/catch problems with the JTAG chain
1540 * earlier and gives more helpful/explicit error messages.
1542 for (bit_count
+= 32; bit_count
< (JTAG_MAX_CHAIN_SIZE
* 32) - 31;bit_count
+= 32)
1544 idcode
= buf_get_u32(idcode_buffer
, bit_count
, 32);
1545 if (unexpected
||(idcode
!= 0x000000FF))
1547 LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count
, idcode
);
1555 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
1556 manufacturer
= EXTRACT_MFG(idcode
);
1557 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
1558 part
= EXTRACT_PART(idcode
);
1559 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
1560 version
= EXTRACT_VER(idcode
);
1562 LOG_INFO("JTAG tap: %s tap/device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1563 ((tap
!= NULL
) ? (tap
->dotted_name
) : "(not-named)"),
1564 idcode
, manufacturer
, part
, version
);
1570 tap
->idcode
= idcode
;
1572 if (tap
->expected_ids_cnt
> 0) {
1573 /* Loop over the expected identification codes and test for a match */
1575 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++) {
1576 if( tap
->idcode
== tap
->expected_ids
[ii
] ){
1581 /* If none of the expected ids matched, log an error */
1582 if (ii
== tap
->expected_ids_cnt
) {
1583 LOG_ERROR("JTAG tap: %s got: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1586 EXTRACT_MFG( tap
->idcode
),
1587 EXTRACT_PART( tap
->idcode
),
1588 EXTRACT_VER( tap
->idcode
) );
1589 for (ii
= 0; ii
< tap
->expected_ids_cnt
; ii
++) {
1590 LOG_ERROR("JTAG tap: %s expected %hhu of %hhu: 0x%08x (mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
1593 tap
->expected_ids_cnt
,
1594 tap
->expected_ids
[ii
],
1595 EXTRACT_MFG( tap
->expected_ids
[ii
] ),
1596 EXTRACT_PART( tap
->expected_ids
[ii
] ),
1597 EXTRACT_VER( tap
->expected_ids
[ii
] ) );
1600 return ERROR_JTAG_INIT_FAILED
;
1602 LOG_INFO("JTAG Tap/device matched");
1606 LOG_INFO("JTAG TAP ID: 0x%08x - Unknown - please report (A) chipname and (B) idcode to the openocd project",
1610 tap
= jtag_NextEnabledTap(tap
);
1615 /* see if number of discovered devices matches configuration */
1616 if (device_count
!= jtag_NumEnabledTaps())
1618 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match (enabled) configuration (%i), total taps: %d",
1619 device_count
, jtag_NumEnabledTaps(), jtag_NumTotalTaps());
1620 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1621 return ERROR_JTAG_INIT_FAILED
;
1627 int jtag_validate_chain(void)
1630 int total_ir_length
= 0;
1636 total_ir_length
= 0;
1638 tap
= jtag_NextEnabledTap(tap
);
1642 total_ir_length
+= tap
->ir_length
;
1645 total_ir_length
+= 2;
1646 ir_test
= malloc(CEIL(total_ir_length
, 8));
1647 buf_set_ones(ir_test
, total_ir_length
);
1650 field
.num_bits
= total_ir_length
;
1651 field
.out_value
= ir_test
;
1652 field
.out_mask
= NULL
;
1653 field
.in_value
= ir_test
;
1654 field
.in_check_value
= NULL
;
1655 field
.in_check_mask
= NULL
;
1656 field
.in_handler
= NULL
;
1657 field
.in_handler_priv
= NULL
;
1659 jtag_add_plain_ir_scan(1, &field
, TAP_RESET
);
1660 jtag_execute_queue();
1665 tap
= jtag_NextEnabledTap(tap
);
1671 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x1)
1673 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1674 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1677 return ERROR_JTAG_INIT_FAILED
;
1679 chain_pos
+= tap
->ir_length
;
1682 if (buf_get_u32(ir_test
, chain_pos
, 2) != 0x3)
1684 char *cbuf
= buf_to_str(ir_test
, total_ir_length
, 16);
1685 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf
);
1688 return ERROR_JTAG_INIT_FAILED
;
1696 enum jtag_tap_cfg_param
{
1700 static Jim_Nvp nvp_config_opts
[] = {
1701 { .name
= "-event", .value
= JCFG_EVENT
},
1703 { .name
= NULL
, .value
= -1 }
1707 jtag_tap_configure_cmd( Jim_GetOptInfo
*goi
,
1714 /* parse config or cget options */
1715 while (goi
->argc
> 0) {
1716 Jim_SetEmptyResult (goi
->interp
);
1718 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
1720 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
1726 if (goi
->argc
== 0) {
1727 Jim_WrongNumArgs( goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ..." );
1731 e
= Jim_GetOpt_Nvp( goi
, nvp_jtag_tap_event
, &n
);
1733 Jim_GetOpt_NvpUnknown(goi
, nvp_jtag_tap_event
, 1);
1737 if (goi
->isconfigure
) {
1738 if (goi
->argc
!= 1) {
1739 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
1743 if (goi
->argc
!= 0) {
1744 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
1750 jtag_tap_event_action_t
*jteap
;
1752 jteap
= tap
->event_action
;
1753 /* replace existing? */
1755 if (jteap
->event
== n
->value
) {
1758 jteap
= jteap
->next
;
1761 if (goi
->isconfigure
) {
1762 if (jteap
== NULL
) {
1764 jteap
= calloc(1, sizeof (*jteap
));
1766 jteap
->event
= n
->value
;
1767 Jim_GetOpt_Obj( goi
, &o
);
1769 Jim_DecrRefCount(interp
, jteap
->body
);
1771 jteap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
1772 Jim_IncrRefCount(jteap
->body
);
1774 /* add to head of event list */
1775 jteap
->next
= tap
->event_action
;
1776 tap
->event_action
= jteap
;
1777 Jim_SetEmptyResult(goi
->interp
);
1780 if (jteap
== NULL
) {
1781 Jim_SetEmptyResult(goi
->interp
);
1783 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, jteap
->body
));
1790 } /* while (goi->argc) */
1795 static int jim_newtap_cmd( Jim_GetOptInfo
*goi
)
1805 const Jim_Nvp opts
[] = {
1806 #define NTAP_OPT_IRLEN 0
1807 { .name
= "-irlen" , .value
= NTAP_OPT_IRLEN
},
1808 #define NTAP_OPT_IRMASK 1
1809 { .name
= "-irmask" , .value
= NTAP_OPT_IRMASK
},
1810 #define NTAP_OPT_IRCAPTURE 2
1811 { .name
= "-ircapture" , .value
= NTAP_OPT_IRCAPTURE
},
1812 #define NTAP_OPT_ENABLED 3
1813 { .name
= "-enable" , .value
= NTAP_OPT_ENABLED
},
1814 #define NTAP_OPT_DISABLED 4
1815 { .name
= "-disable" , .value
= NTAP_OPT_DISABLED
},
1816 #define NTAP_OPT_EXPECTED_ID 5
1817 { .name
= "-expected-id" , .value
= NTAP_OPT_EXPECTED_ID
},
1818 { .name
= NULL
, .value
= -1 },
1821 pTap
= malloc( sizeof(jtag_tap_t
) );
1822 memset( pTap
, 0, sizeof(*pTap
) );
1824 Jim_SetResult_sprintf( goi
->interp
, "no memory");
1828 * we expect CHIP + TAP + OPTIONS
1830 if( goi
->argc
< 3 ){
1831 Jim_SetResult_sprintf(goi
->interp
, "Missing CHIP TAP OPTIONS ....");
1834 Jim_GetOpt_String( goi
, &cp
, NULL
);
1835 pTap
->chip
= strdup(cp
);
1837 Jim_GetOpt_String( goi
, &cp
, NULL
);
1838 pTap
->tapname
= strdup(cp
);
1840 /* name + dot + name + null */
1841 x
= strlen(pTap
->chip
) + 1 + strlen(pTap
->tapname
) + 1;
1843 sprintf( cp
, "%s.%s", pTap
->chip
, pTap
->tapname
);
1844 pTap
->dotted_name
= cp
;
1846 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
1847 pTap
->chip
, pTap
->tapname
, pTap
->dotted_name
, goi
->argc
);
1849 /* default is enabled */
1852 /* deal with options */
1853 #define NTREQ_IRLEN 1
1854 #define NTREQ_IRCAPTURE 2
1855 #define NTREQ_IRMASK 4
1857 /* clear them as we find them */
1858 reqbits
= (NTREQ_IRLEN
| NTREQ_IRCAPTURE
| NTREQ_IRMASK
);
1861 e
= Jim_GetOpt_Nvp( goi
, opts
, &n
);
1863 Jim_GetOpt_NvpUnknown( goi
, opts
, 0 );
1866 LOG_DEBUG("Processing option: %s", n
->name
);
1868 case NTAP_OPT_ENABLED
:
1871 case NTAP_OPT_DISABLED
:
1874 case NTAP_OPT_EXPECTED_ID
:
1876 u32
*new_expected_ids
;
1878 e
= Jim_GetOpt_Wide( goi
, &w
);
1880 Jim_SetResult_sprintf(goi
->interp
, "option: %s bad parameter", n
->name
);
1884 new_expected_ids
= malloc(sizeof(u32
) * (pTap
->expected_ids_cnt
+ 1));
1885 if (new_expected_ids
== NULL
) {
1886 Jim_SetResult_sprintf( goi
->interp
, "no memory");
1890 memcpy(new_expected_ids
, pTap
->expected_ids
, sizeof(u32
) * pTap
->expected_ids_cnt
);
1892 new_expected_ids
[pTap
->expected_ids_cnt
] = w
;
1894 free(pTap
->expected_ids
);
1895 pTap
->expected_ids
= new_expected_ids
;
1896 pTap
->expected_ids_cnt
++;
1899 case NTAP_OPT_IRLEN
:
1900 case NTAP_OPT_IRMASK
:
1901 case NTAP_OPT_IRCAPTURE
:
1902 e
= Jim_GetOpt_Wide( goi
, &w
);
1904 Jim_SetResult_sprintf( goi
->interp
, "option: %s bad parameter", n
->name
);
1907 if( (w
< 0) || (w
> 0xffff) ){
1909 Jim_SetResult_sprintf( goi
->interp
, "option: %s - wacky value: %d (0x%x)",
1910 n
->name
, (int)(w
), (int)(w
));
1914 case NTAP_OPT_IRLEN
:
1915 pTap
->ir_length
= w
;
1916 reqbits
&= (~(NTREQ_IRLEN
));
1918 case NTAP_OPT_IRMASK
:
1919 pTap
->ir_capture_mask
= w
;
1920 reqbits
&= (~(NTREQ_IRMASK
));
1922 case NTAP_OPT_IRCAPTURE
:
1923 pTap
->ir_capture_value
= w
;
1924 reqbits
&= (~(NTREQ_IRCAPTURE
));
1927 } /* switch(n->value) */
1928 } /* while( goi->argc ) */
1930 /* Did we get all the options? */
1933 Jim_SetResult_sprintf( goi
->interp
,
1934 "newtap: %s missing required parameters",
1936 /* TODO: Tell user what is missing :-( */
1937 /* no memory leaks pelase */
1938 free(((void *)(pTap
->expected_ids
)));
1939 free(((void *)(pTap
->chip
)));
1940 free(((void *)(pTap
->tapname
)));
1941 free(((void *)(pTap
->dotted_name
)));
1942 free(((void *)(pTap
)));
1946 pTap
->expected
= malloc( pTap
->ir_length
);
1947 pTap
->expected_mask
= malloc( pTap
->ir_length
);
1948 pTap
->cur_instr
= malloc( pTap
->ir_length
);
1950 buf_set_u32( pTap
->expected
,
1953 pTap
->ir_capture_value
);
1954 buf_set_u32( pTap
->expected_mask
,
1957 pTap
->ir_capture_mask
);
1958 buf_set_ones( pTap
->cur_instr
,
1963 jtag_register_event_callback(jtag_reset_callback
, pTap
);
1965 ppTap
= &(jtag_all_taps
);
1966 while( (*ppTap
) != NULL
){
1967 ppTap
= &((*ppTap
)->next_tap
);
1971 static int n_taps
= 0;
1972 pTap
->abs_chain_position
= n_taps
++;
1974 LOG_DEBUG( "Created Tap: %s @ abs position %d, irlen %d, capture: 0x%x mask: 0x%x",
1975 (*ppTap
)->dotted_name
,
1976 (*ppTap
)->abs_chain_position
,
1977 (*ppTap
)->ir_length
,
1978 (*ppTap
)->ir_capture_value
,
1979 (*ppTap
)->ir_capture_mask
);
1984 static int jim_jtag_command( Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1990 struct command_context_s
*context
;
1994 JTAG_CMD_INIT_RESET
,
1997 JTAG_CMD_TAPDISABLE
,
1998 JTAG_CMD_TAPISENABLED
,
2003 const Jim_Nvp jtag_cmds
[] = {
2004 { .name
= "interface" , .value
= JTAG_CMD_INTERFACE
},
2005 { .name
= "arp_init-reset", .value
= JTAG_CMD_INIT_RESET
},
2006 { .name
= "newtap" , .value
= JTAG_CMD_NEWTAP
},
2007 { .name
= "tapisenabled" , .value
= JTAG_CMD_TAPISENABLED
},
2008 { .name
= "tapenable" , .value
= JTAG_CMD_TAPENABLE
},
2009 { .name
= "tapdisable" , .value
= JTAG_CMD_TAPDISABLE
},
2010 { .name
= "configure" , .value
= JTAG_CMD_CONFIGURE
},
2011 { .name
= "cget" , .value
= JTAG_CMD_CGET
},
2013 { .name
= NULL
, .value
= -1 },
2016 context
= Jim_GetAssocData(interp
, "context");
2017 /* go past the command */
2018 Jim_GetOpt_Setup( &goi
, interp
, argc
-1, argv
+1 );
2020 e
= Jim_GetOpt_Nvp( &goi
, jtag_cmds
, &n
);
2022 Jim_GetOpt_NvpUnknown( &goi
, jtag_cmds
, 0 );
2025 Jim_SetEmptyResult( goi
.interp
);
2027 case JTAG_CMD_INTERFACE
:
2028 /* return the name of the interface */
2029 /* TCL code might need to know the exact type... */
2030 /* FUTURE: we allow this as a means to "set" the interface. */
2031 if( goi
.argc
!= 0 ){
2032 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
-1, "(no params)");
2035 Jim_SetResultString( goi
.interp
, jtag_interface
->name
, -1 );
2037 case JTAG_CMD_INIT_RESET
:
2038 if( goi
.argc
!= 0 ){
2039 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
-1, "(no params)");
2042 e
= jtag_init_reset(context
);
2043 if( e
!= ERROR_OK
){
2044 Jim_SetResult_sprintf( goi
.interp
, "error: %d", e
);
2048 case JTAG_CMD_NEWTAP
:
2049 return jim_newtap_cmd( &goi
);
2051 case JTAG_CMD_TAPISENABLED
:
2052 case JTAG_CMD_TAPENABLE
:
2053 case JTAG_CMD_TAPDISABLE
:
2054 if( goi
.argc
!= 1 ){
2055 Jim_SetResultString( goi
.interp
, "Too many parameters",-1 );
2061 t
= jtag_TapByJimObj( goi
.interp
, goi
.argv
[0] );
2066 case JTAG_CMD_TAPISENABLED
:
2069 case JTAG_CMD_TAPENABLE
:
2070 jtag_tap_handle_event( t
, JTAG_TAP_EVENT_ENABLE
);
2074 case JTAG_CMD_TAPDISABLE
:
2075 jtag_tap_handle_event( t
, JTAG_TAP_EVENT_DISABLE
);
2080 Jim_SetResult( goi
.interp
, Jim_NewIntObj( goi
.interp
, e
) );
2087 Jim_WrongNumArgs( goi
.interp
, 0, NULL
, "?tap-name? -option ...");
2094 Jim_GetOpt_Obj(&goi
, &o
);
2095 t
= jtag_TapByJimObj( goi
.interp
, o
);
2100 goi
.isconfigure
= 0;
2101 return jtag_tap_configure_cmd( &goi
, t
);
2105 case JTAG_CMD_CONFIGURE
:
2107 Jim_WrongNumArgs( goi
.interp
, 0, NULL
, "?tap-name? -option ?VALUE? ...");
2114 Jim_GetOpt_Obj(&goi
, &o
);
2115 t
= jtag_TapByJimObj( goi
.interp
, o
);
2120 goi
.isconfigure
= 1;
2121 return jtag_tap_configure_cmd( &goi
, t
);
2128 int jtag_register_commands(struct command_context_s
*cmd_ctx
)
2130 register_jim( cmd_ctx
, "jtag", jim_jtag_command
, "perform jtag tap actions");
2132 register_command(cmd_ctx
, NULL
, "interface", handle_interface_command
,
2133 COMMAND_CONFIG
, "try to configure interface");
2134 register_command(cmd_ctx
, NULL
, "jtag_speed", handle_jtag_speed_command
,
2135 COMMAND_ANY
, "set jtag speed (if supported)");
2136 register_command(cmd_ctx
, NULL
, "jtag_khz", handle_jtag_khz_command
,
2137 COMMAND_ANY
, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
2138 register_command(cmd_ctx
, NULL
, "jtag_device", handle_jtag_device_command
,
2139 COMMAND_CONFIG
, "jtag_device <ir_length> <ir_expected> <ir_mask>");
2140 register_command(cmd_ctx
, NULL
, "reset_config", handle_reset_config_command
,
2141 COMMAND_CONFIG
, NULL
);
2142 register_command(cmd_ctx
, NULL
, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command
,
2143 COMMAND_ANY
, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
2144 register_command(cmd_ctx
, NULL
, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command
,
2145 COMMAND_ANY
, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
2147 register_command(cmd_ctx
, NULL
, "scan_chain", handle_scan_chain_command
,
2148 COMMAND_EXEC
, "print current scan chain configuration");
2150 register_command(cmd_ctx
, NULL
, "endstate", handle_endstate_command
,
2151 COMMAND_EXEC
, "finish JTAG operations in <tap_state>");
2152 register_command(cmd_ctx
, NULL
, "jtag_reset", handle_jtag_reset_command
,
2153 COMMAND_EXEC
, "toggle reset lines <trst> <srst>");
2154 register_command(cmd_ctx
, NULL
, "runtest", handle_runtest_command
,
2155 COMMAND_EXEC
, "move to Run-Test/Idle, and execute <num_cycles>");
2156 register_command(cmd_ctx
, NULL
, "irscan", handle_irscan_command
,
2157 COMMAND_EXEC
, "execute IR scan <device> <instr> [dev2] [instr2] ...");
2158 register_jim(cmd_ctx
, "drscan", Jim_Command_drscan
, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
2160 register_command(cmd_ctx
, NULL
, "verify_ircapture", handle_verify_ircapture_command
,
2161 COMMAND_ANY
, "verify value captured during Capture-IR <enable|disable>");
2165 int jtag_interface_init(struct command_context_s
*cmd_ctx
)
2170 if (!jtag_interface
)
2172 /* nothing was previously specified by "interface" command */
2173 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
2174 return ERROR_JTAG_INVALID_INTERFACE
;
2178 jtag_interface
->khz(speed_khz
, &jtag_speed
);
2182 if (jtag_interface
->init() != ERROR_OK
)
2183 return ERROR_JTAG_INIT_FAILED
;
2185 jtag
= jtag_interface
;
2189 static int jtag_init_inner(struct command_context_s
*cmd_ctx
)
2194 LOG_DEBUG("Init JTAG chain");
2196 tap
= jtag_NextEnabledTap(NULL
);
2198 LOG_ERROR("There are no enabled taps?");
2199 return ERROR_JTAG_INIT_FAILED
;
2203 if ((retval
=jtag_execute_queue())!=ERROR_OK
)
2206 /* examine chain first, as this could discover the real chain layout */
2207 if (jtag_examine_chain() != ERROR_OK
)
2209 LOG_ERROR("trying to validate configured JTAG chain anyway...");
2212 if (jtag_validate_chain() != ERROR_OK
)
2214 LOG_ERROR("Could not validate JTAG chain, continuing anyway...");
2220 int jtag_init_reset(struct command_context_s
*cmd_ctx
)
2224 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
2227 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
2229 /* Reset can happen after a power cycle.
2231 * Ideally we would only assert TRST or run RESET before the target reset.
2233 * However w/srst_pulls_trst, trst is asserted together with the target
2234 * reset whether we want it or not.
2236 * NB! Some targets have JTAG circuitry disabled until a
2237 * trst & srst has been asserted.
2239 * NB! here we assume nsrst/ntrst delay are sufficient!
2241 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
2244 jtag_add_reset(1, 0); /* RESET or TRST */
2245 if (jtag_reset_config
& RESET_HAS_SRST
)
2247 jtag_add_reset(1, 1);
2248 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
)==0)
2249 jtag_add_reset(0, 1);
2251 jtag_add_reset(0, 0);
2252 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
2255 /* Check that we can communication on the JTAG chain + eventually we want to
2256 * be able to perform enumeration only after OpenOCD has started
2257 * telnet and GDB server
2259 * That would allow users to more easily perform any magic they need to before
2262 return jtag_init_inner(cmd_ctx
);
2265 int jtag_init(struct command_context_s
*cmd_ctx
)
2268 if ((retval
=jtag_interface_init(cmd_ctx
)) != ERROR_OK
)
2270 if (jtag_init_inner(cmd_ctx
)==ERROR_OK
)
2274 return jtag_init_reset(cmd_ctx
);
2277 static int default_khz(int khz
, int *jtag_speed
)
2279 LOG_ERROR("Translation from khz to jtag_speed not implemented");
2283 static int default_speed_div(int speed
, int *khz
)
2285 LOG_ERROR("Translation from jtag_speed to khz not implemented");
2289 static int default_power_dropout(int *dropout
)
2291 *dropout
=0; /* by default we can't detect power dropout */
2295 static int default_srst_asserted(int *srst_asserted
)
2297 *srst_asserted
=0; /* by default we can't detect srst asserted */
2301 int handle_interface_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2306 /* check whether the interface is already configured */
2309 LOG_WARNING("Interface already configured, ignoring");
2313 /* interface name is a mandatory argument */
2314 if (argc
< 1 || args
[0][0] == '\0')
2316 return ERROR_COMMAND_SYNTAX_ERROR
;
2319 for (i
=0; jtag_interfaces
[i
]; i
++)
2321 if (strcmp(args
[0], jtag_interfaces
[i
]->name
) == 0)
2323 if ((retval
= jtag_interfaces
[i
]->register_commands(cmd_ctx
)) != ERROR_OK
)
2328 jtag_interface
= jtag_interfaces
[i
];
2330 if (jtag_interface
->khz
== NULL
)
2332 jtag_interface
->khz
= default_khz
;
2334 if (jtag_interface
->speed_div
== NULL
)
2336 jtag_interface
->speed_div
= default_speed_div
;
2338 if (jtag_interface
->power_dropout
== NULL
)
2340 jtag_interface
->power_dropout
= default_power_dropout
;
2342 if (jtag_interface
->srst_asserted
== NULL
)
2344 jtag_interface
->srst_asserted
= default_srst_asserted
;
2351 /* no valid interface was found (i.e. the configuration option,
2352 * didn't match one of the compiled-in interfaces
2354 LOG_ERROR("No valid jtag interface found (%s)", args
[0]);
2355 LOG_ERROR("compiled-in jtag interfaces:");
2356 for (i
= 0; jtag_interfaces
[i
]; i
++)
2358 LOG_ERROR("%i: %s", i
, jtag_interfaces
[i
]->name
);
2361 return ERROR_JTAG_INVALID_INTERFACE
;
2364 int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2368 Jim_Obj
*newargs
[ 10 ];
2371 * argv[-1] = command
2372 * argv[ 0] = ir length
2373 * argv[ 1] = ir capture
2374 * argv[ 2] = ir mask
2375 * argv[ 3] = not actually used by anything but in the docs
2379 command_print( cmd_ctx
, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
2382 command_print( cmd_ctx
, "OLD SYNTAX: DEPRECATED - translating to new syntax");
2383 command_print( cmd_ctx
, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
2387 command_print( cmd_ctx
, "Example: STM32 has 2 taps, the cortexM3(len4) + boundryscan(len5)");
2388 command_print( cmd_ctx
, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
2389 command_print( cmd_ctx
, "jtag newtap stm32 boundry ....., and the tap: \"stm32.boundery\"");
2390 command_print( cmd_ctx
, "And then refer to the taps by the dotted name.");
2392 newargs
[0] = Jim_NewStringObj( interp
, "jtag", -1 );
2393 newargs
[1] = Jim_NewStringObj( interp
, "newtap", -1 );
2394 sprintf( buf
, "chip%d", jtag_NumTotalTaps() );
2395 newargs
[2] = Jim_NewStringObj( interp
, buf
, -1 );
2396 sprintf( buf
, "tap%d", jtag_NumTotalTaps() );
2397 newargs
[3] = Jim_NewStringObj( interp
, buf
, -1 );
2398 newargs
[4] = Jim_NewStringObj( interp
, "-irlen", -1 );
2399 newargs
[5] = Jim_NewStringObj( interp
, args
[0], -1 );
2400 newargs
[6] = Jim_NewStringObj( interp
, "-ircapture", -1 );
2401 newargs
[7] = Jim_NewStringObj( interp
, args
[1], -1 );
2402 newargs
[8] = Jim_NewStringObj( interp
, "-irmask", -1 );
2403 newargs
[9] = Jim_NewStringObj( interp
, args
[2], -1 );
2405 command_print( cmd_ctx
, "NEW COMMAND:");
2406 sprintf( buf
, "%s %s %s %s %s %s %s %s %s %s",
2407 Jim_GetString( newargs
[0], NULL
),
2408 Jim_GetString( newargs
[1], NULL
),
2409 Jim_GetString( newargs
[2], NULL
),
2410 Jim_GetString( newargs
[3], NULL
),
2411 Jim_GetString( newargs
[4], NULL
),
2412 Jim_GetString( newargs
[5], NULL
),
2413 Jim_GetString( newargs
[6], NULL
),
2414 Jim_GetString( newargs
[7], NULL
),
2415 Jim_GetString( newargs
[8], NULL
),
2416 Jim_GetString( newargs
[9], NULL
) );
2418 e
= jim_jtag_command( interp
, 10, newargs
);
2420 command_print( cmd_ctx
, "%s", Jim_GetString( Jim_GetResult(interp
), NULL
) );
2425 int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2429 tap
= jtag_all_taps
;
2430 command_print(cmd_ctx
, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
2431 command_print(cmd_ctx
, "---|--------------------|---------|------------|------------|------|------|------|---------");
2434 u32 expected
, expected_mask
, cur_instr
, ii
;
2435 expected
= buf_get_u32(tap
->expected
, 0, tap
->ir_length
);
2436 expected_mask
= buf_get_u32(tap
->expected_mask
, 0, tap
->ir_length
);
2437 cur_instr
= buf_get_u32(tap
->cur_instr
, 0, tap
->ir_length
);
2439 command_print(cmd_ctx
,
2440 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
2441 tap
->abs_chain_position
,
2443 tap
->enabled
? 'Y' : 'n',
2445 (tap
->expected_ids_cnt
> 0 ? tap
->expected_ids
[0] : 0),
2451 for (ii
= 1; ii
< tap
->expected_ids_cnt
; ii
++) {
2452 command_print(cmd_ctx
, " | | | | 0x%08x | | | | ",
2453 tap
->expected_ids
[ii
]);
2456 tap
= tap
->next_tap
;
2462 int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2465 return ERROR_COMMAND_SYNTAX_ERROR
;
2469 if (strcmp(args
[0], "none") == 0)
2470 jtag_reset_config
= RESET_NONE
;
2471 else if (strcmp(args
[0], "trst_only") == 0)
2472 jtag_reset_config
= RESET_HAS_TRST
;
2473 else if (strcmp(args
[0], "srst_only") == 0)
2474 jtag_reset_config
= RESET_HAS_SRST
;
2475 else if (strcmp(args
[0], "trst_and_srst") == 0)
2476 jtag_reset_config
= RESET_TRST_AND_SRST
;
2479 LOG_ERROR("(1) invalid reset_config argument (%s), defaulting to none", args
[0]);
2480 jtag_reset_config
= RESET_NONE
;
2481 return ERROR_INVALID_ARGUMENTS
;
2487 if (strcmp(args
[1], "separate") == 0)
2489 /* seperate reset lines - default */
2492 if (strcmp(args
[1], "srst_pulls_trst") == 0)
2493 jtag_reset_config
|= RESET_SRST_PULLS_TRST
;
2494 else if (strcmp(args
[1], "trst_pulls_srst") == 0)
2495 jtag_reset_config
|= RESET_TRST_PULLS_SRST
;
2496 else if (strcmp(args
[1], "combined") == 0)
2497 jtag_reset_config
|= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
2500 LOG_ERROR("(2) invalid reset_config argument (%s), defaulting to none", args
[1]);
2501 jtag_reset_config
= RESET_NONE
;
2502 return ERROR_INVALID_ARGUMENTS
;
2509 if (strcmp(args
[2], "trst_open_drain") == 0)
2510 jtag_reset_config
|= RESET_TRST_OPEN_DRAIN
;
2511 else if (strcmp(args
[2], "trst_push_pull") == 0)
2512 jtag_reset_config
&= ~RESET_TRST_OPEN_DRAIN
;
2515 LOG_ERROR("(3) invalid reset_config argument (%s) defaulting to none", args
[2] );
2516 jtag_reset_config
= RESET_NONE
;
2517 return ERROR_INVALID_ARGUMENTS
;
2523 if (strcmp(args
[3], "srst_push_pull") == 0)
2524 jtag_reset_config
|= RESET_SRST_PUSH_PULL
;
2525 else if (strcmp(args
[3], "srst_open_drain") == 0)
2526 jtag_reset_config
&= ~RESET_SRST_PUSH_PULL
;
2529 LOG_ERROR("(4) invalid reset_config argument (%s), defaulting to none", args
[3]);
2530 jtag_reset_config
= RESET_NONE
;
2531 return ERROR_INVALID_ARGUMENTS
;
2538 int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2542 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
2547 jtag_nsrst_delay
= strtoul(args
[0], NULL
, 0);
2553 int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2557 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
2562 jtag_ntrst_delay
= strtoul(args
[0], NULL
, 0);
2568 int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2570 int retval
=ERROR_OK
;
2574 LOG_DEBUG("handle jtag speed");
2577 cur_speed
= jtag_speed
= strtoul(args
[0], NULL
, 0);
2579 /* this command can be called during CONFIG,
2580 * in which case jtag isn't initialized */
2583 retval
=jtag
->speed(cur_speed
);
2585 } else if (argc
== 0)
2589 return ERROR_COMMAND_SYNTAX_ERROR
;
2591 command_print(cmd_ctx
, "jtag_speed: %d", jtag_speed
);
2596 int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2598 int retval
=ERROR_OK
;
2599 LOG_DEBUG("handle jtag khz");
2603 speed_khz
= strtoul(args
[0], NULL
, 0);
2607 LOG_DEBUG("have interface set up");
2609 if ((retval
=jtag
->khz(speed_khz
, &speed_div1
))!=ERROR_OK
)
2615 cur_speed
= jtag_speed
= speed_div1
;
2617 retval
=jtag
->speed(cur_speed
);
2626 return ERROR_COMMAND_SYNTAX_ERROR
;
2631 if ((retval
=jtag
->speed_div(jtag_speed
, &speed_khz
))!=ERROR_OK
)
2637 command_print(cmd_ctx
, "RCLK - adaptive");
2640 command_print(cmd_ctx
, "%d kHz", speed_khz
);
2646 int handle_endstate_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2648 enum tap_state state
;
2652 return ERROR_COMMAND_SYNTAX_ERROR
;
2656 for (state
= 0; state
< 16; state
++)
2658 if (strcmp(args
[0], jtag_state_name(state
)) == 0)
2660 jtag_add_end_state(state
);
2661 jtag_execute_queue();
2665 command_print(cmd_ctx
, "current endstate: %s", jtag_state_name(cmd_queue_end_state
));
2670 int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2677 return ERROR_COMMAND_SYNTAX_ERROR
;
2680 if (args
[0][0] == '1')
2682 else if (args
[0][0] == '0')
2686 return ERROR_COMMAND_SYNTAX_ERROR
;
2689 if (args
[1][0] == '1')
2691 else if (args
[1][0] == '0')
2695 return ERROR_COMMAND_SYNTAX_ERROR
;
2698 if (jtag_interface_init(cmd_ctx
) != ERROR_OK
)
2699 return ERROR_JTAG_INIT_FAILED
;
2701 jtag_add_reset(trst
, srst
);
2702 jtag_execute_queue();
2707 int handle_runtest_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2711 return ERROR_COMMAND_SYNTAX_ERROR
;
2714 jtag_add_runtest(strtol(args
[0], NULL
, 0), -1);
2715 jtag_execute_queue();
2721 int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2724 scan_field_t
*fields
;
2727 if ((argc
< 2) || (argc
% 2))
2729 return ERROR_COMMAND_SYNTAX_ERROR
;
2732 fields
= malloc(sizeof(scan_field_t
) * argc
/ 2);
2734 for (i
= 0; i
< argc
/ 2; i
++)
2736 tap
= jtag_TapByString( args
[i
*2] );
2739 command_print( cmd_ctx
, "Tap: %s unknown", args
[i
*2] );
2742 int field_size
= tap
->ir_length
;
2743 fields
[i
].tap
= tap
;
2744 fields
[i
].out_value
= malloc(CEIL(field_size
, 8));
2745 buf_set_u32(fields
[i
].out_value
, 0, field_size
, strtoul(args
[i
*2+1], NULL
, 0));
2746 fields
[i
].out_mask
= NULL
;
2747 fields
[i
].in_value
= NULL
;
2748 fields
[i
].in_check_mask
= NULL
;
2749 fields
[i
].in_handler
= NULL
;
2750 fields
[i
].in_handler_priv
= NULL
;
2753 jtag_add_ir_scan(argc
/ 2, fields
, -1);
2754 jtag_execute_queue();
2756 for (i
= 0; i
< argc
/ 2; i
++)
2757 free(fields
[i
].out_value
);
2764 int Jim_Command_drscan(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *args
)
2767 scan_field_t
*fields
;
2769 int field_count
= 0;
2774 * args[2] = num_bits
2775 * args[3] = hex string
2776 * ... repeat num bits and hex string ...
2778 if ((argc
< 4) || ((argc
% 2)!=0))
2780 Jim_WrongNumArgs(interp
, 1, args
, "wrong arguments");
2784 for (i
= 2; i
< argc
; i
+=2)
2788 e
= Jim_GetLong(interp
, args
[i
], &bits
);
2793 tap
= jtag_TapByJimObj( interp
, args
[1] );
2798 num_fields
=(argc
-2)/2;
2799 fields
= malloc(sizeof(scan_field_t
) * num_fields
);
2800 for (i
= 2; i
< argc
; i
+=2)
2806 Jim_GetLong(interp
, args
[i
], &bits
);
2807 str
= Jim_GetString(args
[i
+1], &len
);
2809 fields
[field_count
].tap
= tap
;
2810 fields
[field_count
].num_bits
= bits
;
2811 fields
[field_count
].out_value
= malloc(CEIL(bits
, 8));
2812 str_to_buf(str
, len
, fields
[field_count
].out_value
, bits
, 0);
2813 fields
[field_count
].out_mask
= NULL
;
2814 fields
[field_count
].in_value
= fields
[field_count
].out_value
;
2815 fields
[field_count
].in_check_mask
= NULL
;
2816 fields
[field_count
].in_check_value
= NULL
;
2817 fields
[field_count
].in_handler
= NULL
;
2818 fields
[field_count
++].in_handler_priv
= NULL
;
2821 jtag_add_dr_scan(num_fields
, fields
, -1);
2822 retval
= jtag_execute_queue();
2823 if (retval
!= ERROR_OK
)
2825 Jim_SetResultString(interp
, "drscan: jtag execute failed",-1);
2830 Jim_Obj
*list
= Jim_NewListObj(interp
, NULL
, 0);
2831 for (i
= 2; i
< argc
; i
+=2)
2836 Jim_GetLong(interp
, args
[i
], &bits
);
2837 str
= buf_to_str(fields
[field_count
].in_value
, bits
, 16);
2838 free(fields
[field_count
].out_value
);
2840 Jim_ListAppendElement(interp
, list
, Jim_NewStringObj(interp
, str
, strlen(str
)));
2845 Jim_SetResult(interp
, list
);
2852 int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2856 if (strcmp(args
[0], "enable") == 0)
2858 jtag_verify_capture_ir
= 1;
2860 else if (strcmp(args
[0], "disable") == 0)
2862 jtag_verify_capture_ir
= 0;
2865 return ERROR_COMMAND_SYNTAX_ERROR
;
2867 } else if (argc
!= 0)
2869 return ERROR_COMMAND_SYNTAX_ERROR
;
2872 command_print(cmd_ctx
, "verify Capture-IR is %s", (jtag_verify_capture_ir
) ? "enabled": "disabled");
2877 int jtag_power_dropout(int *dropout
)
2879 return jtag
->power_dropout(dropout
);
2882 int jtag_srst_asserted(int *srst_asserted
)
2884 return jtag
->srst_asserted(srst_asserted
);
2887 void jtag_tap_handle_event( jtag_tap_t
* tap
, enum jtag_tap_event e
)
2889 jtag_tap_event_action_t
* jteap
;
2892 jteap
= tap
->event_action
;
2896 if (jteap
->event
== e
) {
2898 LOG_DEBUG( "JTAG tap: %s event: %d (%s) action: %s\n",
2901 Jim_Nvp_value2name_simple(nvp_jtag_tap_event
, e
)->name
,
2902 Jim_GetString(jteap
->body
, NULL
) );
2903 if (Jim_EvalObj(interp
, jteap
->body
) != JIM_OK
) {
2904 Jim_PrintErrorMessage(interp
);
2908 jteap
= jteap
->next
;
2912 LOG_DEBUG( "event %d %s - no action",
2914 Jim_Nvp_value2name_simple( nvp_jtag_tap_event
, e
)->name
);
2919 /* map state number to SVF state string */
2920 const char* jtag_state_name(enum tap_state state
)
2926 case TAP_RESET
: ret
= "RESET"; break;
2927 case TAP_IDLE
: ret
= "IDLE"; break;
2928 case TAP_DRSELECT
: ret
= "DRSELECT"; break;
2929 case TAP_DRCAPTURE
: ret
= "DRCAPTURE"; break;
2930 case TAP_DRSHIFT
: ret
= "DRSHIFT"; break;
2931 case TAP_DREXIT1
: ret
= "DREXIT1"; break;
2932 case TAP_DRPAUSE
: ret
= "DRPAUSE"; break;
2933 case TAP_DREXIT2
: ret
= "DREXIT2"; break;
2934 case TAP_DRUPDATE
: ret
= "DRUPDATE"; break;
2935 case TAP_IRSELECT
: ret
= "IRSELECT"; break;
2936 case TAP_IRCAPTURE
: ret
= "IRCAPTURE"; break;
2937 case TAP_IRSHIFT
: ret
= "IRSHIFT"; break;
2938 case TAP_IREXIT1
: ret
= "IREXIT1"; break;
2939 case TAP_IRPAUSE
: ret
= "IRPAUSE"; break;
2940 case TAP_IREXIT2
: ret
= "IREXIT2"; break;
2941 case TAP_IRUPDATE
: ret
= "IRUPDATE"; break;
2942 default: ret
= "???";
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)