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 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");
91 fields
[field_count
].out_value
= t
;
92 int ret
= CALL_COMMAND_HANDLER(command_parse_str_to_buf
, CMD_ARGV
[i
+ 1], t
, bits
);
95 fields
[field_count
].in_value
= t
;
102 COMMAND_HANDLER(handle_jtag_command_drscan
)
105 * CMD_ARGV[0] = device
106 * CMD_ARGV[1] = num_bits
107 * CMD_ARGV[2] = hex string
108 * ... repeat num bits and hex string ...
111 * CMD_ARGV[CMD_ARGC-2] = "-endstate"
112 * CMD_ARGV[CMD_ARGC-1] = statename
115 if (CMD_ARGC
< 3 || (CMD_ARGC
% 2) != 1)
116 return ERROR_COMMAND_SYNTAX_ERROR
;
118 struct jtag_tap
*tap
= jtag_tap_by_string(CMD_ARGV
[0]);
120 command_print(CMD
, "Tap '%s' could not be found", CMD_ARGV
[0]);
121 return ERROR_COMMAND_ARGUMENT_INVALID
;
125 command_print(CMD
, "Can't execute as the selected tap is in BYPASS");
129 tap_state_t endstate
= TAP_IDLE
;
130 if (CMD_ARGC
> 3 && !strcmp("-endstate", CMD_ARGV
[CMD_ARGC
- 2])) {
131 const char *state_name
= CMD_ARGV
[CMD_ARGC
- 1];
132 endstate
= tap_state_by_name(state_name
);
134 command_print(CMD
, "endstate: %s invalid", state_name
);
135 return ERROR_COMMAND_ARGUMENT_INVALID
;
138 if (!scan_is_safe(endstate
))
139 LOG_WARNING("drscan with unsafe endstate \"%s\"", state_name
);
144 unsigned int num_fields
= (CMD_ARGC
- 1) / 2;
145 struct scan_field
*fields
= calloc(num_fields
, sizeof(struct scan_field
));
147 LOG_ERROR("Out of memory");
151 int retval
= CALL_COMMAND_HANDLER(handle_jtag_command_drscan_fields
, fields
);
152 if (retval
!= ERROR_OK
)
155 jtag_add_dr_scan(tap
, num_fields
, fields
, endstate
);
157 retval
= jtag_execute_queue();
158 if (retval
!= ERROR_OK
) {
159 command_print(CMD
, "drscan: jtag execute failed");
163 for (unsigned int i
= 0; i
< num_fields
; i
++) {
164 char *str
= buf_to_hex_str(fields
[i
].in_value
, fields
[i
].num_bits
);
165 command_print(CMD
, "%s", str
);
170 for (unsigned int i
= 0; i
< num_fields
; i
++)
171 free(fields
[i
].in_value
);
177 COMMAND_HANDLER(handle_jtag_command_pathmove
)
179 tap_state_t states
[8];
181 if (CMD_ARGC
< 1 || CMD_ARGC
> ARRAY_SIZE(states
))
182 return ERROR_COMMAND_SYNTAX_ERROR
;
184 for (unsigned int i
= 0; i
< CMD_ARGC
; i
++) {
185 states
[i
] = tap_state_by_name(CMD_ARGV
[i
]);
187 command_print(CMD
, "endstate: %s invalid", CMD_ARGV
[i
]);
188 return ERROR_COMMAND_ARGUMENT_INVALID
;
192 int retval
= jtag_add_statemove(states
[0]);
193 if (retval
== ERROR_OK
)
194 retval
= jtag_execute_queue();
195 if (retval
!= ERROR_OK
) {
196 command_print(CMD
, "pathmove: jtag execute failed");
200 jtag_add_pathmove(CMD_ARGC
- 1, states
+ 1);
201 retval
= jtag_execute_queue();
202 if (retval
!= ERROR_OK
) {
203 command_print(CMD
, "pathmove: failed");
210 COMMAND_HANDLER(handle_jtag_flush_count
)
213 return ERROR_COMMAND_SYNTAX_ERROR
;
215 const unsigned int count
= jtag_get_flush_queue_count();
216 command_print_sameline(CMD
, "%u", count
);
221 /* REVISIT Just what about these should "move" ... ?
222 * These registrations, into the main JTAG table?
224 * There's a minor compatibility issue, these all show up twice;
225 * that's not desirable:
226 * - jtag drscan ... NOT DOCUMENTED!
229 * The "irscan" command (for example) doesn't show twice.
231 static const struct command_registration jtag_command_handlers_to_move
[] = {
234 .mode
= COMMAND_EXEC
,
235 .handler
= handle_jtag_command_drscan
,
236 .help
= "Execute Data Register (DR) scan for one TAP. "
237 "Other TAPs must be in BYPASS mode.",
238 .usage
= "tap_name (num_bits value)+ ['-endstate' state_name]",
241 .name
= "flush_count",
242 .mode
= COMMAND_EXEC
,
243 .handler
= handle_jtag_flush_count
,
244 .help
= "Returns the number of times the JTAG queue "
250 .mode
= COMMAND_EXEC
,
251 .handler
= handle_jtag_command_pathmove
,
252 .usage
= "start_state state1 [state2 [state3 ...]]",
253 .help
= "Move JTAG state machine from current state "
254 "(start_state) to state1, then state2, state3, etc.",
256 COMMAND_REGISTRATION_DONE
260 enum jtag_tap_cfg_param
{
265 static struct nvp nvp_config_opts
[] = {
266 { .name
= "-event", .value
= JCFG_EVENT
},
267 { .name
= "-idcode", .value
= JCFG_IDCODE
},
269 { .name
= NULL
, .value
= -1 }
272 static int jtag_tap_set_event(struct command_context
*cmd_ctx
, struct jtag_tap
*tap
,
273 const struct nvp
*event
, Jim_Obj
*body
)
275 struct jtag_tap_event_action
*jteap
= tap
->event_action
;
278 if (jteap
->event
== (enum jtag_event
)event
->value
)
284 jteap
= calloc(1, sizeof(*jteap
));
286 LOG_ERROR("Out of memory");
290 /* add to head of event list */
291 jteap
->next
= tap
->event_action
;
292 tap
->event_action
= jteap
;
294 Jim_DecrRefCount(cmd_ctx
->interp
, jteap
->body
);
297 jteap
->interp
= cmd_ctx
->interp
;
298 jteap
->event
= (enum jtag_event
)event
->value
;
299 jteap
->body
= Jim_DuplicateObj(cmd_ctx
->interp
, body
);
300 Jim_IncrRefCount(jteap
->body
);
305 __COMMAND_HANDLER(handle_jtag_configure
)
307 bool is_configure
= !strcmp(CMD_NAME
, "configure");
309 if (CMD_ARGC
< (is_configure
? 3 : 2))
310 return ERROR_COMMAND_SYNTAX_ERROR
;
312 /* FIXME: rework jtag_tap_by_jim_obj */
313 struct jtag_tap
*tap
= jtag_tap_by_jim_obj(CMD_CTX
->interp
, CMD_JIMTCL_ARGV
[0]);
317 for (unsigned int i
= 1; i
< CMD_ARGC
; i
++) {
318 const struct nvp
*n
= nvp_name2value(nvp_config_opts
, CMD_ARGV
[i
]);
321 if (i
+ (is_configure
? 2 : 1) >= CMD_ARGC
) {
322 command_print(CMD
, "wrong # args: should be \"-event <event-name>%s\"",
323 is_configure
? " <event-body>" : "");
324 return ERROR_COMMAND_ARGUMENT_INVALID
;
327 const struct nvp
*event
= nvp_name2value(nvp_jtag_tap_event
, CMD_ARGV
[i
+ 1]);
329 nvp_unknown_command_print(CMD
, nvp_jtag_tap_event
, CMD_ARGV
[i
], CMD_ARGV
[i
+ 1]);
330 return ERROR_COMMAND_ARGUMENT_INVALID
;
334 int retval
= jtag_tap_set_event(CMD_CTX
, tap
, event
, CMD_JIMTCL_ARGV
[i
+ 2]);
335 if (retval
!= ERROR_OK
)
338 struct jtag_tap_event_action
*jteap
= tap
->event_action
;
340 if (jteap
->event
== (enum jtag_event
)event
->value
) {
341 command_print(CMD
, "%s", Jim_GetString(jteap
->body
, NULL
));
348 i
+= is_configure
? 2 : 1;
352 command_print(CMD
, "not settable: %s", n
->name
);
353 return ERROR_COMMAND_ARGUMENT_INVALID
;
355 command_print(CMD
, "0x%08x", tap
->idcode
);
358 nvp_unknown_command_print(CMD
, nvp_config_opts
, NULL
, CMD_ARGV
[i
]);
359 return ERROR_COMMAND_ARGUMENT_INVALID
;
365 #define NTAP_OPT_IRLEN 0
366 #define NTAP_OPT_IRMASK 1
367 #define NTAP_OPT_IRCAPTURE 2
368 #define NTAP_OPT_ENABLED 3
369 #define NTAP_OPT_DISABLED 4
370 #define NTAP_OPT_EXPECTED_ID 5
371 #define NTAP_OPT_VERSION 6
372 #define NTAP_OPT_BYPASS 7
373 #define NTAP_OPT_IRBYPASS 8
375 static const struct nvp jtag_newtap_opts
[] = {
376 { .name
= "-irlen", .value
= NTAP_OPT_IRLEN
},
377 { .name
= "-irmask", .value
= NTAP_OPT_IRMASK
},
378 { .name
= "-ircapture", .value
= NTAP_OPT_IRCAPTURE
},
379 { .name
= "-enable", .value
= NTAP_OPT_ENABLED
},
380 { .name
= "-disable", .value
= NTAP_OPT_DISABLED
},
381 { .name
= "-expected-id", .value
= NTAP_OPT_EXPECTED_ID
},
382 { .name
= "-ignore-version", .value
= NTAP_OPT_VERSION
},
383 { .name
= "-ignore-bypass", .value
= NTAP_OPT_BYPASS
},
384 { .name
= "-ir-bypass", .value
= NTAP_OPT_IRBYPASS
},
385 { .name
= NULL
, .value
= -1 },
388 static COMMAND_HELPER(handle_jtag_newtap_args
, struct jtag_tap
*tap
)
390 /* we expect CHIP + TAP + OPTIONS */
392 return ERROR_COMMAND_SYNTAX_ERROR
;
394 tap
->chip
= strdup(CMD_ARGV
[0]);
395 tap
->tapname
= strdup(CMD_ARGV
[1]);
396 tap
->dotted_name
= alloc_printf("%s.%s", CMD_ARGV
[0], CMD_ARGV
[1]);
397 if (!tap
->chip
|| !tap
->tapname
|| !tap
->dotted_name
) {
398 LOG_ERROR("Out of memory");
404 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
405 tap
->chip
, tap
->tapname
, tap
->dotted_name
, CMD_ARGC
);
408 * IEEE specifies that the two LSBs of an IR scan are 01, so make
409 * that the default. The "-ircapture" and "-irmask" options are only
410 * needed to cope with nonstandard TAPs, or to specify more bits.
412 tap
->ir_capture_mask
= 0x03;
413 tap
->ir_capture_value
= 0x01;
416 const struct nvp
*n
= nvp_name2value(jtag_newtap_opts
, CMD_ARGV
[0]);
420 case NTAP_OPT_ENABLED
:
421 tap
->disabled_after_reset
= false;
424 case NTAP_OPT_DISABLED
:
425 tap
->disabled_after_reset
= true;
428 case NTAP_OPT_EXPECTED_ID
:
430 return ERROR_COMMAND_ARGUMENT_INVALID
;
432 tap
->expected_ids
= realloc(tap
->expected_ids
,
433 (tap
->expected_ids_cnt
+ 1) * sizeof(uint32_t));
434 if (!tap
->expected_ids
) {
435 LOG_ERROR("Out of memory");
440 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], id
);
443 tap
->expected_ids
[tap
->expected_ids_cnt
++] = id
;
449 return ERROR_COMMAND_ARGUMENT_INVALID
;
451 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], tap
->ir_length
);
454 if (tap
->ir_length
> (8 * sizeof(tap
->ir_capture_value
)))
455 LOG_WARNING("%s: huge IR length %u", tap
->dotted_name
, tap
->ir_length
);
458 case NTAP_OPT_IRMASK
:
460 return ERROR_COMMAND_ARGUMENT_INVALID
;
462 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], tap
->ir_capture_mask
);
465 if ((tap
->ir_capture_mask
& 3) != 3)
466 LOG_WARNING("%s: nonstandard IR mask", tap
->dotted_name
);
469 case NTAP_OPT_IRCAPTURE
:
471 return ERROR_COMMAND_ARGUMENT_INVALID
;
473 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], tap
->ir_capture_value
);
476 if ((tap
->ir_capture_value
& 3) != 1)
477 LOG_WARNING("%s: nonstandard IR value", tap
->dotted_name
);
480 case NTAP_OPT_VERSION
:
481 tap
->ignore_version
= true;
484 case NTAP_OPT_BYPASS
:
485 tap
->ignore_bypass
= true;
488 case NTAP_OPT_IRBYPASS
:
490 return ERROR_COMMAND_ARGUMENT_INVALID
;
492 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[0], tap
->ir_bypass_value
);
498 nvp_unknown_command_print(CMD
, jtag_newtap_opts
, NULL
, CMD_ARGV
[-1]);
499 return ERROR_COMMAND_ARGUMENT_INVALID
;
503 /* default is enabled-after-reset */
504 tap
->enabled
= !tap
->disabled_after_reset
;
506 if (transport_is_jtag() && tap
->ir_length
== 0) {
507 command_print(CMD
, "newtap: %s missing IR length", tap
->dotted_name
);
508 return ERROR_COMMAND_ARGUMENT_INVALID
;
514 __COMMAND_HANDLER(handle_jtag_newtap
)
516 struct jtag_tap
*tap
= calloc(1, sizeof(struct jtag_tap
));
518 LOG_ERROR("Out of memory");
522 int retval
= CALL_COMMAND_HANDLER(handle_jtag_newtap_args
, tap
);
523 if (retval
!= ERROR_OK
) {
526 free(tap
->dotted_name
);
527 free(tap
->expected_ids
);
536 static void jtag_tap_handle_event(struct jtag_tap
*tap
, enum jtag_event e
)
538 struct jtag_tap_event_action
*jteap
;
541 for (jteap
= tap
->event_action
; jteap
; jteap
= jteap
->next
) {
542 if (jteap
->event
!= e
)
545 const struct nvp
*nvp
= nvp_value2name(nvp_jtag_tap_event
, e
);
546 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
547 tap
->dotted_name
, e
, nvp
->name
,
548 Jim_GetString(jteap
->body
, NULL
));
550 retval
= Jim_EvalObj(jteap
->interp
, jteap
->body
);
551 if (retval
== JIM_RETURN
)
552 retval
= jteap
->interp
->returnCode
;
554 if (retval
!= JIM_OK
) {
555 Jim_MakeErrorMessage(jteap
->interp
);
556 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap
->interp
), NULL
));
561 case JTAG_TAP_EVENT_ENABLE
:
562 case JTAG_TAP_EVENT_DISABLE
:
563 /* NOTE: we currently assume the handlers
564 * can't fail. Right here is where we should
565 * really be verifying the scan chains ...
567 tap
->enabled
= (e
== JTAG_TAP_EVENT_ENABLE
);
568 LOG_INFO("JTAG tap: %s %s", tap
->dotted_name
,
569 tap
->enabled
? "enabled" : "disabled");
577 COMMAND_HANDLER(handle_jtag_arp_init
)
580 return ERROR_COMMAND_SYNTAX_ERROR
;
582 return jtag_init_inner(CMD_CTX
);
585 COMMAND_HANDLER(handle_jtag_arp_init_reset
)
588 return ERROR_COMMAND_SYNTAX_ERROR
;
590 if (transport_is_jtag())
591 return jtag_init_reset(CMD_CTX
);
593 if (transport_is_swd())
594 return swd_init_reset(CMD_CTX
);
599 static bool jtag_tap_enable(struct jtag_tap
*t
)
603 jtag_tap_handle_event(t
, JTAG_TAP_EVENT_ENABLE
);
607 /* FIXME add JTAG sanity checks, w/o TLR
608 * - scan chain length grew by one (this)
609 * - IDs and IR lengths are as expected
611 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE
);
614 static bool jtag_tap_disable(struct jtag_tap
*t
)
618 jtag_tap_handle_event(t
, JTAG_TAP_EVENT_DISABLE
);
622 /* FIXME add JTAG sanity checks, w/o TLR
623 * - scan chain length shrank by one (this)
624 * - IDs and IR lengths are as expected
626 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE
);
630 __COMMAND_HANDLER(handle_jtag_tap_enabler
)
633 return ERROR_COMMAND_SYNTAX_ERROR
;
635 struct jtag_tap
*t
= jtag_tap_by_string(CMD_ARGV
[0]);
637 command_print(CMD
, "Tap '%s' could not be found", CMD_ARGV
[0]);
638 return ERROR_COMMAND_ARGUMENT_INVALID
;
641 if (strcmp(CMD_NAME
, "tapisenabled") == 0) {
642 /* do nothing, just return the value */
643 } else if (strcmp(CMD_NAME
, "tapenable") == 0) {
644 if (!jtag_tap_enable(t
)) {
645 command_print(CMD
, "failed to enable tap %s", t
->dotted_name
);
648 } else if (strcmp(CMD_NAME
, "tapdisable") == 0) {
649 if (!jtag_tap_disable(t
)) {
650 command_print(CMD
, "failed to disable tap %s", t
->dotted_name
);
654 command_print(CMD
, "command '%s' unknown", CMD_NAME
);
658 command_print(CMD
, "%d", t
->enabled
? 1 : 0);
662 COMMAND_HANDLER(handle_jtag_names
)
665 return ERROR_COMMAND_SYNTAX_ERROR
;
667 for (struct jtag_tap
*tap
= jtag_all_taps(); tap
; tap
= tap
->next_tap
)
668 command_print(CMD
, "%s", tap
->dotted_name
);
673 COMMAND_HANDLER(handle_jtag_init_command
)
676 return ERROR_COMMAND_SYNTAX_ERROR
;
678 static bool jtag_initialized
;
679 if (jtag_initialized
) {
680 LOG_INFO("'jtag init' has already been called");
683 jtag_initialized
= true;
685 LOG_DEBUG("Initializing jtag devices...");
686 return jtag_init(CMD_CTX
);
689 static const struct command_registration jtag_subcommand_handlers
[] = {
693 .handler
= handle_jtag_init_command
,
694 .help
= "initialize jtag scan chain",
700 .handler
= handle_jtag_arp_init
,
701 .help
= "Validates JTAG scan chain against the list of "
702 "declared TAPs using just the four standard JTAG "
707 .name
= "arp_init-reset",
709 .handler
= handle_jtag_arp_init_reset
,
710 .help
= "Uses TRST and SRST to try resetting everything on "
711 "the JTAG scan chain, then performs 'jtag arp_init'.",
716 .mode
= COMMAND_CONFIG
,
717 .handler
= handle_jtag_newtap
,
718 .help
= "Create a new TAP instance named basename.tap_type, "
719 "and appends it to the scan chain.",
720 .usage
= "basename tap_type '-irlen' count "
721 "['-enable'|'-disable'] "
722 "['-expected_id' number] "
723 "['-ignore-version'] "
724 "['-ignore-bypass'] "
725 "['-ircapture' number] "
726 "['-ir-bypass' number] "
730 .name
= "tapisenabled",
731 .mode
= COMMAND_EXEC
,
732 .handler
= handle_jtag_tap_enabler
,
733 .help
= "Returns a Tcl boolean (0/1) indicating whether "
734 "the TAP is enabled (1) or not (0).",
739 .mode
= COMMAND_EXEC
,
740 .handler
= handle_jtag_tap_enabler
,
741 .help
= "Try to enable the specified TAP using the "
742 "'tap-enable' TAP event.",
746 .name
= "tapdisable",
747 .mode
= COMMAND_EXEC
,
748 .handler
= handle_jtag_tap_enabler
,
749 .help
= "Try to disable the specified TAP using the "
750 "'tap-disable' TAP event.",
756 .handler
= handle_jtag_configure
,
757 .help
= "Provide a Tcl handler for the specified "
759 .usage
= "tap_name '-event' event_name handler",
763 .mode
= COMMAND_EXEC
,
764 .handler
= handle_jtag_configure
,
765 .help
= "Return any Tcl handler for the specified "
766 "TAP event or the value of the IDCODE found in hardware.",
767 .usage
= "tap_name '-event' event_name | "
768 "tap_name '-idcode'",
773 .handler
= handle_jtag_names
,
774 .help
= "Returns list of all JTAG tap names.",
778 .chain
= jtag_command_handlers_to_move
,
780 COMMAND_REGISTRATION_DONE
783 void jtag_notify_event(enum jtag_event event
)
785 struct jtag_tap
*tap
;
787 for (tap
= jtag_all_taps(); tap
; tap
= tap
->next_tap
)
788 jtag_tap_handle_event(tap
, event
);
792 COMMAND_HANDLER(handle_scan_chain_command
)
794 struct jtag_tap
*tap
;
795 char expected_id
[12];
797 tap
= jtag_all_taps();
799 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
801 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
804 uint32_t expected
, expected_mask
, ii
;
806 snprintf(expected_id
, sizeof(expected_id
), "0x%08" PRIx32
,
807 (tap
->expected_ids_cnt
> 0) ? tap
->expected_ids
[0] : 0);
808 if (tap
->ignore_version
)
809 expected_id
[2] = '*';
811 expected
= buf_get_u32(tap
->expected
, 0, tap
->ir_length
);
812 expected_mask
= buf_get_u32(tap
->expected_mask
, 0, tap
->ir_length
);
815 "%2u %-18s %c 0x%08x %s %5u 0x%02x 0x%02x",
816 tap
->abs_chain_position
,
818 tap
->enabled
? 'Y' : 'n',
819 (unsigned int)(tap
->idcode
),
822 (unsigned int)(expected
),
823 (unsigned int)(expected_mask
));
825 for (ii
= 1; ii
< tap
->expected_ids_cnt
; ii
++) {
826 snprintf(expected_id
, sizeof(expected_id
), "0x%08" PRIx32
, tap
->expected_ids
[ii
]);
827 if (tap
->ignore_version
)
828 expected_id
[2] = '*';
841 COMMAND_HANDLER(handle_jtag_ntrst_delay_command
)
844 return ERROR_COMMAND_SYNTAX_ERROR
;
847 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
849 jtag_set_ntrst_delay(delay
);
851 command_print(CMD
, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
855 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command
)
858 return ERROR_COMMAND_SYNTAX_ERROR
;
861 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
863 jtag_set_ntrst_assert_width(delay
);
865 command_print(CMD
, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
869 COMMAND_HANDLER(handle_jtag_rclk_command
)
872 return ERROR_COMMAND_SYNTAX_ERROR
;
874 int retval
= ERROR_OK
;
876 unsigned int khz
= 0;
877 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], khz
);
879 retval
= adapter_config_rclk(khz
);
880 if (retval
!= ERROR_OK
)
884 int cur_khz
= adapter_get_speed_khz();
885 retval
= adapter_get_speed_readable(&cur_khz
);
886 if (retval
!= ERROR_OK
)
890 command_print(CMD
, "RCLK not supported - fallback to %d kHz", cur_khz
);
892 command_print(CMD
, "RCLK - adaptive");
897 COMMAND_HANDLER(handle_runtest_command
)
900 return ERROR_COMMAND_SYNTAX_ERROR
;
902 unsigned int num_clocks
;
903 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num_clocks
);
905 jtag_add_runtest(num_clocks
, TAP_IDLE
);
906 return jtag_execute_queue();
910 * For "irscan" or "drscan" commands, the "end" (really, "next") state
911 * should be stable ... and *NOT* a shift state, otherwise free-running
912 * jtag clocks could change the values latched by the update state.
913 * Not surprisingly, this is the same constraint as SVF; the "irscan"
914 * and "drscan" commands are a write-only subset of what SVF provides.
917 COMMAND_HANDLER(handle_irscan_command
)
920 struct scan_field
*fields
;
921 struct jtag_tap
*tap
= NULL
;
922 tap_state_t endstate
;
924 if ((CMD_ARGC
< 2) || (CMD_ARGC
% 2))
925 return ERROR_COMMAND_SYNTAX_ERROR
;
927 /* optional "-endstate" "statename" at the end of the arguments,
928 * so that e.g. IRPAUSE can let us load the data register before
929 * entering RUN/IDLE to execute the instruction we load here.
934 /* have at least one pair of numbers.
935 * is last pair the magic text? */
936 if (strcmp("-endstate", CMD_ARGV
[CMD_ARGC
- 2]) == 0) {
937 endstate
= tap_state_by_name(CMD_ARGV
[CMD_ARGC
- 1]);
938 if (endstate
== TAP_INVALID
)
939 return ERROR_COMMAND_SYNTAX_ERROR
;
940 if (!scan_is_safe(endstate
))
941 LOG_WARNING("unstable irscan endstate \"%s\"",
942 CMD_ARGV
[CMD_ARGC
- 1]);
947 int num_fields
= CMD_ARGC
/ 2;
948 if (num_fields
> 1) {
949 /* we really should be looking at plain_ir_scan if we want
950 * anything more fancy.
952 LOG_ERROR("Specify a single value for tap");
953 return ERROR_COMMAND_SYNTAX_ERROR
;
956 fields
= calloc(num_fields
, sizeof(*fields
));
959 for (i
= 0; i
< num_fields
; i
++) {
960 tap
= jtag_tap_by_string(CMD_ARGV
[i
*2]);
963 command_print(CMD
, "Tap: %s unknown", CMD_ARGV
[i
*2]);
968 retval
= parse_u64(CMD_ARGV
[i
* 2 + 1], &value
);
969 if (retval
!= ERROR_OK
)
972 unsigned int field_size
= tap
->ir_length
;
973 fields
[i
].num_bits
= field_size
;
974 uint8_t *v
= calloc(1, DIV_ROUND_UP(field_size
, 8));
976 LOG_ERROR("Out of memory");
980 buf_set_u64(v
, 0, field_size
, value
);
981 fields
[i
].out_value
= v
;
982 fields
[i
].in_value
= NULL
;
985 /* did we have an endstate? */
986 jtag_add_ir_scan(tap
, fields
, endstate
);
988 retval
= jtag_execute_queue();
991 for (i
= 0; i
< num_fields
; i
++)
992 free((void *)fields
[i
].out_value
);
999 COMMAND_HANDLER(handle_verify_ircapture_command
)
1002 return ERROR_COMMAND_SYNTAX_ERROR
;
1004 if (CMD_ARGC
== 1) {
1006 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], enable
);
1007 jtag_set_verify_capture_ir(enable
);
1010 const char *status
= jtag_will_verify_capture_ir() ? "enabled" : "disabled";
1011 command_print(CMD
, "verify Capture-IR is %s", status
);
1016 COMMAND_HANDLER(handle_verify_jtag_command
)
1019 return ERROR_COMMAND_SYNTAX_ERROR
;
1021 if (CMD_ARGC
== 1) {
1023 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], enable
);
1024 jtag_set_verify(enable
);
1027 const char *status
= jtag_will_verify() ? "enabled" : "disabled";
1028 command_print(CMD
, "verify jtag capture is %s", status
);
1033 COMMAND_HANDLER(handle_tms_sequence_command
)
1036 return ERROR_COMMAND_SYNTAX_ERROR
;
1038 if (CMD_ARGC
== 1) {
1040 if (strcmp(CMD_ARGV
[0], "short") == 0)
1041 use_new_table
= true;
1042 else if (strcmp(CMD_ARGV
[0], "long") == 0)
1043 use_new_table
= false;
1045 return ERROR_COMMAND_SYNTAX_ERROR
;
1047 tap_use_new_tms_table(use_new_table
);
1050 command_print(CMD
, "tms sequence is %s",
1051 tap_uses_new_tms_table() ? "short" : "long");
1056 COMMAND_HANDLER(handle_jtag_flush_queue_sleep
)
1059 return ERROR_COMMAND_SYNTAX_ERROR
;
1062 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], sleep_ms
);
1064 jtag_set_flush_queue_sleep(sleep_ms
);
1069 COMMAND_HANDLER(handle_wait_srst_deassert
)
1072 return ERROR_COMMAND_SYNTAX_ERROR
;
1075 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], timeout_ms
);
1076 if ((timeout_ms
<= 0) || (timeout_ms
> 100000)) {
1077 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1081 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms
);
1083 int64_t then
= timeval_ms();
1084 while (jtag_srst_asserted(&asserted_yet
) == ERROR_OK
) {
1085 if ((timeval_ms() - then
) > timeout_ms
) {
1086 LOG_ERROR("Timed out");
1092 while (jtag_srst_asserted(&asserted_yet
) == ERROR_OK
) {
1093 if ((timeval_ms() - then
) > timeout_ms
) {
1094 LOG_ERROR("Timed out");
1104 static const struct command_registration jtag_command_handlers
[] = {
1107 .name
= "jtag_flush_queue_sleep",
1108 .handler
= handle_jtag_flush_queue_sleep
,
1109 .mode
= COMMAND_ANY
,
1110 .help
= "For debug purposes(simulate long delays of interface) "
1111 "to test performance or change in behavior. Default 0ms.",
1112 .usage
= "[sleep in ms]",
1115 .name
= "jtag_rclk",
1116 .handler
= handle_jtag_rclk_command
,
1117 .mode
= COMMAND_ANY
,
1118 .help
= "With an argument, change to to use adaptive clocking "
1119 "if possible; else to use the fallback speed. "
1120 "With or without argument, display current setting.",
1121 .usage
= "[fallback_speed_khz]",
1124 .name
= "jtag_ntrst_delay",
1125 .handler
= handle_jtag_ntrst_delay_command
,
1126 .mode
= COMMAND_ANY
,
1127 .help
= "delay after deasserting trst in ms",
1128 .usage
= "[milliseconds]",
1131 .name
= "jtag_ntrst_assert_width",
1132 .handler
= handle_jtag_ntrst_assert_width_command
,
1133 .mode
= COMMAND_ANY
,
1134 .help
= "delay after asserting trst in ms",
1135 .usage
= "[milliseconds]",
1138 .name
= "scan_chain",
1139 .handler
= handle_scan_chain_command
,
1140 .mode
= COMMAND_ANY
,
1141 .help
= "print current scan chain configuration",
1146 .handler
= handle_runtest_command
,
1147 .mode
= COMMAND_EXEC
,
1148 .help
= "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1149 .usage
= "num_cycles"
1153 .handler
= handle_irscan_command
,
1154 .mode
= COMMAND_EXEC
,
1155 .help
= "Execute Instruction Register (IR) scan. The "
1156 "specified opcodes are put into each TAP's IR, "
1157 "and other TAPs are put in BYPASS.",
1158 .usage
= "[tap_name instruction]* ['-endstate' state_name]",
1161 .name
= "verify_ircapture",
1162 .handler
= handle_verify_ircapture_command
,
1163 .mode
= COMMAND_ANY
,
1164 .help
= "Display or assign flag controlling whether to "
1165 "verify values captured during Capture-IR.",
1166 .usage
= "['enable'|'disable']",
1169 .name
= "verify_jtag",
1170 .handler
= handle_verify_jtag_command
,
1171 .mode
= COMMAND_ANY
,
1172 .help
= "Display or assign flag controlling whether to "
1173 "verify values captured during IR and DR scans.",
1174 .usage
= "['enable'|'disable']",
1177 .name
= "tms_sequence",
1178 .handler
= handle_tms_sequence_command
,
1179 .mode
= COMMAND_ANY
,
1180 .help
= "Display or change what style TMS sequences to use "
1181 "for JTAG state transitions: short (default) or "
1182 "long. Only for working around JTAG bugs.",
1183 /* Specifically for working around DRIVER bugs... */
1184 .usage
= "['short'|'long']",
1187 .name
= "wait_srst_deassert",
1188 .handler
= handle_wait_srst_deassert
,
1189 .mode
= COMMAND_ANY
,
1190 .help
= "Wait for an SRST deassert. "
1191 "Useful for cases where you need something to happen within ms "
1192 "of an srst deassert. Timeout in ms",
1197 .mode
= COMMAND_ANY
,
1198 .help
= "perform jtag tap actions",
1201 .chain
= jtag_subcommand_handlers
,
1204 .chain
= jtag_command_handlers_to_move
,
1206 COMMAND_REGISTRATION_DONE
1209 int jtag_register_commands(struct command_context
*cmd_ctx
)
1211 return register_commands(cmd_ctx
, NULL
, jtag_command_handlers
);