rename jtag_nsrst_assert_width as adapter_nsrst_assert_width
[openocd.git] / src / jtag / tcl.c
blob686eb3e89045261e0fb5165a8ceaa5385c0581e2
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2009 SoftPLC Corporation *
9 * http://softplc.com *
10 * dick@softplc.com *
11 * *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
14 * *
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. *
19 * *
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. *
24 * *
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 ***************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
34 #include "jtag.h"
35 #include "minidriver.h"
36 #include "interface.h"
37 #include "interfaces.h"
39 #ifdef HAVE_STRINGS_H
40 #include <strings.h>
41 #endif
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 struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
56 const char *cp = Jim_GetString(o, NULL);
57 struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
58 if (NULL == cp)
59 cp = "(unknown)";
60 if (NULL == t)
61 Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp);
62 return t;
65 static bool scan_is_safe(tap_state_t state)
67 switch (state)
69 case TAP_RESET:
70 case TAP_IDLE:
71 case TAP_DRPAUSE:
72 case TAP_IRPAUSE:
73 return true;
74 default:
75 return false;
79 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
81 int retval;
82 struct scan_field *fields;
83 int num_fields;
84 int field_count = 0;
85 int i, e;
86 struct jtag_tap *tap;
87 tap_state_t endstate;
89 /* args[1] = device
90 * args[2] = num_bits
91 * args[3] = hex string
92 * ... repeat num bits and hex string ...
94 * .. optionally:
95 * args[N-2] = "-endstate"
96 * args[N-1] = statename
98 if ((argc < 4) || ((argc % 2) != 0))
100 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
101 return JIM_ERR;
104 endstate = TAP_IDLE;
106 script_debug(interp, "drscan", argc, args);
108 /* validate arguments as numbers */
109 e = JIM_OK;
110 for (i = 2; i < argc; i += 2)
112 long bits;
113 const char *cp;
115 e = Jim_GetLong(interp, args[i], &bits);
116 /* If valid - try next arg */
117 if (e == JIM_OK) {
118 continue;
121 /* Not valid.. are we at the end? */
122 if (((i + 2) != argc)) {
123 /* nope, then error */
124 return e;
127 /* it could be: "-endstate FOO"
128 * e.g. DRPAUSE so we can issue more instructions
129 * before entering RUN/IDLE and executing them.
132 /* get arg as a string. */
133 cp = Jim_GetString(args[i], NULL);
134 /* is it the magic? */
135 if (0 == strcmp("-endstate", cp)) {
136 /* is the statename valid? */
137 cp = Jim_GetString(args[i + 1], NULL);
139 /* see if it is a valid state name */
140 endstate = tap_state_by_name(cp);
141 if (endstate < 0) {
142 /* update the error message */
143 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
144 } else {
145 if (!scan_is_safe(endstate))
146 LOG_WARNING("drscan with unsafe "
147 "endstate \"%s\"", cp);
149 /* valid - so clear the error */
150 e = JIM_OK;
151 /* and remove the last 2 args */
152 argc -= 2;
156 /* Still an error? */
157 if (e != JIM_OK) {
158 return e; /* too bad */
160 } /* validate args */
162 tap = jtag_tap_by_jim_obj(interp, args[1]);
163 if (tap == NULL) {
164 return JIM_ERR;
167 num_fields = (argc-2)/2;
168 fields = malloc(sizeof(struct scan_field) * num_fields);
169 for (i = 2; i < argc; i += 2)
171 long bits;
172 int len;
173 const char *str;
175 Jim_GetLong(interp, args[i], &bits);
176 str = Jim_GetString(args[i + 1], &len);
178 fields[field_count].num_bits = bits;
179 fields[field_count].out_value = malloc(DIV_ROUND_UP(bits, 8));
180 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
181 fields[field_count].in_value = fields[field_count].out_value;
182 field_count++;
185 jtag_add_dr_scan(tap, num_fields, fields, endstate);
187 retval = jtag_execute_queue();
188 if (retval != ERROR_OK)
190 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
191 return JIM_ERR;
194 field_count = 0;
195 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
196 for (i = 2; i < argc; i += 2)
198 long bits;
199 char *str;
201 Jim_GetLong(interp, args[i], &bits);
202 str = buf_to_str(fields[field_count].in_value, bits, 16);
203 free(fields[field_count].out_value);
205 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
206 free(str);
207 field_count++;
210 Jim_SetResult(interp, list);
212 free(fields);
214 return JIM_OK;
218 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
220 tap_state_t states[8];
222 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
224 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
225 return JIM_ERR;
228 script_debug(interp, "pathmove", argc, args);
230 int i;
231 for (i = 0; i < argc-1; i++)
233 const char *cp;
234 cp = Jim_GetString(args[i + 1], NULL);
235 states[i] = tap_state_by_name(cp);
236 if (states[i] < 0)
238 /* update the error message */
239 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
240 return JIM_ERR;
244 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
246 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
247 return JIM_ERR;
250 jtag_add_pathmove(argc-2, states + 1);
252 if (jtag_execute_queue()!= ERROR_OK)
254 Jim_SetResultString(interp, "pathmove: failed",-1);
255 return JIM_ERR;
258 return JIM_OK;
262 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
264 script_debug(interp, "flush_count", argc, args);
266 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
268 return JIM_OK;
271 /* REVISIT Just what about these should "move" ... ?
272 * These registrations, into the main JTAG table?
274 * There's a minor compatibility issue, these all show up twice;
275 * that's not desirable:
276 * - jtag drscan ... NOT DOCUMENTED!
277 * - drscan ...
279 * The "irscan" command (for example) doesn't show twice.
281 static const struct command_registration jtag_command_handlers_to_move[] = {
283 .name = "drscan",
284 .mode = COMMAND_EXEC,
285 .jim_handler = Jim_Command_drscan,
286 .help = "Execute Data Register (DR) scan for one TAP. "
287 "Other TAPs must be in BYPASS mode.",
288 .usage = "tap_name [num_bits value]* ['-endstate' state_name]",
291 .name = "flush_count",
292 .mode = COMMAND_EXEC,
293 .jim_handler = Jim_Command_flush_count,
294 .help = "Returns the number of times the JTAG queue "
295 "has been flushed.",
298 .name = "pathmove",
299 .mode = COMMAND_EXEC,
300 .jim_handler = Jim_Command_pathmove,
301 .usage = "start_state state1 [state2 [state3 ...]]",
302 .help = "Move JTAG state machine from current state "
303 "(start_state) to state1, then state2, state3, etc.",
305 COMMAND_REGISTRATION_DONE
309 enum jtag_tap_cfg_param {
310 JCFG_EVENT
313 static Jim_Nvp nvp_config_opts[] = {
314 { .name = "-event", .value = JCFG_EVENT },
316 { .name = NULL, .value = -1 }
319 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
321 if (goi->argc == 0)
323 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
324 return JIM_ERR;
327 Jim_Nvp *n;
328 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
329 if (e != JIM_OK)
331 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
332 return e;
335 if (goi->isconfigure) {
336 if (goi->argc != 1) {
337 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
338 return JIM_ERR;
340 } else {
341 if (goi->argc != 0) {
342 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
343 return JIM_ERR;
347 struct jtag_tap_event_action *jteap = tap->event_action;
348 /* replace existing event body */
349 bool found = false;
350 while (jteap)
352 if (jteap->event == (enum jtag_event)n->value)
354 found = true;
355 break;
357 jteap = jteap->next;
360 Jim_SetEmptyResult(goi->interp);
362 if (goi->isconfigure)
364 if (!found)
365 jteap = calloc(1, sizeof(*jteap));
366 else if (NULL != jteap->body)
367 Jim_DecrRefCount(goi->interp, jteap->body);
369 jteap->interp = goi->interp;
370 jteap->event = n->value;
372 Jim_Obj *o;
373 Jim_GetOpt_Obj(goi, &o);
374 jteap->body = Jim_DuplicateObj(goi->interp, o);
375 Jim_IncrRefCount(jteap->body);
377 if (!found)
379 /* add to head of event list */
380 jteap->next = tap->event_action;
381 tap->event_action = jteap;
384 else if (found)
386 jteap->interp = goi->interp;
387 Jim_SetResult(goi->interp,
388 Jim_DuplicateObj(goi->interp, jteap->body));
390 return JIM_OK;
393 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
395 /* parse config or cget options */
396 while (goi->argc > 0)
398 Jim_SetEmptyResult (goi->interp);
400 Jim_Nvp *n;
401 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
402 if (e != JIM_OK)
404 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
405 return e;
408 switch (n->value)
410 case JCFG_EVENT:
411 e = jtag_tap_configure_event(goi, tap);
412 if (e != JIM_OK)
413 return e;
414 break;
415 default:
416 Jim_SetResult_sprintf(goi->interp, "unknown event: %s", n->name);
417 return JIM_ERR;
421 return JIM_OK;
424 static int is_bad_irval(int ir_length, jim_wide w)
426 jim_wide v = 1;
428 v <<= ir_length;
429 v -= 1;
430 v = ~v;
431 return (w & v) != 0;
434 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
435 struct jtag_tap *pTap)
437 jim_wide w;
438 int e = Jim_GetOpt_Wide(goi, &w);
439 if (e != JIM_OK) {
440 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
441 return e;
444 unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
445 uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
446 if (new_expected_ids == NULL)
448 Jim_SetResult_sprintf(goi->interp, "no memory");
449 return JIM_ERR;
452 memcpy(new_expected_ids, pTap->expected_ids, expected_len);
454 new_expected_ids[pTap->expected_ids_cnt] = w;
456 free(pTap->expected_ids);
457 pTap->expected_ids = new_expected_ids;
458 pTap->expected_ids_cnt++;
460 return JIM_OK;
463 #define NTAP_OPT_IRLEN 0
464 #define NTAP_OPT_IRMASK 1
465 #define NTAP_OPT_IRCAPTURE 2
466 #define NTAP_OPT_ENABLED 3
467 #define NTAP_OPT_DISABLED 4
468 #define NTAP_OPT_EXPECTED_ID 5
469 #define NTAP_OPT_VERSION 6
471 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
472 struct jtag_tap *pTap)
474 jim_wide w;
475 int e = Jim_GetOpt_Wide(goi, &w);
476 if (e != JIM_OK)
478 Jim_SetResult_sprintf(goi->interp,
479 "option: %s bad parameter", n->name);
480 free((void *)pTap->dotted_name);
481 return e;
483 switch (n->value) {
484 case NTAP_OPT_IRLEN:
485 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
487 LOG_WARNING("%s: huge IR length %d",
488 pTap->dotted_name, (int) w);
490 pTap->ir_length = w;
491 break;
492 case NTAP_OPT_IRMASK:
493 if (is_bad_irval(pTap->ir_length, w))
495 LOG_ERROR("%s: IR mask %x too big",
496 pTap->dotted_name,
497 (int) w);
498 return JIM_ERR;
500 if ((w & 3) != 3)
501 LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
502 pTap->ir_capture_mask = w;
503 break;
504 case NTAP_OPT_IRCAPTURE:
505 if (is_bad_irval(pTap->ir_length, w))
507 LOG_ERROR("%s: IR capture %x too big",
508 pTap->dotted_name, (int) w);
509 return JIM_ERR;
511 if ((w & 3) != 1)
512 LOG_WARNING("%s: nonstandard IR value",
513 pTap->dotted_name);
514 pTap->ir_capture_value = w;
515 break;
516 default:
517 return JIM_ERR;
519 return JIM_OK;
522 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
524 struct jtag_tap *pTap;
525 int x;
526 int e;
527 Jim_Nvp *n;
528 char *cp;
529 const Jim_Nvp opts[] = {
530 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
531 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
532 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
533 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
534 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
535 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
536 { .name = "-ignore-version" , .value = NTAP_OPT_VERSION },
537 { .name = NULL , .value = -1 },
540 pTap = calloc(1, sizeof(struct jtag_tap));
541 if (!pTap) {
542 Jim_SetResult_sprintf(goi->interp, "no memory");
543 return JIM_ERR;
547 * we expect CHIP + TAP + OPTIONS
548 * */
549 if (goi->argc < 3) {
550 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
551 free(pTap);
552 return JIM_ERR;
554 Jim_GetOpt_String(goi, &cp, NULL);
555 pTap->chip = strdup(cp);
557 Jim_GetOpt_String(goi, &cp, NULL);
558 pTap->tapname = strdup(cp);
560 /* name + dot + name + null */
561 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
562 cp = malloc(x);
563 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
564 pTap->dotted_name = cp;
566 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
567 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
569 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
570 * that the default. The "-irlen" and "-irmask" options are only
571 * needed to cope with nonstandard TAPs, or to specify more bits.
573 pTap->ir_capture_mask = 0x03;
574 pTap->ir_capture_value = 0x01;
576 while (goi->argc) {
577 e = Jim_GetOpt_Nvp(goi, opts, &n);
578 if (e != JIM_OK) {
579 Jim_GetOpt_NvpUnknown(goi, opts, 0);
580 free((void *)pTap->dotted_name);
581 free(pTap);
582 return e;
584 LOG_DEBUG("Processing option: %s", n->name);
585 switch (n->value) {
586 case NTAP_OPT_ENABLED:
587 pTap->disabled_after_reset = false;
588 break;
589 case NTAP_OPT_DISABLED:
590 pTap->disabled_after_reset = true;
591 break;
592 case NTAP_OPT_EXPECTED_ID:
593 e = jim_newtap_expected_id(n, goi, pTap);
594 if (JIM_OK != e)
596 free((void *)pTap->dotted_name);
597 free(pTap);
598 return e;
600 break;
601 case NTAP_OPT_IRLEN:
602 case NTAP_OPT_IRMASK:
603 case NTAP_OPT_IRCAPTURE:
604 e = jim_newtap_ir_param(n, goi, pTap);
605 if (JIM_OK != e)
607 free((void *)pTap->dotted_name);
608 free(pTap);
609 return e;
611 break;
612 case NTAP_OPT_VERSION:
613 pTap->ignore_version = true;
614 break;
615 } /* switch (n->value) */
616 } /* while (goi->argc) */
618 /* default is enabled-after-reset */
619 pTap->enabled = !pTap->disabled_after_reset;
621 /* Did all the required option bits get cleared? */
622 if (pTap->ir_length != 0)
624 jtag_tap_init(pTap);
625 return JIM_OK;
628 Jim_SetResult_sprintf(goi->interp,
629 "newtap: %s missing IR length",
630 pTap->dotted_name);
631 jtag_tap_free(pTap);
632 return JIM_ERR;
635 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
637 struct jtag_tap_event_action * jteap;
639 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
641 if (jteap->event != e)
642 continue;
644 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
645 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
646 tap->dotted_name, e, nvp->name,
647 Jim_GetString(jteap->body, NULL));
649 if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK)
651 Jim_PrintErrorMessage(jteap->interp);
652 continue;
655 switch (e)
657 case JTAG_TAP_EVENT_ENABLE:
658 case JTAG_TAP_EVENT_DISABLE:
659 /* NOTE: we currently assume the handlers
660 * can't fail. Right here is where we should
661 * really be verifying the scan chains ...
663 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
664 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
665 tap->enabled ? "enabled" : "disabled");
666 break;
667 default:
668 break;
673 static int
674 jim_jtag_interface(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
676 Jim_GetOptInfo goi;
677 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
679 /* return the name of the interface */
680 /* TCL code might need to know the exact type... */
681 /* FUTURE: we allow this as a means to "set" the interface. */
682 if (goi.argc != 0) {
683 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
684 return JIM_ERR;
686 const char *name = jtag_interface ? jtag_interface->name : NULL;
687 Jim_SetResultString(goi.interp, name ? : "undefined", -1);
688 return JIM_OK;
691 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
693 Jim_GetOptInfo goi;
694 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
695 if (goi.argc != 0) {
696 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
697 return JIM_ERR;
699 struct command_context *context = Jim_GetAssocData(interp, "context");
700 int e = jtag_init_inner(context);
701 if (e != ERROR_OK) {
702 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
703 return JIM_ERR;
705 return JIM_OK;
708 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
710 Jim_GetOptInfo goi;
711 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
712 if (goi.argc != 0) {
713 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
714 return JIM_ERR;
716 struct command_context *context = Jim_GetAssocData(interp, "context");
717 int e = jtag_init_reset(context);
718 if (e != ERROR_OK) {
719 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
720 return JIM_ERR;
722 return JIM_OK;
725 static int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
727 Jim_GetOptInfo goi;
728 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
729 return jim_newtap_cmd(&goi);
732 static bool jtag_tap_enable(struct jtag_tap *t)
734 if (t->enabled)
735 return false;
736 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
737 if (!t->enabled)
738 return false;
740 /* FIXME add JTAG sanity checks, w/o TLR
741 * - scan chain length grew by one (this)
742 * - IDs and IR lengths are as expected
744 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
745 return true;
747 static bool jtag_tap_disable(struct jtag_tap *t)
749 if (!t->enabled)
750 return false;
751 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
752 if (t->enabled)
753 return false;
755 /* FIXME add JTAG sanity checks, w/o TLR
756 * - scan chain length shrank by one (this)
757 * - IDs and IR lengths are as expected
759 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
760 return true;
763 static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
765 const char *cmd_name = Jim_GetString(argv[0], NULL);
766 Jim_GetOptInfo goi;
767 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
768 if (goi.argc != 1) {
769 Jim_SetResult_sprintf(goi.interp, "usage: %s <name>", cmd_name);
770 return JIM_ERR;
773 struct jtag_tap *t;
775 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
776 if (t == NULL)
777 return JIM_ERR;
779 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
780 // do nothing, just return the value
781 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
782 if (!jtag_tap_enable(t))
783 LOG_WARNING("failed to disable tap");
784 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
785 if (!jtag_tap_disable(t))
786 LOG_WARNING("failed to disable tap");
787 } else {
788 LOG_ERROR("command '%s' unknown", cmd_name);
789 return JIM_ERR;
791 bool e = t->enabled;
792 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
793 return JIM_OK;
796 static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
798 const char *cmd_name = Jim_GetString(argv[0], NULL);
799 Jim_GetOptInfo goi;
800 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
801 goi.isconfigure = !strcmp(cmd_name, "configure");
802 if (goi.argc < 2 + goi.isconfigure) {
803 Jim_WrongNumArgs(goi.interp, 0, NULL,
804 "<tap_name> <attribute> ...");
805 return JIM_ERR;
808 struct jtag_tap *t;
810 Jim_Obj *o;
811 Jim_GetOpt_Obj(&goi, &o);
812 t = jtag_tap_by_jim_obj(goi.interp, o);
813 if (t == NULL) {
814 return JIM_ERR;
817 return jtag_tap_configure_cmd(&goi, t);
820 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
822 Jim_GetOptInfo goi;
823 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
824 if (goi.argc != 0) {
825 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
826 return JIM_ERR;
828 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
829 struct jtag_tap *tap;
831 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
832 Jim_ListAppendElement(goi.interp,
833 Jim_GetResult(goi.interp),
834 Jim_NewStringObj(goi.interp,
835 tap->dotted_name, -1));
837 return JIM_OK;
840 COMMAND_HANDLER(handle_jtag_init_command)
842 if (CMD_ARGC != 0)
843 return ERROR_COMMAND_SYNTAX_ERROR;
845 static bool jtag_initialized = false;
846 if (jtag_initialized)
848 LOG_INFO("'jtag init' has already been called");
849 return ERROR_OK;
851 jtag_initialized = true;
853 LOG_DEBUG("Initializing jtag devices...");
854 return jtag_init(CMD_CTX);
857 static const struct command_registration jtag_subcommand_handlers[] = {
859 .name = "init",
860 .mode = COMMAND_ANY,
861 .handler = handle_jtag_init_command,
862 .help = "initialize jtag scan chain",
865 .name = "interface",
866 .mode = COMMAND_ANY,
867 .jim_handler = jim_jtag_interface,
868 .help = "Returns the name of the currently selected interface.",
871 .name = "arp_init",
872 .mode = COMMAND_ANY,
873 .jim_handler = jim_jtag_arp_init,
874 .help = "Validates JTAG scan chain against the list of "
875 "declared TAPs using just the four standard JTAG "
876 "signals.",
879 .name = "arp_init-reset",
880 .mode = COMMAND_ANY,
881 .jim_handler = jim_jtag_arp_init_reset,
882 .help = "Uses TRST and SRST to try resetting everything on "
883 "the JTAG scan chain, then performs 'jtag arp_init'."
886 .name = "newtap",
887 .mode = COMMAND_CONFIG,
888 .jim_handler = jim_jtag_newtap,
889 .help = "Create a new TAP instance named basename.tap_type, "
890 "and appends it to the scan chain.",
891 .usage = "basename tap_type '-irlen' count "
892 "['-enable'|'-disable'] "
893 "['-expected_id' number] "
894 "['-ignore-version'] "
895 "['-ircapture' number] "
896 "['-mask' number] ",
899 .name = "tapisenabled",
900 .mode = COMMAND_EXEC,
901 .jim_handler = jim_jtag_tap_enabler,
902 .help = "Returns a Tcl boolean (0/1) indicating whether "
903 "the TAP is enabled (1) or not (0).",
904 .usage = "tap_name",
907 .name = "tapenable",
908 .mode = COMMAND_EXEC,
909 .jim_handler = jim_jtag_tap_enabler,
910 .help = "Try to enable the specified TAP using the "
911 "'tap-enable' TAP event.",
912 .usage = "tap_name",
915 .name = "tapdisable",
916 .mode = COMMAND_EXEC,
917 .jim_handler = jim_jtag_tap_enabler,
918 .help = "Try to disable the specified TAP using the "
919 "'tap-disable' TAP event.",
920 .usage = "tap_name",
923 .name = "configure",
924 .mode = COMMAND_EXEC,
925 .jim_handler = jim_jtag_configure,
926 .help = "Provide a Tcl handler for the specified "
927 "TAP event.",
928 .usage = "tap_name '-event' event_name handler",
931 .name = "cget",
932 .mode = COMMAND_EXEC,
933 .jim_handler = jim_jtag_configure,
934 .help = "Return any Tcl handler for the specified "
935 "TAP event.",
936 .usage = "tap_name '-event' event_name",
939 .name = "names",
940 .mode = COMMAND_ANY,
941 .jim_handler = jim_jtag_names,
942 .help = "Returns list of all JTAG tap names.",
945 .chain = jtag_command_handlers_to_move,
947 COMMAND_REGISTRATION_DONE
950 void jtag_notify_event(enum jtag_event event)
952 struct jtag_tap *tap;
954 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
955 jtag_tap_handle_event(tap, event);
959 static int default_khz(int khz, int *jtag_speed)
961 LOG_ERROR("Translation from khz to jtag_speed not implemented");
962 return ERROR_FAIL;
965 static int default_speed_div(int speed, int *khz)
967 LOG_ERROR("Translation from jtag_speed to khz not implemented");
968 return ERROR_FAIL;
971 static int default_power_dropout(int *dropout)
973 *dropout = 0; /* by default we can't detect power dropout */
974 return ERROR_OK;
977 static int default_srst_asserted(int *srst_asserted)
979 *srst_asserted = 0; /* by default we can't detect srst asserted */
980 return ERROR_OK;
983 COMMAND_HANDLER(handle_interface_list_command)
985 if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
986 return ERROR_COMMAND_SYNTAX_ERROR;
988 command_print(CMD_CTX, "The following debug interfaces are available:");
989 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
991 const char *name = jtag_interfaces[i]->name;
992 command_print(CMD_CTX, "%u: %s", i + 1, name);
995 return ERROR_OK;
998 COMMAND_HANDLER(handle_interface_command)
1000 /* check whether the interface is already configured */
1001 if (jtag_interface)
1003 LOG_WARNING("Interface already configured, ignoring");
1004 return ERROR_OK;
1007 /* interface name is a mandatory argument */
1008 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
1009 return ERROR_COMMAND_SYNTAX_ERROR;
1011 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
1013 if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0)
1014 continue;
1016 if (NULL != jtag_interfaces[i]->commands)
1018 int retval = register_commands(CMD_CTX, NULL,
1019 jtag_interfaces[i]->commands);
1020 if (ERROR_OK != retval)
1021 return retval;
1024 jtag_interface = jtag_interfaces[i];
1026 if (jtag_interface->khz == NULL)
1027 jtag_interface->khz = default_khz;
1028 if (jtag_interface->speed_div == NULL)
1029 jtag_interface->speed_div = default_speed_div;
1030 if (jtag_interface->power_dropout == NULL)
1031 jtag_interface->power_dropout = default_power_dropout;
1032 if (jtag_interface->srst_asserted == NULL)
1033 jtag_interface->srst_asserted = default_srst_asserted;
1035 return ERROR_OK;
1038 /* no valid interface was found (i.e. the configuration option,
1039 * didn't match one of the compiled-in interfaces
1041 LOG_ERROR("The specified debug interface was not found (%s)", CMD_ARGV[0]);
1042 CALL_COMMAND_HANDLER(handle_interface_list_command);
1043 return ERROR_JTAG_INVALID_INTERFACE;
1046 COMMAND_HANDLER(handle_scan_chain_command)
1048 struct jtag_tap *tap;
1049 char expected_id[12];
1051 tap = jtag_all_taps();
1052 command_print(CMD_CTX,
1053 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
1054 command_print(CMD_CTX,
1055 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
1057 while (tap) {
1058 uint32_t expected, expected_mask, ii;
1060 snprintf(expected_id, sizeof expected_id, "0x%08x",
1061 (unsigned)((tap->expected_ids_cnt > 0)
1062 ? tap->expected_ids[0]
1063 : 0));
1064 if (tap->ignore_version)
1065 expected_id[2] = '*';
1067 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
1068 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
1070 command_print(CMD_CTX,
1071 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
1072 tap->abs_chain_position,
1073 tap->dotted_name,
1074 tap->enabled ? 'Y' : 'n',
1075 (unsigned int)(tap->idcode),
1076 expected_id,
1077 (unsigned int)(tap->ir_length),
1078 (unsigned int)(expected),
1079 (unsigned int)(expected_mask));
1081 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
1082 snprintf(expected_id, sizeof expected_id, "0x%08x",
1083 (unsigned) tap->expected_ids[1]);
1084 if (tap->ignore_version)
1085 expected_id[2] = '*';
1087 command_print(CMD_CTX,
1088 " %s",
1089 expected_id);
1092 tap = tap->next_tap;
1095 return ERROR_OK;
1098 COMMAND_HANDLER(handle_reset_config_command)
1100 int new_cfg = 0;
1101 int mask = 0;
1103 /* Original versions cared about the order of these tokens:
1104 * reset_config signals [combination [trst_type [srst_type]]]
1105 * They also clobbered the previous configuration even on error.
1107 * Here we don't care about the order, and only change values
1108 * which have been explicitly specified.
1110 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
1111 int tmp = 0;
1112 int m;
1114 /* gating */
1115 m = RESET_SRST_NO_GATING;
1116 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
1117 /* default: don't use JTAG while SRST asserted */;
1118 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
1119 tmp = RESET_SRST_NO_GATING;
1120 else
1121 m = 0;
1122 if (mask & m) {
1123 LOG_ERROR("extra reset_config %s spec (%s)",
1124 "gating", *CMD_ARGV);
1125 return ERROR_INVALID_ARGUMENTS;
1127 if (m)
1128 goto next;
1130 /* signals */
1131 m = RESET_HAS_TRST | RESET_HAS_SRST;
1132 if (strcmp(*CMD_ARGV, "none") == 0)
1133 tmp = RESET_NONE;
1134 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
1135 tmp = RESET_HAS_TRST;
1136 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
1137 tmp = RESET_HAS_SRST;
1138 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
1139 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
1140 else
1141 m = 0;
1142 if (mask & m) {
1143 LOG_ERROR("extra reset_config %s spec (%s)",
1144 "signal", *CMD_ARGV);
1145 return ERROR_INVALID_ARGUMENTS;
1147 if (m)
1148 goto next;
1150 /* combination (options for broken wiring) */
1151 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1152 if (strcmp(*CMD_ARGV, "separate") == 0)
1153 /* separate reset lines - default */;
1154 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
1155 tmp |= RESET_SRST_PULLS_TRST;
1156 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
1157 tmp |= RESET_TRST_PULLS_SRST;
1158 else if (strcmp(*CMD_ARGV, "combined") == 0)
1159 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1160 else
1161 m = 0;
1162 if (mask & m) {
1163 LOG_ERROR("extra reset_config %s spec (%s)",
1164 "combination", *CMD_ARGV);
1165 return ERROR_INVALID_ARGUMENTS;
1167 if (m)
1168 goto next;
1170 /* trst_type (NOP without HAS_TRST) */
1171 m = RESET_TRST_OPEN_DRAIN;
1172 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
1173 tmp |= RESET_TRST_OPEN_DRAIN;
1174 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
1175 /* push/pull from adapter - default */;
1176 else
1177 m = 0;
1178 if (mask & m) {
1179 LOG_ERROR("extra reset_config %s spec (%s)",
1180 "trst_type", *CMD_ARGV);
1181 return ERROR_INVALID_ARGUMENTS;
1183 if (m)
1184 goto next;
1186 /* srst_type (NOP without HAS_SRST) */
1187 m |= RESET_SRST_PUSH_PULL;
1188 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
1189 tmp |= RESET_SRST_PUSH_PULL;
1190 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
1191 /* open drain from adapter - default */;
1192 else
1193 m = 0;
1194 if (mask & m) {
1195 LOG_ERROR("extra reset_config %s spec (%s)",
1196 "srst_type", *CMD_ARGV);
1197 return ERROR_INVALID_ARGUMENTS;
1199 if (m)
1200 goto next;
1202 /* caller provided nonsense; fail */
1203 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
1204 return ERROR_INVALID_ARGUMENTS;
1206 next:
1207 /* Remember the bits which were specified (mask)
1208 * and their new values (new_cfg).
1210 mask |= m;
1211 new_cfg |= tmp;
1214 /* clear previous values of those bits, save new values */
1215 if (mask) {
1216 int old_cfg = jtag_get_reset_config();
1218 old_cfg &= ~mask;
1219 new_cfg |= old_cfg;
1220 jtag_set_reset_config(new_cfg);
1221 } else
1222 new_cfg = jtag_get_reset_config();
1226 * Display the (now-)current reset mode
1228 char *modes[5];
1230 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
1231 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
1232 case RESET_HAS_SRST:
1233 modes[0] = "srst_only";
1234 break;
1235 case RESET_HAS_TRST:
1236 modes[0] = "trst_only";
1237 break;
1238 case RESET_TRST_AND_SRST:
1239 modes[0] = "trst_and_srst";
1240 break;
1241 default:
1242 modes[0] = "none";
1243 break;
1246 /* normally SRST and TRST are decoupled; but bugs happen ... */
1247 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
1248 case RESET_SRST_PULLS_TRST:
1249 modes[1] = "srst_pulls_trst";
1250 break;
1251 case RESET_TRST_PULLS_SRST:
1252 modes[1] = "trst_pulls_srst";
1253 break;
1254 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
1255 modes[1] = "combined";
1256 break;
1257 default:
1258 modes[1] = "separate";
1259 break;
1262 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
1263 if (new_cfg & RESET_HAS_TRST) {
1264 if (new_cfg & RESET_TRST_OPEN_DRAIN)
1265 modes[3] = " trst_open_drain";
1266 else
1267 modes[3] = " trst_push_pull";
1268 } else
1269 modes[3] = "";
1271 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
1272 if (new_cfg & RESET_HAS_SRST) {
1273 if (new_cfg & RESET_SRST_NO_GATING)
1274 modes[2] = " srst_nogate";
1275 else
1276 modes[2] = " srst_gates_jtag";
1278 if (new_cfg & RESET_SRST_PUSH_PULL)
1279 modes[4] = " srst_push_pull";
1280 else
1281 modes[4] = " srst_open_drain";
1282 } else {
1283 modes[2] = "";
1284 modes[4] = "";
1287 command_print(CMD_CTX, "%s %s%s%s%s",
1288 modes[0], modes[1],
1289 modes[2], modes[3], modes[4]);
1291 return ERROR_OK;
1294 COMMAND_HANDLER(handle_adapter_nsrst_delay_command)
1296 if (CMD_ARGC > 1)
1297 return ERROR_COMMAND_SYNTAX_ERROR;
1298 if (CMD_ARGC == 1)
1300 unsigned delay;
1301 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1303 jtag_set_nsrst_delay(delay);
1305 command_print(CMD_CTX, "adapter_nsrst_delay: %u", jtag_get_nsrst_delay());
1306 return ERROR_OK;
1309 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1311 if (CMD_ARGC > 1)
1312 return ERROR_COMMAND_SYNTAX_ERROR;
1313 if (CMD_ARGC == 1)
1315 unsigned delay;
1316 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1318 jtag_set_ntrst_delay(delay);
1320 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1321 return ERROR_OK;
1324 COMMAND_HANDLER(handle_adapter_nsrst_assert_width_command)
1326 if (CMD_ARGC > 1)
1327 return ERROR_COMMAND_SYNTAX_ERROR;
1328 if (CMD_ARGC == 1)
1330 unsigned delay;
1331 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1333 jtag_set_nsrst_assert_width(delay);
1335 command_print(CMD_CTX, "adapter_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
1336 return ERROR_OK;
1339 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1341 if (CMD_ARGC > 1)
1342 return ERROR_COMMAND_SYNTAX_ERROR;
1343 if (CMD_ARGC == 1)
1345 unsigned delay;
1346 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1348 jtag_set_ntrst_assert_width(delay);
1350 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1351 return ERROR_OK;
1354 COMMAND_HANDLER(handle_adapter_khz_command)
1356 if (CMD_ARGC > 1)
1357 return ERROR_COMMAND_SYNTAX_ERROR;
1359 int retval = ERROR_OK;
1360 if (CMD_ARGC == 1)
1362 unsigned khz = 0;
1363 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1365 retval = jtag_config_khz(khz);
1366 if (ERROR_OK != retval)
1367 return retval;
1370 int cur_speed = jtag_get_speed_khz();
1371 retval = jtag_get_speed_readable(&cur_speed);
1372 if (ERROR_OK != retval)
1373 return retval;
1375 if (cur_speed)
1376 command_print(CMD_CTX, "%d kHz", cur_speed);
1377 else
1378 command_print(CMD_CTX, "RCLK - adaptive");
1380 return retval;
1383 COMMAND_HANDLER(handle_jtag_rclk_command)
1385 if (CMD_ARGC > 1)
1386 return ERROR_COMMAND_SYNTAX_ERROR;
1388 int retval = ERROR_OK;
1389 if (CMD_ARGC == 1)
1391 unsigned khz = 0;
1392 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1394 retval = jtag_config_rclk(khz);
1395 if (ERROR_OK != retval)
1396 return retval;
1399 int cur_khz = jtag_get_speed_khz();
1400 retval = jtag_get_speed_readable(&cur_khz);
1401 if (ERROR_OK != retval)
1402 return retval;
1404 if (cur_khz)
1405 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1406 else
1407 command_print(CMD_CTX, "RCLK - adaptive");
1409 return retval;
1412 COMMAND_HANDLER(handle_jtag_reset_command)
1414 if (CMD_ARGC != 2)
1415 return ERROR_COMMAND_SYNTAX_ERROR;
1417 int trst = -1;
1418 if (CMD_ARGV[0][0] == '1')
1419 trst = 1;
1420 else if (CMD_ARGV[0][0] == '0')
1421 trst = 0;
1422 else
1423 return ERROR_COMMAND_SYNTAX_ERROR;
1425 int srst = -1;
1426 if (CMD_ARGV[1][0] == '1')
1427 srst = 1;
1428 else if (CMD_ARGV[1][0] == '0')
1429 srst = 0;
1430 else
1431 return ERROR_COMMAND_SYNTAX_ERROR;
1433 if (adapter_init(CMD_CTX) != ERROR_OK)
1434 return ERROR_JTAG_INIT_FAILED;
1436 jtag_add_reset(trst, srst);
1437 return jtag_execute_queue();
1440 COMMAND_HANDLER(handle_runtest_command)
1442 if (CMD_ARGC != 1)
1443 return ERROR_COMMAND_SYNTAX_ERROR;
1445 unsigned num_clocks;
1446 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1448 jtag_add_runtest(num_clocks, TAP_IDLE);
1449 return jtag_execute_queue();
1453 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1454 * should be stable ... and *NOT* a shift state, otherwise free-running
1455 * jtag clocks could change the values latched by the update state.
1456 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1457 * and "drscan" commands are a write-only subset of what SVF provides.
1460 COMMAND_HANDLER(handle_irscan_command)
1462 int i;
1463 struct scan_field *fields;
1464 struct jtag_tap *tap = NULL;
1465 tap_state_t endstate;
1467 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1469 return ERROR_COMMAND_SYNTAX_ERROR;
1472 /* optional "-endstate" "statename" at the end of the arguments,
1473 * so that e.g. IRPAUSE can let us load the data register before
1474 * entering RUN/IDLE to execute the instruction we load here.
1476 endstate = TAP_IDLE;
1478 if (CMD_ARGC >= 4) {
1479 /* have at least one pair of numbers. */
1480 /* is last pair the magic text? */
1481 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1482 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1483 if (endstate == TAP_INVALID)
1484 return ERROR_COMMAND_SYNTAX_ERROR;
1485 if (!scan_is_safe(endstate))
1486 LOG_WARNING("unstable irscan endstate \"%s\"",
1487 CMD_ARGV[CMD_ARGC - 1]);
1488 CMD_ARGC -= 2;
1492 int num_fields = CMD_ARGC / 2;
1493 if (num_fields > 1)
1495 /* we really should be looking at plain_ir_scan if we want
1496 * anything more fancy.
1498 LOG_ERROR("Specify a single value for tap");
1499 return ERROR_COMMAND_SYNTAX_ERROR;
1502 size_t fields_len = sizeof(struct scan_field) * num_fields;
1503 fields = malloc(fields_len);
1504 memset(fields, 0, fields_len);
1506 int retval;
1507 for (i = 0; i < num_fields; i++)
1509 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1510 if (tap == NULL)
1512 int j;
1513 for (j = 0; j < i; j++)
1514 free(fields[j].out_value);
1515 free(fields);
1516 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1518 return ERROR_FAIL;
1520 int field_size = tap->ir_length;
1521 fields[i].num_bits = field_size;
1522 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1524 uint32_t value;
1525 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1526 if (ERROR_OK != retval)
1527 goto error_return;
1528 buf_set_u32(fields[i].out_value, 0, field_size, value);
1529 fields[i].in_value = NULL;
1532 /* did we have an endstate? */
1533 jtag_add_ir_scan(tap, fields, endstate);
1535 retval = jtag_execute_queue();
1537 error_return:
1538 for (i = 0; i < num_fields; i++)
1540 if (NULL != fields[i].out_value)
1541 free(fields[i].out_value);
1544 free (fields);
1546 return retval;
1550 COMMAND_HANDLER(handle_verify_ircapture_command)
1552 if (CMD_ARGC > 1)
1553 return ERROR_COMMAND_SYNTAX_ERROR;
1555 if (CMD_ARGC == 1)
1557 bool enable;
1558 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1559 jtag_set_verify_capture_ir(enable);
1562 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1563 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1565 return ERROR_OK;
1568 COMMAND_HANDLER(handle_verify_jtag_command)
1570 if (CMD_ARGC > 1)
1571 return ERROR_COMMAND_SYNTAX_ERROR;
1573 if (CMD_ARGC == 1)
1575 bool enable;
1576 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1577 jtag_set_verify(enable);
1580 const char *status = jtag_will_verify() ? "enabled": "disabled";
1581 command_print(CMD_CTX, "verify jtag capture is %s", status);
1583 return ERROR_OK;
1586 COMMAND_HANDLER(handle_tms_sequence_command)
1588 if (CMD_ARGC > 1)
1589 return ERROR_COMMAND_SYNTAX_ERROR;
1591 if (CMD_ARGC == 1)
1593 bool use_new_table;
1594 if (strcmp(CMD_ARGV[0], "short") == 0)
1595 use_new_table = true;
1596 else if (strcmp(CMD_ARGV[0], "long") == 0)
1597 use_new_table = false;
1598 else
1599 return ERROR_COMMAND_SYNTAX_ERROR;
1601 tap_use_new_tms_table(use_new_table);
1604 command_print(CMD_CTX, "tms sequence is %s",
1605 tap_uses_new_tms_table() ? "short": "long");
1607 return ERROR_OK;
1610 static const struct command_registration interface_command_handlers[] = {
1612 .name = "adapter_khz",
1613 .handler = handle_adapter_khz_command,
1614 .mode = COMMAND_ANY,
1615 .help = "With an argument, change to the specified maximum "
1616 "jtag speed. For JTAG, 0 KHz signifies adaptive "
1617 " clocking. "
1618 "With or without argument, display current setting.",
1619 .usage = "[khz]",
1622 .name = "adapter_nsrst_assert_width",
1623 .handler = handle_adapter_nsrst_assert_width_command,
1624 .mode = COMMAND_ANY,
1625 .help = "delay after asserting SRST in ms",
1626 .usage = "[milliseconds]",
1629 .name = "adapter_nsrst_delay",
1630 .handler = handle_adapter_nsrst_delay_command,
1631 .mode = COMMAND_ANY,
1632 .help = "delay after deasserting SRST in ms",
1633 .usage = "[milliseconds]",
1636 .name = "interface",
1637 .handler = handle_interface_command,
1638 .mode = COMMAND_CONFIG,
1639 .help = "Select a debug adapter interface (driver)",
1640 .usage = "driver_name",
1643 .name = "interface_list",
1644 .handler = handle_interface_list_command,
1645 .mode = COMMAND_ANY,
1646 .help = "List all built-in debug adapter interfaces (drivers)",
1648 COMMAND_REGISTRATION_DONE
1652 * Register the commands which deal with arbitrary debug adapter drivers.
1654 * @todo Remove internal assumptions that all debug adapters use JTAG for
1655 * transport. Various types and data structures are not named generically.
1657 int interface_register_commands(struct command_context *ctx)
1659 return register_commands(ctx, NULL, interface_command_handlers);
1662 static const struct command_registration jtag_command_handlers[] = {
1664 .name = "jtag_rclk",
1665 .handler = handle_jtag_rclk_command,
1666 .mode = COMMAND_ANY,
1667 .help = "With an argument, change to to use adaptive clocking "
1668 "if possible; else to use the fallback speed. "
1669 "With or without argument, display current setting.",
1670 .usage = "[fallback_speed_khz]",
1673 .name = "reset_config",
1674 .handler = handle_reset_config_command,
1675 .mode = COMMAND_ANY,
1676 .help = "configure JTAG reset behavior",
1677 .usage = "[none|trst_only|srst_only|trst_and_srst] "
1678 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
1679 "[srst_gates_jtag|srst_nogate] "
1680 "[trst_push_pull|trst_open_drain] "
1681 "[srst_push_pull|srst_open_drain]",
1684 .name = "jtag_ntrst_delay",
1685 .handler = handle_jtag_ntrst_delay_command,
1686 .mode = COMMAND_ANY,
1687 .help = "delay after deasserting trst in ms",
1688 .usage = "[milliseconds]",
1691 .name = "jtag_ntrst_assert_width",
1692 .handler = handle_jtag_ntrst_assert_width_command,
1693 .mode = COMMAND_ANY,
1694 .help = "delay after asserting trst in ms",
1695 .usage = "[milliseconds]",
1698 .name = "scan_chain",
1699 .handler = handle_scan_chain_command,
1700 .mode = COMMAND_ANY,
1701 .help = "print current scan chain configuration",
1704 .name = "jtag_reset",
1705 .handler = handle_jtag_reset_command,
1706 .mode = COMMAND_EXEC,
1707 .help = "Set reset line values. Value '1' is active, "
1708 "value '0' is inactive.",
1709 .usage = "trst_active srst_active",
1712 .name = "runtest",
1713 .handler = handle_runtest_command,
1714 .mode = COMMAND_EXEC,
1715 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1716 .usage = "num_cycles"
1719 .name = "irscan",
1720 .handler = handle_irscan_command,
1721 .mode = COMMAND_EXEC,
1722 .help = "Execute Instruction Register (DR) scan. The "
1723 "specified opcodes are put into each TAP's IR, "
1724 "and other TAPs are put in BYPASS.",
1725 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1728 .name = "verify_ircapture",
1729 .handler = handle_verify_ircapture_command,
1730 .mode = COMMAND_ANY,
1731 .help = "Display or assign flag controlling whether to "
1732 "verify values captured during Capture-IR.",
1733 .usage = "['enable'|'disable']",
1736 .name = "verify_jtag",
1737 .handler = handle_verify_jtag_command,
1738 .mode = COMMAND_ANY,
1739 .help = "Display or assign flag controlling whether to "
1740 "verify values captured during IR and DR scans.",
1741 .usage = "['enable'|'disable']",
1744 .name = "tms_sequence",
1745 .handler = handle_tms_sequence_command,
1746 .mode = COMMAND_ANY,
1747 .help = "Display or change what style TMS sequences to use "
1748 "for JTAG state transitions: short (default) or "
1749 "long. Only for working around JTAG bugs.",
1750 /* Specifically for working around DRIVER bugs... */
1751 .usage = "['short'|'long']",
1754 .name = "jtag",
1755 .mode = COMMAND_ANY,
1756 .help = "perform jtag tap actions",
1758 .chain = jtag_subcommand_handlers,
1761 .chain = jtag_command_handlers_to_move,
1763 COMMAND_REGISTRATION_DONE
1766 int jtag_register_commands(struct command_context *cmd_ctx)
1768 return register_commands(cmd_ctx, NULL, jtag_command_handlers);