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 led_gpio = -1;
31 static int gpiochip = -1;
33 static struct gpiod_chip *gpiod_chip;
34 static struct gpiod_line *gpiod_tck;
35 static struct gpiod_line *gpiod_tms;
36 static struct gpiod_line *gpiod_tdi;
37 static struct gpiod_line *gpiod_tdo;
38 static struct gpiod_line *gpiod_trst;
39 static struct gpiod_line *gpiod_swclk;
40 static struct gpiod_line *gpiod_swdio;
41 static struct gpiod_line *gpiod_srst;
42 static struct gpiod_line *gpiod_led;
44 static int last_swclk;
45 static int last_swdio;
46 static bool last_stored;
47 static bool swdio_input;
49 /* Bitbang interface read of TDO */
50 static bb_value_t linuxgpiod_read(void)
54 retval = gpiod_line_get_value(gpiod_tdo);
56 LOG_WARNING("reading tdo failed");
60 return retval ? BB_HIGH : BB_LOW;
64 * Bitbang interface write of TCK, TMS, TDI
66 * Seeing as this is the only function where the outputs are changed,
67 * we can cache the old value to avoid needlessly writing it.
69 static int linuxgpiod_write(int tck, int tms, int tdi)
75 static int first_time;
86 if (tdi != last_tdi) {
87 retval = gpiod_line_set_value(gpiod_tdi, tdi);
89 LOG_WARNING("writing tdi failed");
92 if (tms != last_tms) {
93 retval = gpiod_line_set_value(gpiod_tms, tms);
95 LOG_WARNING("writing tms failed");
99 if (tck != last_tck) {
100 retval = gpiod_line_set_value(gpiod_tck, tck);
102 LOG_WARNING("writing tck failed");
112 static int linuxgpiod_swdio_read(void)
116 retval = gpiod_line_get_value(gpiod_swdio);
118 LOG_WARNING("Fail read swdio");
125 static void linuxgpiod_swdio_drive(bool is_output)
130 * FIXME: change direction requires release and re-require the line
131 * https://stackoverflow.com/questions/58735140/
132 * this would change in future libgpiod
134 gpiod_line_release(gpiod_swdio);
137 retval = gpiod_line_request_output(gpiod_swdio, "OpenOCD", 1);
139 LOG_WARNING("Fail request_output line swdio");
141 retval = gpiod_line_request_input(gpiod_swdio, "OpenOCD");
143 LOG_WARNING("Fail request_input line swdio");
147 swdio_input = !is_output;
150 static int linuxgpiod_swd_write(int swclk, int swdio)
155 if (!last_stored || (swdio != last_swdio)) {
156 retval = gpiod_line_set_value(gpiod_swdio, swdio);
158 LOG_WARNING("Fail set swdio");
162 /* write swclk last */
163 if (!last_stored || (swclk != last_swclk)) {
164 retval = gpiod_line_set_value(gpiod_swclk, swclk);
166 LOG_WARNING("Fail set swclk");
176 static int linuxgpiod_blink(int on)
183 retval = gpiod_line_set_value(gpiod_led, on);
185 LOG_WARNING("Fail set led");
189 static struct bitbang_interface linuxgpiod_bitbang = {
190 .read = linuxgpiod_read,
191 .write = linuxgpiod_write,
192 .swdio_read = linuxgpiod_swdio_read,
193 .swdio_drive = linuxgpiod_swdio_drive,
194 .swd_write = linuxgpiod_swd_write,
195 .blink = linuxgpiod_blink,
199 * Bitbang interface to manipulate reset lines SRST and TRST
201 * (1) assert or (0) deassert reset lines
203 static int linuxgpiod_reset(int trst, int srst)
205 int retval1 = 0, retval2 = 0;
207 LOG_DEBUG("linuxgpiod_reset");
209 /* assume active low */
211 retval1 = gpiod_line_set_value(gpiod_srst, srst ? 0 : 1);
213 LOG_WARNING("set srst value failed");
216 /* assume active low */
218 retval2 = gpiod_line_set_value(gpiod_trst, trst ? 0 : 1);
220 LOG_WARNING("set trst value failed");
223 return ((retval1 < 0) || (retval2 < 0)) ? ERROR_FAIL : ERROR_OK;
227 * Helper function to determine if gpio number is valid
229 * Assume here that there will be less than 10000 gpios per gpiochip
231 static bool is_gpio_valid(int gpio)
233 return gpio >= 0 && gpio < 10000;
236 static bool linuxgpiod_jtag_mode_possible(void)
238 if (!is_gpio_valid(tck_gpio))
240 if (!is_gpio_valid(tms_gpio))
242 if (!is_gpio_valid(tdi_gpio))
244 if (!is_gpio_valid(tdo_gpio))
249 static bool linuxgpiod_swd_mode_possible(void)
251 if (!is_gpio_valid(swclk_gpio))
253 if (!is_gpio_valid(swdio_gpio))
258 static inline void helper_release(struct gpiod_line *line)
261 gpiod_line_release(line);
264 static int linuxgpiod_quit(void)
266 helper_release(gpiod_led);
267 helper_release(gpiod_srst);
268 helper_release(gpiod_swdio);
269 helper_release(gpiod_swclk);
270 helper_release(gpiod_trst);
271 helper_release(gpiod_tms);
272 helper_release(gpiod_tck);
273 helper_release(gpiod_tdi);
274 helper_release(gpiod_tdo);
276 gpiod_chip_close(gpiod_chip);
281 static struct gpiod_line *helper_get_line(const char *label, unsigned int offset, int val, int dir, int flags)
283 struct gpiod_line *line;
286 line = gpiod_chip_get_line(gpiod_chip, offset);
288 LOG_ERROR("Error get line %s", label);
292 struct gpiod_line_request_config config = {
293 .consumer = "OpenOCD",
298 retval = gpiod_line_request(line, &config, val);
300 LOG_ERROR("Error requesting gpio line %s", label);
307 static struct gpiod_line *helper_get_input_line(const char *label, unsigned int offset)
309 return helper_get_line(label, offset, 0, GPIOD_LINE_REQUEST_DIRECTION_INPUT, 0);
312 static struct gpiod_line *helper_get_output_line(const char *label, unsigned int offset, int val)
314 return helper_get_line(label, offset, val, GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, 0);
317 static struct gpiod_line *helper_get_open_drain_output_line(const char *label, unsigned int offset, int val)
319 return helper_get_line(label, offset, val, GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN);
322 static int linuxgpiod_init(void)
324 LOG_INFO("Linux GPIOD JTAG/SWD bitbang driver");
326 bitbang_interface = &linuxgpiod_bitbang;
328 gpiod_chip = gpiod_chip_open_by_number(gpiochip);
330 LOG_ERROR("Cannot open LinuxGPIOD gpiochip %d", gpiochip);
331 return ERROR_JTAG_INIT_FAILED;
335 * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
336 * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
337 * For SWD, SWCLK and SWDIO are configures as output high.
340 if (transport_is_jtag()) {
341 if (!linuxgpiod_jtag_mode_possible()) {
342 LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode");
346 gpiod_tdo = helper_get_input_line("tdo", tdo_gpio);
350 gpiod_tdi = helper_get_output_line("tdi", tdi_gpio, 0);
354 gpiod_tck = helper_get_output_line("tck", tck_gpio, 0);
358 gpiod_tms = helper_get_output_line("tms", tms_gpio, 1);
362 if (is_gpio_valid(trst_gpio)) {
363 if (jtag_get_reset_config() & RESET_TRST_OPEN_DRAIN)
364 gpiod_trst = helper_get_open_drain_output_line("trst", trst_gpio, 1);
366 gpiod_trst = helper_get_output_line("trst", trst_gpio, 1);
373 if (transport_is_swd()) {
374 if (!linuxgpiod_swd_mode_possible()) {
375 LOG_ERROR("Require swclk and swdio gpio for SWD mode");
379 gpiod_swclk = helper_get_output_line("swclk", swclk_gpio, 1);
383 gpiod_swdio = helper_get_output_line("swdio", swdio_gpio, 1);
388 if (is_gpio_valid(srst_gpio)) {
389 if (jtag_get_reset_config() & RESET_SRST_PUSH_PULL)
390 gpiod_srst = helper_get_output_line("srst", srst_gpio, 1);
392 gpiod_srst = helper_get_open_drain_output_line("srst", srst_gpio, 1);
398 if (is_gpio_valid(led_gpio)) {
399 gpiod_led = helper_get_output_line("led", led_gpio, 0);
409 return ERROR_JTAG_INIT_FAILED;
412 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionums)
415 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
416 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpio);
417 COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], tdi_gpio);
418 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], tdo_gpio);
419 } else if (CMD_ARGC != 0) {
420 return ERROR_COMMAND_SYNTAX_ERROR;
424 "LinuxGPIOD nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
425 tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
430 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tck)
433 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
435 command_print(CMD, "LinuxGPIOD num: tck = %d", tck_gpio);
439 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tms)
442 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
444 command_print(CMD, "LinuxGPIOD num: tms = %d", tms_gpio);
448 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdo)
451 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
453 command_print(CMD, "LinuxGPIOD num: tdo = %d", tdo_gpio);
457 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdi)
460 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
462 command_print(CMD, "LinuxGPIOD num: tdi = %d", tdi_gpio);
466 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_srst)
469 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
471 command_print(CMD, "LinuxGPIOD num: srst = %d", srst_gpio);
475 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_trst)
478 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
480 command_print(CMD, "LinuxGPIOD num: trst = %d", trst_gpio);
484 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionums)
487 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
488 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], swdio_gpio);
489 } else if (CMD_ARGC != 0) {
490 return ERROR_COMMAND_SYNTAX_ERROR;
494 "LinuxGPIOD nums: swclk = %d, swdio = %d",
495 swclk_gpio, swdio_gpio);
500 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swclk)
503 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
505 command_print(CMD, "LinuxGPIOD num: swclk = %d", swclk_gpio);
509 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swdio)
512 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpio);
514 command_print(CMD, "LinuxGPIOD num: swdio = %d", swdio_gpio);
518 COMMAND_HANDLER(linuxgpiod_handle_gpionum_led)
521 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], led_gpio);
523 command_print(CMD, "LinuxGPIOD num: led = %d", led_gpio);
527 COMMAND_HANDLER(linuxgpiod_handle_gpiochip)
530 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], gpiochip);
532 command_print(CMD, "LinuxGPIOD gpiochip = %d", gpiochip);
536 static const struct command_registration linuxgpiod_subcommand_handlers[] = {
539 .handler = linuxgpiod_handle_jtag_gpionums,
540 .mode = COMMAND_CONFIG,
541 .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
542 .usage = "tck tms tdi tdo",
546 .handler = linuxgpiod_handle_jtag_gpionum_tck,
547 .mode = COMMAND_CONFIG,
548 .help = "gpio number for tck.",
553 .handler = linuxgpiod_handle_jtag_gpionum_tms,
554 .mode = COMMAND_CONFIG,
555 .help = "gpio number for tms.",
560 .handler = linuxgpiod_handle_jtag_gpionum_tdo,
561 .mode = COMMAND_CONFIG,
562 .help = "gpio number for tdo.",
567 .handler = linuxgpiod_handle_jtag_gpionum_tdi,
568 .mode = COMMAND_CONFIG,
569 .help = "gpio number for tdi.",
574 .handler = linuxgpiod_handle_jtag_gpionum_srst,
575 .mode = COMMAND_CONFIG,
576 .help = "gpio number for srst.",
581 .handler = linuxgpiod_handle_jtag_gpionum_trst,
582 .mode = COMMAND_CONFIG,
583 .help = "gpio number for trst.",
588 .handler = linuxgpiod_handle_swd_gpionums,
589 .mode = COMMAND_CONFIG,
590 .help = "gpio numbers for swclk, swdio. (in that order)",
591 .usage = "swclk swdio",
595 .handler = linuxgpiod_handle_swd_gpionum_swclk,
596 .mode = COMMAND_CONFIG,
597 .help = "gpio number for swclk.",
602 .handler = linuxgpiod_handle_swd_gpionum_swdio,
603 .mode = COMMAND_CONFIG,
604 .help = "gpio number for swdio.",
609 .handler = linuxgpiod_handle_gpionum_led,
610 .mode = COMMAND_CONFIG,
611 .help = "gpio number for LED.",
616 .handler = linuxgpiod_handle_gpiochip,
617 .mode = COMMAND_CONFIG,
618 .help = "number of the gpiochip.",
621 COMMAND_REGISTRATION_DONE
624 static const struct command_registration linuxgpiod_command_handlers[] = {
626 .name = "linuxgpiod",
628 .help = "perform linuxgpiod management",
629 .chain = linuxgpiod_subcommand_handlers,
632 COMMAND_REGISTRATION_DONE
635 static const char *const linuxgpiod_transport[] = { "swd", "jtag", NULL };
637 static struct jtag_interface linuxgpiod_interface = {
638 .supported = DEBUG_CAP_TMS_SEQ,
639 .execute_queue = bitbang_execute_queue,
642 struct adapter_driver linuxgpiod_adapter_driver = {
643 .name = "linuxgpiod",
644 .transports = linuxgpiod_transport,
645 .commands = linuxgpiod_command_handlers,
647 .init = linuxgpiod_init,
648 .quit = linuxgpiod_quit,
649 .reset = linuxgpiod_reset,
651 .jtag_ops = &linuxgpiod_interface,
652 .swd_ops = &bitbang_swd,