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