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);
198 for (i = 0; i < field_count; i++)
199 free(fields[i].in_value);
206 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
207 for (i = 2; i < argc; i += 2) {
211 Jim_GetLong(interp, args[i], &bits);
212 str = buf_to_hex_str(fields[field_count].in_value, bits);
213 free(fields[field_count].in_value);
215 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
220 Jim_SetResult(interp, list);
228 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
230 tap_state_t states[8];
232 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1))) {
233 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
237 script_debug(interp, argc, args);
240 for (i = 0; i < argc-1; i++) {
242 cp = Jim_GetString(args[i + 1], NULL);
243 states[i] = tap_state_by_name(cp);
245 /* update the error message */
246 Jim_SetResultFormatted(interp, "endstate: %s invalid", cp);
251 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue() != ERROR_OK)) {
252 Jim_SetResultString(interp, "pathmove: jtag execute failed", -1);
256 jtag_add_pathmove(argc - 2, states + 1);
258 if (jtag_execute_queue() != ERROR_OK) {
259 Jim_SetResultString(interp, "pathmove: failed", -1);
267 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
269 script_debug(interp, argc, args);
271 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
276 /* REVISIT Just what about these should "move" ... ?
277 * These registrations, into the main JTAG table?
279 * There's a minor compatibility issue, these all show up twice;
280 * that's not desirable:
281 * - jtag drscan ... NOT DOCUMENTED!
284 * The "irscan" command (for example) doesn't show twice.
286 static const struct command_registration jtag_command_handlers_to_move[] = {
289 .mode = COMMAND_EXEC,
290 .jim_handler = Jim_Command_drscan,
291 .help = "Execute Data Register (DR) scan for one TAP. "
292 "Other TAPs must be in BYPASS mode.",
293 .usage = "tap_name [num_bits value]* ['-endstate' state_name]",
296 .name = "flush_count",
297 .mode = COMMAND_EXEC,
298 .jim_handler = Jim_Command_flush_count,
299 .help = "Returns the number of times the JTAG queue "
304 .mode = COMMAND_EXEC,
305 .jim_handler = Jim_Command_pathmove,
306 .usage = "start_state state1 [state2 [state3 ...]]",
307 .help = "Move JTAG state machine from current state "
308 "(start_state) to state1, then state2, state3, etc.",
310 COMMAND_REGISTRATION_DONE
314 enum jtag_tap_cfg_param {
319 static Jim_Nvp nvp_config_opts[] = {
320 { .name = "-event", .value = JCFG_EVENT },
321 { .name = "-idcode", .value = JCFG_IDCODE },
323 { .name = NULL, .value = -1 }
326 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap *tap)
328 if (goi->argc == 0) {
329 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
334 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
336 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
340 if (goi->isconfigure) {
341 if (goi->argc != 1) {
342 Jim_WrongNumArgs(goi->interp,
345 "-event <event-name> <event-body>");
349 if (goi->argc != 0) {
350 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
355 struct jtag_tap_event_action *jteap = tap->event_action;
356 /* replace existing event body */
359 if (jteap->event == (enum jtag_event)n->value) {
366 Jim_SetEmptyResult(goi->interp);
368 if (goi->isconfigure) {
370 jteap = calloc(1, sizeof(*jteap));
371 else if (NULL != jteap->body)
372 Jim_DecrRefCount(goi->interp, jteap->body);
374 jteap->interp = goi->interp;
375 jteap->event = n->value;
378 Jim_GetOpt_Obj(goi, &o);
379 jteap->body = Jim_DuplicateObj(goi->interp, o);
380 Jim_IncrRefCount(jteap->body);
383 /* add to head of event list */
384 jteap->next = tap->event_action;
385 tap->event_action = jteap;
388 jteap->interp = goi->interp;
389 Jim_SetResult(goi->interp,
390 Jim_DuplicateObj(goi->interp, jteap->body));
395 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap *tap)
397 /* parse config or cget options */
398 while (goi->argc > 0) {
399 Jim_SetEmptyResult(goi->interp);
402 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
404 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
410 e = jtag_tap_configure_event(goi, tap);
415 if (goi->isconfigure) {
416 Jim_SetResultFormatted(goi->interp,
417 "not settable: %s", n->name);
420 if (goi->argc != 0) {
421 Jim_WrongNumArgs(goi->interp,
422 goi->argc, goi->argv,
427 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, tap->idcode));
430 Jim_SetResultFormatted(goi->interp, "unknown value: %s", n->name);
438 static int is_bad_irval(int ir_length, jim_wide w)
448 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
449 struct jtag_tap *pTap)
452 int e = Jim_GetOpt_Wide(goi, &w);
454 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
458 uint32_t *p = realloc(pTap->expected_ids,
459 (pTap->expected_ids_cnt + 1) * sizeof(uint32_t));
461 Jim_SetResultFormatted(goi->interp, "no memory");
465 pTap->expected_ids = p;
466 pTap->expected_ids[pTap->expected_ids_cnt++] = w;
471 #define NTAP_OPT_IRLEN 0
472 #define NTAP_OPT_IRMASK 1
473 #define NTAP_OPT_IRCAPTURE 2
474 #define NTAP_OPT_ENABLED 3
475 #define NTAP_OPT_DISABLED 4
476 #define NTAP_OPT_EXPECTED_ID 5
477 #define NTAP_OPT_VERSION 6
479 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
480 struct jtag_tap *pTap)
483 int e = Jim_GetOpt_Wide(goi, &w);
485 Jim_SetResultFormatted(goi->interp,
486 "option: %s bad parameter", n->name);
491 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value))) {
492 LOG_WARNING("%s: huge IR length %d",
493 pTap->dotted_name, (int) w);
497 case NTAP_OPT_IRMASK:
498 if (is_bad_irval(pTap->ir_length, w)) {
499 LOG_ERROR("%s: IR mask %x too big",
505 LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
506 pTap->ir_capture_mask = w;
508 case NTAP_OPT_IRCAPTURE:
509 if (is_bad_irval(pTap->ir_length, w)) {
510 LOG_ERROR("%s: IR capture %x too big",
511 pTap->dotted_name, (int) w);
515 LOG_WARNING("%s: nonstandard IR value",
517 pTap->ir_capture_value = w;
525 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
527 struct jtag_tap *pTap;
532 const Jim_Nvp opts[] = {
533 { .name = "-irlen", .value = NTAP_OPT_IRLEN },
534 { .name = "-irmask", .value = NTAP_OPT_IRMASK },
535 { .name = "-ircapture", .value = NTAP_OPT_IRCAPTURE },
536 { .name = "-enable", .value = NTAP_OPT_ENABLED },
537 { .name = "-disable", .value = NTAP_OPT_DISABLED },
538 { .name = "-expected-id", .value = NTAP_OPT_EXPECTED_ID },
539 { .name = "-ignore-version", .value = NTAP_OPT_VERSION },
540 { .name = NULL, .value = -1 },
543 pTap = calloc(1, sizeof(struct jtag_tap));
545 Jim_SetResultFormatted(goi->interp, "no memory");
550 * we expect CHIP + TAP + OPTIONS
553 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
559 Jim_GetOpt_String(goi, &tmp, NULL);
560 pTap->chip = strdup(tmp);
562 Jim_GetOpt_String(goi, &tmp, NULL);
563 pTap->tapname = strdup(tmp);
565 /* name + dot + name + null */
566 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
568 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
569 pTap->dotted_name = cp;
571 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
572 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
574 if (!transport_is_jtag()) {
575 /* SWD doesn't require any JTAG tap parameters */
576 pTap->enabled = true;
581 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
582 * that the default. The "-ircapture" and "-irmask" options are only
583 * needed to cope with nonstandard TAPs, or to specify more bits.
585 pTap->ir_capture_mask = 0x03;
586 pTap->ir_capture_value = 0x01;
589 e = Jim_GetOpt_Nvp(goi, opts, &n);
591 Jim_GetOpt_NvpUnknown(goi, opts, 0);
596 LOG_DEBUG("Processing option: %s", n->name);
598 case NTAP_OPT_ENABLED:
599 pTap->disabled_after_reset = false;
601 case NTAP_OPT_DISABLED:
602 pTap->disabled_after_reset = true;
604 case NTAP_OPT_EXPECTED_ID:
605 e = jim_newtap_expected_id(n, goi, pTap);
613 case NTAP_OPT_IRMASK:
614 case NTAP_OPT_IRCAPTURE:
615 e = jim_newtap_ir_param(n, goi, pTap);
622 case NTAP_OPT_VERSION:
623 pTap->ignore_version = true;
625 } /* switch (n->value) */
626 } /* while (goi->argc) */
628 /* default is enabled-after-reset */
629 pTap->enabled = !pTap->disabled_after_reset;
631 /* Did all the required option bits get cleared? */
632 if (pTap->ir_length != 0) {
637 Jim_SetResultFormatted(goi->interp,
638 "newtap: %s missing IR length",
644 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
646 struct jtag_tap_event_action *jteap;
649 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next) {
650 if (jteap->event != e)
653 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
654 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
655 tap->dotted_name, e, nvp->name,
656 Jim_GetString(jteap->body, NULL));
658 retval = Jim_EvalObj(jteap->interp, jteap->body);
659 if (retval == JIM_RETURN)
660 retval = jteap->interp->returnCode;
662 if (retval != JIM_OK) {
663 Jim_MakeErrorMessage(jteap->interp);
664 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
669 case JTAG_TAP_EVENT_ENABLE:
670 case JTAG_TAP_EVENT_DISABLE:
671 /* NOTE: we currently assume the handlers
672 * can't fail. Right here is where we should
673 * really be verifying the scan chains ...
675 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
676 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
677 tap->enabled ? "enabled" : "disabled");
685 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
688 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
690 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
693 struct command_context *context = current_command_context(interp);
694 int e = jtag_init_inner(context);
696 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
697 Jim_IncrRefCount(eObj);
698 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
699 Jim_DecrRefCount(goi.interp, eObj);
705 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
709 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
711 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
714 struct command_context *context = current_command_context(interp);
715 if (transport_is_jtag())
716 e = jtag_init_reset(context);
717 else if (transport_is_swd())
718 e = swd_init_reset(context);
721 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
722 Jim_IncrRefCount(eObj);
723 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
724 Jim_DecrRefCount(goi.interp, eObj);
730 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
733 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
734 return jim_newtap_cmd(&goi);
737 static bool jtag_tap_enable(struct jtag_tap *t)
741 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
745 /* FIXME add JTAG sanity checks, w/o TLR
746 * - scan chain length grew by one (this)
747 * - IDs and IR lengths are as expected
749 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
752 static bool jtag_tap_disable(struct jtag_tap *t)
756 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
760 /* FIXME add JTAG sanity checks, w/o TLR
761 * - scan chain length shrank by one (this)
762 * - IDs and IR lengths are as expected
764 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
768 int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
770 struct command *c = jim_to_command(interp);
771 const char *cmd_name = c->name;
773 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
775 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
781 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
785 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
786 /* do nothing, just return the value */
787 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
788 if (!jtag_tap_enable(t)) {
789 LOG_WARNING("failed to enable tap %s", t->dotted_name);
792 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
793 if (!jtag_tap_disable(t)) {
794 LOG_WARNING("failed to disable tap %s", t->dotted_name);
798 LOG_ERROR("command '%s' unknown", cmd_name);
802 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
806 int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
808 struct command *c = jim_to_command(interp);
809 const char *cmd_name = c->name;
811 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
812 goi.isconfigure = !strcmp(cmd_name, "configure");
813 if (goi.argc < 2 + goi.isconfigure) {
814 Jim_WrongNumArgs(goi.interp, 0, NULL,
815 "<tap_name> <attribute> ...");
822 Jim_GetOpt_Obj(&goi, &o);
823 t = jtag_tap_by_jim_obj(goi.interp, o);
827 return jtag_tap_configure_cmd(&goi, t);
830 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
833 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
835 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
838 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
839 struct jtag_tap *tap;
841 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
842 Jim_ListAppendElement(goi.interp,
843 Jim_GetResult(goi.interp),
844 Jim_NewStringObj(goi.interp,
845 tap->dotted_name, -1));
850 COMMAND_HANDLER(handle_jtag_init_command)
853 return ERROR_COMMAND_SYNTAX_ERROR;
855 static bool jtag_initialized;
856 if (jtag_initialized) {
857 LOG_INFO("'jtag init' has already been called");
860 jtag_initialized = true;
862 LOG_DEBUG("Initializing jtag devices...");
863 return jtag_init(CMD_CTX);
866 static const struct command_registration jtag_subcommand_handlers[] = {
870 .handler = handle_jtag_init_command,
871 .help = "initialize jtag scan chain",
877 .jim_handler = jim_jtag_arp_init,
878 .help = "Validates JTAG scan chain against the list of "
879 "declared TAPs using just the four standard JTAG "
883 .name = "arp_init-reset",
885 .jim_handler = jim_jtag_arp_init_reset,
886 .help = "Uses TRST and SRST to try resetting everything on "
887 "the JTAG scan chain, then performs 'jtag arp_init'."
891 .mode = COMMAND_CONFIG,
892 .jim_handler = jim_jtag_newtap,
893 .help = "Create a new TAP instance named basename.tap_type, "
894 "and appends it to the scan chain.",
895 .usage = "basename tap_type '-irlen' count "
896 "['-enable'|'-disable'] "
897 "['-expected_id' number] "
898 "['-ignore-version'] "
899 "['-ircapture' number] "
903 .name = "tapisenabled",
904 .mode = COMMAND_EXEC,
905 .jim_handler = jim_jtag_tap_enabler,
906 .help = "Returns a Tcl boolean (0/1) indicating whether "
907 "the TAP is enabled (1) or not (0).",
912 .mode = COMMAND_EXEC,
913 .jim_handler = jim_jtag_tap_enabler,
914 .help = "Try to enable the specified TAP using the "
915 "'tap-enable' TAP event.",
919 .name = "tapdisable",
920 .mode = COMMAND_EXEC,
921 .jim_handler = jim_jtag_tap_enabler,
922 .help = "Try to disable the specified TAP using the "
923 "'tap-disable' TAP event.",
929 .jim_handler = jim_jtag_configure,
930 .help = "Provide a Tcl handler for the specified "
932 .usage = "tap_name '-event' event_name handler",
936 .mode = COMMAND_EXEC,
937 .jim_handler = jim_jtag_configure,
938 .help = "Return any Tcl handler for the specified "
940 .usage = "tap_name '-event' event_name",
945 .jim_handler = jim_jtag_names,
946 .help = "Returns list of all JTAG tap names.",
949 .chain = jtag_command_handlers_to_move,
951 COMMAND_REGISTRATION_DONE
954 void jtag_notify_event(enum jtag_event event)
956 struct jtag_tap *tap;
958 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
959 jtag_tap_handle_event(tap, event);
963 COMMAND_HANDLER(handle_scan_chain_command)
965 struct jtag_tap *tap;
966 char expected_id[12];
968 tap = jtag_all_taps();
970 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
972 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
975 uint32_t expected, expected_mask, ii;
977 snprintf(expected_id, sizeof(expected_id), "0x%08x",
978 (unsigned)((tap->expected_ids_cnt > 0)
979 ? tap->expected_ids[0]
981 if (tap->ignore_version)
982 expected_id[2] = '*';
984 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
985 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
988 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
989 tap->abs_chain_position,
991 tap->enabled ? 'Y' : 'n',
992 (unsigned int)(tap->idcode),
994 (unsigned int)(tap->ir_length),
995 (unsigned int)(expected),
996 (unsigned int)(expected_mask));
998 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
999 snprintf(expected_id, sizeof(expected_id), "0x%08x",
1000 (unsigned) tap->expected_ids[ii]);
1001 if (tap->ignore_version)
1002 expected_id[2] = '*';
1009 tap = tap->next_tap;
1015 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1018 return ERROR_COMMAND_SYNTAX_ERROR;
1019 if (CMD_ARGC == 1) {
1021 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1023 jtag_set_ntrst_delay(delay);
1025 command_print(CMD, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1029 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1032 return ERROR_COMMAND_SYNTAX_ERROR;
1033 if (CMD_ARGC == 1) {
1035 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1037 jtag_set_ntrst_assert_width(delay);
1039 command_print(CMD, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1043 COMMAND_HANDLER(handle_jtag_rclk_command)
1046 return ERROR_COMMAND_SYNTAX_ERROR;
1048 int retval = ERROR_OK;
1049 if (CMD_ARGC == 1) {
1051 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1053 retval = jtag_config_rclk(khz);
1054 if (ERROR_OK != retval)
1058 int cur_khz = jtag_get_speed_khz();
1059 retval = jtag_get_speed_readable(&cur_khz);
1060 if (ERROR_OK != retval)
1064 command_print(CMD, "RCLK not supported - fallback to %d kHz", cur_khz);
1066 command_print(CMD, "RCLK - adaptive");
1071 COMMAND_HANDLER(handle_runtest_command)
1074 return ERROR_COMMAND_SYNTAX_ERROR;
1076 unsigned num_clocks;
1077 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1079 jtag_add_runtest(num_clocks, TAP_IDLE);
1080 return jtag_execute_queue();
1084 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1085 * should be stable ... and *NOT* a shift state, otherwise free-running
1086 * jtag clocks could change the values latched by the update state.
1087 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1088 * and "drscan" commands are a write-only subset of what SVF provides.
1091 COMMAND_HANDLER(handle_irscan_command)
1094 struct scan_field *fields;
1095 struct jtag_tap *tap = NULL;
1096 tap_state_t endstate;
1098 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1099 return ERROR_COMMAND_SYNTAX_ERROR;
1101 /* optional "-endstate" "statename" at the end of the arguments,
1102 * so that e.g. IRPAUSE can let us load the data register before
1103 * entering RUN/IDLE to execute the instruction we load here.
1105 endstate = TAP_IDLE;
1107 if (CMD_ARGC >= 4) {
1108 /* have at least one pair of numbers.
1109 * is last pair the magic text? */
1110 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1111 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1112 if (endstate == TAP_INVALID)
1113 return ERROR_COMMAND_SYNTAX_ERROR;
1114 if (!scan_is_safe(endstate))
1115 LOG_WARNING("unstable irscan endstate \"%s\"",
1116 CMD_ARGV[CMD_ARGC - 1]);
1121 int num_fields = CMD_ARGC / 2;
1122 if (num_fields > 1) {
1123 /* we really should be looking at plain_ir_scan if we want
1124 * anything more fancy.
1126 LOG_ERROR("Specify a single value for tap");
1127 return ERROR_COMMAND_SYNTAX_ERROR;
1130 fields = calloc(num_fields, sizeof(*fields));
1133 for (i = 0; i < num_fields; i++) {
1134 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1137 command_print(CMD, "Tap: %s unknown", CMD_ARGV[i*2]);
1142 retval = parse_u64(CMD_ARGV[i * 2 + 1], &value);
1143 if (ERROR_OK != retval)
1146 int field_size = tap->ir_length;
1147 fields[i].num_bits = field_size;
1148 uint8_t *v = calloc(1, DIV_ROUND_UP(field_size, 8));
1150 LOG_ERROR("Out of memory");
1154 buf_set_u64(v, 0, field_size, value);
1155 fields[i].out_value = v;
1156 fields[i].in_value = NULL;
1159 /* did we have an endstate? */
1160 jtag_add_ir_scan(tap, fields, endstate);
1162 retval = jtag_execute_queue();
1165 for (i = 0; i < num_fields; i++)
1166 free((void *)fields[i].out_value);
1173 COMMAND_HANDLER(handle_verify_ircapture_command)
1176 return ERROR_COMMAND_SYNTAX_ERROR;
1178 if (CMD_ARGC == 1) {
1180 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1181 jtag_set_verify_capture_ir(enable);
1184 const char *status = jtag_will_verify_capture_ir() ? "enabled" : "disabled";
1185 command_print(CMD, "verify Capture-IR is %s", status);
1190 COMMAND_HANDLER(handle_verify_jtag_command)
1193 return ERROR_COMMAND_SYNTAX_ERROR;
1195 if (CMD_ARGC == 1) {
1197 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1198 jtag_set_verify(enable);
1201 const char *status = jtag_will_verify() ? "enabled" : "disabled";
1202 command_print(CMD, "verify jtag capture is %s", status);
1207 COMMAND_HANDLER(handle_tms_sequence_command)
1210 return ERROR_COMMAND_SYNTAX_ERROR;
1212 if (CMD_ARGC == 1) {
1214 if (strcmp(CMD_ARGV[0], "short") == 0)
1215 use_new_table = true;
1216 else if (strcmp(CMD_ARGV[0], "long") == 0)
1217 use_new_table = false;
1219 return ERROR_COMMAND_SYNTAX_ERROR;
1221 tap_use_new_tms_table(use_new_table);
1224 command_print(CMD, "tms sequence is %s",
1225 tap_uses_new_tms_table() ? "short" : "long");
1230 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1233 return ERROR_COMMAND_SYNTAX_ERROR;
1236 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1238 jtag_set_flush_queue_sleep(sleep_ms);
1243 COMMAND_HANDLER(handle_wait_srst_deassert)
1246 return ERROR_COMMAND_SYNTAX_ERROR;
1249 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1250 if ((timeout_ms <= 0) || (timeout_ms > 100000)) {
1251 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1255 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1257 int64_t then = timeval_ms();
1258 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1259 if ((timeval_ms() - then) > timeout_ms) {
1260 LOG_ERROR("Timed out");
1266 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1267 if ((timeval_ms() - then) > timeout_ms) {
1268 LOG_ERROR("Timed out");
1278 static const struct command_registration jtag_command_handlers[] = {
1281 .name = "jtag_flush_queue_sleep",
1282 .handler = handle_jtag_flush_queue_sleep,
1283 .mode = COMMAND_ANY,
1284 .help = "For debug purposes(simulate long delays of interface) "
1285 "to test performance or change in behavior. Default 0ms.",
1286 .usage = "[sleep in ms]",
1289 .name = "jtag_rclk",
1290 .handler = handle_jtag_rclk_command,
1291 .mode = COMMAND_ANY,
1292 .help = "With an argument, change to to use adaptive clocking "
1293 "if possible; else to use the fallback speed. "
1294 "With or without argument, display current setting.",
1295 .usage = "[fallback_speed_khz]",
1298 .name = "jtag_ntrst_delay",
1299 .handler = handle_jtag_ntrst_delay_command,
1300 .mode = COMMAND_ANY,
1301 .help = "delay after deasserting trst in ms",
1302 .usage = "[milliseconds]",
1305 .name = "jtag_ntrst_assert_width",
1306 .handler = handle_jtag_ntrst_assert_width_command,
1307 .mode = COMMAND_ANY,
1308 .help = "delay after asserting trst in ms",
1309 .usage = "[milliseconds]",
1312 .name = "scan_chain",
1313 .handler = handle_scan_chain_command,
1314 .mode = COMMAND_ANY,
1315 .help = "print current scan chain configuration",
1320 .handler = handle_runtest_command,
1321 .mode = COMMAND_EXEC,
1322 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1323 .usage = "num_cycles"
1327 .handler = handle_irscan_command,
1328 .mode = COMMAND_EXEC,
1329 .help = "Execute Instruction Register (IR) scan. The "
1330 "specified opcodes are put into each TAP's IR, "
1331 "and other TAPs are put in BYPASS.",
1332 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1335 .name = "verify_ircapture",
1336 .handler = handle_verify_ircapture_command,
1337 .mode = COMMAND_ANY,
1338 .help = "Display or assign flag controlling whether to "
1339 "verify values captured during Capture-IR.",
1340 .usage = "['enable'|'disable']",
1343 .name = "verify_jtag",
1344 .handler = handle_verify_jtag_command,
1345 .mode = COMMAND_ANY,
1346 .help = "Display or assign flag controlling whether to "
1347 "verify values captured during IR and DR scans.",
1348 .usage = "['enable'|'disable']",
1351 .name = "tms_sequence",
1352 .handler = handle_tms_sequence_command,
1353 .mode = COMMAND_ANY,
1354 .help = "Display or change what style TMS sequences to use "
1355 "for JTAG state transitions: short (default) or "
1356 "long. Only for working around JTAG bugs.",
1357 /* Specifically for working around DRIVER bugs... */
1358 .usage = "['short'|'long']",
1361 .name = "wait_srst_deassert",
1362 .handler = handle_wait_srst_deassert,
1363 .mode = COMMAND_ANY,
1364 .help = "Wait for an SRST deassert. "
1365 "Useful for cases where you need something to happen within ms "
1366 "of an srst deassert. Timeout in ms ",
1371 .mode = COMMAND_ANY,
1372 .help = "perform jtag tap actions",
1375 .chain = jtag_subcommand_handlers,
1378 .chain = jtag_command_handlers_to_move,
1380 COMMAND_REGISTRATION_DONE
1383 int jtag_register_commands(struct command_context *cmd_ctx)
1385 return register_commands(cmd_ctx, NULL, jtag_command_handlers);