5056a5c5d58ac10acdd9438f65a72628979d94ae
[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 int retval = parse_uint(args[0], &delay);
1054 if (ERROR_OK != retval)
1055 return retval;
1056 jtag_set_nsrst_delay(delay);
1057 }
1058 command_print(cmd_ctx, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
1059 return ERROR_OK;
1060 }
1061
1062 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx,
1063 char *cmd, char **args, int argc)
1064 {
1065 if (argc > 1)
1066 return ERROR_COMMAND_SYNTAX_ERROR;
1067 if (argc == 1)
1068 {
1069 unsigned delay;
1070 int retval = parse_uint(args[0], &delay);
1071 if (ERROR_OK != retval)
1072 return retval;
1073 jtag_set_ntrst_delay(delay);
1074 }
1075 command_print(cmd_ctx, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1076 return ERROR_OK;
1077 }
1078
1079 static int handle_jtag_nsrst_assert_width_command(struct command_context_s *cmd_ctx,
1080 char *cmd, char **args, int argc)
1081 {
1082 if (argc > 1)
1083 return ERROR_COMMAND_SYNTAX_ERROR;
1084 if (argc == 1)
1085 {
1086 unsigned delay;
1087 int retval = parse_uint(args[0], &delay);
1088 if (ERROR_OK != retval)
1089 return retval;
1090 jtag_set_nsrst_assert_width(delay);
1091 }
1092 command_print(cmd_ctx, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
1093 return ERROR_OK;
1094 }
1095
1096 static int handle_jtag_ntrst_assert_width_command(struct command_context_s *cmd_ctx,
1097 char *cmd, char **args, int argc)
1098 {
1099 if (argc > 1)
1100 return ERROR_COMMAND_SYNTAX_ERROR;
1101 if (argc == 1)
1102 {
1103 unsigned delay;
1104 int retval = parse_uint(args[0], &delay);
1105 if (ERROR_OK != retval)
1106 return retval;
1107 jtag_set_ntrst_assert_width(delay);
1108 }
1109 command_print(cmd_ctx, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1110 return ERROR_OK;
1111 }
1112
1113 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1114 {
1115 int retval = ERROR_OK;
1116
1117 command_print(cmd_ctx, "OLD SYNTAX: DEPRECATED - "
1118 "use jtag_khz, not jtag_speed");
1119
1120 if (argc > 1)
1121 return ERROR_COMMAND_SYNTAX_ERROR;
1122 if (argc == 1)
1123 {
1124 LOG_DEBUG("handle jtag speed");
1125
1126 unsigned cur_speed = 0;
1127 int retval = parse_uint(args[0], &cur_speed);
1128 if (ERROR_OK != retval)
1129 return retval;
1130 retval = jtag_config_speed(cur_speed);
1131
1132 }
1133 command_print(cmd_ctx, "jtag_speed: %d", jtag_get_speed());
1134
1135 return retval;
1136 }
1137
1138 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1139 {
1140 if (argc > 1)
1141 return ERROR_COMMAND_SYNTAX_ERROR;
1142
1143 int retval = ERROR_OK;
1144 if (argc == 1)
1145 {
1146 unsigned khz = 0;
1147 int retval = parse_uint(args[0], &khz);
1148 if (ERROR_OK != retval)
1149 return retval;
1150 retval = jtag_config_khz(khz);
1151 if (ERROR_OK != retval)
1152 return retval;
1153 }
1154
1155 int cur_speed = jtag_get_speed_khz();
1156 retval = jtag_get_speed_readable(&cur_speed);
1157 if (ERROR_OK != retval)
1158 return retval;
1159
1160 if (cur_speed)
1161 command_print(cmd_ctx, "%d kHz", cur_speed);
1162 else
1163 command_print(cmd_ctx, "RCLK - adaptive");
1164
1165 return retval;
1166 }
1167
1168 static int handle_jtag_rclk_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1169 {
1170 if (argc > 1)
1171 return ERROR_COMMAND_SYNTAX_ERROR;
1172
1173 int retval = ERROR_OK;
1174 if (argc == 1)
1175 {
1176 unsigned khz = 0;
1177 int retval = parse_uint(args[0], &khz);
1178 if (ERROR_OK != retval)
1179 return retval;
1180 retval = jtag_config_rclk(khz);
1181 if (ERROR_OK != retval)
1182 return retval;
1183 }
1184
1185 int cur_khz = jtag_get_speed_khz();
1186 retval = jtag_get_speed_readable(&cur_khz);
1187 if (ERROR_OK != retval)
1188 return retval;
1189
1190 if (cur_khz)
1191 command_print(cmd_ctx, "RCLK not supported - fallback to %d kHz", cur_khz);
1192 else
1193 command_print(cmd_ctx, "RCLK - adaptive");
1194
1195 return retval;
1196 }
1197
1198 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx,
1199 char *cmd, char **args, int argc)
1200 {
1201 if (argc != 2)
1202 return ERROR_COMMAND_SYNTAX_ERROR;
1203
1204 int trst = -1;
1205 if (args[0][0] == '1')
1206 trst = 1;
1207 else if (args[0][0] == '0')
1208 trst = 0;
1209 else
1210 return ERROR_COMMAND_SYNTAX_ERROR;
1211
1212 int srst = -1;
1213 if (args[1][0] == '1')
1214 srst = 1;
1215 else if (args[1][0] == '0')
1216 srst = 0;
1217 else
1218 return ERROR_COMMAND_SYNTAX_ERROR;
1219
1220 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
1221 return ERROR_JTAG_INIT_FAILED;
1222
1223 jtag_add_reset(trst, srst);
1224 return jtag_execute_queue();
1225 }
1226
1227 static int handle_runtest_command(struct command_context_s *cmd_ctx,
1228 char *cmd, char **args, int argc)
1229 {
1230 if (argc != 1)
1231 return ERROR_COMMAND_SYNTAX_ERROR;
1232
1233 unsigned num_clocks;
1234 int retval = parse_uint(args[0], &num_clocks);
1235 if (ERROR_OK != retval)
1236 return retval;
1237
1238 jtag_add_runtest(num_clocks, TAP_IDLE);
1239 return jtag_execute_queue();
1240 }
1241
1242 /*
1243 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1244 * should be stable ... and *NOT* a shift state, otherwise free-running
1245 * jtag clocks could change the values latched by the update state.
1246 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1247 * and "drscan" commands are a write-only subset of what SVF provides.
1248 */
1249 static bool scan_is_safe(tap_state_t state)
1250 {
1251 switch (state)
1252 {
1253 case TAP_RESET:
1254 case TAP_IDLE:
1255 case TAP_DRPAUSE:
1256 case TAP_IRPAUSE:
1257 return true;
1258 default:
1259 return false;
1260 }
1261 }
1262
1263
1264 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1265 {
1266 int i;
1267 scan_field_t *fields;
1268 jtag_tap_t *tap;
1269 tap_state_t endstate;
1270
1271 if ((argc < 2) || (argc % 2))
1272 {
1273 return ERROR_COMMAND_SYNTAX_ERROR;
1274 }
1275
1276 /* optional "-endstate" "statename" at the end of the arguments,
1277 * so that e.g. IRPAUSE can let us load the data register before
1278 * entering RUN/IDLE to execute the instruction we load here.
1279 */
1280 endstate = TAP_IDLE;
1281
1282 if (argc >= 4) {
1283 /* have at least one pair of numbers. */
1284 /* is last pair the magic text? */
1285 if (strcmp("-endstate", args[argc - 2]) == 0) {
1286 endstate = tap_state_by_name(args[argc - 1]);
1287 if (endstate == TAP_INVALID)
1288 return ERROR_COMMAND_SYNTAX_ERROR;
1289 if (!scan_is_safe(endstate))
1290 LOG_WARNING("unstable irscan endstate \"%s\"",
1291 args[argc - 1]);
1292 argc -= 2;
1293 }
1294 }
1295
1296 int num_fields = argc / 2;
1297 size_t fields_len = sizeof(scan_field_t) * num_fields;
1298 fields = malloc(fields_len);
1299 memset(fields, 0, fields_len);
1300
1301 int retval;
1302 for (i = 0; i < num_fields; i++)
1303 {
1304 tap = jtag_tap_by_string(args[i*2]);
1305 if (tap == NULL)
1306 {
1307 int j;
1308 for (j = 0; j < i; j++)
1309 free(fields[j].out_value);
1310 free(fields);
1311 command_print(cmd_ctx, "Tap: %s unknown", args[i*2]);
1312
1313 return ERROR_FAIL;
1314 }
1315 int field_size = tap->ir_length;
1316 fields[i].tap = tap;
1317 fields[i].num_bits = field_size;
1318 fields[i].out_value = malloc(CEIL(field_size, 8));
1319
1320 uint32_t value;
1321 retval = parse_u32(args[i * 2 + 1], &value);
1322 if (ERROR_OK != retval)
1323 goto error_return;
1324 buf_set_u32(fields[i].out_value, 0, field_size, value);
1325 fields[i].in_value = NULL;
1326 }
1327
1328 /* did we have an endstate? */
1329 jtag_add_ir_scan(num_fields, fields, endstate);
1330
1331 retval = jtag_execute_queue();
1332
1333 error_return:
1334 for (i = 0; i < num_fields; i++)
1335 {
1336 if (NULL != fields[i].out_value)
1337 free(fields[i].out_value);
1338 }
1339
1340 free (fields);
1341
1342 return retval;
1343 }
1344
1345 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1346 {
1347 int retval;
1348 scan_field_t *fields;
1349 int num_fields;
1350 int field_count = 0;
1351 int i, e;
1352 jtag_tap_t *tap;
1353 tap_state_t endstate;
1354
1355 /* args[1] = device
1356 * args[2] = num_bits
1357 * args[3] = hex string
1358 * ... repeat num bits and hex string ...
1359 *
1360 * .. optionally:
1361 * args[N-2] = "-endstate"
1362 * args[N-1] = statename
1363 */
1364 if ((argc < 4) || ((argc % 2) != 0))
1365 {
1366 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1367 return JIM_ERR;
1368 }
1369
1370 endstate = TAP_IDLE;
1371
1372 script_debug(interp, "drscan", argc, args);
1373
1374 /* validate arguments as numbers */
1375 e = JIM_OK;
1376 for (i = 2; i < argc; i += 2)
1377 {
1378 long bits;
1379 const char *cp;
1380
1381 e = Jim_GetLong(interp, args[i], &bits);
1382 /* If valid - try next arg */
1383 if (e == JIM_OK) {
1384 continue;
1385 }
1386
1387 /* Not valid.. are we at the end? */
1388 if (((i + 2) != argc)) {
1389 /* nope, then error */
1390 return e;
1391 }
1392
1393 /* it could be: "-endstate FOO"
1394 * e.g. DRPAUSE so we can issue more instructions
1395 * before entering RUN/IDLE and executing them.
1396 */
1397
1398 /* get arg as a string. */
1399 cp = Jim_GetString(args[i], NULL);
1400 /* is it the magic? */
1401 if (0 == strcmp("-endstate", cp)) {
1402 /* is the statename valid? */
1403 cp = Jim_GetString(args[i + 1], NULL);
1404
1405 /* see if it is a valid state name */
1406 endstate = tap_state_by_name(cp);
1407 if (endstate < 0) {
1408 /* update the error message */
1409 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1410 } else {
1411 if (!scan_is_safe(endstate))
1412 LOG_WARNING("drscan with unsafe "
1413 "endstate \"%s\"", cp);
1414
1415 /* valid - so clear the error */
1416 e = JIM_OK;
1417 /* and remove the last 2 args */
1418 argc -= 2;
1419 }
1420 }
1421
1422 /* Still an error? */
1423 if (e != JIM_OK) {
1424 return e; /* too bad */
1425 }
1426 } /* validate args */
1427
1428 tap = jtag_tap_by_jim_obj(interp, args[1]);
1429 if (tap == NULL) {
1430 return JIM_ERR;
1431 }
1432
1433 num_fields = (argc-2)/2;
1434 fields = malloc(sizeof(scan_field_t) * num_fields);
1435 for (i = 2; i < argc; i += 2)
1436 {
1437 long bits;
1438 int len;
1439 const char *str;
1440
1441 Jim_GetLong(interp, args[i], &bits);
1442 str = Jim_GetString(args[i + 1], &len);
1443
1444 fields[field_count].tap = tap;
1445 fields[field_count].num_bits = bits;
1446 fields[field_count].out_value = malloc(CEIL(bits, 8));
1447 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
1448 fields[field_count].in_value = fields[field_count].out_value;
1449 field_count++;
1450 }
1451
1452 jtag_add_dr_scan(num_fields, fields, endstate);
1453
1454 retval = jtag_execute_queue();
1455 if (retval != ERROR_OK)
1456 {
1457 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
1458 return JIM_ERR;
1459 }
1460
1461 field_count = 0;
1462 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
1463 for (i = 2; i < argc; i += 2)
1464 {
1465 long bits;
1466 char *str;
1467
1468 Jim_GetLong(interp, args[i], &bits);
1469 str = buf_to_str(fields[field_count].in_value, bits, 16);
1470 free(fields[field_count].out_value);
1471
1472 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
1473 free(str);
1474 field_count++;
1475 }
1476
1477 Jim_SetResult(interp, list);
1478
1479 free(fields);
1480
1481 return JIM_OK;
1482 }
1483
1484
1485 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1486 {
1487 tap_state_t states[8];
1488
1489 if ((argc < 2) || ((size_t)argc > (sizeof(states)/sizeof(*states) + 1)))
1490 {
1491 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1492 return JIM_ERR;
1493 }
1494
1495 script_debug(interp, "pathmove", argc, args);
1496
1497 int i;
1498 for (i = 0; i < argc-1; i++)
1499 {
1500 const char *cp;
1501 cp = Jim_GetString(args[i + 1], NULL);
1502 states[i] = tap_state_by_name(cp);
1503 if (states[i] < 0)
1504 {
1505 /* update the error message */
1506 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1507 return JIM_ERR;
1508 }
1509 }
1510
1511 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
1512 {
1513 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
1514 return JIM_ERR;
1515 }
1516
1517 jtag_add_pathmove(argc-2, states + 1);
1518
1519 if (jtag_execute_queue()!= ERROR_OK)
1520 {
1521 Jim_SetResultString(interp, "pathmove: failed",-1);
1522 return JIM_ERR;
1523 }
1524
1525 return JIM_OK;
1526 }
1527
1528
1529 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1530 {
1531 script_debug(interp, "flush_count", argc, args);
1532
1533 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
1534
1535 return JIM_OK;
1536 }
1537
1538
1539 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1540 {
1541 if (argc > 1)
1542 return ERROR_COMMAND_SYNTAX_ERROR;
1543
1544 if (argc == 1)
1545 {
1546 if (strcmp(args[0], "enable") == 0)
1547 jtag_set_verify_capture_ir(true);
1548 else if (strcmp(args[0], "disable") == 0)
1549 jtag_set_verify_capture_ir(false);
1550 else
1551 return ERROR_COMMAND_SYNTAX_ERROR;
1552 }
1553
1554 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1555 command_print(cmd_ctx, "verify Capture-IR is %s", status);
1556
1557 return ERROR_OK;
1558 }
1559
1560 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1561 {
1562 if (argc > 1)
1563 return ERROR_COMMAND_SYNTAX_ERROR;
1564
1565 if (argc == 1)
1566 {
1567 if (strcmp(args[0], "enable") == 0)
1568 jtag_set_verify(true);
1569 else if (strcmp(args[0], "disable") == 0)
1570 jtag_set_verify(false);
1571 else
1572 return ERROR_COMMAND_SYNTAX_ERROR;
1573 }
1574
1575 const char *status = jtag_will_verify() ? "enabled": "disabled";
1576 command_print(cmd_ctx, "verify jtag capture is %s", status);
1577
1578 return ERROR_OK;
1579 }
1580
1581 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1582 {
1583 if (argc > 1)
1584 return ERROR_COMMAND_SYNTAX_ERROR;
1585
1586 if (argc == 1)
1587 {
1588 bool use_new_table;
1589 if (strcmp(args[0], "short") == 0)
1590 use_new_table = true;
1591 else if (strcmp(args[0], "long") == 0)
1592 use_new_table = false;
1593 else
1594 return ERROR_COMMAND_SYNTAX_ERROR;
1595
1596 tap_use_new_tms_table(use_new_table);
1597 }
1598
1599 command_print(cmd_ctx, "tms sequence is %s",
1600 tap_uses_new_tms_table() ? "short": "long");
1601
1602 return ERROR_OK;
1603 }