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