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

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)