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 */
171 tap = jtag_tap_by_jim_obj(interp, args[1]);
176 num_fields = (argc-2)/2;
177 assert(num_fields > 0);
178 fields = malloc(sizeof(struct scan_field) * num_fields);
179 for (i = 2; i < argc; i += 2)
185 Jim_GetLong(interp, args[i], &bits);
186 str = Jim_GetString(args[i + 1], &len);
188 fields[field_count].num_bits = bits;
189 void * t = malloc(DIV_ROUND_UP(bits, 8));
190 fields[field_count].out_value = t;
191 str_to_buf(str, len, t, bits, 0);
192 fields[field_count].in_value = t;
196 jtag_add_dr_scan(tap, num_fields, fields, endstate);
198 retval = jtag_execute_queue();
199 if (retval != ERROR_OK)
201 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
206 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
207 for (i = 2; i < argc; i += 2)
212 Jim_GetLong(interp, args[i], &bits);
213 str = buf_to_str(fields[field_count].in_value, bits, 16);
214 free((void *)fields[field_count].out_value);
216 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
221 Jim_SetResult(interp, list);
229 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
231 tap_state_t states[8];
233 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
235 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
239 script_debug(interp, "pathmove", argc, args);
242 for (i = 0; i < argc-1; i++)
245 cp = Jim_GetString(args[i + 1], NULL);
246 states[i] = tap_state_by_name(cp);
249 /* update the error message */
250 Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
255 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
257 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
261 jtag_add_pathmove(argc-2, states + 1);
263 if (jtag_execute_queue()!= ERROR_OK)
265 Jim_SetResultString(interp, "pathmove: failed",-1);
273 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
275 script_debug(interp, "flush_count", argc, args);
277 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
282 /* REVISIT Just what about these should "move" ... ?
283 * These registrations, into the main JTAG table?
285 * There's a minor compatibility issue, these all show up twice;
286 * that's not desirable:
287 * - jtag drscan ... NOT DOCUMENTED!
290 * The "irscan" command (for example) doesn't show twice.
292 static const struct command_registration jtag_command_handlers_to_move[] = {
295 .mode = COMMAND_EXEC,
296 .jim_handler = Jim_Command_drscan,
297 .help = "Execute Data Register (DR) scan for one TAP. "
298 "Other TAPs must be in BYPASS mode.",
299 .usage = "tap_name [num_bits value]* ['-endstate' state_name]",
302 .name = "flush_count",
303 .mode = COMMAND_EXEC,
304 .jim_handler = Jim_Command_flush_count,
305 .help = "Returns the number of times the JTAG queue "
310 .mode = COMMAND_EXEC,
311 .jim_handler = Jim_Command_pathmove,
312 .usage = "start_state state1 [state2 [state3 ...]]",
313 .help = "Move JTAG state machine from current state "
314 "(start_state) to state1, then state2, state3, etc.",
316 COMMAND_REGISTRATION_DONE
320 enum jtag_tap_cfg_param {
324 static Jim_Nvp nvp_config_opts[] = {
325 { .name = "-event", .value = JCFG_EVENT },
327 { .name = NULL, .value = -1 }
330 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
334 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
339 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
342 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
346 if (goi->isconfigure) {
347 if (goi->argc != 1) {
348 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
352 if (goi->argc != 0) {
353 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
358 struct jtag_tap_event_action *jteap = tap->event_action;
359 /* replace existing event body */
363 if (jteap->event == (enum jtag_event)n->value)
371 Jim_SetEmptyResult(goi->interp);
373 if (goi->isconfigure)
376 jteap = calloc(1, sizeof(*jteap));
377 else if (NULL != jteap->body)
378 Jim_DecrRefCount(goi->interp, jteap->body);
380 jteap->interp = goi->interp;
381 jteap->event = n->value;
384 Jim_GetOpt_Obj(goi, &o);
385 jteap->body = Jim_DuplicateObj(goi->interp, o);
386 Jim_IncrRefCount(jteap->body);
390 /* add to head of event list */
391 jteap->next = tap->event_action;
392 tap->event_action = jteap;
397 jteap->interp = goi->interp;
398 Jim_SetResult(goi->interp,
399 Jim_DuplicateObj(goi->interp, jteap->body));
404 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
406 /* parse config or cget options */
407 while (goi->argc > 0)
409 Jim_SetEmptyResult (goi->interp);
412 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
415 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
422 e = jtag_tap_configure_event(goi, tap);
427 Jim_SetResultFormatted(goi->interp, "unknown event: %s", n->name);
435 static int is_bad_irval(int ir_length, jim_wide w)
445 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
446 struct jtag_tap *pTap)
449 int e = Jim_GetOpt_Wide(goi, &w);
451 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
455 unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
456 uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
457 if (new_expected_ids == NULL)
459 Jim_SetResultFormatted(goi->interp, "no memory");
463 memcpy(new_expected_ids, pTap->expected_ids, expected_len);
465 new_expected_ids[pTap->expected_ids_cnt] = w;
467 free(pTap->expected_ids);
468 pTap->expected_ids = new_expected_ids;
469 pTap->expected_ids_cnt++;
474 #define NTAP_OPT_IRLEN 0
475 #define NTAP_OPT_IRMASK 1
476 #define NTAP_OPT_IRCAPTURE 2
477 #define NTAP_OPT_ENABLED 3
478 #define NTAP_OPT_DISABLED 4
479 #define NTAP_OPT_EXPECTED_ID 5
480 #define NTAP_OPT_VERSION 6
482 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
483 struct jtag_tap *pTap)
486 int e = Jim_GetOpt_Wide(goi, &w);
489 Jim_SetResultFormatted(goi->interp,
490 "option: %s bad parameter", n->name);
491 free((void *)pTap->dotted_name);
496 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
498 LOG_WARNING("%s: huge IR length %d",
499 pTap->dotted_name, (int) w);
503 case NTAP_OPT_IRMASK:
504 if (is_bad_irval(pTap->ir_length, w))
506 LOG_ERROR("%s: IR mask %x too big",
512 LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
513 pTap->ir_capture_mask = w;
515 case NTAP_OPT_IRCAPTURE:
516 if (is_bad_irval(pTap->ir_length, w))
518 LOG_ERROR("%s: IR capture %x too big",
519 pTap->dotted_name, (int) w);
523 LOG_WARNING("%s: nonstandard IR value",
525 pTap->ir_capture_value = w;
533 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
535 struct jtag_tap *pTap;
540 const Jim_Nvp opts[] = {
541 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
542 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
543 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
544 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
545 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
546 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
547 { .name = "-ignore-version" , .value = NTAP_OPT_VERSION },
548 { .name = NULL , .value = -1 },
551 pTap = calloc(1, sizeof(struct jtag_tap));
553 Jim_SetResultFormatted(goi->interp, "no memory");
558 * we expect CHIP + TAP + OPTIONS
561 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
565 Jim_GetOpt_String(goi, &cp, NULL);
566 pTap->chip = strdup(cp);
568 Jim_GetOpt_String(goi, &cp, NULL);
569 pTap->tapname = strdup(cp);
571 /* name + dot + name + null */
572 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
574 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
575 pTap->dotted_name = cp;
577 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
578 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
580 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
581 * that the default. The "-irlen" and "-irmask" options are only
582 * needed to cope with nonstandard TAPs, or to specify more bits.
584 pTap->ir_capture_mask = 0x03;
585 pTap->ir_capture_value = 0x01;
588 e = Jim_GetOpt_Nvp(goi, opts, &n);
590 Jim_GetOpt_NvpUnknown(goi, opts, 0);
591 free((void *)pTap->dotted_name);
595 LOG_DEBUG("Processing option: %s", n->name);
597 case NTAP_OPT_ENABLED:
598 pTap->disabled_after_reset = false;
600 case NTAP_OPT_DISABLED:
601 pTap->disabled_after_reset = true;
603 case NTAP_OPT_EXPECTED_ID:
604 e = jim_newtap_expected_id(n, goi, pTap);
607 free((void *)pTap->dotted_name);
613 case NTAP_OPT_IRMASK:
614 case NTAP_OPT_IRCAPTURE:
615 e = jim_newtap_ir_param(n, goi, pTap);
618 free((void *)pTap->dotted_name);
623 case NTAP_OPT_VERSION:
624 pTap->ignore_version = true;
626 } /* switch (n->value) */
627 } /* while (goi->argc) */
629 /* default is enabled-after-reset */
630 pTap->enabled = !pTap->disabled_after_reset;
632 /* Did all the required option bits get cleared? */
633 if (pTap->ir_length != 0)
639 Jim_SetResultFormatted(goi->interp,
640 "newtap: %s missing IR length",
646 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
648 struct jtag_tap_event_action * jteap;
650 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
652 if (jteap->event != e)
655 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
656 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
657 tap->dotted_name, e, nvp->name,
658 Jim_GetString(jteap->body, NULL));
660 if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK)
662 Jim_MakeErrorMessage(jteap->interp);
663 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_SetResultFormatted(goi.interp, "error: %#s", eObj);
698 Jim_FreeNewObj(goi.interp, eObj);
704 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
707 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
709 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
712 struct command_context *context = current_command_context(interp);
713 int e = jtag_init_reset(context);
715 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
716 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
717 Jim_FreeNewObj(goi.interp, eObj);
723 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
726 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
727 return jim_newtap_cmd(&goi);
730 static bool jtag_tap_enable(struct jtag_tap *t)
734 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
738 /* FIXME add JTAG sanity checks, w/o TLR
739 * - scan chain length grew by one (this)
740 * - IDs and IR lengths are as expected
742 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
745 static bool jtag_tap_disable(struct jtag_tap *t)
749 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
753 /* FIXME add JTAG sanity checks, w/o TLR
754 * - scan chain length shrank by one (this)
755 * - IDs and IR lengths are as expected
757 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
761 static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
763 const char *cmd_name = Jim_GetString(argv[0], NULL);
765 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
767 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
773 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
777 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
778 // do nothing, just return the value
779 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
780 if (!jtag_tap_enable(t)){
781 LOG_WARNING("failed to enable tap %s", t->dotted_name);
784 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
785 if (!jtag_tap_disable(t)){
786 LOG_WARNING("failed to disable tap %s", t->dotted_name);
790 LOG_ERROR("command '%s' unknown", cmd_name);
794 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
798 static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
800 const char *cmd_name = Jim_GetString(argv[0], NULL);
802 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
803 goi.isconfigure = !strcmp(cmd_name, "configure");
804 if (goi.argc < 2 + goi.isconfigure) {
805 Jim_WrongNumArgs(goi.interp, 0, NULL,
806 "<tap_name> <attribute> ...");
813 Jim_GetOpt_Obj(&goi, &o);
814 t = jtag_tap_by_jim_obj(goi.interp, o);
819 return jtag_tap_configure_cmd(&goi, t);
822 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
825 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
827 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
830 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
831 struct jtag_tap *tap;
833 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
834 Jim_ListAppendElement(goi.interp,
835 Jim_GetResult(goi.interp),
836 Jim_NewStringObj(goi.interp,
837 tap->dotted_name, -1));
842 COMMAND_HANDLER(handle_jtag_init_command)
845 return ERROR_COMMAND_SYNTAX_ERROR;
847 static bool jtag_initialized = false;
848 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",
869 .jim_handler = jim_jtag_arp_init,
870 .help = "Validates JTAG scan chain against the list of "
871 "declared TAPs using just the four standard JTAG "
875 .name = "arp_init-reset",
877 .jim_handler = jim_jtag_arp_init_reset,
878 .help = "Uses TRST and SRST to try resetting everything on "
879 "the JTAG scan chain, then performs 'jtag arp_init'."
883 .mode = COMMAND_CONFIG,
884 .jim_handler = jim_jtag_newtap,
885 .help = "Create a new TAP instance named basename.tap_type, "
886 "and appends it to the scan chain.",
887 .usage = "basename tap_type '-irlen' count "
888 "['-enable'|'-disable'] "
889 "['-expected_id' number] "
890 "['-ignore-version'] "
891 "['-ircapture' number] "
895 .name = "tapisenabled",
896 .mode = COMMAND_EXEC,
897 .jim_handler = jim_jtag_tap_enabler,
898 .help = "Returns a Tcl boolean (0/1) indicating whether "
899 "the TAP is enabled (1) or not (0).",
904 .mode = COMMAND_EXEC,
905 .jim_handler = jim_jtag_tap_enabler,
906 .help = "Try to enable the specified TAP using the "
907 "'tap-enable' TAP event.",
911 .name = "tapdisable",
912 .mode = COMMAND_EXEC,
913 .jim_handler = jim_jtag_tap_enabler,
914 .help = "Try to disable the specified TAP using the "
915 "'tap-disable' TAP event.",
920 .mode = COMMAND_EXEC,
921 .jim_handler = jim_jtag_configure,
922 .help = "Provide a Tcl handler for the specified "
924 .usage = "tap_name '-event' event_name handler",
928 .mode = COMMAND_EXEC,
929 .jim_handler = jim_jtag_configure,
930 .help = "Return any Tcl handler for the specified "
932 .usage = "tap_name '-event' event_name",
937 .jim_handler = jim_jtag_names,
938 .help = "Returns list of all JTAG tap names.",
941 .chain = jtag_command_handlers_to_move,
943 COMMAND_REGISTRATION_DONE
946 void jtag_notify_event(enum jtag_event event)
948 struct jtag_tap *tap;
950 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
951 jtag_tap_handle_event(tap, event);
955 COMMAND_HANDLER(handle_scan_chain_command)
957 struct jtag_tap *tap;
958 char expected_id[12];
960 tap = jtag_all_taps();
961 command_print(CMD_CTX,
962 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
963 command_print(CMD_CTX,
964 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
967 uint32_t expected, expected_mask, ii;
969 snprintf(expected_id, sizeof expected_id, "0x%08x",
970 (unsigned)((tap->expected_ids_cnt > 0)
971 ? tap->expected_ids[0]
973 if (tap->ignore_version)
974 expected_id[2] = '*';
976 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
977 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
979 command_print(CMD_CTX,
980 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
981 tap->abs_chain_position,
983 tap->enabled ? 'Y' : 'n',
984 (unsigned int)(tap->idcode),
986 (unsigned int)(tap->ir_length),
987 (unsigned int)(expected),
988 (unsigned int)(expected_mask));
990 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
991 snprintf(expected_id, sizeof expected_id, "0x%08x",
992 (unsigned) tap->expected_ids[1]);
993 if (tap->ignore_version)
994 expected_id[2] = '*';
996 command_print(CMD_CTX,
1001 tap = tap->next_tap;
1007 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1010 return ERROR_COMMAND_SYNTAX_ERROR;
1014 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1016 jtag_set_ntrst_delay(delay);
1018 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1022 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1025 return ERROR_COMMAND_SYNTAX_ERROR;
1029 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1031 jtag_set_ntrst_assert_width(delay);
1033 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1037 COMMAND_HANDLER(handle_jtag_rclk_command)
1040 return ERROR_COMMAND_SYNTAX_ERROR;
1042 int retval = ERROR_OK;
1046 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1048 retval = jtag_config_rclk(khz);
1049 if (ERROR_OK != retval)
1053 int cur_khz = jtag_get_speed_khz();
1054 retval = jtag_get_speed_readable(&cur_khz);
1055 if (ERROR_OK != retval)
1059 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1061 command_print(CMD_CTX, "RCLK - adaptive");
1066 COMMAND_HANDLER(handle_jtag_reset_command)
1069 return ERROR_COMMAND_SYNTAX_ERROR;
1072 if (CMD_ARGV[0][0] == '1')
1074 else if (CMD_ARGV[0][0] == '0')
1077 return ERROR_COMMAND_SYNTAX_ERROR;
1080 if (CMD_ARGV[1][0] == '1')
1082 else if (CMD_ARGV[1][0] == '0')
1085 return ERROR_COMMAND_SYNTAX_ERROR;
1087 if (adapter_init(CMD_CTX) != ERROR_OK)
1088 return ERROR_JTAG_INIT_FAILED;
1090 jtag_add_reset(trst, srst);
1091 return jtag_execute_queue();
1094 COMMAND_HANDLER(handle_runtest_command)
1097 return ERROR_COMMAND_SYNTAX_ERROR;
1099 unsigned num_clocks;
1100 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1102 jtag_add_runtest(num_clocks, TAP_IDLE);
1103 return jtag_execute_queue();
1107 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1108 * should be stable ... and *NOT* a shift state, otherwise free-running
1109 * jtag clocks could change the values latched by the update state.
1110 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1111 * and "drscan" commands are a write-only subset of what SVF provides.
1114 COMMAND_HANDLER(handle_irscan_command)
1117 struct scan_field *fields;
1118 struct jtag_tap *tap = NULL;
1119 tap_state_t endstate;
1121 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1123 return ERROR_COMMAND_SYNTAX_ERROR;
1126 /* optional "-endstate" "statename" at the end of the arguments,
1127 * so that e.g. IRPAUSE can let us load the data register before
1128 * entering RUN/IDLE to execute the instruction we load here.
1130 endstate = TAP_IDLE;
1132 if (CMD_ARGC >= 4) {
1133 /* have at least one pair of numbers. */
1134 /* is last pair the magic text? */
1135 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1136 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1137 if (endstate == TAP_INVALID)
1138 return ERROR_COMMAND_SYNTAX_ERROR;
1139 if (!scan_is_safe(endstate))
1140 LOG_WARNING("unstable irscan endstate \"%s\"",
1141 CMD_ARGV[CMD_ARGC - 1]);
1146 int num_fields = CMD_ARGC / 2;
1149 /* we really should be looking at plain_ir_scan if we want
1150 * anything more fancy.
1152 LOG_ERROR("Specify a single value for tap");
1153 return ERROR_COMMAND_SYNTAX_ERROR;
1156 size_t fields_len = sizeof(struct scan_field) * num_fields;
1157 fields = malloc(fields_len);
1158 memset(fields, 0, fields_len);
1161 for (i = 0; i < num_fields; i++)
1163 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1167 for (j = 0; j < i; j++)
1168 free((void *)fields[j].out_value);
1170 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1174 int field_size = tap->ir_length;
1175 fields[i].num_bits = field_size;
1176 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1179 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1180 if (ERROR_OK != retval)
1182 void *v = (void *)fields[i].out_value;
1183 buf_set_u32(v, 0, field_size, value);
1184 fields[i].in_value = NULL;
1187 /* did we have an endstate? */
1188 jtag_add_ir_scan(tap, fields, endstate);
1190 retval = jtag_execute_queue();
1193 for (i = 0; i < num_fields; i++)
1195 if (NULL != fields[i].out_value)
1196 free((void *)fields[i].out_value);
1205 COMMAND_HANDLER(handle_verify_ircapture_command)
1208 return ERROR_COMMAND_SYNTAX_ERROR;
1213 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1214 jtag_set_verify_capture_ir(enable);
1217 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1218 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1223 COMMAND_HANDLER(handle_verify_jtag_command)
1226 return ERROR_COMMAND_SYNTAX_ERROR;
1231 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1232 jtag_set_verify(enable);
1235 const char *status = jtag_will_verify() ? "enabled": "disabled";
1236 command_print(CMD_CTX, "verify jtag capture is %s", status);
1241 COMMAND_HANDLER(handle_tms_sequence_command)
1244 return ERROR_COMMAND_SYNTAX_ERROR;
1249 if (strcmp(CMD_ARGV[0], "short") == 0)
1250 use_new_table = true;
1251 else if (strcmp(CMD_ARGV[0], "long") == 0)
1252 use_new_table = false;
1254 return ERROR_COMMAND_SYNTAX_ERROR;
1256 tap_use_new_tms_table(use_new_table);
1259 command_print(CMD_CTX, "tms sequence is %s",
1260 tap_uses_new_tms_table() ? "short": "long");
1265 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1268 return ERROR_COMMAND_SYNTAX_ERROR;
1271 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1273 jtag_set_flush_queue_sleep(sleep_ms);
1278 COMMAND_HANDLER(handle_wait_srst_deassert)
1281 return ERROR_COMMAND_SYNTAX_ERROR;
1284 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1285 if ((timeout_ms <= 0) || (timeout_ms > 100000))
1287 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1291 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1293 long long then = timeval_ms();
1294 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK)
1296 if ((timeval_ms() - then) > timeout_ms)
1298 LOG_ERROR("Timed out");
1304 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK)
1306 if ((timeval_ms() - then) > timeout_ms)
1308 LOG_ERROR("Timed out");
1320 static const struct command_registration jtag_command_handlers[] = {
1323 .name = "jtag_flush_queue_sleep",
1324 .handler = handle_jtag_flush_queue_sleep,
1325 .mode = COMMAND_ANY,
1326 .help = "For debug purposes(simulate long delays of interface) "
1327 "to test performance or change in behavior. Default 0ms.",
1328 .usage = "[sleep in ms]",
1331 .name = "jtag_rclk",
1332 .handler = handle_jtag_rclk_command,
1333 .mode = COMMAND_ANY,
1334 .help = "With an argument, change to to use adaptive clocking "
1335 "if possible; else to use the fallback speed. "
1336 "With or without argument, display current setting.",
1337 .usage = "[fallback_speed_khz]",
1340 .name = "jtag_ntrst_delay",
1341 .handler = handle_jtag_ntrst_delay_command,
1342 .mode = COMMAND_ANY,
1343 .help = "delay after deasserting trst in ms",
1344 .usage = "[milliseconds]",
1347 .name = "jtag_ntrst_assert_width",
1348 .handler = handle_jtag_ntrst_assert_width_command,
1349 .mode = COMMAND_ANY,
1350 .help = "delay after asserting trst in ms",
1351 .usage = "[milliseconds]",
1354 .name = "scan_chain",
1355 .handler = handle_scan_chain_command,
1356 .mode = COMMAND_ANY,
1357 .help = "print current scan chain configuration",
1360 .name = "jtag_reset",
1361 .handler = handle_jtag_reset_command,
1362 .mode = COMMAND_EXEC,
1363 .help = "Set reset line values. Value '1' is active, "
1364 "value '0' is inactive.",
1365 .usage = "trst_active srst_active",
1369 .handler = handle_runtest_command,
1370 .mode = COMMAND_EXEC,
1371 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1372 .usage = "num_cycles"
1376 .handler = handle_irscan_command,
1377 .mode = COMMAND_EXEC,
1378 .help = "Execute Instruction Register (DR) scan. The "
1379 "specified opcodes are put into each TAP's IR, "
1380 "and other TAPs are put in BYPASS.",
1381 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1384 .name = "verify_ircapture",
1385 .handler = handle_verify_ircapture_command,
1386 .mode = COMMAND_ANY,
1387 .help = "Display or assign flag controlling whether to "
1388 "verify values captured during Capture-IR.",
1389 .usage = "['enable'|'disable']",
1392 .name = "verify_jtag",
1393 .handler = handle_verify_jtag_command,
1394 .mode = COMMAND_ANY,
1395 .help = "Display or assign flag controlling whether to "
1396 "verify values captured during IR and DR scans.",
1397 .usage = "['enable'|'disable']",
1400 .name = "tms_sequence",
1401 .handler = handle_tms_sequence_command,
1402 .mode = COMMAND_ANY,
1403 .help = "Display or change what style TMS sequences to use "
1404 "for JTAG state transitions: short (default) or "
1405 "long. Only for working around JTAG bugs.",
1406 /* Specifically for working around DRIVER bugs... */
1407 .usage = "['short'|'long']",
1410 .name = "wait_srst_deassert",
1411 .handler = handle_wait_srst_deassert,
1412 .mode = COMMAND_ANY,
1413 .help = "Wait for an SRST deassert. "
1414 "Useful for cases where you need something to happen within ms "
1415 "of an srst deassert. Timeout in ms ",
1420 .mode = COMMAND_ANY,
1421 .help = "perform jtag tap actions",
1423 .chain = jtag_subcommand_handlers,
1426 .chain = jtag_command_handlers_to_move,
1428 COMMAND_REGISTRATION_DONE
1431 int jtag_register_commands(struct command_context *cmd_ctx)
1433 return register_commands(cmd_ctx, NULL, jtag_command_handlers);