Initialize return value.
[openocd.git] / src / jtag / tcl.c
blobb634ac0db57211440248d3174a529f0e0fd02806
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"
39 #ifdef HAVE_STRINGS_H
40 #include <strings.h>
41 #endif
43 #include <helper/time_support.h>
45 /**
46 * @file
47 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
50 static const Jim_Nvp nvp_jtag_tap_event[] = {
51 { .value = JTAG_TRST_ASSERTED, .name = "post-reset" },
52 { .value = JTAG_TAP_EVENT_SETUP, .name = "setup" },
53 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
54 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
56 { .name = NULL, .value = -1 }
59 extern struct jtag_interface *jtag_interface;
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)
76 case TAP_RESET:
77 case TAP_IDLE:
78 case TAP_DRPAUSE:
79 case TAP_IRPAUSE:
80 return true;
81 default:
82 return false;
86 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
88 int retval;
89 struct scan_field *fields;
90 int num_fields;
91 int field_count = 0;
92 int i, e;
93 struct jtag_tap *tap;
94 tap_state_t endstate;
96 /* args[1] = device
97 * args[2] = num_bits
98 * args[3] = hex string
99 * ... repeat num bits and hex string ...
101 * .. optionally:
102 * args[N-2] = "-endstate"
103 * args[N-1] = statename
105 if ((argc < 4) || ((argc % 2) != 0))
107 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
108 return JIM_ERR;
111 endstate = TAP_IDLE;
113 script_debug(interp, "drscan", argc, args);
115 /* validate arguments as numbers */
116 e = JIM_OK;
117 for (i = 2; i < argc; i += 2)
119 long bits;
120 const char *cp;
122 e = Jim_GetLong(interp, args[i], &bits);
123 /* If valid - try next arg */
124 if (e == JIM_OK) {
125 continue;
128 /* Not valid.. are we at the end? */
129 if (((i + 2) != argc)) {
130 /* nope, then error */
131 return e;
134 /* it could be: "-endstate FOO"
135 * e.g. DRPAUSE so we can issue more instructions
136 * before entering RUN/IDLE and executing them.
139 /* get arg as a string. */
140 cp = Jim_GetString(args[i], NULL);
141 /* is it the magic? */
142 if (0 == strcmp("-endstate", cp)) {
143 /* is the statename valid? */
144 cp = Jim_GetString(args[i + 1], NULL);
146 /* see if it is a valid state name */
147 endstate = tap_state_by_name(cp);
148 if (endstate < 0) {
149 /* update the error message */
150 Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
151 } else {
152 if (!scan_is_safe(endstate))
153 LOG_WARNING("drscan with unsafe "
154 "endstate \"%s\"", cp);
156 /* valid - so clear the error */
157 e = JIM_OK;
158 /* and remove the last 2 args */
159 argc -= 2;
163 /* Still an error? */
164 if (e != JIM_OK) {
165 return e; /* too bad */
167 } /* validate args */
169 assert(e == JIM_OK);
171 tap = jtag_tap_by_jim_obj(interp, args[1]);
172 if (tap == NULL) {
173 return JIM_ERR;
176 num_fields = (argc-2)/2;
177 assert(num_fields > 0);
178 fields = malloc(sizeof(struct scan_field) * num_fields);
179 for (i = 2; i < argc; i += 2)
181 long bits;
182 int len;
183 const char *str;
185 Jim_GetLong(interp, args[i], &bits);
186 str = Jim_GetString(args[i + 1], &len);
188 fields[field_count].num_bits = bits;
189 void * t = malloc(DIV_ROUND_UP(bits, 8));
190 fields[field_count].out_value = t;
191 str_to_buf(str, len, t, bits, 0);
192 fields[field_count].in_value = t;
193 field_count++;
196 jtag_add_dr_scan(tap, num_fields, fields, endstate);
198 retval = jtag_execute_queue();
199 if (retval != ERROR_OK)
201 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
202 return JIM_ERR;
205 field_count = 0;
206 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
207 for (i = 2; i < argc; i += 2)
209 long bits;
210 char *str;
212 Jim_GetLong(interp, args[i], &bits);
213 str = buf_to_str(fields[field_count].in_value, bits, 16);
214 free((void *)fields[field_count].out_value);
216 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
217 free(str);
218 field_count++;
221 Jim_SetResult(interp, list);
223 free(fields);
225 return JIM_OK;
229 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
231 tap_state_t states[8];
233 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
235 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
236 return JIM_ERR;
239 script_debug(interp, "pathmove", argc, args);
241 int i;
242 for (i = 0; i < argc-1; i++)
244 const char *cp;
245 cp = Jim_GetString(args[i + 1], NULL);
246 states[i] = tap_state_by_name(cp);
247 if (states[i] < 0)
249 /* update the error message */
250 Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
251 return JIM_ERR;
255 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
257 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
258 return JIM_ERR;
261 jtag_add_pathmove(argc-2, states + 1);
263 if (jtag_execute_queue()!= ERROR_OK)
265 Jim_SetResultString(interp, "pathmove: failed",-1);
266 return JIM_ERR;
269 return JIM_OK;
273 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
275 script_debug(interp, "flush_count", argc, args);
277 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
279 return JIM_OK;
282 /* REVISIT Just what about these should "move" ... ?
283 * These registrations, into the main JTAG table?
285 * There's a minor compatibility issue, these all show up twice;
286 * that's not desirable:
287 * - jtag drscan ... NOT DOCUMENTED!
288 * - drscan ...
290 * The "irscan" command (for example) doesn't show twice.
292 static const struct command_registration jtag_command_handlers_to_move[] = {
294 .name = "drscan",
295 .mode = COMMAND_EXEC,
296 .jim_handler = Jim_Command_drscan,
297 .help = "Execute Data Register (DR) scan for one TAP. "
298 "Other TAPs must be in BYPASS mode.",
299 .usage = "tap_name [num_bits value]* ['-endstate' state_name]",
302 .name = "flush_count",
303 .mode = COMMAND_EXEC,
304 .jim_handler = Jim_Command_flush_count,
305 .help = "Returns the number of times the JTAG queue "
306 "has been flushed.",
309 .name = "pathmove",
310 .mode = COMMAND_EXEC,
311 .jim_handler = Jim_Command_pathmove,
312 .usage = "start_state state1 [state2 [state3 ...]]",
313 .help = "Move JTAG state machine from current state "
314 "(start_state) to state1, then state2, state3, etc.",
316 COMMAND_REGISTRATION_DONE
320 enum jtag_tap_cfg_param {
321 JCFG_EVENT
324 static Jim_Nvp nvp_config_opts[] = {
325 { .name = "-event", .value = JCFG_EVENT },
327 { .name = NULL, .value = -1 }
330 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
332 if (goi->argc == 0)
334 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
335 return JIM_ERR;
338 Jim_Nvp *n;
339 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
340 if (e != JIM_OK)
342 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
343 return e;
346 if (goi->isconfigure) {
347 if (goi->argc != 1) {
348 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
349 return JIM_ERR;
351 } else {
352 if (goi->argc != 0) {
353 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
354 return JIM_ERR;
358 struct jtag_tap_event_action *jteap = tap->event_action;
359 /* replace existing event body */
360 bool found = false;
361 while (jteap)
363 if (jteap->event == (enum jtag_event)n->value)
365 found = true;
366 break;
368 jteap = jteap->next;
371 Jim_SetEmptyResult(goi->interp);
373 if (goi->isconfigure)
375 if (!found)
376 jteap = calloc(1, sizeof(*jteap));
377 else if (NULL != jteap->body)
378 Jim_DecrRefCount(goi->interp, jteap->body);
380 jteap->interp = goi->interp;
381 jteap->event = n->value;
383 Jim_Obj *o;
384 Jim_GetOpt_Obj(goi, &o);
385 jteap->body = Jim_DuplicateObj(goi->interp, o);
386 Jim_IncrRefCount(jteap->body);
388 if (!found)
390 /* add to head of event list */
391 jteap->next = tap->event_action;
392 tap->event_action = jteap;
395 else if (found)
397 jteap->interp = goi->interp;
398 Jim_SetResult(goi->interp,
399 Jim_DuplicateObj(goi->interp, jteap->body));
401 return JIM_OK;
404 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
406 /* parse config or cget options */
407 while (goi->argc > 0)
409 Jim_SetEmptyResult (goi->interp);
411 Jim_Nvp *n;
412 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
413 if (e != JIM_OK)
415 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
416 return e;
419 switch (n->value)
421 case JCFG_EVENT:
422 e = jtag_tap_configure_event(goi, tap);
423 if (e != JIM_OK)
424 return e;
425 break;
426 default:
427 Jim_SetResultFormatted(goi->interp, "unknown event: %s", n->name);
428 return JIM_ERR;
432 return JIM_OK;
435 static int is_bad_irval(int ir_length, jim_wide w)
437 jim_wide v = 1;
439 v <<= ir_length;
440 v -= 1;
441 v = ~v;
442 return (w & v) != 0;
445 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
446 struct jtag_tap *pTap)
448 jim_wide w;
449 int e = Jim_GetOpt_Wide(goi, &w);
450 if (e != JIM_OK) {
451 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
452 return e;
455 unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
456 uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
457 if (new_expected_ids == NULL)
459 Jim_SetResultFormatted(goi->interp, "no memory");
460 return JIM_ERR;
463 memcpy(new_expected_ids, pTap->expected_ids, expected_len);
465 new_expected_ids[pTap->expected_ids_cnt] = w;
467 free(pTap->expected_ids);
468 pTap->expected_ids = new_expected_ids;
469 pTap->expected_ids_cnt++;
471 return JIM_OK;
474 #define NTAP_OPT_IRLEN 0
475 #define NTAP_OPT_IRMASK 1
476 #define NTAP_OPT_IRCAPTURE 2
477 #define NTAP_OPT_ENABLED 3
478 #define NTAP_OPT_DISABLED 4
479 #define NTAP_OPT_EXPECTED_ID 5
480 #define NTAP_OPT_VERSION 6
482 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
483 struct jtag_tap *pTap)
485 jim_wide w;
486 int e = Jim_GetOpt_Wide(goi, &w);
487 if (e != JIM_OK)
489 Jim_SetResultFormatted(goi->interp,
490 "option: %s bad parameter", n->name);
491 free((void *)pTap->dotted_name);
492 return e;
494 switch (n->value) {
495 case NTAP_OPT_IRLEN:
496 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
498 LOG_WARNING("%s: huge IR length %d",
499 pTap->dotted_name, (int) w);
501 pTap->ir_length = w;
502 break;
503 case NTAP_OPT_IRMASK:
504 if (is_bad_irval(pTap->ir_length, w))
506 LOG_ERROR("%s: IR mask %x too big",
507 pTap->dotted_name,
508 (int) w);
509 return JIM_ERR;
511 if ((w & 3) != 3)
512 LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
513 pTap->ir_capture_mask = w;
514 break;
515 case NTAP_OPT_IRCAPTURE:
516 if (is_bad_irval(pTap->ir_length, w))
518 LOG_ERROR("%s: IR capture %x too big",
519 pTap->dotted_name, (int) w);
520 return JIM_ERR;
522 if ((w & 3) != 1)
523 LOG_WARNING("%s: nonstandard IR value",
524 pTap->dotted_name);
525 pTap->ir_capture_value = w;
526 break;
527 default:
528 return JIM_ERR;
530 return JIM_OK;
533 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
535 struct jtag_tap *pTap;
536 int x;
537 int e;
538 Jim_Nvp *n;
539 char *cp;
540 const Jim_Nvp opts[] = {
541 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
542 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
543 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
544 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
545 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
546 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
547 { .name = "-ignore-version" , .value = NTAP_OPT_VERSION },
548 { .name = NULL , .value = -1 },
551 pTap = calloc(1, sizeof(struct jtag_tap));
552 if (!pTap) {
553 Jim_SetResultFormatted(goi->interp, "no memory");
554 return JIM_ERR;
558 * we expect CHIP + TAP + OPTIONS
559 * */
560 if (goi->argc < 3) {
561 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
562 free(pTap);
563 return JIM_ERR;
565 Jim_GetOpt_String(goi, &cp, NULL);
566 pTap->chip = strdup(cp);
568 Jim_GetOpt_String(goi, &cp, NULL);
569 pTap->tapname = strdup(cp);
571 /* name + dot + name + null */
572 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
573 cp = malloc(x);
574 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
575 pTap->dotted_name = cp;
577 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
578 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
580 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
581 * that the default. The "-irlen" and "-irmask" options are only
582 * needed to cope with nonstandard TAPs, or to specify more bits.
584 pTap->ir_capture_mask = 0x03;
585 pTap->ir_capture_value = 0x01;
587 while (goi->argc) {
588 e = Jim_GetOpt_Nvp(goi, opts, &n);
589 if (e != JIM_OK) {
590 Jim_GetOpt_NvpUnknown(goi, opts, 0);
591 free((void *)pTap->dotted_name);
592 free(pTap);
593 return e;
595 LOG_DEBUG("Processing option: %s", n->name);
596 switch (n->value) {
597 case NTAP_OPT_ENABLED:
598 pTap->disabled_after_reset = false;
599 break;
600 case NTAP_OPT_DISABLED:
601 pTap->disabled_after_reset = true;
602 break;
603 case NTAP_OPT_EXPECTED_ID:
604 e = jim_newtap_expected_id(n, goi, pTap);
605 if (JIM_OK != e)
607 free((void *)pTap->dotted_name);
608 free(pTap);
609 return e;
611 break;
612 case NTAP_OPT_IRLEN:
613 case NTAP_OPT_IRMASK:
614 case NTAP_OPT_IRCAPTURE:
615 e = jim_newtap_ir_param(n, goi, pTap);
616 if (JIM_OK != e)
618 free((void *)pTap->dotted_name);
619 free(pTap);
620 return e;
622 break;
623 case NTAP_OPT_VERSION:
624 pTap->ignore_version = true;
625 break;
626 } /* switch (n->value) */
627 } /* while (goi->argc) */
629 /* default is enabled-after-reset */
630 pTap->enabled = !pTap->disabled_after_reset;
632 /* Did all the required option bits get cleared? */
633 if (pTap->ir_length != 0)
635 jtag_tap_init(pTap);
636 return JIM_OK;
639 Jim_SetResultFormatted(goi->interp,
640 "newtap: %s missing IR length",
641 pTap->dotted_name);
642 jtag_tap_free(pTap);
643 return JIM_ERR;
646 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
648 struct jtag_tap_event_action * jteap;
650 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
652 if (jteap->event != e)
653 continue;
655 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
656 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
657 tap->dotted_name, e, nvp->name,
658 Jim_GetString(jteap->body, NULL));
660 if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK)
662 Jim_MakeErrorMessage(jteap->interp);
663 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
664 continue;
667 switch (e)
669 case JTAG_TAP_EVENT_ENABLE:
670 case JTAG_TAP_EVENT_DISABLE:
671 /* NOTE: we currently assume the handlers
672 * can't fail. Right here is where we should
673 * really be verifying the scan chains ...
675 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
676 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
677 tap->enabled ? "enabled" : "disabled");
678 break;
679 default:
680 break;
685 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
687 Jim_GetOptInfo goi;
688 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
689 if (goi.argc != 0) {
690 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
691 return JIM_ERR;
693 struct command_context *context = current_command_context(interp);
694 int e = jtag_init_inner(context);
695 if (e != ERROR_OK) {
696 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
697 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
698 Jim_FreeNewObj(goi.interp, eObj);
699 return JIM_ERR;
701 return JIM_OK;
704 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
706 Jim_GetOptInfo goi;
707 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
708 if (goi.argc != 0) {
709 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
710 return JIM_ERR;
712 struct command_context *context = current_command_context(interp);
713 int e = jtag_init_reset(context);
714 if (e != ERROR_OK) {
715 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
716 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
717 Jim_FreeNewObj(goi.interp, eObj);
718 return JIM_ERR;
720 return JIM_OK;
723 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
725 Jim_GetOptInfo goi;
726 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
727 return jim_newtap_cmd(&goi);
730 static bool jtag_tap_enable(struct jtag_tap *t)
732 if (t->enabled)
733 return false;
734 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
735 if (!t->enabled)
736 return false;
738 /* FIXME add JTAG sanity checks, w/o TLR
739 * - scan chain length grew by one (this)
740 * - IDs and IR lengths are as expected
742 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
743 return true;
745 static bool jtag_tap_disable(struct jtag_tap *t)
747 if (!t->enabled)
748 return false;
749 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
750 if (t->enabled)
751 return false;
753 /* FIXME add JTAG sanity checks, w/o TLR
754 * - scan chain length shrank by one (this)
755 * - IDs and IR lengths are as expected
757 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
758 return true;
761 static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
763 const char *cmd_name = Jim_GetString(argv[0], NULL);
764 Jim_GetOptInfo goi;
765 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
766 if (goi.argc != 1) {
767 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
768 return JIM_ERR;
771 struct jtag_tap *t;
773 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
774 if (t == NULL)
775 return JIM_ERR;
777 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
778 // do nothing, just return the value
779 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
780 if (!jtag_tap_enable(t)){
781 LOG_WARNING("failed to enable tap %s", t->dotted_name);
782 return JIM_ERR;
784 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
785 if (!jtag_tap_disable(t)){
786 LOG_WARNING("failed to disable tap %s", t->dotted_name);
787 return JIM_ERR;
789 } else {
790 LOG_ERROR("command '%s' unknown", cmd_name);
791 return JIM_ERR;
793 bool e = t->enabled;
794 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
795 return JIM_OK;
798 static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
800 const char *cmd_name = Jim_GetString(argv[0], NULL);
801 Jim_GetOptInfo goi;
802 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
803 goi.isconfigure = !strcmp(cmd_name, "configure");
804 if (goi.argc < 2 + goi.isconfigure) {
805 Jim_WrongNumArgs(goi.interp, 0, NULL,
806 "<tap_name> <attribute> ...");
807 return JIM_ERR;
810 struct jtag_tap *t;
812 Jim_Obj *o;
813 Jim_GetOpt_Obj(&goi, &o);
814 t = jtag_tap_by_jim_obj(goi.interp, o);
815 if (t == NULL) {
816 return JIM_ERR;
819 return jtag_tap_configure_cmd(&goi, t);
822 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
824 Jim_GetOptInfo goi;
825 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
826 if (goi.argc != 0) {
827 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
828 return JIM_ERR;
830 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
831 struct jtag_tap *tap;
833 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
834 Jim_ListAppendElement(goi.interp,
835 Jim_GetResult(goi.interp),
836 Jim_NewStringObj(goi.interp,
837 tap->dotted_name, -1));
839 return JIM_OK;
842 COMMAND_HANDLER(handle_jtag_init_command)
844 if (CMD_ARGC != 0)
845 return ERROR_COMMAND_SYNTAX_ERROR;
847 static bool jtag_initialized = false;
848 if (jtag_initialized)
850 LOG_INFO("'jtag init' has already been called");
851 return ERROR_OK;
853 jtag_initialized = true;
855 LOG_DEBUG("Initializing jtag devices...");
856 return jtag_init(CMD_CTX);
859 static const struct command_registration jtag_subcommand_handlers[] = {
861 .name = "init",
862 .mode = COMMAND_ANY,
863 .handler = handle_jtag_init_command,
864 .help = "initialize jtag scan chain",
867 .name = "arp_init",
868 .mode = COMMAND_ANY,
869 .jim_handler = jim_jtag_arp_init,
870 .help = "Validates JTAG scan chain against the list of "
871 "declared TAPs using just the four standard JTAG "
872 "signals.",
875 .name = "arp_init-reset",
876 .mode = COMMAND_ANY,
877 .jim_handler = jim_jtag_arp_init_reset,
878 .help = "Uses TRST and SRST to try resetting everything on "
879 "the JTAG scan chain, then performs 'jtag arp_init'."
882 .name = "newtap",
883 .mode = COMMAND_CONFIG,
884 .jim_handler = jim_jtag_newtap,
885 .help = "Create a new TAP instance named basename.tap_type, "
886 "and appends it to the scan chain.",
887 .usage = "basename tap_type '-irlen' count "
888 "['-enable'|'-disable'] "
889 "['-expected_id' number] "
890 "['-ignore-version'] "
891 "['-ircapture' number] "
892 "['-mask' number] ",
895 .name = "tapisenabled",
896 .mode = COMMAND_EXEC,
897 .jim_handler = jim_jtag_tap_enabler,
898 .help = "Returns a Tcl boolean (0/1) indicating whether "
899 "the TAP is enabled (1) or not (0).",
900 .usage = "tap_name",
903 .name = "tapenable",
904 .mode = COMMAND_EXEC,
905 .jim_handler = jim_jtag_tap_enabler,
906 .help = "Try to enable the specified TAP using the "
907 "'tap-enable' TAP event.",
908 .usage = "tap_name",
911 .name = "tapdisable",
912 .mode = COMMAND_EXEC,
913 .jim_handler = jim_jtag_tap_enabler,
914 .help = "Try to disable the specified TAP using the "
915 "'tap-disable' TAP event.",
916 .usage = "tap_name",
919 .name = "configure",
920 .mode = COMMAND_EXEC,
921 .jim_handler = jim_jtag_configure,
922 .help = "Provide a Tcl handler for the specified "
923 "TAP event.",
924 .usage = "tap_name '-event' event_name handler",
927 .name = "cget",
928 .mode = COMMAND_EXEC,
929 .jim_handler = jim_jtag_configure,
930 .help = "Return any Tcl handler for the specified "
931 "TAP event.",
932 .usage = "tap_name '-event' event_name",
935 .name = "names",
936 .mode = COMMAND_ANY,
937 .jim_handler = jim_jtag_names,
938 .help = "Returns list of all JTAG tap names.",
941 .chain = jtag_command_handlers_to_move,
943 COMMAND_REGISTRATION_DONE
946 void jtag_notify_event(enum jtag_event event)
948 struct jtag_tap *tap;
950 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
951 jtag_tap_handle_event(tap, event);
955 COMMAND_HANDLER(handle_scan_chain_command)
957 struct jtag_tap *tap;
958 char expected_id[12];
960 tap = jtag_all_taps();
961 command_print(CMD_CTX,
962 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
963 command_print(CMD_CTX,
964 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
966 while (tap) {
967 uint32_t expected, expected_mask, ii;
969 snprintf(expected_id, sizeof expected_id, "0x%08x",
970 (unsigned)((tap->expected_ids_cnt > 0)
971 ? tap->expected_ids[0]
972 : 0));
973 if (tap->ignore_version)
974 expected_id[2] = '*';
976 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
977 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
979 command_print(CMD_CTX,
980 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
981 tap->abs_chain_position,
982 tap->dotted_name,
983 tap->enabled ? 'Y' : 'n',
984 (unsigned int)(tap->idcode),
985 expected_id,
986 (unsigned int)(tap->ir_length),
987 (unsigned int)(expected),
988 (unsigned int)(expected_mask));
990 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
991 snprintf(expected_id, sizeof expected_id, "0x%08x",
992 (unsigned) tap->expected_ids[1]);
993 if (tap->ignore_version)
994 expected_id[2] = '*';
996 command_print(CMD_CTX,
997 " %s",
998 expected_id);
1001 tap = tap->next_tap;
1004 return ERROR_OK;
1007 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1009 if (CMD_ARGC > 1)
1010 return ERROR_COMMAND_SYNTAX_ERROR;
1011 if (CMD_ARGC == 1)
1013 unsigned delay;
1014 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1016 jtag_set_ntrst_delay(delay);
1018 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1019 return ERROR_OK;
1022 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1024 if (CMD_ARGC > 1)
1025 return ERROR_COMMAND_SYNTAX_ERROR;
1026 if (CMD_ARGC == 1)
1028 unsigned delay;
1029 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1031 jtag_set_ntrst_assert_width(delay);
1033 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1034 return ERROR_OK;
1037 COMMAND_HANDLER(handle_jtag_rclk_command)
1039 if (CMD_ARGC > 1)
1040 return ERROR_COMMAND_SYNTAX_ERROR;
1042 int retval = ERROR_OK;
1043 if (CMD_ARGC == 1)
1045 unsigned khz = 0;
1046 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1048 retval = jtag_config_rclk(khz);
1049 if (ERROR_OK != retval)
1050 return retval;
1053 int cur_khz = jtag_get_speed_khz();
1054 retval = jtag_get_speed_readable(&cur_khz);
1055 if (ERROR_OK != retval)
1056 return retval;
1058 if (cur_khz)
1059 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1060 else
1061 command_print(CMD_CTX, "RCLK - adaptive");
1063 return retval;
1066 COMMAND_HANDLER(handle_jtag_reset_command)
1068 if (CMD_ARGC != 2)
1069 return ERROR_COMMAND_SYNTAX_ERROR;
1071 int trst = -1;
1072 if (CMD_ARGV[0][0] == '1')
1073 trst = 1;
1074 else if (CMD_ARGV[0][0] == '0')
1075 trst = 0;
1076 else
1077 return ERROR_COMMAND_SYNTAX_ERROR;
1079 int srst = -1;
1080 if (CMD_ARGV[1][0] == '1')
1081 srst = 1;
1082 else if (CMD_ARGV[1][0] == '0')
1083 srst = 0;
1084 else
1085 return ERROR_COMMAND_SYNTAX_ERROR;
1087 if (adapter_init(CMD_CTX) != ERROR_OK)
1088 return ERROR_JTAG_INIT_FAILED;
1090 jtag_add_reset(trst, srst);
1091 return jtag_execute_queue();
1094 COMMAND_HANDLER(handle_runtest_command)
1096 if (CMD_ARGC != 1)
1097 return ERROR_COMMAND_SYNTAX_ERROR;
1099 unsigned num_clocks;
1100 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1102 jtag_add_runtest(num_clocks, TAP_IDLE);
1103 return jtag_execute_queue();
1107 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1108 * should be stable ... and *NOT* a shift state, otherwise free-running
1109 * jtag clocks could change the values latched by the update state.
1110 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1111 * and "drscan" commands are a write-only subset of what SVF provides.
1114 COMMAND_HANDLER(handle_irscan_command)
1116 int i;
1117 struct scan_field *fields;
1118 struct jtag_tap *tap = NULL;
1119 tap_state_t endstate;
1121 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1123 return ERROR_COMMAND_SYNTAX_ERROR;
1126 /* optional "-endstate" "statename" at the end of the arguments,
1127 * so that e.g. IRPAUSE can let us load the data register before
1128 * entering RUN/IDLE to execute the instruction we load here.
1130 endstate = TAP_IDLE;
1132 if (CMD_ARGC >= 4) {
1133 /* have at least one pair of numbers. */
1134 /* is last pair the magic text? */
1135 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1136 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1137 if (endstate == TAP_INVALID)
1138 return ERROR_COMMAND_SYNTAX_ERROR;
1139 if (!scan_is_safe(endstate))
1140 LOG_WARNING("unstable irscan endstate \"%s\"",
1141 CMD_ARGV[CMD_ARGC - 1]);
1142 CMD_ARGC -= 2;
1146 int num_fields = CMD_ARGC / 2;
1147 if (num_fields > 1)
1149 /* we really should be looking at plain_ir_scan if we want
1150 * anything more fancy.
1152 LOG_ERROR("Specify a single value for tap");
1153 return ERROR_COMMAND_SYNTAX_ERROR;
1156 size_t fields_len = sizeof(struct scan_field) * num_fields;
1157 fields = malloc(fields_len);
1158 memset(fields, 0, fields_len);
1160 int retval;
1161 for (i = 0; i < num_fields; i++)
1163 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1164 if (tap == NULL)
1166 int j;
1167 for (j = 0; j < i; j++)
1168 free((void *)fields[j].out_value);
1169 free(fields);
1170 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1172 return ERROR_FAIL;
1174 int field_size = tap->ir_length;
1175 fields[i].num_bits = field_size;
1176 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1178 uint32_t value;
1179 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1180 if (ERROR_OK != retval)
1181 goto error_return;
1182 void *v = (void *)fields[i].out_value;
1183 buf_set_u32(v, 0, field_size, value);
1184 fields[i].in_value = NULL;
1187 /* did we have an endstate? */
1188 jtag_add_ir_scan(tap, fields, endstate);
1190 retval = jtag_execute_queue();
1192 error_return:
1193 for (i = 0; i < num_fields; i++)
1195 if (NULL != fields[i].out_value)
1196 free((void *)fields[i].out_value);
1199 free (fields);
1201 return retval;
1205 COMMAND_HANDLER(handle_verify_ircapture_command)
1207 if (CMD_ARGC > 1)
1208 return ERROR_COMMAND_SYNTAX_ERROR;
1210 if (CMD_ARGC == 1)
1212 bool enable;
1213 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1214 jtag_set_verify_capture_ir(enable);
1217 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1218 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1220 return ERROR_OK;
1223 COMMAND_HANDLER(handle_verify_jtag_command)
1225 if (CMD_ARGC > 1)
1226 return ERROR_COMMAND_SYNTAX_ERROR;
1228 if (CMD_ARGC == 1)
1230 bool enable;
1231 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1232 jtag_set_verify(enable);
1235 const char *status = jtag_will_verify() ? "enabled": "disabled";
1236 command_print(CMD_CTX, "verify jtag capture is %s", status);
1238 return ERROR_OK;
1241 COMMAND_HANDLER(handle_tms_sequence_command)
1243 if (CMD_ARGC > 1)
1244 return ERROR_COMMAND_SYNTAX_ERROR;
1246 if (CMD_ARGC == 1)
1248 bool use_new_table;
1249 if (strcmp(CMD_ARGV[0], "short") == 0)
1250 use_new_table = true;
1251 else if (strcmp(CMD_ARGV[0], "long") == 0)
1252 use_new_table = false;
1253 else
1254 return ERROR_COMMAND_SYNTAX_ERROR;
1256 tap_use_new_tms_table(use_new_table);
1259 command_print(CMD_CTX, "tms sequence is %s",
1260 tap_uses_new_tms_table() ? "short": "long");
1262 return ERROR_OK;
1265 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1267 if (CMD_ARGC != 1)
1268 return ERROR_COMMAND_SYNTAX_ERROR;
1270 int sleep_ms;
1271 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1273 jtag_set_flush_queue_sleep(sleep_ms);
1275 return ERROR_OK;
1278 COMMAND_HANDLER(handle_wait_srst_deassert)
1280 if (CMD_ARGC != 1)
1281 return ERROR_COMMAND_SYNTAX_ERROR;
1283 int timeout_ms;
1284 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1285 if ((timeout_ms <= 0) || (timeout_ms > 100000))
1287 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1288 return ERROR_FAIL;
1291 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1292 int asserted_yet;
1293 long long then = timeval_ms();
1294 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK)
1296 if ((timeval_ms() - then) > timeout_ms)
1298 LOG_ERROR("Timed out");
1299 return ERROR_FAIL;
1301 if (asserted_yet)
1302 break;
1304 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK)
1306 if ((timeval_ms() - then) > timeout_ms)
1308 LOG_ERROR("Timed out");
1309 return ERROR_FAIL;
1311 if (!asserted_yet)
1312 break;
1315 return ERROR_OK;
1320 static const struct command_registration jtag_command_handlers[] = {
1323 .name = "jtag_flush_queue_sleep",
1324 .handler = handle_jtag_flush_queue_sleep,
1325 .mode = COMMAND_ANY,
1326 .help = "For debug purposes(simulate long delays of interface) "
1327 "to test performance or change in behavior. Default 0ms.",
1328 .usage = "[sleep in ms]",
1331 .name = "jtag_rclk",
1332 .handler = handle_jtag_rclk_command,
1333 .mode = COMMAND_ANY,
1334 .help = "With an argument, change to to use adaptive clocking "
1335 "if possible; else to use the fallback speed. "
1336 "With or without argument, display current setting.",
1337 .usage = "[fallback_speed_khz]",
1340 .name = "jtag_ntrst_delay",
1341 .handler = handle_jtag_ntrst_delay_command,
1342 .mode = COMMAND_ANY,
1343 .help = "delay after deasserting trst in ms",
1344 .usage = "[milliseconds]",
1347 .name = "jtag_ntrst_assert_width",
1348 .handler = handle_jtag_ntrst_assert_width_command,
1349 .mode = COMMAND_ANY,
1350 .help = "delay after asserting trst in ms",
1351 .usage = "[milliseconds]",
1354 .name = "scan_chain",
1355 .handler = handle_scan_chain_command,
1356 .mode = COMMAND_ANY,
1357 .help = "print current scan chain configuration",
1360 .name = "jtag_reset",
1361 .handler = handle_jtag_reset_command,
1362 .mode = COMMAND_EXEC,
1363 .help = "Set reset line values. Value '1' is active, "
1364 "value '0' is inactive.",
1365 .usage = "trst_active srst_active",
1368 .name = "runtest",
1369 .handler = handle_runtest_command,
1370 .mode = COMMAND_EXEC,
1371 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1372 .usage = "num_cycles"
1375 .name = "irscan",
1376 .handler = handle_irscan_command,
1377 .mode = COMMAND_EXEC,
1378 .help = "Execute Instruction Register (DR) scan. The "
1379 "specified opcodes are put into each TAP's IR, "
1380 "and other TAPs are put in BYPASS.",
1381 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1384 .name = "verify_ircapture",
1385 .handler = handle_verify_ircapture_command,
1386 .mode = COMMAND_ANY,
1387 .help = "Display or assign flag controlling whether to "
1388 "verify values captured during Capture-IR.",
1389 .usage = "['enable'|'disable']",
1392 .name = "verify_jtag",
1393 .handler = handle_verify_jtag_command,
1394 .mode = COMMAND_ANY,
1395 .help = "Display or assign flag controlling whether to "
1396 "verify values captured during IR and DR scans.",
1397 .usage = "['enable'|'disable']",
1400 .name = "tms_sequence",
1401 .handler = handle_tms_sequence_command,
1402 .mode = COMMAND_ANY,
1403 .help = "Display or change what style TMS sequences to use "
1404 "for JTAG state transitions: short (default) or "
1405 "long. Only for working around JTAG bugs.",
1406 /* Specifically for working around DRIVER bugs... */
1407 .usage = "['short'|'long']",
1410 .name = "wait_srst_deassert",
1411 .handler = handle_wait_srst_deassert,
1412 .mode = COMMAND_ANY,
1413 .help = "Wait for an SRST deassert. "
1414 "Useful for cases where you need something to happen within ms "
1415 "of an srst deassert. Timeout in ms ",
1416 .usage = "ms",
1419 .name = "jtag",
1420 .mode = COMMAND_ANY,
1421 .help = "perform jtag tap actions",
1423 .chain = jtag_subcommand_handlers,
1426 .chain = jtag_command_handlers_to_move,
1428 COMMAND_REGISTRATION_DONE
1431 int jtag_register_commands(struct command_context *cmd_ctx)
1433 return register_commands(cmd_ctx, NULL, jtag_command_handlers);