You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

682 lines
17 KiB

  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, see <http://www.gnu.org/licenses/>. *
  16. ***************************************************************************/
  17. /* 2014-12: Addition of the SWD protocol support is based on the initial work
  18. * on bcm2835gpio.c by Paul Fertser and modifications by Jean-Christian de Rivaz. */
  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. #include <jtag/interface.h>
  53. #include "bitbang.h"
  54. /*
  55. * Helper func to determine if gpio number valid
  56. *
  57. * Assume here that there will be less than 1000 gpios on a system
  58. */
  59. static int is_gpio_valid(int gpio)
  60. {
  61. return gpio >= 0 && gpio < 1000;
  62. }
  63. /*
  64. * Helper func to open, write to and close a file
  65. * name and valstr must be null terminated.
  66. *
  67. * Returns negative on failure.
  68. */
  69. static int open_write_close(const char *name, const char *valstr)
  70. {
  71. int ret;
  72. int fd = open(name, O_WRONLY);
  73. if (fd < 0)
  74. return fd;
  75. ret = write(fd, valstr, strlen(valstr));
  76. close(fd);
  77. return ret;
  78. }
  79. /*
  80. * Helper func to unexport gpio from sysfs
  81. */
  82. static void unexport_sysfs_gpio(int gpio)
  83. {
  84. char gpiostr[4];
  85. if (!is_gpio_valid(gpio))
  86. return;
  87. snprintf(gpiostr, sizeof(gpiostr), "%d", gpio);
  88. if (open_write_close("/sys/class/gpio/unexport", gpiostr) < 0)
  89. LOG_ERROR("Couldn't unexport gpio %d", gpio);
  90. return;
  91. }
  92. /*
  93. * Exports and sets up direction for gpio.
  94. * If the gpio is an output, it is initialized according to init_high,
  95. * otherwise it is ignored.
  96. *
  97. * If the gpio is already exported we just show a warning and continue; if
  98. * openocd happened to crash (or was killed by user) then the gpios will not
  99. * have been cleaned up.
  100. */
  101. static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
  102. {
  103. char buf[40];
  104. char gpiostr[4];
  105. int ret;
  106. if (!is_gpio_valid(gpio))
  107. return ERROR_OK;
  108. snprintf(gpiostr, sizeof(gpiostr), "%d", gpio);
  109. ret = open_write_close("/sys/class/gpio/export", gpiostr);
  110. if (ret < 0) {
  111. if (errno == EBUSY) {
  112. LOG_WARNING("gpio %d is already exported", gpio);
  113. } else {
  114. LOG_ERROR("Couldn't export gpio %d", gpio);
  115. perror("sysfsgpio: ");
  116. return ERROR_FAIL;
  117. }
  118. }
  119. snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
  120. ret = open_write_close(buf, is_output ? (init_high ? "high" : "low") : "in");
  121. if (ret < 0) {
  122. LOG_ERROR("Couldn't set direction for gpio %d", gpio);
  123. perror("sysfsgpio: ");
  124. unexport_sysfs_gpio(gpio);
  125. return ERROR_FAIL;
  126. }
  127. snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
  128. ret = open(buf, O_RDWR | O_NONBLOCK | O_SYNC);
  129. if (ret < 0) {
  130. LOG_ERROR("Couldn't open value for gpio %d", gpio);
  131. perror("sysfsgpio: ");
  132. unexport_sysfs_gpio(gpio);
  133. }
  134. return ret;
  135. }
  136. /* gpio numbers for each gpio. Negative values are invalid */
  137. static int tck_gpio = -1;
  138. static int tms_gpio = -1;
  139. static int tdi_gpio = -1;
  140. static int tdo_gpio = -1;
  141. static int trst_gpio = -1;
  142. static int srst_gpio = -1;
  143. static int swclk_gpio = -1;
  144. static int swdio_gpio = -1;
  145. /*
  146. * file descriptors for /sys/class/gpio/gpioXX/value
  147. * Set up during init.
  148. */
  149. static int tck_fd = -1;
  150. static int tms_fd = -1;
  151. static int tdi_fd = -1;
  152. static int tdo_fd = -1;
  153. static int trst_fd = -1;
  154. static int srst_fd = -1;
  155. static int swclk_fd = -1;
  156. static int swdio_fd = -1;
  157. static int last_swclk;
  158. static int last_swdio;
  159. static bool last_stored;
  160. static bool swdio_input;
  161. static void sysfsgpio_swdio_drive(bool is_output)
  162. {
  163. char buf[40];
  164. int ret;
  165. snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", swdio_gpio);
  166. ret = open_write_close(buf, is_output ? "high" : "in");
  167. if (ret < 0) {
  168. LOG_ERROR("Couldn't set direction for gpio %d", swdio_gpio);
  169. perror("sysfsgpio: ");
  170. }
  171. last_stored = false;
  172. swdio_input = !is_output;
  173. }
  174. static int sysfsgpio_swdio_read(void)
  175. {
  176. char buf[1];
  177. /* important to seek to signal sysfs of new read */
  178. lseek(swdio_fd, 0, SEEK_SET);
  179. int ret = read(swdio_fd, &buf, sizeof(buf));
  180. if (ret < 0) {
  181. LOG_WARNING("reading swdio failed");
  182. return 0;
  183. }
  184. return buf[0] != '0';
  185. }
  186. static void sysfsgpio_swdio_write(int swclk, int swdio)
  187. {
  188. const char one[] = "1";
  189. const char zero[] = "0";
  190. size_t bytes_written;
  191. if (!swdio_input) {
  192. if (!last_stored || (swdio != last_swdio)) {
  193. bytes_written = write(swdio_fd, swdio ? &one : &zero, 1);
  194. if (bytes_written != 1)
  195. LOG_WARNING("writing swdio failed");
  196. }
  197. }
  198. /* write swclk last */
  199. if (!last_stored || (swclk != last_swclk)) {
  200. bytes_written = write(swclk_fd, swclk ? &one : &zero, 1);
  201. if (bytes_written != 1)
  202. LOG_WARNING("writing swclk failed");
  203. }
  204. last_swdio = swdio;
  205. last_swclk = swclk;
  206. last_stored = true;
  207. }
  208. /*
  209. * Bitbang interface read of TDO
  210. *
  211. * The sysfs value will read back either '0' or '1'. The trick here is to call
  212. * lseek to bypass buffering in the sysfs kernel driver.
  213. */
  214. static int sysfsgpio_read(void)
  215. {
  216. char buf[1];
  217. /* important to seek to signal sysfs of new read */
  218. lseek(tdo_fd, 0, SEEK_SET);
  219. int ret = read(tdo_fd, &buf, sizeof(buf));
  220. if (ret < 0) {
  221. LOG_WARNING("reading tdo failed");
  222. return 0;
  223. }
  224. return buf[0] != '0';
  225. }
  226. /*
  227. * Bitbang interface write of TCK, TMS, TDI
  228. *
  229. * Seeing as this is the only function where the outputs are changed,
  230. * we can cache the old value to avoid needlessly writing it.
  231. */
  232. static void sysfsgpio_write(int tck, int tms, int tdi)
  233. {
  234. if (swd_mode) {
  235. sysfsgpio_swdio_write(tck, tdi);
  236. return;
  237. }
  238. const char one[] = "1";
  239. const char zero[] = "0";
  240. static int last_tck;
  241. static int last_tms;
  242. static int last_tdi;
  243. static int first_time;
  244. size_t bytes_written;
  245. if (!first_time) {
  246. last_tck = !tck;
  247. last_tms = !tms;
  248. last_tdi = !tdi;
  249. first_time = 1;
  250. }
  251. if (tdi != last_tdi) {
  252. bytes_written = write(tdi_fd, tdi ? &one : &zero, 1);
  253. if (bytes_written != 1)
  254. LOG_WARNING("writing tdi failed");
  255. }
  256. if (tms != last_tms) {
  257. bytes_written = write(tms_fd, tms ? &one : &zero, 1);
  258. if (bytes_written != 1)
  259. LOG_WARNING("writing tms failed");
  260. }
  261. /* write clk last */
  262. if (tck != last_tck) {
  263. bytes_written = write(tck_fd, tck ? &one : &zero, 1);
  264. if (bytes_written != 1)
  265. LOG_WARNING("writing tck failed");
  266. }
  267. last_tdi = tdi;
  268. last_tms = tms;
  269. last_tck = tck;
  270. }
  271. /*
  272. * Bitbang interface to manipulate reset lines SRST and TRST
  273. *
  274. * (1) assert or (0) deassert reset lines
  275. */
  276. static void sysfsgpio_reset(int trst, int srst)
  277. {
  278. LOG_DEBUG("sysfsgpio_reset");
  279. const char one[] = "1";
  280. const char zero[] = "0";
  281. size_t bytes_written;
  282. /* assume active low */
  283. if (srst_fd >= 0) {
  284. bytes_written = write(srst_fd, srst ? &zero : &one, 1);
  285. if (bytes_written != 1)
  286. LOG_WARNING("writing srst failed");
  287. }
  288. /* assume active low */
  289. if (trst_fd >= 0) {
  290. bytes_written = write(trst_fd, trst ? &zero : &one, 1);
  291. if (bytes_written != 1)
  292. LOG_WARNING("writing trst failed");
  293. }
  294. }
  295. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionums)
  296. {
  297. if (CMD_ARGC == 4) {
  298. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
  299. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpio);
  300. COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], tdi_gpio);
  301. COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], tdo_gpio);
  302. } else if (CMD_ARGC != 0) {
  303. return ERROR_COMMAND_SYNTAX_ERROR;
  304. }
  305. command_print(CMD_CTX,
  306. "SysfsGPIO nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
  307. tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
  308. return ERROR_OK;
  309. }
  310. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tck)
  311. {
  312. if (CMD_ARGC == 1)
  313. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
  314. command_print(CMD_CTX, "SysfsGPIO num: tck = %d", tck_gpio);
  315. return ERROR_OK;
  316. }
  317. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tms)
  318. {
  319. if (CMD_ARGC == 1)
  320. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
  321. command_print(CMD_CTX, "SysfsGPIO num: tms = %d", tms_gpio);
  322. return ERROR_OK;
  323. }
  324. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tdo)
  325. {
  326. if (CMD_ARGC == 1)
  327. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
  328. command_print(CMD_CTX, "SysfsGPIO num: tdo = %d", tdo_gpio);
  329. return ERROR_OK;
  330. }
  331. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tdi)
  332. {
  333. if (CMD_ARGC == 1)
  334. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
  335. command_print(CMD_CTX, "SysfsGPIO num: tdi = %d", tdi_gpio);
  336. return ERROR_OK;
  337. }
  338. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_srst)
  339. {
  340. if (CMD_ARGC == 1)
  341. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
  342. command_print(CMD_CTX, "SysfsGPIO num: srst = %d", srst_gpio);
  343. return ERROR_OK;
  344. }
  345. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_trst)
  346. {
  347. if (CMD_ARGC == 1)
  348. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
  349. command_print(CMD_CTX, "SysfsGPIO num: trst = %d", trst_gpio);
  350. return ERROR_OK;
  351. }
  352. COMMAND_HANDLER(sysfsgpio_handle_swd_gpionums)
  353. {
  354. if (CMD_ARGC == 2) {
  355. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
  356. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], swdio_gpio);
  357. } else if (CMD_ARGC != 0) {
  358. return ERROR_COMMAND_SYNTAX_ERROR;
  359. }
  360. command_print(CMD_CTX,
  361. "SysfsGPIO nums: swclk = %d, swdio = %d",
  362. swclk_gpio, swdio_gpio);
  363. return ERROR_OK;
  364. }
  365. COMMAND_HANDLER(sysfsgpio_handle_swd_gpionum_swclk)
  366. {
  367. if (CMD_ARGC == 1)
  368. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
  369. command_print(CMD_CTX, "SysfsGPIO num: swclk = %d", swclk_gpio);
  370. return ERROR_OK;
  371. }
  372. COMMAND_HANDLER(sysfsgpio_handle_swd_gpionum_swdio)
  373. {
  374. if (CMD_ARGC == 1)
  375. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpio);
  376. command_print(CMD_CTX, "SysfsGPIO num: swdio = %d", swdio_gpio);
  377. return ERROR_OK;
  378. }
  379. static const struct command_registration sysfsgpio_command_handlers[] = {
  380. {
  381. .name = "sysfsgpio_jtag_nums",
  382. .handler = &sysfsgpio_handle_jtag_gpionums,
  383. .mode = COMMAND_CONFIG,
  384. .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
  385. .usage = "(tck tms tdi tdo)* ",
  386. },
  387. {
  388. .name = "sysfsgpio_tck_num",
  389. .handler = &sysfsgpio_handle_jtag_gpionum_tck,
  390. .mode = COMMAND_CONFIG,
  391. .help = "gpio number for tck.",
  392. },
  393. {
  394. .name = "sysfsgpio_tms_num",
  395. .handler = &sysfsgpio_handle_jtag_gpionum_tms,
  396. .mode = COMMAND_CONFIG,
  397. .help = "gpio number for tms.",
  398. },
  399. {
  400. .name = "sysfsgpio_tdo_num",
  401. .handler = &sysfsgpio_handle_jtag_gpionum_tdo,
  402. .mode = COMMAND_CONFIG,
  403. .help = "gpio number for tdo.",
  404. },
  405. {
  406. .name = "sysfsgpio_tdi_num",
  407. .handler = &sysfsgpio_handle_jtag_gpionum_tdi,
  408. .mode = COMMAND_CONFIG,
  409. .help = "gpio number for tdi.",
  410. },
  411. {
  412. .name = "sysfsgpio_srst_num",
  413. .handler = &sysfsgpio_handle_jtag_gpionum_srst,
  414. .mode = COMMAND_CONFIG,
  415. .help = "gpio number for srst.",
  416. },
  417. {
  418. .name = "sysfsgpio_trst_num",
  419. .handler = &sysfsgpio_handle_jtag_gpionum_trst,
  420. .mode = COMMAND_CONFIG,
  421. .help = "gpio number for trst.",
  422. },
  423. {
  424. .name = "sysfsgpio_swd_nums",
  425. .handler = &sysfsgpio_handle_swd_gpionums,
  426. .mode = COMMAND_CONFIG,
  427. .help = "gpio numbers for swclk, swdio. (in that order)",
  428. .usage = "(swclk swdio)* ",
  429. },
  430. {
  431. .name = "sysfsgpio_swclk_num",
  432. .handler = &sysfsgpio_handle_swd_gpionum_swclk,
  433. .mode = COMMAND_CONFIG,
  434. .help = "gpio number for swclk.",
  435. },
  436. {
  437. .name = "sysfsgpio_swdio_num",
  438. .handler = &sysfsgpio_handle_swd_gpionum_swdio,
  439. .mode = COMMAND_CONFIG,
  440. .help = "gpio number for swdio.",
  441. },
  442. COMMAND_REGISTRATION_DONE
  443. };
  444. static int sysfsgpio_init(void);
  445. static int sysfsgpio_quit(void);
  446. static const char * const sysfsgpio_transports[] = { "jtag", "swd", NULL };
  447. struct jtag_interface sysfsgpio_interface = {
  448. .name = "sysfsgpio",
  449. .supported = DEBUG_CAP_TMS_SEQ,
  450. .execute_queue = bitbang_execute_queue,
  451. .transports = sysfsgpio_transports,
  452. .swd = &bitbang_swd,
  453. .commands = sysfsgpio_command_handlers,
  454. .init = sysfsgpio_init,
  455. .quit = sysfsgpio_quit,
  456. };
  457. static struct bitbang_interface sysfsgpio_bitbang = {
  458. .read = sysfsgpio_read,
  459. .write = sysfsgpio_write,
  460. .reset = sysfsgpio_reset,
  461. .swdio_read = sysfsgpio_swdio_read,
  462. .swdio_drive = sysfsgpio_swdio_drive,
  463. .blink = 0
  464. };
  465. /* helper func to close and cleanup files only if they were valid/ used */
  466. static void cleanup_fd(int fd, int gpio)
  467. {
  468. if (gpio >= 0) {
  469. if (fd >= 0)
  470. close(fd);
  471. unexport_sysfs_gpio(gpio);
  472. }
  473. }
  474. static void cleanup_all_fds(void)
  475. {
  476. cleanup_fd(tck_fd, tck_gpio);
  477. cleanup_fd(tms_fd, tms_gpio);
  478. cleanup_fd(tdi_fd, tdi_gpio);
  479. cleanup_fd(tdo_fd, tdo_gpio);
  480. cleanup_fd(trst_fd, trst_gpio);
  481. cleanup_fd(srst_fd, srst_gpio);
  482. }
  483. static bool sysfsgpio_jtag_mode_possible(void)
  484. {
  485. if (!is_gpio_valid(tck_gpio))
  486. return 0;
  487. if (!is_gpio_valid(tms_gpio))
  488. return 0;
  489. if (!is_gpio_valid(tdi_gpio))
  490. return 0;
  491. if (!is_gpio_valid(tdo_gpio))
  492. return 0;
  493. return 1;
  494. }
  495. static bool sysfsgpio_swd_mode_possible(void)
  496. {
  497. if (!is_gpio_valid(swclk_gpio))
  498. return 0;
  499. if (!is_gpio_valid(swdio_gpio))
  500. return 0;
  501. return 1;
  502. }
  503. static int sysfsgpio_init(void)
  504. {
  505. bitbang_interface = &sysfsgpio_bitbang;
  506. LOG_INFO("SysfsGPIO JTAG/SWD bitbang driver");
  507. if (sysfsgpio_jtag_mode_possible()) {
  508. if (sysfsgpio_swd_mode_possible())
  509. LOG_INFO("JTAG and SWD modes enabled");
  510. else
  511. LOG_INFO("JTAG only mode enabled (specify swclk and swdio gpio to add SWD mode)");
  512. if (!is_gpio_valid(trst_gpio) && !is_gpio_valid(srst_gpio)) {
  513. LOG_ERROR("Require at least one of trst or srst gpios to be specified");
  514. return ERROR_JTAG_INIT_FAILED;
  515. }
  516. } else if (sysfsgpio_swd_mode_possible()) {
  517. LOG_INFO("SWD only mode enabled (specify tck, tms, tdi and tdo gpios to add JTAG mode)");
  518. } else {
  519. LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode and/or swclk and swdio gpio for SWD mode");
  520. return ERROR_JTAG_INIT_FAILED;
  521. }
  522. /*
  523. * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
  524. * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
  525. * For SWD, SWCLK and SWDIO are configures as output high.
  526. */
  527. if (tck_gpio >= 0) {
  528. tck_fd = setup_sysfs_gpio(tck_gpio, 1, 0);
  529. if (tck_fd < 0)
  530. goto out_error;
  531. }
  532. if (tms_gpio >= 0) {
  533. tms_fd = setup_sysfs_gpio(tms_gpio, 1, 1);
  534. if (tms_fd < 0)
  535. goto out_error;
  536. }
  537. if (tdi_gpio >= 0) {
  538. tdi_fd = setup_sysfs_gpio(tdi_gpio, 1, 0);
  539. if (tdi_fd < 0)
  540. goto out_error;
  541. }
  542. if (tdo_gpio >= 0) {
  543. tdo_fd = setup_sysfs_gpio(tdo_gpio, 0, 0);
  544. if (tdo_fd < 0)
  545. goto out_error;
  546. }
  547. /* assume active low*/
  548. if (trst_gpio >= 0) {
  549. trst_fd = setup_sysfs_gpio(trst_gpio, 1, 1);
  550. if (trst_fd < 0)
  551. goto out_error;
  552. }
  553. /* assume active low*/
  554. if (srst_gpio >= 0) {
  555. srst_fd = setup_sysfs_gpio(srst_gpio, 1, 1);
  556. if (srst_fd < 0)
  557. goto out_error;
  558. }
  559. if (swclk_gpio >= 0) {
  560. swclk_fd = setup_sysfs_gpio(swclk_gpio, 1, 0);
  561. if (swclk_fd < 0)
  562. goto out_error;
  563. }
  564. if (swdio_gpio >= 0) {
  565. swdio_fd = setup_sysfs_gpio(swdio_gpio, 1, 0);
  566. if (swdio_fd < 0)
  567. goto out_error;
  568. }
  569. if (sysfsgpio_swd_mode_possible()) {
  570. if (swd_mode)
  571. bitbang_swd_switch_seq(JTAG_TO_SWD);
  572. else
  573. bitbang_swd_switch_seq(SWD_TO_JTAG);
  574. }
  575. return ERROR_OK;
  576. out_error:
  577. cleanup_all_fds();
  578. return ERROR_JTAG_INIT_FAILED;
  579. }
  580. static int sysfsgpio_quit(void)
  581. {
  582. cleanup_all_fds();
  583. return ERROR_OK;
  584. }