target/arm: opcodes: rename CamelCase symbols and uppercase variables
[openocd.git] / src / jtag / tcl.c
blobb765305e521edcdad6e2467376ee84939ca32b63
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 struct 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 /* validate arguments as numbers */
112 e = JIM_OK;
113 for (i = 2; i < argc; i += 2) {
114 long bits;
115 const char *cp;
117 e = Jim_GetLong(interp, args[i], &bits);
118 /* If valid - try next arg */
119 if (e == JIM_OK)
120 continue;
122 /* Not valid.. are we at the end? */
123 if (((i + 2) != argc)) {
124 /* nope, then error */
125 return e;
128 /* it could be: "-endstate FOO"
129 * e.g. DRPAUSE so we can issue more instructions
130 * before entering RUN/IDLE and executing them.
133 /* get arg as a string. */
134 cp = Jim_GetString(args[i], NULL);
135 /* is it the magic? */
136 if (0 == strcmp("-endstate", cp)) {
137 /* is the statename valid? */
138 cp = Jim_GetString(args[i + 1], NULL);
140 /* see if it is a valid state name */
141 endstate = tap_state_by_name(cp);
142 if (endstate < 0) {
143 /* update the error message */
144 Jim_SetResultFormatted(interp, "endstate: %s invalid", cp);
145 } else {
146 if (!scan_is_safe(endstate))
147 LOG_WARNING("drscan with unsafe "
148 "endstate \"%s\"", cp);
150 /* valid - so clear the error */
151 e = JIM_OK;
152 /* and remove the last 2 args */
153 argc -= 2;
157 /* Still an error? */
158 if (e != JIM_OK)
159 return e; /* too bad */
160 } /* validate args */
162 assert(e == JIM_OK);
164 tap = jtag_tap_by_jim_obj(interp, args[1]);
165 if (tap == NULL)
166 return JIM_ERR;
168 num_fields = (argc-2)/2;
169 if (num_fields <= 0) {
170 Jim_SetResultString(interp, "drscan: no scan fields supplied", -1);
171 return JIM_ERR;
173 fields = malloc(sizeof(struct scan_field) * num_fields);
174 for (i = 2; i < argc; i += 2) {
175 long bits;
176 int len;
177 const char *str;
179 Jim_GetLong(interp, args[i], &bits);
180 str = Jim_GetString(args[i + 1], &len);
182 fields[field_count].num_bits = bits;
183 void *t = malloc(DIV_ROUND_UP(bits, 8));
184 fields[field_count].out_value = t;
185 str_to_buf(str, len, t, bits, 0);
186 fields[field_count].in_value = t;
187 field_count++;
190 jtag_add_dr_scan(tap, num_fields, fields, endstate);
192 retval = jtag_execute_queue();
193 if (retval != ERROR_OK) {
194 Jim_SetResultString(interp, "drscan: jtag execute failed", -1);
196 for (i = 0; i < field_count; i++)
197 free(fields[i].in_value);
198 free(fields);
200 return JIM_ERR;
203 field_count = 0;
204 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
205 for (i = 2; i < argc; i += 2) {
206 long bits;
207 char *str;
209 Jim_GetLong(interp, args[i], &bits);
210 str = buf_to_hex_str(fields[field_count].in_value, bits);
211 free(fields[field_count].in_value);
213 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
214 free(str);
215 field_count++;
218 Jim_SetResult(interp, list);
220 free(fields);
222 return JIM_OK;
226 static int jim_command_pathmove(Jim_Interp *interp, int argc, Jim_Obj * const *args)
228 tap_state_t states[8];
230 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1))) {
231 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
232 return JIM_ERR;
235 int i;
236 for (i = 0; i < argc-1; i++) {
237 const char *cp;
238 cp = Jim_GetString(args[i + 1], NULL);
239 states[i] = tap_state_by_name(cp);
240 if (states[i] < 0) {
241 /* update the error message */
242 Jim_SetResultFormatted(interp, "endstate: %s invalid", cp);
243 return JIM_ERR;
247 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue() != ERROR_OK)) {
248 Jim_SetResultString(interp, "pathmove: jtag execute failed", -1);
249 return JIM_ERR;
252 jtag_add_pathmove(argc - 2, states + 1);
254 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 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,
310 JCFG_IDCODE,
313 static struct jim_nvp nvp_config_opts[] = {
314 { .name = "-event", .value = JCFG_EVENT },
315 { .name = "-idcode", .value = JCFG_IDCODE },
317 { .name = NULL, .value = -1 }
320 static int jtag_tap_configure_event(struct jim_getopt_info *goi, struct jtag_tap *tap)
322 if (goi->argc == 0) {
323 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
324 return JIM_ERR;
327 struct jim_nvp *n;
328 int e = jim_getopt_nvp(goi, nvp_jtag_tap_event, &n);
329 if (e != JIM_OK) {
330 jim_getopt_nvp_unknown(goi, nvp_jtag_tap_event, 1);
331 return e;
334 if (goi->isconfigure) {
335 if (goi->argc != 1) {
336 Jim_WrongNumArgs(goi->interp,
337 goi->argc,
338 goi->argv,
339 "-event <event-name> <event-body>");
340 return JIM_ERR;
342 } else {
343 if (goi->argc != 0) {
344 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
345 return JIM_ERR;
349 struct jtag_tap_event_action *jteap = tap->event_action;
350 /* replace existing event body */
351 bool found = false;
352 while (jteap) {
353 if (jteap->event == (enum jtag_event)n->value) {
354 found = true;
355 break;
357 jteap = jteap->next;
360 Jim_SetEmptyResult(goi->interp);
362 if (goi->isconfigure) {
363 if (!found)
364 jteap = calloc(1, sizeof(*jteap));
365 else if (NULL != jteap->body)
366 Jim_DecrRefCount(goi->interp, jteap->body);
368 jteap->interp = goi->interp;
369 jteap->event = n->value;
371 Jim_Obj *o;
372 jim_getopt_obj(goi, &o);
373 jteap->body = Jim_DuplicateObj(goi->interp, o);
374 Jim_IncrRefCount(jteap->body);
376 if (!found) {
377 /* add to head of event list */
378 jteap->next = tap->event_action;
379 tap->event_action = jteap;
381 } else if (found) {
382 jteap->interp = goi->interp;
383 Jim_SetResult(goi->interp,
384 Jim_DuplicateObj(goi->interp, jteap->body));
386 return JIM_OK;
389 static int jtag_tap_configure_cmd(struct jim_getopt_info *goi, struct jtag_tap *tap)
391 /* parse config or cget options */
392 while (goi->argc > 0) {
393 Jim_SetEmptyResult(goi->interp);
395 struct jim_nvp *n;
396 int e = jim_getopt_nvp(goi, nvp_config_opts, &n);
397 if (e != JIM_OK) {
398 jim_getopt_nvp_unknown(goi, nvp_config_opts, 0);
399 return e;
402 switch (n->value) {
403 case JCFG_EVENT:
404 e = jtag_tap_configure_event(goi, tap);
405 if (e != JIM_OK)
406 return e;
407 break;
408 case JCFG_IDCODE:
409 if (goi->isconfigure) {
410 Jim_SetResultFormatted(goi->interp,
411 "not settable: %s", n->name);
412 return JIM_ERR;
413 } else {
414 if (goi->argc != 0) {
415 Jim_WrongNumArgs(goi->interp,
416 goi->argc, goi->argv,
417 "NO PARAMS");
418 return JIM_ERR;
421 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, tap->idcode));
422 break;
423 default:
424 Jim_SetResultFormatted(goi->interp, "unknown value: %s", n->name);
425 return JIM_ERR;
429 return JIM_OK;
432 static int is_bad_irval(int ir_length, jim_wide w)
434 jim_wide v = 1;
436 v <<= ir_length;
437 v -= 1;
438 v = ~v;
439 return (w & v) != 0;
442 static int jim_newtap_expected_id(struct jim_nvp *n, struct jim_getopt_info *goi,
443 struct jtag_tap *tap)
445 jim_wide w;
446 int e = jim_getopt_wide(goi, &w);
447 if (e != JIM_OK) {
448 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
449 return e;
452 uint32_t *p = realloc(tap->expected_ids,
453 (tap->expected_ids_cnt + 1) * sizeof(uint32_t));
454 if (!p) {
455 Jim_SetResultFormatted(goi->interp, "no memory");
456 return JIM_ERR;
459 tap->expected_ids = p;
460 tap->expected_ids[tap->expected_ids_cnt++] = w;
462 return JIM_OK;
465 #define NTAP_OPT_IRLEN 0
466 #define NTAP_OPT_IRMASK 1
467 #define NTAP_OPT_IRCAPTURE 2
468 #define NTAP_OPT_ENABLED 3
469 #define NTAP_OPT_DISABLED 4
470 #define NTAP_OPT_EXPECTED_ID 5
471 #define NTAP_OPT_VERSION 6
473 static int jim_newtap_ir_param(struct jim_nvp *n, struct jim_getopt_info *goi,
474 struct jtag_tap *tap)
476 jim_wide w;
477 int e = jim_getopt_wide(goi, &w);
478 if (e != JIM_OK) {
479 Jim_SetResultFormatted(goi->interp,
480 "option: %s bad parameter", n->name);
481 return e;
483 switch (n->value) {
484 case NTAP_OPT_IRLEN:
485 if (w > (jim_wide) (8 * sizeof(tap->ir_capture_value))) {
486 LOG_WARNING("%s: huge IR length %d",
487 tap->dotted_name, (int) w);
489 tap->ir_length = w;
490 break;
491 case NTAP_OPT_IRMASK:
492 if (is_bad_irval(tap->ir_length, w)) {
493 LOG_ERROR("%s: IR mask %x too big",
494 tap->dotted_name,
495 (int) w);
496 return JIM_ERR;
498 if ((w & 3) != 3)
499 LOG_WARNING("%s: nonstandard IR mask", tap->dotted_name);
500 tap->ir_capture_mask = w;
501 break;
502 case NTAP_OPT_IRCAPTURE:
503 if (is_bad_irval(tap->ir_length, w)) {
504 LOG_ERROR("%s: IR capture %x too big",
505 tap->dotted_name, (int) w);
506 return JIM_ERR;
508 if ((w & 3) != 1)
509 LOG_WARNING("%s: nonstandard IR value",
510 tap->dotted_name);
511 tap->ir_capture_value = w;
512 break;
513 default:
514 return JIM_ERR;
516 return JIM_OK;
519 static int jim_newtap_cmd(struct jim_getopt_info *goi)
521 struct jtag_tap *tap;
522 int x;
523 int e;
524 struct jim_nvp *n;
525 char *cp;
526 const struct jim_nvp opts[] = {
527 { .name = "-irlen", .value = NTAP_OPT_IRLEN },
528 { .name = "-irmask", .value = NTAP_OPT_IRMASK },
529 { .name = "-ircapture", .value = NTAP_OPT_IRCAPTURE },
530 { .name = "-enable", .value = NTAP_OPT_ENABLED },
531 { .name = "-disable", .value = NTAP_OPT_DISABLED },
532 { .name = "-expected-id", .value = NTAP_OPT_EXPECTED_ID },
533 { .name = "-ignore-version", .value = NTAP_OPT_VERSION },
534 { .name = NULL, .value = -1 },
537 tap = calloc(1, sizeof(struct jtag_tap));
538 if (!tap) {
539 Jim_SetResultFormatted(goi->interp, "no memory");
540 return JIM_ERR;
544 * we expect CHIP + TAP + OPTIONS
545 * */
546 if (goi->argc < 3) {
547 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
548 free(tap);
549 return JIM_ERR;
552 const char *tmp;
553 jim_getopt_string(goi, &tmp, NULL);
554 tap->chip = strdup(tmp);
556 jim_getopt_string(goi, &tmp, NULL);
557 tap->tapname = strdup(tmp);
559 /* name + dot + name + null */
560 x = strlen(tap->chip) + 1 + strlen(tap->tapname) + 1;
561 cp = malloc(x);
562 sprintf(cp, "%s.%s", tap->chip, tap->tapname);
563 tap->dotted_name = cp;
565 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
566 tap->chip, tap->tapname, tap->dotted_name, goi->argc);
568 if (!transport_is_jtag()) {
569 /* SWD doesn't require any JTAG tap parameters */
570 tap->enabled = true;
571 jtag_tap_init(tap);
572 return JIM_OK;
575 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
576 * that the default. The "-ircapture" and "-irmask" options are only
577 * needed to cope with nonstandard TAPs, or to specify more bits.
579 tap->ir_capture_mask = 0x03;
580 tap->ir_capture_value = 0x01;
582 while (goi->argc) {
583 e = jim_getopt_nvp(goi, opts, &n);
584 if (e != JIM_OK) {
585 jim_getopt_nvp_unknown(goi, opts, 0);
586 free(cp);
587 free(tap);
588 return e;
590 LOG_DEBUG("Processing option: %s", n->name);
591 switch (n->value) {
592 case NTAP_OPT_ENABLED:
593 tap->disabled_after_reset = false;
594 break;
595 case NTAP_OPT_DISABLED:
596 tap->disabled_after_reset = true;
597 break;
598 case NTAP_OPT_EXPECTED_ID:
599 e = jim_newtap_expected_id(n, goi, tap);
600 if (JIM_OK != e) {
601 free(cp);
602 free(tap);
603 return e;
605 break;
606 case NTAP_OPT_IRLEN:
607 case NTAP_OPT_IRMASK:
608 case NTAP_OPT_IRCAPTURE:
609 e = jim_newtap_ir_param(n, goi, tap);
610 if (JIM_OK != e) {
611 free(cp);
612 free(tap);
613 return e;
615 break;
616 case NTAP_OPT_VERSION:
617 tap->ignore_version = true;
618 break;
619 } /* switch (n->value) */
620 } /* while (goi->argc) */
622 /* default is enabled-after-reset */
623 tap->enabled = !tap->disabled_after_reset;
625 /* Did all the required option bits get cleared? */
626 if (tap->ir_length != 0) {
627 jtag_tap_init(tap);
628 return JIM_OK;
631 Jim_SetResultFormatted(goi->interp,
632 "newtap: %s missing IR length",
633 tap->dotted_name);
634 jtag_tap_free(tap);
635 return JIM_ERR;
638 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
640 struct jtag_tap_event_action *jteap;
641 int retval;
643 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next) {
644 if (jteap->event != e)
645 continue;
647 struct jim_nvp *nvp = jim_nvp_value2name_simple(nvp_jtag_tap_event, e);
648 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
649 tap->dotted_name, e, nvp->name,
650 Jim_GetString(jteap->body, NULL));
652 retval = Jim_EvalObj(jteap->interp, jteap->body);
653 if (retval == JIM_RETURN)
654 retval = jteap->interp->returnCode;
656 if (retval != JIM_OK) {
657 Jim_MakeErrorMessage(jteap->interp);
658 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
659 continue;
662 switch (e) {
663 case JTAG_TAP_EVENT_ENABLE:
664 case JTAG_TAP_EVENT_DISABLE:
665 /* NOTE: we currently assume the handlers
666 * can't fail. Right here is where we should
667 * really be verifying the scan chains ...
669 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
670 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
671 tap->enabled ? "enabled" : "disabled");
672 break;
673 default:
674 break;
679 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
681 struct jim_getopt_info goi;
682 jim_getopt_setup(&goi, interp, argc-1, argv + 1);
683 if (goi.argc != 0) {
684 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
685 return JIM_ERR;
687 struct command_context *context = current_command_context(interp);
688 int e = jtag_init_inner(context);
689 if (e != ERROR_OK) {
690 Jim_Obj *obj = Jim_NewIntObj(goi.interp, e);
691 Jim_SetResultFormatted(goi.interp, "error: %#s", obj);
692 return JIM_ERR;
694 return JIM_OK;
697 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
699 int e = ERROR_OK;
700 struct jim_getopt_info goi;
701 jim_getopt_setup(&goi, interp, argc-1, argv + 1);
702 if (goi.argc != 0) {
703 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
704 return JIM_ERR;
706 struct command_context *context = current_command_context(interp);
707 if (transport_is_jtag())
708 e = jtag_init_reset(context);
709 else if (transport_is_swd())
710 e = swd_init_reset(context);
712 if (e != ERROR_OK) {
713 Jim_Obj *obj = Jim_NewIntObj(goi.interp, e);
714 Jim_SetResultFormatted(goi.interp, "error: %#s", obj);
715 return JIM_ERR;
717 return JIM_OK;
720 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
722 struct jim_getopt_info goi;
723 jim_getopt_setup(&goi, interp, argc-1, argv + 1);
724 return jim_newtap_cmd(&goi);
727 static bool jtag_tap_enable(struct jtag_tap *t)
729 if (t->enabled)
730 return false;
731 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
732 if (!t->enabled)
733 return false;
735 /* FIXME add JTAG sanity checks, w/o TLR
736 * - scan chain length grew by one (this)
737 * - IDs and IR lengths are as expected
739 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
740 return true;
742 static bool jtag_tap_disable(struct jtag_tap *t)
744 if (!t->enabled)
745 return false;
746 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
747 if (t->enabled)
748 return false;
750 /* FIXME add JTAG sanity checks, w/o TLR
751 * - scan chain length shrank by one (this)
752 * - IDs and IR lengths are as expected
754 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
755 return true;
758 int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
760 struct command *c = jim_to_command(interp);
761 const char *cmd_name = c->name;
762 struct jim_getopt_info goi;
763 jim_getopt_setup(&goi, interp, argc-1, argv + 1);
764 if (goi.argc != 1) {
765 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
766 return JIM_ERR;
769 struct jtag_tap *t;
771 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
772 if (t == NULL)
773 return JIM_ERR;
775 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
776 /* do nothing, just return the value */
777 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
778 if (!jtag_tap_enable(t)) {
779 LOG_WARNING("failed to enable tap %s", t->dotted_name);
780 return JIM_ERR;
782 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
783 if (!jtag_tap_disable(t)) {
784 LOG_WARNING("failed to disable tap %s", t->dotted_name);
785 return JIM_ERR;
787 } else {
788 LOG_ERROR("command '%s' unknown", cmd_name);
789 return JIM_ERR;
791 bool e = t->enabled;
792 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
793 return JIM_OK;
796 int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
798 struct command *c = jim_to_command(interp);
799 const char *cmd_name = c->name;
800 struct jim_getopt_info goi;
801 jim_getopt_setup(&goi, interp, argc-1, argv + 1);
802 goi.isconfigure = !strcmp(cmd_name, "configure");
803 if (goi.argc < 2 + goi.isconfigure) {
804 Jim_WrongNumArgs(goi.interp, 0, NULL,
805 "<tap_name> <attribute> ...");
806 return JIM_ERR;
809 struct jtag_tap *t;
811 Jim_Obj *o;
812 jim_getopt_obj(&goi, &o);
813 t = jtag_tap_by_jim_obj(goi.interp, o);
814 if (t == NULL)
815 return JIM_ERR;
817 return jtag_tap_configure_cmd(&goi, t);
820 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
822 struct jim_getopt_info goi;
823 jim_getopt_setup(&goi, interp, argc-1, argv + 1);
824 if (goi.argc != 0) {
825 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
826 return JIM_ERR;
828 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
829 struct jtag_tap *tap;
831 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
832 Jim_ListAppendElement(goi.interp,
833 Jim_GetResult(goi.interp),
834 Jim_NewStringObj(goi.interp,
835 tap->dotted_name, -1));
837 return JIM_OK;
840 COMMAND_HANDLER(handle_jtag_init_command)
842 if (CMD_ARGC != 0)
843 return ERROR_COMMAND_SYNTAX_ERROR;
845 static bool jtag_initialized;
846 if (jtag_initialized) {
847 LOG_INFO("'jtag init' has already been called");
848 return ERROR_OK;
850 jtag_initialized = true;
852 LOG_DEBUG("Initializing jtag devices...");
853 return jtag_init(CMD_CTX);
856 static const struct command_registration jtag_subcommand_handlers[] = {
858 .name = "init",
859 .mode = COMMAND_ANY,
860 .handler = handle_jtag_init_command,
861 .help = "initialize jtag scan chain",
862 .usage = ""
865 .name = "arp_init",
866 .mode = COMMAND_ANY,
867 .jim_handler = jim_jtag_arp_init,
868 .help = "Validates JTAG scan chain against the list of "
869 "declared TAPs using just the four standard JTAG "
870 "signals.",
873 .name = "arp_init-reset",
874 .mode = COMMAND_ANY,
875 .jim_handler = jim_jtag_arp_init_reset,
876 .help = "Uses TRST and SRST to try resetting everything on "
877 "the JTAG scan chain, then performs 'jtag arp_init'."
880 .name = "newtap",
881 .mode = COMMAND_CONFIG,
882 .jim_handler = jim_jtag_newtap,
883 .help = "Create a new TAP instance named basename.tap_type, "
884 "and appends it to the scan chain.",
885 .usage = "basename tap_type '-irlen' count "
886 "['-enable'|'-disable'] "
887 "['-expected_id' number] "
888 "['-ignore-version'] "
889 "['-ircapture' number] "
890 "['-mask' number]",
893 .name = "tapisenabled",
894 .mode = COMMAND_EXEC,
895 .jim_handler = jim_jtag_tap_enabler,
896 .help = "Returns a Tcl boolean (0/1) indicating whether "
897 "the TAP is enabled (1) or not (0).",
898 .usage = "tap_name",
901 .name = "tapenable",
902 .mode = COMMAND_EXEC,
903 .jim_handler = jim_jtag_tap_enabler,
904 .help = "Try to enable the specified TAP using the "
905 "'tap-enable' TAP event.",
906 .usage = "tap_name",
909 .name = "tapdisable",
910 .mode = COMMAND_EXEC,
911 .jim_handler = jim_jtag_tap_enabler,
912 .help = "Try to disable the specified TAP using the "
913 "'tap-disable' TAP event.",
914 .usage = "tap_name",
917 .name = "configure",
918 .mode = COMMAND_ANY,
919 .jim_handler = jim_jtag_configure,
920 .help = "Provide a Tcl handler for the specified "
921 "TAP event.",
922 .usage = "tap_name '-event' event_name handler",
925 .name = "cget",
926 .mode = COMMAND_EXEC,
927 .jim_handler = jim_jtag_configure,
928 .help = "Return any Tcl handler for the specified "
929 "TAP event.",
930 .usage = "tap_name '-event' event_name",
933 .name = "names",
934 .mode = COMMAND_ANY,
935 .jim_handler = jim_jtag_names,
936 .help = "Returns list of all JTAG tap names.",
939 .chain = jtag_command_handlers_to_move,
941 COMMAND_REGISTRATION_DONE
944 void jtag_notify_event(enum jtag_event event)
946 struct jtag_tap *tap;
948 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
949 jtag_tap_handle_event(tap, event);
953 COMMAND_HANDLER(handle_scan_chain_command)
955 struct jtag_tap *tap;
956 char expected_id[12];
958 tap = jtag_all_taps();
959 command_print(CMD,
960 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
961 command_print(CMD,
962 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
964 while (tap) {
965 uint32_t expected, expected_mask, ii;
967 snprintf(expected_id, sizeof(expected_id), "0x%08x",
968 (unsigned)((tap->expected_ids_cnt > 0)
969 ? tap->expected_ids[0]
970 : 0));
971 if (tap->ignore_version)
972 expected_id[2] = '*';
974 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
975 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
977 command_print(CMD,
978 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
979 tap->abs_chain_position,
980 tap->dotted_name,
981 tap->enabled ? 'Y' : 'n',
982 (unsigned int)(tap->idcode),
983 expected_id,
984 (unsigned int)(tap->ir_length),
985 (unsigned int)(expected),
986 (unsigned int)(expected_mask));
988 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
989 snprintf(expected_id, sizeof(expected_id), "0x%08x",
990 (unsigned) tap->expected_ids[ii]);
991 if (tap->ignore_version)
992 expected_id[2] = '*';
994 command_print(CMD,
995 " %s",
996 expected_id);
999 tap = tap->next_tap;
1002 return ERROR_OK;
1005 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1007 if (CMD_ARGC > 1)
1008 return ERROR_COMMAND_SYNTAX_ERROR;
1009 if (CMD_ARGC == 1) {
1010 unsigned delay;
1011 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1013 jtag_set_ntrst_delay(delay);
1015 command_print(CMD, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1016 return ERROR_OK;
1019 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1021 if (CMD_ARGC > 1)
1022 return ERROR_COMMAND_SYNTAX_ERROR;
1023 if (CMD_ARGC == 1) {
1024 unsigned delay;
1025 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1027 jtag_set_ntrst_assert_width(delay);
1029 command_print(CMD, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1030 return ERROR_OK;
1033 COMMAND_HANDLER(handle_jtag_rclk_command)
1035 if (CMD_ARGC > 1)
1036 return ERROR_COMMAND_SYNTAX_ERROR;
1038 int retval = ERROR_OK;
1039 if (CMD_ARGC == 1) {
1040 unsigned khz = 0;
1041 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1043 retval = jtag_config_rclk(khz);
1044 if (ERROR_OK != retval)
1045 return retval;
1048 int cur_khz = jtag_get_speed_khz();
1049 retval = jtag_get_speed_readable(&cur_khz);
1050 if (ERROR_OK != retval)
1051 return retval;
1053 if (cur_khz)
1054 command_print(CMD, "RCLK not supported - fallback to %d kHz", cur_khz);
1055 else
1056 command_print(CMD, "RCLK - adaptive");
1058 return retval;
1061 COMMAND_HANDLER(handle_runtest_command)
1063 if (CMD_ARGC != 1)
1064 return ERROR_COMMAND_SYNTAX_ERROR;
1066 unsigned num_clocks;
1067 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1069 jtag_add_runtest(num_clocks, TAP_IDLE);
1070 return jtag_execute_queue();
1074 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1075 * should be stable ... and *NOT* a shift state, otherwise free-running
1076 * jtag clocks could change the values latched by the update state.
1077 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1078 * and "drscan" commands are a write-only subset of what SVF provides.
1081 COMMAND_HANDLER(handle_irscan_command)
1083 int i;
1084 struct scan_field *fields;
1085 struct jtag_tap *tap = NULL;
1086 tap_state_t endstate;
1088 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1089 return ERROR_COMMAND_SYNTAX_ERROR;
1091 /* optional "-endstate" "statename" at the end of the arguments,
1092 * so that e.g. IRPAUSE can let us load the data register before
1093 * entering RUN/IDLE to execute the instruction we load here.
1095 endstate = TAP_IDLE;
1097 if (CMD_ARGC >= 4) {
1098 /* have at least one pair of numbers.
1099 * is last pair the magic text? */
1100 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1101 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1102 if (endstate == TAP_INVALID)
1103 return ERROR_COMMAND_SYNTAX_ERROR;
1104 if (!scan_is_safe(endstate))
1105 LOG_WARNING("unstable irscan endstate \"%s\"",
1106 CMD_ARGV[CMD_ARGC - 1]);
1107 CMD_ARGC -= 2;
1111 int num_fields = CMD_ARGC / 2;
1112 if (num_fields > 1) {
1113 /* we really should be looking at plain_ir_scan if we want
1114 * anything more fancy.
1116 LOG_ERROR("Specify a single value for tap");
1117 return ERROR_COMMAND_SYNTAX_ERROR;
1120 fields = calloc(num_fields, sizeof(*fields));
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 free(fields);
1127 command_print(CMD, "Tap: %s unknown", CMD_ARGV[i*2]);
1129 return ERROR_FAIL;
1131 uint64_t value;
1132 retval = parse_u64(CMD_ARGV[i * 2 + 1], &value);
1133 if (ERROR_OK != retval)
1134 goto error_return;
1136 int field_size = tap->ir_length;
1137 fields[i].num_bits = field_size;
1138 uint8_t *v = calloc(1, DIV_ROUND_UP(field_size, 8));
1139 if (!v) {
1140 LOG_ERROR("Out of memory");
1141 goto error_return;
1144 buf_set_u64(v, 0, field_size, value);
1145 fields[i].out_value = v;
1146 fields[i].in_value = NULL;
1149 /* did we have an endstate? */
1150 jtag_add_ir_scan(tap, fields, endstate);
1152 retval = jtag_execute_queue();
1154 error_return:
1155 for (i = 0; i < num_fields; i++)
1156 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, "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, "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, "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 int64_t 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 = "runtest",
1310 .handler = handle_runtest_command,
1311 .mode = COMMAND_EXEC,
1312 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1313 .usage = "num_cycles"
1316 .name = "irscan",
1317 .handler = handle_irscan_command,
1318 .mode = COMMAND_EXEC,
1319 .help = "Execute Instruction Register (IR) scan. The "
1320 "specified opcodes are put into each TAP's IR, "
1321 "and other TAPs are put in BYPASS.",
1322 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1325 .name = "verify_ircapture",
1326 .handler = handle_verify_ircapture_command,
1327 .mode = COMMAND_ANY,
1328 .help = "Display or assign flag controlling whether to "
1329 "verify values captured during Capture-IR.",
1330 .usage = "['enable'|'disable']",
1333 .name = "verify_jtag",
1334 .handler = handle_verify_jtag_command,
1335 .mode = COMMAND_ANY,
1336 .help = "Display or assign flag controlling whether to "
1337 "verify values captured during IR and DR scans.",
1338 .usage = "['enable'|'disable']",
1341 .name = "tms_sequence",
1342 .handler = handle_tms_sequence_command,
1343 .mode = COMMAND_ANY,
1344 .help = "Display or change what style TMS sequences to use "
1345 "for JTAG state transitions: short (default) or "
1346 "long. Only for working around JTAG bugs.",
1347 /* Specifically for working around DRIVER bugs... */
1348 .usage = "['short'|'long']",
1351 .name = "wait_srst_deassert",
1352 .handler = handle_wait_srst_deassert,
1353 .mode = COMMAND_ANY,
1354 .help = "Wait for an SRST deassert. "
1355 "Useful for cases where you need something to happen within ms "
1356 "of an srst deassert. Timeout in ms",
1357 .usage = "ms",
1360 .name = "jtag",
1361 .mode = COMMAND_ANY,
1362 .help = "perform jtag tap actions",
1363 .usage = "",
1365 .chain = jtag_subcommand_handlers,
1368 .chain = jtag_command_handlers_to_move,
1370 COMMAND_REGISTRATION_DONE
1373 int jtag_register_commands(struct command_context *cmd_ctx)
1375 return register_commands(cmd_ctx, NULL, jtag_command_handlers);