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