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 /* validate arguments as numbers */
113 for (i = 2; i < argc; i += 2) {
117 e = Jim_GetLong(interp, args[i], &bits);
118 /* If valid - try next arg */
122 /* Not valid.. are we at the end? */
123 if (((i + 2) != argc)) {
124 /* nope, then error */
128 /* it could be: "-endstate FOO"
129 * e.g. DRPAUSE so we can issue more instructions
130 * before entering RUN/IDLE and executing them.
133 /* get arg as a string. */
134 cp = Jim_GetString(args[i], NULL);
135 /* is it the magic? */
136 if (0 == strcmp("-endstate", cp)) {
137 /* is the statename valid? */
138 cp = Jim_GetString(args[i + 1], NULL);
140 /* see if it is a valid state name */
141 endstate = tap_state_by_name(cp);
143 /* update the error message */
144 Jim_SetResultFormatted(interp, "endstate: %s invalid", cp);
146 if (!scan_is_safe(endstate))
147 LOG_WARNING("drscan with unsafe "
148 "endstate \"%s\"", cp);
150 /* valid - so clear the error */
152 /* and remove the last 2 args */
157 /* Still an error? */
159 return e; /* too bad */
160 } /* validate args */
164 tap = jtag_tap_by_jim_obj(interp, args[1]);
168 num_fields = (argc-2)/2;
169 if (num_fields <= 0) {
170 Jim_SetResultString(interp, "drscan: no scan fields supplied", -1);
173 fields = malloc(sizeof(struct scan_field) * num_fields);
174 for (i = 2; i < argc; i += 2) {
179 Jim_GetLong(interp, args[i], &bits);
180 str = Jim_GetString(args[i + 1], &len);
182 fields[field_count].num_bits = bits;
183 void *t = malloc(DIV_ROUND_UP(bits, 8));
184 fields[field_count].out_value = t;
185 str_to_buf(str, len, t, bits, 0);
186 fields[field_count].in_value = t;
190 jtag_add_dr_scan(tap, num_fields, fields, endstate);
192 retval = jtag_execute_queue();
193 if (retval != ERROR_OK) {
194 Jim_SetResultString(interp, "drscan: jtag execute failed", -1);
196 for (i = 0; i < field_count; i++)
197 free(fields[i].in_value);
204 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
205 for (i = 2; i < argc; i += 2) {
209 Jim_GetLong(interp, args[i], &bits);
210 str = buf_to_hex_str(fields[field_count].in_value, bits);
211 free(fields[field_count].in_value);
213 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
218 Jim_SetResult(interp, list);
226 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
228 tap_state_t states[8];
230 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1))) {
231 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
236 for (i = 0; i < argc-1; i++) {
238 cp = Jim_GetString(args[i + 1], NULL);
239 states[i] = tap_state_by_name(cp);
241 /* update the error message */
242 Jim_SetResultFormatted(interp, "endstate: %s invalid", cp);
247 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue() != ERROR_OK)) {
248 Jim_SetResultString(interp, "pathmove: jtag execute failed", -1);
252 jtag_add_pathmove(argc - 2, states + 1);
254 if (jtag_execute_queue() != ERROR_OK) {
255 Jim_SetResultString(interp, "pathmove: failed", -1);
263 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
265 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
270 /* REVISIT Just what about these should "move" ... ?
271 * These registrations, into the main JTAG table?
273 * There's a minor compatibility issue, these all show up twice;
274 * that's not desirable:
275 * - jtag drscan ... NOT DOCUMENTED!
278 * The "irscan" command (for example) doesn't show twice.
280 static const struct command_registration jtag_command_handlers_to_move[] = {
283 .mode = COMMAND_EXEC,
284 .jim_handler = Jim_Command_drscan,
285 .help = "Execute Data Register (DR) scan for one TAP. "
286 "Other TAPs must be in BYPASS mode.",
287 .usage = "tap_name [num_bits value]* ['-endstate' state_name]",
290 .name = "flush_count",
291 .mode = COMMAND_EXEC,
292 .jim_handler = Jim_Command_flush_count,
293 .help = "Returns the number of times the JTAG queue "
298 .mode = COMMAND_EXEC,
299 .jim_handler = Jim_Command_pathmove,
300 .usage = "start_state state1 [state2 [state3 ...]]",
301 .help = "Move JTAG state machine from current state "
302 "(start_state) to state1, then state2, state3, etc.",
304 COMMAND_REGISTRATION_DONE
308 enum jtag_tap_cfg_param {
313 static Jim_Nvp nvp_config_opts[] = {
314 { .name = "-event", .value = JCFG_EVENT },
315 { .name = "-idcode", .value = JCFG_IDCODE },
317 { .name = NULL, .value = -1 }
320 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap *tap)
322 if (goi->argc == 0) {
323 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
328 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
330 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
334 if (goi->isconfigure) {
335 if (goi->argc != 1) {
336 Jim_WrongNumArgs(goi->interp,
339 "-event <event-name> <event-body>");
343 if (goi->argc != 0) {
344 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
349 struct jtag_tap_event_action *jteap = tap->event_action;
350 /* replace existing event body */
353 if (jteap->event == (enum jtag_event)n->value) {
360 Jim_SetEmptyResult(goi->interp);
362 if (goi->isconfigure) {
364 jteap = calloc(1, sizeof(*jteap));
365 else if (NULL != jteap->body)
366 Jim_DecrRefCount(goi->interp, jteap->body);
368 jteap->interp = goi->interp;
369 jteap->event = n->value;
372 Jim_GetOpt_Obj(goi, &o);
373 jteap->body = Jim_DuplicateObj(goi->interp, o);
374 Jim_IncrRefCount(jteap->body);
377 /* add to head of event list */
378 jteap->next = tap->event_action;
379 tap->event_action = jteap;
382 jteap->interp = goi->interp;
383 Jim_SetResult(goi->interp,
384 Jim_DuplicateObj(goi->interp, jteap->body));
389 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap *tap)
391 /* parse config or cget options */
392 while (goi->argc > 0) {
393 Jim_SetEmptyResult(goi->interp);
396 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
398 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
404 e = jtag_tap_configure_event(goi, tap);
409 if (goi->isconfigure) {
410 Jim_SetResultFormatted(goi->interp,
411 "not settable: %s", n->name);
414 if (goi->argc != 0) {
415 Jim_WrongNumArgs(goi->interp,
416 goi->argc, goi->argv,
421 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, tap->idcode));
424 Jim_SetResultFormatted(goi->interp, "unknown value: %s", n->name);
432 static int is_bad_irval(int ir_length, jim_wide w)
442 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
443 struct jtag_tap *pTap)
446 int e = Jim_GetOpt_Wide(goi, &w);
448 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
452 uint32_t *p = realloc(pTap->expected_ids,
453 (pTap->expected_ids_cnt + 1) * sizeof(uint32_t));
455 Jim_SetResultFormatted(goi->interp, "no memory");
459 pTap->expected_ids = p;
460 pTap->expected_ids[pTap->expected_ids_cnt++] = w;
465 #define NTAP_OPT_IRLEN 0
466 #define NTAP_OPT_IRMASK 1
467 #define NTAP_OPT_IRCAPTURE 2
468 #define NTAP_OPT_ENABLED 3
469 #define NTAP_OPT_DISABLED 4
470 #define NTAP_OPT_EXPECTED_ID 5
471 #define NTAP_OPT_VERSION 6
473 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
474 struct jtag_tap *pTap)
477 int e = Jim_GetOpt_Wide(goi, &w);
479 Jim_SetResultFormatted(goi->interp,
480 "option: %s bad parameter", n->name);
485 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value))) {
486 LOG_WARNING("%s: huge IR length %d",
487 pTap->dotted_name, (int) w);
491 case NTAP_OPT_IRMASK:
492 if (is_bad_irval(pTap->ir_length, w)) {
493 LOG_ERROR("%s: IR mask %x too big",
499 LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
500 pTap->ir_capture_mask = w;
502 case NTAP_OPT_IRCAPTURE:
503 if (is_bad_irval(pTap->ir_length, w)) {
504 LOG_ERROR("%s: IR capture %x too big",
505 pTap->dotted_name, (int) w);
509 LOG_WARNING("%s: nonstandard IR value",
511 pTap->ir_capture_value = w;
519 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
521 struct jtag_tap *pTap;
526 const Jim_Nvp opts[] = {
527 { .name = "-irlen", .value = NTAP_OPT_IRLEN },
528 { .name = "-irmask", .value = NTAP_OPT_IRMASK },
529 { .name = "-ircapture", .value = NTAP_OPT_IRCAPTURE },
530 { .name = "-enable", .value = NTAP_OPT_ENABLED },
531 { .name = "-disable", .value = NTAP_OPT_DISABLED },
532 { .name = "-expected-id", .value = NTAP_OPT_EXPECTED_ID },
533 { .name = "-ignore-version", .value = NTAP_OPT_VERSION },
534 { .name = NULL, .value = -1 },
537 pTap = calloc(1, sizeof(struct jtag_tap));
539 Jim_SetResultFormatted(goi->interp, "no memory");
544 * we expect CHIP + TAP + OPTIONS
547 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
553 Jim_GetOpt_String(goi, &tmp, NULL);
554 pTap->chip = strdup(tmp);
556 Jim_GetOpt_String(goi, &tmp, NULL);
557 pTap->tapname = strdup(tmp);
559 /* name + dot + name + null */
560 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
562 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
563 pTap->dotted_name = cp;
565 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
566 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
568 if (!transport_is_jtag()) {
569 /* SWD doesn't require any JTAG tap parameters */
570 pTap->enabled = true;
575 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
576 * that the default. The "-ircapture" and "-irmask" options are only
577 * needed to cope with nonstandard TAPs, or to specify more bits.
579 pTap->ir_capture_mask = 0x03;
580 pTap->ir_capture_value = 0x01;
583 e = Jim_GetOpt_Nvp(goi, opts, &n);
585 Jim_GetOpt_NvpUnknown(goi, opts, 0);
590 LOG_DEBUG("Processing option: %s", n->name);
592 case NTAP_OPT_ENABLED:
593 pTap->disabled_after_reset = false;
595 case NTAP_OPT_DISABLED:
596 pTap->disabled_after_reset = true;
598 case NTAP_OPT_EXPECTED_ID:
599 e = jim_newtap_expected_id(n, goi, pTap);
607 case NTAP_OPT_IRMASK:
608 case NTAP_OPT_IRCAPTURE:
609 e = jim_newtap_ir_param(n, goi, pTap);
616 case NTAP_OPT_VERSION:
617 pTap->ignore_version = true;
619 } /* switch (n->value) */
620 } /* while (goi->argc) */
622 /* default is enabled-after-reset */
623 pTap->enabled = !pTap->disabled_after_reset;
625 /* Did all the required option bits get cleared? */
626 if (pTap->ir_length != 0) {
631 Jim_SetResultFormatted(goi->interp,
632 "newtap: %s missing IR length",
638 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
640 struct jtag_tap_event_action *jteap;
643 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next) {
644 if (jteap->event != e)
647 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
648 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
649 tap->dotted_name, e, nvp->name,
650 Jim_GetString(jteap->body, NULL));
652 retval = Jim_EvalObj(jteap->interp, jteap->body);
653 if (retval == JIM_RETURN)
654 retval = jteap->interp->returnCode;
656 if (retval != JIM_OK) {
657 Jim_MakeErrorMessage(jteap->interp);
658 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
663 case JTAG_TAP_EVENT_ENABLE:
664 case JTAG_TAP_EVENT_DISABLE:
665 /* NOTE: we currently assume the handlers
666 * can't fail. Right here is where we should
667 * really be verifying the scan chains ...
669 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
670 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
671 tap->enabled ? "enabled" : "disabled");
679 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
682 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
684 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
687 struct command_context *context = current_command_context(interp);
688 int e = jtag_init_inner(context);
690 Jim_Obj *obj = Jim_NewIntObj(goi.interp, e);
691 Jim_SetResultFormatted(goi.interp, "error: %#s", obj);
697 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
701 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
703 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
706 struct command_context *context = current_command_context(interp);
707 if (transport_is_jtag())
708 e = jtag_init_reset(context);
709 else if (transport_is_swd())
710 e = swd_init_reset(context);
713 Jim_Obj *obj = Jim_NewIntObj(goi.interp, e);
714 Jim_SetResultFormatted(goi.interp, "error: %#s", obj);
720 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
723 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
724 return jim_newtap_cmd(&goi);
727 static bool jtag_tap_enable(struct jtag_tap *t)
731 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
735 /* FIXME add JTAG sanity checks, w/o TLR
736 * - scan chain length grew by one (this)
737 * - IDs and IR lengths are as expected
739 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
742 static bool jtag_tap_disable(struct jtag_tap *t)
746 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
750 /* FIXME add JTAG sanity checks, w/o TLR
751 * - scan chain length shrank by one (this)
752 * - IDs and IR lengths are as expected
754 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
758 int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
760 struct command *c = jim_to_command(interp);
761 const char *cmd_name = c->name;
763 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
765 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
771 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
775 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
776 /* do nothing, just return the value */
777 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
778 if (!jtag_tap_enable(t)) {
779 LOG_WARNING("failed to enable tap %s", t->dotted_name);
782 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
783 if (!jtag_tap_disable(t)) {
784 LOG_WARNING("failed to disable tap %s", t->dotted_name);
788 LOG_ERROR("command '%s' unknown", cmd_name);
792 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
796 int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
798 struct command *c = jim_to_command(interp);
799 const char *cmd_name = c->name;
801 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
802 goi.isconfigure = !strcmp(cmd_name, "configure");
803 if (goi.argc < 2 + goi.isconfigure) {
804 Jim_WrongNumArgs(goi.interp, 0, NULL,
805 "<tap_name> <attribute> ...");
812 Jim_GetOpt_Obj(&goi, &o);
813 t = jtag_tap_by_jim_obj(goi.interp, o);
817 return jtag_tap_configure_cmd(&goi, t);
820 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
823 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
825 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
828 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
829 struct jtag_tap *tap;
831 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
832 Jim_ListAppendElement(goi.interp,
833 Jim_GetResult(goi.interp),
834 Jim_NewStringObj(goi.interp,
835 tap->dotted_name, -1));
840 COMMAND_HANDLER(handle_jtag_init_command)
843 return ERROR_COMMAND_SYNTAX_ERROR;
845 static bool jtag_initialized;
846 if (jtag_initialized) {
847 LOG_INFO("'jtag init' has already been called");
850 jtag_initialized = true;
852 LOG_DEBUG("Initializing jtag devices...");
853 return jtag_init(CMD_CTX);
856 static const struct command_registration jtag_subcommand_handlers[] = {
860 .handler = handle_jtag_init_command,
861 .help = "initialize jtag scan chain",
867 .jim_handler = jim_jtag_arp_init,
868 .help = "Validates JTAG scan chain against the list of "
869 "declared TAPs using just the four standard JTAG "
873 .name = "arp_init-reset",
875 .jim_handler = jim_jtag_arp_init_reset,
876 .help = "Uses TRST and SRST to try resetting everything on "
877 "the JTAG scan chain, then performs 'jtag arp_init'."
881 .mode = COMMAND_CONFIG,
882 .jim_handler = jim_jtag_newtap,
883 .help = "Create a new TAP instance named basename.tap_type, "
884 "and appends it to the scan chain.",
885 .usage = "basename tap_type '-irlen' count "
886 "['-enable'|'-disable'] "
887 "['-expected_id' number] "
888 "['-ignore-version'] "
889 "['-ircapture' number] "
893 .name = "tapisenabled",
894 .mode = COMMAND_EXEC,
895 .jim_handler = jim_jtag_tap_enabler,
896 .help = "Returns a Tcl boolean (0/1) indicating whether "
897 "the TAP is enabled (1) or not (0).",
902 .mode = COMMAND_EXEC,
903 .jim_handler = jim_jtag_tap_enabler,
904 .help = "Try to enable the specified TAP using the "
905 "'tap-enable' TAP event.",
909 .name = "tapdisable",
910 .mode = COMMAND_EXEC,
911 .jim_handler = jim_jtag_tap_enabler,
912 .help = "Try to disable the specified TAP using the "
913 "'tap-disable' TAP event.",
919 .jim_handler = jim_jtag_configure,
920 .help = "Provide a Tcl handler for the specified "
922 .usage = "tap_name '-event' event_name handler",
926 .mode = COMMAND_EXEC,
927 .jim_handler = jim_jtag_configure,
928 .help = "Return any Tcl handler for the specified "
930 .usage = "tap_name '-event' event_name",
935 .jim_handler = jim_jtag_names,
936 .help = "Returns list of all JTAG tap names.",
939 .chain = jtag_command_handlers_to_move,
941 COMMAND_REGISTRATION_DONE
944 void jtag_notify_event(enum jtag_event event)
946 struct jtag_tap *tap;
948 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
949 jtag_tap_handle_event(tap, event);
953 COMMAND_HANDLER(handle_scan_chain_command)
955 struct jtag_tap *tap;
956 char expected_id[12];
958 tap = jtag_all_taps();
960 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
962 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
965 uint32_t expected, expected_mask, ii;
967 snprintf(expected_id, sizeof(expected_id), "0x%08x",
968 (unsigned)((tap->expected_ids_cnt > 0)
969 ? tap->expected_ids[0]
971 if (tap->ignore_version)
972 expected_id[2] = '*';
974 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
975 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
978 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
979 tap->abs_chain_position,
981 tap->enabled ? 'Y' : 'n',
982 (unsigned int)(tap->idcode),
984 (unsigned int)(tap->ir_length),
985 (unsigned int)(expected),
986 (unsigned int)(expected_mask));
988 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
989 snprintf(expected_id, sizeof(expected_id), "0x%08x",
990 (unsigned) tap->expected_ids[ii]);
991 if (tap->ignore_version)
992 expected_id[2] = '*';
1005 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1008 return ERROR_COMMAND_SYNTAX_ERROR;
1009 if (CMD_ARGC == 1) {
1011 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1013 jtag_set_ntrst_delay(delay);
1015 command_print(CMD, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1019 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1022 return ERROR_COMMAND_SYNTAX_ERROR;
1023 if (CMD_ARGC == 1) {
1025 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1027 jtag_set_ntrst_assert_width(delay);
1029 command_print(CMD, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1033 COMMAND_HANDLER(handle_jtag_rclk_command)
1036 return ERROR_COMMAND_SYNTAX_ERROR;
1038 int retval = ERROR_OK;
1039 if (CMD_ARGC == 1) {
1041 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1043 retval = jtag_config_rclk(khz);
1044 if (ERROR_OK != retval)
1048 int cur_khz = jtag_get_speed_khz();
1049 retval = jtag_get_speed_readable(&cur_khz);
1050 if (ERROR_OK != retval)
1054 command_print(CMD, "RCLK not supported - fallback to %d kHz", cur_khz);
1056 command_print(CMD, "RCLK - adaptive");
1061 COMMAND_HANDLER(handle_runtest_command)
1064 return ERROR_COMMAND_SYNTAX_ERROR;
1066 unsigned num_clocks;
1067 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1069 jtag_add_runtest(num_clocks, TAP_IDLE);
1070 return jtag_execute_queue();
1074 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1075 * should be stable ... and *NOT* a shift state, otherwise free-running
1076 * jtag clocks could change the values latched by the update state.
1077 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1078 * and "drscan" commands are a write-only subset of what SVF provides.
1081 COMMAND_HANDLER(handle_irscan_command)
1084 struct scan_field *fields;
1085 struct jtag_tap *tap = NULL;
1086 tap_state_t endstate;
1088 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1089 return ERROR_COMMAND_SYNTAX_ERROR;
1091 /* optional "-endstate" "statename" at the end of the arguments,
1092 * so that e.g. IRPAUSE can let us load the data register before
1093 * entering RUN/IDLE to execute the instruction we load here.
1095 endstate = TAP_IDLE;
1097 if (CMD_ARGC >= 4) {
1098 /* have at least one pair of numbers.
1099 * is last pair the magic text? */
1100 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1101 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1102 if (endstate == TAP_INVALID)
1103 return ERROR_COMMAND_SYNTAX_ERROR;
1104 if (!scan_is_safe(endstate))
1105 LOG_WARNING("unstable irscan endstate \"%s\"",
1106 CMD_ARGV[CMD_ARGC - 1]);
1111 int num_fields = CMD_ARGC / 2;
1112 if (num_fields > 1) {
1113 /* we really should be looking at plain_ir_scan if we want
1114 * anything more fancy.
1116 LOG_ERROR("Specify a single value for tap");
1117 return ERROR_COMMAND_SYNTAX_ERROR;
1120 fields = calloc(num_fields, sizeof(*fields));
1123 for (i = 0; i < num_fields; i++) {
1124 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1127 command_print(CMD, "Tap: %s unknown", CMD_ARGV[i*2]);
1132 retval = parse_u64(CMD_ARGV[i * 2 + 1], &value);
1133 if (ERROR_OK != retval)
1136 int field_size = tap->ir_length;
1137 fields[i].num_bits = field_size;
1138 uint8_t *v = calloc(1, DIV_ROUND_UP(field_size, 8));
1140 LOG_ERROR("Out of memory");
1144 buf_set_u64(v, 0, field_size, value);
1145 fields[i].out_value = v;
1146 fields[i].in_value = NULL;
1149 /* did we have an endstate? */
1150 jtag_add_ir_scan(tap, fields, endstate);
1152 retval = jtag_execute_queue();
1155 for (i = 0; i < num_fields; i++)
1156 free((void *)fields[i].out_value);
1163 COMMAND_HANDLER(handle_verify_ircapture_command)
1166 return ERROR_COMMAND_SYNTAX_ERROR;
1168 if (CMD_ARGC == 1) {
1170 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1171 jtag_set_verify_capture_ir(enable);
1174 const char *status = jtag_will_verify_capture_ir() ? "enabled" : "disabled";
1175 command_print(CMD, "verify Capture-IR is %s", status);
1180 COMMAND_HANDLER(handle_verify_jtag_command)
1183 return ERROR_COMMAND_SYNTAX_ERROR;
1185 if (CMD_ARGC == 1) {
1187 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1188 jtag_set_verify(enable);
1191 const char *status = jtag_will_verify() ? "enabled" : "disabled";
1192 command_print(CMD, "verify jtag capture is %s", status);
1197 COMMAND_HANDLER(handle_tms_sequence_command)
1200 return ERROR_COMMAND_SYNTAX_ERROR;
1202 if (CMD_ARGC == 1) {
1204 if (strcmp(CMD_ARGV[0], "short") == 0)
1205 use_new_table = true;
1206 else if (strcmp(CMD_ARGV[0], "long") == 0)
1207 use_new_table = false;
1209 return ERROR_COMMAND_SYNTAX_ERROR;
1211 tap_use_new_tms_table(use_new_table);
1214 command_print(CMD, "tms sequence is %s",
1215 tap_uses_new_tms_table() ? "short" : "long");
1220 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1223 return ERROR_COMMAND_SYNTAX_ERROR;
1226 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1228 jtag_set_flush_queue_sleep(sleep_ms);
1233 COMMAND_HANDLER(handle_wait_srst_deassert)
1236 return ERROR_COMMAND_SYNTAX_ERROR;
1239 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1240 if ((timeout_ms <= 0) || (timeout_ms > 100000)) {
1241 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1245 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1247 int64_t then = timeval_ms();
1248 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1249 if ((timeval_ms() - then) > timeout_ms) {
1250 LOG_ERROR("Timed out");
1256 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1257 if ((timeval_ms() - then) > timeout_ms) {
1258 LOG_ERROR("Timed out");
1268 static const struct command_registration jtag_command_handlers[] = {
1271 .name = "jtag_flush_queue_sleep",
1272 .handler = handle_jtag_flush_queue_sleep,
1273 .mode = COMMAND_ANY,
1274 .help = "For debug purposes(simulate long delays of interface) "
1275 "to test performance or change in behavior. Default 0ms.",
1276 .usage = "[sleep in ms]",
1279 .name = "jtag_rclk",
1280 .handler = handle_jtag_rclk_command,
1281 .mode = COMMAND_ANY,
1282 .help = "With an argument, change to to use adaptive clocking "
1283 "if possible; else to use the fallback speed. "
1284 "With or without argument, display current setting.",
1285 .usage = "[fallback_speed_khz]",
1288 .name = "jtag_ntrst_delay",
1289 .handler = handle_jtag_ntrst_delay_command,
1290 .mode = COMMAND_ANY,
1291 .help = "delay after deasserting trst in ms",
1292 .usage = "[milliseconds]",
1295 .name = "jtag_ntrst_assert_width",
1296 .handler = handle_jtag_ntrst_assert_width_command,
1297 .mode = COMMAND_ANY,
1298 .help = "delay after asserting trst in ms",
1299 .usage = "[milliseconds]",
1302 .name = "scan_chain",
1303 .handler = handle_scan_chain_command,
1304 .mode = COMMAND_ANY,
1305 .help = "print current scan chain configuration",
1310 .handler = handle_runtest_command,
1311 .mode = COMMAND_EXEC,
1312 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1313 .usage = "num_cycles"
1317 .handler = handle_irscan_command,
1318 .mode = COMMAND_EXEC,
1319 .help = "Execute Instruction Register (IR) scan. The "
1320 "specified opcodes are put into each TAP's IR, "
1321 "and other TAPs are put in BYPASS.",
1322 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1325 .name = "verify_ircapture",
1326 .handler = handle_verify_ircapture_command,
1327 .mode = COMMAND_ANY,
1328 .help = "Display or assign flag controlling whether to "
1329 "verify values captured during Capture-IR.",
1330 .usage = "['enable'|'disable']",
1333 .name = "verify_jtag",
1334 .handler = handle_verify_jtag_command,
1335 .mode = COMMAND_ANY,
1336 .help = "Display or assign flag controlling whether to "
1337 "verify values captured during IR and DR scans.",
1338 .usage = "['enable'|'disable']",
1341 .name = "tms_sequence",
1342 .handler = handle_tms_sequence_command,
1343 .mode = COMMAND_ANY,
1344 .help = "Display or change what style TMS sequences to use "
1345 "for JTAG state transitions: short (default) or "
1346 "long. Only for working around JTAG bugs.",
1347 /* Specifically for working around DRIVER bugs... */
1348 .usage = "['short'|'long']",
1351 .name = "wait_srst_deassert",
1352 .handler = handle_wait_srst_deassert,
1353 .mode = COMMAND_ANY,
1354 .help = "Wait for an SRST deassert. "
1355 "Useful for cases where you need something to happen within ms "
1356 "of an srst deassert. Timeout in ms",
1361 .mode = COMMAND_ANY,
1362 .help = "perform jtag tap actions",
1365 .chain = jtag_subcommand_handlers,
1368 .chain = jtag_command_handlers_to_move,
1370 COMMAND_REGISTRATION_DONE
1373 int jtag_register_commands(struct command_context *cmd_ctx)
1375 return register_commands(cmd_ctx, NULL, jtag_command_handlers);