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/interface.h>
18 #include <transport/transport.h>
21 /* gpio numbers for each gpio. Negative values are invalid */
22 static int tck_gpio = -1;
23 static int tms_gpio = -1;
24 static int tdi_gpio = -1;
25 static int tdo_gpio = -1;
26 static int trst_gpio = -1;
27 static int srst_gpio = -1;
28 static int swclk_gpio = -1;
29 static int swdio_gpio = -1;
30 static int gpiochip = -1;
32 static struct gpiod_chip *gpiod_chip;
33 static struct gpiod_line *gpiod_tck;
34 static struct gpiod_line *gpiod_tms;
35 static struct gpiod_line *gpiod_tdi;
36 static struct gpiod_line *gpiod_tdo;
37 static struct gpiod_line *gpiod_trst;
38 static struct gpiod_line *gpiod_swclk;
39 static struct gpiod_line *gpiod_swdio;
40 static struct gpiod_line *gpiod_srst;
42 static int last_swclk;
43 static int last_swdio;
44 static bool last_stored;
45 static bool swdio_input;
47 /* Bitbang interface read of TDO */
48 static bb_value_t linuxgpiod_read(void)
52 retval = gpiod_line_get_value(gpiod_tdo);
54 LOG_WARNING("reading tdo failed");
58 return retval ? BB_HIGH : BB_LOW;
62 * Bitbang interface write of TCK, TMS, TDI
64 * Seeing as this is the only function where the outputs are changed,
65 * we can cache the old value to avoid needlessly writing it.
67 static int linuxgpiod_write(int tck, int tms, int tdi)
73 static int first_time;
84 if (tdi != last_tdi) {
85 retval = gpiod_line_set_value(gpiod_tdi, tdi);
87 LOG_WARNING("writing tdi failed");
90 if (tms != last_tms) {
91 retval = gpiod_line_set_value(gpiod_tms, tms);
93 LOG_WARNING("writing tms failed");
97 if (tck != last_tck) {
98 retval = gpiod_line_set_value(gpiod_tck, tck);
100 LOG_WARNING("writing tck failed");
110 static int linuxgpiod_swdio_read(void)
114 retval = gpiod_line_get_value(gpiod_swdio);
116 LOG_WARNING("Fail read swdio");
123 static void linuxgpiod_swdio_drive(bool is_output)
128 * FIXME: change direction requires release and re-require the line
129 * https://stackoverflow.com/questions/58735140/
130 * this would change in future libgpiod
132 gpiod_line_release(gpiod_swdio);
135 retval = gpiod_line_request_output(gpiod_swdio, "OpenOCD", 1);
137 LOG_WARNING("Fail request_output line swdio");
139 retval = gpiod_line_request_input(gpiod_swdio, "OpenOCD");
141 LOG_WARNING("Fail request_input line swdio");
145 swdio_input = !is_output;
148 static int linuxgpiod_swd_write(int swclk, int swdio)
153 if (!last_stored || (swdio != last_swdio)) {
154 retval = gpiod_line_set_value(gpiod_swdio, swdio);
156 LOG_WARNING("Fail set swdio");
160 /* write swclk last */
161 if (!last_stored || (swclk != last_swclk)) {
162 retval = gpiod_line_set_value(gpiod_swclk, swclk);
164 LOG_WARNING("Fail set swclk");
174 static struct bitbang_interface linuxgpiod_bitbang = {
175 .read = linuxgpiod_read,
176 .write = linuxgpiod_write,
177 .swdio_read = linuxgpiod_swdio_read,
178 .swdio_drive = linuxgpiod_swdio_drive,
179 .swd_write = linuxgpiod_swd_write,
184 * Bitbang interface to manipulate reset lines SRST and TRST
186 * (1) assert or (0) deassert reset lines
188 static int linuxgpiod_reset(int trst, int srst)
190 int retval1 = 0, retval2 = 0;
192 LOG_DEBUG("linuxgpiod_reset");
194 /* assume active low */
195 if (gpiod_srst != NULL) {
196 retval1 = gpiod_line_set_value(gpiod_srst, srst ? 0 : 1);
198 LOG_WARNING("set srst value failed");
201 /* assume active low */
202 if (gpiod_trst != NULL) {
203 retval2 = gpiod_line_set_value(gpiod_trst, trst ? 0 : 1);
205 LOG_WARNING("set trst value failed");
208 return ((retval1 < 0) || (retval2 < 0)) ? ERROR_FAIL : ERROR_OK;
212 * Helper function to determine if gpio number is valid
214 * Assume here that there will be less than 10000 gpios per gpiochip
216 static bool is_gpio_valid(int gpio)
218 return gpio >= 0 && gpio < 10000;
221 static bool linuxgpiod_jtag_mode_possible(void)
223 if (!is_gpio_valid(tck_gpio))
225 if (!is_gpio_valid(tms_gpio))
227 if (!is_gpio_valid(tdi_gpio))
229 if (!is_gpio_valid(tdo_gpio))
234 static bool linuxgpiod_swd_mode_possible(void)
236 if (!is_gpio_valid(swclk_gpio))
238 if (!is_gpio_valid(swdio_gpio))
243 static inline void helper_release(struct gpiod_line *line)
246 gpiod_line_release(line);
249 static int linuxgpiod_quit(void)
251 helper_release(gpiod_srst);
252 helper_release(gpiod_swdio);
253 helper_release(gpiod_swclk);
254 helper_release(gpiod_trst);
255 helper_release(gpiod_tms);
256 helper_release(gpiod_tck);
257 helper_release(gpiod_tdi);
258 helper_release(gpiod_tdo);
260 gpiod_chip_close(gpiod_chip);
265 static struct gpiod_line *helper_get_input_line(const char *label, unsigned int offset)
267 struct gpiod_line *line;
270 line = gpiod_chip_get_line(gpiod_chip, offset);
272 LOG_ERROR("Error get line %s", label);
276 retval = gpiod_line_request_input(line, "OpenOCD");
278 LOG_ERROR("Error request_input line %s", label);
285 static struct gpiod_line *helper_get_output_line(const char *label, unsigned int offset, int val)
287 struct gpiod_line *line;
290 line = gpiod_chip_get_line(gpiod_chip, offset);
292 LOG_ERROR("Error get line %s", label);
296 retval = gpiod_line_request_output(line, "OpenOCD", val);
298 LOG_ERROR("Error request_output line %s", label);
305 static int linuxgpiod_init(void)
307 LOG_INFO("Linux GPIOD JTAG/SWD bitbang driver");
309 bitbang_interface = &linuxgpiod_bitbang;
311 gpiod_chip = gpiod_chip_open_by_number(gpiochip);
312 if (gpiod_chip == NULL) {
313 LOG_ERROR("Cannot open LinuxGPIOD gpiochip %d", gpiochip);
314 return ERROR_JTAG_INIT_FAILED;
318 * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
319 * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
320 * For SWD, SWCLK and SWDIO are configures as output high.
323 if (transport_is_jtag()) {
324 if (!linuxgpiod_jtag_mode_possible()) {
325 LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode");
329 gpiod_tdo = helper_get_input_line("tdo", tdo_gpio);
330 if (gpiod_tdo == NULL)
333 gpiod_tdi = helper_get_output_line("tdi", tdi_gpio, 0);
334 if (gpiod_tdi == NULL)
337 gpiod_tck = helper_get_output_line("tck", tck_gpio, 0);
338 if (gpiod_tck == NULL)
341 gpiod_tms = helper_get_output_line("tms", tms_gpio, 1);
342 if (gpiod_tms == NULL)
345 if (is_gpio_valid(trst_gpio)) {
346 gpiod_trst = helper_get_output_line("trst", trst_gpio, 1);
347 if (gpiod_trst == NULL)
352 if (transport_is_swd()) {
353 if (!linuxgpiod_swd_mode_possible()) {
354 LOG_ERROR("Require swclk and swdio gpio for SWD mode");
358 gpiod_swclk = helper_get_output_line("swclk", swclk_gpio, 1);
359 if (gpiod_swclk == NULL)
362 gpiod_swdio = helper_get_output_line("swdio", swdio_gpio, 1);
363 if (gpiod_swdio == NULL)
367 if (is_gpio_valid(srst_gpio)) {
368 gpiod_srst = helper_get_output_line("srst", srst_gpio, 1);
369 if (gpiod_srst == NULL)
378 return ERROR_JTAG_INIT_FAILED;
381 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionums)
384 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
385 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpio);
386 COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], tdi_gpio);
387 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], tdo_gpio);
388 } else if (CMD_ARGC != 0) {
389 return ERROR_COMMAND_SYNTAX_ERROR;
393 "LinuxGPIOD nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
394 tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
399 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tck)
402 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
404 command_print(CMD, "LinuxGPIOD num: tck = %d", tck_gpio);
408 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tms)
411 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
413 command_print(CMD, "LinuxGPIOD num: tms = %d", tms_gpio);
417 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdo)
420 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
422 command_print(CMD, "LinuxGPIOD num: tdo = %d", tdo_gpio);
426 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdi)
429 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
431 command_print(CMD, "LinuxGPIOD num: tdi = %d", tdi_gpio);
435 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_srst)
438 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
440 command_print(CMD, "LinuxGPIOD num: srst = %d", srst_gpio);
444 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_trst)
447 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
449 command_print(CMD, "LinuxGPIOD num: trst = %d", trst_gpio);
453 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionums)
456 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
457 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], swdio_gpio);
458 } else if (CMD_ARGC != 0) {
459 return ERROR_COMMAND_SYNTAX_ERROR;
463 "LinuxGPIOD nums: swclk = %d, swdio = %d",
464 swclk_gpio, swdio_gpio);
469 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swclk)
472 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
474 command_print(CMD, "LinuxGPIOD num: swclk = %d", swclk_gpio);
478 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swdio)
481 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpio);
483 command_print(CMD, "LinuxGPIOD num: swdio = %d", swdio_gpio);
487 COMMAND_HANDLER(linuxgpiod_handle_gpiochip)
490 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], gpiochip);
492 command_print(CMD, "LinuxGPIOD gpiochip = %d", gpiochip);
496 static const struct command_registration linuxgpiod_command_handlers[] = {
498 .name = "linuxgpiod_jtag_nums",
499 .handler = linuxgpiod_handle_jtag_gpionums,
500 .mode = COMMAND_CONFIG,
501 .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
502 .usage = "tck tms tdi tdo",
505 .name = "linuxgpiod_tck_num",
506 .handler = linuxgpiod_handle_jtag_gpionum_tck,
507 .mode = COMMAND_CONFIG,
508 .help = "gpio number for tck.",
512 .name = "linuxgpiod_tms_num",
513 .handler = linuxgpiod_handle_jtag_gpionum_tms,
514 .mode = COMMAND_CONFIG,
515 .help = "gpio number for tms.",
519 .name = "linuxgpiod_tdo_num",
520 .handler = linuxgpiod_handle_jtag_gpionum_tdo,
521 .mode = COMMAND_CONFIG,
522 .help = "gpio number for tdo.",
526 .name = "linuxgpiod_tdi_num",
527 .handler = linuxgpiod_handle_jtag_gpionum_tdi,
528 .mode = COMMAND_CONFIG,
529 .help = "gpio number for tdi.",
533 .name = "linuxgpiod_srst_num",
534 .handler = linuxgpiod_handle_jtag_gpionum_srst,
535 .mode = COMMAND_CONFIG,
536 .help = "gpio number for srst.",
540 .name = "linuxgpiod_trst_num",
541 .handler = linuxgpiod_handle_jtag_gpionum_trst,
542 .mode = COMMAND_CONFIG,
543 .help = "gpio number for trst.",
547 .name = "linuxgpiod_swd_nums",
548 .handler = linuxgpiod_handle_swd_gpionums,
549 .mode = COMMAND_CONFIG,
550 .help = "gpio numbers for swclk, swdio. (in that order)",
551 .usage = "swclk swdio",
554 .name = "linuxgpiod_swclk_num",
555 .handler = linuxgpiod_handle_swd_gpionum_swclk,
556 .mode = COMMAND_CONFIG,
557 .help = "gpio number for swclk.",
561 .name = "linuxgpiod_swdio_num",
562 .handler = linuxgpiod_handle_swd_gpionum_swdio,
563 .mode = COMMAND_CONFIG,
564 .help = "gpio number for swdio.",
568 .name = "linuxgpiod_gpiochip",
569 .handler = linuxgpiod_handle_gpiochip,
570 .mode = COMMAND_CONFIG,
571 .help = "number of the gpiochip.",
574 COMMAND_REGISTRATION_DONE
577 static const char *const linuxgpiod_transport[] = { "swd", "jtag", NULL };
579 static struct jtag_interface linuxgpiod_interface = {
580 .supported = DEBUG_CAP_TMS_SEQ,
581 .execute_queue = bitbang_execute_queue,
584 struct adapter_driver linuxgpiod_adapter_driver = {
585 .name = "linuxgpiod",
586 .transports = linuxgpiod_transport,
587 .commands = linuxgpiod_command_handlers,
589 .init = linuxgpiod_init,
590 .quit = linuxgpiod_quit,
591 .reset = linuxgpiod_reset,
593 .jtag_ops = &linuxgpiod_interface,
594 .swd_ops = &bitbang_swd,