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, see <http://www.gnu.org/licenses/>. *
27 ***************************************************************************/
35 #include "minidriver.h"
36 #include "interface.h"
37 #include "interfaces.h"
44 #include <helper/time_support.h>
45 #include "transport/transport.h"
49 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
52 static const Jim_Nvp nvp_jtag_tap_event[] = {
53 { .value = JTAG_TRST_ASSERTED, .name = "post-reset" },
54 { .value = JTAG_TAP_EVENT_SETUP, .name = "setup" },
55 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
56 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
58 { .name = NULL, .value = -1 }
61 struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
63 const char *cp = Jim_GetString(o, NULL);
64 struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
68 Jim_SetResultFormatted(interp, "Tap '%s' could not be found", cp);
72 static bool scan_is_safe(tap_state_t state)
85 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
88 struct scan_field *fields;
97 * args[3] = hex string
98 * ... repeat num bits and hex string ...
101 * args[N-2] = "-endstate"
102 * args[N-1] = statename
104 if ((argc < 4) || ((argc % 2) != 0)) {
105 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
111 script_debug(interp, argc, args);
113 /* validate arguments as numbers */
115 for (i = 2; i < argc; i += 2) {
119 e = Jim_GetLong(interp, args[i], &bits);
120 /* If valid - try next arg */
124 /* Not valid.. are we at the end? */
125 if (((i + 2) != argc)) {
126 /* nope, then error */
130 /* it could be: "-endstate FOO"
131 * e.g. DRPAUSE so we can issue more instructions
132 * before entering RUN/IDLE and executing them.
135 /* get arg as a string. */
136 cp = Jim_GetString(args[i], NULL);
137 /* is it the magic? */
138 if (0 == strcmp("-endstate", cp)) {
139 /* is the statename valid? */
140 cp = Jim_GetString(args[i + 1], NULL);
142 /* see if it is a valid state name */
143 endstate = tap_state_by_name(cp);
145 /* update the error message */
146 Jim_SetResultFormatted(interp, "endstate: %s invalid", cp);
148 if (!scan_is_safe(endstate))
149 LOG_WARNING("drscan with unsafe "
150 "endstate \"%s\"", cp);
152 /* valid - so clear the error */
154 /* and remove the last 2 args */
159 /* Still an error? */
161 return e; /* too bad */
162 } /* validate args */
166 tap = jtag_tap_by_jim_obj(interp, args[1]);
170 num_fields = (argc-2)/2;
171 if (num_fields <= 0) {
172 Jim_SetResultString(interp, "drscan: no scan fields supplied", -1);
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_hex_str(fields[field_count].in_value, bits);
208 free(fields[field_count].in_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, 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, 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 {
314 static Jim_Nvp nvp_config_opts[] = {
315 { .name = "-event", .value = JCFG_EVENT },
316 { .name = "-idcode", .value = JCFG_IDCODE },
318 { .name = NULL, .value = -1 }
321 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap *tap)
323 if (goi->argc == 0) {
324 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
329 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
331 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
335 if (goi->isconfigure) {
336 if (goi->argc != 1) {
337 Jim_WrongNumArgs(goi->interp,
340 "-event <event-name> <event-body>");
344 if (goi->argc != 0) {
345 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
350 struct jtag_tap_event_action *jteap = tap->event_action;
351 /* replace existing event body */
354 if (jteap->event == (enum jtag_event)n->value) {
361 Jim_SetEmptyResult(goi->interp);
363 if (goi->isconfigure) {
365 jteap = calloc(1, sizeof(*jteap));
366 else if (NULL != jteap->body)
367 Jim_DecrRefCount(goi->interp, jteap->body);
369 jteap->interp = goi->interp;
370 jteap->event = n->value;
373 Jim_GetOpt_Obj(goi, &o);
374 jteap->body = Jim_DuplicateObj(goi->interp, o);
375 Jim_IncrRefCount(jteap->body);
378 /* add to head of event list */
379 jteap->next = tap->event_action;
380 tap->event_action = jteap;
383 jteap->interp = goi->interp;
384 Jim_SetResult(goi->interp,
385 Jim_DuplicateObj(goi->interp, jteap->body));
390 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap *tap)
392 /* parse config or cget options */
393 while (goi->argc > 0) {
394 Jim_SetEmptyResult(goi->interp);
397 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
399 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
405 e = jtag_tap_configure_event(goi, tap);
410 if (goi->isconfigure) {
411 Jim_SetResultFormatted(goi->interp,
412 "not settable: %s", n->name);
415 if (goi->argc != 0) {
416 Jim_WrongNumArgs(goi->interp,
417 goi->argc, goi->argv,
422 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, tap->idcode));
425 Jim_SetResultFormatted(goi->interp, "unknown value: %s", n->name);
433 static int is_bad_irval(int ir_length, jim_wide w)
443 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
444 struct jtag_tap *pTap)
447 int e = Jim_GetOpt_Wide(goi, &w);
449 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
453 uint32_t *p = realloc(pTap->expected_ids,
454 (pTap->expected_ids_cnt + 1) * sizeof(uint32_t));
456 Jim_SetResultFormatted(goi->interp, "no memory");
460 pTap->expected_ids = p;
461 pTap->expected_ids[pTap->expected_ids_cnt++] = w;
466 #define NTAP_OPT_IRLEN 0
467 #define NTAP_OPT_IRMASK 1
468 #define NTAP_OPT_IRCAPTURE 2
469 #define NTAP_OPT_ENABLED 3
470 #define NTAP_OPT_DISABLED 4
471 #define NTAP_OPT_EXPECTED_ID 5
472 #define NTAP_OPT_VERSION 6
474 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
475 struct jtag_tap *pTap)
478 int e = Jim_GetOpt_Wide(goi, &w);
480 Jim_SetResultFormatted(goi->interp,
481 "option: %s bad parameter", n->name);
486 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value))) {
487 LOG_WARNING("%s: huge IR length %d",
488 pTap->dotted_name, (int) w);
492 case NTAP_OPT_IRMASK:
493 if (is_bad_irval(pTap->ir_length, w)) {
494 LOG_ERROR("%s: IR mask %x too big",
500 LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
501 pTap->ir_capture_mask = w;
503 case NTAP_OPT_IRCAPTURE:
504 if (is_bad_irval(pTap->ir_length, w)) {
505 LOG_ERROR("%s: IR capture %x too big",
506 pTap->dotted_name, (int) w);
510 LOG_WARNING("%s: nonstandard IR value",
512 pTap->ir_capture_value = w;
520 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
522 struct jtag_tap *pTap;
527 const Jim_Nvp opts[] = {
528 { .name = "-irlen", .value = NTAP_OPT_IRLEN },
529 { .name = "-irmask", .value = NTAP_OPT_IRMASK },
530 { .name = "-ircapture", .value = NTAP_OPT_IRCAPTURE },
531 { .name = "-enable", .value = NTAP_OPT_ENABLED },
532 { .name = "-disable", .value = NTAP_OPT_DISABLED },
533 { .name = "-expected-id", .value = NTAP_OPT_EXPECTED_ID },
534 { .name = "-ignore-version", .value = NTAP_OPT_VERSION },
535 { .name = NULL, .value = -1 },
538 pTap = calloc(1, sizeof(struct jtag_tap));
540 Jim_SetResultFormatted(goi->interp, "no memory");
545 * we expect CHIP + TAP + OPTIONS
548 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
554 Jim_GetOpt_String(goi, &tmp, NULL);
555 pTap->chip = strdup(tmp);
557 Jim_GetOpt_String(goi, &tmp, NULL);
558 pTap->tapname = strdup(tmp);
560 /* name + dot + name + null */
561 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
563 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
564 pTap->dotted_name = cp;
566 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
567 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
569 if (!transport_is_jtag()) {
570 /* SWD doesn't require any JTAG tap parameters */
571 pTap->enabled = true;
576 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
577 * that the default. The "-ircapture" and "-irmask" options are only
578 * needed to cope with nonstandard TAPs, or to specify more bits.
580 pTap->ir_capture_mask = 0x03;
581 pTap->ir_capture_value = 0x01;
584 e = Jim_GetOpt_Nvp(goi, opts, &n);
586 Jim_GetOpt_NvpUnknown(goi, opts, 0);
591 LOG_DEBUG("Processing option: %s", n->name);
593 case NTAP_OPT_ENABLED:
594 pTap->disabled_after_reset = false;
596 case NTAP_OPT_DISABLED:
597 pTap->disabled_after_reset = true;
599 case NTAP_OPT_EXPECTED_ID:
600 e = jim_newtap_expected_id(n, goi, pTap);
608 case NTAP_OPT_IRMASK:
609 case NTAP_OPT_IRCAPTURE:
610 e = jim_newtap_ir_param(n, goi, pTap);
617 case NTAP_OPT_VERSION:
618 pTap->ignore_version = true;
620 } /* switch (n->value) */
621 } /* while (goi->argc) */
623 /* default is enabled-after-reset */
624 pTap->enabled = !pTap->disabled_after_reset;
626 /* Did all the required option bits get cleared? */
627 if (pTap->ir_length != 0) {
632 Jim_SetResultFormatted(goi->interp,
633 "newtap: %s missing IR length",
639 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
641 struct jtag_tap_event_action *jteap;
644 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next) {
645 if (jteap->event != e)
648 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
649 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
650 tap->dotted_name, e, nvp->name,
651 Jim_GetString(jteap->body, NULL));
653 retval = Jim_EvalObj(jteap->interp, jteap->body);
654 if (retval == JIM_RETURN)
655 retval = jteap->interp->returnCode;
657 if (retval != JIM_OK) {
658 Jim_MakeErrorMessage(jteap->interp);
659 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
664 case JTAG_TAP_EVENT_ENABLE:
665 case JTAG_TAP_EVENT_DISABLE:
666 /* NOTE: we currently assume the handlers
667 * can't fail. Right here is where we should
668 * really be verifying the scan chains ...
670 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
671 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
672 tap->enabled ? "enabled" : "disabled");
680 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
683 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
685 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
688 struct command_context *context = current_command_context(interp);
689 int e = jtag_init_inner(context);
691 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
692 Jim_IncrRefCount(eObj);
693 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
694 Jim_DecrRefCount(goi.interp, eObj);
700 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
704 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
706 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
709 struct command_context *context = current_command_context(interp);
710 if (transport_is_jtag())
711 e = jtag_init_reset(context);
712 else if (transport_is_swd())
713 e = swd_init_reset(context);
716 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
717 Jim_IncrRefCount(eObj);
718 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
719 Jim_DecrRefCount(goi.interp, eObj);
725 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
728 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
729 return jim_newtap_cmd(&goi);
732 static bool jtag_tap_enable(struct jtag_tap *t)
736 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
740 /* FIXME add JTAG sanity checks, w/o TLR
741 * - scan chain length grew by one (this)
742 * - IDs and IR lengths are as expected
744 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
747 static bool jtag_tap_disable(struct jtag_tap *t)
751 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
755 /* FIXME add JTAG sanity checks, w/o TLR
756 * - scan chain length shrank by one (this)
757 * - IDs and IR lengths are as expected
759 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
763 int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
765 const char *cmd_name = Jim_GetString(argv[0], NULL);
767 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
769 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
775 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
779 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
780 /* do nothing, just return the value */
781 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
782 if (!jtag_tap_enable(t)) {
783 LOG_WARNING("failed to enable tap %s", t->dotted_name);
786 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
787 if (!jtag_tap_disable(t)) {
788 LOG_WARNING("failed to disable tap %s", t->dotted_name);
792 LOG_ERROR("command '%s' unknown", cmd_name);
796 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
800 int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
802 const char *cmd_name = Jim_GetString(argv[0], NULL);
804 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
805 goi.isconfigure = !strcmp(cmd_name, "configure");
806 if (goi.argc < 2 + goi.isconfigure) {
807 Jim_WrongNumArgs(goi.interp, 0, NULL,
808 "<tap_name> <attribute> ...");
815 Jim_GetOpt_Obj(&goi, &o);
816 t = jtag_tap_by_jim_obj(goi.interp, o);
820 return jtag_tap_configure_cmd(&goi, t);
823 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
826 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
828 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
831 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
832 struct jtag_tap *tap;
834 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
835 Jim_ListAppendElement(goi.interp,
836 Jim_GetResult(goi.interp),
837 Jim_NewStringObj(goi.interp,
838 tap->dotted_name, -1));
843 COMMAND_HANDLER(handle_jtag_init_command)
846 return ERROR_COMMAND_SYNTAX_ERROR;
848 static bool jtag_initialized;
849 if (jtag_initialized) {
850 LOG_INFO("'jtag init' has already been called");
853 jtag_initialized = true;
855 LOG_DEBUG("Initializing jtag devices...");
856 return jtag_init(CMD_CTX);
859 static const struct command_registration jtag_subcommand_handlers[] = {
863 .handler = handle_jtag_init_command,
864 .help = "initialize jtag scan chain",
870 .jim_handler = jim_jtag_arp_init,
871 .help = "Validates JTAG scan chain against the list of "
872 "declared TAPs using just the four standard JTAG "
876 .name = "arp_init-reset",
878 .jim_handler = jim_jtag_arp_init_reset,
879 .help = "Uses TRST and SRST to try resetting everything on "
880 "the JTAG scan chain, then performs 'jtag arp_init'."
884 .mode = COMMAND_CONFIG,
885 .jim_handler = jim_jtag_newtap,
886 .help = "Create a new TAP instance named basename.tap_type, "
887 "and appends it to the scan chain.",
888 .usage = "basename tap_type '-irlen' count "
889 "['-enable'|'-disable'] "
890 "['-expected_id' number] "
891 "['-ignore-version'] "
892 "['-ircapture' number] "
896 .name = "tapisenabled",
897 .mode = COMMAND_EXEC,
898 .jim_handler = jim_jtag_tap_enabler,
899 .help = "Returns a Tcl boolean (0/1) indicating whether "
900 "the TAP is enabled (1) or not (0).",
905 .mode = COMMAND_EXEC,
906 .jim_handler = jim_jtag_tap_enabler,
907 .help = "Try to enable the specified TAP using the "
908 "'tap-enable' TAP event.",
912 .name = "tapdisable",
913 .mode = COMMAND_EXEC,
914 .jim_handler = jim_jtag_tap_enabler,
915 .help = "Try to disable the specified TAP using the "
916 "'tap-disable' TAP event.",
922 .jim_handler = jim_jtag_configure,
923 .help = "Provide a Tcl handler for the specified "
925 .usage = "tap_name '-event' event_name handler",
929 .mode = COMMAND_EXEC,
930 .jim_handler = jim_jtag_configure,
931 .help = "Return any Tcl handler for the specified "
933 .usage = "tap_name '-event' event_name",
938 .jim_handler = jim_jtag_names,
939 .help = "Returns list of all JTAG tap names.",
942 .chain = jtag_command_handlers_to_move,
944 COMMAND_REGISTRATION_DONE
947 void jtag_notify_event(enum jtag_event event)
949 struct jtag_tap *tap;
951 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
952 jtag_tap_handle_event(tap, event);
956 COMMAND_HANDLER(handle_scan_chain_command)
958 struct jtag_tap *tap;
959 char expected_id[12];
961 tap = jtag_all_taps();
963 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
965 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
968 uint32_t expected, expected_mask, ii;
970 snprintf(expected_id, sizeof(expected_id), "0x%08x",
971 (unsigned)((tap->expected_ids_cnt > 0)
972 ? tap->expected_ids[0]
974 if (tap->ignore_version)
975 expected_id[2] = '*';
977 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
978 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
981 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
982 tap->abs_chain_position,
984 tap->enabled ? 'Y' : 'n',
985 (unsigned int)(tap->idcode),
987 (unsigned int)(tap->ir_length),
988 (unsigned int)(expected),
989 (unsigned int)(expected_mask));
991 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
992 snprintf(expected_id, sizeof(expected_id), "0x%08x",
993 (unsigned) tap->expected_ids[ii]);
994 if (tap->ignore_version)
995 expected_id[2] = '*';
1002 tap = tap->next_tap;
1008 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1011 return ERROR_COMMAND_SYNTAX_ERROR;
1012 if (CMD_ARGC == 1) {
1014 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1016 jtag_set_ntrst_delay(delay);
1018 command_print(CMD, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1022 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1025 return ERROR_COMMAND_SYNTAX_ERROR;
1026 if (CMD_ARGC == 1) {
1028 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1030 jtag_set_ntrst_assert_width(delay);
1032 command_print(CMD, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1036 COMMAND_HANDLER(handle_jtag_rclk_command)
1039 return ERROR_COMMAND_SYNTAX_ERROR;
1041 int retval = ERROR_OK;
1042 if (CMD_ARGC == 1) {
1044 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1046 retval = jtag_config_rclk(khz);
1047 if (ERROR_OK != retval)
1051 int cur_khz = jtag_get_speed_khz();
1052 retval = jtag_get_speed_readable(&cur_khz);
1053 if (ERROR_OK != retval)
1057 command_print(CMD, "RCLK not supported - fallback to %d kHz", cur_khz);
1059 command_print(CMD, "RCLK - adaptive");
1064 COMMAND_HANDLER(handle_runtest_command)
1067 return ERROR_COMMAND_SYNTAX_ERROR;
1069 unsigned num_clocks;
1070 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1072 jtag_add_runtest(num_clocks, TAP_IDLE);
1073 return jtag_execute_queue();
1077 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1078 * should be stable ... and *NOT* a shift state, otherwise free-running
1079 * jtag clocks could change the values latched by the update state.
1080 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1081 * and "drscan" commands are a write-only subset of what SVF provides.
1084 COMMAND_HANDLER(handle_irscan_command)
1087 struct scan_field *fields;
1088 struct jtag_tap *tap = NULL;
1089 tap_state_t endstate;
1091 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1092 return ERROR_COMMAND_SYNTAX_ERROR;
1094 /* optional "-endstate" "statename" at the end of the arguments,
1095 * so that e.g. IRPAUSE can let us load the data register before
1096 * entering RUN/IDLE to execute the instruction we load here.
1098 endstate = TAP_IDLE;
1100 if (CMD_ARGC >= 4) {
1101 /* have at least one pair of numbers.
1102 * is last pair the magic text? */
1103 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1104 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1105 if (endstate == TAP_INVALID)
1106 return ERROR_COMMAND_SYNTAX_ERROR;
1107 if (!scan_is_safe(endstate))
1108 LOG_WARNING("unstable irscan endstate \"%s\"",
1109 CMD_ARGV[CMD_ARGC - 1]);
1114 int num_fields = CMD_ARGC / 2;
1115 if (num_fields > 1) {
1116 /* we really should be looking at plain_ir_scan if we want
1117 * anything more fancy.
1119 LOG_ERROR("Specify a single value for tap");
1120 return ERROR_COMMAND_SYNTAX_ERROR;
1123 fields = calloc(num_fields, sizeof(*fields));
1126 for (i = 0; i < num_fields; i++) {
1127 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1130 command_print(CMD, "Tap: %s unknown", CMD_ARGV[i*2]);
1135 retval = parse_u64(CMD_ARGV[i * 2 + 1], &value);
1136 if (ERROR_OK != retval)
1139 int field_size = tap->ir_length;
1140 fields[i].num_bits = field_size;
1141 uint8_t *v = calloc(1, DIV_ROUND_UP(field_size, 8));
1143 LOG_ERROR("Out of memory");
1147 buf_set_u64(v, 0, field_size, value);
1148 fields[i].out_value = v;
1149 fields[i].in_value = NULL;
1152 /* did we have an endstate? */
1153 jtag_add_ir_scan(tap, fields, endstate);
1155 retval = jtag_execute_queue();
1158 for (i = 0; i < num_fields; i++) {
1159 if (NULL != fields[i].out_value)
1160 free((void *)fields[i].out_value);
1168 COMMAND_HANDLER(handle_verify_ircapture_command)
1171 return ERROR_COMMAND_SYNTAX_ERROR;
1173 if (CMD_ARGC == 1) {
1175 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1176 jtag_set_verify_capture_ir(enable);
1179 const char *status = jtag_will_verify_capture_ir() ? "enabled" : "disabled";
1180 command_print(CMD, "verify Capture-IR is %s", status);
1185 COMMAND_HANDLER(handle_verify_jtag_command)
1188 return ERROR_COMMAND_SYNTAX_ERROR;
1190 if (CMD_ARGC == 1) {
1192 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1193 jtag_set_verify(enable);
1196 const char *status = jtag_will_verify() ? "enabled" : "disabled";
1197 command_print(CMD, "verify jtag capture is %s", status);
1202 COMMAND_HANDLER(handle_tms_sequence_command)
1205 return ERROR_COMMAND_SYNTAX_ERROR;
1207 if (CMD_ARGC == 1) {
1209 if (strcmp(CMD_ARGV[0], "short") == 0)
1210 use_new_table = true;
1211 else if (strcmp(CMD_ARGV[0], "long") == 0)
1212 use_new_table = false;
1214 return ERROR_COMMAND_SYNTAX_ERROR;
1216 tap_use_new_tms_table(use_new_table);
1219 command_print(CMD, "tms sequence is %s",
1220 tap_uses_new_tms_table() ? "short" : "long");
1225 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1228 return ERROR_COMMAND_SYNTAX_ERROR;
1231 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1233 jtag_set_flush_queue_sleep(sleep_ms);
1238 COMMAND_HANDLER(handle_wait_srst_deassert)
1241 return ERROR_COMMAND_SYNTAX_ERROR;
1244 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1245 if ((timeout_ms <= 0) || (timeout_ms > 100000)) {
1246 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1250 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1252 int64_t then = timeval_ms();
1253 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1254 if ((timeval_ms() - then) > timeout_ms) {
1255 LOG_ERROR("Timed out");
1261 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1262 if ((timeval_ms() - then) > timeout_ms) {
1263 LOG_ERROR("Timed out");
1273 static const struct command_registration jtag_command_handlers[] = {
1276 .name = "jtag_flush_queue_sleep",
1277 .handler = handle_jtag_flush_queue_sleep,
1278 .mode = COMMAND_ANY,
1279 .help = "For debug purposes(simulate long delays of interface) "
1280 "to test performance or change in behavior. Default 0ms.",
1281 .usage = "[sleep in ms]",
1284 .name = "jtag_rclk",
1285 .handler = handle_jtag_rclk_command,
1286 .mode = COMMAND_ANY,
1287 .help = "With an argument, change to to use adaptive clocking "
1288 "if possible; else to use the fallback speed. "
1289 "With or without argument, display current setting.",
1290 .usage = "[fallback_speed_khz]",
1293 .name = "jtag_ntrst_delay",
1294 .handler = handle_jtag_ntrst_delay_command,
1295 .mode = COMMAND_ANY,
1296 .help = "delay after deasserting trst in ms",
1297 .usage = "[milliseconds]",
1300 .name = "jtag_ntrst_assert_width",
1301 .handler = handle_jtag_ntrst_assert_width_command,
1302 .mode = COMMAND_ANY,
1303 .help = "delay after asserting trst in ms",
1304 .usage = "[milliseconds]",
1307 .name = "scan_chain",
1308 .handler = handle_scan_chain_command,
1309 .mode = COMMAND_ANY,
1310 .help = "print current scan chain configuration",
1315 .handler = handle_runtest_command,
1316 .mode = COMMAND_EXEC,
1317 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1318 .usage = "num_cycles"
1322 .handler = handle_irscan_command,
1323 .mode = COMMAND_EXEC,
1324 .help = "Execute Instruction Register (IR) scan. The "
1325 "specified opcodes are put into each TAP's IR, "
1326 "and other TAPs are put in BYPASS.",
1327 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1330 .name = "verify_ircapture",
1331 .handler = handle_verify_ircapture_command,
1332 .mode = COMMAND_ANY,
1333 .help = "Display or assign flag controlling whether to "
1334 "verify values captured during Capture-IR.",
1335 .usage = "['enable'|'disable']",
1338 .name = "verify_jtag",
1339 .handler = handle_verify_jtag_command,
1340 .mode = COMMAND_ANY,
1341 .help = "Display or assign flag controlling whether to "
1342 "verify values captured during IR and DR scans.",
1343 .usage = "['enable'|'disable']",
1346 .name = "tms_sequence",
1347 .handler = handle_tms_sequence_command,
1348 .mode = COMMAND_ANY,
1349 .help = "Display or change what style TMS sequences to use "
1350 "for JTAG state transitions: short (default) or "
1351 "long. Only for working around JTAG bugs.",
1352 /* Specifically for working around DRIVER bugs... */
1353 .usage = "['short'|'long']",
1356 .name = "wait_srst_deassert",
1357 .handler = handle_wait_srst_deassert,
1358 .mode = COMMAND_ANY,
1359 .help = "Wait for an SRST deassert. "
1360 "Useful for cases where you need something to happen within ms "
1361 "of an srst deassert. Timeout in ms ",
1366 .mode = COMMAND_ANY,
1367 .help = "perform jtag tap actions",
1370 .chain = jtag_subcommand_handlers,
1373 .chain = jtag_command_handlers_to_move,
1375 COMMAND_REGISTRATION_DONE
1378 int jtag_register_commands(struct command_context *cmd_ctx)
1380 return register_commands(cmd_ctx, NULL, jtag_command_handlers);