jtag/drivers: add linuxgpiod driver
[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 gpiochip = -1;
31
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;
41
42 static int last_swclk;
43 static int last_swdio;
44 static bool last_stored;
45 static bool swdio_input;
46
47 /* Bitbang interface read of TDO */
48 static bb_value_t linuxgpiod_read(void)
49 {
50 int retval;
51
52 retval = gpiod_line_get_value(gpiod_tdo);
53 if (retval < 0) {
54 LOG_WARNING("reading tdo failed");
55 return 0;
56 }
57
58 return retval ? BB_HIGH : BB_LOW;
59 }
60
61 /*
62 * Bitbang interface write of TCK, TMS, TDI
63 *
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.
66 */
67 static int linuxgpiod_write(int tck, int tms, int tdi)
68 {
69 static int last_tck;
70 static int last_tms;
71 static int last_tdi;
72
73 static int first_time;
74
75 int retval;
76
77 if (!first_time) {
78 last_tck = !tck;
79 last_tms = !tms;
80 last_tdi = !tdi;
81 first_time = 1;
82 }
83
84 if (tdi != last_tdi) {
85 retval = gpiod_line_set_value(gpiod_tdi, tdi);
86 if (retval < 0)
87 LOG_WARNING("writing tdi failed");
88 }
89
90 if (tms != last_tms) {
91 retval = gpiod_line_set_value(gpiod_tms, tms);
92 if (retval < 0)
93 LOG_WARNING("writing tms failed");
94 }
95
96 /* write clk last */
97 if (tck != last_tck) {
98 retval = gpiod_line_set_value(gpiod_tck, tck);
99 if (retval < 0)
100 LOG_WARNING("writing tck failed");
101 }
102
103 last_tdi = tdi;
104 last_tms = tms;
105 last_tck = tck;
106
107 return ERROR_OK;
108 }
109
110 static int linuxgpiod_swdio_read(void)
111 {
112 int retval;
113
114 retval = gpiod_line_get_value(gpiod_swdio);
115 if (retval < 0) {
116 LOG_WARNING("Fail read swdio");
117 return 0;
118 }
119
120 return retval;
121 }
122
123 static void linuxgpiod_swdio_drive(bool is_output)
124 {
125 int retval;
126
127 /*
128 * FIXME: change direction requires release and re-require the line
129 * https://stackoverflow.com/questions/58735140/
130 * this would change in future libgpiod
131 */
132 gpiod_line_release(gpiod_swdio);
133
134 if (is_output) {
135 retval = gpiod_line_request_output(gpiod_swdio, "OpenOCD", 1);
136 if (retval < 0)
137 LOG_WARNING("Fail request_output line swdio");
138 } else {
139 retval = gpiod_line_request_input(gpiod_swdio, "OpenOCD");
140 if (retval < 0)
141 LOG_WARNING("Fail request_input line swdio");
142 }
143
144 last_stored = false;
145 swdio_input = !is_output;
146 }
147
148 static int linuxgpiod_swd_write(int swclk, int swdio)
149 {
150 int retval;
151
152 if (!swdio_input) {
153 if (!last_stored || (swdio != last_swdio)) {
154 retval = gpiod_line_set_value(gpiod_swdio, swdio);
155 if (retval < 0)
156 LOG_WARNING("Fail set swdio");
157 }
158 }
159
160 /* write swclk last */
161 if (!last_stored || (swclk != last_swclk)) {
162 retval = gpiod_line_set_value(gpiod_swclk, swclk);
163 if (retval < 0)
164 LOG_WARNING("Fail set swclk");
165 }
166
167 last_swdio = swdio;
168 last_swclk = swclk;
169 last_stored = true;
170
171 return ERROR_OK;
172 }
173
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,
180 .blink = NULL,
181 };
182
183 /*
184 * Bitbang interface to manipulate reset lines SRST and TRST
185 *
186 * (1) assert or (0) deassert reset lines
187 */
188 static int linuxgpiod_reset(int trst, int srst)
189 {
190 int retval1 = 0, retval2 = 0;
191
192 LOG_DEBUG("linuxgpiod_reset");
193
194 /* assume active low */
195 if (gpiod_srst != NULL) {
196 retval1 = gpiod_line_set_value(gpiod_srst, srst ? 0 : 1);
197 if (retval1 < 0)
198 LOG_WARNING("set srst value failed");
199 }
200
201 /* assume active low */
202 if (gpiod_trst != NULL) {
203 retval2 = gpiod_line_set_value(gpiod_trst, trst ? 0 : 1);
204 if (retval2 < 0)
205 LOG_WARNING("set trst value failed");
206 }
207
208 return ((retval1 < 0) || (retval2 < 0)) ? ERROR_FAIL : ERROR_OK;
209 }
210
211 /*
212 * Helper function to determine if gpio number is valid
213 *
214 * Assume here that there will be less than 10000 gpios per gpiochip
215 */
216 static bool is_gpio_valid(int gpio)
217 {
218 return gpio >= 0 && gpio < 10000;
219 }
220
221 static bool linuxgpiod_jtag_mode_possible(void)
222 {
223 if (!is_gpio_valid(tck_gpio))
224 return false;
225 if (!is_gpio_valid(tms_gpio))
226 return false;
227 if (!is_gpio_valid(tdi_gpio))
228 return false;
229 if (!is_gpio_valid(tdo_gpio))
230 return false;
231 return true;
232 }
233
234 static bool linuxgpiod_swd_mode_possible(void)
235 {
236 if (!is_gpio_valid(swclk_gpio))
237 return false;
238 if (!is_gpio_valid(swdio_gpio))
239 return false;
240 return true;
241 }
242
243 static inline void helper_release(struct gpiod_line *line)
244 {
245 if (line)
246 gpiod_line_release(line);
247 }
248
249 static int linuxgpiod_quit(void)
250 {
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);
259
260 gpiod_chip_close(gpiod_chip);
261
262 return ERROR_OK;
263 }
264
265 static struct gpiod_line *helper_get_input_line(const char *label, unsigned int offset)
266 {
267 struct gpiod_line *line;
268 int retval;
269
270 line = gpiod_chip_get_line(gpiod_chip, offset);
271 if (line == NULL) {
272 LOG_ERROR("Error get line %s", label);
273 return NULL;
274 }
275
276 retval = gpiod_line_request_input(line, "OpenOCD");
277 if (retval < 0) {
278 LOG_ERROR("Error request_input line %s", label);
279 return NULL;
280 }
281
282 return line;
283 }
284
285 static struct gpiod_line *helper_get_output_line(const char *label, unsigned int offset, int val)
286 {
287 struct gpiod_line *line;
288 int retval;
289
290 line = gpiod_chip_get_line(gpiod_chip, offset);
291 if (line == NULL) {
292 LOG_ERROR("Error get line %s", label);
293 return NULL;
294 }
295
296 retval = gpiod_line_request_output(line, "OpenOCD", val);
297 if (retval < 0) {
298 LOG_ERROR("Error request_output line %s", label);
299 return NULL;
300 }
301
302 return line;
303 }
304
305 static int linuxgpiod_init(void)
306 {
307 LOG_INFO("Linux GPIOD JTAG/SWD bitbang driver");
308
309 bitbang_interface = &linuxgpiod_bitbang;
310
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;
315 }
316
317 /*
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.
321 */
322
323 if (transport_is_jtag()) {
324 if (!linuxgpiod_jtag_mode_possible()) {
325 LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode");
326 goto out_error;
327 }
328
329 gpiod_tdo = helper_get_input_line("tdo", tdo_gpio);
330 if (gpiod_tdo == NULL)
331 goto out_error;
332
333 gpiod_tdi = helper_get_output_line("tdi", tdi_gpio, 0);
334 if (gpiod_tdi == NULL)
335 goto out_error;
336
337 gpiod_tck = helper_get_output_line("tck", tck_gpio, 0);
338 if (gpiod_tck == NULL)
339 goto out_error;
340
341 gpiod_tms = helper_get_output_line("tms", tms_gpio, 1);
342 if (gpiod_tms == NULL)
343 goto out_error;
344
345 if (is_gpio_valid(trst_gpio)) {
346 gpiod_trst = helper_get_output_line("trst", trst_gpio, 1);
347 if (gpiod_trst == NULL)
348 goto out_error;
349 }
350 }
351
352 if (transport_is_swd()) {
353 if (!linuxgpiod_swd_mode_possible()) {
354 LOG_ERROR("Require swclk and swdio gpio for SWD mode");
355 goto out_error;
356 }
357
358 gpiod_swclk = helper_get_output_line("swclk", swclk_gpio, 1);
359 if (gpiod_swclk == NULL)
360 goto out_error;
361
362 gpiod_swdio = helper_get_output_line("swdio", swdio_gpio, 1);
363 if (gpiod_swdio == NULL)
364 goto out_error;
365 }
366
367 if (is_gpio_valid(srst_gpio)) {
368 gpiod_srst = helper_get_output_line("srst", srst_gpio, 1);
369 if (gpiod_srst == NULL)
370 goto out_error;
371 }
372
373 return ERROR_OK;
374
375 out_error:
376 linuxgpiod_quit();
377
378 return ERROR_JTAG_INIT_FAILED;
379 }
380
381 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionums)
382 {
383 if (CMD_ARGC == 4) {
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;
390 }
391
392 command_print(CMD,
393 "LinuxGPIOD nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
394 tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
395
396 return ERROR_OK;
397 }
398
399 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tck)
400 {
401 if (CMD_ARGC == 1)
402 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
403
404 command_print(CMD, "LinuxGPIOD num: tck = %d", tck_gpio);
405 return ERROR_OK;
406 }
407
408 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tms)
409 {
410 if (CMD_ARGC == 1)
411 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
412
413 command_print(CMD, "LinuxGPIOD num: tms = %d", tms_gpio);
414 return ERROR_OK;
415 }
416
417 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdo)
418 {
419 if (CMD_ARGC == 1)
420 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
421
422 command_print(CMD, "LinuxGPIOD num: tdo = %d", tdo_gpio);
423 return ERROR_OK;
424 }
425
426 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdi)
427 {
428 if (CMD_ARGC == 1)
429 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
430
431 command_print(CMD, "LinuxGPIOD num: tdi = %d", tdi_gpio);
432 return ERROR_OK;
433 }
434
435 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_srst)
436 {
437 if (CMD_ARGC == 1)
438 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
439
440 command_print(CMD, "LinuxGPIOD num: srst = %d", srst_gpio);
441 return ERROR_OK;
442 }
443
444 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_trst)
445 {
446 if (CMD_ARGC == 1)
447 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
448
449 command_print(CMD, "LinuxGPIOD num: trst = %d", trst_gpio);
450 return ERROR_OK;
451 }
452
453 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionums)
454 {
455 if (CMD_ARGC == 2) {
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;
460 }
461
462 command_print(CMD,
463 "LinuxGPIOD nums: swclk = %d, swdio = %d",
464 swclk_gpio, swdio_gpio);
465
466 return ERROR_OK;
467 }
468
469 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swclk)
470 {
471 if (CMD_ARGC == 1)
472 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
473
474 command_print(CMD, "LinuxGPIOD num: swclk = %d", swclk_gpio);
475 return ERROR_OK;
476 }
477
478 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swdio)
479 {
480 if (CMD_ARGC == 1)
481 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpio);
482
483 command_print(CMD, "LinuxGPIOD num: swdio = %d", swdio_gpio);
484 return ERROR_OK;
485 }
486
487 COMMAND_HANDLER(linuxgpiod_handle_gpiochip)
488 {
489 if (CMD_ARGC == 1)
490 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], gpiochip);
491
492 command_print(CMD, "LinuxGPIOD gpiochip = %d", gpiochip);
493 return ERROR_OK;
494 }
495
496 static const struct command_registration linuxgpiod_command_handlers[] = {
497 {
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",
503 },
504 {
505 .name = "linuxgpiod_tck_num",
506 .handler = linuxgpiod_handle_jtag_gpionum_tck,
507 .mode = COMMAND_CONFIG,
508 .help = "gpio number for tck.",
509 .usage = "tck",
510 },
511 {
512 .name = "linuxgpiod_tms_num",
513 .handler = linuxgpiod_handle_jtag_gpionum_tms,
514 .mode = COMMAND_CONFIG,
515 .help = "gpio number for tms.",
516 .usage = "tms",
517 },
518 {
519 .name = "linuxgpiod_tdo_num",
520 .handler = linuxgpiod_handle_jtag_gpionum_tdo,
521 .mode = COMMAND_CONFIG,
522 .help = "gpio number for tdo.",
523 .usage = "tdo",
524 },
525 {
526 .name = "linuxgpiod_tdi_num",
527 .handler = linuxgpiod_handle_jtag_gpionum_tdi,
528 .mode = COMMAND_CONFIG,
529 .help = "gpio number for tdi.",
530 .usage = "tdi",
531 },
532 {
533 .name = "linuxgpiod_srst_num",
534 .handler = linuxgpiod_handle_jtag_gpionum_srst,
535 .mode = COMMAND_CONFIG,
536 .help = "gpio number for srst.",
537 .usage = "srst",
538 },
539 {
540 .name = "linuxgpiod_trst_num",
541 .handler = linuxgpiod_handle_jtag_gpionum_trst,
542 .mode = COMMAND_CONFIG,
543 .help = "gpio number for trst.",
544 .usage = "trst",
545 },
546 {
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",
552 },
553 {
554 .name = "linuxgpiod_swclk_num",
555 .handler = linuxgpiod_handle_swd_gpionum_swclk,
556 .mode = COMMAND_CONFIG,
557 .help = "gpio number for swclk.",
558 .usage = "swclk",
559 },
560 {
561 .name = "linuxgpiod_swdio_num",
562 .handler = linuxgpiod_handle_swd_gpionum_swdio,
563 .mode = COMMAND_CONFIG,
564 .help = "gpio number for swdio.",
565 .usage = "swdio",
566 },
567 {
568 .name = "linuxgpiod_gpiochip",
569 .handler = linuxgpiod_handle_gpiochip,
570 .mode = COMMAND_CONFIG,
571 .help = "number of the gpiochip.",
572 .usage = "gpiochip",
573 },
574 COMMAND_REGISTRATION_DONE
575 };
576
577 static const char *const linuxgpiod_transport[] = { "swd", "jtag", NULL };
578
579 static struct jtag_interface linuxgpiod_interface = {
580 .supported = DEBUG_CAP_TMS_SEQ,
581 .execute_queue = bitbang_execute_queue,
582 };
583
584 struct adapter_driver linuxgpiod_adapter_driver = {
585 .name = "linuxgpiod",
586 .transports = linuxgpiod_transport,
587 .commands = linuxgpiod_command_handlers,
588
589 .init = linuxgpiod_init,
590 .quit = linuxgpiod_quit,
591 .reset = linuxgpiod_reset,
592
593 .jtag_ops = &linuxgpiod_interface,
594 .swd_ops = &bitbang_swd,
595 };

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)