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