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 enum jtag_tap_cfg_param {
58 static Jim_Nvp nvp_config_opts[] = {
59 { .name = "-event", .value = JCFG_EVENT },
61 { .name = NULL, .value = -1 }
64 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
70 /* parse config or cget options */
71 while (goi->argc > 0) {
72 Jim_SetEmptyResult (goi->interp);
74 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
76 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
83 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
87 e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
89 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
93 if (goi->isconfigure) {
95 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
100 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
106 struct jtag_tap_event_action *jteap;
108 jteap = tap->event_action;
109 /* replace existing? */
111 if (jteap->event == (enum jtag_event)n->value) {
117 if (goi->isconfigure) {
121 jteap = calloc(1, sizeof (*jteap));
124 jteap->event = n->value;
125 Jim_GetOpt_Obj(goi, &o);
127 Jim_DecrRefCount(interp, jteap->body);
129 jteap->body = Jim_DuplicateObj(goi->interp, o);
130 Jim_IncrRefCount(jteap->body);
134 /* add to head of event list */
135 jteap->next = tap->event_action;
136 tap->event_action = jteap;
138 Jim_SetEmptyResult(goi->interp);
142 Jim_SetEmptyResult(goi->interp);
144 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
151 } /* while (goi->argc) */
156 static int is_bad_irval(int ir_length, jim_wide w)
166 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
168 struct jtag_tap *pTap;
174 const Jim_Nvp opts[] = {
175 #define NTAP_OPT_IRLEN 0
176 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
177 #define NTAP_OPT_IRMASK 1
178 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
179 #define NTAP_OPT_IRCAPTURE 2
180 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
181 #define NTAP_OPT_ENABLED 3
182 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
183 #define NTAP_OPT_DISABLED 4
184 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
185 #define NTAP_OPT_EXPECTED_ID 5
186 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
187 { .name = NULL , .value = -1 },
190 pTap = calloc(1, sizeof(struct jtag_tap));
192 Jim_SetResult_sprintf(goi->interp, "no memory");
197 * we expect CHIP + TAP + OPTIONS
200 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
204 Jim_GetOpt_String(goi, &cp, NULL);
205 pTap->chip = strdup(cp);
207 Jim_GetOpt_String(goi, &cp, NULL);
208 pTap->tapname = strdup(cp);
210 /* name + dot + name + null */
211 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
213 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
214 pTap->dotted_name = cp;
216 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
217 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
219 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
220 * that the default. The "-irlen" and "-irmask" options are only
221 * needed to cope with nonstandard TAPs, or to specify more bits.
223 pTap->ir_capture_mask = 0x03;
224 pTap->ir_capture_value = 0x01;
227 e = Jim_GetOpt_Nvp(goi, opts, &n);
229 Jim_GetOpt_NvpUnknown(goi, opts, 0);
230 free((void *)pTap->dotted_name);
234 LOG_DEBUG("Processing option: %s", n->name);
236 case NTAP_OPT_ENABLED:
237 pTap->disabled_after_reset = false;
239 case NTAP_OPT_DISABLED:
240 pTap->disabled_after_reset = true;
242 case NTAP_OPT_EXPECTED_ID:
244 uint32_t *new_expected_ids;
246 e = Jim_GetOpt_Wide(goi, &w);
248 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
249 free((void *)pTap->dotted_name);
254 new_expected_ids = malloc(sizeof(uint32_t) * (pTap->expected_ids_cnt + 1));
255 if (new_expected_ids == NULL) {
256 Jim_SetResult_sprintf(goi->interp, "no memory");
257 free((void *)pTap->dotted_name);
262 memcpy(new_expected_ids, pTap->expected_ids, sizeof(uint32_t) * pTap->expected_ids_cnt);
264 new_expected_ids[pTap->expected_ids_cnt] = w;
266 free(pTap->expected_ids);
267 pTap->expected_ids = new_expected_ids;
268 pTap->expected_ids_cnt++;
272 case NTAP_OPT_IRMASK:
273 case NTAP_OPT_IRCAPTURE:
274 e = Jim_GetOpt_Wide(goi, &w);
276 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
277 free((void *)pTap->dotted_name);
283 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
284 LOG_WARNING("%s: huge IR length %d",
289 case NTAP_OPT_IRMASK:
290 if (is_bad_irval(pTap->ir_length, w)) {
291 LOG_ERROR("%s: IR mask %x too big",
294 free((void *)pTap->dotted_name);
299 LOG_WARNING("%s: nonstandard IR mask",
301 pTap->ir_capture_mask = w;
303 case NTAP_OPT_IRCAPTURE:
304 if (is_bad_irval(pTap->ir_length, w)) {
305 LOG_ERROR("%s: IR capture %x too big",
308 free((void *)pTap->dotted_name);
313 LOG_WARNING("%s: nonstandard IR value",
315 pTap->ir_capture_value = w;
318 } /* switch (n->value) */
319 } /* while (goi->argc) */
321 /* default is enabled-after-reset */
322 pTap->enabled = !pTap->disabled_after_reset;
324 /* Did all the required option bits get cleared? */
325 if (pTap->ir_length != 0)
331 Jim_SetResult_sprintf(goi->interp,
332 "newtap: %s missing IR length",
338 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
340 struct jtag_tap_event_action * jteap;
342 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next) {
343 if (jteap->event == e) {
344 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
347 Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
348 Jim_GetString(jteap->body, NULL));
349 if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
350 Jim_PrintErrorMessage(interp);
352 case JTAG_TAP_EVENT_ENABLE:
353 case JTAG_TAP_EVENT_DISABLE:
354 /* NOTE: we currently assume the handlers
355 * can't fail. Right here is where we should
356 * really be verifying the scan chains ...
358 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
359 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
360 tap->enabled ? "enabled" : "disabled");
369 static int jim_jtag_interface(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
372 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
374 /* return the name of the interface */
375 /* TCL code might need to know the exact type... */
376 /* FUTURE: we allow this as a means to "set" the interface. */
378 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
381 const char *name = jtag_interface ? jtag_interface->name : NULL;
382 Jim_SetResultString(goi.interp, name ? : "undefined", -1);
386 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
389 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
391 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
394 struct command_context *context = Jim_GetAssocData(interp, "context");
395 int e = jtag_init_inner(context);
397 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
403 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
406 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
408 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
411 struct command_context *context = Jim_GetAssocData(interp, "context");
412 int e = jtag_init_reset(context);
414 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
420 static int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
423 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
424 return jim_newtap_cmd(&goi);
427 static bool jtag_tap_enable(struct jtag_tap *t)
431 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
435 /* FIXME add JTAG sanity checks, w/o TLR
436 * - scan chain length grew by one (this)
437 * - IDs and IR lengths are as expected
439 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
442 static bool jtag_tap_disable(struct jtag_tap *t)
446 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
450 /* FIXME add JTAG sanity checks, w/o TLR
451 * - scan chain length shrank by one (this)
452 * - IDs and IR lengths are as expected
454 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
458 static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
460 const char *cmd_name = Jim_GetString(argv[0], NULL);
462 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
464 Jim_SetResult_sprintf(goi.interp, "usage: %s <name>", cmd_name);
470 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
474 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
475 // do nothing, just return the value
476 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
477 if (!jtag_tap_enable(t))
478 LOG_WARNING("failed to disable tap");
479 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
480 if (!jtag_tap_disable(t))
481 LOG_WARNING("failed to disable tap");
483 LOG_ERROR("command '%s' unknown", cmd_name);
487 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
491 static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
493 const char *cmd_name = Jim_GetString(argv[0], NULL);
495 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
496 goi.isconfigure = !strcmp(cmd_name, "configure");
497 if (goi.argc < 2 + goi.isconfigure) {
498 Jim_WrongNumArgs(goi.interp, 0, NULL,
499 "<tap_name> <attribute> ...");
506 Jim_GetOpt_Obj(&goi, &o);
507 t = jtag_tap_by_jim_obj(goi.interp, o);
512 return jtag_tap_configure_cmd(&goi, t);
515 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
518 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
520 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
523 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
524 struct jtag_tap *tap;
526 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
527 Jim_ListAppendElement(goi.interp,
528 Jim_GetResult(goi.interp),
529 Jim_NewStringObj(goi.interp,
530 tap->dotted_name, -1));
535 static const struct command_registration jtag_subcommand_handlers[] = {
539 .jim_handler = &jim_jtag_interface,
540 .help = "Returns the selected interface",
545 .jim_handler = &jim_jtag_arp_init,
548 .name = "arp_init-reset",
550 .jim_handler = &jim_jtag_arp_init_reset,
554 .mode = COMMAND_CONFIG,
555 .jim_handler = &jim_jtag_newtap,
556 .help = "Create a new TAP instance",
557 .usage = "<name> <type> -irlen <count> [-ircapture <count>] "
558 "[-irmask <count>] [-enable|-disable]",
561 .name = "tapisenabled",
562 .mode = COMMAND_EXEC,
563 .jim_handler = &jim_jtag_tap_enabler,
564 .help = "Returns a integer indicating TAP state (0/1)",
569 .mode = COMMAND_EXEC,
570 .jim_handler = &jim_jtag_tap_enabler,
571 .help = "Enable the specified TAP",
575 .name = "tapdisable",
576 .mode = COMMAND_EXEC,
577 .jim_handler = &jim_jtag_tap_enabler,
578 .help = "Enable the specified TAP",
583 .mode = COMMAND_EXEC,
584 .jim_handler = &jim_jtag_configure,
585 .help = "Enable the specified TAP",
586 .usage = "<name> [<key> <value> ...]",
590 .mode = COMMAND_EXEC,
591 .jim_handler = &jim_jtag_configure,
592 .help = "Enable the specified TAP",
593 .usage = "<name> [<key> <value> ...]",
598 .jim_handler = &jim_jtag_names,
599 .help = "Returns list of all JTAG tap names",
601 COMMAND_REGISTRATION_DONE
604 void jtag_notify_event(enum jtag_event event)
606 struct jtag_tap *tap;
608 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
609 jtag_tap_handle_event(tap, event);
613 static int default_khz(int khz, int *jtag_speed)
615 LOG_ERROR("Translation from khz to jtag_speed not implemented");
619 static int default_speed_div(int speed, int *khz)
621 LOG_ERROR("Translation from jtag_speed to khz not implemented");
625 static int default_power_dropout(int *dropout)
627 *dropout = 0; /* by default we can't detect power dropout */
631 static int default_srst_asserted(int *srst_asserted)
633 *srst_asserted = 0; /* by default we can't detect srst asserted */
637 COMMAND_HANDLER(handle_interface_list_command)
639 if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
640 return ERROR_COMMAND_SYNTAX_ERROR;
642 command_print(CMD_CTX, "The following JTAG interfaces are available:");
643 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
645 const char *name = jtag_interfaces[i]->name;
646 command_print(CMD_CTX, "%u: %s", i + 1, name);
652 COMMAND_HANDLER(handle_interface_command)
654 /* check whether the interface is already configured */
657 LOG_WARNING("Interface already configured, ignoring");
661 /* interface name is a mandatory argument */
662 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
663 return ERROR_COMMAND_SYNTAX_ERROR;
665 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
667 if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0)
670 if (NULL != jtag_interfaces[i]->commands)
672 int retval = register_commands(CMD_CTX, NULL,
673 jtag_interfaces[i]->commands);
674 if (ERROR_OK != retval)
678 jtag_interface = jtag_interfaces[i];
680 if (jtag_interface->khz == NULL)
681 jtag_interface->khz = default_khz;
682 if (jtag_interface->speed_div == NULL)
683 jtag_interface->speed_div = default_speed_div;
684 if (jtag_interface->power_dropout == NULL)
685 jtag_interface->power_dropout = default_power_dropout;
686 if (jtag_interface->srst_asserted == NULL)
687 jtag_interface->srst_asserted = default_srst_asserted;
692 /* no valid interface was found (i.e. the configuration option,
693 * didn't match one of the compiled-in interfaces
695 LOG_ERROR("The specified JTAG interface was not found (%s)", CMD_ARGV[0]);
696 CALL_COMMAND_HANDLER(handle_interface_list_command);
697 return ERROR_JTAG_INVALID_INTERFACE;
700 COMMAND_HANDLER(handle_scan_chain_command)
702 struct jtag_tap *tap;
704 tap = jtag_all_taps();
705 command_print(CMD_CTX, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
706 command_print(CMD_CTX, "---|--------------------|---------|------------|------------|------|------|------|---------");
709 uint32_t expected, expected_mask, cur_instr, ii;
710 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
711 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
712 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
714 command_print(CMD_CTX,
715 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
716 tap->abs_chain_position,
718 tap->enabled ? 'Y' : 'n',
719 (unsigned int)(tap->idcode),
720 (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
721 (unsigned int)(tap->ir_length),
722 (unsigned int)(expected),
723 (unsigned int)(expected_mask),
724 (unsigned int)(cur_instr));
726 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
727 command_print(CMD_CTX, " | | | | 0x%08x | | | | ",
728 (unsigned int)(tap->expected_ids[ii]));
737 COMMAND_HANDLER(handle_reset_config_command)
742 /* Original versions cared about the order of these tokens:
743 * reset_config signals [combination [trst_type [srst_type]]]
744 * They also clobbered the previous configuration even on error.
746 * Here we don't care about the order, and only change values
747 * which have been explicitly specified.
749 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
754 m = RESET_SRST_NO_GATING;
755 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
756 /* default: don't use JTAG while SRST asserted */;
757 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
758 tmp = RESET_SRST_NO_GATING;
762 LOG_ERROR("extra reset_config %s spec (%s)",
763 "gating", *CMD_ARGV);
764 return ERROR_INVALID_ARGUMENTS;
770 m = RESET_HAS_TRST | RESET_HAS_SRST;
771 if (strcmp(*CMD_ARGV, "none") == 0)
773 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
774 tmp = RESET_HAS_TRST;
775 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
776 tmp = RESET_HAS_SRST;
777 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
778 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
782 LOG_ERROR("extra reset_config %s spec (%s)",
783 "signal", *CMD_ARGV);
784 return ERROR_INVALID_ARGUMENTS;
789 /* combination (options for broken wiring) */
790 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
791 if (strcmp(*CMD_ARGV, "separate") == 0)
792 /* separate reset lines - default */;
793 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
794 tmp |= RESET_SRST_PULLS_TRST;
795 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
796 tmp |= RESET_TRST_PULLS_SRST;
797 else if (strcmp(*CMD_ARGV, "combined") == 0)
798 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
802 LOG_ERROR("extra reset_config %s spec (%s)",
803 "combination", *CMD_ARGV);
804 return ERROR_INVALID_ARGUMENTS;
809 /* trst_type (NOP without HAS_TRST) */
810 m = RESET_TRST_OPEN_DRAIN;
811 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
812 tmp |= RESET_TRST_OPEN_DRAIN;
813 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
814 /* push/pull from adapter - default */;
818 LOG_ERROR("extra reset_config %s spec (%s)",
819 "trst_type", *CMD_ARGV);
820 return ERROR_INVALID_ARGUMENTS;
825 /* srst_type (NOP without HAS_SRST) */
826 m |= RESET_SRST_PUSH_PULL;
827 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
828 tmp |= RESET_SRST_PUSH_PULL;
829 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
830 /* open drain from adapter - default */;
834 LOG_ERROR("extra reset_config %s spec (%s)",
835 "srst_type", *CMD_ARGV);
836 return ERROR_INVALID_ARGUMENTS;
841 /* caller provided nonsense; fail */
842 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
843 return ERROR_INVALID_ARGUMENTS;
846 /* Remember the bits which were specified (mask)
847 * and their new values (new_cfg).
853 /* clear previous values of those bits, save new values */
855 int old_cfg = jtag_get_reset_config();
859 jtag_set_reset_config(new_cfg);
861 new_cfg = jtag_get_reset_config();
865 * Display the (now-)current reset mode
869 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
870 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
872 modes[0] = "srst_only";
875 modes[0] = "trst_only";
877 case RESET_TRST_AND_SRST:
878 modes[0] = "trst_and_srst";
885 /* normally SRST and TRST are decoupled; but bugs happen ... */
886 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
887 case RESET_SRST_PULLS_TRST:
888 modes[1] = "srst_pulls_trst";
890 case RESET_TRST_PULLS_SRST:
891 modes[1] = "trst_pulls_srst";
893 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
894 modes[1] = "combined";
897 modes[1] = "separate";
901 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
902 if (new_cfg & RESET_HAS_TRST) {
903 if (new_cfg & RESET_TRST_OPEN_DRAIN)
904 modes[3] = " trst_open_drain";
906 modes[3] = " trst_push_pull";
910 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
911 if (new_cfg & RESET_HAS_SRST) {
912 if (new_cfg & RESET_SRST_NO_GATING)
913 modes[2] = " srst_nogate";
915 modes[2] = " srst_gates_jtag";
917 if (new_cfg & RESET_SRST_PUSH_PULL)
918 modes[4] = " srst_push_pull";
920 modes[4] = " srst_open_drain";
926 command_print(CMD_CTX, "%s %s%s%s%s",
928 modes[2], modes[3], modes[4]);
933 COMMAND_HANDLER(handle_jtag_nsrst_delay_command)
936 return ERROR_COMMAND_SYNTAX_ERROR;
940 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
942 jtag_set_nsrst_delay(delay);
944 command_print(CMD_CTX, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
948 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
951 return ERROR_COMMAND_SYNTAX_ERROR;
955 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
957 jtag_set_ntrst_delay(delay);
959 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
963 COMMAND_HANDLER(handle_jtag_nsrst_assert_width_command)
966 return ERROR_COMMAND_SYNTAX_ERROR;
970 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
972 jtag_set_nsrst_assert_width(delay);
974 command_print(CMD_CTX, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
978 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
981 return ERROR_COMMAND_SYNTAX_ERROR;
985 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
987 jtag_set_ntrst_assert_width(delay);
989 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
993 COMMAND_HANDLER(handle_jtag_khz_command)
996 return ERROR_COMMAND_SYNTAX_ERROR;
998 int retval = ERROR_OK;
1002 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1004 retval = jtag_config_khz(khz);
1005 if (ERROR_OK != retval)
1009 int cur_speed = jtag_get_speed_khz();
1010 retval = jtag_get_speed_readable(&cur_speed);
1011 if (ERROR_OK != retval)
1015 command_print(CMD_CTX, "%d kHz", cur_speed);
1017 command_print(CMD_CTX, "RCLK - adaptive");
1022 COMMAND_HANDLER(handle_jtag_rclk_command)
1025 return ERROR_COMMAND_SYNTAX_ERROR;
1027 int retval = ERROR_OK;
1031 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1033 retval = jtag_config_rclk(khz);
1034 if (ERROR_OK != retval)
1038 int cur_khz = jtag_get_speed_khz();
1039 retval = jtag_get_speed_readable(&cur_khz);
1040 if (ERROR_OK != retval)
1044 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1046 command_print(CMD_CTX, "RCLK - adaptive");
1051 COMMAND_HANDLER(handle_jtag_reset_command)
1054 return ERROR_COMMAND_SYNTAX_ERROR;
1057 if (CMD_ARGV[0][0] == '1')
1059 else if (CMD_ARGV[0][0] == '0')
1062 return ERROR_COMMAND_SYNTAX_ERROR;
1065 if (CMD_ARGV[1][0] == '1')
1067 else if (CMD_ARGV[1][0] == '0')
1070 return ERROR_COMMAND_SYNTAX_ERROR;
1072 if (jtag_interface_init(CMD_CTX) != ERROR_OK)
1073 return ERROR_JTAG_INIT_FAILED;
1075 jtag_add_reset(trst, srst);
1076 return jtag_execute_queue();
1079 COMMAND_HANDLER(handle_runtest_command)
1082 return ERROR_COMMAND_SYNTAX_ERROR;
1084 unsigned num_clocks;
1085 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1087 jtag_add_runtest(num_clocks, TAP_IDLE);
1088 return jtag_execute_queue();
1092 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1093 * should be stable ... and *NOT* a shift state, otherwise free-running
1094 * jtag clocks could change the values latched by the update state.
1095 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1096 * and "drscan" commands are a write-only subset of what SVF provides.
1098 static bool scan_is_safe(tap_state_t state)
1113 COMMAND_HANDLER(handle_irscan_command)
1116 struct scan_field *fields;
1117 struct jtag_tap *tap;
1118 tap_state_t endstate;
1120 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1122 return ERROR_COMMAND_SYNTAX_ERROR;
1125 /* optional "-endstate" "statename" at the end of the arguments,
1126 * so that e.g. IRPAUSE can let us load the data register before
1127 * entering RUN/IDLE to execute the instruction we load here.
1129 endstate = TAP_IDLE;
1131 if (CMD_ARGC >= 4) {
1132 /* have at least one pair of numbers. */
1133 /* is last pair the magic text? */
1134 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1135 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1136 if (endstate == TAP_INVALID)
1137 return ERROR_COMMAND_SYNTAX_ERROR;
1138 if (!scan_is_safe(endstate))
1139 LOG_WARNING("unstable irscan endstate \"%s\"",
1140 CMD_ARGV[CMD_ARGC - 1]);
1145 int num_fields = CMD_ARGC / 2;
1146 size_t fields_len = sizeof(struct scan_field) * num_fields;
1147 fields = malloc(fields_len);
1148 memset(fields, 0, fields_len);
1151 for (i = 0; i < num_fields; i++)
1153 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1157 for (j = 0; j < i; j++)
1158 free(fields[j].out_value);
1160 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1164 int field_size = tap->ir_length;
1165 fields[i].tap = tap;
1166 fields[i].num_bits = field_size;
1167 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1170 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1171 if (ERROR_OK != retval)
1173 buf_set_u32(fields[i].out_value, 0, field_size, value);
1174 fields[i].in_value = NULL;
1177 /* did we have an endstate? */
1178 jtag_add_ir_scan(num_fields, fields, endstate);
1180 retval = jtag_execute_queue();
1183 for (i = 0; i < num_fields; i++)
1185 if (NULL != fields[i].out_value)
1186 free(fields[i].out_value);
1194 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1197 struct scan_field *fields;
1199 int field_count = 0;
1201 struct jtag_tap *tap;
1202 tap_state_t endstate;
1205 * args[2] = num_bits
1206 * args[3] = hex string
1207 * ... repeat num bits and hex string ...
1210 * args[N-2] = "-endstate"
1211 * args[N-1] = statename
1213 if ((argc < 4) || ((argc % 2) != 0))
1215 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1219 endstate = TAP_IDLE;
1221 script_debug(interp, "drscan", argc, args);
1223 /* validate arguments as numbers */
1225 for (i = 2; i < argc; i += 2)
1230 e = Jim_GetLong(interp, args[i], &bits);
1231 /* If valid - try next arg */
1236 /* Not valid.. are we at the end? */
1237 if (((i + 2) != argc)) {
1238 /* nope, then error */
1242 /* it could be: "-endstate FOO"
1243 * e.g. DRPAUSE so we can issue more instructions
1244 * before entering RUN/IDLE and executing them.
1247 /* get arg as a string. */
1248 cp = Jim_GetString(args[i], NULL);
1249 /* is it the magic? */
1250 if (0 == strcmp("-endstate", cp)) {
1251 /* is the statename valid? */
1252 cp = Jim_GetString(args[i + 1], NULL);
1254 /* see if it is a valid state name */
1255 endstate = tap_state_by_name(cp);
1257 /* update the error message */
1258 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1260 if (!scan_is_safe(endstate))
1261 LOG_WARNING("drscan with unsafe "
1262 "endstate \"%s\"", cp);
1264 /* valid - so clear the error */
1266 /* and remove the last 2 args */
1271 /* Still an error? */
1273 return e; /* too bad */
1275 } /* validate args */
1277 tap = jtag_tap_by_jim_obj(interp, args[1]);
1282 num_fields = (argc-2)/2;
1283 fields = malloc(sizeof(struct scan_field) * num_fields);
1284 for (i = 2; i < argc; i += 2)
1290 Jim_GetLong(interp, args[i], &bits);
1291 str = Jim_GetString(args[i + 1], &len);
1293 fields[field_count].tap = tap;
1294 fields[field_count].num_bits = bits;
1295 fields[field_count].out_value = malloc(DIV_ROUND_UP(bits, 8));
1296 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
1297 fields[field_count].in_value = fields[field_count].out_value;
1301 jtag_add_dr_scan(num_fields, fields, endstate);
1303 retval = jtag_execute_queue();
1304 if (retval != ERROR_OK)
1306 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
1311 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
1312 for (i = 2; i < argc; i += 2)
1317 Jim_GetLong(interp, args[i], &bits);
1318 str = buf_to_str(fields[field_count].in_value, bits, 16);
1319 free(fields[field_count].out_value);
1321 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
1326 Jim_SetResult(interp, list);
1334 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1336 tap_state_t states[8];
1338 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
1340 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1344 script_debug(interp, "pathmove", argc, args);
1347 for (i = 0; i < argc-1; i++)
1350 cp = Jim_GetString(args[i + 1], NULL);
1351 states[i] = tap_state_by_name(cp);
1354 /* update the error message */
1355 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1360 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
1362 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
1366 jtag_add_pathmove(argc-2, states + 1);
1368 if (jtag_execute_queue()!= ERROR_OK)
1370 Jim_SetResultString(interp, "pathmove: failed",-1);
1378 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1380 script_debug(interp, "flush_count", argc, args);
1382 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
1388 COMMAND_HANDLER(handle_verify_ircapture_command)
1391 return ERROR_COMMAND_SYNTAX_ERROR;
1396 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1397 jtag_set_verify_capture_ir(enable);
1400 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1401 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1406 COMMAND_HANDLER(handle_verify_jtag_command)
1409 return ERROR_COMMAND_SYNTAX_ERROR;
1414 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1415 jtag_set_verify(enable);
1418 const char *status = jtag_will_verify() ? "enabled": "disabled";
1419 command_print(CMD_CTX, "verify jtag capture is %s", status);
1424 COMMAND_HANDLER(handle_tms_sequence_command)
1427 return ERROR_COMMAND_SYNTAX_ERROR;
1432 if (strcmp(CMD_ARGV[0], "short") == 0)
1433 use_new_table = true;
1434 else if (strcmp(CMD_ARGV[0], "long") == 0)
1435 use_new_table = false;
1437 return ERROR_COMMAND_SYNTAX_ERROR;
1439 tap_use_new_tms_table(use_new_table);
1442 command_print(CMD_CTX, "tms sequence is %s",
1443 tap_uses_new_tms_table() ? "short": "long");
1448 static const struct command_registration jtag_command_handlers[] = {
1450 .name = "interface",
1451 .handler = &handle_interface_command,
1452 .mode = COMMAND_CONFIG,
1453 .help = "select a JTAG interface",
1454 .usage = "<driver_name>",
1457 .name = "interface_list",
1458 .handler = &handle_interface_list_command,
1459 .mode = COMMAND_ANY,
1460 .help = "list all built-in interfaces",
1464 .handler = &handle_jtag_khz_command,
1465 .mode = COMMAND_ANY,
1466 .help = "set maximum jtag speed (if supported)",
1467 .usage = "<khz:0=rtck>",
1470 .name = "jtag_rclk",
1471 .handler = &handle_jtag_rclk_command,
1472 .mode = COMMAND_ANY,
1473 .help = "set JTAG speed to RCLK or use fallback speed",
1474 .usage = "<fallback_speed_khz>",
1477 .name = "reset_config",
1478 .handler = &handle_reset_config_command,
1479 .mode = COMMAND_ANY,
1480 .help = "configure JTAG reset behavior",
1481 .usage = "[none|trst_only|srst_only|trst_and_srst] "
1482 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
1483 "[srst_gates_jtag|srst_nogate] "
1484 "[trst_push_pull|trst_open_drain] "
1485 "[srst_push_pull|srst_open_drain]",
1488 .name = "jtag_nsrst_delay",
1489 .handler = &handle_jtag_nsrst_delay_command,
1490 .mode = COMMAND_ANY,
1491 .help = "delay after deasserting srst in ms",
1495 .name = "jtag_ntrst_delay",
1496 .handler = &handle_jtag_ntrst_delay_command,
1497 .mode = COMMAND_ANY,
1498 .help = "delay after deasserting trst in ms",
1502 .name = "jtag_nsrst_assert_width",
1503 .handler = &handle_jtag_nsrst_assert_width_command,
1504 .mode = COMMAND_ANY,
1505 .help = "delay after asserting srst in ms",
1509 .name = "jtag_ntrst_assert_width",
1510 .handler = &handle_jtag_ntrst_assert_width_command,
1511 .mode = COMMAND_ANY,
1512 .help = "delay after asserting trst in ms",
1516 .name = "scan_chain",
1517 .handler = &handle_scan_chain_command,
1518 .mode = COMMAND_EXEC,
1519 .help = "print current scan chain configuration",
1522 .name = "jtag_reset",
1523 .handler = &handle_jtag_reset_command,
1524 .mode = COMMAND_EXEC,
1525 .help = "toggle reset lines",
1526 .usage = "<trst> <srst>",
1530 .handler = &handle_runtest_command,
1531 .mode = COMMAND_EXEC,
1532 .help = "move to Run-Test/Idle, and execute <num_cycles>",
1533 .usage = "<num_cycles>"
1537 .handler = &handle_irscan_command,
1538 .mode = COMMAND_EXEC,
1539 .help = "execute IR scan",
1540 .usage = "<device> <instr> [dev2] [instr2] ...",
1543 .name = "verify_ircapture",
1544 .handler = &handle_verify_ircapture_command,
1545 .mode = COMMAND_ANY,
1546 .help = "verify value captured during Capture-IR",
1547 .usage = "<enable | disable>",
1550 .name = "verify_jtag",
1551 .handler = &handle_verify_jtag_command,
1552 .mode = COMMAND_ANY,
1553 .help = "verify value capture",
1554 .usage = "<enable | disable>",
1557 .name = "tms_sequence",
1558 .handler = &handle_tms_sequence_command,
1559 .mode = COMMAND_ANY,
1560 .help = "choose short(default) or long tms_sequence",
1561 .usage = "<short | long>",
1566 .mode = COMMAND_ANY,
1567 .help = "perform jtag tap actions",
1569 .chain = jtag_subcommand_handlers,
1573 .mode = COMMAND_EXEC,
1574 .jim_handler = &Jim_Command_drscan,
1575 .help = "execute DR scan <device> "
1576 "<num_bits> <value> <num_bits1> <value2> ...",
1579 .name = "flush_count",
1580 .mode = COMMAND_EXEC,
1581 .jim_handler = &Jim_Command_flush_count,
1582 .help = "returns number of times the JTAG queue has been flushed",
1586 .mode = COMMAND_EXEC,
1587 .jim_handler = &Jim_Command_pathmove,
1588 .usage = "<state1>,<state2>,<state3>... ",
1589 .help = "move JTAG to state1 then to state2, state3, etc.",
1591 COMMAND_REGISTRATION_DONE
1594 int jtag_register_commands(struct command_context *cmd_ctx)
1596 return register_commands(cmd_ctx, NULL, jtag_command_handlers);