1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007-2010 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
10 * Copyright (C) 2009 SoftPLC Corporation *
11 * http://softplc.com *
14 * Copyright (C) 2009 Zachary T Welch *
15 * zw@superlucidity.net *
16 ***************************************************************************/
25 #include "minidriver.h"
26 #include "interface.h"
27 #include "interfaces.h"
34 #include <helper/command.h>
35 #include <helper/nvp.h>
36 #include <helper/time_support.h>
37 #include "transport/transport.h"
41 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
44 static const struct jim_nvp nvp_jtag_tap_event
[] = {
45 { .value
= JTAG_TRST_ASSERTED
, .name
= "post-reset" },
46 { .value
= JTAG_TAP_EVENT_SETUP
, .name
= "setup" },
47 { .value
= JTAG_TAP_EVENT_ENABLE
, .name
= "tap-enable" },
48 { .value
= JTAG_TAP_EVENT_DISABLE
, .name
= "tap-disable" },
50 { .name
= NULL
, .value
= -1 }
53 struct jtag_tap
*jtag_tap_by_jim_obj(Jim_Interp
*interp
, Jim_Obj
*o
)
55 const char *cp
= Jim_GetString(o
, NULL
);
56 struct jtag_tap
*t
= cp
? jtag_tap_by_string(cp
) : NULL
;
60 Jim_SetResultFormatted(interp
, "Tap '%s' could not be found", cp
);
64 static bool scan_is_safe(tap_state_t state
)
77 static COMMAND_HELPER(handle_jtag_command_drscan_fields
, struct scan_field
*fields
)
79 unsigned int field_count
= 0;
80 for (unsigned int i
= 1; i
< CMD_ARGC
; i
+= 2) {
82 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[i
], bits
);
83 fields
[field_count
].num_bits
= bits
;
85 void *t
= malloc(DIV_ROUND_UP(bits
, 8));
87 LOG_ERROR("Out of memory");
90 fields
[field_count
].out_value
= t
;
91 str_to_buf(CMD_ARGV
[i
+ 1], strlen(CMD_ARGV
[i
+ 1]), t
, bits
, 0);
92 fields
[field_count
].in_value
= t
;
99 COMMAND_HANDLER(handle_jtag_command_drscan
)
102 * CMD_ARGV[0] = device
103 * CMD_ARGV[1] = num_bits
104 * CMD_ARGV[2] = hex string
105 * ... repeat num bits and hex string ...
108 * CMD_ARGV[CMD_ARGC-2] = "-endstate"
109 * CMD_ARGV[CMD_ARGC-1] = statename
112 if (CMD_ARGC
< 3 || (CMD_ARGC
% 2) != 1)
113 return ERROR_COMMAND_SYNTAX_ERROR
;
115 struct jtag_tap
*tap
= jtag_tap_by_string(CMD_ARGV
[0]);
117 command_print(CMD
, "Tap '%s' could not be found", CMD_ARGV
[0]);
118 return ERROR_COMMAND_ARGUMENT_INVALID
;
122 command_print(CMD
, "Can't execute as the selected tap is in BYPASS");
126 tap_state_t endstate
= TAP_IDLE
;
127 if (CMD_ARGC
> 3 && !strcmp("-endstate", CMD_ARGV
[CMD_ARGC
- 2])) {
128 const char *state_name
= CMD_ARGV
[CMD_ARGC
- 1];
129 endstate
= tap_state_by_name(state_name
);
131 command_print(CMD
, "endstate: %s invalid", state_name
);
132 return ERROR_COMMAND_ARGUMENT_INVALID
;
135 if (!scan_is_safe(endstate
))
136 LOG_WARNING("drscan with unsafe endstate \"%s\"", state_name
);
141 unsigned int num_fields
= (CMD_ARGC
- 1) / 2;
142 struct scan_field
*fields
= calloc(num_fields
, sizeof(struct scan_field
));
144 LOG_ERROR("Out of memory");
148 int retval
= CALL_COMMAND_HANDLER(handle_jtag_command_drscan_fields
, fields
);
149 if (retval
!= ERROR_OK
)
152 jtag_add_dr_scan(tap
, num_fields
, fields
, endstate
);
154 retval
= jtag_execute_queue();
155 if (retval
!= ERROR_OK
) {
156 command_print(CMD
, "drscan: jtag execute failed");
160 for (unsigned int i
= 0; i
< num_fields
; i
++) {
161 char *str
= buf_to_hex_str(fields
[i
].in_value
, fields
[i
].num_bits
);
162 command_print(CMD
, "%s", str
);
167 for (unsigned int i
= 0; i
< num_fields
; i
++)
168 free(fields
[i
].in_value
);
174 COMMAND_HANDLER(handle_jtag_command_pathmove
)
176 tap_state_t states
[8];
178 if (CMD_ARGC
< 1 || CMD_ARGC
> ARRAY_SIZE(states
))
179 return ERROR_COMMAND_SYNTAX_ERROR
;
181 for (unsigned int i
= 0; i
< CMD_ARGC
; i
++) {
182 states
[i
] = tap_state_by_name(CMD_ARGV
[i
]);
184 command_print(CMD
, "endstate: %s invalid", CMD_ARGV
[i
]);
185 return ERROR_COMMAND_ARGUMENT_INVALID
;
189 int retval
= jtag_add_statemove(states
[0]);
190 if (retval
== ERROR_OK
)
191 retval
= jtag_execute_queue();
192 if (retval
!= ERROR_OK
) {
193 command_print(CMD
, "pathmove: jtag execute failed");
197 jtag_add_pathmove(CMD_ARGC
- 1, states
+ 1);
198 retval
= jtag_execute_queue();
199 if (retval
!= ERROR_OK
) {
200 command_print(CMD
, "pathmove: failed");
207 COMMAND_HANDLER(handle_jtag_flush_count
)
210 return ERROR_COMMAND_SYNTAX_ERROR
;
212 int count
= jtag_get_flush_queue_count();
213 command_print_sameline(CMD
, "%d", count
);
218 /* REVISIT Just what about these should "move" ... ?
219 * These registrations, into the main JTAG table?
221 * There's a minor compatibility issue, these all show up twice;
222 * that's not desirable:
223 * - jtag drscan ... NOT DOCUMENTED!
226 * The "irscan" command (for example) doesn't show twice.
228 static const struct command_registration jtag_command_handlers_to_move
[] = {
231 .mode
= COMMAND_EXEC
,
232 .handler
= handle_jtag_command_drscan
,
233 .help
= "Execute Data Register (DR) scan for one TAP. "
234 "Other TAPs must be in BYPASS mode.",
235 .usage
= "tap_name (num_bits value)+ ['-endstate' state_name]",
238 .name
= "flush_count",
239 .mode
= COMMAND_EXEC
,
240 .handler
= handle_jtag_flush_count
,
241 .help
= "Returns the number of times the JTAG queue "
247 .mode
= COMMAND_EXEC
,
248 .handler
= handle_jtag_command_pathmove
,
249 .usage
= "start_state state1 [state2 [state3 ...]]",
250 .help
= "Move JTAG state machine from current state "
251 "(start_state) to state1, then state2, state3, etc.",
253 COMMAND_REGISTRATION_DONE
257 enum jtag_tap_cfg_param
{
262 static struct jim_nvp nvp_config_opts
[] = {
263 { .name
= "-event", .value
= JCFG_EVENT
},
264 { .name
= "-idcode", .value
= JCFG_IDCODE
},
266 { .name
= NULL
, .value
= -1 }
269 static int jtag_tap_configure_event(struct jim_getopt_info
*goi
, struct jtag_tap
*tap
)
271 if (goi
->argc
== 0) {
272 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event <event-name> ...");
277 int e
= jim_getopt_nvp(goi
, nvp_jtag_tap_event
, &n
);
279 jim_getopt_nvp_unknown(goi
, nvp_jtag_tap_event
, 1);
283 if (goi
->isconfigure
) {
284 if (goi
->argc
!= 1) {
285 Jim_WrongNumArgs(goi
->interp
,
288 "-event <event-name> <event-body>");
292 if (goi
->argc
!= 0) {
293 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event <event-name>");
298 struct jtag_tap_event_action
*jteap
= tap
->event_action
;
299 /* replace existing event body */
302 if (jteap
->event
== (enum jtag_event
)n
->value
) {
309 Jim_SetEmptyResult(goi
->interp
);
311 if (goi
->isconfigure
) {
313 jteap
= calloc(1, sizeof(*jteap
));
314 else if (jteap
->body
)
315 Jim_DecrRefCount(goi
->interp
, jteap
->body
);
317 jteap
->interp
= goi
->interp
;
318 jteap
->event
= n
->value
;
321 jim_getopt_obj(goi
, &o
);
322 jteap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
323 Jim_IncrRefCount(jteap
->body
);
326 /* add to head of event list */
327 jteap
->next
= tap
->event_action
;
328 tap
->event_action
= jteap
;
331 jteap
->interp
= goi
->interp
;
332 Jim_SetResult(goi
->interp
,
333 Jim_DuplicateObj(goi
->interp
, jteap
->body
));
338 static int jtag_tap_configure_cmd(struct jim_getopt_info
*goi
, struct jtag_tap
*tap
)
340 /* parse config or cget options */
341 while (goi
->argc
> 0) {
342 Jim_SetEmptyResult(goi
->interp
);
345 int e
= jim_getopt_nvp(goi
, nvp_config_opts
, &n
);
347 jim_getopt_nvp_unknown(goi
, nvp_config_opts
, 0);
353 e
= jtag_tap_configure_event(goi
, tap
);
358 if (goi
->isconfigure
) {
359 Jim_SetResultFormatted(goi
->interp
,
360 "not settable: %s", n
->name
);
363 if (goi
->argc
!= 0) {
364 Jim_WrongNumArgs(goi
->interp
,
365 goi
->argc
, goi
->argv
,
370 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, tap
->idcode
));
373 Jim_SetResultFormatted(goi
->interp
, "unknown value: %s", n
->name
);
381 #define NTAP_OPT_IRLEN 0
382 #define NTAP_OPT_IRMASK 1
383 #define NTAP_OPT_IRCAPTURE 2
384 #define NTAP_OPT_ENABLED 3
385 #define NTAP_OPT_DISABLED 4
386 #define NTAP_OPT_EXPECTED_ID 5
387 #define NTAP_OPT_VERSION 6
388 #define NTAP_OPT_BYPASS 7
389 #define NTAP_OPT_IRBYPASS 8
391 static const struct nvp jtag_newtap_opts
[] = {
392 { .name
= "-irlen", .value
= NTAP_OPT_IRLEN
},
393 { .name
= "-irmask", .value
= NTAP_OPT_IRMASK
},
394 { .name
= "-ircapture", .value
= NTAP_OPT_IRCAPTURE
},
395 { .name
= "-enable", .value
= NTAP_OPT_ENABLED
},
396 { .name
= "-disable", .value
= NTAP_OPT_DISABLED
},
397 { .name
= "-expected-id", .value
= NTAP_OPT_EXPECTED_ID
},
398 { .name
= "-ignore-version", .value
= NTAP_OPT_VERSION
},
399 { .name
= "-ignore-bypass", .value
= NTAP_OPT_BYPASS
},
400 { .name
= "-ir-bypass", .value
= NTAP_OPT_IRBYPASS
},
401 { .name
= NULL
, .value
= -1 },
404 static COMMAND_HELPER(handle_jtag_newtap_args
, struct jtag_tap
*tap
)
406 /* we expect CHIP + TAP + OPTIONS */
408 return ERROR_COMMAND_SYNTAX_ERROR
;
410 tap
->chip
= strdup(CMD_ARGV
[0]);
411 tap
->tapname
= strdup(CMD_ARGV
[1]);
412 tap
->dotted_name
= alloc_printf("%s.%s", CMD_ARGV
[0], CMD_ARGV
[1]);
413 if (!tap
->chip
|| !tap
->tapname
|| !tap
->dotted_name
) {
414 LOG_ERROR("Out of memory");
420 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
421 tap
->chip
, tap
->tapname
, tap
->dotted_name
, CMD_ARGC
);
424 * IEEE specifies that the two LSBs of an IR scan are 01, so make
425 * that the default. The "-ircapture" and "-irmask" options are only
426 * needed to cope with nonstandard TAPs, or to specify more bits.
428 tap
->ir_capture_mask
= 0x03;
429 tap
->ir_capture_value
= 0x01;
432 const struct nvp
*n
= nvp_name2value(jtag_newtap_opts
, CMD_ARGV
[0]);
436 case NTAP_OPT_ENABLED
:
437 tap
->disabled_after_reset
= false;
440 case NTAP_OPT_DISABLED
:
441 tap
->disabled_after_reset
= true;
444 case NTAP_OPT_EXPECTED_ID
:
446 return ERROR_COMMAND_ARGUMENT_INVALID
;
448 tap
->expected_ids
= realloc(tap
->expected_ids
,
449 (tap
->expected_ids_cnt
+ 1) * sizeof(uint32_t));
450 if (!tap
->expected_ids
) {
451 LOG_ERROR("Out of memory");
456 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], id
);
459 tap
->expected_ids
[tap
->expected_ids_cnt
++] = id
;
465 return ERROR_COMMAND_ARGUMENT_INVALID
;
467 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], tap
->ir_length
);
470 if (tap
->ir_length
> (int)(8 * sizeof(tap
->ir_capture_value
)))
471 LOG_WARNING("%s: huge IR length %d", tap
->dotted_name
, tap
->ir_length
);
474 case NTAP_OPT_IRMASK
:
476 return ERROR_COMMAND_ARGUMENT_INVALID
;
478 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], tap
->ir_capture_mask
);
481 if ((tap
->ir_capture_mask
& 3) != 3)
482 LOG_WARNING("%s: nonstandard IR mask", tap
->dotted_name
);
485 case NTAP_OPT_IRCAPTURE
:
487 return ERROR_COMMAND_ARGUMENT_INVALID
;
489 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], tap
->ir_capture_value
);
492 if ((tap
->ir_capture_value
& 3) != 1)
493 LOG_WARNING("%s: nonstandard IR value", tap
->dotted_name
);
496 case NTAP_OPT_VERSION
:
497 tap
->ignore_version
= true;
500 case NTAP_OPT_BYPASS
:
501 tap
->ignore_bypass
= true;
504 case NTAP_OPT_IRBYPASS
:
506 return ERROR_COMMAND_ARGUMENT_INVALID
;
508 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[0], tap
->ir_bypass_value
);
514 nvp_unknown_command_print(CMD
, jtag_newtap_opts
, NULL
, CMD_ARGV
[-1]);
515 return ERROR_COMMAND_ARGUMENT_INVALID
;
519 /* default is enabled-after-reset */
520 tap
->enabled
= !tap
->disabled_after_reset
;
522 if (transport_is_jtag() && tap
->ir_length
== 0) {
523 command_print(CMD
, "newtap: %s missing IR length", tap
->dotted_name
);
524 return ERROR_COMMAND_ARGUMENT_INVALID
;
530 __COMMAND_HANDLER(handle_jtag_newtap
)
532 struct jtag_tap
*tap
= calloc(1, sizeof(struct jtag_tap
));
534 LOG_ERROR("Out of memory");
538 int retval
= CALL_COMMAND_HANDLER(handle_jtag_newtap_args
, tap
);
539 if (retval
!= ERROR_OK
) {
542 free(tap
->dotted_name
);
543 free(tap
->expected_ids
);
552 static void jtag_tap_handle_event(struct jtag_tap
*tap
, enum jtag_event e
)
554 struct jtag_tap_event_action
*jteap
;
557 for (jteap
= tap
->event_action
; jteap
; jteap
= jteap
->next
) {
558 if (jteap
->event
!= e
)
561 struct jim_nvp
*nvp
= jim_nvp_value2name_simple(nvp_jtag_tap_event
, e
);
562 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
563 tap
->dotted_name
, e
, nvp
->name
,
564 Jim_GetString(jteap
->body
, NULL
));
566 retval
= Jim_EvalObj(jteap
->interp
, jteap
->body
);
567 if (retval
== JIM_RETURN
)
568 retval
= jteap
->interp
->returnCode
;
570 if (retval
!= JIM_OK
) {
571 Jim_MakeErrorMessage(jteap
->interp
);
572 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap
->interp
), NULL
));
577 case JTAG_TAP_EVENT_ENABLE
:
578 case JTAG_TAP_EVENT_DISABLE
:
579 /* NOTE: we currently assume the handlers
580 * can't fail. Right here is where we should
581 * really be verifying the scan chains ...
583 tap
->enabled
= (e
== JTAG_TAP_EVENT_ENABLE
);
584 LOG_INFO("JTAG tap: %s %s", tap
->dotted_name
,
585 tap
->enabled
? "enabled" : "disabled");
593 COMMAND_HANDLER(handle_jtag_arp_init
)
596 return ERROR_COMMAND_SYNTAX_ERROR
;
598 return jtag_init_inner(CMD_CTX
);
601 COMMAND_HANDLER(handle_jtag_arp_init_reset
)
604 return ERROR_COMMAND_SYNTAX_ERROR
;
606 if (transport_is_jtag())
607 return jtag_init_reset(CMD_CTX
);
609 if (transport_is_swd())
610 return swd_init_reset(CMD_CTX
);
615 static bool jtag_tap_enable(struct jtag_tap
*t
)
619 jtag_tap_handle_event(t
, JTAG_TAP_EVENT_ENABLE
);
623 /* FIXME add JTAG sanity checks, w/o TLR
624 * - scan chain length grew by one (this)
625 * - IDs and IR lengths are as expected
627 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE
);
630 static bool jtag_tap_disable(struct jtag_tap
*t
)
634 jtag_tap_handle_event(t
, JTAG_TAP_EVENT_DISABLE
);
638 /* FIXME add JTAG sanity checks, w/o TLR
639 * - scan chain length shrank by one (this)
640 * - IDs and IR lengths are as expected
642 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE
);
646 __COMMAND_HANDLER(handle_jtag_tap_enabler
)
649 return ERROR_COMMAND_SYNTAX_ERROR
;
651 struct jtag_tap
*t
= jtag_tap_by_string(CMD_ARGV
[0]);
653 command_print(CMD
, "Tap '%s' could not be found", CMD_ARGV
[0]);
654 return ERROR_COMMAND_ARGUMENT_INVALID
;
657 if (strcmp(CMD_NAME
, "tapisenabled") == 0) {
658 /* do nothing, just return the value */
659 } else if (strcmp(CMD_NAME
, "tapenable") == 0) {
660 if (!jtag_tap_enable(t
)) {
661 command_print(CMD
, "failed to enable tap %s", t
->dotted_name
);
664 } else if (strcmp(CMD_NAME
, "tapdisable") == 0) {
665 if (!jtag_tap_disable(t
)) {
666 command_print(CMD
, "failed to disable tap %s", t
->dotted_name
);
670 command_print(CMD
, "command '%s' unknown", CMD_NAME
);
674 command_print(CMD
, "%d", t
->enabled
? 1 : 0);
678 int jim_jtag_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
680 struct command
*c
= jim_to_command(interp
);
681 const char *cmd_name
= c
->name
;
682 struct jim_getopt_info goi
;
683 jim_getopt_setup(&goi
, interp
, argc
-1, argv
+ 1);
684 goi
.isconfigure
= !strcmp(cmd_name
, "configure");
685 if (goi
.argc
< 2 + goi
.isconfigure
) {
686 Jim_WrongNumArgs(goi
.interp
, 0, NULL
,
687 "<tap_name> <attribute> ...");
694 jim_getopt_obj(&goi
, &o
);
695 t
= jtag_tap_by_jim_obj(goi
.interp
, o
);
699 return jtag_tap_configure_cmd(&goi
, t
);
702 COMMAND_HANDLER(handle_jtag_names
)
705 return ERROR_COMMAND_SYNTAX_ERROR
;
707 for (struct jtag_tap
*tap
= jtag_all_taps(); tap
; tap
= tap
->next_tap
)
708 command_print(CMD
, "%s", tap
->dotted_name
);
713 COMMAND_HANDLER(handle_jtag_init_command
)
716 return ERROR_COMMAND_SYNTAX_ERROR
;
718 static bool jtag_initialized
;
719 if (jtag_initialized
) {
720 LOG_INFO("'jtag init' has already been called");
723 jtag_initialized
= true;
725 LOG_DEBUG("Initializing jtag devices...");
726 return jtag_init(CMD_CTX
);
729 static const struct command_registration jtag_subcommand_handlers
[] = {
733 .handler
= handle_jtag_init_command
,
734 .help
= "initialize jtag scan chain",
740 .handler
= handle_jtag_arp_init
,
741 .help
= "Validates JTAG scan chain against the list of "
742 "declared TAPs using just the four standard JTAG "
747 .name
= "arp_init-reset",
749 .handler
= handle_jtag_arp_init_reset
,
750 .help
= "Uses TRST and SRST to try resetting everything on "
751 "the JTAG scan chain, then performs 'jtag arp_init'.",
756 .mode
= COMMAND_CONFIG
,
757 .handler
= handle_jtag_newtap
,
758 .help
= "Create a new TAP instance named basename.tap_type, "
759 "and appends it to the scan chain.",
760 .usage
= "basename tap_type '-irlen' count "
761 "['-enable'|'-disable'] "
762 "['-expected_id' number] "
763 "['-ignore-version'] "
764 "['-ignore-bypass'] "
765 "['-ircapture' number] "
766 "['-ir-bypass' number] "
770 .name
= "tapisenabled",
771 .mode
= COMMAND_EXEC
,
772 .handler
= handle_jtag_tap_enabler
,
773 .help
= "Returns a Tcl boolean (0/1) indicating whether "
774 "the TAP is enabled (1) or not (0).",
779 .mode
= COMMAND_EXEC
,
780 .handler
= handle_jtag_tap_enabler
,
781 .help
= "Try to enable the specified TAP using the "
782 "'tap-enable' TAP event.",
786 .name
= "tapdisable",
787 .mode
= COMMAND_EXEC
,
788 .handler
= handle_jtag_tap_enabler
,
789 .help
= "Try to disable the specified TAP using the "
790 "'tap-disable' TAP event.",
796 .jim_handler
= jim_jtag_configure
,
797 .help
= "Provide a Tcl handler for the specified "
799 .usage
= "tap_name '-event' event_name handler",
803 .mode
= COMMAND_EXEC
,
804 .jim_handler
= jim_jtag_configure
,
805 .help
= "Return any Tcl handler for the specified "
807 .usage
= "tap_name '-event' event_name",
812 .handler
= handle_jtag_names
,
813 .help
= "Returns list of all JTAG tap names.",
817 .chain
= jtag_command_handlers_to_move
,
819 COMMAND_REGISTRATION_DONE
822 void jtag_notify_event(enum jtag_event event
)
824 struct jtag_tap
*tap
;
826 for (tap
= jtag_all_taps(); tap
; tap
= tap
->next_tap
)
827 jtag_tap_handle_event(tap
, event
);
831 COMMAND_HANDLER(handle_scan_chain_command
)
833 struct jtag_tap
*tap
;
834 char expected_id
[12];
836 tap
= jtag_all_taps();
838 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
840 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
843 uint32_t expected
, expected_mask
, ii
;
845 snprintf(expected_id
, sizeof(expected_id
), "0x%08x",
846 (unsigned)((tap
->expected_ids_cnt
> 0)
847 ? tap
->expected_ids
[0]
849 if (tap
->ignore_version
)
850 expected_id
[2] = '*';
852 expected
= buf_get_u32(tap
->expected
, 0, tap
->ir_length
);
853 expected_mask
= buf_get_u32(tap
->expected_mask
, 0, tap
->ir_length
);
856 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
857 tap
->abs_chain_position
,
859 tap
->enabled
? 'Y' : 'n',
860 (unsigned int)(tap
->idcode
),
862 (unsigned int)(tap
->ir_length
),
863 (unsigned int)(expected
),
864 (unsigned int)(expected_mask
));
866 for (ii
= 1; ii
< tap
->expected_ids_cnt
; ii
++) {
867 snprintf(expected_id
, sizeof(expected_id
), "0x%08x",
868 (unsigned) tap
->expected_ids
[ii
]);
869 if (tap
->ignore_version
)
870 expected_id
[2] = '*';
883 COMMAND_HANDLER(handle_jtag_ntrst_delay_command
)
886 return ERROR_COMMAND_SYNTAX_ERROR
;
889 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
891 jtag_set_ntrst_delay(delay
);
893 command_print(CMD
, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
897 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command
)
900 return ERROR_COMMAND_SYNTAX_ERROR
;
903 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
905 jtag_set_ntrst_assert_width(delay
);
907 command_print(CMD
, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
911 COMMAND_HANDLER(handle_jtag_rclk_command
)
914 return ERROR_COMMAND_SYNTAX_ERROR
;
916 int retval
= ERROR_OK
;
919 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], khz
);
921 retval
= adapter_config_rclk(khz
);
922 if (retval
!= ERROR_OK
)
926 int cur_khz
= adapter_get_speed_khz();
927 retval
= adapter_get_speed_readable(&cur_khz
);
928 if (retval
!= ERROR_OK
)
932 command_print(CMD
, "RCLK not supported - fallback to %d kHz", cur_khz
);
934 command_print(CMD
, "RCLK - adaptive");
939 COMMAND_HANDLER(handle_runtest_command
)
942 return ERROR_COMMAND_SYNTAX_ERROR
;
945 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num_clocks
);
947 jtag_add_runtest(num_clocks
, TAP_IDLE
);
948 return jtag_execute_queue();
952 * For "irscan" or "drscan" commands, the "end" (really, "next") state
953 * should be stable ... and *NOT* a shift state, otherwise free-running
954 * jtag clocks could change the values latched by the update state.
955 * Not surprisingly, this is the same constraint as SVF; the "irscan"
956 * and "drscan" commands are a write-only subset of what SVF provides.
959 COMMAND_HANDLER(handle_irscan_command
)
962 struct scan_field
*fields
;
963 struct jtag_tap
*tap
= NULL
;
964 tap_state_t endstate
;
966 if ((CMD_ARGC
< 2) || (CMD_ARGC
% 2))
967 return ERROR_COMMAND_SYNTAX_ERROR
;
969 /* optional "-endstate" "statename" at the end of the arguments,
970 * so that e.g. IRPAUSE can let us load the data register before
971 * entering RUN/IDLE to execute the instruction we load here.
976 /* have at least one pair of numbers.
977 * is last pair the magic text? */
978 if (strcmp("-endstate", CMD_ARGV
[CMD_ARGC
- 2]) == 0) {
979 endstate
= tap_state_by_name(CMD_ARGV
[CMD_ARGC
- 1]);
980 if (endstate
== TAP_INVALID
)
981 return ERROR_COMMAND_SYNTAX_ERROR
;
982 if (!scan_is_safe(endstate
))
983 LOG_WARNING("unstable irscan endstate \"%s\"",
984 CMD_ARGV
[CMD_ARGC
- 1]);
989 int num_fields
= CMD_ARGC
/ 2;
990 if (num_fields
> 1) {
991 /* we really should be looking at plain_ir_scan if we want
992 * anything more fancy.
994 LOG_ERROR("Specify a single value for tap");
995 return ERROR_COMMAND_SYNTAX_ERROR
;
998 fields
= calloc(num_fields
, sizeof(*fields
));
1001 for (i
= 0; i
< num_fields
; i
++) {
1002 tap
= jtag_tap_by_string(CMD_ARGV
[i
*2]);
1005 command_print(CMD
, "Tap: %s unknown", CMD_ARGV
[i
*2]);
1010 retval
= parse_u64(CMD_ARGV
[i
* 2 + 1], &value
);
1011 if (retval
!= ERROR_OK
)
1014 int field_size
= tap
->ir_length
;
1015 fields
[i
].num_bits
= field_size
;
1016 uint8_t *v
= calloc(1, DIV_ROUND_UP(field_size
, 8));
1018 LOG_ERROR("Out of memory");
1022 buf_set_u64(v
, 0, field_size
, value
);
1023 fields
[i
].out_value
= v
;
1024 fields
[i
].in_value
= NULL
;
1027 /* did we have an endstate? */
1028 jtag_add_ir_scan(tap
, fields
, endstate
);
1030 retval
= jtag_execute_queue();
1033 for (i
= 0; i
< num_fields
; i
++)
1034 free((void *)fields
[i
].out_value
);
1041 COMMAND_HANDLER(handle_verify_ircapture_command
)
1044 return ERROR_COMMAND_SYNTAX_ERROR
;
1046 if (CMD_ARGC
== 1) {
1048 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], enable
);
1049 jtag_set_verify_capture_ir(enable
);
1052 const char *status
= jtag_will_verify_capture_ir() ? "enabled" : "disabled";
1053 command_print(CMD
, "verify Capture-IR is %s", status
);
1058 COMMAND_HANDLER(handle_verify_jtag_command
)
1061 return ERROR_COMMAND_SYNTAX_ERROR
;
1063 if (CMD_ARGC
== 1) {
1065 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], enable
);
1066 jtag_set_verify(enable
);
1069 const char *status
= jtag_will_verify() ? "enabled" : "disabled";
1070 command_print(CMD
, "verify jtag capture is %s", status
);
1075 COMMAND_HANDLER(handle_tms_sequence_command
)
1078 return ERROR_COMMAND_SYNTAX_ERROR
;
1080 if (CMD_ARGC
== 1) {
1082 if (strcmp(CMD_ARGV
[0], "short") == 0)
1083 use_new_table
= true;
1084 else if (strcmp(CMD_ARGV
[0], "long") == 0)
1085 use_new_table
= false;
1087 return ERROR_COMMAND_SYNTAX_ERROR
;
1089 tap_use_new_tms_table(use_new_table
);
1092 command_print(CMD
, "tms sequence is %s",
1093 tap_uses_new_tms_table() ? "short" : "long");
1098 COMMAND_HANDLER(handle_jtag_flush_queue_sleep
)
1101 return ERROR_COMMAND_SYNTAX_ERROR
;
1104 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], sleep_ms
);
1106 jtag_set_flush_queue_sleep(sleep_ms
);
1111 COMMAND_HANDLER(handle_wait_srst_deassert
)
1114 return ERROR_COMMAND_SYNTAX_ERROR
;
1117 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], timeout_ms
);
1118 if ((timeout_ms
<= 0) || (timeout_ms
> 100000)) {
1119 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1123 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms
);
1125 int64_t then
= timeval_ms();
1126 while (jtag_srst_asserted(&asserted_yet
) == ERROR_OK
) {
1127 if ((timeval_ms() - then
) > timeout_ms
) {
1128 LOG_ERROR("Timed out");
1134 while (jtag_srst_asserted(&asserted_yet
) == ERROR_OK
) {
1135 if ((timeval_ms() - then
) > timeout_ms
) {
1136 LOG_ERROR("Timed out");
1146 static const struct command_registration jtag_command_handlers
[] = {
1149 .name
= "jtag_flush_queue_sleep",
1150 .handler
= handle_jtag_flush_queue_sleep
,
1151 .mode
= COMMAND_ANY
,
1152 .help
= "For debug purposes(simulate long delays of interface) "
1153 "to test performance or change in behavior. Default 0ms.",
1154 .usage
= "[sleep in ms]",
1157 .name
= "jtag_rclk",
1158 .handler
= handle_jtag_rclk_command
,
1159 .mode
= COMMAND_ANY
,
1160 .help
= "With an argument, change to to use adaptive clocking "
1161 "if possible; else to use the fallback speed. "
1162 "With or without argument, display current setting.",
1163 .usage
= "[fallback_speed_khz]",
1166 .name
= "jtag_ntrst_delay",
1167 .handler
= handle_jtag_ntrst_delay_command
,
1168 .mode
= COMMAND_ANY
,
1169 .help
= "delay after deasserting trst in ms",
1170 .usage
= "[milliseconds]",
1173 .name
= "jtag_ntrst_assert_width",
1174 .handler
= handle_jtag_ntrst_assert_width_command
,
1175 .mode
= COMMAND_ANY
,
1176 .help
= "delay after asserting trst in ms",
1177 .usage
= "[milliseconds]",
1180 .name
= "scan_chain",
1181 .handler
= handle_scan_chain_command
,
1182 .mode
= COMMAND_ANY
,
1183 .help
= "print current scan chain configuration",
1188 .handler
= handle_runtest_command
,
1189 .mode
= COMMAND_EXEC
,
1190 .help
= "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1191 .usage
= "num_cycles"
1195 .handler
= handle_irscan_command
,
1196 .mode
= COMMAND_EXEC
,
1197 .help
= "Execute Instruction Register (IR) scan. The "
1198 "specified opcodes are put into each TAP's IR, "
1199 "and other TAPs are put in BYPASS.",
1200 .usage
= "[tap_name instruction]* ['-endstate' state_name]",
1203 .name
= "verify_ircapture",
1204 .handler
= handle_verify_ircapture_command
,
1205 .mode
= COMMAND_ANY
,
1206 .help
= "Display or assign flag controlling whether to "
1207 "verify values captured during Capture-IR.",
1208 .usage
= "['enable'|'disable']",
1211 .name
= "verify_jtag",
1212 .handler
= handle_verify_jtag_command
,
1213 .mode
= COMMAND_ANY
,
1214 .help
= "Display or assign flag controlling whether to "
1215 "verify values captured during IR and DR scans.",
1216 .usage
= "['enable'|'disable']",
1219 .name
= "tms_sequence",
1220 .handler
= handle_tms_sequence_command
,
1221 .mode
= COMMAND_ANY
,
1222 .help
= "Display or change what style TMS sequences to use "
1223 "for JTAG state transitions: short (default) or "
1224 "long. Only for working around JTAG bugs.",
1225 /* Specifically for working around DRIVER bugs... */
1226 .usage
= "['short'|'long']",
1229 .name
= "wait_srst_deassert",
1230 .handler
= handle_wait_srst_deassert
,
1231 .mode
= COMMAND_ANY
,
1232 .help
= "Wait for an SRST deassert. "
1233 "Useful for cases where you need something to happen within ms "
1234 "of an srst deassert. Timeout in ms",
1239 .mode
= COMMAND_ANY
,
1240 .help
= "perform jtag tap actions",
1243 .chain
= jtag_subcommand_handlers
,
1246 .chain
= jtag_command_handlers_to_move
,
1248 COMMAND_REGISTRATION_DONE
1251 int jtag_register_commands(struct command_context
*cmd_ctx
)
1253 return register_commands(cmd_ctx
, NULL
, jtag_command_handlers
);