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"
44 #include <helper/time_support.h>
48 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
51 static const Jim_Nvp nvp_jtag_tap_event[] = {
52 { .value = JTAG_TRST_ASSERTED, .name = "post-reset" },
53 { .value = JTAG_TAP_EVENT_SETUP, .name = "setup" },
54 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
55 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
57 { .name = NULL, .value = -1 }
60 extern struct jtag_interface *jtag_interface;
62 struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
64 const char *cp = Jim_GetString(o, NULL);
65 struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
69 Jim_SetResultFormatted(interp, "Tap '%s' could not be found", cp);
73 static bool scan_is_safe(tap_state_t state)
87 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
90 struct scan_field *fields;
99 * args[3] = hex string
100 * ... repeat num bits and hex string ...
103 * args[N-2] = "-endstate"
104 * args[N-1] = statename
106 if ((argc < 4) || ((argc % 2) != 0))
108 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
114 script_debug(interp, "drscan", argc, args);
116 /* validate arguments as numbers */
118 for (i = 2; i < argc; i += 2)
123 e = Jim_GetLong(interp, args[i], &bits);
124 /* If valid - try next arg */
129 /* Not valid.. are we at the end? */
130 if (((i + 2) != argc)) {
131 /* nope, then error */
135 /* it could be: "-endstate FOO"
136 * e.g. DRPAUSE so we can issue more instructions
137 * before entering RUN/IDLE and executing them.
140 /* get arg as a string. */
141 cp = Jim_GetString(args[i], NULL);
142 /* is it the magic? */
143 if (0 == strcmp("-endstate", cp)) {
144 /* is the statename valid? */
145 cp = Jim_GetString(args[i + 1], NULL);
147 /* see if it is a valid state name */
148 endstate = tap_state_by_name(cp);
150 /* update the error message */
151 Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
153 if (!scan_is_safe(endstate))
154 LOG_WARNING("drscan with unsafe "
155 "endstate \"%s\"", cp);
157 /* valid - so clear the error */
159 /* and remove the last 2 args */
164 /* Still an error? */
166 return e; /* too bad */
168 } /* validate args */
172 tap = jtag_tap_by_jim_obj(interp, args[1]);
177 num_fields = (argc-2)/2;
178 assert(num_fields > 0);
179 fields = malloc(sizeof(struct scan_field) * num_fields);
180 for (i = 2; i < argc; i += 2)
186 Jim_GetLong(interp, args[i], &bits);
187 str = Jim_GetString(args[i + 1], &len);
189 fields[field_count].num_bits = bits;
190 void * t = malloc(DIV_ROUND_UP(bits, 8));
191 fields[field_count].out_value = t;
192 str_to_buf(str, len, t, bits, 0);
193 fields[field_count].in_value = t;
197 jtag_add_dr_scan(tap, num_fields, fields, endstate);
199 retval = jtag_execute_queue();
200 if (retval != ERROR_OK)
202 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
207 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
208 for (i = 2; i < argc; i += 2)
213 Jim_GetLong(interp, args[i], &bits);
214 str = buf_to_str(fields[field_count].in_value, bits, 16);
215 free((void *)fields[field_count].out_value);
217 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
222 Jim_SetResult(interp, list);
230 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
232 tap_state_t states[8];
234 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
236 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
240 script_debug(interp, "pathmove", argc, args);
243 for (i = 0; i < argc-1; i++)
246 cp = Jim_GetString(args[i + 1], NULL);
247 states[i] = tap_state_by_name(cp);
250 /* update the error message */
251 Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
256 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
258 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
262 jtag_add_pathmove(argc-2, states + 1);
264 if (jtag_execute_queue()!= ERROR_OK)
266 Jim_SetResultString(interp, "pathmove: failed",-1);
274 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
276 script_debug(interp, "flush_count", argc, args);
278 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
283 /* REVISIT Just what about these should "move" ... ?
284 * These registrations, into the main JTAG table?
286 * There's a minor compatibility issue, these all show up twice;
287 * that's not desirable:
288 * - jtag drscan ... NOT DOCUMENTED!
291 * The "irscan" command (for example) doesn't show twice.
293 static const struct command_registration jtag_command_handlers_to_move[] = {
296 .mode = COMMAND_EXEC,
297 .jim_handler = Jim_Command_drscan,
298 .help = "Execute Data Register (DR) scan for one TAP. "
299 "Other TAPs must be in BYPASS mode.",
300 .usage = "tap_name [num_bits value]* ['-endstate' state_name]",
303 .name = "flush_count",
304 .mode = COMMAND_EXEC,
305 .jim_handler = Jim_Command_flush_count,
306 .help = "Returns the number of times the JTAG queue "
311 .mode = COMMAND_EXEC,
312 .jim_handler = Jim_Command_pathmove,
313 .usage = "start_state state1 [state2 [state3 ...]]",
314 .help = "Move JTAG state machine from current state "
315 "(start_state) to state1, then state2, state3, etc.",
317 COMMAND_REGISTRATION_DONE
321 enum jtag_tap_cfg_param {
325 static Jim_Nvp nvp_config_opts[] = {
326 { .name = "-event", .value = JCFG_EVENT },
328 { .name = NULL, .value = -1 }
331 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
335 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
340 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
343 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
347 if (goi->isconfigure) {
348 if (goi->argc != 1) {
349 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
353 if (goi->argc != 0) {
354 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
359 struct jtag_tap_event_action *jteap = tap->event_action;
360 /* replace existing event body */
364 if (jteap->event == (enum jtag_event)n->value)
372 Jim_SetEmptyResult(goi->interp);
374 if (goi->isconfigure)
377 jteap = calloc(1, sizeof(*jteap));
378 else if (NULL != jteap->body)
379 Jim_DecrRefCount(goi->interp, jteap->body);
381 jteap->interp = goi->interp;
382 jteap->event = n->value;
385 Jim_GetOpt_Obj(goi, &o);
386 jteap->body = Jim_DuplicateObj(goi->interp, o);
387 Jim_IncrRefCount(jteap->body);
391 /* add to head of event list */
392 jteap->next = tap->event_action;
393 tap->event_action = jteap;
398 jteap->interp = goi->interp;
399 Jim_SetResult(goi->interp,
400 Jim_DuplicateObj(goi->interp, jteap->body));
405 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
407 /* parse config or cget options */
408 while (goi->argc > 0)
410 Jim_SetEmptyResult (goi->interp);
413 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
416 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
423 e = jtag_tap_configure_event(goi, tap);
428 Jim_SetResultFormatted(goi->interp, "unknown event: %s", n->name);
436 static int is_bad_irval(int ir_length, jim_wide w)
446 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
447 struct jtag_tap *pTap)
450 int e = Jim_GetOpt_Wide(goi, &w);
452 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
456 unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
457 uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
458 if (new_expected_ids == NULL)
460 Jim_SetResultFormatted(goi->interp, "no memory");
464 memcpy(new_expected_ids, pTap->expected_ids, expected_len);
466 new_expected_ids[pTap->expected_ids_cnt] = w;
468 free(pTap->expected_ids);
469 pTap->expected_ids = new_expected_ids;
470 pTap->expected_ids_cnt++;
475 #define NTAP_OPT_IRLEN 0
476 #define NTAP_OPT_IRMASK 1
477 #define NTAP_OPT_IRCAPTURE 2
478 #define NTAP_OPT_ENABLED 3
479 #define NTAP_OPT_DISABLED 4
480 #define NTAP_OPT_EXPECTED_ID 5
481 #define NTAP_OPT_VERSION 6
483 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
484 struct jtag_tap *pTap)
487 int e = Jim_GetOpt_Wide(goi, &w);
490 Jim_SetResultFormatted(goi->interp,
491 "option: %s bad parameter", n->name);
492 free((void *)pTap->dotted_name);
497 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
499 LOG_WARNING("%s: huge IR length %d",
500 pTap->dotted_name, (int) w);
504 case NTAP_OPT_IRMASK:
505 if (is_bad_irval(pTap->ir_length, w))
507 LOG_ERROR("%s: IR mask %x too big",
513 LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
514 pTap->ir_capture_mask = w;
516 case NTAP_OPT_IRCAPTURE:
517 if (is_bad_irval(pTap->ir_length, w))
519 LOG_ERROR("%s: IR capture %x too big",
520 pTap->dotted_name, (int) w);
524 LOG_WARNING("%s: nonstandard IR value",
526 pTap->ir_capture_value = w;
534 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
536 struct jtag_tap *pTap;
541 const Jim_Nvp opts[] = {
542 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
543 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
544 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
545 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
546 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
547 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
548 { .name = "-ignore-version" , .value = NTAP_OPT_VERSION },
549 { .name = NULL , .value = -1 },
552 pTap = calloc(1, sizeof(struct jtag_tap));
554 Jim_SetResultFormatted(goi->interp, "no memory");
559 * we expect CHIP + TAP + OPTIONS
562 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
566 Jim_GetOpt_String(goi, &cp, NULL);
567 pTap->chip = strdup(cp);
569 Jim_GetOpt_String(goi, &cp, NULL);
570 pTap->tapname = strdup(cp);
572 /* name + dot + name + null */
573 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
575 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
576 pTap->dotted_name = cp;
578 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
579 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
581 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
582 * that the default. The "-irlen" 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);
592 free((void *)pTap->dotted_name);
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);
608 free((void *)pTap->dotted_name);
614 case NTAP_OPT_IRMASK:
615 case NTAP_OPT_IRCAPTURE:
616 e = jim_newtap_ir_param(n, goi, pTap);
619 free((void *)pTap->dotted_name);
624 case NTAP_OPT_VERSION:
625 pTap->ignore_version = true;
627 } /* switch (n->value) */
628 } /* while (goi->argc) */
630 /* default is enabled-after-reset */
631 pTap->enabled = !pTap->disabled_after_reset;
633 /* Did all the required option bits get cleared? */
634 if (pTap->ir_length != 0)
640 Jim_SetResultFormatted(goi->interp,
641 "newtap: %s missing IR length",
647 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
649 struct jtag_tap_event_action * jteap;
651 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
653 if (jteap->event != e)
656 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
657 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
658 tap->dotted_name, e, nvp->name,
659 Jim_GetString(jteap->body, NULL));
661 if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK)
663 Jim_MakeErrorMessage(jteap->interp);
664 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
670 case JTAG_TAP_EVENT_ENABLE:
671 case JTAG_TAP_EVENT_DISABLE:
672 /* NOTE: we currently assume the handlers
673 * can't fail. Right here is where we should
674 * really be verifying the scan chains ...
676 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
677 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
678 tap->enabled ? "enabled" : "disabled");
686 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
689 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
691 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
694 struct command_context *context = current_command_context(interp);
695 int e = jtag_init_inner(context);
697 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
698 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
699 Jim_FreeNewObj(goi.interp, eObj);
705 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
708 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
710 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
713 struct command_context *context = current_command_context(interp);
714 int e = jtag_init_reset(context);
716 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
717 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
718 Jim_FreeNewObj(goi.interp, eObj);
724 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
727 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
728 return jim_newtap_cmd(&goi);
731 static bool jtag_tap_enable(struct jtag_tap *t)
735 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
739 /* FIXME add JTAG sanity checks, w/o TLR
740 * - scan chain length grew by one (this)
741 * - IDs and IR lengths are as expected
743 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
746 static bool jtag_tap_disable(struct jtag_tap *t)
750 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
754 /* FIXME add JTAG sanity checks, w/o TLR
755 * - scan chain length shrank by one (this)
756 * - IDs and IR lengths are as expected
758 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
762 int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
764 const char *cmd_name = Jim_GetString(argv[0], NULL);
766 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
768 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
774 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
778 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
779 // do nothing, just return the value
780 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
781 if (!jtag_tap_enable(t)){
782 LOG_WARNING("failed to enable tap %s", t->dotted_name);
785 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
786 if (!jtag_tap_disable(t)){
787 LOG_WARNING("failed to disable tap %s", t->dotted_name);
791 LOG_ERROR("command '%s' unknown", cmd_name);
795 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
799 int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
801 const char *cmd_name = Jim_GetString(argv[0], NULL);
803 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
804 goi.isconfigure = !strcmp(cmd_name, "configure");
805 if (goi.argc < 2 + goi.isconfigure) {
806 Jim_WrongNumArgs(goi.interp, 0, NULL,
807 "<tap_name> <attribute> ...");
814 Jim_GetOpt_Obj(&goi, &o);
815 t = jtag_tap_by_jim_obj(goi.interp, o);
820 return jtag_tap_configure_cmd(&goi, t);
823 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
826 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
828 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
831 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
832 struct jtag_tap *tap;
834 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
835 Jim_ListAppendElement(goi.interp,
836 Jim_GetResult(goi.interp),
837 Jim_NewStringObj(goi.interp,
838 tap->dotted_name, -1));
843 COMMAND_HANDLER(handle_jtag_init_command)
846 return ERROR_COMMAND_SYNTAX_ERROR;
848 static bool jtag_initialized = false;
849 if (jtag_initialized)
851 LOG_INFO("'jtag init' has already been called");
854 jtag_initialized = true;
856 LOG_DEBUG("Initializing jtag devices...");
857 return jtag_init(CMD_CTX);
860 static const struct command_registration jtag_subcommand_handlers[] = {
864 .handler = handle_jtag_init_command,
865 .help = "initialize jtag scan chain",
871 .jim_handler = jim_jtag_arp_init,
872 .help = "Validates JTAG scan chain against the list of "
873 "declared TAPs using just the four standard JTAG "
877 .name = "arp_init-reset",
879 .jim_handler = jim_jtag_arp_init_reset,
880 .help = "Uses TRST and SRST to try resetting everything on "
881 "the JTAG scan chain, then performs 'jtag arp_init'."
885 .mode = COMMAND_CONFIG,
886 .jim_handler = jim_jtag_newtap,
887 .help = "Create a new TAP instance named basename.tap_type, "
888 "and appends it to the scan chain.",
889 .usage = "basename tap_type '-irlen' count "
890 "['-enable'|'-disable'] "
891 "['-expected_id' number] "
892 "['-ignore-version'] "
893 "['-ircapture' number] "
897 .name = "tapisenabled",
898 .mode = COMMAND_EXEC,
899 .jim_handler = jim_jtag_tap_enabler,
900 .help = "Returns a Tcl boolean (0/1) indicating whether "
901 "the TAP is enabled (1) or not (0).",
906 .mode = COMMAND_EXEC,
907 .jim_handler = jim_jtag_tap_enabler,
908 .help = "Try to enable the specified TAP using the "
909 "'tap-enable' TAP event.",
913 .name = "tapdisable",
914 .mode = COMMAND_EXEC,
915 .jim_handler = jim_jtag_tap_enabler,
916 .help = "Try to disable the specified TAP using the "
917 "'tap-disable' TAP event.",
922 .mode = COMMAND_EXEC,
923 .jim_handler = jim_jtag_configure,
924 .help = "Provide a Tcl handler for the specified "
926 .usage = "tap_name '-event' event_name handler",
930 .mode = COMMAND_EXEC,
931 .jim_handler = jim_jtag_configure,
932 .help = "Return any Tcl handler for the specified "
934 .usage = "tap_name '-event' event_name",
939 .jim_handler = jim_jtag_names,
940 .help = "Returns list of all JTAG tap names.",
943 .chain = jtag_command_handlers_to_move,
945 COMMAND_REGISTRATION_DONE
948 void jtag_notify_event(enum jtag_event event)
950 struct jtag_tap *tap;
952 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
953 jtag_tap_handle_event(tap, event);
957 COMMAND_HANDLER(handle_scan_chain_command)
959 struct jtag_tap *tap;
960 char expected_id[12];
962 tap = jtag_all_taps();
963 command_print(CMD_CTX,
964 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
965 command_print(CMD_CTX,
966 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
969 uint32_t expected, expected_mask, ii;
971 snprintf(expected_id, sizeof expected_id, "0x%08x",
972 (unsigned)((tap->expected_ids_cnt > 0)
973 ? tap->expected_ids[0]
975 if (tap->ignore_version)
976 expected_id[2] = '*';
978 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
979 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
981 command_print(CMD_CTX,
982 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
983 tap->abs_chain_position,
985 tap->enabled ? 'Y' : 'n',
986 (unsigned int)(tap->idcode),
988 (unsigned int)(tap->ir_length),
989 (unsigned int)(expected),
990 (unsigned int)(expected_mask));
992 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
993 snprintf(expected_id, sizeof expected_id, "0x%08x",
994 (unsigned) tap->expected_ids[1]);
995 if (tap->ignore_version)
996 expected_id[2] = '*';
998 command_print(CMD_CTX,
1003 tap = tap->next_tap;
1009 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1012 return ERROR_COMMAND_SYNTAX_ERROR;
1016 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1018 jtag_set_ntrst_delay(delay);
1020 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1024 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1027 return ERROR_COMMAND_SYNTAX_ERROR;
1031 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1033 jtag_set_ntrst_assert_width(delay);
1035 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1039 COMMAND_HANDLER(handle_jtag_rclk_command)
1042 return ERROR_COMMAND_SYNTAX_ERROR;
1044 int retval = ERROR_OK;
1048 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1050 retval = jtag_config_rclk(khz);
1051 if (ERROR_OK != retval)
1055 int cur_khz = jtag_get_speed_khz();
1056 retval = jtag_get_speed_readable(&cur_khz);
1057 if (ERROR_OK != retval)
1061 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1063 command_print(CMD_CTX, "RCLK - adaptive");
1068 COMMAND_HANDLER(handle_jtag_reset_command)
1071 return ERROR_COMMAND_SYNTAX_ERROR;
1074 if (CMD_ARGV[0][0] == '1')
1076 else if (CMD_ARGV[0][0] == '0')
1079 return ERROR_COMMAND_SYNTAX_ERROR;
1082 if (CMD_ARGV[1][0] == '1')
1084 else if (CMD_ARGV[1][0] == '0')
1087 return ERROR_COMMAND_SYNTAX_ERROR;
1089 if (adapter_init(CMD_CTX) != ERROR_OK)
1090 return ERROR_JTAG_INIT_FAILED;
1092 jtag_add_reset(trst, srst);
1093 return jtag_execute_queue();
1096 COMMAND_HANDLER(handle_runtest_command)
1099 return ERROR_COMMAND_SYNTAX_ERROR;
1101 unsigned num_clocks;
1102 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1104 jtag_add_runtest(num_clocks, TAP_IDLE);
1105 return jtag_execute_queue();
1109 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1110 * should be stable ... and *NOT* a shift state, otherwise free-running
1111 * jtag clocks could change the values latched by the update state.
1112 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1113 * and "drscan" commands are a write-only subset of what SVF provides.
1116 COMMAND_HANDLER(handle_irscan_command)
1119 struct scan_field *fields;
1120 struct jtag_tap *tap = NULL;
1121 tap_state_t endstate;
1123 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1125 return ERROR_COMMAND_SYNTAX_ERROR;
1128 /* optional "-endstate" "statename" at the end of the arguments,
1129 * so that e.g. IRPAUSE can let us load the data register before
1130 * entering RUN/IDLE to execute the instruction we load here.
1132 endstate = TAP_IDLE;
1134 if (CMD_ARGC >= 4) {
1135 /* have at least one pair of numbers. */
1136 /* is last pair the magic text? */
1137 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1138 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1139 if (endstate == TAP_INVALID)
1140 return ERROR_COMMAND_SYNTAX_ERROR;
1141 if (!scan_is_safe(endstate))
1142 LOG_WARNING("unstable irscan endstate \"%s\"",
1143 CMD_ARGV[CMD_ARGC - 1]);
1148 int num_fields = CMD_ARGC / 2;
1151 /* we really should be looking at plain_ir_scan if we want
1152 * anything more fancy.
1154 LOG_ERROR("Specify a single value for tap");
1155 return ERROR_COMMAND_SYNTAX_ERROR;
1158 size_t fields_len = sizeof(struct scan_field) * num_fields;
1159 fields = malloc(fields_len);
1160 memset(fields, 0, fields_len);
1163 for (i = 0; i < num_fields; i++)
1165 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1169 for (j = 0; j < i; j++)
1170 free((void *)fields[j].out_value);
1172 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1176 int field_size = tap->ir_length;
1177 fields[i].num_bits = field_size;
1178 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1181 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1182 if (ERROR_OK != retval)
1184 void *v = (void *)fields[i].out_value;
1185 buf_set_u32(v, 0, field_size, value);
1186 fields[i].in_value = NULL;
1189 /* did we have an endstate? */
1190 jtag_add_ir_scan(tap, fields, endstate);
1192 retval = jtag_execute_queue();
1195 for (i = 0; i < num_fields; i++)
1197 if (NULL != fields[i].out_value)
1198 free((void *)fields[i].out_value);
1207 COMMAND_HANDLER(handle_verify_ircapture_command)
1210 return ERROR_COMMAND_SYNTAX_ERROR;
1215 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1216 jtag_set_verify_capture_ir(enable);
1219 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1220 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1225 COMMAND_HANDLER(handle_verify_jtag_command)
1228 return ERROR_COMMAND_SYNTAX_ERROR;
1233 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1234 jtag_set_verify(enable);
1237 const char *status = jtag_will_verify() ? "enabled": "disabled";
1238 command_print(CMD_CTX, "verify jtag capture is %s", status);
1243 COMMAND_HANDLER(handle_tms_sequence_command)
1246 return ERROR_COMMAND_SYNTAX_ERROR;
1251 if (strcmp(CMD_ARGV[0], "short") == 0)
1252 use_new_table = true;
1253 else if (strcmp(CMD_ARGV[0], "long") == 0)
1254 use_new_table = false;
1256 return ERROR_COMMAND_SYNTAX_ERROR;
1258 tap_use_new_tms_table(use_new_table);
1261 command_print(CMD_CTX, "tms sequence is %s",
1262 tap_uses_new_tms_table() ? "short": "long");
1267 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1270 return ERROR_COMMAND_SYNTAX_ERROR;
1273 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1275 jtag_set_flush_queue_sleep(sleep_ms);
1280 COMMAND_HANDLER(handle_wait_srst_deassert)
1283 return ERROR_COMMAND_SYNTAX_ERROR;
1286 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1287 if ((timeout_ms <= 0) || (timeout_ms > 100000))
1289 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1293 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1295 long long then = timeval_ms();
1296 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK)
1298 if ((timeval_ms() - then) > timeout_ms)
1300 LOG_ERROR("Timed out");
1306 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK)
1308 if ((timeval_ms() - then) > timeout_ms)
1310 LOG_ERROR("Timed out");
1322 static const struct command_registration jtag_command_handlers[] = {
1325 .name = "jtag_flush_queue_sleep",
1326 .handler = handle_jtag_flush_queue_sleep,
1327 .mode = COMMAND_ANY,
1328 .help = "For debug purposes(simulate long delays of interface) "
1329 "to test performance or change in behavior. Default 0ms.",
1330 .usage = "[sleep in ms]",
1333 .name = "jtag_rclk",
1334 .handler = handle_jtag_rclk_command,
1335 .mode = COMMAND_ANY,
1336 .help = "With an argument, change to to use adaptive clocking "
1337 "if possible; else to use the fallback speed. "
1338 "With or without argument, display current setting.",
1339 .usage = "[fallback_speed_khz]",
1342 .name = "jtag_ntrst_delay",
1343 .handler = handle_jtag_ntrst_delay_command,
1344 .mode = COMMAND_ANY,
1345 .help = "delay after deasserting trst in ms",
1346 .usage = "[milliseconds]",
1349 .name = "jtag_ntrst_assert_width",
1350 .handler = handle_jtag_ntrst_assert_width_command,
1351 .mode = COMMAND_ANY,
1352 .help = "delay after asserting trst in ms",
1353 .usage = "[milliseconds]",
1356 .name = "scan_chain",
1357 .handler = handle_scan_chain_command,
1358 .mode = COMMAND_ANY,
1359 .help = "print current scan chain configuration",
1363 .name = "jtag_reset",
1364 .handler = handle_jtag_reset_command,
1365 .mode = COMMAND_EXEC,
1366 .help = "Set reset line values. Value '1' is active, "
1367 "value '0' is inactive.",
1368 .usage = "trst_active srst_active",
1372 .handler = handle_runtest_command,
1373 .mode = COMMAND_EXEC,
1374 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1375 .usage = "num_cycles"
1379 .handler = handle_irscan_command,
1380 .mode = COMMAND_EXEC,
1381 .help = "Execute Instruction Register (DR) scan. The "
1382 "specified opcodes are put into each TAP's IR, "
1383 "and other TAPs are put in BYPASS.",
1384 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1387 .name = "verify_ircapture",
1388 .handler = handle_verify_ircapture_command,
1389 .mode = COMMAND_ANY,
1390 .help = "Display or assign flag controlling whether to "
1391 "verify values captured during Capture-IR.",
1392 .usage = "['enable'|'disable']",
1395 .name = "verify_jtag",
1396 .handler = handle_verify_jtag_command,
1397 .mode = COMMAND_ANY,
1398 .help = "Display or assign flag controlling whether to "
1399 "verify values captured during IR and DR scans.",
1400 .usage = "['enable'|'disable']",
1403 .name = "tms_sequence",
1404 .handler = handle_tms_sequence_command,
1405 .mode = COMMAND_ANY,
1406 .help = "Display or change what style TMS sequences to use "
1407 "for JTAG state transitions: short (default) or "
1408 "long. Only for working around JTAG bugs.",
1409 /* Specifically for working around DRIVER bugs... */
1410 .usage = "['short'|'long']",
1413 .name = "wait_srst_deassert",
1414 .handler = handle_wait_srst_deassert,
1415 .mode = COMMAND_ANY,
1416 .help = "Wait for an SRST deassert. "
1417 "Useful for cases where you need something to happen within ms "
1418 "of an srst deassert. Timeout in ms ",
1423 .mode = COMMAND_ANY,
1424 .help = "perform jtag tap actions",
1427 .chain = jtag_subcommand_handlers,
1430 .chain = jtag_command_handlers_to_move,
1432 COMMAND_REGISTRATION_DONE
1435 int jtag_register_commands(struct command_context *cmd_ctx)
1437 return register_commands(cmd_ctx, NULL, jtag_command_handlers);