jtag_interface: .speed can be NULL when not needed
[openocd.git] / src / jtag / drivers / sysfsgpio.c
blob283ec445a2c870b4d7938853063425ef056105e6
1 /***************************************************************************
2 * Copyright (C) 2012 by Creative Product Design, marc @ cpdesign.com.au *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19 /**
20 * @file
21 * This driver implements a bitbang jtag interface using gpio lines via
22 * sysfs.
23 * The aim of this driver implementation is use system GPIOs but avoid the
24 * need for a additional kernel driver.
25 * (Note memory mapped IO is another option, however it doesn't mix well with
26 * the kernel gpiolib driver - which makes sense I guess.)
28 * A gpio is required for tck, tms, tdi and tdo. One or both of srst and trst
29 * must be also be specified. The required jtag gpios are specified via the
30 * sysfsgpio_jtag_nums command or the relevant sysfsgpio_XXX_num commang.
31 * The srst and trst gpios are set via the sysfsgpio_srst_num and
32 * sysfsgpio_trst_num respectively. GPIO numbering follows the kernel
33 * convention of starting from 0.
35 * The gpios should not be in use by another entity, and must not be requested
36 * by a kernel driver without also being exported by it (otherwise they can't
37 * be exported by sysfs).
39 * The sysfs gpio interface can only manipulate one gpio at a time, so the
40 * bitbang write handler remembers the last state for tck, tms, tdi to avoid
41 * superfluous writes.
42 * For speed the sysfs "value" entry is opened at init and held open.
43 * This results in considerable gains over open-write-close (45s vs 900s)
45 * Further work could address:
46 * -srst and trst open drain/ push pull
47 * -configurable active high/low for srst & trst
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
53 #include <jtag/interface.h>
54 #include "bitbang.h"
57 * Helper func to determine if gpio number valid
59 * Assume here that there will be less than 1000 gpios on a system
61 static int is_gpio_valid(int gpio)
63 return gpio >= 0 && gpio < 1000;
67 * Helper func to open, write to and close a file
68 * name and valstr must be null terminated.
70 * Returns negative on failure.
72 static int open_write_close(const char *name, const char *valstr)
74 int ret;
75 int fd = open(name, O_WRONLY);
76 if (fd < 0)
77 return fd;
79 ret = write(fd, valstr, strlen(valstr));
80 close(fd);
82 return ret;
86 * Helper func to unexport gpio from sysfs
88 static void unexport_sysfs_gpio(int gpio)
90 char gpiostr[4];
92 if (!is_gpio_valid(gpio))
93 return;
95 snprintf(gpiostr, sizeof(gpiostr), "%d", gpio);
96 if (open_write_close("/sys/class/gpio/unexport", gpiostr) < 0)
97 LOG_ERROR("Couldn't unexport gpio %d", gpio);
99 return;
103 * Exports and sets up direction for gpio.
104 * If the gpio is an output, it is initialized according to init_high,
105 * otherwise it is ignored.
107 * If the gpio is already exported we just show a warning and continue; if
108 * openocd happened to crash (or was killed by user) then the gpios will not
109 * have been cleaned up.
111 static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
113 char buf[40];
114 char gpiostr[4];
115 int ret;
117 if (!is_gpio_valid(gpio))
118 return ERROR_OK;
120 snprintf(gpiostr, sizeof(gpiostr), "%d", gpio);
121 ret = open_write_close("/sys/class/gpio/export", gpiostr);
122 if (ret < 0) {
123 if (errno == EBUSY) {
124 LOG_WARNING("gpio %d is already exported", gpio);
125 } else {
126 LOG_ERROR("Couldn't export gpio %d", gpio);
127 perror("sysfsgpio: ");
128 return ERROR_FAIL;
132 snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
133 ret = open_write_close(buf, is_output ? (init_high ? "high" : "low") : "in");
134 if (ret < 0) {
135 LOG_ERROR("Couldn't set direction for gpio %d", gpio);
136 perror("sysfsgpio: ");
137 unexport_sysfs_gpio(gpio);
138 return ERROR_FAIL;
141 snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
142 if (is_output)
143 ret = open(buf, O_WRONLY | O_NONBLOCK | O_SYNC);
144 else
145 ret = open(buf, O_RDONLY | O_NONBLOCK | O_SYNC);
147 if (ret < 0)
148 unexport_sysfs_gpio(gpio);
150 return ret;
154 * file descriptors for /sys/class/gpio/gpioXX/value
155 * Set up during init.
157 static int tck_fd = -1;
158 static int tms_fd = -1;
159 static int tdi_fd = -1;
160 static int tdo_fd = -1;
161 static int trst_fd = -1;
162 static int srst_fd = -1;
165 * Bitbang interface read of TDO
167 * The sysfs value will read back either '0' or '1'. The trick here is to call
168 * lseek to bypass buffering in the sysfs kernel driver.
170 static int sysfsgpio_read(void)
172 char buf[1];
174 /* important to seek to signal sysfs of new read */
175 lseek(tdo_fd, 0, SEEK_SET);
176 int ret = read(tdo_fd, &buf, sizeof(buf));
178 if (ret < 0) {
179 LOG_WARNING("reading tdo failed");
180 return 0;
183 return buf[0] == '1';
187 * Bitbang interface write of TCK, TMS, TDI
189 * Seeing as this is the only function where the outputs are changed,
190 * we can cache the old value to avoid needlessly writing it.
192 static void sysfsgpio_write(int tck, int tms, int tdi)
194 const char one[] = "1";
195 const char zero[] = "0";
197 static int last_tck;
198 static int last_tms;
199 static int last_tdi;
201 static int first_time;
202 size_t bytes_written;
204 if (!first_time) {
205 last_tck = !tck;
206 last_tms = !tms;
207 last_tdi = !tdi;
208 first_time = 1;
211 if (tdi != last_tdi) {
212 bytes_written = write(tdi_fd, tdi ? &one : &zero, 1);
213 if (bytes_written != 1)
214 LOG_WARNING("writing tdi failed");
217 if (tms != last_tms) {
218 bytes_written = write(tms_fd, tms ? &one : &zero, 1);
219 if (bytes_written != 1)
220 LOG_WARNING("writing tms failed");
223 /* write clk last */
224 if (tck != last_tck) {
225 bytes_written = write(tck_fd, tck ? &one : &zero, 1);
226 if (bytes_written != 1)
227 LOG_WARNING("writing tck failed");
230 last_tdi = tdi;
231 last_tms = tms;
232 last_tck = tck;
236 * Bitbang interface to manipulate reset lines SRST and TRST
238 * (1) assert or (0) deassert reset lines
240 static void sysfsgpio_reset(int trst, int srst)
242 const char one[] = "1";
243 const char zero[] = "0";
244 size_t bytes_written;
246 /* assume active low */
247 if (srst_fd >= 0) {
248 bytes_written = write(srst_fd, srst ? &zero : &one, 1);
249 if (bytes_written != 1)
250 LOG_WARNING("writing srst failed");
253 /* assume active low */
254 if (trst_fd >= 0) {
255 bytes_written = write(trst_fd, trst ? &zero : &one, 1);
256 if (bytes_written != 1)
257 LOG_WARNING("writing trst failed");
261 /* gpio numbers for each gpio. Negative values are invalid */
262 static int tck_gpio = -1;
263 static int tms_gpio = -1;
264 static int tdi_gpio = -1;
265 static int tdo_gpio = -1;
266 static int trst_gpio = -1;
267 static int srst_gpio = -1;
269 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionums)
271 if (CMD_ARGC == 4) {
272 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
273 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpio);
274 COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], tdi_gpio);
275 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], tdo_gpio);
276 } else if (CMD_ARGC != 0) {
277 return ERROR_COMMAND_SYNTAX_ERROR;
280 command_print(CMD_CTX,
281 "SysfsGPIO nums: tck = %d, tms = %d, tdi = %d, tdi = %d",
282 tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
284 return ERROR_OK;
287 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tck)
289 if (CMD_ARGC == 1)
290 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
292 command_print(CMD_CTX, "SysfsGPIO num: tck = %d", tck_gpio);
293 return ERROR_OK;
296 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tms)
298 if (CMD_ARGC == 1)
299 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
301 command_print(CMD_CTX, "SysfsGPIO num: tms = %d", tms_gpio);
302 return ERROR_OK;
305 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tdo)
307 if (CMD_ARGC == 1)
308 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
310 command_print(CMD_CTX, "SysfsGPIO num: tdo = %d", tdo_gpio);
311 return ERROR_OK;
314 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tdi)
316 if (CMD_ARGC == 1)
317 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
319 command_print(CMD_CTX, "SysfsGPIO num: tdi = %d", tdi_gpio);
320 return ERROR_OK;
323 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_srst)
325 if (CMD_ARGC == 1)
326 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
328 command_print(CMD_CTX, "SysfsGPIO num: srst = %d", srst_gpio);
329 return ERROR_OK;
332 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_trst)
334 if (CMD_ARGC == 1)
335 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
337 command_print(CMD_CTX, "SysfsGPIO num: trst = %d", trst_gpio);
338 return ERROR_OK;
341 static const struct command_registration sysfsgpio_command_handlers[] = {
343 .name = "sysfsgpio_jtag_nums",
344 .handler = &sysfsgpio_handle_jtag_gpionums,
345 .mode = COMMAND_CONFIG,
346 .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
347 .usage = "(tck tms tdi tdo)* ",
350 .name = "sysfsgpio_tck_num",
351 .handler = &sysfsgpio_handle_jtag_gpionum_tck,
352 .mode = COMMAND_CONFIG,
353 .help = "gpio number for tck.",
356 .name = "sysfsgpio_tms_num",
357 .handler = &sysfsgpio_handle_jtag_gpionum_tms,
358 .mode = COMMAND_CONFIG,
359 .help = "gpio number for tms.",
362 .name = "sysfsgpio_tdo_num",
363 .handler = &sysfsgpio_handle_jtag_gpionum_tdo,
364 .mode = COMMAND_CONFIG,
365 .help = "gpio number for tdo.",
368 .name = "sysfsgpio_tdi_num",
369 .handler = &sysfsgpio_handle_jtag_gpionum_tdi,
370 .mode = COMMAND_CONFIG,
371 .help = "gpio number for tdi.",
374 .name = "sysfsgpio_srst_num",
375 .handler = &sysfsgpio_handle_jtag_gpionum_srst,
376 .mode = COMMAND_CONFIG,
377 .help = "gpio number for srst.",
380 .name = "sysfsgpio_trst_num",
381 .handler = &sysfsgpio_handle_jtag_gpionum_trst,
382 .mode = COMMAND_CONFIG,
383 .help = "gpio number for trst.",
385 COMMAND_REGISTRATION_DONE
388 static int sysfsgpio_init(void);
389 static int sysfsgpio_quit(void);
391 struct jtag_interface sysfsgpio_interface = {
392 .name = "sysfsgpio",
393 .supported = DEBUG_CAP_TMS_SEQ,
394 .execute_queue = bitbang_execute_queue,
395 .transports = jtag_only,
396 .commands = sysfsgpio_command_handlers,
397 .init = sysfsgpio_init,
398 .quit = sysfsgpio_quit,
401 static struct bitbang_interface sysfsgpio_bitbang = {
402 .read = sysfsgpio_read,
403 .write = sysfsgpio_write,
404 .reset = sysfsgpio_reset,
405 .blink = 0
408 /* helper func to close and cleanup files only if they were valid/ used */
409 static void cleanup_fd(int fd, int gpio)
411 if (gpio >= 0) {
412 if (fd >= 0)
413 close(fd);
415 unexport_sysfs_gpio(gpio);
419 static void cleanup_all_fds(void)
421 cleanup_fd(tck_fd, tck_gpio);
422 cleanup_fd(tms_fd, tms_gpio);
423 cleanup_fd(tdi_fd, tdi_gpio);
424 cleanup_fd(tdo_fd, tdo_gpio);
425 cleanup_fd(trst_fd, trst_gpio);
426 cleanup_fd(srst_fd, srst_gpio);
429 static int sysfsgpio_init(void)
431 bitbang_interface = &sysfsgpio_bitbang;
433 LOG_INFO("SysfsGPIO JTAG bitbang driver");
435 if (!(is_gpio_valid(tck_gpio)
436 && is_gpio_valid(tms_gpio)
437 && is_gpio_valid(tdi_gpio)
438 && is_gpio_valid(tdo_gpio))) {
439 if (!is_gpio_valid(tck_gpio))
440 LOG_ERROR("gpio num for tck is invalid");
441 if (!is_gpio_valid(tms_gpio))
442 LOG_ERROR("gpio num for tms is invalid");
443 if (!is_gpio_valid(tdo_gpio))
444 LOG_ERROR("gpio num for tdo is invalid");
445 if (!is_gpio_valid(tdi_gpio))
446 LOG_ERROR("gpio num for tdi is invalid");
448 LOG_ERROR("Require tck, tms, tdi and tdo gpios to all be specified");
449 return ERROR_JTAG_INIT_FAILED;
452 if (!is_gpio_valid(trst_gpio) && !is_gpio_valid(srst_gpio)) {
453 LOG_ERROR("Require at least one of trst or srst gpios to be specified");
454 return ERROR_JTAG_INIT_FAILED;
458 * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
459 * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
461 tck_fd = setup_sysfs_gpio(tck_gpio, 1, 0);
462 if (tck_fd < 0)
463 goto out_error;
465 tms_fd = setup_sysfs_gpio(tms_gpio, 1, 1);
466 if (tms_fd < 0)
467 goto out_error;
469 tdi_fd = setup_sysfs_gpio(tdi_gpio, 1, 0);
470 if (tdi_fd < 0)
471 goto out_error;
473 tdo_fd = setup_sysfs_gpio(tdo_gpio, 0, 0);
474 if (tdo_fd < 0)
475 goto out_error;
477 /* assume active low*/
478 trst_fd = setup_sysfs_gpio(trst_gpio, 1, 1);
479 if (trst_gpio > 0 && trst_fd < 0)
480 goto out_error;
482 /* assume active low*/
483 srst_fd = setup_sysfs_gpio(srst_gpio, 1, 1);
484 if (srst_gpio > 0 && srst_fd < 0)
485 goto out_error;
487 return ERROR_OK;
489 out_error:
490 cleanup_all_fds();
491 return ERROR_JTAG_INIT_FAILED;
494 static int sysfsgpio_quit(void)
496 cleanup_all_fds();
497 return ERROR_OK;