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

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)