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"
45 * Holds support for configuring debug adapters from TCl scripts.
48 extern struct jtag_interface *jtag_interface;
53 jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
56 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
58 /* return the name of the interface */
59 /* TCL code might need to know the exact type... */
60 /* FUTURE: we allow this as a means to "set" the interface. */
62 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
65 const char *name = jtag_interface ? jtag_interface->name : NULL;
66 Jim_SetResultString(goi.interp, name ? : "undefined", -1);
71 static int default_khz(int khz, int *jtag_speed)
73 LOG_ERROR("Translation from khz to jtag_speed not implemented");
77 static int default_speed_div(int speed, int *khz)
79 LOG_ERROR("Translation from jtag_speed to khz not implemented");
83 static int default_power_dropout(int *dropout)
85 *dropout = 0; /* by default we can't detect power dropout */
89 static int default_srst_asserted(int *srst_asserted)
91 *srst_asserted = 0; /* by default we can't detect srst asserted */
95 COMMAND_HANDLER(handle_interface_list_command)
97 if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
98 return ERROR_COMMAND_SYNTAX_ERROR;
100 command_print(CMD_CTX, "The following debug interfaces are available:");
101 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
103 const char *name = jtag_interfaces[i]->name;
104 command_print(CMD_CTX, "%u: %s", i + 1, name);
110 COMMAND_HANDLER(handle_interface_command)
112 /* check whether the interface is already configured */
115 LOG_WARNING("Interface already configured, ignoring");
119 /* interface name is a mandatory argument */
120 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
121 return ERROR_COMMAND_SYNTAX_ERROR;
123 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
125 if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0)
128 if (NULL != jtag_interfaces[i]->commands)
130 int retval = register_commands(CMD_CTX, NULL,
131 jtag_interfaces[i]->commands);
132 if (ERROR_OK != retval)
136 jtag_interface = jtag_interfaces[i];
138 if (jtag_interface->khz == NULL)
139 jtag_interface->khz = default_khz;
140 if (jtag_interface->speed_div == NULL)
141 jtag_interface->speed_div = default_speed_div;
142 if (jtag_interface->power_dropout == NULL)
143 jtag_interface->power_dropout = default_power_dropout;
144 if (jtag_interface->srst_asserted == NULL)
145 jtag_interface->srst_asserted = default_srst_asserted;
150 /* no valid interface was found (i.e. the configuration option,
151 * didn't match one of the compiled-in interfaces
153 LOG_ERROR("The specified debug interface was not found (%s)", CMD_ARGV[0]);
154 CALL_COMMAND_HANDLER(handle_interface_list_command);
155 return ERROR_JTAG_INVALID_INTERFACE;
158 COMMAND_HANDLER(handle_reset_config_command)
163 /* Original versions cared about the order of these tokens:
164 * reset_config signals [combination [trst_type [srst_type]]]
165 * They also clobbered the previous configuration even on error.
167 * Here we don't care about the order, and only change values
168 * which have been explicitly specified.
170 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
175 m = RESET_SRST_NO_GATING;
176 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
177 /* default: don't use JTAG while SRST asserted */;
178 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
179 tmp = RESET_SRST_NO_GATING;
183 LOG_ERROR("extra reset_config %s spec (%s)",
184 "gating", *CMD_ARGV);
185 return ERROR_INVALID_ARGUMENTS;
191 m = RESET_HAS_TRST | RESET_HAS_SRST;
192 if (strcmp(*CMD_ARGV, "none") == 0)
194 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
195 tmp = RESET_HAS_TRST;
196 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
197 tmp = RESET_HAS_SRST;
198 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
199 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
203 LOG_ERROR("extra reset_config %s spec (%s)",
204 "signal", *CMD_ARGV);
205 return ERROR_INVALID_ARGUMENTS;
210 /* combination (options for broken wiring) */
211 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
212 if (strcmp(*CMD_ARGV, "separate") == 0)
213 /* separate reset lines - default */;
214 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
215 tmp |= RESET_SRST_PULLS_TRST;
216 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
217 tmp |= RESET_TRST_PULLS_SRST;
218 else if (strcmp(*CMD_ARGV, "combined") == 0)
219 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
223 LOG_ERROR("extra reset_config %s spec (%s)",
224 "combination", *CMD_ARGV);
225 return ERROR_INVALID_ARGUMENTS;
230 /* trst_type (NOP without HAS_TRST) */
231 m = RESET_TRST_OPEN_DRAIN;
232 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
233 tmp |= RESET_TRST_OPEN_DRAIN;
234 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
235 /* push/pull from adapter - default */;
239 LOG_ERROR("extra reset_config %s spec (%s)",
240 "trst_type", *CMD_ARGV);
241 return ERROR_INVALID_ARGUMENTS;
246 /* srst_type (NOP without HAS_SRST) */
247 m |= RESET_SRST_PUSH_PULL;
248 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
249 tmp |= RESET_SRST_PUSH_PULL;
250 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
251 /* open drain from adapter - default */;
255 LOG_ERROR("extra reset_config %s spec (%s)",
256 "srst_type", *CMD_ARGV);
257 return ERROR_INVALID_ARGUMENTS;
262 /* caller provided nonsense; fail */
263 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
264 return ERROR_INVALID_ARGUMENTS;
267 /* Remember the bits which were specified (mask)
268 * and their new values (new_cfg).
274 /* clear previous values of those bits, save new values */
276 int old_cfg = jtag_get_reset_config();
280 jtag_set_reset_config(new_cfg);
282 new_cfg = jtag_get_reset_config();
286 * Display the (now-)current reset mode
290 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
291 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
293 modes[0] = "srst_only";
296 modes[0] = "trst_only";
298 case RESET_TRST_AND_SRST:
299 modes[0] = "trst_and_srst";
306 /* normally SRST and TRST are decoupled; but bugs happen ... */
307 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
308 case RESET_SRST_PULLS_TRST:
309 modes[1] = "srst_pulls_trst";
311 case RESET_TRST_PULLS_SRST:
312 modes[1] = "trst_pulls_srst";
314 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
315 modes[1] = "combined";
318 modes[1] = "separate";
322 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
323 if (new_cfg & RESET_HAS_TRST) {
324 if (new_cfg & RESET_TRST_OPEN_DRAIN)
325 modes[3] = " trst_open_drain";
327 modes[3] = " trst_push_pull";
331 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
332 if (new_cfg & RESET_HAS_SRST) {
333 if (new_cfg & RESET_SRST_NO_GATING)
334 modes[2] = " srst_nogate";
336 modes[2] = " srst_gates_jtag";
338 if (new_cfg & RESET_SRST_PUSH_PULL)
339 modes[4] = " srst_push_pull";
341 modes[4] = " srst_open_drain";
347 command_print(CMD_CTX, "%s %s%s%s%s",
349 modes[2], modes[3], modes[4]);
354 COMMAND_HANDLER(handle_adapter_nsrst_delay_command)
357 return ERROR_COMMAND_SYNTAX_ERROR;
361 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
363 jtag_set_nsrst_delay(delay);
365 command_print(CMD_CTX, "adapter_nsrst_delay: %u", jtag_get_nsrst_delay());
369 COMMAND_HANDLER(handle_adapter_nsrst_assert_width_command)
372 return ERROR_COMMAND_SYNTAX_ERROR;
376 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], width);
378 jtag_set_nsrst_assert_width(width);
380 command_print(CMD_CTX, "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;
395 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
397 retval = jtag_config_khz(khz);
398 if (ERROR_OK != retval)
402 int cur_speed = jtag_get_speed_khz();
403 retval = jtag_get_speed_readable(&cur_speed);
404 if (ERROR_OK != retval)
408 command_print(CMD_CTX, "%d kHz", cur_speed);
410 command_print(CMD_CTX, "RCLK - adaptive");
415 static const struct command_registration interface_command_handlers[] = {
417 .name = "adapter_khz",
418 .handler = handle_adapter_khz_command,
420 .help = "With an argument, change to the specified maximum "
421 "jtag speed. For JTAG, 0 KHz signifies adaptive "
423 "With or without argument, display current setting.",
427 .name = "adapter_name",
429 .jim_handler = jim_adapter_name,
430 .help = "Returns the name of the currently "
431 "selected adapter (driver)",
434 .name = "adapter_nsrst_delay",
435 .handler = handle_adapter_nsrst_delay_command,
437 .help = "delay after deasserting SRST in ms",
438 .usage = "[milliseconds]",
441 .name = "adapter_nsrst_assert_width",
442 .handler = handle_adapter_nsrst_assert_width_command,
444 .help = "delay after asserting SRST in ms",
445 .usage = "[milliseconds]",
449 .handler = handle_interface_command,
450 .mode = COMMAND_CONFIG,
451 .help = "Select a debug adapter interface (driver)",
452 .usage = "driver_name",
455 .name = "interface_list",
456 .handler = handle_interface_list_command,
458 .help = "List all built-in debug adapter interfaces (drivers)",
461 .name = "reset_config",
462 .handler = handle_reset_config_command,
464 .help = "configure adapter reset behavior",
465 .usage = "[none|trst_only|srst_only|trst_and_srst] "
466 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
467 "[srst_gates_jtag|srst_nogate] "
468 "[trst_push_pull|trst_open_drain] "
469 "[srst_push_pull|srst_open_drain]",
471 COMMAND_REGISTRATION_DONE
475 * Register the commands which deal with arbitrary debug adapter drivers.
477 * @todo Remove internal assumptions that all debug adapters use JTAG for
478 * transport. Various types and data structures are not named generically.
480 int interface_register_commands(struct command_context *ctx)
482 return register_commands(ctx, NULL, interface_command_handlers);