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