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