1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
16 #include "minidriver.h"
17 #include "interface.h"
18 #include "interfaces.h"
19 #include <transport/transport.h>
23 * Holds support for configuring debug adapters from TCl scripts.
26 struct adapter_driver
*adapter_driver
;
27 const char * const jtag_only
[] = { "jtag", NULL
};
29 enum adapter_clk_mode
{
30 CLOCK_MODE_UNSELECTED
= 0,
35 #define DEFAULT_CLOCK_SPEED_KHZ 100U
38 * Adapter configuration
41 bool adapter_initialized
;
44 enum adapter_clk_mode clock_mode
;
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 */
51 static const struct gpio_map
{
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, },
69 bool is_adapter_initialized(void)
71 return adapter_config
.adapter_initialized
;
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.
78 static void sync_adapter_reset_with_gpios(void)
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
;
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
;
88 adapter_config
.gpios
[ADAPTER_GPIO_IDX_TRST
].drive
= ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL
;
91 static void adapter_driver_gpios_init(void)
93 if (adapter_config
.gpios_initialized
)
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
;
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();
109 /* JTAG GPIOs should be inactive except for tms */
110 adapter_config
.gpios
[ADAPTER_GPIO_IDX_TMS
].init_state
= ADAPTER_GPIO_INIT_STATE_ACTIVE
;
112 adapter_config
.gpios_initialized
= true;
116 * Do low-level setup like initializing registers, output signals,
119 int adapter_init(struct command_context
*cmd_ctx
)
121 if (is_adapter_initialized())
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
;
131 adapter_driver_gpios_init();
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
;
148 retval
= adapter_driver
->init();
149 if (retval
!= ERROR_OK
)
151 adapter_config
.adapter_initialized
= true;
153 if (!adapter_driver
->speed
) {
154 LOG_INFO("Note: The adapter \"%s\" doesn't support configurable speed", adapter_driver
->name
);
158 int requested_khz
= adapter_get_speed_khz();
159 int actual_khz
= requested_khz
;
161 retval
= adapter_get_speed(&speed_var
);
162 if (retval
!= ERROR_OK
)
164 retval
= adapter_driver
->speed(speed_var
);
165 if (retval
!= ERROR_OK
)
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"
177 LOG_INFO("clock speed %d kHz", actual_khz
);
179 LOG_INFO("RCLK (adaptive clock speed)");
184 int adapter_quit(void)
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
);
193 free(adapter_config
.serial
);
194 free(adapter_config
.usb_location
);
196 struct jtag_tap
*t
= jtag_all_taps();
198 struct jtag_tap
*n
= t
->next_tap
;
206 unsigned int adapter_get_speed_khz(void)
208 return adapter_config
.speed_khz
;
211 static int adapter_khz_to_speed(unsigned int khz
, int *speed
)
213 LOG_DEBUG("convert khz to adapter specific speed value");
214 adapter_config
.speed_khz
= khz
;
215 if (!is_adapter_initialized())
217 LOG_DEBUG("have adapter set up");
218 if (!adapter_driver
->khz
) {
219 LOG_ERROR("Translation from khz to adapter speed not implemented");
223 int retval
= adapter_driver
->khz(adapter_get_speed_khz(), &speed_div1
);
224 if (retval
!= ERROR_OK
)
230 static int adapter_rclk_to_speed(unsigned int fallback_speed_khz
, int *speed
)
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
);
240 static int adapter_set_speed(int speed
)
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
;
247 int adapter_config_khz(unsigned int khz
)
249 LOG_DEBUG("handle adapter khz");
250 adapter_config
.clock_mode
= CLOCK_MODE_KHZ
;
252 int retval
= adapter_khz_to_speed(khz
, &speed
);
253 return (retval
!= ERROR_OK
) ? retval
: adapter_set_speed(speed
);
256 int adapter_config_rclk(unsigned int fallback_speed_khz
)
258 LOG_DEBUG("handle adapter rclk");
259 adapter_config
.clock_mode
= CLOCK_MODE_RCLK
;
260 adapter_config
.rclk_fallback_speed_khz
= fallback_speed_khz
;
262 int retval
= adapter_rclk_to_speed(fallback_speed_khz
, &speed
);
263 return (retval
!= ERROR_OK
) ? retval
: adapter_set_speed(speed
);
266 int adapter_get_speed(int *speed
)
268 switch (adapter_config
.clock_mode
) {
270 adapter_khz_to_speed(adapter_get_speed_khz(), speed
);
272 case CLOCK_MODE_RCLK
:
273 adapter_rclk_to_speed(adapter_config
.rclk_fallback_speed_khz
, speed
);
276 LOG_ERROR("BUG: unknown adapter clock mode");
282 int adapter_get_speed_readable(int *khz
)
285 int retval
= adapter_get_speed(&speed_var
);
286 if (retval
!= ERROR_OK
)
288 if (!is_adapter_initialized())
290 if (!adapter_driver
->speed_div
) {
291 LOG_ERROR("Translation from adapter speed to khz not implemented");
294 return adapter_driver
->speed_div(speed_var
, khz
);
297 const char *adapter_get_required_serial(void)
299 return adapter_config
.serial
;
304 * 2 * 7 chars: max 7 ports
305 * 1 char: test for overflow
309 #define USB_MAX_LOCATION_LENGTH 16
311 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
312 static void adapter_usb_set_location(const char *location
)
314 if (strnlen(location
, USB_MAX_LOCATION_LENGTH
) == USB_MAX_LOCATION_LENGTH
)
315 LOG_WARNING("usb location string is too long!!");
317 free(adapter_config
.usb_location
);
319 adapter_config
.usb_location
= strndup(location
, USB_MAX_LOCATION_LENGTH
);
321 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
323 const char *adapter_usb_get_location(void)
325 return adapter_config
.usb_location
;
328 bool adapter_usb_location_equal(uint8_t dev_bus
, uint8_t *port_path
, size_t path_len
)
330 size_t path_step
, string_length
;
334 if (!adapter_usb_get_location())
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
);
341 ptr
= strtok(loc
, "-");
343 LOG_WARNING("no '-' in usb path\n");
347 string_length
-= strnlen(ptr
, string_length
);
348 /* check bus mismatch */
349 if (atoi(ptr
) != dev_bus
)
353 while (path_step
< path_len
) {
354 ptr
= strtok(NULL
, ".");
356 /* no more tokens in path */
360 /* path mismatch at some step */
361 if (path_step
< path_len
&& atoi(ptr
) != port_path
[path_step
])
365 string_length
-= strnlen(ptr
, string_length
) + 1;
368 /* walked the full path, all elements match */
369 if (path_step
== path_len
&& !string_length
)
377 COMMAND_HANDLER(handle_adapter_name
)
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. */
384 return ERROR_COMMAND_SYNTAX_ERROR
;
386 command_print(CMD
, "%s", adapter_driver
? adapter_driver
->name
: "undefined");
391 COMMAND_HANDLER(adapter_transports_command
)
396 retval
= CALL_COMMAND_HANDLER(transport_list_parse
, &transports
);
397 if (retval
!= ERROR_OK
)
400 retval
= allow_transports(CMD_CTX
, (const char **)transports
);
402 if (retval
!= ERROR_OK
) {
403 for (unsigned i
= 0; transports
[i
]; i
++)
410 COMMAND_HANDLER(handle_adapter_list_command
)
412 if (strcmp(CMD_NAME
, "list") == 0 && CMD_ARGC
> 0)
413 return ERROR_COMMAND_SYNTAX_ERROR
;
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
);
424 COMMAND_HANDLER(handle_adapter_driver_command
)
428 /* check whether the interface is already configured */
429 if (adapter_driver
) {
430 LOG_WARNING("Interface already configured, ignoring");
434 /* interface name is a mandatory argument */
435 if (CMD_ARGC
!= 1 || CMD_ARGV
[0][0] == '\0')
436 return ERROR_COMMAND_SYNTAX_ERROR
;
438 for (unsigned i
= 0; adapter_drivers
[i
]; i
++) {
439 if (strcmp(CMD_ARGV
[0], adapter_drivers
[i
]->name
) != 0)
442 if (adapter_drivers
[i
]->commands
) {
443 retval
= register_commands(CMD_CTX
, NULL
, adapter_drivers
[i
]->commands
);
444 if (retval
!= ERROR_OK
)
448 adapter_driver
= adapter_drivers
[i
];
450 return allow_transports(CMD_CTX
, adapter_driver
->transports
);
453 /* no valid interface was found (i.e. the configuration option,
454 * didn't match one of the compiled-in interfaces
456 LOG_ERROR("The specified debug interface was not found (%s)",
458 CALL_COMMAND_HANDLER(handle_adapter_list_command
);
459 return ERROR_JTAG_INVALID_INTERFACE
;
462 COMMAND_HANDLER(handle_reset_config_command
)
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.
471 * Here we don't care about the order, and only change values
472 * which have been explicitly specified.
474 for (; CMD_ARGC
; CMD_ARGC
--, CMD_ARGV
++) {
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
;
487 LOG_ERROR("extra reset_config %s spec (%s)",
488 "gating", *CMD_ARGV
);
489 return ERROR_COMMAND_SYNTAX_ERROR
;
495 m
= RESET_HAS_TRST
| RESET_HAS_SRST
;
496 if (strcmp(*CMD_ARGV
, "none") == 0)
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
;
507 LOG_ERROR("extra reset_config %s spec (%s)",
508 "signal", *CMD_ARGV
);
509 return ERROR_COMMAND_SYNTAX_ERROR
;
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
;
527 LOG_ERROR("extra reset_config %s spec (%s)",
528 "combination", *CMD_ARGV
);
529 return ERROR_COMMAND_SYNTAX_ERROR
;
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 */;
543 LOG_ERROR("extra reset_config %s spec (%s)",
544 "trst_type", *CMD_ARGV
);
545 return ERROR_COMMAND_SYNTAX_ERROR
;
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 */;
559 LOG_ERROR("extra reset_config %s spec (%s)",
560 "srst_type", *CMD_ARGV
);
561 return ERROR_COMMAND_SYNTAX_ERROR
;
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 */;
575 LOG_ERROR("extra reset_config %s spec (%s)",
576 "connect_type", *CMD_ARGV
);
577 return ERROR_COMMAND_SYNTAX_ERROR
;
582 /* caller provided nonsense; fail */
583 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV
);
584 return ERROR_COMMAND_SYNTAX_ERROR
;
587 /* Remember the bits which were specified (mask)
588 * and their new values (new_cfg).
594 /* clear previous values of those bits, save new values */
596 int old_cfg
= jtag_get_reset_config();
600 jtag_set_reset_config(new_cfg
);
601 sync_adapter_reset_with_gpios();
604 new_cfg
= jtag_get_reset_config();
607 * Display the (now-)current reset mode
611 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
612 switch (new_cfg
& (RESET_HAS_TRST
| RESET_HAS_SRST
)) {
614 modes
[0] = "srst_only";
617 modes
[0] = "trst_only";
619 case RESET_TRST_AND_SRST
:
620 modes
[0] = "trst_and_srst";
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";
632 case RESET_TRST_PULLS_SRST
:
633 modes
[1] = "trst_pulls_srst";
635 case RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
:
636 modes
[1] = "combined";
639 modes
[1] = "separate";
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";
648 modes
[3] = " trst_push_pull";
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";
657 modes
[2] = " srst_gates_jtag";
659 if (new_cfg
& RESET_SRST_PUSH_PULL
)
660 modes
[4] = " srst_push_pull";
662 modes
[4] = " srst_open_drain";
664 if (new_cfg
& RESET_CNCT_UNDER_SRST
)
665 modes
[5] = " connect_assert_srst";
667 modes
[5] = " connect_deassert_srst";
674 command_print(CMD
, "%s %s%s%s%s%s",
676 modes
[2], modes
[3], modes
[4], modes
[5]);
681 COMMAND_HANDLER(handle_adapter_srst_delay_command
)
684 return ERROR_COMMAND_SYNTAX_ERROR
;
687 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
689 jtag_set_nsrst_delay(delay
);
691 command_print(CMD
, "adapter srst delay: %u", jtag_get_nsrst_delay());
695 COMMAND_HANDLER(handle_adapter_srst_pulse_width_command
)
698 return ERROR_COMMAND_SYNTAX_ERROR
;
701 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], width
);
703 jtag_set_nsrst_assert_width(width
);
705 command_print(CMD
, "adapter srst pulse_width: %u", jtag_get_nsrst_assert_width());
709 COMMAND_HANDLER(handle_adapter_speed_command
)
712 return ERROR_COMMAND_SYNTAX_ERROR
;
714 int retval
= ERROR_OK
;
717 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], khz
);
719 retval
= adapter_config_khz(khz
);
720 if (retval
!= ERROR_OK
)
724 int cur_speed
= adapter_get_speed_khz();
725 retval
= adapter_get_speed_readable(&cur_speed
);
726 if (retval
!= ERROR_OK
)
730 command_print(CMD
, "adapter speed: %d kHz", cur_speed
);
732 command_print(CMD
, "adapter speed: RCLK - adaptive");
737 COMMAND_HANDLER(handle_adapter_serial_command
)
740 return ERROR_COMMAND_SYNTAX_ERROR
;
742 free(adapter_config
.serial
);
743 adapter_config
.serial
= strdup(CMD_ARGV
[0]);
747 COMMAND_HANDLER(handle_adapter_reset_de_assert
)
750 VALUE_UNDEFINED
= -1,
755 enum values srst
= VALUE_UNDEFINED
;
756 enum values trst
= VALUE_UNDEFINED
;
757 enum reset_types jtag_reset_config
= jtag_get_reset_config();
761 if (transport_is_jtag()) {
762 if (jtag_reset_config
& RESET_HAS_TRST
)
763 signal
= jtag_get_trst() ? "asserted" : "deasserted";
765 signal
= "not present";
766 command_print(CMD
, "trst %s", signal
);
769 if (jtag_reset_config
& RESET_HAS_SRST
)
770 signal
= jtag_get_srst() ? "asserted" : "deasserted";
772 signal
= "not present";
773 command_print(CMD
, "srst %s", signal
);
778 if (CMD_ARGC
!= 1 && CMD_ARGC
!= 3)
779 return ERROR_COMMAND_SYNTAX_ERROR
;
781 value
= (strcmp(CMD_NAME
, "assert") == 0) ? VALUE_ASSERT
: VALUE_DEASSERT
;
782 if (strcmp(CMD_ARGV
[0], "srst") == 0)
784 else if (strcmp(CMD_ARGV
[0], "trst") == 0)
787 return ERROR_COMMAND_SYNTAX_ERROR
;
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
;
795 return ERROR_COMMAND_SYNTAX_ERROR
;
797 if (strcmp(CMD_ARGV
[2], "srst") == 0 && srst
== VALUE_UNDEFINED
)
799 else if (strcmp(CMD_ARGV
[2], "trst") == 0 && trst
== VALUE_UNDEFINED
)
802 return ERROR_COMMAND_SYNTAX_ERROR
;
805 if (trst
== VALUE_UNDEFINED
) {
806 if (transport_is_jtag())
807 trst
= jtag_get_trst() ? VALUE_ASSERT
: VALUE_DEASSERT
;
809 trst
= VALUE_DEASSERT
; /* unused, safe value */
812 if (srst
== VALUE_UNDEFINED
) {
813 if (jtag_reset_config
& RESET_HAS_SRST
)
814 srst
= jtag_get_srst() ? VALUE_ASSERT
: VALUE_DEASSERT
;
816 srst
= VALUE_DEASSERT
; /* unused, safe value */
819 if (trst
== VALUE_ASSERT
&& !transport_is_jtag()) {
820 LOG_ERROR("transport has no trst signal");
824 if (srst
== VALUE_ASSERT
&& !(jtag_reset_config
& RESET_HAS_SRST
)) {
825 LOG_ERROR("adapter has no srst signal");
829 return adapter_resets((trst
== VALUE_DEASSERT
) ? TRST_DEASSERT
: TRST_ASSERT
,
830 (srst
== VALUE_DEASSERT
) ? SRST_DEASSERT
: SRST_ASSERT
);
833 static int get_gpio_index(const char *signal_name
)
835 for (int i
= 0; i
< ADAPTER_GPIO_IDX_NUM
; ++i
) {
836 if (strcmp(gpio_map
[i
].name
, signal_name
) == 0)
842 static COMMAND_HELPER(helper_adapter_gpio_print_config
, enum adapter_gpio_config_index gpio_idx
)
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
= "";
851 switch (gpio_map
[gpio_idx
].direction
) {
852 case ADAPTER_GPIO_DIRECTION_INPUT
:
855 case ADAPTER_GPIO_DIRECTION_OUTPUT
:
858 case ADAPTER_GPIO_DIRECTION_BIDIRECTIONAL
:
859 dir
= "bidirectional";
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";
868 case ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN
:
869 drive
= ", open-drain";
871 case ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE
:
872 drive
= ", open-source";
877 switch (gpio_config
->pull
) {
878 case ADAPTER_GPIO_PULL_NONE
:
879 pull
= ", pull-none";
881 case ADAPTER_GPIO_PULL_UP
:
884 case ADAPTER_GPIO_PULL_DOWN
:
885 pull
= ", pull-down";
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";
894 case ADAPTER_GPIO_INIT_STATE_ACTIVE
:
895 init_state
= ", init-state active";
897 case ADAPTER_GPIO_INIT_STATE_INPUT
:
898 init_state
= ", init-state input";
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
);
910 COMMAND_HANDLER(helper_adapter_gpio_print_all_configs
)
912 for (int i
= 0; i
< ADAPTER_GPIO_IDX_NUM
; ++i
)
913 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_config
, i
);
917 COMMAND_HANDLER(adapter_gpio_config_handler
)
920 struct adapter_gpio_config
*gpio_config
;
922 adapter_driver_gpios_init();
925 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_all_configs
);
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
;
936 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_config
, gpio_idx
);
940 gpio_config
= &adapter_config
.gpios
[gpio_idx
];
941 while (i
< CMD_ARGC
) {
942 LOG_DEBUG("Processing %s", CMD_ARGV
[i
]);
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
;
952 if (strcmp(CMD_ARGV
[i
], "-chip") == 0) {
953 if (CMD_ARGC
- i
< 2) {
954 LOG_ERROR("-chip option requires a parameter");
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
;
965 if (strcmp(CMD_ARGV
[i
], "-active-high") == 0) {
967 gpio_config
->active_low
= false;
970 if (strcmp(CMD_ARGV
[i
], "-active-low") == 0) {
972 gpio_config
->active_low
= true;
976 if (gpio_map
[gpio_idx
].permit_drive_option
) {
977 if (strcmp(CMD_ARGV
[i
], "-push-pull") == 0) {
979 gpio_config
->drive
= ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL
;
982 if (strcmp(CMD_ARGV
[i
], "-open-drain") == 0) {
984 gpio_config
->drive
= ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN
;
987 if (strcmp(CMD_ARGV
[i
], "-open-source") == 0) {
989 gpio_config
->drive
= ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE
;
994 if (strcmp(CMD_ARGV
[i
], "-pull-none") == 0) {
996 gpio_config
->pull
= ADAPTER_GPIO_PULL_NONE
;
999 if (strcmp(CMD_ARGV
[i
], "-pull-up") == 0) {
1001 gpio_config
->pull
= ADAPTER_GPIO_PULL_UP
;
1004 if (strcmp(CMD_ARGV
[i
], "-pull-down") == 0) {
1006 gpio_config
->pull
= ADAPTER_GPIO_PULL_DOWN
;
1010 if (gpio_map
[gpio_idx
].permit_init_state_option
) {
1011 if (strcmp(CMD_ARGV
[i
], "-init-inactive") == 0) {
1013 gpio_config
->init_state
= ADAPTER_GPIO_INIT_STATE_INACTIVE
;
1016 if (strcmp(CMD_ARGV
[i
], "-init-active") == 0) {
1018 gpio_config
->init_state
= ADAPTER_GPIO_INIT_STATE_ACTIVE
;
1022 if (gpio_map
[gpio_idx
].direction
== ADAPTER_GPIO_DIRECTION_BIDIRECTIONAL
&&
1023 strcmp(CMD_ARGV
[i
], "-init-input") == 0) {
1025 gpio_config
->init_state
= ADAPTER_GPIO_INIT_STATE_INPUT
;
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
;
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
;
1045 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
1046 COMMAND_HANDLER(handle_usb_location_command
)
1049 adapter_usb_set_location(CMD_ARGV
[0]);
1051 command_print(CMD
, "adapter usb location: %s", adapter_usb_get_location());
1055 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
1057 static const struct command_registration adapter_usb_command_handlers
[] = {
1058 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
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]...]",
1066 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
1067 COMMAND_REGISTRATION_DONE
1070 static const struct command_registration adapter_srst_command_handlers
[] = {
1073 .handler
= handle_adapter_srst_delay_command
,
1074 .mode
= COMMAND_ANY
,
1075 .help
= "delay after deasserting SRST in ms",
1076 .usage
= "[milliseconds]",
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]",
1085 COMMAND_REGISTRATION_DONE
1088 static const struct command_registration adapter_command_handlers
[] = {
1091 .handler
= handle_adapter_driver_command
,
1092 .mode
= COMMAND_CONFIG
,
1093 .help
= "Select a debug adapter driver",
1094 .usage
= "driver_name",
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 "
1103 "With or without argument, display current setting.",
1108 .handler
= handle_adapter_serial_command
,
1109 .mode
= COMMAND_CONFIG
,
1110 .help
= "Set the serial number of the adapter",
1111 .usage
= "serial_string",
1115 .handler
= handle_adapter_list_command
,
1116 .mode
= COMMAND_ANY
,
1117 .help
= "List all built-in debug adapter drivers",
1122 .mode
= COMMAND_ANY
,
1123 .handler
= handle_adapter_name
,
1124 .help
= "Returns the name of the currently "
1125 "selected adapter (driver)",
1130 .mode
= COMMAND_ANY
,
1131 .help
= "srst adapter command group",
1133 .chain
= adapter_srst_command_handlers
,
1136 .name
= "transports",
1137 .handler
= adapter_transports_command
,
1138 .mode
= COMMAND_CONFIG
,
1139 .help
= "Declare transports the adapter supports.",
1140 .usage
= "transport ...",
1144 .mode
= COMMAND_ANY
,
1145 .help
= "usb adapter command group",
1147 .chain
= adapter_usb_command_handlers
,
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]]",
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]]",
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"
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] ]",
1176 COMMAND_REGISTRATION_DONE
1179 static const struct command_registration interface_command_handlers
[] = {
1182 .mode
= COMMAND_ANY
,
1183 .help
= "adapter command group",
1185 .chain
= adapter_command_handlers
,
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]",
1199 COMMAND_REGISTRATION_DONE
1203 * Register the commands which deal with arbitrary debug adapter drivers.
1205 * @todo Remove internal assumptions that all debug adapters use JTAG for
1206 * transport. Various types and data structures are not named generically.
1208 int adapter_register_commands(struct command_context
*ctx
)
1210 return register_commands(ctx
, NULL
, interface_command_handlers
);
1213 const char *adapter_gpio_get_name(enum adapter_gpio_config_index idx
)
1215 return gpio_map
[idx
].name
;
1218 /* Allow drivers access to the GPIO configuration */
1219 const struct adapter_gpio_config
*adapter_gpio_get_config(void)
1221 return adapter_config
.gpios
;