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.
 
 
 
 
 
 

500 lines
13 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, 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. #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. if (is_output)
  129. ret = open(buf, O_WRONLY | O_NONBLOCK | O_SYNC);
  130. else
  131. ret = open(buf, O_RDONLY | O_NONBLOCK | O_SYNC);
  132. if (ret < 0)
  133. unexport_sysfs_gpio(gpio);
  134. return ret;
  135. }
  136. /*
  137. * file descriptors for /sys/class/gpio/gpioXX/value
  138. * Set up during init.
  139. */
  140. static int tck_fd = -1;
  141. static int tms_fd = -1;
  142. static int tdi_fd = -1;
  143. static int tdo_fd = -1;
  144. static int trst_fd = -1;
  145. static int srst_fd = -1;
  146. /*
  147. * Bitbang interface read of TDO
  148. *
  149. * The sysfs value will read back either '0' or '1'. The trick here is to call
  150. * lseek to bypass buffering in the sysfs kernel driver.
  151. */
  152. static int sysfsgpio_read(void)
  153. {
  154. char buf[1];
  155. /* important to seek to signal sysfs of new read */
  156. lseek(tdo_fd, 0, SEEK_SET);
  157. int ret = read(tdo_fd, &buf, sizeof(buf));
  158. if (ret < 0) {
  159. LOG_WARNING("reading tdo failed");
  160. return 0;
  161. }
  162. return buf[0] == '1';
  163. }
  164. /*
  165. * Bitbang interface write of TCK, TMS, TDI
  166. *
  167. * Seeing as this is the only function where the outputs are changed,
  168. * we can cache the old value to avoid needlessly writing it.
  169. */
  170. static void sysfsgpio_write(int tck, int tms, int tdi)
  171. {
  172. const char one[] = "1";
  173. const char zero[] = "0";
  174. static int last_tck;
  175. static int last_tms;
  176. static int last_tdi;
  177. static int first_time;
  178. size_t bytes_written;
  179. if (!first_time) {
  180. last_tck = !tck;
  181. last_tms = !tms;
  182. last_tdi = !tdi;
  183. first_time = 1;
  184. }
  185. if (tdi != last_tdi) {
  186. bytes_written = write(tdi_fd, tdi ? &one : &zero, 1);
  187. if (bytes_written != 1)
  188. LOG_WARNING("writing tdi failed");
  189. }
  190. if (tms != last_tms) {
  191. bytes_written = write(tms_fd, tms ? &one : &zero, 1);
  192. if (bytes_written != 1)
  193. LOG_WARNING("writing tms failed");
  194. }
  195. /* write clk last */
  196. if (tck != last_tck) {
  197. bytes_written = write(tck_fd, tck ? &one : &zero, 1);
  198. if (bytes_written != 1)
  199. LOG_WARNING("writing tck failed");
  200. }
  201. last_tdi = tdi;
  202. last_tms = tms;
  203. last_tck = tck;
  204. }
  205. /*
  206. * Bitbang interface to manipulate reset lines SRST and TRST
  207. *
  208. * (1) assert or (0) deassert reset lines
  209. */
  210. static void sysfsgpio_reset(int trst, int srst)
  211. {
  212. const char one[] = "1";
  213. const char zero[] = "0";
  214. size_t bytes_written;
  215. /* assume active low */
  216. if (srst_fd >= 0) {
  217. bytes_written = write(srst_fd, srst ? &zero : &one, 1);
  218. if (bytes_written != 1)
  219. LOG_WARNING("writing srst failed");
  220. }
  221. /* assume active low */
  222. if (trst_fd >= 0) {
  223. bytes_written = write(trst_fd, trst ? &zero : &one, 1);
  224. if (bytes_written != 1)
  225. LOG_WARNING("writing trst failed");
  226. }
  227. }
  228. /* gpio numbers for each gpio. Negative values are invalid */
  229. static int tck_gpio = -1;
  230. static int tms_gpio = -1;
  231. static int tdi_gpio = -1;
  232. static int tdo_gpio = -1;
  233. static int trst_gpio = -1;
  234. static int srst_gpio = -1;
  235. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionums)
  236. {
  237. if (CMD_ARGC == 4) {
  238. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
  239. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpio);
  240. COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], tdi_gpio);
  241. COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], tdo_gpio);
  242. } else if (CMD_ARGC != 0) {
  243. return ERROR_COMMAND_SYNTAX_ERROR;
  244. }
  245. command_print(CMD_CTX,
  246. "SysfsGPIO nums: tck = %d, tms = %d, tdi = %d, tdi = %d",
  247. tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
  248. return ERROR_OK;
  249. }
  250. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tck)
  251. {
  252. if (CMD_ARGC == 1)
  253. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
  254. command_print(CMD_CTX, "SysfsGPIO num: tck = %d", tck_gpio);
  255. return ERROR_OK;
  256. }
  257. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tms)
  258. {
  259. if (CMD_ARGC == 1)
  260. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
  261. command_print(CMD_CTX, "SysfsGPIO num: tms = %d", tms_gpio);
  262. return ERROR_OK;
  263. }
  264. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tdo)
  265. {
  266. if (CMD_ARGC == 1)
  267. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
  268. command_print(CMD_CTX, "SysfsGPIO num: tdo = %d", tdo_gpio);
  269. return ERROR_OK;
  270. }
  271. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_tdi)
  272. {
  273. if (CMD_ARGC == 1)
  274. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
  275. command_print(CMD_CTX, "SysfsGPIO num: tdi = %d", tdi_gpio);
  276. return ERROR_OK;
  277. }
  278. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_srst)
  279. {
  280. if (CMD_ARGC == 1)
  281. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
  282. command_print(CMD_CTX, "SysfsGPIO num: srst = %d", srst_gpio);
  283. return ERROR_OK;
  284. }
  285. COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionum_trst)
  286. {
  287. if (CMD_ARGC == 1)
  288. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
  289. command_print(CMD_CTX, "SysfsGPIO num: trst = %d", trst_gpio);
  290. return ERROR_OK;
  291. }
  292. static const struct command_registration sysfsgpio_command_handlers[] = {
  293. {
  294. .name = "sysfsgpio_jtag_nums",
  295. .handler = &sysfsgpio_handle_jtag_gpionums,
  296. .mode = COMMAND_CONFIG,
  297. .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
  298. .usage = "(tck tms tdi tdo)* ",
  299. },
  300. {
  301. .name = "sysfsgpio_tck_num",
  302. .handler = &sysfsgpio_handle_jtag_gpionum_tck,
  303. .mode = COMMAND_CONFIG,
  304. .help = "gpio number for tck.",
  305. },
  306. {
  307. .name = "sysfsgpio_tms_num",
  308. .handler = &sysfsgpio_handle_jtag_gpionum_tms,
  309. .mode = COMMAND_CONFIG,
  310. .help = "gpio number for tms.",
  311. },
  312. {
  313. .name = "sysfsgpio_tdo_num",
  314. .handler = &sysfsgpio_handle_jtag_gpionum_tdo,
  315. .mode = COMMAND_CONFIG,
  316. .help = "gpio number for tdo.",
  317. },
  318. {
  319. .name = "sysfsgpio_tdi_num",
  320. .handler = &sysfsgpio_handle_jtag_gpionum_tdi,
  321. .mode = COMMAND_CONFIG,
  322. .help = "gpio number for tdi.",
  323. },
  324. {
  325. .name = "sysfsgpio_srst_num",
  326. .handler = &sysfsgpio_handle_jtag_gpionum_srst,
  327. .mode = COMMAND_CONFIG,
  328. .help = "gpio number for srst.",
  329. },
  330. {
  331. .name = "sysfsgpio_trst_num",
  332. .handler = &sysfsgpio_handle_jtag_gpionum_trst,
  333. .mode = COMMAND_CONFIG,
  334. .help = "gpio number for trst.",
  335. },
  336. COMMAND_REGISTRATION_DONE
  337. };
  338. static int sysfsgpio_init(void);
  339. static int sysfsgpio_quit(void);
  340. struct jtag_interface sysfsgpio_interface = {
  341. .name = "sysfsgpio",
  342. .supported = DEBUG_CAP_TMS_SEQ,
  343. .execute_queue = bitbang_execute_queue,
  344. .transports = jtag_only,
  345. .commands = sysfsgpio_command_handlers,
  346. .init = sysfsgpio_init,
  347. .quit = sysfsgpio_quit,
  348. };
  349. static struct bitbang_interface sysfsgpio_bitbang = {
  350. .read = sysfsgpio_read,
  351. .write = sysfsgpio_write,
  352. .reset = sysfsgpio_reset,
  353. .blink = 0
  354. };
  355. /* helper func to close and cleanup files only if they were valid/ used */
  356. static void cleanup_fd(int fd, int gpio)
  357. {
  358. if (gpio >= 0) {
  359. if (fd >= 0)
  360. close(fd);
  361. unexport_sysfs_gpio(gpio);
  362. }
  363. }
  364. static void cleanup_all_fds(void)
  365. {
  366. cleanup_fd(tck_fd, tck_gpio);
  367. cleanup_fd(tms_fd, tms_gpio);
  368. cleanup_fd(tdi_fd, tdi_gpio);
  369. cleanup_fd(tdo_fd, tdo_gpio);
  370. cleanup_fd(trst_fd, trst_gpio);
  371. cleanup_fd(srst_fd, srst_gpio);
  372. }
  373. static int sysfsgpio_init(void)
  374. {
  375. bitbang_interface = &sysfsgpio_bitbang;
  376. LOG_INFO("SysfsGPIO JTAG bitbang driver");
  377. if (!(is_gpio_valid(tck_gpio)
  378. && is_gpio_valid(tms_gpio)
  379. && is_gpio_valid(tdi_gpio)
  380. && is_gpio_valid(tdo_gpio))) {
  381. if (!is_gpio_valid(tck_gpio))
  382. LOG_ERROR("gpio num for tck is invalid");
  383. if (!is_gpio_valid(tms_gpio))
  384. LOG_ERROR("gpio num for tms is invalid");
  385. if (!is_gpio_valid(tdo_gpio))
  386. LOG_ERROR("gpio num for tdo is invalid");
  387. if (!is_gpio_valid(tdi_gpio))
  388. LOG_ERROR("gpio num for tdi is invalid");
  389. LOG_ERROR("Require tck, tms, tdi and tdo gpios to all be specified");
  390. return ERROR_JTAG_INIT_FAILED;
  391. }
  392. if (!is_gpio_valid(trst_gpio) && !is_gpio_valid(srst_gpio)) {
  393. LOG_ERROR("Require at least one of trst or srst gpios to be specified");
  394. return ERROR_JTAG_INIT_FAILED;
  395. }
  396. /*
  397. * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
  398. * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
  399. */
  400. tck_fd = setup_sysfs_gpio(tck_gpio, 1, 0);
  401. if (tck_fd < 0)
  402. goto out_error;
  403. tms_fd = setup_sysfs_gpio(tms_gpio, 1, 1);
  404. if (tms_fd < 0)
  405. goto out_error;
  406. tdi_fd = setup_sysfs_gpio(tdi_gpio, 1, 0);
  407. if (tdi_fd < 0)
  408. goto out_error;
  409. tdo_fd = setup_sysfs_gpio(tdo_gpio, 0, 0);
  410. if (tdo_fd < 0)
  411. goto out_error;
  412. /* assume active low*/
  413. trst_fd = setup_sysfs_gpio(trst_gpio, 1, 1);
  414. if (trst_gpio > 0 && trst_fd < 0)
  415. goto out_error;
  416. /* assume active low*/
  417. srst_fd = setup_sysfs_gpio(srst_gpio, 1, 1);
  418. if (srst_gpio > 0 && srst_fd < 0)
  419. goto out_error;
  420. return ERROR_OK;
  421. out_error:
  422. cleanup_all_fds();
  423. return ERROR_JTAG_INIT_FAILED;
  424. }
  425. static int sysfsgpio_quit(void)
  426. {
  427. cleanup_all_fds();
  428. return ERROR_OK;
  429. }