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 ***************************************************************************/
35 #include "minidriver.h"
36 #include "interface.h"
37 #include "interfaces.h"
43 #include <helper/time_support.h>
47 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
50 static const Jim_Nvp nvp_jtag_tap_event[] = {
51 { .value = JTAG_TRST_ASSERTED, .name = "post-reset" },
52 { .value = JTAG_TAP_EVENT_SETUP, .name = "setup" },
53 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
54 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
56 { .name = NULL, .value = -1 }
59 extern struct jtag_interface *jtag_interface;
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)
86 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
89 struct scan_field *fields;
98 * args[3] = hex string
99 * ... repeat num bits and hex string ...
102 * args[N-2] = "-endstate"
103 * args[N-1] = statename
105 if ((argc < 4) || ((argc % 2) != 0))
107 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
113 script_debug(interp, "drscan", argc, args);
115 /* validate arguments as numbers */
117 for (i = 2; i < argc; i += 2)
122 e = Jim_GetLong(interp, args[i], &bits);
123 /* If valid - try next arg */
128 /* Not valid.. are we at the end? */
129 if (((i + 2) != argc)) {
130 /* nope, then error */
134 /* it could be: "-endstate FOO"
135 * e.g. DRPAUSE so we can issue more instructions
136 * before entering RUN/IDLE and executing them.
139 /* get arg as a string. */
140 cp = Jim_GetString(args[i], NULL);
141 /* is it the magic? */
142 if (0 == strcmp("-endstate", cp)) {
143 /* is the statename valid? */
144 cp = Jim_GetString(args[i + 1], NULL);
146 /* see if it is a valid state name */
147 endstate = tap_state_by_name(cp);
149 /* update the error message */
150 Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
152 if (!scan_is_safe(endstate))
153 LOG_WARNING("drscan with unsafe "
154 "endstate \"%s\"", cp);
156 /* valid - so clear the error */
158 /* and remove the last 2 args */
163 /* Still an error? */
165 return e; /* too bad */
167 } /* validate args */
169 tap = jtag_tap_by_jim_obj(interp, args[1]);
174 num_fields = (argc-2)/2;
175 assert(num_fields > 0);
176 fields = malloc(sizeof(struct scan_field) * num_fields);
177 for (i = 2; i < argc; i += 2)
183 Jim_GetLong(interp, args[i], &bits);
184 str = Jim_GetString(args[i + 1], &len);
186 fields[field_count].num_bits = bits;
187 void * t = malloc(DIV_ROUND_UP(bits, 8));
188 fields[field_count].out_value = t;
189 str_to_buf(str, len, t, bits, 0);
190 fields[field_count].in_value = t;
194 jtag_add_dr_scan(tap, num_fields, fields, endstate);
196 retval = jtag_execute_queue();
197 if (retval != ERROR_OK)
199 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
204 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
205 for (i = 2; i < argc; i += 2)
210 Jim_GetLong(interp, args[i], &bits);
211 str = buf_to_str(fields[field_count].in_value, bits, 16);
212 free((void *)fields[field_count].out_value);
214 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
219 Jim_SetResult(interp, list);
227 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
229 tap_state_t states[8];
231 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
233 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
237 script_debug(interp, "pathmove", argc, args);
240 for (i = 0; i < argc-1; i++)
243 cp = Jim_GetString(args[i + 1], NULL);
244 states[i] = tap_state_by_name(cp);
247 /* update the error message */
248 Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
253 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
255 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
259 jtag_add_pathmove(argc-2, states + 1);
261 if (jtag_execute_queue()!= ERROR_OK)
263 Jim_SetResultString(interp, "pathmove: failed",-1);
271 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
273 script_debug(interp, "flush_count", argc, args);
275 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
280 /* REVISIT Just what about these should "move" ... ?
281 * These registrations, into the main JTAG table?
283 * There's a minor compatibility issue, these all show up twice;
284 * that's not desirable:
285 * - jtag drscan ... NOT DOCUMENTED!
288 * The "irscan" command (for example) doesn't show twice.
290 static const struct command_registration jtag_command_handlers_to_move[] = {
293 .mode = COMMAND_EXEC,
294 .jim_handler = Jim_Command_drscan,
295 .help = "Execute Data Register (DR) scan for one TAP. "
296 "Other TAPs must be in BYPASS mode.",
297 .usage = "tap_name [num_bits value]* ['-endstate' state_name]",
300 .name = "flush_count",
301 .mode = COMMAND_EXEC,
302 .jim_handler = Jim_Command_flush_count,
303 .help = "Returns the number of times the JTAG queue "
308 .mode = COMMAND_EXEC,
309 .jim_handler = Jim_Command_pathmove,
310 .usage = "start_state state1 [state2 [state3 ...]]",
311 .help = "Move JTAG state machine from current state "
312 "(start_state) to state1, then state2, state3, etc.",
314 COMMAND_REGISTRATION_DONE
318 enum jtag_tap_cfg_param {
322 static Jim_Nvp nvp_config_opts[] = {
323 { .name = "-event", .value = JCFG_EVENT },
325 { .name = NULL, .value = -1 }
328 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
332 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
337 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
340 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
344 if (goi->isconfigure) {
345 if (goi->argc != 1) {
346 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
350 if (goi->argc != 0) {
351 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
356 struct jtag_tap_event_action *jteap = tap->event_action;
357 /* replace existing event body */
361 if (jteap->event == (enum jtag_event)n->value)
369 Jim_SetEmptyResult(goi->interp);
371 if (goi->isconfigure)
374 jteap = calloc(1, sizeof(*jteap));
375 else if (NULL != jteap->body)
376 Jim_DecrRefCount(goi->interp, jteap->body);
378 jteap->interp = goi->interp;
379 jteap->event = n->value;
382 Jim_GetOpt_Obj(goi, &o);
383 jteap->body = Jim_DuplicateObj(goi->interp, o);
384 Jim_IncrRefCount(jteap->body);
388 /* add to head of event list */
389 jteap->next = tap->event_action;
390 tap->event_action = jteap;
395 jteap->interp = goi->interp;
396 Jim_SetResult(goi->interp,
397 Jim_DuplicateObj(goi->interp, jteap->body));
402 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
404 /* parse config or cget options */
405 while (goi->argc > 0)
407 Jim_SetEmptyResult (goi->interp);
410 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
413 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
420 e = jtag_tap_configure_event(goi, tap);
425 Jim_SetResultFormatted(goi->interp, "unknown event: %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 unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
454 uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
455 if (new_expected_ids == NULL)
457 Jim_SetResultFormatted(goi->interp, "no memory");
461 memcpy(new_expected_ids, pTap->expected_ids, expected_len);
463 new_expected_ids[pTap->expected_ids_cnt] = w;
465 free(pTap->expected_ids);
466 pTap->expected_ids = new_expected_ids;
467 pTap->expected_ids_cnt++;
472 #define NTAP_OPT_IRLEN 0
473 #define NTAP_OPT_IRMASK 1
474 #define NTAP_OPT_IRCAPTURE 2
475 #define NTAP_OPT_ENABLED 3
476 #define NTAP_OPT_DISABLED 4
477 #define NTAP_OPT_EXPECTED_ID 5
478 #define NTAP_OPT_VERSION 6
480 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
481 struct jtag_tap *pTap)
484 int e = Jim_GetOpt_Wide(goi, &w);
487 Jim_SetResultFormatted(goi->interp,
488 "option: %s bad parameter", n->name);
489 free((void *)pTap->dotted_name);
494 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
496 LOG_WARNING("%s: huge IR length %d",
497 pTap->dotted_name, (int) w);
501 case NTAP_OPT_IRMASK:
502 if (is_bad_irval(pTap->ir_length, w))
504 LOG_ERROR("%s: IR mask %x too big",
510 LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
511 pTap->ir_capture_mask = w;
513 case NTAP_OPT_IRCAPTURE:
514 if (is_bad_irval(pTap->ir_length, w))
516 LOG_ERROR("%s: IR capture %x too big",
517 pTap->dotted_name, (int) w);
521 LOG_WARNING("%s: nonstandard IR value",
523 pTap->ir_capture_value = w;
531 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
533 struct jtag_tap *pTap;
538 const Jim_Nvp opts[] = {
539 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
540 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
541 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
542 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
543 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
544 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
545 { .name = "-ignore-version" , .value = NTAP_OPT_VERSION },
546 { .name = NULL , .value = -1 },
549 pTap = calloc(1, sizeof(struct jtag_tap));
551 Jim_SetResultFormatted(goi->interp, "no memory");
556 * we expect CHIP + TAP + OPTIONS
559 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
563 Jim_GetOpt_String(goi, &cp, NULL);
564 pTap->chip = strdup(cp);
566 Jim_GetOpt_String(goi, &cp, NULL);
567 pTap->tapname = strdup(cp);
569 /* name + dot + name + null */
570 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
572 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
573 pTap->dotted_name = cp;
575 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
576 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
578 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
579 * that the default. The "-irlen" and "-irmask" options are only
580 * needed to cope with nonstandard TAPs, or to specify more bits.
582 pTap->ir_capture_mask = 0x03;
583 pTap->ir_capture_value = 0x01;
586 e = Jim_GetOpt_Nvp(goi, opts, &n);
588 Jim_GetOpt_NvpUnknown(goi, opts, 0);
589 free((void *)pTap->dotted_name);
593 LOG_DEBUG("Processing option: %s", n->name);
595 case NTAP_OPT_ENABLED:
596 pTap->disabled_after_reset = false;
598 case NTAP_OPT_DISABLED:
599 pTap->disabled_after_reset = true;
601 case NTAP_OPT_EXPECTED_ID:
602 e = jim_newtap_expected_id(n, goi, pTap);
605 free((void *)pTap->dotted_name);
611 case NTAP_OPT_IRMASK:
612 case NTAP_OPT_IRCAPTURE:
613 e = jim_newtap_ir_param(n, goi, pTap);
616 free((void *)pTap->dotted_name);
621 case NTAP_OPT_VERSION:
622 pTap->ignore_version = true;
624 } /* switch (n->value) */
625 } /* while (goi->argc) */
627 /* default is enabled-after-reset */
628 pTap->enabled = !pTap->disabled_after_reset;
630 /* Did all the required option bits get cleared? */
631 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;
648 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 if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK)
660 Jim_MakeErrorMessage(jteap->interp);
661 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
667 case JTAG_TAP_EVENT_ENABLE:
668 case JTAG_TAP_EVENT_DISABLE:
669 /* NOTE: we currently assume the handlers
670 * can't fail. Right here is where we should
671 * really be verifying the scan chains ...
673 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
674 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
675 tap->enabled ? "enabled" : "disabled");
683 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
686 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
688 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
691 struct command_context *context = current_command_context(interp);
692 int e = jtag_init_inner(context);
694 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
695 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
696 Jim_FreeNewObj(goi.interp, eObj);
702 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
705 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
707 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
710 struct command_context *context = current_command_context(interp);
711 int e = jtag_init_reset(context);
713 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
714 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
715 Jim_FreeNewObj(goi.interp, eObj);
721 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
724 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
725 return jim_newtap_cmd(&goi);
728 static bool jtag_tap_enable(struct jtag_tap *t)
732 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
736 /* FIXME add JTAG sanity checks, w/o TLR
737 * - scan chain length grew by one (this)
738 * - IDs and IR lengths are as expected
740 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
743 static bool jtag_tap_disable(struct jtag_tap *t)
747 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
751 /* FIXME add JTAG sanity checks, w/o TLR
752 * - scan chain length shrank by one (this)
753 * - IDs and IR lengths are as expected
755 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
759 static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
761 const char *cmd_name = Jim_GetString(argv[0], NULL);
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 static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
798 const char *cmd_name = Jim_GetString(argv[0], NULL);
800 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
801 goi.isconfigure = !strcmp(cmd_name, "configure");
802 if (goi.argc < 2 + goi.isconfigure) {
803 Jim_WrongNumArgs(goi.interp, 0, NULL,
804 "<tap_name> <attribute> ...");
811 Jim_GetOpt_Obj(&goi, &o);
812 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 = false;
846 if (jtag_initialized)
848 LOG_INFO("'jtag init' has already been called");
851 jtag_initialized = true;
853 LOG_DEBUG("Initializing jtag devices...");
854 return jtag_init(CMD_CTX);
857 static const struct command_registration jtag_subcommand_handlers[] = {
861 .handler = handle_jtag_init_command,
862 .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.",
918 .mode = COMMAND_EXEC,
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();
959 command_print(CMD_CTX,
960 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
961 command_print(CMD_CTX,
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);
977 command_print(CMD_CTX,
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[1]);
991 if (tap->ignore_version)
992 expected_id[2] = '*';
994 command_print(CMD_CTX,
1005 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1008 return ERROR_COMMAND_SYNTAX_ERROR;
1012 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1014 jtag_set_ntrst_delay(delay);
1016 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1020 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1023 return ERROR_COMMAND_SYNTAX_ERROR;
1027 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1029 jtag_set_ntrst_assert_width(delay);
1031 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1035 COMMAND_HANDLER(handle_jtag_rclk_command)
1038 return ERROR_COMMAND_SYNTAX_ERROR;
1040 int retval = ERROR_OK;
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_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1059 command_print(CMD_CTX, "RCLK - adaptive");
1064 COMMAND_HANDLER(handle_jtag_reset_command)
1067 return ERROR_COMMAND_SYNTAX_ERROR;
1070 if (CMD_ARGV[0][0] == '1')
1072 else if (CMD_ARGV[0][0] == '0')
1075 return ERROR_COMMAND_SYNTAX_ERROR;
1078 if (CMD_ARGV[1][0] == '1')
1080 else if (CMD_ARGV[1][0] == '0')
1083 return ERROR_COMMAND_SYNTAX_ERROR;
1085 if (adapter_init(CMD_CTX) != ERROR_OK)
1086 return ERROR_JTAG_INIT_FAILED;
1088 jtag_add_reset(trst, srst);
1089 return jtag_execute_queue();
1092 COMMAND_HANDLER(handle_runtest_command)
1095 return ERROR_COMMAND_SYNTAX_ERROR;
1097 unsigned num_clocks;
1098 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1100 jtag_add_runtest(num_clocks, TAP_IDLE);
1101 return jtag_execute_queue();
1105 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1106 * should be stable ... and *NOT* a shift state, otherwise free-running
1107 * jtag clocks could change the values latched by the update state.
1108 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1109 * and "drscan" commands are a write-only subset of what SVF provides.
1112 COMMAND_HANDLER(handle_irscan_command)
1115 struct scan_field *fields;
1116 struct jtag_tap *tap = NULL;
1117 tap_state_t endstate;
1119 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1121 return ERROR_COMMAND_SYNTAX_ERROR;
1124 /* optional "-endstate" "statename" at the end of the arguments,
1125 * so that e.g. IRPAUSE can let us load the data register before
1126 * entering RUN/IDLE to execute the instruction we load here.
1128 endstate = TAP_IDLE;
1130 if (CMD_ARGC >= 4) {
1131 /* have at least one pair of numbers. */
1132 /* is last pair the magic text? */
1133 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1134 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1135 if (endstate == TAP_INVALID)
1136 return ERROR_COMMAND_SYNTAX_ERROR;
1137 if (!scan_is_safe(endstate))
1138 LOG_WARNING("unstable irscan endstate \"%s\"",
1139 CMD_ARGV[CMD_ARGC - 1]);
1144 int num_fields = CMD_ARGC / 2;
1147 /* we really should be looking at plain_ir_scan if we want
1148 * anything more fancy.
1150 LOG_ERROR("Specify a single value for tap");
1151 return ERROR_COMMAND_SYNTAX_ERROR;
1154 size_t fields_len = sizeof(struct scan_field) * num_fields;
1155 fields = malloc(fields_len);
1156 memset(fields, 0, fields_len);
1159 for (i = 0; i < num_fields; i++)
1161 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1165 for (j = 0; j < i; j++)
1166 free((void *)fields[j].out_value);
1168 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1172 int field_size = tap->ir_length;
1173 fields[i].num_bits = field_size;
1174 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1177 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1178 if (ERROR_OK != retval)
1180 void *v = (void *)fields[i].out_value;
1181 buf_set_u32(v, 0, field_size, value);
1182 fields[i].in_value = NULL;
1185 /* did we have an endstate? */
1186 jtag_add_ir_scan(tap, fields, endstate);
1188 retval = jtag_execute_queue();
1191 for (i = 0; i < num_fields; i++)
1193 if (NULL != fields[i].out_value)
1194 free((void *)fields[i].out_value);
1203 COMMAND_HANDLER(handle_verify_ircapture_command)
1206 return ERROR_COMMAND_SYNTAX_ERROR;
1211 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1212 jtag_set_verify_capture_ir(enable);
1215 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1216 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1221 COMMAND_HANDLER(handle_verify_jtag_command)
1224 return ERROR_COMMAND_SYNTAX_ERROR;
1229 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1230 jtag_set_verify(enable);
1233 const char *status = jtag_will_verify() ? "enabled": "disabled";
1234 command_print(CMD_CTX, "verify jtag capture is %s", status);
1239 COMMAND_HANDLER(handle_tms_sequence_command)
1242 return ERROR_COMMAND_SYNTAX_ERROR;
1247 if (strcmp(CMD_ARGV[0], "short") == 0)
1248 use_new_table = true;
1249 else if (strcmp(CMD_ARGV[0], "long") == 0)
1250 use_new_table = false;
1252 return ERROR_COMMAND_SYNTAX_ERROR;
1254 tap_use_new_tms_table(use_new_table);
1257 command_print(CMD_CTX, "tms sequence is %s",
1258 tap_uses_new_tms_table() ? "short": "long");
1263 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1266 return ERROR_COMMAND_SYNTAX_ERROR;
1269 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1271 jtag_set_flush_queue_sleep(sleep_ms);
1276 COMMAND_HANDLER(handle_wait_srst_deassert)
1279 return ERROR_COMMAND_SYNTAX_ERROR;
1282 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1283 if ((timeout_ms <= 0) || (timeout_ms > 100000))
1285 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1289 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1291 long long then = timeval_ms();
1292 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK)
1294 if ((timeval_ms() - then) > timeout_ms)
1296 LOG_ERROR("Timed out");
1302 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK)
1304 if ((timeval_ms() - then) > timeout_ms)
1306 LOG_ERROR("Timed out");
1318 static const struct command_registration jtag_command_handlers[] = {
1321 .name = "jtag_flush_queue_sleep",
1322 .handler = handle_jtag_flush_queue_sleep,
1323 .mode = COMMAND_ANY,
1324 .help = "For debug purposes(simulate long delays of interface) "
1325 "to test performance or change in behavior. Default 0ms.",
1326 .usage = "[sleep in ms]",
1329 .name = "jtag_rclk",
1330 .handler = handle_jtag_rclk_command,
1331 .mode = COMMAND_ANY,
1332 .help = "With an argument, change to to use adaptive clocking "
1333 "if possible; else to use the fallback speed. "
1334 "With or without argument, display current setting.",
1335 .usage = "[fallback_speed_khz]",
1338 .name = "jtag_ntrst_delay",
1339 .handler = handle_jtag_ntrst_delay_command,
1340 .mode = COMMAND_ANY,
1341 .help = "delay after deasserting trst in ms",
1342 .usage = "[milliseconds]",
1345 .name = "jtag_ntrst_assert_width",
1346 .handler = handle_jtag_ntrst_assert_width_command,
1347 .mode = COMMAND_ANY,
1348 .help = "delay after asserting trst in ms",
1349 .usage = "[milliseconds]",
1352 .name = "scan_chain",
1353 .handler = handle_scan_chain_command,
1354 .mode = COMMAND_ANY,
1355 .help = "print current scan chain configuration",
1358 .name = "jtag_reset",
1359 .handler = handle_jtag_reset_command,
1360 .mode = COMMAND_EXEC,
1361 .help = "Set reset line values. Value '1' is active, "
1362 "value '0' is inactive.",
1363 .usage = "trst_active srst_active",
1367 .handler = handle_runtest_command,
1368 .mode = COMMAND_EXEC,
1369 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1370 .usage = "num_cycles"
1374 .handler = handle_irscan_command,
1375 .mode = COMMAND_EXEC,
1376 .help = "Execute Instruction Register (DR) scan. The "
1377 "specified opcodes are put into each TAP's IR, "
1378 "and other TAPs are put in BYPASS.",
1379 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1382 .name = "verify_ircapture",
1383 .handler = handle_verify_ircapture_command,
1384 .mode = COMMAND_ANY,
1385 .help = "Display or assign flag controlling whether to "
1386 "verify values captured during Capture-IR.",
1387 .usage = "['enable'|'disable']",
1390 .name = "verify_jtag",
1391 .handler = handle_verify_jtag_command,
1392 .mode = COMMAND_ANY,
1393 .help = "Display or assign flag controlling whether to "
1394 "verify values captured during IR and DR scans.",
1395 .usage = "['enable'|'disable']",
1398 .name = "tms_sequence",
1399 .handler = handle_tms_sequence_command,
1400 .mode = COMMAND_ANY,
1401 .help = "Display or change what style TMS sequences to use "
1402 "for JTAG state transitions: short (default) or "
1403 "long. Only for working around JTAG bugs.",
1404 /* Specifically for working around DRIVER bugs... */
1405 .usage = "['short'|'long']",
1408 .name = "wait_srst_deassert",
1409 .handler = handle_wait_srst_deassert,
1410 .mode = COMMAND_ANY,
1411 .help = "Wait for an SRST deassert. "
1412 "Useful for cases where you need something to happen within ms "
1413 "of an srst deassert. Timeout in ms ",
1418 .mode = COMMAND_ANY,
1419 .help = "perform jtag tap actions",
1421 .chain = jtag_subcommand_handlers,
1424 .chain = jtag_command_handlers_to_move,
1426 COMMAND_REGISTRATION_DONE
1429 int jtag_register_commands(struct command_context *cmd_ctx)
1431 return register_commands(cmd_ctx, NULL, jtag_command_handlers);