1 /***************************************************************************
2 * Copyright (C) 2012 by Creative Product Design, marc @ cpdesign.com.au *
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. *
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. *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
18 ***************************************************************************/
20 /* 2014-12: Addition of the SWD protocol support is based on the initial work
21 * on bcm2835gpio.c by Paul Fertser and modifications by Jean-Christian de Rivaz. */
25 * This driver implements a bitbang jtag interface using gpio lines via
27 * The aim of this driver implementation is use system GPIOs but avoid the
28 * need for a additional kernel driver.
29 * (Note memory mapped IO is another option, however it doesn't mix well with
30 * the kernel gpiolib driver - which makes sense I guess.)
32 * A gpio is required for tck, tms, tdi and tdo. One or both of srst and trst
33 * must be also be specified. The required jtag gpios are specified via the
34 * sysfsgpio_jtag_nums command or the relevant sysfsgpio_XXX_num commang.
35 * The srst and trst gpios are set via the sysfsgpio_srst_num and
36 * sysfsgpio_trst_num respectively. GPIO numbering follows the kernel
37 * convention of starting from 0.
39 * The gpios should not be in use by another entity, and must not be requested
40 * by a kernel driver without also being exported by it (otherwise they can't
41 * be exported by sysfs).
43 * The sysfs gpio interface can only manipulate one gpio at a time, so the
44 * bitbang write handler remembers the last state for tck, tms, tdi to avoid
46 * For speed the sysfs "value" entry is opened at init and held open.
47 * This results in considerable gains over open-write-close (45s vs 900s)
49 * Further work could address:
50 * -srst and trst open drain/ push pull
51 * -configurable active high/low for srst & trst
57 #include <jtag/interface.h>
61 * Helper func to determine if gpio number valid
63 * Assume here that there will be less than 1000 gpios on a system
65 static int is_gpio_valid(int gpio)
67 return gpio >= 0 && gpio < 1000;
71 * Helper func to open, write to and close a file
72 * name and valstr must be null terminated.
74 * Returns negative on failure.
76 static int open_write_close(const char *name, const char *valstr)
79 int fd = open(name, O_WRONLY);
83 ret = write(fd, valstr, strlen(valstr));
90 * Helper func to unexport gpio from sysfs
92 static void unexport_sysfs_gpio(int gpio)
96 if (!is_gpio_valid(gpio))
99 snprintf(gpiostr, sizeof(gpiostr), "%d", gpio);
100 if (open_write_close("/sys/class/gpio/unexport", gpiostr) < 0)
101 LOG_ERROR("Couldn't unexport gpio %d", gpio);
107 * Exports and sets up direction for gpio.
108 * If the gpio is an output, it is initialized according to init_high,
109 * otherwise it is ignored.
111 * If the gpio is already exported we just show a warning and continue; if
112 * openocd happened to crash (or was killed by user) then the gpios will not
113 * have been cleaned up.
115 static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
121 if (!is_gpio_valid(gpio))
124 snprintf(gpiostr, sizeof(gpiostr), "%d", gpio);
125 ret = open_write_close("/sys/class/gpio/export", gpiostr);
127 if (errno == EBUSY) {
128 LOG_WARNING("gpio %d is already exported", gpio);
130 LOG_ERROR("Couldn't export gpio %d", gpio);
131 perror("sysfsgpio: ");
136 snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
137 ret = open_write_close(buf, is_output ? (init_high ? "high" : "low") : "in");
139 LOG_ERROR("Couldn't set direction for gpio %d", gpio);
140 perror("sysfsgpio: ");
141 unexport_sysfs_gpio(gpio);
145 snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
146 ret = open(buf, O_RDWR | O_NONBLOCK | O_SYNC);
148 LOG_ERROR("Couldn't open value for gpio %d", gpio);
149 perror("sysfsgpio: ");
150 unexport_sysfs_gpio(gpio);
156 /* gpio numbers for each gpio. Negative values are invalid */
157 static int tck_gpio = -1;
158 static int tms_gpio = -1;
159 static int tdi_gpio = -1;
160 static int tdo_gpio = -1;
161 static int trst_gpio = -1;
162 static int srst_gpio = -1;
163 static int swclk_gpio = -1;
164 static int swdio_gpio = -1;
167 * file descriptors for /sys/class/gpio/gpioXX/value
168 * Set up during init.
170 static int tck_fd = -1;
171 static int tms_fd = -1;
172 static int tdi_fd = -1;
173 static int tdo_fd = -1;
174 static int trst_fd = -1;
175 static int srst_fd = -1;
176 static int swclk_fd = -1;
177 static int swdio_fd = -1;
179 static int last_swclk;
180 static int last_swdio;
181 static bool last_stored;
182 static bool swdio_input;
184 static void sysfsgpio_swdio_drive(bool is_output)
189 snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", swdio_gpio);
190 ret = open_write_close(buf, is_output ? "high" : "in");
192 LOG_ERROR("Couldn't set direction for gpio %d", swdio_gpio);
193 perror("sysfsgpio: ");
197 swdio_input = !is_output;
200 static int sysfsgpio_swdio_read(void)
204 /* important to seek to signal sysfs of new read */
205 lseek(swdio_fd, 0, SEEK_SET);
206 int ret = read(swdio_fd, &buf, sizeof(buf));
209 LOG_WARNING("reading swdio failed");
213 return buf[0] == '1';
216 static void sysfsgpio_swdio_write(int swclk, int swdio)
218 const char one[] = "1";
219 const char zero[] = "0";
221 size_t bytes_written;
224 if (!last_stored || (swdio != last_swdio)) {
225 bytes_written = write(swdio_fd, swdio ? &one : &zero, 1);
226 if (bytes_written != 1)
227 LOG_WARNING("writing swdio failed");
231 /* write swclk last */
232 if (!last_stored || (swclk != last_swclk)) {
233 bytes_written = write(swclk_fd, swclk ? &one : &zero, 1);
234 if (bytes_written != 1)
235 LOG_WARNING("writing swclk failed");
244 * Bitbang interface read of TDO
246 * The sysfs value will read back either '0' or '1'. The trick here is to call
247 * lseek to bypass buffering in the sysfs kernel driver.
249 static int sysfsgpio_read(void)
253 /* important to seek to signal sysfs of new read */
254 lseek(tdo_fd, 0, SEEK_SET);
255 int ret = read(tdo_fd, &buf, sizeof(buf));
258 LOG_WARNING("reading tdo failed");
262 return buf[0] == '1';
266 * Bitbang interface write of TCK, TMS, TDI
268 * Seeing as this is the only function where the outputs are changed,
269 * we can cache the old value to avoid needlessly writing it.
271 static void sysfsgpio_write(int tck, int tms, int tdi)
274 sysfsgpio_swdio_write(tck, tdi);
278 const char one[] = "1";
279 const char zero[] = "0";
285 static int first_time;
286 size_t bytes_written;
295 if (tdi != last_tdi) {
296 bytes_written = write(tdi_fd, tdi ? &one : &zero, 1);
297 if (bytes_written != 1)
298 LOG_WARNING("writing tdi failed");
301 if (tms != last_tms) {
302 bytes_written = write(tms_fd, tms ? &one : &zero, 1);
303 if (bytes_written != 1)
304 LOG_WARNING("writing tms failed");
308 if (tck != last_tck) {
309 bytes_written = write(tck_fd, tck ? &one : &zero, 1);
310 if (bytes_written != 1)
311 LOG_WARNING("writing tck failed");
320 * Bitbang interface to manipulate reset lines SRST and TRST
322 * (1) assert or (0) deassert reset lines
324 static void sysfsgpio_reset(int trst, int srst)
326 LOG_DEBUG("sysfsgpio_reset");
327 const char one[] = "1";
328 const char zero[] = "0";
329 size_t bytes_written;
331 /* assume active low */
333 bytes_written = write(srst_fd, srst ? &zero : &one, 1);
334 if (bytes_written != 1)
335 LOG_WARNING("writing srst failed");
338 /* assume active low */
340 bytes_written = write(trst_fd, trst ? &zero : &one, 1);
341 if (bytes_written != 1)
342 LOG_WARNING("writing trst failed");
346 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionums)
349 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
350 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpio);
351 COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], tdi_gpio);
352 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], tdo_gpio);
353 } else if (CMD_ARGC != 0) {
354 return ERROR_COMMAND_SYNTAX_ERROR;
357 command_print(CMD_CTX,
358 "SysfsGPIO nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
359 tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
364 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tck)
367 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
369 command_print(CMD_CTX, "SysfsGPIO num: tck = %d", tck_gpio);
373 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tms)
376 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
378 command_print(CMD_CTX, "SysfsGPIO num: tms = %d", tms_gpio);
382 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tdo)
385 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
387 command_print(CMD_CTX, "SysfsGPIO num: tdo = %d", tdo_gpio);
391 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tdi)
394 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
396 command_print(CMD_CTX, "SysfsGPIO num: tdi = %d", tdi_gpio);
400 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_srst)
403 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
405 command_print(CMD_CTX, "SysfsGPIO num: srst = %d", srst_gpio);
409 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_trst)
412 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
414 command_print(CMD_CTX, "SysfsGPIO num: trst = %d", trst_gpio);
418 COMMAND_HANDLER(sysfsgpio_handle_swd_gpionums)
421 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
422 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], swdio_gpio);
423 } else if (CMD_ARGC != 0) {
424 return ERROR_COMMAND_SYNTAX_ERROR;
427 command_print(CMD_CTX,
428 "SysfsGPIO nums: swclk = %d, swdio = %d",
429 swclk_gpio, swdio_gpio);
434 COMMAND_HANDLER(sysfsgpio_handle_swd_gpionum_swclk)
437 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
439 command_print(CMD_CTX, "SysfsGPIO num: swclk = %d", swclk_gpio);
443 COMMAND_HANDLER(sysfsgpio_handle_swd_gpionum_swdio)
446 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpio);
448 command_print(CMD_CTX, "SysfsGPIO num: swdio = %d", swdio_gpio);
452 static const struct command_registration sysfsgpio_command_handlers[] = {
454 .name = "sysfsgpio_jtag_nums",
455 .handler = &sysfsgpio_handle_jtag_gpionums,
456 .mode = COMMAND_CONFIG,
457 .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
458 .usage = "(tck tms tdi tdo)* ",
461 .name = "sysfsgpio_tck_num",
462 .handler = &sysfsgpio_handle_jtag_gpionum_tck,
463 .mode = COMMAND_CONFIG,
464 .help = "gpio number for tck.",
467 .name = "sysfsgpio_tms_num",
468 .handler = &sysfsgpio_handle_jtag_gpionum_tms,
469 .mode = COMMAND_CONFIG,
470 .help = "gpio number for tms.",
473 .name = "sysfsgpio_tdo_num",
474 .handler = &sysfsgpio_handle_jtag_gpionum_tdo,
475 .mode = COMMAND_CONFIG,
476 .help = "gpio number for tdo.",
479 .name = "sysfsgpio_tdi_num",
480 .handler = &sysfsgpio_handle_jtag_gpionum_tdi,
481 .mode = COMMAND_CONFIG,
482 .help = "gpio number for tdi.",
485 .name = "sysfsgpio_srst_num",
486 .handler = &sysfsgpio_handle_jtag_gpionum_srst,
487 .mode = COMMAND_CONFIG,
488 .help = "gpio number for srst.",
491 .name = "sysfsgpio_trst_num",
492 .handler = &sysfsgpio_handle_jtag_gpionum_trst,
493 .mode = COMMAND_CONFIG,
494 .help = "gpio number for trst.",
497 .name = "sysfsgpio_swd_nums",
498 .handler = &sysfsgpio_handle_swd_gpionums,
499 .mode = COMMAND_CONFIG,
500 .help = "gpio numbers for swclk, swdio. (in that order)",
501 .usage = "(swclk swdio)* ",
504 .name = "sysfsgpio_swclk_num",
505 .handler = &sysfsgpio_handle_swd_gpionum_swclk,
506 .mode = COMMAND_CONFIG,
507 .help = "gpio number for swclk.",
510 .name = "sysfsgpio_swdio_num",
511 .handler = &sysfsgpio_handle_swd_gpionum_swdio,
512 .mode = COMMAND_CONFIG,
513 .help = "gpio number for swdio.",
515 COMMAND_REGISTRATION_DONE
518 static int sysfsgpio_init(void);
519 static int sysfsgpio_quit(void);
521 static const char * const sysfsgpio_transports[] = { "jtag", "swd", NULL };
523 struct jtag_interface sysfsgpio_interface = {
525 .supported = DEBUG_CAP_TMS_SEQ,
526 .execute_queue = bitbang_execute_queue,
527 .transports = sysfsgpio_transports,
529 .commands = sysfsgpio_command_handlers,
530 .init = sysfsgpio_init,
531 .quit = sysfsgpio_quit,
534 static struct bitbang_interface sysfsgpio_bitbang = {
535 .read = sysfsgpio_read,
536 .write = sysfsgpio_write,
537 .reset = sysfsgpio_reset,
538 .swdio_read = sysfsgpio_swdio_read,
539 .swdio_drive = sysfsgpio_swdio_drive,
543 /* helper func to close and cleanup files only if they were valid/ used */
544 static void cleanup_fd(int fd, int gpio)
550 unexport_sysfs_gpio(gpio);
554 static void cleanup_all_fds(void)
556 cleanup_fd(tck_fd, tck_gpio);
557 cleanup_fd(tms_fd, tms_gpio);
558 cleanup_fd(tdi_fd, tdi_gpio);
559 cleanup_fd(tdo_fd, tdo_gpio);
560 cleanup_fd(trst_fd, trst_gpio);
561 cleanup_fd(srst_fd, srst_gpio);
564 static bool sysfsgpio_jtag_mode_possible(void)
566 if (!is_gpio_valid(tck_gpio))
568 if (!is_gpio_valid(tms_gpio))
570 if (!is_gpio_valid(tdi_gpio))
572 if (!is_gpio_valid(tdo_gpio))
577 static bool sysfsgpio_swd_mode_possible(void)
579 if (!is_gpio_valid(swclk_gpio))
581 if (!is_gpio_valid(swdio_gpio))
586 static int sysfsgpio_init(void)
588 bitbang_interface = &sysfsgpio_bitbang;
590 LOG_INFO("SysfsGPIO JTAG/SWD bitbang driver");
592 if (sysfsgpio_jtag_mode_possible()) {
593 if (sysfsgpio_swd_mode_possible())
594 LOG_INFO("JTAG and SWD modes enabled");
596 LOG_INFO("JTAG only mode enabled (specify swclk and swdio gpio to add SWD mode)");
597 if (!is_gpio_valid(trst_gpio) && !is_gpio_valid(srst_gpio)) {
598 LOG_ERROR("Require at least one of trst or srst gpios to be specified");
599 return ERROR_JTAG_INIT_FAILED;
601 } else if (sysfsgpio_swd_mode_possible()) {
602 LOG_INFO("SWD only mode enabled (specify tck, tms, tdi and tdo gpios to add JTAG mode)");
604 LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode and/or swclk and swdio gpio for SWD mode");
605 return ERROR_JTAG_INIT_FAILED;
610 * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
611 * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
612 * For SWD, SWCLK and SWDIO are configures as output high.
615 tck_fd = setup_sysfs_gpio(tck_gpio, 1, 0);
621 tms_fd = setup_sysfs_gpio(tms_gpio, 1, 1);
627 tdi_fd = setup_sysfs_gpio(tdi_gpio, 1, 0);
633 tdo_fd = setup_sysfs_gpio(tdo_gpio, 0, 0);
638 /* assume active low*/
639 if (trst_gpio >= 0) {
640 trst_fd = setup_sysfs_gpio(trst_gpio, 1, 1);
645 /* assume active low*/
646 if (srst_gpio >= 0) {
647 srst_fd = setup_sysfs_gpio(srst_gpio, 1, 1);
652 if (swclk_gpio >= 0) {
653 swclk_fd = setup_sysfs_gpio(swclk_gpio, 1, 0);
658 if (swdio_gpio >= 0) {
659 swdio_fd = setup_sysfs_gpio(swdio_gpio, 1, 0);
664 if (sysfsgpio_swd_mode_possible()) {
666 bitbang_swd_switch_seq(NULL, JTAG_TO_SWD);
668 bitbang_swd_switch_seq(NULL, SWD_TO_JTAG);
675 return ERROR_JTAG_INIT_FAILED;
678 static int sysfsgpio_quit(void)