Add get and set accessors for jtag_speed:
[openocd.git] / src / jtag / tcl.c
blobed24498b87e10e2847aa224bdbc7dac730afb384
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
34 #include "jtag.h"
35 #include "minidriver.h"
36 #include "interface.h"
38 #ifdef HAVE_STRINGS_H
39 #include <strings.h>
40 #endif
42 extern bool hasKHz;
44 extern const Jim_Nvp nvp_jtag_tap_event[];
46 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
49 #if BUILD_ECOSBOARD == 1
50 extern jtag_interface_t zy1000_interface;
51 #elif defined(BUILD_MINIDRIVER_DUMMY)
52 extern jtag_interface_t minidummy_interface;
53 #else // standard drivers
54 #if BUILD_PARPORT == 1
55 extern jtag_interface_t parport_interface;
56 #endif
58 #if BUILD_DUMMY == 1
59 extern jtag_interface_t dummy_interface;
60 #endif
62 #if BUILD_FT2232_FTD2XX == 1
63 extern jtag_interface_t ft2232_interface;
64 #endif
66 #if BUILD_FT2232_LIBFTDI == 1
67 extern jtag_interface_t ft2232_interface;
68 #endif
70 #if BUILD_AMTJTAGACCEL == 1
71 extern jtag_interface_t amt_jtagaccel_interface;
72 #endif
74 #if BUILD_EP93XX == 1
75 extern jtag_interface_t ep93xx_interface;
76 #endif
78 #if BUILD_AT91RM9200 == 1
79 extern jtag_interface_t at91rm9200_interface;
80 #endif
82 #if BUILD_GW16012 == 1
83 extern jtag_interface_t gw16012_interface;
84 #endif
86 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
87 extern jtag_interface_t presto_interface;
88 #endif
90 #if BUILD_USBPROG == 1
91 extern jtag_interface_t usbprog_interface;
92 #endif
94 #if BUILD_JLINK == 1
95 extern jtag_interface_t jlink_interface;
96 #endif
98 #if BUILD_VSLLINK == 1
99 extern jtag_interface_t vsllink_interface;
100 #endif
102 #if BUILD_RLINK == 1
103 extern jtag_interface_t rlink_interface;
104 #endif
106 #if BUILD_ARMJTAGEW == 1
107 extern jtag_interface_t armjtagew_interface;
108 #endif
109 #endif // standard drivers
112 * The list of built-in JTAG interfaces, containing entries for those
113 * drivers that were enabled by the @c configure script.
115 * The list should be defined to contain either one minidriver interface
116 * or some number of standard driver interfaces, never both.
118 jtag_interface_t *jtag_interfaces[] = {
119 #if BUILD_ECOSBOARD == 1
120 &zy1000_interface,
121 #elif defined(BUILD_MINIDRIVER_DUMMY)
122 &minidummy_interface,
123 #else // standard drivers
124 #if BUILD_PARPORT == 1
125 &parport_interface,
126 #endif
127 #if BUILD_DUMMY == 1
128 &dummy_interface,
129 #endif
130 #if BUILD_FT2232_FTD2XX == 1
131 &ft2232_interface,
132 #endif
133 #if BUILD_FT2232_LIBFTDI == 1
134 &ft2232_interface,
135 #endif
136 #if BUILD_AMTJTAGACCEL == 1
137 &amt_jtagaccel_interface,
138 #endif
139 #if BUILD_EP93XX == 1
140 &ep93xx_interface,
141 #endif
142 #if BUILD_AT91RM9200 == 1
143 &at91rm9200_interface,
144 #endif
145 #if BUILD_GW16012 == 1
146 &gw16012_interface,
147 #endif
148 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
149 &presto_interface,
150 #endif
151 #if BUILD_USBPROG == 1
152 &usbprog_interface,
153 #endif
154 #if BUILD_JLINK == 1
155 &jlink_interface,
156 #endif
157 #if BUILD_VSLLINK == 1
158 &vsllink_interface,
159 #endif
160 #if BUILD_RLINK == 1
161 &rlink_interface,
162 #endif
163 #if BUILD_ARMJTAGEW == 1
164 &armjtagew_interface,
165 #endif
166 #endif // standard drivers
167 NULL,
170 extern struct jtag_interface_s *jtag;
171 extern jtag_interface_t *jtag_interface;
173 /* jtag commands */
174 static int handle_interface_list_command(struct command_context_s *cmd_ctx,
175 char *cmd, char **args, int argc);
176 static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
177 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
178 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
179 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
180 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
181 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
182 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
184 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
186 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
187 static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
188 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
189 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
190 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
192 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
193 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
194 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
196 extern int jtag_examine_chain(void);
197 extern int jtag_validate_chain(void);
199 enum jtag_tap_cfg_param {
200 JCFG_EVENT
203 static Jim_Nvp nvp_config_opts[] = {
204 { .name = "-event", .value = JCFG_EVENT },
206 { .name = NULL, .value = -1 }
209 static int jtag_tap_configure_cmd( Jim_GetOptInfo *goi, jtag_tap_t * tap)
211 Jim_Nvp *n;
212 Jim_Obj *o;
213 int e;
215 /* parse config or cget options */
216 while (goi->argc > 0) {
217 Jim_SetEmptyResult (goi->interp);
219 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
220 if (e != JIM_OK) {
221 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
222 return e;
225 switch (n->value) {
226 case JCFG_EVENT:
227 if (goi->argc == 0) {
228 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ..." );
229 return JIM_ERR;
232 e = Jim_GetOpt_Nvp( goi, nvp_jtag_tap_event, &n );
233 if (e != JIM_OK) {
234 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
235 return e;
238 if (goi->isconfigure) {
239 if (goi->argc != 1) {
240 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
241 return JIM_ERR;
243 } else {
244 if (goi->argc != 0) {
245 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
246 return JIM_ERR;
251 jtag_tap_event_action_t *jteap;
253 jteap = tap->event_action;
254 /* replace existing? */
255 while (jteap) {
256 if (jteap->event == (enum jtag_tap_event)n->value) {
257 break;
259 jteap = jteap->next;
262 if (goi->isconfigure) {
263 if (jteap == NULL) {
264 /* create new */
265 jteap = calloc(1, sizeof (*jteap));
267 jteap->event = n->value;
268 Jim_GetOpt_Obj( goi, &o);
269 if (jteap->body) {
270 Jim_DecrRefCount(interp, jteap->body);
272 jteap->body = Jim_DuplicateObj(goi->interp, o);
273 Jim_IncrRefCount(jteap->body);
275 /* add to head of event list */
276 jteap->next = tap->event_action;
277 tap->event_action = jteap;
278 Jim_SetEmptyResult(goi->interp);
279 } else {
280 /* get */
281 if (jteap == NULL) {
282 Jim_SetEmptyResult(goi->interp);
283 } else {
284 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
288 /* loop for more */
289 break;
291 } /* while (goi->argc) */
293 return JIM_OK;
297 extern void jtag_tap_init(jtag_tap_t *tap);
298 extern void jtag_tap_free(jtag_tap_t *tap);
300 static int jim_newtap_cmd( Jim_GetOptInfo *goi )
302 jtag_tap_t *pTap;
303 jim_wide w;
304 int x;
305 int e;
306 int reqbits;
307 Jim_Nvp *n;
308 char *cp;
309 const Jim_Nvp opts[] = {
310 #define NTAP_OPT_IRLEN 0
311 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
312 #define NTAP_OPT_IRMASK 1
313 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
314 #define NTAP_OPT_IRCAPTURE 2
315 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
316 #define NTAP_OPT_ENABLED 3
317 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
318 #define NTAP_OPT_DISABLED 4
319 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
320 #define NTAP_OPT_EXPECTED_ID 5
321 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
322 { .name = NULL , .value = -1 },
325 pTap = malloc( sizeof(jtag_tap_t) );
326 memset( pTap, 0, sizeof(*pTap) );
327 if( !pTap ){
328 Jim_SetResult_sprintf( goi->interp, "no memory");
329 return JIM_ERR;
332 * we expect CHIP + TAP + OPTIONS
333 * */
334 if( goi->argc < 3 ){
335 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
336 return JIM_ERR;
338 Jim_GetOpt_String( goi, &cp, NULL );
339 pTap->chip = strdup(cp);
341 Jim_GetOpt_String( goi, &cp, NULL );
342 pTap->tapname = strdup(cp);
344 /* name + dot + name + null */
345 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
346 cp = malloc( x );
347 sprintf( cp, "%s.%s", pTap->chip, pTap->tapname );
348 pTap->dotted_name = cp;
350 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
351 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
353 /* default is enabled */
354 pTap->enabled = 1;
356 /* deal with options */
357 #define NTREQ_IRLEN 1
358 #define NTREQ_IRCAPTURE 2
359 #define NTREQ_IRMASK 4
361 /* clear them as we find them */
362 reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
364 while( goi->argc ){
365 e = Jim_GetOpt_Nvp( goi, opts, &n );
366 if( e != JIM_OK ){
367 Jim_GetOpt_NvpUnknown( goi, opts, 0 );
368 return e;
370 LOG_DEBUG("Processing option: %s", n->name );
371 switch( n->value ){
372 case NTAP_OPT_ENABLED:
373 pTap->enabled = 1;
374 break;
375 case NTAP_OPT_DISABLED:
376 pTap->enabled = 0;
377 break;
378 case NTAP_OPT_EXPECTED_ID:
380 u32 *new_expected_ids;
382 e = Jim_GetOpt_Wide( goi, &w );
383 if( e != JIM_OK) {
384 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
385 return e;
388 new_expected_ids = malloc(sizeof(u32) * (pTap->expected_ids_cnt + 1));
389 if (new_expected_ids == NULL) {
390 Jim_SetResult_sprintf( goi->interp, "no memory");
391 return JIM_ERR;
394 memcpy(new_expected_ids, pTap->expected_ids, sizeof(u32) * pTap->expected_ids_cnt);
396 new_expected_ids[pTap->expected_ids_cnt] = w;
398 free(pTap->expected_ids);
399 pTap->expected_ids = new_expected_ids;
400 pTap->expected_ids_cnt++;
401 break;
403 case NTAP_OPT_IRLEN:
404 case NTAP_OPT_IRMASK:
405 case NTAP_OPT_IRCAPTURE:
406 e = Jim_GetOpt_Wide( goi, &w );
407 if( e != JIM_OK ){
408 Jim_SetResult_sprintf( goi->interp, "option: %s bad parameter", n->name );
409 return e;
411 if( (w < 0) || (w > 0xffff) ){
412 /* wacky value */
413 Jim_SetResult_sprintf( goi->interp, "option: %s - wacky value: %d (0x%x)",
414 n->name, (int)(w), (int)(w));
415 return JIM_ERR;
417 switch(n->value){
418 case NTAP_OPT_IRLEN:
419 pTap->ir_length = w;
420 reqbits &= (~(NTREQ_IRLEN));
421 break;
422 case NTAP_OPT_IRMASK:
423 pTap->ir_capture_mask = w;
424 reqbits &= (~(NTREQ_IRMASK));
425 break;
426 case NTAP_OPT_IRCAPTURE:
427 pTap->ir_capture_value = w;
428 reqbits &= (~(NTREQ_IRCAPTURE));
429 break;
431 } /* switch(n->value) */
432 } /* while( goi->argc ) */
434 /* Did all the required option bits get cleared? */
435 if (0 == reqbits)
437 jtag_tap_init(pTap);
438 return ERROR_OK;
441 Jim_SetResult_sprintf(goi->interp,
442 "newtap: %s missing required parameters",
443 pTap->dotted_name);
444 jtag_tap_free(pTap);
445 return JIM_ERR;
448 static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
450 Jim_GetOptInfo goi;
451 int e;
452 Jim_Nvp *n;
453 Jim_Obj *o;
454 struct command_context_s *context;
456 enum {
457 JTAG_CMD_INTERFACE,
458 JTAG_CMD_INIT_RESET,
459 JTAG_CMD_NEWTAP,
460 JTAG_CMD_TAPENABLE,
461 JTAG_CMD_TAPDISABLE,
462 JTAG_CMD_TAPISENABLED,
463 JTAG_CMD_CONFIGURE,
464 JTAG_CMD_CGET
467 const Jim_Nvp jtag_cmds[] = {
468 { .name = "interface" , .value = JTAG_CMD_INTERFACE },
469 { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
470 { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
471 { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
472 { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
473 { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
474 { .name = "configure" , .value = JTAG_CMD_CONFIGURE },
475 { .name = "cget" , .value = JTAG_CMD_CGET },
477 { .name = NULL, .value = -1 },
480 context = Jim_GetAssocData(interp, "context");
481 /* go past the command */
482 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
484 e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
485 if( e != JIM_OK ){
486 Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
487 return e;
489 Jim_SetEmptyResult( goi.interp );
490 switch( n->value ){
491 case JTAG_CMD_INTERFACE:
492 /* return the name of the interface */
493 /* TCL code might need to know the exact type... */
494 /* FUTURE: we allow this as a means to "set" the interface. */
495 if( goi.argc != 0 ){
496 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
497 return JIM_ERR;
499 Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
500 return JIM_OK;
501 case JTAG_CMD_INIT_RESET:
502 if( goi.argc != 0 ){
503 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
504 return JIM_ERR;
506 e = jtag_init_reset(context);
507 if( e != ERROR_OK ){
508 Jim_SetResult_sprintf( goi.interp, "error: %d", e);
509 return JIM_ERR;
511 return JIM_OK;
512 case JTAG_CMD_NEWTAP:
513 return jim_newtap_cmd( &goi );
514 break;
515 case JTAG_CMD_TAPISENABLED:
516 case JTAG_CMD_TAPENABLE:
517 case JTAG_CMD_TAPDISABLE:
518 if( goi.argc != 1 ){
519 Jim_SetResultString( goi.interp, "Too many parameters",-1 );
520 return JIM_ERR;
524 jtag_tap_t *t;
525 t = jtag_tap_by_jim_obj( goi.interp, goi.argv[0] );
526 if( t == NULL ){
527 return JIM_ERR;
529 switch( n->value ){
530 case JTAG_CMD_TAPISENABLED:
531 e = t->enabled;
532 break;
533 case JTAG_CMD_TAPENABLE:
534 jtag_tap_handle_event( t, JTAG_TAP_EVENT_ENABLE);
535 e = 1;
536 t->enabled = e;
537 break;
538 case JTAG_CMD_TAPDISABLE:
539 jtag_tap_handle_event( t, JTAG_TAP_EVENT_DISABLE);
540 e = 0;
541 t->enabled = e;
542 break;
544 Jim_SetResult( goi.interp, Jim_NewIntObj( goi.interp, e ) );
545 return JIM_OK;
547 break;
549 case JTAG_CMD_CGET:
550 if( goi.argc < 2 ){
551 Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ...");
552 return JIM_ERR;
556 jtag_tap_t *t;
558 Jim_GetOpt_Obj(&goi, &o);
559 t = jtag_tap_by_jim_obj( goi.interp, o );
560 if( t == NULL ){
561 return JIM_ERR;
564 goi.isconfigure = 0;
565 return jtag_tap_configure_cmd( &goi, t);
567 break;
569 case JTAG_CMD_CONFIGURE:
570 if( goi.argc < 3 ){
571 Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ?VALUE? ...");
572 return JIM_ERR;
576 jtag_tap_t *t;
578 Jim_GetOpt_Obj(&goi, &o);
579 t = jtag_tap_by_jim_obj( goi.interp, o );
580 if( t == NULL ){
581 return JIM_ERR;
584 goi.isconfigure = 1;
585 return jtag_tap_configure_cmd( &goi, t);
589 return JIM_ERR;
592 int jtag_register_commands(struct command_context_s *cmd_ctx)
594 register_jim( cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
596 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
597 COMMAND_CONFIG, "try to configure interface");
598 register_command(cmd_ctx, NULL,
599 "interface_list", &handle_interface_list_command,
600 COMMAND_ANY, "list all built-in interfaces");
601 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
602 COMMAND_ANY, "(DEPRECATED) set jtag speed (if supported)");
603 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
604 COMMAND_ANY, "set maximum jtag speed (if supported); "
605 "parameter is maximum khz, or 0 for adaptive clocking (RTCK).");
606 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
607 COMMAND_CONFIG, "(DEPRECATED) jtag_device <ir_length> <ir_expected> <ir_mask>");
608 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
609 COMMAND_ANY,
610 "[none/trst_only/srst_only/trst_and_srst] [srst_pulls_trst/trst_pulls_srst] [combined/separate] [trst_push_pull/trst_open_drain] [srst_push_pull/srst_open_drain]");
611 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
612 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
613 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
614 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
616 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
617 COMMAND_EXEC, "print current scan chain configuration");
619 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
620 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
621 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
622 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
623 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
624 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
625 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
626 register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count, "returns number of times the JTAG queue has been flushed");
628 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
629 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
630 register_command(cmd_ctx, NULL, "verify_jtag", handle_verify_jtag_command,
631 COMMAND_ANY, "verify value capture <enable|disable>");
632 register_command(cmd_ctx, NULL, "tms_sequence", handle_tms_sequence_command,
633 COMMAND_ANY, "choose short(default) or long tms_sequence <short|long>");
634 return ERROR_OK;
637 static int default_khz(int khz, int *jtag_speed)
639 LOG_ERROR("Translation from khz to jtag_speed not implemented");
640 return ERROR_FAIL;
643 static int default_speed_div(int speed, int *khz)
645 LOG_ERROR("Translation from jtag_speed to khz not implemented");
646 return ERROR_FAIL;
649 static int default_power_dropout(int *dropout)
651 *dropout=0; /* by default we can't detect power dropout */
652 return ERROR_OK;
655 static int default_srst_asserted(int *srst_asserted)
657 *srst_asserted=0; /* by default we can't detect srst asserted */
658 return ERROR_OK;
661 static int handle_interface_command(struct command_context_s *cmd_ctx,
662 char *cmd, char **args, int argc)
664 /* check whether the interface is already configured */
665 if (jtag_interface)
667 LOG_WARNING("Interface already configured, ignoring");
668 return ERROR_OK;
671 /* interface name is a mandatory argument */
672 if (argc != 1 || args[0][0] == '\0')
673 return ERROR_COMMAND_SYNTAX_ERROR;
675 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
677 if (strcmp(args[0], jtag_interfaces[i]->name) != 0)
678 continue;
680 int retval = jtag_interfaces[i]->register_commands(cmd_ctx);
681 if (ERROR_OK != retval)
682 return retval;
684 jtag_interface = jtag_interfaces[i];
686 if (jtag_interface->khz == NULL)
687 jtag_interface->khz = default_khz;
688 if (jtag_interface->speed_div == NULL)
689 jtag_interface->speed_div = default_speed_div;
690 if (jtag_interface->power_dropout == NULL)
691 jtag_interface->power_dropout = default_power_dropout;
692 if (jtag_interface->srst_asserted == NULL)
693 jtag_interface->srst_asserted = default_srst_asserted;
695 return ERROR_OK;
698 /* no valid interface was found (i.e. the configuration option,
699 * didn't match one of the compiled-in interfaces
701 LOG_ERROR("The specified JTAG interface was not found (%s)", args[0]);
702 handle_interface_list_command(cmd_ctx, cmd, args, argc);
703 return ERROR_JTAG_INVALID_INTERFACE;
706 static int handle_interface_list_command(struct command_context_s *cmd_ctx,
707 char *cmd, char **args, int argc)
709 if (strcmp(cmd, "interface_list") == 0 && argc > 0)
710 return ERROR_COMMAND_SYNTAX_ERROR;
712 command_print(cmd_ctx, "The following JTAG interfaces are available:");
713 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
715 const char *name = jtag_interfaces[i]->name;
716 command_print(cmd_ctx, "%u: %s", i + 1, name);
719 return ERROR_OK;
722 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
724 int e;
725 char buf[1024];
726 Jim_Obj *newargs[ 10 ];
728 * CONVERT SYNTAX
729 * argv[-1] = command
730 * argv[ 0] = ir length
731 * argv[ 1] = ir capture
732 * argv[ 2] = ir mask
733 * argv[ 3] = not actually used by anything but in the docs
736 if( argc < 4 ){
737 command_print( cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
738 return ERROR_OK;
740 command_print( cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
741 command_print( cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
742 args[0],
743 args[1],
744 args[2] );
745 command_print( cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundaryscan(len5)");
746 command_print( cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
747 command_print( cmd_ctx, "jtag newtap stm32 boundary ....., and the tap: \"stm32.boundary\"");
748 command_print( cmd_ctx, "And then refer to the taps by the dotted name.");
750 newargs[0] = Jim_NewStringObj( interp, "jtag", -1 );
751 newargs[1] = Jim_NewStringObj( interp, "newtap", -1 );
752 sprintf( buf, "chip%d", jtag_tap_count() );
753 newargs[2] = Jim_NewStringObj( interp, buf, -1 );
754 sprintf( buf, "tap%d", jtag_tap_count() );
755 newargs[3] = Jim_NewStringObj( interp, buf, -1 );
756 newargs[4] = Jim_NewStringObj( interp, "-irlen", -1 );
757 newargs[5] = Jim_NewStringObj( interp, args[0], -1 );
758 newargs[6] = Jim_NewStringObj( interp, "-ircapture", -1 );
759 newargs[7] = Jim_NewStringObj( interp, args[1], -1 );
760 newargs[8] = Jim_NewStringObj( interp, "-irmask", -1 );
761 newargs[9] = Jim_NewStringObj( interp, args[2], -1 );
763 command_print( cmd_ctx, "NEW COMMAND:");
764 sprintf( buf, "%s %s %s %s %s %s %s %s %s %s",
765 Jim_GetString( newargs[0], NULL ),
766 Jim_GetString( newargs[1], NULL ),
767 Jim_GetString( newargs[2], NULL ),
768 Jim_GetString( newargs[3], NULL ),
769 Jim_GetString( newargs[4], NULL ),
770 Jim_GetString( newargs[5], NULL ),
771 Jim_GetString( newargs[6], NULL ),
772 Jim_GetString( newargs[7], NULL ),
773 Jim_GetString( newargs[8], NULL ),
774 Jim_GetString( newargs[9], NULL ) );
776 e = jim_jtag_command( interp, 10, newargs );
777 if( e != JIM_OK ){
778 command_print( cmd_ctx, "%s", Jim_GetString( Jim_GetResult(interp), NULL ) );
780 return e;
783 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
785 jtag_tap_t *tap;
787 tap = jtag_all_taps();
788 command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
789 command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
791 while( tap ){
792 u32 expected, expected_mask, cur_instr, ii;
793 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
794 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
795 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
797 command_print(cmd_ctx,
798 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
799 tap->abs_chain_position,
800 tap->dotted_name,
801 tap->enabled ? 'Y' : 'n',
802 tap->idcode,
803 (tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
804 tap->ir_length,
805 expected,
806 expected_mask,
807 cur_instr);
809 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
810 command_print(cmd_ctx, " | | | | 0x%08x | | | | ",
811 tap->expected_ids[ii]);
814 tap = tap->next_tap;
817 return ERROR_OK;
820 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
822 int new_cfg = 0;
823 int mask = 0;
825 if (argc < 1)
826 return ERROR_COMMAND_SYNTAX_ERROR;
828 /* Original versions cared about the order of these tokens:
829 * reset_config signals [combination [trst_type [srst_type]]]
830 * They also clobbered the previous configuration even on error.
832 * Here we don't care about the order, and only change values
833 * which have been explicitly specified.
835 for (; argc; argc--, args++) {
836 int tmp = 0;
837 int m;
839 /* signals */
840 m = RESET_HAS_TRST | RESET_HAS_SRST;
841 if (strcmp(*args, "none") == 0)
842 tmp = RESET_NONE;
843 else if (strcmp(*args, "trst_only") == 0)
844 tmp = RESET_HAS_TRST;
845 else if (strcmp(*args, "srst_only") == 0)
846 tmp = RESET_HAS_SRST;
847 else if (strcmp(*args, "trst_and_srst") == 0)
848 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
849 else
850 m = 0;
851 if (mask & m) {
852 LOG_ERROR("extra reset_config %s spec (%s)",
853 "signal", *args);
854 return ERROR_INVALID_ARGUMENTS;
856 if (m)
857 goto next;
859 /* combination (options for broken wiring) */
860 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
861 if (strcmp(*args, "separate") == 0)
862 /* separate reset lines - default */;
863 else if (strcmp(*args, "srst_pulls_trst") == 0)
864 tmp |= RESET_SRST_PULLS_TRST;
865 else if (strcmp(*args, "trst_pulls_srst") == 0)
866 tmp |= RESET_TRST_PULLS_SRST;
867 else if (strcmp(*args, "combined") == 0)
868 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
869 else
870 m = 0;
871 if (mask & m) {
872 LOG_ERROR("extra reset_config %s spec (%s)",
873 "combination", *args);
874 return ERROR_INVALID_ARGUMENTS;
876 if (m)
877 goto next;
879 /* trst_type (NOP without HAS_TRST) */
880 m = RESET_TRST_OPEN_DRAIN;
881 if (strcmp(*args, "trst_open_drain") == 0)
882 tmp |= RESET_TRST_OPEN_DRAIN;
883 else if (strcmp(*args, "trst_push_pull") == 0)
884 /* push/pull from adapter - default */;
885 else
886 m = 0;
887 if (mask & m) {
888 LOG_ERROR("extra reset_config %s spec (%s)",
889 "trst_type", *args);
890 return ERROR_INVALID_ARGUMENTS;
892 if (m)
893 goto next;
895 /* srst_type (NOP without HAS_SRST) */
896 m |= RESET_SRST_PUSH_PULL;
897 if (strcmp(*args, "srst_push_pull") == 0)
898 tmp |= RESET_SRST_PUSH_PULL;
899 else if (strcmp(*args, "srst_open_drain") == 0)
900 /* open drain from adapter - default */;
901 else
902 m = 0;
903 if (mask & m) {
904 LOG_ERROR("extra reset_config %s spec (%s)",
905 "srst_type", *args);
906 return ERROR_INVALID_ARGUMENTS;
908 if (m)
909 goto next;
911 /* caller provided nonsense; fail */
912 LOG_ERROR("unknown reset_config flag (%s)", *args);
913 return ERROR_INVALID_ARGUMENTS;
915 next:
916 /* Remember the bits which were specified (mask)
917 * and their new values (new_cfg).
919 mask |= m;
920 new_cfg |= tmp;
923 /* clear previous values of those bits, save new values */
924 jtag_reset_config &= ~mask;
925 jtag_reset_config |= new_cfg;
927 return ERROR_OK;
930 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx,
931 char *cmd, char **args, int argc)
933 if (argc > 1)
934 return ERROR_COMMAND_SYNTAX_ERROR;
935 if (argc == 1)
936 jtag_set_nsrst_delay(strtoul(args[0], NULL, 0));
937 command_print(cmd_ctx, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
938 return ERROR_OK;
941 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx,
942 char *cmd, char **args, int argc)
944 if (argc > 1)
945 return ERROR_COMMAND_SYNTAX_ERROR;
946 if (argc == 1)
947 jtag_set_ntrst_delay(strtoul(args[0], NULL, 0));
948 command_print(cmd_ctx, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
949 return ERROR_OK;
952 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
954 int retval = ERROR_OK;
956 if (argc > 1)
957 return ERROR_COMMAND_SYNTAX_ERROR;
958 if (argc == 1)
960 LOG_DEBUG("handle jtag speed");
962 int cur_speed = 0;
963 cur_speed = strtoul(args[0], NULL, 0);
964 retval = jtag_set_speed(cur_speed);
967 command_print(cmd_ctx, "jtag_speed: %d", jtag_get_speed());
969 return retval;
972 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
974 if (argc > 1)
975 return ERROR_COMMAND_SYNTAX_ERROR;
977 int retval = ERROR_OK;
978 int cur_speed = 0;
979 if (argc == 1)
981 LOG_DEBUG("handle jtag khz");
983 jtag_set_speed_khz(strtoul(args[0], NULL, 0));
984 if (jtag != NULL)
986 LOG_DEBUG("have interface set up");
987 int speed_div1;
988 retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
989 if (ERROR_OK != retval)
991 jtag_set_speed_khz(0);
992 return retval;
994 cur_speed = speed_div1;
996 retval = jtag_set_speed(cur_speed);
998 else
999 hasKHz = true;
1002 cur_speed = jtag_get_speed_khz();
1003 if (jtag != NULL)
1005 retval = jtag->speed_div(jtag_get_speed(), &cur_speed);
1006 if (ERROR_OK != retval)
1007 return retval;
1010 if (cur_speed)
1011 command_print(cmd_ctx, "%d kHz", cur_speed);
1012 else
1013 command_print(cmd_ctx, "RCLK - adaptive");
1014 return retval;
1018 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx,
1019 char *cmd, char **args, int argc)
1021 if (argc != 2)
1022 return ERROR_COMMAND_SYNTAX_ERROR;
1024 int trst = -1;
1025 if (args[0][0] == '1')
1026 trst = 1;
1027 else if (args[0][0] == '0')
1028 trst = 0;
1029 else
1030 return ERROR_COMMAND_SYNTAX_ERROR;
1032 int srst = -1;
1033 if (args[1][0] == '1')
1034 srst = 1;
1035 else if (args[1][0] == '0')
1036 srst = 0;
1037 else
1038 return ERROR_COMMAND_SYNTAX_ERROR;
1040 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
1041 return ERROR_JTAG_INIT_FAILED;
1043 jtag_add_reset(trst, srst);
1044 jtag_execute_queue();
1046 return ERROR_OK;
1049 static int handle_runtest_command(struct command_context_s *cmd_ctx,
1050 char *cmd, char **args, int argc)
1052 if (argc != 1)
1053 return ERROR_COMMAND_SYNTAX_ERROR;
1055 jtag_add_runtest(strtol(args[0], NULL, 0), jtag_get_end_state());
1056 jtag_execute_queue();
1058 return ERROR_OK;
1062 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1063 * should be stable ... and *NOT* a shift state, otherwise free-running
1064 * jtag clocks could change the values latched by the update state.
1066 static bool scan_is_safe(tap_state_t state)
1068 switch (state)
1070 case TAP_RESET:
1071 case TAP_IDLE:
1072 case TAP_DRPAUSE:
1073 case TAP_IRPAUSE:
1074 return true;
1075 default:
1076 return false;
1081 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1083 int i;
1084 scan_field_t *fields;
1085 jtag_tap_t *tap;
1086 tap_state_t endstate;
1088 if ((argc < 2) || (argc % 2))
1090 return ERROR_COMMAND_SYNTAX_ERROR;
1093 /* optional "-endstate" "statename" at the end of the arguments,
1094 * so that e.g. IRPAUSE can let us load the data register before
1095 * entering RUN/IDLE to execute the instruction we load here.
1097 endstate = TAP_IDLE;
1099 if( argc >= 4 ){
1100 /* have at least one pair of numbers. */
1101 /* is last pair the magic text? */
1102 if( 0 == strcmp( "-endstate", args[ argc - 2 ] ) ){
1103 const char *cpA;
1104 const char *cpS;
1105 cpA = args[ argc-1 ];
1106 for( endstate = 0 ; endstate < TAP_NUM_STATES ; endstate++ ){
1107 cpS = tap_state_name( endstate );
1108 if( 0 == strcmp( cpA, cpS ) ){
1109 break;
1112 if( endstate >= TAP_NUM_STATES ){
1113 return ERROR_COMMAND_SYNTAX_ERROR;
1114 } else {
1115 if (!scan_is_safe(endstate))
1116 LOG_WARNING("irscan with unsafe "
1117 "endstate \"%s\"", cpA);
1118 /* found - remove the last 2 args */
1119 argc -= 2;
1124 int num_fields = argc / 2;
1126 fields = malloc(sizeof(scan_field_t) * num_fields);
1128 for (i = 0; i < num_fields; i++)
1130 tap = jtag_tap_by_string( args[i*2] );
1131 if (tap==NULL)
1133 command_print( cmd_ctx, "Tap: %s unknown", args[i*2] );
1134 return ERROR_FAIL;
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 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
1141 fields[i].in_value = NULL;
1144 /* did we have an endstate? */
1145 jtag_add_ir_scan(num_fields, fields, endstate);
1147 int retval=jtag_execute_queue();
1149 for (i = 0; i < num_fields; i++)
1150 free(fields[i].out_value);
1152 free (fields);
1154 return retval;
1157 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1159 int retval;
1160 scan_field_t *fields;
1161 int num_fields;
1162 int field_count = 0;
1163 int i, e;
1164 jtag_tap_t *tap;
1165 tap_state_t endstate;
1167 /* args[1] = device
1168 * args[2] = num_bits
1169 * args[3] = hex string
1170 * ... repeat num bits and hex string ...
1172 * .. optionally:
1173 * args[N-2] = "-endstate"
1174 * args[N-1] = statename
1176 if ((argc < 4) || ((argc % 2)!=0))
1178 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1179 return JIM_ERR;
1182 endstate = TAP_IDLE;
1184 /* validate arguments as numbers */
1185 e = JIM_OK;
1186 for (i = 2; i < argc; i+=2)
1188 long bits;
1189 const char *cp;
1191 e = Jim_GetLong(interp, args[i], &bits);
1192 /* If valid - try next arg */
1193 if( e == JIM_OK ){
1194 continue;
1197 /* Not valid.. are we at the end? */
1198 if ( ((i+2) != argc) ){
1199 /* nope, then error */
1200 return e;
1203 /* it could be: "-endstate FOO"
1204 * e.g. DRPAUSE so we can issue more instructions
1205 * before entering RUN/IDLE and executing them.
1208 /* get arg as a string. */
1209 cp = Jim_GetString( args[i], NULL );
1210 /* is it the magic? */
1211 if( 0 == strcmp( "-endstate", cp ) ){
1212 /* is the statename valid? */
1213 cp = Jim_GetString( args[i+1], NULL );
1215 /* see if it is a valid state name */
1216 endstate = tap_state_by_name(cp);
1217 if( endstate < 0 ){
1218 /* update the error message */
1219 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp );
1220 } else {
1221 if (!scan_is_safe(endstate))
1222 LOG_WARNING("drscan with unsafe "
1223 "endstate \"%s\"", cp);
1225 /* valid - so clear the error */
1226 e = JIM_OK;
1227 /* and remove the last 2 args */
1228 argc -= 2;
1232 /* Still an error? */
1233 if( e != JIM_OK ){
1234 return e; /* too bad */
1236 } /* validate args */
1238 tap = jtag_tap_by_jim_obj( interp, args[1] );
1239 if( tap == NULL ){
1240 return JIM_ERR;
1243 num_fields=(argc-2)/2;
1244 fields = malloc(sizeof(scan_field_t) * num_fields);
1245 for (i = 2; i < argc; i+=2)
1247 long bits;
1248 int len;
1249 const char *str;
1251 Jim_GetLong(interp, args[i], &bits);
1252 str = Jim_GetString(args[i+1], &len);
1254 fields[field_count].tap = tap;
1255 fields[field_count].num_bits = bits;
1256 fields[field_count].out_value = malloc(CEIL(bits, 8));
1257 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
1258 fields[field_count].in_value = fields[field_count].out_value;
1259 field_count++;
1262 jtag_add_dr_scan(num_fields, fields, endstate);
1264 retval = jtag_execute_queue();
1265 if (retval != ERROR_OK)
1267 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
1268 return JIM_ERR;
1271 field_count=0;
1272 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
1273 for (i = 2; i < argc; i+=2)
1275 long bits;
1276 char *str;
1278 Jim_GetLong(interp, args[i], &bits);
1279 str = buf_to_str(fields[field_count].in_value, bits, 16);
1280 free(fields[field_count].out_value);
1282 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
1283 free(str);
1284 field_count++;
1287 Jim_SetResult(interp, list);
1289 free(fields);
1291 return JIM_OK;
1295 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1297 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
1299 return JIM_OK;
1303 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1305 if (argc == 1)
1307 if (strcmp(args[0], "enable") == 0)
1309 jtag_verify_capture_ir = 1;
1311 else if (strcmp(args[0], "disable") == 0)
1313 jtag_verify_capture_ir = 0;
1314 } else
1316 return ERROR_COMMAND_SYNTAX_ERROR;
1318 } else if (argc != 0)
1320 return ERROR_COMMAND_SYNTAX_ERROR;
1323 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
1325 return ERROR_OK;
1328 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1330 if (argc > 1)
1331 return ERROR_COMMAND_SYNTAX_ERROR;
1333 if (argc == 1)
1335 if (strcmp(args[0], "enable") == 0)
1336 jtag_set_verify(true);
1337 else if (strcmp(args[0], "disable") == 0)
1338 jtag_set_verify(false);
1339 else
1340 return ERROR_COMMAND_SYNTAX_ERROR;
1343 const char *status = jtag_will_verify() ? "enabled": "disabled";
1344 command_print(cmd_ctx, "verify jtag capture is %s", status);
1346 return ERROR_OK;
1349 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1351 if (argc > 1)
1352 return ERROR_COMMAND_SYNTAX_ERROR;
1354 if (argc == 1)
1356 bool use_new_table;
1357 if (strcmp(args[0], "short") == 0)
1358 use_new_table = true;
1359 else if (strcmp(args[0], "long") == 0)
1360 use_new_table = false;
1361 else
1362 return ERROR_COMMAND_SYNTAX_ERROR;
1364 tap_use_new_tms_table(use_new_table);
1367 command_print(cmd_ctx, "tms sequence is %s",
1368 tap_uses_new_tms_table() ? "short": "long");
1370 return ERROR_OK;