Make jim functions public accessible.
[openocd/andreasf.git] / src / jtag / tcl.c
blob8808666f4273dd6e734c764e2f2321574b4002e1
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 ***************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
34 #include "jtag.h"
35 #include "minidriver.h"
36 #include "interface.h"
37 #include "interfaces.h"
38 #include "tcl.h"
40 #ifdef HAVE_STRINGS_H
41 #include <strings.h>
42 #endif
44 #include <helper/time_support.h>
46 /**
47 * @file
48 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
51 static const Jim_Nvp nvp_jtag_tap_event[] = {
52 { .value = JTAG_TRST_ASSERTED, .name = "post-reset" },
53 { .value = JTAG_TAP_EVENT_SETUP, .name = "setup" },
54 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
55 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
57 { .name = NULL, .value = -1 }
60 extern struct jtag_interface *jtag_interface;
62 struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
64 const char *cp = Jim_GetString(o, NULL);
65 struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
66 if (NULL == cp)
67 cp = "(unknown)";
68 if (NULL == t)
69 Jim_SetResultFormatted(interp, "Tap '%s' could not be found", cp);
70 return t;
73 static bool scan_is_safe(tap_state_t state)
75 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))
108 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
109 return JIM_ERR;
112 endstate = TAP_IDLE;
114 script_debug(interp, "drscan", argc, args);
116 /* validate arguments as numbers */
117 e = JIM_OK;
118 for (i = 2; i < argc; i += 2)
120 long bits;
121 const char *cp;
123 e = Jim_GetLong(interp, args[i], &bits);
124 /* If valid - try next arg */
125 if (e == JIM_OK) {
126 continue;
129 /* Not valid.. are we at the end? */
130 if (((i + 2) != argc)) {
131 /* nope, then error */
132 return e;
135 /* it could be: "-endstate FOO"
136 * e.g. DRPAUSE so we can issue more instructions
137 * before entering RUN/IDLE and executing them.
140 /* get arg as a string. */
141 cp = Jim_GetString(args[i], NULL);
142 /* is it the magic? */
143 if (0 == strcmp("-endstate", cp)) {
144 /* is the statename valid? */
145 cp = Jim_GetString(args[i + 1], NULL);
147 /* see if it is a valid state name */
148 endstate = tap_state_by_name(cp);
149 if (endstate < 0) {
150 /* update the error message */
151 Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
152 } else {
153 if (!scan_is_safe(endstate))
154 LOG_WARNING("drscan with unsafe "
155 "endstate \"%s\"", cp);
157 /* valid - so clear the error */
158 e = JIM_OK;
159 /* and remove the last 2 args */
160 argc -= 2;
164 /* Still an error? */
165 if (e != JIM_OK) {
166 return e; /* too bad */
168 } /* validate args */
170 assert(e == JIM_OK);
172 tap = jtag_tap_by_jim_obj(interp, args[1]);
173 if (tap == NULL) {
174 return JIM_ERR;
177 num_fields = (argc-2)/2;
178 assert(num_fields > 0);
179 fields = malloc(sizeof(struct scan_field) * num_fields);
180 for (i = 2; i < argc; i += 2)
182 long bits;
183 int len;
184 const char *str;
186 Jim_GetLong(interp, args[i], &bits);
187 str = Jim_GetString(args[i + 1], &len);
189 fields[field_count].num_bits = bits;
190 void * t = malloc(DIV_ROUND_UP(bits, 8));
191 fields[field_count].out_value = t;
192 str_to_buf(str, len, t, bits, 0);
193 fields[field_count].in_value = t;
194 field_count++;
197 jtag_add_dr_scan(tap, num_fields, fields, endstate);
199 retval = jtag_execute_queue();
200 if (retval != ERROR_OK)
202 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
203 return JIM_ERR;
206 field_count = 0;
207 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
208 for (i = 2; i < argc; i += 2)
210 long bits;
211 char *str;
213 Jim_GetLong(interp, args[i], &bits);
214 str = buf_to_str(fields[field_count].in_value, bits, 16);
215 free((void *)fields[field_count].out_value);
217 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
218 free(str);
219 field_count++;
222 Jim_SetResult(interp, list);
224 free(fields);
226 return JIM_OK;
230 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
232 tap_state_t states[8];
234 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
236 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
237 return JIM_ERR;
240 script_debug(interp, "pathmove", argc, args);
242 int i;
243 for (i = 0; i < argc-1; i++)
245 const char *cp;
246 cp = Jim_GetString(args[i + 1], NULL);
247 states[i] = tap_state_by_name(cp);
248 if (states[i] < 0)
250 /* update the error message */
251 Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
252 return JIM_ERR;
256 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
258 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
259 return JIM_ERR;
262 jtag_add_pathmove(argc-2, states + 1);
264 if (jtag_execute_queue()!= ERROR_OK)
266 Jim_SetResultString(interp, "pathmove: failed",-1);
267 return JIM_ERR;
270 return JIM_OK;
274 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
276 script_debug(interp, "flush_count", argc, args);
278 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
280 return JIM_OK;
283 /* REVISIT Just what about these should "move" ... ?
284 * These registrations, into the main JTAG table?
286 * There's a minor compatibility issue, these all show up twice;
287 * that's not desirable:
288 * - jtag drscan ... NOT DOCUMENTED!
289 * - drscan ...
291 * The "irscan" command (for example) doesn't show twice.
293 static const struct command_registration jtag_command_handlers_to_move[] = {
295 .name = "drscan",
296 .mode = COMMAND_EXEC,
297 .jim_handler = Jim_Command_drscan,
298 .help = "Execute Data Register (DR) scan for one TAP. "
299 "Other TAPs must be in BYPASS mode.",
300 .usage = "tap_name [num_bits value]* ['-endstate' state_name]",
303 .name = "flush_count",
304 .mode = COMMAND_EXEC,
305 .jim_handler = Jim_Command_flush_count,
306 .help = "Returns the number of times the JTAG queue "
307 "has been flushed.",
310 .name = "pathmove",
311 .mode = COMMAND_EXEC,
312 .jim_handler = Jim_Command_pathmove,
313 .usage = "start_state state1 [state2 [state3 ...]]",
314 .help = "Move JTAG state machine from current state "
315 "(start_state) to state1, then state2, state3, etc.",
317 COMMAND_REGISTRATION_DONE
321 enum jtag_tap_cfg_param {
322 JCFG_EVENT
325 static Jim_Nvp nvp_config_opts[] = {
326 { .name = "-event", .value = JCFG_EVENT },
328 { .name = NULL, .value = -1 }
331 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
333 if (goi->argc == 0)
335 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
336 return JIM_ERR;
339 Jim_Nvp *n;
340 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
341 if (e != JIM_OK)
343 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
344 return e;
347 if (goi->isconfigure) {
348 if (goi->argc != 1) {
349 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
350 return JIM_ERR;
352 } else {
353 if (goi->argc != 0) {
354 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
355 return JIM_ERR;
359 struct jtag_tap_event_action *jteap = tap->event_action;
360 /* replace existing event body */
361 bool found = false;
362 while (jteap)
364 if (jteap->event == (enum jtag_event)n->value)
366 found = true;
367 break;
369 jteap = jteap->next;
372 Jim_SetEmptyResult(goi->interp);
374 if (goi->isconfigure)
376 if (!found)
377 jteap = calloc(1, sizeof(*jteap));
378 else if (NULL != jteap->body)
379 Jim_DecrRefCount(goi->interp, jteap->body);
381 jteap->interp = goi->interp;
382 jteap->event = n->value;
384 Jim_Obj *o;
385 Jim_GetOpt_Obj(goi, &o);
386 jteap->body = Jim_DuplicateObj(goi->interp, o);
387 Jim_IncrRefCount(jteap->body);
389 if (!found)
391 /* add to head of event list */
392 jteap->next = tap->event_action;
393 tap->event_action = jteap;
396 else if (found)
398 jteap->interp = goi->interp;
399 Jim_SetResult(goi->interp,
400 Jim_DuplicateObj(goi->interp, jteap->body));
402 return JIM_OK;
405 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
407 /* parse config or cget options */
408 while (goi->argc > 0)
410 Jim_SetEmptyResult (goi->interp);
412 Jim_Nvp *n;
413 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
414 if (e != JIM_OK)
416 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
417 return e;
420 switch (n->value)
422 case JCFG_EVENT:
423 e = jtag_tap_configure_event(goi, tap);
424 if (e != JIM_OK)
425 return e;
426 break;
427 default:
428 Jim_SetResultFormatted(goi->interp, "unknown event: %s", n->name);
429 return JIM_ERR;
433 return JIM_OK;
436 static int is_bad_irval(int ir_length, jim_wide w)
438 jim_wide v = 1;
440 v <<= ir_length;
441 v -= 1;
442 v = ~v;
443 return (w & v) != 0;
446 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
447 struct jtag_tap *pTap)
449 jim_wide w;
450 int e = Jim_GetOpt_Wide(goi, &w);
451 if (e != JIM_OK) {
452 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
453 return e;
456 unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
457 uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
458 if (new_expected_ids == NULL)
460 Jim_SetResultFormatted(goi->interp, "no memory");
461 return JIM_ERR;
464 memcpy(new_expected_ids, pTap->expected_ids, expected_len);
466 new_expected_ids[pTap->expected_ids_cnt] = w;
468 free(pTap->expected_ids);
469 pTap->expected_ids = new_expected_ids;
470 pTap->expected_ids_cnt++;
472 return JIM_OK;
475 #define NTAP_OPT_IRLEN 0
476 #define NTAP_OPT_IRMASK 1
477 #define NTAP_OPT_IRCAPTURE 2
478 #define NTAP_OPT_ENABLED 3
479 #define NTAP_OPT_DISABLED 4
480 #define NTAP_OPT_EXPECTED_ID 5
481 #define NTAP_OPT_VERSION 6
483 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
484 struct jtag_tap *pTap)
486 jim_wide w;
487 int e = Jim_GetOpt_Wide(goi, &w);
488 if (e != JIM_OK)
490 Jim_SetResultFormatted(goi->interp,
491 "option: %s bad parameter", n->name);
492 free((void *)pTap->dotted_name);
493 return e;
495 switch (n->value) {
496 case NTAP_OPT_IRLEN:
497 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
499 LOG_WARNING("%s: huge IR length %d",
500 pTap->dotted_name, (int) w);
502 pTap->ir_length = w;
503 break;
504 case NTAP_OPT_IRMASK:
505 if (is_bad_irval(pTap->ir_length, w))
507 LOG_ERROR("%s: IR mask %x too big",
508 pTap->dotted_name,
509 (int) w);
510 return JIM_ERR;
512 if ((w & 3) != 3)
513 LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
514 pTap->ir_capture_mask = w;
515 break;
516 case NTAP_OPT_IRCAPTURE:
517 if (is_bad_irval(pTap->ir_length, w))
519 LOG_ERROR("%s: IR capture %x too big",
520 pTap->dotted_name, (int) w);
521 return JIM_ERR;
523 if ((w & 3) != 1)
524 LOG_WARNING("%s: nonstandard IR value",
525 pTap->dotted_name);
526 pTap->ir_capture_value = w;
527 break;
528 default:
529 return JIM_ERR;
531 return JIM_OK;
534 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
536 struct jtag_tap *pTap;
537 int x;
538 int e;
539 Jim_Nvp *n;
540 char *cp;
541 const Jim_Nvp opts[] = {
542 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
543 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
544 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
545 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
546 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
547 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
548 { .name = "-ignore-version" , .value = NTAP_OPT_VERSION },
549 { .name = NULL , .value = -1 },
552 pTap = calloc(1, sizeof(struct jtag_tap));
553 if (!pTap) {
554 Jim_SetResultFormatted(goi->interp, "no memory");
555 return JIM_ERR;
559 * we expect CHIP + TAP + OPTIONS
560 * */
561 if (goi->argc < 3) {
562 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
563 free(pTap);
564 return JIM_ERR;
566 Jim_GetOpt_String(goi, &cp, NULL);
567 pTap->chip = strdup(cp);
569 Jim_GetOpt_String(goi, &cp, NULL);
570 pTap->tapname = strdup(cp);
572 /* name + dot + name + null */
573 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
574 cp = malloc(x);
575 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
576 pTap->dotted_name = cp;
578 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
579 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
581 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
582 * that the default. The "-irlen" and "-irmask" options are only
583 * needed to cope with nonstandard TAPs, or to specify more bits.
585 pTap->ir_capture_mask = 0x03;
586 pTap->ir_capture_value = 0x01;
588 while (goi->argc) {
589 e = Jim_GetOpt_Nvp(goi, opts, &n);
590 if (e != JIM_OK) {
591 Jim_GetOpt_NvpUnknown(goi, opts, 0);
592 free((void *)pTap->dotted_name);
593 free(pTap);
594 return e;
596 LOG_DEBUG("Processing option: %s", n->name);
597 switch (n->value) {
598 case NTAP_OPT_ENABLED:
599 pTap->disabled_after_reset = false;
600 break;
601 case NTAP_OPT_DISABLED:
602 pTap->disabled_after_reset = true;
603 break;
604 case NTAP_OPT_EXPECTED_ID:
605 e = jim_newtap_expected_id(n, goi, pTap);
606 if (JIM_OK != e)
608 free((void *)pTap->dotted_name);
609 free(pTap);
610 return e;
612 break;
613 case NTAP_OPT_IRLEN:
614 case NTAP_OPT_IRMASK:
615 case NTAP_OPT_IRCAPTURE:
616 e = jim_newtap_ir_param(n, goi, pTap);
617 if (JIM_OK != e)
619 free((void *)pTap->dotted_name);
620 free(pTap);
621 return e;
623 break;
624 case NTAP_OPT_VERSION:
625 pTap->ignore_version = true;
626 break;
627 } /* switch (n->value) */
628 } /* while (goi->argc) */
630 /* default is enabled-after-reset */
631 pTap->enabled = !pTap->disabled_after_reset;
633 /* Did all the required option bits get cleared? */
634 if (pTap->ir_length != 0)
636 jtag_tap_init(pTap);
637 return JIM_OK;
640 Jim_SetResultFormatted(goi->interp,
641 "newtap: %s missing IR length",
642 pTap->dotted_name);
643 jtag_tap_free(pTap);
644 return JIM_ERR;
647 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
649 struct jtag_tap_event_action * jteap;
651 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
653 if (jteap->event != e)
654 continue;
656 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
657 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
658 tap->dotted_name, e, nvp->name,
659 Jim_GetString(jteap->body, NULL));
661 if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK)
663 Jim_MakeErrorMessage(jteap->interp);
664 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
665 continue;
668 switch (e)
670 case JTAG_TAP_EVENT_ENABLE:
671 case JTAG_TAP_EVENT_DISABLE:
672 /* NOTE: we currently assume the handlers
673 * can't fail. Right here is where we should
674 * really be verifying the scan chains ...
676 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
677 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
678 tap->enabled ? "enabled" : "disabled");
679 break;
680 default:
681 break;
686 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
688 Jim_GetOptInfo goi;
689 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
690 if (goi.argc != 0) {
691 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
692 return JIM_ERR;
694 struct command_context *context = current_command_context(interp);
695 int e = jtag_init_inner(context);
696 if (e != ERROR_OK) {
697 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
698 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
699 Jim_FreeNewObj(goi.interp, eObj);
700 return JIM_ERR;
702 return JIM_OK;
705 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
707 Jim_GetOptInfo goi;
708 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
709 if (goi.argc != 0) {
710 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
711 return JIM_ERR;
713 struct command_context *context = current_command_context(interp);
714 int e = jtag_init_reset(context);
715 if (e != ERROR_OK) {
716 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
717 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
718 Jim_FreeNewObj(goi.interp, eObj);
719 return JIM_ERR;
721 return JIM_OK;
724 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
726 Jim_GetOptInfo goi;
727 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
728 return jim_newtap_cmd(&goi);
731 static bool jtag_tap_enable(struct jtag_tap *t)
733 if (t->enabled)
734 return false;
735 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
736 if (!t->enabled)
737 return false;
739 /* FIXME add JTAG sanity checks, w/o TLR
740 * - scan chain length grew by one (this)
741 * - IDs and IR lengths are as expected
743 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
744 return true;
746 static bool jtag_tap_disable(struct jtag_tap *t)
748 if (!t->enabled)
749 return false;
750 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
751 if (t->enabled)
752 return false;
754 /* FIXME add JTAG sanity checks, w/o TLR
755 * - scan chain length shrank by one (this)
756 * - IDs and IR lengths are as expected
758 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
759 return true;
762 int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
764 const char *cmd_name = Jim_GetString(argv[0], NULL);
765 Jim_GetOptInfo goi;
766 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
767 if (goi.argc != 1) {
768 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
769 return JIM_ERR;
772 struct jtag_tap *t;
774 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
775 if (t == NULL)
776 return JIM_ERR;
778 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
779 // do nothing, just return the value
780 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
781 if (!jtag_tap_enable(t)){
782 LOG_WARNING("failed to enable tap %s", t->dotted_name);
783 return JIM_ERR;
785 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
786 if (!jtag_tap_disable(t)){
787 LOG_WARNING("failed to disable tap %s", t->dotted_name);
788 return JIM_ERR;
790 } else {
791 LOG_ERROR("command '%s' unknown", cmd_name);
792 return JIM_ERR;
794 bool e = t->enabled;
795 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
796 return JIM_OK;
799 int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
801 const char *cmd_name = Jim_GetString(argv[0], NULL);
802 Jim_GetOptInfo goi;
803 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
804 goi.isconfigure = !strcmp(cmd_name, "configure");
805 if (goi.argc < 2 + goi.isconfigure) {
806 Jim_WrongNumArgs(goi.interp, 0, NULL,
807 "<tap_name> <attribute> ...");
808 return JIM_ERR;
811 struct jtag_tap *t;
813 Jim_Obj *o;
814 Jim_GetOpt_Obj(&goi, &o);
815 t = jtag_tap_by_jim_obj(goi.interp, o);
816 if (t == NULL) {
817 return JIM_ERR;
820 return jtag_tap_configure_cmd(&goi, t);
823 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
825 Jim_GetOptInfo goi;
826 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
827 if (goi.argc != 0) {
828 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
829 return JIM_ERR;
831 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
832 struct jtag_tap *tap;
834 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
835 Jim_ListAppendElement(goi.interp,
836 Jim_GetResult(goi.interp),
837 Jim_NewStringObj(goi.interp,
838 tap->dotted_name, -1));
840 return JIM_OK;
843 COMMAND_HANDLER(handle_jtag_init_command)
845 if (CMD_ARGC != 0)
846 return ERROR_COMMAND_SYNTAX_ERROR;
848 static bool jtag_initialized = false;
849 if (jtag_initialized)
851 LOG_INFO("'jtag init' has already been called");
852 return ERROR_OK;
854 jtag_initialized = true;
856 LOG_DEBUG("Initializing jtag devices...");
857 return jtag_init(CMD_CTX);
860 static const struct command_registration jtag_subcommand_handlers[] = {
862 .name = "init",
863 .mode = COMMAND_ANY,
864 .handler = handle_jtag_init_command,
865 .help = "initialize jtag scan chain",
868 .name = "arp_init",
869 .mode = COMMAND_ANY,
870 .jim_handler = jim_jtag_arp_init,
871 .help = "Validates JTAG scan chain against the list of "
872 "declared TAPs using just the four standard JTAG "
873 "signals.",
876 .name = "arp_init-reset",
877 .mode = COMMAND_ANY,
878 .jim_handler = jim_jtag_arp_init_reset,
879 .help = "Uses TRST and SRST to try resetting everything on "
880 "the JTAG scan chain, then performs 'jtag arp_init'."
883 .name = "newtap",
884 .mode = COMMAND_CONFIG,
885 .jim_handler = jim_jtag_newtap,
886 .help = "Create a new TAP instance named basename.tap_type, "
887 "and appends it to the scan chain.",
888 .usage = "basename tap_type '-irlen' count "
889 "['-enable'|'-disable'] "
890 "['-expected_id' number] "
891 "['-ignore-version'] "
892 "['-ircapture' number] "
893 "['-mask' number] ",
896 .name = "tapisenabled",
897 .mode = COMMAND_EXEC,
898 .jim_handler = jim_jtag_tap_enabler,
899 .help = "Returns a Tcl boolean (0/1) indicating whether "
900 "the TAP is enabled (1) or not (0).",
901 .usage = "tap_name",
904 .name = "tapenable",
905 .mode = COMMAND_EXEC,
906 .jim_handler = jim_jtag_tap_enabler,
907 .help = "Try to enable the specified TAP using the "
908 "'tap-enable' TAP event.",
909 .usage = "tap_name",
912 .name = "tapdisable",
913 .mode = COMMAND_EXEC,
914 .jim_handler = jim_jtag_tap_enabler,
915 .help = "Try to disable the specified TAP using the "
916 "'tap-disable' TAP event.",
917 .usage = "tap_name",
920 .name = "configure",
921 .mode = COMMAND_EXEC,
922 .jim_handler = jim_jtag_configure,
923 .help = "Provide a Tcl handler for the specified "
924 "TAP event.",
925 .usage = "tap_name '-event' event_name handler",
928 .name = "cget",
929 .mode = COMMAND_EXEC,
930 .jim_handler = jim_jtag_configure,
931 .help = "Return any Tcl handler for the specified "
932 "TAP event.",
933 .usage = "tap_name '-event' event_name",
936 .name = "names",
937 .mode = COMMAND_ANY,
938 .jim_handler = jim_jtag_names,
939 .help = "Returns list of all JTAG tap names.",
942 .chain = jtag_command_handlers_to_move,
944 COMMAND_REGISTRATION_DONE
947 void jtag_notify_event(enum jtag_event event)
949 struct jtag_tap *tap;
951 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
952 jtag_tap_handle_event(tap, event);
956 COMMAND_HANDLER(handle_scan_chain_command)
958 struct jtag_tap *tap;
959 char expected_id[12];
961 tap = jtag_all_taps();
962 command_print(CMD_CTX,
963 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
964 command_print(CMD_CTX,
965 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
967 while (tap) {
968 uint32_t expected, expected_mask, ii;
970 snprintf(expected_id, sizeof expected_id, "0x%08x",
971 (unsigned)((tap->expected_ids_cnt > 0)
972 ? tap->expected_ids[0]
973 : 0));
974 if (tap->ignore_version)
975 expected_id[2] = '*';
977 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
978 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
980 command_print(CMD_CTX,
981 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
982 tap->abs_chain_position,
983 tap->dotted_name,
984 tap->enabled ? 'Y' : 'n',
985 (unsigned int)(tap->idcode),
986 expected_id,
987 (unsigned int)(tap->ir_length),
988 (unsigned int)(expected),
989 (unsigned int)(expected_mask));
991 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
992 snprintf(expected_id, sizeof expected_id, "0x%08x",
993 (unsigned) tap->expected_ids[1]);
994 if (tap->ignore_version)
995 expected_id[2] = '*';
997 command_print(CMD_CTX,
998 " %s",
999 expected_id);
1002 tap = tap->next_tap;
1005 return ERROR_OK;
1008 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1010 if (CMD_ARGC > 1)
1011 return ERROR_COMMAND_SYNTAX_ERROR;
1012 if (CMD_ARGC == 1)
1014 unsigned delay;
1015 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1017 jtag_set_ntrst_delay(delay);
1019 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1020 return ERROR_OK;
1023 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1025 if (CMD_ARGC > 1)
1026 return ERROR_COMMAND_SYNTAX_ERROR;
1027 if (CMD_ARGC == 1)
1029 unsigned delay;
1030 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1032 jtag_set_ntrst_assert_width(delay);
1034 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1035 return ERROR_OK;
1038 COMMAND_HANDLER(handle_jtag_rclk_command)
1040 if (CMD_ARGC > 1)
1041 return ERROR_COMMAND_SYNTAX_ERROR;
1043 int retval = ERROR_OK;
1044 if (CMD_ARGC == 1)
1046 unsigned khz = 0;
1047 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1049 retval = jtag_config_rclk(khz);
1050 if (ERROR_OK != retval)
1051 return retval;
1054 int cur_khz = jtag_get_speed_khz();
1055 retval = jtag_get_speed_readable(&cur_khz);
1056 if (ERROR_OK != retval)
1057 return retval;
1059 if (cur_khz)
1060 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1061 else
1062 command_print(CMD_CTX, "RCLK - adaptive");
1064 return retval;
1067 COMMAND_HANDLER(handle_jtag_reset_command)
1069 if (CMD_ARGC != 2)
1070 return ERROR_COMMAND_SYNTAX_ERROR;
1072 int trst = -1;
1073 if (CMD_ARGV[0][0] == '1')
1074 trst = 1;
1075 else if (CMD_ARGV[0][0] == '0')
1076 trst = 0;
1077 else
1078 return ERROR_COMMAND_SYNTAX_ERROR;
1080 int srst = -1;
1081 if (CMD_ARGV[1][0] == '1')
1082 srst = 1;
1083 else if (CMD_ARGV[1][0] == '0')
1084 srst = 0;
1085 else
1086 return ERROR_COMMAND_SYNTAX_ERROR;
1088 if (adapter_init(CMD_CTX) != ERROR_OK)
1089 return ERROR_JTAG_INIT_FAILED;
1091 jtag_add_reset(trst, srst);
1092 return jtag_execute_queue();
1095 COMMAND_HANDLER(handle_runtest_command)
1097 if (CMD_ARGC != 1)
1098 return ERROR_COMMAND_SYNTAX_ERROR;
1100 unsigned num_clocks;
1101 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1103 jtag_add_runtest(num_clocks, TAP_IDLE);
1104 return jtag_execute_queue();
1108 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1109 * should be stable ... and *NOT* a shift state, otherwise free-running
1110 * jtag clocks could change the values latched by the update state.
1111 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1112 * and "drscan" commands are a write-only subset of what SVF provides.
1115 COMMAND_HANDLER(handle_irscan_command)
1117 int i;
1118 struct scan_field *fields;
1119 struct jtag_tap *tap = NULL;
1120 tap_state_t endstate;
1122 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1124 return ERROR_COMMAND_SYNTAX_ERROR;
1127 /* optional "-endstate" "statename" at the end of the arguments,
1128 * so that e.g. IRPAUSE can let us load the data register before
1129 * entering RUN/IDLE to execute the instruction we load here.
1131 endstate = TAP_IDLE;
1133 if (CMD_ARGC >= 4) {
1134 /* have at least one pair of numbers. */
1135 /* is last pair the magic text? */
1136 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1137 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1138 if (endstate == TAP_INVALID)
1139 return ERROR_COMMAND_SYNTAX_ERROR;
1140 if (!scan_is_safe(endstate))
1141 LOG_WARNING("unstable irscan endstate \"%s\"",
1142 CMD_ARGV[CMD_ARGC - 1]);
1143 CMD_ARGC -= 2;
1147 int num_fields = CMD_ARGC / 2;
1148 if (num_fields > 1)
1150 /* we really should be looking at plain_ir_scan if we want
1151 * anything more fancy.
1153 LOG_ERROR("Specify a single value for tap");
1154 return ERROR_COMMAND_SYNTAX_ERROR;
1157 size_t fields_len = sizeof(struct scan_field) * num_fields;
1158 fields = malloc(fields_len);
1159 memset(fields, 0, fields_len);
1161 int retval;
1162 for (i = 0; i < num_fields; i++)
1164 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1165 if (tap == NULL)
1167 int j;
1168 for (j = 0; j < i; j++)
1169 free((void *)fields[j].out_value);
1170 free(fields);
1171 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1173 return ERROR_FAIL;
1175 int field_size = tap->ir_length;
1176 fields[i].num_bits = field_size;
1177 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1179 uint32_t value;
1180 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1181 if (ERROR_OK != retval)
1182 goto error_return;
1183 void *v = (void *)fields[i].out_value;
1184 buf_set_u32(v, 0, field_size, value);
1185 fields[i].in_value = NULL;
1188 /* did we have an endstate? */
1189 jtag_add_ir_scan(tap, fields, endstate);
1191 retval = jtag_execute_queue();
1193 error_return:
1194 for (i = 0; i < num_fields; i++)
1196 if (NULL != fields[i].out_value)
1197 free((void *)fields[i].out_value);
1200 free (fields);
1202 return retval;
1206 COMMAND_HANDLER(handle_verify_ircapture_command)
1208 if (CMD_ARGC > 1)
1209 return ERROR_COMMAND_SYNTAX_ERROR;
1211 if (CMD_ARGC == 1)
1213 bool enable;
1214 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1215 jtag_set_verify_capture_ir(enable);
1218 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1219 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1221 return ERROR_OK;
1224 COMMAND_HANDLER(handle_verify_jtag_command)
1226 if (CMD_ARGC > 1)
1227 return ERROR_COMMAND_SYNTAX_ERROR;
1229 if (CMD_ARGC == 1)
1231 bool enable;
1232 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1233 jtag_set_verify(enable);
1236 const char *status = jtag_will_verify() ? "enabled": "disabled";
1237 command_print(CMD_CTX, "verify jtag capture is %s", status);
1239 return ERROR_OK;
1242 COMMAND_HANDLER(handle_tms_sequence_command)
1244 if (CMD_ARGC > 1)
1245 return ERROR_COMMAND_SYNTAX_ERROR;
1247 if (CMD_ARGC == 1)
1249 bool use_new_table;
1250 if (strcmp(CMD_ARGV[0], "short") == 0)
1251 use_new_table = true;
1252 else if (strcmp(CMD_ARGV[0], "long") == 0)
1253 use_new_table = false;
1254 else
1255 return ERROR_COMMAND_SYNTAX_ERROR;
1257 tap_use_new_tms_table(use_new_table);
1260 command_print(CMD_CTX, "tms sequence is %s",
1261 tap_uses_new_tms_table() ? "short": "long");
1263 return ERROR_OK;
1266 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1268 if (CMD_ARGC != 1)
1269 return ERROR_COMMAND_SYNTAX_ERROR;
1271 int sleep_ms;
1272 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1274 jtag_set_flush_queue_sleep(sleep_ms);
1276 return ERROR_OK;
1279 COMMAND_HANDLER(handle_wait_srst_deassert)
1281 if (CMD_ARGC != 1)
1282 return ERROR_COMMAND_SYNTAX_ERROR;
1284 int timeout_ms;
1285 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1286 if ((timeout_ms <= 0) || (timeout_ms > 100000))
1288 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1289 return ERROR_FAIL;
1292 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1293 int asserted_yet;
1294 long long then = timeval_ms();
1295 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK)
1297 if ((timeval_ms() - then) > timeout_ms)
1299 LOG_ERROR("Timed out");
1300 return ERROR_FAIL;
1302 if (asserted_yet)
1303 break;
1305 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK)
1307 if ((timeval_ms() - then) > timeout_ms)
1309 LOG_ERROR("Timed out");
1310 return ERROR_FAIL;
1312 if (!asserted_yet)
1313 break;
1316 return ERROR_OK;
1321 static const struct command_registration jtag_command_handlers[] = {
1324 .name = "jtag_flush_queue_sleep",
1325 .handler = handle_jtag_flush_queue_sleep,
1326 .mode = COMMAND_ANY,
1327 .help = "For debug purposes(simulate long delays of interface) "
1328 "to test performance or change in behavior. Default 0ms.",
1329 .usage = "[sleep in ms]",
1332 .name = "jtag_rclk",
1333 .handler = handle_jtag_rclk_command,
1334 .mode = COMMAND_ANY,
1335 .help = "With an argument, change to to use adaptive clocking "
1336 "if possible; else to use the fallback speed. "
1337 "With or without argument, display current setting.",
1338 .usage = "[fallback_speed_khz]",
1341 .name = "jtag_ntrst_delay",
1342 .handler = handle_jtag_ntrst_delay_command,
1343 .mode = COMMAND_ANY,
1344 .help = "delay after deasserting trst in ms",
1345 .usage = "[milliseconds]",
1348 .name = "jtag_ntrst_assert_width",
1349 .handler = handle_jtag_ntrst_assert_width_command,
1350 .mode = COMMAND_ANY,
1351 .help = "delay after asserting trst in ms",
1352 .usage = "[milliseconds]",
1355 .name = "scan_chain",
1356 .handler = handle_scan_chain_command,
1357 .mode = COMMAND_ANY,
1358 .help = "print current scan chain configuration",
1361 .name = "jtag_reset",
1362 .handler = handle_jtag_reset_command,
1363 .mode = COMMAND_EXEC,
1364 .help = "Set reset line values. Value '1' is active, "
1365 "value '0' is inactive.",
1366 .usage = "trst_active srst_active",
1369 .name = "runtest",
1370 .handler = handle_runtest_command,
1371 .mode = COMMAND_EXEC,
1372 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1373 .usage = "num_cycles"
1376 .name = "irscan",
1377 .handler = handle_irscan_command,
1378 .mode = COMMAND_EXEC,
1379 .help = "Execute Instruction Register (DR) scan. The "
1380 "specified opcodes are put into each TAP's IR, "
1381 "and other TAPs are put in BYPASS.",
1382 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1385 .name = "verify_ircapture",
1386 .handler = handle_verify_ircapture_command,
1387 .mode = COMMAND_ANY,
1388 .help = "Display or assign flag controlling whether to "
1389 "verify values captured during Capture-IR.",
1390 .usage = "['enable'|'disable']",
1393 .name = "verify_jtag",
1394 .handler = handle_verify_jtag_command,
1395 .mode = COMMAND_ANY,
1396 .help = "Display or assign flag controlling whether to "
1397 "verify values captured during IR and DR scans.",
1398 .usage = "['enable'|'disable']",
1401 .name = "tms_sequence",
1402 .handler = handle_tms_sequence_command,
1403 .mode = COMMAND_ANY,
1404 .help = "Display or change what style TMS sequences to use "
1405 "for JTAG state transitions: short (default) or "
1406 "long. Only for working around JTAG bugs.",
1407 /* Specifically for working around DRIVER bugs... */
1408 .usage = "['short'|'long']",
1411 .name = "wait_srst_deassert",
1412 .handler = handle_wait_srst_deassert,
1413 .mode = COMMAND_ANY,
1414 .help = "Wait for an SRST deassert. "
1415 "Useful for cases where you need something to happen within ms "
1416 "of an srst deassert. Timeout in ms ",
1417 .usage = "ms",
1420 .name = "jtag",
1421 .mode = COMMAND_ANY,
1422 .help = "perform jtag tap actions",
1424 .chain = jtag_subcommand_handlers,
1427 .chain = jtag_command_handlers_to_move,
1429 COMMAND_REGISTRATION_DONE
1432 int jtag_register_commands(struct command_context *cmd_ctx)
1434 return register_commands(cmd_ctx, NULL, jtag_command_handlers);