initial SWD transport (SWD infrastructure #2)
[openocd.git] / src / jtag / tcl.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 √ėyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2009 SoftPLC Corporation *
9 * http://softplc.com *
10 * dick@softplc.com *
11 * *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
14 * *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
19 * *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
24 * *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
29 ***************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
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 /**
44 * @file
45 * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
46 */
47
48 static const Jim_Nvp nvp_jtag_tap_event[] = {
49 { .value = JTAG_TRST_ASSERTED, .name = "post-reset" },
50 { .value = JTAG_TAP_EVENT_SETUP, .name = "setup" },
51 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
52 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
53
54 { .name = NULL, .value = -1 }
55 };
56
57 extern struct jtag_interface *jtag_interface;
58
59 struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
60 {
61 const char *cp = Jim_GetString(o, NULL);
62 struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
63 if (NULL == cp)
64 cp = "(unknown)";
65 if (NULL == t)
66 Jim_SetResultFormatted(interp, "Tap '%s' could not be found", cp);
67 return t;
68 }
69
70 static bool scan_is_safe(tap_state_t state)
71 {
72 switch (state)
73 {
74 case TAP_RESET:
75 case TAP_IDLE:
76 case TAP_DRPAUSE:
77 case TAP_IRPAUSE:
78 return true;
79 default:
80 return false;
81 }
82 }
83
84 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
85 {
86 int retval;
87 struct scan_field *fields;
88 int num_fields;
89 int field_count = 0;
90 int i, e;
91 struct jtag_tap *tap;
92 tap_state_t endstate;
93
94 /* args[1] = device
95 * args[2] = num_bits
96 * args[3] = hex string
97 * ... repeat num bits and hex string ...
98 *
99 * .. optionally:
100 * args[N-2] = "-endstate"
101 * args[N-1] = statename
102 */
103 if ((argc < 4) || ((argc % 2) != 0))
104 {
105 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
106 return JIM_ERR;
107 }
108
109 endstate = TAP_IDLE;
110
111 script_debug(interp, "drscan", argc, args);
112
113 /* validate arguments as numbers */
114 e = JIM_OK;
115 for (i = 2; i < argc; i += 2)
116 {
117 long bits;
118 const char *cp;
119
120 e = Jim_GetLong(interp, args[i], &bits);
121 /* If valid - try next arg */
122 if (e == JIM_OK) {
123 continue;
124 }
125
126 /* Not valid.. are we at the end? */
127 if (((i + 2) != argc)) {
128 /* nope, then error */
129 return e;
130 }
131
132 /* it could be: "-endstate FOO"
133 * e.g. DRPAUSE so we can issue more instructions
134 * before entering RUN/IDLE and executing them.
135 */
136
137 /* get arg as a string. */
138 cp = Jim_GetString(args[i], NULL);
139 /* is it the magic? */
140 if (0 == strcmp("-endstate", cp)) {
141 /* is the statename valid? */
142 cp = Jim_GetString(args[i + 1], NULL);
143
144 /* see if it is a valid state name */
145 endstate = tap_state_by_name(cp);
146 if (endstate < 0) {
147 /* update the error message */
148 Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
149 } else {
150 if (!scan_is_safe(endstate))
151 LOG_WARNING("drscan with unsafe "
152 "endstate \"%s\"", cp);
153
154 /* valid - so clear the error */
155 e = JIM_OK;
156 /* and remove the last 2 args */
157 argc -= 2;
158 }
159 }
160
161 /* Still an error? */
162 if (e != JIM_OK) {
163 return e; /* too bad */
164 }
165 } /* validate args */
166
167 tap = jtag_tap_by_jim_obj(interp, args[1]);
168 if (tap == NULL) {
169 return JIM_ERR;
170 }
171
172 num_fields = (argc-2)/2;
173 fields = malloc(sizeof(struct scan_field) * num_fields);
174 for (i = 2; i < argc; i += 2)
175 {
176 long bits;
177 int len;
178 const char *str;
179
180 Jim_GetLong(interp, args[i], &bits);
181 str = Jim_GetString(args[i + 1], &len);
182
183 fields[field_count].num_bits = bits;
184 void * t = malloc(DIV_ROUND_UP(bits, 8));
185 fields[field_count].out_value = t;
186 str_to_buf(str, len, t, bits, 0);
187 fields[field_count].in_value = t;
188 field_count++;
189 }
190
191 jtag_add_dr_scan(tap, num_fields, fields, endstate);
192
193 retval = jtag_execute_queue();
194 if (retval != ERROR_OK)
195 {
196 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
197 return JIM_ERR;
198 }
199
200 field_count = 0;
201 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
202 for (i = 2; i < argc; i += 2)
203 {
204 long bits;
205 char *str;
206
207 Jim_GetLong(interp, args[i], &bits);
208 str = buf_to_str(fields[field_count].in_value, bits, 16);
209 free((void *)fields[field_count].out_value);
210
211 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
212 free(str);
213 field_count++;
214 }
215
216 Jim_SetResult(interp, list);
217
218 free(fields);
219
220 return JIM_OK;
221 }
222
223
224 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
225 {
226 tap_state_t states[8];
227
228 if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
229 {
230 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
231 return JIM_ERR;
232 }
233
234 script_debug(interp, "pathmove", argc, args);
235
236 int i;
237 for (i = 0; i < argc-1; i++)
238 {
239 const char *cp;
240 cp = Jim_GetString(args[i + 1], NULL);
241 states[i] = tap_state_by_name(cp);
242 if (states[i] < 0)
243 {
244 /* update the error message */
245 Jim_SetResultFormatted(interp,"endstate: %s invalid", cp);
246 return JIM_ERR;
247 }
248 }
249
250 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
251 {
252 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
253 return JIM_ERR;
254 }
255
256 jtag_add_pathmove(argc-2, states + 1);
257
258 if (jtag_execute_queue()!= ERROR_OK)
259 {
260 Jim_SetResultString(interp, "pathmove: failed",-1);
261 return JIM_ERR;
262 }
263
264 return JIM_OK;
265 }
266
267
268 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
269 {
270 script_debug(interp, "flush_count", argc, args);
271
272 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
273
274 return JIM_OK;
275 }
276
277 /* REVISIT Just what about these should "move" ... ?
278 * These registrations, into the main JTAG table?
279 *
280 * There's a minor compatibility issue, these all show up twice;
281 * that's not desirable:
282 * - jtag drscan ... NOT DOCUMENTED!
283 * - drscan ...
284 *
285 * The "irscan" command (for example) doesn't show twice.
286 */
287 static const struct command_registration jtag_command_handlers_to_move[] = {
288 {
289 .name = "drscan",
290 .mode = COMMAND_EXEC,
291 .jim_handler = Jim_Command_drscan,
292 .help = "Execute Data Register (DR) scan for one TAP. "
293 "Other TAPs must be in BYPASS mode.",
294 .usage = "tap_name [num_bits value]* ['-endstate' state_name]",
295 },
296 {
297 .name = "flush_count",
298 .mode = COMMAND_EXEC,
299 .jim_handler = Jim_Command_flush_count,
300 .help = "Returns the number of times the JTAG queue "
301 "has been flushed.",
302 },
303 {
304 .name = "pathmove",
305 .mode = COMMAND_EXEC,
306 .jim_handler = Jim_Command_pathmove,
307 .usage = "start_state state1 [state2 [state3 ...]]",
308 .help = "Move JTAG state machine from current state "
309 "(start_state) to state1, then state2, state3, etc.",
310 },
311 COMMAND_REGISTRATION_DONE
312 };
313
314
315 enum jtag_tap_cfg_param {
316 JCFG_EVENT
317 };
318
319 static Jim_Nvp nvp_config_opts[] = {
320 { .name = "-event", .value = JCFG_EVENT },
321
322 { .name = NULL, .value = -1 }
323 };
324
325 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
326 {
327 if (goi->argc == 0)
328 {
329 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
330 return JIM_ERR;
331 }
332
333 Jim_Nvp *n;
334 int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
335 if (e != JIM_OK)
336 {
337 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
338 return e;
339 }
340
341 if (goi->isconfigure) {
342 if (goi->argc != 1) {
343 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
344 return JIM_ERR;
345 }
346 } else {
347 if (goi->argc != 0) {
348 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
349 return JIM_ERR;
350 }
351 }
352
353 struct jtag_tap_event_action *jteap = tap->event_action;
354 /* replace existing event body */
355 bool found = false;
356 while (jteap)
357 {
358 if (jteap->event == (enum jtag_event)n->value)
359 {
360 found = true;
361 break;
362 }
363 jteap = jteap->next;
364 }
365
366 Jim_SetEmptyResult(goi->interp);
367
368 if (goi->isconfigure)
369 {
370 if (!found)
371 jteap = calloc(1, sizeof(*jteap));
372 else if (NULL != jteap->body)
373 Jim_DecrRefCount(goi->interp, jteap->body);
374
375 jteap->interp = goi->interp;
376 jteap->event = n->value;
377
378 Jim_Obj *o;
379 Jim_GetOpt_Obj(goi, &o);
380 jteap->body = Jim_DuplicateObj(goi->interp, o);
381 Jim_IncrRefCount(jteap->body);
382
383 if (!found)
384 {
385 /* add to head of event list */
386 jteap->next = tap->event_action;
387 tap->event_action = jteap;
388 }
389 }
390 else if (found)
391 {
392 jteap->interp = goi->interp;
393 Jim_SetResult(goi->interp,
394 Jim_DuplicateObj(goi->interp, jteap->body));
395 }
396 return JIM_OK;
397 }
398
399 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
400 {
401 /* parse config or cget options */
402 while (goi->argc > 0)
403 {
404 Jim_SetEmptyResult (goi->interp);
405
406 Jim_Nvp *n;
407 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
408 if (e != JIM_OK)
409 {
410 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
411 return e;
412 }
413
414 switch (n->value)
415 {
416 case JCFG_EVENT:
417 e = jtag_tap_configure_event(goi, tap);
418 if (e != JIM_OK)
419 return e;
420 break;
421 default:
422 Jim_SetResultFormatted(goi->interp, "unknown event: %s", n->name);
423 return JIM_ERR;
424 }
425 }
426
427 return JIM_OK;
428 }
429
430 static int is_bad_irval(int ir_length, jim_wide w)
431 {
432 jim_wide v = 1;
433
434 v <<= ir_length;
435 v -= 1;
436 v = ~v;
437 return (w & v) != 0;
438 }
439
440 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
441 struct jtag_tap *pTap)
442 {
443 jim_wide w;
444 int e = Jim_GetOpt_Wide(goi, &w);
445 if (e != JIM_OK) {
446 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
447 return e;
448 }
449
450 unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
451 uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
452 if (new_expected_ids == NULL)
453 {
454 Jim_SetResultFormatted(goi->interp, "no memory");
455 return JIM_ERR;
456 }
457
458 memcpy(new_expected_ids, pTap->expected_ids, expected_len);
459
460 new_expected_ids[pTap->expected_ids_cnt] = w;
461
462 free(pTap->expected_ids);
463 pTap->expected_ids = new_expected_ids;
464 pTap->expected_ids_cnt++;
465
466 return JIM_OK;
467 }
468
469 #define NTAP_OPT_IRLEN 0
470 #define NTAP_OPT_IRMASK 1
471 #define NTAP_OPT_IRCAPTURE 2
472 #define NTAP_OPT_ENABLED 3
473 #define NTAP_OPT_DISABLED 4
474 #define NTAP_OPT_EXPECTED_ID 5
475 #define NTAP_OPT_VERSION 6
476
477 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
478 struct jtag_tap *pTap)
479 {
480 jim_wide w;
481 int e = Jim_GetOpt_Wide(goi, &w);
482 if (e != JIM_OK)
483 {
484 Jim_SetResultFormatted(goi->interp,
485 "option: %s bad parameter", n->name);
486 free((void *)pTap->dotted_name);
487 return e;
488 }
489 switch (n->value) {
490 case NTAP_OPT_IRLEN:
491 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
492 {
493 LOG_WARNING("%s: huge IR length %d",
494 pTap->dotted_name, (int) w);
495 }
496 pTap->ir_length = w;
497 break;
498 case NTAP_OPT_IRMASK:
499 if (is_bad_irval(pTap->ir_length, w))
500 {
501 LOG_ERROR("%s: IR mask %x too big",
502 pTap->dotted_name,
503 (int) w);
504 return JIM_ERR;
505 }
506 if ((w & 3) != 3)
507 LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
508 pTap->ir_capture_mask = w;
509 break;
510 case NTAP_OPT_IRCAPTURE:
511 if (is_bad_irval(pTap->ir_length, w))
512 {
513 LOG_ERROR("%s: IR capture %x too big",
514 pTap->dotted_name, (int) w);
515 return JIM_ERR;
516 }
517 if ((w & 3) != 1)
518 LOG_WARNING("%s: nonstandard IR value",
519 pTap->dotted_name);
520 pTap->ir_capture_value = w;
521 break;
522 default:
523 return JIM_ERR;
524 }
525 return JIM_OK;
526 }
527
528 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
529 {
530 struct jtag_tap *pTap;
531 int x;
532 int e;
533 Jim_Nvp *n;
534 char *cp;
535 const Jim_Nvp opts[] = {
536 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
537 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
538 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
539 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
540 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
541 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
542 { .name = "-ignore-version" , .value = NTAP_OPT_VERSION },
543 { .name = NULL , .value = -1 },
544 };
545
546 pTap = calloc(1, sizeof(struct jtag_tap));
547 if (!pTap) {
548 Jim_SetResultFormatted(goi->interp, "no memory");
549 return JIM_ERR;
550 }
551
552 /*
553 * we expect CHIP + TAP + OPTIONS
554 * */
555 if (goi->argc < 3) {
556 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
557 free(pTap);
558 return JIM_ERR;
559 }
560 Jim_GetOpt_String(goi, &cp, NULL);
561 pTap->chip = strdup(cp);
562
563 Jim_GetOpt_String(goi, &cp, NULL);
564 pTap->tapname = strdup(cp);
565
566 /* name + dot + name + null */
567 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
568 cp = malloc(x);
569 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
570 pTap->dotted_name = cp;
571
572 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
573 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
574
575 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
576 * that the default. The "-irlen" and "-irmask" options are only
577 * needed to cope with nonstandard TAPs, or to specify more bits.
578 */
579 pTap->ir_capture_mask = 0x03;
580 pTap->ir_capture_value = 0x01;
581
582 while (goi->argc) {
583 e = Jim_GetOpt_Nvp(goi, opts, &n);
584 if (e != JIM_OK) {
585 Jim_GetOpt_NvpUnknown(goi, opts, 0);
586 free((void *)pTap->dotted_name);
587 free(pTap);
588 return e;
589 }
590 LOG_DEBUG("Processing option: %s", n->name);
591 switch (n->value) {
592 case NTAP_OPT_ENABLED:
593 pTap->disabled_after_reset = false;
594 break;
595 case NTAP_OPT_DISABLED:
596 pTap->disabled_after_reset = true;
597 break;
598 case NTAP_OPT_EXPECTED_ID:
599 e = jim_newtap_expected_id(n, goi, pTap);
600 if (JIM_OK != e)
601 {
602 free((void *)pTap->dotted_name);
603 free(pTap);
604 return e;
605 }
606 break;
607 case NTAP_OPT_IRLEN:
608 case NTAP_OPT_IRMASK:
609 case NTAP_OPT_IRCAPTURE:
610 e = jim_newtap_ir_param(n, goi, pTap);
611 if (JIM_OK != e)
612 {
613 free((void *)pTap->dotted_name);
614 free(pTap);
615 return e;
616 }
617 break;
618 case NTAP_OPT_VERSION:
619 pTap->ignore_version = true;
620 break;
621 } /* switch (n->value) */
622 } /* while (goi->argc) */
623
624 /* default is enabled-after-reset */
625 pTap->enabled = !pTap->disabled_after_reset;
626
627 /* Did all the required option bits get cleared? */
628 if (pTap->ir_length != 0)
629 {
630 jtag_tap_init(pTap);
631 return JIM_OK;
632 }
633
634 Jim_SetResultFormatted(goi->interp,
635 "newtap: %s missing IR length",
636 pTap->dotted_name);
637 jtag_tap_free(pTap);
638 return JIM_ERR;
639 }
640
641 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
642 {
643 struct jtag_tap_event_action * jteap;
644
645 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
646 {
647 if (jteap->event != e)
648 continue;
649
650 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
651 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
652 tap->dotted_name, e, nvp->name,
653 Jim_GetString(jteap->body, NULL));
654
655 if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK)
656 {
657 Jim_MakeErrorMessage(jteap->interp);
658 LOG_USER_N("%s\n", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
659 continue;
660 }
661
662 switch (e)
663 {
664 case JTAG_TAP_EVENT_ENABLE:
665 case JTAG_TAP_EVENT_DISABLE:
666 /* NOTE: we currently assume the handlers
667 * can't fail. Right here is where we should
668 * really be verifying the scan chains ...
669 */
670 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
671 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
672 tap->enabled ? "enabled" : "disabled");
673 break;
674 default:
675 break;
676 }
677 }
678 }
679
680 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
681 {
682 Jim_GetOptInfo goi;
683 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
684 if (goi.argc != 0) {
685 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
686 return JIM_ERR;
687 }
688 struct command_context *context = current_command_context(interp);
689 int e = jtag_init_inner(context);
690 if (e != ERROR_OK) {
691 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
692 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
693 Jim_FreeNewObj(goi.interp, eObj);
694 return JIM_ERR;
695 }
696 return JIM_OK;
697 }
698
699 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
700 {
701 Jim_GetOptInfo goi;
702 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
703 if (goi.argc != 0) {
704 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
705 return JIM_ERR;
706 }
707 struct command_context *context = current_command_context(interp);
708 int e = jtag_init_reset(context);
709 if (e != ERROR_OK) {
710 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
711 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
712 Jim_FreeNewObj(goi.interp, eObj);
713 return JIM_ERR;
714 }
715 return JIM_OK;
716 }
717
718 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
719 {
720 Jim_GetOptInfo goi;
721 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
722 return jim_newtap_cmd(&goi);
723 }
724
725 static bool jtag_tap_enable(struct jtag_tap *t)
726 {
727 if (t->enabled)
728 return false;
729 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
730 if (!t->enabled)
731 return false;
732
733 /* FIXME add JTAG sanity checks, w/o TLR
734 * - scan chain length grew by one (this)
735 * - IDs and IR lengths are as expected
736 */
737 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
738 return true;
739 }
740 static bool jtag_tap_disable(struct jtag_tap *t)
741 {
742 if (!t->enabled)
743 return false;
744 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
745 if (t->enabled)
746 return false;
747
748 /* FIXME add JTAG sanity checks, w/o TLR
749 * - scan chain length shrank by one (this)
750 * - IDs and IR lengths are as expected
751 */
752 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
753 return true;
754 }
755
756 static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
757 {
758 const char *cmd_name = Jim_GetString(argv[0], NULL);
759 Jim_GetOptInfo goi;
760 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
761 if (goi.argc != 1) {
762 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
763 return JIM_ERR;
764 }
765
766 struct jtag_tap *t;
767
768 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
769 if (t == NULL)
770 return JIM_ERR;
771
772 if (strcasecmp(cmd_name, "tapisenabled") == 0) {
773 // do nothing, just return the value
774 } else if (strcasecmp(cmd_name, "tapenable") == 0) {
775 if (!jtag_tap_enable(t))
776 LOG_WARNING("failed to enable tap %s", t->dotted_name);
777 } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
778 if (!jtag_tap_disable(t))
779 LOG_WARNING("failed to disable tap %s", t->dotted_name);
780 } else {
781 LOG_ERROR("command '%s' unknown", cmd_name);
782 return JIM_ERR;
783 }
784 bool e = t->enabled;
785 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
786 return JIM_OK;
787 }
788
789 static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
790 {
791 const char *cmd_name = Jim_GetString(argv[0], NULL);
792 Jim_GetOptInfo goi;
793 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
794 goi.isconfigure = !strcmp(cmd_name, "configure");
795 if (goi.argc < 2 + goi.isconfigure) {
796 Jim_WrongNumArgs(goi.interp, 0, NULL,
797 "<tap_name> <attribute> ...");
798 return JIM_ERR;
799 }
800
801 struct jtag_tap *t;
802
803 Jim_Obj *o;
804 Jim_GetOpt_Obj(&goi, &o);
805 t = jtag_tap_by_jim_obj(goi.interp, o);
806 if (t == NULL) {
807 return JIM_ERR;
808 }
809
810 return jtag_tap_configure_cmd(&goi, t);
811 }
812
813 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
814 {
815 Jim_GetOptInfo goi;
816 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
817 if (goi.argc != 0) {
818 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
819 return JIM_ERR;
820 }
821 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
822 struct jtag_tap *tap;
823
824 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
825 Jim_ListAppendElement(goi.interp,
826 Jim_GetResult(goi.interp),
827 Jim_NewStringObj(goi.interp,
828 tap->dotted_name, -1));
829 }
830 return JIM_OK;
831 }
832
833 COMMAND_HANDLER(handle_jtag_init_command)
834 {
835 if (CMD_ARGC != 0)
836 return ERROR_COMMAND_SYNTAX_ERROR;
837
838 static bool jtag_initialized = false;
839 if (jtag_initialized)
840 {
841 LOG_INFO("'jtag init' has already been called");
842 return ERROR_OK;
843 }
844 jtag_initialized = true;
845
846 LOG_DEBUG("Initializing jtag devices...");
847 return jtag_init(CMD_CTX);
848 }
849
850 static const struct command_registration jtag_subcommand_handlers[] = {
851 {
852 .name = "init",
853 .mode = COMMAND_ANY,
854 .handler = handle_jtag_init_command,
855 .help = "initialize jtag scan chain",
856 },
857 {
858 .name = "arp_init",
859 .mode = COMMAND_ANY,
860 .jim_handler = jim_jtag_arp_init,
861 .help = "Validates JTAG scan chain against the list of "
862 "declared TAPs using just the four standard JTAG "
863 "signals.",
864 },
865 {
866 .name = "arp_init-reset",
867 .mode = COMMAND_ANY,
868 .jim_handler = jim_jtag_arp_init_reset,
869 .help = "Uses TRST and SRST to try resetting everything on "
870 "the JTAG scan chain, then performs 'jtag arp_init'."
871 },
872 {
873 .name = "newtap",
874 .mode = COMMAND_CONFIG,
875 .jim_handler = jim_jtag_newtap,
876 .help = "Create a new TAP instance named basename.tap_type, "
877 "and appends it to the scan chain.",
878 .usage = "basename tap_type '-irlen' count "
879 "['-enable'|'-disable'] "
880 "['-expected_id' number] "
881 "['-ignore-version'] "
882 "['-ircapture' number] "
883 "['-mask' number] ",
884 },
885 {
886 .name = "tapisenabled",
887 .mode = COMMAND_EXEC,
888 .jim_handler = jim_jtag_tap_enabler,
889 .help = "Returns a Tcl boolean (0/1) indicating whether "
890 "the TAP is enabled (1) or not (0).",
891 .usage = "tap_name",
892 },
893 {
894 .name = "tapenable",
895 .mode = COMMAND_EXEC,
896 .jim_handler = jim_jtag_tap_enabler,
897 .help = "Try to enable the specified TAP using the "
898 "'tap-enable' TAP event.",
899 .usage = "tap_name",
900 },
901 {
902 .name = "tapdisable",
903 .mode = COMMAND_EXEC,
904 .jim_handler = jim_jtag_tap_enabler,
905 .help = "Try to disable the specified TAP using the "
906 "'tap-disable' TAP event.",
907 .usage = "tap_name",
908 },
909 {
910 .name = "configure",
911 .mode = COMMAND_EXEC,
912 .jim_handler = jim_jtag_configure,
913 .help = "Provide a Tcl handler for the specified "
914 "TAP event.",
915 .usage = "tap_name '-event' event_name handler",
916 },
917 {
918 .name = "cget",
919 .mode = COMMAND_EXEC,
920 .jim_handler = jim_jtag_configure,
921 .help = "Return any Tcl handler for the specified "
922 "TAP event.",
923 .usage = "tap_name '-event' event_name",
924 },
925 {
926 .name = "names",
927 .mode = COMMAND_ANY,
928 .jim_handler = jim_jtag_names,
929 .help = "Returns list of all JTAG tap names.",
930 },
931 {
932 .chain = jtag_command_handlers_to_move,
933 },
934 COMMAND_REGISTRATION_DONE
935 };
936
937 void jtag_notify_event(enum jtag_event event)
938 {
939 struct jtag_tap *tap;
940
941 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
942 jtag_tap_handle_event(tap, event);
943 }
944
945
946 COMMAND_HANDLER(handle_scan_chain_command)
947 {
948 struct jtag_tap *tap;
949 char expected_id[12];
950
951 tap = jtag_all_taps();
952 command_print(CMD_CTX,
953 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
954 command_print(CMD_CTX,
955 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
956
957 while (tap) {
958 uint32_t expected, expected_mask, ii;
959
960 snprintf(expected_id, sizeof expected_id, "0x%08x",
961 (unsigned)((tap->expected_ids_cnt > 0)
962 ? tap->expected_ids[0]
963 : 0));
964 if (tap->ignore_version)
965 expected_id[2] = '*';
966
967 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
968 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
969
970 command_print(CMD_CTX,
971 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
972 tap->abs_chain_position,
973 tap->dotted_name,
974 tap->enabled ? 'Y' : 'n',
975 (unsigned int)(tap->idcode),
976 expected_id,
977 (unsigned int)(tap->ir_length),
978 (unsigned int)(expected),
979 (unsigned int)(expected_mask));
980
981 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
982 snprintf(expected_id, sizeof expected_id, "0x%08x",
983 (unsigned) tap->expected_ids[1]);
984 if (tap->ignore_version)
985 expected_id[2] = '*';
986
987 command_print(CMD_CTX,
988 " %s",
989 expected_id);
990 }
991
992 tap = tap->next_tap;
993 }
994
995 return ERROR_OK;
996 }
997
998 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
999 {
1000 if (CMD_ARGC > 1)
1001 return ERROR_COMMAND_SYNTAX_ERROR;
1002 if (CMD_ARGC == 1)
1003 {
1004 unsigned delay;
1005 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1006
1007 jtag_set_ntrst_delay(delay);
1008 }
1009 command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1010 return ERROR_OK;
1011 }
1012
1013 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1014 {
1015 if (CMD_ARGC > 1)
1016 return ERROR_COMMAND_SYNTAX_ERROR;
1017 if (CMD_ARGC == 1)
1018 {
1019 unsigned delay;
1020 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1021
1022 jtag_set_ntrst_assert_width(delay);
1023 }
1024 command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1025 return ERROR_OK;
1026 }
1027
1028 COMMAND_HANDLER(handle_jtag_rclk_command)
1029 {
1030 if (CMD_ARGC > 1)
1031 return ERROR_COMMAND_SYNTAX_ERROR;
1032
1033 int retval = ERROR_OK;
1034 if (CMD_ARGC == 1)
1035 {
1036 unsigned khz = 0;
1037 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1038
1039 retval = jtag_config_rclk(khz);
1040 if (ERROR_OK != retval)
1041 return retval;
1042 }
1043
1044 int cur_khz = jtag_get_speed_khz();
1045 retval = jtag_get_speed_readable(&cur_khz);
1046 if (ERROR_OK != retval)
1047 return retval;
1048
1049 if (cur_khz)
1050 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1051 else
1052 command_print(CMD_CTX, "RCLK - adaptive");
1053
1054 return retval;
1055 }
1056
1057 COMMAND_HANDLER(handle_jtag_reset_command)
1058 {
1059 if (CMD_ARGC != 2)
1060 return ERROR_COMMAND_SYNTAX_ERROR;
1061
1062 int trst = -1;
1063 if (CMD_ARGV[0][0] == '1')
1064 trst = 1;
1065 else if (CMD_ARGV[0][0] == '0')
1066 trst = 0;
1067 else
1068 return ERROR_COMMAND_SYNTAX_ERROR;
1069
1070 int srst = -1;
1071 if (CMD_ARGV[1][0] == '1')
1072 srst = 1;
1073 else if (CMD_ARGV[1][0] == '0')
1074 srst = 0;
1075 else
1076 return ERROR_COMMAND_SYNTAX_ERROR;
1077
1078 if (adapter_init(CMD_CTX) != ERROR_OK)
1079 return ERROR_JTAG_INIT_FAILED;
1080
1081 jtag_add_reset(trst, srst);
1082 return jtag_execute_queue();
1083 }
1084
1085 COMMAND_HANDLER(handle_runtest_command)
1086 {
1087 if (CMD_ARGC != 1)
1088 return ERROR_COMMAND_SYNTAX_ERROR;
1089
1090 unsigned num_clocks;
1091 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1092
1093 jtag_add_runtest(num_clocks, TAP_IDLE);
1094 return jtag_execute_queue();
1095 }
1096
1097 /*
1098 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1099 * should be stable ... and *NOT* a shift state, otherwise free-running
1100 * jtag clocks could change the values latched by the update state.
1101 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1102 * and "drscan" commands are a write-only subset of what SVF provides.
1103 */
1104
1105 COMMAND_HANDLER(handle_irscan_command)
1106 {
1107 int i;
1108 struct scan_field *fields;
1109 struct jtag_tap *tap = NULL;
1110 tap_state_t endstate;
1111
1112 if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1113 {
1114 return ERROR_COMMAND_SYNTAX_ERROR;
1115 }
1116
1117 /* optional "-endstate" "statename" at the end of the arguments,
1118 * so that e.g. IRPAUSE can let us load the data register before
1119 * entering RUN/IDLE to execute the instruction we load here.
1120 */
1121 endstate = TAP_IDLE;
1122
1123 if (CMD_ARGC >= 4) {
1124 /* have at least one pair of numbers. */
1125 /* is last pair the magic text? */
1126 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1127 endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1128 if (endstate == TAP_INVALID)
1129 return ERROR_COMMAND_SYNTAX_ERROR;
1130 if (!scan_is_safe(endstate))
1131 LOG_WARNING("unstable irscan endstate \"%s\"",
1132 CMD_ARGV[CMD_ARGC - 1]);
1133 CMD_ARGC -= 2;
1134 }
1135 }
1136
1137 int num_fields = CMD_ARGC / 2;
1138 if (num_fields > 1)
1139 {
1140 /* we really should be looking at plain_ir_scan if we want
1141 * anything more fancy.
1142 */
1143 LOG_ERROR("Specify a single value for tap");
1144 return ERROR_COMMAND_SYNTAX_ERROR;
1145 }
1146
1147 size_t fields_len = sizeof(struct scan_field) * num_fields;
1148 fields = malloc(fields_len);
1149 memset(fields, 0, fields_len);
1150
1151 int retval;
1152 for (i = 0; i < num_fields; i++)
1153 {
1154 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1155 if (tap == NULL)
1156 {
1157 int j;
1158 for (j = 0; j < i; j++)
1159 free((void *)fields[j].out_value);
1160 free(fields);
1161 command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1162
1163 return ERROR_FAIL;
1164 }
1165 int field_size = tap->ir_length;
1166 fields[i].num_bits = field_size;
1167 fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
1168
1169 uint32_t value;
1170 retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
1171 if (ERROR_OK != retval)
1172 goto error_return;
1173 void *v = (void *)fields[i].out_value;
1174 buf_set_u32(v, 0, field_size, value);
1175 fields[i].in_value = NULL;
1176 }
1177
1178 /* did we have an endstate? */
1179 jtag_add_ir_scan(tap, fields, endstate);
1180
1181 retval = jtag_execute_queue();
1182
1183 error_return:
1184 for (i = 0; i < num_fields; i++)
1185 {
1186 if (NULL != fields[i].out_value)
1187 free((void *)fields[i].out_value);
1188 }
1189
1190 free (fields);
1191
1192 return retval;
1193 }
1194
1195
1196 COMMAND_HANDLER(handle_verify_ircapture_command)
1197 {
1198 if (CMD_ARGC > 1)
1199 return ERROR_COMMAND_SYNTAX_ERROR;
1200
1201 if (CMD_ARGC == 1)
1202 {
1203 bool enable;
1204 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1205 jtag_set_verify_capture_ir(enable);
1206 }
1207
1208 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1209 command_print(CMD_CTX, "verify Capture-IR is %s", status);
1210
1211 return ERROR_OK;
1212 }
1213
1214 COMMAND_HANDLER(handle_verify_jtag_command)
1215 {
1216 if (CMD_ARGC > 1)
1217 return ERROR_COMMAND_SYNTAX_ERROR;
1218
1219 if (CMD_ARGC == 1)
1220 {
1221 bool enable;
1222 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1223 jtag_set_verify(enable);
1224 }
1225
1226 const char *status = jtag_will_verify() ? "enabled": "disabled";
1227 command_print(CMD_CTX, "verify jtag capture is %s", status);
1228
1229 return ERROR_OK;
1230 }
1231
1232 COMMAND_HANDLER(handle_tms_sequence_command)
1233 {
1234 if (CMD_ARGC > 1)
1235 return ERROR_COMMAND_SYNTAX_ERROR;
1236
1237 if (CMD_ARGC == 1)
1238 {
1239 bool use_new_table;
1240 if (strcmp(CMD_ARGV[0], "short") == 0)
1241 use_new_table = true;
1242 else if (strcmp(CMD_ARGV[0], "long") == 0)
1243 use_new_table = false;
1244 else
1245 return ERROR_COMMAND_SYNTAX_ERROR;
1246
1247 tap_use_new_tms_table(use_new_table);
1248 }
1249
1250 command_print(CMD_CTX, "tms sequence is %s",
1251 tap_uses_new_tms_table() ? "short": "long");
1252
1253 return ERROR_OK;
1254 }
1255
1256 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1257 {
1258 if (CMD_ARGC != 1)
1259 return ERROR_COMMAND_SYNTAX_ERROR;
1260
1261 int sleep_ms;
1262 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1263
1264 jtag_set_flush_queue_sleep(sleep_ms);
1265
1266 return ERROR_OK;
1267 }
1268
1269
1270
1271 static const struct command_registration jtag_command_handlers[] = {
1272
1273 {
1274 .name = "jtag_flush_queue_sleep",
1275 .handler = handle_jtag_flush_queue_sleep,
1276 .mode = COMMAND_ANY,
1277 .help = "For debug purposes(simulate long delays of interface) "
1278 "to test performance or change in behavior. Default 0ms.",
1279 .usage = "[sleep in ms]",
1280 },
1281 {
1282 .name = "jtag_rclk",
1283 .handler = handle_jtag_rclk_command,
1284 .mode = COMMAND_ANY,
1285 .help = "With an argument, change to to use adaptive clocking "
1286 "if possible; else to use the fallback speed. "
1287 "With or without argument, display current setting.",
1288 .usage = "[fallback_speed_khz]",
1289 },
1290 {
1291 .name = "jtag_ntrst_delay",
1292 .handler = handle_jtag_ntrst_delay_command,
1293 .mode = COMMAND_ANY,
1294 .help = "delay after deasserting trst in ms",
1295 .usage = "[milliseconds]",
1296 },
1297 {
1298 .name = "jtag_ntrst_assert_width",
1299 .handler = handle_jtag_ntrst_assert_width_command,
1300 .mode = COMMAND_ANY,
1301 .help = "delay after asserting trst in ms",
1302 .usage = "[milliseconds]",
1303 },
1304 {
1305 .name = "scan_chain",
1306 .handler = handle_scan_chain_command,
1307 .mode = COMMAND_ANY,
1308 .help = "print current scan chain configuration",
1309 },
1310 {
1311 .name = "jtag_reset",
1312 .handler = handle_jtag_reset_command,
1313 .mode = COMMAND_EXEC,
1314 .help = "Set reset line values. Value '1' is active, "
1315 "value '0' is inactive.",
1316 .usage = "trst_active srst_active",
1317 },
1318 {
1319 .name = "runtest",
1320 .handler = handle_runtest_command,
1321 .mode = COMMAND_EXEC,
1322 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1323 .usage = "num_cycles"
1324 },
1325 {
1326 .name = "irscan",
1327 .handler = handle_irscan_command,
1328 .mode = COMMAND_EXEC,
1329 .help = "Execute Instruction Register (DR) scan. The "
1330 "specified opcodes are put into each TAP's IR, "
1331 "and other TAPs are put in BYPASS.",
1332 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1333 },
1334 {
1335 .name = "verify_ircapture",
1336 .handler = handle_verify_ircapture_command,
1337 .mode = COMMAND_ANY,
1338 .help = "Display or assign flag controlling whether to "
1339 "verify values captured during Capture-IR.",
1340 .usage = "['enable'|'disable']",
1341 },
1342 {
1343 .name = "verify_jtag",
1344 .handler = handle_verify_jtag_command,
1345 .mode = COMMAND_ANY,
1346 .help = "Display or assign flag controlling whether to "
1347 "verify values captured during IR and DR scans.",
1348 .usage = "['enable'|'disable']",
1349 },
1350 {
1351 .name = "tms_sequence",
1352 .handler = handle_tms_sequence_command,
1353 .mode = COMMAND_ANY,
1354 .help = "Display or change what style TMS sequences to use "
1355 "for JTAG state transitions: short (default) or "
1356 "long. Only for working around JTAG bugs.",
1357 /* Specifically for working around DRIVER bugs... */
1358 .usage = "['short'|'long']",
1359 },
1360 {
1361 .name = "jtag",
1362 .mode = COMMAND_ANY,
1363 .help = "perform jtag tap actions",
1364
1365 .chain = jtag_subcommand_handlers,
1366 },
1367 {
1368 .chain = jtag_command_handlers_to_move,
1369 },
1370 COMMAND_REGISTRATION_DONE
1371 };
1372
1373 int jtag_register_commands(struct command_context *cmd_ctx)
1374 {
1375 return register_commands(cmd_ctx, NULL, jtag_command_handlers);
1376 }