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) {
575 if (jteap->event == e) {
576 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
579 Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
580 Jim_GetString(jteap->body, NULL));
581 if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
582 Jim_PrintErrorMessage(interp);
584 case JTAG_TAP_EVENT_ENABLE:
585 case JTAG_TAP_EVENT_DISABLE:
586 /* NOTE: we currently assume the handlers
587 * can't fail. Right here is where we should
588 * really be verifying the scan chains ...
590 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
591 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
592 tap->enabled ? "enabled" : "disabled");
601 static int jim_jtag_interface(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
604 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
606 /* return the name of the interface */
607 /* TCL code might need to know the exact type... */
608 /* FUTURE: we allow this as a means to "set" the interface. */
610 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
613 const char *name = jtag_interface ? jtag_interface->name : NULL;
614 Jim_SetResultString(goi.interp, name ? : "undefined", -1);
618 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
621 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
623 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
626 struct command_context *context = Jim_GetAssocData(interp, "context");
627 int e = jtag_init_inner(context);
629 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
635 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
638 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
640 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
643 struct command_context *context = Jim_GetAssocData(interp, "context");
644 int e = jtag_init_reset(context);
646 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
652 static int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
655 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
656 return jim_newtap_cmd(&goi);
659 static bool jtag_tap_enable(struct jtag_tap *t)
663 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
667 /* FIXME add JTAG sanity checks, w/o TLR
668 * - scan chain length grew by one (this)
669 * - IDs and IR lengths are as expected
671 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
674 static bool jtag_tap_disable(struct jtag_tap *t)
678 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
682 /* FIXME add JTAG sanity checks, w/o TLR
683 * - scan chain length shrank by one (this)
684 * - IDs and IR lengths are as expected
686 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
690 static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
692 const char *cmd_name = Jim_GetString(argv[0], NULL);
694 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
696 Jim_SetResult_sprintf(goi.interp, "usage: %s <name>", cmd_name);
702 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
706 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
707 // do nothing, just return the value
708 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
709 if (!jtag_tap_enable(t))
710 LOG_WARNING("failed to disable tap");
711 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
712 if (!jtag_tap_disable(t))
713 LOG_WARNING("failed to disable tap");
715 LOG_ERROR("command '%s' unknown", cmd_name);
719 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
723 static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
725 const char *cmd_name = Jim_GetString(argv[0], NULL);
727 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
728 goi.isconfigure = !strcmp(cmd_name, "configure");
729 if (goi.argc < 2 + goi.isconfigure) {
730 Jim_WrongNumArgs(goi.interp, 0, NULL,
731 "<tap_name> <attribute> ...");
738 Jim_GetOpt_Obj(&goi, &o);
739 t = jtag_tap_by_jim_obj(goi.interp, o);
744 return jtag_tap_configure_cmd(&goi, t);
747 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
750 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
752 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
755 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
756 struct jtag_tap *tap;
758 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
759 Jim_ListAppendElement(goi.interp,
760 Jim_GetResult(goi.interp),
761 Jim_NewStringObj(goi.interp,
762 tap->dotted_name, -1));
767 static const struct command_registration jtag_subcommand_handlers[] = {
771 .jim_handler = &jim_jtag_interface,
772 .help = "Returns the selected interface",
777 .jim_handler = &jim_jtag_arp_init,
780 .name = "arp_init-reset",
782 .jim_handler = &jim_jtag_arp_init_reset,
786 .mode = COMMAND_CONFIG,
787 .jim_handler = &jim_jtag_newtap,
788 .help = "Create a new TAP instance",
789 .usage = "<name> <type> -irlen <count> [-ircapture <count>] "
790 "[-irmask <count>] [-enable|-disable]",
793 .name = "tapisenabled",
794 .mode = COMMAND_EXEC,
795 .jim_handler = &jim_jtag_tap_enabler,
796 .help = "Returns a integer indicating TAP state (0/1)",
801 .mode = COMMAND_EXEC,
802 .jim_handler = &jim_jtag_tap_enabler,
803 .help = "Enable the specified TAP",
807 .name = "tapdisable",
808 .mode = COMMAND_EXEC,
809 .jim_handler = &jim_jtag_tap_enabler,
810 .help = "Enable the specified TAP",
815 .mode = COMMAND_EXEC,
816 .jim_handler = &jim_jtag_configure,
817 .help = "Enable the specified TAP",
818 .usage = "<name> [<key> <value> ...]",
822 .mode = COMMAND_EXEC,
823 .jim_handler = &jim_jtag_configure,
824 .help = "Enable the specified TAP",
825 .usage = "<name> [<key> <value> ...]",
830 .jim_handler = &jim_jtag_names,
831 .help = "Returns list of all JTAG tap names",
834 .chain = jtag_command_handlers_to_move,
836 COMMAND_REGISTRATION_DONE
839 void jtag_notify_event(enum jtag_event event)
841 struct jtag_tap *tap;
843 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
844 jtag_tap_handle_event(tap, event);
848 static int default_khz(int khz, int *jtag_speed)
850 LOG_ERROR("Translation from khz to jtag_speed not implemented");
854 static int default_speed_div(int speed, int *khz)
856 LOG_ERROR("Translation from jtag_speed to khz not implemented");
860 static int default_power_dropout(int *dropout)
862 *dropout = 0; /* by default we can't detect power dropout */
866 static int default_srst_asserted(int *srst_asserted)
868 *srst_asserted = 0; /* by default we can't detect srst asserted */
872 COMMAND_HANDLER(handle_interface_list_command)
874 if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
875 return ERROR_COMMAND_SYNTAX_ERROR;
877 command_print(CMD_CTX, "The following JTAG interfaces are available:");
878 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
880 const char *name = jtag_interfaces[i]->name;
881 command_print(CMD_CTX, "%u: %s", i + 1, name);
887 COMMAND_HANDLER(handle_interface_command)
889 /* check whether the interface is already configured */
892 LOG_WARNING("Interface already configured, ignoring");
896 /* interface name is a mandatory argument */
897 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
898 return ERROR_COMMAND_SYNTAX_ERROR;
900 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
902 if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0)
905 if (NULL != jtag_interfaces[i]->commands)
907 int retval = register_commands(CMD_CTX, NULL,
908 jtag_interfaces[i]->commands);
909 if (ERROR_OK != retval)
913 jtag_interface = jtag_interfaces[i];
915 if (jtag_interface->khz == NULL)
916 jtag_interface->khz = default_khz;
917 if (jtag_interface->speed_div == NULL)
918 jtag_interface->speed_div = default_speed_div;
919 if (jtag_interface->power_dropout == NULL)
920 jtag_interface->power_dropout = default_power_dropout;
921 if (jtag_interface->srst_asserted == NULL)
922 jtag_interface->srst_asserted = default_srst_asserted;
927 /* no valid interface was found (i.e. the configuration option,
928 * didn't match one of the compiled-in interfaces
930 LOG_ERROR("The specified JTAG interface was not found (%s)", CMD_ARGV[0]);
931 CALL_COMMAND_HANDLER(handle_interface_list_command);
932 return ERROR_JTAG_INVALID_INTERFACE;
935 COMMAND_HANDLER(handle_scan_chain_command)
937 struct jtag_tap *tap;
939 tap = jtag_all_taps();
940 command_print(CMD_CTX, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
941 command_print(CMD_CTX, "---|--------------------|---------|------------|------------|------|------|------|---------");
944 uint32_t expected, expected_mask, cur_instr, ii;
945 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
946 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
947 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
949 command_print(CMD_CTX,
950 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
951 tap->abs_chain_position,
953 tap->enabled ? 'Y' : 'n',
954 (unsigned int)(tap->idcode),
955 (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
956 (unsigned int)(tap->ir_length),
957 (unsigned int)(expected),
958 (unsigned int)(expected_mask),
959 (unsigned int)(cur_instr));
961 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
962 command_print(CMD_CTX, " | | | | 0x%08x | | | | ",
963 (unsigned int)(tap->expected_ids[ii]));
972 COMMAND_HANDLER(handle_reset_config_command)
977 /* Original versions cared about the order of these tokens:
978 * reset_config signals [combination [trst_type [srst_type]]]
979 * They also clobbered the previous configuration even on error.
981 * Here we don't care about the order, and only change values
982 * which have been explicitly specified.
984 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
989 m = RESET_SRST_NO_GATING;
990 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
991 /* default: don't use JTAG while SRST asserted */;
992 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
993 tmp = RESET_SRST_NO_GATING;
997 LOG_ERROR("extra reset_config %s spec (%s)",
998 "gating", *CMD_ARGV);
999 return ERROR_INVALID_ARGUMENTS;
1005 m = RESET_HAS_TRST | RESET_HAS_SRST;
1006 if (strcmp(*CMD_ARGV, "none") == 0)
1008 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
1009 tmp = RESET_HAS_TRST;
1010 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
1011 tmp = RESET_HAS_SRST;
1012 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
1013 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
1017 LOG_ERROR("extra reset_config %s spec (%s)",
1018 "signal", *CMD_ARGV);
1019 return ERROR_INVALID_ARGUMENTS;
1024 /* combination (options for broken wiring) */
1025 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1026 if (strcmp(*CMD_ARGV, "separate") == 0)
1027 /* separate reset lines - default */;
1028 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
1029 tmp |= RESET_SRST_PULLS_TRST;
1030 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
1031 tmp |= RESET_TRST_PULLS_SRST;
1032 else if (strcmp(*CMD_ARGV, "combined") == 0)
1033 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1037 LOG_ERROR("extra reset_config %s spec (%s)",
1038 "combination", *CMD_ARGV);
1039 return ERROR_INVALID_ARGUMENTS;
1044 /* trst_type (NOP without HAS_TRST) */
1045 m = RESET_TRST_OPEN_DRAIN;
1046 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
1047 tmp |= RESET_TRST_OPEN_DRAIN;
1048 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
1049 /* push/pull from adapter - default */;
1053 LOG_ERROR("extra reset_config %s spec (%s)",
1054 "trst_type", *CMD_ARGV);
1055 return ERROR_INVALID_ARGUMENTS;
1060 /* srst_type (NOP without HAS_SRST) */
1061 m |= RESET_SRST_PUSH_PULL;
1062 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
1063 tmp |= RESET_SRST_PUSH_PULL;
1064 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
1065 /* open drain from adapter - default */;
1069 LOG_ERROR("extra reset_config %s spec (%s)",
1070 "srst_type", *CMD_ARGV);
1071 return ERROR_INVALID_ARGUMENTS;
1076 /* caller provided nonsense; fail */
1077 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
1078 return ERROR_INVALID_ARGUMENTS;
1081 /* Remember the bits which were specified (mask)
1082 * and their new values (new_cfg).
1088 /* clear previous values of those bits, save new values */
1090 int old_cfg = jtag_get_reset_config();
1094 jtag_set_reset_config(new_cfg);
1096 new_cfg = jtag_get_reset_config();
1100 * Display the (now-)current reset mode
1104 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
1105 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
1106 case RESET_HAS_SRST:
1107 modes[0] = "srst_only";
1109 case RESET_HAS_TRST:
1110 modes[0] = "trst_only";
1112 case RESET_TRST_AND_SRST:
1113 modes[0] = "trst_and_srst";
1120 /* normally SRST and TRST are decoupled; but bugs happen ... */
1121 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
1122 case RESET_SRST_PULLS_TRST:
1123 modes[1] = "srst_pulls_trst";
1125 case RESET_TRST_PULLS_SRST:
1126 modes[1] = "trst_pulls_srst";
1128 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
1129 modes[1] = "combined";
1132 modes[1] = "separate";
1136 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
1137 if (new_cfg & RESET_HAS_TRST) {
1138 if (new_cfg & RESET_TRST_OPEN_DRAIN)
1139 modes[3] = " trst_open_drain";
1141 modes[3] = " trst_push_pull";
1145 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
1146 if (new_cfg & RESET_HAS_SRST) {
1147 if (new_cfg & RESET_SRST_NO_GATING)
1148 modes[2] = " srst_nogate";
1150 modes[2] = " srst_gates_jtag";
1152 if (new_cfg & RESET_SRST_PUSH_PULL)
1153 modes[4] = " srst_push_pull";
1155 modes[4] = " srst_open_drain";
1161 command_print(CMD_CTX, "%s %s%s%s%s",
1163 modes[2], modes[3], modes[4]);
1168 COMMAND_HANDLER(handle_jtag_nsrst_delay_command)
1171 return ERROR_COMMAND_SYNTAX_ERROR;
1175 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1177 jtag_set_nsrst_delay(delay);
1179 command_print(CMD_CTX, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
1183 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1186 return ERROR_COMMAND_SYNTAX_ERROR;
1190 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1192 jtag_set_ntrst_delay(delay);
1194 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1198 COMMAND_HANDLER(handle_jtag_nsrst_assert_width_command)
1201 return ERROR_COMMAND_SYNTAX_ERROR;
1205 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1207 jtag_set_nsrst_assert_width(delay);
1209 command_print(CMD_CTX, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
1213 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1216 return ERROR_COMMAND_SYNTAX_ERROR;
1220 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1222 jtag_set_ntrst_assert_width(delay);
1224 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1228 COMMAND_HANDLER(handle_jtag_khz_command)
1231 return ERROR_COMMAND_SYNTAX_ERROR;
1233 int retval = ERROR_OK;
1237 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1239 retval = jtag_config_khz(khz);
1240 if (ERROR_OK != retval)
1244 int cur_speed = jtag_get_speed_khz();
1245 retval = jtag_get_speed_readable(&cur_speed);
1246 if (ERROR_OK != retval)
1250 command_print(CMD_CTX, "%d kHz", cur_speed);
1252 command_print(CMD_CTX, "RCLK - adaptive");
1257 COMMAND_HANDLER(handle_jtag_rclk_command)
1260 return ERROR_COMMAND_SYNTAX_ERROR;
1262 int retval = ERROR_OK;
1266 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1268 retval = jtag_config_rclk(khz);
1269 if (ERROR_OK != retval)
1273 int cur_khz = jtag_get_speed_khz();
1274 retval = jtag_get_speed_readable(&cur_khz);
1275 if (ERROR_OK != retval)
1279 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1281 command_print(CMD_CTX, "RCLK - adaptive");
1286 COMMAND_HANDLER(handle_jtag_reset_command)
1289 return ERROR_COMMAND_SYNTAX_ERROR;
1292 if (CMD_ARGV[0][0] == '1')
1294 else if (CMD_ARGV[0][0] == '0')
1297 return ERROR_COMMAND_SYNTAX_ERROR;
1300 if (CMD_ARGV[1][0] == '1')
1302 else if (CMD_ARGV[1][0] == '0')
1305 return ERROR_COMMAND_SYNTAX_ERROR;
1307 if (jtag_interface_init(CMD_CTX) != ERROR_OK)
1308 return ERROR_JTAG_INIT_FAILED;
1310 jtag_add_reset(trst, srst);
1311 return jtag_execute_queue();
1314 COMMAND_HANDLER(handle_runtest_command)
1317 return ERROR_COMMAND_SYNTAX_ERROR;
1319 unsigned num_clocks;
1320 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1322 jtag_add_runtest(num_clocks, TAP_IDLE);
1323 return jtag_execute_queue();
1327 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1328 * should be stable ... and *NOT* a shift state, otherwise free-running
1329 * jtag clocks could change the values latched by the update state.
1330 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1331 * and "drscan" commands are a write-only subset of what SVF provides.
1334 COMMAND_HANDLER(handle_irscan_command)
1337 struct scan_field *fields;
1338 struct jtag_tap *tap;
1339 tap_state_t endstate;
1341 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1343 return ERROR_COMMAND_SYNTAX_ERROR;
1346 /* optional "-endstate" "statename" at the end of the arguments,
1347 * so that e.g. IRPAUSE can let us load the data register before
1348 * entering RUN/IDLE to execute the instruction we load here.
1350 endstate = TAP_IDLE;
1352 if (CMD_ARGC >= 4) {
1353 /* have at least one pair of numbers. */
1354 /* is last pair the magic text? */
1355 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1356 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1357 if (endstate == TAP_INVALID)
1358 return ERROR_COMMAND_SYNTAX_ERROR;
1359 if (!scan_is_safe(endstate))
1360 LOG_WARNING("unstable irscan endstate \"%s\"",
1361 CMD_ARGV[CMD_ARGC - 1]);
1366 int num_fields = CMD_ARGC / 2;
1367 size_t fields_len = sizeof(struct scan_field) * num_fields;
1368 fields = malloc(fields_len);
1369 memset(fields, 0, fields_len);
1372 for (i = 0; i < num_fields; i++)
1374 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1378 for (j = 0; j < i; j++)
1379 free(fields[j].out_value);
1381 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1385 int field_size = tap->ir_length;
1386 fields[i].tap = tap;
1387 fields[i].num_bits = field_size;
1388 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1391 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1392 if (ERROR_OK != retval)
1394 buf_set_u32(fields[i].out_value, 0, field_size, value);
1395 fields[i].in_value = NULL;
1398 /* did we have an endstate? */
1399 jtag_add_ir_scan(num_fields, fields, endstate);
1401 retval = jtag_execute_queue();
1404 for (i = 0; i < num_fields; i++)
1406 if (NULL != fields[i].out_value)
1407 free(fields[i].out_value);
1416 COMMAND_HANDLER(handle_verify_ircapture_command)
1419 return ERROR_COMMAND_SYNTAX_ERROR;
1424 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1425 jtag_set_verify_capture_ir(enable);
1428 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1429 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1434 COMMAND_HANDLER(handle_verify_jtag_command)
1437 return ERROR_COMMAND_SYNTAX_ERROR;
1442 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1443 jtag_set_verify(enable);
1446 const char *status = jtag_will_verify() ? "enabled": "disabled";
1447 command_print(CMD_CTX, "verify jtag capture is %s", status);
1452 COMMAND_HANDLER(handle_tms_sequence_command)
1455 return ERROR_COMMAND_SYNTAX_ERROR;
1460 if (strcmp(CMD_ARGV[0], "short") == 0)
1461 use_new_table = true;
1462 else if (strcmp(CMD_ARGV[0], "long") == 0)
1463 use_new_table = false;
1465 return ERROR_COMMAND_SYNTAX_ERROR;
1467 tap_use_new_tms_table(use_new_table);
1470 command_print(CMD_CTX, "tms sequence is %s",
1471 tap_uses_new_tms_table() ? "short": "long");
1476 static const struct command_registration jtag_command_handlers[] = {
1478 .name = "interface",
1479 .handler = &handle_interface_command,
1480 .mode = COMMAND_CONFIG,
1481 .help = "select a JTAG interface",
1482 .usage = "<driver_name>",
1485 .name = "interface_list",
1486 .handler = &handle_interface_list_command,
1487 .mode = COMMAND_ANY,
1488 .help = "list all built-in interfaces",
1492 .handler = &handle_jtag_khz_command,
1493 .mode = COMMAND_ANY,
1494 .help = "set maximum jtag speed (if supported)",
1495 .usage = "<khz:0=rtck>",
1498 .name = "jtag_rclk",
1499 .handler = &handle_jtag_rclk_command,
1500 .mode = COMMAND_ANY,
1501 .help = "set JTAG speed to RCLK or use fallback speed",
1502 .usage = "<fallback_speed_khz>",
1505 .name = "reset_config",
1506 .handler = &handle_reset_config_command,
1507 .mode = COMMAND_ANY,
1508 .help = "configure JTAG reset behavior",
1509 .usage = "[none|trst_only|srst_only|trst_and_srst] "
1510 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
1511 "[srst_gates_jtag|srst_nogate] "
1512 "[trst_push_pull|trst_open_drain] "
1513 "[srst_push_pull|srst_open_drain]",
1516 .name = "jtag_nsrst_delay",
1517 .handler = &handle_jtag_nsrst_delay_command,
1518 .mode = COMMAND_ANY,
1519 .help = "delay after deasserting srst in ms",
1523 .name = "jtag_ntrst_delay",
1524 .handler = &handle_jtag_ntrst_delay_command,
1525 .mode = COMMAND_ANY,
1526 .help = "delay after deasserting trst in ms",
1530 .name = "jtag_nsrst_assert_width",
1531 .handler = &handle_jtag_nsrst_assert_width_command,
1532 .mode = COMMAND_ANY,
1533 .help = "delay after asserting srst in ms",
1537 .name = "jtag_ntrst_assert_width",
1538 .handler = &handle_jtag_ntrst_assert_width_command,
1539 .mode = COMMAND_ANY,
1540 .help = "delay after asserting trst in ms",
1544 .name = "scan_chain",
1545 .handler = &handle_scan_chain_command,
1546 .mode = COMMAND_EXEC,
1547 .help = "print current scan chain configuration",
1550 .name = "jtag_reset",
1551 .handler = &handle_jtag_reset_command,
1552 .mode = COMMAND_EXEC,
1553 .help = "toggle reset lines",
1554 .usage = "<trst> <srst>",
1558 .handler = &handle_runtest_command,
1559 .mode = COMMAND_EXEC,
1560 .help = "move to Run-Test/Idle, and execute <num_cycles>",
1561 .usage = "<num_cycles>"
1565 .handler = &handle_irscan_command,
1566 .mode = COMMAND_EXEC,
1567 .help = "execute IR scan",
1568 .usage = "<device> <instr> [dev2] [instr2] ...",
1571 .name = "verify_ircapture",
1572 .handler = &handle_verify_ircapture_command,
1573 .mode = COMMAND_ANY,
1574 .help = "verify value captured during Capture-IR",
1575 .usage = "<enable | disable>",
1578 .name = "verify_jtag",
1579 .handler = &handle_verify_jtag_command,
1580 .mode = COMMAND_ANY,
1581 .help = "verify value capture",
1582 .usage = "<enable | disable>",
1585 .name = "tms_sequence",
1586 .handler = &handle_tms_sequence_command,
1587 .mode = COMMAND_ANY,
1588 .help = "choose short(default) or long tms_sequence",
1589 .usage = "<short | long>",
1593 .mode = COMMAND_ANY,
1594 .help = "perform jtag tap actions",
1596 .chain = jtag_subcommand_handlers,
1599 .chain = jtag_command_handlers_to_move,
1601 COMMAND_REGISTRATION_DONE
1603 int jtag_register_commands(struct command_context *cmd_ctx)
1605 return register_commands(cmd_ctx, NULL, jtag_command_handlers);