cortex_a: fix virt2phys when mmu is disabled
[openocd.git] / src / jtag / tcl.c
blobe32f0ca078ceb2436568a9b5f699539996fd2c85
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, see <http://www.gnu.org/licenses/>. *
27 ***************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #include "jtag.h"
34 #include "swd.h"
35 #include "minidriver.h"
36 #include "interface.h"
37 #include "interfaces.h"
38 #include "tcl.h"
40 #ifdef HAVE_STRINGS_H
41 #include <strings.h>
42 #endif
44 #include <helper/time_support.h>
45 #include "transport/transport.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 struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
63 const char *cp = Jim_GetString(o, NULL);
64 struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
65 if (NULL == cp)
66 cp = "(unknown)";
67 if (NULL == t)
68 Jim_SetResultFormatted(interp, "Tap '%s' could not be found", cp);
69 return t;
72 static bool scan_is_safe(tap_state_t state)
74 switch (state) {
75 case TAP_RESET:
76 case TAP_IDLE:
77 case TAP_DRPAUSE:
78 case TAP_IRPAUSE:
79 return true;
80 default:
81 return false;
85 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
87 int retval;
88 struct scan_field *fields;
89 int num_fields;
90 int field_count = 0;
91 int i, e;
92 struct jtag_tap *tap;
93 tap_state_t endstate;
95 /* args[1] = device
96 * args[2] = num_bits
97 * args[3] = hex string
98 * ... repeat num bits and hex string ...
100 * .. optionally:
101 * args[N-2] = "-endstate"
102 * args[N-1] = statename
104 if ((argc < 4) || ((argc % 2) != 0)) {
105 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
106 return JIM_ERR;
109 endstate = TAP_IDLE;
111 script_debug(interp, "drscan", argc, args);
113 /* validate arguments as numbers */
114 e = JIM_OK;
115 for (i = 2; i < argc; i += 2) {
116 long bits;
117 const char *cp;
119 e = Jim_GetLong(interp, args[i], &bits);
120 /* If valid - try next arg */
121 if (e == JIM_OK)
122 continue;
124 /* Not valid.. are we at the end? */
125 if (((i + 2) != argc)) {
126 /* nope, then error */
127 return e;
130 /* it could be: "-endstate FOO"
131 * e.g. DRPAUSE so we can issue more instructions
132 * before entering RUN/IDLE and executing them.
135 /* get arg as a string. */
136 cp = Jim_GetString(args[i], NULL);
137 /* is it the magic? */
138 if (0 == strcmp("-endstate", cp)) {
139 /* is the statename valid? */
140 cp = Jim_GetString(args[i + 1], NULL);
142 /* see if it is a valid state name */
143 endstate = tap_state_by_name(cp);
144 if (endstate < 0) {
145 /* update the error message */
146 Jim_SetResultFormatted(interp, "endstate: %s invalid", cp);
147 } else {
148 if (!scan_is_safe(endstate))
149 LOG_WARNING("drscan with unsafe "
150 "endstate \"%s\"", cp);
152 /* valid - so clear the error */
153 e = JIM_OK;
154 /* and remove the last 2 args */
155 argc -= 2;
159 /* Still an error? */
160 if (e != JIM_OK)
161 return e; /* too bad */
162 } /* validate args */
164 assert(e == JIM_OK);
166 tap = jtag_tap_by_jim_obj(interp, args[1]);
167 if (tap == NULL)
168 return JIM_ERR;
170 num_fields = (argc-2)/2;
171 if (num_fields <= 0) {
172 Jim_SetResultString(interp, "drscan: no scan fields supplied", -1);
173 return JIM_ERR;
175 fields = malloc(sizeof(struct scan_field) * num_fields);
176 for (i = 2; i < argc; i += 2) {
177 long bits;
178 int len;
179 const char *str;
181 Jim_GetLong(interp, args[i], &bits);
182 str = Jim_GetString(args[i + 1], &len);
184 fields[field_count].num_bits = bits;
185 void *t = malloc(DIV_ROUND_UP(bits, 8));
186 fields[field_count].out_value = t;
187 str_to_buf(str, len, t, bits, 0);
188 fields[field_count].in_value = t;
189 field_count++;
192 jtag_add_dr_scan(tap, num_fields, fields, endstate);
194 retval = jtag_execute_queue();
195 if (retval != ERROR_OK) {
196 Jim_SetResultString(interp, "drscan: jtag execute failed", -1);
197 return JIM_ERR;
200 field_count = 0;
201 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
202 for (i = 2; i < argc; i += 2) {
203 long bits;
204 char *str;
206 Jim_GetLong(interp, args[i], &bits);
207 str = buf_to_str(fields[field_count].in_value, bits, 16);
208 free(fields[field_count].in_value);
210 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
211 free(str);
212 field_count++;
215 Jim_SetResult(interp, list);
217 free(fields);
219 return JIM_OK;
223 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
225 tap_state_t states[8];
227 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1))) {
228 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
229 return JIM_ERR;
232 script_debug(interp, "pathmove", argc, args);
234 int i;
235 for (i = 0; i < argc-1; i++) {
236 const char *cp;
237 cp = Jim_GetString(args[i + 1], NULL);
238 states[i] = tap_state_by_name(cp);
239 if (states[i] < 0) {
240 /* update the error message */
241 Jim_SetResultFormatted(interp, "endstate: %s invalid", cp);
242 return JIM_ERR;
246 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) {
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) {
322 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
323 return JIM_ERR;
326 Jim_Nvp *n;
327 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
328 if (e != JIM_OK) {
329 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
330 return e;
333 if (goi->isconfigure) {
334 if (goi->argc != 1) {
335 Jim_WrongNumArgs(goi->interp,
336 goi->argc,
337 goi->argv,
338 "-event <event-name> <event-body>");
339 return JIM_ERR;
341 } else {
342 if (goi->argc != 0) {
343 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
344 return JIM_ERR;
348 struct jtag_tap_event_action *jteap = tap->event_action;
349 /* replace existing event body */
350 bool found = false;
351 while (jteap) {
352 if (jteap->event == (enum jtag_event)n->value) {
353 found = true;
354 break;
356 jteap = jteap->next;
359 Jim_SetEmptyResult(goi->interp);
361 if (goi->isconfigure) {
362 if (!found)
363 jteap = calloc(1, sizeof(*jteap));
364 else if (NULL != jteap->body)
365 Jim_DecrRefCount(goi->interp, jteap->body);
367 jteap->interp = goi->interp;
368 jteap->event = n->value;
370 Jim_Obj *o;
371 Jim_GetOpt_Obj(goi, &o);
372 jteap->body = Jim_DuplicateObj(goi->interp, o);
373 Jim_IncrRefCount(jteap->body);
375 if (!found) {
376 /* add to head of event list */
377 jteap->next = tap->event_action;
378 tap->event_action = jteap;
380 } else if (found) {
381 jteap->interp = goi->interp;
382 Jim_SetResult(goi->interp,
383 Jim_DuplicateObj(goi->interp, jteap->body));
385 return JIM_OK;
388 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap *tap)
390 /* parse config or cget options */
391 while (goi->argc > 0) {
392 Jim_SetEmptyResult(goi->interp);
394 Jim_Nvp *n;
395 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
396 if (e != JIM_OK) {
397 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
398 return e;
401 switch (n->value) {
402 case JCFG_EVENT:
403 e = jtag_tap_configure_event(goi, tap);
404 if (e != JIM_OK)
405 return e;
406 break;
407 default:
408 Jim_SetResultFormatted(goi->interp, "unknown event: %s", n->name);
409 return JIM_ERR;
413 return JIM_OK;
416 static int is_bad_irval(int ir_length, jim_wide w)
418 jim_wide v = 1;
420 v <<= ir_length;
421 v -= 1;
422 v = ~v;
423 return (w & v) != 0;
426 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
427 struct jtag_tap *pTap)
429 jim_wide w;
430 int e = Jim_GetOpt_Wide(goi, &w);
431 if (e != JIM_OK) {
432 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
433 return e;
436 uint32_t *p = realloc(pTap->expected_ids,
437 (pTap->expected_ids_cnt + 1) * sizeof(uint32_t));
438 if (!p) {
439 Jim_SetResultFormatted(goi->interp, "no memory");
440 return JIM_ERR;
443 pTap->expected_ids = p;
444 pTap->expected_ids[pTap->expected_ids_cnt++] = w;
446 return JIM_OK;
449 #define NTAP_OPT_IRLEN 0
450 #define NTAP_OPT_IRMASK 1
451 #define NTAP_OPT_IRCAPTURE 2
452 #define NTAP_OPT_ENABLED 3
453 #define NTAP_OPT_DISABLED 4
454 #define NTAP_OPT_EXPECTED_ID 5
455 #define NTAP_OPT_VERSION 6
457 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
458 struct jtag_tap *pTap)
460 jim_wide w;
461 int e = Jim_GetOpt_Wide(goi, &w);
462 if (e != JIM_OK) {
463 Jim_SetResultFormatted(goi->interp,
464 "option: %s bad parameter", n->name);
465 return e;
467 switch (n->value) {
468 case NTAP_OPT_IRLEN:
469 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value))) {
470 LOG_WARNING("%s: huge IR length %d",
471 pTap->dotted_name, (int) w);
473 pTap->ir_length = w;
474 break;
475 case NTAP_OPT_IRMASK:
476 if (is_bad_irval(pTap->ir_length, w)) {
477 LOG_ERROR("%s: IR mask %x too big",
478 pTap->dotted_name,
479 (int) w);
480 return JIM_ERR;
482 if ((w & 3) != 3)
483 LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
484 pTap->ir_capture_mask = w;
485 break;
486 case NTAP_OPT_IRCAPTURE:
487 if (is_bad_irval(pTap->ir_length, w)) {
488 LOG_ERROR("%s: IR capture %x too big",
489 pTap->dotted_name, (int) w);
490 return JIM_ERR;
492 if ((w & 3) != 1)
493 LOG_WARNING("%s: nonstandard IR value",
494 pTap->dotted_name);
495 pTap->ir_capture_value = w;
496 break;
497 default:
498 return JIM_ERR;
500 return JIM_OK;
503 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
505 struct jtag_tap *pTap;
506 int x;
507 int e;
508 Jim_Nvp *n;
509 char *cp;
510 const Jim_Nvp opts[] = {
511 { .name = "-irlen", .value = NTAP_OPT_IRLEN },
512 { .name = "-irmask", .value = NTAP_OPT_IRMASK },
513 { .name = "-ircapture", .value = NTAP_OPT_IRCAPTURE },
514 { .name = "-enable", .value = NTAP_OPT_ENABLED },
515 { .name = "-disable", .value = NTAP_OPT_DISABLED },
516 { .name = "-expected-id", .value = NTAP_OPT_EXPECTED_ID },
517 { .name = "-ignore-version", .value = NTAP_OPT_VERSION },
518 { .name = NULL, .value = -1 },
521 pTap = calloc(1, sizeof(struct jtag_tap));
522 if (!pTap) {
523 Jim_SetResultFormatted(goi->interp, "no memory");
524 return JIM_ERR;
528 * we expect CHIP + TAP + OPTIONS
529 * */
530 if (goi->argc < 3) {
531 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
532 free(pTap);
533 return JIM_ERR;
536 const char *tmp;
537 Jim_GetOpt_String(goi, &tmp, NULL);
538 pTap->chip = strdup(tmp);
540 Jim_GetOpt_String(goi, &tmp, NULL);
541 pTap->tapname = strdup(tmp);
543 /* name + dot + name + null */
544 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
545 cp = malloc(x);
546 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
547 pTap->dotted_name = cp;
549 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
550 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
552 if (!transport_is_jtag()) {
553 /* SWD doesn't require any JTAG tap parameters */
554 pTap->enabled = true;
555 jtag_tap_init(pTap);
556 return JIM_OK;
559 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
560 * that the default. The "-ircapture" and "-irmask" options are only
561 * needed to cope with nonstandard TAPs, or to specify more bits.
563 pTap->ir_capture_mask = 0x03;
564 pTap->ir_capture_value = 0x01;
566 while (goi->argc) {
567 e = Jim_GetOpt_Nvp(goi, opts, &n);
568 if (e != JIM_OK) {
569 Jim_GetOpt_NvpUnknown(goi, opts, 0);
570 free(cp);
571 free(pTap);
572 return e;
574 LOG_DEBUG("Processing option: %s", n->name);
575 switch (n->value) {
576 case NTAP_OPT_ENABLED:
577 pTap->disabled_after_reset = false;
578 break;
579 case NTAP_OPT_DISABLED:
580 pTap->disabled_after_reset = true;
581 break;
582 case NTAP_OPT_EXPECTED_ID:
583 e = jim_newtap_expected_id(n, goi, pTap);
584 if (JIM_OK != e) {
585 free(cp);
586 free(pTap);
587 return e;
589 break;
590 case NTAP_OPT_IRLEN:
591 case NTAP_OPT_IRMASK:
592 case NTAP_OPT_IRCAPTURE:
593 e = jim_newtap_ir_param(n, goi, pTap);
594 if (JIM_OK != e) {
595 free(cp);
596 free(pTap);
597 return e;
599 break;
600 case NTAP_OPT_VERSION:
601 pTap->ignore_version = true;
602 break;
603 } /* switch (n->value) */
604 } /* while (goi->argc) */
606 /* default is enabled-after-reset */
607 pTap->enabled = !pTap->disabled_after_reset;
609 /* Did all the required option bits get cleared? */
610 if (pTap->ir_length != 0) {
611 jtag_tap_init(pTap);
612 return JIM_OK;
615 Jim_SetResultFormatted(goi->interp,
616 "newtap: %s missing IR length",
617 pTap->dotted_name);
618 jtag_tap_free(pTap);
619 return JIM_ERR;
622 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
624 struct jtag_tap_event_action *jteap;
626 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next) {
627 if (jteap->event != e)
628 continue;
630 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
631 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
632 tap->dotted_name, e, nvp->name,
633 Jim_GetString(jteap->body, NULL));
635 if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK) {
636 Jim_MakeErrorMessage(jteap->interp);
637 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
638 continue;
641 switch (e) {
642 case JTAG_TAP_EVENT_ENABLE:
643 case JTAG_TAP_EVENT_DISABLE:
644 /* NOTE: we currently assume the handlers
645 * can't fail. Right here is where we should
646 * really be verifying the scan chains ...
648 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
649 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
650 tap->enabled ? "enabled" : "disabled");
651 break;
652 default:
653 break;
658 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
660 Jim_GetOptInfo goi;
661 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
662 if (goi.argc != 0) {
663 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
664 return JIM_ERR;
666 struct command_context *context = current_command_context(interp);
667 int e = jtag_init_inner(context);
668 if (e != ERROR_OK) {
669 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
670 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
671 Jim_FreeNewObj(goi.interp, eObj);
672 return JIM_ERR;
674 return JIM_OK;
677 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
679 int e = ERROR_OK;
680 Jim_GetOptInfo goi;
681 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
682 if (goi.argc != 0) {
683 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
684 return JIM_ERR;
686 struct command_context *context = current_command_context(interp);
687 if (transport_is_jtag())
688 e = jtag_init_reset(context);
689 else if (transport_is_swd())
690 e = swd_init_reset(context);
692 if (e != ERROR_OK) {
693 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
694 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
695 Jim_FreeNewObj(goi.interp, eObj);
696 return JIM_ERR;
698 return JIM_OK;
701 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
703 Jim_GetOptInfo goi;
704 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
705 return jim_newtap_cmd(&goi);
708 static bool jtag_tap_enable(struct jtag_tap *t)
710 if (t->enabled)
711 return false;
712 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
713 if (!t->enabled)
714 return false;
716 /* FIXME add JTAG sanity checks, w/o TLR
717 * - scan chain length grew by one (this)
718 * - IDs and IR lengths are as expected
720 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
721 return true;
723 static bool jtag_tap_disable(struct jtag_tap *t)
725 if (!t->enabled)
726 return false;
727 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
728 if (t->enabled)
729 return false;
731 /* FIXME add JTAG sanity checks, w/o TLR
732 * - scan chain length shrank by one (this)
733 * - IDs and IR lengths are as expected
735 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
736 return true;
739 int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
741 const char *cmd_name = Jim_GetString(argv[0], NULL);
742 Jim_GetOptInfo goi;
743 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
744 if (goi.argc != 1) {
745 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
746 return JIM_ERR;
749 struct jtag_tap *t;
751 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
752 if (t == NULL)
753 return JIM_ERR;
755 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
756 /* do nothing, just return the value */
757 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
758 if (!jtag_tap_enable(t)) {
759 LOG_WARNING("failed to enable tap %s", t->dotted_name);
760 return JIM_ERR;
762 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
763 if (!jtag_tap_disable(t)) {
764 LOG_WARNING("failed to disable tap %s", t->dotted_name);
765 return JIM_ERR;
767 } else {
768 LOG_ERROR("command '%s' unknown", cmd_name);
769 return JIM_ERR;
771 bool e = t->enabled;
772 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
773 return JIM_OK;
776 int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
778 const char *cmd_name = Jim_GetString(argv[0], NULL);
779 Jim_GetOptInfo goi;
780 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
781 goi.isconfigure = !strcmp(cmd_name, "configure");
782 if (goi.argc < 2 + goi.isconfigure) {
783 Jim_WrongNumArgs(goi.interp, 0, NULL,
784 "<tap_name> <attribute> ...");
785 return JIM_ERR;
788 struct jtag_tap *t;
790 Jim_Obj *o;
791 Jim_GetOpt_Obj(&goi, &o);
792 t = jtag_tap_by_jim_obj(goi.interp, o);
793 if (t == NULL)
794 return JIM_ERR;
796 return jtag_tap_configure_cmd(&goi, t);
799 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
801 Jim_GetOptInfo goi;
802 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
803 if (goi.argc != 0) {
804 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
805 return JIM_ERR;
807 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
808 struct jtag_tap *tap;
810 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
811 Jim_ListAppendElement(goi.interp,
812 Jim_GetResult(goi.interp),
813 Jim_NewStringObj(goi.interp,
814 tap->dotted_name, -1));
816 return JIM_OK;
819 COMMAND_HANDLER(handle_jtag_init_command)
821 if (CMD_ARGC != 0)
822 return ERROR_COMMAND_SYNTAX_ERROR;
824 static bool jtag_initialized;
825 if (jtag_initialized) {
826 LOG_INFO("'jtag init' has already been called");
827 return ERROR_OK;
829 jtag_initialized = true;
831 LOG_DEBUG("Initializing jtag devices...");
832 return jtag_init(CMD_CTX);
835 static const struct command_registration jtag_subcommand_handlers[] = {
837 .name = "init",
838 .mode = COMMAND_ANY,
839 .handler = handle_jtag_init_command,
840 .help = "initialize jtag scan chain",
841 .usage = ""
844 .name = "arp_init",
845 .mode = COMMAND_ANY,
846 .jim_handler = jim_jtag_arp_init,
847 .help = "Validates JTAG scan chain against the list of "
848 "declared TAPs using just the four standard JTAG "
849 "signals.",
852 .name = "arp_init-reset",
853 .mode = COMMAND_ANY,
854 .jim_handler = jim_jtag_arp_init_reset,
855 .help = "Uses TRST and SRST to try resetting everything on "
856 "the JTAG scan chain, then performs 'jtag arp_init'."
859 .name = "newtap",
860 .mode = COMMAND_CONFIG,
861 .jim_handler = jim_jtag_newtap,
862 .help = "Create a new TAP instance named basename.tap_type, "
863 "and appends it to the scan chain.",
864 .usage = "basename tap_type '-irlen' count "
865 "['-enable'|'-disable'] "
866 "['-expected_id' number] "
867 "['-ignore-version'] "
868 "['-ircapture' number] "
869 "['-mask' number] ",
872 .name = "tapisenabled",
873 .mode = COMMAND_EXEC,
874 .jim_handler = jim_jtag_tap_enabler,
875 .help = "Returns a Tcl boolean (0/1) indicating whether "
876 "the TAP is enabled (1) or not (0).",
877 .usage = "tap_name",
880 .name = "tapenable",
881 .mode = COMMAND_EXEC,
882 .jim_handler = jim_jtag_tap_enabler,
883 .help = "Try to enable the specified TAP using the "
884 "'tap-enable' TAP event.",
885 .usage = "tap_name",
888 .name = "tapdisable",
889 .mode = COMMAND_EXEC,
890 .jim_handler = jim_jtag_tap_enabler,
891 .help = "Try to disable the specified TAP using the "
892 "'tap-disable' TAP event.",
893 .usage = "tap_name",
896 .name = "configure",
897 .mode = COMMAND_EXEC,
898 .jim_handler = jim_jtag_configure,
899 .help = "Provide a Tcl handler for the specified "
900 "TAP event.",
901 .usage = "tap_name '-event' event_name handler",
904 .name = "cget",
905 .mode = COMMAND_EXEC,
906 .jim_handler = jim_jtag_configure,
907 .help = "Return any Tcl handler for the specified "
908 "TAP event.",
909 .usage = "tap_name '-event' event_name",
912 .name = "names",
913 .mode = COMMAND_ANY,
914 .jim_handler = jim_jtag_names,
915 .help = "Returns list of all JTAG tap names.",
918 .chain = jtag_command_handlers_to_move,
920 COMMAND_REGISTRATION_DONE
923 void jtag_notify_event(enum jtag_event event)
925 struct jtag_tap *tap;
927 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
928 jtag_tap_handle_event(tap, event);
932 COMMAND_HANDLER(handle_scan_chain_command)
934 struct jtag_tap *tap;
935 char expected_id[12];
937 tap = jtag_all_taps();
938 command_print(CMD_CTX,
939 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
940 command_print(CMD_CTX,
941 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
943 while (tap) {
944 uint32_t expected, expected_mask, ii;
946 snprintf(expected_id, sizeof expected_id, "0x%08x",
947 (unsigned)((tap->expected_ids_cnt > 0)
948 ? tap->expected_ids[0]
949 : 0));
950 if (tap->ignore_version)
951 expected_id[2] = '*';
953 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
954 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
956 command_print(CMD_CTX,
957 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
958 tap->abs_chain_position,
959 tap->dotted_name,
960 tap->enabled ? 'Y' : 'n',
961 (unsigned int)(tap->idcode),
962 expected_id,
963 (unsigned int)(tap->ir_length),
964 (unsigned int)(expected),
965 (unsigned int)(expected_mask));
967 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
968 snprintf(expected_id, sizeof expected_id, "0x%08x",
969 (unsigned) tap->expected_ids[ii]);
970 if (tap->ignore_version)
971 expected_id[2] = '*';
973 command_print(CMD_CTX,
974 " %s",
975 expected_id);
978 tap = tap->next_tap;
981 return ERROR_OK;
984 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
986 if (CMD_ARGC > 1)
987 return ERROR_COMMAND_SYNTAX_ERROR;
988 if (CMD_ARGC == 1) {
989 unsigned delay;
990 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
992 jtag_set_ntrst_delay(delay);
994 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
995 return ERROR_OK;
998 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1000 if (CMD_ARGC > 1)
1001 return ERROR_COMMAND_SYNTAX_ERROR;
1002 if (CMD_ARGC == 1) {
1003 unsigned delay;
1004 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1006 jtag_set_ntrst_assert_width(delay);
1008 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1009 return ERROR_OK;
1012 COMMAND_HANDLER(handle_jtag_rclk_command)
1014 if (CMD_ARGC > 1)
1015 return ERROR_COMMAND_SYNTAX_ERROR;
1017 int retval = ERROR_OK;
1018 if (CMD_ARGC == 1) {
1019 unsigned khz = 0;
1020 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1022 retval = jtag_config_rclk(khz);
1023 if (ERROR_OK != retval)
1024 return retval;
1027 int cur_khz = jtag_get_speed_khz();
1028 retval = jtag_get_speed_readable(&cur_khz);
1029 if (ERROR_OK != retval)
1030 return retval;
1032 if (cur_khz)
1033 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1034 else
1035 command_print(CMD_CTX, "RCLK - adaptive");
1037 return retval;
1040 COMMAND_HANDLER(handle_jtag_reset_command)
1042 if (CMD_ARGC != 2)
1043 return ERROR_COMMAND_SYNTAX_ERROR;
1045 int trst = -1;
1046 if (CMD_ARGV[0][0] == '1')
1047 trst = 1;
1048 else if (CMD_ARGV[0][0] == '0')
1049 trst = 0;
1050 else
1051 return ERROR_COMMAND_SYNTAX_ERROR;
1053 int srst = -1;
1054 if (CMD_ARGV[1][0] == '1')
1055 srst = 1;
1056 else if (CMD_ARGV[1][0] == '0')
1057 srst = 0;
1058 else
1059 return ERROR_COMMAND_SYNTAX_ERROR;
1061 if (adapter_init(CMD_CTX) != ERROR_OK)
1062 return ERROR_JTAG_INIT_FAILED;
1064 jtag_add_reset(trst, srst);
1065 return jtag_execute_queue();
1068 COMMAND_HANDLER(handle_runtest_command)
1070 if (CMD_ARGC != 1)
1071 return ERROR_COMMAND_SYNTAX_ERROR;
1073 unsigned num_clocks;
1074 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1076 jtag_add_runtest(num_clocks, TAP_IDLE);
1077 return jtag_execute_queue();
1081 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1082 * should be stable ... and *NOT* a shift state, otherwise free-running
1083 * jtag clocks could change the values latched by the update state.
1084 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1085 * and "drscan" commands are a write-only subset of what SVF provides.
1088 COMMAND_HANDLER(handle_irscan_command)
1090 int i;
1091 struct scan_field *fields;
1092 struct jtag_tap *tap = NULL;
1093 tap_state_t endstate;
1095 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1096 return ERROR_COMMAND_SYNTAX_ERROR;
1098 /* optional "-endstate" "statename" at the end of the arguments,
1099 * so that e.g. IRPAUSE can let us load the data register before
1100 * entering RUN/IDLE to execute the instruction we load here.
1102 endstate = TAP_IDLE;
1104 if (CMD_ARGC >= 4) {
1105 /* have at least one pair of numbers.
1106 * is last pair the magic text? */
1107 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1108 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1109 if (endstate == TAP_INVALID)
1110 return ERROR_COMMAND_SYNTAX_ERROR;
1111 if (!scan_is_safe(endstate))
1112 LOG_WARNING("unstable irscan endstate \"%s\"",
1113 CMD_ARGV[CMD_ARGC - 1]);
1114 CMD_ARGC -= 2;
1118 int num_fields = CMD_ARGC / 2;
1119 if (num_fields > 1) {
1120 /* we really should be looking at plain_ir_scan if we want
1121 * anything more fancy.
1123 LOG_ERROR("Specify a single value for tap");
1124 return ERROR_COMMAND_SYNTAX_ERROR;
1127 fields = calloc(num_fields, sizeof(*fields));
1129 int retval;
1130 for (i = 0; i < num_fields; i++) {
1131 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1132 if (tap == NULL) {
1133 free(fields);
1134 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1136 return ERROR_FAIL;
1138 int field_size = tap->ir_length;
1139 fields[i].num_bits = field_size;
1140 uint8_t *v = malloc(DIV_ROUND_UP(field_size, 8));
1142 uint64_t value;
1143 retval = parse_u64(CMD_ARGV[i * 2 + 1], &value);
1144 if (ERROR_OK != retval)
1145 goto error_return;
1146 buf_set_u64(v, 0, field_size, value);
1147 fields[i].out_value = v;
1148 fields[i].in_value = NULL;
1151 /* did we have an endstate? */
1152 jtag_add_ir_scan(tap, fields, endstate);
1154 retval = jtag_execute_queue();
1156 error_return:
1157 for (i = 0; i < num_fields; i++) {
1158 if (NULL != fields[i].out_value)
1159 free((void *)fields[i].out_value);
1162 free(fields);
1164 return retval;
1167 COMMAND_HANDLER(handle_verify_ircapture_command)
1169 if (CMD_ARGC > 1)
1170 return ERROR_COMMAND_SYNTAX_ERROR;
1172 if (CMD_ARGC == 1) {
1173 bool enable;
1174 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1175 jtag_set_verify_capture_ir(enable);
1178 const char *status = jtag_will_verify_capture_ir() ? "enabled" : "disabled";
1179 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1181 return ERROR_OK;
1184 COMMAND_HANDLER(handle_verify_jtag_command)
1186 if (CMD_ARGC > 1)
1187 return ERROR_COMMAND_SYNTAX_ERROR;
1189 if (CMD_ARGC == 1) {
1190 bool enable;
1191 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1192 jtag_set_verify(enable);
1195 const char *status = jtag_will_verify() ? "enabled" : "disabled";
1196 command_print(CMD_CTX, "verify jtag capture is %s", status);
1198 return ERROR_OK;
1201 COMMAND_HANDLER(handle_tms_sequence_command)
1203 if (CMD_ARGC > 1)
1204 return ERROR_COMMAND_SYNTAX_ERROR;
1206 if (CMD_ARGC == 1) {
1207 bool use_new_table;
1208 if (strcmp(CMD_ARGV[0], "short") == 0)
1209 use_new_table = true;
1210 else if (strcmp(CMD_ARGV[0], "long") == 0)
1211 use_new_table = false;
1212 else
1213 return ERROR_COMMAND_SYNTAX_ERROR;
1215 tap_use_new_tms_table(use_new_table);
1218 command_print(CMD_CTX, "tms sequence is %s",
1219 tap_uses_new_tms_table() ? "short" : "long");
1221 return ERROR_OK;
1224 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1226 if (CMD_ARGC != 1)
1227 return ERROR_COMMAND_SYNTAX_ERROR;
1229 int sleep_ms;
1230 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1232 jtag_set_flush_queue_sleep(sleep_ms);
1234 return ERROR_OK;
1237 COMMAND_HANDLER(handle_wait_srst_deassert)
1239 if (CMD_ARGC != 1)
1240 return ERROR_COMMAND_SYNTAX_ERROR;
1242 int timeout_ms;
1243 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1244 if ((timeout_ms <= 0) || (timeout_ms > 100000)) {
1245 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1246 return ERROR_FAIL;
1249 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1250 int asserted_yet;
1251 int64_t then = timeval_ms();
1252 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1253 if ((timeval_ms() - then) > timeout_ms) {
1254 LOG_ERROR("Timed out");
1255 return ERROR_FAIL;
1257 if (asserted_yet)
1258 break;
1260 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1261 if ((timeval_ms() - then) > timeout_ms) {
1262 LOG_ERROR("Timed out");
1263 return ERROR_FAIL;
1265 if (!asserted_yet)
1266 break;
1269 return ERROR_OK;
1272 static const struct command_registration jtag_command_handlers[] = {
1275 .name = "jtag_flush_queue_sleep",
1276 .handler = handle_jtag_flush_queue_sleep,
1277 .mode = COMMAND_ANY,
1278 .help = "For debug purposes(simulate long delays of interface) "
1279 "to test performance or change in behavior. Default 0ms.",
1280 .usage = "[sleep in ms]",
1283 .name = "jtag_rclk",
1284 .handler = handle_jtag_rclk_command,
1285 .mode = COMMAND_ANY,
1286 .help = "With an argument, change to to use adaptive clocking "
1287 "if possible; else to use the fallback speed. "
1288 "With or without argument, display current setting.",
1289 .usage = "[fallback_speed_khz]",
1292 .name = "jtag_ntrst_delay",
1293 .handler = handle_jtag_ntrst_delay_command,
1294 .mode = COMMAND_ANY,
1295 .help = "delay after deasserting trst in ms",
1296 .usage = "[milliseconds]",
1299 .name = "jtag_ntrst_assert_width",
1300 .handler = handle_jtag_ntrst_assert_width_command,
1301 .mode = COMMAND_ANY,
1302 .help = "delay after asserting trst in ms",
1303 .usage = "[milliseconds]",
1306 .name = "scan_chain",
1307 .handler = handle_scan_chain_command,
1308 .mode = COMMAND_ANY,
1309 .help = "print current scan chain configuration",
1310 .usage = ""
1313 .name = "jtag_reset",
1314 .handler = handle_jtag_reset_command,
1315 .mode = COMMAND_EXEC,
1316 .help = "Set reset line values. Value '1' is active, "
1317 "value '0' is inactive.",
1318 .usage = "trst_active srst_active",
1321 .name = "runtest",
1322 .handler = handle_runtest_command,
1323 .mode = COMMAND_EXEC,
1324 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1325 .usage = "num_cycles"
1328 .name = "irscan",
1329 .handler = handle_irscan_command,
1330 .mode = COMMAND_EXEC,
1331 .help = "Execute Instruction Register (DR) scan. The "
1332 "specified opcodes are put into each TAP's IR, "
1333 "and other TAPs are put in BYPASS.",
1334 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1337 .name = "verify_ircapture",
1338 .handler = handle_verify_ircapture_command,
1339 .mode = COMMAND_ANY,
1340 .help = "Display or assign flag controlling whether to "
1341 "verify values captured during Capture-IR.",
1342 .usage = "['enable'|'disable']",
1345 .name = "verify_jtag",
1346 .handler = handle_verify_jtag_command,
1347 .mode = COMMAND_ANY,
1348 .help = "Display or assign flag controlling whether to "
1349 "verify values captured during IR and DR scans.",
1350 .usage = "['enable'|'disable']",
1353 .name = "tms_sequence",
1354 .handler = handle_tms_sequence_command,
1355 .mode = COMMAND_ANY,
1356 .help = "Display or change what style TMS sequences to use "
1357 "for JTAG state transitions: short (default) or "
1358 "long. Only for working around JTAG bugs.",
1359 /* Specifically for working around DRIVER bugs... */
1360 .usage = "['short'|'long']",
1363 .name = "wait_srst_deassert",
1364 .handler = handle_wait_srst_deassert,
1365 .mode = COMMAND_ANY,
1366 .help = "Wait for an SRST deassert. "
1367 "Useful for cases where you need something to happen within ms "
1368 "of an srst deassert. Timeout in ms ",
1369 .usage = "ms",
1372 .name = "jtag",
1373 .mode = COMMAND_ANY,
1374 .help = "perform jtag tap actions",
1375 .usage = "",
1377 .chain = jtag_subcommand_handlers,
1380 .chain = jtag_command_handlers_to_move,
1382 COMMAND_REGISTRATION_DONE
1385 int jtag_register_commands(struct command_context *cmd_ctx)
1387 return register_commands(cmd_ctx, NULL, jtag_command_handlers);