1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Ø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"
37 #include "interfaces.h"
38 #include "transport.h"
46 * Holds support for configuring debug adapters from TCl scripts.
49 extern struct jtag_interface
*jtag_interface
;
54 jim_adapter_name(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
57 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
59 /* return the name of the interface */
60 /* TCL code might need to know the exact type... */
61 /* FUTURE: we allow this as a means to "set" the interface. */
63 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
-1, "(no params)");
66 const char *name
= jtag_interface
? jtag_interface
->name
: NULL
;
67 Jim_SetResultString(goi
.interp
, name
? : "undefined", -1);
72 static int default_khz(int khz
, int *jtag_speed
)
74 LOG_ERROR("Translation from khz to jtag_speed not implemented");
78 static int default_speed_div(int speed
, int *khz
)
80 LOG_ERROR("Translation from jtag_speed to khz not implemented");
84 static int default_power_dropout(int *dropout
)
86 *dropout
= 0; /* by default we can't detect power dropout */
90 static int default_srst_asserted(int *srst_asserted
)
92 *srst_asserted
= 0; /* by default we can't detect srst asserted */
96 COMMAND_HANDLER(interface_transport_command
)
101 retval
= CALL_COMMAND_HANDLER(transport_list_parse
, &transports
);
102 if (retval
!= ERROR_OK
) {
106 retval
= allow_transports(CMD_CTX
, (const char **)transports
);
108 if (retval
!= ERROR_OK
) {
109 for (unsigned i
= 0; transports
[i
]; i
++)
116 COMMAND_HANDLER(handle_interface_list_command
)
118 if (strcmp(CMD_NAME
, "interface_list") == 0 && CMD_ARGC
> 0)
119 return ERROR_COMMAND_SYNTAX_ERROR
;
121 command_print(CMD_CTX
, "The following debug interfaces are available:");
122 for (unsigned i
= 0; NULL
!= jtag_interfaces
[i
]; i
++)
124 const char *name
= jtag_interfaces
[i
]->name
;
125 command_print(CMD_CTX
, "%u: %s", i
+ 1, name
);
131 COMMAND_HANDLER(handle_interface_command
)
133 /* check whether the interface is already configured */
136 LOG_WARNING("Interface already configured, ignoring");
140 /* interface name is a mandatory argument */
141 if (CMD_ARGC
!= 1 || CMD_ARGV
[0][0] == '\0')
142 return ERROR_COMMAND_SYNTAX_ERROR
;
144 for (unsigned i
= 0; NULL
!= jtag_interfaces
[i
]; i
++)
146 if (strcmp(CMD_ARGV
[0], jtag_interfaces
[i
]->name
) != 0)
149 if (NULL
!= jtag_interfaces
[i
]->commands
)
151 int retval
= register_commands(CMD_CTX
, NULL
,
152 jtag_interfaces
[i
]->commands
);
153 if (ERROR_OK
!= retval
)
157 jtag_interface
= jtag_interfaces
[i
];
159 if (jtag_interface
->khz
== NULL
)
160 jtag_interface
->khz
= default_khz
;
161 if (jtag_interface
->speed_div
== NULL
)
162 jtag_interface
->speed_div
= default_speed_div
;
163 if (jtag_interface
->power_dropout
== NULL
)
164 jtag_interface
->power_dropout
= default_power_dropout
;
165 if (jtag_interface
->srst_asserted
== NULL
)
166 jtag_interface
->srst_asserted
= default_srst_asserted
;
171 /* no valid interface was found (i.e. the configuration option,
172 * didn't match one of the compiled-in interfaces
174 LOG_ERROR("The specified debug interface was not found (%s)", CMD_ARGV
[0]);
175 CALL_COMMAND_HANDLER(handle_interface_list_command
);
176 return ERROR_JTAG_INVALID_INTERFACE
;
179 COMMAND_HANDLER(handle_reset_config_command
)
184 /* Original versions cared about the order of these tokens:
185 * reset_config signals [combination [trst_type [srst_type]]]
186 * They also clobbered the previous configuration even on error.
188 * Here we don't care about the order, and only change values
189 * which have been explicitly specified.
191 for (; CMD_ARGC
; CMD_ARGC
--, CMD_ARGV
++) {
196 m
= RESET_SRST_NO_GATING
;
197 if (strcmp(*CMD_ARGV
, "srst_gates_jtag") == 0)
198 /* default: don't use JTAG while SRST asserted */;
199 else if (strcmp(*CMD_ARGV
, "srst_nogate") == 0)
200 tmp
= RESET_SRST_NO_GATING
;
204 LOG_ERROR("extra reset_config %s spec (%s)",
205 "gating", *CMD_ARGV
);
206 return ERROR_INVALID_ARGUMENTS
;
212 m
= RESET_HAS_TRST
| RESET_HAS_SRST
;
213 if (strcmp(*CMD_ARGV
, "none") == 0)
215 else if (strcmp(*CMD_ARGV
, "trst_only") == 0)
216 tmp
= RESET_HAS_TRST
;
217 else if (strcmp(*CMD_ARGV
, "srst_only") == 0)
218 tmp
= RESET_HAS_SRST
;
219 else if (strcmp(*CMD_ARGV
, "trst_and_srst") == 0)
220 tmp
= RESET_HAS_TRST
| RESET_HAS_SRST
;
224 LOG_ERROR("extra reset_config %s spec (%s)",
225 "signal", *CMD_ARGV
);
226 return ERROR_INVALID_ARGUMENTS
;
231 /* combination (options for broken wiring) */
232 m
= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
233 if (strcmp(*CMD_ARGV
, "separate") == 0)
234 /* separate reset lines - default */;
235 else if (strcmp(*CMD_ARGV
, "srst_pulls_trst") == 0)
236 tmp
|= RESET_SRST_PULLS_TRST
;
237 else if (strcmp(*CMD_ARGV
, "trst_pulls_srst") == 0)
238 tmp
|= RESET_TRST_PULLS_SRST
;
239 else if (strcmp(*CMD_ARGV
, "combined") == 0)
240 tmp
|= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
244 LOG_ERROR("extra reset_config %s spec (%s)",
245 "combination", *CMD_ARGV
);
246 return ERROR_INVALID_ARGUMENTS
;
251 /* trst_type (NOP without HAS_TRST) */
252 m
= RESET_TRST_OPEN_DRAIN
;
253 if (strcmp(*CMD_ARGV
, "trst_open_drain") == 0)
254 tmp
|= RESET_TRST_OPEN_DRAIN
;
255 else if (strcmp(*CMD_ARGV
, "trst_push_pull") == 0)
256 /* push/pull from adapter - default */;
260 LOG_ERROR("extra reset_config %s spec (%s)",
261 "trst_type", *CMD_ARGV
);
262 return ERROR_INVALID_ARGUMENTS
;
267 /* srst_type (NOP without HAS_SRST) */
268 m
|= RESET_SRST_PUSH_PULL
;
269 if (strcmp(*CMD_ARGV
, "srst_push_pull") == 0)
270 tmp
|= RESET_SRST_PUSH_PULL
;
271 else if (strcmp(*CMD_ARGV
, "srst_open_drain") == 0)
272 /* open drain from adapter - default */;
276 LOG_ERROR("extra reset_config %s spec (%s)",
277 "srst_type", *CMD_ARGV
);
278 return ERROR_INVALID_ARGUMENTS
;
283 /* caller provided nonsense; fail */
284 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV
);
285 return ERROR_INVALID_ARGUMENTS
;
288 /* Remember the bits which were specified (mask)
289 * and their new values (new_cfg).
295 /* clear previous values of those bits, save new values */
297 int old_cfg
= jtag_get_reset_config();
301 jtag_set_reset_config(new_cfg
);
303 new_cfg
= jtag_get_reset_config();
307 * Display the (now-)current reset mode
311 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
312 switch (new_cfg
& (RESET_HAS_TRST
| RESET_HAS_SRST
)) {
314 modes
[0] = "srst_only";
317 modes
[0] = "trst_only";
319 case RESET_TRST_AND_SRST
:
320 modes
[0] = "trst_and_srst";
327 /* normally SRST and TRST are decoupled; but bugs happen ... */
328 switch (new_cfg
& (RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
)) {
329 case RESET_SRST_PULLS_TRST
:
330 modes
[1] = "srst_pulls_trst";
332 case RESET_TRST_PULLS_SRST
:
333 modes
[1] = "trst_pulls_srst";
335 case RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
:
336 modes
[1] = "combined";
339 modes
[1] = "separate";
343 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
344 if (new_cfg
& RESET_HAS_TRST
) {
345 if (new_cfg
& RESET_TRST_OPEN_DRAIN
)
346 modes
[3] = " trst_open_drain";
348 modes
[3] = " trst_push_pull";
352 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
353 if (new_cfg
& RESET_HAS_SRST
) {
354 if (new_cfg
& RESET_SRST_NO_GATING
)
355 modes
[2] = " srst_nogate";
357 modes
[2] = " srst_gates_jtag";
359 if (new_cfg
& RESET_SRST_PUSH_PULL
)
360 modes
[4] = " srst_push_pull";
362 modes
[4] = " srst_open_drain";
368 command_print(CMD_CTX
, "%s %s%s%s%s",
370 modes
[2], modes
[3], modes
[4]);
375 COMMAND_HANDLER(handle_adapter_nsrst_delay_command
)
378 return ERROR_COMMAND_SYNTAX_ERROR
;
382 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
384 jtag_set_nsrst_delay(delay
);
386 command_print(CMD_CTX
, "adapter_nsrst_delay: %u", jtag_get_nsrst_delay());
390 COMMAND_HANDLER(handle_adapter_nsrst_assert_width_command
)
393 return ERROR_COMMAND_SYNTAX_ERROR
;
397 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], width
);
399 jtag_set_nsrst_assert_width(width
);
401 command_print(CMD_CTX
, "adapter_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
407 COMMAND_HANDLER(handle_adapter_khz_command
)
410 return ERROR_COMMAND_SYNTAX_ERROR
;
412 int retval
= ERROR_OK
;
416 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], khz
);
418 retval
= jtag_config_khz(khz
);
419 if (ERROR_OK
!= retval
)
423 int cur_speed
= jtag_get_speed_khz();
424 retval
= jtag_get_speed_readable(&cur_speed
);
425 if (ERROR_OK
!= retval
)
429 command_print(CMD_CTX
, "%d kHz", cur_speed
);
431 command_print(CMD_CTX
, "RCLK - adaptive");
436 static const struct command_registration interface_command_handlers
[] = {
438 .name
= "adapter_khz",
439 .handler
= handle_adapter_khz_command
,
441 .help
= "With an argument, change to the specified maximum "
442 "jtag speed. For JTAG, 0 KHz signifies adaptive "
444 "With or without argument, display current setting.",
448 .name
= "adapter_name",
450 .jim_handler
= jim_adapter_name
,
451 .help
= "Returns the name of the currently "
452 "selected adapter (driver)",
455 .name
= "adapter_nsrst_delay",
456 .handler
= handle_adapter_nsrst_delay_command
,
458 .help
= "delay after deasserting SRST in ms",
459 .usage
= "[milliseconds]",
462 .name
= "adapter_nsrst_assert_width",
463 .handler
= handle_adapter_nsrst_assert_width_command
,
465 .help
= "delay after asserting SRST in ms",
466 .usage
= "[milliseconds]",
470 .handler
= handle_interface_command
,
471 .mode
= COMMAND_CONFIG
,
472 .help
= "Select a debug adapter interface (driver)",
473 .usage
= "driver_name",
476 .name
= "interface_transports",
477 .handler
= interface_transport_command
,
478 .mode
= COMMAND_CONFIG
,
479 .help
= "Declare transports the interface supports.",
480 .usage
= "transport ... ",
483 .name
= "interface_list",
484 .handler
= handle_interface_list_command
,
486 .help
= "List all built-in debug adapter interfaces (drivers)",
489 .name
= "reset_config",
490 .handler
= handle_reset_config_command
,
492 .help
= "configure adapter reset behavior",
493 .usage
= "[none|trst_only|srst_only|trst_and_srst] "
494 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
495 "[srst_gates_jtag|srst_nogate] "
496 "[trst_push_pull|trst_open_drain] "
497 "[srst_push_pull|srst_open_drain]",
499 COMMAND_REGISTRATION_DONE
503 * Register the commands which deal with arbitrary debug adapter drivers.
505 * @todo Remove internal assumptions that all debug adapters use JTAG for
506 * transport. Various types and data structures are not named generically.
508 int interface_register_commands(struct command_context
*ctx
)
510 return register_commands(ctx
, NULL
, interface_command_handlers
);