1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Bitbang driver for Linux GPIO descriptors through libgpiod
4 * Copyright (C) 2020 Antonio Borneo <borneo.antonio@gmail.com>
6 * Largely based on sysfsgpio driver
7 * Copyright (C) 2012 by Creative Product Design, marc @ cpdesign.com.au
8 * Copyright (C) 2014 by Jean-Christian de Rivaz <jc@eclis.ch>
9 * Copyright (C) 2014 by Paul Fertser <fercerpav@gmail.com>
17 #include <jtag/adapter.h>
18 #include <jtag/interface.h>
19 #include <transport/transport.h>
22 static struct gpiod_chip
*gpiod_chip
[ADAPTER_GPIO_IDX_NUM
] = {};
23 static struct gpiod_line
*gpiod_line
[ADAPTER_GPIO_IDX_NUM
] = {};
25 static int last_swclk
;
26 static int last_swdio
;
27 static bool last_stored
;
28 static bool swdio_input
;
30 static const struct adapter_gpio_config
*adapter_gpio_config
;
33 * Helper function to determine if gpio config is valid
35 * Assume here that there will be less than 10000 gpios per gpiochip, and less
36 * than 1000 gpiochips.
38 static bool is_gpio_config_valid(enum adapter_gpio_config_index idx
)
40 return adapter_gpio_config
[idx
].chip_num
>= 0
41 && adapter_gpio_config
[idx
].chip_num
< 1000
42 && adapter_gpio_config
[idx
].gpio_num
>= 0
43 && adapter_gpio_config
[idx
].gpio_num
< 10000;
46 /* Bitbang interface read of TDO */
47 static bb_value_t
linuxgpiod_read(void)
51 retval
= gpiod_line_get_value(gpiod_line
[ADAPTER_GPIO_IDX_TDO
]);
53 LOG_WARNING("reading tdo failed");
57 return retval
? BB_HIGH
: BB_LOW
;
61 * Bitbang interface write of TCK, TMS, TDI
63 * Seeing as this is the only function where the outputs are changed,
64 * we can cache the old value to avoid needlessly writing it.
66 static int linuxgpiod_write(int tck
, int tms
, int tdi
)
72 static int first_time
;
83 if (tdi
!= last_tdi
) {
84 retval
= gpiod_line_set_value(gpiod_line
[ADAPTER_GPIO_IDX_TDI
], tdi
);
86 LOG_WARNING("writing tdi failed");
89 if (tms
!= last_tms
) {
90 retval
= gpiod_line_set_value(gpiod_line
[ADAPTER_GPIO_IDX_TMS
], tms
);
92 LOG_WARNING("writing tms failed");
96 if (tck
!= last_tck
) {
97 retval
= gpiod_line_set_value(gpiod_line
[ADAPTER_GPIO_IDX_TCK
], tck
);
99 LOG_WARNING("writing tck failed");
109 static int linuxgpiod_swdio_read(void)
113 retval
= gpiod_line_get_value(gpiod_line
[ADAPTER_GPIO_IDX_SWDIO
]);
115 LOG_WARNING("Fail read swdio");
122 static void linuxgpiod_swdio_drive(bool is_output
)
127 * FIXME: change direction requires release and re-require the line
128 * https://stackoverflow.com/questions/58735140/
129 * this would change in future libgpiod
131 gpiod_line_release(gpiod_line
[ADAPTER_GPIO_IDX_SWDIO
]);
134 if (gpiod_line
[ADAPTER_GPIO_IDX_SWDIO_DIR
]) {
135 retval
= gpiod_line_set_value(gpiod_line
[ADAPTER_GPIO_IDX_SWDIO_DIR
], 1);
137 LOG_WARNING("Fail set swdio_dir");
139 retval
= gpiod_line_request_output(gpiod_line
[ADAPTER_GPIO_IDX_SWDIO
], "OpenOCD", 1);
141 LOG_WARNING("Fail request_output line swdio");
143 retval
= gpiod_line_request_input(gpiod_line
[ADAPTER_GPIO_IDX_SWDIO
], "OpenOCD");
145 LOG_WARNING("Fail request_input line swdio");
146 if (gpiod_line
[ADAPTER_GPIO_IDX_SWDIO_DIR
]) {
147 retval
= gpiod_line_set_value(gpiod_line
[ADAPTER_GPIO_IDX_SWDIO_DIR
], 0);
149 LOG_WARNING("Fail set swdio_dir");
154 swdio_input
= !is_output
;
157 static int linuxgpiod_swd_write(int swclk
, int swdio
)
162 if (!last_stored
|| (swdio
!= last_swdio
)) {
163 retval
= gpiod_line_set_value(gpiod_line
[ADAPTER_GPIO_IDX_SWDIO
], swdio
);
165 LOG_WARNING("Fail set swdio");
169 /* write swclk last */
170 if (!last_stored
|| (swclk
!= last_swclk
)) {
171 retval
= gpiod_line_set_value(gpiod_line
[ADAPTER_GPIO_IDX_SWCLK
], swclk
);
173 LOG_WARNING("Fail set swclk");
183 static int linuxgpiod_blink(int on
)
187 if (!is_gpio_config_valid(ADAPTER_GPIO_IDX_LED
))
190 retval
= gpiod_line_set_value(gpiod_line
[ADAPTER_GPIO_IDX_LED
], on
);
192 LOG_WARNING("Fail set led");
196 static struct bitbang_interface linuxgpiod_bitbang
= {
197 .read
= linuxgpiod_read
,
198 .write
= linuxgpiod_write
,
199 .swdio_read
= linuxgpiod_swdio_read
,
200 .swdio_drive
= linuxgpiod_swdio_drive
,
201 .swd_write
= linuxgpiod_swd_write
,
202 .blink
= linuxgpiod_blink
,
206 * Bitbang interface to manipulate reset lines SRST and TRST
208 * (1) assert or (0) deassert reset lines
210 static int linuxgpiod_reset(int trst
, int srst
)
212 int retval1
= 0, retval2
= 0;
214 LOG_DEBUG("linuxgpiod_reset");
217 * active low behaviour handled by "adaptor gpio" command and
218 * GPIOD_LINE_REQUEST_FLAG_ACTIVE_LOW flag when requesting the line.
220 if (gpiod_line
[ADAPTER_GPIO_IDX_SRST
]) {
221 retval1
= gpiod_line_set_value(gpiod_line
[ADAPTER_GPIO_IDX_SRST
], srst
);
223 LOG_WARNING("set srst value failed");
226 if (gpiod_line
[ADAPTER_GPIO_IDX_TRST
]) {
227 retval2
= gpiod_line_set_value(gpiod_line
[ADAPTER_GPIO_IDX_TRST
], trst
);
229 LOG_WARNING("set trst value failed");
232 return ((retval1
< 0) || (retval2
< 0)) ? ERROR_FAIL
: ERROR_OK
;
235 static bool linuxgpiod_jtag_mode_possible(void)
237 if (!is_gpio_config_valid(ADAPTER_GPIO_IDX_TCK
))
239 if (!is_gpio_config_valid(ADAPTER_GPIO_IDX_TMS
))
241 if (!is_gpio_config_valid(ADAPTER_GPIO_IDX_TDI
))
243 if (!is_gpio_config_valid(ADAPTER_GPIO_IDX_TDO
))
248 static bool linuxgpiod_swd_mode_possible(void)
250 if (!is_gpio_config_valid(ADAPTER_GPIO_IDX_SWCLK
))
252 if (!is_gpio_config_valid(ADAPTER_GPIO_IDX_SWDIO
))
257 static inline void helper_release(enum adapter_gpio_config_index idx
)
259 if (gpiod_line
[idx
]) {
260 gpiod_line_release(gpiod_line
[idx
]);
261 gpiod_line
[idx
] = NULL
;
263 if (gpiod_chip
[idx
]) {
264 gpiod_chip_close(gpiod_chip
[idx
]);
265 gpiod_chip
[idx
] = NULL
;
269 static int linuxgpiod_quit(void)
271 LOG_DEBUG("linuxgpiod_quit");
272 for (int i
= 0; i
< ADAPTER_GPIO_IDX_NUM
; ++i
)
278 static int helper_get_line(enum adapter_gpio_config_index idx
)
280 if (!is_gpio_config_valid(idx
))
283 int dir
= GPIOD_LINE_REQUEST_DIRECTION_INPUT
, flags
= 0, val
= 0, retval
;
285 gpiod_chip
[idx
] = gpiod_chip_open_by_number(adapter_gpio_config
[idx
].chip_num
);
286 if (!gpiod_chip
[idx
]) {
287 LOG_ERROR("Cannot open LinuxGPIOD chip %d for %s", adapter_gpio_config
[idx
].chip_num
,
288 adapter_gpio_get_name(idx
));
289 return ERROR_JTAG_INIT_FAILED
;
292 gpiod_line
[idx
] = gpiod_chip_get_line(gpiod_chip
[idx
], adapter_gpio_config
[idx
].gpio_num
);
293 if (!gpiod_line
[idx
]) {
294 LOG_ERROR("Error get line %s", adapter_gpio_get_name(idx
));
295 return ERROR_JTAG_INIT_FAILED
;
298 switch (adapter_gpio_config
[idx
].init_state
) {
299 case ADAPTER_GPIO_INIT_STATE_INPUT
:
300 dir
= GPIOD_LINE_REQUEST_DIRECTION_INPUT
;
302 case ADAPTER_GPIO_INIT_STATE_INACTIVE
:
303 dir
= GPIOD_LINE_REQUEST_DIRECTION_OUTPUT
;
306 case ADAPTER_GPIO_INIT_STATE_ACTIVE
:
307 dir
= GPIOD_LINE_REQUEST_DIRECTION_OUTPUT
;
312 switch (adapter_gpio_config
[idx
].drive
) {
313 case ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL
:
315 case ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN
:
316 flags
|= GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN
;
318 case ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE
:
319 flags
|= GPIOD_LINE_REQUEST_FLAG_OPEN_SOURCE
;
323 switch (adapter_gpio_config
[idx
].pull
) {
324 case ADAPTER_GPIO_PULL_NONE
:
325 #ifdef GPIOD_LINE_REQUEST_FLAG_BIAS_DISABLE
326 flags
|= GPIOD_LINE_REQUEST_FLAG_BIAS_DISABLE
;
329 case ADAPTER_GPIO_PULL_UP
:
330 #ifdef GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP
331 flags
|= GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP
;
333 LOG_WARNING("linuxgpiod: ignoring request for pull-up on %s: not supported by gpiod v%s",
334 adapter_gpio_get_name(idx
), gpiod_version_string());
337 case ADAPTER_GPIO_PULL_DOWN
:
338 #ifdef GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_DOWN
339 flags
|= GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_DOWN
;
341 LOG_WARNING("linuxgpiod: ignoring request for pull-down on %s: not supported by gpiod v%s",
342 adapter_gpio_get_name(idx
), gpiod_version_string());
347 if (adapter_gpio_config
[idx
].active_low
)
348 flags
|= GPIOD_LINE_REQUEST_FLAG_ACTIVE_LOW
;
350 struct gpiod_line_request_config config
= {
351 .consumer
= "OpenOCD",
356 retval
= gpiod_line_request(gpiod_line
[idx
], &config
, val
);
358 LOG_ERROR("Error requesting gpio line %s", adapter_gpio_get_name(idx
));
359 return ERROR_JTAG_INIT_FAILED
;
365 static int linuxgpiod_init(void)
367 LOG_INFO("Linux GPIOD JTAG/SWD bitbang driver");
369 bitbang_interface
= &linuxgpiod_bitbang
;
370 adapter_gpio_config
= adapter_gpio_get_config();
373 * Configure JTAG/SWD signals. Default directions and initial states are handled
374 * by adapter.c and "adapter gpio" command.
377 if (transport_is_jtag()) {
378 if (!linuxgpiod_jtag_mode_possible()) {
379 LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode");
383 if (helper_get_line(ADAPTER_GPIO_IDX_TDO
) != ERROR_OK
||
384 helper_get_line(ADAPTER_GPIO_IDX_TDI
) != ERROR_OK
||
385 helper_get_line(ADAPTER_GPIO_IDX_TCK
) != ERROR_OK
||
386 helper_get_line(ADAPTER_GPIO_IDX_TMS
) != ERROR_OK
||
387 helper_get_line(ADAPTER_GPIO_IDX_TRST
) != ERROR_OK
)
391 if (transport_is_swd()) {
392 int retval1
, retval2
;
393 if (!linuxgpiod_swd_mode_possible()) {
394 LOG_ERROR("Require swclk and swdio gpio for SWD mode");
399 * swdio and its buffer should be initialized in the order that prevents
400 * two outputs from being connected together. This will occur if the
401 * swdio GPIO is configured as an output while the external buffer is
402 * configured to send the swdio signal from the target to the GPIO.
404 if (adapter_gpio_config
[ADAPTER_GPIO_IDX_SWDIO
].init_state
== ADAPTER_GPIO_INIT_STATE_INPUT
) {
405 retval1
= helper_get_line(ADAPTER_GPIO_IDX_SWDIO
);
406 retval2
= helper_get_line(ADAPTER_GPIO_IDX_SWDIO_DIR
);
408 retval1
= helper_get_line(ADAPTER_GPIO_IDX_SWDIO_DIR
);
409 retval2
= helper_get_line(ADAPTER_GPIO_IDX_SWDIO
);
411 if (retval1
!= ERROR_OK
|| retval2
!= ERROR_OK
)
414 if (helper_get_line(ADAPTER_GPIO_IDX_SWCLK
) != ERROR_OK
)
418 if (helper_get_line(ADAPTER_GPIO_IDX_SRST
) != ERROR_OK
||
419 helper_get_line(ADAPTER_GPIO_IDX_LED
) != ERROR_OK
)
427 return ERROR_JTAG_INIT_FAILED
;
430 static const char *const linuxgpiod_transport
[] = { "swd", "jtag", NULL
};
432 static struct jtag_interface linuxgpiod_interface
= {
433 .supported
= DEBUG_CAP_TMS_SEQ
,
434 .execute_queue
= bitbang_execute_queue
,
437 struct adapter_driver linuxgpiod_adapter_driver
= {
438 .name
= "linuxgpiod",
439 .transports
= linuxgpiod_transport
,
441 .init
= linuxgpiod_init
,
442 .quit
= linuxgpiod_quit
,
443 .reset
= linuxgpiod_reset
,
445 .jtag_ops
= &linuxgpiod_interface
,
446 .swd_ops
= &bitbang_swd
,
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)