Merge branch 'master' of ssh://dbrownell@openocd.git.sourceforge.net/gitroot/openocd...
[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 int reqbits;
201 Jim_Nvp *n;
202 char *cp;
203 const Jim_Nvp opts[] = {
204 #define NTAP_OPT_IRLEN 0
205 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
206 #define NTAP_OPT_IRMASK 1
207 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
208 #define NTAP_OPT_IRCAPTURE 2
209 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
210 #define NTAP_OPT_ENABLED 3
211 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
212 #define NTAP_OPT_DISABLED 4
213 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
214 #define NTAP_OPT_EXPECTED_ID 5
215 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
216 { .name = NULL , .value = -1 },
217 };
218
219 pTap = calloc(1, sizeof(jtag_tap_t));
220 if (!pTap) {
221 Jim_SetResult_sprintf(goi->interp, "no memory");
222 return JIM_ERR;
223 }
224
225 /*
226 * we expect CHIP + TAP + OPTIONS
227 * */
228 if (goi->argc < 3) {
229 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
230 free(pTap);
231 return JIM_ERR;
232 }
233 Jim_GetOpt_String(goi, &cp, NULL);
234 pTap->chip = strdup(cp);
235
236 Jim_GetOpt_String(goi, &cp, NULL);
237 pTap->tapname = strdup(cp);
238
239 /* name + dot + name + null */
240 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
241 cp = malloc(x);
242 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
243 pTap->dotted_name = cp;
244
245 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
246 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
247
248 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
249 * that the default. The "-irlen" and "-irmask" options are only
250 * needed to cope with nonstandard TAPs, or to specify more bits.
251 */
252 pTap->ir_capture_mask = 0x03;
253 pTap->ir_capture_value = 0x01;
254
255 /* clear flags for "required options" them as we find them */
256 reqbits = 1;
257
258 while (goi->argc) {
259 e = Jim_GetOpt_Nvp(goi, opts, &n);
260 if (e != JIM_OK) {
261 Jim_GetOpt_NvpUnknown(goi, opts, 0);
262 free((void *)pTap->dotted_name);
263 free(pTap);
264 return e;
265 }
266 LOG_DEBUG("Processing option: %s", n->name);
267 switch (n->value) {
268 case NTAP_OPT_ENABLED:
269 pTap->disabled_after_reset = false;
270 break;
271 case NTAP_OPT_DISABLED:
272 pTap->disabled_after_reset = true;
273 break;
274 case NTAP_OPT_EXPECTED_ID:
275 {
276 uint32_t *new_expected_ids;
277
278 e = Jim_GetOpt_Wide(goi, &w);
279 if (e != JIM_OK) {
280 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
281 free((void *)pTap->dotted_name);
282 free(pTap);
283 return e;
284 }
285
286 new_expected_ids = malloc(sizeof(uint32_t) * (pTap->expected_ids_cnt + 1));
287 if (new_expected_ids == NULL) {
288 Jim_SetResult_sprintf(goi->interp, "no memory");
289 free((void *)pTap->dotted_name);
290 free(pTap);
291 return JIM_ERR;
292 }
293
294 memcpy(new_expected_ids, pTap->expected_ids, sizeof(uint32_t) * pTap->expected_ids_cnt);
295
296 new_expected_ids[pTap->expected_ids_cnt] = w;
297
298 free(pTap->expected_ids);
299 pTap->expected_ids = new_expected_ids;
300 pTap->expected_ids_cnt++;
301 break;
302 }
303 case NTAP_OPT_IRLEN:
304 case NTAP_OPT_IRMASK:
305 case NTAP_OPT_IRCAPTURE:
306 e = Jim_GetOpt_Wide(goi, &w);
307 if (e != JIM_OK) {
308 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
309 free((void *)pTap->dotted_name);
310 free(pTap);
311 return e;
312 }
313 switch (n->value) {
314 case NTAP_OPT_IRLEN:
315 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
316 LOG_WARNING("%s: huge IR length %d",
317 pTap->dotted_name,
318 (int) w);
319 pTap->ir_length = w;
320 reqbits = 0;
321 break;
322 case NTAP_OPT_IRMASK:
323 if (is_bad_irval(pTap->ir_length, w)) {
324 LOG_ERROR("%s: IR mask %x too big",
325 pTap->dotted_name,
326 (int) w);
327 free((void *)pTap->dotted_name);
328 free(pTap);
329 return ERROR_FAIL;
330 }
331 if ((w & 3) != 3)
332 LOG_WARNING("%s: nonstandard IR mask",
333 pTap->dotted_name);
334 pTap->ir_capture_mask = w;
335 break;
336 case NTAP_OPT_IRCAPTURE:
337 if (is_bad_irval(pTap->ir_length, w)) {
338 LOG_ERROR("%s: IR capture %x too big",
339 pTap->dotted_name,
340 (int) w);
341 free((void *)pTap->dotted_name);
342 free(pTap);
343 return ERROR_FAIL;
344 }
345 if ((w & 3) != 1)
346 LOG_WARNING("%s: nonstandard IR value",
347 pTap->dotted_name);
348 pTap->ir_capture_value = w;
349 break;
350 }
351 } /* switch (n->value) */
352 } /* while (goi->argc) */
353
354 /* default is enabled-after-reset */
355 pTap->enabled = !pTap->disabled_after_reset;
356
357 /* Did all the required option bits get cleared? */
358 if (0 == reqbits)
359 {
360 jtag_tap_init(pTap);
361 return ERROR_OK;
362 }
363
364 Jim_SetResult_sprintf(goi->interp,
365 "newtap: %s missing required parameters",
366 pTap->dotted_name);
367 jtag_tap_free(pTap);
368 return JIM_ERR;
369 }
370
371 static void jtag_tap_handle_event(jtag_tap_t *tap, enum jtag_event e)
372 {
373 jtag_tap_event_action_t * jteap;
374
375 for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next) {
376 if (jteap->event == e) {
377 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
378 tap->dotted_name,
379 e,
380 Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
381 Jim_GetString(jteap->body, NULL));
382 if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
383 Jim_PrintErrorMessage(interp);
384 } else switch (e) {
385 case JTAG_TAP_EVENT_ENABLE:
386 case JTAG_TAP_EVENT_DISABLE:
387 /* NOTE: we currently assume the handlers
388 * can't fail. Right here is where we should
389 * really be verifying the scan chains ...
390 */
391 tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
392 LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
393 tap->enabled ? "enabled" : "disabled");
394 break;
395 default:
396 break;
397 }
398 }
399 }
400 }
401
402
403 static int jim_jtag_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
404 {
405 Jim_GetOptInfo goi;
406 int e;
407 Jim_Nvp *n;
408 Jim_Obj *o;
409 struct command_context_s *context;
410
411 enum {
412 JTAG_CMD_INTERFACE,
413 JTAG_CMD_INIT,
414 JTAG_CMD_INIT_RESET,
415 JTAG_CMD_NEWTAP,
416 JTAG_CMD_TAPENABLE,
417 JTAG_CMD_TAPDISABLE,
418 JTAG_CMD_TAPISENABLED,
419 JTAG_CMD_CONFIGURE,
420 JTAG_CMD_CGET,
421 JTAG_CMD_NAMES,
422 };
423
424 const Jim_Nvp jtag_cmds[] = {
425 { .name = "interface" , .value = JTAG_CMD_INTERFACE },
426 { .name = "arp_init" , .value = JTAG_CMD_INIT },
427 { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
428 { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
429 { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
430 { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
431 { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
432 { .name = "configure" , .value = JTAG_CMD_CONFIGURE },
433 { .name = "cget" , .value = JTAG_CMD_CGET },
434 { .name = "names" , .value = JTAG_CMD_NAMES },
435
436 { .name = NULL, .value = -1 },
437 };
438
439 context = Jim_GetAssocData(interp, "context");
440 /* go past the command */
441 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
442
443 e = Jim_GetOpt_Nvp(&goi, jtag_cmds, &n);
444 if (e != JIM_OK) {
445 Jim_GetOpt_NvpUnknown(&goi, jtag_cmds, 0);
446 return e;
447 }
448 Jim_SetEmptyResult(goi.interp);
449 switch (n->value) {
450 case JTAG_CMD_INTERFACE:
451 /* return the name of the interface */
452 /* TCL code might need to know the exact type... */
453 /* FUTURE: we allow this as a means to "set" the interface. */
454 if (goi.argc != 0) {
455 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
456 return JIM_ERR;
457 }
458 Jim_SetResultString(goi.interp, jtag_interface->name, -1);
459 return JIM_OK;
460 case JTAG_CMD_INIT:
461 if (goi.argc != 0) {
462 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
463 return JIM_ERR;
464 }
465 e = jtag_init_inner(context);
466 if (e != ERROR_OK) {
467 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
468 return JIM_ERR;
469 }
470 return JIM_OK;
471 case JTAG_CMD_INIT_RESET:
472 if (goi.argc != 0) {
473 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
474 return JIM_ERR;
475 }
476 e = jtag_init_reset(context);
477 if (e != ERROR_OK) {
478 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
479 return JIM_ERR;
480 }
481 return JIM_OK;
482 case JTAG_CMD_NEWTAP:
483 return jim_newtap_cmd(&goi);
484 break;
485 case JTAG_CMD_TAPISENABLED:
486 case JTAG_CMD_TAPENABLE:
487 case JTAG_CMD_TAPDISABLE:
488 if (goi.argc != 1) {
489 Jim_SetResultString(goi.interp, "Too many parameters",-1);
490 return JIM_ERR;
491 }
492
493 {
494 jtag_tap_t *t;
495
496 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
497 if (t == NULL)
498 return JIM_ERR;
499
500 switch (n->value) {
501 case JTAG_CMD_TAPISENABLED:
502 break;
503 case JTAG_CMD_TAPENABLE:
504 if (t->enabled)
505 break;
506 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
507 if (!t->enabled)
508 break;
509
510 /* FIXME add JTAG sanity checks, w/o TLR
511 * - scan chain length grew by one (this)
512 * - IDs and IR lengths are as expected
513 */
514
515 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
516 break;
517 case JTAG_CMD_TAPDISABLE:
518 if (!t->enabled)
519 break;
520 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
521 if (t->enabled)
522 break;
523
524 /* FIXME add JTAG sanity checks, w/o TLR
525 * - scan chain length shrank by one (this)
526 * - IDs and IR lengths are as expected
527 */
528
529 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
530 break;
531 }
532 e = t->enabled;
533 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
534 return JIM_OK;
535 }
536 break;
537
538 case JTAG_CMD_CGET:
539 if (goi.argc < 2) {
540 Jim_WrongNumArgs(goi.interp, 0, NULL,
541 "cget tap_name queryparm");
542 return JIM_ERR;
543 }
544
545 {
546 jtag_tap_t *t;
547
548 Jim_GetOpt_Obj(&goi, &o);
549 t = jtag_tap_by_jim_obj(goi.interp, o);
550 if (t == NULL) {
551 return JIM_ERR;
552 }
553
554 goi.isconfigure = 0;
555 return jtag_tap_configure_cmd(&goi, t);
556 }
557 break;
558
559 case JTAG_CMD_CONFIGURE:
560 if (goi.argc < 3) {
561 Jim_WrongNumArgs(goi.interp, 0, NULL,
562 "configure tap_name attribute value ...");
563 return JIM_ERR;
564 }
565
566 {
567 jtag_tap_t *t;
568
569 Jim_GetOpt_Obj(&goi, &o);
570 t = jtag_tap_by_jim_obj(goi.interp, o);
571 if (t == NULL) {
572 return JIM_ERR;
573 }
574
575 goi.isconfigure = 1;
576 return jtag_tap_configure_cmd(&goi, t);
577 }
578 break;
579
580 case JTAG_CMD_NAMES:
581 if (goi.argc != 0) {
582 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
583 return JIM_ERR;
584 }
585 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
586 {
587 jtag_tap_t *tap;
588
589 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
590 Jim_ListAppendElement(goi.interp,
591 Jim_GetResult(goi.interp),
592 Jim_NewStringObj(goi.interp,
593 tap->dotted_name, -1));
594 }
595 return JIM_OK;
596 }
597 break;
598
599 }
600
601 return JIM_ERR;
602 }
603
604
605 void jtag_notify_event(enum jtag_event event)
606 {
607 jtag_tap_t *tap;
608
609 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
610 jtag_tap_handle_event(tap, event);
611 }
612
613
614 int jtag_register_commands(struct command_context_s *cmd_ctx)
615 {
616 register_jim(cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
617
618 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
619 COMMAND_CONFIG, "try to configure interface");
620 register_command(cmd_ctx, NULL,
621 "interface_list", &handle_interface_list_command,
622 COMMAND_ANY, "list all built-in interfaces");
623 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
624 COMMAND_ANY, "(DEPRECATED) set jtag speed (if supported)");
625 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
626 COMMAND_ANY, "set maximum jtag speed (if supported); "
627 "parameter is maximum khz, or 0 for adaptive clocking (RTCK).");
628 register_command(cmd_ctx, NULL, "jtag_rclk", handle_jtag_rclk_command,
629 COMMAND_ANY, "fallback_speed_khz - set JTAG speed to RCLK or use fallback speed");
630 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
631 COMMAND_CONFIG, "(DEPRECATED) jtag_device <ir_length> <ir_expected> <ir_mask>");
632 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
633 COMMAND_ANY,
634 "[none/trst_only/srst_only/trst_and_srst] [srst_pulls_trst/trst_pulls_srst] [combined/separate] [trst_push_pull/trst_open_drain] [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 if (argc < 1)
855 return ERROR_COMMAND_SYNTAX_ERROR;
856
857 /* Original versions cared about the order of these tokens:
858 * reset_config signals [combination [trst_type [srst_type]]]
859 * They also clobbered the previous configuration even on error.
860 *
861 * Here we don't care about the order, and only change values
862 * which have been explicitly specified.
863 */
864 for (; argc; argc--, args++) {
865 int tmp = 0;
866 int m;
867
868 m = RESET_SRST_GATES_JTAG;
869 tmp = 0;
870 if (strcmp(*args, "srst_gates_jtag") == 0)
871 {
872 tmp = RESET_SRST_GATES_JTAG;
873 goto next;
874 }
875
876 /* signals */
877 m = RESET_HAS_TRST | RESET_HAS_SRST;
878 if (strcmp(*args, "none") == 0)
879 tmp = RESET_NONE;
880 else if (strcmp(*args, "trst_only") == 0)
881 tmp = RESET_HAS_TRST;
882 else if (strcmp(*args, "srst_only") == 0)
883 tmp = RESET_HAS_SRST;
884 else if (strcmp(*args, "trst_and_srst") == 0)
885 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
886 else
887 m = 0;
888 if (mask & m) {
889 LOG_ERROR("extra reset_config %s spec (%s)",
890 "signal", *args);
891 return ERROR_INVALID_ARGUMENTS;
892 }
893 if (m)
894 goto next;
895
896 /* combination (options for broken wiring) */
897 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
898 if (strcmp(*args, "separate") == 0)
899 /* separate reset lines - default */;
900 else if (strcmp(*args, "srst_pulls_trst") == 0)
901 tmp |= RESET_SRST_PULLS_TRST;
902 else if (strcmp(*args, "trst_pulls_srst") == 0)
903 tmp |= RESET_TRST_PULLS_SRST;
904 else if (strcmp(*args, "combined") == 0)
905 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
906 else
907 m = 0;
908 if (mask & m) {
909 LOG_ERROR("extra reset_config %s spec (%s)",
910 "combination", *args);
911 return ERROR_INVALID_ARGUMENTS;
912 }
913 if (m)
914 goto next;
915
916 /* trst_type (NOP without HAS_TRST) */
917 m = RESET_TRST_OPEN_DRAIN;
918 if (strcmp(*args, "trst_open_drain") == 0)
919 tmp |= RESET_TRST_OPEN_DRAIN;
920 else if (strcmp(*args, "trst_push_pull") == 0)
921 /* push/pull from adapter - default */;
922 else
923 m = 0;
924 if (mask & m) {
925 LOG_ERROR("extra reset_config %s spec (%s)",
926 "trst_type", *args);
927 return ERROR_INVALID_ARGUMENTS;
928 }
929 if (m)
930 goto next;
931
932 /* srst_type (NOP without HAS_SRST) */
933 m |= RESET_SRST_PUSH_PULL;
934 if (strcmp(*args, "srst_push_pull") == 0)
935 tmp |= RESET_SRST_PUSH_PULL;
936 else if (strcmp(*args, "srst_open_drain") == 0)
937 /* open drain from adapter - default */;
938 else
939 m = 0;
940 if (mask & m) {
941 LOG_ERROR("extra reset_config %s spec (%s)",
942 "srst_type", *args);
943 return ERROR_INVALID_ARGUMENTS;
944 }
945 if (m)
946 goto next;
947
948 /* caller provided nonsense; fail */
949 LOG_ERROR("unknown reset_config flag (%s)", *args);
950 return ERROR_INVALID_ARGUMENTS;
951
952 next:
953 /* Remember the bits which were specified (mask)
954 * and their new values (new_cfg).
955 */
956 mask |= m;
957 new_cfg |= tmp;
958 }
959
960 /* clear previous values of those bits, save new values */
961 enum reset_types old_cfg = jtag_get_reset_config();
962 old_cfg &= ~mask;
963 new_cfg |= old_cfg;
964 jtag_set_reset_config(new_cfg);
965
966 return ERROR_OK;
967 }
968
969 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx,
970 char *cmd, char **args, int argc)
971 {
972 if (argc > 1)
973 return ERROR_COMMAND_SYNTAX_ERROR;
974 if (argc == 1)
975 {
976 unsigned delay;
977 int retval = parse_uint(args[0], &delay);
978 if (ERROR_OK != retval)
979 return retval;
980 jtag_set_nsrst_delay(delay);
981 }
982 command_print(cmd_ctx, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
983 return ERROR_OK;
984 }
985
986 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx,
987 char *cmd, char **args, int argc)
988 {
989 if (argc > 1)
990 return ERROR_COMMAND_SYNTAX_ERROR;
991 if (argc == 1)
992 {
993 unsigned delay;
994 int retval = parse_uint(args[0], &delay);
995 if (ERROR_OK != retval)
996 return retval;
997 jtag_set_ntrst_delay(delay);
998 }
999 command_print(cmd_ctx, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1000 return ERROR_OK;
1001 }
1002
1003 static int handle_jtag_nsrst_assert_width_command(struct command_context_s *cmd_ctx,
1004 char *cmd, char **args, int argc)
1005 {
1006 if (argc > 1)
1007 return ERROR_COMMAND_SYNTAX_ERROR;
1008 if (argc == 1)
1009 {
1010 unsigned delay;
1011 int retval = parse_uint(args[0], &delay);
1012 if (ERROR_OK != retval)
1013 return retval;
1014 jtag_set_nsrst_assert_width(delay);
1015 }
1016 command_print(cmd_ctx, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
1017 return ERROR_OK;
1018 }
1019
1020 static int handle_jtag_ntrst_assert_width_command(struct command_context_s *cmd_ctx,
1021 char *cmd, char **args, int argc)
1022 {
1023 if (argc > 1)
1024 return ERROR_COMMAND_SYNTAX_ERROR;
1025 if (argc == 1)
1026 {
1027 unsigned delay;
1028 int retval = parse_uint(args[0], &delay);
1029 if (ERROR_OK != retval)
1030 return retval;
1031 jtag_set_ntrst_assert_width(delay);
1032 }
1033 command_print(cmd_ctx, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1034 return ERROR_OK;
1035 }
1036
1037 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1038 {
1039 int retval = ERROR_OK;
1040
1041 command_print(cmd_ctx, "OLD SYNTAX: DEPRECATED - "
1042 "use jtag_khz, not jtag_speed");
1043
1044 if (argc > 1)
1045 return ERROR_COMMAND_SYNTAX_ERROR;
1046 if (argc == 1)
1047 {
1048 LOG_DEBUG("handle jtag speed");
1049
1050 unsigned cur_speed = 0;
1051 int retval = parse_uint(args[0], &cur_speed);
1052 if (ERROR_OK != retval)
1053 return retval;
1054 retval = jtag_config_speed(cur_speed);
1055
1056 }
1057 command_print(cmd_ctx, "jtag_speed: %d", jtag_get_speed());
1058
1059 return retval;
1060 }
1061
1062 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1063 {
1064 if (argc > 1)
1065 return ERROR_COMMAND_SYNTAX_ERROR;
1066
1067 int retval = ERROR_OK;
1068 if (argc == 1)
1069 {
1070 unsigned khz = 0;
1071 int retval = parse_uint(args[0], &khz);
1072 if (ERROR_OK != retval)
1073 return retval;
1074 retval = jtag_config_khz(khz);
1075 if (ERROR_OK != retval)
1076 return retval;
1077 }
1078
1079 int cur_speed = jtag_get_speed_khz();
1080 retval = jtag_get_speed_readable(&cur_speed);
1081 if (ERROR_OK != retval)
1082 return retval;
1083
1084 if (cur_speed)
1085 command_print(cmd_ctx, "%d kHz", cur_speed);
1086 else
1087 command_print(cmd_ctx, "RCLK - adaptive");
1088
1089 return retval;
1090 }
1091
1092 static int handle_jtag_rclk_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1093 {
1094 if (argc > 1)
1095 return ERROR_COMMAND_SYNTAX_ERROR;
1096
1097 int retval = ERROR_OK;
1098 if (argc == 1)
1099 {
1100 unsigned khz = 0;
1101 int retval = parse_uint(args[0], &khz);
1102 if (ERROR_OK != retval)
1103 return retval;
1104 retval = jtag_config_rclk(khz);
1105 if (ERROR_OK != retval)
1106 return retval;
1107 }
1108
1109 int cur_khz = jtag_get_speed_khz();
1110 retval = jtag_get_speed_readable(&cur_khz);
1111 if (ERROR_OK != retval)
1112 return retval;
1113
1114 if (cur_khz)
1115 command_print(cmd_ctx, "RCLK not supported - fallback to %d kHz", cur_khz);
1116 else
1117 command_print(cmd_ctx, "RCLK - adaptive");
1118
1119 return retval;
1120 }
1121
1122 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx,
1123 char *cmd, char **args, int argc)
1124 {
1125 if (argc != 2)
1126 return ERROR_COMMAND_SYNTAX_ERROR;
1127
1128 int trst = -1;
1129 if (args[0][0] == '1')
1130 trst = 1;
1131 else if (args[0][0] == '0')
1132 trst = 0;
1133 else
1134 return ERROR_COMMAND_SYNTAX_ERROR;
1135
1136 int srst = -1;
1137 if (args[1][0] == '1')
1138 srst = 1;
1139 else if (args[1][0] == '0')
1140 srst = 0;
1141 else
1142 return ERROR_COMMAND_SYNTAX_ERROR;
1143
1144 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
1145 return ERROR_JTAG_INIT_FAILED;
1146
1147 jtag_add_reset(trst, srst);
1148 return jtag_execute_queue();
1149 }
1150
1151 static int handle_runtest_command(struct command_context_s *cmd_ctx,
1152 char *cmd, char **args, int argc)
1153 {
1154 if (argc != 1)
1155 return ERROR_COMMAND_SYNTAX_ERROR;
1156
1157 unsigned num_clocks;
1158 int retval = parse_uint(args[0], &num_clocks);
1159 if (ERROR_OK != retval)
1160 return retval;
1161
1162 jtag_add_runtest(num_clocks, TAP_IDLE);
1163 return jtag_execute_queue();
1164 }
1165
1166 /*
1167 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1168 * should be stable ... and *NOT* a shift state, otherwise free-running
1169 * jtag clocks could change the values latched by the update state.
1170 */
1171 static bool scan_is_safe(tap_state_t state)
1172 {
1173 switch (state)
1174 {
1175 case TAP_RESET:
1176 case TAP_IDLE:
1177 case TAP_DRPAUSE:
1178 case TAP_IRPAUSE:
1179 return true;
1180 default:
1181 return false;
1182 }
1183 }
1184
1185
1186 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1187 {
1188 int i;
1189 scan_field_t *fields;
1190 jtag_tap_t *tap;
1191 tap_state_t endstate;
1192
1193 if ((argc < 2) || (argc % 2))
1194 {
1195 return ERROR_COMMAND_SYNTAX_ERROR;
1196 }
1197
1198 /* optional "-endstate" "statename" at the end of the arguments,
1199 * so that e.g. IRPAUSE can let us load the data register before
1200 * entering RUN/IDLE to execute the instruction we load here.
1201 */
1202 endstate = TAP_IDLE;
1203
1204 if (argc >= 4) {
1205 /* have at least one pair of numbers. */
1206 /* is last pair the magic text? */
1207 if (0 == strcmp("-endstate", args[ argc - 2 ])) {
1208 const char *cpA;
1209 const char *cpS;
1210 cpA = args[ argc-1 ];
1211 for (endstate = 0 ; endstate < TAP_NUM_STATES ; endstate++) {
1212 cpS = tap_state_name(endstate);
1213 if (0 == strcmp(cpA, cpS)) {
1214 break;
1215 }
1216 }
1217 if (endstate >= TAP_NUM_STATES) {
1218 return ERROR_COMMAND_SYNTAX_ERROR;
1219 } else {
1220 if (!scan_is_safe(endstate))
1221 LOG_WARNING("irscan with unsafe "
1222 "endstate \"%s\"", cpA);
1223 /* found - remove the last 2 args */
1224 argc -= 2;
1225 }
1226 }
1227 }
1228
1229 int num_fields = argc / 2;
1230 size_t fields_len = sizeof(scan_field_t) * num_fields;
1231 fields = malloc(fields_len);
1232 memset(fields, 0, fields_len);
1233
1234 int retval;
1235 for (i = 0; i < num_fields; i++)
1236 {
1237 tap = jtag_tap_by_string(args[i*2]);
1238 if (tap == NULL)
1239 {
1240 int j;
1241 for (j = 0; j < i; j++)
1242 free(fields[j].out_value);
1243 free(fields);
1244 command_print(cmd_ctx, "Tap: %s unknown", args[i*2]);
1245
1246 return ERROR_FAIL;
1247 }
1248 int field_size = tap->ir_length;
1249 fields[i].tap = tap;
1250 fields[i].num_bits = field_size;
1251 fields[i].out_value = malloc(CEIL(field_size, 8));
1252
1253 uint32_t value;
1254 retval = parse_u32(args[i * 2 + 1], &value);
1255 if (ERROR_OK != retval)
1256 goto error_return;
1257 buf_set_u32(fields[i].out_value, 0, field_size, value);
1258 fields[i].in_value = NULL;
1259 }
1260
1261 /* did we have an endstate? */
1262 jtag_add_ir_scan(num_fields, fields, endstate);
1263
1264 retval = jtag_execute_queue();
1265
1266 error_return:
1267 for (i = 0; i < num_fields; i++)
1268 {
1269 if (NULL != fields[i].out_value)
1270 free(fields[i].out_value);
1271 }
1272
1273 free (fields);
1274
1275 return retval;
1276 }
1277
1278 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1279 {
1280 int retval;
1281 scan_field_t *fields;
1282 int num_fields;
1283 int field_count = 0;
1284 int i, e;
1285 jtag_tap_t *tap;
1286 tap_state_t endstate;
1287
1288 /* args[1] = device
1289 * args[2] = num_bits
1290 * args[3] = hex string
1291 * ... repeat num bits and hex string ...
1292 *
1293 * .. optionally:
1294 * args[N-2] = "-endstate"
1295 * args[N-1] = statename
1296 */
1297 if ((argc < 4) || ((argc % 2) != 0))
1298 {
1299 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1300 return JIM_ERR;
1301 }
1302
1303 endstate = TAP_IDLE;
1304
1305 script_debug(interp, "drscan", argc, args);
1306
1307 /* validate arguments as numbers */
1308 e = JIM_OK;
1309 for (i = 2; i < argc; i += 2)
1310 {
1311 long bits;
1312 const char *cp;
1313
1314 e = Jim_GetLong(interp, args[i], &bits);
1315 /* If valid - try next arg */
1316 if (e == JIM_OK) {
1317 continue;
1318 }
1319
1320 /* Not valid.. are we at the end? */
1321 if (((i + 2) != argc)) {
1322 /* nope, then error */
1323 return e;
1324 }
1325
1326 /* it could be: "-endstate FOO"
1327 * e.g. DRPAUSE so we can issue more instructions
1328 * before entering RUN/IDLE and executing them.
1329 */
1330
1331 /* get arg as a string. */
1332 cp = Jim_GetString(args[i], NULL);
1333 /* is it the magic? */
1334 if (0 == strcmp("-endstate", cp)) {
1335 /* is the statename valid? */
1336 cp = Jim_GetString(args[i + 1], NULL);
1337
1338 /* see if it is a valid state name */
1339 endstate = tap_state_by_name(cp);
1340 if (endstate < 0) {
1341 /* update the error message */
1342 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1343 } else {
1344 if (!scan_is_safe(endstate))
1345 LOG_WARNING("drscan with unsafe "
1346 "endstate \"%s\"", cp);
1347
1348 /* valid - so clear the error */
1349 e = JIM_OK;
1350 /* and remove the last 2 args */
1351 argc -= 2;
1352 }
1353 }
1354
1355 /* Still an error? */
1356 if (e != JIM_OK) {
1357 return e; /* too bad */
1358 }
1359 } /* validate args */
1360
1361 tap = jtag_tap_by_jim_obj(interp, args[1]);
1362 if (tap == NULL) {
1363 return JIM_ERR;
1364 }
1365
1366 num_fields = (argc-2)/2;
1367 fields = malloc(sizeof(scan_field_t) * num_fields);
1368 for (i = 2; i < argc; i += 2)
1369 {
1370 long bits;
1371 int len;
1372 const char *str;
1373
1374 Jim_GetLong(interp, args[i], &bits);
1375 str = Jim_GetString(args[i + 1], &len);
1376
1377 fields[field_count].tap = tap;
1378 fields[field_count].num_bits = bits;
1379 fields[field_count].out_value = malloc(CEIL(bits, 8));
1380 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
1381 fields[field_count].in_value = fields[field_count].out_value;
1382 field_count++;
1383 }
1384
1385 jtag_add_dr_scan(num_fields, fields, endstate);
1386
1387 retval = jtag_execute_queue();
1388 if (retval != ERROR_OK)
1389 {
1390 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
1391 return JIM_ERR;
1392 }
1393
1394 field_count = 0;
1395 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
1396 for (i = 2; i < argc; i += 2)
1397 {
1398 long bits;
1399 char *str;
1400
1401 Jim_GetLong(interp, args[i], &bits);
1402 str = buf_to_str(fields[field_count].in_value, bits, 16);
1403 free(fields[field_count].out_value);
1404
1405 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
1406 free(str);
1407 field_count++;
1408 }
1409
1410 Jim_SetResult(interp, list);
1411
1412 free(fields);
1413
1414 return JIM_OK;
1415 }
1416
1417
1418 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1419 {
1420 tap_state_t states[8];
1421
1422 if ((argc < 2) || ((size_t)argc > (sizeof(states)/sizeof(*states) + 1)))
1423 {
1424 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1425 return JIM_ERR;
1426 }
1427
1428 script_debug(interp, "pathmove", argc, args);
1429
1430 int i;
1431 for (i = 0; i < argc-1; i++)
1432 {
1433 const char *cp;
1434 cp = Jim_GetString(args[i + 1], NULL);
1435 states[i] = tap_state_by_name(cp);
1436 if (states[i] < 0)
1437 {
1438 /* update the error message */
1439 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1440 return JIM_ERR;
1441 }
1442 }
1443
1444 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
1445 {
1446 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
1447 return JIM_ERR;
1448 }
1449
1450 jtag_add_pathmove(argc-2, states + 1);
1451
1452 if (jtag_execute_queue()!= ERROR_OK)
1453 {
1454 Jim_SetResultString(interp, "pathmove: failed",-1);
1455 return JIM_ERR;
1456 }
1457
1458 return JIM_OK;
1459 }
1460
1461
1462 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1463 {
1464 script_debug(interp, "flush_count", argc, args);
1465
1466 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
1467
1468 return JIM_OK;
1469 }
1470
1471
1472 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1473 {
1474 if (argc > 1)
1475 return ERROR_COMMAND_SYNTAX_ERROR;
1476
1477 if (argc == 1)
1478 {
1479 if (strcmp(args[0], "enable") == 0)
1480 jtag_set_verify_capture_ir(true);
1481 else if (strcmp(args[0], "disable") == 0)
1482 jtag_set_verify_capture_ir(false);
1483 else
1484 return ERROR_COMMAND_SYNTAX_ERROR;
1485 }
1486
1487 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1488 command_print(cmd_ctx, "verify Capture-IR is %s", status);
1489
1490 return ERROR_OK;
1491 }
1492
1493 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1494 {
1495 if (argc > 1)
1496 return ERROR_COMMAND_SYNTAX_ERROR;
1497
1498 if (argc == 1)
1499 {
1500 if (strcmp(args[0], "enable") == 0)
1501 jtag_set_verify(true);
1502 else if (strcmp(args[0], "disable") == 0)
1503 jtag_set_verify(false);
1504 else
1505 return ERROR_COMMAND_SYNTAX_ERROR;
1506 }
1507
1508 const char *status = jtag_will_verify() ? "enabled": "disabled";
1509 command_print(cmd_ctx, "verify jtag capture is %s", status);
1510
1511 return ERROR_OK;
1512 }
1513
1514 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1515 {
1516 if (argc > 1)
1517 return ERROR_COMMAND_SYNTAX_ERROR;
1518
1519 if (argc == 1)
1520 {
1521 bool use_new_table;
1522 if (strcmp(args[0], "short") == 0)
1523 use_new_table = true;
1524 else if (strcmp(args[0], "long") == 0)
1525 use_new_table = false;
1526 else
1527 return ERROR_COMMAND_SYNTAX_ERROR;
1528
1529 tap_use_new_tms_table(use_new_table);
1530 }
1531
1532 command_print(cmd_ctx, "tms sequence is %s",
1533 tap_uses_new_tms_table() ? "short": "long");
1534
1535 return ERROR_OK;
1536 }