driver/linuxgpiod: add support for opendrain srst
[openocd.git] / src / jtag / drivers / linuxgpiod.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Bitbang driver for Linux GPIO descriptors through libgpiod
4 * Copyright (C) 2020 Antonio Borneo <borneo.antonio@gmail.com>
5 *
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>
10 */
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <gpiod.h>
17 #include <jtag/interface.h>
18 #include <transport/transport.h>
19 #include "bitbang.h"
20
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;
32
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;
43
44 static int last_swclk;
45 static int last_swdio;
46 static bool last_stored;
47 static bool swdio_input;
48
49 /* Bitbang interface read of TDO */
50 static bb_value_t linuxgpiod_read(void)
51 {
52 int retval;
53
54 retval = gpiod_line_get_value(gpiod_tdo);
55 if (retval < 0) {
56 LOG_WARNING("reading tdo failed");
57 return 0;
58 }
59
60 return retval ? BB_HIGH : BB_LOW;
61 }
62
63 /*
64 * Bitbang interface write of TCK, TMS, TDI
65 *
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.
68 */
69 static int linuxgpiod_write(int tck, int tms, int tdi)
70 {
71 static int last_tck;
72 static int last_tms;
73 static int last_tdi;
74
75 static int first_time;
76
77 int retval;
78
79 if (!first_time) {
80 last_tck = !tck;
81 last_tms = !tms;
82 last_tdi = !tdi;
83 first_time = 1;
84 }
85
86 if (tdi != last_tdi) {
87 retval = gpiod_line_set_value(gpiod_tdi, tdi);
88 if (retval < 0)
89 LOG_WARNING("writing tdi failed");
90 }
91
92 if (tms != last_tms) {
93 retval = gpiod_line_set_value(gpiod_tms, tms);
94 if (retval < 0)
95 LOG_WARNING("writing tms failed");
96 }
97
98 /* write clk last */
99 if (tck != last_tck) {
100 retval = gpiod_line_set_value(gpiod_tck, tck);
101 if (retval < 0)
102 LOG_WARNING("writing tck failed");
103 }
104
105 last_tdi = tdi;
106 last_tms = tms;
107 last_tck = tck;
108
109 return ERROR_OK;
110 }
111
112 static int linuxgpiod_swdio_read(void)
113 {
114 int retval;
115
116 retval = gpiod_line_get_value(gpiod_swdio);
117 if (retval < 0) {
118 LOG_WARNING("Fail read swdio");
119 return 0;
120 }
121
122 return retval;
123 }
124
125 static void linuxgpiod_swdio_drive(bool is_output)
126 {
127 int retval;
128
129 /*
130 * FIXME: change direction requires release and re-require the line
131 * https://stackoverflow.com/questions/58735140/
132 * this would change in future libgpiod
133 */
134 gpiod_line_release(gpiod_swdio);
135
136 if (is_output) {
137 retval = gpiod_line_request_output(gpiod_swdio, "OpenOCD", 1);
138 if (retval < 0)
139 LOG_WARNING("Fail request_output line swdio");
140 } else {
141 retval = gpiod_line_request_input(gpiod_swdio, "OpenOCD");
142 if (retval < 0)
143 LOG_WARNING("Fail request_input line swdio");
144 }
145
146 last_stored = false;
147 swdio_input = !is_output;
148 }
149
150 static int linuxgpiod_swd_write(int swclk, int swdio)
151 {
152 int retval;
153
154 if (!swdio_input) {
155 if (!last_stored || (swdio != last_swdio)) {
156 retval = gpiod_line_set_value(gpiod_swdio, swdio);
157 if (retval < 0)
158 LOG_WARNING("Fail set swdio");
159 }
160 }
161
162 /* write swclk last */
163 if (!last_stored || (swclk != last_swclk)) {
164 retval = gpiod_line_set_value(gpiod_swclk, swclk);
165 if (retval < 0)
166 LOG_WARNING("Fail set swclk");
167 }
168
169 last_swdio = swdio;
170 last_swclk = swclk;
171 last_stored = true;
172
173 return ERROR_OK;
174 }
175
176 static int linuxgpiod_blink(int on)
177 {
178 int retval;
179
180 if (!gpiod_led)
181 return ERROR_OK;
182
183 retval = gpiod_line_set_value(gpiod_led, on);
184 if (retval < 0)
185 LOG_WARNING("Fail set led");
186 return retval;
187 }
188
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,
196 };
197
198 /*
199 * Bitbang interface to manipulate reset lines SRST and TRST
200 *
201 * (1) assert or (0) deassert reset lines
202 */
203 static int linuxgpiod_reset(int trst, int srst)
204 {
205 int retval1 = 0, retval2 = 0;
206
207 LOG_DEBUG("linuxgpiod_reset");
208
209 /* assume active low */
210 if (gpiod_srst) {
211 retval1 = gpiod_line_set_value(gpiod_srst, srst ? 0 : 1);
212 if (retval1 < 0)
213 LOG_WARNING("set srst value failed");
214 }
215
216 /* assume active low */
217 if (gpiod_trst) {
218 retval2 = gpiod_line_set_value(gpiod_trst, trst ? 0 : 1);
219 if (retval2 < 0)
220 LOG_WARNING("set trst value failed");
221 }
222
223 return ((retval1 < 0) || (retval2 < 0)) ? ERROR_FAIL : ERROR_OK;
224 }
225
226 /*
227 * Helper function to determine if gpio number is valid
228 *
229 * Assume here that there will be less than 10000 gpios per gpiochip
230 */
231 static bool is_gpio_valid(int gpio)
232 {
233 return gpio >= 0 && gpio < 10000;
234 }
235
236 static bool linuxgpiod_jtag_mode_possible(void)
237 {
238 if (!is_gpio_valid(tck_gpio))
239 return false;
240 if (!is_gpio_valid(tms_gpio))
241 return false;
242 if (!is_gpio_valid(tdi_gpio))
243 return false;
244 if (!is_gpio_valid(tdo_gpio))
245 return false;
246 return true;
247 }
248
249 static bool linuxgpiod_swd_mode_possible(void)
250 {
251 if (!is_gpio_valid(swclk_gpio))
252 return false;
253 if (!is_gpio_valid(swdio_gpio))
254 return false;
255 return true;
256 }
257
258 static inline void helper_release(struct gpiod_line *line)
259 {
260 if (line)
261 gpiod_line_release(line);
262 }
263
264 static int linuxgpiod_quit(void)
265 {
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);
275
276 gpiod_chip_close(gpiod_chip);
277
278 return ERROR_OK;
279 }
280
281 static struct gpiod_line *helper_get_line(const char *label, unsigned int offset, int val, int dir, int flags)
282 {
283 struct gpiod_line *line;
284 int retval;
285
286 line = gpiod_chip_get_line(gpiod_chip, offset);
287 if (!line) {
288 LOG_ERROR("Error get line %s", label);
289 return NULL;
290 }
291
292 struct gpiod_line_request_config config = {
293 .consumer = "OpenOCD",
294 .request_type = dir,
295 .flags = flags,
296 };
297
298 retval = gpiod_line_request(line, &config, val);
299 if (retval < 0) {
300 LOG_ERROR("Error requesting gpio line %s", label);
301 return NULL;
302 }
303
304 return line;
305 }
306
307 static struct gpiod_line *helper_get_input_line(const char *label, unsigned int offset)
308 {
309 return helper_get_line(label, offset, 0, GPIOD_LINE_REQUEST_DIRECTION_INPUT, 0);
310 }
311
312 static struct gpiod_line *helper_get_output_line(const char *label, unsigned int offset, int val)
313 {
314 return helper_get_line(label, offset, val, GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, 0);
315 }
316
317 static struct gpiod_line *helper_get_open_drain_output_line(const char *label, unsigned int offset, int val)
318 {
319 return helper_get_line(label, offset, val, GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN);
320 }
321
322 static int linuxgpiod_init(void)
323 {
324 LOG_INFO("Linux GPIOD JTAG/SWD bitbang driver");
325
326 bitbang_interface = &linuxgpiod_bitbang;
327
328 gpiod_chip = gpiod_chip_open_by_number(gpiochip);
329 if (!gpiod_chip) {
330 LOG_ERROR("Cannot open LinuxGPIOD gpiochip %d", gpiochip);
331 return ERROR_JTAG_INIT_FAILED;
332 }
333
334 /*
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.
338 */
339
340 if (transport_is_jtag()) {
341 if (!linuxgpiod_jtag_mode_possible()) {
342 LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode");
343 goto out_error;
344 }
345
346 gpiod_tdo = helper_get_input_line("tdo", tdo_gpio);
347 if (!gpiod_tdo)
348 goto out_error;
349
350 gpiod_tdi = helper_get_output_line("tdi", tdi_gpio, 0);
351 if (!gpiod_tdi)
352 goto out_error;
353
354 gpiod_tck = helper_get_output_line("tck", tck_gpio, 0);
355 if (!gpiod_tck)
356 goto out_error;
357
358 gpiod_tms = helper_get_output_line("tms", tms_gpio, 1);
359 if (!gpiod_tms)
360 goto out_error;
361
362 if (is_gpio_valid(trst_gpio)) {
363 gpiod_trst = helper_get_output_line("trst", trst_gpio, 1);
364 if (!gpiod_trst)
365 goto out_error;
366 }
367 }
368
369 if (transport_is_swd()) {
370 if (!linuxgpiod_swd_mode_possible()) {
371 LOG_ERROR("Require swclk and swdio gpio for SWD mode");
372 goto out_error;
373 }
374
375 gpiod_swclk = helper_get_output_line("swclk", swclk_gpio, 1);
376 if (!gpiod_swclk)
377 goto out_error;
378
379 gpiod_swdio = helper_get_output_line("swdio", swdio_gpio, 1);
380 if (!gpiod_swdio)
381 goto out_error;
382 }
383
384 if (is_gpio_valid(srst_gpio)) {
385 if (jtag_get_reset_config() & RESET_SRST_PUSH_PULL)
386 gpiod_srst = helper_get_output_line("srst", srst_gpio, 1);
387 else
388 gpiod_srst = helper_get_open_drain_output_line("srst", srst_gpio, 1);
389
390 if (!gpiod_srst)
391 goto out_error;
392 }
393
394 if (is_gpio_valid(led_gpio)) {
395 gpiod_led = helper_get_output_line("led", led_gpio, 0);
396 if (!gpiod_led)
397 goto out_error;
398 }
399
400 return ERROR_OK;
401
402 out_error:
403 linuxgpiod_quit();
404
405 return ERROR_JTAG_INIT_FAILED;
406 }
407
408 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionums)
409 {
410 if (CMD_ARGC == 4) {
411 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
412 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpio);
413 COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], tdi_gpio);
414 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], tdo_gpio);
415 } else if (CMD_ARGC != 0) {
416 return ERROR_COMMAND_SYNTAX_ERROR;
417 }
418
419 command_print(CMD,
420 "LinuxGPIOD nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
421 tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
422
423 return ERROR_OK;
424 }
425
426 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tck)
427 {
428 if (CMD_ARGC == 1)
429 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
430
431 command_print(CMD, "LinuxGPIOD num: tck = %d", tck_gpio);
432 return ERROR_OK;
433 }
434
435 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tms)
436 {
437 if (CMD_ARGC == 1)
438 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
439
440 command_print(CMD, "LinuxGPIOD num: tms = %d", tms_gpio);
441 return ERROR_OK;
442 }
443
444 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdo)
445 {
446 if (CMD_ARGC == 1)
447 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
448
449 command_print(CMD, "LinuxGPIOD num: tdo = %d", tdo_gpio);
450 return ERROR_OK;
451 }
452
453 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdi)
454 {
455 if (CMD_ARGC == 1)
456 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
457
458 command_print(CMD, "LinuxGPIOD num: tdi = %d", tdi_gpio);
459 return ERROR_OK;
460 }
461
462 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_srst)
463 {
464 if (CMD_ARGC == 1)
465 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
466
467 command_print(CMD, "LinuxGPIOD num: srst = %d", srst_gpio);
468 return ERROR_OK;
469 }
470
471 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_trst)
472 {
473 if (CMD_ARGC == 1)
474 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
475
476 command_print(CMD, "LinuxGPIOD num: trst = %d", trst_gpio);
477 return ERROR_OK;
478 }
479
480 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionums)
481 {
482 if (CMD_ARGC == 2) {
483 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
484 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], swdio_gpio);
485 } else if (CMD_ARGC != 0) {
486 return ERROR_COMMAND_SYNTAX_ERROR;
487 }
488
489 command_print(CMD,
490 "LinuxGPIOD nums: swclk = %d, swdio = %d",
491 swclk_gpio, swdio_gpio);
492
493 return ERROR_OK;
494 }
495
496 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swclk)
497 {
498 if (CMD_ARGC == 1)
499 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
500
501 command_print(CMD, "LinuxGPIOD num: swclk = %d", swclk_gpio);
502 return ERROR_OK;
503 }
504
505 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swdio)
506 {
507 if (CMD_ARGC == 1)
508 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpio);
509
510 command_print(CMD, "LinuxGPIOD num: swdio = %d", swdio_gpio);
511 return ERROR_OK;
512 }
513
514 COMMAND_HANDLER(linuxgpiod_handle_gpionum_led)
515 {
516 if (CMD_ARGC == 1)
517 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], led_gpio);
518
519 command_print(CMD, "LinuxGPIOD num: led = %d", led_gpio);
520 return ERROR_OK;
521 }
522
523 COMMAND_HANDLER(linuxgpiod_handle_gpiochip)
524 {
525 if (CMD_ARGC == 1)
526 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], gpiochip);
527
528 command_print(CMD, "LinuxGPIOD gpiochip = %d", gpiochip);
529 return ERROR_OK;
530 }
531
532 static const struct command_registration linuxgpiod_subcommand_handlers[] = {
533 {
534 .name = "jtag_nums",
535 .handler = linuxgpiod_handle_jtag_gpionums,
536 .mode = COMMAND_CONFIG,
537 .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
538 .usage = "tck tms tdi tdo",
539 },
540 {
541 .name = "tck_num",
542 .handler = linuxgpiod_handle_jtag_gpionum_tck,
543 .mode = COMMAND_CONFIG,
544 .help = "gpio number for tck.",
545 .usage = "tck",
546 },
547 {
548 .name = "tms_num",
549 .handler = linuxgpiod_handle_jtag_gpionum_tms,
550 .mode = COMMAND_CONFIG,
551 .help = "gpio number for tms.",
552 .usage = "tms",
553 },
554 {
555 .name = "tdo_num",
556 .handler = linuxgpiod_handle_jtag_gpionum_tdo,
557 .mode = COMMAND_CONFIG,
558 .help = "gpio number for tdo.",
559 .usage = "tdo",
560 },
561 {
562 .name = "tdi_num",
563 .handler = linuxgpiod_handle_jtag_gpionum_tdi,
564 .mode = COMMAND_CONFIG,
565 .help = "gpio number for tdi.",
566 .usage = "tdi",
567 },
568 {
569 .name = "srst_num",
570 .handler = linuxgpiod_handle_jtag_gpionum_srst,
571 .mode = COMMAND_CONFIG,
572 .help = "gpio number for srst.",
573 .usage = "srst",
574 },
575 {
576 .name = "trst_num",
577 .handler = linuxgpiod_handle_jtag_gpionum_trst,
578 .mode = COMMAND_CONFIG,
579 .help = "gpio number for trst.",
580 .usage = "trst",
581 },
582 {
583 .name = "swd_nums",
584 .handler = linuxgpiod_handle_swd_gpionums,
585 .mode = COMMAND_CONFIG,
586 .help = "gpio numbers for swclk, swdio. (in that order)",
587 .usage = "swclk swdio",
588 },
589 {
590 .name = "swclk_num",
591 .handler = linuxgpiod_handle_swd_gpionum_swclk,
592 .mode = COMMAND_CONFIG,
593 .help = "gpio number for swclk.",
594 .usage = "swclk",
595 },
596 {
597 .name = "swdio_num",
598 .handler = linuxgpiod_handle_swd_gpionum_swdio,
599 .mode = COMMAND_CONFIG,
600 .help = "gpio number for swdio.",
601 .usage = "swdio",
602 },
603 {
604 .name = "led_num",
605 .handler = linuxgpiod_handle_gpionum_led,
606 .mode = COMMAND_CONFIG,
607 .help = "gpio number for LED.",
608 .usage = "led",
609 },
610 {
611 .name = "gpiochip",
612 .handler = linuxgpiod_handle_gpiochip,
613 .mode = COMMAND_CONFIG,
614 .help = "number of the gpiochip.",
615 .usage = "gpiochip",
616 },
617 COMMAND_REGISTRATION_DONE
618 };
619
620 static const struct command_registration linuxgpiod_command_handlers[] = {
621 {
622 .name = "linuxgpiod",
623 .mode = COMMAND_ANY,
624 .help = "perform linuxgpiod management",
625 .chain = linuxgpiod_subcommand_handlers,
626 .usage = "",
627 },
628 COMMAND_REGISTRATION_DONE
629 };
630
631 static const char *const linuxgpiod_transport[] = { "swd", "jtag", NULL };
632
633 static struct jtag_interface linuxgpiod_interface = {
634 .supported = DEBUG_CAP_TMS_SEQ,
635 .execute_queue = bitbang_execute_queue,
636 };
637
638 struct adapter_driver linuxgpiod_adapter_driver = {
639 .name = "linuxgpiod",
640 .transports = linuxgpiod_transport,
641 .commands = linuxgpiod_command_handlers,
642
643 .init = linuxgpiod_init,
644 .quit = linuxgpiod_quit,
645 .reset = linuxgpiod_reset,
646
647 .jtag_ops = &linuxgpiod_interface,
648 .swd_ops = &bitbang_swd,
649 };

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)