1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2009 SoftPLC Corporation *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
29 ***************************************************************************/
37 #include "minidriver.h"
38 #include "interface.h"
39 #include "interfaces.h"
46 #include <helper/time_support.h>
50 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
53 static const Jim_Nvp nvp_jtag_tap_event
[] = {
54 { .value
= JTAG_TRST_ASSERTED
, .name
= "post-reset" },
55 { .value
= JTAG_TAP_EVENT_SETUP
, .name
= "setup" },
56 { .value
= JTAG_TAP_EVENT_ENABLE
, .name
= "tap-enable" },
57 { .value
= JTAG_TAP_EVENT_DISABLE
, .name
= "tap-disable" },
59 { .name
= NULL
, .value
= -1 }
62 extern struct jtag_interface
*jtag_interface
;
64 struct jtag_tap
*jtag_tap_by_jim_obj(Jim_Interp
*interp
, Jim_Obj
*o
)
66 const char *cp
= Jim_GetString(o
, NULL
);
67 struct jtag_tap
*t
= cp
? jtag_tap_by_string(cp
) : NULL
;
71 Jim_SetResultFormatted(interp
, "Tap '%s' could not be found", cp
);
75 static bool scan_is_safe(tap_state_t state
)
88 static int Jim_Command_drscan(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *args
)
91 struct scan_field
*fields
;
100 * args[3] = hex string
101 * ... repeat num bits and hex string ...
104 * args[N-2] = "-endstate"
105 * args[N-1] = statename
107 if ((argc
< 4) || ((argc
% 2) != 0)) {
108 Jim_WrongNumArgs(interp
, 1, args
, "wrong arguments");
114 script_debug(interp
, "drscan", argc
, args
);
116 /* validate arguments as numbers */
118 for (i
= 2; i
< argc
; i
+= 2) {
122 e
= Jim_GetLong(interp
, args
[i
], &bits
);
123 /* If valid - try next arg */
127 /* Not valid.. are we at the end? */
128 if (((i
+ 2) != argc
)) {
129 /* nope, then error */
133 /* it could be: "-endstate FOO"
134 * e.g. DRPAUSE so we can issue more instructions
135 * before entering RUN/IDLE and executing them.
138 /* get arg as a string. */
139 cp
= Jim_GetString(args
[i
], NULL
);
140 /* is it the magic? */
141 if (0 == strcmp("-endstate", cp
)) {
142 /* is the statename valid? */
143 cp
= Jim_GetString(args
[i
+ 1], NULL
);
145 /* see if it is a valid state name */
146 endstate
= tap_state_by_name(cp
);
148 /* update the error message */
149 Jim_SetResultFormatted(interp
, "endstate: %s invalid", cp
);
151 if (!scan_is_safe(endstate
))
152 LOG_WARNING("drscan with unsafe "
153 "endstate \"%s\"", cp
);
155 /* valid - so clear the error */
157 /* and remove the last 2 args */
162 /* Still an error? */
164 return e
; /* too bad */
165 } /* validate args */
169 tap
= jtag_tap_by_jim_obj(interp
, args
[1]);
173 num_fields
= (argc
-2)/2;
174 assert(num_fields
> 0);
175 fields
= malloc(sizeof(struct scan_field
) * num_fields
);
176 for (i
= 2; i
< argc
; i
+= 2) {
181 Jim_GetLong(interp
, args
[i
], &bits
);
182 str
= Jim_GetString(args
[i
+ 1], &len
);
184 fields
[field_count
].num_bits
= bits
;
185 void *t
= malloc(DIV_ROUND_UP(bits
, 8));
186 fields
[field_count
].out_value
= t
;
187 str_to_buf(str
, len
, t
, bits
, 0);
188 fields
[field_count
].in_value
= t
;
192 jtag_add_dr_scan(tap
, num_fields
, fields
, endstate
);
194 retval
= jtag_execute_queue();
195 if (retval
!= ERROR_OK
) {
196 Jim_SetResultString(interp
, "drscan: jtag execute failed", -1);
201 Jim_Obj
*list
= Jim_NewListObj(interp
, NULL
, 0);
202 for (i
= 2; i
< argc
; i
+= 2) {
206 Jim_GetLong(interp
, args
[i
], &bits
);
207 str
= buf_to_str(fields
[field_count
].in_value
, bits
, 16);
208 free((void *)fields
[field_count
].out_value
);
210 Jim_ListAppendElement(interp
, list
, Jim_NewStringObj(interp
, str
, strlen(str
)));
215 Jim_SetResult(interp
, list
);
223 static int Jim_Command_pathmove(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *args
)
225 tap_state_t states
[8];
227 if ((argc
< 2) || ((size_t)argc
> (ARRAY_SIZE(states
) + 1))) {
228 Jim_WrongNumArgs(interp
, 1, args
, "wrong arguments");
232 script_debug(interp
, "pathmove", argc
, args
);
235 for (i
= 0; i
< argc
-1; i
++) {
237 cp
= Jim_GetString(args
[i
+ 1], NULL
);
238 states
[i
] = tap_state_by_name(cp
);
240 /* update the error message */
241 Jim_SetResultFormatted(interp
, "endstate: %s invalid", cp
);
246 if ((jtag_add_statemove(states
[0]) != ERROR_OK
) || (jtag_execute_queue() != ERROR_OK
)) {
247 Jim_SetResultString(interp
, "pathmove: jtag execute failed", -1);
251 jtag_add_pathmove(argc
- 2, states
+ 1);
253 if (jtag_execute_queue() != ERROR_OK
) {
254 Jim_SetResultString(interp
, "pathmove: failed", -1);
262 static int Jim_Command_flush_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *args
)
264 script_debug(interp
, "flush_count", argc
, args
);
266 Jim_SetResult(interp
, Jim_NewIntObj(interp
, jtag_get_flush_queue_count()));
271 /* REVISIT Just what about these should "move" ... ?
272 * These registrations, into the main JTAG table?
274 * There's a minor compatibility issue, these all show up twice;
275 * that's not desirable:
276 * - jtag drscan ... NOT DOCUMENTED!
279 * The "irscan" command (for example) doesn't show twice.
281 static const struct command_registration jtag_command_handlers_to_move
[] = {
284 .mode
= COMMAND_EXEC
,
285 .jim_handler
= Jim_Command_drscan
,
286 .help
= "Execute Data Register (DR) scan for one TAP. "
287 "Other TAPs must be in BYPASS mode.",
288 .usage
= "tap_name [num_bits value]* ['-endstate' state_name]",
291 .name
= "flush_count",
292 .mode
= COMMAND_EXEC
,
293 .jim_handler
= Jim_Command_flush_count
,
294 .help
= "Returns the number of times the JTAG queue "
299 .mode
= COMMAND_EXEC
,
300 .jim_handler
= Jim_Command_pathmove
,
301 .usage
= "start_state state1 [state2 [state3 ...]]",
302 .help
= "Move JTAG state machine from current state "
303 "(start_state) to state1, then state2, state3, etc.",
305 COMMAND_REGISTRATION_DONE
309 enum jtag_tap_cfg_param
{
313 static Jim_Nvp nvp_config_opts
[] = {
314 { .name
= "-event", .value
= JCFG_EVENT
},
316 { .name
= NULL
, .value
= -1 }
319 static int jtag_tap_configure_event(Jim_GetOptInfo
*goi
, struct jtag_tap
*tap
)
321 if (goi
->argc
== 0) {
322 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event <event-name> ...");
327 int e
= Jim_GetOpt_Nvp(goi
, nvp_jtag_tap_event
, &n
);
329 Jim_GetOpt_NvpUnknown(goi
, nvp_jtag_tap_event
, 1);
333 if (goi
->isconfigure
) {
334 if (goi
->argc
!= 1) {
335 Jim_WrongNumArgs(goi
->interp
,
338 "-event <event-name> <event-body>");
342 if (goi
->argc
!= 0) {
343 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event <event-name>");
348 struct jtag_tap_event_action
*jteap
= tap
->event_action
;
349 /* replace existing event body */
352 if (jteap
->event
== (enum jtag_event
)n
->value
) {
359 Jim_SetEmptyResult(goi
->interp
);
361 if (goi
->isconfigure
) {
363 jteap
= calloc(1, sizeof(*jteap
));
364 else if (NULL
!= jteap
->body
)
365 Jim_DecrRefCount(goi
->interp
, jteap
->body
);
367 jteap
->interp
= goi
->interp
;
368 jteap
->event
= n
->value
;
371 Jim_GetOpt_Obj(goi
, &o
);
372 jteap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
373 Jim_IncrRefCount(jteap
->body
);
376 /* add to head of event list */
377 jteap
->next
= tap
->event_action
;
378 tap
->event_action
= jteap
;
381 jteap
->interp
= goi
->interp
;
382 Jim_SetResult(goi
->interp
,
383 Jim_DuplicateObj(goi
->interp
, jteap
->body
));
388 static int jtag_tap_configure_cmd(Jim_GetOptInfo
*goi
, struct jtag_tap
*tap
)
390 /* parse config or cget options */
391 while (goi
->argc
> 0) {
392 Jim_SetEmptyResult(goi
->interp
);
395 int e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
397 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
403 e
= jtag_tap_configure_event(goi
, tap
);
408 Jim_SetResultFormatted(goi
->interp
, "unknown event: %s", n
->name
);
416 static int is_bad_irval(int ir_length
, jim_wide w
)
426 static int jim_newtap_expected_id(Jim_Nvp
*n
, Jim_GetOptInfo
*goi
,
427 struct jtag_tap
*pTap
)
430 int e
= Jim_GetOpt_Wide(goi
, &w
);
432 Jim_SetResultFormatted(goi
->interp
, "option: %s bad parameter", n
->name
);
436 unsigned expected_len
= sizeof(uint32_t) * pTap
->expected_ids_cnt
;
437 uint32_t *new_expected_ids
= malloc(expected_len
+ sizeof(uint32_t));
438 if (new_expected_ids
== NULL
) {
439 Jim_SetResultFormatted(goi
->interp
, "no memory");
443 memcpy(new_expected_ids
, pTap
->expected_ids
, expected_len
);
445 new_expected_ids
[pTap
->expected_ids_cnt
] = w
;
447 free(pTap
->expected_ids
);
448 pTap
->expected_ids
= new_expected_ids
;
449 pTap
->expected_ids_cnt
++;
454 #define NTAP_OPT_IRLEN 0
455 #define NTAP_OPT_IRMASK 1
456 #define NTAP_OPT_IRCAPTURE 2
457 #define NTAP_OPT_ENABLED 3
458 #define NTAP_OPT_DISABLED 4
459 #define NTAP_OPT_EXPECTED_ID 5
460 #define NTAP_OPT_VERSION 6
462 static int jim_newtap_ir_param(Jim_Nvp
*n
, Jim_GetOptInfo
*goi
,
463 struct jtag_tap
*pTap
)
466 int e
= Jim_GetOpt_Wide(goi
, &w
);
468 Jim_SetResultFormatted(goi
->interp
,
469 "option: %s bad parameter", n
->name
);
470 free((void *)pTap
->dotted_name
);
475 if (w
> (jim_wide
) (8 * sizeof(pTap
->ir_capture_value
))) {
476 LOG_WARNING("%s: huge IR length %d",
477 pTap
->dotted_name
, (int) w
);
481 case NTAP_OPT_IRMASK
:
482 if (is_bad_irval(pTap
->ir_length
, w
)) {
483 LOG_ERROR("%s: IR mask %x too big",
489 LOG_WARNING("%s: nonstandard IR mask", pTap
->dotted_name
);
490 pTap
->ir_capture_mask
= w
;
492 case NTAP_OPT_IRCAPTURE
:
493 if (is_bad_irval(pTap
->ir_length
, w
)) {
494 LOG_ERROR("%s: IR capture %x too big",
495 pTap
->dotted_name
, (int) w
);
499 LOG_WARNING("%s: nonstandard IR value",
501 pTap
->ir_capture_value
= w
;
509 static int jim_newtap_cmd(Jim_GetOptInfo
*goi
)
511 struct jtag_tap
*pTap
;
516 const Jim_Nvp opts
[] = {
517 { .name
= "-irlen", .value
= NTAP_OPT_IRLEN
},
518 { .name
= "-irmask", .value
= NTAP_OPT_IRMASK
},
519 { .name
= "-ircapture", .value
= NTAP_OPT_IRCAPTURE
},
520 { .name
= "-enable", .value
= NTAP_OPT_ENABLED
},
521 { .name
= "-disable", .value
= NTAP_OPT_DISABLED
},
522 { .name
= "-expected-id", .value
= NTAP_OPT_EXPECTED_ID
},
523 { .name
= "-ignore-version", .value
= NTAP_OPT_VERSION
},
524 { .name
= NULL
, .value
= -1 },
527 pTap
= calloc(1, sizeof(struct jtag_tap
));
529 Jim_SetResultFormatted(goi
->interp
, "no memory");
534 * we expect CHIP + TAP + OPTIONS
537 Jim_SetResultFormatted(goi
->interp
, "Missing CHIP TAP OPTIONS ....");
541 Jim_GetOpt_String(goi
, &cp
, NULL
);
542 pTap
->chip
= strdup(cp
);
544 Jim_GetOpt_String(goi
, &cp
, NULL
);
545 pTap
->tapname
= strdup(cp
);
547 /* name + dot + name + null */
548 x
= strlen(pTap
->chip
) + 1 + strlen(pTap
->tapname
) + 1;
550 sprintf(cp
, "%s.%s", pTap
->chip
, pTap
->tapname
);
551 pTap
->dotted_name
= cp
;
553 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
554 pTap
->chip
, pTap
->tapname
, pTap
->dotted_name
, goi
->argc
);
556 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
557 * that the default. The "-irlen" and "-irmask" options are only
558 * needed to cope with nonstandard TAPs, or to specify more bits.
560 pTap
->ir_capture_mask
= 0x03;
561 pTap
->ir_capture_value
= 0x01;
564 e
= Jim_GetOpt_Nvp(goi
, opts
, &n
);
566 Jim_GetOpt_NvpUnknown(goi
, opts
, 0);
567 free((void *)pTap
->dotted_name
);
571 LOG_DEBUG("Processing option: %s", n
->name
);
573 case NTAP_OPT_ENABLED
:
574 pTap
->disabled_after_reset
= false;
576 case NTAP_OPT_DISABLED
:
577 pTap
->disabled_after_reset
= true;
579 case NTAP_OPT_EXPECTED_ID
:
580 e
= jim_newtap_expected_id(n
, goi
, pTap
);
582 free((void *)pTap
->dotted_name
);
588 case NTAP_OPT_IRMASK
:
589 case NTAP_OPT_IRCAPTURE
:
590 e
= jim_newtap_ir_param(n
, goi
, pTap
);
592 free((void *)pTap
->dotted_name
);
597 case NTAP_OPT_VERSION
:
598 pTap
->ignore_version
= true;
600 } /* switch (n->value) */
601 } /* while (goi->argc) */
603 /* default is enabled-after-reset */
604 pTap
->enabled
= !pTap
->disabled_after_reset
;
606 /* Did all the required option bits get cleared? */
607 if (pTap
->ir_length
!= 0) {
612 Jim_SetResultFormatted(goi
->interp
,
613 "newtap: %s missing IR length",
619 static void jtag_tap_handle_event(struct jtag_tap
*tap
, enum jtag_event e
)
621 struct jtag_tap_event_action
*jteap
;
623 for (jteap
= tap
->event_action
; jteap
!= NULL
; jteap
= jteap
->next
) {
624 if (jteap
->event
!= e
)
627 Jim_Nvp
*nvp
= Jim_Nvp_value2name_simple(nvp_jtag_tap_event
, e
);
628 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
629 tap
->dotted_name
, e
, nvp
->name
,
630 Jim_GetString(jteap
->body
, NULL
));
632 if (Jim_EvalObj(jteap
->interp
, jteap
->body
) != JIM_OK
) {
633 Jim_MakeErrorMessage(jteap
->interp
);
634 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap
->interp
), NULL
));
639 case JTAG_TAP_EVENT_ENABLE
:
640 case JTAG_TAP_EVENT_DISABLE
:
641 /* NOTE: we currently assume the handlers
642 * can't fail. Right here is where we should
643 * really be verifying the scan chains ...
645 tap
->enabled
= (e
== JTAG_TAP_EVENT_ENABLE
);
646 LOG_INFO("JTAG tap: %s %s", tap
->dotted_name
,
647 tap
->enabled
? "enabled" : "disabled");
655 static int jim_jtag_arp_init(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
658 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
660 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
-1, "(no params)");
663 struct command_context
*context
= current_command_context(interp
);
664 int e
= jtag_init_inner(context
);
666 Jim_Obj
*eObj
= Jim_NewIntObj(goi
.interp
, e
);
667 Jim_SetResultFormatted(goi
.interp
, "error: %#s", eObj
);
668 Jim_FreeNewObj(goi
.interp
, eObj
);
674 static int jim_jtag_arp_init_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
678 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
680 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
-1, "(no params)");
683 struct command_context
*context
= current_command_context(interp
);
684 if (transport_is_jtag())
685 e
= jtag_init_reset(context
);
686 else if (transport_is_swd())
687 e
= swd_init_reset(context
);
690 Jim_Obj
*eObj
= Jim_NewIntObj(goi
.interp
, e
);
691 Jim_SetResultFormatted(goi
.interp
, "error: %#s", eObj
);
692 Jim_FreeNewObj(goi
.interp
, eObj
);
698 int jim_jtag_newtap(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
701 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
702 return jim_newtap_cmd(&goi
);
705 static bool jtag_tap_enable(struct jtag_tap
*t
)
709 jtag_tap_handle_event(t
, JTAG_TAP_EVENT_ENABLE
);
713 /* FIXME add JTAG sanity checks, w/o TLR
714 * - scan chain length grew by one (this)
715 * - IDs and IR lengths are as expected
717 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE
);
720 static bool jtag_tap_disable(struct jtag_tap
*t
)
724 jtag_tap_handle_event(t
, JTAG_TAP_EVENT_DISABLE
);
728 /* FIXME add JTAG sanity checks, w/o TLR
729 * - scan chain length shrank by one (this)
730 * - IDs and IR lengths are as expected
732 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE
);
736 int jim_jtag_tap_enabler(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
738 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
740 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
742 Jim_SetResultFormatted(goi
.interp
, "usage: %s <name>", cmd_name
);
748 t
= jtag_tap_by_jim_obj(goi
.interp
, goi
.argv
[0]);
752 if (strcasecmp(cmd_name
, "tapisenabled") == 0) {
753 /* do nothing, just return the value */
754 } else if (strcasecmp(cmd_name
, "tapenable") == 0) {
755 if (!jtag_tap_enable(t
)) {
756 LOG_WARNING("failed to enable tap %s", t
->dotted_name
);
759 } else if (strcasecmp(cmd_name
, "tapdisable") == 0) {
760 if (!jtag_tap_disable(t
)) {
761 LOG_WARNING("failed to disable tap %s", t
->dotted_name
);
765 LOG_ERROR("command '%s' unknown", cmd_name
);
769 Jim_SetResult(goi
.interp
, Jim_NewIntObj(goi
.interp
, e
));
773 int jim_jtag_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
775 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
777 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
778 goi
.isconfigure
= !strcmp(cmd_name
, "configure");
779 if (goi
.argc
< 2 + goi
.isconfigure
) {
780 Jim_WrongNumArgs(goi
.interp
, 0, NULL
,
781 "<tap_name> <attribute> ...");
788 Jim_GetOpt_Obj(&goi
, &o
);
789 t
= jtag_tap_by_jim_obj(goi
.interp
, o
);
793 return jtag_tap_configure_cmd(&goi
, t
);
796 static int jim_jtag_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
799 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
801 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
, "Too many parameters");
804 Jim_SetResult(goi
.interp
, Jim_NewListObj(goi
.interp
, NULL
, 0));
805 struct jtag_tap
*tap
;
807 for (tap
= jtag_all_taps(); tap
; tap
= tap
->next_tap
) {
808 Jim_ListAppendElement(goi
.interp
,
809 Jim_GetResult(goi
.interp
),
810 Jim_NewStringObj(goi
.interp
,
811 tap
->dotted_name
, -1));
816 COMMAND_HANDLER(handle_jtag_init_command
)
819 return ERROR_COMMAND_SYNTAX_ERROR
;
821 static bool jtag_initialized
;
822 if (jtag_initialized
) {
823 LOG_INFO("'jtag init' has already been called");
826 jtag_initialized
= true;
828 LOG_DEBUG("Initializing jtag devices...");
829 return jtag_init(CMD_CTX
);
832 static const struct command_registration jtag_subcommand_handlers
[] = {
836 .handler
= handle_jtag_init_command
,
837 .help
= "initialize jtag scan chain",
843 .jim_handler
= jim_jtag_arp_init
,
844 .help
= "Validates JTAG scan chain against the list of "
845 "declared TAPs using just the four standard JTAG "
849 .name
= "arp_init-reset",
851 .jim_handler
= jim_jtag_arp_init_reset
,
852 .help
= "Uses TRST and SRST to try resetting everything on "
853 "the JTAG scan chain, then performs 'jtag arp_init'."
857 .mode
= COMMAND_CONFIG
,
858 .jim_handler
= jim_jtag_newtap
,
859 .help
= "Create a new TAP instance named basename.tap_type, "
860 "and appends it to the scan chain.",
861 .usage
= "basename tap_type '-irlen' count "
862 "['-enable'|'-disable'] "
863 "['-expected_id' number] "
864 "['-ignore-version'] "
865 "['-ircapture' number] "
869 .name
= "tapisenabled",
870 .mode
= COMMAND_EXEC
,
871 .jim_handler
= jim_jtag_tap_enabler
,
872 .help
= "Returns a Tcl boolean (0/1) indicating whether "
873 "the TAP is enabled (1) or not (0).",
878 .mode
= COMMAND_EXEC
,
879 .jim_handler
= jim_jtag_tap_enabler
,
880 .help
= "Try to enable the specified TAP using the "
881 "'tap-enable' TAP event.",
885 .name
= "tapdisable",
886 .mode
= COMMAND_EXEC
,
887 .jim_handler
= jim_jtag_tap_enabler
,
888 .help
= "Try to disable the specified TAP using the "
889 "'tap-disable' TAP event.",
894 .mode
= COMMAND_EXEC
,
895 .jim_handler
= jim_jtag_configure
,
896 .help
= "Provide a Tcl handler for the specified "
898 .usage
= "tap_name '-event' event_name handler",
902 .mode
= COMMAND_EXEC
,
903 .jim_handler
= jim_jtag_configure
,
904 .help
= "Return any Tcl handler for the specified "
906 .usage
= "tap_name '-event' event_name",
911 .jim_handler
= jim_jtag_names
,
912 .help
= "Returns list of all JTAG tap names.",
915 .chain
= jtag_command_handlers_to_move
,
917 COMMAND_REGISTRATION_DONE
920 void jtag_notify_event(enum jtag_event event
)
922 struct jtag_tap
*tap
;
924 for (tap
= jtag_all_taps(); tap
; tap
= tap
->next_tap
)
925 jtag_tap_handle_event(tap
, event
);
929 COMMAND_HANDLER(handle_scan_chain_command
)
931 struct jtag_tap
*tap
;
932 char expected_id
[12];
934 tap
= jtag_all_taps();
935 command_print(CMD_CTX
,
936 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
937 command_print(CMD_CTX
,
938 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
941 uint32_t expected
, expected_mask
, ii
;
943 snprintf(expected_id
, sizeof expected_id
, "0x%08x",
944 (unsigned)((tap
->expected_ids_cnt
> 0)
945 ? tap
->expected_ids
[0]
947 if (tap
->ignore_version
)
948 expected_id
[2] = '*';
950 expected
= buf_get_u32(tap
->expected
, 0, tap
->ir_length
);
951 expected_mask
= buf_get_u32(tap
->expected_mask
, 0, tap
->ir_length
);
953 command_print(CMD_CTX
,
954 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
955 tap
->abs_chain_position
,
957 tap
->enabled
? 'Y' : 'n',
958 (unsigned int)(tap
->idcode
),
960 (unsigned int)(tap
->ir_length
),
961 (unsigned int)(expected
),
962 (unsigned int)(expected_mask
));
964 for (ii
= 1; ii
< tap
->expected_ids_cnt
; ii
++) {
965 snprintf(expected_id
, sizeof expected_id
, "0x%08x",
966 (unsigned) tap
->expected_ids
[ii
]);
967 if (tap
->ignore_version
)
968 expected_id
[2] = '*';
970 command_print(CMD_CTX
,
981 COMMAND_HANDLER(handle_jtag_ntrst_delay_command
)
984 return ERROR_COMMAND_SYNTAX_ERROR
;
987 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
989 jtag_set_ntrst_delay(delay
);
991 command_print(CMD_CTX
, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
995 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command
)
998 return ERROR_COMMAND_SYNTAX_ERROR
;
1001 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
1003 jtag_set_ntrst_assert_width(delay
);
1005 command_print(CMD_CTX
, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1009 COMMAND_HANDLER(handle_jtag_rclk_command
)
1012 return ERROR_COMMAND_SYNTAX_ERROR
;
1014 int retval
= ERROR_OK
;
1015 if (CMD_ARGC
== 1) {
1017 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], khz
);
1019 retval
= jtag_config_rclk(khz
);
1020 if (ERROR_OK
!= retval
)
1024 int cur_khz
= jtag_get_speed_khz();
1025 retval
= jtag_get_speed_readable(&cur_khz
);
1026 if (ERROR_OK
!= retval
)
1030 command_print(CMD_CTX
, "RCLK not supported - fallback to %d kHz", cur_khz
);
1032 command_print(CMD_CTX
, "RCLK - adaptive");
1037 COMMAND_HANDLER(handle_jtag_reset_command
)
1040 return ERROR_COMMAND_SYNTAX_ERROR
;
1043 if (CMD_ARGV
[0][0] == '1')
1045 else if (CMD_ARGV
[0][0] == '0')
1048 return ERROR_COMMAND_SYNTAX_ERROR
;
1051 if (CMD_ARGV
[1][0] == '1')
1053 else if (CMD_ARGV
[1][0] == '0')
1056 return ERROR_COMMAND_SYNTAX_ERROR
;
1058 if (adapter_init(CMD_CTX
) != ERROR_OK
)
1059 return ERROR_JTAG_INIT_FAILED
;
1061 jtag_add_reset(trst
, srst
);
1062 return jtag_execute_queue();
1065 COMMAND_HANDLER(handle_runtest_command
)
1068 return ERROR_COMMAND_SYNTAX_ERROR
;
1070 unsigned num_clocks
;
1071 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num_clocks
);
1073 jtag_add_runtest(num_clocks
, TAP_IDLE
);
1074 return jtag_execute_queue();
1078 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1079 * should be stable ... and *NOT* a shift state, otherwise free-running
1080 * jtag clocks could change the values latched by the update state.
1081 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1082 * and "drscan" commands are a write-only subset of what SVF provides.
1085 COMMAND_HANDLER(handle_irscan_command
)
1088 struct scan_field
*fields
;
1089 struct jtag_tap
*tap
= NULL
;
1090 tap_state_t endstate
;
1092 if ((CMD_ARGC
< 2) || (CMD_ARGC
% 2))
1093 return ERROR_COMMAND_SYNTAX_ERROR
;
1095 /* optional "-endstate" "statename" at the end of the arguments,
1096 * so that e.g. IRPAUSE can let us load the data register before
1097 * entering RUN/IDLE to execute the instruction we load here.
1099 endstate
= TAP_IDLE
;
1101 if (CMD_ARGC
>= 4) {
1102 /* have at least one pair of numbers.
1103 * is last pair the magic text? */
1104 if (strcmp("-endstate", CMD_ARGV
[CMD_ARGC
- 2]) == 0) {
1105 endstate
= tap_state_by_name(CMD_ARGV
[CMD_ARGC
- 1]);
1106 if (endstate
== TAP_INVALID
)
1107 return ERROR_COMMAND_SYNTAX_ERROR
;
1108 if (!scan_is_safe(endstate
))
1109 LOG_WARNING("unstable irscan endstate \"%s\"",
1110 CMD_ARGV
[CMD_ARGC
- 1]);
1115 int num_fields
= CMD_ARGC
/ 2;
1116 if (num_fields
> 1) {
1117 /* we really should be looking at plain_ir_scan if we want
1118 * anything more fancy.
1120 LOG_ERROR("Specify a single value for tap");
1121 return ERROR_COMMAND_SYNTAX_ERROR
;
1124 size_t fields_len
= sizeof(struct scan_field
) * num_fields
;
1125 fields
= malloc(fields_len
);
1126 memset(fields
, 0, fields_len
);
1129 for (i
= 0; i
< num_fields
; i
++) {
1130 tap
= jtag_tap_by_string(CMD_ARGV
[i
*2]);
1133 for (j
= 0; j
< i
; j
++)
1134 free((void *)fields
[j
].out_value
);
1136 command_print(CMD_CTX
, "Tap: %s unknown", CMD_ARGV
[i
*2]);
1140 int field_size
= tap
->ir_length
;
1141 fields
[i
].num_bits
= field_size
;
1142 fields
[i
].out_value
= malloc(DIV_ROUND_UP(field_size
, 8));
1145 retval
= parse_u32(CMD_ARGV
[i
* 2 + 1], &value
);
1146 if (ERROR_OK
!= retval
)
1148 void *v
= (void *)fields
[i
].out_value
;
1149 buf_set_u32(v
, 0, field_size
, value
);
1150 fields
[i
].in_value
= NULL
;
1153 /* did we have an endstate? */
1154 jtag_add_ir_scan(tap
, fields
, endstate
);
1156 retval
= jtag_execute_queue();
1159 for (i
= 0; i
< num_fields
; i
++) {
1160 if (NULL
!= fields
[i
].out_value
)
1161 free((void *)fields
[i
].out_value
);
1169 COMMAND_HANDLER(handle_verify_ircapture_command
)
1172 return ERROR_COMMAND_SYNTAX_ERROR
;
1174 if (CMD_ARGC
== 1) {
1176 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], enable
);
1177 jtag_set_verify_capture_ir(enable
);
1180 const char *status
= jtag_will_verify_capture_ir() ? "enabled" : "disabled";
1181 command_print(CMD_CTX
, "verify Capture-IR is %s", status
);
1186 COMMAND_HANDLER(handle_verify_jtag_command
)
1189 return ERROR_COMMAND_SYNTAX_ERROR
;
1191 if (CMD_ARGC
== 1) {
1193 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], enable
);
1194 jtag_set_verify(enable
);
1197 const char *status
= jtag_will_verify() ? "enabled" : "disabled";
1198 command_print(CMD_CTX
, "verify jtag capture is %s", status
);
1203 COMMAND_HANDLER(handle_tms_sequence_command
)
1206 return ERROR_COMMAND_SYNTAX_ERROR
;
1208 if (CMD_ARGC
== 1) {
1210 if (strcmp(CMD_ARGV
[0], "short") == 0)
1211 use_new_table
= true;
1212 else if (strcmp(CMD_ARGV
[0], "long") == 0)
1213 use_new_table
= false;
1215 return ERROR_COMMAND_SYNTAX_ERROR
;
1217 tap_use_new_tms_table(use_new_table
);
1220 command_print(CMD_CTX
, "tms sequence is %s",
1221 tap_uses_new_tms_table() ? "short" : "long");
1226 COMMAND_HANDLER(handle_jtag_flush_queue_sleep
)
1229 return ERROR_COMMAND_SYNTAX_ERROR
;
1232 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], sleep_ms
);
1234 jtag_set_flush_queue_sleep(sleep_ms
);
1239 COMMAND_HANDLER(handle_wait_srst_deassert
)
1242 return ERROR_COMMAND_SYNTAX_ERROR
;
1245 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], timeout_ms
);
1246 if ((timeout_ms
<= 0) || (timeout_ms
> 100000)) {
1247 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1251 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms
);
1253 long long then
= timeval_ms();
1254 while (jtag_srst_asserted(&asserted_yet
) == ERROR_OK
) {
1255 if ((timeval_ms() - then
) > timeout_ms
) {
1256 LOG_ERROR("Timed out");
1262 while (jtag_srst_asserted(&asserted_yet
) == ERROR_OK
) {
1263 if ((timeval_ms() - then
) > timeout_ms
) {
1264 LOG_ERROR("Timed out");
1274 static const struct command_registration jtag_command_handlers
[] = {
1277 .name
= "jtag_flush_queue_sleep",
1278 .handler
= handle_jtag_flush_queue_sleep
,
1279 .mode
= COMMAND_ANY
,
1280 .help
= "For debug purposes(simulate long delays of interface) "
1281 "to test performance or change in behavior. Default 0ms.",
1282 .usage
= "[sleep in ms]",
1285 .name
= "jtag_rclk",
1286 .handler
= handle_jtag_rclk_command
,
1287 .mode
= COMMAND_ANY
,
1288 .help
= "With an argument, change to to use adaptive clocking "
1289 "if possible; else to use the fallback speed. "
1290 "With or without argument, display current setting.",
1291 .usage
= "[fallback_speed_khz]",
1294 .name
= "jtag_ntrst_delay",
1295 .handler
= handle_jtag_ntrst_delay_command
,
1296 .mode
= COMMAND_ANY
,
1297 .help
= "delay after deasserting trst in ms",
1298 .usage
= "[milliseconds]",
1301 .name
= "jtag_ntrst_assert_width",
1302 .handler
= handle_jtag_ntrst_assert_width_command
,
1303 .mode
= COMMAND_ANY
,
1304 .help
= "delay after asserting trst in ms",
1305 .usage
= "[milliseconds]",
1308 .name
= "scan_chain",
1309 .handler
= handle_scan_chain_command
,
1310 .mode
= COMMAND_ANY
,
1311 .help
= "print current scan chain configuration",
1315 .name
= "jtag_reset",
1316 .handler
= handle_jtag_reset_command
,
1317 .mode
= COMMAND_EXEC
,
1318 .help
= "Set reset line values. Value '1' is active, "
1319 "value '0' is inactive.",
1320 .usage
= "trst_active srst_active",
1324 .handler
= handle_runtest_command
,
1325 .mode
= COMMAND_EXEC
,
1326 .help
= "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1327 .usage
= "num_cycles"
1331 .handler
= handle_irscan_command
,
1332 .mode
= COMMAND_EXEC
,
1333 .help
= "Execute Instruction Register (DR) scan. The "
1334 "specified opcodes are put into each TAP's IR, "
1335 "and other TAPs are put in BYPASS.",
1336 .usage
= "[tap_name instruction]* ['-endstate' state_name]",
1339 .name
= "verify_ircapture",
1340 .handler
= handle_verify_ircapture_command
,
1341 .mode
= COMMAND_ANY
,
1342 .help
= "Display or assign flag controlling whether to "
1343 "verify values captured during Capture-IR.",
1344 .usage
= "['enable'|'disable']",
1347 .name
= "verify_jtag",
1348 .handler
= handle_verify_jtag_command
,
1349 .mode
= COMMAND_ANY
,
1350 .help
= "Display or assign flag controlling whether to "
1351 "verify values captured during IR and DR scans.",
1352 .usage
= "['enable'|'disable']",
1355 .name
= "tms_sequence",
1356 .handler
= handle_tms_sequence_command
,
1357 .mode
= COMMAND_ANY
,
1358 .help
= "Display or change what style TMS sequences to use "
1359 "for JTAG state transitions: short (default) or "
1360 "long. Only for working around JTAG bugs.",
1361 /* Specifically for working around DRIVER bugs... */
1362 .usage
= "['short'|'long']",
1365 .name
= "wait_srst_deassert",
1366 .handler
= handle_wait_srst_deassert
,
1367 .mode
= COMMAND_ANY
,
1368 .help
= "Wait for an SRST deassert. "
1369 "Useful for cases where you need something to happen within ms "
1370 "of an srst deassert. Timeout in ms ",
1375 .mode
= COMMAND_ANY
,
1376 .help
= "perform jtag tap actions",
1379 .chain
= jtag_subcommand_handlers
,
1382 .chain
= jtag_command_handlers_to_move
,
1384 COMMAND_REGISTRATION_DONE
1387 int jtag_register_commands(struct command_context
*cmd_ctx
)
1389 return register_commands(cmd_ctx
, NULL
, jtag_command_handlers
);