jtag: rewrite commands 'jtag newtap' and 'swd newdap' 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 COMMAND_HANDLER(handle_adapter_name)
382 {
383 /* return the name of the interface */
384 /* TCL code might need to know the exact type... */
385 /* FUTURE: we allow this as a means to "set" the interface. */
386
387 if (CMD_ARGC != 0)
388 return ERROR_COMMAND_SYNTAX_ERROR;
389
390 command_print(CMD, "%s", adapter_driver ? adapter_driver->name : "undefined");
391
392 return ERROR_OK;
393 }
394
395 COMMAND_HANDLER(adapter_transports_command)
396 {
397 char **transports;
398 int retval;
399
400 retval = CALL_COMMAND_HANDLER(transport_list_parse, &transports);
401 if (retval != ERROR_OK)
402 return retval;
403
404 retval = allow_transports(CMD_CTX, (const char **)transports);
405
406 if (retval != ERROR_OK) {
407 for (unsigned i = 0; transports[i]; i++)
408 free(transports[i]);
409 free(transports);
410 }
411 return retval;
412 }
413
414 COMMAND_HANDLER(handle_adapter_list_command)
415 {
416 if (strcmp(CMD_NAME, "list") == 0 && CMD_ARGC > 0)
417 return ERROR_COMMAND_SYNTAX_ERROR;
418
419 command_print(CMD, "The following debug adapters are available:");
420 for (unsigned i = 0; adapter_drivers[i]; i++) {
421 const char *name = adapter_drivers[i]->name;
422 command_print(CMD, "%u: %s", i + 1, name);
423 }
424
425 return ERROR_OK;
426 }
427
428 COMMAND_HANDLER(handle_adapter_driver_command)
429 {
430 int retval;
431
432 /* check whether the interface is already configured */
433 if (adapter_driver) {
434 LOG_WARNING("Interface already configured, ignoring");
435 return ERROR_OK;
436 }
437
438 /* interface name is a mandatory argument */
439 if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
440 return ERROR_COMMAND_SYNTAX_ERROR;
441
442 for (unsigned i = 0; adapter_drivers[i]; i++) {
443 if (strcmp(CMD_ARGV[0], adapter_drivers[i]->name) != 0)
444 continue;
445
446 if (adapter_drivers[i]->commands) {
447 retval = register_commands(CMD_CTX, NULL, adapter_drivers[i]->commands);
448 if (retval != ERROR_OK)
449 return retval;
450 }
451
452 adapter_driver = adapter_drivers[i];
453
454 return allow_transports(CMD_CTX, adapter_driver->transports);
455 }
456
457 /* no valid interface was found (i.e. the configuration option,
458 * didn't match one of the compiled-in interfaces
459 */
460 LOG_ERROR("The specified debug interface was not found (%s)",
461 CMD_ARGV[0]);
462 CALL_COMMAND_HANDLER(handle_adapter_list_command);
463 return ERROR_JTAG_INVALID_INTERFACE;
464 }
465
466 COMMAND_HANDLER(handle_reset_config_command)
467 {
468 int new_cfg = 0;
469 int mask = 0;
470
471 /* Original versions cared about the order of these tokens:
472 * reset_config signals [combination [trst_type [srst_type]]]
473 * They also clobbered the previous configuration even on error.
474 *
475 * Here we don't care about the order, and only change values
476 * which have been explicitly specified.
477 */
478 for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
479 int tmp = 0;
480 int m;
481
482 /* gating */
483 m = RESET_SRST_NO_GATING;
484 if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
485 /* default: don't use JTAG while SRST asserted */;
486 else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
487 tmp = RESET_SRST_NO_GATING;
488 else
489 m = 0;
490 if (mask & m) {
491 LOG_ERROR("extra reset_config %s spec (%s)",
492 "gating", *CMD_ARGV);
493 return ERROR_COMMAND_SYNTAX_ERROR;
494 }
495 if (m)
496 goto next;
497
498 /* signals */
499 m = RESET_HAS_TRST | RESET_HAS_SRST;
500 if (strcmp(*CMD_ARGV, "none") == 0)
501 tmp = RESET_NONE;
502 else if (strcmp(*CMD_ARGV, "trst_only") == 0)
503 tmp = RESET_HAS_TRST;
504 else if (strcmp(*CMD_ARGV, "srst_only") == 0)
505 tmp = RESET_HAS_SRST;
506 else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
507 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
508 else
509 m = 0;
510 if (mask & m) {
511 LOG_ERROR("extra reset_config %s spec (%s)",
512 "signal", *CMD_ARGV);
513 return ERROR_COMMAND_SYNTAX_ERROR;
514 }
515 if (m)
516 goto next;
517
518 /* combination (options for broken wiring) */
519 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
520 if (strcmp(*CMD_ARGV, "separate") == 0)
521 /* separate reset lines - default */;
522 else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
523 tmp |= RESET_SRST_PULLS_TRST;
524 else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
525 tmp |= RESET_TRST_PULLS_SRST;
526 else if (strcmp(*CMD_ARGV, "combined") == 0)
527 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
528 else
529 m = 0;
530 if (mask & m) {
531 LOG_ERROR("extra reset_config %s spec (%s)",
532 "combination", *CMD_ARGV);
533 return ERROR_COMMAND_SYNTAX_ERROR;
534 }
535 if (m)
536 goto next;
537
538 /* trst_type (NOP without HAS_TRST) */
539 m = RESET_TRST_OPEN_DRAIN;
540 if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
541 tmp |= RESET_TRST_OPEN_DRAIN;
542 else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
543 /* push/pull from adapter - default */;
544 else
545 m = 0;
546 if (mask & m) {
547 LOG_ERROR("extra reset_config %s spec (%s)",
548 "trst_type", *CMD_ARGV);
549 return ERROR_COMMAND_SYNTAX_ERROR;
550 }
551 if (m)
552 goto next;
553
554 /* srst_type (NOP without HAS_SRST) */
555 m = RESET_SRST_PUSH_PULL;
556 if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
557 tmp |= RESET_SRST_PUSH_PULL;
558 else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
559 /* open drain from adapter - default */;
560 else
561 m = 0;
562 if (mask & m) {
563 LOG_ERROR("extra reset_config %s spec (%s)",
564 "srst_type", *CMD_ARGV);
565 return ERROR_COMMAND_SYNTAX_ERROR;
566 }
567 if (m)
568 goto next;
569
570 /* connect_type - only valid when srst_nogate */
571 m = RESET_CNCT_UNDER_SRST;
572 if (strcmp(*CMD_ARGV, "connect_assert_srst") == 0)
573 tmp |= RESET_CNCT_UNDER_SRST;
574 else if (strcmp(*CMD_ARGV, "connect_deassert_srst") == 0)
575 /* connect normally - default */;
576 else
577 m = 0;
578 if (mask & m) {
579 LOG_ERROR("extra reset_config %s spec (%s)",
580 "connect_type", *CMD_ARGV);
581 return ERROR_COMMAND_SYNTAX_ERROR;
582 }
583 if (m)
584 goto next;
585
586 /* caller provided nonsense; fail */
587 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
588 return ERROR_COMMAND_SYNTAX_ERROR;
589
590 next:
591 /* Remember the bits which were specified (mask)
592 * and their new values (new_cfg).
593 */
594 mask |= m;
595 new_cfg |= tmp;
596 }
597
598 /* clear previous values of those bits, save new values */
599 if (mask) {
600 int old_cfg = jtag_get_reset_config();
601
602 old_cfg &= ~mask;
603 new_cfg |= old_cfg;
604 jtag_set_reset_config(new_cfg);
605 sync_adapter_reset_with_gpios();
606
607 } else
608 new_cfg = jtag_get_reset_config();
609
610 /*
611 * Display the (now-)current reset mode
612 */
613 char *modes[6];
614
615 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
616 switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
617 case RESET_HAS_SRST:
618 modes[0] = "srst_only";
619 break;
620 case RESET_HAS_TRST:
621 modes[0] = "trst_only";
622 break;
623 case RESET_TRST_AND_SRST:
624 modes[0] = "trst_and_srst";
625 break;
626 default:
627 modes[0] = "none";
628 break;
629 }
630
631 /* normally SRST and TRST are decoupled; but bugs happen ... */
632 switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
633 case RESET_SRST_PULLS_TRST:
634 modes[1] = "srst_pulls_trst";
635 break;
636 case RESET_TRST_PULLS_SRST:
637 modes[1] = "trst_pulls_srst";
638 break;
639 case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
640 modes[1] = "combined";
641 break;
642 default:
643 modes[1] = "separate";
644 break;
645 }
646
647 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
648 if (new_cfg & RESET_HAS_TRST) {
649 if (new_cfg & RESET_TRST_OPEN_DRAIN)
650 modes[3] = " trst_open_drain";
651 else
652 modes[3] = " trst_push_pull";
653 } else
654 modes[3] = "";
655
656 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
657 if (new_cfg & RESET_HAS_SRST) {
658 if (new_cfg & RESET_SRST_NO_GATING)
659 modes[2] = " srst_nogate";
660 else
661 modes[2] = " srst_gates_jtag";
662
663 if (new_cfg & RESET_SRST_PUSH_PULL)
664 modes[4] = " srst_push_pull";
665 else
666 modes[4] = " srst_open_drain";
667
668 if (new_cfg & RESET_CNCT_UNDER_SRST)
669 modes[5] = " connect_assert_srst";
670 else
671 modes[5] = " connect_deassert_srst";
672 } else {
673 modes[2] = "";
674 modes[4] = "";
675 modes[5] = "";
676 }
677
678 command_print(CMD, "%s %s%s%s%s%s",
679 modes[0], modes[1],
680 modes[2], modes[3], modes[4], modes[5]);
681
682 return ERROR_OK;
683 }
684
685 COMMAND_HANDLER(handle_adapter_srst_delay_command)
686 {
687 if (CMD_ARGC > 1)
688 return ERROR_COMMAND_SYNTAX_ERROR;
689 if (CMD_ARGC == 1) {
690 unsigned delay;
691 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
692
693 jtag_set_nsrst_delay(delay);
694 }
695 command_print(CMD, "adapter srst delay: %u", jtag_get_nsrst_delay());
696 return ERROR_OK;
697 }
698
699 COMMAND_HANDLER(handle_adapter_srst_pulse_width_command)
700 {
701 if (CMD_ARGC > 1)
702 return ERROR_COMMAND_SYNTAX_ERROR;
703 if (CMD_ARGC == 1) {
704 unsigned width;
705 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], width);
706
707 jtag_set_nsrst_assert_width(width);
708 }
709 command_print(CMD, "adapter srst pulse_width: %u", jtag_get_nsrst_assert_width());
710 return ERROR_OK;
711 }
712
713 COMMAND_HANDLER(handle_adapter_speed_command)
714 {
715 if (CMD_ARGC > 1)
716 return ERROR_COMMAND_SYNTAX_ERROR;
717
718 int retval = ERROR_OK;
719 if (CMD_ARGC == 1) {
720 unsigned khz = 0;
721 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
722
723 retval = adapter_config_khz(khz);
724 if (retval != ERROR_OK)
725 return retval;
726 }
727
728 int cur_speed = adapter_get_speed_khz();
729 retval = adapter_get_speed_readable(&cur_speed);
730 if (retval != ERROR_OK)
731 return retval;
732
733 if (cur_speed)
734 command_print(CMD, "adapter speed: %d kHz", cur_speed);
735 else
736 command_print(CMD, "adapter speed: RCLK - adaptive");
737
738 return retval;
739 }
740
741 COMMAND_HANDLER(handle_adapter_serial_command)
742 {
743 if (CMD_ARGC != 1)
744 return ERROR_COMMAND_SYNTAX_ERROR;
745
746 free(adapter_config.serial);
747 adapter_config.serial = strdup(CMD_ARGV[0]);
748 return ERROR_OK;
749 }
750
751 COMMAND_HANDLER(handle_adapter_reset_de_assert)
752 {
753 enum values {
754 VALUE_UNDEFINED = -1,
755 VALUE_DEASSERT = 0,
756 VALUE_ASSERT = 1,
757 };
758 enum values value;
759 enum values srst = VALUE_UNDEFINED;
760 enum values trst = VALUE_UNDEFINED;
761 enum reset_types jtag_reset_config = jtag_get_reset_config();
762 char *signal;
763
764 if (CMD_ARGC == 0) {
765 if (transport_is_jtag()) {
766 if (jtag_reset_config & RESET_HAS_TRST)
767 signal = jtag_get_trst() ? "asserted" : "deasserted";
768 else
769 signal = "not present";
770 command_print(CMD, "trst %s", signal);
771 }
772
773 if (jtag_reset_config & RESET_HAS_SRST)
774 signal = jtag_get_srst() ? "asserted" : "deasserted";
775 else
776 signal = "not present";
777 command_print(CMD, "srst %s", signal);
778
779 return ERROR_OK;
780 }
781
782 if (CMD_ARGC != 1 && CMD_ARGC != 3)
783 return ERROR_COMMAND_SYNTAX_ERROR;
784
785 value = (strcmp(CMD_NAME, "assert") == 0) ? VALUE_ASSERT : VALUE_DEASSERT;
786 if (strcmp(CMD_ARGV[0], "srst") == 0)
787 srst = value;
788 else if (strcmp(CMD_ARGV[0], "trst") == 0)
789 trst = value;
790 else
791 return ERROR_COMMAND_SYNTAX_ERROR;
792
793 if (CMD_ARGC == 3) {
794 if (strcmp(CMD_ARGV[1], "assert") == 0)
795 value = VALUE_ASSERT;
796 else if (strcmp(CMD_ARGV[1], "deassert") == 0)
797 value = VALUE_DEASSERT;
798 else
799 return ERROR_COMMAND_SYNTAX_ERROR;
800
801 if (strcmp(CMD_ARGV[2], "srst") == 0 && srst == VALUE_UNDEFINED)
802 srst = value;
803 else if (strcmp(CMD_ARGV[2], "trst") == 0 && trst == VALUE_UNDEFINED)
804 trst = value;
805 else
806 return ERROR_COMMAND_SYNTAX_ERROR;
807 }
808
809 if (trst == VALUE_UNDEFINED) {
810 if (transport_is_jtag())
811 trst = jtag_get_trst() ? VALUE_ASSERT : VALUE_DEASSERT;
812 else
813 trst = VALUE_DEASSERT; /* unused, safe value */
814 }
815
816 if (srst == VALUE_UNDEFINED) {
817 if (jtag_reset_config & RESET_HAS_SRST)
818 srst = jtag_get_srst() ? VALUE_ASSERT : VALUE_DEASSERT;
819 else
820 srst = VALUE_DEASSERT; /* unused, safe value */
821 }
822
823 if (trst == VALUE_ASSERT && !transport_is_jtag()) {
824 LOG_ERROR("transport has no trst signal");
825 return ERROR_FAIL;
826 }
827
828 if (srst == VALUE_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
829 LOG_ERROR("adapter has no srst signal");
830 return ERROR_FAIL;
831 }
832
833 return adapter_resets((trst == VALUE_DEASSERT) ? TRST_DEASSERT : TRST_ASSERT,
834 (srst == VALUE_DEASSERT) ? SRST_DEASSERT : SRST_ASSERT);
835 }
836
837 static int get_gpio_index(const char *signal_name)
838 {
839 for (int i = 0; i < ADAPTER_GPIO_IDX_NUM; ++i) {
840 if (strcmp(gpio_map[i].name, signal_name) == 0)
841 return i;
842 }
843 return -1;
844 }
845
846 static COMMAND_HELPER(helper_adapter_gpio_print_config, enum adapter_gpio_config_index gpio_idx)
847 {
848 struct adapter_gpio_config *gpio_config = &adapter_config.gpios[gpio_idx];
849 const char *active_state = gpio_config->active_low ? "low" : "high";
850 const char *dir = "";
851 const char *drive = "";
852 const char *pull = "";
853 const char *init_state = "";
854
855 switch (gpio_map[gpio_idx].direction) {
856 case ADAPTER_GPIO_DIRECTION_INPUT:
857 dir = "input";
858 break;
859 case ADAPTER_GPIO_DIRECTION_OUTPUT:
860 dir = "output";
861 break;
862 case ADAPTER_GPIO_DIRECTION_BIDIRECTIONAL:
863 dir = "bidirectional";
864 break;
865 }
866
867 if (gpio_map[gpio_idx].permit_drive_option) {
868 switch (gpio_config->drive) {
869 case ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL:
870 drive = ", push-pull";
871 break;
872 case ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN:
873 drive = ", open-drain";
874 break;
875 case ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE:
876 drive = ", open-source";
877 break;
878 }
879 }
880
881 switch (gpio_config->pull) {
882 case ADAPTER_GPIO_PULL_NONE:
883 pull = ", pull-none";
884 break;
885 case ADAPTER_GPIO_PULL_UP:
886 pull = ", pull-up";
887 break;
888 case ADAPTER_GPIO_PULL_DOWN:
889 pull = ", pull-down";
890 break;
891 }
892
893 if (gpio_map[gpio_idx].permit_init_state_option) {
894 switch (gpio_config->init_state) {
895 case ADAPTER_GPIO_INIT_STATE_INACTIVE:
896 init_state = ", init-state inactive";
897 break;
898 case ADAPTER_GPIO_INIT_STATE_ACTIVE:
899 init_state = ", init-state active";
900 break;
901 case ADAPTER_GPIO_INIT_STATE_INPUT:
902 init_state = ", init-state input";
903 break;
904 }
905 }
906
907 command_print(CMD, "adapter gpio %s (%s): num %d, chip %d, active-%s%s%s%s",
908 gpio_map[gpio_idx].name, dir, gpio_config->gpio_num, gpio_config->chip_num, active_state,
909 drive, pull, init_state);
910
911 return ERROR_OK;
912 }
913
914 COMMAND_HANDLER(helper_adapter_gpio_print_all_configs)
915 {
916 for (int i = 0; i < ADAPTER_GPIO_IDX_NUM; ++i)
917 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_config, i);
918 return ERROR_OK;
919 }
920
921 COMMAND_HANDLER(adapter_gpio_config_handler)
922 {
923 unsigned int i = 1;
924 struct adapter_gpio_config *gpio_config;
925
926 adapter_driver_gpios_init();
927
928 if (CMD_ARGC == 0) {
929 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_all_configs);
930 return ERROR_OK;
931 }
932
933 int gpio_idx = get_gpio_index(CMD_ARGV[0]);
934 if (gpio_idx == -1) {
935 LOG_ERROR("adapter has no gpio named %s", CMD_ARGV[0]);
936 return ERROR_COMMAND_SYNTAX_ERROR;
937 }
938
939 if (CMD_ARGC == 1) {
940 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_config, gpio_idx);
941 return ERROR_OK;
942 }
943
944 gpio_config = &adapter_config.gpios[gpio_idx];
945 while (i < CMD_ARGC) {
946 LOG_DEBUG("Processing %s", CMD_ARGV[i]);
947
948 if (isdigit(*CMD_ARGV[i])) {
949 int gpio_num; /* Use a meaningful output parameter for more helpful error messages */
950 COMMAND_PARSE_NUMBER(int, CMD_ARGV[i], gpio_num);
951 gpio_config->gpio_num = 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 int chip_num; /* Use a meaningful output parameter for more helpful error messages */
963 COMMAND_PARSE_NUMBER(int, CMD_ARGV[i + 1], chip_num);
964 gpio_config->chip_num = chip_num;
965 i += 2;
966 continue;
967 }
968
969 if (strcmp(CMD_ARGV[i], "-active-high") == 0) {
970 ++i;
971 gpio_config->active_low = false;
972 continue;
973 }
974 if (strcmp(CMD_ARGV[i], "-active-low") == 0) {
975 ++i;
976 gpio_config->active_low = true;
977 continue;
978 }
979
980 if (gpio_map[gpio_idx].permit_drive_option) {
981 if (strcmp(CMD_ARGV[i], "-push-pull") == 0) {
982 ++i;
983 gpio_config->drive = ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL;
984 continue;
985 }
986 if (strcmp(CMD_ARGV[i], "-open-drain") == 0) {
987 ++i;
988 gpio_config->drive = ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN;
989 continue;
990 }
991 if (strcmp(CMD_ARGV[i], "-open-source") == 0) {
992 ++i;
993 gpio_config->drive = ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE;
994 continue;
995 }
996 }
997
998 if (strcmp(CMD_ARGV[i], "-pull-none") == 0) {
999 ++i;
1000 gpio_config->pull = ADAPTER_GPIO_PULL_NONE;
1001 continue;
1002 }
1003 if (strcmp(CMD_ARGV[i], "-pull-up") == 0) {
1004 ++i;
1005 gpio_config->pull = ADAPTER_GPIO_PULL_UP;
1006 continue;
1007 }
1008 if (strcmp(CMD_ARGV[i], "-pull-down") == 0) {
1009 ++i;
1010 gpio_config->pull = ADAPTER_GPIO_PULL_DOWN;
1011 continue;
1012 }
1013
1014 if (gpio_map[gpio_idx].permit_init_state_option) {
1015 if (strcmp(CMD_ARGV[i], "-init-inactive") == 0) {
1016 ++i;
1017 gpio_config->init_state = ADAPTER_GPIO_INIT_STATE_INACTIVE;
1018 continue;
1019 }
1020 if (strcmp(CMD_ARGV[i], "-init-active") == 0) {
1021 ++i;
1022 gpio_config->init_state = ADAPTER_GPIO_INIT_STATE_ACTIVE;
1023 continue;
1024 }
1025
1026 if (gpio_map[gpio_idx].direction == ADAPTER_GPIO_DIRECTION_BIDIRECTIONAL &&
1027 strcmp(CMD_ARGV[i], "-init-input") == 0) {
1028 ++i;
1029 gpio_config->init_state = ADAPTER_GPIO_INIT_STATE_INPUT;
1030 continue;
1031 }
1032 }
1033
1034 LOG_ERROR("illegal option for adapter %s %s: %s",
1035 CMD_NAME, gpio_map[gpio_idx].name, CMD_ARGV[i]);
1036 return ERROR_COMMAND_SYNTAX_ERROR;
1037 }
1038
1039 /* Force swdio_dir init state to be compatible with swdio init state */
1040 if (gpio_idx == ADAPTER_GPIO_IDX_SWDIO)
1041 adapter_config.gpios[ADAPTER_GPIO_IDX_SWDIO_DIR].init_state =
1042 (gpio_config->init_state == ADAPTER_GPIO_INIT_STATE_INPUT) ?
1043 ADAPTER_GPIO_INIT_STATE_INACTIVE :
1044 ADAPTER_GPIO_INIT_STATE_ACTIVE;
1045
1046 return ERROR_OK;
1047 }
1048
1049 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
1050 COMMAND_HANDLER(handle_usb_location_command)
1051 {
1052 if (CMD_ARGC == 1)
1053 adapter_usb_set_location(CMD_ARGV[0]);
1054
1055 command_print(CMD, "adapter usb location: %s", adapter_usb_get_location());
1056
1057 return ERROR_OK;
1058 }
1059 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
1060
1061 static const struct command_registration adapter_usb_command_handlers[] = {
1062 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
1063 {
1064 .name = "location",
1065 .handler = &handle_usb_location_command,
1066 .mode = COMMAND_CONFIG,
1067 .help = "display or set the USB bus location of the USB device",
1068 .usage = "[<bus>-port[.port]...]",
1069 },
1070 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
1071 COMMAND_REGISTRATION_DONE
1072 };
1073
1074 static const struct command_registration adapter_srst_command_handlers[] = {
1075 {
1076 .name = "delay",
1077 .handler = handle_adapter_srst_delay_command,
1078 .mode = COMMAND_ANY,
1079 .help = "delay after deasserting SRST in ms",
1080 .usage = "[milliseconds]",
1081 },
1082 {
1083 .name = "pulse_width",
1084 .handler = handle_adapter_srst_pulse_width_command,
1085 .mode = COMMAND_ANY,
1086 .help = "SRST assertion pulse width in ms",
1087 .usage = "[milliseconds]",
1088 },
1089 COMMAND_REGISTRATION_DONE
1090 };
1091
1092 static const struct command_registration adapter_command_handlers[] = {
1093 {
1094 .name = "driver",
1095 .handler = handle_adapter_driver_command,
1096 .mode = COMMAND_CONFIG,
1097 .help = "Select a debug adapter driver",
1098 .usage = "driver_name",
1099 },
1100 {
1101 .name = "speed",
1102 .handler = handle_adapter_speed_command,
1103 .mode = COMMAND_ANY,
1104 .help = "With an argument, change to the specified maximum "
1105 "jtag speed. For JTAG, 0 KHz signifies adaptive "
1106 "clocking. "
1107 "With or without argument, display current setting.",
1108 .usage = "[khz]",
1109 },
1110 {
1111 .name = "serial",
1112 .handler = handle_adapter_serial_command,
1113 .mode = COMMAND_CONFIG,
1114 .help = "Set the serial number of the adapter",
1115 .usage = "serial_string",
1116 },
1117 {
1118 .name = "list",
1119 .handler = handle_adapter_list_command,
1120 .mode = COMMAND_ANY,
1121 .help = "List all built-in debug adapter drivers",
1122 .usage = "",
1123 },
1124 {
1125 .name = "name",
1126 .mode = COMMAND_ANY,
1127 .handler = handle_adapter_name,
1128 .help = "Returns the name of the currently "
1129 "selected adapter (driver)",
1130 .usage = "",
1131 },
1132 {
1133 .name = "srst",
1134 .mode = COMMAND_ANY,
1135 .help = "srst adapter command group",
1136 .usage = "",
1137 .chain = adapter_srst_command_handlers,
1138 },
1139 {
1140 .name = "transports",
1141 .handler = adapter_transports_command,
1142 .mode = COMMAND_CONFIG,
1143 .help = "Declare transports the adapter supports.",
1144 .usage = "transport ...",
1145 },
1146 {
1147 .name = "usb",
1148 .mode = COMMAND_ANY,
1149 .help = "usb adapter command group",
1150 .usage = "",
1151 .chain = adapter_usb_command_handlers,
1152 },
1153 {
1154 .name = "assert",
1155 .handler = handle_adapter_reset_de_assert,
1156 .mode = COMMAND_EXEC,
1157 .help = "Controls SRST and TRST lines.",
1158 .usage = "|deassert [srst|trst [assert|deassert srst|trst]]",
1159 },
1160 {
1161 .name = "deassert",
1162 .handler = handle_adapter_reset_de_assert,
1163 .mode = COMMAND_EXEC,
1164 .help = "Controls SRST and TRST lines.",
1165 .usage = "|assert [srst|trst [deassert|assert srst|trst]]",
1166 },
1167 {
1168 .name = "gpio",
1169 .handler = adapter_gpio_config_handler,
1170 .mode = COMMAND_CONFIG,
1171 .help = "gpio adapter command group",
1172 .usage = "[ tdo|tdi|tms|tck|trst|swdio|swdio_dir|swclk|srst|led"
1173 "[gpio_number] "
1174 "[-chip chip_number] "
1175 "[-active-high|-active-low] "
1176 "[-push-pull|-open-drain|-open-source] "
1177 "[-pull-none|-pull-up|-pull-down]"
1178 "[-init-inactive|-init-active|-init-input] ]",
1179 },
1180 COMMAND_REGISTRATION_DONE
1181 };
1182
1183 static const struct command_registration interface_command_handlers[] = {
1184 {
1185 .name = "adapter",
1186 .mode = COMMAND_ANY,
1187 .help = "adapter command group",
1188 .usage = "",
1189 .chain = adapter_command_handlers,
1190 },
1191 {
1192 .name = "reset_config",
1193 .handler = handle_reset_config_command,
1194 .mode = COMMAND_ANY,
1195 .help = "configure adapter reset behavior",
1196 .usage = "[none|trst_only|srst_only|trst_and_srst] "
1197 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
1198 "[srst_gates_jtag|srst_nogate] "
1199 "[trst_push_pull|trst_open_drain] "
1200 "[srst_push_pull|srst_open_drain] "
1201 "[connect_deassert_srst|connect_assert_srst]",
1202 },
1203 COMMAND_REGISTRATION_DONE
1204 };
1205
1206 /**
1207 * Register the commands which deal with arbitrary debug adapter drivers.
1208 *
1209 * @todo Remove internal assumptions that all debug adapters use JTAG for
1210 * transport. Various types and data structures are not named generically.
1211 */
1212 int adapter_register_commands(struct command_context *ctx)
1213 {
1214 return register_commands(ctx, NULL, interface_command_handlers);
1215 }
1216
1217 const char *adapter_gpio_get_name(enum adapter_gpio_config_index idx)
1218 {
1219 return gpio_map[idx].name;
1220 }
1221
1222 /* Allow drivers access to the GPIO configuration */
1223 const struct adapter_gpio_config *adapter_gpio_get_config(void)
1224 {
1225 return adapter_config.gpios;
1226 }

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)