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_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
302 /* parse config or cget options */
303 while (goi->argc > 0) {
304 Jim_SetEmptyResult (goi->interp);
306 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
308 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
314 if (goi->argc == 0) {
315 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
319 e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
321 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
325 if (goi->isconfigure) {
326 if (goi->argc != 1) {
327 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
331 if (goi->argc != 0) {
332 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
338 struct jtag_tap_event_action *jteap;
340 jteap = tap->event_action;
341 /* replace existing? */
343 if (jteap->event == (enum jtag_event)n->value) {
349 if (goi->isconfigure) {
353 jteap = calloc(1, sizeof (*jteap));
356 jteap->event = n->value;
357 Jim_GetOpt_Obj(goi, &o);
359 Jim_DecrRefCount(interp, jteap->body);
361 jteap->body = Jim_DuplicateObj(goi->interp, o);
362 Jim_IncrRefCount(jteap->body);
366 /* add to head of event list */
367 jteap->next = tap->event_action;
368 tap->event_action = jteap;
370 Jim_SetEmptyResult(goi->interp);
374 Jim_SetEmptyResult(goi->interp);
376 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
383 } /* while (goi->argc) */
388 static int is_bad_irval(int ir_length, jim_wide w)
398 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
400 struct jtag_tap *pTap;
406 const Jim_Nvp opts[] = {
407 #define NTAP_OPT_IRLEN 0
408 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
409 #define NTAP_OPT_IRMASK 1
410 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
411 #define NTAP_OPT_IRCAPTURE 2
412 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
413 #define NTAP_OPT_ENABLED 3
414 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
415 #define NTAP_OPT_DISABLED 4
416 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
417 #define NTAP_OPT_EXPECTED_ID 5
418 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
419 { .name = NULL , .value = -1 },
422 pTap = calloc(1, sizeof(struct jtag_tap));
424 Jim_SetResult_sprintf(goi->interp, "no memory");
429 * we expect CHIP + TAP + OPTIONS
432 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
436 Jim_GetOpt_String(goi, &cp, NULL);
437 pTap->chip = strdup(cp);
439 Jim_GetOpt_String(goi, &cp, NULL);
440 pTap->tapname = strdup(cp);
442 /* name + dot + name + null */
443 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
445 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
446 pTap->dotted_name = cp;
448 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
449 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
451 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
452 * that the default. The "-irlen" and "-irmask" options are only
453 * needed to cope with nonstandard TAPs, or to specify more bits.
455 pTap->ir_capture_mask = 0x03;
456 pTap->ir_capture_value = 0x01;
459 e = Jim_GetOpt_Nvp(goi, opts, &n);
461 Jim_GetOpt_NvpUnknown(goi, opts, 0);
462 free((void *)pTap->dotted_name);
466 LOG_DEBUG("Processing option: %s", n->name);
468 case NTAP_OPT_ENABLED:
469 pTap->disabled_after_reset = false;
471 case NTAP_OPT_DISABLED:
472 pTap->disabled_after_reset = true;
474 case NTAP_OPT_EXPECTED_ID:
476 uint32_t *new_expected_ids;
478 e = Jim_GetOpt_Wide(goi, &w);
480 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
481 free((void *)pTap->dotted_name);
486 new_expected_ids = malloc(sizeof(uint32_t) * (pTap->expected_ids_cnt + 1));
487 if (new_expected_ids == NULL) {
488 Jim_SetResult_sprintf(goi->interp, "no memory");
489 free((void *)pTap->dotted_name);
494 memcpy(new_expected_ids, pTap->expected_ids, sizeof(uint32_t) * pTap->expected_ids_cnt);
496 new_expected_ids[pTap->expected_ids_cnt] = w;
498 free(pTap->expected_ids);
499 pTap->expected_ids = new_expected_ids;
500 pTap->expected_ids_cnt++;
504 case NTAP_OPT_IRMASK:
505 case NTAP_OPT_IRCAPTURE:
506 e = Jim_GetOpt_Wide(goi, &w);
508 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
509 free((void *)pTap->dotted_name);
515 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
516 LOG_WARNING("%s: huge IR length %d",
521 case NTAP_OPT_IRMASK:
522 if (is_bad_irval(pTap->ir_length, w)) {
523 LOG_ERROR("%s: IR mask %x too big",
526 free((void *)pTap->dotted_name);
531 LOG_WARNING("%s: nonstandard IR mask",
533 pTap->ir_capture_mask = w;
535 case NTAP_OPT_IRCAPTURE:
536 if (is_bad_irval(pTap->ir_length, w)) {
537 LOG_ERROR("%s: IR capture %x too big",
540 free((void *)pTap->dotted_name);
545 LOG_WARNING("%s: nonstandard IR value",
547 pTap->ir_capture_value = w;
550 } /* switch (n->value) */
551 } /* while (goi->argc) */
553 /* default is enabled-after-reset */
554 pTap->enabled = !pTap->disabled_after_reset;
556 /* Did all the required option bits get cleared? */
557 if (pTap->ir_length != 0)
563 Jim_SetResult_sprintf(goi->interp,
564 "newtap: %s missing IR length",
570 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
572 struct jtag_tap_event_action * jteap;
574 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
576 if (jteap->event != e)
579 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
580 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
581 tap->dotted_name, e, nvp->name,
582 Jim_GetString(jteap->body, NULL));
584 if (Jim_EvalObj(interp, jteap->body) != JIM_OK)
586 Jim_PrintErrorMessage(interp);
592 case JTAG_TAP_EVENT_ENABLE:
593 case JTAG_TAP_EVENT_DISABLE:
594 /* NOTE: we currently assume the handlers
595 * can't fail. Right here is where we should
596 * really be verifying the scan chains ...
598 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
599 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
600 tap->enabled ? "enabled" : "disabled");
608 static int jim_jtag_interface(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
611 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
613 /* return the name of the interface */
614 /* TCL code might need to know the exact type... */
615 /* FUTURE: we allow this as a means to "set" the interface. */
617 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
620 const char *name = jtag_interface ? jtag_interface->name : NULL;
621 Jim_SetResultString(goi.interp, name ? : "undefined", -1);
625 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
628 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
630 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
633 struct command_context *context = Jim_GetAssocData(interp, "context");
634 int e = jtag_init_inner(context);
636 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
642 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
645 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
647 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
650 struct command_context *context = Jim_GetAssocData(interp, "context");
651 int e = jtag_init_reset(context);
653 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
659 static int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
662 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
663 return jim_newtap_cmd(&goi);
666 static bool jtag_tap_enable(struct jtag_tap *t)
670 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
674 /* FIXME add JTAG sanity checks, w/o TLR
675 * - scan chain length grew by one (this)
676 * - IDs and IR lengths are as expected
678 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
681 static bool jtag_tap_disable(struct jtag_tap *t)
685 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
689 /* FIXME add JTAG sanity checks, w/o TLR
690 * - scan chain length shrank by one (this)
691 * - IDs and IR lengths are as expected
693 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
697 static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
699 const char *cmd_name = Jim_GetString(argv[0], NULL);
701 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
703 Jim_SetResult_sprintf(goi.interp, "usage: %s <name>", cmd_name);
709 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
713 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
714 // do nothing, just return the value
715 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
716 if (!jtag_tap_enable(t))
717 LOG_WARNING("failed to disable tap");
718 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
719 if (!jtag_tap_disable(t))
720 LOG_WARNING("failed to disable tap");
722 LOG_ERROR("command '%s' unknown", cmd_name);
726 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
730 static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
732 const char *cmd_name = Jim_GetString(argv[0], NULL);
734 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
735 goi.isconfigure = !strcmp(cmd_name, "configure");
736 if (goi.argc < 2 + goi.isconfigure) {
737 Jim_WrongNumArgs(goi.interp, 0, NULL,
738 "<tap_name> <attribute> ...");
745 Jim_GetOpt_Obj(&goi, &o);
746 t = jtag_tap_by_jim_obj(goi.interp, o);
751 return jtag_tap_configure_cmd(&goi, t);
754 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
757 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
759 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
762 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
763 struct jtag_tap *tap;
765 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
766 Jim_ListAppendElement(goi.interp,
767 Jim_GetResult(goi.interp),
768 Jim_NewStringObj(goi.interp,
769 tap->dotted_name, -1));
774 static const struct command_registration jtag_subcommand_handlers[] = {
778 .jim_handler = &jim_jtag_interface,
779 .help = "Returns the selected interface",
784 .jim_handler = &jim_jtag_arp_init,
787 .name = "arp_init-reset",
789 .jim_handler = &jim_jtag_arp_init_reset,
793 .mode = COMMAND_CONFIG,
794 .jim_handler = &jim_jtag_newtap,
795 .help = "Create a new TAP instance",
796 .usage = "<name> <type> -irlen <count> [-ircapture <count>] "
797 "[-irmask <count>] [-enable|-disable]",
800 .name = "tapisenabled",
801 .mode = COMMAND_EXEC,
802 .jim_handler = &jim_jtag_tap_enabler,
803 .help = "Returns a integer indicating TAP state (0/1)",
808 .mode = COMMAND_EXEC,
809 .jim_handler = &jim_jtag_tap_enabler,
810 .help = "Enable the specified TAP",
814 .name = "tapdisable",
815 .mode = COMMAND_EXEC,
816 .jim_handler = &jim_jtag_tap_enabler,
817 .help = "Enable the specified TAP",
822 .mode = COMMAND_EXEC,
823 .jim_handler = &jim_jtag_configure,
824 .help = "Enable the specified TAP",
825 .usage = "<name> [<key> <value> ...]",
829 .mode = COMMAND_EXEC,
830 .jim_handler = &jim_jtag_configure,
831 .help = "Enable the specified TAP",
832 .usage = "<name> [<key> <value> ...]",
837 .jim_handler = &jim_jtag_names,
838 .help = "Returns list of all JTAG tap names",
841 .chain = jtag_command_handlers_to_move,
843 COMMAND_REGISTRATION_DONE
846 void jtag_notify_event(enum jtag_event event)
848 struct jtag_tap *tap;
850 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
851 jtag_tap_handle_event(tap, event);
855 static int default_khz(int khz, int *jtag_speed)
857 LOG_ERROR("Translation from khz to jtag_speed not implemented");
861 static int default_speed_div(int speed, int *khz)
863 LOG_ERROR("Translation from jtag_speed to khz not implemented");
867 static int default_power_dropout(int *dropout)
869 *dropout = 0; /* by default we can't detect power dropout */
873 static int default_srst_asserted(int *srst_asserted)
875 *srst_asserted = 0; /* by default we can't detect srst asserted */
879 COMMAND_HANDLER(handle_interface_list_command)
881 if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
882 return ERROR_COMMAND_SYNTAX_ERROR;
884 command_print(CMD_CTX, "The following JTAG interfaces are available:");
885 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
887 const char *name = jtag_interfaces[i]->name;
888 command_print(CMD_CTX, "%u: %s", i + 1, name);
894 COMMAND_HANDLER(handle_interface_command)
896 /* check whether the interface is already configured */
899 LOG_WARNING("Interface already configured, ignoring");
903 /* interface name is a mandatory argument */
904 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
905 return ERROR_COMMAND_SYNTAX_ERROR;
907 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
909 if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0)
912 if (NULL != jtag_interfaces[i]->commands)
914 int retval = register_commands(CMD_CTX, NULL,
915 jtag_interfaces[i]->commands);
916 if (ERROR_OK != retval)
920 jtag_interface = jtag_interfaces[i];
922 if (jtag_interface->khz == NULL)
923 jtag_interface->khz = default_khz;
924 if (jtag_interface->speed_div == NULL)
925 jtag_interface->speed_div = default_speed_div;
926 if (jtag_interface->power_dropout == NULL)
927 jtag_interface->power_dropout = default_power_dropout;
928 if (jtag_interface->srst_asserted == NULL)
929 jtag_interface->srst_asserted = default_srst_asserted;
934 /* no valid interface was found (i.e. the configuration option,
935 * didn't match one of the compiled-in interfaces
937 LOG_ERROR("The specified JTAG interface was not found (%s)", CMD_ARGV[0]);
938 CALL_COMMAND_HANDLER(handle_interface_list_command);
939 return ERROR_JTAG_INVALID_INTERFACE;
942 COMMAND_HANDLER(handle_scan_chain_command)
944 struct jtag_tap *tap;
946 tap = jtag_all_taps();
947 command_print(CMD_CTX, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
948 command_print(CMD_CTX, "---|--------------------|---------|------------|------------|------|------|------|---------");
951 uint32_t expected, expected_mask, cur_instr, ii;
952 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
953 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
954 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
956 command_print(CMD_CTX,
957 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
958 tap->abs_chain_position,
960 tap->enabled ? 'Y' : 'n',
961 (unsigned int)(tap->idcode),
962 (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
963 (unsigned int)(tap->ir_length),
964 (unsigned int)(expected),
965 (unsigned int)(expected_mask),
966 (unsigned int)(cur_instr));
968 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
969 command_print(CMD_CTX, " | | | | 0x%08x | | | | ",
970 (unsigned int)(tap->expected_ids[ii]));
979 COMMAND_HANDLER(handle_reset_config_command)
984 /* Original versions cared about the order of these tokens:
985 * reset_config signals [combination [trst_type [srst_type]]]
986 * They also clobbered the previous configuration even on error.
988 * Here we don't care about the order, and only change values
989 * which have been explicitly specified.
991 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
996 m = RESET_SRST_NO_GATING;
997 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
998 /* default: don't use JTAG while SRST asserted */;
999 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
1000 tmp = RESET_SRST_NO_GATING;
1004 LOG_ERROR("extra reset_config %s spec (%s)",
1005 "gating", *CMD_ARGV);
1006 return ERROR_INVALID_ARGUMENTS;
1012 m = RESET_HAS_TRST | RESET_HAS_SRST;
1013 if (strcmp(*CMD_ARGV, "none") == 0)
1015 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
1016 tmp = RESET_HAS_TRST;
1017 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
1018 tmp = RESET_HAS_SRST;
1019 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
1020 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
1024 LOG_ERROR("extra reset_config %s spec (%s)",
1025 "signal", *CMD_ARGV);
1026 return ERROR_INVALID_ARGUMENTS;
1031 /* combination (options for broken wiring) */
1032 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1033 if (strcmp(*CMD_ARGV, "separate") == 0)
1034 /* separate reset lines - default */;
1035 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
1036 tmp |= RESET_SRST_PULLS_TRST;
1037 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
1038 tmp |= RESET_TRST_PULLS_SRST;
1039 else if (strcmp(*CMD_ARGV, "combined") == 0)
1040 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1044 LOG_ERROR("extra reset_config %s spec (%s)",
1045 "combination", *CMD_ARGV);
1046 return ERROR_INVALID_ARGUMENTS;
1051 /* trst_type (NOP without HAS_TRST) */
1052 m = RESET_TRST_OPEN_DRAIN;
1053 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
1054 tmp |= RESET_TRST_OPEN_DRAIN;
1055 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
1056 /* push/pull from adapter - default */;
1060 LOG_ERROR("extra reset_config %s spec (%s)",
1061 "trst_type", *CMD_ARGV);
1062 return ERROR_INVALID_ARGUMENTS;
1067 /* srst_type (NOP without HAS_SRST) */
1068 m |= RESET_SRST_PUSH_PULL;
1069 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
1070 tmp |= RESET_SRST_PUSH_PULL;
1071 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
1072 /* open drain from adapter - default */;
1076 LOG_ERROR("extra reset_config %s spec (%s)",
1077 "srst_type", *CMD_ARGV);
1078 return ERROR_INVALID_ARGUMENTS;
1083 /* caller provided nonsense; fail */
1084 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
1085 return ERROR_INVALID_ARGUMENTS;
1088 /* Remember the bits which were specified (mask)
1089 * and their new values (new_cfg).
1095 /* clear previous values of those bits, save new values */
1097 int old_cfg = jtag_get_reset_config();
1101 jtag_set_reset_config(new_cfg);
1103 new_cfg = jtag_get_reset_config();
1107 * Display the (now-)current reset mode
1111 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
1112 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
1113 case RESET_HAS_SRST:
1114 modes[0] = "srst_only";
1116 case RESET_HAS_TRST:
1117 modes[0] = "trst_only";
1119 case RESET_TRST_AND_SRST:
1120 modes[0] = "trst_and_srst";
1127 /* normally SRST and TRST are decoupled; but bugs happen ... */
1128 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
1129 case RESET_SRST_PULLS_TRST:
1130 modes[1] = "srst_pulls_trst";
1132 case RESET_TRST_PULLS_SRST:
1133 modes[1] = "trst_pulls_srst";
1135 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
1136 modes[1] = "combined";
1139 modes[1] = "separate";
1143 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
1144 if (new_cfg & RESET_HAS_TRST) {
1145 if (new_cfg & RESET_TRST_OPEN_DRAIN)
1146 modes[3] = " trst_open_drain";
1148 modes[3] = " trst_push_pull";
1152 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
1153 if (new_cfg & RESET_HAS_SRST) {
1154 if (new_cfg & RESET_SRST_NO_GATING)
1155 modes[2] = " srst_nogate";
1157 modes[2] = " srst_gates_jtag";
1159 if (new_cfg & RESET_SRST_PUSH_PULL)
1160 modes[4] = " srst_push_pull";
1162 modes[4] = " srst_open_drain";
1168 command_print(CMD_CTX, "%s %s%s%s%s",
1170 modes[2], modes[3], modes[4]);
1175 COMMAND_HANDLER(handle_jtag_nsrst_delay_command)
1178 return ERROR_COMMAND_SYNTAX_ERROR;
1182 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1184 jtag_set_nsrst_delay(delay);
1186 command_print(CMD_CTX, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
1190 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1193 return ERROR_COMMAND_SYNTAX_ERROR;
1197 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1199 jtag_set_ntrst_delay(delay);
1201 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1205 COMMAND_HANDLER(handle_jtag_nsrst_assert_width_command)
1208 return ERROR_COMMAND_SYNTAX_ERROR;
1212 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1214 jtag_set_nsrst_assert_width(delay);
1216 command_print(CMD_CTX, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
1220 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1223 return ERROR_COMMAND_SYNTAX_ERROR;
1227 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1229 jtag_set_ntrst_assert_width(delay);
1231 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1235 COMMAND_HANDLER(handle_jtag_khz_command)
1238 return ERROR_COMMAND_SYNTAX_ERROR;
1240 int retval = ERROR_OK;
1244 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1246 retval = jtag_config_khz(khz);
1247 if (ERROR_OK != retval)
1251 int cur_speed = jtag_get_speed_khz();
1252 retval = jtag_get_speed_readable(&cur_speed);
1253 if (ERROR_OK != retval)
1257 command_print(CMD_CTX, "%d kHz", cur_speed);
1259 command_print(CMD_CTX, "RCLK - adaptive");
1264 COMMAND_HANDLER(handle_jtag_rclk_command)
1267 return ERROR_COMMAND_SYNTAX_ERROR;
1269 int retval = ERROR_OK;
1273 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1275 retval = jtag_config_rclk(khz);
1276 if (ERROR_OK != retval)
1280 int cur_khz = jtag_get_speed_khz();
1281 retval = jtag_get_speed_readable(&cur_khz);
1282 if (ERROR_OK != retval)
1286 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1288 command_print(CMD_CTX, "RCLK - adaptive");
1293 COMMAND_HANDLER(handle_jtag_reset_command)
1296 return ERROR_COMMAND_SYNTAX_ERROR;
1299 if (CMD_ARGV[0][0] == '1')
1301 else if (CMD_ARGV[0][0] == '0')
1304 return ERROR_COMMAND_SYNTAX_ERROR;
1307 if (CMD_ARGV[1][0] == '1')
1309 else if (CMD_ARGV[1][0] == '0')
1312 return ERROR_COMMAND_SYNTAX_ERROR;
1314 if (jtag_interface_init(CMD_CTX) != ERROR_OK)
1315 return ERROR_JTAG_INIT_FAILED;
1317 jtag_add_reset(trst, srst);
1318 return jtag_execute_queue();
1321 COMMAND_HANDLER(handle_runtest_command)
1324 return ERROR_COMMAND_SYNTAX_ERROR;
1326 unsigned num_clocks;
1327 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1329 jtag_add_runtest(num_clocks, TAP_IDLE);
1330 return jtag_execute_queue();
1334 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1335 * should be stable ... and *NOT* a shift state, otherwise free-running
1336 * jtag clocks could change the values latched by the update state.
1337 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1338 * and "drscan" commands are a write-only subset of what SVF provides.
1341 COMMAND_HANDLER(handle_irscan_command)
1344 struct scan_field *fields;
1345 struct jtag_tap *tap;
1346 tap_state_t endstate;
1348 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1350 return ERROR_COMMAND_SYNTAX_ERROR;
1353 /* optional "-endstate" "statename" at the end of the arguments,
1354 * so that e.g. IRPAUSE can let us load the data register before
1355 * entering RUN/IDLE to execute the instruction we load here.
1357 endstate = TAP_IDLE;
1359 if (CMD_ARGC >= 4) {
1360 /* have at least one pair of numbers. */
1361 /* is last pair the magic text? */
1362 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1363 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1364 if (endstate == TAP_INVALID)
1365 return ERROR_COMMAND_SYNTAX_ERROR;
1366 if (!scan_is_safe(endstate))
1367 LOG_WARNING("unstable irscan endstate \"%s\"",
1368 CMD_ARGV[CMD_ARGC - 1]);
1373 int num_fields = CMD_ARGC / 2;
1374 size_t fields_len = sizeof(struct scan_field) * num_fields;
1375 fields = malloc(fields_len);
1376 memset(fields, 0, fields_len);
1379 for (i = 0; i < num_fields; i++)
1381 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1385 for (j = 0; j < i; j++)
1386 free(fields[j].out_value);
1388 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1392 int field_size = tap->ir_length;
1393 fields[i].tap = tap;
1394 fields[i].num_bits = field_size;
1395 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1398 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1399 if (ERROR_OK != retval)
1401 buf_set_u32(fields[i].out_value, 0, field_size, value);
1402 fields[i].in_value = NULL;
1405 /* did we have an endstate? */
1406 jtag_add_ir_scan(num_fields, fields, endstate);
1408 retval = jtag_execute_queue();
1411 for (i = 0; i < num_fields; i++)
1413 if (NULL != fields[i].out_value)
1414 free(fields[i].out_value);
1423 COMMAND_HANDLER(handle_verify_ircapture_command)
1426 return ERROR_COMMAND_SYNTAX_ERROR;
1431 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1432 jtag_set_verify_capture_ir(enable);
1435 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1436 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1441 COMMAND_HANDLER(handle_verify_jtag_command)
1444 return ERROR_COMMAND_SYNTAX_ERROR;
1449 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1450 jtag_set_verify(enable);
1453 const char *status = jtag_will_verify() ? "enabled": "disabled";
1454 command_print(CMD_CTX, "verify jtag capture is %s", status);
1459 COMMAND_HANDLER(handle_tms_sequence_command)
1462 return ERROR_COMMAND_SYNTAX_ERROR;
1467 if (strcmp(CMD_ARGV[0], "short") == 0)
1468 use_new_table = true;
1469 else if (strcmp(CMD_ARGV[0], "long") == 0)
1470 use_new_table = false;
1472 return ERROR_COMMAND_SYNTAX_ERROR;
1474 tap_use_new_tms_table(use_new_table);
1477 command_print(CMD_CTX, "tms sequence is %s",
1478 tap_uses_new_tms_table() ? "short": "long");
1483 static const struct command_registration jtag_command_handlers[] = {
1485 .name = "interface",
1486 .handler = &handle_interface_command,
1487 .mode = COMMAND_CONFIG,
1488 .help = "select a JTAG interface",
1489 .usage = "<driver_name>",
1492 .name = "interface_list",
1493 .handler = &handle_interface_list_command,
1494 .mode = COMMAND_ANY,
1495 .help = "list all built-in interfaces",
1499 .handler = &handle_jtag_khz_command,
1500 .mode = COMMAND_ANY,
1501 .help = "set maximum jtag speed (if supported)",
1502 .usage = "<khz:0=rtck>",
1505 .name = "jtag_rclk",
1506 .handler = &handle_jtag_rclk_command,
1507 .mode = COMMAND_ANY,
1508 .help = "set JTAG speed to RCLK or use fallback speed",
1509 .usage = "<fallback_speed_khz>",
1512 .name = "reset_config",
1513 .handler = &handle_reset_config_command,
1514 .mode = COMMAND_ANY,
1515 .help = "configure JTAG reset behavior",
1516 .usage = "[none|trst_only|srst_only|trst_and_srst] "
1517 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
1518 "[srst_gates_jtag|srst_nogate] "
1519 "[trst_push_pull|trst_open_drain] "
1520 "[srst_push_pull|srst_open_drain]",
1523 .name = "jtag_nsrst_delay",
1524 .handler = &handle_jtag_nsrst_delay_command,
1525 .mode = COMMAND_ANY,
1526 .help = "delay after deasserting srst in ms",
1530 .name = "jtag_ntrst_delay",
1531 .handler = &handle_jtag_ntrst_delay_command,
1532 .mode = COMMAND_ANY,
1533 .help = "delay after deasserting trst in ms",
1537 .name = "jtag_nsrst_assert_width",
1538 .handler = &handle_jtag_nsrst_assert_width_command,
1539 .mode = COMMAND_ANY,
1540 .help = "delay after asserting srst in ms",
1544 .name = "jtag_ntrst_assert_width",
1545 .handler = &handle_jtag_ntrst_assert_width_command,
1546 .mode = COMMAND_ANY,
1547 .help = "delay after asserting trst in ms",
1551 .name = "scan_chain",
1552 .handler = &handle_scan_chain_command,
1553 .mode = COMMAND_EXEC,
1554 .help = "print current scan chain configuration",
1557 .name = "jtag_reset",
1558 .handler = &handle_jtag_reset_command,
1559 .mode = COMMAND_EXEC,
1560 .help = "toggle reset lines",
1561 .usage = "<trst> <srst>",
1565 .handler = &handle_runtest_command,
1566 .mode = COMMAND_EXEC,
1567 .help = "move to Run-Test/Idle, and execute <num_cycles>",
1568 .usage = "<num_cycles>"
1572 .handler = &handle_irscan_command,
1573 .mode = COMMAND_EXEC,
1574 .help = "execute IR scan",
1575 .usage = "<device> <instr> [dev2] [instr2] ...",
1578 .name = "verify_ircapture",
1579 .handler = &handle_verify_ircapture_command,
1580 .mode = COMMAND_ANY,
1581 .help = "verify value captured during Capture-IR",
1582 .usage = "<enable | disable>",
1585 .name = "verify_jtag",
1586 .handler = &handle_verify_jtag_command,
1587 .mode = COMMAND_ANY,
1588 .help = "verify value capture",
1589 .usage = "<enable | disable>",
1592 .name = "tms_sequence",
1593 .handler = &handle_tms_sequence_command,
1594 .mode = COMMAND_ANY,
1595 .help = "choose short(default) or long tms_sequence",
1596 .usage = "<short | long>",
1600 .mode = COMMAND_ANY,
1601 .help = "perform jtag tap actions",
1603 .chain = jtag_subcommand_handlers,
1606 .chain = jtag_command_handlers_to_move,
1608 COMMAND_REGISTRATION_DONE
1610 int jtag_register_commands(struct command_context *cmd_ctx)
1612 return register_commands(cmd_ctx, NULL, jtag_command_handlers);