OpenOCD
linuxgpiod.c
Go to the documentation of this file.
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/adapter.h>
18 #include <jtag/interface.h>
19 #include <transport/transport.h>
20 #include "bitbang.h"
21 
24 
25 static int last_swclk;
26 static int last_swdio;
27 static bool last_stored;
28 static bool swdio_input;
29 
31 
32 /*
33  * Helper function to determine if gpio config is valid
34  *
35  * Assume here that there will be less than 10000 gpios per gpiochip, and less
36  * than 1000 gpiochips.
37  */
39 {
40  return adapter_gpio_config[idx].chip_num < 1000
41  && adapter_gpio_config[idx].gpio_num < 10000;
42 }
43 
44 /* Bitbang interface read of TDO */
46 {
47  int retval;
48 
49  retval = gpiod_line_get_value(gpiod_line[ADAPTER_GPIO_IDX_TDO]);
50  if (retval < 0) {
51  LOG_WARNING("reading tdo failed");
52  return 0;
53  }
54 
55  return retval ? BB_HIGH : BB_LOW;
56 }
57 
58 /*
59  * Bitbang interface write of TCK, TMS, TDI
60  *
61  * Seeing as this is the only function where the outputs are changed,
62  * we can cache the old value to avoid needlessly writing it.
63  */
64 static int linuxgpiod_write(int tck, int tms, int tdi)
65 {
66  static int last_tck;
67  static int last_tms;
68  static int last_tdi;
69 
70  static int first_time;
71 
72  int retval;
73 
74  if (!first_time) {
75  last_tck = !tck;
76  last_tms = !tms;
77  last_tdi = !tdi;
78  first_time = 1;
79  }
80 
81  if (tdi != last_tdi) {
82  retval = gpiod_line_set_value(gpiod_line[ADAPTER_GPIO_IDX_TDI], tdi);
83  if (retval < 0)
84  LOG_WARNING("writing tdi failed");
85  }
86 
87  if (tms != last_tms) {
88  retval = gpiod_line_set_value(gpiod_line[ADAPTER_GPIO_IDX_TMS], tms);
89  if (retval < 0)
90  LOG_WARNING("writing tms failed");
91  }
92 
93  /* write clk last */
94  if (tck != last_tck) {
95  retval = gpiod_line_set_value(gpiod_line[ADAPTER_GPIO_IDX_TCK], tck);
96  if (retval < 0)
97  LOG_WARNING("writing tck failed");
98  }
99 
100  last_tdi = tdi;
101  last_tms = tms;
102  last_tck = tck;
103 
104  return ERROR_OK;
105 }
106 
107 static int linuxgpiod_swdio_read(void)
108 {
109  int retval;
110 
111  retval = gpiod_line_get_value(gpiod_line[ADAPTER_GPIO_IDX_SWDIO]);
112  if (retval < 0) {
113  LOG_WARNING("Fail read swdio");
114  return 0;
115  }
116 
117  return retval;
118 }
119 
120 static void linuxgpiod_swdio_drive(bool is_output)
121 {
122  int retval;
123 
124  /*
125  * FIXME: change direction requires release and re-require the line
126  * https://stackoverflow.com/questions/58735140/
127  * this would change in future libgpiod
128  */
129  gpiod_line_release(gpiod_line[ADAPTER_GPIO_IDX_SWDIO]);
130 
131  if (is_output) {
133  retval = gpiod_line_set_value(gpiod_line[ADAPTER_GPIO_IDX_SWDIO_DIR], 1);
134  if (retval < 0)
135  LOG_WARNING("Fail set swdio_dir");
136  }
137  retval = gpiod_line_request_output(gpiod_line[ADAPTER_GPIO_IDX_SWDIO], "OpenOCD", 1);
138  if (retval < 0)
139  LOG_WARNING("Fail request_output line swdio");
140  } else {
141  retval = gpiod_line_request_input(gpiod_line[ADAPTER_GPIO_IDX_SWDIO], "OpenOCD");
142  if (retval < 0)
143  LOG_WARNING("Fail request_input line swdio");
145  retval = gpiod_line_set_value(gpiod_line[ADAPTER_GPIO_IDX_SWDIO_DIR], 0);
146  if (retval < 0)
147  LOG_WARNING("Fail set swdio_dir");
148  }
149  }
150 
151  last_stored = false;
152  swdio_input = !is_output;
153 }
154 
155 static int linuxgpiod_swd_write(int swclk, int swdio)
156 {
157  int retval;
158 
159  if (!swdio_input) {
160  if (!last_stored || swdio != last_swdio) {
161  retval = gpiod_line_set_value(gpiod_line[ADAPTER_GPIO_IDX_SWDIO], swdio);
162  if (retval < 0)
163  LOG_WARNING("Fail set swdio");
164  }
165  }
166 
167  /* write swclk last */
168  if (!last_stored || swclk != last_swclk) {
169  retval = gpiod_line_set_value(gpiod_line[ADAPTER_GPIO_IDX_SWCLK], swclk);
170  if (retval < 0)
171  LOG_WARNING("Fail set swclk");
172  }
173 
174  last_swdio = swdio;
175  last_swclk = swclk;
176  last_stored = true;
177 
178  return ERROR_OK;
179 }
180 
181 static int linuxgpiod_blink(bool on)
182 {
183  int retval;
184 
186  return ERROR_OK;
187 
188  retval = gpiod_line_set_value(gpiod_line[ADAPTER_GPIO_IDX_LED], on ? 1 : 0);
189  if (retval < 0)
190  LOG_WARNING("Fail set led");
191  return retval;
192 }
193 
194 static struct bitbang_interface linuxgpiod_bitbang = {
196  .write = linuxgpiod_write,
197  .swdio_read = linuxgpiod_swdio_read,
198  .swdio_drive = linuxgpiod_swdio_drive,
199  .swd_write = linuxgpiod_swd_write,
200  .blink = linuxgpiod_blink,
201 };
202 
203 /*
204  * Bitbang interface to manipulate reset lines SRST and TRST
205  *
206  * (1) assert or (0) deassert reset lines
207  */
208 static int linuxgpiod_reset(int trst, int srst)
209 {
210  int retval1 = 0, retval2 = 0;
211 
212  LOG_DEBUG("linuxgpiod_reset");
213 
214  /*
215  * active low behaviour handled by "adaptor gpio" command and
216  * GPIOD_LINE_REQUEST_FLAG_ACTIVE_LOW flag when requesting the line.
217  */
219  retval1 = gpiod_line_set_value(gpiod_line[ADAPTER_GPIO_IDX_SRST], srst);
220  if (retval1 < 0)
221  LOG_WARNING("set srst value failed");
222  }
223 
225  retval2 = gpiod_line_set_value(gpiod_line[ADAPTER_GPIO_IDX_TRST], trst);
226  if (retval2 < 0)
227  LOG_WARNING("set trst value failed");
228  }
229 
230  return ((retval1 < 0) || (retval2 < 0)) ? ERROR_FAIL : ERROR_OK;
231 }
232 
234 {
236  return false;
238  return false;
240  return false;
242  return false;
243  return true;
244 }
245 
247 {
249  return false;
251  return false;
252  return true;
253 }
254 
255 static inline void helper_release(enum adapter_gpio_config_index idx)
256 {
257  if (gpiod_line[idx]) {
258  gpiod_line_release(gpiod_line[idx]);
259  gpiod_line[idx] = NULL;
260  }
261  if (gpiod_chip[idx]) {
262  gpiod_chip_close(gpiod_chip[idx]);
263  gpiod_chip[idx] = NULL;
264  }
265 }
266 
267 static int linuxgpiod_quit(void)
268 {
269  LOG_DEBUG("linuxgpiod_quit");
270  for (int i = 0; i < ADAPTER_GPIO_IDX_NUM; ++i)
271  helper_release(i);
272 
273  return ERROR_OK;
274 }
275 
277 {
278  if (!is_gpio_config_valid(idx))
279  return ERROR_OK;
280 
281  int dir = GPIOD_LINE_REQUEST_DIRECTION_INPUT, flags = 0, val = 0, retval;
282 
283  gpiod_chip[idx] = gpiod_chip_open_by_number(adapter_gpio_config[idx].chip_num);
284  if (!gpiod_chip[idx]) {
285  LOG_ERROR("Cannot open LinuxGPIOD chip %d for %s", adapter_gpio_config[idx].chip_num,
286  adapter_gpio_get_name(idx));
287  return ERROR_JTAG_INIT_FAILED;
288  }
289 
290  gpiod_line[idx] = gpiod_chip_get_line(gpiod_chip[idx], adapter_gpio_config[idx].gpio_num);
291  if (!gpiod_line[idx]) {
292  LOG_ERROR("Error get line %s", adapter_gpio_get_name(idx));
293  return ERROR_JTAG_INIT_FAILED;
294  }
295 
296  switch (adapter_gpio_config[idx].init_state) {
298  dir = GPIOD_LINE_REQUEST_DIRECTION_INPUT;
299  break;
301  dir = GPIOD_LINE_REQUEST_DIRECTION_OUTPUT;
302  val = 0;
303  break;
305  dir = GPIOD_LINE_REQUEST_DIRECTION_OUTPUT;
306  val = 1;
307  break;
308  }
309 
310  switch (adapter_gpio_config[idx].drive) {
312  break;
314  flags |= GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN;
315  break;
317  flags |= GPIOD_LINE_REQUEST_FLAG_OPEN_SOURCE;
318  break;
319  }
320 
321  switch (adapter_gpio_config[idx].pull) {
323 #ifdef HAVE_LIBGPIOD1_FLAGS_BIAS
324  flags |= GPIOD_LINE_REQUEST_FLAG_BIAS_DISABLE;
325 #endif
326  break;
328 #ifdef HAVE_LIBGPIOD1_FLAGS_BIAS
329  flags |= GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP;
330 #else
331  LOG_WARNING("linuxgpiod: ignoring request for pull-up on %s: not supported by gpiod v%s",
332  adapter_gpio_get_name(idx), gpiod_version_string());
333 #endif
334  break;
336 #ifdef HAVE_LIBGPIOD1_FLAGS_BIAS
337  flags |= GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_DOWN;
338 #else
339  LOG_WARNING("linuxgpiod: ignoring request for pull-down on %s: not supported by gpiod v%s",
340  adapter_gpio_get_name(idx), gpiod_version_string());
341 #endif
342  break;
343  }
344 
345  if (adapter_gpio_config[idx].active_low)
346  flags |= GPIOD_LINE_REQUEST_FLAG_ACTIVE_LOW;
347 
348  struct gpiod_line_request_config config = {
349  .consumer = "OpenOCD",
350  .request_type = dir,
351  .flags = flags,
352  };
353 
354  retval = gpiod_line_request(gpiod_line[idx], &config, val);
355  if (retval < 0) {
356  LOG_ERROR("Error requesting gpio line %s", adapter_gpio_get_name(idx));
357  return ERROR_JTAG_INIT_FAILED;
358  }
359 
360  return ERROR_OK;
361 }
362 
363 static int linuxgpiod_init(void)
364 {
365  LOG_INFO("Linux GPIOD JTAG/SWD bitbang driver");
366 
369 
370  /*
371  * Configure JTAG/SWD signals. Default directions and initial states are handled
372  * by adapter.c and "adapter gpio" command.
373  */
374 
375  if (transport_is_jtag()) {
377  LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode");
378  goto out_error;
379  }
380 
386  goto out_error;
387  }
388 
389  if (transport_is_swd()) {
390  int retval1, retval2;
392  LOG_ERROR("Require swclk and swdio gpio for SWD mode");
393  goto out_error;
394  }
395 
396  /*
397  * swdio and its buffer should be initialized in the order that prevents
398  * two outputs from being connected together. This will occur if the
399  * swdio GPIO is configured as an output while the external buffer is
400  * configured to send the swdio signal from the target to the GPIO.
401  */
405  } else {
408  }
409  if (retval1 != ERROR_OK || retval2 != ERROR_OK)
410  goto out_error;
411 
413  goto out_error;
414  }
415 
418  goto out_error;
419 
420  return ERROR_OK;
421 
422 out_error:
423  linuxgpiod_quit();
424 
425  return ERROR_JTAG_INIT_FAILED;
426 }
427 
428 static const char *const linuxgpiod_transport[] = { "swd", "jtag", NULL };
429 
430 static struct jtag_interface linuxgpiod_interface = {
432  .execute_queue = bitbang_execute_queue,
433 };
434 
436  .name = "linuxgpiod",
437  .transports = linuxgpiod_transport,
438 
439  .init = linuxgpiod_init,
440  .quit = linuxgpiod_quit,
441  .reset = linuxgpiod_reset,
442 
443  .jtag_ops = &linuxgpiod_interface,
444  .swd_ops = &bitbang_swd,
445 };
const struct adapter_gpio_config * adapter_gpio_get_config(void)
Retrieves gpio configuration set with command "adapter gpio <signal_name>".
Definition: adapter.c:1224
const char * adapter_gpio_get_name(enum adapter_gpio_config_index idx)
Retrieves gpio name.
Definition: adapter.c:1218
@ ADAPTER_GPIO_INIT_STATE_ACTIVE
Definition: adapter.h:32
@ ADAPTER_GPIO_INIT_STATE_INPUT
Definition: adapter.h:33
@ ADAPTER_GPIO_INIT_STATE_INACTIVE
Definition: adapter.h:31
adapter_gpio_config_index
Adapter GPIO.
Definition: adapter.h:44
@ ADAPTER_GPIO_IDX_LED
Definition: adapter.h:54
@ ADAPTER_GPIO_IDX_NUM
Definition: adapter.h:55
@ ADAPTER_GPIO_IDX_SWCLK
Definition: adapter.h:52
@ ADAPTER_GPIO_IDX_SWDIO_DIR
Definition: adapter.h:51
@ ADAPTER_GPIO_IDX_SRST
Definition: adapter.h:53
@ ADAPTER_GPIO_IDX_TRST
Definition: adapter.h:49
@ ADAPTER_GPIO_IDX_TDI
Definition: adapter.h:46
@ ADAPTER_GPIO_IDX_TMS
Definition: adapter.h:47
@ ADAPTER_GPIO_IDX_TCK
Definition: adapter.h:48
@ ADAPTER_GPIO_IDX_TDO
Definition: adapter.h:45
@ ADAPTER_GPIO_IDX_SWDIO
Definition: adapter.h:50
@ ADAPTER_GPIO_PULL_UP
Definition: adapter.h:39
@ ADAPTER_GPIO_PULL_DOWN
Definition: adapter.h:40
@ ADAPTER_GPIO_PULL_NONE
Definition: adapter.h:38
@ ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE
Definition: adapter.h:19
@ ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN
Definition: adapter.h:18
@ ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL
Definition: adapter.h:17
bool transport_is_swd(void)
Returns true if the current debug session is using SWD as its transport.
Definition: adi_v5_swd.c:776
static int last_tms
Definition: arm-jtag-ew.c:524
int bitbang_execute_queue(struct jtag_command *cmd_queue)
Definition: bitbang.c:293
const struct swd_driver bitbang_swd
Definition: bitbang.c:614
bb_value_t
Definition: bitbang.h:17
@ BB_LOW
Definition: bitbang.h:18
@ BB_HIGH
Definition: bitbang.h:19
#define DEBUG_CAP_TMS_SEQ
Definition: interface.h:187
bool transport_is_jtag(void)
Returns true if the current debug session is using JTAG as its transport.
Definition: jtag/core.c:1833
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:552
static int linuxgpiod_quit(void)
Definition: linuxgpiod.c:267
static bool last_stored
Definition: linuxgpiod.c:27
static int linuxgpiod_blink(bool on)
Definition: linuxgpiod.c:181
struct adapter_driver linuxgpiod_adapter_driver
Definition: linuxgpiod.c:435
static const char *const linuxgpiod_transport[]
Definition: linuxgpiod.c:428
static bb_value_t linuxgpiod_read(void)
Definition: linuxgpiod.c:45
static void helper_release(enum adapter_gpio_config_index idx)
Definition: linuxgpiod.c:255
static int last_swclk
Definition: linuxgpiod.c:25
static bool linuxgpiod_jtag_mode_possible(void)
Definition: linuxgpiod.c:233
static struct bitbang_interface linuxgpiod_bitbang
Definition: linuxgpiod.c:194
static bool is_gpio_config_valid(enum adapter_gpio_config_index idx)
Definition: linuxgpiod.c:38
static struct gpiod_line * gpiod_line[ADAPTER_GPIO_IDX_NUM]
Definition: linuxgpiod.c:23
static struct jtag_interface linuxgpiod_interface
Definition: linuxgpiod.c:430
static int linuxgpiod_swdio_read(void)
Definition: linuxgpiod.c:107
static int helper_get_line(enum adapter_gpio_config_index idx)
Definition: linuxgpiod.c:276
static const struct adapter_gpio_config * adapter_gpio_config
Definition: linuxgpiod.c:30
static void linuxgpiod_swdio_drive(bool is_output)
Definition: linuxgpiod.c:120
static int linuxgpiod_write(int tck, int tms, int tdi)
Definition: linuxgpiod.c:64
static int last_swdio
Definition: linuxgpiod.c:26
static bool swdio_input
Definition: linuxgpiod.c:28
static struct gpiod_chip * gpiod_chip[ADAPTER_GPIO_IDX_NUM]
Definition: linuxgpiod.c:22
static int linuxgpiod_swd_write(int swclk, int swdio)
Definition: linuxgpiod.c:155
static int linuxgpiod_reset(int trst, int srst)
Definition: linuxgpiod.c:208
static int linuxgpiod_init(void)
Definition: linuxgpiod.c:363
static bool linuxgpiod_swd_mode_possible(void)
Definition: linuxgpiod.c:246
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_INFO(expr ...)
Definition: log.h:126
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
Represents a driver for a debugging interface.
Definition: interface.h:207
const char *const name
The name of the interface driver.
Definition: interface.h:209
Configuration options for a single GPIO.
Definition: adapter.h:59
unsigned int gpio_num
Definition: adapter.h:60
unsigned int chip_num
Definition: adapter.h:61
Low level callbacks (for bitbang).
Definition: bitbang.h:30
bb_value_t(* read)(void)
Sample TDO and return the value.
Definition: bitbang.h:32
Represents a driver for a debugging interface.
Definition: interface.h:182
unsigned int supported
Bit vector listing capabilities exposed by this driver.
Definition: interface.h:186
#define NULL
Definition: usb.h:16