jtag: tcl: Add cget -idcode
[fw/openocd] / src / jtag / tcl.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007-2010 Ã˜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, see <http://www.gnu.org/licenses/>. *
27  ***************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "jtag.h"
34 #include "swd.h"
35 #include "minidriver.h"
36 #include "interface.h"
37 #include "interfaces.h"
38 #include "tcl.h"
39
40 #ifdef HAVE_STRINGS_H
41 #include <strings.h>
42 #endif
43
44 #include <helper/time_support.h>
45 #include "transport/transport.h"
46
47 /**
48  * @file
49  * Holds support for accessing JTAG-specific mechanisms from TCl scripts.
50  */
51
52 static const Jim_Nvp nvp_jtag_tap_event[] = {
53         { .value = JTAG_TRST_ASSERTED,          .name = "post-reset" },
54         { .value = JTAG_TAP_EVENT_SETUP,        .name = "setup" },
55         { .value = JTAG_TAP_EVENT_ENABLE,       .name = "tap-enable" },
56         { .value = JTAG_TAP_EVENT_DISABLE,      .name = "tap-disable" },
57
58         { .name = NULL, .value = -1 }
59 };
60
61 struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
62 {
63         const char *cp = Jim_GetString(o, NULL);
64         struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
65         if (NULL == cp)
66                 cp = "(unknown)";
67         if (NULL == t)
68                 Jim_SetResultFormatted(interp, "Tap '%s' could not be found", cp);
69         return t;
70 }
71
72 static bool scan_is_safe(tap_state_t state)
73 {
74         switch (state) {
75             case TAP_RESET:
76             case TAP_IDLE:
77             case TAP_DRPAUSE:
78             case TAP_IRPAUSE:
79                     return true;
80             default:
81                     return false;
82         }
83 }
84
85 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
86 {
87         int retval;
88         struct scan_field *fields;
89         int num_fields;
90         int field_count = 0;
91         int i, e;
92         struct jtag_tap *tap;
93         tap_state_t endstate;
94
95         /* args[1] = device
96          * args[2] = num_bits
97          * args[3] = hex string
98          * ... repeat num bits and hex string ...
99          *
100          * .. optionally:
101         *     args[N-2] = "-endstate"
102          *     args[N-1] = statename
103          */
104         if ((argc < 4) || ((argc % 2) != 0)) {
105                 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
106                 return JIM_ERR;
107         }
108
109         endstate = TAP_IDLE;
110
111         script_debug(interp, "drscan", argc, args);
112
113         /* validate arguments as numbers */
114         e = JIM_OK;
115         for (i = 2; i < argc; i += 2) {
116                 long bits;
117                 const char *cp;
118
119                 e = Jim_GetLong(interp, args[i], &bits);
120                 /* If valid - try next arg */
121                 if (e == JIM_OK)
122                         continue;
123
124                 /* Not valid.. are we at the end? */
125                 if (((i + 2) != argc)) {
126                         /* nope, then error */
127                         return e;
128                 }
129
130                 /* it could be: "-endstate FOO"
131                  * e.g. DRPAUSE so we can issue more instructions
132                  * before entering RUN/IDLE and executing them.
133                  */
134
135                 /* get arg as a string. */
136                 cp = Jim_GetString(args[i], NULL);
137                 /* is it the magic? */
138                 if (0 == strcmp("-endstate", cp)) {
139                         /* is the statename valid? */
140                         cp = Jim_GetString(args[i + 1], NULL);
141
142                         /* see if it is a valid state name */
143                         endstate = tap_state_by_name(cp);
144                         if (endstate < 0) {
145                                 /* update the error message */
146                                 Jim_SetResultFormatted(interp, "endstate: %s invalid", cp);
147                         } else {
148                                 if (!scan_is_safe(endstate))
149                                         LOG_WARNING("drscan with unsafe "
150                                                 "endstate \"%s\"", cp);
151
152                                 /* valid - so clear the error */
153                                 e = JIM_OK;
154                                 /* and remove the last 2 args */
155                                 argc -= 2;
156                         }
157                 }
158
159                 /* Still an error? */
160                 if (e != JIM_OK)
161                         return e;       /* too bad */
162         }       /* validate args */
163
164         assert(e == JIM_OK);
165
166         tap = jtag_tap_by_jim_obj(interp, args[1]);
167         if (tap == NULL)
168                 return JIM_ERR;
169
170         num_fields = (argc-2)/2;
171         if (num_fields <= 0) {
172                 Jim_SetResultString(interp, "drscan: no scan fields supplied", -1);
173                 return JIM_ERR;
174         }
175         fields = malloc(sizeof(struct scan_field) * num_fields);
176         for (i = 2; i < argc; i += 2) {
177                 long bits;
178                 int len;
179                 const char *str;
180
181                 Jim_GetLong(interp, args[i], &bits);
182                 str = Jim_GetString(args[i + 1], &len);
183
184                 fields[field_count].num_bits = bits;
185                 void *t = malloc(DIV_ROUND_UP(bits, 8));
186                 fields[field_count].out_value = t;
187                 str_to_buf(str, len, t, bits, 0);
188                 fields[field_count].in_value = t;
189                 field_count++;
190         }
191
192         jtag_add_dr_scan(tap, num_fields, fields, endstate);
193
194         retval = jtag_execute_queue();
195         if (retval != ERROR_OK) {
196                 Jim_SetResultString(interp, "drscan: jtag execute failed", -1);
197                 return JIM_ERR;
198         }
199
200         field_count = 0;
201         Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
202         for (i = 2; i < argc; i += 2) {
203                 long bits;
204                 char *str;
205
206                 Jim_GetLong(interp, args[i], &bits);
207                 str = buf_to_str(fields[field_count].in_value, bits, 16);
208                 free(fields[field_count].in_value);
209
210                 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
211                 free(str);
212                 field_count++;
213         }
214
215         Jim_SetResult(interp, list);
216
217         free(fields);
218
219         return JIM_OK;
220 }
221
222
223 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
224 {
225         tap_state_t states[8];
226
227         if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1))) {
228                 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
229                 return JIM_ERR;
230         }
231
232         script_debug(interp, "pathmove", argc, args);
233
234         int i;
235         for (i = 0; i < argc-1; i++) {
236                 const char *cp;
237                 cp = Jim_GetString(args[i + 1], NULL);
238                 states[i] = tap_state_by_name(cp);
239                 if (states[i] < 0) {
240                         /* update the error message */
241                         Jim_SetResultFormatted(interp, "endstate: %s invalid", cp);
242                         return JIM_ERR;
243                 }
244         }
245
246         if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue() != ERROR_OK)) {
247                 Jim_SetResultString(interp, "pathmove: jtag execute failed", -1);
248                 return JIM_ERR;
249         }
250
251         jtag_add_pathmove(argc - 2, states + 1);
252
253         if (jtag_execute_queue() != ERROR_OK) {
254                 Jim_SetResultString(interp, "pathmove: failed", -1);
255                 return JIM_ERR;
256         }
257
258         return JIM_OK;
259 }
260
261
262 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
263 {
264         script_debug(interp, "flush_count", argc, args);
265
266         Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
267
268         return JIM_OK;
269 }
270
271 /* REVISIT Just what about these should "move" ... ?
272  * These registrations, into the main JTAG table?
273  *
274  * There's a minor compatibility issue, these all show up twice;
275  * that's not desirable:
276  *  - jtag drscan ... NOT DOCUMENTED!
277  *  - drscan ...
278  *
279  * The "irscan" command (for example) doesn't show twice.
280  */
281 static const struct command_registration jtag_command_handlers_to_move[] = {
282         {
283                 .name = "drscan",
284                 .mode = COMMAND_EXEC,
285                 .jim_handler = Jim_Command_drscan,
286                 .help = "Execute Data Register (DR) scan for one TAP.  "
287                         "Other TAPs must be in BYPASS mode.",
288                 .usage = "tap_name [num_bits value]* ['-endstate' state_name]",
289         },
290         {
291                 .name = "flush_count",
292                 .mode = COMMAND_EXEC,
293                 .jim_handler = Jim_Command_flush_count,
294                 .help = "Returns the number of times the JTAG queue "
295                         "has been flushed.",
296         },
297         {
298                 .name = "pathmove",
299                 .mode = COMMAND_EXEC,
300                 .jim_handler = Jim_Command_pathmove,
301                 .usage = "start_state state1 [state2 [state3 ...]]",
302                 .help = "Move JTAG state machine from current state "
303                         "(start_state) to state1, then state2, state3, etc.",
304         },
305         COMMAND_REGISTRATION_DONE
306 };
307
308
309 enum jtag_tap_cfg_param {
310         JCFG_EVENT,
311         JCFG_IDCODE,
312 };
313
314 static Jim_Nvp nvp_config_opts[] = {
315         { .name = "-event",      .value = JCFG_EVENT },
316         { .name = "-idcode",     .value = JCFG_IDCODE },
317
318         { .name = NULL,          .value = -1 }
319 };
320
321 static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap *tap)
322 {
323         if (goi->argc == 0) {
324                 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
325                 return JIM_ERR;
326         }
327
328         Jim_Nvp *n;
329         int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
330         if (e != JIM_OK) {
331                 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
332                 return e;
333         }
334
335         if (goi->isconfigure) {
336                 if (goi->argc != 1) {
337                         Jim_WrongNumArgs(goi->interp,
338                                 goi->argc,
339                                 goi->argv,
340                                 "-event <event-name> <event-body>");
341                         return JIM_ERR;
342                 }
343         } else {
344                 if (goi->argc != 0) {
345                         Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
346                         return JIM_ERR;
347                 }
348         }
349
350         struct jtag_tap_event_action *jteap  = tap->event_action;
351         /* replace existing event body */
352         bool found = false;
353         while (jteap) {
354                 if (jteap->event == (enum jtag_event)n->value) {
355                         found = true;
356                         break;
357                 }
358                 jteap = jteap->next;
359         }
360
361         Jim_SetEmptyResult(goi->interp);
362
363         if (goi->isconfigure) {
364                 if (!found)
365                         jteap = calloc(1, sizeof(*jteap));
366                 else if (NULL != jteap->body)
367                         Jim_DecrRefCount(goi->interp, jteap->body);
368
369                 jteap->interp = goi->interp;
370                 jteap->event = n->value;
371
372                 Jim_Obj *o;
373                 Jim_GetOpt_Obj(goi, &o);
374                 jteap->body = Jim_DuplicateObj(goi->interp, o);
375                 Jim_IncrRefCount(jteap->body);
376
377                 if (!found) {
378                         /* add to head of event list */
379                         jteap->next = tap->event_action;
380                         tap->event_action = jteap;
381                 }
382         } else if (found) {
383                 jteap->interp = goi->interp;
384                 Jim_SetResult(goi->interp,
385                         Jim_DuplicateObj(goi->interp, jteap->body));
386         }
387         return JIM_OK;
388 }
389
390 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap *tap)
391 {
392         /* parse config or cget options */
393         while (goi->argc > 0) {
394                 Jim_SetEmptyResult(goi->interp);
395
396                 Jim_Nvp *n;
397                 int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
398                 if (e != JIM_OK) {
399                         Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
400                         return e;
401                 }
402
403                 switch (n->value) {
404                         case JCFG_EVENT:
405                                 e = jtag_tap_configure_event(goi, tap);
406                                 if (e != JIM_OK)
407                                         return e;
408                                 break;
409                         case JCFG_IDCODE:
410                                 if (goi->isconfigure) {
411                                         Jim_SetResultFormatted(goi->interp,
412                                                         "not settable: %s", n->name);
413                                         return JIM_ERR;
414                                 } else {
415                                         if (goi->argc != 0) {
416                                                 Jim_WrongNumArgs(goi->interp,
417                                                                 goi->argc, goi->argv,
418                                                                 "NO PARAMS");
419                                                 return JIM_ERR;
420                                         }
421                                 }
422                                 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, tap->idcode));
423                                 break;
424                         default:
425                                 Jim_SetResultFormatted(goi->interp, "unknown value: %s", n->name);
426                                 return JIM_ERR;
427                 }
428         }
429
430         return JIM_OK;
431 }
432
433 static int is_bad_irval(int ir_length, jim_wide w)
434 {
435         jim_wide v = 1;
436
437         v <<= ir_length;
438         v -= 1;
439         v = ~v;
440         return (w & v) != 0;
441 }
442
443 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
444         struct jtag_tap *pTap)
445 {
446         jim_wide w;
447         int e = Jim_GetOpt_Wide(goi, &w);
448         if (e != JIM_OK) {
449                 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter", n->name);
450                 return e;
451         }
452
453         uint32_t *p = realloc(pTap->expected_ids,
454                               (pTap->expected_ids_cnt + 1) * sizeof(uint32_t));
455         if (!p) {
456                 Jim_SetResultFormatted(goi->interp, "no memory");
457                 return JIM_ERR;
458         }
459
460         pTap->expected_ids = p;
461         pTap->expected_ids[pTap->expected_ids_cnt++] = w;
462
463         return JIM_OK;
464 }
465
466 #define NTAP_OPT_IRLEN     0
467 #define NTAP_OPT_IRMASK    1
468 #define NTAP_OPT_IRCAPTURE 2
469 #define NTAP_OPT_ENABLED   3
470 #define NTAP_OPT_DISABLED  4
471 #define NTAP_OPT_EXPECTED_ID 5
472 #define NTAP_OPT_VERSION   6
473
474 static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
475         struct jtag_tap *pTap)
476 {
477         jim_wide w;
478         int e = Jim_GetOpt_Wide(goi, &w);
479         if (e != JIM_OK) {
480                 Jim_SetResultFormatted(goi->interp,
481                         "option: %s bad parameter", n->name);
482                 return e;
483         }
484         switch (n->value) {
485             case NTAP_OPT_IRLEN:
486                     if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value))) {
487                             LOG_WARNING("%s: huge IR length %d",
488                                     pTap->dotted_name, (int) w);
489                     }
490                     pTap->ir_length = w;
491                     break;
492             case NTAP_OPT_IRMASK:
493                     if (is_bad_irval(pTap->ir_length, w)) {
494                             LOG_ERROR("%s: IR mask %x too big",
495                                     pTap->dotted_name,
496                                     (int) w);
497                             return JIM_ERR;
498                     }
499                     if ((w & 3) != 3)
500                             LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
501                     pTap->ir_capture_mask = w;
502                     break;
503             case NTAP_OPT_IRCAPTURE:
504                     if (is_bad_irval(pTap->ir_length, w)) {
505                             LOG_ERROR("%s: IR capture %x too big",
506                                     pTap->dotted_name, (int) w);
507                             return JIM_ERR;
508                     }
509                     if ((w & 3) != 1)
510                             LOG_WARNING("%s: nonstandard IR value",
511                                     pTap->dotted_name);
512                     pTap->ir_capture_value = w;
513                     break;
514             default:
515                     return JIM_ERR;
516         }
517         return JIM_OK;
518 }
519
520 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
521 {
522         struct jtag_tap *pTap;
523         int x;
524         int e;
525         Jim_Nvp *n;
526         char *cp;
527         const Jim_Nvp opts[] = {
528                 { .name = "-irlen",       .value = NTAP_OPT_IRLEN },
529                 { .name = "-irmask",       .value = NTAP_OPT_IRMASK },
530                 { .name = "-ircapture",       .value = NTAP_OPT_IRCAPTURE },
531                 { .name = "-enable",       .value = NTAP_OPT_ENABLED },
532                 { .name = "-disable",       .value = NTAP_OPT_DISABLED },
533                 { .name = "-expected-id",       .value = NTAP_OPT_EXPECTED_ID },
534                 { .name = "-ignore-version",       .value = NTAP_OPT_VERSION },
535                 { .name = NULL,       .value = -1 },
536         };
537
538         pTap = calloc(1, sizeof(struct jtag_tap));
539         if (!pTap) {
540                 Jim_SetResultFormatted(goi->interp, "no memory");
541                 return JIM_ERR;
542         }
543
544         /*
545          * we expect CHIP + TAP + OPTIONS
546          * */
547         if (goi->argc < 3) {
548                 Jim_SetResultFormatted(goi->interp, "Missing CHIP TAP OPTIONS ....");
549                 free(pTap);
550                 return JIM_ERR;
551         }
552
553         const char *tmp;
554         Jim_GetOpt_String(goi, &tmp, NULL);
555         pTap->chip = strdup(tmp);
556
557         Jim_GetOpt_String(goi, &tmp, NULL);
558         pTap->tapname = strdup(tmp);
559
560         /* name + dot + name + null */
561         x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
562         cp = malloc(x);
563         sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
564         pTap->dotted_name = cp;
565
566         LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
567                 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
568
569         if (!transport_is_jtag()) {
570                 /* SWD doesn't require any JTAG tap parameters */
571                 pTap->enabled = true;
572                 jtag_tap_init(pTap);
573                 return JIM_OK;
574         }
575
576         /* IEEE specifies that the two LSBs of an IR scan are 01, so make
577          * that the default.  The "-ircapture" and "-irmask" options are only
578          * needed to cope with nonstandard TAPs, or to specify more bits.
579          */
580         pTap->ir_capture_mask = 0x03;
581         pTap->ir_capture_value = 0x01;
582
583         while (goi->argc) {
584                 e = Jim_GetOpt_Nvp(goi, opts, &n);
585                 if (e != JIM_OK) {
586                         Jim_GetOpt_NvpUnknown(goi, opts, 0);
587                         free(cp);
588                         free(pTap);
589                         return e;
590                 }
591                 LOG_DEBUG("Processing option: %s", n->name);
592                 switch (n->value) {
593                     case NTAP_OPT_ENABLED:
594                             pTap->disabled_after_reset = false;
595                             break;
596                     case NTAP_OPT_DISABLED:
597                             pTap->disabled_after_reset = true;
598                             break;
599                     case NTAP_OPT_EXPECTED_ID:
600                             e = jim_newtap_expected_id(n, goi, pTap);
601                             if (JIM_OK != e) {
602                                     free(cp);
603                                     free(pTap);
604                                     return e;
605                             }
606                             break;
607                     case NTAP_OPT_IRLEN:
608                     case NTAP_OPT_IRMASK:
609                     case NTAP_OPT_IRCAPTURE:
610                             e = jim_newtap_ir_param(n, goi, pTap);
611                             if (JIM_OK != e) {
612                                     free(cp);
613                                     free(pTap);
614                                     return e;
615                             }
616                             break;
617                     case NTAP_OPT_VERSION:
618                             pTap->ignore_version = true;
619                             break;
620                 }       /* switch (n->value) */
621         }       /* while (goi->argc) */
622
623         /* default is enabled-after-reset */
624         pTap->enabled = !pTap->disabled_after_reset;
625
626         /* Did all the required option bits get cleared? */
627         if (pTap->ir_length != 0) {
628                 jtag_tap_init(pTap);
629                 return JIM_OK;
630         }
631
632         Jim_SetResultFormatted(goi->interp,
633                 "newtap: %s missing IR length",
634                 pTap->dotted_name);
635         jtag_tap_free(pTap);
636         return JIM_ERR;
637 }
638
639 static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
640 {
641         struct jtag_tap_event_action *jteap;
642
643         for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next) {
644                 if (jteap->event != e)
645                         continue;
646
647                 Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
648                 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
649                         tap->dotted_name, e, nvp->name,
650                         Jim_GetString(jteap->body, NULL));
651
652                 if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK) {
653                         Jim_MakeErrorMessage(jteap->interp);
654                         LOG_USER("%s", Jim_GetString(Jim_GetResult(jteap->interp), NULL));
655                         continue;
656                 }
657
658                 switch (e) {
659                     case JTAG_TAP_EVENT_ENABLE:
660                     case JTAG_TAP_EVENT_DISABLE:
661                                 /* NOTE:  we currently assume the handlers
662                                  * can't fail.  Right here is where we should
663                                  * really be verifying the scan chains ...
664                                  */
665                             tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
666                             LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
667                                 tap->enabled ? "enabled" : "disabled");
668                             break;
669                     default:
670                             break;
671                 }
672         }
673 }
674
675 static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
676 {
677         Jim_GetOptInfo goi;
678         Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
679         if (goi.argc != 0) {
680                 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
681                 return JIM_ERR;
682         }
683         struct command_context *context = current_command_context(interp);
684         int e = jtag_init_inner(context);
685         if (e != ERROR_OK) {
686                 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
687                 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
688                 Jim_FreeNewObj(goi.interp, eObj);
689                 return JIM_ERR;
690         }
691         return JIM_OK;
692 }
693
694 static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
695 {
696         int e = ERROR_OK;
697         Jim_GetOptInfo goi;
698         Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
699         if (goi.argc != 0) {
700                 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
701                 return JIM_ERR;
702         }
703         struct command_context *context = current_command_context(interp);
704         if (transport_is_jtag())
705                 e = jtag_init_reset(context);
706         else if (transport_is_swd())
707                 e = swd_init_reset(context);
708
709         if (e != ERROR_OK) {
710                 Jim_Obj *eObj = Jim_NewIntObj(goi.interp, e);
711                 Jim_SetResultFormatted(goi.interp, "error: %#s", eObj);
712                 Jim_FreeNewObj(goi.interp, eObj);
713                 return JIM_ERR;
714         }
715         return JIM_OK;
716 }
717
718 int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
719 {
720         Jim_GetOptInfo goi;
721         Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
722         return jim_newtap_cmd(&goi);
723 }
724
725 static bool jtag_tap_enable(struct jtag_tap *t)
726 {
727         if (t->enabled)
728                 return false;
729         jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
730         if (!t->enabled)
731                 return false;
732
733         /* FIXME add JTAG sanity checks, w/o TLR
734          *  - scan chain length grew by one (this)
735          *  - IDs and IR lengths are as expected
736          */
737         jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
738         return true;
739 }
740 static bool jtag_tap_disable(struct jtag_tap *t)
741 {
742         if (!t->enabled)
743                 return false;
744         jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
745         if (t->enabled)
746                 return false;
747
748         /* FIXME add JTAG sanity checks, w/o TLR
749          *  - scan chain length shrank by one (this)
750          *  - IDs and IR lengths are as expected
751          */
752         jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
753         return true;
754 }
755
756 int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
757 {
758         const char *cmd_name = Jim_GetString(argv[0], NULL);
759         Jim_GetOptInfo goi;
760         Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
761         if (goi.argc != 1) {
762                 Jim_SetResultFormatted(goi.interp, "usage: %s <name>", cmd_name);
763                 return JIM_ERR;
764         }
765
766         struct jtag_tap *t;
767
768         t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
769         if (t == NULL)
770                 return JIM_ERR;
771
772         if (strcasecmp(cmd_name, "tapisenabled") == 0) {
773                 /* do nothing, just return the value */
774         } else if (strcasecmp(cmd_name, "tapenable") == 0) {
775                 if (!jtag_tap_enable(t)) {
776                         LOG_WARNING("failed to enable tap %s", t->dotted_name);
777                         return JIM_ERR;
778                 }
779         } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
780                 if (!jtag_tap_disable(t)) {
781                         LOG_WARNING("failed to disable tap %s", t->dotted_name);
782                         return JIM_ERR;
783                 }
784         } else {
785                 LOG_ERROR("command '%s' unknown", cmd_name);
786                 return JIM_ERR;
787         }
788         bool e = t->enabled;
789         Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
790         return JIM_OK;
791 }
792
793 int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
794 {
795         const char *cmd_name = Jim_GetString(argv[0], NULL);
796         Jim_GetOptInfo goi;
797         Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
798         goi.isconfigure = !strcmp(cmd_name, "configure");
799         if (goi.argc < 2 + goi.isconfigure) {
800                 Jim_WrongNumArgs(goi.interp, 0, NULL,
801                         "<tap_name> <attribute> ...");
802                 return JIM_ERR;
803         }
804
805         struct jtag_tap *t;
806
807         Jim_Obj *o;
808         Jim_GetOpt_Obj(&goi, &o);
809         t = jtag_tap_by_jim_obj(goi.interp, o);
810         if (t == NULL)
811                 return JIM_ERR;
812
813         return jtag_tap_configure_cmd(&goi, t);
814 }
815
816 static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
817 {
818         Jim_GetOptInfo goi;
819         Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
820         if (goi.argc != 0) {
821                 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
822                 return JIM_ERR;
823         }
824         Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
825         struct jtag_tap *tap;
826
827         for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
828                 Jim_ListAppendElement(goi.interp,
829                         Jim_GetResult(goi.interp),
830                         Jim_NewStringObj(goi.interp,
831                                 tap->dotted_name, -1));
832         }
833         return JIM_OK;
834 }
835
836 COMMAND_HANDLER(handle_jtag_init_command)
837 {
838         if (CMD_ARGC != 0)
839                 return ERROR_COMMAND_SYNTAX_ERROR;
840
841         static bool jtag_initialized;
842         if (jtag_initialized) {
843                 LOG_INFO("'jtag init' has already been called");
844                 return ERROR_OK;
845         }
846         jtag_initialized = true;
847
848         LOG_DEBUG("Initializing jtag devices...");
849         return jtag_init(CMD_CTX);
850 }
851
852 static const struct command_registration jtag_subcommand_handlers[] = {
853         {
854                 .name = "init",
855                 .mode = COMMAND_ANY,
856                 .handler = handle_jtag_init_command,
857                 .help = "initialize jtag scan chain",
858                 .usage = ""
859         },
860         {
861                 .name = "arp_init",
862                 .mode = COMMAND_ANY,
863                 .jim_handler = jim_jtag_arp_init,
864                 .help = "Validates JTAG scan chain against the list of "
865                         "declared TAPs using just the four standard JTAG "
866                         "signals.",
867         },
868         {
869                 .name = "arp_init-reset",
870                 .mode = COMMAND_ANY,
871                 .jim_handler = jim_jtag_arp_init_reset,
872                 .help = "Uses TRST and SRST to try resetting everything on "
873                         "the JTAG scan chain, then performs 'jtag arp_init'."
874         },
875         {
876                 .name = "newtap",
877                 .mode = COMMAND_CONFIG,
878                 .jim_handler = jim_jtag_newtap,
879                 .help = "Create a new TAP instance named basename.tap_type, "
880                         "and appends it to the scan chain.",
881                 .usage = "basename tap_type '-irlen' count "
882                         "['-enable'|'-disable'] "
883                         "['-expected_id' number] "
884                         "['-ignore-version'] "
885                         "['-ircapture' number] "
886                         "['-mask' number] ",
887         },
888         {
889                 .name = "tapisenabled",
890                 .mode = COMMAND_EXEC,
891                 .jim_handler = jim_jtag_tap_enabler,
892                 .help = "Returns a Tcl boolean (0/1) indicating whether "
893                         "the TAP is enabled (1) or not (0).",
894                 .usage = "tap_name",
895         },
896         {
897                 .name = "tapenable",
898                 .mode = COMMAND_EXEC,
899                 .jim_handler = jim_jtag_tap_enabler,
900                 .help = "Try to enable the specified TAP using the "
901                         "'tap-enable' TAP event.",
902                 .usage = "tap_name",
903         },
904         {
905                 .name = "tapdisable",
906                 .mode = COMMAND_EXEC,
907                 .jim_handler = jim_jtag_tap_enabler,
908                 .help = "Try to disable the specified TAP using the "
909                         "'tap-disable' TAP event.",
910                 .usage = "tap_name",
911         },
912         {
913                 .name = "configure",
914                 .mode = COMMAND_ANY,
915                 .jim_handler = jim_jtag_configure,
916                 .help = "Provide a Tcl handler for the specified "
917                         "TAP event.",
918                 .usage = "tap_name '-event' event_name handler",
919         },
920         {
921                 .name = "cget",
922                 .mode = COMMAND_EXEC,
923                 .jim_handler = jim_jtag_configure,
924                 .help = "Return any Tcl handler for the specified "
925                         "TAP event.",
926                 .usage = "tap_name '-event' event_name",
927         },
928         {
929                 .name = "names",
930                 .mode = COMMAND_ANY,
931                 .jim_handler = jim_jtag_names,
932                 .help = "Returns list of all JTAG tap names.",
933         },
934         {
935                 .chain = jtag_command_handlers_to_move,
936         },
937         COMMAND_REGISTRATION_DONE
938 };
939
940 void jtag_notify_event(enum jtag_event event)
941 {
942         struct jtag_tap *tap;
943
944         for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
945                 jtag_tap_handle_event(tap, event);
946 }
947
948
949 COMMAND_HANDLER(handle_scan_chain_command)
950 {
951         struct jtag_tap *tap;
952         char expected_id[12];
953
954         tap = jtag_all_taps();
955         command_print(CMD_CTX,
956                 "   TapName             Enabled  IdCode     Expected   IrLen IrCap IrMask");
957         command_print(CMD_CTX,
958                 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
959
960         while (tap) {
961                 uint32_t expected, expected_mask, ii;
962
963                 snprintf(expected_id, sizeof expected_id, "0x%08x",
964                         (unsigned)((tap->expected_ids_cnt > 0)
965                                    ? tap->expected_ids[0]
966                                    : 0));
967                 if (tap->ignore_version)
968                         expected_id[2] = '*';
969
970                 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
971                 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
972
973                 command_print(CMD_CTX,
974                         "%2d %-18s     %c     0x%08x %s %5d 0x%02x  0x%02x",
975                         tap->abs_chain_position,
976                         tap->dotted_name,
977                         tap->enabled ? 'Y' : 'n',
978                         (unsigned int)(tap->idcode),
979                         expected_id,
980                         (unsigned int)(tap->ir_length),
981                         (unsigned int)(expected),
982                         (unsigned int)(expected_mask));
983
984                 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
985                         snprintf(expected_id, sizeof expected_id, "0x%08x",
986                                 (unsigned) tap->expected_ids[ii]);
987                         if (tap->ignore_version)
988                                 expected_id[2] = '*';
989
990                         command_print(CMD_CTX,
991                                 "                                           %s",
992                                 expected_id);
993                 }
994
995                 tap = tap->next_tap;
996         }
997
998         return ERROR_OK;
999 }
1000
1001 COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
1002 {
1003         if (CMD_ARGC > 1)
1004                 return ERROR_COMMAND_SYNTAX_ERROR;
1005         if (CMD_ARGC == 1) {
1006                 unsigned delay;
1007                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1008
1009                 jtag_set_ntrst_delay(delay);
1010         }
1011         command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
1012         return ERROR_OK;
1013 }
1014
1015 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
1016 {
1017         if (CMD_ARGC > 1)
1018                 return ERROR_COMMAND_SYNTAX_ERROR;
1019         if (CMD_ARGC == 1) {
1020                 unsigned delay;
1021                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
1022
1023                 jtag_set_ntrst_assert_width(delay);
1024         }
1025         command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
1026         return ERROR_OK;
1027 }
1028
1029 COMMAND_HANDLER(handle_jtag_rclk_command)
1030 {
1031         if (CMD_ARGC > 1)
1032                 return ERROR_COMMAND_SYNTAX_ERROR;
1033
1034         int retval = ERROR_OK;
1035         if (CMD_ARGC == 1) {
1036                 unsigned khz = 0;
1037                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
1038
1039                 retval = jtag_config_rclk(khz);
1040                 if (ERROR_OK != retval)
1041                         return retval;
1042         }
1043
1044         int cur_khz = jtag_get_speed_khz();
1045         retval = jtag_get_speed_readable(&cur_khz);
1046         if (ERROR_OK != retval)
1047                 return retval;
1048
1049         if (cur_khz)
1050                 command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
1051         else
1052                 command_print(CMD_CTX, "RCLK - adaptive");
1053
1054         return retval;
1055 }
1056
1057 COMMAND_HANDLER(handle_jtag_reset_command)
1058 {
1059         if (CMD_ARGC != 2)
1060                 return ERROR_COMMAND_SYNTAX_ERROR;
1061
1062         int trst = -1;
1063         if (CMD_ARGV[0][0] == '1')
1064                 trst = 1;
1065         else if (CMD_ARGV[0][0] == '0')
1066                 trst = 0;
1067         else
1068                 return ERROR_COMMAND_SYNTAX_ERROR;
1069
1070         int srst = -1;
1071         if (CMD_ARGV[1][0] == '1')
1072                 srst = 1;
1073         else if (CMD_ARGV[1][0] == '0')
1074                 srst = 0;
1075         else
1076                 return ERROR_COMMAND_SYNTAX_ERROR;
1077
1078         if (adapter_init(CMD_CTX) != ERROR_OK)
1079                 return ERROR_JTAG_INIT_FAILED;
1080
1081         jtag_add_reset(trst, srst);
1082         return jtag_execute_queue();
1083 }
1084
1085 COMMAND_HANDLER(handle_runtest_command)
1086 {
1087         if (CMD_ARGC != 1)
1088                 return ERROR_COMMAND_SYNTAX_ERROR;
1089
1090         unsigned num_clocks;
1091         COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
1092
1093         jtag_add_runtest(num_clocks, TAP_IDLE);
1094         return jtag_execute_queue();
1095 }
1096
1097 /*
1098  * For "irscan" or "drscan" commands, the "end" (really, "next") state
1099  * should be stable ... and *NOT* a shift state, otherwise free-running
1100  * jtag clocks could change the values latched by the update state.
1101  * Not surprisingly, this is the same constraint as SVF; the "irscan"
1102  * and "drscan" commands are a write-only subset of what SVF provides.
1103  */
1104
1105 COMMAND_HANDLER(handle_irscan_command)
1106 {
1107         int i;
1108         struct scan_field *fields;
1109         struct jtag_tap *tap = NULL;
1110         tap_state_t endstate;
1111
1112         if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
1113                 return ERROR_COMMAND_SYNTAX_ERROR;
1114
1115         /* optional "-endstate" "statename" at the end of the arguments,
1116          * so that e.g. IRPAUSE can let us load the data register before
1117          * entering RUN/IDLE to execute the instruction we load here.
1118          */
1119         endstate = TAP_IDLE;
1120
1121         if (CMD_ARGC >= 4) {
1122                 /* have at least one pair of numbers.
1123                  * is last pair the magic text? */
1124                 if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
1125                         endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
1126                         if (endstate == TAP_INVALID)
1127                                 return ERROR_COMMAND_SYNTAX_ERROR;
1128                         if (!scan_is_safe(endstate))
1129                                 LOG_WARNING("unstable irscan endstate \"%s\"",
1130                                         CMD_ARGV[CMD_ARGC - 1]);
1131                         CMD_ARGC -= 2;
1132                 }
1133         }
1134
1135         int num_fields = CMD_ARGC / 2;
1136         if (num_fields > 1) {
1137                 /* we really should be looking at plain_ir_scan if we want
1138                  * anything more fancy.
1139                  */
1140                 LOG_ERROR("Specify a single value for tap");
1141                 return ERROR_COMMAND_SYNTAX_ERROR;
1142         }
1143
1144         fields = calloc(num_fields, sizeof(*fields));
1145
1146         int retval;
1147         for (i = 0; i < num_fields; i++) {
1148                 tap = jtag_tap_by_string(CMD_ARGV[i*2]);
1149                 if (tap == NULL) {
1150                         free(fields);
1151                         command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
1152
1153                         return ERROR_FAIL;
1154                 }
1155                 int field_size = tap->ir_length;
1156                 fields[i].num_bits = field_size;
1157                 uint8_t *v = malloc(DIV_ROUND_UP(field_size, 8));
1158
1159                 uint64_t value;
1160                 retval = parse_u64(CMD_ARGV[i * 2 + 1], &value);
1161                 if (ERROR_OK != retval)
1162                         goto error_return;
1163                 buf_set_u64(v, 0, field_size, value);
1164                 fields[i].out_value = v;
1165                 fields[i].in_value = NULL;
1166         }
1167
1168         /* did we have an endstate? */
1169         jtag_add_ir_scan(tap, fields, endstate);
1170
1171         retval = jtag_execute_queue();
1172
1173 error_return:
1174         for (i = 0; i < num_fields; i++) {
1175                 if (NULL != fields[i].out_value)
1176                         free((void *)fields[i].out_value);
1177         }
1178
1179         free(fields);
1180
1181         return retval;
1182 }
1183
1184 COMMAND_HANDLER(handle_verify_ircapture_command)
1185 {
1186         if (CMD_ARGC > 1)
1187                 return ERROR_COMMAND_SYNTAX_ERROR;
1188
1189         if (CMD_ARGC == 1) {
1190                 bool enable;
1191                 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1192                 jtag_set_verify_capture_ir(enable);
1193         }
1194
1195         const char *status = jtag_will_verify_capture_ir() ? "enabled" : "disabled";
1196         command_print(CMD_CTX, "verify Capture-IR is %s", status);
1197
1198         return ERROR_OK;
1199 }
1200
1201 COMMAND_HANDLER(handle_verify_jtag_command)
1202 {
1203         if (CMD_ARGC > 1)
1204                 return ERROR_COMMAND_SYNTAX_ERROR;
1205
1206         if (CMD_ARGC == 1) {
1207                 bool enable;
1208                 COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
1209                 jtag_set_verify(enable);
1210         }
1211
1212         const char *status = jtag_will_verify() ? "enabled" : "disabled";
1213         command_print(CMD_CTX, "verify jtag capture is %s", status);
1214
1215         return ERROR_OK;
1216 }
1217
1218 COMMAND_HANDLER(handle_tms_sequence_command)
1219 {
1220         if (CMD_ARGC > 1)
1221                 return ERROR_COMMAND_SYNTAX_ERROR;
1222
1223         if (CMD_ARGC == 1) {
1224                 bool use_new_table;
1225                 if (strcmp(CMD_ARGV[0], "short") == 0)
1226                         use_new_table = true;
1227                 else if (strcmp(CMD_ARGV[0], "long") == 0)
1228                         use_new_table = false;
1229                 else
1230                         return ERROR_COMMAND_SYNTAX_ERROR;
1231
1232                 tap_use_new_tms_table(use_new_table);
1233         }
1234
1235         command_print(CMD_CTX, "tms sequence is  %s",
1236                 tap_uses_new_tms_table() ? "short" : "long");
1237
1238         return ERROR_OK;
1239 }
1240
1241 COMMAND_HANDLER(handle_jtag_flush_queue_sleep)
1242 {
1243         if (CMD_ARGC != 1)
1244                 return ERROR_COMMAND_SYNTAX_ERROR;
1245
1246         int sleep_ms;
1247         COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], sleep_ms);
1248
1249         jtag_set_flush_queue_sleep(sleep_ms);
1250
1251         return ERROR_OK;
1252 }
1253
1254 COMMAND_HANDLER(handle_wait_srst_deassert)
1255 {
1256         if (CMD_ARGC != 1)
1257                 return ERROR_COMMAND_SYNTAX_ERROR;
1258
1259         int timeout_ms;
1260         COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], timeout_ms);
1261         if ((timeout_ms <= 0) || (timeout_ms > 100000)) {
1262                 LOG_ERROR("Timeout must be an integer between 0 and 100000");
1263                 return ERROR_FAIL;
1264         }
1265
1266         LOG_USER("Waiting for srst assert + deassert for at most %dms", timeout_ms);
1267         int asserted_yet;
1268         int64_t then = timeval_ms();
1269         while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1270                 if ((timeval_ms() - then) > timeout_ms) {
1271                         LOG_ERROR("Timed out");
1272                         return ERROR_FAIL;
1273                 }
1274                 if (asserted_yet)
1275                         break;
1276         }
1277         while (jtag_srst_asserted(&asserted_yet) == ERROR_OK) {
1278                 if ((timeval_ms() - then) > timeout_ms) {
1279                         LOG_ERROR("Timed out");
1280                         return ERROR_FAIL;
1281                 }
1282                 if (!asserted_yet)
1283                         break;
1284         }
1285
1286         return ERROR_OK;
1287 }
1288
1289 static const struct command_registration jtag_command_handlers[] = {
1290
1291         {
1292                 .name = "jtag_flush_queue_sleep",
1293                 .handler = handle_jtag_flush_queue_sleep,
1294                 .mode = COMMAND_ANY,
1295                 .help = "For debug purposes(simulate long delays of interface) "
1296                         "to test performance or change in behavior. Default 0ms.",
1297                 .usage = "[sleep in ms]",
1298         },
1299         {
1300                 .name = "jtag_rclk",
1301                 .handler = handle_jtag_rclk_command,
1302                 .mode = COMMAND_ANY,
1303                 .help = "With an argument, change to to use adaptive clocking "
1304                         "if possible; else to use the fallback speed.  "
1305                         "With or without argument, display current setting.",
1306                 .usage = "[fallback_speed_khz]",
1307         },
1308         {
1309                 .name = "jtag_ntrst_delay",
1310                 .handler = handle_jtag_ntrst_delay_command,
1311                 .mode = COMMAND_ANY,
1312                 .help = "delay after deasserting trst in ms",
1313                 .usage = "[milliseconds]",
1314         },
1315         {
1316                 .name = "jtag_ntrst_assert_width",
1317                 .handler = handle_jtag_ntrst_assert_width_command,
1318                 .mode = COMMAND_ANY,
1319                 .help = "delay after asserting trst in ms",
1320                 .usage = "[milliseconds]",
1321         },
1322         {
1323                 .name = "scan_chain",
1324                 .handler = handle_scan_chain_command,
1325                 .mode = COMMAND_ANY,
1326                 .help = "print current scan chain configuration",
1327                 .usage = ""
1328         },
1329         {
1330                 .name = "jtag_reset",
1331                 .handler = handle_jtag_reset_command,
1332                 .mode = COMMAND_EXEC,
1333                 .help = "Set reset line values.  Value '1' is active, "
1334                         "value '0' is inactive.",
1335                 .usage = "trst_active srst_active",
1336         },
1337         {
1338                 .name = "runtest",
1339                 .handler = handle_runtest_command,
1340                 .mode = COMMAND_EXEC,
1341                 .help = "Move to Run-Test/Idle, and issue TCK for num_cycles.",
1342                 .usage = "num_cycles"
1343         },
1344         {
1345                 .name = "irscan",
1346                 .handler = handle_irscan_command,
1347                 .mode = COMMAND_EXEC,
1348                 .help = "Execute Instruction Register (DR) scan.  The "
1349                         "specified opcodes are put into each TAP's IR, "
1350                         "and other TAPs are put in BYPASS.",
1351                 .usage = "[tap_name instruction]* ['-endstate' state_name]",
1352         },
1353         {
1354                 .name = "verify_ircapture",
1355                 .handler = handle_verify_ircapture_command,
1356                 .mode = COMMAND_ANY,
1357                 .help = "Display or assign flag controlling whether to "
1358                         "verify values captured during Capture-IR.",
1359                 .usage = "['enable'|'disable']",
1360         },
1361         {
1362                 .name = "verify_jtag",
1363                 .handler = handle_verify_jtag_command,
1364                 .mode = COMMAND_ANY,
1365                 .help = "Display or assign flag controlling whether to "
1366                         "verify values captured during IR and DR scans.",
1367                 .usage = "['enable'|'disable']",
1368         },
1369         {
1370                 .name = "tms_sequence",
1371                 .handler = handle_tms_sequence_command,
1372                 .mode = COMMAND_ANY,
1373                 .help = "Display or change what style TMS sequences to use "
1374                         "for JTAG state transitions:  short (default) or "
1375                         "long.  Only for working around JTAG bugs.",
1376                 /* Specifically for working around DRIVER bugs... */
1377                 .usage = "['short'|'long']",
1378         },
1379         {
1380                 .name = "wait_srst_deassert",
1381                 .handler = handle_wait_srst_deassert,
1382                 .mode = COMMAND_ANY,
1383                 .help = "Wait for an SRST deassert. "
1384                         "Useful for cases where you need something to happen within ms "
1385                         "of an srst deassert. Timeout in ms ",
1386                 .usage = "ms",
1387         },
1388         {
1389                 .name = "jtag",
1390                 .mode = COMMAND_ANY,
1391                 .help = "perform jtag tap actions",
1392                 .usage = "",
1393
1394                 .chain = jtag_subcommand_handlers,
1395         },
1396         {
1397                 .chain = jtag_command_handlers_to_move,
1398         },
1399         COMMAND_REGISTRATION_DONE
1400 };
1401
1402 int jtag_register_commands(struct command_context *cmd_ctx)
1403 {
1404         return register_commands(cmd_ctx, NULL, jtag_command_handlers);
1405 }