helper/align.h: Fix macro IS_PWR_OF_2
[openocd.git] / src / jtag / tcl.c
blob790aedfc4618eb168297205b4abb473299c78830
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
6 * *
7 * Copyright (C) 2007-2010 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
9 * *
10 * Copyright (C) 2009 SoftPLC Corporation *
11 * http://softplc.com *
12 * dick@softplc.com *
13 * *
14 * Copyright (C) 2009 Zachary T Welch *
15 * zw@superlucidity.net *
16 ***************************************************************************/
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
22 #include "adapter.h"
23 #include "jtag.h"
24 #include "swd.h"
25 #include "minidriver.h"
26 #include "interface.h"
27 #include "interfaces.h"
28 #include "tcl.h"
30 #ifdef HAVE_STRINGS_H
31 #include <strings.h>
32 #endif
34 #include <helper/command.h>
35 #include <helper/nvp.h>
36 #include <helper/time_support.h>
37 #include "transport/transport.h"
39 /**
40 * @file
41 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
44 static const struct nvp nvp_jtag_tap_event[] = {
45 { .value = JTAG_TRST_ASSERTED, .name = "post-reset" },
46 { .value = JTAG_TAP_EVENT_SETUP, .name = "setup" },
47 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
48 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
50 { .name = NULL, .value = -1 }
53 struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
55 const char *cp = Jim_GetString(o, NULL);
56 struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
57 if (!cp)
58 cp = "(unknown)";
59 if (!t)
60 Jim_SetResultFormatted(interp, "Tap '%s' could not be found", cp);
61 return t;
64 static bool scan_is_safe(tap_state_t state)
66 switch (state) {
67 case TAP_RESET:
68 case TAP_IDLE:
69 case TAP_DRPAUSE:
70 case TAP_IRPAUSE:
71 return true;
72 default:
73 return false;
77 static COMMAND_HELPER(handle_jtag_command_drscan_fields, struct scan_field *fields)
79 unsigned int field_count = 0;
80 for (unsigned int i = 1; i < CMD_ARGC; i += 2) {
81 unsigned int bits;
82 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[i], bits);
83 fields[field_count].num_bits = bits;
85 void *t = malloc(DIV_ROUND_UP(bits, 8));
86 if (!t) {
87 LOG_ERROR("Out of memory");
88 return ERROR_FAIL;
91 fields[field_count].out_value = t;
92 int ret = CALL_COMMAND_HANDLER(command_parse_str_to_buf, CMD_ARGV[i + 1], t, bits);
93 if (ret != ERROR_OK)
94 return ret;
95 fields[field_count].in_value = t;
96 field_count++;
99 return ERROR_OK;
102 COMMAND_HANDLER(handle_jtag_command_drscan)
105 * CMD_ARGV[0] = device
106 * CMD_ARGV[1] = num_bits
107 * CMD_ARGV[2] = hex string
108 * ... repeat num bits and hex string ...
110 * ... optionally:
111 * CMD_ARGV[CMD_ARGC-2] = "-endstate"
112 * CMD_ARGV[CMD_ARGC-1] = statename
115 if (CMD_ARGC < 3 || (CMD_ARGC % 2) != 1)
116 return ERROR_COMMAND_SYNTAX_ERROR;
118 struct jtag_tap *tap = jtag_tap_by_string(CMD_ARGV[0]);
119 if (!tap) {
120 command_print(CMD, "Tap '%s' could not be found", CMD_ARGV[0]);
121 return ERROR_COMMAND_ARGUMENT_INVALID;
124 if (tap->bypass) {
125 command_print(CMD, "Can't execute as the selected tap is in BYPASS");
126 return ERROR_FAIL;
129 tap_state_t endstate = TAP_IDLE;
130 if (CMD_ARGC > 3 && !strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2])) {
131 const char *state_name = CMD_ARGV[CMD_ARGC - 1];
132 endstate = tap_state_by_name(state_name);
133 if (endstate < 0) {
134 command_print(CMD, "endstate: %s invalid", state_name);
135 return ERROR_COMMAND_ARGUMENT_INVALID;
138 if (!scan_is_safe(endstate))
139 LOG_WARNING("drscan with unsafe endstate \"%s\"", state_name);
141 CMD_ARGC -= 2;
144 unsigned int num_fields = (CMD_ARGC - 1) / 2;
145 struct scan_field *fields = calloc(num_fields, sizeof(struct scan_field));
146 if (!fields) {
147 LOG_ERROR("Out of memory");
148 return ERROR_FAIL;
151 int retval = CALL_COMMAND_HANDLER(handle_jtag_command_drscan_fields, fields);
152 if (retval != ERROR_OK)
153 goto fail;
155 jtag_add_dr_scan(tap, num_fields, fields, endstate);
157 retval = jtag_execute_queue();
158 if (retval != ERROR_OK) {
159 command_print(CMD, "drscan: jtag execute failed");
160 goto fail;
163 for (unsigned int i = 0; i < num_fields; i++) {
164 char *str = buf_to_hex_str(fields[i].in_value, fields[i].num_bits);
165 command_print(CMD, "%s", str);
166 free(str);
169 fail:
170 for (unsigned int i = 0; i < num_fields; i++)
171 free(fields[i].in_value);
172 free(fields);
174 return retval;
177 COMMAND_HANDLER(handle_jtag_command_pathmove)
179 tap_state_t states[8];
181 if (CMD_ARGC < 1 || CMD_ARGC > ARRAY_SIZE(states))
182 return ERROR_COMMAND_SYNTAX_ERROR;
184 for (unsigned int i = 0; i < CMD_ARGC; i++) {
185 states[i] = tap_state_by_name(CMD_ARGV[i]);
186 if (states[i] < 0) {
187 command_print(CMD, "endstate: %s invalid", CMD_ARGV[i]);
188 return ERROR_COMMAND_ARGUMENT_INVALID;
192 int retval = jtag_add_statemove(states[0]);
193 if (retval == ERROR_OK)
194 retval = jtag_execute_queue();
195 if (retval != ERROR_OK) {
196 command_print(CMD, "pathmove: jtag execute failed");
197 return retval;
200 jtag_add_pathmove(CMD_ARGC - 1, states + 1);
201 retval = jtag_execute_queue();
202 if (retval != ERROR_OK) {
203 command_print(CMD, "pathmove: failed");
204 return retval;
207 return ERROR_OK;
210 COMMAND_HANDLER(handle_jtag_flush_count)
212 if (CMD_ARGC != 0)
213 return ERROR_COMMAND_SYNTAX_ERROR;
215 const unsigned int count = jtag_get_flush_queue_count();
216 command_print_sameline(CMD, "%u", count);
218 return ERROR_OK;
221 /* REVISIT Just what about these should "move" ... ?
222 * These registrations, into the main JTAG table?
224 * There's a minor compatibility issue, these all show up twice;
225 * that's not desirable:
226 * - jtag drscan ... NOT DOCUMENTED!
227 * - drscan ...
229 * The "irscan" command (for example) doesn't show twice.
231 static const struct command_registration jtag_command_handlers_to_move[] = {
233 .name = "drscan",
234 .mode = COMMAND_EXEC,
235 .handler = handle_jtag_command_drscan,
236 .help = "Execute Data Register (DR) scan for one TAP. "
237 "Other TAPs must be in BYPASS mode.",
238 .usage = "tap_name (num_bits value)+ ['-endstate' state_name]",
241 .name = "flush_count",
242 .mode = COMMAND_EXEC,
243 .handler = handle_jtag_flush_count,
244 .help = "Returns the number of times the JTAG queue "
245 "has been flushed.",
246 .usage = "",
249 .name = "pathmove",
250 .mode = COMMAND_EXEC,
251 .handler = handle_jtag_command_pathmove,
252 .usage = "start_state state1 [state2 [state3 ...]]",
253 .help = "Move JTAG state machine from current state "
254 "(start_state) to state1, then state2, state3, etc.",
256 COMMAND_REGISTRATION_DONE
260 enum jtag_tap_cfg_param {
261 JCFG_EVENT,
262 JCFG_IDCODE,
265 static struct nvp nvp_config_opts[] = {
266 { .name = "-event", .value = JCFG_EVENT },
267 { .name = "-idcode", .value = JCFG_IDCODE },
269 { .name = NULL, .value = -1 }
272 static int jtag_tap_set_event(struct command_context *cmd_ctx, struct jtag_tap *tap,
273 const struct nvp *event, Jim_Obj *body)
275 struct jtag_tap_event_action *jteap = tap->event_action;
277 while (jteap) {
278 if (jteap->event == (enum jtag_event)event->value)
279 break;
280 jteap = jteap->next;
283 if (!jteap) {
284 jteap = calloc(1, sizeof(*jteap));
285 if (!jteap) {
286 LOG_ERROR("Out of memory");
287 return ERROR_FAIL;
290 /* add to head of event list */
291 jteap->next = tap->event_action;
292 tap->event_action = jteap;
293 } else {
294 Jim_DecrRefCount(cmd_ctx->interp, jteap->body);
297 jteap->interp = cmd_ctx->interp;
298 jteap->event = (enum jtag_event)event->value;
299 jteap->body = Jim_DuplicateObj(cmd_ctx->interp, body);
300 Jim_IncrRefCount(jteap->body);
302 return ERROR_OK;
305 __COMMAND_HANDLER(handle_jtag_configure)
307 bool is_configure = !strcmp(CMD_NAME, "configure");
309 if (CMD_ARGC < (is_configure ? 3 : 2))
310 return ERROR_COMMAND_SYNTAX_ERROR;
312 /* FIXME: rework jtag_tap_by_jim_obj */
313 struct jtag_tap *tap = jtag_tap_by_jim_obj(CMD_CTX->interp, CMD_JIMTCL_ARGV[0]);
314 if (!tap)
315 return ERROR_FAIL;
317 for (unsigned int i = 1; i < CMD_ARGC; i++) {
318 const struct nvp *n = nvp_name2value(nvp_config_opts, CMD_ARGV[i]);
319 switch (n->value) {
320 case JCFG_EVENT:
321 if (i + (is_configure ? 2 : 1) >= CMD_ARGC) {
322 command_print(CMD, "wrong # args: should be \"-event <event-name>%s\"",
323 is_configure ? " <event-body>" : "");
324 return ERROR_COMMAND_ARGUMENT_INVALID;
327 const struct nvp *event = nvp_name2value(nvp_jtag_tap_event, CMD_ARGV[i + 1]);
328 if (!event->name) {
329 nvp_unknown_command_print(CMD, nvp_jtag_tap_event, CMD_ARGV[i], CMD_ARGV[i + 1]);
330 return ERROR_COMMAND_ARGUMENT_INVALID;
333 if (is_configure) {
334 int retval = jtag_tap_set_event(CMD_CTX, tap, event, CMD_JIMTCL_ARGV[i + 2]);
335 if (retval != ERROR_OK)
336 return retval;
337 } else {
338 struct jtag_tap_event_action *jteap = tap->event_action;
339 while (jteap) {
340 if (jteap->event == (enum jtag_event)event->value) {
341 command_print(CMD, "%s", Jim_GetString(jteap->body, NULL));
342 break;
344 jteap = jteap->next;
348 i += is_configure ? 2 : 1;
349 break;
350 case JCFG_IDCODE:
351 if (is_configure) {
352 command_print(CMD, "not settable: %s", n->name);
353 return ERROR_COMMAND_ARGUMENT_INVALID;
355 command_print(CMD, "0x%08x", tap->idcode);
356 break;
357 default:
358 nvp_unknown_command_print(CMD, nvp_config_opts, NULL, CMD_ARGV[i]);
359 return ERROR_COMMAND_ARGUMENT_INVALID;
362 return ERROR_OK;
365 #define NTAP_OPT_IRLEN 0
366 #define NTAP_OPT_IRMASK 1
367 #define NTAP_OPT_IRCAPTURE 2
368 #define NTAP_OPT_ENABLED 3
369 #define NTAP_OPT_DISABLED 4
370 #define NTAP_OPT_EXPECTED_ID 5
371 #define NTAP_OPT_VERSION 6
372 #define NTAP_OPT_BYPASS 7
373 #define NTAP_OPT_IRBYPASS 8
375 static const struct nvp jtag_newtap_opts[] = {
376 { .name = "-irlen", .value = NTAP_OPT_IRLEN },
377 { .name = "-irmask", .value = NTAP_OPT_IRMASK },
378 { .name = "-ircapture", .value = NTAP_OPT_IRCAPTURE },
379 { .name = "-enable", .value = NTAP_OPT_ENABLED },
380 { .name = "-disable", .value = NTAP_OPT_DISABLED },
381 { .name = "-expected-id", .value = NTAP_OPT_EXPECTED_ID },
382 { .name = "-ignore-version", .value = NTAP_OPT_VERSION },
383 { .name = "-ignore-bypass", .value = NTAP_OPT_BYPASS },
384 { .name = "-ir-bypass", .value = NTAP_OPT_IRBYPASS },
385 { .name = NULL, .value = -1 },
388 static COMMAND_HELPER(handle_jtag_newtap_args, struct jtag_tap *tap)
390 /* we expect CHIP + TAP + OPTIONS */
391 if (CMD_ARGC < 2)
392 return ERROR_COMMAND_SYNTAX_ERROR;
394 tap->chip = strdup(CMD_ARGV[0]);
395 tap->tapname = strdup(CMD_ARGV[1]);
396 tap->dotted_name = alloc_printf("%s.%s", CMD_ARGV[0], CMD_ARGV[1]);
397 if (!tap->chip || !tap->tapname || !tap->dotted_name) {
398 LOG_ERROR("Out of memory");
399 return ERROR_FAIL;
401 CMD_ARGC -= 2;
402 CMD_ARGV += 2;
404 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
405 tap->chip, tap->tapname, tap->dotted_name, CMD_ARGC);
408 * IEEE specifies that the two LSBs of an IR scan are 01, so make
409 * that the default. The "-ircapture" and "-irmask" options are only
410 * needed to cope with nonstandard TAPs, or to specify more bits.
412 tap->ir_capture_mask = 0x03;
413 tap->ir_capture_value = 0x01;
415 while (CMD_ARGC) {
416 const struct nvp *n = nvp_name2value(jtag_newtap_opts, CMD_ARGV[0]);
417 CMD_ARGC--;
418 CMD_ARGV++;
419 switch (n->value) {
420 case NTAP_OPT_ENABLED:
421 tap->disabled_after_reset = false;
422 break;
424 case NTAP_OPT_DISABLED:
425 tap->disabled_after_reset = true;
426 break;
428 case NTAP_OPT_EXPECTED_ID:
429 if (!CMD_ARGC)
430 return ERROR_COMMAND_ARGUMENT_INVALID;
432 tap->expected_ids = realloc(tap->expected_ids,
433 (tap->expected_ids_cnt + 1) * sizeof(uint32_t));
434 if (!tap->expected_ids) {
435 LOG_ERROR("Out of memory");
436 return ERROR_FAIL;
439 uint32_t id;
440 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], id);
441 CMD_ARGC--;
442 CMD_ARGV++;
443 tap->expected_ids[tap->expected_ids_cnt++] = id;
445 break;
447 case NTAP_OPT_IRLEN:
448 if (!CMD_ARGC)
449 return ERROR_COMMAND_ARGUMENT_INVALID;
451 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], tap->ir_length);
452 CMD_ARGC--;
453 CMD_ARGV++;
454 if (tap->ir_length > (8 * sizeof(tap->ir_capture_value)))
455 LOG_WARNING("%s: huge IR length %u", tap->dotted_name, tap->ir_length);
456 break;
458 case NTAP_OPT_IRMASK:
459 if (!CMD_ARGC)
460 return ERROR_COMMAND_ARGUMENT_INVALID;
462 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], tap->ir_capture_mask);
463 CMD_ARGC--;
464 CMD_ARGV++;
465 if ((tap->ir_capture_mask & 3) != 3)
466 LOG_WARNING("%s: nonstandard IR mask", tap->dotted_name);
467 break;
469 case NTAP_OPT_IRCAPTURE:
470 if (!CMD_ARGC)
471 return ERROR_COMMAND_ARGUMENT_INVALID;
473 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], tap->ir_capture_value);
474 CMD_ARGC--;
475 CMD_ARGV++;
476 if ((tap->ir_capture_value & 3) != 1)
477 LOG_WARNING("%s: nonstandard IR value", tap->dotted_name);
478 break;
480 case NTAP_OPT_VERSION:
481 tap->ignore_version = true;
482 break;
484 case NTAP_OPT_BYPASS:
485 tap->ignore_bypass = true;
486 break;
488 case NTAP_OPT_IRBYPASS:
489 if (!CMD_ARGC)
490 return ERROR_COMMAND_ARGUMENT_INVALID;
492 COMMAND_PARSE_NUMBER(u64, CMD_ARGV[0], tap->ir_bypass_value);
493 CMD_ARGC--;
494 CMD_ARGV++;
495 break;
497 default:
498 nvp_unknown_command_print(CMD, jtag_newtap_opts, NULL, CMD_ARGV[-1]);
499 return ERROR_COMMAND_ARGUMENT_INVALID;
503 /* default is enabled-after-reset */
504 tap->enabled = !tap->disabled_after_reset;
506 if (transport_is_jtag() && tap->ir_length == 0) {
507 command_print(CMD, "newtap: %s missing IR length", tap->dotted_name);
508 return ERROR_COMMAND_ARGUMENT_INVALID;
511 return ERROR_OK;
514 __COMMAND_HANDLER(handle_jtag_newtap)
516 struct jtag_tap *tap = calloc(1, sizeof(struct jtag_tap));
517 if (!tap) {
518 LOG_ERROR("Out of memory");
519 return ERROR_FAIL;
522 int retval = CALL_COMMAND_HANDLER(handle_jtag_newtap_args, tap);
523 if (retval != ERROR_OK) {
524 free(tap->chip);
525 free(tap->tapname);
526 free(tap->dotted_name);
527 free(tap->expected_ids);
528 free(tap);
529 return retval;
532 jtag_tap_init(tap);
533 return ERROR_OK;
536 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
538 struct jtag_tap_event_action *jteap;
539 int retval;
541 for (jteap = tap->event_action; jteap; jteap = jteap->next) {
542 if (jteap->event != e)
543 continue;
545 const struct nvp *nvp = nvp_value2name(nvp_jtag_tap_event, e);
546 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
547 tap->dotted_name, e, nvp->name,
548 Jim_GetString(jteap->body, NULL));
550 retval = Jim_EvalObj(jteap->interp, jteap->body);
551 if (retval == JIM_RETURN)
552 retval = jteap->interp->returnCode;
554 if (retval != JIM_OK) {
555 Jim_MakeErrorMessage(jteap->interp);
556 LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
557 continue;
560 switch (e) {
561 case JTAG_TAP_EVENT_ENABLE:
562 case JTAG_TAP_EVENT_DISABLE:
563 /* NOTE: we currently assume the handlers
564 * can't fail. Right here is where we should
565 * really be verifying the scan chains ...
567 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
568 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
569 tap->enabled ? "enabled" : "disabled");
570 break;
571 default:
572 break;
577 COMMAND_HANDLER(handle_jtag_arp_init)
579 if (CMD_ARGC != 0)
580 return ERROR_COMMAND_SYNTAX_ERROR;
582 return jtag_init_inner(CMD_CTX);
585 COMMAND_HANDLER(handle_jtag_arp_init_reset)
587 if (CMD_ARGC != 0)
588 return ERROR_COMMAND_SYNTAX_ERROR;
590 if (transport_is_jtag())
591 return jtag_init_reset(CMD_CTX);
593 if (transport_is_swd())
594 return swd_init_reset(CMD_CTX);
596 return ERROR_OK;
599 static bool jtag_tap_enable(struct jtag_tap *t)
601 if (t->enabled)
602 return true;
603 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
604 if (!t->enabled)
605 return false;
607 /* FIXME add JTAG sanity checks, w/o TLR
608 * - scan chain length grew by one (this)
609 * - IDs and IR lengths are as expected
611 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
612 return true;
614 static bool jtag_tap_disable(struct jtag_tap *t)
616 if (!t->enabled)
617 return true;
618 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
619 if (t->enabled)
620 return false;
622 /* FIXME add JTAG sanity checks, w/o TLR
623 * - scan chain length shrank by one (this)
624 * - IDs and IR lengths are as expected
626 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
627 return true;
630 __COMMAND_HANDLER(handle_jtag_tap_enabler)
632 if (CMD_ARGC != 1)
633 return ERROR_COMMAND_SYNTAX_ERROR;
635 struct jtag_tap *t = jtag_tap_by_string(CMD_ARGV[0]);
636 if (!t) {
637 command_print(CMD, "Tap '%s' could not be found", CMD_ARGV[0]);
638 return ERROR_COMMAND_ARGUMENT_INVALID;
641 if (strcmp(CMD_NAME, "tapisenabled") == 0) {
642 /* do nothing, just return the value */
643 } else if (strcmp(CMD_NAME, "tapenable") == 0) {
644 if (!jtag_tap_enable(t)) {
645 command_print(CMD, "failed to enable tap %s", t->dotted_name);
646 return ERROR_FAIL;
648 } else if (strcmp(CMD_NAME, "tapdisable") == 0) {
649 if (!jtag_tap_disable(t)) {
650 command_print(CMD, "failed to disable tap %s", t->dotted_name);
651 return ERROR_FAIL;
653 } else {
654 command_print(CMD, "command '%s' unknown", CMD_NAME);
655 return ERROR_FAIL;
658 command_print(CMD, "%d", t->enabled ? 1 : 0);
659 return ERROR_OK;
662 COMMAND_HANDLER(handle_jtag_names)
664 if (CMD_ARGC != 0)
665 return ERROR_COMMAND_SYNTAX_ERROR;
667 for (struct jtag_tap *tap = jtag_all_taps(); tap; tap = tap->next_tap)
668 command_print(CMD, "%s", tap->dotted_name);
670 return ERROR_OK;
673 COMMAND_HANDLER(handle_jtag_init_command)
675 if (CMD_ARGC != 0)
676 return ERROR_COMMAND_SYNTAX_ERROR;
678 static bool jtag_initialized;
679 if (jtag_initialized) {
680 LOG_INFO("'jtag init' has already been called");
681 return ERROR_OK;
683 jtag_initialized = true;
685 LOG_DEBUG("Initializing jtag devices...");
686 return jtag_init(CMD_CTX);
689 static const struct command_registration jtag_subcommand_handlers[] = {
691 .name = "init",
692 .mode = COMMAND_ANY,
693 .handler = handle_jtag_init_command,
694 .help = "initialize jtag scan chain",
695 .usage = ""
698 .name = "arp_init",
699 .mode = COMMAND_ANY,
700 .handler = handle_jtag_arp_init,
701 .help = "Validates JTAG scan chain against the list of "
702 "declared TAPs using just the four standard JTAG "
703 "signals.",
704 .usage = "",
707 .name = "arp_init-reset",
708 .mode = COMMAND_ANY,
709 .handler = handle_jtag_arp_init_reset,
710 .help = "Uses TRST and SRST to try resetting everything on "
711 "the JTAG scan chain, then performs 'jtag arp_init'.",
712 .usage = "",
715 .name = "newtap",
716 .mode = COMMAND_CONFIG,
717 .handler = handle_jtag_newtap,
718 .help = "Create a new TAP instance named basename.tap_type, "
719 "and appends it to the scan chain.",
720 .usage = "basename tap_type '-irlen' count "
721 "['-enable'|'-disable'] "
722 "['-expected_id' number] "
723 "['-ignore-version'] "
724 "['-ignore-bypass'] "
725 "['-ircapture' number] "
726 "['-ir-bypass' number] "
727 "['-mask' number]",
730 .name = "tapisenabled",
731 .mode = COMMAND_EXEC,
732 .handler = handle_jtag_tap_enabler,
733 .help = "Returns a Tcl boolean (0/1) indicating whether "
734 "the TAP is enabled (1) or not (0).",
735 .usage = "tap_name",
738 .name = "tapenable",
739 .mode = COMMAND_EXEC,
740 .handler = handle_jtag_tap_enabler,
741 .help = "Try to enable the specified TAP using the "
742 "'tap-enable' TAP event.",
743 .usage = "tap_name",
746 .name = "tapdisable",
747 .mode = COMMAND_EXEC,
748 .handler = handle_jtag_tap_enabler,
749 .help = "Try to disable the specified TAP using the "
750 "'tap-disable' TAP event.",
751 .usage = "tap_name",
754 .name = "configure",
755 .mode = COMMAND_ANY,
756 .handler = handle_jtag_configure,
757 .help = "Provide a Tcl handler for the specified "
758 "TAP event.",
759 .usage = "tap_name '-event' event_name handler",
762 .name = "cget",
763 .mode = COMMAND_EXEC,
764 .handler = handle_jtag_configure,
765 .help = "Return any Tcl handler for the specified "
766 "TAP event or the value of the IDCODE found in hardware.",
767 .usage = "tap_name '-event' event_name | "
768 "tap_name '-idcode'",
771 .name = "names",
772 .mode = COMMAND_ANY,
773 .handler = handle_jtag_names,
774 .help = "Returns list of all JTAG tap names.",
775 .usage = "",
778 .chain = jtag_command_handlers_to_move,
780 COMMAND_REGISTRATION_DONE
783 void jtag_notify_event(enum jtag_event event)
785 struct jtag_tap *tap;
787 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
788 jtag_tap_handle_event(tap, event);
792 COMMAND_HANDLER(handle_scan_chain_command)
794 struct jtag_tap *tap;
795 char expected_id[12];
797 tap = jtag_all_taps();
798 command_print(CMD,
799 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
800 command_print(CMD,
801 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
803 while (tap) {
804 uint32_t expected, expected_mask, ii;
806 snprintf(expected_id, sizeof(expected_id), "0x%08" PRIx32,
807 (tap->expected_ids_cnt > 0) ? tap->expected_ids[0] : 0);
808 if (tap->ignore_version)
809 expected_id[2] = '*';
811 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
812 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
814 command_print(CMD,
815 "%2u %-18s %c 0x%08x %s %5u 0x%02x 0x%02x",
816 tap->abs_chain_position,
817 tap->dotted_name,
818 tap->enabled ? 'Y' : 'n',
819 (unsigned int)(tap->idcode),
820 expected_id,
821 tap->ir_length,
822 (unsigned int)(expected),
823 (unsigned int)(expected_mask));
825 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
826 snprintf(expected_id, sizeof(expected_id), "0x%08" PRIx32, tap->expected_ids[ii]);
827 if (tap->ignore_version)
828 expected_id[2] = '*';
830 command_print(CMD,
831 " %s",
832 expected_id);
835 tap = tap->next_tap;
838 return ERROR_OK;
841 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
843 if (CMD_ARGC > 1)
844 return ERROR_COMMAND_SYNTAX_ERROR;
845 if (CMD_ARGC == 1) {
846 unsigned int delay;
847 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
849 jtag_set_ntrst_delay(delay);
851 command_print(CMD, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
852 return ERROR_OK;
855 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
857 if (CMD_ARGC > 1)
858 return ERROR_COMMAND_SYNTAX_ERROR;
859 if (CMD_ARGC == 1) {
860 unsigned int delay;
861 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
863 jtag_set_ntrst_assert_width(delay);
865 command_print(CMD, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
866 return ERROR_OK;
869 COMMAND_HANDLER(handle_jtag_rclk_command)
871 if (CMD_ARGC > 1)
872 return ERROR_COMMAND_SYNTAX_ERROR;
874 int retval = ERROR_OK;
875 if (CMD_ARGC == 1) {
876 unsigned int khz = 0;
877 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
879 retval = adapter_config_rclk(khz);
880 if (retval != ERROR_OK)
881 return retval;
884 int cur_khz = adapter_get_speed_khz();
885 retval = adapter_get_speed_readable(&cur_khz);
886 if (retval != ERROR_OK)
887 return retval;
889 if (cur_khz)
890 command_print(CMD, "RCLK not supported - fallback to %d kHz", cur_khz);
891 else
892 command_print(CMD, "RCLK - adaptive");
894 return retval;
897 COMMAND_HANDLER(handle_runtest_command)
899 if (CMD_ARGC != 1)
900 return ERROR_COMMAND_SYNTAX_ERROR;
902 unsigned int num_clocks;
903 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
905 jtag_add_runtest(num_clocks, TAP_IDLE);
906 return jtag_execute_queue();
910 * For "irscan" or "drscan" commands, the "end" (really, "next") state
911 * should be stable ... and *NOT* a shift state, otherwise free-running
912 * jtag clocks could change the values latched by the update state.
913 * Not surprisingly, this is the same constraint as SVF; the "irscan"
914 * and "drscan" commands are a write-only subset of what SVF provides.
917 COMMAND_HANDLER(handle_irscan_command)
919 int i;
920 struct scan_field *fields;
921 struct jtag_tap *tap = NULL;
922 tap_state_t endstate;
924 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
925 return ERROR_COMMAND_SYNTAX_ERROR;
927 /* optional "-endstate" "statename" at the end of the arguments,
928 * so that e.g. IRPAUSE can let us load the data register before
929 * entering RUN/IDLE to execute the instruction we load here.
931 endstate = TAP_IDLE;
933 if (CMD_ARGC >= 4) {
934 /* have at least one pair of numbers.
935 * is last pair the magic text? */
936 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
937 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
938 if (endstate == TAP_INVALID)
939 return ERROR_COMMAND_SYNTAX_ERROR;
940 if (!scan_is_safe(endstate))
941 LOG_WARNING("unstable irscan endstate \"%s\"",
942 CMD_ARGV[CMD_ARGC - 1]);
943 CMD_ARGC -= 2;
947 int num_fields = CMD_ARGC / 2;
948 if (num_fields > 1) {
949 /* we really should be looking at plain_ir_scan if we want
950 * anything more fancy.
952 LOG_ERROR("Specify a single value for tap");
953 return ERROR_COMMAND_SYNTAX_ERROR;
956 fields = calloc(num_fields, sizeof(*fields));
958 int retval;
959 for (i = 0; i < num_fields; i++) {
960 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
961 if (!tap) {
962 free(fields);
963 command_print(CMD, "Tap: %s unknown", CMD_ARGV[i*2]);
965 return ERROR_FAIL;
967 uint64_t value;
968 retval = parse_u64(CMD_ARGV[i * 2 + 1], &value);
969 if (retval != ERROR_OK)
970 goto error_return;
972 unsigned int field_size = tap->ir_length;
973 fields[i].num_bits = field_size;
974 uint8_t *v = calloc(1, DIV_ROUND_UP(field_size, 8));
975 if (!v) {
976 LOG_ERROR("Out of memory");
977 goto error_return;
980 buf_set_u64(v, 0, field_size, value);
981 fields[i].out_value = v;
982 fields[i].in_value = NULL;
985 /* did we have an endstate? */
986 jtag_add_ir_scan(tap, fields, endstate);
988 retval = jtag_execute_queue();
990 error_return:
991 for (i = 0; i < num_fields; i++)
992 free((void *)fields[i].out_value);
994 free(fields);
996 return retval;
999 COMMAND_HANDLER(handle_verify_ircapture_command)
1001 if (CMD_ARGC > 1)
1002 return ERROR_COMMAND_SYNTAX_ERROR;
1004 if (CMD_ARGC == 1) {
1005 bool enable;
1006 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1007 jtag_set_verify_capture_ir(enable);
1010 const char *status = jtag_will_verify_capture_ir() ? "enabled" : "disabled";
1011 command_print(CMD, "verify Capture-IR is %s", status);
1013 return ERROR_OK;
1016 COMMAND_HANDLER(handle_verify_jtag_command)
1018 if (CMD_ARGC > 1)
1019 return ERROR_COMMAND_SYNTAX_ERROR;
1021 if (CMD_ARGC == 1) {
1022 bool enable;
1023 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1024 jtag_set_verify(enable);
1027 const char *status = jtag_will_verify() ? "enabled" : "disabled";
1028 command_print(CMD, "verify jtag capture is %s", status);
1030 return ERROR_OK;
1033 COMMAND_HANDLER(handle_tms_sequence_command)
1035 if (CMD_ARGC > 1)
1036 return ERROR_COMMAND_SYNTAX_ERROR;
1038 if (CMD_ARGC == 1) {
1039 bool use_new_table;
1040 if (strcmp(CMD_ARGV[0], "short") == 0)
1041 use_new_table = true;
1042 else if (strcmp(CMD_ARGV[0], "long") == 0)
1043 use_new_table = false;
1044 else
1045 return ERROR_COMMAND_SYNTAX_ERROR;
1047 tap_use_new_tms_table(use_new_table);
1050 command_print(CMD, "tms sequence is %s",
1051 tap_uses_new_tms_table() ? "short" : "long");
1053 return ERROR_OK;
1056 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1058 if (CMD_ARGC != 1)
1059 return ERROR_COMMAND_SYNTAX_ERROR;
1061 int sleep_ms;
1062 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1064 jtag_set_flush_queue_sleep(sleep_ms);
1066 return ERROR_OK;
1069 COMMAND_HANDLER(handle_wait_srst_deassert)
1071 if (CMD_ARGC != 1)
1072 return ERROR_COMMAND_SYNTAX_ERROR;
1074 int timeout_ms;
1075 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1076 if ((timeout_ms <= 0) || (timeout_ms > 100000)) {
1077 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1078 return ERROR_FAIL;
1081 LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1082 int asserted_yet;
1083 int64_t then = timeval_ms();
1084 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1085 if ((timeval_ms() - then) > timeout_ms) {
1086 LOG_ERROR("Timed out");
1087 return ERROR_FAIL;
1089 if (asserted_yet)
1090 break;
1092 while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1093 if ((timeval_ms() - then) > timeout_ms) {
1094 LOG_ERROR("Timed out");
1095 return ERROR_FAIL;
1097 if (!asserted_yet)
1098 break;
1101 return ERROR_OK;
1104 static const struct command_registration jtag_command_handlers[] = {
1107 .name = "jtag_flush_queue_sleep",
1108 .handler = handle_jtag_flush_queue_sleep,
1109 .mode = COMMAND_ANY,
1110 .help = "For debug purposes(simulate long delays of interface) "
1111 "to test performance or change in behavior. Default 0ms.",
1112 .usage = "[sleep in ms]",
1115 .name = "jtag_rclk",
1116 .handler = handle_jtag_rclk_command,
1117 .mode = COMMAND_ANY,
1118 .help = "With an argument, change to to use adaptive clocking "
1119 "if possible; else to use the fallback speed. "
1120 "With or without argument, display current setting.",
1121 .usage = "[fallback_speed_khz]",
1124 .name = "jtag_ntrst_delay",
1125 .handler = handle_jtag_ntrst_delay_command,
1126 .mode = COMMAND_ANY,
1127 .help = "delay after deasserting trst in ms",
1128 .usage = "[milliseconds]",
1131 .name = "jtag_ntrst_assert_width",
1132 .handler = handle_jtag_ntrst_assert_width_command,
1133 .mode = COMMAND_ANY,
1134 .help = "delay after asserting trst in ms",
1135 .usage = "[milliseconds]",
1138 .name = "scan_chain",
1139 .handler = handle_scan_chain_command,
1140 .mode = COMMAND_ANY,
1141 .help = "print current scan chain configuration",
1142 .usage = ""
1145 .name = "runtest",
1146 .handler = handle_runtest_command,
1147 .mode = COMMAND_EXEC,
1148 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1149 .usage = "num_cycles"
1152 .name = "irscan",
1153 .handler = handle_irscan_command,
1154 .mode = COMMAND_EXEC,
1155 .help = "Execute Instruction Register (IR) scan. The "
1156 "specified opcodes are put into each TAP's IR, "
1157 "and other TAPs are put in BYPASS.",
1158 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1161 .name = "verify_ircapture",
1162 .handler = handle_verify_ircapture_command,
1163 .mode = COMMAND_ANY,
1164 .help = "Display or assign flag controlling whether to "
1165 "verify values captured during Capture-IR.",
1166 .usage = "['enable'|'disable']",
1169 .name = "verify_jtag",
1170 .handler = handle_verify_jtag_command,
1171 .mode = COMMAND_ANY,
1172 .help = "Display or assign flag controlling whether to "
1173 "verify values captured during IR and DR scans.",
1174 .usage = "['enable'|'disable']",
1177 .name = "tms_sequence",
1178 .handler = handle_tms_sequence_command,
1179 .mode = COMMAND_ANY,
1180 .help = "Display or change what style TMS sequences to use "
1181 "for JTAG state transitions: short (default) or "
1182 "long. Only for working around JTAG bugs.",
1183 /* Specifically for working around DRIVER bugs... */
1184 .usage = "['short'|'long']",
1187 .name = "wait_srst_deassert",
1188 .handler = handle_wait_srst_deassert,
1189 .mode = COMMAND_ANY,
1190 .help = "Wait for an SRST deassert. "
1191 "Useful for cases where you need something to happen within ms "
1192 "of an srst deassert. Timeout in ms",
1193 .usage = "ms",
1196 .name = "jtag",
1197 .mode = COMMAND_ANY,
1198 .help = "perform jtag tap actions",
1199 .usage = "",
1201 .chain = jtag_subcommand_handlers,
1204 .chain = jtag_command_handlers_to_move,
1206 COMMAND_REGISTRATION_DONE
1209 int jtag_register_commands(struct command_context *cmd_ctx)
1211 return register_commands(cmd_ctx, NULL, jtag_command_handlers);