flash/stm32l4x: fix dual bank support for STM32L552xC devices
[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_input_line(const char *label, unsigned int offset)
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 retval = gpiod_line_request_input(line, "OpenOCD");
293 if (retval < 0) {
294 LOG_ERROR("Error request_input line %s", label);
295 return NULL;
296 }
297
298 return line;
299 }
300
301 static struct gpiod_line *helper_get_output_line(const char *label, unsigned int offset, int val)
302 {
303 struct gpiod_line *line;
304 int retval;
305
306 line = gpiod_chip_get_line(gpiod_chip, offset);
307 if (!line) {
308 LOG_ERROR("Error get line %s", label);
309 return NULL;
310 }
311
312 retval = gpiod_line_request_output(line, "OpenOCD", val);
313 if (retval < 0) {
314 LOG_ERROR("Error request_output line %s", label);
315 return NULL;
316 }
317
318 return line;
319 }
320
321 static int linuxgpiod_init(void)
322 {
323 LOG_INFO("Linux GPIOD JTAG/SWD bitbang driver");
324
325 bitbang_interface = &linuxgpiod_bitbang;
326
327 gpiod_chip = gpiod_chip_open_by_number(gpiochip);
328 if (!gpiod_chip) {
329 LOG_ERROR("Cannot open LinuxGPIOD gpiochip %d", gpiochip);
330 return ERROR_JTAG_INIT_FAILED;
331 }
332
333 /*
334 * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
335 * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
336 * For SWD, SWCLK and SWDIO are configures as output high.
337 */
338
339 if (transport_is_jtag()) {
340 if (!linuxgpiod_jtag_mode_possible()) {
341 LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode");
342 goto out_error;
343 }
344
345 gpiod_tdo = helper_get_input_line("tdo", tdo_gpio);
346 if (!gpiod_tdo)
347 goto out_error;
348
349 gpiod_tdi = helper_get_output_line("tdi", tdi_gpio, 0);
350 if (!gpiod_tdi)
351 goto out_error;
352
353 gpiod_tck = helper_get_output_line("tck", tck_gpio, 0);
354 if (!gpiod_tck)
355 goto out_error;
356
357 gpiod_tms = helper_get_output_line("tms", tms_gpio, 1);
358 if (!gpiod_tms)
359 goto out_error;
360
361 if (is_gpio_valid(trst_gpio)) {
362 gpiod_trst = helper_get_output_line("trst", trst_gpio, 1);
363 if (!gpiod_trst)
364 goto out_error;
365 }
366 }
367
368 if (transport_is_swd()) {
369 if (!linuxgpiod_swd_mode_possible()) {
370 LOG_ERROR("Require swclk and swdio gpio for SWD mode");
371 goto out_error;
372 }
373
374 gpiod_swclk = helper_get_output_line("swclk", swclk_gpio, 1);
375 if (!gpiod_swclk)
376 goto out_error;
377
378 gpiod_swdio = helper_get_output_line("swdio", swdio_gpio, 1);
379 if (!gpiod_swdio)
380 goto out_error;
381 }
382
383 if (is_gpio_valid(srst_gpio)) {
384 gpiod_srst = helper_get_output_line("srst", srst_gpio, 1);
385 if (!gpiod_srst)
386 goto out_error;
387 }
388
389 if (is_gpio_valid(led_gpio)) {
390 gpiod_led = helper_get_output_line("led", led_gpio, 0);
391 if (!gpiod_led)
392 goto out_error;
393 }
394
395 return ERROR_OK;
396
397 out_error:
398 linuxgpiod_quit();
399
400 return ERROR_JTAG_INIT_FAILED;
401 }
402
403 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionums)
404 {
405 if (CMD_ARGC == 4) {
406 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
407 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpio);
408 COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], tdi_gpio);
409 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], tdo_gpio);
410 } else if (CMD_ARGC != 0) {
411 return ERROR_COMMAND_SYNTAX_ERROR;
412 }
413
414 command_print(CMD,
415 "LinuxGPIOD nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
416 tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
417
418 return ERROR_OK;
419 }
420
421 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tck)
422 {
423 if (CMD_ARGC == 1)
424 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
425
426 command_print(CMD, "LinuxGPIOD num: tck = %d", tck_gpio);
427 return ERROR_OK;
428 }
429
430 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tms)
431 {
432 if (CMD_ARGC == 1)
433 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
434
435 command_print(CMD, "LinuxGPIOD num: tms = %d", tms_gpio);
436 return ERROR_OK;
437 }
438
439 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdo)
440 {
441 if (CMD_ARGC == 1)
442 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
443
444 command_print(CMD, "LinuxGPIOD num: tdo = %d", tdo_gpio);
445 return ERROR_OK;
446 }
447
448 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdi)
449 {
450 if (CMD_ARGC == 1)
451 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
452
453 command_print(CMD, "LinuxGPIOD num: tdi = %d", tdi_gpio);
454 return ERROR_OK;
455 }
456
457 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_srst)
458 {
459 if (CMD_ARGC == 1)
460 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
461
462 command_print(CMD, "LinuxGPIOD num: srst = %d", srst_gpio);
463 return ERROR_OK;
464 }
465
466 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_trst)
467 {
468 if (CMD_ARGC == 1)
469 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
470
471 command_print(CMD, "LinuxGPIOD num: trst = %d", trst_gpio);
472 return ERROR_OK;
473 }
474
475 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionums)
476 {
477 if (CMD_ARGC == 2) {
478 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
479 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], swdio_gpio);
480 } else if (CMD_ARGC != 0) {
481 return ERROR_COMMAND_SYNTAX_ERROR;
482 }
483
484 command_print(CMD,
485 "LinuxGPIOD nums: swclk = %d, swdio = %d",
486 swclk_gpio, swdio_gpio);
487
488 return ERROR_OK;
489 }
490
491 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swclk)
492 {
493 if (CMD_ARGC == 1)
494 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
495
496 command_print(CMD, "LinuxGPIOD num: swclk = %d", swclk_gpio);
497 return ERROR_OK;
498 }
499
500 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swdio)
501 {
502 if (CMD_ARGC == 1)
503 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpio);
504
505 command_print(CMD, "LinuxGPIOD num: swdio = %d", swdio_gpio);
506 return ERROR_OK;
507 }
508
509 COMMAND_HANDLER(linuxgpiod_handle_gpionum_led)
510 {
511 if (CMD_ARGC == 1)
512 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], led_gpio);
513
514 command_print(CMD, "LinuxGPIOD num: led = %d", led_gpio);
515 return ERROR_OK;
516 }
517
518 COMMAND_HANDLER(linuxgpiod_handle_gpiochip)
519 {
520 if (CMD_ARGC == 1)
521 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], gpiochip);
522
523 command_print(CMD, "LinuxGPIOD gpiochip = %d", gpiochip);
524 return ERROR_OK;
525 }
526
527 static const struct command_registration linuxgpiod_subcommand_handlers[] = {
528 {
529 .name = "jtag_nums",
530 .handler = linuxgpiod_handle_jtag_gpionums,
531 .mode = COMMAND_CONFIG,
532 .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
533 .usage = "tck tms tdi tdo",
534 },
535 {
536 .name = "tck_num",
537 .handler = linuxgpiod_handle_jtag_gpionum_tck,
538 .mode = COMMAND_CONFIG,
539 .help = "gpio number for tck.",
540 .usage = "tck",
541 },
542 {
543 .name = "tms_num",
544 .handler = linuxgpiod_handle_jtag_gpionum_tms,
545 .mode = COMMAND_CONFIG,
546 .help = "gpio number for tms.",
547 .usage = "tms",
548 },
549 {
550 .name = "tdo_num",
551 .handler = linuxgpiod_handle_jtag_gpionum_tdo,
552 .mode = COMMAND_CONFIG,
553 .help = "gpio number for tdo.",
554 .usage = "tdo",
555 },
556 {
557 .name = "tdi_num",
558 .handler = linuxgpiod_handle_jtag_gpionum_tdi,
559 .mode = COMMAND_CONFIG,
560 .help = "gpio number for tdi.",
561 .usage = "tdi",
562 },
563 {
564 .name = "srst_num",
565 .handler = linuxgpiod_handle_jtag_gpionum_srst,
566 .mode = COMMAND_CONFIG,
567 .help = "gpio number for srst.",
568 .usage = "srst",
569 },
570 {
571 .name = "trst_num",
572 .handler = linuxgpiod_handle_jtag_gpionum_trst,
573 .mode = COMMAND_CONFIG,
574 .help = "gpio number for trst.",
575 .usage = "trst",
576 },
577 {
578 .name = "swd_nums",
579 .handler = linuxgpiod_handle_swd_gpionums,
580 .mode = COMMAND_CONFIG,
581 .help = "gpio numbers for swclk, swdio. (in that order)",
582 .usage = "swclk swdio",
583 },
584 {
585 .name = "swclk_num",
586 .handler = linuxgpiod_handle_swd_gpionum_swclk,
587 .mode = COMMAND_CONFIG,
588 .help = "gpio number for swclk.",
589 .usage = "swclk",
590 },
591 {
592 .name = "swdio_num",
593 .handler = linuxgpiod_handle_swd_gpionum_swdio,
594 .mode = COMMAND_CONFIG,
595 .help = "gpio number for swdio.",
596 .usage = "swdio",
597 },
598 {
599 .name = "led_num",
600 .handler = linuxgpiod_handle_gpionum_led,
601 .mode = COMMAND_CONFIG,
602 .help = "gpio number for LED.",
603 .usage = "led",
604 },
605 {
606 .name = "gpiochip",
607 .handler = linuxgpiod_handle_gpiochip,
608 .mode = COMMAND_CONFIG,
609 .help = "number of the gpiochip.",
610 .usage = "gpiochip",
611 },
612 COMMAND_REGISTRATION_DONE
613 };
614
615 static const struct command_registration linuxgpiod_command_handlers[] = {
616 {
617 .name = "linuxgpiod",
618 .mode = COMMAND_ANY,
619 .help = "perform linuxgpiod management",
620 .chain = linuxgpiod_subcommand_handlers,
621 .usage = "",
622 },
623 COMMAND_REGISTRATION_DONE
624 };
625
626 static const char *const linuxgpiod_transport[] = { "swd", "jtag", NULL };
627
628 static struct jtag_interface linuxgpiod_interface = {
629 .supported = DEBUG_CAP_TMS_SEQ,
630 .execute_queue = bitbang_execute_queue,
631 };
632
633 struct adapter_driver linuxgpiod_adapter_driver = {
634 .name = "linuxgpiod",
635 .transports = linuxgpiod_transport,
636 .commands = linuxgpiod_command_handlers,
637
638 .init = linuxgpiod_init,
639 .quit = linuxgpiod_quit,
640 .reset = linuxgpiod_reset,
641
642 .jtag_ops = &linuxgpiod_interface,
643 .swd_ops = &bitbang_swd,
644 };

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)