allow multiple bitbang interfaces
[openocd/ztw.git] / src / jtag / tcl.c
blob6910100f9218e62c632cbef1f13f546e3321d397
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].tap = tap;
179 fields[field_count].num_bits = bits;
180 fields[field_count].out_value = malloc(DIV_ROUND_UP(bits, 8));
181 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
182 fields[field_count].in_value = fields[field_count].out_value;
183 field_count++;
186 jtag_add_dr_scan(num_fields, fields, endstate);
188 retval = jtag_execute_queue();
189 if (retval != ERROR_OK)
191 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
192 return JIM_ERR;
195 field_count = 0;
196 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
197 for (i = 2; i < argc; i += 2)
199 long bits;
200 char *str;
202 Jim_GetLong(interp, args[i], &bits);
203 str = buf_to_str(fields[field_count].in_value, bits, 16);
204 free(fields[field_count].out_value);
206 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
207 free(str);
208 field_count++;
211 Jim_SetResult(interp, list);
213 free(fields);
215 return JIM_OK;
219 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
221 tap_state_t states[8];
223 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
225 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
226 return JIM_ERR;
229 script_debug(interp, "pathmove", argc, args);
231 int i;
232 for (i = 0; i < argc-1; i++)
234 const char *cp;
235 cp = Jim_GetString(args[i + 1], NULL);
236 states[i] = tap_state_by_name(cp);
237 if (states[i] < 0)
239 /* update the error message */
240 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
241 return JIM_ERR;
245 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
247 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
248 return JIM_ERR;
251 jtag_add_pathmove(argc-2, states + 1);
253 if (jtag_execute_queue()!= ERROR_OK)
255 Jim_SetResultString(interp, "pathmove: failed",-1);
256 return JIM_ERR;
259 return JIM_OK;
263 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
265 script_debug(interp, "flush_count", argc, args);
267 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
269 return JIM_OK;
272 static const struct command_registration jtag_command_handlers_to_move[] = {
274 .name = "drscan",
275 .mode = COMMAND_EXEC,
276 .jim_handler = &Jim_Command_drscan,
277 .help = "execute DR scan <device> "
278 "<num_bits> <value> <num_bits1> <value2> ...",
281 .name = "flush_count",
282 .mode = COMMAND_EXEC,
283 .jim_handler = &Jim_Command_flush_count,
284 .help = "returns number of times the JTAG queue has been flushed",
287 .name = "pathmove",
288 .mode = COMMAND_EXEC,
289 .jim_handler = &Jim_Command_pathmove,
290 .usage = "<state1>,<state2>,<state3>... ",
291 .help = "move JTAG to state1 then to state2, state3, etc.",
293 COMMAND_REGISTRATION_DONE
297 enum jtag_tap_cfg_param {
298 JCFG_EVENT
301 static Jim_Nvp nvp_config_opts[] = {
302 { .name = "-event", .value = JCFG_EVENT },
304 { .name = NULL, .value = -1 }
307 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
309 if (goi->argc == 0)
311 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
312 return JIM_ERR;
315 Jim_Nvp *n;
316 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
317 if (e != JIM_OK)
319 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
320 return e;
323 if (goi->isconfigure) {
324 if (goi->argc != 1) {
325 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
326 return JIM_ERR;
328 } else {
329 if (goi->argc != 0) {
330 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
331 return JIM_ERR;
335 struct jtag_tap_event_action *jteap = tap->event_action;
336 /* replace existing event body */
337 bool found = false;
338 while (jteap)
340 if (jteap->event == (enum jtag_event)n->value)
342 found = true;
343 break;
345 jteap = jteap->next;
348 Jim_SetEmptyResult(goi->interp);
350 if (goi->isconfigure)
352 if (!found)
353 jteap = calloc(1, sizeof(*jteap));
354 else if (NULL != jteap->body)
355 Jim_DecrRefCount(goi->interp, jteap->body);
357 jteap->interp = goi->interp;
358 jteap->event = n->value;
360 Jim_Obj *o;
361 Jim_GetOpt_Obj(goi, &o);
362 jteap->body = Jim_DuplicateObj(goi->interp, o);
363 Jim_IncrRefCount(jteap->body);
365 if (!found)
367 /* add to head of event list */
368 jteap->next = tap->event_action;
369 tap->event_action = jteap;
372 else if (found)
374 jteap->interp = goi->interp;
375 Jim_SetResult(goi->interp,
376 Jim_DuplicateObj(goi->interp, jteap->body));
378 return JIM_OK;
381 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
383 /* parse config or cget options */
384 while (goi->argc > 0)
386 Jim_SetEmptyResult (goi->interp);
388 Jim_Nvp *n;
389 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
390 if (e != JIM_OK)
392 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
393 return e;
396 switch (n->value)
398 case JCFG_EVENT:
399 e = jtag_tap_configure_event(goi, tap);
400 if (e != JIM_OK)
401 return e;
402 break;
403 default:
404 Jim_SetResult_sprintf(goi->interp, "unknown event: %s", n->name);
405 return JIM_ERR;
409 return JIM_OK;
412 static int is_bad_irval(int ir_length, jim_wide w)
414 jim_wide v = 1;
416 v <<= ir_length;
417 v -= 1;
418 v = ~v;
419 return (w & v) != 0;
422 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
423 struct jtag_tap *pTap)
425 jim_wide w;
426 int e = Jim_GetOpt_Wide(goi, &w);
427 if (e != JIM_OK) {
428 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
429 return e;
432 unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
433 uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
434 if (new_expected_ids == NULL)
436 Jim_SetResult_sprintf(goi->interp, "no memory");
437 return JIM_ERR;
440 memcpy(new_expected_ids, pTap->expected_ids, expected_len);
442 new_expected_ids[pTap->expected_ids_cnt] = w;
444 free(pTap->expected_ids);
445 pTap->expected_ids = new_expected_ids;
446 pTap->expected_ids_cnt++;
448 return JIM_OK;
451 #define NTAP_OPT_IRLEN 0
452 #define NTAP_OPT_IRMASK 1
453 #define NTAP_OPT_IRCAPTURE 2
454 #define NTAP_OPT_ENABLED 3
455 #define NTAP_OPT_DISABLED 4
456 #define NTAP_OPT_EXPECTED_ID 5
458 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
459 struct jtag_tap *pTap)
461 jim_wide w;
462 int e = Jim_GetOpt_Wide(goi, &w);
463 if (e != JIM_OK)
465 Jim_SetResult_sprintf(goi->interp,
466 "option: %s bad parameter", n->name);
467 free((void *)pTap->dotted_name);
468 return e;
470 switch (n->value) {
471 case NTAP_OPT_IRLEN:
472 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
474 LOG_WARNING("%s: huge IR length %d",
475 pTap->dotted_name, (int) w);
477 pTap->ir_length = w;
478 break;
479 case NTAP_OPT_IRMASK:
480 if (is_bad_irval(pTap->ir_length, w))
482 LOG_ERROR("%s: IR mask %x too big",
483 pTap->dotted_name,
484 (int) w);
485 return JIM_ERR;
487 if ((w & 3) != 3)
488 LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
489 pTap->ir_capture_mask = w;
490 break;
491 case NTAP_OPT_IRCAPTURE:
492 if (is_bad_irval(pTap->ir_length, w))
494 LOG_ERROR("%s: IR capture %x too big",
495 pTap->dotted_name, (int) w);
496 return JIM_ERR;
498 if ((w & 3) != 1)
499 LOG_WARNING("%s: nonstandard IR value",
500 pTap->dotted_name);
501 pTap->ir_capture_value = w;
502 break;
503 default:
504 return JIM_ERR;
506 return JIM_OK;
509 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
511 struct jtag_tap *pTap;
512 int x;
513 int e;
514 Jim_Nvp *n;
515 char *cp;
516 const Jim_Nvp opts[] = {
517 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
518 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
519 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
520 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
521 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
522 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
523 { .name = NULL , .value = -1 },
526 pTap = calloc(1, sizeof(struct jtag_tap));
527 if (!pTap) {
528 Jim_SetResult_sprintf(goi->interp, "no memory");
529 return JIM_ERR;
533 * we expect CHIP + TAP + OPTIONS
534 * */
535 if (goi->argc < 3) {
536 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
537 free(pTap);
538 return JIM_ERR;
540 Jim_GetOpt_String(goi, &cp, NULL);
541 pTap->chip = strdup(cp);
543 Jim_GetOpt_String(goi, &cp, NULL);
544 pTap->tapname = strdup(cp);
546 /* name + dot + name + null */
547 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
548 cp = malloc(x);
549 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
550 pTap->dotted_name = cp;
552 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
553 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
555 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
556 * that the default. The "-irlen" and "-irmask" options are only
557 * needed to cope with nonstandard TAPs, or to specify more bits.
559 pTap->ir_capture_mask = 0x03;
560 pTap->ir_capture_value = 0x01;
562 while (goi->argc) {
563 e = Jim_GetOpt_Nvp(goi, opts, &n);
564 if (e != JIM_OK) {
565 Jim_GetOpt_NvpUnknown(goi, opts, 0);
566 free((void *)pTap->dotted_name);
567 free(pTap);
568 return e;
570 LOG_DEBUG("Processing option: %s", n->name);
571 switch (n->value) {
572 case NTAP_OPT_ENABLED:
573 pTap->disabled_after_reset = false;
574 break;
575 case NTAP_OPT_DISABLED:
576 pTap->disabled_after_reset = true;
577 break;
578 case NTAP_OPT_EXPECTED_ID:
579 e = jim_newtap_expected_id(n, goi, pTap);
580 if (JIM_OK != e)
582 free((void *)pTap->dotted_name);
583 free(pTap);
584 return e;
586 break;
587 case NTAP_OPT_IRLEN:
588 case NTAP_OPT_IRMASK:
589 case NTAP_OPT_IRCAPTURE:
590 e = jim_newtap_ir_param(n, goi, pTap);
591 if (JIM_OK != e)
593 free((void *)pTap->dotted_name);
594 free(pTap);
595 return e;
597 break;
598 } /* switch (n->value) */
599 } /* while (goi->argc) */
601 /* default is enabled-after-reset */
602 pTap->enabled = !pTap->disabled_after_reset;
604 /* Did all the required option bits get cleared? */
605 if (pTap->ir_length != 0)
607 jtag_tap_init(pTap);
608 return ERROR_OK;
611 Jim_SetResult_sprintf(goi->interp,
612 "newtap: %s missing IR length",
613 pTap->dotted_name);
614 jtag_tap_free(pTap);
615 return JIM_ERR;
618 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
620 struct jtag_tap_event_action * jteap;
622 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
624 if (jteap->event != e)
625 continue;
627 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
628 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
629 tap->dotted_name, e, nvp->name,
630 Jim_GetString(jteap->body, NULL));
632 if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK)
634 Jim_PrintErrorMessage(jteap->interp);
635 continue;
638 switch (e)
640 case JTAG_TAP_EVENT_ENABLE:
641 case JTAG_TAP_EVENT_DISABLE:
642 /* NOTE: we currently assume the handlers
643 * can't fail. Right here is where we should
644 * really be verifying the scan chains ...
646 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
647 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
648 tap->enabled ? "enabled" : "disabled");
649 break;
650 default:
651 break;
656 static int jim_jtag_interface(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
658 Jim_GetOptInfo goi;
659 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
661 /* return the name of the interface */
662 /* TCL code might need to know the exact type... */
663 /* FUTURE: we allow this as a means to "set" the interface. */
664 if (goi.argc != 0) {
665 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
666 return JIM_ERR;
668 const char *name = jtag_interface ? jtag_driver_name(jtag_interface) : NULL;
669 Jim_SetResultString(goi.interp, name ? : "undefined", -1);
670 return JIM_OK;
673 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
675 Jim_GetOptInfo goi;
676 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
677 if (goi.argc != 0) {
678 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
679 return JIM_ERR;
681 struct command_context *context = Jim_GetAssocData(interp, "context");
682 int e = jtag_init_inner(context);
683 if (e != ERROR_OK) {
684 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
685 return JIM_ERR;
687 return JIM_OK;
690 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
692 Jim_GetOptInfo goi;
693 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
694 if (goi.argc != 0) {
695 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
696 return JIM_ERR;
698 struct command_context *context = Jim_GetAssocData(interp, "context");
699 int e = jtag_init_reset(context);
700 if (e != ERROR_OK) {
701 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
702 return JIM_ERR;
704 return JIM_OK;
707 static int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
709 Jim_GetOptInfo goi;
710 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
711 return jim_newtap_cmd(&goi);
714 static bool jtag_tap_enable(struct jtag_tap *t)
716 if (t->enabled)
717 return false;
718 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
719 if (!t->enabled)
720 return false;
722 /* FIXME add JTAG sanity checks, w/o TLR
723 * - scan chain length grew by one (this)
724 * - IDs and IR lengths are as expected
726 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
727 return true;
729 static bool jtag_tap_disable(struct jtag_tap *t)
731 if (!t->enabled)
732 return false;
733 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
734 if (t->enabled)
735 return false;
737 /* FIXME add JTAG sanity checks, w/o TLR
738 * - scan chain length shrank by one (this)
739 * - IDs and IR lengths are as expected
741 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
742 return true;
745 static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
747 const char *cmd_name = Jim_GetString(argv[0], NULL);
748 Jim_GetOptInfo goi;
749 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
750 if (goi.argc != 1) {
751 Jim_SetResult_sprintf(goi.interp, "usage: %s <name>", cmd_name);
752 return JIM_ERR;
755 struct jtag_tap *t;
757 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
758 if (t == NULL)
759 return JIM_ERR;
761 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
762 // do nothing, just return the value
763 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
764 if (!jtag_tap_enable(t))
765 LOG_WARNING("failed to disable tap");
766 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
767 if (!jtag_tap_disable(t))
768 LOG_WARNING("failed to disable tap");
769 } else {
770 LOG_ERROR("command '%s' unknown", cmd_name);
771 return JIM_ERR;
773 bool e = t->enabled;
774 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
775 return JIM_OK;
778 static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
780 const char *cmd_name = Jim_GetString(argv[0], NULL);
781 Jim_GetOptInfo goi;
782 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
783 goi.isconfigure = !strcmp(cmd_name, "configure");
784 if (goi.argc < 2 + goi.isconfigure) {
785 Jim_WrongNumArgs(goi.interp, 0, NULL,
786 "<tap_name> <attribute> ...");
787 return JIM_ERR;
790 struct jtag_tap *t;
792 Jim_Obj *o;
793 Jim_GetOpt_Obj(&goi, &o);
794 t = jtag_tap_by_jim_obj(goi.interp, o);
795 if (t == NULL) {
796 return JIM_ERR;
799 return jtag_tap_configure_cmd(&goi, t);
802 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
804 Jim_GetOptInfo goi;
805 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
806 if (goi.argc != 0) {
807 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
808 return JIM_ERR;
810 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
811 struct jtag_tap *tap;
813 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
814 Jim_ListAppendElement(goi.interp,
815 Jim_GetResult(goi.interp),
816 Jim_NewStringObj(goi.interp,
817 tap->dotted_name, -1));
819 return JIM_OK;
822 COMMAND_HANDLER(handle_jtag_init_command)
824 if (CMD_ARGC != 0)
825 return ERROR_COMMAND_SYNTAX_ERROR;
827 static bool jtag_initialized = false;
828 if (jtag_initialized)
830 LOG_INFO("'jtag init' has already been called");
831 return ERROR_OK;
833 jtag_initialized = true;
835 LOG_DEBUG("Initializing jtag devices...");
836 return jtag_init(CMD_CTX);
839 static const struct command_registration jtag_subcommand_handlers[] = {
841 .name = "init",
842 .mode = COMMAND_ANY,
843 .handler = &handle_jtag_init_command,
844 .help = "initialize jtag scan chain",
847 .name = "interface",
848 .mode = COMMAND_ANY,
849 .jim_handler = &jim_jtag_interface,
850 .help = "Returns the selected interface",
853 .name = "arp_init",
854 .mode = COMMAND_ANY,
855 .jim_handler = &jim_jtag_arp_init,
858 .name = "arp_init-reset",
859 .mode = COMMAND_ANY,
860 .jim_handler = &jim_jtag_arp_init_reset,
863 .name = "newtap",
864 .mode = COMMAND_CONFIG,
865 .jim_handler = &jim_jtag_newtap,
866 .help = "Create a new TAP instance",
867 .usage = "<name> <type> -irlen <count> [-ircapture <count>] "
868 "[-irmask <count>] [-enable|-disable]",
871 .name = "tapisenabled",
872 .mode = COMMAND_EXEC,
873 .jim_handler = &jim_jtag_tap_enabler,
874 .help = "Returns a integer indicating TAP state (0/1)",
875 .usage = "<name>",
878 .name = "tapenable",
879 .mode = COMMAND_EXEC,
880 .jim_handler = &jim_jtag_tap_enabler,
881 .help = "Enable the specified TAP",
882 .usage = "<name>",
885 .name = "tapdisable",
886 .mode = COMMAND_EXEC,
887 .jim_handler = &jim_jtag_tap_enabler,
888 .help = "Enable the specified TAP",
889 .usage = "<name>",
892 .name = "configure",
893 .mode = COMMAND_EXEC,
894 .jim_handler = &jim_jtag_configure,
895 .help = "Enable the specified TAP",
896 .usage = "<name> [<key> <value> ...]",
899 .name = "cget",
900 .mode = COMMAND_EXEC,
901 .jim_handler = &jim_jtag_configure,
902 .help = "Enable the specified TAP",
903 .usage = "<name> [<key> <value> ...]",
906 .name = "names",
907 .mode = COMMAND_ANY,
908 .jim_handler = &jim_jtag_names,
909 .help = "Returns list of all JTAG tap names",
912 .chain = jtag_command_handlers_to_move,
914 COMMAND_REGISTRATION_DONE
917 void jtag_notify_event(enum jtag_event event)
919 struct jtag_tap *tap;
921 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
922 jtag_tap_handle_event(tap, event);
926 static int default_khz(struct jtag_device *interface, int khz, int *jtag_speed)
928 LOG_ERROR("Translation from khz to jtag_speed not implemented");
929 return ERROR_FAIL;
932 static int default_speed_div(struct jtag_device *interface, int speed, int *khz)
934 LOG_ERROR("Translation from jtag_speed to khz not implemented");
935 return ERROR_FAIL;
938 static int default_power_dropout(struct jtag_device *interface, int *dropout)
940 *dropout = 0; /* by default we can't detect power dropout */
941 return ERROR_OK;
944 static int default_srst_asserted(struct jtag_device *interface, int *srst_asserted)
946 *srst_asserted = 0; /* by default we can't detect srst asserted */
947 return ERROR_OK;
950 COMMAND_HANDLER(handle_interface_list_command)
952 if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
953 return ERROR_COMMAND_SYNTAX_ERROR;
955 command_print(CMD_CTX, "The following JTAG interfaces are available:");
956 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
958 const char *name = jtag_driver_name(jtag_interfaces[i]);
959 command_print(CMD_CTX, "%u: %s", i + 1, name);
962 return ERROR_OK;
965 COMMAND_HANDLER(handle_interface_command)
967 /* check whether the interface is already configured */
968 if (jtag_interface)
970 LOG_WARNING("Interface already configured, ignoring");
971 return ERROR_OK;
974 /* interface name is a mandatory argument */
975 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
976 return ERROR_COMMAND_SYNTAX_ERROR;
978 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
980 if (strcmp(CMD_ARGV[0], jtag_driver_name(jtag_interfaces[i])) != 0)
981 continue;
983 const struct command_registration *commands;
984 commands = jtag_driver_commands(jtag_interfaces[i]);
985 if (NULL != commands)
987 int retval = register_commands(CMD_CTX, NULL, commands);
988 if (ERROR_OK != retval)
989 return retval;
992 jtag_interface = jtag_interfaces[i];
994 if (jtag_interface->khz == NULL)
995 jtag_interface->khz = default_khz;
996 if (jtag_interface->speed_div == NULL)
997 jtag_interface->speed_div = default_speed_div;
998 if (jtag_interface->power_dropout == NULL)
999 jtag_interface->power_dropout = default_power_dropout;
1000 if (jtag_interface->srst_asserted == NULL)
1001 jtag_interface->srst_asserted = default_srst_asserted;
1003 return ERROR_OK;
1006 /* no valid interface was found (i.e. the configuration option,
1007 * didn't match one of the compiled-in interfaces
1009 LOG_ERROR("The specified JTAG interface was not found (%s)", CMD_ARGV[0]);
1010 CALL_COMMAND_HANDLER(handle_interface_list_command);
1011 return ERROR_JTAG_INVALID_INTERFACE;
1014 COMMAND_HANDLER(handle_scan_chain_command)
1016 struct jtag_tap *tap;
1018 tap = jtag_all_taps();
1019 command_print(CMD_CTX, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
1020 command_print(CMD_CTX, "---|--------------------|---------|------------|------------|------|------|------|---------");
1022 while (tap) {
1023 uint32_t expected, expected_mask, cur_instr, ii;
1024 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
1025 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
1026 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
1028 command_print(CMD_CTX,
1029 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
1030 tap->abs_chain_position,
1031 tap->dotted_name,
1032 tap->enabled ? 'Y' : 'n',
1033 (unsigned int)(tap->idcode),
1034 (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
1035 (unsigned int)(tap->ir_length),
1036 (unsigned int)(expected),
1037 (unsigned int)(expected_mask),
1038 (unsigned int)(cur_instr));
1040 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
1041 command_print(CMD_CTX, " | | | | 0x%08x | | | | ",
1042 (unsigned int)(tap->expected_ids[ii]));
1045 tap = tap->next_tap;
1048 return ERROR_OK;
1051 COMMAND_HANDLER(handle_reset_config_command)
1053 int new_cfg = 0;
1054 int mask = 0;
1056 /* Original versions cared about the order of these tokens:
1057 * reset_config signals [combination [trst_type [srst_type]]]
1058 * They also clobbered the previous configuration even on error.
1060 * Here we don't care about the order, and only change values
1061 * which have been explicitly specified.
1063 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
1064 int tmp = 0;
1065 int m;
1067 /* gating */
1068 m = RESET_SRST_NO_GATING;
1069 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
1070 /* default: don't use JTAG while SRST asserted */;
1071 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
1072 tmp = RESET_SRST_NO_GATING;
1073 else
1074 m = 0;
1075 if (mask & m) {
1076 LOG_ERROR("extra reset_config %s spec (%s)",
1077 "gating", *CMD_ARGV);
1078 return ERROR_INVALID_ARGUMENTS;
1080 if (m)
1081 goto next;
1083 /* signals */
1084 m = RESET_HAS_TRST | RESET_HAS_SRST;
1085 if (strcmp(*CMD_ARGV, "none") == 0)
1086 tmp = RESET_NONE;
1087 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
1088 tmp = RESET_HAS_TRST;
1089 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
1090 tmp = RESET_HAS_SRST;
1091 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
1092 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
1093 else
1094 m = 0;
1095 if (mask & m) {
1096 LOG_ERROR("extra reset_config %s spec (%s)",
1097 "signal", *CMD_ARGV);
1098 return ERROR_INVALID_ARGUMENTS;
1100 if (m)
1101 goto next;
1103 /* combination (options for broken wiring) */
1104 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1105 if (strcmp(*CMD_ARGV, "separate") == 0)
1106 /* separate reset lines - default */;
1107 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
1108 tmp |= RESET_SRST_PULLS_TRST;
1109 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
1110 tmp |= RESET_TRST_PULLS_SRST;
1111 else if (strcmp(*CMD_ARGV, "combined") == 0)
1112 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1113 else
1114 m = 0;
1115 if (mask & m) {
1116 LOG_ERROR("extra reset_config %s spec (%s)",
1117 "combination", *CMD_ARGV);
1118 return ERROR_INVALID_ARGUMENTS;
1120 if (m)
1121 goto next;
1123 /* trst_type (NOP without HAS_TRST) */
1124 m = RESET_TRST_OPEN_DRAIN;
1125 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
1126 tmp |= RESET_TRST_OPEN_DRAIN;
1127 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
1128 /* push/pull from adapter - default */;
1129 else
1130 m = 0;
1131 if (mask & m) {
1132 LOG_ERROR("extra reset_config %s spec (%s)",
1133 "trst_type", *CMD_ARGV);
1134 return ERROR_INVALID_ARGUMENTS;
1136 if (m)
1137 goto next;
1139 /* srst_type (NOP without HAS_SRST) */
1140 m |= RESET_SRST_PUSH_PULL;
1141 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
1142 tmp |= RESET_SRST_PUSH_PULL;
1143 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
1144 /* open drain from adapter - default */;
1145 else
1146 m = 0;
1147 if (mask & m) {
1148 LOG_ERROR("extra reset_config %s spec (%s)",
1149 "srst_type", *CMD_ARGV);
1150 return ERROR_INVALID_ARGUMENTS;
1152 if (m)
1153 goto next;
1155 /* caller provided nonsense; fail */
1156 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
1157 return ERROR_INVALID_ARGUMENTS;
1159 next:
1160 /* Remember the bits which were specified (mask)
1161 * and their new values (new_cfg).
1163 mask |= m;
1164 new_cfg |= tmp;
1167 /* clear previous values of those bits, save new values */
1168 if (mask) {
1169 int old_cfg = jtag_get_reset_config();
1171 old_cfg &= ~mask;
1172 new_cfg |= old_cfg;
1173 jtag_set_reset_config(new_cfg);
1174 } else
1175 new_cfg = jtag_get_reset_config();
1179 * Display the (now-)current reset mode
1181 char *modes[5];
1183 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
1184 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
1185 case RESET_HAS_SRST:
1186 modes[0] = "srst_only";
1187 break;
1188 case RESET_HAS_TRST:
1189 modes[0] = "trst_only";
1190 break;
1191 case RESET_TRST_AND_SRST:
1192 modes[0] = "trst_and_srst";
1193 break;
1194 default:
1195 modes[0] = "none";
1196 break;
1199 /* normally SRST and TRST are decoupled; but bugs happen ... */
1200 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
1201 case RESET_SRST_PULLS_TRST:
1202 modes[1] = "srst_pulls_trst";
1203 break;
1204 case RESET_TRST_PULLS_SRST:
1205 modes[1] = "trst_pulls_srst";
1206 break;
1207 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
1208 modes[1] = "combined";
1209 break;
1210 default:
1211 modes[1] = "separate";
1212 break;
1215 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
1216 if (new_cfg & RESET_HAS_TRST) {
1217 if (new_cfg & RESET_TRST_OPEN_DRAIN)
1218 modes[3] = " trst_open_drain";
1219 else
1220 modes[3] = " trst_push_pull";
1221 } else
1222 modes[3] = "";
1224 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
1225 if (new_cfg & RESET_HAS_SRST) {
1226 if (new_cfg & RESET_SRST_NO_GATING)
1227 modes[2] = " srst_nogate";
1228 else
1229 modes[2] = " srst_gates_jtag";
1231 if (new_cfg & RESET_SRST_PUSH_PULL)
1232 modes[4] = " srst_push_pull";
1233 else
1234 modes[4] = " srst_open_drain";
1235 } else {
1236 modes[2] = "";
1237 modes[4] = "";
1240 command_print(CMD_CTX, "%s %s%s%s%s",
1241 modes[0], modes[1],
1242 modes[2], modes[3], modes[4]);
1244 return ERROR_OK;
1247 COMMAND_HANDLER(handle_jtag_nsrst_delay_command)
1249 if (CMD_ARGC > 1)
1250 return ERROR_COMMAND_SYNTAX_ERROR;
1251 if (CMD_ARGC == 1)
1253 unsigned delay;
1254 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1256 jtag_set_nsrst_delay(delay);
1258 command_print(CMD_CTX, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
1259 return ERROR_OK;
1262 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1264 if (CMD_ARGC > 1)
1265 return ERROR_COMMAND_SYNTAX_ERROR;
1266 if (CMD_ARGC == 1)
1268 unsigned delay;
1269 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1271 jtag_set_ntrst_delay(delay);
1273 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1274 return ERROR_OK;
1277 COMMAND_HANDLER(handle_jtag_nsrst_assert_width_command)
1279 if (CMD_ARGC > 1)
1280 return ERROR_COMMAND_SYNTAX_ERROR;
1281 if (CMD_ARGC == 1)
1283 unsigned delay;
1284 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1286 jtag_set_nsrst_assert_width(delay);
1288 command_print(CMD_CTX, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
1289 return ERROR_OK;
1292 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1294 if (CMD_ARGC > 1)
1295 return ERROR_COMMAND_SYNTAX_ERROR;
1296 if (CMD_ARGC == 1)
1298 unsigned delay;
1299 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1301 jtag_set_ntrst_assert_width(delay);
1303 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1304 return ERROR_OK;
1307 COMMAND_HANDLER(handle_jtag_khz_command)
1309 if (CMD_ARGC > 1)
1310 return ERROR_COMMAND_SYNTAX_ERROR;
1312 int retval = ERROR_OK;
1313 if (CMD_ARGC == 1)
1315 unsigned khz = 0;
1316 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1318 retval = jtag_config_khz(khz);
1319 if (ERROR_OK != retval)
1320 return retval;
1323 int cur_speed = jtag_get_speed_khz();
1324 retval = jtag_get_speed_readable(&cur_speed);
1325 if (ERROR_OK != retval)
1326 return retval;
1328 if (cur_speed)
1329 command_print(CMD_CTX, "%d kHz", cur_speed);
1330 else
1331 command_print(CMD_CTX, "RCLK - adaptive");
1333 return retval;
1336 COMMAND_HANDLER(handle_jtag_rclk_command)
1338 if (CMD_ARGC > 1)
1339 return ERROR_COMMAND_SYNTAX_ERROR;
1341 int retval = ERROR_OK;
1342 if (CMD_ARGC == 1)
1344 unsigned khz = 0;
1345 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1347 retval = jtag_config_rclk(khz);
1348 if (ERROR_OK != retval)
1349 return retval;
1352 int cur_khz = jtag_get_speed_khz();
1353 retval = jtag_get_speed_readable(&cur_khz);
1354 if (ERROR_OK != retval)
1355 return retval;
1357 if (cur_khz)
1358 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1359 else
1360 command_print(CMD_CTX, "RCLK - adaptive");
1362 return retval;
1365 COMMAND_HANDLER(handle_jtag_reset_command)
1367 if (CMD_ARGC != 2)
1368 return ERROR_COMMAND_SYNTAX_ERROR;
1370 int trst = -1;
1371 if (CMD_ARGV[0][0] == '1')
1372 trst = 1;
1373 else if (CMD_ARGV[0][0] == '0')
1374 trst = 0;
1375 else
1376 return ERROR_COMMAND_SYNTAX_ERROR;
1378 int srst = -1;
1379 if (CMD_ARGV[1][0] == '1')
1380 srst = 1;
1381 else if (CMD_ARGV[1][0] == '0')
1382 srst = 0;
1383 else
1384 return ERROR_COMMAND_SYNTAX_ERROR;
1386 if (jtag_interface_init(CMD_CTX) != ERROR_OK)
1387 return ERROR_JTAG_INIT_FAILED;
1389 jtag_add_reset(trst, srst);
1390 return jtag_execute_queue();
1393 COMMAND_HANDLER(handle_runtest_command)
1395 if (CMD_ARGC != 1)
1396 return ERROR_COMMAND_SYNTAX_ERROR;
1398 unsigned num_clocks;
1399 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1401 jtag_add_runtest(num_clocks, TAP_IDLE);
1402 return jtag_execute_queue();
1406 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1407 * should be stable ... and *NOT* a shift state, otherwise free-running
1408 * jtag clocks could change the values latched by the update state.
1409 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1410 * and "drscan" commands are a write-only subset of what SVF provides.
1413 COMMAND_HANDLER(handle_irscan_command)
1415 int i;
1416 struct scan_field *fields;
1417 struct jtag_tap *tap;
1418 tap_state_t endstate;
1420 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1422 return ERROR_COMMAND_SYNTAX_ERROR;
1425 /* optional "-endstate" "statename" at the end of the arguments,
1426 * so that e.g. IRPAUSE can let us load the data register before
1427 * entering RUN/IDLE to execute the instruction we load here.
1429 endstate = TAP_IDLE;
1431 if (CMD_ARGC >= 4) {
1432 /* have at least one pair of numbers. */
1433 /* is last pair the magic text? */
1434 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1435 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1436 if (endstate == TAP_INVALID)
1437 return ERROR_COMMAND_SYNTAX_ERROR;
1438 if (!scan_is_safe(endstate))
1439 LOG_WARNING("unstable irscan endstate \"%s\"",
1440 CMD_ARGV[CMD_ARGC - 1]);
1441 CMD_ARGC -= 2;
1445 int num_fields = CMD_ARGC / 2;
1446 size_t fields_len = sizeof(struct scan_field) * num_fields;
1447 fields = malloc(fields_len);
1448 memset(fields, 0, fields_len);
1450 int retval;
1451 for (i = 0; i < num_fields; i++)
1453 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1454 if (tap == NULL)
1456 int j;
1457 for (j = 0; j < i; j++)
1458 free(fields[j].out_value);
1459 free(fields);
1460 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1462 return ERROR_FAIL;
1464 int field_size = tap->ir_length;
1465 fields[i].tap = tap;
1466 fields[i].num_bits = field_size;
1467 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1469 uint32_t value;
1470 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1471 if (ERROR_OK != retval)
1472 goto error_return;
1473 buf_set_u32(fields[i].out_value, 0, field_size, value);
1474 fields[i].in_value = NULL;
1477 /* did we have an endstate? */
1478 jtag_add_ir_scan(num_fields, fields, endstate);
1480 retval = jtag_execute_queue();
1482 error_return:
1483 for (i = 0; i < num_fields; i++)
1485 if (NULL != fields[i].out_value)
1486 free(fields[i].out_value);
1489 free (fields);
1491 return retval;
1495 COMMAND_HANDLER(handle_verify_ircapture_command)
1497 if (CMD_ARGC > 1)
1498 return ERROR_COMMAND_SYNTAX_ERROR;
1500 if (CMD_ARGC == 1)
1502 bool enable;
1503 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1504 jtag_set_verify_capture_ir(enable);
1507 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1508 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1510 return ERROR_OK;
1513 COMMAND_HANDLER(handle_verify_jtag_command)
1515 if (CMD_ARGC > 1)
1516 return ERROR_COMMAND_SYNTAX_ERROR;
1518 if (CMD_ARGC == 1)
1520 bool enable;
1521 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1522 jtag_set_verify(enable);
1525 const char *status = jtag_will_verify() ? "enabled": "disabled";
1526 command_print(CMD_CTX, "verify jtag capture is %s", status);
1528 return ERROR_OK;
1531 COMMAND_HANDLER(handle_tms_sequence_command)
1533 if (CMD_ARGC > 1)
1534 return ERROR_COMMAND_SYNTAX_ERROR;
1536 if (CMD_ARGC == 1)
1538 bool use_new_table;
1539 if (strcmp(CMD_ARGV[0], "short") == 0)
1540 use_new_table = true;
1541 else if (strcmp(CMD_ARGV[0], "long") == 0)
1542 use_new_table = false;
1543 else
1544 return ERROR_COMMAND_SYNTAX_ERROR;
1546 tap_use_new_tms_table(use_new_table);
1549 command_print(CMD_CTX, "tms sequence is %s",
1550 tap_uses_new_tms_table() ? "short": "long");
1552 return ERROR_OK;
1555 static const struct command_registration jtag_command_handlers[] = {
1557 .name = "interface",
1558 .handler = &handle_interface_command,
1559 .mode = COMMAND_CONFIG,
1560 .help = "select a JTAG interface",
1561 .usage = "<driver_name>",
1564 .name = "interface_list",
1565 .handler = &handle_interface_list_command,
1566 .mode = COMMAND_ANY,
1567 .help = "list all built-in interfaces",
1570 .name = "jtag_khz",
1571 .handler = &handle_jtag_khz_command,
1572 .mode = COMMAND_ANY,
1573 .help = "set maximum jtag speed (if supported)",
1574 .usage = "<khz:0=rtck>",
1577 .name = "jtag_rclk",
1578 .handler = &handle_jtag_rclk_command,
1579 .mode = COMMAND_ANY,
1580 .help = "set JTAG speed to RCLK or use fallback speed",
1581 .usage = "<fallback_speed_khz>",
1584 .name = "reset_config",
1585 .handler = &handle_reset_config_command,
1586 .mode = COMMAND_ANY,
1587 .help = "configure JTAG reset behavior",
1588 .usage = "[none|trst_only|srst_only|trst_and_srst] "
1589 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
1590 "[srst_gates_jtag|srst_nogate] "
1591 "[trst_push_pull|trst_open_drain] "
1592 "[srst_push_pull|srst_open_drain]",
1595 .name = "jtag_nsrst_delay",
1596 .handler = &handle_jtag_nsrst_delay_command,
1597 .mode = COMMAND_ANY,
1598 .help = "delay after deasserting srst in ms",
1599 .usage = "<ms>",
1602 .name = "jtag_ntrst_delay",
1603 .handler = &handle_jtag_ntrst_delay_command,
1604 .mode = COMMAND_ANY,
1605 .help = "delay after deasserting trst in ms",
1606 .usage = "<ms>"
1609 .name = "jtag_nsrst_assert_width",
1610 .handler = &handle_jtag_nsrst_assert_width_command,
1611 .mode = COMMAND_ANY,
1612 .help = "delay after asserting srst in ms",
1613 .usage = "<ms>"
1616 .name = "jtag_ntrst_assert_width",
1617 .handler = &handle_jtag_ntrst_assert_width_command,
1618 .mode = COMMAND_ANY,
1619 .help = "delay after asserting trst in ms",
1620 .usage = "<ms>"
1623 .name = "scan_chain",
1624 .handler = &handle_scan_chain_command,
1625 .mode = COMMAND_EXEC,
1626 .help = "print current scan chain configuration",
1629 .name = "jtag_reset",
1630 .handler = &handle_jtag_reset_command,
1631 .mode = COMMAND_EXEC,
1632 .help = "toggle reset lines",
1633 .usage = "<trst> <srst>",
1636 .name = "runtest",
1637 .handler = &handle_runtest_command,
1638 .mode = COMMAND_EXEC,
1639 .help = "move to Run-Test/Idle, and execute <num_cycles>",
1640 .usage = "<num_cycles>"
1643 .name = "irscan",
1644 .handler = &handle_irscan_command,
1645 .mode = COMMAND_EXEC,
1646 .help = "execute IR scan",
1647 .usage = "<device> <instr> [dev2] [instr2] ...",
1650 .name = "verify_ircapture",
1651 .handler = &handle_verify_ircapture_command,
1652 .mode = COMMAND_ANY,
1653 .help = "verify value captured during Capture-IR",
1654 .usage = "<enable | disable>",
1657 .name = "verify_jtag",
1658 .handler = &handle_verify_jtag_command,
1659 .mode = COMMAND_ANY,
1660 .help = "verify value capture",
1661 .usage = "<enable | disable>",
1664 .name = "tms_sequence",
1665 .handler = &handle_tms_sequence_command,
1666 .mode = COMMAND_ANY,
1667 .help = "choose short(default) or long tms_sequence",
1668 .usage = "<short | long>",
1671 .name = "jtag",
1672 .mode = COMMAND_ANY,
1673 .help = "perform jtag tap actions",
1675 .chain = jtag_subcommand_handlers,
1678 .chain = jtag_command_handlers_to_move,
1680 COMMAND_REGISTRATION_DONE
1682 int jtag_register_commands(struct command_context *cmd_ctx)
1684 return register_commands(cmd_ctx, NULL, jtag_command_handlers);