transport: fix bug/typo in interface_transports command
[openocd/cortex.git] / src / jtag / adapter.c
blob37e6ee9ae315f861f5936ab1ad7ebcc78a5c48b1
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Ø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"
37 #include "interfaces.h"
38 #include "transport.h"
40 #ifdef HAVE_STRINGS_H
41 #include <strings.h>
42 #endif
44 /**
45 * @file
46 * Holds support for configuring debug adapters from TCl scripts.
49 extern struct jtag_interface *jtag_interface;
53 static int
54 jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
56 Jim_GetOptInfo goi;
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. */
62 if (goi.argc != 0) {
63 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
64 return JIM_ERR;
66 const char *name = jtag_interface ? jtag_interface->name : NULL;
67 Jim_SetResultString(goi.interp, name ? : "undefined", -1);
68 return JIM_OK;
72 static int default_khz(int khz, int *jtag_speed)
74 LOG_ERROR("Translation from khz to jtag_speed not implemented");
75 return ERROR_FAIL;
78 static int default_speed_div(int speed, int *khz)
80 LOG_ERROR("Translation from jtag_speed to khz not implemented");
81 return ERROR_FAIL;
84 static int default_power_dropout(int *dropout)
86 *dropout = 0; /* by default we can't detect power dropout */
87 return ERROR_OK;
90 static int default_srst_asserted(int *srst_asserted)
92 *srst_asserted = 0; /* by default we can't detect srst asserted */
93 return ERROR_OK;
96 COMMAND_HANDLER(interface_transport_command)
98 char **transports;
99 int retval;
101 retval = CALL_COMMAND_HANDLER(transport_list_parse, &transports);
102 if (retval != ERROR_OK) {
103 return retval;
106 retval = allow_transports(CMD_CTX, (const char **)transports);
108 if (retval != ERROR_OK) {
109 for (unsigned i = 0; transports[i]; i++)
110 free(transports[i]);
111 free(transports);
113 return retval;
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);
128 return ERROR_OK;
131 COMMAND_HANDLER(handle_interface_command)
133 /* check whether the interface is already configured */
134 if (jtag_interface)
136 LOG_WARNING("Interface already configured, ignoring");
137 return ERROR_OK;
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)
147 continue;
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)
154 return 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;
168 return ERROR_OK;
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)
181 int new_cfg = 0;
182 int mask = 0;
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++) {
192 int tmp = 0;
193 int m;
195 /* gating */
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;
201 else
202 m = 0;
203 if (mask & m) {
204 LOG_ERROR("extra reset_config %s spec (%s)",
205 "gating", *CMD_ARGV);
206 return ERROR_INVALID_ARGUMENTS;
208 if (m)
209 goto next;
211 /* signals */
212 m = RESET_HAS_TRST | RESET_HAS_SRST;
213 if (strcmp(*CMD_ARGV, "none") == 0)
214 tmp = RESET_NONE;
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;
221 else
222 m = 0;
223 if (mask & m) {
224 LOG_ERROR("extra reset_config %s spec (%s)",
225 "signal", *CMD_ARGV);
226 return ERROR_INVALID_ARGUMENTS;
228 if (m)
229 goto next;
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;
241 else
242 m = 0;
243 if (mask & m) {
244 LOG_ERROR("extra reset_config %s spec (%s)",
245 "combination", *CMD_ARGV);
246 return ERROR_INVALID_ARGUMENTS;
248 if (m)
249 goto next;
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 */;
257 else
258 m = 0;
259 if (mask & m) {
260 LOG_ERROR("extra reset_config %s spec (%s)",
261 "trst_type", *CMD_ARGV);
262 return ERROR_INVALID_ARGUMENTS;
264 if (m)
265 goto next;
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 */;
273 else
274 m = 0;
275 if (mask & m) {
276 LOG_ERROR("extra reset_config %s spec (%s)",
277 "srst_type", *CMD_ARGV);
278 return ERROR_INVALID_ARGUMENTS;
280 if (m)
281 goto next;
283 /* caller provided nonsense; fail */
284 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
285 return ERROR_INVALID_ARGUMENTS;
287 next:
288 /* Remember the bits which were specified (mask)
289 * and their new values (new_cfg).
291 mask |= m;
292 new_cfg |= tmp;
295 /* clear previous values of those bits, save new values */
296 if (mask) {
297 int old_cfg = jtag_get_reset_config();
299 old_cfg &= ~mask;
300 new_cfg |= old_cfg;
301 jtag_set_reset_config(new_cfg);
302 } else
303 new_cfg = jtag_get_reset_config();
307 * Display the (now-)current reset mode
309 char *modes[5];
311 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
312 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
313 case RESET_HAS_SRST:
314 modes[0] = "srst_only";
315 break;
316 case RESET_HAS_TRST:
317 modes[0] = "trst_only";
318 break;
319 case RESET_TRST_AND_SRST:
320 modes[0] = "trst_and_srst";
321 break;
322 default:
323 modes[0] = "none";
324 break;
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";
331 break;
332 case RESET_TRST_PULLS_SRST:
333 modes[1] = "trst_pulls_srst";
334 break;
335 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
336 modes[1] = "combined";
337 break;
338 default:
339 modes[1] = "separate";
340 break;
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";
347 else
348 modes[3] = " trst_push_pull";
349 } else
350 modes[3] = "";
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";
356 else
357 modes[2] = " srst_gates_jtag";
359 if (new_cfg & RESET_SRST_PUSH_PULL)
360 modes[4] = " srst_push_pull";
361 else
362 modes[4] = " srst_open_drain";
363 } else {
364 modes[2] = "";
365 modes[4] = "";
368 command_print(CMD_CTX, "%s %s%s%s%s",
369 modes[0], modes[1],
370 modes[2], modes[3], modes[4]);
372 return ERROR_OK;
375 COMMAND_HANDLER(handle_adapter_nsrst_delay_command)
377 if (CMD_ARGC > 1)
378 return ERROR_COMMAND_SYNTAX_ERROR;
379 if (CMD_ARGC == 1)
381 unsigned delay;
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());
387 return ERROR_OK;
390 COMMAND_HANDLER(handle_adapter_nsrst_assert_width_command)
392 if (CMD_ARGC > 1)
393 return ERROR_COMMAND_SYNTAX_ERROR;
394 if (CMD_ARGC == 1)
396 unsigned width;
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());
402 return ERROR_OK;
407 COMMAND_HANDLER(handle_adapter_khz_command)
409 if (CMD_ARGC > 1)
410 return ERROR_COMMAND_SYNTAX_ERROR;
412 int retval = ERROR_OK;
413 if (CMD_ARGC == 1)
415 unsigned khz = 0;
416 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
418 retval = jtag_config_khz(khz);
419 if (ERROR_OK != retval)
420 return retval;
423 int cur_speed = jtag_get_speed_khz();
424 retval = jtag_get_speed_readable(&cur_speed);
425 if (ERROR_OK != retval)
426 return retval;
428 if (cur_speed)
429 command_print(CMD_CTX, "%d kHz", cur_speed);
430 else
431 command_print(CMD_CTX, "RCLK - adaptive");
433 return retval;
436 static const struct command_registration interface_command_handlers[] = {
438 .name = "adapter_khz",
439 .handler = handle_adapter_khz_command,
440 .mode = COMMAND_ANY,
441 .help = "With an argument, change to the specified maximum "
442 "jtag speed. For JTAG, 0 KHz signifies adaptive "
443 " clocking. "
444 "With or without argument, display current setting.",
445 .usage = "[khz]",
448 .name = "adapter_name",
449 .mode = COMMAND_ANY,
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,
457 .mode = COMMAND_ANY,
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,
464 .mode = COMMAND_ANY,
465 .help = "delay after asserting SRST in ms",
466 .usage = "[milliseconds]",
469 .name = "interface",
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,
485 .mode = COMMAND_ANY,
486 .help = "List all built-in debug adapter interfaces (drivers)",
489 .name = "reset_config",
490 .handler = handle_reset_config_command,
491 .mode = COMMAND_ANY,
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);