improve jtag_tap_configure
[openocd.git] / src / jtag / tcl.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 √ė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
33
34 #include "jtag.h"
35 #include "minidriver.h"
36 #include "interface.h"
37 #include "interfaces.h"
38
39 #ifdef HAVE_STRINGS_H
40 #include <strings.h>
41 #endif
42
43 static const Jim_Nvp nvp_jtag_tap_event[] = {
44 { .value = JTAG_TRST_ASSERTED, .name = "post-reset" },
45 { .value = JTAG_TAP_EVENT_SETUP, .name = "setup" },
46 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
47 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
48
49 { .name = NULL, .value = -1 }
50 };
51
52 extern struct jtag_interface *jtag_interface;
53
54 static bool scan_is_safe(tap_state_t state)
55 {
56 switch (state)
57 {
58 case TAP_RESET:
59 case TAP_IDLE:
60 case TAP_DRPAUSE:
61 case TAP_IRPAUSE:
62 return true;
63 default:
64 return false;
65 }
66 }
67
68 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
69 {
70 int retval;
71 struct scan_field *fields;
72 int num_fields;
73 int field_count = 0;
74 int i, e;
75 struct jtag_tap *tap;
76 tap_state_t endstate;
77
78 /* args[1] = device
79 * args[2] = num_bits
80 * args[3] = hex string
81 * ... repeat num bits and hex string ...
82 *
83 * .. optionally:
84 * args[N-2] = "-endstate"
85 * args[N-1] = statename
86 */
87 if ((argc < 4) || ((argc % 2) != 0))
88 {
89 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
90 return JIM_ERR;
91 }
92
93 endstate = TAP_IDLE;
94
95 script_debug(interp, "drscan", argc, args);
96
97 /* validate arguments as numbers */
98 e = JIM_OK;
99 for (i = 2; i < argc; i += 2)
100 {
101 long bits;
102 const char *cp;
103
104 e = Jim_GetLong(interp, args[i], &bits);
105 /* If valid - try next arg */
106 if (e == JIM_OK) {
107 continue;
108 }
109
110 /* Not valid.. are we at the end? */
111 if (((i + 2) != argc)) {
112 /* nope, then error */
113 return e;
114 }
115
116 /* it could be: "-endstate FOO"
117 * e.g. DRPAUSE so we can issue more instructions
118 * before entering RUN/IDLE and executing them.
119 */
120
121 /* get arg as a string. */
122 cp = Jim_GetString(args[i], NULL);
123 /* is it the magic? */
124 if (0 == strcmp("-endstate", cp)) {
125 /* is the statename valid? */
126 cp = Jim_GetString(args[i + 1], NULL);
127
128 /* see if it is a valid state name */
129 endstate = tap_state_by_name(cp);
130 if (endstate < 0) {
131 /* update the error message */
132 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
133 } else {
134 if (!scan_is_safe(endstate))
135 LOG_WARNING("drscan with unsafe "
136 "endstate \"%s\"", cp);
137
138 /* valid - so clear the error */
139 e = JIM_OK;
140 /* and remove the last 2 args */
141 argc -= 2;
142 }
143 }
144
145 /* Still an error? */
146 if (e != JIM_OK) {
147 return e; /* too bad */
148 }
149 } /* validate args */
150
151 tap = jtag_tap_by_jim_obj(interp, args[1]);
152 if (tap == NULL) {
153 return JIM_ERR;
154 }
155
156 num_fields = (argc-2)/2;
157 fields = malloc(sizeof(struct scan_field) * num_fields);
158 for (i = 2; i < argc; i += 2)
159 {
160 long bits;
161 int len;
162 const char *str;
163
164 Jim_GetLong(interp, args[i], &bits);
165 str = Jim_GetString(args[i + 1], &len);
166
167 fields[field_count].tap = tap;
168 fields[field_count].num_bits = bits;
169 fields[field_count].out_value = malloc(DIV_ROUND_UP(bits, 8));
170 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
171 fields[field_count].in_value = fields[field_count].out_value;
172 field_count++;
173 }
174
175 jtag_add_dr_scan(num_fields, fields, endstate);
176
177 retval = jtag_execute_queue();
178 if (retval != ERROR_OK)
179 {
180 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
181 return JIM_ERR;
182 }
183
184 field_count = 0;
185 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
186 for (i = 2; i < argc; i += 2)
187 {
188 long bits;
189 char *str;
190
191 Jim_GetLong(interp, args[i], &bits);
192 str = buf_to_str(fields[field_count].in_value, bits, 16);
193 free(fields[field_count].out_value);
194
195 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
196 free(str);
197 field_count++;
198 }
199
200 Jim_SetResult(interp, list);
201
202 free(fields);
203
204 return JIM_OK;
205 }
206
207
208 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
209 {
210 tap_state_t states[8];
211
212 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
213 {
214 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
215 return JIM_ERR;
216 }
217
218 script_debug(interp, "pathmove", argc, args);
219
220 int i;
221 for (i = 0; i < argc-1; i++)
222 {
223 const char *cp;
224 cp = Jim_GetString(args[i + 1], NULL);
225 states[i] = tap_state_by_name(cp);
226 if (states[i] < 0)
227 {
228 /* update the error message */
229 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
230 return JIM_ERR;
231 }
232 }
233
234 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
235 {
236 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
237 return JIM_ERR;
238 }
239
240 jtag_add_pathmove(argc-2, states + 1);
241
242 if (jtag_execute_queue()!= ERROR_OK)
243 {
244 Jim_SetResultString(interp, "pathmove: failed",-1);
245 return JIM_ERR;
246 }
247
248 return JIM_OK;
249 }
250
251
252 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
253 {
254 script_debug(interp, "flush_count", argc, args);
255
256 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
257
258 return JIM_OK;
259 }
260
261 static const struct command_registration jtag_command_handlers_to_move[] = {
262 {
263 .name = "drscan",
264 .mode = COMMAND_EXEC,
265 .jim_handler = &Jim_Command_drscan,
266 .help = "execute DR scan <device> "
267 "<num_bits> <value> <num_bits1> <value2> ...",
268 },
269 {
270 .name = "flush_count",
271 .mode = COMMAND_EXEC,
272 .jim_handler = &Jim_Command_flush_count,
273 .help = "returns number of times the JTAG queue has been flushed",
274 },
275 {
276 .name = "pathmove",
277 .mode = COMMAND_EXEC,
278 .jim_handler = &Jim_Command_pathmove,
279 .usage = "<state1>,<state2>,<state3>... ",
280 .help = "move JTAG to state1 then to state2, state3, etc.",
281 },
282 COMMAND_REGISTRATION_DONE
283 };
284
285
286 enum jtag_tap_cfg_param {
287 JCFG_EVENT
288 };
289
290 static Jim_Nvp nvp_config_opts[] = {
291 { .name = "-event", .value = JCFG_EVENT },
292
293 { .name = NULL, .value = -1 }
294 };
295
296 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
297 {
298 if (goi->argc == 0)
299 {
300 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
301 return JIM_ERR;
302 }
303
304 Jim_Nvp *n;
305 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
306 if (e != JIM_OK)
307 {
308 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
309 return e;
310 }
311
312 if (goi->isconfigure) {
313 if (goi->argc != 1) {
314 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
315 return JIM_ERR;
316 }
317 } else {
318 if (goi->argc != 0) {
319 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
320 return JIM_ERR;
321 }
322 }
323
324 struct jtag_tap_event_action *jteap = tap->event_action;
325 /* replace existing event body */
326 bool found = false;
327 while (jteap)
328 {
329 if (jteap->event == (enum jtag_event)n->value)
330 {
331 found = true;
332 break;
333 }
334 jteap = jteap->next;
335 }
336
337 Jim_SetEmptyResult(goi->interp);
338
339 if (goi->isconfigure)
340 {
341 if (!found)
342 jteap = calloc(1, sizeof(*jteap));
343 else if (NULL != jteap->body)
344 Jim_DecrRefCount(interp, jteap->body);
345
346 jteap->event = n->value;
347
348 Jim_Obj *o;
349 Jim_GetOpt_Obj(goi, &o);
350 jteap->body = Jim_DuplicateObj(goi->interp, o);
351 Jim_IncrRefCount(jteap->body);
352
353 if (!found)
354 {
355 /* add to head of event list */
356 jteap->next = tap->event_action;
357 tap->event_action = jteap;
358 }
359 }
360 else if (found)
361 {
362 Jim_SetResult(goi->interp,
363 Jim_DuplicateObj(goi->interp, jteap->body));
364 }
365 return JIM_OK;
366 }
367
368 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
369 {
370 /* parse config or cget options */
371 while (goi->argc > 0)
372 {
373 Jim_SetEmptyResult (goi->interp);
374
375 Jim_Nvp *n;
376 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
377 if (e != JIM_OK)
378 {
379 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
380 return e;
381 }
382
383 switch (n->value)
384 {
385 case JCFG_EVENT:
386 e = jtag_tap_configure_event(goi, tap);
387 if (e != JIM_OK)
388 return e;
389 break;
390 default:
391 Jim_SetResult_sprintf(goi->interp, "unknown event: %s", n->name);
392 return JIM_ERR;
393 }
394 }
395
396 return JIM_OK;
397 }
398
399 static int is_bad_irval(int ir_length, jim_wide w)
400 {
401 jim_wide v = 1;
402
403 v <<= ir_length;
404 v -= 1;
405 v = ~v;
406 return (w & v) != 0;
407 }
408
409 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
410 {
411 struct jtag_tap *pTap;
412 jim_wide w;
413 int x;
414 int e;
415 Jim_Nvp *n;
416 char *cp;
417 const Jim_Nvp opts[] = {
418 #define NTAP_OPT_IRLEN 0
419 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
420 #define NTAP_OPT_IRMASK 1
421 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
422 #define NTAP_OPT_IRCAPTURE 2
423 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
424 #define NTAP_OPT_ENABLED 3
425 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
426 #define NTAP_OPT_DISABLED 4
427 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
428 #define NTAP_OPT_EXPECTED_ID 5
429 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
430 { .name = NULL , .value = -1 },
431 };
432
433 pTap = calloc(1, sizeof(struct jtag_tap));
434 if (!pTap) {
435 Jim_SetResult_sprintf(goi->interp, "no memory");
436 return JIM_ERR;
437 }
438
439 /*
440 * we expect CHIP + TAP + OPTIONS
441 * */
442 if (goi->argc < 3) {
443 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
444 free(pTap);
445 return JIM_ERR;
446 }
447 Jim_GetOpt_String(goi, &cp, NULL);
448 pTap->chip = strdup(cp);
449
450 Jim_GetOpt_String(goi, &cp, NULL);
451 pTap->tapname = strdup(cp);
452
453 /* name + dot + name + null */
454 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
455 cp = malloc(x);
456 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
457 pTap->dotted_name = cp;
458
459 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
460 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
461
462 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
463 * that the default. The "-irlen" and "-irmask" options are only
464 * needed to cope with nonstandard TAPs, or to specify more bits.
465 */
466 pTap->ir_capture_mask = 0x03;
467 pTap->ir_capture_value = 0x01;
468
469 while (goi->argc) {
470 e = Jim_GetOpt_Nvp(goi, opts, &n);
471 if (e != JIM_OK) {
472 Jim_GetOpt_NvpUnknown(goi, opts, 0);
473 free((void *)pTap->dotted_name);
474 free(pTap);
475 return e;
476 }
477 LOG_DEBUG("Processing option: %s", n->name);
478 switch (n->value) {
479 case NTAP_OPT_ENABLED:
480 pTap->disabled_after_reset = false;
481 break;
482 case NTAP_OPT_DISABLED:
483 pTap->disabled_after_reset = true;
484 break;
485 case NTAP_OPT_EXPECTED_ID:
486 {
487 uint32_t *new_expected_ids;
488
489 e = Jim_GetOpt_Wide(goi, &w);
490 if (e != JIM_OK) {
491 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
492 free((void *)pTap->dotted_name);
493 free(pTap);
494 return e;
495 }
496
497 new_expected_ids = malloc(sizeof(uint32_t) * (pTap->expected_ids_cnt + 1));
498 if (new_expected_ids == NULL) {
499 Jim_SetResult_sprintf(goi->interp, "no memory");
500 free((void *)pTap->dotted_name);
501 free(pTap);
502 return JIM_ERR;
503 }
504
505 memcpy(new_expected_ids, pTap->expected_ids, sizeof(uint32_t) * pTap->expected_ids_cnt);
506
507 new_expected_ids[pTap->expected_ids_cnt] = w;
508
509 free(pTap->expected_ids);
510 pTap->expected_ids = new_expected_ids;
511 pTap->expected_ids_cnt++;
512 break;
513 }
514 case NTAP_OPT_IRLEN:
515 case NTAP_OPT_IRMASK:
516 case NTAP_OPT_IRCAPTURE:
517 e = Jim_GetOpt_Wide(goi, &w);
518 if (e != JIM_OK) {
519 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
520 free((void *)pTap->dotted_name);
521 free(pTap);
522 return e;
523 }
524 switch (n->value) {
525 case NTAP_OPT_IRLEN:
526 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
527 LOG_WARNING("%s: huge IR length %d",
528 pTap->dotted_name,
529 (int) w);
530 pTap->ir_length = w;
531 break;
532 case NTAP_OPT_IRMASK:
533 if (is_bad_irval(pTap->ir_length, w)) {
534 LOG_ERROR("%s: IR mask %x too big",
535 pTap->dotted_name,
536 (int) w);
537 free((void *)pTap->dotted_name);
538 free(pTap);
539 return ERROR_FAIL;
540 }
541 if ((w & 3) != 3)
542 LOG_WARNING("%s: nonstandard IR mask",
543 pTap->dotted_name);
544 pTap->ir_capture_mask = w;
545 break;
546 case NTAP_OPT_IRCAPTURE:
547 if (is_bad_irval(pTap->ir_length, w)) {
548 LOG_ERROR("%s: IR capture %x too big",
549 pTap->dotted_name,
550 (int) w);
551 free((void *)pTap->dotted_name);
552 free(pTap);
553 return ERROR_FAIL;
554 }
555 if ((w & 3) != 1)
556 LOG_WARNING("%s: nonstandard IR value",
557 pTap->dotted_name);
558 pTap->ir_capture_value = w;
559 break;
560 }
561 } /* switch (n->value) */
562 } /* while (goi->argc) */
563
564 /* default is enabled-after-reset */
565 pTap->enabled = !pTap->disabled_after_reset;
566
567 /* Did all the required option bits get cleared? */
568 if (pTap->ir_length != 0)
569 {
570 jtag_tap_init(pTap);
571 return ERROR_OK;
572 }
573
574 Jim_SetResult_sprintf(goi->interp,
575 "newtap: %s missing IR length",
576 pTap->dotted_name);
577 jtag_tap_free(pTap);
578 return JIM_ERR;
579 }
580
581 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
582 {
583 struct jtag_tap_event_action * jteap;
584
585 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
586 {
587 if (jteap->event != e)
588 continue;
589
590 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
591 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
592 tap->dotted_name, e, nvp->name,
593 Jim_GetString(jteap->body, NULL));
594
595 if (Jim_EvalObj(interp, jteap->body) != JIM_OK)
596 {
597 Jim_PrintErrorMessage(interp);
598 continue;
599 }
600
601 switch (e)
602 {
603 case JTAG_TAP_EVENT_ENABLE:
604 case JTAG_TAP_EVENT_DISABLE:
605 /* NOTE: we currently assume the handlers
606 * can't fail. Right here is where we should
607 * really be verifying the scan chains ...
608 */
609 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
610 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
611 tap->enabled ? "enabled" : "disabled");
612 break;
613 default:
614 break;
615 }
616 }
617 }
618
619 static int jim_jtag_interface(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
620 {
621 Jim_GetOptInfo goi;
622 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
623
624 /* return the name of the interface */
625 /* TCL code might need to know the exact type... */
626 /* FUTURE: we allow this as a means to "set" the interface. */
627 if (goi.argc != 0) {
628 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
629 return JIM_ERR;
630 }
631 const char *name = jtag_interface ? jtag_interface->name : NULL;
632 Jim_SetResultString(goi.interp, name ? : "undefined", -1);
633 return JIM_OK;
634 }
635
636 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
637 {
638 Jim_GetOptInfo goi;
639 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
640 if (goi.argc != 0) {
641 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
642 return JIM_ERR;
643 }
644 struct command_context *context = Jim_GetAssocData(interp, "context");
645 int e = jtag_init_inner(context);
646 if (e != ERROR_OK) {
647 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
648 return JIM_ERR;
649 }
650 return JIM_OK;
651 }
652
653 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
654 {
655 Jim_GetOptInfo goi;
656 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
657 if (goi.argc != 0) {
658 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
659 return JIM_ERR;
660 }
661 struct command_context *context = Jim_GetAssocData(interp, "context");
662 int e = jtag_init_reset(context);
663 if (e != ERROR_OK) {
664 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
665 return JIM_ERR;
666 }
667 return JIM_OK;
668 }
669
670 static int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
671 {
672 Jim_GetOptInfo goi;
673 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
674 return jim_newtap_cmd(&goi);
675 }
676
677 static bool jtag_tap_enable(struct jtag_tap *t)
678 {
679 if (t->enabled)
680 return false;
681 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
682 if (!t->enabled)
683 return false;
684
685 /* FIXME add JTAG sanity checks, w/o TLR
686 * - scan chain length grew by one (this)
687 * - IDs and IR lengths are as expected
688 */
689 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
690 return true;
691 }
692 static bool jtag_tap_disable(struct jtag_tap *t)
693 {
694 if (!t->enabled)
695 return false;
696 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
697 if (t->enabled)
698 return false;
699
700 /* FIXME add JTAG sanity checks, w/o TLR
701 * - scan chain length shrank by one (this)
702 * - IDs and IR lengths are as expected
703 */
704 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
705 return true;
706 }
707
708 static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
709 {
710 const char *cmd_name = Jim_GetString(argv[0], NULL);
711 Jim_GetOptInfo goi;
712 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
713 if (goi.argc != 1) {
714 Jim_SetResult_sprintf(goi.interp, "usage: %s <name>", cmd_name);
715 return JIM_ERR;
716 }
717
718 struct jtag_tap *t;
719
720 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
721 if (t == NULL)
722 return JIM_ERR;
723
724 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
725 // do nothing, just return the value
726 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
727 if (!jtag_tap_enable(t))
728 LOG_WARNING("failed to disable tap");
729 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
730 if (!jtag_tap_disable(t))
731 LOG_WARNING("failed to disable tap");
732 } else {
733 LOG_ERROR("command '%s' unknown", cmd_name);
734 return JIM_ERR;
735 }
736 bool e = t->enabled;
737 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
738 return JIM_OK;
739 }
740
741 static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
742 {
743 const char *cmd_name = Jim_GetString(argv[0], NULL);
744 Jim_GetOptInfo goi;
745 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
746 goi.isconfigure = !strcmp(cmd_name, "configure");
747 if (goi.argc < 2 + goi.isconfigure) {
748 Jim_WrongNumArgs(goi.interp, 0, NULL,
749 "<tap_name> <attribute> ...");
750 return JIM_ERR;
751 }
752
753 struct jtag_tap *t;
754
755 Jim_Obj *o;
756 Jim_GetOpt_Obj(&goi, &o);
757 t = jtag_tap_by_jim_obj(goi.interp, o);
758 if (t == NULL) {
759 return JIM_ERR;
760 }
761
762 return jtag_tap_configure_cmd(&goi, t);
763 }
764
765 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
766 {
767 Jim_GetOptInfo goi;
768 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
769 if (goi.argc != 0) {
770 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
771 return JIM_ERR;
772 }
773 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
774 struct jtag_tap *tap;
775
776 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
777 Jim_ListAppendElement(goi.interp,
778 Jim_GetResult(goi.interp),
779 Jim_NewStringObj(goi.interp,
780 tap->dotted_name, -1));
781 }
782 return JIM_OK;
783 }
784
785 static const struct command_registration jtag_subcommand_handlers[] = {
786 {
787 .name = "interface",
788 .mode = COMMAND_ANY,
789 .jim_handler = &jim_jtag_interface,
790 .help = "Returns the selected interface",
791 },
792 {
793 .name = "arp_init",
794 .mode = COMMAND_ANY,
795 .jim_handler = &jim_jtag_arp_init,
796 },
797 {
798 .name = "arp_init-reset",
799 .mode = COMMAND_ANY,
800 .jim_handler = &jim_jtag_arp_init_reset,
801 },
802 {
803 .name = "newtap",
804 .mode = COMMAND_CONFIG,
805 .jim_handler = &jim_jtag_newtap,
806 .help = "Create a new TAP instance",
807 .usage = "<name> <type> -irlen <count> [-ircapture <count>] "
808 "[-irmask <count>] [-enable|-disable]",
809 },
810 {
811 .name = "tapisenabled",
812 .mode = COMMAND_EXEC,
813 .jim_handler = &jim_jtag_tap_enabler,
814 .help = "Returns a integer indicating TAP state (0/1)",
815 .usage = "<name>",
816 },
817 {
818 .name = "tapenable",
819 .mode = COMMAND_EXEC,
820 .jim_handler = &jim_jtag_tap_enabler,
821 .help = "Enable the specified TAP",
822 .usage = "<name>",
823 },
824 {
825 .name = "tapdisable",
826 .mode = COMMAND_EXEC,
827 .jim_handler = &jim_jtag_tap_enabler,
828 .help = "Enable the specified TAP",
829 .usage = "<name>",
830 },
831 {
832 .name = "configure",
833 .mode = COMMAND_EXEC,
834 .jim_handler = &jim_jtag_configure,
835 .help = "Enable the specified TAP",
836 .usage = "<name> [<key> <value> ...]",
837 },
838 {
839 .name = "cget",
840 .mode = COMMAND_EXEC,
841 .jim_handler = &jim_jtag_configure,
842 .help = "Enable the specified TAP",
843 .usage = "<name> [<key> <value> ...]",
844 },
845 {
846 .name = "names",
847 .mode = COMMAND_ANY,
848 .jim_handler = &jim_jtag_names,
849 .help = "Returns list of all JTAG tap names",
850 },
851 {
852 .chain = jtag_command_handlers_to_move,
853 },
854 COMMAND_REGISTRATION_DONE
855 };
856
857 void jtag_notify_event(enum jtag_event event)
858 {
859 struct jtag_tap *tap;
860
861 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
862 jtag_tap_handle_event(tap, event);
863 }
864
865
866 static int default_khz(int khz, int *jtag_speed)
867 {
868 LOG_ERROR("Translation from khz to jtag_speed not implemented");
869 return ERROR_FAIL;
870 }
871
872 static int default_speed_div(int speed, int *khz)
873 {
874 LOG_ERROR("Translation from jtag_speed to khz not implemented");
875 return ERROR_FAIL;
876 }
877
878 static int default_power_dropout(int *dropout)
879 {
880 *dropout = 0; /* by default we can't detect power dropout */
881 return ERROR_OK;
882 }
883
884 static int default_srst_asserted(int *srst_asserted)
885 {
886 *srst_asserted = 0; /* by default we can't detect srst asserted */
887 return ERROR_OK;
888 }
889
890 COMMAND_HANDLER(handle_interface_list_command)
891 {
892 if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
893 return ERROR_COMMAND_SYNTAX_ERROR;
894
895 command_print(CMD_CTX, "The following JTAG interfaces are available:");
896 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
897 {
898 const char *name = jtag_interfaces[i]->name;
899 command_print(CMD_CTX, "%u: %s", i + 1, name);
900 }
901
902 return ERROR_OK;
903 }
904
905 COMMAND_HANDLER(handle_interface_command)
906 {
907 /* check whether the interface is already configured */
908 if (jtag_interface)
909 {
910 LOG_WARNING("Interface already configured, ignoring");
911 return ERROR_OK;
912 }
913
914 /* interface name is a mandatory argument */
915 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
916 return ERROR_COMMAND_SYNTAX_ERROR;
917
918 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
919 {
920 if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0)
921 continue;
922
923 if (NULL != jtag_interfaces[i]->commands)
924 {
925 int retval = register_commands(CMD_CTX, NULL,
926 jtag_interfaces[i]->commands);
927 if (ERROR_OK != retval)
928 return retval;
929 }
930
931 jtag_interface = jtag_interfaces[i];
932
933 if (jtag_interface->khz == NULL)
934 jtag_interface->khz = default_khz;
935 if (jtag_interface->speed_div == NULL)
936 jtag_interface->speed_div = default_speed_div;
937 if (jtag_interface->power_dropout == NULL)
938 jtag_interface->power_dropout = default_power_dropout;
939 if (jtag_interface->srst_asserted == NULL)
940 jtag_interface->srst_asserted = default_srst_asserted;
941
942 return ERROR_OK;
943 }
944
945 /* no valid interface was found (i.e. the configuration option,
946 * didn't match one of the compiled-in interfaces
947 */
948 LOG_ERROR("The specified JTAG interface was not found (%s)", CMD_ARGV[0]);
949 CALL_COMMAND_HANDLER(handle_interface_list_command);
950 return ERROR_JTAG_INVALID_INTERFACE;
951 }
952
953 COMMAND_HANDLER(handle_scan_chain_command)
954 {
955 struct jtag_tap *tap;
956
957 tap = jtag_all_taps();
958 command_print(CMD_CTX, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
959 command_print(CMD_CTX, "---|--------------------|---------|------------|------------|------|------|------|---------");
960
961 while (tap) {
962 uint32_t expected, expected_mask, cur_instr, ii;
963 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
964 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
965 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
966
967 command_print(CMD_CTX,
968 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
969 tap->abs_chain_position,
970 tap->dotted_name,
971 tap->enabled ? 'Y' : 'n',
972 (unsigned int)(tap->idcode),
973 (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
974 (unsigned int)(tap->ir_length),
975 (unsigned int)(expected),
976 (unsigned int)(expected_mask),
977 (unsigned int)(cur_instr));
978
979 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
980 command_print(CMD_CTX, " | | | | 0x%08x | | | | ",
981 (unsigned int)(tap->expected_ids[ii]));
982 }
983
984 tap = tap->next_tap;
985 }
986
987 return ERROR_OK;
988 }
989
990 COMMAND_HANDLER(handle_reset_config_command)
991 {
992 int new_cfg = 0;
993 int mask = 0;
994
995 /* Original versions cared about the order of these tokens:
996 * reset_config signals [combination [trst_type [srst_type]]]
997 * They also clobbered the previous configuration even on error.
998 *
999 * Here we don't care about the order, and only change values
1000 * which have been explicitly specified.
1001 */
1002 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
1003 int tmp = 0;
1004 int m;
1005
1006 /* gating */
1007 m = RESET_SRST_NO_GATING;
1008 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
1009 /* default: don't use JTAG while SRST asserted */;
1010 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
1011 tmp = RESET_SRST_NO_GATING;
1012 else
1013 m = 0;
1014 if (mask & m) {
1015 LOG_ERROR("extra reset_config %s spec (%s)",
1016 "gating", *CMD_ARGV);
1017 return ERROR_INVALID_ARGUMENTS;
1018 }
1019 if (m)
1020 goto next;
1021
1022 /* signals */
1023 m = RESET_HAS_TRST | RESET_HAS_SRST;
1024 if (strcmp(*CMD_ARGV, "none") == 0)
1025 tmp = RESET_NONE;
1026 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
1027 tmp = RESET_HAS_TRST;
1028 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
1029 tmp = RESET_HAS_SRST;
1030 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
1031 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
1032 else
1033 m = 0;
1034 if (mask & m) {
1035 LOG_ERROR("extra reset_config %s spec (%s)",
1036 "signal", *CMD_ARGV);
1037 return ERROR_INVALID_ARGUMENTS;
1038 }
1039 if (m)
1040 goto next;
1041
1042 /* combination (options for broken wiring) */
1043 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1044 if (strcmp(*CMD_ARGV, "separate") == 0)
1045 /* separate reset lines - default */;
1046 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
1047 tmp |= RESET_SRST_PULLS_TRST;
1048 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
1049 tmp |= RESET_TRST_PULLS_SRST;
1050 else if (strcmp(*CMD_ARGV, "combined") == 0)
1051 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1052 else
1053 m = 0;
1054 if (mask & m) {
1055 LOG_ERROR("extra reset_config %s spec (%s)",
1056 "combination", *CMD_ARGV);
1057 return ERROR_INVALID_ARGUMENTS;
1058 }
1059 if (m)
1060 goto next;
1061
1062 /* trst_type (NOP without HAS_TRST) */
1063 m = RESET_TRST_OPEN_DRAIN;
1064 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
1065 tmp |= RESET_TRST_OPEN_DRAIN;
1066 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
1067 /* push/pull from adapter - default */;
1068 else
1069 m = 0;
1070 if (mask & m) {
1071 LOG_ERROR("extra reset_config %s spec (%s)",
1072 "trst_type", *CMD_ARGV);
1073 return ERROR_INVALID_ARGUMENTS;
1074 }
1075 if (m)
1076 goto next;
1077
1078 /* srst_type (NOP without HAS_SRST) */
1079 m |= RESET_SRST_PUSH_PULL;
1080 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
1081 tmp |= RESET_SRST_PUSH_PULL;
1082 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
1083 /* open drain from adapter - default */;
1084 else
1085 m = 0;
1086 if (mask & m) {
1087 LOG_ERROR("extra reset_config %s spec (%s)",
1088 "srst_type", *CMD_ARGV);
1089 return ERROR_INVALID_ARGUMENTS;
1090 }
1091 if (m)
1092 goto next;
1093
1094 /* caller provided nonsense; fail */
1095 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
1096 return ERROR_INVALID_ARGUMENTS;
1097
1098 next:
1099 /* Remember the bits which were specified (mask)
1100 * and their new values (new_cfg).
1101 */
1102 mask |= m;
1103 new_cfg |= tmp;
1104 }
1105
1106 /* clear previous values of those bits, save new values */
1107 if (mask) {
1108 int old_cfg = jtag_get_reset_config();
1109
1110 old_cfg &= ~mask;
1111 new_cfg |= old_cfg;
1112 jtag_set_reset_config(new_cfg);
1113 } else
1114 new_cfg = jtag_get_reset_config();
1115
1116
1117 /*
1118 * Display the (now-)current reset mode
1119 */
1120 char *modes[5];
1121
1122 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
1123 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
1124 case RESET_HAS_SRST:
1125 modes[0] = "srst_only";
1126 break;
1127 case RESET_HAS_TRST:
1128 modes[0] = "trst_only";
1129 break;
1130 case RESET_TRST_AND_SRST:
1131 modes[0] = "trst_and_srst";
1132 break;
1133 default:
1134 modes[0] = "none";
1135 break;
1136 }
1137
1138 /* normally SRST and TRST are decoupled; but bugs happen ... */
1139 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
1140 case RESET_SRST_PULLS_TRST:
1141 modes[1] = "srst_pulls_trst";
1142 break;
1143 case RESET_TRST_PULLS_SRST:
1144 modes[1] = "trst_pulls_srst";
1145 break;
1146 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
1147 modes[1] = "combined";
1148 break;
1149 default:
1150 modes[1] = "separate";
1151 break;
1152 }
1153
1154 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
1155 if (new_cfg & RESET_HAS_TRST) {
1156 if (new_cfg & RESET_TRST_OPEN_DRAIN)
1157 modes[3] = " trst_open_drain";
1158 else
1159 modes[3] = " trst_push_pull";
1160 } else
1161 modes[3] = "";
1162
1163 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
1164 if (new_cfg & RESET_HAS_SRST) {
1165 if (new_cfg & RESET_SRST_NO_GATING)
1166 modes[2] = " srst_nogate";
1167 else
1168 modes[2] = " srst_gates_jtag";
1169
1170 if (new_cfg & RESET_SRST_PUSH_PULL)
1171 modes[4] = " srst_push_pull";
1172 else
1173 modes[4] = " srst_open_drain";
1174 } else {
1175 modes[2] = "";
1176 modes[4] = "";
1177 }
1178
1179 command_print(CMD_CTX, "%s %s%s%s%s",
1180 modes[0], modes[1],
1181 modes[2], modes[3], modes[4]);
1182
1183 return ERROR_OK;
1184 }
1185
1186 COMMAND_HANDLER(handle_jtag_nsrst_delay_command)
1187 {
1188 if (CMD_ARGC > 1)
1189 return ERROR_COMMAND_SYNTAX_ERROR;
1190 if (CMD_ARGC == 1)
1191 {
1192 unsigned delay;
1193 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1194
1195 jtag_set_nsrst_delay(delay);
1196 }
1197 command_print(CMD_CTX, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
1198 return ERROR_OK;
1199 }
1200
1201 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1202 {
1203 if (CMD_ARGC > 1)
1204 return ERROR_COMMAND_SYNTAX_ERROR;
1205 if (CMD_ARGC == 1)
1206 {
1207 unsigned delay;
1208 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1209
1210 jtag_set_ntrst_delay(delay);
1211 }
1212 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1213 return ERROR_OK;
1214 }
1215
1216 COMMAND_HANDLER(handle_jtag_nsrst_assert_width_command)
1217 {
1218 if (CMD_ARGC > 1)
1219 return ERROR_COMMAND_SYNTAX_ERROR;
1220 if (CMD_ARGC == 1)
1221 {
1222 unsigned delay;
1223 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1224
1225 jtag_set_nsrst_assert_width(delay);
1226 }
1227 command_print(CMD_CTX, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
1228 return ERROR_OK;
1229 }
1230
1231 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1232 {
1233 if (CMD_ARGC > 1)
1234 return ERROR_COMMAND_SYNTAX_ERROR;
1235 if (CMD_ARGC == 1)
1236 {
1237 unsigned delay;
1238 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1239
1240 jtag_set_ntrst_assert_width(delay);
1241 }
1242 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1243 return ERROR_OK;
1244 }
1245
1246 COMMAND_HANDLER(handle_jtag_khz_command)
1247 {
1248 if (CMD_ARGC > 1)
1249 return ERROR_COMMAND_SYNTAX_ERROR;
1250
1251 int retval = ERROR_OK;
1252 if (CMD_ARGC == 1)
1253 {
1254 unsigned khz = 0;
1255 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1256
1257 retval = jtag_config_khz(khz);
1258 if (ERROR_OK != retval)
1259 return retval;
1260 }
1261
1262 int cur_speed = jtag_get_speed_khz();
1263 retval = jtag_get_speed_readable(&cur_speed);
1264 if (ERROR_OK != retval)
1265 return retval;
1266
1267 if (cur_speed)
1268 command_print(CMD_CTX, "%d kHz", cur_speed);
1269 else
1270 command_print(CMD_CTX, "RCLK - adaptive");
1271
1272 return retval;
1273 }
1274
1275 COMMAND_HANDLER(handle_jtag_rclk_command)
1276 {
1277 if (CMD_ARGC > 1)
1278 return ERROR_COMMAND_SYNTAX_ERROR;
1279
1280 int retval = ERROR_OK;
1281 if (CMD_ARGC == 1)
1282 {
1283 unsigned khz = 0;
1284 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1285
1286 retval = jtag_config_rclk(khz);
1287 if (ERROR_OK != retval)
1288 return retval;
1289 }
1290
1291 int cur_khz = jtag_get_speed_khz();
1292 retval = jtag_get_speed_readable(&cur_khz);
1293 if (ERROR_OK != retval)
1294 return retval;
1295
1296 if (cur_khz)
1297 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1298 else
1299 command_print(CMD_CTX, "RCLK - adaptive");
1300
1301 return retval;
1302 }
1303
1304 COMMAND_HANDLER(handle_jtag_reset_command)
1305 {
1306 if (CMD_ARGC != 2)
1307 return ERROR_COMMAND_SYNTAX_ERROR;
1308
1309 int trst = -1;
1310 if (CMD_ARGV[0][0] == '1')
1311 trst = 1;
1312 else if (CMD_ARGV[0][0] == '0')
1313 trst = 0;
1314 else
1315 return ERROR_COMMAND_SYNTAX_ERROR;
1316
1317 int srst = -1;
1318 if (CMD_ARGV[1][0] == '1')
1319 srst = 1;
1320 else if (CMD_ARGV[1][0] == '0')
1321 srst = 0;
1322 else
1323 return ERROR_COMMAND_SYNTAX_ERROR;
1324
1325 if (jtag_interface_init(CMD_CTX) != ERROR_OK)
1326 return ERROR_JTAG_INIT_FAILED;
1327
1328 jtag_add_reset(trst, srst);
1329 return jtag_execute_queue();
1330 }
1331
1332 COMMAND_HANDLER(handle_runtest_command)
1333 {
1334 if (CMD_ARGC != 1)
1335 return ERROR_COMMAND_SYNTAX_ERROR;
1336
1337 unsigned num_clocks;
1338 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1339
1340 jtag_add_runtest(num_clocks, TAP_IDLE);
1341 return jtag_execute_queue();
1342 }
1343
1344 /*
1345 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1346 * should be stable ... and *NOT* a shift state, otherwise free-running
1347 * jtag clocks could change the values latched by the update state.
1348 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1349 * and "drscan" commands are a write-only subset of what SVF provides.
1350 */
1351
1352 COMMAND_HANDLER(handle_irscan_command)
1353 {
1354 int i;
1355 struct scan_field *fields;
1356 struct jtag_tap *tap;
1357 tap_state_t endstate;
1358
1359 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1360 {
1361 return ERROR_COMMAND_SYNTAX_ERROR;
1362 }
1363
1364 /* optional "-endstate" "statename" at the end of the arguments,
1365 * so that e.g. IRPAUSE can let us load the data register before
1366 * entering RUN/IDLE to execute the instruction we load here.
1367 */
1368 endstate = TAP_IDLE;
1369
1370 if (CMD_ARGC >= 4) {
1371 /* have at least one pair of numbers. */
1372 /* is last pair the magic text? */
1373 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1374 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1375 if (endstate == TAP_INVALID)
1376 return ERROR_COMMAND_SYNTAX_ERROR;
1377 if (!scan_is_safe(endstate))
1378 LOG_WARNING("unstable irscan endstate \"%s\"",
1379 CMD_ARGV[CMD_ARGC - 1]);
1380 CMD_ARGC -= 2;
1381 }
1382 }
1383
1384 int num_fields = CMD_ARGC / 2;
1385 size_t fields_len = sizeof(struct scan_field) * num_fields;
1386 fields = malloc(fields_len);
1387 memset(fields, 0, fields_len);
1388
1389 int retval;
1390 for (i = 0; i < num_fields; i++)
1391 {
1392 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1393 if (tap == NULL)
1394 {
1395 int j;
1396 for (j = 0; j < i; j++)
1397 free(fields[j].out_value);
1398 free(fields);
1399 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1400
1401 return ERROR_FAIL;
1402 }
1403 int field_size = tap->ir_length;
1404 fields[i].tap = tap;
1405 fields[i].num_bits = field_size;
1406 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1407
1408 uint32_t value;
1409 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1410 if (ERROR_OK != retval)
1411 goto error_return;
1412 buf_set_u32(fields[i].out_value, 0, field_size, value);
1413 fields[i].in_value = NULL;
1414 }
1415
1416 /* did we have an endstate? */
1417 jtag_add_ir_scan(num_fields, fields, endstate);
1418
1419 retval = jtag_execute_queue();
1420
1421 error_return:
1422 for (i = 0; i < num_fields; i++)
1423 {
1424 if (NULL != fields[i].out_value)
1425 free(fields[i].out_value);
1426 }
1427
1428 free (fields);
1429
1430 return retval;
1431 }
1432
1433
1434 COMMAND_HANDLER(handle_verify_ircapture_command)
1435 {
1436 if (CMD_ARGC > 1)
1437 return ERROR_COMMAND_SYNTAX_ERROR;
1438
1439 if (CMD_ARGC == 1)
1440 {
1441 bool enable;
1442 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1443 jtag_set_verify_capture_ir(enable);
1444 }
1445
1446 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1447 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1448
1449 return ERROR_OK;
1450 }
1451
1452 COMMAND_HANDLER(handle_verify_jtag_command)
1453 {
1454 if (CMD_ARGC > 1)
1455 return ERROR_COMMAND_SYNTAX_ERROR;
1456
1457 if (CMD_ARGC == 1)
1458 {
1459 bool enable;
1460 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1461 jtag_set_verify(enable);
1462 }
1463
1464 const char *status = jtag_will_verify() ? "enabled": "disabled";
1465 command_print(CMD_CTX, "verify jtag capture is %s", status);
1466
1467 return ERROR_OK;
1468 }
1469
1470 COMMAND_HANDLER(handle_tms_sequence_command)
1471 {
1472 if (CMD_ARGC > 1)
1473 return ERROR_COMMAND_SYNTAX_ERROR;
1474
1475 if (CMD_ARGC == 1)
1476 {
1477 bool use_new_table;
1478 if (strcmp(CMD_ARGV[0], "short") == 0)
1479 use_new_table = true;
1480 else if (strcmp(CMD_ARGV[0], "long") == 0)
1481 use_new_table = false;
1482 else
1483 return ERROR_COMMAND_SYNTAX_ERROR;
1484
1485 tap_use_new_tms_table(use_new_table);
1486 }
1487
1488 command_print(CMD_CTX, "tms sequence is %s",
1489 tap_uses_new_tms_table() ? "short": "long");
1490
1491 return ERROR_OK;
1492 }
1493
1494 static const struct command_registration jtag_command_handlers[] = {
1495 {
1496 .name = "interface",
1497 .handler = &handle_interface_command,
1498 .mode = COMMAND_CONFIG,
1499 .help = "select a JTAG interface",
1500 .usage = "<driver_name>",
1501 },
1502 {
1503 .name = "interface_list",
1504 .handler = &handle_interface_list_command,
1505 .mode = COMMAND_ANY,
1506 .help = "list all built-in interfaces",
1507 },
1508 {
1509 .name = "jtag_khz",
1510 .handler = &handle_jtag_khz_command,
1511 .mode = COMMAND_ANY,
1512 .help = "set maximum jtag speed (if supported)",
1513 .usage = "<khz:0=rtck>",
1514 },
1515 {
1516 .name = "jtag_rclk",
1517 .handler = &handle_jtag_rclk_command,
1518 .mode = COMMAND_ANY,
1519 .help = "set JTAG speed to RCLK or use fallback speed",
1520 .usage = "<fallback_speed_khz>",
1521 },
1522 {
1523 .name = "reset_config",
1524 .handler = &handle_reset_config_command,
1525 .mode = COMMAND_ANY,
1526 .help = "configure JTAG reset behavior",
1527 .usage = "[none|trst_only|srst_only|trst_and_srst] "
1528 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
1529 "[srst_gates_jtag|srst_nogate] "
1530 "[trst_push_pull|trst_open_drain] "
1531 "[srst_push_pull|srst_open_drain]",
1532 },
1533 {
1534 .name = "jtag_nsrst_delay",
1535 .handler = &handle_jtag_nsrst_delay_command,
1536 .mode = COMMAND_ANY,
1537 .help = "delay after deasserting srst in ms",
1538 .usage = "<ms>",
1539 },
1540 {
1541 .name = "jtag_ntrst_delay",
1542 .handler = &handle_jtag_ntrst_delay_command,
1543 .mode = COMMAND_ANY,
1544 .help = "delay after deasserting trst in ms",
1545 .usage = "<ms>"
1546 },
1547 {
1548 .name = "jtag_nsrst_assert_width",
1549 .handler = &handle_jtag_nsrst_assert_width_command,
1550 .mode = COMMAND_ANY,
1551 .help = "delay after asserting srst in ms",
1552 .usage = "<ms>"
1553 },
1554 {
1555 .name = "jtag_ntrst_assert_width",
1556 .handler = &handle_jtag_ntrst_assert_width_command,
1557 .mode = COMMAND_ANY,
1558 .help = "delay after asserting trst in ms",
1559 .usage = "<ms>"
1560 },
1561 {
1562 .name = "scan_chain",
1563 .handler = &handle_scan_chain_command,
1564 .mode = COMMAND_EXEC,
1565 .help = "print current scan chain configuration",
1566 },
1567 {
1568 .name = "jtag_reset",
1569 .handler = &handle_jtag_reset_command,
1570 .mode = COMMAND_EXEC,
1571 .help = "toggle reset lines",
1572 .usage = "<trst> <srst>",
1573 },
1574 {
1575 .name = "runtest",
1576 .handler = &handle_runtest_command,
1577 .mode = COMMAND_EXEC,
1578 .help = "move to Run-Test/Idle, and execute <num_cycles>",
1579 .usage = "<num_cycles>"
1580 },
1581 {
1582 .name = "irscan",
1583 .handler = &handle_irscan_command,
1584 .mode = COMMAND_EXEC,
1585 .help = "execute IR scan",
1586 .usage = "<device> <instr> [dev2] [instr2] ...",
1587 },
1588 {
1589 .name = "verify_ircapture",
1590 .handler = &handle_verify_ircapture_command,
1591 .mode = COMMAND_ANY,
1592 .help = "verify value captured during Capture-IR",
1593 .usage = "<enable | disable>",
1594 },
1595 {
1596 .name = "verify_jtag",
1597 .handler = &handle_verify_jtag_command,
1598 .mode = COMMAND_ANY,
1599 .help = "verify value capture",
1600 .usage = "<enable | disable>",
1601 },
1602 {
1603 .name = "tms_sequence",
1604 .handler = &handle_tms_sequence_command,
1605 .mode = COMMAND_ANY,
1606 .help = "choose short(default) or long tms_sequence",
1607 .usage = "<short | long>",
1608 },
1609 {
1610 .name = "jtag",
1611 .mode = COMMAND_ANY,
1612 .help = "perform jtag tap actions",
1613
1614 .chain = jtag_subcommand_handlers,
1615 },
1616 {
1617 .chain = jtag_command_handlers_to_move,
1618 },
1619 COMMAND_REGISTRATION_DONE
1620 };
1621 int jtag_register_commands(struct command_context *cmd_ctx)
1622 {
1623 return register_commands(cmd_ctx, NULL, jtag_command_handlers);
1624 }