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