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