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.
 
 
 
 
 
 

645 lines
14 KiB

  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. #ifdef HAVE_CONFIG_H
  12. #include "config.h"
  13. #endif
  14. #include <gpiod.h>
  15. #include <jtag/interface.h>
  16. #include <transport/transport.h>
  17. #include "bitbang.h"
  18. /* gpio numbers for each gpio. Negative values are invalid */
  19. static int tck_gpio = -1;
  20. static int tms_gpio = -1;
  21. static int tdi_gpio = -1;
  22. static int tdo_gpio = -1;
  23. static int trst_gpio = -1;
  24. static int srst_gpio = -1;
  25. static int swclk_gpio = -1;
  26. static int swdio_gpio = -1;
  27. static int led_gpio = -1;
  28. static int gpiochip = -1;
  29. static struct gpiod_chip *gpiod_chip;
  30. static struct gpiod_line *gpiod_tck;
  31. static struct gpiod_line *gpiod_tms;
  32. static struct gpiod_line *gpiod_tdi;
  33. static struct gpiod_line *gpiod_tdo;
  34. static struct gpiod_line *gpiod_trst;
  35. static struct gpiod_line *gpiod_swclk;
  36. static struct gpiod_line *gpiod_swdio;
  37. static struct gpiod_line *gpiod_srst;
  38. static struct gpiod_line *gpiod_led;
  39. static int last_swclk;
  40. static int last_swdio;
  41. static bool last_stored;
  42. static bool swdio_input;
  43. /* Bitbang interface read of TDO */
  44. static bb_value_t linuxgpiod_read(void)
  45. {
  46. int retval;
  47. retval = gpiod_line_get_value(gpiod_tdo);
  48. if (retval < 0) {
  49. LOG_WARNING("reading tdo failed");
  50. return 0;
  51. }
  52. return retval ? BB_HIGH : BB_LOW;
  53. }
  54. /*
  55. * Bitbang interface write of TCK, TMS, TDI
  56. *
  57. * Seeing as this is the only function where the outputs are changed,
  58. * we can cache the old value to avoid needlessly writing it.
  59. */
  60. static int linuxgpiod_write(int tck, int tms, int tdi)
  61. {
  62. static int last_tck;
  63. static int last_tms;
  64. static int last_tdi;
  65. static int first_time;
  66. int retval;
  67. if (!first_time) {
  68. last_tck = !tck;
  69. last_tms = !tms;
  70. last_tdi = !tdi;
  71. first_time = 1;
  72. }
  73. if (tdi != last_tdi) {
  74. retval = gpiod_line_set_value(gpiod_tdi, tdi);
  75. if (retval < 0)
  76. LOG_WARNING("writing tdi failed");
  77. }
  78. if (tms != last_tms) {
  79. retval = gpiod_line_set_value(gpiod_tms, tms);
  80. if (retval < 0)
  81. LOG_WARNING("writing tms failed");
  82. }
  83. /* write clk last */
  84. if (tck != last_tck) {
  85. retval = gpiod_line_set_value(gpiod_tck, tck);
  86. if (retval < 0)
  87. LOG_WARNING("writing tck failed");
  88. }
  89. last_tdi = tdi;
  90. last_tms = tms;
  91. last_tck = tck;
  92. return ERROR_OK;
  93. }
  94. static int linuxgpiod_swdio_read(void)
  95. {
  96. int retval;
  97. retval = gpiod_line_get_value(gpiod_swdio);
  98. if (retval < 0) {
  99. LOG_WARNING("Fail read swdio");
  100. return 0;
  101. }
  102. return retval;
  103. }
  104. static void linuxgpiod_swdio_drive(bool is_output)
  105. {
  106. int retval;
  107. /*
  108. * FIXME: change direction requires release and re-require the line
  109. * https://stackoverflow.com/questions/58735140/
  110. * this would change in future libgpiod
  111. */
  112. gpiod_line_release(gpiod_swdio);
  113. if (is_output) {
  114. retval = gpiod_line_request_output(gpiod_swdio, "OpenOCD", 1);
  115. if (retval < 0)
  116. LOG_WARNING("Fail request_output line swdio");
  117. } else {
  118. retval = gpiod_line_request_input(gpiod_swdio, "OpenOCD");
  119. if (retval < 0)
  120. LOG_WARNING("Fail request_input line swdio");
  121. }
  122. last_stored = false;
  123. swdio_input = !is_output;
  124. }
  125. static int linuxgpiod_swd_write(int swclk, int swdio)
  126. {
  127. int retval;
  128. if (!swdio_input) {
  129. if (!last_stored || (swdio != last_swdio)) {
  130. retval = gpiod_line_set_value(gpiod_swdio, swdio);
  131. if (retval < 0)
  132. LOG_WARNING("Fail set swdio");
  133. }
  134. }
  135. /* write swclk last */
  136. if (!last_stored || (swclk != last_swclk)) {
  137. retval = gpiod_line_set_value(gpiod_swclk, swclk);
  138. if (retval < 0)
  139. LOG_WARNING("Fail set swclk");
  140. }
  141. last_swdio = swdio;
  142. last_swclk = swclk;
  143. last_stored = true;
  144. return ERROR_OK;
  145. }
  146. static int linuxgpiod_blink(int on)
  147. {
  148. int retval;
  149. if (!gpiod_led)
  150. return ERROR_OK;
  151. retval = gpiod_line_set_value(gpiod_led, on);
  152. if (retval < 0)
  153. LOG_WARNING("Fail set led");
  154. return retval;
  155. }
  156. static struct bitbang_interface linuxgpiod_bitbang = {
  157. .read = linuxgpiod_read,
  158. .write = linuxgpiod_write,
  159. .swdio_read = linuxgpiod_swdio_read,
  160. .swdio_drive = linuxgpiod_swdio_drive,
  161. .swd_write = linuxgpiod_swd_write,
  162. .blink = linuxgpiod_blink,
  163. };
  164. /*
  165. * Bitbang interface to manipulate reset lines SRST and TRST
  166. *
  167. * (1) assert or (0) deassert reset lines
  168. */
  169. static int linuxgpiod_reset(int trst, int srst)
  170. {
  171. int retval1 = 0, retval2 = 0;
  172. LOG_DEBUG("linuxgpiod_reset");
  173. /* assume active low */
  174. if (gpiod_srst) {
  175. retval1 = gpiod_line_set_value(gpiod_srst, srst ? 0 : 1);
  176. if (retval1 < 0)
  177. LOG_WARNING("set srst value failed");
  178. }
  179. /* assume active low */
  180. if (gpiod_trst) {
  181. retval2 = gpiod_line_set_value(gpiod_trst, trst ? 0 : 1);
  182. if (retval2 < 0)
  183. LOG_WARNING("set trst value failed");
  184. }
  185. return ((retval1 < 0) || (retval2 < 0)) ? ERROR_FAIL : ERROR_OK;
  186. }
  187. /*
  188. * Helper function to determine if gpio number is valid
  189. *
  190. * Assume here that there will be less than 10000 gpios per gpiochip
  191. */
  192. static bool is_gpio_valid(int gpio)
  193. {
  194. return gpio >= 0 && gpio < 10000;
  195. }
  196. static bool linuxgpiod_jtag_mode_possible(void)
  197. {
  198. if (!is_gpio_valid(tck_gpio))
  199. return false;
  200. if (!is_gpio_valid(tms_gpio))
  201. return false;
  202. if (!is_gpio_valid(tdi_gpio))
  203. return false;
  204. if (!is_gpio_valid(tdo_gpio))
  205. return false;
  206. return true;
  207. }
  208. static bool linuxgpiod_swd_mode_possible(void)
  209. {
  210. if (!is_gpio_valid(swclk_gpio))
  211. return false;
  212. if (!is_gpio_valid(swdio_gpio))
  213. return false;
  214. return true;
  215. }
  216. static inline void helper_release(struct gpiod_line *line)
  217. {
  218. if (line)
  219. gpiod_line_release(line);
  220. }
  221. static int linuxgpiod_quit(void)
  222. {
  223. helper_release(gpiod_led);
  224. helper_release(gpiod_srst);
  225. helper_release(gpiod_swdio);
  226. helper_release(gpiod_swclk);
  227. helper_release(gpiod_trst);
  228. helper_release(gpiod_tms);
  229. helper_release(gpiod_tck);
  230. helper_release(gpiod_tdi);
  231. helper_release(gpiod_tdo);
  232. gpiod_chip_close(gpiod_chip);
  233. return ERROR_OK;
  234. }
  235. static struct gpiod_line *helper_get_input_line(const char *label, unsigned int offset)
  236. {
  237. struct gpiod_line *line;
  238. int retval;
  239. line = gpiod_chip_get_line(gpiod_chip, offset);
  240. if (!line) {
  241. LOG_ERROR("Error get line %s", label);
  242. return NULL;
  243. }
  244. retval = gpiod_line_request_input(line, "OpenOCD");
  245. if (retval < 0) {
  246. LOG_ERROR("Error request_input line %s", label);
  247. return NULL;
  248. }
  249. return line;
  250. }
  251. static struct gpiod_line *helper_get_output_line(const char *label, unsigned int offset, int val)
  252. {
  253. struct gpiod_line *line;
  254. int retval;
  255. line = gpiod_chip_get_line(gpiod_chip, offset);
  256. if (!line) {
  257. LOG_ERROR("Error get line %s", label);
  258. return NULL;
  259. }
  260. retval = gpiod_line_request_output(line, "OpenOCD", val);
  261. if (retval < 0) {
  262. LOG_ERROR("Error request_output line %s", label);
  263. return NULL;
  264. }
  265. return line;
  266. }
  267. static int linuxgpiod_init(void)
  268. {
  269. LOG_INFO("Linux GPIOD JTAG/SWD bitbang driver");
  270. bitbang_interface = &linuxgpiod_bitbang;
  271. gpiod_chip = gpiod_chip_open_by_number(gpiochip);
  272. if (!gpiod_chip) {
  273. LOG_ERROR("Cannot open LinuxGPIOD gpiochip %d", gpiochip);
  274. return ERROR_JTAG_INIT_FAILED;
  275. }
  276. /*
  277. * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
  278. * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
  279. * For SWD, SWCLK and SWDIO are configures as output high.
  280. */
  281. if (transport_is_jtag()) {
  282. if (!linuxgpiod_jtag_mode_possible()) {
  283. LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode");
  284. goto out_error;
  285. }
  286. gpiod_tdo = helper_get_input_line("tdo", tdo_gpio);
  287. if (!gpiod_tdo)
  288. goto out_error;
  289. gpiod_tdi = helper_get_output_line("tdi", tdi_gpio, 0);
  290. if (!gpiod_tdi)
  291. goto out_error;
  292. gpiod_tck = helper_get_output_line("tck", tck_gpio, 0);
  293. if (!gpiod_tck)
  294. goto out_error;
  295. gpiod_tms = helper_get_output_line("tms", tms_gpio, 1);
  296. if (!gpiod_tms)
  297. goto out_error;
  298. if (is_gpio_valid(trst_gpio)) {
  299. gpiod_trst = helper_get_output_line("trst", trst_gpio, 1);
  300. if (!gpiod_trst)
  301. goto out_error;
  302. }
  303. }
  304. if (transport_is_swd()) {
  305. if (!linuxgpiod_swd_mode_possible()) {
  306. LOG_ERROR("Require swclk and swdio gpio for SWD mode");
  307. goto out_error;
  308. }
  309. gpiod_swclk = helper_get_output_line("swclk", swclk_gpio, 1);
  310. if (!gpiod_swclk)
  311. goto out_error;
  312. gpiod_swdio = helper_get_output_line("swdio", swdio_gpio, 1);
  313. if (!gpiod_swdio)
  314. goto out_error;
  315. }
  316. if (is_gpio_valid(srst_gpio)) {
  317. gpiod_srst = helper_get_output_line("srst", srst_gpio, 1);
  318. if (!gpiod_srst)
  319. goto out_error;
  320. }
  321. if (is_gpio_valid(led_gpio)) {
  322. gpiod_led = helper_get_output_line("led", led_gpio, 0);
  323. if (!gpiod_led)
  324. goto out_error;
  325. }
  326. return ERROR_OK;
  327. out_error:
  328. linuxgpiod_quit();
  329. return ERROR_JTAG_INIT_FAILED;
  330. }
  331. COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionums)
  332. {
  333. if (CMD_ARGC == 4) {
  334. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
  335. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpio);
  336. COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], tdi_gpio);
  337. COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], tdo_gpio);
  338. } else if (CMD_ARGC != 0) {
  339. return ERROR_COMMAND_SYNTAX_ERROR;
  340. }
  341. command_print(CMD,
  342. "LinuxGPIOD nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
  343. tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
  344. return ERROR_OK;
  345. }
  346. COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tck)
  347. {
  348. if (CMD_ARGC == 1)
  349. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
  350. command_print(CMD, "LinuxGPIOD num: tck = %d", tck_gpio);
  351. return ERROR_OK;
  352. }
  353. COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tms)
  354. {
  355. if (CMD_ARGC == 1)
  356. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
  357. command_print(CMD, "LinuxGPIOD num: tms = %d", tms_gpio);
  358. return ERROR_OK;
  359. }
  360. COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdo)
  361. {
  362. if (CMD_ARGC == 1)
  363. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
  364. command_print(CMD, "LinuxGPIOD num: tdo = %d", tdo_gpio);
  365. return ERROR_OK;
  366. }
  367. COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdi)
  368. {
  369. if (CMD_ARGC == 1)
  370. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
  371. command_print(CMD, "LinuxGPIOD num: tdi = %d", tdi_gpio);
  372. return ERROR_OK;
  373. }
  374. COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_srst)
  375. {
  376. if (CMD_ARGC == 1)
  377. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
  378. command_print(CMD, "LinuxGPIOD num: srst = %d", srst_gpio);
  379. return ERROR_OK;
  380. }
  381. COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_trst)
  382. {
  383. if (CMD_ARGC == 1)
  384. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
  385. command_print(CMD, "LinuxGPIOD num: trst = %d", trst_gpio);
  386. return ERROR_OK;
  387. }
  388. COMMAND_HANDLER(linuxgpiod_handle_swd_gpionums)
  389. {
  390. if (CMD_ARGC == 2) {
  391. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
  392. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], swdio_gpio);
  393. } else if (CMD_ARGC != 0) {
  394. return ERROR_COMMAND_SYNTAX_ERROR;
  395. }
  396. command_print(CMD,
  397. "LinuxGPIOD nums: swclk = %d, swdio = %d",
  398. swclk_gpio, swdio_gpio);
  399. return ERROR_OK;
  400. }
  401. COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swclk)
  402. {
  403. if (CMD_ARGC == 1)
  404. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
  405. command_print(CMD, "LinuxGPIOD num: swclk = %d", swclk_gpio);
  406. return ERROR_OK;
  407. }
  408. COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swdio)
  409. {
  410. if (CMD_ARGC == 1)
  411. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpio);
  412. command_print(CMD, "LinuxGPIOD num: swdio = %d", swdio_gpio);
  413. return ERROR_OK;
  414. }
  415. COMMAND_HANDLER(linuxgpiod_handle_gpionum_led)
  416. {
  417. if (CMD_ARGC == 1)
  418. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], led_gpio);
  419. command_print(CMD, "LinuxGPIOD num: led = %d", led_gpio);
  420. return ERROR_OK;
  421. }
  422. COMMAND_HANDLER(linuxgpiod_handle_gpiochip)
  423. {
  424. if (CMD_ARGC == 1)
  425. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], gpiochip);
  426. command_print(CMD, "LinuxGPIOD gpiochip = %d", gpiochip);
  427. return ERROR_OK;
  428. }
  429. static const struct command_registration linuxgpiod_subcommand_handlers[] = {
  430. {
  431. .name = "jtag_nums",
  432. .handler = linuxgpiod_handle_jtag_gpionums,
  433. .mode = COMMAND_CONFIG,
  434. .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
  435. .usage = "tck tms tdi tdo",
  436. },
  437. {
  438. .name = "tck_num",
  439. .handler = linuxgpiod_handle_jtag_gpionum_tck,
  440. .mode = COMMAND_CONFIG,
  441. .help = "gpio number for tck.",
  442. .usage = "tck",
  443. },
  444. {
  445. .name = "tms_num",
  446. .handler = linuxgpiod_handle_jtag_gpionum_tms,
  447. .mode = COMMAND_CONFIG,
  448. .help = "gpio number for tms.",
  449. .usage = "tms",
  450. },
  451. {
  452. .name = "tdo_num",
  453. .handler = linuxgpiod_handle_jtag_gpionum_tdo,
  454. .mode = COMMAND_CONFIG,
  455. .help = "gpio number for tdo.",
  456. .usage = "tdo",
  457. },
  458. {
  459. .name = "tdi_num",
  460. .handler = linuxgpiod_handle_jtag_gpionum_tdi,
  461. .mode = COMMAND_CONFIG,
  462. .help = "gpio number for tdi.",
  463. .usage = "tdi",
  464. },
  465. {
  466. .name = "srst_num",
  467. .handler = linuxgpiod_handle_jtag_gpionum_srst,
  468. .mode = COMMAND_CONFIG,
  469. .help = "gpio number for srst.",
  470. .usage = "srst",
  471. },
  472. {
  473. .name = "trst_num",
  474. .handler = linuxgpiod_handle_jtag_gpionum_trst,
  475. .mode = COMMAND_CONFIG,
  476. .help = "gpio number for trst.",
  477. .usage = "trst",
  478. },
  479. {
  480. .name = "swd_nums",
  481. .handler = linuxgpiod_handle_swd_gpionums,
  482. .mode = COMMAND_CONFIG,
  483. .help = "gpio numbers for swclk, swdio. (in that order)",
  484. .usage = "swclk swdio",
  485. },
  486. {
  487. .name = "swclk_num",
  488. .handler = linuxgpiod_handle_swd_gpionum_swclk,
  489. .mode = COMMAND_CONFIG,
  490. .help = "gpio number for swclk.",
  491. .usage = "swclk",
  492. },
  493. {
  494. .name = "swdio_num",
  495. .handler = linuxgpiod_handle_swd_gpionum_swdio,
  496. .mode = COMMAND_CONFIG,
  497. .help = "gpio number for swdio.",
  498. .usage = "swdio",
  499. },
  500. {
  501. .name = "led_num",
  502. .handler = linuxgpiod_handle_gpionum_led,
  503. .mode = COMMAND_CONFIG,
  504. .help = "gpio number for LED.",
  505. .usage = "led",
  506. },
  507. {
  508. .name = "gpiochip",
  509. .handler = linuxgpiod_handle_gpiochip,
  510. .mode = COMMAND_CONFIG,
  511. .help = "number of the gpiochip.",
  512. .usage = "gpiochip",
  513. },
  514. COMMAND_REGISTRATION_DONE
  515. };
  516. static const struct command_registration linuxgpiod_command_handlers[] = {
  517. {
  518. .name = "linuxgpiod",
  519. .mode = COMMAND_ANY,
  520. .help = "perform linuxgpiod management",
  521. .chain = linuxgpiod_subcommand_handlers,
  522. .usage = "",
  523. },
  524. COMMAND_REGISTRATION_DONE
  525. };
  526. static const char *const linuxgpiod_transport[] = { "swd", "jtag", NULL };
  527. static struct jtag_interface linuxgpiod_interface = {
  528. .supported = DEBUG_CAP_TMS_SEQ,
  529. .execute_queue = bitbang_execute_queue,
  530. };
  531. struct adapter_driver linuxgpiod_adapter_driver = {
  532. .name = "linuxgpiod",
  533. .transports = linuxgpiod_transport,
  534. .commands = linuxgpiod_command_handlers,
  535. .init = linuxgpiod_init,
  536. .quit = linuxgpiod_quit,
  537. .reset = linuxgpiod_reset,
  538. .jtag_ops = &linuxgpiod_interface,
  539. .swd_ops = &bitbang_swd,
  540. };