jtag: drivers: provide initial support for usb path filtering
[openocd.git] / src / jtag / adapter.c
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, see <http://www.gnu.org/licenses/>. *
27 ***************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "jtag.h"
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>
39
40 #ifdef HAVE_STRINGS_H
41 #include <strings.h>
42 #endif
43
44 /**
45 * @file
46 * Holds support for configuring debug adapters from TCl scripts.
47 */
48
49 extern struct jtag_interface *jtag_interface;
50 const char * const jtag_only[] = { "jtag", NULL };
51
52 static int jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
53 {
54 Jim_GetOptInfo goi;
55 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
56
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. */
60 if (goi.argc != 0) {
61 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
62 return JIM_ERR;
63 }
64 const char *name = jtag_interface ? jtag_interface->name : NULL;
65 Jim_SetResultString(goi.interp, name ? : "undefined", -1);
66 return JIM_OK;
67 }
68
69 static int default_khz(int khz, int *jtag_speed)
70 {
71 LOG_ERROR("Translation from khz to jtag_speed not implemented");
72 return ERROR_FAIL;
73 }
74
75 static int default_speed_div(int speed, int *khz)
76 {
77 LOG_ERROR("Translation from jtag_speed to khz not implemented");
78 return ERROR_FAIL;
79 }
80
81 static int default_power_dropout(int *dropout)
82 {
83 *dropout = 0; /* by default we can't detect power dropout */
84 return ERROR_OK;
85 }
86
87 static int default_srst_asserted(int *srst_asserted)
88 {
89 *srst_asserted = 0; /* by default we can't detect srst asserted */
90 return ERROR_OK;
91 }
92
93 COMMAND_HANDLER(interface_transport_command)
94 {
95 char **transports;
96 int retval;
97
98 retval = CALL_COMMAND_HANDLER(transport_list_parse, &transports);
99 if (retval != ERROR_OK)
100 return retval;
101
102 retval = allow_transports(CMD_CTX, (const char **)transports);
103
104 if (retval != ERROR_OK) {
105 for (unsigned i = 0; transports[i]; i++)
106 free(transports[i]);
107 free(transports);
108 }
109 return retval;
110 }
111
112 COMMAND_HANDLER(handle_interface_list_command)
113 {
114 if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
115 return ERROR_COMMAND_SYNTAX_ERROR;
116
117 command_print(CMD_CTX, "The following debug interfaces are available:");
118 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++) {
119 const char *name = jtag_interfaces[i]->name;
120 command_print(CMD_CTX, "%u: %s", i + 1, name);
121 }
122
123 return ERROR_OK;
124 }
125
126 COMMAND_HANDLER(handle_interface_command)
127 {
128 int retval;
129
130 /* check whether the interface is already configured */
131 if (jtag_interface) {
132 LOG_WARNING("Interface already configured, ignoring");
133 return ERROR_OK;
134 }
135
136 /* interface name is a mandatory argument */
137 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
138 return ERROR_COMMAND_SYNTAX_ERROR;
139
140 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++) {
141 if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0)
142 continue;
143
144 if (NULL != jtag_interfaces[i]->commands) {
145 retval = register_commands(CMD_CTX, NULL,
146 jtag_interfaces[i]->commands);
147 if (ERROR_OK != retval)
148 return retval;
149 }
150
151 jtag_interface = jtag_interfaces[i];
152
153 /* LEGACY SUPPORT ... adapter drivers must declare what
154 * transports they allow. Until they all do so, assume
155 * the legacy drivers are JTAG-only
156 */
157 if (!jtag_interface->transports)
158 LOG_WARNING("Adapter driver '%s' did not declare "
159 "which transports it allows; assuming "
160 "legacy JTAG-only", jtag_interface->name);
161 retval = allow_transports(CMD_CTX, jtag_interface->transports
162 ? jtag_interface->transports : jtag_only);
163 if (ERROR_OK != retval)
164 return retval;
165
166 if (jtag_interface->khz == NULL)
167 jtag_interface->khz = default_khz;
168 if (jtag_interface->speed_div == NULL)
169 jtag_interface->speed_div = default_speed_div;
170 if (jtag_interface->power_dropout == NULL)
171 jtag_interface->power_dropout = default_power_dropout;
172 if (jtag_interface->srst_asserted == NULL)
173 jtag_interface->srst_asserted = default_srst_asserted;
174
175 return ERROR_OK;
176 }
177
178 /* no valid interface was found (i.e. the configuration option,
179 * didn't match one of the compiled-in interfaces
180 */
181 LOG_ERROR("The specified debug interface was not found (%s)",
182 CMD_ARGV[0]);
183 CALL_COMMAND_HANDLER(handle_interface_list_command);
184 return ERROR_JTAG_INVALID_INTERFACE;
185 }
186
187 COMMAND_HANDLER(handle_reset_config_command)
188 {
189 int new_cfg = 0;
190 int mask = 0;
191
192 /* Original versions cared about the order of these tokens:
193 * reset_config signals [combination [trst_type [srst_type]]]
194 * They also clobbered the previous configuration even on error.
195 *
196 * Here we don't care about the order, and only change values
197 * which have been explicitly specified.
198 */
199 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
200 int tmp = 0;
201 int m;
202
203 /* gating */
204 m = RESET_SRST_NO_GATING;
205 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
206 /* default: don't use JTAG while SRST asserted */;
207 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
208 tmp = RESET_SRST_NO_GATING;
209 else
210 m = 0;
211 if (mask & m) {
212 LOG_ERROR("extra reset_config %s spec (%s)",
213 "gating", *CMD_ARGV);
214 return ERROR_COMMAND_SYNTAX_ERROR;
215 }
216 if (m)
217 goto next;
218
219 /* signals */
220 m = RESET_HAS_TRST | RESET_HAS_SRST;
221 if (strcmp(*CMD_ARGV, "none") == 0)
222 tmp = RESET_NONE;
223 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
224 tmp = RESET_HAS_TRST;
225 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
226 tmp = RESET_HAS_SRST;
227 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
228 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
229 else
230 m = 0;
231 if (mask & m) {
232 LOG_ERROR("extra reset_config %s spec (%s)",
233 "signal", *CMD_ARGV);
234 return ERROR_COMMAND_SYNTAX_ERROR;
235 }
236 if (m)
237 goto next;
238
239 /* combination (options for broken wiring) */
240 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
241 if (strcmp(*CMD_ARGV, "separate") == 0)
242 /* separate reset lines - default */;
243 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
244 tmp |= RESET_SRST_PULLS_TRST;
245 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
246 tmp |= RESET_TRST_PULLS_SRST;
247 else if (strcmp(*CMD_ARGV, "combined") == 0)
248 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
249 else
250 m = 0;
251 if (mask & m) {
252 LOG_ERROR("extra reset_config %s spec (%s)",
253 "combination", *CMD_ARGV);
254 return ERROR_COMMAND_SYNTAX_ERROR;
255 }
256 if (m)
257 goto next;
258
259 /* trst_type (NOP without HAS_TRST) */
260 m = RESET_TRST_OPEN_DRAIN;
261 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
262 tmp |= RESET_TRST_OPEN_DRAIN;
263 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
264 /* push/pull from adapter - default */;
265 else
266 m = 0;
267 if (mask & m) {
268 LOG_ERROR("extra reset_config %s spec (%s)",
269 "trst_type", *CMD_ARGV);
270 return ERROR_COMMAND_SYNTAX_ERROR;
271 }
272 if (m)
273 goto next;
274
275 /* srst_type (NOP without HAS_SRST) */
276 m = RESET_SRST_PUSH_PULL;
277 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
278 tmp |= RESET_SRST_PUSH_PULL;
279 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
280 /* open drain from adapter - default */;
281 else
282 m = 0;
283 if (mask & m) {
284 LOG_ERROR("extra reset_config %s spec (%s)",
285 "srst_type", *CMD_ARGV);
286 return ERROR_COMMAND_SYNTAX_ERROR;
287 }
288 if (m)
289 goto next;
290
291 /* connect_type - only valid when srst_nogate */
292 m = RESET_CNCT_UNDER_SRST;
293 if (strcmp(*CMD_ARGV, "connect_assert_srst") == 0)
294 tmp |= RESET_CNCT_UNDER_SRST;
295 else if (strcmp(*CMD_ARGV, "connect_deassert_srst") == 0)
296 /* connect normally - default */;
297 else
298 m = 0;
299 if (mask & m) {
300 LOG_ERROR("extra reset_config %s spec (%s)",
301 "connect_type", *CMD_ARGV);
302 return ERROR_COMMAND_SYNTAX_ERROR;
303 }
304 if (m)
305 goto next;
306
307 /* caller provided nonsense; fail */
308 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
309 return ERROR_COMMAND_SYNTAX_ERROR;
310
311 next:
312 /* Remember the bits which were specified (mask)
313 * and their new values (new_cfg).
314 */
315 mask |= m;
316 new_cfg |= tmp;
317 }
318
319 /* clear previous values of those bits, save new values */
320 if (mask) {
321 int old_cfg = jtag_get_reset_config();
322
323 old_cfg &= ~mask;
324 new_cfg |= old_cfg;
325 jtag_set_reset_config(new_cfg);
326 } else
327 new_cfg = jtag_get_reset_config();
328
329 /*
330 * Display the (now-)current reset mode
331 */
332 char *modes[6];
333
334 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
335 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
336 case RESET_HAS_SRST:
337 modes[0] = "srst_only";
338 break;
339 case RESET_HAS_TRST:
340 modes[0] = "trst_only";
341 break;
342 case RESET_TRST_AND_SRST:
343 modes[0] = "trst_and_srst";
344 break;
345 default:
346 modes[0] = "none";
347 break;
348 }
349
350 /* normally SRST and TRST are decoupled; but bugs happen ... */
351 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
352 case RESET_SRST_PULLS_TRST:
353 modes[1] = "srst_pulls_trst";
354 break;
355 case RESET_TRST_PULLS_SRST:
356 modes[1] = "trst_pulls_srst";
357 break;
358 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
359 modes[1] = "combined";
360 break;
361 default:
362 modes[1] = "separate";
363 break;
364 }
365
366 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
367 if (new_cfg & RESET_HAS_TRST) {
368 if (new_cfg & RESET_TRST_OPEN_DRAIN)
369 modes[3] = " trst_open_drain";
370 else
371 modes[3] = " trst_push_pull";
372 } else
373 modes[3] = "";
374
375 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
376 if (new_cfg & RESET_HAS_SRST) {
377 if (new_cfg & RESET_SRST_NO_GATING)
378 modes[2] = " srst_nogate";
379 else
380 modes[2] = " srst_gates_jtag";
381
382 if (new_cfg & RESET_SRST_PUSH_PULL)
383 modes[4] = " srst_push_pull";
384 else
385 modes[4] = " srst_open_drain";
386
387 if (new_cfg & RESET_CNCT_UNDER_SRST)
388 modes[5] = " connect_assert_srst";
389 else
390 modes[5] = " connect_deassert_srst";
391 } else {
392 modes[2] = "";
393 modes[4] = "";
394 modes[5] = "";
395 }
396
397 command_print(CMD_CTX, "%s %s%s%s%s%s",
398 modes[0], modes[1],
399 modes[2], modes[3], modes[4], modes[5]);
400
401 return ERROR_OK;
402 }
403
404 COMMAND_HANDLER(handle_adapter_nsrst_delay_command)
405 {
406 if (CMD_ARGC > 1)
407 return ERROR_COMMAND_SYNTAX_ERROR;
408 if (CMD_ARGC == 1) {
409 unsigned delay;
410 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
411
412 jtag_set_nsrst_delay(delay);
413 }
414 command_print(CMD_CTX, "adapter_nsrst_delay: %u", jtag_get_nsrst_delay());
415 return ERROR_OK;
416 }
417
418 COMMAND_HANDLER(handle_adapter_nsrst_assert_width_command)
419 {
420 if (CMD_ARGC > 1)
421 return ERROR_COMMAND_SYNTAX_ERROR;
422 if (CMD_ARGC == 1) {
423 unsigned width;
424 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], width);
425
426 jtag_set_nsrst_assert_width(width);
427 }
428 command_print(CMD_CTX, "adapter_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
429 return ERROR_OK;
430 }
431
432 COMMAND_HANDLER(handle_adapter_khz_command)
433 {
434 if (CMD_ARGC > 1)
435 return ERROR_COMMAND_SYNTAX_ERROR;
436
437 int retval = ERROR_OK;
438 if (CMD_ARGC == 1) {
439 unsigned khz = 0;
440 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
441
442 retval = jtag_config_khz(khz);
443 if (ERROR_OK != retval)
444 return retval;
445 }
446
447 int cur_speed = jtag_get_speed_khz();
448 retval = jtag_get_speed_readable(&cur_speed);
449 if (ERROR_OK != retval)
450 return retval;
451
452 if (cur_speed)
453 command_print(CMD_CTX, "adapter speed: %d kHz", cur_speed);
454 else
455 command_print(CMD_CTX, "adapter speed: RCLK - adaptive");
456
457 return retval;
458 }
459
460 #ifndef HAVE_JTAG_MINIDRIVER_H
461 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
462 COMMAND_HANDLER(handle_usb_location_command)
463 {
464 if (CMD_ARGC == 1)
465 jtag_usb_set_location(CMD_ARGV[0]);
466
467 command_print(CMD_CTX, "adapter usb location: %s", jtag_usb_get_location());
468
469 return ERROR_OK;
470 }
471 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
472
473 static const struct command_registration adapter_usb_command_handlers[] = {
474 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
475 {
476 .name = "location",
477 .handler = &handle_usb_location_command,
478 .mode = COMMAND_CONFIG,
479 .help = "set the USB bus location of the USB device",
480 .usage = "<bus>-port[.port]...",
481 },
482 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
483 COMMAND_REGISTRATION_DONE
484 };
485 #endif /* MINIDRIVER */
486
487 static const struct command_registration adapter_command_handlers[] = {
488 #ifndef HAVE_JTAG_MINIDRIVER_H
489 {
490 .name = "usb",
491 .mode = COMMAND_ANY,
492 .help = "usb adapter command group",
493 .usage = "",
494 .chain = adapter_usb_command_handlers,
495 },
496 #endif /* MINIDRIVER */
497 COMMAND_REGISTRATION_DONE
498 };
499
500 static const struct command_registration interface_command_handlers[] = {
501 {
502 .name = "adapter",
503 .mode = COMMAND_ANY,
504 .help = "adapter command group",
505 .usage = "",
506 .chain = adapter_command_handlers,
507 },
508 {
509 .name = "adapter_khz",
510 .handler = handle_adapter_khz_command,
511 .mode = COMMAND_ANY,
512 .help = "With an argument, change to the specified maximum "
513 "jtag speed. For JTAG, 0 KHz signifies adaptive "
514 " clocking. "
515 "With or without argument, display current setting.",
516 .usage = "[khz]",
517 },
518 {
519 .name = "adapter_name",
520 .mode = COMMAND_ANY,
521 .jim_handler = jim_adapter_name,
522 .help = "Returns the name of the currently "
523 "selected adapter (driver)",
524 },
525 {
526 .name = "adapter_nsrst_delay",
527 .handler = handle_adapter_nsrst_delay_command,
528 .mode = COMMAND_ANY,
529 .help = "delay after deasserting SRST in ms",
530 .usage = "[milliseconds]",
531 },
532 {
533 .name = "adapter_nsrst_assert_width",
534 .handler = handle_adapter_nsrst_assert_width_command,
535 .mode = COMMAND_ANY,
536 .help = "delay after asserting SRST in ms",
537 .usage = "[milliseconds]",
538 },
539 {
540 .name = "interface",
541 .handler = handle_interface_command,
542 .mode = COMMAND_CONFIG,
543 .help = "Select a debug adapter interface (driver)",
544 .usage = "driver_name",
545 },
546 {
547 .name = "interface_transports",
548 .handler = interface_transport_command,
549 .mode = COMMAND_CONFIG,
550 .help = "Declare transports the interface supports.",
551 .usage = "transport ... ",
552 },
553 {
554 .name = "interface_list",
555 .handler = handle_interface_list_command,
556 .mode = COMMAND_ANY,
557 .help = "List all built-in debug adapter interfaces (drivers)",
558 },
559 {
560 .name = "reset_config",
561 .handler = handle_reset_config_command,
562 .mode = COMMAND_ANY,
563 .help = "configure adapter reset behavior",
564 .usage = "[none|trst_only|srst_only|trst_and_srst] "
565 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
566 "[srst_gates_jtag|srst_nogate] "
567 "[trst_push_pull|trst_open_drain] "
568 "[srst_push_pull|srst_open_drain] "
569 "[connect_deassert_srst|connect_assert_srst]",
570 },
571 COMMAND_REGISTRATION_DONE
572 };
573
574 /**
575 * Register the commands which deal with arbitrary debug adapter drivers.
576 *
577 * @todo Remove internal assumptions that all debug adapters use JTAG for
578 * transport. Various types and data structures are not named generically.
579 */
580 int interface_register_commands(struct command_context *ctx)
581 {
582 return register_commands(ctx, NULL, interface_command_handlers);
583 }