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 /* 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
;
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();
110 /* JTAG GPIOs should be inactive except for tms */
111 adapter_config
.gpios
[ADAPTER_GPIO_IDX_TMS
].init_state
= ADAPTER_GPIO_INIT_STATE_ACTIVE
;
113 adapter_config
.gpios_initialized
= true;
117 * Do low-level setup like initializing registers, output signals,
120 int adapter_init(struct command_context
*cmd_ctx
)
122 if (is_adapter_initialized())
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
;
132 adapter_driver_gpios_init();
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
;
149 retval
= adapter_driver
->init();
150 if (retval
!= ERROR_OK
)
152 adapter_config
.adapter_initialized
= true;
154 if (!adapter_driver
->speed
) {
155 LOG_INFO("Note: The adapter \"%s\" doesn't support configurable speed", adapter_driver
->name
);
159 int requested_khz
= adapter_get_speed_khz();
160 int actual_khz
= requested_khz
;
162 retval
= adapter_get_speed(&speed_var
);
163 if (retval
!= ERROR_OK
)
165 retval
= adapter_driver
->speed(speed_var
);
166 if (retval
!= ERROR_OK
)
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"
178 LOG_INFO("clock speed %d kHz", actual_khz
);
180 LOG_INFO("RCLK (adaptive clock speed)");
185 int adapter_quit(void)
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
);
194 free(adapter_config
.serial
);
195 free(adapter_config
.usb_location
);
197 struct jtag_tap
*t
= jtag_all_taps();
199 struct jtag_tap
*n
= t
->next_tap
;
207 unsigned int adapter_get_speed_khz(void)
209 return adapter_config
.speed_khz
;
212 static int adapter_khz_to_speed(unsigned int khz
, int *speed
)
214 LOG_DEBUG("convert khz to adapter specific speed value");
215 adapter_config
.speed_khz
= khz
;
216 if (!is_adapter_initialized())
218 LOG_DEBUG("have adapter set up");
219 if (!adapter_driver
->khz
) {
220 LOG_ERROR("Translation from khz to adapter speed not implemented");
224 int retval
= adapter_driver
->khz(adapter_get_speed_khz(), &speed_div1
);
225 if (retval
!= ERROR_OK
)
231 static int adapter_rclk_to_speed(unsigned int fallback_speed_khz
, int *speed
)
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
);
241 static int adapter_set_speed(int speed
)
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
;
248 int adapter_config_khz(unsigned int khz
)
250 LOG_DEBUG("handle adapter khz");
251 adapter_config
.clock_mode
= CLOCK_MODE_KHZ
;
253 int retval
= adapter_khz_to_speed(khz
, &speed
);
254 return (retval
!= ERROR_OK
) ? retval
: adapter_set_speed(speed
);
257 int adapter_config_rclk(unsigned int fallback_speed_khz
)
259 LOG_DEBUG("handle adapter rclk");
260 adapter_config
.clock_mode
= CLOCK_MODE_RCLK
;
261 adapter_config
.rclk_fallback_speed_khz
= fallback_speed_khz
;
263 int retval
= adapter_rclk_to_speed(fallback_speed_khz
, &speed
);
264 return (retval
!= ERROR_OK
) ? retval
: adapter_set_speed(speed
);
267 int adapter_get_speed(int *speed
)
269 switch (adapter_config
.clock_mode
) {
271 adapter_khz_to_speed(adapter_get_speed_khz(), speed
);
273 case CLOCK_MODE_RCLK
:
274 adapter_rclk_to_speed(adapter_config
.rclk_fallback_speed_khz
, speed
);
277 LOG_ERROR("BUG: unknown adapter clock mode");
283 int adapter_get_speed_readable(int *khz
)
286 int retval
= adapter_get_speed(&speed_var
);
287 if (retval
!= ERROR_OK
)
289 if (!is_adapter_initialized())
291 if (!adapter_driver
->speed_div
) {
292 LOG_ERROR("Translation from adapter speed to khz not implemented");
295 return adapter_driver
->speed_div(speed_var
, khz
);
298 const char *adapter_get_required_serial(void)
300 return adapter_config
.serial
;
305 * 2 * 7 chars: max 7 ports
306 * 1 char: test for overflow
310 #define USB_MAX_LOCATION_LENGTH 16
312 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
313 static void adapter_usb_set_location(const char *location
)
315 if (strnlen(location
, USB_MAX_LOCATION_LENGTH
) == USB_MAX_LOCATION_LENGTH
)
316 LOG_WARNING("usb location string is too long!!");
318 free(adapter_config
.usb_location
);
320 adapter_config
.usb_location
= strndup(location
, USB_MAX_LOCATION_LENGTH
);
322 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
324 const char *adapter_usb_get_location(void)
326 return adapter_config
.usb_location
;
329 bool adapter_usb_location_equal(uint8_t dev_bus
, uint8_t *port_path
, size_t path_len
)
331 size_t path_step
, string_length
;
335 if (!adapter_usb_get_location())
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
);
342 ptr
= strtok(loc
, "-");
344 LOG_WARNING("no '-' in usb path\n");
348 string_length
-= strnlen(ptr
, string_length
);
349 /* check bus mismatch */
350 if (atoi(ptr
) != dev_bus
)
354 while (path_step
< path_len
) {
355 ptr
= strtok(NULL
, ".");
357 /* no more tokens in path */
361 /* path mismatch at some step */
362 if (path_step
< path_len
&& atoi(ptr
) != port_path
[path_step
])
366 string_length
-= strnlen(ptr
, string_length
) + 1;
369 /* walked the full path, all elements match */
370 if (path_step
== path_len
&& !string_length
)
378 COMMAND_HANDLER(handle_adapter_name
)
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. */
385 return ERROR_COMMAND_SYNTAX_ERROR
;
387 command_print(CMD
, "%s", adapter_driver
? adapter_driver
->name
: "undefined");
392 COMMAND_HANDLER(adapter_transports_command
)
397 retval
= CALL_COMMAND_HANDLER(transport_list_parse
, &transports
);
398 if (retval
!= ERROR_OK
)
401 retval
= allow_transports(CMD_CTX
, (const char **)transports
);
403 if (retval
!= ERROR_OK
) {
404 for (unsigned i
= 0; transports
[i
]; i
++)
411 COMMAND_HANDLER(handle_adapter_list_command
)
413 if (strcmp(CMD_NAME
, "list") == 0 && CMD_ARGC
> 0)
414 return ERROR_COMMAND_SYNTAX_ERROR
;
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
);
425 COMMAND_HANDLER(handle_adapter_driver_command
)
429 /* check whether the interface is already configured */
430 if (adapter_driver
) {
431 LOG_WARNING("Interface already configured, ignoring");
435 /* interface name is a mandatory argument */
436 if (CMD_ARGC
!= 1 || CMD_ARGV
[0][0] == '\0')
437 return ERROR_COMMAND_SYNTAX_ERROR
;
439 for (unsigned i
= 0; adapter_drivers
[i
]; i
++) {
440 if (strcmp(CMD_ARGV
[0], adapter_drivers
[i
]->name
) != 0)
443 if (adapter_drivers
[i
]->commands
) {
444 retval
= register_commands(CMD_CTX
, NULL
, adapter_drivers
[i
]->commands
);
445 if (retval
!= ERROR_OK
)
449 adapter_driver
= adapter_drivers
[i
];
451 return allow_transports(CMD_CTX
, adapter_driver
->transports
);
454 /* no valid interface was found (i.e. the configuration option,
455 * didn't match one of the compiled-in interfaces
457 LOG_ERROR("The specified debug interface was not found (%s)",
459 CALL_COMMAND_HANDLER(handle_adapter_list_command
);
460 return ERROR_JTAG_INVALID_INTERFACE
;
463 COMMAND_HANDLER(handle_reset_config_command
)
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.
472 * Here we don't care about the order, and only change values
473 * which have been explicitly specified.
475 for (; CMD_ARGC
; CMD_ARGC
--, CMD_ARGV
++) {
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
;
488 LOG_ERROR("extra reset_config %s spec (%s)",
489 "gating", *CMD_ARGV
);
490 return ERROR_COMMAND_SYNTAX_ERROR
;
496 m
= RESET_HAS_TRST
| RESET_HAS_SRST
;
497 if (strcmp(*CMD_ARGV
, "none") == 0)
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
;
508 LOG_ERROR("extra reset_config %s spec (%s)",
509 "signal", *CMD_ARGV
);
510 return ERROR_COMMAND_SYNTAX_ERROR
;
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
;
528 LOG_ERROR("extra reset_config %s spec (%s)",
529 "combination", *CMD_ARGV
);
530 return ERROR_COMMAND_SYNTAX_ERROR
;
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 */;
544 LOG_ERROR("extra reset_config %s spec (%s)",
545 "trst_type", *CMD_ARGV
);
546 return ERROR_COMMAND_SYNTAX_ERROR
;
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 */;
560 LOG_ERROR("extra reset_config %s spec (%s)",
561 "srst_type", *CMD_ARGV
);
562 return ERROR_COMMAND_SYNTAX_ERROR
;
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 */;
576 LOG_ERROR("extra reset_config %s spec (%s)",
577 "connect_type", *CMD_ARGV
);
578 return ERROR_COMMAND_SYNTAX_ERROR
;
583 /* caller provided nonsense; fail */
584 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV
);
585 return ERROR_COMMAND_SYNTAX_ERROR
;
588 /* Remember the bits which were specified (mask)
589 * and their new values (new_cfg).
595 /* clear previous values of those bits, save new values */
597 int old_cfg
= jtag_get_reset_config();
601 jtag_set_reset_config(new_cfg
);
602 sync_adapter_reset_with_gpios();
605 new_cfg
= jtag_get_reset_config();
608 * Display the (now-)current reset mode
612 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
613 switch (new_cfg
& (RESET_HAS_TRST
| RESET_HAS_SRST
)) {
615 modes
[0] = "srst_only";
618 modes
[0] = "trst_only";
620 case RESET_TRST_AND_SRST
:
621 modes
[0] = "trst_and_srst";
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";
633 case RESET_TRST_PULLS_SRST
:
634 modes
[1] = "trst_pulls_srst";
636 case RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
:
637 modes
[1] = "combined";
640 modes
[1] = "separate";
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";
649 modes
[3] = " trst_push_pull";
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";
658 modes
[2] = " srst_gates_jtag";
660 if (new_cfg
& RESET_SRST_PUSH_PULL
)
661 modes
[4] = " srst_push_pull";
663 modes
[4] = " srst_open_drain";
665 if (new_cfg
& RESET_CNCT_UNDER_SRST
)
666 modes
[5] = " connect_assert_srst";
668 modes
[5] = " connect_deassert_srst";
675 command_print(CMD
, "%s %s%s%s%s%s",
677 modes
[2], modes
[3], modes
[4], modes
[5]);
682 COMMAND_HANDLER(handle_adapter_srst_delay_command
)
685 return ERROR_COMMAND_SYNTAX_ERROR
;
688 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
690 jtag_set_nsrst_delay(delay
);
692 command_print(CMD
, "adapter srst delay: %u", jtag_get_nsrst_delay());
696 COMMAND_HANDLER(handle_adapter_srst_pulse_width_command
)
699 return ERROR_COMMAND_SYNTAX_ERROR
;
702 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], width
);
704 jtag_set_nsrst_assert_width(width
);
706 command_print(CMD
, "adapter srst pulse_width: %u", jtag_get_nsrst_assert_width());
710 COMMAND_HANDLER(handle_adapter_speed_command
)
713 return ERROR_COMMAND_SYNTAX_ERROR
;
715 int retval
= ERROR_OK
;
718 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], khz
);
720 retval
= adapter_config_khz(khz
);
721 if (retval
!= ERROR_OK
)
725 int cur_speed
= adapter_get_speed_khz();
726 retval
= adapter_get_speed_readable(&cur_speed
);
727 if (retval
!= ERROR_OK
)
731 command_print(CMD
, "adapter speed: %d kHz", cur_speed
);
733 command_print(CMD
, "adapter speed: RCLK - adaptive");
738 COMMAND_HANDLER(handle_adapter_serial_command
)
741 return ERROR_COMMAND_SYNTAX_ERROR
;
743 free(adapter_config
.serial
);
744 adapter_config
.serial
= strdup(CMD_ARGV
[0]);
748 COMMAND_HANDLER(handle_adapter_reset_de_assert
)
751 VALUE_UNDEFINED
= -1,
756 enum values srst
= VALUE_UNDEFINED
;
757 enum values trst
= VALUE_UNDEFINED
;
758 enum reset_types jtag_reset_config
= jtag_get_reset_config();
762 if (transport_is_jtag()) {
763 if (jtag_reset_config
& RESET_HAS_TRST
)
764 signal
= jtag_get_trst() ? "asserted" : "deasserted";
766 signal
= "not present";
767 command_print(CMD
, "trst %s", signal
);
770 if (jtag_reset_config
& RESET_HAS_SRST
)
771 signal
= jtag_get_srst() ? "asserted" : "deasserted";
773 signal
= "not present";
774 command_print(CMD
, "srst %s", signal
);
779 if (CMD_ARGC
!= 1 && CMD_ARGC
!= 3)
780 return ERROR_COMMAND_SYNTAX_ERROR
;
782 value
= (strcmp(CMD_NAME
, "assert") == 0) ? VALUE_ASSERT
: VALUE_DEASSERT
;
783 if (strcmp(CMD_ARGV
[0], "srst") == 0)
785 else if (strcmp(CMD_ARGV
[0], "trst") == 0)
788 return ERROR_COMMAND_SYNTAX_ERROR
;
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
;
796 return ERROR_COMMAND_SYNTAX_ERROR
;
798 if (strcmp(CMD_ARGV
[2], "srst") == 0 && srst
== VALUE_UNDEFINED
)
800 else if (strcmp(CMD_ARGV
[2], "trst") == 0 && trst
== VALUE_UNDEFINED
)
803 return ERROR_COMMAND_SYNTAX_ERROR
;
806 if (trst
== VALUE_UNDEFINED
) {
807 if (transport_is_jtag())
808 trst
= jtag_get_trst() ? VALUE_ASSERT
: VALUE_DEASSERT
;
810 trst
= VALUE_DEASSERT
; /* unused, safe value */
813 if (srst
== VALUE_UNDEFINED
) {
814 if (jtag_reset_config
& RESET_HAS_SRST
)
815 srst
= jtag_get_srst() ? VALUE_ASSERT
: VALUE_DEASSERT
;
817 srst
= VALUE_DEASSERT
; /* unused, safe value */
820 if (trst
== VALUE_ASSERT
&& !transport_is_jtag()) {
821 LOG_ERROR("transport has no trst signal");
825 if (srst
== VALUE_ASSERT
&& !(jtag_reset_config
& RESET_HAS_SRST
)) {
826 LOG_ERROR("adapter has no srst signal");
830 return adapter_resets((trst
== VALUE_DEASSERT
) ? TRST_DEASSERT
: TRST_ASSERT
,
831 (srst
== VALUE_DEASSERT
) ? SRST_DEASSERT
: SRST_ASSERT
);
834 static int get_gpio_index(const char *signal_name
)
836 for (int i
= 0; i
< ADAPTER_GPIO_IDX_NUM
; ++i
) {
837 if (strcmp(gpio_map
[i
].name
, signal_name
) == 0)
843 static COMMAND_HELPER(helper_adapter_gpio_print_config
, enum adapter_gpio_config_index gpio_idx
)
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
= "";
852 if (gpio_config
->gpio_num
== ADAPTER_GPIO_NOT_SET
) {
853 command_print(CMD
, "adapter gpio %s: not configured", gpio_map
[gpio_idx
].name
);
857 switch (gpio_map
[gpio_idx
].direction
) {
858 case ADAPTER_GPIO_DIRECTION_INPUT
:
861 case ADAPTER_GPIO_DIRECTION_OUTPUT
:
864 case ADAPTER_GPIO_DIRECTION_BIDIRECTIONAL
:
865 dir
= "bidirectional";
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";
874 case ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN
:
875 drive
= ", open-drain";
877 case ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE
:
878 drive
= ", open-source";
883 switch (gpio_config
->pull
) {
884 case ADAPTER_GPIO_PULL_NONE
:
885 pull
= ", pull-none";
887 case ADAPTER_GPIO_PULL_UP
:
890 case ADAPTER_GPIO_PULL_DOWN
:
891 pull
= ", pull-down";
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";
900 case ADAPTER_GPIO_INIT_STATE_ACTIVE
:
901 init_state
= ", init-state active";
903 case ADAPTER_GPIO_INIT_STATE_INPUT
:
904 init_state
= ", init-state input";
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
);
916 COMMAND_HANDLER(helper_adapter_gpio_print_all_configs
)
918 for (int i
= 0; i
< ADAPTER_GPIO_IDX_NUM
; ++i
)
919 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_config
, i
);
923 COMMAND_HANDLER(adapter_gpio_config_handler
)
926 struct adapter_gpio_config
*gpio_config
;
928 adapter_driver_gpios_init();
931 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_all_configs
);
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
;
942 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_config
, gpio_idx
);
946 gpio_config
= &adapter_config
.gpios
[gpio_idx
];
947 while (i
< CMD_ARGC
) {
948 LOG_DEBUG("Processing %s", CMD_ARGV
[i
]);
950 if (isdigit(*CMD_ARGV
[i
])) {
951 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[i
], gpio_config
->gpio_num
);
956 if (strcmp(CMD_ARGV
[i
], "-chip") == 0) {
957 if (CMD_ARGC
- i
< 2) {
958 LOG_ERROR("-chip option requires a parameter");
961 LOG_DEBUG("-chip arg is %s", CMD_ARGV
[i
+ 1]);
962 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[i
+ 1], gpio_config
->chip_num
);
967 if (strcmp(CMD_ARGV
[i
], "-active-high") == 0) {
969 gpio_config
->active_low
= false;
972 if (strcmp(CMD_ARGV
[i
], "-active-low") == 0) {
974 gpio_config
->active_low
= true;
978 if (gpio_map
[gpio_idx
].permit_drive_option
) {
979 if (strcmp(CMD_ARGV
[i
], "-push-pull") == 0) {
981 gpio_config
->drive
= ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL
;
984 if (strcmp(CMD_ARGV
[i
], "-open-drain") == 0) {
986 gpio_config
->drive
= ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN
;
989 if (strcmp(CMD_ARGV
[i
], "-open-source") == 0) {
991 gpio_config
->drive
= ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE
;
996 if (strcmp(CMD_ARGV
[i
], "-pull-none") == 0) {
998 gpio_config
->pull
= ADAPTER_GPIO_PULL_NONE
;
1001 if (strcmp(CMD_ARGV
[i
], "-pull-up") == 0) {
1003 gpio_config
->pull
= ADAPTER_GPIO_PULL_UP
;
1006 if (strcmp(CMD_ARGV
[i
], "-pull-down") == 0) {
1008 gpio_config
->pull
= ADAPTER_GPIO_PULL_DOWN
;
1012 if (gpio_map
[gpio_idx
].permit_init_state_option
) {
1013 if (strcmp(CMD_ARGV
[i
], "-init-inactive") == 0) {
1015 gpio_config
->init_state
= ADAPTER_GPIO_INIT_STATE_INACTIVE
;
1018 if (strcmp(CMD_ARGV
[i
], "-init-active") == 0) {
1020 gpio_config
->init_state
= ADAPTER_GPIO_INIT_STATE_ACTIVE
;
1024 if (gpio_map
[gpio_idx
].direction
== ADAPTER_GPIO_DIRECTION_BIDIRECTIONAL
&&
1025 strcmp(CMD_ARGV
[i
], "-init-input") == 0) {
1027 gpio_config
->init_state
= ADAPTER_GPIO_INIT_STATE_INPUT
;
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
;
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
;
1047 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
1048 COMMAND_HANDLER(handle_usb_location_command
)
1051 adapter_usb_set_location(CMD_ARGV
[0]);
1053 command_print(CMD
, "adapter usb location: %s", adapter_usb_get_location());
1057 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
1059 static const struct command_registration adapter_usb_command_handlers
[] = {
1060 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
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]...]",
1068 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
1069 COMMAND_REGISTRATION_DONE
1072 static const struct command_registration adapter_srst_command_handlers
[] = {
1075 .handler
= handle_adapter_srst_delay_command
,
1076 .mode
= COMMAND_ANY
,
1077 .help
= "delay after deasserting SRST in ms",
1078 .usage
= "[milliseconds]",
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]",
1087 COMMAND_REGISTRATION_DONE
1090 static const struct command_registration adapter_command_handlers
[] = {
1093 .handler
= handle_adapter_driver_command
,
1094 .mode
= COMMAND_CONFIG
,
1095 .help
= "Select a debug adapter driver",
1096 .usage
= "driver_name",
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 "
1105 "With or without argument, display current setting.",
1110 .handler
= handle_adapter_serial_command
,
1111 .mode
= COMMAND_CONFIG
,
1112 .help
= "Set the serial number of the adapter",
1113 .usage
= "serial_string",
1117 .handler
= handle_adapter_list_command
,
1118 .mode
= COMMAND_ANY
,
1119 .help
= "List all built-in debug adapter drivers",
1124 .mode
= COMMAND_ANY
,
1125 .handler
= handle_adapter_name
,
1126 .help
= "Returns the name of the currently "
1127 "selected adapter (driver)",
1132 .mode
= COMMAND_ANY
,
1133 .help
= "srst adapter command group",
1135 .chain
= adapter_srst_command_handlers
,
1138 .name
= "transports",
1139 .handler
= adapter_transports_command
,
1140 .mode
= COMMAND_CONFIG
,
1141 .help
= "Declare transports the adapter supports.",
1142 .usage
= "transport ...",
1146 .mode
= COMMAND_ANY
,
1147 .help
= "usb adapter command group",
1149 .chain
= adapter_usb_command_handlers
,
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]]",
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]]",
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"
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] ]",
1178 COMMAND_REGISTRATION_DONE
1181 static const struct command_registration interface_command_handlers
[] = {
1184 .mode
= COMMAND_ANY
,
1185 .help
= "adapter command group",
1187 .chain
= adapter_command_handlers
,
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]",
1201 COMMAND_REGISTRATION_DONE
1205 * Register the commands which deal with arbitrary debug adapter drivers.
1207 * @todo Remove internal assumptions that all debug adapters use JTAG for
1208 * transport. Various types and data structures are not named generically.
1210 int adapter_register_commands(struct command_context
*ctx
)
1212 return register_commands(ctx
, NULL
, interface_command_handlers
);
1215 const char *adapter_gpio_get_name(enum adapter_gpio_config_index idx
)
1217 return gpio_map
[idx
].name
;
1220 /* Allow drivers access to the GPIO configuration */
1221 const struct adapter_gpio_config
*adapter_gpio_get_config(void)
1223 return adapter_config
.gpios
;