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, see <http://www.gnu.org/licenses/>. *
27 ***************************************************************************/
34 #include "minidriver.h"
35 #include "interface.h"
36 #include "interfaces.h"
37 #include <transport/transport.h>
38 #include <jtag/drivers/jtag_usb_common.h>
46 * Holds support for configuring debug adapters from TCl scripts.
49 extern struct jtag_interface *jtag_interface;
50 const char * const jtag_only[] = { "jtag", NULL };
52 static int jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
55 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
57 /* return the name of the interface */
58 /* TCL code might need to know the exact type... */
59 /* FUTURE: we allow this as a means to "set" the interface. */
61 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
64 const char *name = jtag_interface ? jtag_interface->name : NULL;
65 Jim_SetResultString(goi.interp, name ? : "undefined", -1);
69 COMMAND_HANDLER(interface_transport_command)
74 retval = CALL_COMMAND_HANDLER(transport_list_parse, &transports);
75 if (retval != ERROR_OK)
78 retval = allow_transports(CMD_CTX, (const char **)transports);
80 if (retval != ERROR_OK) {
81 for (unsigned i = 0; transports[i]; i++)
88 COMMAND_HANDLER(handle_interface_list_command)
90 if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
91 return ERROR_COMMAND_SYNTAX_ERROR;
93 command_print(CMD, "The following debug interfaces are available:");
94 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++) {
95 const char *name = jtag_interfaces[i]->name;
96 command_print(CMD, "%u: %s", i + 1, name);
102 COMMAND_HANDLER(handle_interface_command)
106 /* check whether the interface is already configured */
107 if (jtag_interface) {
108 LOG_WARNING("Interface already configured, ignoring");
112 /* interface name is a mandatory argument */
113 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
114 return ERROR_COMMAND_SYNTAX_ERROR;
116 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++) {
117 if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0)
120 if (NULL != jtag_interfaces[i]->commands) {
121 retval = register_commands(CMD_CTX, NULL,
122 jtag_interfaces[i]->commands);
123 if (ERROR_OK != retval)
127 jtag_interface = jtag_interfaces[i];
129 return allow_transports(CMD_CTX, jtag_interface->transports);
132 /* no valid interface was found (i.e. the configuration option,
133 * didn't match one of the compiled-in interfaces
135 LOG_ERROR("The specified debug interface was not found (%s)",
137 CALL_COMMAND_HANDLER(handle_interface_list_command);
138 return ERROR_JTAG_INVALID_INTERFACE;
141 COMMAND_HANDLER(handle_reset_config_command)
146 /* Original versions cared about the order of these tokens:
147 * reset_config signals [combination [trst_type [srst_type]]]
148 * They also clobbered the previous configuration even on error.
150 * Here we don't care about the order, and only change values
151 * which have been explicitly specified.
153 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
158 m = RESET_SRST_NO_GATING;
159 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
160 /* default: don't use JTAG while SRST asserted */;
161 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
162 tmp = RESET_SRST_NO_GATING;
166 LOG_ERROR("extra reset_config %s spec (%s)",
167 "gating", *CMD_ARGV);
168 return ERROR_COMMAND_SYNTAX_ERROR;
174 m = RESET_HAS_TRST | RESET_HAS_SRST;
175 if (strcmp(*CMD_ARGV, "none") == 0)
177 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
178 tmp = RESET_HAS_TRST;
179 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
180 tmp = RESET_HAS_SRST;
181 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
182 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
186 LOG_ERROR("extra reset_config %s spec (%s)",
187 "signal", *CMD_ARGV);
188 return ERROR_COMMAND_SYNTAX_ERROR;
193 /* combination (options for broken wiring) */
194 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
195 if (strcmp(*CMD_ARGV, "separate") == 0)
196 /* separate reset lines - default */;
197 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
198 tmp |= RESET_SRST_PULLS_TRST;
199 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
200 tmp |= RESET_TRST_PULLS_SRST;
201 else if (strcmp(*CMD_ARGV, "combined") == 0)
202 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
206 LOG_ERROR("extra reset_config %s spec (%s)",
207 "combination", *CMD_ARGV);
208 return ERROR_COMMAND_SYNTAX_ERROR;
213 /* trst_type (NOP without HAS_TRST) */
214 m = RESET_TRST_OPEN_DRAIN;
215 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
216 tmp |= RESET_TRST_OPEN_DRAIN;
217 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
218 /* push/pull from adapter - default */;
222 LOG_ERROR("extra reset_config %s spec (%s)",
223 "trst_type", *CMD_ARGV);
224 return ERROR_COMMAND_SYNTAX_ERROR;
229 /* srst_type (NOP without HAS_SRST) */
230 m = RESET_SRST_PUSH_PULL;
231 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
232 tmp |= RESET_SRST_PUSH_PULL;
233 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
234 /* open drain from adapter - default */;
238 LOG_ERROR("extra reset_config %s spec (%s)",
239 "srst_type", *CMD_ARGV);
240 return ERROR_COMMAND_SYNTAX_ERROR;
245 /* connect_type - only valid when srst_nogate */
246 m = RESET_CNCT_UNDER_SRST;
247 if (strcmp(*CMD_ARGV, "connect_assert_srst") == 0)
248 tmp |= RESET_CNCT_UNDER_SRST;
249 else if (strcmp(*CMD_ARGV, "connect_deassert_srst") == 0)
250 /* connect normally - default */;
254 LOG_ERROR("extra reset_config %s spec (%s)",
255 "connect_type", *CMD_ARGV);
256 return ERROR_COMMAND_SYNTAX_ERROR;
261 /* caller provided nonsense; fail */
262 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
263 return ERROR_COMMAND_SYNTAX_ERROR;
266 /* Remember the bits which were specified (mask)
267 * and their new values (new_cfg).
273 /* clear previous values of those bits, save new values */
275 int old_cfg = jtag_get_reset_config();
279 jtag_set_reset_config(new_cfg);
281 new_cfg = jtag_get_reset_config();
284 * Display the (now-)current reset mode
288 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
289 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
291 modes[0] = "srst_only";
294 modes[0] = "trst_only";
296 case RESET_TRST_AND_SRST:
297 modes[0] = "trst_and_srst";
304 /* normally SRST and TRST are decoupled; but bugs happen ... */
305 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
306 case RESET_SRST_PULLS_TRST:
307 modes[1] = "srst_pulls_trst";
309 case RESET_TRST_PULLS_SRST:
310 modes[1] = "trst_pulls_srst";
312 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
313 modes[1] = "combined";
316 modes[1] = "separate";
320 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
321 if (new_cfg & RESET_HAS_TRST) {
322 if (new_cfg & RESET_TRST_OPEN_DRAIN)
323 modes[3] = " trst_open_drain";
325 modes[3] = " trst_push_pull";
329 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
330 if (new_cfg & RESET_HAS_SRST) {
331 if (new_cfg & RESET_SRST_NO_GATING)
332 modes[2] = " srst_nogate";
334 modes[2] = " srst_gates_jtag";
336 if (new_cfg & RESET_SRST_PUSH_PULL)
337 modes[4] = " srst_push_pull";
339 modes[4] = " srst_open_drain";
341 if (new_cfg & RESET_CNCT_UNDER_SRST)
342 modes[5] = " connect_assert_srst";
344 modes[5] = " connect_deassert_srst";
351 command_print(CMD, "%s %s%s%s%s%s",
353 modes[2], modes[3], modes[4], modes[5]);
358 COMMAND_HANDLER(handle_adapter_nsrst_delay_command)
361 return ERROR_COMMAND_SYNTAX_ERROR;
364 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
366 jtag_set_nsrst_delay(delay);
368 command_print(CMD, "adapter_nsrst_delay: %u", jtag_get_nsrst_delay());
372 COMMAND_HANDLER(handle_adapter_nsrst_assert_width_command)
375 return ERROR_COMMAND_SYNTAX_ERROR;
378 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], width);
380 jtag_set_nsrst_assert_width(width);
382 command_print(CMD, "adapter_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
386 COMMAND_HANDLER(handle_adapter_khz_command)
389 return ERROR_COMMAND_SYNTAX_ERROR;
391 int retval = ERROR_OK;
394 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
396 retval = jtag_config_khz(khz);
397 if (ERROR_OK != retval)
401 int cur_speed = jtag_get_speed_khz();
402 retval = jtag_get_speed_readable(&cur_speed);
403 if (ERROR_OK != retval)
407 command_print(CMD, "adapter speed: %d kHz", cur_speed);
409 command_print(CMD, "adapter speed: RCLK - adaptive");
414 #ifndef HAVE_JTAG_MINIDRIVER_H
415 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
416 COMMAND_HANDLER(handle_usb_location_command)
419 jtag_usb_set_location(CMD_ARGV[0]);
421 command_print(CMD, "adapter usb location: %s", jtag_usb_get_location());
425 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
427 static const struct command_registration adapter_usb_command_handlers[] = {
428 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
431 .handler = &handle_usb_location_command,
432 .mode = COMMAND_CONFIG,
433 .help = "display or set the USB bus location of the USB device",
434 .usage = "[<bus>-port[.port]...]",
436 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
437 COMMAND_REGISTRATION_DONE
439 #endif /* MINIDRIVER */
441 static const struct command_registration adapter_command_handlers[] = {
442 #ifndef HAVE_JTAG_MINIDRIVER_H
446 .help = "usb adapter command group",
448 .chain = adapter_usb_command_handlers,
450 #endif /* MINIDRIVER */
451 COMMAND_REGISTRATION_DONE
454 static const struct command_registration interface_command_handlers[] = {
458 .help = "adapter command group",
460 .chain = adapter_command_handlers,
463 .name = "adapter_khz",
464 .handler = handle_adapter_khz_command,
466 .help = "With an argument, change to the specified maximum "
467 "jtag speed. For JTAG, 0 KHz signifies adaptive "
469 "With or without argument, display current setting.",
473 .name = "adapter_name",
475 .jim_handler = jim_adapter_name,
476 .help = "Returns the name of the currently "
477 "selected adapter (driver)",
480 .name = "adapter_nsrst_delay",
481 .handler = handle_adapter_nsrst_delay_command,
483 .help = "delay after deasserting SRST in ms",
484 .usage = "[milliseconds]",
487 .name = "adapter_nsrst_assert_width",
488 .handler = handle_adapter_nsrst_assert_width_command,
490 .help = "delay after asserting SRST in ms",
491 .usage = "[milliseconds]",
495 .handler = handle_interface_command,
496 .mode = COMMAND_CONFIG,
497 .help = "Select a debug adapter interface (driver)",
498 .usage = "driver_name",
501 .name = "interface_transports",
502 .handler = interface_transport_command,
503 .mode = COMMAND_CONFIG,
504 .help = "Declare transports the interface supports.",
505 .usage = "transport ... ",
508 .name = "interface_list",
509 .handler = handle_interface_list_command,
511 .help = "List all built-in debug adapter interfaces (drivers)",
515 .name = "reset_config",
516 .handler = handle_reset_config_command,
518 .help = "configure adapter reset behavior",
519 .usage = "[none|trst_only|srst_only|trst_and_srst] "
520 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
521 "[srst_gates_jtag|srst_nogate] "
522 "[trst_push_pull|trst_open_drain] "
523 "[srst_push_pull|srst_open_drain] "
524 "[connect_deassert_srst|connect_assert_srst]",
526 COMMAND_REGISTRATION_DONE
530 * Register the commands which deal with arbitrary debug adapter drivers.
532 * @todo Remove internal assumptions that all debug adapters use JTAG for
533 * transport. Various types and data structures are not named generically.
535 int interface_register_commands(struct command_context *ctx)
537 return register_commands(ctx, NULL, interface_command_handlers);