adapter: remove superfluous line breaks
[openocd.git] / src / jtag / adapter.c
blobec65827041b1de869151354f3baaf75281c50ba9
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 ***************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #include "jtag.h"
36 #include "minidriver.h"
37 #include "interface.h"
38 #include "interfaces.h"
39 #include <transport/transport.h>
41 #ifdef HAVE_STRINGS_H
42 #include <strings.h>
43 #endif
45 /**
46 * @file
47 * Holds support for configuring debug adapters from TCl scripts.
50 extern struct jtag_interface *jtag_interface;
51 const char *jtag_only[] = { "jtag", NULL };
53 static int jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
55 Jim_GetOptInfo goi;
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. */
61 if (goi.argc != 0) {
62 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
63 return JIM_ERR;
65 const char *name = jtag_interface ? jtag_interface->name : NULL;
66 Jim_SetResultString(goi.interp, name ? : "undefined", -1);
67 return JIM_OK;
70 static int default_khz(int khz, int *jtag_speed)
72 LOG_ERROR("Translation from khz to jtag_speed not implemented");
73 return ERROR_FAIL;
76 static int default_speed_div(int speed, int *khz)
78 LOG_ERROR("Translation from jtag_speed to khz not implemented");
79 return ERROR_FAIL;
82 static int default_power_dropout(int *dropout)
84 *dropout = 0; /* by default we can't detect power dropout */
85 return ERROR_OK;
88 static int default_srst_asserted(int *srst_asserted)
90 *srst_asserted = 0; /* by default we can't detect srst asserted */
91 return ERROR_OK;
94 COMMAND_HANDLER(interface_transport_command)
96 char **transports;
97 int retval;
99 retval = CALL_COMMAND_HANDLER(transport_list_parse, &transports);
100 if (retval != ERROR_OK)
101 return retval;
103 retval = allow_transports(CMD_CTX, (const char **)transports);
105 if (retval != ERROR_OK) {
106 for (unsigned i = 0; transports[i]; i++)
107 free(transports[i]);
108 free(transports);
110 return retval;
113 COMMAND_HANDLER(handle_interface_list_command)
115 if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
116 return ERROR_COMMAND_SYNTAX_ERROR;
118 command_print(CMD_CTX, "The following debug interfaces are available:");
119 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++) {
120 const char *name = jtag_interfaces[i]->name;
121 command_print(CMD_CTX, "%u: %s", i + 1, name);
124 return ERROR_OK;
127 COMMAND_HANDLER(handle_interface_command)
129 int retval;
131 /* check whether the interface is already configured */
132 if (jtag_interface) {
133 LOG_WARNING("Interface already configured, ignoring");
134 return ERROR_OK;
137 /* interface name is a mandatory argument */
138 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
139 return ERROR_COMMAND_SYNTAX_ERROR;
141 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++) {
142 if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0)
143 continue;
145 if (NULL != jtag_interfaces[i]->commands) {
146 retval = register_commands(CMD_CTX, NULL,
147 jtag_interfaces[i]->commands);
148 if (ERROR_OK != retval)
149 return retval;
152 jtag_interface = jtag_interfaces[i];
154 /* LEGACY SUPPORT ... adapter drivers must declare what
155 * transports they allow. Until they all do so, assume
156 * the legacy drivers are JTAG-only
158 if (!jtag_interface->transports)
159 LOG_WARNING("Adapter driver '%s' did not declare "
160 "which transports it allows; assuming "
161 "legacy JTAG-only", jtag_interface->name);
162 retval = allow_transports(CMD_CTX, jtag_interface->transports
163 ? jtag_interface->transports : jtag_only);
164 if (ERROR_OK != retval)
165 return retval;
167 if (jtag_interface->khz == NULL)
168 jtag_interface->khz = default_khz;
169 if (jtag_interface->speed_div == NULL)
170 jtag_interface->speed_div = default_speed_div;
171 if (jtag_interface->power_dropout == NULL)
172 jtag_interface->power_dropout = default_power_dropout;
173 if (jtag_interface->srst_asserted == NULL)
174 jtag_interface->srst_asserted = default_srst_asserted;
176 return ERROR_OK;
179 /* no valid interface was found (i.e. the configuration option,
180 * didn't match one of the compiled-in interfaces
182 LOG_ERROR("The specified debug interface was not found (%s)",
183 CMD_ARGV[0]);
184 CALL_COMMAND_HANDLER(handle_interface_list_command);
185 return ERROR_JTAG_INVALID_INTERFACE;
188 COMMAND_HANDLER(handle_reset_config_command)
190 int new_cfg = 0;
191 int mask = 0;
193 /* Original versions cared about the order of these tokens:
194 * reset_config signals [combination [trst_type [srst_type]]]
195 * They also clobbered the previous configuration even on error.
197 * Here we don't care about the order, and only change values
198 * which have been explicitly specified.
200 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
201 int tmp = 0;
202 int m;
204 /* gating */
205 m = RESET_SRST_NO_GATING;
206 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
207 /* default: don't use JTAG while SRST asserted */;
208 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
209 tmp = RESET_SRST_NO_GATING;
210 else
211 m = 0;
212 if (mask & m) {
213 LOG_ERROR("extra reset_config %s spec (%s)",
214 "gating", *CMD_ARGV);
215 return ERROR_COMMAND_SYNTAX_ERROR;
217 if (m)
218 goto next;
220 /* signals */
221 m = RESET_HAS_TRST | RESET_HAS_SRST;
222 if (strcmp(*CMD_ARGV, "none") == 0)
223 tmp = RESET_NONE;
224 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
225 tmp = RESET_HAS_TRST;
226 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
227 tmp = RESET_HAS_SRST;
228 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
229 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
230 else
231 m = 0;
232 if (mask & m) {
233 LOG_ERROR("extra reset_config %s spec (%s)",
234 "signal", *CMD_ARGV);
235 return ERROR_COMMAND_SYNTAX_ERROR;
237 if (m)
238 goto next;
240 /* combination (options for broken wiring) */
241 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
242 if (strcmp(*CMD_ARGV, "separate") == 0)
243 /* separate reset lines - default */;
244 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
245 tmp |= RESET_SRST_PULLS_TRST;
246 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
247 tmp |= RESET_TRST_PULLS_SRST;
248 else if (strcmp(*CMD_ARGV, "combined") == 0)
249 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
250 else
251 m = 0;
252 if (mask & m) {
253 LOG_ERROR("extra reset_config %s spec (%s)",
254 "combination", *CMD_ARGV);
255 return ERROR_COMMAND_SYNTAX_ERROR;
257 if (m)
258 goto next;
260 /* trst_type (NOP without HAS_TRST) */
261 m = RESET_TRST_OPEN_DRAIN;
262 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
263 tmp |= RESET_TRST_OPEN_DRAIN;
264 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
265 /* push/pull from adapter - default */;
266 else
267 m = 0;
268 if (mask & m) {
269 LOG_ERROR("extra reset_config %s spec (%s)",
270 "trst_type", *CMD_ARGV);
271 return ERROR_COMMAND_SYNTAX_ERROR;
273 if (m)
274 goto next;
276 /* srst_type (NOP without HAS_SRST) */
277 m |= RESET_SRST_PUSH_PULL;
278 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
279 tmp |= RESET_SRST_PUSH_PULL;
280 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
281 /* open drain from adapter - default */;
282 else
283 m = 0;
284 if (mask & m) {
285 LOG_ERROR("extra reset_config %s spec (%s)",
286 "srst_type", *CMD_ARGV);
287 return ERROR_COMMAND_SYNTAX_ERROR;
289 if (m)
290 goto next;
292 /* caller provided nonsense; fail */
293 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
294 return ERROR_COMMAND_SYNTAX_ERROR;
296 next:
297 /* Remember the bits which were specified (mask)
298 * and their new values (new_cfg).
300 mask |= m;
301 new_cfg |= tmp;
304 /* clear previous values of those bits, save new values */
305 if (mask) {
306 int old_cfg = jtag_get_reset_config();
308 old_cfg &= ~mask;
309 new_cfg |= old_cfg;
310 jtag_set_reset_config(new_cfg);
311 } else
312 new_cfg = jtag_get_reset_config();
315 * Display the (now-)current reset mode
317 char *modes[5];
319 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
320 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
321 case RESET_HAS_SRST:
322 modes[0] = "srst_only";
323 break;
324 case RESET_HAS_TRST:
325 modes[0] = "trst_only";
326 break;
327 case RESET_TRST_AND_SRST:
328 modes[0] = "trst_and_srst";
329 break;
330 default:
331 modes[0] = "none";
332 break;
335 /* normally SRST and TRST are decoupled; but bugs happen ... */
336 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
337 case RESET_SRST_PULLS_TRST:
338 modes[1] = "srst_pulls_trst";
339 break;
340 case RESET_TRST_PULLS_SRST:
341 modes[1] = "trst_pulls_srst";
342 break;
343 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
344 modes[1] = "combined";
345 break;
346 default:
347 modes[1] = "separate";
348 break;
351 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
352 if (new_cfg & RESET_HAS_TRST) {
353 if (new_cfg & RESET_TRST_OPEN_DRAIN)
354 modes[3] = " trst_open_drain";
355 else
356 modes[3] = " trst_push_pull";
357 } else
358 modes[3] = "";
360 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
361 if (new_cfg & RESET_HAS_SRST) {
362 if (new_cfg & RESET_SRST_NO_GATING)
363 modes[2] = " srst_nogate";
364 else
365 modes[2] = " srst_gates_jtag";
367 if (new_cfg & RESET_SRST_PUSH_PULL)
368 modes[4] = " srst_push_pull";
369 else
370 modes[4] = " srst_open_drain";
371 } else {
372 modes[2] = "";
373 modes[4] = "";
376 command_print(CMD_CTX, "%s %s%s%s%s",
377 modes[0], modes[1],
378 modes[2], modes[3], modes[4]);
380 return ERROR_OK;
383 COMMAND_HANDLER(handle_adapter_nsrst_delay_command)
385 if (CMD_ARGC > 1)
386 return ERROR_COMMAND_SYNTAX_ERROR;
387 if (CMD_ARGC == 1) {
388 unsigned delay;
389 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
391 jtag_set_nsrst_delay(delay);
393 command_print(CMD_CTX, "adapter_nsrst_delay: %u", jtag_get_nsrst_delay());
394 return ERROR_OK;
397 COMMAND_HANDLER(handle_adapter_nsrst_assert_width_command)
399 if (CMD_ARGC > 1)
400 return ERROR_COMMAND_SYNTAX_ERROR;
401 if (CMD_ARGC == 1) {
402 unsigned width;
403 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], width);
405 jtag_set_nsrst_assert_width(width);
407 command_print(CMD_CTX, "adapter_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
408 return ERROR_OK;
411 COMMAND_HANDLER(handle_adapter_khz_command)
413 if (CMD_ARGC > 1)
414 return ERROR_COMMAND_SYNTAX_ERROR;
416 int retval = ERROR_OK;
417 if (CMD_ARGC == 1) {
418 unsigned khz = 0;
419 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
421 retval = jtag_config_khz(khz);
422 if (ERROR_OK != retval)
423 return retval;
426 int cur_speed = jtag_get_speed_khz();
427 retval = jtag_get_speed_readable(&cur_speed);
428 if (ERROR_OK != retval)
429 return retval;
431 if (cur_speed)
432 command_print(CMD_CTX, "adapter speed: %d kHz", cur_speed);
433 else
434 command_print(CMD_CTX, "adapter speed: RCLK - adaptive");
436 return retval;
439 static const struct command_registration interface_command_handlers[] = {
441 .name = "adapter_khz",
442 .handler = handle_adapter_khz_command,
443 .mode = COMMAND_ANY,
444 .help = "With an argument, change to the specified maximum "
445 "jtag speed. For JTAG, 0 KHz signifies adaptive "
446 " clocking. "
447 "With or without argument, display current setting.",
448 .usage = "[khz]",
451 .name = "adapter_name",
452 .mode = COMMAND_ANY,
453 .jim_handler = jim_adapter_name,
454 .help = "Returns the name of the currently "
455 "selected adapter (driver)",
458 .name = "adapter_nsrst_delay",
459 .handler = handle_adapter_nsrst_delay_command,
460 .mode = COMMAND_ANY,
461 .help = "delay after deasserting SRST in ms",
462 .usage = "[milliseconds]",
465 .name = "adapter_nsrst_assert_width",
466 .handler = handle_adapter_nsrst_assert_width_command,
467 .mode = COMMAND_ANY,
468 .help = "delay after asserting SRST in ms",
469 .usage = "[milliseconds]",
472 .name = "interface",
473 .handler = handle_interface_command,
474 .mode = COMMAND_CONFIG,
475 .help = "Select a debug adapter interface (driver)",
476 .usage = "driver_name",
479 .name = "interface_transports",
480 .handler = interface_transport_command,
481 .mode = COMMAND_CONFIG,
482 .help = "Declare transports the interface supports.",
483 .usage = "transport ... ",
486 .name = "interface_list",
487 .handler = handle_interface_list_command,
488 .mode = COMMAND_ANY,
489 .help = "List all built-in debug adapter interfaces (drivers)",
492 .name = "reset_config",
493 .handler = handle_reset_config_command,
494 .mode = COMMAND_ANY,
495 .help = "configure adapter reset behavior",
496 .usage = "[none|trst_only|srst_only|trst_and_srst] "
497 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
498 "[srst_gates_jtag|srst_nogate] "
499 "[trst_push_pull|trst_open_drain] "
500 "[srst_push_pull|srst_open_drain]",
502 COMMAND_REGISTRATION_DONE
506 * Register the commands which deal with arbitrary debug adapter drivers.
508 * @todo Remove internal assumptions that all debug adapters use JTAG for
509 * transport. Various types and data structures are not named generically.
511 int interface_register_commands(struct command_context *ctx)
513 return register_commands(ctx, NULL, interface_command_handlers);