1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Ø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 static const Jim_Nvp nvp_jtag_tap_event[] = {
44 { .value = JTAG_TRST_ASSERTED, .name = "post-reset" },
45 { .value = JTAG_TAP_EVENT_SETUP, .name = "setup" },
46 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
47 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
49 { .name = NULL, .value = -1 }
52 extern struct jtag_interface *jtag_interface;
54 static bool scan_is_safe(tap_state_t state)
68 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
71 struct scan_field *fields;
80 * args[3] = hex string
81 * ... repeat num bits and hex string ...
84 * args[N-2] = "-endstate"
85 * args[N-1] = statename
87 if ((argc < 4) || ((argc % 2) != 0))
89 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
95 script_debug(interp, "drscan", argc, args);
97 /* validate arguments as numbers */
99 for (i = 2; i < argc; i += 2)
104 e = Jim_GetLong(interp, args[i], &bits);
105 /* If valid - try next arg */
110 /* Not valid.. are we at the end? */
111 if (((i + 2) != argc)) {
112 /* nope, then error */
116 /* it could be: "-endstate FOO"
117 * e.g. DRPAUSE so we can issue more instructions
118 * before entering RUN/IDLE and executing them.
121 /* get arg as a string. */
122 cp = Jim_GetString(args[i], NULL);
123 /* is it the magic? */
124 if (0 == strcmp("-endstate", cp)) {
125 /* is the statename valid? */
126 cp = Jim_GetString(args[i + 1], NULL);
128 /* see if it is a valid state name */
129 endstate = tap_state_by_name(cp);
131 /* update the error message */
132 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
134 if (!scan_is_safe(endstate))
135 LOG_WARNING("drscan with unsafe "
136 "endstate \"%s\"", cp);
138 /* valid - so clear the error */
140 /* and remove the last 2 args */
145 /* Still an error? */
147 return e; /* too bad */
149 } /* validate args */
151 tap = jtag_tap_by_jim_obj(interp, args[1]);
156 num_fields = (argc-2)/2;
157 fields = malloc(sizeof(struct scan_field) * num_fields);
158 for (i = 2; i < argc; i += 2)
164 Jim_GetLong(interp, args[i], &bits);
165 str = Jim_GetString(args[i + 1], &len);
167 fields[field_count].tap = tap;
168 fields[field_count].num_bits = bits;
169 fields[field_count].out_value = malloc(DIV_ROUND_UP(bits, 8));
170 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
171 fields[field_count].in_value = fields[field_count].out_value;
175 jtag_add_dr_scan(num_fields, fields, endstate);
177 retval = jtag_execute_queue();
178 if (retval != ERROR_OK)
180 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
185 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
186 for (i = 2; i < argc; i += 2)
191 Jim_GetLong(interp, args[i], &bits);
192 str = buf_to_str(fields[field_count].in_value, bits, 16);
193 free(fields[field_count].out_value);
195 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
200 Jim_SetResult(interp, list);
208 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
210 tap_state_t states[8];
212 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
214 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
218 script_debug(interp, "pathmove", argc, args);
221 for (i = 0; i < argc-1; i++)
224 cp = Jim_GetString(args[i + 1], NULL);
225 states[i] = tap_state_by_name(cp);
228 /* update the error message */
229 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
234 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
236 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
240 jtag_add_pathmove(argc-2, states + 1);
242 if (jtag_execute_queue()!= ERROR_OK)
244 Jim_SetResultString(interp, "pathmove: failed",-1);
252 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
254 script_debug(interp, "flush_count", argc, args);
256 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
261 static const struct command_registration jtag_command_handlers_to_move[] = {
264 .mode = COMMAND_EXEC,
265 .jim_handler = &Jim_Command_drscan,
266 .help = "execute DR scan <device> "
267 "<num_bits> <value> <num_bits1> <value2> ...",
270 .name = "flush_count",
271 .mode = COMMAND_EXEC,
272 .jim_handler = &Jim_Command_flush_count,
273 .help = "returns number of times the JTAG queue has been flushed",
277 .mode = COMMAND_EXEC,
278 .jim_handler = &Jim_Command_pathmove,
279 .usage = "<state1>,<state2>,<state3>... ",
280 .help = "move JTAG to state1 then to state2, state3, etc.",
282 COMMAND_REGISTRATION_DONE
286 enum jtag_tap_cfg_param {
290 static Jim_Nvp nvp_config_opts[] = {
291 { .name = "-event", .value = JCFG_EVENT },
293 { .name = NULL, .value = -1 }
296 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
300 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
305 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
308 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
312 if (goi->isconfigure) {
313 if (goi->argc != 1) {
314 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
318 if (goi->argc != 0) {
319 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
324 struct jtag_tap_event_action *jteap = tap->event_action;
325 /* replace existing event body */
329 if (jteap->event == (enum jtag_event)n->value)
337 Jim_SetEmptyResult(goi->interp);
339 if (goi->isconfigure)
342 jteap = calloc(1, sizeof(*jteap));
343 else if (NULL != jteap->body)
344 Jim_DecrRefCount(interp, jteap->body);
346 jteap->event = n->value;
349 Jim_GetOpt_Obj(goi, &o);
350 jteap->body = Jim_DuplicateObj(goi->interp, o);
351 Jim_IncrRefCount(jteap->body);
355 /* add to head of event list */
356 jteap->next = tap->event_action;
357 tap->event_action = jteap;
362 Jim_SetResult(goi->interp,
363 Jim_DuplicateObj(goi->interp, jteap->body));
368 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
370 /* parse config or cget options */
371 while (goi->argc > 0)
373 Jim_SetEmptyResult (goi->interp);
376 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
379 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
386 e = jtag_tap_configure_event(goi, tap);
391 Jim_SetResult_sprintf(goi->interp, "unknown event: %s", n->name);
399 static int is_bad_irval(int ir_length, jim_wide w)
409 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
411 struct jtag_tap *pTap;
417 const Jim_Nvp opts[] = {
418 #define NTAP_OPT_IRLEN 0
419 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
420 #define NTAP_OPT_IRMASK 1
421 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
422 #define NTAP_OPT_IRCAPTURE 2
423 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
424 #define NTAP_OPT_ENABLED 3
425 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
426 #define NTAP_OPT_DISABLED 4
427 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
428 #define NTAP_OPT_EXPECTED_ID 5
429 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
430 { .name = NULL , .value = -1 },
433 pTap = calloc(1, sizeof(struct jtag_tap));
435 Jim_SetResult_sprintf(goi->interp, "no memory");
440 * we expect CHIP + TAP + OPTIONS
443 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
447 Jim_GetOpt_String(goi, &cp, NULL);
448 pTap->chip = strdup(cp);
450 Jim_GetOpt_String(goi, &cp, NULL);
451 pTap->tapname = strdup(cp);
453 /* name + dot + name + null */
454 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
456 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
457 pTap->dotted_name = cp;
459 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
460 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
462 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
463 * that the default. The "-irlen" and "-irmask" options are only
464 * needed to cope with nonstandard TAPs, or to specify more bits.
466 pTap->ir_capture_mask = 0x03;
467 pTap->ir_capture_value = 0x01;
470 e = Jim_GetOpt_Nvp(goi, opts, &n);
472 Jim_GetOpt_NvpUnknown(goi, opts, 0);
473 free((void *)pTap->dotted_name);
477 LOG_DEBUG("Processing option: %s", n->name);
479 case NTAP_OPT_ENABLED:
480 pTap->disabled_after_reset = false;
482 case NTAP_OPT_DISABLED:
483 pTap->disabled_after_reset = true;
485 case NTAP_OPT_EXPECTED_ID:
487 uint32_t *new_expected_ids;
489 e = Jim_GetOpt_Wide(goi, &w);
491 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
492 free((void *)pTap->dotted_name);
497 new_expected_ids = malloc(sizeof(uint32_t) * (pTap->expected_ids_cnt + 1));
498 if (new_expected_ids == NULL) {
499 Jim_SetResult_sprintf(goi->interp, "no memory");
500 free((void *)pTap->dotted_name);
505 memcpy(new_expected_ids, pTap->expected_ids, sizeof(uint32_t) * pTap->expected_ids_cnt);
507 new_expected_ids[pTap->expected_ids_cnt] = w;
509 free(pTap->expected_ids);
510 pTap->expected_ids = new_expected_ids;
511 pTap->expected_ids_cnt++;
515 case NTAP_OPT_IRMASK:
516 case NTAP_OPT_IRCAPTURE:
517 e = Jim_GetOpt_Wide(goi, &w);
519 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
520 free((void *)pTap->dotted_name);
526 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
527 LOG_WARNING("%s: huge IR length %d",
532 case NTAP_OPT_IRMASK:
533 if (is_bad_irval(pTap->ir_length, w)) {
534 LOG_ERROR("%s: IR mask %x too big",
537 free((void *)pTap->dotted_name);
542 LOG_WARNING("%s: nonstandard IR mask",
544 pTap->ir_capture_mask = w;
546 case NTAP_OPT_IRCAPTURE:
547 if (is_bad_irval(pTap->ir_length, w)) {
548 LOG_ERROR("%s: IR capture %x too big",
551 free((void *)pTap->dotted_name);
556 LOG_WARNING("%s: nonstandard IR value",
558 pTap->ir_capture_value = w;
561 } /* switch (n->value) */
562 } /* while (goi->argc) */
564 /* default is enabled-after-reset */
565 pTap->enabled = !pTap->disabled_after_reset;
567 /* Did all the required option bits get cleared? */
568 if (pTap->ir_length != 0)
574 Jim_SetResult_sprintf(goi->interp,
575 "newtap: %s missing IR length",
581 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
583 struct jtag_tap_event_action * jteap;
585 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
587 if (jteap->event != e)
590 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
591 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
592 tap->dotted_name, e, nvp->name,
593 Jim_GetString(jteap->body, NULL));
595 if (Jim_EvalObj(interp, jteap->body) != JIM_OK)
597 Jim_PrintErrorMessage(interp);
603 case JTAG_TAP_EVENT_ENABLE:
604 case JTAG_TAP_EVENT_DISABLE:
605 /* NOTE: we currently assume the handlers
606 * can't fail. Right here is where we should
607 * really be verifying the scan chains ...
609 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
610 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
611 tap->enabled ? "enabled" : "disabled");
619 static int jim_jtag_interface(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
622 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
624 /* return the name of the interface */
625 /* TCL code might need to know the exact type... */
626 /* FUTURE: we allow this as a means to "set" the interface. */
628 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
631 const char *name = jtag_interface ? jtag_interface->name : NULL;
632 Jim_SetResultString(goi.interp, name ? : "undefined", -1);
636 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
639 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
641 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
644 struct command_context *context = Jim_GetAssocData(interp, "context");
645 int e = jtag_init_inner(context);
647 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
653 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
656 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
658 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
661 struct command_context *context = Jim_GetAssocData(interp, "context");
662 int e = jtag_init_reset(context);
664 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
670 static int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
673 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
674 return jim_newtap_cmd(&goi);
677 static bool jtag_tap_enable(struct jtag_tap *t)
681 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
685 /* FIXME add JTAG sanity checks, w/o TLR
686 * - scan chain length grew by one (this)
687 * - IDs and IR lengths are as expected
689 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
692 static bool jtag_tap_disable(struct jtag_tap *t)
696 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
700 /* FIXME add JTAG sanity checks, w/o TLR
701 * - scan chain length shrank by one (this)
702 * - IDs and IR lengths are as expected
704 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
708 static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
710 const char *cmd_name = Jim_GetString(argv[0], NULL);
712 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
714 Jim_SetResult_sprintf(goi.interp, "usage: %s <name>", cmd_name);
720 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
724 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
725 // do nothing, just return the value
726 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
727 if (!jtag_tap_enable(t))
728 LOG_WARNING("failed to disable tap");
729 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
730 if (!jtag_tap_disable(t))
731 LOG_WARNING("failed to disable tap");
733 LOG_ERROR("command '%s' unknown", cmd_name);
737 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
741 static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
743 const char *cmd_name = Jim_GetString(argv[0], NULL);
745 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
746 goi.isconfigure = !strcmp(cmd_name, "configure");
747 if (goi.argc < 2 + goi.isconfigure) {
748 Jim_WrongNumArgs(goi.interp, 0, NULL,
749 "<tap_name> <attribute> ...");
756 Jim_GetOpt_Obj(&goi, &o);
757 t = jtag_tap_by_jim_obj(goi.interp, o);
762 return jtag_tap_configure_cmd(&goi, t);
765 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
768 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
770 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
773 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
774 struct jtag_tap *tap;
776 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
777 Jim_ListAppendElement(goi.interp,
778 Jim_GetResult(goi.interp),
779 Jim_NewStringObj(goi.interp,
780 tap->dotted_name, -1));
785 static const struct command_registration jtag_subcommand_handlers[] = {
789 .jim_handler = &jim_jtag_interface,
790 .help = "Returns the selected interface",
795 .jim_handler = &jim_jtag_arp_init,
798 .name = "arp_init-reset",
800 .jim_handler = &jim_jtag_arp_init_reset,
804 .mode = COMMAND_CONFIG,
805 .jim_handler = &jim_jtag_newtap,
806 .help = "Create a new TAP instance",
807 .usage = "<name> <type> -irlen <count> [-ircapture <count>] "
808 "[-irmask <count>] [-enable|-disable]",
811 .name = "tapisenabled",
812 .mode = COMMAND_EXEC,
813 .jim_handler = &jim_jtag_tap_enabler,
814 .help = "Returns a integer indicating TAP state (0/1)",
819 .mode = COMMAND_EXEC,
820 .jim_handler = &jim_jtag_tap_enabler,
821 .help = "Enable the specified TAP",
825 .name = "tapdisable",
826 .mode = COMMAND_EXEC,
827 .jim_handler = &jim_jtag_tap_enabler,
828 .help = "Enable the specified TAP",
833 .mode = COMMAND_EXEC,
834 .jim_handler = &jim_jtag_configure,
835 .help = "Enable the specified TAP",
836 .usage = "<name> [<key> <value> ...]",
840 .mode = COMMAND_EXEC,
841 .jim_handler = &jim_jtag_configure,
842 .help = "Enable the specified TAP",
843 .usage = "<name> [<key> <value> ...]",
848 .jim_handler = &jim_jtag_names,
849 .help = "Returns list of all JTAG tap names",
852 .chain = jtag_command_handlers_to_move,
854 COMMAND_REGISTRATION_DONE
857 void jtag_notify_event(enum jtag_event event)
859 struct jtag_tap *tap;
861 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
862 jtag_tap_handle_event(tap, event);
866 static int default_khz(int khz, int *jtag_speed)
868 LOG_ERROR("Translation from khz to jtag_speed not implemented");
872 static int default_speed_div(int speed, int *khz)
874 LOG_ERROR("Translation from jtag_speed to khz not implemented");
878 static int default_power_dropout(int *dropout)
880 *dropout = 0; /* by default we can't detect power dropout */
884 static int default_srst_asserted(int *srst_asserted)
886 *srst_asserted = 0; /* by default we can't detect srst asserted */
890 COMMAND_HANDLER(handle_interface_list_command)
892 if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
893 return ERROR_COMMAND_SYNTAX_ERROR;
895 command_print(CMD_CTX, "The following JTAG interfaces are available:");
896 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
898 const char *name = jtag_interfaces[i]->name;
899 command_print(CMD_CTX, "%u: %s", i + 1, name);
905 COMMAND_HANDLER(handle_interface_command)
907 /* check whether the interface is already configured */
910 LOG_WARNING("Interface already configured, ignoring");
914 /* interface name is a mandatory argument */
915 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
916 return ERROR_COMMAND_SYNTAX_ERROR;
918 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
920 if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0)
923 if (NULL != jtag_interfaces[i]->commands)
925 int retval = register_commands(CMD_CTX, NULL,
926 jtag_interfaces[i]->commands);
927 if (ERROR_OK != retval)
931 jtag_interface = jtag_interfaces[i];
933 if (jtag_interface->khz == NULL)
934 jtag_interface->khz = default_khz;
935 if (jtag_interface->speed_div == NULL)
936 jtag_interface->speed_div = default_speed_div;
937 if (jtag_interface->power_dropout == NULL)
938 jtag_interface->power_dropout = default_power_dropout;
939 if (jtag_interface->srst_asserted == NULL)
940 jtag_interface->srst_asserted = default_srst_asserted;
945 /* no valid interface was found (i.e. the configuration option,
946 * didn't match one of the compiled-in interfaces
948 LOG_ERROR("The specified JTAG interface was not found (%s)", CMD_ARGV[0]);
949 CALL_COMMAND_HANDLER(handle_interface_list_command);
950 return ERROR_JTAG_INVALID_INTERFACE;
953 COMMAND_HANDLER(handle_scan_chain_command)
955 struct jtag_tap *tap;
957 tap = jtag_all_taps();
958 command_print(CMD_CTX, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
959 command_print(CMD_CTX, "---|--------------------|---------|------------|------------|------|------|------|---------");
962 uint32_t expected, expected_mask, cur_instr, ii;
963 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
964 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
965 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
967 command_print(CMD_CTX,
968 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
969 tap->abs_chain_position,
971 tap->enabled ? 'Y' : 'n',
972 (unsigned int)(tap->idcode),
973 (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
974 (unsigned int)(tap->ir_length),
975 (unsigned int)(expected),
976 (unsigned int)(expected_mask),
977 (unsigned int)(cur_instr));
979 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
980 command_print(CMD_CTX, " | | | | 0x%08x | | | | ",
981 (unsigned int)(tap->expected_ids[ii]));
990 COMMAND_HANDLER(handle_reset_config_command)
995 /* Original versions cared about the order of these tokens:
996 * reset_config signals [combination [trst_type [srst_type]]]
997 * They also clobbered the previous configuration even on error.
999 * Here we don't care about the order, and only change values
1000 * which have been explicitly specified.
1002 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
1007 m = RESET_SRST_NO_GATING;
1008 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
1009 /* default: don't use JTAG while SRST asserted */;
1010 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
1011 tmp = RESET_SRST_NO_GATING;
1015 LOG_ERROR("extra reset_config %s spec (%s)",
1016 "gating", *CMD_ARGV);
1017 return ERROR_INVALID_ARGUMENTS;
1023 m = RESET_HAS_TRST | RESET_HAS_SRST;
1024 if (strcmp(*CMD_ARGV, "none") == 0)
1026 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
1027 tmp = RESET_HAS_TRST;
1028 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
1029 tmp = RESET_HAS_SRST;
1030 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
1031 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
1035 LOG_ERROR("extra reset_config %s spec (%s)",
1036 "signal", *CMD_ARGV);
1037 return ERROR_INVALID_ARGUMENTS;
1042 /* combination (options for broken wiring) */
1043 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1044 if (strcmp(*CMD_ARGV, "separate") == 0)
1045 /* separate reset lines - default */;
1046 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
1047 tmp |= RESET_SRST_PULLS_TRST;
1048 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
1049 tmp |= RESET_TRST_PULLS_SRST;
1050 else if (strcmp(*CMD_ARGV, "combined") == 0)
1051 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1055 LOG_ERROR("extra reset_config %s spec (%s)",
1056 "combination", *CMD_ARGV);
1057 return ERROR_INVALID_ARGUMENTS;
1062 /* trst_type (NOP without HAS_TRST) */
1063 m = RESET_TRST_OPEN_DRAIN;
1064 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
1065 tmp |= RESET_TRST_OPEN_DRAIN;
1066 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
1067 /* push/pull from adapter - default */;
1071 LOG_ERROR("extra reset_config %s spec (%s)",
1072 "trst_type", *CMD_ARGV);
1073 return ERROR_INVALID_ARGUMENTS;
1078 /* srst_type (NOP without HAS_SRST) */
1079 m |= RESET_SRST_PUSH_PULL;
1080 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
1081 tmp |= RESET_SRST_PUSH_PULL;
1082 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
1083 /* open drain from adapter - default */;
1087 LOG_ERROR("extra reset_config %s spec (%s)",
1088 "srst_type", *CMD_ARGV);
1089 return ERROR_INVALID_ARGUMENTS;
1094 /* caller provided nonsense; fail */
1095 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
1096 return ERROR_INVALID_ARGUMENTS;
1099 /* Remember the bits which were specified (mask)
1100 * and their new values (new_cfg).
1106 /* clear previous values of those bits, save new values */
1108 int old_cfg = jtag_get_reset_config();
1112 jtag_set_reset_config(new_cfg);
1114 new_cfg = jtag_get_reset_config();
1118 * Display the (now-)current reset mode
1122 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
1123 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
1124 case RESET_HAS_SRST:
1125 modes[0] = "srst_only";
1127 case RESET_HAS_TRST:
1128 modes[0] = "trst_only";
1130 case RESET_TRST_AND_SRST:
1131 modes[0] = "trst_and_srst";
1138 /* normally SRST and TRST are decoupled; but bugs happen ... */
1139 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
1140 case RESET_SRST_PULLS_TRST:
1141 modes[1] = "srst_pulls_trst";
1143 case RESET_TRST_PULLS_SRST:
1144 modes[1] = "trst_pulls_srst";
1146 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
1147 modes[1] = "combined";
1150 modes[1] = "separate";
1154 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
1155 if (new_cfg & RESET_HAS_TRST) {
1156 if (new_cfg & RESET_TRST_OPEN_DRAIN)
1157 modes[3] = " trst_open_drain";
1159 modes[3] = " trst_push_pull";
1163 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
1164 if (new_cfg & RESET_HAS_SRST) {
1165 if (new_cfg & RESET_SRST_NO_GATING)
1166 modes[2] = " srst_nogate";
1168 modes[2] = " srst_gates_jtag";
1170 if (new_cfg & RESET_SRST_PUSH_PULL)
1171 modes[4] = " srst_push_pull";
1173 modes[4] = " srst_open_drain";
1179 command_print(CMD_CTX, "%s %s%s%s%s",
1181 modes[2], modes[3], modes[4]);
1186 COMMAND_HANDLER(handle_jtag_nsrst_delay_command)
1189 return ERROR_COMMAND_SYNTAX_ERROR;
1193 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1195 jtag_set_nsrst_delay(delay);
1197 command_print(CMD_CTX, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
1201 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1204 return ERROR_COMMAND_SYNTAX_ERROR;
1208 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1210 jtag_set_ntrst_delay(delay);
1212 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1216 COMMAND_HANDLER(handle_jtag_nsrst_assert_width_command)
1219 return ERROR_COMMAND_SYNTAX_ERROR;
1223 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1225 jtag_set_nsrst_assert_width(delay);
1227 command_print(CMD_CTX, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
1231 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1234 return ERROR_COMMAND_SYNTAX_ERROR;
1238 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1240 jtag_set_ntrst_assert_width(delay);
1242 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1246 COMMAND_HANDLER(handle_jtag_khz_command)
1249 return ERROR_COMMAND_SYNTAX_ERROR;
1251 int retval = ERROR_OK;
1255 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1257 retval = jtag_config_khz(khz);
1258 if (ERROR_OK != retval)
1262 int cur_speed = jtag_get_speed_khz();
1263 retval = jtag_get_speed_readable(&cur_speed);
1264 if (ERROR_OK != retval)
1268 command_print(CMD_CTX, "%d kHz", cur_speed);
1270 command_print(CMD_CTX, "RCLK - adaptive");
1275 COMMAND_HANDLER(handle_jtag_rclk_command)
1278 return ERROR_COMMAND_SYNTAX_ERROR;
1280 int retval = ERROR_OK;
1284 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1286 retval = jtag_config_rclk(khz);
1287 if (ERROR_OK != retval)
1291 int cur_khz = jtag_get_speed_khz();
1292 retval = jtag_get_speed_readable(&cur_khz);
1293 if (ERROR_OK != retval)
1297 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1299 command_print(CMD_CTX, "RCLK - adaptive");
1304 COMMAND_HANDLER(handle_jtag_reset_command)
1307 return ERROR_COMMAND_SYNTAX_ERROR;
1310 if (CMD_ARGV[0][0] == '1')
1312 else if (CMD_ARGV[0][0] == '0')
1315 return ERROR_COMMAND_SYNTAX_ERROR;
1318 if (CMD_ARGV[1][0] == '1')
1320 else if (CMD_ARGV[1][0] == '0')
1323 return ERROR_COMMAND_SYNTAX_ERROR;
1325 if (jtag_interface_init(CMD_CTX) != ERROR_OK)
1326 return ERROR_JTAG_INIT_FAILED;
1328 jtag_add_reset(trst, srst);
1329 return jtag_execute_queue();
1332 COMMAND_HANDLER(handle_runtest_command)
1335 return ERROR_COMMAND_SYNTAX_ERROR;
1337 unsigned num_clocks;
1338 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1340 jtag_add_runtest(num_clocks, TAP_IDLE);
1341 return jtag_execute_queue();
1345 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1346 * should be stable ... and *NOT* a shift state, otherwise free-running
1347 * jtag clocks could change the values latched by the update state.
1348 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1349 * and "drscan" commands are a write-only subset of what SVF provides.
1352 COMMAND_HANDLER(handle_irscan_command)
1355 struct scan_field *fields;
1356 struct jtag_tap *tap;
1357 tap_state_t endstate;
1359 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1361 return ERROR_COMMAND_SYNTAX_ERROR;
1364 /* optional "-endstate" "statename" at the end of the arguments,
1365 * so that e.g. IRPAUSE can let us load the data register before
1366 * entering RUN/IDLE to execute the instruction we load here.
1368 endstate = TAP_IDLE;
1370 if (CMD_ARGC >= 4) {
1371 /* have at least one pair of numbers. */
1372 /* is last pair the magic text? */
1373 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1374 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1375 if (endstate == TAP_INVALID)
1376 return ERROR_COMMAND_SYNTAX_ERROR;
1377 if (!scan_is_safe(endstate))
1378 LOG_WARNING("unstable irscan endstate \"%s\"",
1379 CMD_ARGV[CMD_ARGC - 1]);
1384 int num_fields = CMD_ARGC / 2;
1385 size_t fields_len = sizeof(struct scan_field) * num_fields;
1386 fields = malloc(fields_len);
1387 memset(fields, 0, fields_len);
1390 for (i = 0; i < num_fields; i++)
1392 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1396 for (j = 0; j < i; j++)
1397 free(fields[j].out_value);
1399 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1403 int field_size = tap->ir_length;
1404 fields[i].tap = tap;
1405 fields[i].num_bits = field_size;
1406 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1409 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1410 if (ERROR_OK != retval)
1412 buf_set_u32(fields[i].out_value, 0, field_size, value);
1413 fields[i].in_value = NULL;
1416 /* did we have an endstate? */
1417 jtag_add_ir_scan(num_fields, fields, endstate);
1419 retval = jtag_execute_queue();
1422 for (i = 0; i < num_fields; i++)
1424 if (NULL != fields[i].out_value)
1425 free(fields[i].out_value);
1434 COMMAND_HANDLER(handle_verify_ircapture_command)
1437 return ERROR_COMMAND_SYNTAX_ERROR;
1442 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1443 jtag_set_verify_capture_ir(enable);
1446 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1447 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1452 COMMAND_HANDLER(handle_verify_jtag_command)
1455 return ERROR_COMMAND_SYNTAX_ERROR;
1460 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1461 jtag_set_verify(enable);
1464 const char *status = jtag_will_verify() ? "enabled": "disabled";
1465 command_print(CMD_CTX, "verify jtag capture is %s", status);
1470 COMMAND_HANDLER(handle_tms_sequence_command)
1473 return ERROR_COMMAND_SYNTAX_ERROR;
1478 if (strcmp(CMD_ARGV[0], "short") == 0)
1479 use_new_table = true;
1480 else if (strcmp(CMD_ARGV[0], "long") == 0)
1481 use_new_table = false;
1483 return ERROR_COMMAND_SYNTAX_ERROR;
1485 tap_use_new_tms_table(use_new_table);
1488 command_print(CMD_CTX, "tms sequence is %s",
1489 tap_uses_new_tms_table() ? "short": "long");
1494 static const struct command_registration jtag_command_handlers[] = {
1496 .name = "interface",
1497 .handler = &handle_interface_command,
1498 .mode = COMMAND_CONFIG,
1499 .help = "select a JTAG interface",
1500 .usage = "<driver_name>",
1503 .name = "interface_list",
1504 .handler = &handle_interface_list_command,
1505 .mode = COMMAND_ANY,
1506 .help = "list all built-in interfaces",
1510 .handler = &handle_jtag_khz_command,
1511 .mode = COMMAND_ANY,
1512 .help = "set maximum jtag speed (if supported)",
1513 .usage = "<khz:0=rtck>",
1516 .name = "jtag_rclk",
1517 .handler = &handle_jtag_rclk_command,
1518 .mode = COMMAND_ANY,
1519 .help = "set JTAG speed to RCLK or use fallback speed",
1520 .usage = "<fallback_speed_khz>",
1523 .name = "reset_config",
1524 .handler = &handle_reset_config_command,
1525 .mode = COMMAND_ANY,
1526 .help = "configure JTAG reset behavior",
1527 .usage = "[none|trst_only|srst_only|trst_and_srst] "
1528 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
1529 "[srst_gates_jtag|srst_nogate] "
1530 "[trst_push_pull|trst_open_drain] "
1531 "[srst_push_pull|srst_open_drain]",
1534 .name = "jtag_nsrst_delay",
1535 .handler = &handle_jtag_nsrst_delay_command,
1536 .mode = COMMAND_ANY,
1537 .help = "delay after deasserting srst in ms",
1541 .name = "jtag_ntrst_delay",
1542 .handler = &handle_jtag_ntrst_delay_command,
1543 .mode = COMMAND_ANY,
1544 .help = "delay after deasserting trst in ms",
1548 .name = "jtag_nsrst_assert_width",
1549 .handler = &handle_jtag_nsrst_assert_width_command,
1550 .mode = COMMAND_ANY,
1551 .help = "delay after asserting srst in ms",
1555 .name = "jtag_ntrst_assert_width",
1556 .handler = &handle_jtag_ntrst_assert_width_command,
1557 .mode = COMMAND_ANY,
1558 .help = "delay after asserting trst in ms",
1562 .name = "scan_chain",
1563 .handler = &handle_scan_chain_command,
1564 .mode = COMMAND_EXEC,
1565 .help = "print current scan chain configuration",
1568 .name = "jtag_reset",
1569 .handler = &handle_jtag_reset_command,
1570 .mode = COMMAND_EXEC,
1571 .help = "toggle reset lines",
1572 .usage = "<trst> <srst>",
1576 .handler = &handle_runtest_command,
1577 .mode = COMMAND_EXEC,
1578 .help = "move to Run-Test/Idle, and execute <num_cycles>",
1579 .usage = "<num_cycles>"
1583 .handler = &handle_irscan_command,
1584 .mode = COMMAND_EXEC,
1585 .help = "execute IR scan",
1586 .usage = "<device> <instr> [dev2] [instr2] ...",
1589 .name = "verify_ircapture",
1590 .handler = &handle_verify_ircapture_command,
1591 .mode = COMMAND_ANY,
1592 .help = "verify value captured during Capture-IR",
1593 .usage = "<enable | disable>",
1596 .name = "verify_jtag",
1597 .handler = &handle_verify_jtag_command,
1598 .mode = COMMAND_ANY,
1599 .help = "verify value capture",
1600 .usage = "<enable | disable>",
1603 .name = "tms_sequence",
1604 .handler = &handle_tms_sequence_command,
1605 .mode = COMMAND_ANY,
1606 .help = "choose short(default) or long tms_sequence",
1607 .usage = "<short | long>",
1611 .mode = COMMAND_ANY,
1612 .help = "perform jtag tap actions",
1614 .chain = jtag_subcommand_handlers,
1617 .chain = jtag_command_handlers_to_move,
1619 COMMAND_REGISTRATION_DONE
1621 int jtag_register_commands(struct command_context *cmd_ctx)
1623 return register_commands(cmd_ctx, NULL, jtag_command_handlers);