1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2009 SoftPLC Corporation *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
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. *
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. *
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 ***************************************************************************/
35 #include "minidriver.h"
36 #include "interface.h"
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
;
59 extern jtag_interface_t dummy_interface
;
62 #if BUILD_FT2232_FTD2XX == 1
63 extern jtag_interface_t ft2232_interface
;
66 #if BUILD_FT2232_LIBFTDI == 1
67 extern jtag_interface_t ft2232_interface
;
70 #if BUILD_AMTJTAGACCEL == 1
71 extern jtag_interface_t amt_jtagaccel_interface
;
75 extern jtag_interface_t ep93xx_interface
;
78 #if BUILD_AT91RM9200 == 1
79 extern jtag_interface_t at91rm9200_interface
;
82 #if BUILD_GW16012 == 1
83 extern jtag_interface_t gw16012_interface
;
86 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
87 extern jtag_interface_t presto_interface
;
90 #if BUILD_USBPROG == 1
91 extern jtag_interface_t usbprog_interface
;
95 extern jtag_interface_t jlink_interface
;
98 #if BUILD_VSLLINK == 1
99 extern jtag_interface_t vsllink_interface
;
103 extern jtag_interface_t rlink_interface
;
106 #if BUILD_ARMJTAGEW == 1
107 extern jtag_interface_t armjtagew_interface
;
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
121 #elif defined(BUILD_MINIDRIVER_DUMMY)
122 &minidummy_interface
,
123 #else // standard drivers
124 #if BUILD_PARPORT == 1
130 #if BUILD_FT2232_FTD2XX == 1
133 #if BUILD_FT2232_LIBFTDI == 1
136 #if BUILD_AMTJTAGACCEL == 1
137 &amt_jtagaccel_interface
,
139 #if BUILD_EP93XX == 1
142 #if BUILD_AT91RM9200 == 1
143 &at91rm9200_interface
,
145 #if BUILD_GW16012 == 1
148 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
151 #if BUILD_USBPROG == 1
157 #if BUILD_VSLLINK == 1
163 #if BUILD_ARMJTAGEW == 1
164 &armjtagew_interface
,
166 #endif // standard drivers
170 extern struct jtag_interface_s
*jtag
;
171 extern jtag_interface_t
*jtag_interface
;
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
{
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
)
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
);
221 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
227 if (goi
->argc
== 0) {
228 Jim_WrongNumArgs( goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ..." );
232 e
= Jim_GetOpt_Nvp( goi
, nvp_jtag_tap_event
, &n
);
234 Jim_GetOpt_NvpUnknown(goi
, nvp_jtag_tap_event
, 1);
238 if (goi
->isconfigure
) {
239 if (goi
->argc
!= 1) {
240 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
244 if (goi
->argc
!= 0) {
245 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
251 jtag_tap_event_action_t
*jteap
;
253 jteap
= tap
->event_action
;
254 /* replace existing? */
256 if (jteap
->event
== (enum jtag_tap_event
)n
->value
) {
262 if (goi
->isconfigure
) {
265 jteap
= calloc(1, sizeof (*jteap
));
267 jteap
->event
= n
->value
;
268 Jim_GetOpt_Obj( goi
, &o
);
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
);
282 Jim_SetEmptyResult(goi
->interp
);
284 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, jteap
->body
));
291 } /* while (goi->argc) */
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
)
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
) );
328 Jim_SetResult_sprintf( goi
->interp
, "no memory");
332 * we expect CHIP + TAP + OPTIONS
335 Jim_SetResult_sprintf(goi
->interp
, "Missing CHIP TAP OPTIONS ....");
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;
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 */
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
);
365 e
= Jim_GetOpt_Nvp( goi
, opts
, &n
);
367 Jim_GetOpt_NvpUnknown( goi
, opts
, 0 );
370 LOG_DEBUG("Processing option: %s", n
->name
);
372 case NTAP_OPT_ENABLED
:
375 case NTAP_OPT_DISABLED
:
378 case NTAP_OPT_EXPECTED_ID
:
380 u32
*new_expected_ids
;
382 e
= Jim_GetOpt_Wide( goi
, &w
);
384 Jim_SetResult_sprintf(goi
->interp
, "option: %s bad parameter", n
->name
);
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");
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
++;
404 case NTAP_OPT_IRMASK
:
405 case NTAP_OPT_IRCAPTURE
:
406 e
= Jim_GetOpt_Wide( goi
, &w
);
408 Jim_SetResult_sprintf( goi
->interp
, "option: %s bad parameter", n
->name
);
411 if( (w
< 0) || (w
> 0xffff) ){
413 Jim_SetResult_sprintf( goi
->interp
, "option: %s - wacky value: %d (0x%x)",
414 n
->name
, (int)(w
), (int)(w
));
420 reqbits
&= (~(NTREQ_IRLEN
));
422 case NTAP_OPT_IRMASK
:
423 pTap
->ir_capture_mask
= w
;
424 reqbits
&= (~(NTREQ_IRMASK
));
426 case NTAP_OPT_IRCAPTURE
:
427 pTap
->ir_capture_value
= w
;
428 reqbits
&= (~(NTREQ_IRCAPTURE
));
431 } /* switch(n->value) */
432 } /* while( goi->argc ) */
434 /* Did all the required option bits get cleared? */
441 Jim_SetResult_sprintf(goi
->interp
,
442 "newtap: %s missing required parameters",
448 static int jim_jtag_command( Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
454 struct command_context_s
*context
;
462 JTAG_CMD_TAPISENABLED
,
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
);
486 Jim_GetOpt_NvpUnknown( &goi
, jtag_cmds
, 0 );
489 Jim_SetEmptyResult( goi
.interp
);
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. */
496 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
-1, "(no params)");
499 Jim_SetResultString( goi
.interp
, jtag_interface
->name
, -1 );
501 case JTAG_CMD_INIT_RESET
:
503 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
-1, "(no params)");
506 e
= jtag_init_reset(context
);
508 Jim_SetResult_sprintf( goi
.interp
, "error: %d", e
);
512 case JTAG_CMD_NEWTAP
:
513 return jim_newtap_cmd( &goi
);
515 case JTAG_CMD_TAPISENABLED
:
516 case JTAG_CMD_TAPENABLE
:
517 case JTAG_CMD_TAPDISABLE
:
519 Jim_SetResultString( goi
.interp
, "Too many parameters",-1 );
525 t
= jtag_tap_by_jim_obj( goi
.interp
, goi
.argv
[0] );
530 case JTAG_CMD_TAPISENABLED
:
533 case JTAG_CMD_TAPENABLE
:
534 jtag_tap_handle_event( t
, JTAG_TAP_EVENT_ENABLE
);
538 case JTAG_CMD_TAPDISABLE
:
539 jtag_tap_handle_event( t
, JTAG_TAP_EVENT_DISABLE
);
544 Jim_SetResult( goi
.interp
, Jim_NewIntObj( goi
.interp
, e
) );
551 Jim_WrongNumArgs( goi
.interp
, 0, NULL
, "?tap-name? -option ...");
558 Jim_GetOpt_Obj(&goi
, &o
);
559 t
= jtag_tap_by_jim_obj( goi
.interp
, o
);
565 return jtag_tap_configure_cmd( &goi
, t
);
569 case JTAG_CMD_CONFIGURE
:
571 Jim_WrongNumArgs( goi
.interp
, 0, NULL
, "?tap-name? -option ?VALUE? ...");
578 Jim_GetOpt_Obj(&goi
, &o
);
579 t
= jtag_tap_by_jim_obj( goi
.interp
, o
);
585 return jtag_tap_configure_cmd( &goi
, t
);
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
,
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>");
637 static int default_khz(int khz
, int *jtag_speed
)
639 LOG_ERROR("Translation from khz to jtag_speed not implemented");
643 static int default_speed_div(int speed
, int *khz
)
645 LOG_ERROR("Translation from jtag_speed to khz not implemented");
649 static int default_power_dropout(int *dropout
)
651 *dropout
=0; /* by default we can't detect power dropout */
655 static int default_srst_asserted(int *srst_asserted
)
657 *srst_asserted
=0; /* by default we can't detect srst asserted */
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 */
667 LOG_WARNING("Interface already configured, ignoring");
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)
680 int retval
= jtag_interfaces
[i
]->register_commands(cmd_ctx
);
681 if (ERROR_OK
!= 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
;
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
);
722 static int handle_jtag_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
726 Jim_Obj
*newargs
[ 10 ];
730 * argv[ 0] = ir length
731 * argv[ 1] = ir capture
733 * argv[ 3] = not actually used by anything but in the docs
737 command_print( cmd_ctx
, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
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",
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
);
778 command_print( cmd_ctx
, "%s", Jim_GetString( Jim_GetResult(interp
), NULL
) );
783 static int handle_scan_chain_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
787 tap
= jtag_all_taps();
788 command_print(cmd_ctx
, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
789 command_print(cmd_ctx
, "---|--------------------|---------|------------|------------|------|------|------|---------");
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
,
801 tap
->enabled
? 'Y' : 'n',
803 (tap
->expected_ids_cnt
> 0 ? tap
->expected_ids
[0] : 0),
809 for (ii
= 1; ii
< tap
->expected_ids_cnt
; ii
++) {
810 command_print(cmd_ctx
, " | | | | 0x%08x | | | | ",
811 tap
->expected_ids
[ii
]);
820 static int handle_reset_config_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
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
++) {
840 m
= RESET_HAS_TRST
| RESET_HAS_SRST
;
841 if (strcmp(*args
, "none") == 0)
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
;
852 LOG_ERROR("extra reset_config %s spec (%s)",
854 return ERROR_INVALID_ARGUMENTS
;
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
;
872 LOG_ERROR("extra reset_config %s spec (%s)",
873 "combination", *args
);
874 return ERROR_INVALID_ARGUMENTS
;
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 */;
888 LOG_ERROR("extra reset_config %s spec (%s)",
890 return ERROR_INVALID_ARGUMENTS
;
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 */;
904 LOG_ERROR("extra reset_config %s spec (%s)",
906 return ERROR_INVALID_ARGUMENTS
;
911 /* caller provided nonsense; fail */
912 LOG_ERROR("unknown reset_config flag (%s)", *args
);
913 return ERROR_INVALID_ARGUMENTS
;
916 /* Remember the bits which were specified (mask)
917 * and their new values (new_cfg).
923 /* clear previous values of those bits, save new values */
924 jtag_reset_config
&= ~mask
;
925 jtag_reset_config
|= new_cfg
;
930 static int handle_jtag_nsrst_delay_command(struct command_context_s
*cmd_ctx
,
931 char *cmd
, char **args
, int argc
)
934 return ERROR_COMMAND_SYNTAX_ERROR
;
936 jtag_set_nsrst_delay(strtoul(args
[0], NULL
, 0));
937 command_print(cmd_ctx
, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
941 static int handle_jtag_ntrst_delay_command(struct command_context_s
*cmd_ctx
,
942 char *cmd
, char **args
, int argc
)
945 return ERROR_COMMAND_SYNTAX_ERROR
;
947 jtag_set_ntrst_delay(strtoul(args
[0], NULL
, 0));
948 command_print(cmd_ctx
, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
952 static int handle_jtag_speed_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
954 int retval
= ERROR_OK
;
957 return ERROR_COMMAND_SYNTAX_ERROR
;
960 LOG_DEBUG("handle jtag speed");
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());
972 static int handle_jtag_khz_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
975 return ERROR_COMMAND_SYNTAX_ERROR
;
977 int retval
= ERROR_OK
;
981 LOG_DEBUG("handle jtag khz");
983 jtag_set_speed_khz(strtoul(args
[0], NULL
, 0));
986 LOG_DEBUG("have interface set up");
988 retval
= jtag
->khz(jtag_get_speed_khz(), &speed_div1
);
989 if (ERROR_OK
!= retval
)
991 jtag_set_speed_khz(0);
994 cur_speed
= speed_div1
;
996 retval
= jtag_set_speed(cur_speed
);
1002 cur_speed
= jtag_get_speed_khz();
1005 retval
= jtag
->speed_div(jtag_get_speed(), &cur_speed
);
1006 if (ERROR_OK
!= retval
)
1011 command_print(cmd_ctx
, "%d kHz", cur_speed
);
1013 command_print(cmd_ctx
, "RCLK - adaptive");
1018 static int handle_jtag_reset_command(struct command_context_s
*cmd_ctx
,
1019 char *cmd
, char **args
, int argc
)
1022 return ERROR_COMMAND_SYNTAX_ERROR
;
1025 if (args
[0][0] == '1')
1027 else if (args
[0][0] == '0')
1030 return ERROR_COMMAND_SYNTAX_ERROR
;
1033 if (args
[1][0] == '1')
1035 else if (args
[1][0] == '0')
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();
1049 static int handle_runtest_command(struct command_context_s
*cmd_ctx
,
1050 char *cmd
, char **args
, int argc
)
1053 return ERROR_COMMAND_SYNTAX_ERROR
;
1055 jtag_add_runtest(strtol(args
[0], NULL
, 0), jtag_get_end_state());
1056 jtag_execute_queue();
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
)
1081 static int handle_irscan_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1084 scan_field_t
*fields
;
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
;
1100 /* have at least one pair of numbers. */
1101 /* is last pair the magic text? */
1102 if( 0 == strcmp( "-endstate", args
[ argc
- 2 ] ) ){
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
) ){
1112 if( endstate
>= TAP_NUM_STATES
){
1113 return ERROR_COMMAND_SYNTAX_ERROR
;
1115 if (!scan_is_safe(endstate
))
1116 LOG_WARNING("irscan with unsafe "
1117 "endstate \"%s\"", cpA
);
1118 /* found - remove the last 2 args */
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] );
1133 command_print( cmd_ctx
, "Tap: %s unknown", args
[i
*2] );
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
);
1157 static int Jim_Command_drscan(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *args
)
1160 scan_field_t
*fields
;
1162 int field_count
= 0;
1165 tap_state_t endstate
;
1168 * args[2] = num_bits
1169 * args[3] = hex string
1170 * ... repeat num bits and hex string ...
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");
1182 endstate
= TAP_IDLE
;
1184 /* validate arguments as numbers */
1186 for (i
= 2; i
< argc
; i
+=2)
1191 e
= Jim_GetLong(interp
, args
[i
], &bits
);
1192 /* If valid - try next arg */
1197 /* Not valid.. are we at the end? */
1198 if ( ((i
+2) != argc
) ){
1199 /* nope, then error */
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
);
1218 /* update the error message */
1219 Jim_SetResult_sprintf(interp
,"endstate: %s invalid", cp
);
1221 if (!scan_is_safe(endstate
))
1222 LOG_WARNING("drscan with unsafe "
1223 "endstate \"%s\"", cp
);
1225 /* valid - so clear the error */
1227 /* and remove the last 2 args */
1232 /* Still an error? */
1234 return e
; /* too bad */
1236 } /* validate args */
1238 tap
= jtag_tap_by_jim_obj( interp
, args
[1] );
1243 num_fields
=(argc
-2)/2;
1244 fields
= malloc(sizeof(scan_field_t
) * num_fields
);
1245 for (i
= 2; i
< argc
; i
+=2)
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
;
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);
1272 Jim_Obj
*list
= Jim_NewListObj(interp
, NULL
, 0);
1273 for (i
= 2; i
< argc
; i
+=2)
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
)));
1287 Jim_SetResult(interp
, list
);
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()));
1303 static int handle_verify_ircapture_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
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;
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");
1328 static int handle_verify_jtag_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1331 return ERROR_COMMAND_SYNTAX_ERROR
;
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);
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
);
1349 static int handle_tms_sequence_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1352 return ERROR_COMMAND_SYNTAX_ERROR
;
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;
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");