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>
27 * Holds support for configuring debug adapters from TCl scripts.
30 struct adapter_driver
*adapter_driver
;
31 const char * const jtag_only
[] = { "jtag", NULL
};
33 enum adapter_clk_mode
{
34 CLOCK_MODE_UNSELECTED
= 0,
39 #define DEFAULT_CLOCK_SPEED_KHZ 100U
42 * Adapter configuration
45 bool adapter_initialized
;
48 enum adapter_clk_mode clock_mode
;
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 */
55 static const struct gpio_map
{
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, },
73 bool is_adapter_initialized(void)
75 return adapter_config
.adapter_initialized
;
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.
82 static void sync_adapter_reset_with_gpios(void)
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
;
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
;
92 adapter_config
.gpios
[ADAPTER_GPIO_IDX_TRST
].drive
= ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL
;
95 static void adapter_driver_gpios_init(void)
97 if (adapter_config
.gpios_initialized
)
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
;
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();
113 /* JTAG GPIOs should be inactive except for tms */
114 adapter_config
.gpios
[ADAPTER_GPIO_IDX_TMS
].init_state
= ADAPTER_GPIO_INIT_STATE_ACTIVE
;
116 adapter_config
.gpios_initialized
= true;
120 * Do low-level setup like initializing registers, output signals,
123 int adapter_init(struct command_context
*cmd_ctx
)
125 if (is_adapter_initialized())
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
;
135 adapter_driver_gpios_init();
139 if (adapter_config
.clock_mode
== CLOCK_MODE_UNSELECTED
) {
140 LOG_WARNING("An adapter speed is not selected in the init scripts."
141 " OpenOCD will try to run the adapter at the low speed (%d kHz)",
142 DEFAULT_CLOCK_SPEED_KHZ
);
143 LOG_WARNING("To remove this warnings and achieve reasonable communication speed with the target,"
144 " set \"adapter speed\" or \"jtag_rclk\" in the init scripts.");
145 retval
= adapter_config_khz(DEFAULT_CLOCK_SPEED_KHZ
);
146 if (retval
!= ERROR_OK
)
147 return ERROR_JTAG_INIT_FAILED
;
150 retval
= adapter_driver
->init();
151 if (retval
!= ERROR_OK
)
153 adapter_config
.adapter_initialized
= true;
155 if (!adapter_driver
->speed
) {
156 LOG_INFO("This adapter doesn't support configurable speed");
160 int requested_khz
= adapter_get_speed_khz();
161 int actual_khz
= requested_khz
;
163 retval
= adapter_get_speed(&speed_var
);
164 if (retval
!= ERROR_OK
)
166 retval
= adapter_driver
->speed(speed_var
);
167 if (retval
!= ERROR_OK
)
169 retval
= adapter_get_speed_readable(&actual_khz
);
170 if (retval
!= ERROR_OK
)
171 LOG_INFO("adapter-specific clock speed value %d", speed_var
);
172 else if (actual_khz
) {
173 /* Adaptive clocking -- JTAG-specific */
174 if ((adapter_config
.clock_mode
== CLOCK_MODE_RCLK
)
175 || ((adapter_config
.clock_mode
== CLOCK_MODE_KHZ
) && !requested_khz
)) {
176 LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
179 LOG_INFO("clock speed %d kHz", actual_khz
);
181 LOG_INFO("RCLK (adaptive clock speed)");
186 int adapter_quit(void)
188 if (is_adapter_initialized() && adapter_driver
->quit
) {
189 /* close the JTAG interface */
190 int result
= adapter_driver
->quit();
191 if (result
!= ERROR_OK
)
192 LOG_ERROR("failed: %d", result
);
195 free(adapter_config
.serial
);
196 free(adapter_config
.usb_location
);
198 struct jtag_tap
*t
= jtag_all_taps();
200 struct jtag_tap
*n
= t
->next_tap
;
208 unsigned int adapter_get_speed_khz(void)
210 return adapter_config
.speed_khz
;
213 static int adapter_khz_to_speed(unsigned int khz
, int *speed
)
215 LOG_DEBUG("convert khz to adapter specific speed value");
216 adapter_config
.speed_khz
= khz
;
217 if (!is_adapter_initialized())
219 LOG_DEBUG("have adapter set up");
220 if (!adapter_driver
->khz
) {
221 LOG_ERROR("Translation from khz to adapter speed not implemented");
225 int retval
= adapter_driver
->khz(adapter_get_speed_khz(), &speed_div1
);
226 if (retval
!= ERROR_OK
)
232 static int adapter_rclk_to_speed(unsigned int fallback_speed_khz
, int *speed
)
234 int retval
= adapter_khz_to_speed(0, speed
);
235 if ((retval
!= ERROR_OK
) && fallback_speed_khz
) {
236 LOG_DEBUG("trying fallback speed...");
237 retval
= adapter_khz_to_speed(fallback_speed_khz
, speed
);
242 static int adapter_set_speed(int speed
)
244 /* this command can be called during CONFIG,
245 * in which case adapter isn't initialized */
246 return is_adapter_initialized() ? adapter_driver
->speed(speed
) : ERROR_OK
;
249 int adapter_config_khz(unsigned int khz
)
251 LOG_DEBUG("handle adapter khz");
252 adapter_config
.clock_mode
= CLOCK_MODE_KHZ
;
254 int retval
= adapter_khz_to_speed(khz
, &speed
);
255 return (retval
!= ERROR_OK
) ? retval
: adapter_set_speed(speed
);
258 int adapter_config_rclk(unsigned int fallback_speed_khz
)
260 LOG_DEBUG("handle adapter rclk");
261 adapter_config
.clock_mode
= CLOCK_MODE_RCLK
;
262 adapter_config
.rclk_fallback_speed_khz
= fallback_speed_khz
;
264 int retval
= adapter_rclk_to_speed(fallback_speed_khz
, &speed
);
265 return (retval
!= ERROR_OK
) ? retval
: adapter_set_speed(speed
);
268 int adapter_get_speed(int *speed
)
270 switch (adapter_config
.clock_mode
) {
272 adapter_khz_to_speed(adapter_get_speed_khz(), speed
);
274 case CLOCK_MODE_RCLK
:
275 adapter_rclk_to_speed(adapter_config
.rclk_fallback_speed_khz
, speed
);
278 LOG_ERROR("BUG: unknown adapter clock mode");
284 int adapter_get_speed_readable(int *khz
)
287 int retval
= adapter_get_speed(&speed_var
);
288 if (retval
!= ERROR_OK
)
290 if (!is_adapter_initialized())
292 if (!adapter_driver
->speed_div
) {
293 LOG_ERROR("Translation from adapter speed to khz not implemented");
296 return adapter_driver
->speed_div(speed_var
, khz
);
299 const char *adapter_get_required_serial(void)
301 return adapter_config
.serial
;
306 * 2 * 7 chars: max 7 ports
307 * 1 char: test for overflow
311 #define USB_MAX_LOCATION_LENGTH 16
313 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
314 static void adapter_usb_set_location(const char *location
)
316 if (strnlen(location
, USB_MAX_LOCATION_LENGTH
) == USB_MAX_LOCATION_LENGTH
)
317 LOG_WARNING("usb location string is too long!!");
319 free(adapter_config
.usb_location
);
321 adapter_config
.usb_location
= strndup(location
, USB_MAX_LOCATION_LENGTH
);
323 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
325 const char *adapter_usb_get_location(void)
327 return adapter_config
.usb_location
;
330 bool adapter_usb_location_equal(uint8_t dev_bus
, uint8_t *port_path
, size_t path_len
)
332 size_t path_step
, string_length
;
336 if (!adapter_usb_get_location())
339 /* strtok need non const char */
340 loc
= strndup(adapter_usb_get_location(), USB_MAX_LOCATION_LENGTH
);
341 string_length
= strnlen(loc
, USB_MAX_LOCATION_LENGTH
);
343 ptr
= strtok(loc
, "-");
345 LOG_WARNING("no '-' in usb path\n");
349 string_length
-= strnlen(ptr
, string_length
);
350 /* check bus mismatch */
351 if (atoi(ptr
) != dev_bus
)
355 while (path_step
< path_len
) {
356 ptr
= strtok(NULL
, ".");
358 /* no more tokens in path */
362 /* path mismatch at some step */
363 if (path_step
< path_len
&& atoi(ptr
) != port_path
[path_step
])
367 string_length
-= strnlen(ptr
, string_length
) + 1;
370 /* walked the full path, all elements match */
371 if (path_step
== path_len
&& !string_length
)
379 static int jim_adapter_name(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
381 struct jim_getopt_info goi
;
382 jim_getopt_setup(&goi
, interp
, argc
-1, argv
+ 1);
384 /* return the name of the interface */
385 /* TCL code might need to know the exact type... */
386 /* FUTURE: we allow this as a means to "set" the interface. */
388 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
-1, "(no params)");
391 const char *name
= adapter_driver
? adapter_driver
->name
: NULL
;
392 Jim_SetResultString(goi
.interp
, name
? name
: "undefined", -1);
396 COMMAND_HANDLER(adapter_transports_command
)
401 retval
= CALL_COMMAND_HANDLER(transport_list_parse
, &transports
);
402 if (retval
!= ERROR_OK
)
405 retval
= allow_transports(CMD_CTX
, (const char **)transports
);
407 if (retval
!= ERROR_OK
) {
408 for (unsigned i
= 0; transports
[i
]; i
++)
415 COMMAND_HANDLER(handle_adapter_list_command
)
417 if (strcmp(CMD_NAME
, "list") == 0 && CMD_ARGC
> 0)
418 return ERROR_COMMAND_SYNTAX_ERROR
;
420 command_print(CMD
, "The following debug adapters are available:");
421 for (unsigned i
= 0; adapter_drivers
[i
]; i
++) {
422 const char *name
= adapter_drivers
[i
]->name
;
423 command_print(CMD
, "%u: %s", i
+ 1, name
);
429 COMMAND_HANDLER(handle_adapter_driver_command
)
433 /* check whether the interface is already configured */
434 if (adapter_driver
) {
435 LOG_WARNING("Interface already configured, ignoring");
439 /* interface name is a mandatory argument */
440 if (CMD_ARGC
!= 1 || CMD_ARGV
[0][0] == '\0')
441 return ERROR_COMMAND_SYNTAX_ERROR
;
443 for (unsigned i
= 0; adapter_drivers
[i
]; i
++) {
444 if (strcmp(CMD_ARGV
[0], adapter_drivers
[i
]->name
) != 0)
447 if (adapter_drivers
[i
]->commands
) {
448 retval
= register_commands(CMD_CTX
, NULL
, adapter_drivers
[i
]->commands
);
449 if (retval
!= ERROR_OK
)
453 adapter_driver
= adapter_drivers
[i
];
455 return allow_transports(CMD_CTX
, adapter_driver
->transports
);
458 /* no valid interface was found (i.e. the configuration option,
459 * didn't match one of the compiled-in interfaces
461 LOG_ERROR("The specified debug interface was not found (%s)",
463 CALL_COMMAND_HANDLER(handle_adapter_list_command
);
464 return ERROR_JTAG_INVALID_INTERFACE
;
467 COMMAND_HANDLER(handle_reset_config_command
)
472 /* Original versions cared about the order of these tokens:
473 * reset_config signals [combination [trst_type [srst_type]]]
474 * They also clobbered the previous configuration even on error.
476 * Here we don't care about the order, and only change values
477 * which have been explicitly specified.
479 for (; CMD_ARGC
; CMD_ARGC
--, CMD_ARGV
++) {
484 m
= RESET_SRST_NO_GATING
;
485 if (strcmp(*CMD_ARGV
, "srst_gates_jtag") == 0)
486 /* default: don't use JTAG while SRST asserted */;
487 else if (strcmp(*CMD_ARGV
, "srst_nogate") == 0)
488 tmp
= RESET_SRST_NO_GATING
;
492 LOG_ERROR("extra reset_config %s spec (%s)",
493 "gating", *CMD_ARGV
);
494 return ERROR_COMMAND_SYNTAX_ERROR
;
500 m
= RESET_HAS_TRST
| RESET_HAS_SRST
;
501 if (strcmp(*CMD_ARGV
, "none") == 0)
503 else if (strcmp(*CMD_ARGV
, "trst_only") == 0)
504 tmp
= RESET_HAS_TRST
;
505 else if (strcmp(*CMD_ARGV
, "srst_only") == 0)
506 tmp
= RESET_HAS_SRST
;
507 else if (strcmp(*CMD_ARGV
, "trst_and_srst") == 0)
508 tmp
= RESET_HAS_TRST
| RESET_HAS_SRST
;
512 LOG_ERROR("extra reset_config %s spec (%s)",
513 "signal", *CMD_ARGV
);
514 return ERROR_COMMAND_SYNTAX_ERROR
;
519 /* combination (options for broken wiring) */
520 m
= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
521 if (strcmp(*CMD_ARGV
, "separate") == 0)
522 /* separate reset lines - default */;
523 else if (strcmp(*CMD_ARGV
, "srst_pulls_trst") == 0)
524 tmp
|= RESET_SRST_PULLS_TRST
;
525 else if (strcmp(*CMD_ARGV
, "trst_pulls_srst") == 0)
526 tmp
|= RESET_TRST_PULLS_SRST
;
527 else if (strcmp(*CMD_ARGV
, "combined") == 0)
528 tmp
|= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
532 LOG_ERROR("extra reset_config %s spec (%s)",
533 "combination", *CMD_ARGV
);
534 return ERROR_COMMAND_SYNTAX_ERROR
;
539 /* trst_type (NOP without HAS_TRST) */
540 m
= RESET_TRST_OPEN_DRAIN
;
541 if (strcmp(*CMD_ARGV
, "trst_open_drain") == 0)
542 tmp
|= RESET_TRST_OPEN_DRAIN
;
543 else if (strcmp(*CMD_ARGV
, "trst_push_pull") == 0)
544 /* push/pull from adapter - default */;
548 LOG_ERROR("extra reset_config %s spec (%s)",
549 "trst_type", *CMD_ARGV
);
550 return ERROR_COMMAND_SYNTAX_ERROR
;
555 /* srst_type (NOP without HAS_SRST) */
556 m
= RESET_SRST_PUSH_PULL
;
557 if (strcmp(*CMD_ARGV
, "srst_push_pull") == 0)
558 tmp
|= RESET_SRST_PUSH_PULL
;
559 else if (strcmp(*CMD_ARGV
, "srst_open_drain") == 0)
560 /* open drain from adapter - default */;
564 LOG_ERROR("extra reset_config %s spec (%s)",
565 "srst_type", *CMD_ARGV
);
566 return ERROR_COMMAND_SYNTAX_ERROR
;
571 /* connect_type - only valid when srst_nogate */
572 m
= RESET_CNCT_UNDER_SRST
;
573 if (strcmp(*CMD_ARGV
, "connect_assert_srst") == 0)
574 tmp
|= RESET_CNCT_UNDER_SRST
;
575 else if (strcmp(*CMD_ARGV
, "connect_deassert_srst") == 0)
576 /* connect normally - default */;
580 LOG_ERROR("extra reset_config %s spec (%s)",
581 "connect_type", *CMD_ARGV
);
582 return ERROR_COMMAND_SYNTAX_ERROR
;
587 /* caller provided nonsense; fail */
588 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV
);
589 return ERROR_COMMAND_SYNTAX_ERROR
;
592 /* Remember the bits which were specified (mask)
593 * and their new values (new_cfg).
599 /* clear previous values of those bits, save new values */
601 int old_cfg
= jtag_get_reset_config();
605 jtag_set_reset_config(new_cfg
);
606 sync_adapter_reset_with_gpios();
609 new_cfg
= jtag_get_reset_config();
612 * Display the (now-)current reset mode
616 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
617 switch (new_cfg
& (RESET_HAS_TRST
| RESET_HAS_SRST
)) {
619 modes
[0] = "srst_only";
622 modes
[0] = "trst_only";
624 case RESET_TRST_AND_SRST
:
625 modes
[0] = "trst_and_srst";
632 /* normally SRST and TRST are decoupled; but bugs happen ... */
633 switch (new_cfg
& (RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
)) {
634 case RESET_SRST_PULLS_TRST
:
635 modes
[1] = "srst_pulls_trst";
637 case RESET_TRST_PULLS_SRST
:
638 modes
[1] = "trst_pulls_srst";
640 case RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
:
641 modes
[1] = "combined";
644 modes
[1] = "separate";
648 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
649 if (new_cfg
& RESET_HAS_TRST
) {
650 if (new_cfg
& RESET_TRST_OPEN_DRAIN
)
651 modes
[3] = " trst_open_drain";
653 modes
[3] = " trst_push_pull";
657 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
658 if (new_cfg
& RESET_HAS_SRST
) {
659 if (new_cfg
& RESET_SRST_NO_GATING
)
660 modes
[2] = " srst_nogate";
662 modes
[2] = " srst_gates_jtag";
664 if (new_cfg
& RESET_SRST_PUSH_PULL
)
665 modes
[4] = " srst_push_pull";
667 modes
[4] = " srst_open_drain";
669 if (new_cfg
& RESET_CNCT_UNDER_SRST
)
670 modes
[5] = " connect_assert_srst";
672 modes
[5] = " connect_deassert_srst";
679 command_print(CMD
, "%s %s%s%s%s%s",
681 modes
[2], modes
[3], modes
[4], modes
[5]);
686 COMMAND_HANDLER(handle_adapter_srst_delay_command
)
689 return ERROR_COMMAND_SYNTAX_ERROR
;
692 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
694 jtag_set_nsrst_delay(delay
);
696 command_print(CMD
, "adapter srst delay: %u", jtag_get_nsrst_delay());
700 COMMAND_HANDLER(handle_adapter_srst_pulse_width_command
)
703 return ERROR_COMMAND_SYNTAX_ERROR
;
706 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], width
);
708 jtag_set_nsrst_assert_width(width
);
710 command_print(CMD
, "adapter srst pulse_width: %u", jtag_get_nsrst_assert_width());
714 COMMAND_HANDLER(handle_adapter_speed_command
)
717 return ERROR_COMMAND_SYNTAX_ERROR
;
719 int retval
= ERROR_OK
;
722 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], khz
);
724 retval
= adapter_config_khz(khz
);
725 if (retval
!= ERROR_OK
)
729 int cur_speed
= adapter_get_speed_khz();
730 retval
= adapter_get_speed_readable(&cur_speed
);
731 if (retval
!= ERROR_OK
)
735 command_print(CMD
, "adapter speed: %d kHz", cur_speed
);
737 command_print(CMD
, "adapter speed: RCLK - adaptive");
742 COMMAND_HANDLER(handle_adapter_serial_command
)
745 return ERROR_COMMAND_SYNTAX_ERROR
;
747 free(adapter_config
.serial
);
748 adapter_config
.serial
= strdup(CMD_ARGV
[0]);
752 COMMAND_HANDLER(handle_adapter_reset_de_assert
)
755 VALUE_UNDEFINED
= -1,
760 enum values srst
= VALUE_UNDEFINED
;
761 enum values trst
= VALUE_UNDEFINED
;
762 enum reset_types jtag_reset_config
= jtag_get_reset_config();
766 if (transport_is_jtag()) {
767 if (jtag_reset_config
& RESET_HAS_TRST
)
768 signal
= jtag_get_trst() ? "asserted" : "deasserted";
770 signal
= "not present";
771 command_print(CMD
, "trst %s", signal
);
774 if (jtag_reset_config
& RESET_HAS_SRST
)
775 signal
= jtag_get_srst() ? "asserted" : "deasserted";
777 signal
= "not present";
778 command_print(CMD
, "srst %s", signal
);
783 if (CMD_ARGC
!= 1 && CMD_ARGC
!= 3)
784 return ERROR_COMMAND_SYNTAX_ERROR
;
786 value
= (strcmp(CMD_NAME
, "assert") == 0) ? VALUE_ASSERT
: VALUE_DEASSERT
;
787 if (strcmp(CMD_ARGV
[0], "srst") == 0)
789 else if (strcmp(CMD_ARGV
[0], "trst") == 0)
792 return ERROR_COMMAND_SYNTAX_ERROR
;
795 if (strcmp(CMD_ARGV
[1], "assert") == 0)
796 value
= VALUE_ASSERT
;
797 else if (strcmp(CMD_ARGV
[1], "deassert") == 0)
798 value
= VALUE_DEASSERT
;
800 return ERROR_COMMAND_SYNTAX_ERROR
;
802 if (strcmp(CMD_ARGV
[2], "srst") == 0 && srst
== VALUE_UNDEFINED
)
804 else if (strcmp(CMD_ARGV
[2], "trst") == 0 && trst
== VALUE_UNDEFINED
)
807 return ERROR_COMMAND_SYNTAX_ERROR
;
810 if (trst
== VALUE_UNDEFINED
) {
811 if (transport_is_jtag())
812 trst
= jtag_get_trst() ? VALUE_ASSERT
: VALUE_DEASSERT
;
814 trst
= VALUE_DEASSERT
; /* unused, safe value */
817 if (srst
== VALUE_UNDEFINED
) {
818 if (jtag_reset_config
& RESET_HAS_SRST
)
819 srst
= jtag_get_srst() ? VALUE_ASSERT
: VALUE_DEASSERT
;
821 srst
= VALUE_DEASSERT
; /* unused, safe value */
824 if (trst
== VALUE_ASSERT
&& !transport_is_jtag()) {
825 LOG_ERROR("transport has no trst signal");
829 if (srst
== VALUE_ASSERT
&& !(jtag_reset_config
& RESET_HAS_SRST
)) {
830 LOG_ERROR("adapter has no srst signal");
834 return adapter_resets((trst
== VALUE_DEASSERT
) ? TRST_DEASSERT
: TRST_ASSERT
,
835 (srst
== VALUE_DEASSERT
) ? SRST_DEASSERT
: SRST_ASSERT
);
838 static int get_gpio_index(const char *signal_name
)
840 for (int i
= 0; i
< ADAPTER_GPIO_IDX_NUM
; ++i
) {
841 if (strcmp(gpio_map
[i
].name
, signal_name
) == 0)
847 COMMAND_HELPER(helper_adapter_gpio_print_config
, enum adapter_gpio_config_index gpio_idx
)
849 struct adapter_gpio_config
*gpio_config
= &adapter_config
.gpios
[gpio_idx
];
850 const char *active_state
= gpio_config
->active_low
? "low" : "high";
851 const char *dir
= "";
852 const char *drive
= "";
853 const char *pull
= "";
854 const char *init_state
= "";
856 switch (gpio_map
[gpio_idx
].direction
) {
857 case ADAPTER_GPIO_DIRECTION_INPUT
:
860 case ADAPTER_GPIO_DIRECTION_OUTPUT
:
863 case ADAPTER_GPIO_DIRECTION_BIDIRECTIONAL
:
864 dir
= "bidirectional";
868 if (gpio_map
[gpio_idx
].permit_drive_option
) {
869 switch (gpio_config
->drive
) {
870 case ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL
:
871 drive
= ", push-pull";
873 case ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN
:
874 drive
= ", open-drain";
876 case ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE
:
877 drive
= ", open-source";
882 switch (gpio_config
->pull
) {
883 case ADAPTER_GPIO_PULL_NONE
:
884 pull
= ", pull-none";
886 case ADAPTER_GPIO_PULL_UP
:
889 case ADAPTER_GPIO_PULL_DOWN
:
890 pull
= ", pull-down";
894 if (gpio_map
[gpio_idx
].permit_init_state_option
) {
895 switch (gpio_config
->init_state
) {
896 case ADAPTER_GPIO_INIT_STATE_INACTIVE
:
897 init_state
= ", init-state inactive";
899 case ADAPTER_GPIO_INIT_STATE_ACTIVE
:
900 init_state
= ", init-state active";
902 case ADAPTER_GPIO_INIT_STATE_INPUT
:
903 init_state
= ", init-state input";
908 command_print(CMD
, "adapter gpio %s (%s): num %d, chip %d, active-%s%s%s%s",
909 gpio_map
[gpio_idx
].name
, dir
, gpio_config
->gpio_num
, gpio_config
->chip_num
, active_state
,
910 drive
, pull
, init_state
);
915 COMMAND_HANDLER(helper_adapter_gpio_print_all_configs
)
917 for (int i
= 0; i
< ADAPTER_GPIO_IDX_NUM
; ++i
)
918 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_config
, i
);
922 COMMAND_HANDLER(adapter_gpio_config_handler
)
925 struct adapter_gpio_config
*gpio_config
;
927 adapter_driver_gpios_init();
930 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_all_configs
);
934 int gpio_idx
= get_gpio_index(CMD_ARGV
[0]);
935 if (gpio_idx
== -1) {
936 LOG_ERROR("adapter has no gpio named %s", CMD_ARGV
[0]);
937 return ERROR_COMMAND_SYNTAX_ERROR
;
941 CALL_COMMAND_HANDLER(helper_adapter_gpio_print_config
, gpio_idx
);
945 gpio_config
= &adapter_config
.gpios
[gpio_idx
];
946 while (i
< CMD_ARGC
) {
947 LOG_DEBUG("Processing %s", CMD_ARGV
[i
]);
949 if (isdigit(*CMD_ARGV
[i
])) {
950 int gpio_num
; /* Use a meaningful output parameter for more helpful error messages */
951 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[i
], gpio_num
);
952 gpio_config
->gpio_num
= gpio_num
;
957 if (strcmp(CMD_ARGV
[i
], "-chip") == 0) {
958 if (CMD_ARGC
- i
< 2) {
959 LOG_ERROR("-chip option requires a parameter");
962 LOG_DEBUG("-chip arg is %s", CMD_ARGV
[i
+ 1]);
963 int chip_num
; /* Use a meaningful output parameter for more helpful error messages */
964 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[i
+ 1], chip_num
);
965 gpio_config
->chip_num
= chip_num
;
970 if (strcmp(CMD_ARGV
[i
], "-active-high") == 0) {
972 gpio_config
->active_low
= false;
975 if (strcmp(CMD_ARGV
[i
], "-active-low") == 0) {
977 gpio_config
->active_low
= true;
981 if (gpio_map
[gpio_idx
].permit_drive_option
) {
982 if (strcmp(CMD_ARGV
[i
], "-push-pull") == 0) {
984 gpio_config
->drive
= ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL
;
987 if (strcmp(CMD_ARGV
[i
], "-open-drain") == 0) {
989 gpio_config
->drive
= ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN
;
992 if (strcmp(CMD_ARGV
[i
], "-open-source") == 0) {
994 gpio_config
->drive
= ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE
;
999 if (strcmp(CMD_ARGV
[i
], "-pull-none") == 0) {
1001 gpio_config
->pull
= ADAPTER_GPIO_PULL_NONE
;
1004 if (strcmp(CMD_ARGV
[i
], "-pull-up") == 0) {
1006 gpio_config
->pull
= ADAPTER_GPIO_PULL_UP
;
1009 if (strcmp(CMD_ARGV
[i
], "-pull-down") == 0) {
1011 gpio_config
->pull
= ADAPTER_GPIO_PULL_DOWN
;
1015 if (gpio_map
[gpio_idx
].permit_init_state_option
) {
1016 if (strcmp(CMD_ARGV
[i
], "-init-inactive") == 0) {
1018 gpio_config
->init_state
= ADAPTER_GPIO_INIT_STATE_INACTIVE
;
1021 if (strcmp(CMD_ARGV
[i
], "-init-active") == 0) {
1023 gpio_config
->init_state
= ADAPTER_GPIO_INIT_STATE_ACTIVE
;
1027 if (gpio_map
[gpio_idx
].direction
== ADAPTER_GPIO_DIRECTION_BIDIRECTIONAL
&&
1028 strcmp(CMD_ARGV
[i
], "-init-input") == 0) {
1030 gpio_config
->init_state
= ADAPTER_GPIO_INIT_STATE_INPUT
;
1035 LOG_ERROR("illegal option for adapter %s %s: %s",
1036 CMD_NAME
, gpio_map
[gpio_idx
].name
, CMD_ARGV
[i
]);
1037 return ERROR_COMMAND_SYNTAX_ERROR
;
1040 /* Force swdio_dir init state to be compatible with swdio init state */
1041 if (gpio_idx
== ADAPTER_GPIO_IDX_SWDIO
)
1042 adapter_config
.gpios
[ADAPTER_GPIO_IDX_SWDIO_DIR
].init_state
=
1043 (gpio_config
->init_state
== ADAPTER_GPIO_INIT_STATE_INPUT
) ?
1044 ADAPTER_GPIO_INIT_STATE_INACTIVE
:
1045 ADAPTER_GPIO_INIT_STATE_ACTIVE
;
1050 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
1051 COMMAND_HANDLER(handle_usb_location_command
)
1054 adapter_usb_set_location(CMD_ARGV
[0]);
1056 command_print(CMD
, "adapter usb location: %s", adapter_usb_get_location());
1060 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
1062 static const struct command_registration adapter_usb_command_handlers
[] = {
1063 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
1066 .handler
= &handle_usb_location_command
,
1067 .mode
= COMMAND_CONFIG
,
1068 .help
= "display or set the USB bus location of the USB device",
1069 .usage
= "[<bus>-port[.port]...]",
1071 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
1072 COMMAND_REGISTRATION_DONE
1075 static const struct command_registration adapter_srst_command_handlers
[] = {
1078 .handler
= handle_adapter_srst_delay_command
,
1079 .mode
= COMMAND_ANY
,
1080 .help
= "delay after deasserting SRST in ms",
1081 .usage
= "[milliseconds]",
1084 .name
= "pulse_width",
1085 .handler
= handle_adapter_srst_pulse_width_command
,
1086 .mode
= COMMAND_ANY
,
1087 .help
= "SRST assertion pulse width in ms",
1088 .usage
= "[milliseconds]",
1090 COMMAND_REGISTRATION_DONE
1093 static const struct command_registration adapter_command_handlers
[] = {
1096 .handler
= handle_adapter_driver_command
,
1097 .mode
= COMMAND_CONFIG
,
1098 .help
= "Select a debug adapter driver",
1099 .usage
= "driver_name",
1103 .handler
= handle_adapter_speed_command
,
1104 .mode
= COMMAND_ANY
,
1105 .help
= "With an argument, change to the specified maximum "
1106 "jtag speed. For JTAG, 0 KHz signifies adaptive "
1108 "With or without argument, display current setting.",
1113 .handler
= handle_adapter_serial_command
,
1114 .mode
= COMMAND_CONFIG
,
1115 .help
= "Set the serial number of the adapter",
1116 .usage
= "serial_string",
1120 .handler
= handle_adapter_list_command
,
1121 .mode
= COMMAND_ANY
,
1122 .help
= "List all built-in debug adapter drivers",
1127 .mode
= COMMAND_ANY
,
1128 .jim_handler
= jim_adapter_name
,
1129 .help
= "Returns the name of the currently "
1130 "selected adapter (driver)",
1134 .mode
= COMMAND_ANY
,
1135 .help
= "srst adapter command group",
1137 .chain
= adapter_srst_command_handlers
,
1140 .name
= "transports",
1141 .handler
= adapter_transports_command
,
1142 .mode
= COMMAND_CONFIG
,
1143 .help
= "Declare transports the adapter supports.",
1144 .usage
= "transport ...",
1148 .mode
= COMMAND_ANY
,
1149 .help
= "usb adapter command group",
1151 .chain
= adapter_usb_command_handlers
,
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]]",
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]]",
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"
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] ]",
1180 COMMAND_REGISTRATION_DONE
1183 static const struct command_registration interface_command_handlers
[] = {
1186 .mode
= COMMAND_ANY
,
1187 .help
= "adapter command group",
1189 .chain
= adapter_command_handlers
,
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]",
1203 COMMAND_REGISTRATION_DONE
1207 * Register the commands which deal with arbitrary debug adapter drivers.
1209 * @todo Remove internal assumptions that all debug adapters use JTAG for
1210 * transport. Various types and data structures are not named generically.
1212 int adapter_register_commands(struct command_context
*ctx
)
1214 return register_commands(ctx
, NULL
, interface_command_handlers
);
1217 const char *adapter_gpio_get_name(enum adapter_gpio_config_index idx
)
1219 return gpio_map
[idx
].name
;
1222 /* Allow drivers access to the GPIO configuration */
1223 const struct adapter_gpio_config
*adapter_gpio_get_config(void)
1225 return adapter_config
.gpios
;