71efc8b5d3d8dbd149cee63a31a6eedf6787fd64
[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                 Jim_SetResultString(goi.interp, jtag_interface->name, -1);
426                 return JIM_OK;
427         case JTAG_CMD_INIT:
428                 if (goi.argc != 0) {
429                         Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
430                         return JIM_ERR;
431                 }
432                 e = jtag_init_inner(context);
433                 if (e != ERROR_OK) {
434                         Jim_SetResult_sprintf(goi.interp, "error: %d", e);
435                         return JIM_ERR;
436                 }
437                 return JIM_OK;
438         case JTAG_CMD_INIT_RESET:
439                 if (goi.argc != 0) {
440                         Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
441                         return JIM_ERR;
442                 }
443                 e = jtag_init_reset(context);
444                 if (e != ERROR_OK) {
445                         Jim_SetResult_sprintf(goi.interp, "error: %d", e);
446                         return JIM_ERR;
447                 }
448                 return JIM_OK;
449         case JTAG_CMD_NEWTAP:
450                 return jim_newtap_cmd(&goi);
451                 break;
452         case JTAG_CMD_TAPISENABLED:
453         case JTAG_CMD_TAPENABLE:
454         case JTAG_CMD_TAPDISABLE:
455                 if (goi.argc != 1) {
456                         Jim_SetResultString(goi.interp, "Too many parameters",-1);
457                         return JIM_ERR;
458                 }
459
460                 {
461                         jtag_tap_t *t;
462
463                         t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
464                         if (t == NULL)
465                                 return JIM_ERR;
466
467                         switch (n->value) {
468                         case JTAG_CMD_TAPISENABLED:
469                                 break;
470                         case JTAG_CMD_TAPENABLE:
471                                 if (t->enabled)
472                                         break;
473                                 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
474                                 if (!t->enabled)
475                                         break;
476
477                                 /* FIXME add JTAG sanity checks, w/o TLR
478                                  *  - scan chain length grew by one (this)
479                                  *  - IDs and IR lengths are as expected
480                                  */
481
482                                 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
483                                 break;
484                         case JTAG_CMD_TAPDISABLE:
485                                 if (!t->enabled)
486                                         break;
487                                 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
488                                 if (t->enabled)
489                                         break;
490
491                                 /* FIXME add JTAG sanity checks, w/o TLR
492                                  *  - scan chain length shrank by one (this)
493                                  *  - IDs and IR lengths are as expected
494                                  */
495
496                                 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
497                                 break;
498                         }
499                         e = t->enabled;
500                         Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
501                         return JIM_OK;
502                 }
503                 break;
504
505         case JTAG_CMD_CGET:
506                 if (goi.argc < 2) {
507                         Jim_WrongNumArgs(goi.interp, 0, NULL,
508                                         "cget tap_name queryparm");
509                         return JIM_ERR;
510                 }
511
512                 {
513                         jtag_tap_t *t;
514
515                         Jim_GetOpt_Obj(&goi, &o);
516                         t = jtag_tap_by_jim_obj(goi.interp, o);
517                         if (t == NULL) {
518                                 return JIM_ERR;
519                         }
520
521                         goi.isconfigure = 0;
522                         return jtag_tap_configure_cmd(&goi, t);
523                 }
524                 break;
525
526         case JTAG_CMD_CONFIGURE:
527                 if (goi.argc < 3) {
528                         Jim_WrongNumArgs(goi.interp, 0, NULL,
529                                         "configure tap_name attribute value ...");
530                         return JIM_ERR;
531                 }
532
533                 {
534                         jtag_tap_t *t;
535
536                         Jim_GetOpt_Obj(&goi, &o);
537                         t = jtag_tap_by_jim_obj(goi.interp, o);
538                         if (t == NULL) {
539                                 return JIM_ERR;
540                         }
541
542                         goi.isconfigure = 1;
543                         return jtag_tap_configure_cmd(&goi, t);
544                 }
545                 break;
546
547         case JTAG_CMD_NAMES:
548                 if (goi.argc != 0) {
549                         Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
550                         return JIM_ERR;
551                 }
552                 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
553                 {
554                         jtag_tap_t *tap;
555
556                         for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
557                                 Jim_ListAppendElement(goi.interp,
558                                         Jim_GetResult(goi.interp),
559                                         Jim_NewStringObj(goi.interp,
560                                                 tap->dotted_name, -1));
561                         }
562                         return JIM_OK;
563                 }
564                 break;
565
566         }
567
568         return JIM_ERR;
569 }
570
571
572 void jtag_notify_event(enum jtag_event event)
573 {
574         jtag_tap_t *tap;
575
576         for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
577                 jtag_tap_handle_event(tap, event);
578 }
579
580
581 static int default_khz(int khz, int *jtag_speed)
582 {
583         LOG_ERROR("Translation from khz to jtag_speed not implemented");
584         return ERROR_FAIL;
585 }
586
587 static int default_speed_div(int speed, int *khz)
588 {
589         LOG_ERROR("Translation from jtag_speed to khz not implemented");
590         return ERROR_FAIL;
591 }
592
593 static int default_power_dropout(int *dropout)
594 {
595         *dropout = 0; /* by default we can't detect power dropout */
596         return ERROR_OK;
597 }
598
599 static int default_srst_asserted(int *srst_asserted)
600 {
601         *srst_asserted = 0; /* by default we can't detect srst asserted */
602         return ERROR_OK;
603 }
604
605 static int handle_interface_list_command(struct command_context_s *cmd_ctx,
606                 char *cmd, char **args, int argc)
607 {
608         if (strcmp(cmd, "interface_list") == 0 && argc > 0)
609                 return ERROR_COMMAND_SYNTAX_ERROR;
610
611         command_print(cmd_ctx, "The following JTAG interfaces are available:");
612         for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
613         {
614                 const char *name = jtag_interfaces[i]->name;
615                 command_print(cmd_ctx, "%u: %s", i + 1, name);
616         }
617
618         return ERROR_OK;
619 }
620
621 static int handle_interface_command(struct command_context_s *cmd_ctx,
622                 char *cmd, char **args, int argc)
623 {
624         /* check whether the interface is already configured */
625         if (jtag_interface)
626         {
627                 LOG_WARNING("Interface already configured, ignoring");
628                 return ERROR_OK;
629         }
630
631         /* interface name is a mandatory argument */
632         if (argc != 1 || args[0][0] == '\0')
633                 return ERROR_COMMAND_SYNTAX_ERROR;
634
635         for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
636         {
637                 if (strcmp(args[0], jtag_interfaces[i]->name) != 0)
638                         continue;
639
640                 int retval = jtag_interfaces[i]->register_commands(cmd_ctx);
641                 if (ERROR_OK != retval)
642                                 return retval;
643
644                 jtag_interface = jtag_interfaces[i];
645
646                 if (jtag_interface->khz == NULL)
647                         jtag_interface->khz = default_khz;
648                 if (jtag_interface->speed_div == NULL)
649                         jtag_interface->speed_div = default_speed_div;
650                 if (jtag_interface->power_dropout == NULL)
651                         jtag_interface->power_dropout = default_power_dropout;
652                 if (jtag_interface->srst_asserted == NULL)
653                         jtag_interface->srst_asserted = default_srst_asserted;
654
655                 return ERROR_OK;
656         }
657
658         /* no valid interface was found (i.e. the configuration option,
659          * didn't match one of the compiled-in interfaces
660          */
661         LOG_ERROR("The specified JTAG interface was not found (%s)", args[0]);
662         handle_interface_list_command(cmd_ctx, cmd, args, argc);
663         return ERROR_JTAG_INVALID_INTERFACE;
664 }
665
666 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
667 {
668         jtag_tap_t *tap;
669
670         tap = jtag_all_taps();
671         command_print(cmd_ctx, "     TapName            | Enabled |   IdCode      Expected    IrLen IrCap  IrMask Instr     ");
672         command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
673
674         while (tap) {
675                 uint32_t expected, expected_mask, cur_instr, ii;
676                 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
677                 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
678                 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
679
680                 command_print(cmd_ctx,
681                                           "%2d | %-18s |    %c    | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
682                                           tap->abs_chain_position,
683                                           tap->dotted_name,
684                                           tap->enabled ? 'Y' : 'n',
685                                           (unsigned int)(tap->idcode),
686                                           (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
687                                           (unsigned int)(tap->ir_length),
688                                           (unsigned int)(expected),
689                                           (unsigned int)(expected_mask),
690                                           (unsigned int)(cur_instr));
691
692                 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
693                         command_print(cmd_ctx, "   |                    |         |            | 0x%08x |      |      |      |         ",
694                                                   (unsigned int)(tap->expected_ids[ii]));
695                 }
696
697                 tap = tap->next_tap;
698         }
699
700         return ERROR_OK;
701 }
702
703 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
704 {
705         int new_cfg = 0;
706         int mask = 0;
707
708         /* Original versions cared about the order of these tokens:
709          *   reset_config signals [combination [trst_type [srst_type]]]
710          * They also clobbered the previous configuration even on error.
711          *
712          * Here we don't care about the order, and only change values
713          * which have been explicitly specified.
714          */
715         for (; argc; argc--, args++) {
716                 int tmp = 0;
717                 int m;
718
719                 /* gating */
720                 m = RESET_SRST_NO_GATING;
721                 if (strcmp(*args, "srst_gates_jtag") == 0)
722                         /* default: don't use JTAG while SRST asserted */;
723                 else if (strcmp(*args, "srst_nogate") == 0)
724                         tmp = RESET_SRST_NO_GATING;
725                 else
726                         m = 0;
727                 if (mask & m) {
728                         LOG_ERROR("extra reset_config %s spec (%s)",
729                                         "gating", *args);
730                         return ERROR_INVALID_ARGUMENTS;
731                 }
732                 if (m)
733                         goto next;
734
735                 /* signals */
736                 m = RESET_HAS_TRST | RESET_HAS_SRST;
737                 if (strcmp(*args, "none") == 0)
738                         tmp = RESET_NONE;
739                 else if (strcmp(*args, "trst_only") == 0)
740                         tmp = RESET_HAS_TRST;
741                 else if (strcmp(*args, "srst_only") == 0)
742                         tmp = RESET_HAS_SRST;
743                 else if (strcmp(*args, "trst_and_srst") == 0)
744                         tmp = RESET_HAS_TRST | RESET_HAS_SRST;
745                 else
746                         m = 0;
747                 if (mask & m) {
748                         LOG_ERROR("extra reset_config %s spec (%s)",
749                                         "signal", *args);
750                         return ERROR_INVALID_ARGUMENTS;
751                 }
752                 if (m)
753                         goto next;
754
755                 /* combination (options for broken wiring) */
756                 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
757                 if (strcmp(*args, "separate") == 0)
758                         /* separate reset lines - default */;
759                 else if (strcmp(*args, "srst_pulls_trst") == 0)
760                         tmp |= RESET_SRST_PULLS_TRST;
761                 else if (strcmp(*args, "trst_pulls_srst") == 0)
762                         tmp |= RESET_TRST_PULLS_SRST;
763                 else if (strcmp(*args, "combined") == 0)
764                         tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
765                 else
766                         m = 0;
767                 if (mask & m) {
768                         LOG_ERROR("extra reset_config %s spec (%s)",
769                                         "combination", *args);
770                         return ERROR_INVALID_ARGUMENTS;
771                 }
772                 if (m)
773                         goto next;
774
775                 /* trst_type (NOP without HAS_TRST) */
776                 m = RESET_TRST_OPEN_DRAIN;
777                 if (strcmp(*args, "trst_open_drain") == 0)
778                         tmp |= RESET_TRST_OPEN_DRAIN;
779                 else if (strcmp(*args, "trst_push_pull") == 0)
780                         /* push/pull from adapter - default */;
781                 else
782                         m = 0;
783                 if (mask & m) {
784                         LOG_ERROR("extra reset_config %s spec (%s)",
785                                         "trst_type", *args);
786                         return ERROR_INVALID_ARGUMENTS;
787                 }
788                 if (m)
789                         goto next;
790
791                 /* srst_type (NOP without HAS_SRST) */
792                 m |= RESET_SRST_PUSH_PULL;
793                 if (strcmp(*args, "srst_push_pull") == 0)
794                         tmp |= RESET_SRST_PUSH_PULL;
795                 else if (strcmp(*args, "srst_open_drain") == 0)
796                         /* open drain from adapter - default */;
797                 else
798                         m = 0;
799                 if (mask & m) {
800                         LOG_ERROR("extra reset_config %s spec (%s)",
801                                         "srst_type", *args);
802                         return ERROR_INVALID_ARGUMENTS;
803                 }
804                 if (m)
805                         goto next;
806
807                 /* caller provided nonsense; fail */
808                 LOG_ERROR("unknown reset_config flag (%s)", *args);
809                 return ERROR_INVALID_ARGUMENTS;
810
811 next:
812                 /* Remember the bits which were specified (mask)
813                  * and their new values (new_cfg).
814                  */
815                 mask |= m;
816                 new_cfg |= tmp;
817         }
818
819         /* clear previous values of those bits, save new values */
820         if (mask) {
821                 int old_cfg = jtag_get_reset_config();
822
823                 old_cfg &= ~mask;
824                 new_cfg |= old_cfg;
825                 jtag_set_reset_config(new_cfg);
826         } else
827                 new_cfg = jtag_get_reset_config();
828
829
830         /*
831          * Display the (now-)current reset mode
832          */
833         char *modes[5];
834
835         /* minimal JTAG has neither SRST nor TRST (so that's the default) */
836         switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
837         case RESET_HAS_SRST:
838                 modes[0] = "srst_only";
839                 break;
840         case RESET_HAS_TRST:
841                 modes[0] = "trst_only";
842                 break;
843         case RESET_TRST_AND_SRST:
844                 modes[0] = "trst_and_srst";
845                 break;
846         default:
847                 modes[0] = "none";
848                 break;
849         }
850
851         /* normally SRST and TRST are decoupled; but bugs happen ... */
852         switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
853         case RESET_SRST_PULLS_TRST:
854                 modes[1] = "srst_pulls_trst";
855                 break;
856         case RESET_TRST_PULLS_SRST:
857                 modes[1] = "trst_pulls_srst";
858                 break;
859         case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
860                 modes[1] = "combined";
861                 break;
862         default:
863                 modes[1] = "separate";
864                 break;
865         }
866
867         /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
868         if (new_cfg & RESET_HAS_TRST) {
869                 if (new_cfg & RESET_TRST_OPEN_DRAIN)
870                         modes[3] = " trst_open_drain";
871                 else
872                         modes[3] = " trst_push_pull";
873         } else
874                 modes[3] = "";
875
876         /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
877         if (new_cfg & RESET_HAS_SRST) {
878                 if (new_cfg & RESET_SRST_NO_GATING)
879                         modes[2] = " srst_nogate";
880                 else
881                         modes[2] = " srst_gates_jtag";
882
883                 if (new_cfg & RESET_SRST_PUSH_PULL)
884                         modes[4] = " srst_push_pull";
885                 else
886                         modes[4] = " srst_open_drain";
887         } else {
888                 modes[2] = "";
889                 modes[4] = "";
890         }
891
892         command_print(cmd_ctx, "%s %s%s%s%s",
893                         modes[0], modes[1],
894                         modes[2], modes[3], modes[4]);
895
896         return ERROR_OK;
897 }
898
899 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx,
900                 char *cmd, char **args, int argc)
901 {
902         if (argc > 1)
903                 return ERROR_COMMAND_SYNTAX_ERROR;
904         if (argc == 1)
905         {
906                 unsigned delay;
907                 COMMAND_PARSE_NUMBER(uint, args[0], delay);
908
909                 jtag_set_nsrst_delay(delay);
910         }
911         command_print(cmd_ctx, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
912         return ERROR_OK;
913 }
914
915 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx,
916                 char *cmd, char **args, int argc)
917 {
918         if (argc > 1)
919                 return ERROR_COMMAND_SYNTAX_ERROR;
920         if (argc == 1)
921         {
922                 unsigned delay;
923                 COMMAND_PARSE_NUMBER(uint, args[0], delay);
924
925                 jtag_set_ntrst_delay(delay);
926         }
927         command_print(cmd_ctx, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
928         return ERROR_OK;
929 }
930
931 static int handle_jtag_nsrst_assert_width_command(struct command_context_s *cmd_ctx,
932                 char *cmd, char **args, int argc)
933 {
934         if (argc > 1)
935                 return ERROR_COMMAND_SYNTAX_ERROR;
936         if (argc == 1)
937         {
938                 unsigned delay;
939                 COMMAND_PARSE_NUMBER(uint, args[0], delay);
940
941                 jtag_set_nsrst_assert_width(delay);
942         }
943         command_print(cmd_ctx, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
944         return ERROR_OK;
945 }
946
947 static int handle_jtag_ntrst_assert_width_command(struct command_context_s *cmd_ctx,
948                 char *cmd, char **args, int argc)
949 {
950         if (argc > 1)
951                 return ERROR_COMMAND_SYNTAX_ERROR;
952         if (argc == 1)
953         {
954                 unsigned delay;
955                 COMMAND_PARSE_NUMBER(uint, args[0], delay);
956
957                 jtag_set_ntrst_assert_width(delay);
958         }
959         command_print(cmd_ctx, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
960         return ERROR_OK;
961 }
962
963 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
964 {
965         if (argc > 1)
966                 return ERROR_COMMAND_SYNTAX_ERROR;
967
968         int retval = ERROR_OK;
969         if (argc == 1)
970         {
971                 unsigned khz = 0;
972                 COMMAND_PARSE_NUMBER(uint, args[0], khz);
973
974                 retval = jtag_config_khz(khz);
975                 if (ERROR_OK != retval)
976                         return retval;
977         }
978
979         int cur_speed = jtag_get_speed_khz();
980         retval = jtag_get_speed_readable(&cur_speed);
981         if (ERROR_OK != retval)
982                 return retval;
983
984         if (cur_speed)
985                 command_print(cmd_ctx, "%d kHz", cur_speed);
986         else
987                 command_print(cmd_ctx, "RCLK - adaptive");
988
989         return retval;
990 }
991
992 static int handle_jtag_rclk_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
993 {
994         if (argc > 1)
995                 return ERROR_COMMAND_SYNTAX_ERROR;
996
997         int retval = ERROR_OK;
998         if (argc == 1)
999         {
1000                 unsigned khz = 0;
1001                 COMMAND_PARSE_NUMBER(uint, args[0], khz);
1002
1003                 retval = jtag_config_rclk(khz);
1004                 if (ERROR_OK != retval)
1005                         return retval;
1006         }
1007
1008         int cur_khz = jtag_get_speed_khz();
1009         retval = jtag_get_speed_readable(&cur_khz);
1010         if (ERROR_OK != retval)
1011                 return retval;
1012
1013         if (cur_khz)
1014                 command_print(cmd_ctx, "RCLK not supported - fallback to %d kHz", cur_khz);
1015         else
1016                 command_print(cmd_ctx, "RCLK - adaptive");
1017
1018         return retval;
1019 }
1020
1021 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx,
1022                 char *cmd, char **args, int argc)
1023 {
1024         if (argc != 2)
1025                 return ERROR_COMMAND_SYNTAX_ERROR;
1026
1027         int trst = -1;
1028         if (args[0][0] == '1')
1029                 trst = 1;
1030         else if (args[0][0] == '0')
1031                 trst = 0;
1032         else
1033                 return ERROR_COMMAND_SYNTAX_ERROR;
1034
1035         int srst = -1;
1036         if (args[1][0] == '1')
1037                 srst = 1;
1038         else if (args[1][0] == '0')
1039                 srst = 0;
1040         else
1041                 return ERROR_COMMAND_SYNTAX_ERROR;
1042
1043         if (jtag_interface_init(cmd_ctx) != ERROR_OK)
1044                 return ERROR_JTAG_INIT_FAILED;
1045
1046         jtag_add_reset(trst, srst);
1047         return jtag_execute_queue();
1048 }
1049
1050 static int handle_runtest_command(struct command_context_s *cmd_ctx,
1051                 char *cmd, char **args, int argc)
1052 {
1053         if (argc != 1)
1054                 return ERROR_COMMAND_SYNTAX_ERROR;
1055
1056         unsigned num_clocks;
1057         COMMAND_PARSE_NUMBER(uint, args[0], num_clocks);
1058
1059         jtag_add_runtest(num_clocks, TAP_IDLE);
1060         return jtag_execute_queue();
1061 }
1062
1063 /*
1064  * For "irscan" or "drscan" commands, the "end" (really, "next") state
1065  * should be stable ... and *NOT* a shift state, otherwise free-running
1066  * jtag clocks could change the values latched by the update state.
1067  * Not surprisingly, this is the same constraint as SVF; the "irscan"
1068  * and "drscan" commands are a write-only subset of what SVF provides.
1069  */
1070 static bool scan_is_safe(tap_state_t state)
1071 {
1072         switch (state)
1073         {
1074         case TAP_RESET:
1075         case TAP_IDLE:
1076         case TAP_DRPAUSE:
1077         case TAP_IRPAUSE:
1078                 return true;
1079         default:
1080                 return false;
1081         }
1082 }
1083
1084
1085 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1086 {
1087         int i;
1088         scan_field_t *fields;
1089         jtag_tap_t *tap;
1090         tap_state_t endstate;
1091
1092         if ((argc < 2) || (argc % 2))
1093         {
1094                 return ERROR_COMMAND_SYNTAX_ERROR;
1095         }
1096
1097         /* optional "-endstate" "statename" at the end of the arguments,
1098          * so that e.g. IRPAUSE can let us load the data register before
1099          * entering RUN/IDLE to execute the instruction we load here.
1100          */
1101         endstate = TAP_IDLE;
1102
1103         if (argc >= 4) {
1104                 /* have at least one pair of numbers. */
1105                 /* is last pair the magic text? */
1106                 if (strcmp("-endstate", args[argc - 2]) == 0) {
1107                         endstate = tap_state_by_name(args[argc - 1]);
1108                         if (endstate == TAP_INVALID)
1109                                 return ERROR_COMMAND_SYNTAX_ERROR;
1110                         if (!scan_is_safe(endstate))
1111                                 LOG_WARNING("unstable irscan endstate \"%s\"",
1112                                                 args[argc - 1]);
1113                         argc -= 2;
1114                 }
1115         }
1116
1117         int num_fields = argc / 2;
1118         size_t fields_len = sizeof(scan_field_t) * num_fields;
1119         fields = malloc(fields_len);
1120         memset(fields, 0, fields_len);
1121
1122         int retval;
1123         for (i = 0; i < num_fields; i++)
1124         {
1125                 tap = jtag_tap_by_string(args[i*2]);
1126                 if (tap == NULL)
1127                 {
1128                         int j;
1129                         for (j = 0; j < i; j++)
1130                                 free(fields[j].out_value);
1131                         free(fields);
1132                         command_print(cmd_ctx, "Tap: %s unknown", args[i*2]);
1133
1134                         return ERROR_FAIL;
1135                 }
1136                 int field_size = tap->ir_length;
1137                 fields[i].tap = tap;
1138                 fields[i].num_bits = field_size;
1139                 fields[i].out_value = malloc(CEIL(field_size, 8));
1140
1141                 uint32_t value;
1142                 retval = parse_u32(args[i * 2 + 1], &value);
1143                 if (ERROR_OK != retval)
1144                         goto error_return;
1145                 buf_set_u32(fields[i].out_value, 0, field_size, value);
1146                 fields[i].in_value = NULL;
1147         }
1148
1149         /* did we have an endstate? */
1150         jtag_add_ir_scan(num_fields, fields, endstate);
1151
1152         retval = jtag_execute_queue();
1153
1154 error_return:
1155         for (i = 0; i < num_fields; i++)
1156         {
1157                 if (NULL != fields[i].out_value)
1158                         free(fields[i].out_value);
1159         }
1160
1161         free (fields);
1162
1163         return retval;
1164 }
1165
1166 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1167 {
1168         int retval;
1169         scan_field_t *fields;
1170         int num_fields;
1171         int field_count = 0;
1172         int i, e;
1173         jtag_tap_t *tap;
1174         tap_state_t endstate;
1175
1176         /* args[1] = device
1177          * args[2] = num_bits
1178          * args[3] = hex string
1179          * ... repeat num bits and hex string ...
1180          *
1181          * .. optionally:
1182         *     args[N-2] = "-endstate"
1183          *     args[N-1] = statename
1184          */
1185         if ((argc < 4) || ((argc % 2) != 0))
1186         {
1187                 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1188                 return JIM_ERR;
1189         }
1190
1191         endstate = TAP_IDLE;
1192
1193         script_debug(interp, "drscan", argc, args);
1194
1195         /* validate arguments as numbers */
1196         e = JIM_OK;
1197         for (i = 2; i < argc; i += 2)
1198         {
1199                 long bits;
1200                 const char *cp;
1201
1202                 e = Jim_GetLong(interp, args[i], &bits);
1203                 /* If valid - try next arg */
1204                 if (e == JIM_OK) {
1205                         continue;
1206                 }
1207
1208                 /* Not valid.. are we at the end? */
1209                 if (((i + 2) != argc)) {
1210                         /* nope, then error */
1211                         return e;
1212                 }
1213
1214                 /* it could be: "-endstate FOO"
1215                  * e.g. DRPAUSE so we can issue more instructions
1216                  * before entering RUN/IDLE and executing them.
1217                  */
1218
1219                 /* get arg as a string. */
1220                 cp = Jim_GetString(args[i], NULL);
1221                 /* is it the magic? */
1222                 if (0 == strcmp("-endstate", cp)) {
1223                         /* is the statename valid? */
1224                         cp = Jim_GetString(args[i + 1], NULL);
1225
1226                         /* see if it is a valid state name */
1227                         endstate = tap_state_by_name(cp);
1228                         if (endstate < 0) {
1229                                 /* update the error message */
1230                                 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1231                         } else {
1232                                 if (!scan_is_safe(endstate))
1233                                         LOG_WARNING("drscan with unsafe "
1234                                                         "endstate \"%s\"", cp);
1235
1236                                 /* valid - so clear the error */
1237                                 e = JIM_OK;
1238                                 /* and remove the last 2 args */
1239                                 argc -= 2;
1240                         }
1241                 }
1242
1243                 /* Still an error? */
1244                 if (e != JIM_OK) {
1245                         return e; /* too bad */
1246                 }
1247         } /* validate args */
1248
1249         tap = jtag_tap_by_jim_obj(interp, args[1]);
1250         if (tap == NULL) {
1251                 return JIM_ERR;
1252         }
1253
1254         num_fields = (argc-2)/2;
1255         fields = malloc(sizeof(scan_field_t) * num_fields);
1256         for (i = 2; i < argc; i += 2)
1257         {
1258                 long bits;
1259                 int len;
1260                 const char *str;
1261
1262                 Jim_GetLong(interp, args[i], &bits);
1263                 str = Jim_GetString(args[i + 1], &len);
1264
1265                 fields[field_count].tap = tap;
1266                 fields[field_count].num_bits = bits;
1267                 fields[field_count].out_value = malloc(CEIL(bits, 8));
1268                 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
1269                 fields[field_count].in_value = fields[field_count].out_value;
1270                 field_count++;
1271         }
1272
1273         jtag_add_dr_scan(num_fields, fields, endstate);
1274
1275         retval = jtag_execute_queue();
1276         if (retval != ERROR_OK)
1277         {
1278                 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
1279                 return JIM_ERR;
1280         }
1281
1282         field_count = 0;
1283         Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
1284         for (i = 2; i < argc; i += 2)
1285         {
1286                 long bits;
1287                 char *str;
1288
1289                 Jim_GetLong(interp, args[i], &bits);
1290                 str = buf_to_str(fields[field_count].in_value, bits, 16);
1291                 free(fields[field_count].out_value);
1292
1293                 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
1294                 free(str);
1295                 field_count++;
1296         }
1297
1298         Jim_SetResult(interp, list);
1299
1300         free(fields);
1301
1302         return JIM_OK;
1303 }
1304
1305
1306 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1307 {
1308         tap_state_t states[8];
1309
1310         if ((argc < 2) || ((size_t)argc > (sizeof(states)/sizeof(*states) + 1)))
1311         {
1312                 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1313                 return JIM_ERR;
1314         }
1315
1316         script_debug(interp, "pathmove", argc, args);
1317
1318         int i;
1319         for (i = 0; i < argc-1; i++)
1320         {
1321                 const char *cp;
1322                 cp = Jim_GetString(args[i + 1], NULL);
1323                 states[i] = tap_state_by_name(cp);
1324                 if (states[i] < 0)
1325                 {
1326                         /* update the error message */
1327                         Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1328                         return JIM_ERR;
1329                 }
1330         }
1331
1332         if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
1333         {
1334                 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
1335                 return JIM_ERR;
1336         }
1337
1338         jtag_add_pathmove(argc-2, states + 1);
1339
1340         if (jtag_execute_queue()!= ERROR_OK)
1341         {
1342                 Jim_SetResultString(interp, "pathmove: failed",-1);
1343                 return JIM_ERR;
1344         }
1345
1346         return JIM_OK;
1347 }
1348
1349
1350 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1351 {
1352         script_debug(interp, "flush_count", argc, args);
1353
1354         Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
1355
1356         return JIM_OK;
1357 }
1358
1359
1360 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1361 {
1362         if (argc > 1)
1363                 return ERROR_COMMAND_SYNTAX_ERROR;
1364
1365         if (argc == 1)
1366         {
1367                 if (strcmp(args[0], "enable") == 0)
1368                         jtag_set_verify_capture_ir(true);
1369                 else if (strcmp(args[0], "disable") == 0)
1370                         jtag_set_verify_capture_ir(false);
1371                 else
1372                         return ERROR_COMMAND_SYNTAX_ERROR;
1373         }
1374
1375         const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1376         command_print(cmd_ctx, "verify Capture-IR is %s", status);
1377
1378         return ERROR_OK;
1379 }
1380
1381 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1382 {
1383         if (argc > 1)
1384                 return ERROR_COMMAND_SYNTAX_ERROR;
1385
1386         if (argc == 1)
1387         {
1388                 if (strcmp(args[0], "enable") == 0)
1389                         jtag_set_verify(true);
1390                 else if (strcmp(args[0], "disable") == 0)
1391                         jtag_set_verify(false);
1392                 else
1393                         return ERROR_COMMAND_SYNTAX_ERROR;
1394         }
1395
1396         const char *status = jtag_will_verify() ? "enabled": "disabled";
1397         command_print(cmd_ctx, "verify jtag capture is %s", status);
1398
1399         return ERROR_OK;
1400 }
1401
1402 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1403 {
1404         if (argc > 1)
1405                 return ERROR_COMMAND_SYNTAX_ERROR;
1406
1407         if (argc == 1)
1408         {
1409                 bool use_new_table;
1410                 if (strcmp(args[0], "short") == 0)
1411                         use_new_table = true;
1412                 else if (strcmp(args[0], "long") == 0)
1413                         use_new_table = false;
1414                 else
1415                         return ERROR_COMMAND_SYNTAX_ERROR;
1416
1417                 tap_use_new_tms_table(use_new_table);
1418         }
1419
1420         command_print(cmd_ctx, "tms sequence is  %s",
1421                         tap_uses_new_tms_table() ? "short": "long");
1422
1423         return ERROR_OK;
1424 }
1425
1426 int jtag_register_commands(struct command_context_s *cmd_ctx)
1427 {
1428         register_jim(cmd_ctx, "jtag", jim_jtag_command,
1429                         "perform jtag tap actions");
1430
1431         register_command(cmd_ctx, NULL, "interface",
1432                         handle_interface_command, COMMAND_CONFIG,
1433                         "try to configure interface");
1434         register_command(cmd_ctx, NULL, "interface_list",
1435                         &handle_interface_list_command, COMMAND_ANY,
1436                         "list all built-in interfaces");
1437
1438         register_command(cmd_ctx, NULL, "jtag_khz",
1439                         handle_jtag_khz_command, COMMAND_ANY,
1440                         "set maximum jtag speed (if supported); "
1441                         "parameter is maximum khz, or 0 for adaptive clocking (RTCK).");
1442         register_command(cmd_ctx, NULL, "jtag_rclk",
1443                         handle_jtag_rclk_command, COMMAND_ANY,
1444                         "fallback_speed_khz - set JTAG speed to RCLK or use fallback speed");
1445         register_command(cmd_ctx, NULL, "reset_config",
1446                         handle_reset_config_command, COMMAND_ANY,
1447                         "reset_config "
1448                         "[none|trst_only|srst_only|trst_and_srst] "
1449                         "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
1450                         "[srst_gates_jtag|srst_nogate] "
1451                         "[trst_push_pull|trst_open_drain] "
1452                         "[srst_push_pull|srst_open_drain]");
1453
1454         register_command(cmd_ctx, NULL, "jtag_nsrst_delay",
1455                         handle_jtag_nsrst_delay_command, COMMAND_ANY,
1456                         "jtag_nsrst_delay <ms> "
1457                         "- delay after deasserting srst in ms");
1458         register_command(cmd_ctx, NULL, "jtag_ntrst_delay",
1459                         handle_jtag_ntrst_delay_command, COMMAND_ANY,
1460                         "jtag_ntrst_delay <ms> "
1461                         "- delay after deasserting trst in ms");
1462
1463         register_command(cmd_ctx, NULL, "jtag_nsrst_assert_width",
1464                         handle_jtag_nsrst_assert_width_command, COMMAND_ANY,
1465                         "jtag_nsrst_assert_width <ms> "
1466                         "- delay after asserting srst in ms");
1467         register_command(cmd_ctx, NULL, "jtag_ntrst_assert_width",
1468                         handle_jtag_ntrst_assert_width_command, COMMAND_ANY,
1469                         "jtag_ntrst_assert_width <ms> "
1470                         "- delay after asserting trst in ms");
1471
1472         register_command(cmd_ctx, NULL, "scan_chain",
1473                         handle_scan_chain_command, COMMAND_EXEC,
1474                         "print current scan chain configuration");
1475
1476         register_command(cmd_ctx, NULL, "jtag_reset",
1477                         handle_jtag_reset_command, COMMAND_EXEC,
1478                         "toggle reset lines <trst> <srst>");
1479         register_command(cmd_ctx, NULL, "runtest",
1480                         handle_runtest_command, COMMAND_EXEC,
1481                         "move to Run-Test/Idle, and execute <num_cycles>");
1482         register_command(cmd_ctx, NULL, "irscan",
1483                         handle_irscan_command, COMMAND_EXEC,
1484                         "execute IR scan <device> <instr> [dev2] [instr2] ...");
1485
1486         register_jim(cmd_ctx, "drscan", Jim_Command_drscan,
1487                         "execute DR scan <device> "
1488                         "<num_bits> <value> <num_bits1> <value2> ...");
1489
1490         register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count,
1491                         "returns number of times the JTAG queue has been flushed");
1492
1493         register_jim(cmd_ctx, "pathmove", Jim_Command_pathmove,
1494                         "<state1>,<state2>,<state3>... "
1495                         "- move JTAG to state1 then to state2, state3, etc.");
1496
1497         register_command(cmd_ctx, NULL, "verify_ircapture",
1498                         handle_verify_ircapture_command, COMMAND_ANY,
1499                         "verify value captured during Capture-IR <enable | disable>");
1500         register_command(cmd_ctx, NULL, "verify_jtag",
1501                         handle_verify_jtag_command, COMMAND_ANY,
1502                         "verify value capture <enable | disable>");
1503
1504         register_command(cmd_ctx, NULL, "tms_sequence",
1505                         handle_tms_sequence_command, COMMAND_ANY,
1506                         "choose short(default) or long tms_sequence <short | long>");
1507
1508         return ERROR_OK;
1509 }
1510
1511