drivers: new jtag bitbang driver using sysfs gpio
[fw/openocd] / src / jtag / drivers / sysfsgpio.c
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.)
27  *
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.
34  *
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).
38  *
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)
44  *
45  * Further work could address:
46  *  -srst and trst open drain/ push pull
47  *  -configurable active high/low for srst & trst
48  */
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include <jtag/interface.h>
54 #include "bitbang.h"
55
56 /*
57  * Helper func to determine if gpio number valid
58  *
59  * Assume here that there will be less than 1000 gpios on a system
60  */
61 static int is_gpio_valid(int gpio)
62 {
63         return gpio >= 0 && gpio < 1000;
64 }
65
66 /*
67  * Helper func to open, write to and close a file
68  * name and valstr must be null terminated.
69  *
70  * Returns negative on failure.
71  */
72 static int open_write_close(const char *name, const char *valstr)
73 {
74         int ret;
75         int fd = open(name, O_WRONLY);
76         if (fd < 0)
77                 return fd;
78
79         ret = write(fd, valstr, strlen(valstr));
80         close(fd);
81
82         return ret;
83 }
84
85 /*
86  * Helper func to unexport gpio from sysfs
87  */
88 static void unexport_sysfs_gpio(int gpio)
89 {
90         char gpiostr[4];
91
92         if (!is_gpio_valid(gpio))
93                 return;
94
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);
98
99         return;
100 }
101
102 /*
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.
106  *
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.
110  */
111 static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
112 {
113         char buf[40];
114         char gpiostr[4];
115         int ret;
116
117         if (!is_gpio_valid(gpio))
118                 return ERROR_OK;
119
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;
129                 }
130         }
131
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;
139         }
140
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);
146
147         if (ret < 0)
148                 unexport_sysfs_gpio(gpio);
149
150         return ret;
151 }
152
153 /*
154  * file descriptors for /sys/class/gpio/gpioXX/value
155  * Set up during init.
156  */
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;
163
164 /*
165  * Bitbang interface read of TDO
166  *
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.
169  */
170 static int sysfsgpio_read(void)
171 {
172         char buf[1];
173
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));
177
178         if (ret < 0) {
179                 LOG_WARNING("reading tdo failed");
180                 return 0;
181         }
182
183         return buf[0] == '1';
184 }
185
186 /*
187  * Bitbang interface write of TCK, TMS, TDI
188  *
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.
191  */
192 static void sysfsgpio_write(int tck, int tms, int tdi)
193 {
194         const char one[] = "1";
195         const char zero[] = "0";
196
197         static int last_tck;
198         static int last_tms;
199         static int last_tdi;
200
201         static int first_time;
202
203         if (!first_time) {
204                 last_tck = !tck;
205                 last_tms = !tms;
206                 last_tdi = !tdi;
207                 first_time = 1;
208         }
209
210         if (tdi != last_tdi)
211                 write(tdi_fd, tdi ? &one : &zero, 1);
212         if (tms != last_tms)
213                 write(tms_fd, tms ? &one : &zero, 1);
214         /* write clk last */
215         if (tck != last_tck)
216                 write(tck_fd, tck ? &one : &zero, 1);
217
218         last_tdi = tdi;
219         last_tms = tms;
220         last_tck = tck;
221 }
222
223 /*
224  * Bitbang interface to manipulate reset lines SRST and TRST
225  *
226  * (1) assert or (0) deassert reset lines
227  */
228 static void sysfsgpio_reset(int trst, int srst)
229 {
230         const char one[] = "1";
231         const char zero[] = "0";
232
233         /* assume active low */
234         if (srst_fd >= 0)
235                 write(srst_fd, srst ? &zero : &one, 1);
236
237         /* assume active low */
238         if (trst_fd >= 0)
239                 write(trst_fd, trst ? &zero : &one, 1);
240 }
241
242 /* No speed control is implemented yet */
243 static int sysfsgpio_speed(int speed)
244 {
245         return ERROR_OK;
246 }
247
248 static int sysfsgpio_khz(int khz, int *jtag_speed)
249 {
250         /* no adaptive clocking */
251         if (khz == 0)
252                 return ERROR_FAIL;
253
254         *jtag_speed = 0;
255         return ERROR_OK;
256 }
257
258 static int sysfsgpio_speed_div(int speed, int *khz)
259 {
260         *khz = 1;
261         return ERROR_OK;
262 }
263
264 /* gpio numbers for each gpio. Negative values are invalid */
265 static int tck_gpio = -1;
266 static int tms_gpio = -1;
267 static int tdi_gpio = -1;
268 static int tdo_gpio = -1;
269 static int trst_gpio = -1;
270 static int srst_gpio = -1;
271
272 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionums)
273 {
274         if (CMD_ARGC == 4) {
275                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
276                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpio);
277                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], tdi_gpio);
278                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], tdo_gpio);
279         } else if (CMD_ARGC != 0) {
280                 return ERROR_COMMAND_SYNTAX_ERROR;
281         }
282
283         command_print(CMD_CTX,
284                         "SysfsGPIO nums: tck = %d, tms = %d, tdi = %d, tdi = %d",
285                         tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
286
287         return ERROR_OK;
288 }
289
290 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tck)
291 {
292         if (CMD_ARGC == 1)
293                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
294
295         command_print(CMD_CTX, "SysfsGPIO num: tck = %d", tck_gpio);
296         return ERROR_OK;
297 }
298
299 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tms)
300 {
301         if (CMD_ARGC == 1)
302                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
303
304         command_print(CMD_CTX, "SysfsGPIO num: tms = %d", tms_gpio);
305         return ERROR_OK;
306 }
307
308 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tdo)
309 {
310         if (CMD_ARGC == 1)
311                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
312
313         command_print(CMD_CTX, "SysfsGPIO num: tdo = %d", tdo_gpio);
314         return ERROR_OK;
315 }
316
317 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tdi)
318 {
319         if (CMD_ARGC == 1)
320                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
321
322         command_print(CMD_CTX, "SysfsGPIO num: tdi = %d", tdi_gpio);
323         return ERROR_OK;
324 }
325
326 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_srst)
327 {
328         if (CMD_ARGC == 1)
329                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
330
331         command_print(CMD_CTX, "SysfsGPIO num: srst = %d", srst_gpio);
332         return ERROR_OK;
333 }
334
335 COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_trst)
336 {
337         if (CMD_ARGC == 1)
338                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
339
340         command_print(CMD_CTX, "SysfsGPIO num: trst = %d", trst_gpio);
341         return ERROR_OK;
342 }
343
344 static const struct command_registration sysfsgpio_command_handlers[] = {
345         {
346                 .name = "sysfsgpio_jtag_nums",
347                 .handler = &sysfsgpio_handle_jtag_gpionums,
348                 .mode = COMMAND_CONFIG,
349                 .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
350                 .usage = "(tck tms tdi tdo)* ",
351         },
352         {
353                 .name = "sysfsgpio_tck_num",
354                 .handler = &sysfsgpio_handle_jtag_gpionum_tck,
355                 .mode = COMMAND_CONFIG,
356                 .help = "gpio number for tck.",
357         },
358         {
359                 .name = "sysfsgpio_tms_num",
360                 .handler = &sysfsgpio_handle_jtag_gpionum_tms,
361                 .mode = COMMAND_CONFIG,
362                 .help = "gpio number for tms.",
363         },
364         {
365                 .name = "sysfsgpio_tdo_num",
366                 .handler = &sysfsgpio_handle_jtag_gpionum_tdo,
367                 .mode = COMMAND_CONFIG,
368                 .help = "gpio number for tdo.",
369         },
370         {
371                 .name = "sysfsgpio_tdi_num",
372                 .handler = &sysfsgpio_handle_jtag_gpionum_tdi,
373                 .mode = COMMAND_CONFIG,
374                 .help = "gpio number for tdi.",
375         },
376         {
377                 .name = "sysfsgpio_srst_num",
378                 .handler = &sysfsgpio_handle_jtag_gpionum_srst,
379                 .mode = COMMAND_CONFIG,
380                 .help = "gpio number for srst.",
381         },
382         {
383                 .name = "sysfsgpio_trst_num",
384                 .handler = &sysfsgpio_handle_jtag_gpionum_trst,
385                 .mode = COMMAND_CONFIG,
386                 .help = "gpio number for trst.",
387         },
388         COMMAND_REGISTRATION_DONE
389 };
390
391 static int sysfsgpio_init(void);
392 static int sysfsgpio_quit(void);
393
394 struct jtag_interface sysfsgpio_interface = {
395         .name = "sysfsgpio",
396         .supported = DEBUG_CAP_TMS_SEQ,
397         .execute_queue = bitbang_execute_queue,
398         .transports = jtag_only,
399         .speed = sysfsgpio_speed,
400         .khz = sysfsgpio_khz,
401         .speed_div = sysfsgpio_speed_div,
402         .commands = sysfsgpio_command_handlers,
403         .init = sysfsgpio_init,
404         .quit = sysfsgpio_quit,
405 };
406
407 static struct bitbang_interface sysfsgpio_bitbang = {
408         .read = sysfsgpio_read,
409         .write = sysfsgpio_write,
410         .reset = sysfsgpio_reset,
411         .blink = 0
412 };
413
414 /* helper func to close and cleanup files only if they were valid/ used */
415 static void cleanup_fd(int fd, int gpio)
416 {
417         if (gpio >= 0) {
418                 if (fd >= 0)
419                         close(fd);
420
421                 unexport_sysfs_gpio(gpio);
422         }
423 }
424
425 static void cleanup_all_fds(void)
426 {
427         cleanup_fd(tck_fd, tck_gpio);
428         cleanup_fd(tms_fd, tms_gpio);
429         cleanup_fd(tdi_fd, tdi_gpio);
430         cleanup_fd(tdo_fd, tdo_gpio);
431         cleanup_fd(trst_fd, trst_gpio);
432         cleanup_fd(srst_fd, srst_gpio);
433 }
434
435 static int sysfsgpio_init(void)
436 {
437         bitbang_interface = &sysfsgpio_bitbang;
438
439         LOG_INFO("SysfsGPIO JTAG bitbang driver");
440
441         if (!(is_gpio_valid(tck_gpio)
442                         && is_gpio_valid(tms_gpio)
443                         && is_gpio_valid(tdi_gpio)
444                         && is_gpio_valid(tdo_gpio))) {
445                 if (!is_gpio_valid(tck_gpio))
446                         LOG_ERROR("gpio num for tck is invalid");
447                 if (!is_gpio_valid(tms_gpio))
448                         LOG_ERROR("gpio num for tms is invalid");
449                 if (!is_gpio_valid(tdo_gpio))
450                         LOG_ERROR("gpio num for tdo is invalid");
451                 if (!is_gpio_valid(tdi_gpio))
452                         LOG_ERROR("gpio num for tdi is invalid");
453
454                 LOG_ERROR("Require tck, tms, tdi and tdo gpios to all be specified");
455                 return ERROR_JTAG_INIT_FAILED;
456         }
457
458         if (!is_gpio_valid(trst_gpio) && !is_gpio_valid(srst_gpio)) {
459                 LOG_ERROR("Require at least one of trst or srst gpios to be specified");
460                 return ERROR_JTAG_INIT_FAILED;
461         }
462
463         /*
464          * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
465          * as outputs.  Drive TDI and TCK low, and TMS/TRST/SRST high.
466          */
467         tck_fd = setup_sysfs_gpio(tck_gpio, 1, 0);
468         if (tck_fd < 0)
469                 goto out_error;
470
471         tms_fd = setup_sysfs_gpio(tms_gpio, 1, 1);
472         if (tms_fd < 0)
473                 goto out_error;
474
475         tdi_fd = setup_sysfs_gpio(tdi_gpio, 1, 0);
476         if (tdi_fd < 0)
477                 goto out_error;
478
479         tdo_fd = setup_sysfs_gpio(tdo_gpio, 0, 0);
480         if (tdo_fd < 0)
481                 goto out_error;
482
483         /* assume active low*/
484         trst_fd = setup_sysfs_gpio(trst_gpio, 1, 1);
485         if (trst_gpio > 0 && trst_fd < 0)
486                 goto out_error;
487
488         /* assume active low*/
489         srst_fd = setup_sysfs_gpio(srst_gpio, 1, 1);
490         if (srst_gpio > 0 && srst_fd < 0)
491                 goto out_error;
492
493         return ERROR_OK;
494
495 out_error:
496         cleanup_all_fds();
497         return ERROR_JTAG_INIT_FAILED;
498 }
499
500 static int sysfsgpio_quit(void)
501 {
502         cleanup_all_fds();
503         return ERROR_OK;
504 }
505