jtag: fix cmd scan_chain expected_ids bug
[openocd.git] / src / jtag / tcl.c
blobc74df5ed793faccba9e6e2d880a7feb6f45ff5f3
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Ø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 ***************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #include "jtag.h"
36 #include "minidriver.h"
37 #include "interface.h"
38 #include "interfaces.h"
39 #include "tcl.h"
41 #ifdef HAVE_STRINGS_H
42 #include <strings.h>
43 #endif
45 #include <helper/time_support.h>
47 /**
48 * @file
49 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
52 static const Jim_Nvp nvp_jtag_tap_event[] = {
53 { .value = JTAG_TRST_ASSERTED, .name = "post-reset" },
54 { .value = JTAG_TAP_EVENT_SETUP, .name = "setup" },
55 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
56 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
58 { .name = NULL, .value = -1 }
61 extern struct jtag_interface *jtag_interface;
63 struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
65 const char *cp = Jim_GetString(o, NULL);
66 struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
67 if (NULL == cp)
68 cp = "(unknown)";
69 if (NULL == t)
70 Jim_SetResultFormatted(interp, "Tap '%s' could not be found", cp);
71 return t;
74 static bool scan_is_safe(tap_state_t state)
76 switch (state) {
77 case TAP_RESET:
78 case TAP_IDLE:
79 case TAP_DRPAUSE:
80 case TAP_IRPAUSE:
81 return true;
82 default:
83 return false;
87 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
89 int retval;
90 struct scan_field *fields;
91 int num_fields;
92 int field_count = 0;
93 int i, e;
94 struct jtag_tap *tap;
95 tap_state_t endstate;
97 /* args[1] = device
98 * args[2] = num_bits
99 * args[3] = hex string
100 * ... repeat num bits and hex string ...
102 * .. optionally:
103 * args[N-2] = "-endstate"
104 * args[N-1] = statename
106 if ((argc < 4) || ((argc % 2) != 0)) {
107 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
108 return JIM_ERR;
111 endstate = TAP_IDLE;
113 script_debug(interp, "drscan", argc, args);
115 /* validate arguments as numbers */
116 e = JIM_OK;
117 for (i = 2; i < argc; i += 2) {
118 long bits;
119 const char *cp;
121 e = Jim_GetLong(interp, args[i], &bits);
122 /* If valid - try next arg */
123 if (e == JIM_OK)
124 continue;
126 /* Not valid.. are we at the end? */
127 if (((i + 2) != argc)) {
128 /* nope, then error */
129 return e;
132 /* it could be: "-endstate FOO"
133 * e.g. DRPAUSE so we can issue more instructions
134 * before entering RUN/IDLE and executing them.
137 /* get arg as a string. */
138 cp = Jim_GetString(args[i], NULL);
139 /* is it the magic? */
140 if (0 == strcmp("-endstate", cp)) {
141 /* is the statename valid? */
142 cp = Jim_GetString(args[i + 1], NULL);
144 /* see if it is a valid state name */
145 endstate = tap_state_by_name(cp);
146 if (endstate < 0) {
147 /* update the error message */
148 Jim_SetResultFormatted(interp, "endstate: %s invalid", cp);
149 } else {
150 if (!scan_is_safe(endstate))
151 LOG_WARNING("drscan with unsafe "
152 "endstate \"%s\"", cp);
154 /* valid - so clear the error */
155 e = JIM_OK;
156 /* and remove the last 2 args */
157 argc -= 2;
161 /* Still an error? */
162 if (e != JIM_OK)
163 return e; /* too bad */
164 } /* validate args */
166 assert(e == JIM_OK);
168 tap = jtag_tap_by_jim_obj(interp, args[1]);
169 if (tap == NULL)
170 return JIM_ERR;
172 num_fields = (argc-2)/2;
173 assert(num_fields > 0);
174 fields = malloc(sizeof(struct scan_field) * num_fields);
175 for (i = 2; i < argc; i += 2) {
176 long bits;
177 int len;
178 const char *str;
180 Jim_GetLong(interp, args[i], &bits);
181 str = Jim_GetString(args[i + 1], &len);
183 fields[field_count].num_bits = bits;
184 void *t = malloc(DIV_ROUND_UP(bits, 8));
185 fields[field_count].out_value = t;
186 str_to_buf(str, len, t, bits, 0);
187 fields[field_count].in_value = t;
188 field_count++;
191 jtag_add_dr_scan(tap, num_fields, fields, endstate);
193 retval = jtag_execute_queue();
194 if (retval != ERROR_OK) {
195 Jim_SetResultString(interp, "drscan: jtag execute failed", -1);
196 return JIM_ERR;
199 field_count = 0;
200 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
201 for (i = 2; i < argc; i += 2) {
202 long bits;
203 char *str;
205 Jim_GetLong(interp, args[i], &bits);
206 str = buf_to_str(fields[field_count].in_value, bits, 16);
207 free((void *)fields[field_count].out_value);
209 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
210 free(str);
211 field_count++;
214 Jim_SetResult(interp, list);
216 free(fields);
218 return JIM_OK;
222 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
224 tap_state_t states[8];
226 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1))) {
227 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
228 return JIM_ERR;
231 script_debug(interp, "pathmove", argc, args);
233 int i;
234 for (i = 0; i < argc-1; i++) {
235 const char *cp;
236 cp = Jim_GetString(args[i + 1], NULL);
237 states[i] = tap_state_by_name(cp);
238 if (states[i] < 0) {
239 /* update the error message */
240 Jim_SetResultFormatted(interp, "endstate: %s invalid", cp);
241 return JIM_ERR;
245 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) {
253 Jim_SetResultString(interp, "pathmove: failed", -1);
254 return JIM_ERR;
257 return JIM_OK;
261 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
263 script_debug(interp, "flush_count", argc, args);
265 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
267 return JIM_OK;
270 /* REVISIT Just what about these should "move" ... ?
271 * These registrations, into the main JTAG table?
273 * There's a minor compatibility issue, these all show up twice;
274 * that's not desirable:
275 * - jtag drscan ... NOT DOCUMENTED!
276 * - drscan ...
278 * The "irscan" command (for example) doesn't show twice.
280 static const struct command_registration jtag_command_handlers_to_move[] = {
282 .name = "drscan",
283 .mode = COMMAND_EXEC,
284 .jim_handler = Jim_Command_drscan,
285 .help = "Execute Data Register (DR) scan for one TAP. "
286 "Other TAPs must be in BYPASS mode.",
287 .usage = "tap_name [num_bits value]* ['-endstate' state_name]",
290 .name = "flush_count",
291 .mode = COMMAND_EXEC,
292 .jim_handler = Jim_Command_flush_count,
293 .help = "Returns the number of times the JTAG queue "
294 "has been flushed.",
297 .name = "pathmove",
298 .mode = COMMAND_EXEC,
299 .jim_handler = Jim_Command_pathmove,
300 .usage = "start_state state1 [state2 [state3 ...]]",
301 .help = "Move JTAG state machine from current state "
302 "(start_state) to state1, then state2, state3, etc.",
304 COMMAND_REGISTRATION_DONE
308 enum jtag_tap_cfg_param {
309 JCFG_EVENT
312 static Jim_Nvp nvp_config_opts[] = {
313 { .name = "-event", .value = JCFG_EVENT },
315 { .name = NULL, .value = -1 }
318 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap *tap)
320 if (goi->argc == 0) {
321 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
322 return JIM_ERR;
325 Jim_Nvp *n;
326 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
327 if (e != JIM_OK) {
328 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
329 return e;
332 if (goi->isconfigure) {
333 if (goi->argc != 1) {
334 Jim_WrongNumArgs(goi->interp,
335 goi->argc,
336 goi->argv,
337 "-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) {
351 if (jteap->event == (enum jtag_event)n->value) {
352 found = true;
353 break;
355 jteap = jteap->next;
358 Jim_SetEmptyResult(goi->interp);
360 if (goi->isconfigure) {
361 if (!found)
362 jteap = calloc(1, sizeof(*jteap));
363 else if (NULL != jteap->body)
364 Jim_DecrRefCount(goi->interp, jteap->body);
366 jteap->interp = goi->interp;
367 jteap->event = n->value;
369 Jim_Obj *o;
370 Jim_GetOpt_Obj(goi, &o);
371 jteap->body = Jim_DuplicateObj(goi->interp, o);
372 Jim_IncrRefCount(jteap->body);
374 if (!found) {
375 /* add to head of event list */
376 jteap->next = tap->event_action;
377 tap->event_action = jteap;
379 } else if (found) {
380 jteap->interp = goi->interp;
381 Jim_SetResult(goi->interp,
382 Jim_DuplicateObj(goi->interp, jteap->body));
384 return JIM_OK;
387 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap *tap)
389 /* parse config or cget options */
390 while (goi->argc > 0) {
391 Jim_SetEmptyResult(goi->interp);
393 Jim_Nvp *n;
394 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
395 if (e != JIM_OK) {
396 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
397 return e;
400 switch (n->value) {
401 case JCFG_EVENT:
402 e = jtag_tap_configure_event(goi, tap);
403 if (e != JIM_OK)
404 return e;
405 break;
406 default:
407 Jim_SetResultFormatted(goi->interp, "unknown event: %s", n->name);
408 return JIM_ERR;
412 return JIM_OK;
415 static int is_bad_irval(int ir_length, jim_wide w)
417 jim_wide v = 1;
419 v <<= ir_length;
420 v -= 1;
421 v = ~v;
422 return (w & v) != 0;
425 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
426 struct jtag_tap *pTap)
428 jim_wide w;
429 int e = Jim_GetOpt_Wide(goi, &w);
430 if (e != JIM_OK) {
431 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
432 return e;
435 unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
436 uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
437 if (new_expected_ids == NULL) {
438 Jim_SetResultFormatted(goi->interp, "no memory");
439 return JIM_ERR;
442 memcpy(new_expected_ids, pTap->expected_ids, expected_len);
444 new_expected_ids[pTap->expected_ids_cnt] = w;
446 free(pTap->expected_ids);
447 pTap->expected_ids = new_expected_ids;
448 pTap->expected_ids_cnt++;
450 return JIM_OK;
453 #define NTAP_OPT_IRLEN 0
454 #define NTAP_OPT_IRMASK 1
455 #define NTAP_OPT_IRCAPTURE 2
456 #define NTAP_OPT_ENABLED 3
457 #define NTAP_OPT_DISABLED 4
458 #define NTAP_OPT_EXPECTED_ID 5
459 #define NTAP_OPT_VERSION 6
461 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
462 struct jtag_tap *pTap)
464 jim_wide w;
465 int e = Jim_GetOpt_Wide(goi, &w);
466 if (e != JIM_OK) {
467 Jim_SetResultFormatted(goi->interp,
468 "option: %s bad parameter", n->name);
469 free((void *)pTap->dotted_name);
470 return e;
472 switch (n->value) {
473 case NTAP_OPT_IRLEN:
474 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value))) {
475 LOG_WARNING("%s: huge IR length %d",
476 pTap->dotted_name, (int) w);
478 pTap->ir_length = w;
479 break;
480 case NTAP_OPT_IRMASK:
481 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)) {
493 LOG_ERROR("%s: IR capture %x too big",
494 pTap->dotted_name, (int) w);
495 return JIM_ERR;
497 if ((w & 3) != 1)
498 LOG_WARNING("%s: nonstandard IR value",
499 pTap->dotted_name);
500 pTap->ir_capture_value = w;
501 break;
502 default:
503 return JIM_ERR;
505 return JIM_OK;
508 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
510 struct jtag_tap *pTap;
511 int x;
512 int e;
513 Jim_Nvp *n;
514 char *cp;
515 const Jim_Nvp opts[] = {
516 { .name = "-irlen", .value = NTAP_OPT_IRLEN },
517 { .name = "-irmask", .value = NTAP_OPT_IRMASK },
518 { .name = "-ircapture", .value = NTAP_OPT_IRCAPTURE },
519 { .name = "-enable", .value = NTAP_OPT_ENABLED },
520 { .name = "-disable", .value = NTAP_OPT_DISABLED },
521 { .name = "-expected-id", .value = NTAP_OPT_EXPECTED_ID },
522 { .name = "-ignore-version", .value = NTAP_OPT_VERSION },
523 { .name = NULL, .value = -1 },
526 pTap = calloc(1, sizeof(struct jtag_tap));
527 if (!pTap) {
528 Jim_SetResultFormatted(goi->interp, "no memory");
529 return JIM_ERR;
533 * we expect CHIP + TAP + OPTIONS
534 * */
535 if (goi->argc < 3) {
536 Jim_SetResultFormatted(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) {
581 free((void *)pTap->dotted_name);
582 free(pTap);
583 return e;
585 break;
586 case NTAP_OPT_IRLEN:
587 case NTAP_OPT_IRMASK:
588 case NTAP_OPT_IRCAPTURE:
589 e = jim_newtap_ir_param(n, goi, pTap);
590 if (JIM_OK != e) {
591 free((void *)pTap->dotted_name);
592 free(pTap);
593 return e;
595 break;
596 case NTAP_OPT_VERSION:
597 pTap->ignore_version = true;
598 break;
599 } /* switch (n->value) */
600 } /* while (goi->argc) */
602 /* default is enabled-after-reset */
603 pTap->enabled = !pTap->disabled_after_reset;
605 /* Did all the required option bits get cleared? */
606 if (pTap->ir_length != 0) {
607 jtag_tap_init(pTap);
608 return JIM_OK;
611 Jim_SetResultFormatted(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) {
623 if (jteap->event != e)
624 continue;
626 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
627 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
628 tap->dotted_name, e, nvp->name,
629 Jim_GetString(jteap->body, NULL));
631 if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK) {
632 Jim_MakeErrorMessage(jteap->interp);
633 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
634 continue;
637 switch (e) {
638 case JTAG_TAP_EVENT_ENABLE:
639 case JTAG_TAP_EVENT_DISABLE:
640 /* NOTE: we currently assume the handlers
641 * can't fail. Right here is where we should
642 * really be verifying the scan chains ...
644 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
645 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
646 tap->enabled ? "enabled" : "disabled");
647 break;
648 default:
649 break;
654 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
656 Jim_GetOptInfo goi;
657 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
658 if (goi.argc != 0) {
659 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
660 return JIM_ERR;
662 struct command_context *context = current_command_context(interp);
663 int e = jtag_init_inner(context);
664 if (e != ERROR_OK) {
665 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
666 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
667 Jim_FreeNewObj(goi.interp, eObj);
668 return JIM_ERR;
670 return JIM_OK;
673 static int jim_jtag_arp_init_reset(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 = current_command_context(interp);
682 int e = jtag_init_reset(context);
683 if (e != ERROR_OK) {
684 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
685 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
686 Jim_FreeNewObj(goi.interp, eObj);
687 return JIM_ERR;
689 return JIM_OK;
692 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
694 Jim_GetOptInfo goi;
695 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
696 return jim_newtap_cmd(&goi);
699 static bool jtag_tap_enable(struct jtag_tap *t)
701 if (t->enabled)
702 return false;
703 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
704 if (!t->enabled)
705 return false;
707 /* FIXME add JTAG sanity checks, w/o TLR
708 * - scan chain length grew by one (this)
709 * - IDs and IR lengths are as expected
711 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
712 return true;
714 static bool jtag_tap_disable(struct jtag_tap *t)
716 if (!t->enabled)
717 return false;
718 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
719 if (t->enabled)
720 return false;
722 /* FIXME add JTAG sanity checks, w/o TLR
723 * - scan chain length shrank by one (this)
724 * - IDs and IR lengths are as expected
726 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
727 return true;
730 int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
732 const char *cmd_name = Jim_GetString(argv[0], NULL);
733 Jim_GetOptInfo goi;
734 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
735 if (goi.argc != 1) {
736 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
737 return JIM_ERR;
740 struct jtag_tap *t;
742 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
743 if (t == NULL)
744 return JIM_ERR;
746 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
747 /* do nothing, just return the value */
748 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
749 if (!jtag_tap_enable(t)) {
750 LOG_WARNING("failed to enable tap %s", t->dotted_name);
751 return JIM_ERR;
753 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
754 if (!jtag_tap_disable(t)) {
755 LOG_WARNING("failed to disable tap %s", t->dotted_name);
756 return JIM_ERR;
758 } else {
759 LOG_ERROR("command '%s' unknown", cmd_name);
760 return JIM_ERR;
762 bool e = t->enabled;
763 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
764 return JIM_OK;
767 int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
769 const char *cmd_name = Jim_GetString(argv[0], NULL);
770 Jim_GetOptInfo goi;
771 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
772 goi.isconfigure = !strcmp(cmd_name, "configure");
773 if (goi.argc < 2 + goi.isconfigure) {
774 Jim_WrongNumArgs(goi.interp, 0, NULL,
775 "<tap_name> <attribute> ...");
776 return JIM_ERR;
779 struct jtag_tap *t;
781 Jim_Obj *o;
782 Jim_GetOpt_Obj(&goi, &o);
783 t = jtag_tap_by_jim_obj(goi.interp, o);
784 if (t == NULL)
785 return JIM_ERR;
787 return jtag_tap_configure_cmd(&goi, t);
790 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
792 Jim_GetOptInfo goi;
793 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
794 if (goi.argc != 0) {
795 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
796 return JIM_ERR;
798 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
799 struct jtag_tap *tap;
801 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
802 Jim_ListAppendElement(goi.interp,
803 Jim_GetResult(goi.interp),
804 Jim_NewStringObj(goi.interp,
805 tap->dotted_name, -1));
807 return JIM_OK;
810 COMMAND_HANDLER(handle_jtag_init_command)
812 if (CMD_ARGC != 0)
813 return ERROR_COMMAND_SYNTAX_ERROR;
815 static bool jtag_initialized;
816 if (jtag_initialized) {
817 LOG_INFO("'jtag init' has already been called");
818 return ERROR_OK;
820 jtag_initialized = true;
822 LOG_DEBUG("Initializing jtag devices...");
823 return jtag_init(CMD_CTX);
826 static const struct command_registration jtag_subcommand_handlers[] = {
828 .name = "init",
829 .mode = COMMAND_ANY,
830 .handler = handle_jtag_init_command,
831 .help = "initialize jtag scan chain",
832 .usage = ""
835 .name = "arp_init",
836 .mode = COMMAND_ANY,
837 .jim_handler = jim_jtag_arp_init,
838 .help = "Validates JTAG scan chain against the list of "
839 "declared TAPs using just the four standard JTAG "
840 "signals.",
843 .name = "arp_init-reset",
844 .mode = COMMAND_ANY,
845 .jim_handler = jim_jtag_arp_init_reset,
846 .help = "Uses TRST and SRST to try resetting everything on "
847 "the JTAG scan chain, then performs 'jtag arp_init'."
850 .name = "newtap",
851 .mode = COMMAND_CONFIG,
852 .jim_handler = jim_jtag_newtap,
853 .help = "Create a new TAP instance named basename.tap_type, "
854 "and appends it to the scan chain.",
855 .usage = "basename tap_type '-irlen' count "
856 "['-enable'|'-disable'] "
857 "['-expected_id' number] "
858 "['-ignore-version'] "
859 "['-ircapture' number] "
860 "['-mask' number] ",
863 .name = "tapisenabled",
864 .mode = COMMAND_EXEC,
865 .jim_handler = jim_jtag_tap_enabler,
866 .help = "Returns a Tcl boolean (0/1) indicating whether "
867 "the TAP is enabled (1) or not (0).",
868 .usage = "tap_name",
871 .name = "tapenable",
872 .mode = COMMAND_EXEC,
873 .jim_handler = jim_jtag_tap_enabler,
874 .help = "Try to enable the specified TAP using the "
875 "'tap-enable' TAP event.",
876 .usage = "tap_name",
879 .name = "tapdisable",
880 .mode = COMMAND_EXEC,
881 .jim_handler = jim_jtag_tap_enabler,
882 .help = "Try to disable the specified TAP using the "
883 "'tap-disable' TAP event.",
884 .usage = "tap_name",
887 .name = "configure",
888 .mode = COMMAND_EXEC,
889 .jim_handler = jim_jtag_configure,
890 .help = "Provide a Tcl handler for the specified "
891 "TAP event.",
892 .usage = "tap_name '-event' event_name handler",
895 .name = "cget",
896 .mode = COMMAND_EXEC,
897 .jim_handler = jim_jtag_configure,
898 .help = "Return any Tcl handler for the specified "
899 "TAP event.",
900 .usage = "tap_name '-event' event_name",
903 .name = "names",
904 .mode = COMMAND_ANY,
905 .jim_handler = jim_jtag_names,
906 .help = "Returns list of all JTAG tap names.",
909 .chain = jtag_command_handlers_to_move,
911 COMMAND_REGISTRATION_DONE
914 void jtag_notify_event(enum jtag_event event)
916 struct jtag_tap *tap;
918 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
919 jtag_tap_handle_event(tap, event);
923 COMMAND_HANDLER(handle_scan_chain_command)
925 struct jtag_tap *tap;
926 char expected_id[12];
928 tap = jtag_all_taps();
929 command_print(CMD_CTX,
930 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
931 command_print(CMD_CTX,
932 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
934 while (tap) {
935 uint32_t expected, expected_mask, ii;
937 snprintf(expected_id, sizeof expected_id, "0x%08x",
938 (unsigned)((tap->expected_ids_cnt > 0)
939 ? tap->expected_ids[0]
940 : 0));
941 if (tap->ignore_version)
942 expected_id[2] = '*';
944 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
945 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
947 command_print(CMD_CTX,
948 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
949 tap->abs_chain_position,
950 tap->dotted_name,
951 tap->enabled ? 'Y' : 'n',
952 (unsigned int)(tap->idcode),
953 expected_id,
954 (unsigned int)(tap->ir_length),
955 (unsigned int)(expected),
956 (unsigned int)(expected_mask));
958 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
959 snprintf(expected_id, sizeof expected_id, "0x%08x",
960 (unsigned) tap->expected_ids[ii]);
961 if (tap->ignore_version)
962 expected_id[2] = '*';
964 command_print(CMD_CTX,
965 " %s",
966 expected_id);
969 tap = tap->next_tap;
972 return ERROR_OK;
975 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
977 if (CMD_ARGC > 1)
978 return ERROR_COMMAND_SYNTAX_ERROR;
979 if (CMD_ARGC == 1) {
980 unsigned delay;
981 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
983 jtag_set_ntrst_delay(delay);
985 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
986 return ERROR_OK;
989 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
991 if (CMD_ARGC > 1)
992 return ERROR_COMMAND_SYNTAX_ERROR;
993 if (CMD_ARGC == 1) {
994 unsigned delay;
995 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
997 jtag_set_ntrst_assert_width(delay);
999 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1000 return ERROR_OK;
1003 COMMAND_HANDLER(handle_jtag_rclk_command)
1005 if (CMD_ARGC > 1)
1006 return ERROR_COMMAND_SYNTAX_ERROR;
1008 int retval = ERROR_OK;
1009 if (CMD_ARGC == 1) {
1010 unsigned khz = 0;
1011 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1013 retval = jtag_config_rclk(khz);
1014 if (ERROR_OK != retval)
1015 return retval;
1018 int cur_khz = jtag_get_speed_khz();
1019 retval = jtag_get_speed_readable(&cur_khz);
1020 if (ERROR_OK != retval)
1021 return retval;
1023 if (cur_khz)
1024 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1025 else
1026 command_print(CMD_CTX, "RCLK - adaptive");
1028 return retval;
1031 COMMAND_HANDLER(handle_jtag_reset_command)
1033 if (CMD_ARGC != 2)
1034 return ERROR_COMMAND_SYNTAX_ERROR;
1036 int trst = -1;
1037 if (CMD_ARGV[0][0] == '1')
1038 trst = 1;
1039 else if (CMD_ARGV[0][0] == '0')
1040 trst = 0;
1041 else
1042 return ERROR_COMMAND_SYNTAX_ERROR;
1044 int srst = -1;
1045 if (CMD_ARGV[1][0] == '1')
1046 srst = 1;
1047 else if (CMD_ARGV[1][0] == '0')
1048 srst = 0;
1049 else
1050 return ERROR_COMMAND_SYNTAX_ERROR;
1052 if (adapter_init(CMD_CTX) != ERROR_OK)
1053 return ERROR_JTAG_INIT_FAILED;
1055 jtag_add_reset(trst, srst);
1056 return jtag_execute_queue();
1059 COMMAND_HANDLER(handle_runtest_command)
1061 if (CMD_ARGC != 1)
1062 return ERROR_COMMAND_SYNTAX_ERROR;
1064 unsigned num_clocks;
1065 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1067 jtag_add_runtest(num_clocks, TAP_IDLE);
1068 return jtag_execute_queue();
1072 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1073 * should be stable ... and *NOT* a shift state, otherwise free-running
1074 * jtag clocks could change the values latched by the update state.
1075 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1076 * and "drscan" commands are a write-only subset of what SVF provides.
1079 COMMAND_HANDLER(handle_irscan_command)
1081 int i;
1082 struct scan_field *fields;
1083 struct jtag_tap *tap = NULL;
1084 tap_state_t endstate;
1086 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1087 return ERROR_COMMAND_SYNTAX_ERROR;
1089 /* optional "-endstate" "statename" at the end of the arguments,
1090 * so that e.g. IRPAUSE can let us load the data register before
1091 * entering RUN/IDLE to execute the instruction we load here.
1093 endstate = TAP_IDLE;
1095 if (CMD_ARGC >= 4) {
1096 /* have at least one pair of numbers.
1097 * is last pair the magic text? */
1098 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1099 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1100 if (endstate == TAP_INVALID)
1101 return ERROR_COMMAND_SYNTAX_ERROR;
1102 if (!scan_is_safe(endstate))
1103 LOG_WARNING("unstable irscan endstate \"%s\"",
1104 CMD_ARGV[CMD_ARGC - 1]);
1105 CMD_ARGC -= 2;
1109 int num_fields = CMD_ARGC / 2;
1110 if (num_fields > 1) {
1111 /* we really should be looking at plain_ir_scan if we want
1112 * anything more fancy.
1114 LOG_ERROR("Specify a single value for tap");
1115 return ERROR_COMMAND_SYNTAX_ERROR;
1118 size_t fields_len = sizeof(struct scan_field) * num_fields;
1119 fields = malloc(fields_len);
1120 memset(fields, 0, fields_len);
1122 int retval;
1123 for (i = 0; i < num_fields; i++) {
1124 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1125 if (tap == NULL) {
1126 int j;
1127 for (j = 0; j < i; j++)
1128 free((void *)fields[j].out_value);
1129 free(fields);
1130 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1132 return ERROR_FAIL;
1134 int field_size = tap->ir_length;
1135 fields[i].num_bits = field_size;
1136 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1138 uint32_t value;
1139 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1140 if (ERROR_OK != retval)
1141 goto error_return;
1142 void *v = (void *)fields[i].out_value;
1143 buf_set_u32(v, 0, field_size, value);
1144 fields[i].in_value = NULL;
1147 /* did we have an endstate? */
1148 jtag_add_ir_scan(tap, fields, endstate);
1150 retval = jtag_execute_queue();
1152 error_return:
1153 for (i = 0; i < num_fields; i++) {
1154 if (NULL != fields[i].out_value)
1155 free((void *)fields[i].out_value);
1158 free(fields);
1160 return retval;
1163 COMMAND_HANDLER(handle_verify_ircapture_command)
1165 if (CMD_ARGC > 1)
1166 return ERROR_COMMAND_SYNTAX_ERROR;
1168 if (CMD_ARGC == 1) {
1169 bool enable;
1170 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1171 jtag_set_verify_capture_ir(enable);
1174 const char *status = jtag_will_verify_capture_ir() ? "enabled" : "disabled";
1175 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1177 return ERROR_OK;
1180 COMMAND_HANDLER(handle_verify_jtag_command)
1182 if (CMD_ARGC > 1)
1183 return ERROR_COMMAND_SYNTAX_ERROR;
1185 if (CMD_ARGC == 1) {
1186 bool enable;
1187 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1188 jtag_set_verify(enable);
1191 const char *status = jtag_will_verify() ? "enabled" : "disabled";
1192 command_print(CMD_CTX, "verify jtag capture is %s", status);
1194 return ERROR_OK;
1197 COMMAND_HANDLER(handle_tms_sequence_command)
1199 if (CMD_ARGC > 1)
1200 return ERROR_COMMAND_SYNTAX_ERROR;
1202 if (CMD_ARGC == 1) {
1203 bool use_new_table;
1204 if (strcmp(CMD_ARGV[0], "short") == 0)
1205 use_new_table = true;
1206 else if (strcmp(CMD_ARGV[0], "long") == 0)
1207 use_new_table = false;
1208 else
1209 return ERROR_COMMAND_SYNTAX_ERROR;
1211 tap_use_new_tms_table(use_new_table);
1214 command_print(CMD_CTX, "tms sequence is %s",
1215 tap_uses_new_tms_table() ? "short" : "long");
1217 return ERROR_OK;
1220 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1222 if (CMD_ARGC != 1)
1223 return ERROR_COMMAND_SYNTAX_ERROR;
1225 int sleep_ms;
1226 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1228 jtag_set_flush_queue_sleep(sleep_ms);
1230 return ERROR_OK;
1233 COMMAND_HANDLER(handle_wait_srst_deassert)
1235 if (CMD_ARGC != 1)
1236 return ERROR_COMMAND_SYNTAX_ERROR;
1238 int timeout_ms;
1239 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1240 if ((timeout_ms <= 0) || (timeout_ms > 100000)) {
1241 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1242 return ERROR_FAIL;
1245 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1246 int asserted_yet;
1247 long long then = timeval_ms();
1248 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1249 if ((timeval_ms() - then) > timeout_ms) {
1250 LOG_ERROR("Timed out");
1251 return ERROR_FAIL;
1253 if (asserted_yet)
1254 break;
1256 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1257 if ((timeval_ms() - then) > timeout_ms) {
1258 LOG_ERROR("Timed out");
1259 return ERROR_FAIL;
1261 if (!asserted_yet)
1262 break;
1265 return ERROR_OK;
1268 static const struct command_registration jtag_command_handlers[] = {
1271 .name = "jtag_flush_queue_sleep",
1272 .handler = handle_jtag_flush_queue_sleep,
1273 .mode = COMMAND_ANY,
1274 .help = "For debug purposes(simulate long delays of interface) "
1275 "to test performance or change in behavior. Default 0ms.",
1276 .usage = "[sleep in ms]",
1279 .name = "jtag_rclk",
1280 .handler = handle_jtag_rclk_command,
1281 .mode = COMMAND_ANY,
1282 .help = "With an argument, change to to use adaptive clocking "
1283 "if possible; else to use the fallback speed. "
1284 "With or without argument, display current setting.",
1285 .usage = "[fallback_speed_khz]",
1288 .name = "jtag_ntrst_delay",
1289 .handler = handle_jtag_ntrst_delay_command,
1290 .mode = COMMAND_ANY,
1291 .help = "delay after deasserting trst in ms",
1292 .usage = "[milliseconds]",
1295 .name = "jtag_ntrst_assert_width",
1296 .handler = handle_jtag_ntrst_assert_width_command,
1297 .mode = COMMAND_ANY,
1298 .help = "delay after asserting trst in ms",
1299 .usage = "[milliseconds]",
1302 .name = "scan_chain",
1303 .handler = handle_scan_chain_command,
1304 .mode = COMMAND_ANY,
1305 .help = "print current scan chain configuration",
1306 .usage = ""
1309 .name = "jtag_reset",
1310 .handler = handle_jtag_reset_command,
1311 .mode = COMMAND_EXEC,
1312 .help = "Set reset line values. Value '1' is active, "
1313 "value '0' is inactive.",
1314 .usage = "trst_active srst_active",
1317 .name = "runtest",
1318 .handler = handle_runtest_command,
1319 .mode = COMMAND_EXEC,
1320 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1321 .usage = "num_cycles"
1324 .name = "irscan",
1325 .handler = handle_irscan_command,
1326 .mode = COMMAND_EXEC,
1327 .help = "Execute Instruction Register (DR) scan. The "
1328 "specified opcodes are put into each TAP's IR, "
1329 "and other TAPs are put in BYPASS.",
1330 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1333 .name = "verify_ircapture",
1334 .handler = handle_verify_ircapture_command,
1335 .mode = COMMAND_ANY,
1336 .help = "Display or assign flag controlling whether to "
1337 "verify values captured during Capture-IR.",
1338 .usage = "['enable'|'disable']",
1341 .name = "verify_jtag",
1342 .handler = handle_verify_jtag_command,
1343 .mode = COMMAND_ANY,
1344 .help = "Display or assign flag controlling whether to "
1345 "verify values captured during IR and DR scans.",
1346 .usage = "['enable'|'disable']",
1349 .name = "tms_sequence",
1350 .handler = handle_tms_sequence_command,
1351 .mode = COMMAND_ANY,
1352 .help = "Display or change what style TMS sequences to use "
1353 "for JTAG state transitions: short (default) or "
1354 "long. Only for working around JTAG bugs.",
1355 /* Specifically for working around DRIVER bugs... */
1356 .usage = "['short'|'long']",
1359 .name = "wait_srst_deassert",
1360 .handler = handle_wait_srst_deassert,
1361 .mode = COMMAND_ANY,
1362 .help = "Wait for an SRST deassert. "
1363 "Useful for cases where you need something to happen within ms "
1364 "of an srst deassert. Timeout in ms ",
1365 .usage = "ms",
1368 .name = "jtag",
1369 .mode = COMMAND_ANY,
1370 .help = "perform jtag tap actions",
1371 .usage = "",
1373 .chain = jtag_subcommand_handlers,
1376 .chain = jtag_command_handlers_to_move,
1378 COMMAND_REGISTRATION_DONE
1381 int jtag_register_commands(struct command_context *cmd_ctx)
1383 return register_commands(cmd_ctx, NULL, jtag_command_handlers);