75f6152bec56f4adf7b88b4ba57b55748d4a69af
[fw/openocd] / 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                         if (jtag_get_reset_config() & RESET_TRST_OPEN_DRAIN)
364                                 gpiod_trst = helper_get_open_drain_output_line("trst", trst_gpio, 1);
365                         else
366                                 gpiod_trst = helper_get_output_line("trst", trst_gpio, 1);
367
368                         if (!gpiod_trst)
369                                 goto out_error;
370                 }
371         }
372
373         if (transport_is_swd()) {
374                 if (!linuxgpiod_swd_mode_possible()) {
375                         LOG_ERROR("Require swclk and swdio gpio for SWD mode");
376                         goto out_error;
377                 }
378
379                 gpiod_swclk = helper_get_output_line("swclk", swclk_gpio, 1);
380                 if (!gpiod_swclk)
381                         goto out_error;
382
383                 gpiod_swdio = helper_get_output_line("swdio", swdio_gpio, 1);
384                 if (!gpiod_swdio)
385                         goto out_error;
386         }
387
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);
391                 else
392                         gpiod_srst = helper_get_open_drain_output_line("srst", srst_gpio, 1);
393
394                 if (!gpiod_srst)
395                         goto out_error;
396         }
397
398         if (is_gpio_valid(led_gpio)) {
399                 gpiod_led = helper_get_output_line("led", led_gpio, 0);
400                 if (!gpiod_led)
401                         goto out_error;
402         }
403
404         return ERROR_OK;
405
406 out_error:
407         linuxgpiod_quit();
408
409         return ERROR_JTAG_INIT_FAILED;
410 }
411
412 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionums)
413 {
414         if (CMD_ARGC == 4) {
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;
421         }
422
423         command_print(CMD,
424                         "LinuxGPIOD nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
425                         tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
426
427         return ERROR_OK;
428 }
429
430 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tck)
431 {
432         if (CMD_ARGC == 1)
433                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
434
435         command_print(CMD, "LinuxGPIOD num: tck = %d", tck_gpio);
436         return ERROR_OK;
437 }
438
439 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tms)
440 {
441         if (CMD_ARGC == 1)
442                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
443
444         command_print(CMD, "LinuxGPIOD num: tms = %d", tms_gpio);
445         return ERROR_OK;
446 }
447
448 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdo)
449 {
450         if (CMD_ARGC == 1)
451                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
452
453         command_print(CMD, "LinuxGPIOD num: tdo = %d", tdo_gpio);
454         return ERROR_OK;
455 }
456
457 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdi)
458 {
459         if (CMD_ARGC == 1)
460                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
461
462         command_print(CMD, "LinuxGPIOD num: tdi = %d", tdi_gpio);
463         return ERROR_OK;
464 }
465
466 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_srst)
467 {
468         if (CMD_ARGC == 1)
469                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
470
471         command_print(CMD, "LinuxGPIOD num: srst = %d", srst_gpio);
472         return ERROR_OK;
473 }
474
475 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_trst)
476 {
477         if (CMD_ARGC == 1)
478                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
479
480         command_print(CMD, "LinuxGPIOD num: trst = %d", trst_gpio);
481         return ERROR_OK;
482 }
483
484 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionums)
485 {
486         if (CMD_ARGC == 2) {
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;
491         }
492
493         command_print(CMD,
494                         "LinuxGPIOD nums: swclk = %d, swdio = %d",
495                         swclk_gpio, swdio_gpio);
496
497         return ERROR_OK;
498 }
499
500 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swclk)
501 {
502         if (CMD_ARGC == 1)
503                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
504
505         command_print(CMD, "LinuxGPIOD num: swclk = %d", swclk_gpio);
506         return ERROR_OK;
507 }
508
509 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swdio)
510 {
511         if (CMD_ARGC == 1)
512                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpio);
513
514         command_print(CMD, "LinuxGPIOD num: swdio = %d", swdio_gpio);
515         return ERROR_OK;
516 }
517
518 COMMAND_HANDLER(linuxgpiod_handle_gpionum_led)
519 {
520         if (CMD_ARGC == 1)
521                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], led_gpio);
522
523         command_print(CMD, "LinuxGPIOD num: led = %d", led_gpio);
524         return ERROR_OK;
525 }
526
527 COMMAND_HANDLER(linuxgpiod_handle_gpiochip)
528 {
529         if (CMD_ARGC == 1)
530                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], gpiochip);
531
532         command_print(CMD, "LinuxGPIOD gpiochip = %d", gpiochip);
533         return ERROR_OK;
534 }
535
536 static const struct command_registration linuxgpiod_subcommand_handlers[] = {
537         {
538                 .name = "jtag_nums",
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",
543         },
544         {
545                 .name = "tck_num",
546                 .handler = linuxgpiod_handle_jtag_gpionum_tck,
547                 .mode = COMMAND_CONFIG,
548                 .help = "gpio number for tck.",
549                 .usage = "tck",
550         },
551         {
552                 .name = "tms_num",
553                 .handler = linuxgpiod_handle_jtag_gpionum_tms,
554                 .mode = COMMAND_CONFIG,
555                 .help = "gpio number for tms.",
556                 .usage = "tms",
557         },
558         {
559                 .name = "tdo_num",
560                 .handler = linuxgpiod_handle_jtag_gpionum_tdo,
561                 .mode = COMMAND_CONFIG,
562                 .help = "gpio number for tdo.",
563                 .usage = "tdo",
564         },
565         {
566                 .name = "tdi_num",
567                 .handler = linuxgpiod_handle_jtag_gpionum_tdi,
568                 .mode = COMMAND_CONFIG,
569                 .help = "gpio number for tdi.",
570                 .usage = "tdi",
571         },
572         {
573                 .name = "srst_num",
574                 .handler = linuxgpiod_handle_jtag_gpionum_srst,
575                 .mode = COMMAND_CONFIG,
576                 .help = "gpio number for srst.",
577                 .usage = "srst",
578         },
579         {
580                 .name = "trst_num",
581                 .handler = linuxgpiod_handle_jtag_gpionum_trst,
582                 .mode = COMMAND_CONFIG,
583                 .help = "gpio number for trst.",
584                 .usage = "trst",
585         },
586         {
587                 .name = "swd_nums",
588                 .handler = linuxgpiod_handle_swd_gpionums,
589                 .mode = COMMAND_CONFIG,
590                 .help = "gpio numbers for swclk, swdio. (in that order)",
591                 .usage = "swclk swdio",
592         },
593         {
594                 .name = "swclk_num",
595                 .handler = linuxgpiod_handle_swd_gpionum_swclk,
596                 .mode = COMMAND_CONFIG,
597                 .help = "gpio number for swclk.",
598                 .usage = "swclk",
599         },
600         {
601                 .name = "swdio_num",
602                 .handler = linuxgpiod_handle_swd_gpionum_swdio,
603                 .mode = COMMAND_CONFIG,
604                 .help = "gpio number for swdio.",
605                 .usage = "swdio",
606         },
607         {
608                 .name = "led_num",
609                 .handler = linuxgpiod_handle_gpionum_led,
610                 .mode = COMMAND_CONFIG,
611                 .help = "gpio number for LED.",
612                 .usage = "led",
613         },
614         {
615                 .name = "gpiochip",
616                 .handler = linuxgpiod_handle_gpiochip,
617                 .mode = COMMAND_CONFIG,
618                 .help = "number of the gpiochip.",
619                 .usage = "gpiochip",
620         },
621         COMMAND_REGISTRATION_DONE
622 };
623
624 static const struct command_registration linuxgpiod_command_handlers[] = {
625         {
626                 .name = "linuxgpiod",
627                 .mode = COMMAND_ANY,
628                 .help = "perform linuxgpiod management",
629                 .chain = linuxgpiod_subcommand_handlers,
630                 .usage = "",
631         },
632         COMMAND_REGISTRATION_DONE
633 };
634
635 static const char *const linuxgpiod_transport[] = { "swd", "jtag", NULL };
636
637 static struct jtag_interface linuxgpiod_interface = {
638         .supported = DEBUG_CAP_TMS_SEQ,
639         .execute_queue = bitbang_execute_queue,
640 };
641
642 struct adapter_driver linuxgpiod_adapter_driver = {
643         .name = "linuxgpiod",
644         .transports = linuxgpiod_transport,
645         .commands = linuxgpiod_command_handlers,
646
647         .init = linuxgpiod_init,
648         .quit = linuxgpiod_quit,
649         .reset = linuxgpiod_reset,
650
651         .jtag_ops = &linuxgpiod_interface,
652         .swd_ops = &bitbang_swd,
653 };