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/time_support.h>
35 #include "transport/transport.h"
39 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
42 static const struct jim_nvp nvp_jtag_tap_event
[] = {
43 { .value
= JTAG_TRST_ASSERTED
, .name
= "post-reset" },
44 { .value
= JTAG_TAP_EVENT_SETUP
, .name
= "setup" },
45 { .value
= JTAG_TAP_EVENT_ENABLE
, .name
= "tap-enable" },
46 { .value
= JTAG_TAP_EVENT_DISABLE
, .name
= "tap-disable" },
48 { .name
= NULL
, .value
= -1 }
51 struct jtag_tap
*jtag_tap_by_jim_obj(Jim_Interp
*interp
, Jim_Obj
*o
)
53 const char *cp
= Jim_GetString(o
, NULL
);
54 struct jtag_tap
*t
= cp
? jtag_tap_by_string(cp
) : NULL
;
58 Jim_SetResultFormatted(interp
, "Tap '%s' could not be found", cp
);
62 static bool scan_is_safe(tap_state_t state
)
75 static COMMAND_HELPER(handle_jtag_command_drscan_fields
, struct scan_field
*fields
)
77 unsigned int field_count
= 0;
78 for (unsigned int i
= 1; i
< CMD_ARGC
; i
+= 2) {
80 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[i
], bits
);
81 fields
[field_count
].num_bits
= bits
;
83 void *t
= malloc(DIV_ROUND_UP(bits
, 8));
85 LOG_ERROR("Out of memory");
88 fields
[field_count
].out_value
= t
;
89 str_to_buf(CMD_ARGV
[i
+ 1], strlen(CMD_ARGV
[i
+ 1]), t
, bits
, 0);
90 fields
[field_count
].in_value
= t
;
97 COMMAND_HANDLER(handle_jtag_command_drscan
)
100 * CMD_ARGV[0] = device
101 * CMD_ARGV[1] = num_bits
102 * CMD_ARGV[2] = hex string
103 * ... repeat num bits and hex string ...
106 * CMD_ARGV[CMD_ARGC-2] = "-endstate"
107 * CMD_ARGV[CMD_ARGC-1] = statename
110 if (CMD_ARGC
< 3 || (CMD_ARGC
% 2) != 1)
111 return ERROR_COMMAND_SYNTAX_ERROR
;
113 struct jtag_tap
*tap
= jtag_tap_by_string(CMD_ARGV
[0]);
115 command_print(CMD
, "Tap '%s' could not be found", CMD_ARGV
[0]);
116 return ERROR_COMMAND_ARGUMENT_INVALID
;
119 tap_state_t endstate
= TAP_IDLE
;
120 if (CMD_ARGC
> 3 && !strcmp("-endstate", CMD_ARGV
[CMD_ARGC
- 2])) {
121 const char *state_name
= CMD_ARGV
[CMD_ARGC
- 1];
122 endstate
= tap_state_by_name(state_name
);
124 command_print(CMD
, "endstate: %s invalid", state_name
);
125 return ERROR_COMMAND_ARGUMENT_INVALID
;
128 if (!scan_is_safe(endstate
))
129 LOG_WARNING("drscan with unsafe endstate \"%s\"", state_name
);
134 unsigned int num_fields
= (CMD_ARGC
- 1) / 2;
135 struct scan_field
*fields
= calloc(num_fields
, sizeof(struct scan_field
));
137 LOG_ERROR("Out of memory");
141 int retval
= CALL_COMMAND_HANDLER(handle_jtag_command_drscan_fields
, fields
);
142 if (retval
!= ERROR_OK
)
145 jtag_add_dr_scan(tap
, num_fields
, fields
, endstate
);
147 retval
= jtag_execute_queue();
148 if (retval
!= ERROR_OK
) {
149 command_print(CMD
, "drscan: jtag execute failed");
153 for (unsigned int i
= 0; i
< num_fields
; i
++) {
154 char *str
= buf_to_hex_str(fields
[i
].in_value
, fields
[i
].num_bits
);
155 command_print(CMD
, "%s", str
);
160 for (unsigned int i
= 0; i
< num_fields
; i
++)
161 free(fields
[i
].in_value
);
167 COMMAND_HANDLER(handle_jtag_command_pathmove
)
169 tap_state_t states
[8];
171 if (CMD_ARGC
< 1 || CMD_ARGC
> ARRAY_SIZE(states
))
172 return ERROR_COMMAND_SYNTAX_ERROR
;
174 for (unsigned int i
= 0; i
< CMD_ARGC
; i
++) {
175 states
[i
] = tap_state_by_name(CMD_ARGV
[i
]);
177 command_print(CMD
, "endstate: %s invalid", CMD_ARGV
[i
]);
178 return ERROR_COMMAND_ARGUMENT_INVALID
;
182 int retval
= jtag_add_statemove(states
[0]);
183 if (retval
== ERROR_OK
)
184 retval
= jtag_execute_queue();
185 if (retval
!= ERROR_OK
) {
186 command_print(CMD
, "pathmove: jtag execute failed");
190 jtag_add_pathmove(CMD_ARGC
- 1, states
+ 1);
191 retval
= jtag_execute_queue();
192 if (retval
!= ERROR_OK
) {
193 command_print(CMD
, "pathmove: failed");
200 COMMAND_HANDLER(handle_jtag_flush_count
)
203 return ERROR_COMMAND_SYNTAX_ERROR
;
205 int count
= jtag_get_flush_queue_count();
206 command_print_sameline(CMD
, "%d", count
);
211 /* REVISIT Just what about these should "move" ... ?
212 * These registrations, into the main JTAG table?
214 * There's a minor compatibility issue, these all show up twice;
215 * that's not desirable:
216 * - jtag drscan ... NOT DOCUMENTED!
219 * The "irscan" command (for example) doesn't show twice.
221 static const struct command_registration jtag_command_handlers_to_move
[] = {
224 .mode
= COMMAND_EXEC
,
225 .handler
= handle_jtag_command_drscan
,
226 .help
= "Execute Data Register (DR) scan for one TAP. "
227 "Other TAPs must be in BYPASS mode.",
228 .usage
= "tap_name [num_bits value]* ['-endstate' state_name]",
231 .name
= "flush_count",
232 .mode
= COMMAND_EXEC
,
233 .handler
= handle_jtag_flush_count
,
234 .help
= "Returns the number of times the JTAG queue "
240 .mode
= COMMAND_EXEC
,
241 .handler
= handle_jtag_command_pathmove
,
242 .usage
= "start_state state1 [state2 [state3 ...]]",
243 .help
= "Move JTAG state machine from current state "
244 "(start_state) to state1, then state2, state3, etc.",
246 COMMAND_REGISTRATION_DONE
250 enum jtag_tap_cfg_param
{
255 static struct jim_nvp nvp_config_opts
[] = {
256 { .name
= "-event", .value
= JCFG_EVENT
},
257 { .name
= "-idcode", .value
= JCFG_IDCODE
},
259 { .name
= NULL
, .value
= -1 }
262 static int jtag_tap_configure_event(struct jim_getopt_info
*goi
, struct jtag_tap
*tap
)
264 if (goi
->argc
== 0) {
265 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event <event-name> ...");
270 int e
= jim_getopt_nvp(goi
, nvp_jtag_tap_event
, &n
);
272 jim_getopt_nvp_unknown(goi
, nvp_jtag_tap_event
, 1);
276 if (goi
->isconfigure
) {
277 if (goi
->argc
!= 1) {
278 Jim_WrongNumArgs(goi
->interp
,
281 "-event <event-name> <event-body>");
285 if (goi
->argc
!= 0) {
286 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event <event-name>");
291 struct jtag_tap_event_action
*jteap
= tap
->event_action
;
292 /* replace existing event body */
295 if (jteap
->event
== (enum jtag_event
)n
->value
) {
302 Jim_SetEmptyResult(goi
->interp
);
304 if (goi
->isconfigure
) {
306 jteap
= calloc(1, sizeof(*jteap
));
307 else if (jteap
->body
)
308 Jim_DecrRefCount(goi
->interp
, jteap
->body
);
310 jteap
->interp
= goi
->interp
;
311 jteap
->event
= n
->value
;
314 jim_getopt_obj(goi
, &o
);
315 jteap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
316 Jim_IncrRefCount(jteap
->body
);
319 /* add to head of event list */
320 jteap
->next
= tap
->event_action
;
321 tap
->event_action
= jteap
;
324 jteap
->interp
= goi
->interp
;
325 Jim_SetResult(goi
->interp
,
326 Jim_DuplicateObj(goi
->interp
, jteap
->body
));
331 static int jtag_tap_configure_cmd(struct jim_getopt_info
*goi
, struct jtag_tap
*tap
)
333 /* parse config or cget options */
334 while (goi
->argc
> 0) {
335 Jim_SetEmptyResult(goi
->interp
);
338 int e
= jim_getopt_nvp(goi
, nvp_config_opts
, &n
);
340 jim_getopt_nvp_unknown(goi
, nvp_config_opts
, 0);
346 e
= jtag_tap_configure_event(goi
, tap
);
351 if (goi
->isconfigure
) {
352 Jim_SetResultFormatted(goi
->interp
,
353 "not settable: %s", n
->name
);
356 if (goi
->argc
!= 0) {
357 Jim_WrongNumArgs(goi
->interp
,
358 goi
->argc
, goi
->argv
,
363 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, tap
->idcode
));
366 Jim_SetResultFormatted(goi
->interp
, "unknown value: %s", n
->name
);
374 static int is_bad_irval(int ir_length
, jim_wide w
)
384 static int jim_newtap_expected_id(struct jim_nvp
*n
, struct jim_getopt_info
*goi
,
385 struct jtag_tap
*tap
)
388 int e
= jim_getopt_wide(goi
, &w
);
390 Jim_SetResultFormatted(goi
->interp
, "option: %s bad parameter", n
->name
);
394 uint32_t *p
= realloc(tap
->expected_ids
,
395 (tap
->expected_ids_cnt
+ 1) * sizeof(uint32_t));
397 Jim_SetResultFormatted(goi
->interp
, "no memory");
401 tap
->expected_ids
= p
;
402 tap
->expected_ids
[tap
->expected_ids_cnt
++] = w
;
407 #define NTAP_OPT_IRLEN 0
408 #define NTAP_OPT_IRMASK 1
409 #define NTAP_OPT_IRCAPTURE 2
410 #define NTAP_OPT_ENABLED 3
411 #define NTAP_OPT_DISABLED 4
412 #define NTAP_OPT_EXPECTED_ID 5
413 #define NTAP_OPT_VERSION 6
414 #define NTAP_OPT_BYPASS 7
416 static int jim_newtap_ir_param(struct jim_nvp
*n
, struct jim_getopt_info
*goi
,
417 struct jtag_tap
*tap
)
420 int e
= jim_getopt_wide(goi
, &w
);
422 Jim_SetResultFormatted(goi
->interp
,
423 "option: %s bad parameter", n
->name
);
428 if (w
> (jim_wide
) (8 * sizeof(tap
->ir_capture_value
))) {
429 LOG_WARNING("%s: huge IR length %d",
430 tap
->dotted_name
, (int) w
);
434 case NTAP_OPT_IRMASK
:
435 if (is_bad_irval(tap
->ir_length
, w
)) {
436 LOG_ERROR("%s: IR mask %x too big",
442 LOG_WARNING("%s: nonstandard IR mask", tap
->dotted_name
);
443 tap
->ir_capture_mask
= w
;
445 case NTAP_OPT_IRCAPTURE
:
446 if (is_bad_irval(tap
->ir_length
, w
)) {
447 LOG_ERROR("%s: IR capture %x too big",
448 tap
->dotted_name
, (int) w
);
452 LOG_WARNING("%s: nonstandard IR value",
454 tap
->ir_capture_value
= w
;
462 static int jim_newtap_cmd(struct jim_getopt_info
*goi
)
464 struct jtag_tap
*tap
;
469 const struct jim_nvp opts
[] = {
470 { .name
= "-irlen", .value
= NTAP_OPT_IRLEN
},
471 { .name
= "-irmask", .value
= NTAP_OPT_IRMASK
},
472 { .name
= "-ircapture", .value
= NTAP_OPT_IRCAPTURE
},
473 { .name
= "-enable", .value
= NTAP_OPT_ENABLED
},
474 { .name
= "-disable", .value
= NTAP_OPT_DISABLED
},
475 { .name
= "-expected-id", .value
= NTAP_OPT_EXPECTED_ID
},
476 { .name
= "-ignore-version", .value
= NTAP_OPT_VERSION
},
477 { .name
= "-ignore-bypass", .value
= NTAP_OPT_BYPASS
},
478 { .name
= NULL
, .value
= -1 },
481 tap
= calloc(1, sizeof(struct jtag_tap
));
483 Jim_SetResultFormatted(goi
->interp
, "no memory");
488 * we expect CHIP + TAP + OPTIONS
491 Jim_SetResultFormatted(goi
->interp
, "Missing CHIP TAP OPTIONS ....");
497 jim_getopt_string(goi
, &tmp
, NULL
);
498 tap
->chip
= strdup(tmp
);
500 jim_getopt_string(goi
, &tmp
, NULL
);
501 tap
->tapname
= strdup(tmp
);
503 /* name + dot + name + null */
504 x
= strlen(tap
->chip
) + 1 + strlen(tap
->tapname
) + 1;
506 sprintf(cp
, "%s.%s", tap
->chip
, tap
->tapname
);
507 tap
->dotted_name
= cp
;
509 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
510 tap
->chip
, tap
->tapname
, tap
->dotted_name
, goi
->argc
);
512 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
513 * that the default. The "-ircapture" and "-irmask" options are only
514 * needed to cope with nonstandard TAPs, or to specify more bits.
516 tap
->ir_capture_mask
= 0x03;
517 tap
->ir_capture_value
= 0x01;
520 e
= jim_getopt_nvp(goi
, opts
, &n
);
522 jim_getopt_nvp_unknown(goi
, opts
, 0);
527 LOG_DEBUG("Processing option: %s", n
->name
);
529 case NTAP_OPT_ENABLED
:
530 tap
->disabled_after_reset
= false;
532 case NTAP_OPT_DISABLED
:
533 tap
->disabled_after_reset
= true;
535 case NTAP_OPT_EXPECTED_ID
:
536 e
= jim_newtap_expected_id(n
, goi
, tap
);
544 case NTAP_OPT_IRMASK
:
545 case NTAP_OPT_IRCAPTURE
:
546 e
= jim_newtap_ir_param(n
, goi
, tap
);
553 case NTAP_OPT_VERSION
:
554 tap
->ignore_version
= true;
556 case NTAP_OPT_BYPASS
:
557 tap
->ignore_bypass
= true;
559 } /* switch (n->value) */
560 } /* while (goi->argc) */
562 /* default is enabled-after-reset */
563 tap
->enabled
= !tap
->disabled_after_reset
;
565 /* Did all the required option bits get cleared? */
566 if (!transport_is_jtag() || tap
->ir_length
!= 0) {
571 Jim_SetResultFormatted(goi
->interp
,
572 "newtap: %s missing IR length",
578 static void jtag_tap_handle_event(struct jtag_tap
*tap
, enum jtag_event e
)
580 struct jtag_tap_event_action
*jteap
;
583 for (jteap
= tap
->event_action
; jteap
; jteap
= jteap
->next
) {
584 if (jteap
->event
!= e
)
587 struct jim_nvp
*nvp
= jim_nvp_value2name_simple(nvp_jtag_tap_event
, e
);
588 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
589 tap
->dotted_name
, e
, nvp
->name
,
590 Jim_GetString(jteap
->body
, NULL
));
592 retval
= Jim_EvalObj(jteap
->interp
, jteap
->body
);
593 if (retval
== JIM_RETURN
)
594 retval
= jteap
->interp
->returnCode
;
596 if (retval
!= JIM_OK
) {
597 Jim_MakeErrorMessage(jteap
->interp
);
598 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap
->interp
), NULL
));
603 case JTAG_TAP_EVENT_ENABLE
:
604 case JTAG_TAP_EVENT_DISABLE
:
605 /* NOTE: we currently assume the handlers
606 * can't fail. Right here is where we should
607 * really be verifying the scan chains ...
609 tap
->enabled
= (e
== JTAG_TAP_EVENT_ENABLE
);
610 LOG_INFO("JTAG tap: %s %s", tap
->dotted_name
,
611 tap
->enabled
? "enabled" : "disabled");
619 COMMAND_HANDLER(handle_jtag_arp_init
)
622 return ERROR_COMMAND_SYNTAX_ERROR
;
624 return jtag_init_inner(CMD_CTX
);
627 COMMAND_HANDLER(handle_jtag_arp_init_reset
)
630 return ERROR_COMMAND_SYNTAX_ERROR
;
632 if (transport_is_jtag())
633 return jtag_init_reset(CMD_CTX
);
635 if (transport_is_swd())
636 return swd_init_reset(CMD_CTX
);
641 int jim_jtag_newtap(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
643 struct jim_getopt_info goi
;
644 jim_getopt_setup(&goi
, interp
, argc
-1, argv
+ 1);
645 return jim_newtap_cmd(&goi
);
648 static bool jtag_tap_enable(struct jtag_tap
*t
)
652 jtag_tap_handle_event(t
, JTAG_TAP_EVENT_ENABLE
);
656 /* FIXME add JTAG sanity checks, w/o TLR
657 * - scan chain length grew by one (this)
658 * - IDs and IR lengths are as expected
660 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE
);
663 static bool jtag_tap_disable(struct jtag_tap
*t
)
667 jtag_tap_handle_event(t
, JTAG_TAP_EVENT_DISABLE
);
671 /* FIXME add JTAG sanity checks, w/o TLR
672 * - scan chain length shrank by one (this)
673 * - IDs and IR lengths are as expected
675 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE
);
679 int jim_jtag_tap_enabler(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
681 struct command
*c
= jim_to_command(interp
);
682 const char *cmd_name
= c
->name
;
683 struct jim_getopt_info goi
;
684 jim_getopt_setup(&goi
, interp
, argc
-1, argv
+ 1);
686 Jim_SetResultFormatted(goi
.interp
, "usage: %s <name>", cmd_name
);
692 t
= jtag_tap_by_jim_obj(goi
.interp
, goi
.argv
[0]);
696 if (strcasecmp(cmd_name
, "tapisenabled") == 0) {
697 /* do nothing, just return the value */
698 } else if (strcasecmp(cmd_name
, "tapenable") == 0) {
699 if (!jtag_tap_enable(t
)) {
700 LOG_WARNING("failed to enable tap %s", t
->dotted_name
);
703 } else if (strcasecmp(cmd_name
, "tapdisable") == 0) {
704 if (!jtag_tap_disable(t
)) {
705 LOG_WARNING("failed to disable tap %s", t
->dotted_name
);
709 LOG_ERROR("command '%s' unknown", cmd_name
);
713 Jim_SetResult(goi
.interp
, Jim_NewIntObj(goi
.interp
, e
));
717 int jim_jtag_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
719 struct command
*c
= jim_to_command(interp
);
720 const char *cmd_name
= c
->name
;
721 struct jim_getopt_info goi
;
722 jim_getopt_setup(&goi
, interp
, argc
-1, argv
+ 1);
723 goi
.isconfigure
= !strcmp(cmd_name
, "configure");
724 if (goi
.argc
< 2 + goi
.isconfigure
) {
725 Jim_WrongNumArgs(goi
.interp
, 0, NULL
,
726 "<tap_name> <attribute> ...");
733 jim_getopt_obj(&goi
, &o
);
734 t
= jtag_tap_by_jim_obj(goi
.interp
, o
);
738 return jtag_tap_configure_cmd(&goi
, t
);
741 COMMAND_HANDLER(handle_jtag_names
)
744 return ERROR_COMMAND_SYNTAX_ERROR
;
746 for (struct jtag_tap
*tap
= jtag_all_taps(); tap
; tap
= tap
->next_tap
)
747 command_print(CMD
, "%s", tap
->dotted_name
);
752 COMMAND_HANDLER(handle_jtag_init_command
)
755 return ERROR_COMMAND_SYNTAX_ERROR
;
757 static bool jtag_initialized
;
758 if (jtag_initialized
) {
759 LOG_INFO("'jtag init' has already been called");
762 jtag_initialized
= true;
764 LOG_DEBUG("Initializing jtag devices...");
765 return jtag_init(CMD_CTX
);
768 static const struct command_registration jtag_subcommand_handlers
[] = {
772 .handler
= handle_jtag_init_command
,
773 .help
= "initialize jtag scan chain",
779 .handler
= handle_jtag_arp_init
,
780 .help
= "Validates JTAG scan chain against the list of "
781 "declared TAPs using just the four standard JTAG "
786 .name
= "arp_init-reset",
788 .handler
= handle_jtag_arp_init_reset
,
789 .help
= "Uses TRST and SRST to try resetting everything on "
790 "the JTAG scan chain, then performs 'jtag arp_init'.",
795 .mode
= COMMAND_CONFIG
,
796 .jim_handler
= jim_jtag_newtap
,
797 .help
= "Create a new TAP instance named basename.tap_type, "
798 "and appends it to the scan chain.",
799 .usage
= "basename tap_type '-irlen' count "
800 "['-enable'|'-disable'] "
801 "['-expected_id' number] "
802 "['-ignore-version'] "
803 "['-ignore-bypass'] "
804 "['-ircapture' number] "
808 .name
= "tapisenabled",
809 .mode
= COMMAND_EXEC
,
810 .jim_handler
= jim_jtag_tap_enabler
,
811 .help
= "Returns a Tcl boolean (0/1) indicating whether "
812 "the TAP is enabled (1) or not (0).",
817 .mode
= COMMAND_EXEC
,
818 .jim_handler
= jim_jtag_tap_enabler
,
819 .help
= "Try to enable the specified TAP using the "
820 "'tap-enable' TAP event.",
824 .name
= "tapdisable",
825 .mode
= COMMAND_EXEC
,
826 .jim_handler
= jim_jtag_tap_enabler
,
827 .help
= "Try to disable the specified TAP using the "
828 "'tap-disable' TAP event.",
834 .jim_handler
= jim_jtag_configure
,
835 .help
= "Provide a Tcl handler for the specified "
837 .usage
= "tap_name '-event' event_name handler",
841 .mode
= COMMAND_EXEC
,
842 .jim_handler
= jim_jtag_configure
,
843 .help
= "Return any Tcl handler for the specified "
845 .usage
= "tap_name '-event' event_name",
850 .handler
= handle_jtag_names
,
851 .help
= "Returns list of all JTAG tap names.",
855 .chain
= jtag_command_handlers_to_move
,
857 COMMAND_REGISTRATION_DONE
860 void jtag_notify_event(enum jtag_event event
)
862 struct jtag_tap
*tap
;
864 for (tap
= jtag_all_taps(); tap
; tap
= tap
->next_tap
)
865 jtag_tap_handle_event(tap
, event
);
869 COMMAND_HANDLER(handle_scan_chain_command
)
871 struct jtag_tap
*tap
;
872 char expected_id
[12];
874 tap
= jtag_all_taps();
876 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
878 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
881 uint32_t expected
, expected_mask
, ii
;
883 snprintf(expected_id
, sizeof(expected_id
), "0x%08x",
884 (unsigned)((tap
->expected_ids_cnt
> 0)
885 ? tap
->expected_ids
[0]
887 if (tap
->ignore_version
)
888 expected_id
[2] = '*';
890 expected
= buf_get_u32(tap
->expected
, 0, tap
->ir_length
);
891 expected_mask
= buf_get_u32(tap
->expected_mask
, 0, tap
->ir_length
);
894 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
895 tap
->abs_chain_position
,
897 tap
->enabled
? 'Y' : 'n',
898 (unsigned int)(tap
->idcode
),
900 (unsigned int)(tap
->ir_length
),
901 (unsigned int)(expected
),
902 (unsigned int)(expected_mask
));
904 for (ii
= 1; ii
< tap
->expected_ids_cnt
; ii
++) {
905 snprintf(expected_id
, sizeof(expected_id
), "0x%08x",
906 (unsigned) tap
->expected_ids
[ii
]);
907 if (tap
->ignore_version
)
908 expected_id
[2] = '*';
921 COMMAND_HANDLER(handle_jtag_ntrst_delay_command
)
924 return ERROR_COMMAND_SYNTAX_ERROR
;
927 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
929 jtag_set_ntrst_delay(delay
);
931 command_print(CMD
, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
935 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command
)
938 return ERROR_COMMAND_SYNTAX_ERROR
;
941 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
943 jtag_set_ntrst_assert_width(delay
);
945 command_print(CMD
, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
949 COMMAND_HANDLER(handle_jtag_rclk_command
)
952 return ERROR_COMMAND_SYNTAX_ERROR
;
954 int retval
= ERROR_OK
;
957 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], khz
);
959 retval
= adapter_config_rclk(khz
);
960 if (retval
!= ERROR_OK
)
964 int cur_khz
= adapter_get_speed_khz();
965 retval
= adapter_get_speed_readable(&cur_khz
);
966 if (retval
!= ERROR_OK
)
970 command_print(CMD
, "RCLK not supported - fallback to %d kHz", cur_khz
);
972 command_print(CMD
, "RCLK - adaptive");
977 COMMAND_HANDLER(handle_runtest_command
)
980 return ERROR_COMMAND_SYNTAX_ERROR
;
983 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num_clocks
);
985 jtag_add_runtest(num_clocks
, TAP_IDLE
);
986 return jtag_execute_queue();
990 * For "irscan" or "drscan" commands, the "end" (really, "next") state
991 * should be stable ... and *NOT* a shift state, otherwise free-running
992 * jtag clocks could change the values latched by the update state.
993 * Not surprisingly, this is the same constraint as SVF; the "irscan"
994 * and "drscan" commands are a write-only subset of what SVF provides.
997 COMMAND_HANDLER(handle_irscan_command
)
1000 struct scan_field
*fields
;
1001 struct jtag_tap
*tap
= NULL
;
1002 tap_state_t endstate
;
1004 if ((CMD_ARGC
< 2) || (CMD_ARGC
% 2))
1005 return ERROR_COMMAND_SYNTAX_ERROR
;
1007 /* optional "-endstate" "statename" at the end of the arguments,
1008 * so that e.g. IRPAUSE can let us load the data register before
1009 * entering RUN/IDLE to execute the instruction we load here.
1011 endstate
= TAP_IDLE
;
1013 if (CMD_ARGC
>= 4) {
1014 /* have at least one pair of numbers.
1015 * is last pair the magic text? */
1016 if (strcmp("-endstate", CMD_ARGV
[CMD_ARGC
- 2]) == 0) {
1017 endstate
= tap_state_by_name(CMD_ARGV
[CMD_ARGC
- 1]);
1018 if (endstate
== TAP_INVALID
)
1019 return ERROR_COMMAND_SYNTAX_ERROR
;
1020 if (!scan_is_safe(endstate
))
1021 LOG_WARNING("unstable irscan endstate \"%s\"",
1022 CMD_ARGV
[CMD_ARGC
- 1]);
1027 int num_fields
= CMD_ARGC
/ 2;
1028 if (num_fields
> 1) {
1029 /* we really should be looking at plain_ir_scan if we want
1030 * anything more fancy.
1032 LOG_ERROR("Specify a single value for tap");
1033 return ERROR_COMMAND_SYNTAX_ERROR
;
1036 fields
= calloc(num_fields
, sizeof(*fields
));
1039 for (i
= 0; i
< num_fields
; i
++) {
1040 tap
= jtag_tap_by_string(CMD_ARGV
[i
*2]);
1043 command_print(CMD
, "Tap: %s unknown", CMD_ARGV
[i
*2]);
1048 retval
= parse_u64(CMD_ARGV
[i
* 2 + 1], &value
);
1049 if (retval
!= ERROR_OK
)
1052 int field_size
= tap
->ir_length
;
1053 fields
[i
].num_bits
= field_size
;
1054 uint8_t *v
= calloc(1, DIV_ROUND_UP(field_size
, 8));
1056 LOG_ERROR("Out of memory");
1060 buf_set_u64(v
, 0, field_size
, value
);
1061 fields
[i
].out_value
= v
;
1062 fields
[i
].in_value
= NULL
;
1065 /* did we have an endstate? */
1066 jtag_add_ir_scan(tap
, fields
, endstate
);
1068 retval
= jtag_execute_queue();
1071 for (i
= 0; i
< num_fields
; i
++)
1072 free((void *)fields
[i
].out_value
);
1079 COMMAND_HANDLER(handle_verify_ircapture_command
)
1082 return ERROR_COMMAND_SYNTAX_ERROR
;
1084 if (CMD_ARGC
== 1) {
1086 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], enable
);
1087 jtag_set_verify_capture_ir(enable
);
1090 const char *status
= jtag_will_verify_capture_ir() ? "enabled" : "disabled";
1091 command_print(CMD
, "verify Capture-IR is %s", status
);
1096 COMMAND_HANDLER(handle_verify_jtag_command
)
1099 return ERROR_COMMAND_SYNTAX_ERROR
;
1101 if (CMD_ARGC
== 1) {
1103 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], enable
);
1104 jtag_set_verify(enable
);
1107 const char *status
= jtag_will_verify() ? "enabled" : "disabled";
1108 command_print(CMD
, "verify jtag capture is %s", status
);
1113 COMMAND_HANDLER(handle_tms_sequence_command
)
1116 return ERROR_COMMAND_SYNTAX_ERROR
;
1118 if (CMD_ARGC
== 1) {
1120 if (strcmp(CMD_ARGV
[0], "short") == 0)
1121 use_new_table
= true;
1122 else if (strcmp(CMD_ARGV
[0], "long") == 0)
1123 use_new_table
= false;
1125 return ERROR_COMMAND_SYNTAX_ERROR
;
1127 tap_use_new_tms_table(use_new_table
);
1130 command_print(CMD
, "tms sequence is %s",
1131 tap_uses_new_tms_table() ? "short" : "long");
1136 COMMAND_HANDLER(handle_jtag_flush_queue_sleep
)
1139 return ERROR_COMMAND_SYNTAX_ERROR
;
1142 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], sleep_ms
);
1144 jtag_set_flush_queue_sleep(sleep_ms
);
1149 COMMAND_HANDLER(handle_wait_srst_deassert
)
1152 return ERROR_COMMAND_SYNTAX_ERROR
;
1155 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], timeout_ms
);
1156 if ((timeout_ms
<= 0) || (timeout_ms
> 100000)) {
1157 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1161 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms
);
1163 int64_t then
= timeval_ms();
1164 while (jtag_srst_asserted(&asserted_yet
) == ERROR_OK
) {
1165 if ((timeval_ms() - then
) > timeout_ms
) {
1166 LOG_ERROR("Timed out");
1172 while (jtag_srst_asserted(&asserted_yet
) == ERROR_OK
) {
1173 if ((timeval_ms() - then
) > timeout_ms
) {
1174 LOG_ERROR("Timed out");
1184 static const struct command_registration jtag_command_handlers
[] = {
1187 .name
= "jtag_flush_queue_sleep",
1188 .handler
= handle_jtag_flush_queue_sleep
,
1189 .mode
= COMMAND_ANY
,
1190 .help
= "For debug purposes(simulate long delays of interface) "
1191 "to test performance or change in behavior. Default 0ms.",
1192 .usage
= "[sleep in ms]",
1195 .name
= "jtag_rclk",
1196 .handler
= handle_jtag_rclk_command
,
1197 .mode
= COMMAND_ANY
,
1198 .help
= "With an argument, change to to use adaptive clocking "
1199 "if possible; else to use the fallback speed. "
1200 "With or without argument, display current setting.",
1201 .usage
= "[fallback_speed_khz]",
1204 .name
= "jtag_ntrst_delay",
1205 .handler
= handle_jtag_ntrst_delay_command
,
1206 .mode
= COMMAND_ANY
,
1207 .help
= "delay after deasserting trst in ms",
1208 .usage
= "[milliseconds]",
1211 .name
= "jtag_ntrst_assert_width",
1212 .handler
= handle_jtag_ntrst_assert_width_command
,
1213 .mode
= COMMAND_ANY
,
1214 .help
= "delay after asserting trst in ms",
1215 .usage
= "[milliseconds]",
1218 .name
= "scan_chain",
1219 .handler
= handle_scan_chain_command
,
1220 .mode
= COMMAND_ANY
,
1221 .help
= "print current scan chain configuration",
1226 .handler
= handle_runtest_command
,
1227 .mode
= COMMAND_EXEC
,
1228 .help
= "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1229 .usage
= "num_cycles"
1233 .handler
= handle_irscan_command
,
1234 .mode
= COMMAND_EXEC
,
1235 .help
= "Execute Instruction Register (IR) scan. The "
1236 "specified opcodes are put into each TAP's IR, "
1237 "and other TAPs are put in BYPASS.",
1238 .usage
= "[tap_name instruction]* ['-endstate' state_name]",
1241 .name
= "verify_ircapture",
1242 .handler
= handle_verify_ircapture_command
,
1243 .mode
= COMMAND_ANY
,
1244 .help
= "Display or assign flag controlling whether to "
1245 "verify values captured during Capture-IR.",
1246 .usage
= "['enable'|'disable']",
1249 .name
= "verify_jtag",
1250 .handler
= handle_verify_jtag_command
,
1251 .mode
= COMMAND_ANY
,
1252 .help
= "Display or assign flag controlling whether to "
1253 "verify values captured during IR and DR scans.",
1254 .usage
= "['enable'|'disable']",
1257 .name
= "tms_sequence",
1258 .handler
= handle_tms_sequence_command
,
1259 .mode
= COMMAND_ANY
,
1260 .help
= "Display or change what style TMS sequences to use "
1261 "for JTAG state transitions: short (default) or "
1262 "long. Only for working around JTAG bugs.",
1263 /* Specifically for working around DRIVER bugs... */
1264 .usage
= "['short'|'long']",
1267 .name
= "wait_srst_deassert",
1268 .handler
= handle_wait_srst_deassert
,
1269 .mode
= COMMAND_ANY
,
1270 .help
= "Wait for an SRST deassert. "
1271 "Useful for cases where you need something to happen within ms "
1272 "of an srst deassert. Timeout in ms",
1277 .mode
= COMMAND_ANY
,
1278 .help
= "perform jtag tap actions",
1281 .chain
= jtag_subcommand_handlers
,
1284 .chain
= jtag_command_handlers_to_move
,
1286 COMMAND_REGISTRATION_DONE
1289 int jtag_register_commands(struct command_context
*cmd_ctx
)
1291 return register_commands(cmd_ctx
, NULL
, jtag_command_handlers
);