helper: util: rewrite command 'ms' as COMMAND_HANDLER
[openocd.git] / src / jtag / adapter.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de>
4 * Copyright (C) 2007-2010 Øyvind Harboe <oyvind.harboe@zylin.com>
5 * Copyright (C) 2009 SoftPLC Corporation, http://softplc.com, Dick Hollenbeck <dick@softplc.com>
6 * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net>
7 * Copyright (C) 2018 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
8 */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include "adapter.h"
15 #include "jtag.h"
16 #include "minidriver.h"
17 #include "interface.h"
18 #include "interfaces.h"
19 #include <transport/transport.h>
20
21 #ifdef HAVE_STRINGS_H
22 #include <strings.h>
23 #endif
24
25 /**
26 * @file
27 * Holds support for configuring debug adapters from TCl scripts.
28 */
29
30 struct adapter_driver *adapter_driver;
31 const char * const jtag_only[] = { "jtag", NULL };
32
33 enum adapter_clk_mode {
34 CLOCK_MODE_UNSELECTED = 0,
35 CLOCK_MODE_KHZ,
36 CLOCK_MODE_RCLK
37 };
38
39 #define DEFAULT_CLOCK_SPEED_KHZ 100U
40
41 /**
42 * Adapter configuration
43 */
44 static struct {
45 bool adapter_initialized;
46 char *usb_location;
47 char *serial;
48 enum adapter_clk_mode clock_mode;
49 int speed_khz;
50 int rclk_fallback_speed_khz;
51 struct adapter_gpio_config gpios[ADAPTER_GPIO_IDX_NUM];
52 bool gpios_initialized; /* Initialization of GPIOs to their unset values performed at run time */
53 } adapter_config;
54
55 static const struct gpio_map {
56 const char *name;
57 enum adapter_gpio_direction direction;
58 bool permit_drive_option;
59 bool permit_init_state_option;
60 } gpio_map[ADAPTER_GPIO_IDX_NUM] = {
61 [ADAPTER_GPIO_IDX_TDO] = { "tdo", ADAPTER_GPIO_DIRECTION_INPUT, false, true, },
62 [ADAPTER_GPIO_IDX_TDI] = { "tdi", ADAPTER_GPIO_DIRECTION_OUTPUT, true, true, },
63 [ADAPTER_GPIO_IDX_TMS] = { "tms", ADAPTER_GPIO_DIRECTION_OUTPUT, true, true, },
64 [ADAPTER_GPIO_IDX_TCK] = { "tck", ADAPTER_GPIO_DIRECTION_OUTPUT, true, true, },
65 [ADAPTER_GPIO_IDX_SWDIO] = { "swdio", ADAPTER_GPIO_DIRECTION_BIDIRECTIONAL, true, true, },
66 [ADAPTER_GPIO_IDX_SWDIO_DIR] = { "swdio_dir", ADAPTER_GPIO_DIRECTION_OUTPUT, true, false, },
67 [ADAPTER_GPIO_IDX_SWCLK] = { "swclk", ADAPTER_GPIO_DIRECTION_OUTPUT, true, true, },
68 [ADAPTER_GPIO_IDX_TRST] = { "trst", ADAPTER_GPIO_DIRECTION_OUTPUT, false, true, },
69 [ADAPTER_GPIO_IDX_SRST] = { "srst", ADAPTER_GPIO_DIRECTION_OUTPUT, false, true, },
70 [ADAPTER_GPIO_IDX_LED] = { "led", ADAPTER_GPIO_DIRECTION_OUTPUT, true, true, },
71 };
72
73 bool is_adapter_initialized(void)
74 {
75 return adapter_config.adapter_initialized;
76 }
77
78 /* For convenience of the bit-banging drivers keep the gpio_config drive
79 * settings for srst and trst in sync with values set by the "adapter
80 * reset_config" command.
81 */
82 static void sync_adapter_reset_with_gpios(void)
83 {
84 enum reset_types cfg = jtag_get_reset_config();
85 if (cfg & RESET_SRST_PUSH_PULL)
86 adapter_config.gpios[ADAPTER_GPIO_IDX_SRST].drive = ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL;
87 else
88 adapter_config.gpios[ADAPTER_GPIO_IDX_SRST].drive = ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN;
89 if (cfg & RESET_TRST_OPEN_DRAIN)
90 adapter_config.gpios[ADAPTER_GPIO_IDX_TRST].drive = ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN;
91 else
92 adapter_config.gpios[ADAPTER_GPIO_IDX_TRST].drive = ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL;
93 }
94
95 static void adapter_driver_gpios_init(void)
96 {
97 if (adapter_config.gpios_initialized)
98 return;
99
100 for (int i = 0; i < ADAPTER_GPIO_IDX_NUM; ++i) {
101 adapter_config.gpios[i].gpio_num = -1;
102 adapter_config.gpios[i].chip_num = -1;
103 if (gpio_map[i].direction == ADAPTER_GPIO_DIRECTION_INPUT)
104 adapter_config.gpios[i].init_state = ADAPTER_GPIO_INIT_STATE_INPUT;
105 }
106
107 /* Drivers assume active low, and this is the normal behaviour for reset
108 * lines so should be the default. */
109 adapter_config.gpios[ADAPTER_GPIO_IDX_SRST].active_low = true;
110 adapter_config.gpios[ADAPTER_GPIO_IDX_TRST].active_low = true;
111 sync_adapter_reset_with_gpios();
112
113 /* JTAG GPIOs should be inactive except for tms */
114 adapter_config.gpios[ADAPTER_GPIO_IDX_TMS].init_state = ADAPTER_GPIO_INIT_STATE_ACTIVE;
115
116 adapter_config.gpios_initialized = true;
117 }
118
119 /**
120 * Do low-level setup like initializing registers, output signals,
121 * and clocking.
122 */
123 int adapter_init(struct command_context *cmd_ctx)
124 {
125 if (is_adapter_initialized())
126 return ERROR_OK;
127
128 if (!adapter_driver) {
129 /* nothing was previously specified by "adapter driver" command */
130 LOG_ERROR("Debug Adapter has to be specified, "
131 "see \"adapter driver\" command");
132 return ERROR_JTAG_INVALID_INTERFACE;
133 }
134
135 adapter_driver_gpios_init();
136
137 int retval;
138
139 /* If the adapter supports configurable speed but the speed is not configured,
140 * provide a hint to the user. */
141 if (adapter_driver->speed && adapter_config.clock_mode == CLOCK_MODE_UNSELECTED) {
142 LOG_WARNING("An adapter speed is not selected in the init scripts."
143 " OpenOCD will try to run the adapter at very low speed (%d kHz).",
144 DEFAULT_CLOCK_SPEED_KHZ);
145 LOG_WARNING("To remove this warnings and achieve reasonable communication speed with the target,"
146 " set \"adapter speed\" or \"jtag_rclk\" in the init scripts.");
147 retval = adapter_config_khz(DEFAULT_CLOCK_SPEED_KHZ);
148 if (retval != ERROR_OK)
149 return ERROR_JTAG_INIT_FAILED;
150 }
151
152 retval = adapter_driver->init();
153 if (retval != ERROR_OK)
154 return retval;
155 adapter_config.adapter_initialized = true;
156
157 if (!adapter_driver->speed) {
158 LOG_INFO("Note: The adapter \"%s\" doesn't support configurable speed", adapter_driver->name);
159 return ERROR_OK;
160 }
161
162 int requested_khz = adapter_get_speed_khz();
163 int actual_khz = requested_khz;
164 int speed_var = 0;
165 retval = adapter_get_speed(&speed_var);
166 if (retval != ERROR_OK)
167 return retval;
168 retval = adapter_driver->speed(speed_var);
169 if (retval != ERROR_OK)
170 return retval;
171 retval = adapter_get_speed_readable(&actual_khz);
172 if (retval != ERROR_OK)
173 LOG_INFO("adapter-specific clock speed value %d", speed_var);
174 else if (actual_khz) {
175 /* Adaptive clocking -- JTAG-specific */
176 if ((adapter_config.clock_mode == CLOCK_MODE_RCLK)
177 || ((adapter_config.clock_mode == CLOCK_MODE_KHZ) && !requested_khz)) {
178 LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
179 , actual_khz);
180 } else
181 LOG_INFO("clock speed %d kHz", actual_khz);
182 } else
183 LOG_INFO("RCLK (adaptive clock speed)");
184
185 return ERROR_OK;
186 }
187
188 int adapter_quit(void)
189 {
190 if (is_adapter_initialized() && adapter_driver->quit) {
191 /* close the JTAG interface */
192 int result = adapter_driver->quit();
193 if (result != ERROR_OK)
194 LOG_ERROR("failed: %d", result);
195 }
196
197 free(adapter_config.serial);
198 free(adapter_config.usb_location);
199
200 struct jtag_tap *t = jtag_all_taps();
201 while (t) {
202 struct jtag_tap *n = t->next_tap;
203 jtag_tap_free(t);
204 t = n;
205 }
206
207 return ERROR_OK;
208 }
209
210 unsigned int adapter_get_speed_khz(void)
211 {
212 return adapter_config.speed_khz;
213 }
214
215 static int adapter_khz_to_speed(unsigned int khz, int *speed)
216 {
217 LOG_DEBUG("convert khz to adapter specific speed value");
218 adapter_config.speed_khz = khz;
219 if (!is_adapter_initialized())
220 return ERROR_OK;
221 LOG_DEBUG("have adapter set up");
222 if (!adapter_driver->khz) {
223 LOG_ERROR("Translation from khz to adapter speed not implemented");
224 return ERROR_FAIL;
225 }
226 int speed_div1;
227 int retval = adapter_driver->khz(adapter_get_speed_khz(), &speed_div1);
228 if (retval != ERROR_OK)
229 return retval;
230 *speed = speed_div1;
231 return ERROR_OK;
232 }
233
234 static int adapter_rclk_to_speed(unsigned int fallback_speed_khz, int *speed)
235 {
236 int retval = adapter_khz_to_speed(0, speed);
237 if ((retval != ERROR_OK) && fallback_speed_khz) {
238 LOG_DEBUG("trying fallback speed...");
239 retval = adapter_khz_to_speed(fallback_speed_khz, speed);
240 }
241 return retval;
242 }
243
244 static int adapter_set_speed(int speed)
245 {
246 /* this command can be called during CONFIG,
247 * in which case adapter isn't initialized */
248 return is_adapter_initialized() ? adapter_driver->speed(speed) : ERROR_OK;
249 }
250
251 int adapter_config_khz(unsigned int khz)
252 {
253 LOG_DEBUG("handle adapter khz");
254 adapter_config.clock_mode = CLOCK_MODE_KHZ;
255 int speed = 0;
256 int retval = adapter_khz_to_speed(khz, &speed);
257 return (retval != ERROR_OK) ? retval : adapter_set_speed(speed);
258 }
259
260 int adapter_config_rclk(unsigned int fallback_speed_khz)
261 {
262 LOG_DEBUG("handle adapter rclk");
263 adapter_config.clock_mode = CLOCK_MODE_RCLK;
264 adapter_config.rclk_fallback_speed_khz = fallback_speed_khz;
265 int speed = 0;
266 int retval = adapter_rclk_to_speed(fallback_speed_khz, &speed);
267 return (retval != ERROR_OK) ? retval : adapter_set_speed(speed);
268 }
269
270 int adapter_get_speed(int *speed)
271 {
272 switch (adapter_config.clock_mode) {
273 case CLOCK_MODE_KHZ:
274 adapter_khz_to_speed(adapter_get_speed_khz(), speed);
275 break;
276 case CLOCK_MODE_RCLK:
277 adapter_rclk_to_speed(adapter_config.rclk_fallback_speed_khz, speed);
278 break;
279 default:
280 LOG_ERROR("BUG: unknown adapter clock mode");
281 return ERROR_FAIL;
282 }
283 return ERROR_OK;
284 }
285
286 int adapter_get_speed_readable(int *khz)
287 {
288 int speed_var = 0;
289 int retval = adapter_get_speed(&speed_var);
290 if (retval != ERROR_OK)
291 return retval;
292 if (!is_adapter_initialized())
293 return ERROR_OK;
294 if (!adapter_driver->speed_div) {
295 LOG_ERROR("Translation from adapter speed to khz not implemented");
296 return ERROR_FAIL;
297 }
298 return adapter_driver->speed_div(speed_var, khz);
299 }
300
301 const char *adapter_get_required_serial(void)
302 {
303 return adapter_config.serial;
304 }
305
306 /*
307 * 1 char: bus
308 * 2 * 7 chars: max 7 ports
309 * 1 char: test for overflow
310 * ------
311 * 16 chars
312 */
313 #define USB_MAX_LOCATION_LENGTH 16
314
315 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
316 static void adapter_usb_set_location(const char *location)
317 {
318 if (strnlen(location, USB_MAX_LOCATION_LENGTH) == USB_MAX_LOCATION_LENGTH)
319 LOG_WARNING("usb location string is too long!!");
320
321 free(adapter_config.usb_location);
322
323 adapter_config.usb_location = strndup(location, USB_MAX_LOCATION_LENGTH);
324 }
325 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
326
327 const char *adapter_usb_get_location(void)
328 {
329 return adapter_config.usb_location;
330 }
331
332 bool adapter_usb_location_equal(uint8_t dev_bus, uint8_t *port_path, size_t path_len)
333 {
334 size_t path_step, string_length;
335 char *ptr, *loc;
336 bool equal = false;
337
338 if (!adapter_usb_get_location())
339 return equal;
340
341 /* strtok need non const char */
342 loc = strndup(adapter_usb_get_location(), USB_MAX_LOCATION_LENGTH);
343 string_length = strnlen(loc, USB_MAX_LOCATION_LENGTH);
344
345 ptr = strtok(loc, "-");
346 if (!ptr) {
347 LOG_WARNING("no '-' in usb path\n");
348 goto done;
349 }
350
351 string_length -= strnlen(ptr, string_length);
352 /* check bus mismatch */
353 if (atoi(ptr) != dev_bus)
354 goto done;
355
356 path_step = 0;
357 while (path_step < path_len) {
358 ptr = strtok(NULL, ".");
359
360 /* no more tokens in path */
361 if (!ptr)
362 break;
363
364 /* path mismatch at some step */
365 if (path_step < path_len && atoi(ptr) != port_path[path_step])
366 break;
367
368 path_step++;
369 string_length -= strnlen(ptr, string_length) + 1;
370 };
371
372 /* walked the full path, all elements match */
373 if (path_step == path_len && !string_length)
374 equal = true;
375
376 done:
377 free(loc);
378 return equal;
379 }
380
381 static int jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
382 {
383 struct jim_getopt_info goi;
384 jim_getopt_setup(&goi, interp, argc-1, argv + 1);
385
386 /* return the name of the interface */
387 /* TCL code might need to know the exact type... */
388 /* FUTURE: we allow this as a means to "set" the interface. */
389 if (goi.argc != 0) {
390 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
391 return JIM_ERR;
392 }
393 const char *name = adapter_driver ? adapter_driver->name : NULL;
394 Jim_SetResultString(goi.interp, name ? name : "undefined", -1);
395 return JIM_OK;
396 }
397
398 COMMAND_HANDLER(adapter_transports_command)
399 {
400 char **transports;
401 int retval;
402
403 retval = CALL_COMMAND_HANDLER(transport_list_parse, &transports);
404 if (retval != ERROR_OK)
405 return retval;
406
407 retval = allow_transports(CMD_CTX, (const char **)transports);
408
409 if (retval != ERROR_OK) {
410 for (unsigned i = 0; transports[i]; i++)
411 free(transports[i]);
412 free(transports);
413 }
414 return retval;
415 }
416
417 COMMAND_HANDLER(handle_adapter_list_command)
418 {
419 if (strcmp(CMD_NAME, "list") == 0 && CMD_ARGC > 0)
420 return ERROR_COMMAND_SYNTAX_ERROR;
421
422 command_print(CMD, "The following debug adapters are available:");
423 for (unsigned i = 0; adapter_drivers[i]; i++) {
424 const char *name = adapter_drivers[i]->name;
425 command_print(CMD, "%u: %s", i + 1, name);
426 }
427
428 return ERROR_OK;
429 }
430
431 COMMAND_HANDLER(handle_adapter_driver_command)
432 {
433 int retval;
434
435 /* check whether the interface is already configured */
436 if (adapter_driver) {
437 LOG_WARNING("Interface already configured, ignoring");
438 return ERROR_OK;
439 }
440
441 /* interface name is a mandatory argument */
442 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
443 return ERROR_COMMAND_SYNTAX_ERROR;
444
445 for (unsigned i = 0; adapter_drivers[i]; i++) {
446 if (strcmp(CMD_ARGV[0], adapter_drivers[i]->name) != 0)
447 continue;
448
449 if (adapter_drivers[i]->commands) {
450 retval = register_commands(CMD_CTX, NULL, adapter_drivers[i]->commands);
451 if (retval != ERROR_OK)
452 return retval;
453 }
454
455 adapter_driver = adapter_drivers[i];
456
457 return allow_transports(CMD_CTX, adapter_driver->transports);
458 }
459
460 /* no valid interface was found (i.e. the configuration option,
461 * didn't match one of the compiled-in interfaces
462 */
463 LOG_ERROR("The specified debug interface was not found (%s)",
464 CMD_ARGV[0]);
465 CALL_COMMAND_HANDLER(handle_adapter_list_command);
466 return ERROR_JTAG_INVALID_INTERFACE;
467 }
468
469 COMMAND_HANDLER(handle_reset_config_command)
470 {
471 int new_cfg = 0;
472 int mask = 0;
473
474 /* Original versions cared about the order of these tokens:
475 * reset_config signals [combination [trst_type [srst_type]]]
476 * They also clobbered the previous configuration even on error.
477 *
478 * Here we don't care about the order, and only change values
479 * which have been explicitly specified.
480 */
481 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
482 int tmp = 0;
483 int m;
484
485 /* gating */
486 m = RESET_SRST_NO_GATING;
487 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
488 /* default: don't use JTAG while SRST asserted */;
489 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
490 tmp = RESET_SRST_NO_GATING;
491 else
492 m = 0;
493 if (mask & m) {
494 LOG_ERROR("extra reset_config %s spec (%s)",
495 "gating", *CMD_ARGV);
496 return ERROR_COMMAND_SYNTAX_ERROR;
497 }
498 if (m)
499 goto next;
500
501 /* signals */
502 m = RESET_HAS_TRST | RESET_HAS_SRST;
503 if (strcmp(*CMD_ARGV, "none") == 0)
504 tmp = RESET_NONE;
505 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
506 tmp = RESET_HAS_TRST;
507 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
508 tmp = RESET_HAS_SRST;
509 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
510 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
511 else
512 m = 0;
513 if (mask & m) {
514 LOG_ERROR("extra reset_config %s spec (%s)",
515 "signal", *CMD_ARGV);
516 return ERROR_COMMAND_SYNTAX_ERROR;
517 }
518 if (m)
519 goto next;
520
521 /* combination (options for broken wiring) */
522 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
523 if (strcmp(*CMD_ARGV, "separate") == 0)
524 /* separate reset lines - default */;
525 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
526 tmp |= RESET_SRST_PULLS_TRST;
527 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
528 tmp |= RESET_TRST_PULLS_SRST;
529 else if (strcmp(*CMD_ARGV, "combined") == 0)
530 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
531 else
532 m = 0;
533 if (mask & m) {
534 LOG_ERROR("extra reset_config %s spec (%s)",
535 "combination", *CMD_ARGV);
536 return ERROR_COMMAND_SYNTAX_ERROR;
537 }
538 if (m)
539 goto next;
540
541 /* trst_type (NOP without HAS_TRST) */
542 m = RESET_TRST_OPEN_DRAIN;
543 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
544 tmp |= RESET_TRST_OPEN_DRAIN;
545 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
546 /* push/pull from adapter - default */;
547 else
548 m = 0;
549 if (mask & m) {
550 LOG_ERROR("extra reset_config %s spec (%s)",
551 "trst_type", *CMD_ARGV);
552 return ERROR_COMMAND_SYNTAX_ERROR;
553 }
554 if (m)
555 goto next;
556
557 /* srst_type (NOP without HAS_SRST) */
558 m = RESET_SRST_PUSH_PULL;
559 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
560 tmp |= RESET_SRST_PUSH_PULL;
561 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
562 /* open drain from adapter - default */;
563 else
564 m = 0;
565 if (mask & m) {
566 LOG_ERROR("extra reset_config %s spec (%s)",
567 "srst_type", *CMD_ARGV);
568 return ERROR_COMMAND_SYNTAX_ERROR;
569 }
570 if (m)
571 goto next;
572
573 /* connect_type - only valid when srst_nogate */
574 m = RESET_CNCT_UNDER_SRST;
575 if (strcmp(*CMD_ARGV, "connect_assert_srst") == 0)
576 tmp |= RESET_CNCT_UNDER_SRST;
577 else if (strcmp(*CMD_ARGV, "connect_deassert_srst") == 0)
578 /* connect normally - default */;
579 else
580 m = 0;
581 if (mask & m) {
582 LOG_ERROR("extra reset_config %s spec (%s)",
583 "connect_type", *CMD_ARGV);
584 return ERROR_COMMAND_SYNTAX_ERROR;
585 }
586 if (m)
587 goto next;
588
589 /* caller provided nonsense; fail */
590 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
591 return ERROR_COMMAND_SYNTAX_ERROR;
592
593 next:
594 /* Remember the bits which were specified (mask)
595 * and their new values (new_cfg).
596 */
597 mask |= m;
598 new_cfg |= tmp;
599 }
600
601 /* clear previous values of those bits, save new values */
602 if (mask) {
603 int old_cfg = jtag_get_reset_config();
604
605 old_cfg &= ~mask;
606 new_cfg |= old_cfg;
607 jtag_set_reset_config(new_cfg);
608 sync_adapter_reset_with_gpios();
609
610 } else
611 new_cfg = jtag_get_reset_config();
612
613 /*
614 * Display the (now-)current reset mode
615 */
616 char *modes[6];
617
618 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
619 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
620 case RESET_HAS_SRST:
621 modes[0] = "srst_only";
622 break;
623 case RESET_HAS_TRST:
624 modes[0] = "trst_only";
625 break;
626 case RESET_TRST_AND_SRST:
627 modes[0] = "trst_and_srst";
628 break;
629 default:
630 modes[0] = "none";
631 break;
632 }
633
634 /* normally SRST and TRST are decoupled; but bugs happen ... */
635 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
636 case RESET_SRST_PULLS_TRST:
637 modes[1] = "srst_pulls_trst";
638 break;
639 case RESET_TRST_PULLS_SRST:
640 modes[1] = "trst_pulls_srst";
641 break;
642 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
643 modes[1] = "combined";
644 break;
645 default:
646 modes[1] = "separate";
647 break;
648 }
649
650 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
651 if (new_cfg & RESET_HAS_TRST) {
652 if (new_cfg & RESET_TRST_OPEN_DRAIN)
653 modes[3] = " trst_open_drain";
654 else
655 modes[3] = " trst_push_pull";
656 } else
657 modes[3] = "";
658
659 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
660 if (new_cfg & RESET_HAS_SRST) {
661 if (new_cfg & RESET_SRST_NO_GATING)
662 modes[2] = " srst_nogate";
663 else
664 modes[2] = " srst_gates_jtag";
665
666 if (new_cfg & RESET_SRST_PUSH_PULL)
667 modes[4] = " srst_push_pull";
668 else
669 modes[4] = " srst_open_drain";
670
671 if (new_cfg & RESET_CNCT_UNDER_SRST)
672 modes[5] = " connect_assert_srst";
673 else
674 modes[5] = " connect_deassert_srst";
675 } else {
676 modes[2] = "";
677 modes[4] = "";
678 modes[5] = "";
679 }
680
681 command_print(CMD, "%s %s%s%s%s%s",
682 modes[0], modes[1],
683 modes[2], modes[3], modes[4], modes[5]);
684
685 return ERROR_OK;
686 }
687
688 COMMAND_HANDLER(handle_adapter_srst_delay_command)
689 {
690 if (CMD_ARGC > 1)
691 return ERROR_COMMAND_SYNTAX_ERROR;
692 if (CMD_ARGC == 1) {
693 unsigned delay;
694 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
695
696 jtag_set_nsrst_delay(delay);
697 }
698 command_print(CMD, "adapter srst delay: %u", jtag_get_nsrst_delay());
699 return ERROR_OK;
700 }
701
702 COMMAND_HANDLER(handle_adapter_srst_pulse_width_command)
703 {
704 if (CMD_ARGC > 1)
705 return ERROR_COMMAND_SYNTAX_ERROR;
706 if (CMD_ARGC == 1) {
707 unsigned width;
708 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], width);
709
710 jtag_set_nsrst_assert_width(width);
711 }
712 command_print(CMD, "adapter srst pulse_width: %u", jtag_get_nsrst_assert_width());
713 return ERROR_OK;
714 }
715
716 COMMAND_HANDLER(handle_adapter_speed_command)
717 {
718 if (CMD_ARGC > 1)
719 return ERROR_COMMAND_SYNTAX_ERROR;
720
721 int retval = ERROR_OK;
722 if (CMD_ARGC == 1) {
723 unsigned khz = 0;
724 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
725
726 retval = adapter_config_khz(khz);
727 if (retval != ERROR_OK)
728 return retval;
729 }
730
731 int cur_speed = adapter_get_speed_khz();
732 retval = adapter_get_speed_readable(&cur_speed);
733 if (retval != ERROR_OK)
734 return retval;
735
736 if (cur_speed)
737 command_print(CMD, "adapter speed: %d kHz", cur_speed);
738 else
739 command_print(CMD, "adapter speed: RCLK - adaptive");
740
741 return retval;
742 }
743
744 COMMAND_HANDLER(handle_adapter_serial_command)
745 {
746 if (CMD_ARGC != 1)
747 return ERROR_COMMAND_SYNTAX_ERROR;
748
749 free(adapter_config.serial);
750 adapter_config.serial = strdup(CMD_ARGV[0]);
751 return ERROR_OK;
752 }
753
754 COMMAND_HANDLER(handle_adapter_reset_de_assert)
755 {
756 enum values {
757 VALUE_UNDEFINED = -1,
758 VALUE_DEASSERT = 0,
759 VALUE_ASSERT = 1,
760 };
761 enum values value;
762 enum values srst = VALUE_UNDEFINED;
763 enum values trst = VALUE_UNDEFINED;
764 enum reset_types jtag_reset_config = jtag_get_reset_config();
765 char *signal;
766
767 if (CMD_ARGC == 0) {
768 if (transport_is_jtag()) {
769 if (jtag_reset_config & RESET_HAS_TRST)
770 signal = jtag_get_trst() ? "asserted" : "deasserted";
771 else
772 signal = "not present";
773 command_print(CMD, "trst %s", signal);
774 }
775
776 if (jtag_reset_config & RESET_HAS_SRST)
777 signal = jtag_get_srst() ? "asserted" : "deasserted";
778 else
779 signal = "not present";
780 command_print(CMD, "srst %s", signal);
781
782 return ERROR_OK;
783 }
784
785 if (CMD_ARGC != 1 && CMD_ARGC != 3)
786 return ERROR_COMMAND_SYNTAX_ERROR;
787
788 value = (strcmp(CMD_NAME, "assert") == 0) ? VALUE_ASSERT : VALUE_DEASSERT;
789 if (strcmp(CMD_ARGV[0], "srst") == 0)
790 srst = value;
791 else if (strcmp(CMD_ARGV[0], "trst") == 0)
792 trst = value;
793 else
794 return ERROR_COMMAND_SYNTAX_ERROR;
795
796 if (CMD_ARGC == 3) {
797 if (strcmp(CMD_ARGV[1], "assert") == 0)
798 value = VALUE_ASSERT;
799 else if (strcmp(CMD_ARGV[1], "deassert") == 0)
800 value = VALUE_DEASSERT;
801 else
802 return ERROR_COMMAND_SYNTAX_ERROR;
803
804 if (strcmp(CMD_ARGV[2], "srst") == 0 && srst == VALUE_UNDEFINED)
805 srst = value;
806 else if (strcmp(CMD_ARGV[2], "trst") == 0 && trst == VALUE_UNDEFINED)
807 trst = value;
808 else
809 return ERROR_COMMAND_SYNTAX_ERROR;
810 }
811
812 if (trst == VALUE_UNDEFINED) {
813 if (transport_is_jtag())
814 trst = jtag_get_trst() ? VALUE_ASSERT : VALUE_DEASSERT;
815 else
816 trst = VALUE_DEASSERT; /* unused, safe value */
817 }
818
819 if (srst == VALUE_UNDEFINED) {
820 if (jtag_reset_config & RESET_HAS_SRST)
821 srst = jtag_get_srst() ? VALUE_ASSERT : VALUE_DEASSERT;
822 else
823 srst = VALUE_DEASSERT; /* unused, safe value */
824 }
825
826 if (trst == VALUE_ASSERT && !transport_is_jtag()) {
827 LOG_ERROR("transport has no trst signal");
828 return ERROR_FAIL;
829 }
830
831 if (srst == VALUE_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
832 LOG_ERROR("adapter has no srst signal");
833 return ERROR_FAIL;
834 }
835
836 return adapter_resets((trst == VALUE_DEASSERT) ? TRST_DEASSERT : TRST_ASSERT,
837 (srst == VALUE_DEASSERT) ? SRST_DEASSERT : SRST_ASSERT);
838 }
839
840 static int get_gpio_index(const char *signal_name)
841 {
842 for (int i = 0; i < ADAPTER_GPIO_IDX_NUM; ++i) {
843 if (strcmp(gpio_map[i].name, signal_name) == 0)
844 return i;
845 }
846 return -1;
847 }
848
849 static COMMAND_HELPER(helper_adapter_gpio_print_config, enum adapter_gpio_config_index gpio_idx)
850 {
851 struct adapter_gpio_config *gpio_config = &adapter_config.gpios[gpio_idx];
852 const char *active_state = gpio_config->active_low ? "low" : "high";
853 const char *dir = "";
854 const char *drive = "";
855 const char *pull = "";
856 const char *init_state = "";
857
858 switch (gpio_map[gpio_idx].direction) {
859 case ADAPTER_GPIO_DIRECTION_INPUT:
860 dir = "input";
861 break;
862 case ADAPTER_GPIO_DIRECTION_OUTPUT:
863 dir = "output";
864 break;
865 case ADAPTER_GPIO_DIRECTION_BIDIRECTIONAL:
866 dir = "bidirectional";
867 break;
868 }
869
870 if (gpio_map[gpio_idx].permit_drive_option) {
871 switch (gpio_config->drive) {
872 case ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL:
873 drive = ", push-pull";
874 break;
875 case ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN:
876 drive = ", open-drain";
877 break;
878 case ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE:
879 drive = ", open-source";
880 break;
881 }
882 }
883
884 switch (gpio_config->pull) {
885 case ADAPTER_GPIO_PULL_NONE:
886 pull = ", pull-none";
887 break;
888 case ADAPTER_GPIO_PULL_UP:
889 pull = ", pull-up";
890 break;
891 case ADAPTER_GPIO_PULL_DOWN:
892 pull = ", pull-down";
893 break;
894 }
895
896 if (gpio_map[gpio_idx].permit_init_state_option) {
897 switch (gpio_config->init_state) {
898 case ADAPTER_GPIO_INIT_STATE_INACTIVE:
899 init_state = ", init-state inactive";
900 break;
901 case ADAPTER_GPIO_INIT_STATE_ACTIVE:
902 init_state = ", init-state active";
903 break;
904 case ADAPTER_GPIO_INIT_STATE_INPUT:
905 init_state = ", init-state input";
906 break;
907 }
908 }
909
910 command_print(CMD, "adapter gpio %s (%s): num %d, chip %d, active-%s%s%s%s",
911 gpio_map[gpio_idx].name, dir, gpio_config->gpio_num, gpio_config->chip_num, active_state,
912 drive, pull, init_state);
913
914 return ERROR_OK;
915 }
916
917 COMMAND_HANDLER(helper_adapter_gpio_print_all_configs)
918 {
919 for (int i = 0; i < ADAPTER_GPIO_IDX_NUM; ++i)
920 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_config, i);
921 return ERROR_OK;
922 }
923
924 COMMAND_HANDLER(adapter_gpio_config_handler)
925 {
926 unsigned int i = 1;
927 struct adapter_gpio_config *gpio_config;
928
929 adapter_driver_gpios_init();
930
931 if (CMD_ARGC == 0) {
932 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_all_configs);
933 return ERROR_OK;
934 }
935
936 int gpio_idx = get_gpio_index(CMD_ARGV[0]);
937 if (gpio_idx == -1) {
938 LOG_ERROR("adapter has no gpio named %s", CMD_ARGV[0]);
939 return ERROR_COMMAND_SYNTAX_ERROR;
940 }
941
942 if (CMD_ARGC == 1) {
943 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_config, gpio_idx);
944 return ERROR_OK;
945 }
946
947 gpio_config = &adapter_config.gpios[gpio_idx];
948 while (i < CMD_ARGC) {
949 LOG_DEBUG("Processing %s", CMD_ARGV[i]);
950
951 if (isdigit(*CMD_ARGV[i])) {
952 int gpio_num; /* Use a meaningful output parameter for more helpful error messages */
953 COMMAND_PARSE_NUMBER(int, CMD_ARGV[i], gpio_num);
954 gpio_config->gpio_num = gpio_num;
955 ++i;
956 continue;
957 }
958
959 if (strcmp(CMD_ARGV[i], "-chip") == 0) {
960 if (CMD_ARGC - i < 2) {
961 LOG_ERROR("-chip option requires a parameter");
962 return ERROR_FAIL;
963 }
964 LOG_DEBUG("-chip arg is %s", CMD_ARGV[i + 1]);
965 int chip_num; /* Use a meaningful output parameter for more helpful error messages */
966 COMMAND_PARSE_NUMBER(int, CMD_ARGV[i + 1], chip_num);
967 gpio_config->chip_num = chip_num;
968 i += 2;
969 continue;
970 }
971
972 if (strcmp(CMD_ARGV[i], "-active-high") == 0) {
973 ++i;
974 gpio_config->active_low = false;
975 continue;
976 }
977 if (strcmp(CMD_ARGV[i], "-active-low") == 0) {
978 ++i;
979 gpio_config->active_low = true;
980 continue;
981 }
982
983 if (gpio_map[gpio_idx].permit_drive_option) {
984 if (strcmp(CMD_ARGV[i], "-push-pull") == 0) {
985 ++i;
986 gpio_config->drive = ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL;
987 continue;
988 }
989 if (strcmp(CMD_ARGV[i], "-open-drain") == 0) {
990 ++i;
991 gpio_config->drive = ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN;
992 continue;
993 }
994 if (strcmp(CMD_ARGV[i], "-open-source") == 0) {
995 ++i;
996 gpio_config->drive = ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE;
997 continue;
998 }
999 }
1000
1001 if (strcmp(CMD_ARGV[i], "-pull-none") == 0) {
1002 ++i;
1003 gpio_config->pull = ADAPTER_GPIO_PULL_NONE;
1004 continue;
1005 }
1006 if (strcmp(CMD_ARGV[i], "-pull-up") == 0) {
1007 ++i;
1008 gpio_config->pull = ADAPTER_GPIO_PULL_UP;
1009 continue;
1010 }
1011 if (strcmp(CMD_ARGV[i], "-pull-down") == 0) {
1012 ++i;
1013 gpio_config->pull = ADAPTER_GPIO_PULL_DOWN;
1014 continue;
1015 }
1016
1017 if (gpio_map[gpio_idx].permit_init_state_option) {
1018 if (strcmp(CMD_ARGV[i], "-init-inactive") == 0) {
1019 ++i;
1020 gpio_config->init_state = ADAPTER_GPIO_INIT_STATE_INACTIVE;
1021 continue;
1022 }
1023 if (strcmp(CMD_ARGV[i], "-init-active") == 0) {
1024 ++i;
1025 gpio_config->init_state = ADAPTER_GPIO_INIT_STATE_ACTIVE;
1026 continue;
1027 }
1028
1029 if (gpio_map[gpio_idx].direction == ADAPTER_GPIO_DIRECTION_BIDIRECTIONAL &&
1030 strcmp(CMD_ARGV[i], "-init-input") == 0) {
1031 ++i;
1032 gpio_config->init_state = ADAPTER_GPIO_INIT_STATE_INPUT;
1033 continue;
1034 }
1035 }
1036
1037 LOG_ERROR("illegal option for adapter %s %s: %s",
1038 CMD_NAME, gpio_map[gpio_idx].name, CMD_ARGV[i]);
1039 return ERROR_COMMAND_SYNTAX_ERROR;
1040 }
1041
1042 /* Force swdio_dir init state to be compatible with swdio init state */
1043 if (gpio_idx == ADAPTER_GPIO_IDX_SWDIO)
1044 adapter_config.gpios[ADAPTER_GPIO_IDX_SWDIO_DIR].init_state =
1045 (gpio_config->init_state == ADAPTER_GPIO_INIT_STATE_INPUT) ?
1046 ADAPTER_GPIO_INIT_STATE_INACTIVE :
1047 ADAPTER_GPIO_INIT_STATE_ACTIVE;
1048
1049 return ERROR_OK;
1050 }
1051
1052 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
1053 COMMAND_HANDLER(handle_usb_location_command)
1054 {
1055 if (CMD_ARGC == 1)
1056 adapter_usb_set_location(CMD_ARGV[0]);
1057
1058 command_print(CMD, "adapter usb location: %s", adapter_usb_get_location());
1059
1060 return ERROR_OK;
1061 }
1062 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
1063
1064 static const struct command_registration adapter_usb_command_handlers[] = {
1065 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
1066 {
1067 .name = "location",
1068 .handler = &handle_usb_location_command,
1069 .mode = COMMAND_CONFIG,
1070 .help = "display or set the USB bus location of the USB device",
1071 .usage = "[<bus>-port[.port]...]",
1072 },
1073 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
1074 COMMAND_REGISTRATION_DONE
1075 };
1076
1077 static const struct command_registration adapter_srst_command_handlers[] = {
1078 {
1079 .name = "delay",
1080 .handler = handle_adapter_srst_delay_command,
1081 .mode = COMMAND_ANY,
1082 .help = "delay after deasserting SRST in ms",
1083 .usage = "[milliseconds]",
1084 },
1085 {
1086 .name = "pulse_width",
1087 .handler = handle_adapter_srst_pulse_width_command,
1088 .mode = COMMAND_ANY,
1089 .help = "SRST assertion pulse width in ms",
1090 .usage = "[milliseconds]",
1091 },
1092 COMMAND_REGISTRATION_DONE
1093 };
1094
1095 static const struct command_registration adapter_command_handlers[] = {
1096 {
1097 .name = "driver",
1098 .handler = handle_adapter_driver_command,
1099 .mode = COMMAND_CONFIG,
1100 .help = "Select a debug adapter driver",
1101 .usage = "driver_name",
1102 },
1103 {
1104 .name = "speed",
1105 .handler = handle_adapter_speed_command,
1106 .mode = COMMAND_ANY,
1107 .help = "With an argument, change to the specified maximum "
1108 "jtag speed. For JTAG, 0 KHz signifies adaptive "
1109 "clocking. "
1110 "With or without argument, display current setting.",
1111 .usage = "[khz]",
1112 },
1113 {
1114 .name = "serial",
1115 .handler = handle_adapter_serial_command,
1116 .mode = COMMAND_CONFIG,
1117 .help = "Set the serial number of the adapter",
1118 .usage = "serial_string",
1119 },
1120 {
1121 .name = "list",
1122 .handler = handle_adapter_list_command,
1123 .mode = COMMAND_ANY,
1124 .help = "List all built-in debug adapter drivers",
1125 .usage = "",
1126 },
1127 {
1128 .name = "name",
1129 .mode = COMMAND_ANY,
1130 .jim_handler = jim_adapter_name,
1131 .help = "Returns the name of the currently "
1132 "selected adapter (driver)",
1133 },
1134 {
1135 .name = "srst",
1136 .mode = COMMAND_ANY,
1137 .help = "srst adapter command group",
1138 .usage = "",
1139 .chain = adapter_srst_command_handlers,
1140 },
1141 {
1142 .name = "transports",
1143 .handler = adapter_transports_command,
1144 .mode = COMMAND_CONFIG,
1145 .help = "Declare transports the adapter supports.",
1146 .usage = "transport ...",
1147 },
1148 {
1149 .name = "usb",
1150 .mode = COMMAND_ANY,
1151 .help = "usb adapter command group",
1152 .usage = "",
1153 .chain = adapter_usb_command_handlers,
1154 },
1155 {
1156 .name = "assert",
1157 .handler = handle_adapter_reset_de_assert,
1158 .mode = COMMAND_EXEC,
1159 .help = "Controls SRST and TRST lines.",
1160 .usage = "|deassert [srst|trst [assert|deassert srst|trst]]",
1161 },
1162 {
1163 .name = "deassert",
1164 .handler = handle_adapter_reset_de_assert,
1165 .mode = COMMAND_EXEC,
1166 .help = "Controls SRST and TRST lines.",
1167 .usage = "|assert [srst|trst [deassert|assert srst|trst]]",
1168 },
1169 {
1170 .name = "gpio",
1171 .handler = adapter_gpio_config_handler,
1172 .mode = COMMAND_CONFIG,
1173 .help = "gpio adapter command group",
1174 .usage = "[ tdo|tdi|tms|tck|trst|swdio|swdio_dir|swclk|srst|led"
1175 "[gpio_number] "
1176 "[-chip chip_number] "
1177 "[-active-high|-active-low] "
1178 "[-push-pull|-open-drain|-open-source] "
1179 "[-pull-none|-pull-up|-pull-down]"
1180 "[-init-inactive|-init-active|-init-input] ]",
1181 },
1182 COMMAND_REGISTRATION_DONE
1183 };
1184
1185 static const struct command_registration interface_command_handlers[] = {
1186 {
1187 .name = "adapter",
1188 .mode = COMMAND_ANY,
1189 .help = "adapter command group",
1190 .usage = "",
1191 .chain = adapter_command_handlers,
1192 },
1193 {
1194 .name = "reset_config",
1195 .handler = handle_reset_config_command,
1196 .mode = COMMAND_ANY,
1197 .help = "configure adapter reset behavior",
1198 .usage = "[none|trst_only|srst_only|trst_and_srst] "
1199 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
1200 "[srst_gates_jtag|srst_nogate] "
1201 "[trst_push_pull|trst_open_drain] "
1202 "[srst_push_pull|srst_open_drain] "
1203 "[connect_deassert_srst|connect_assert_srst]",
1204 },
1205 COMMAND_REGISTRATION_DONE
1206 };
1207
1208 /**
1209 * Register the commands which deal with arbitrary debug adapter drivers.
1210 *
1211 * @todo Remove internal assumptions that all debug adapters use JTAG for
1212 * transport. Various types and data structures are not named generically.
1213 */
1214 int adapter_register_commands(struct command_context *ctx)
1215 {
1216 return register_commands(ctx, NULL, interface_command_handlers);
1217 }
1218
1219 const char *adapter_gpio_get_name(enum adapter_gpio_config_index idx)
1220 {
1221 return gpio_map[idx].name;
1222 }
1223
1224 /* Allow drivers access to the GPIO configuration */
1225 const struct adapter_gpio_config *adapter_gpio_get_config(void)
1226 {
1227 return adapter_config.gpios;
1228 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)