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.
 
 
 
 
 
 

532 lines
14 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2013 by Paul Fertser, fercerpav@gmail.com *
  3. * *
  4. * Copyright (C) 2012 by Creative Product Design, marc @ cpdesign.com.au *
  5. * Based on at91rm9200.c (c) Anders Larsen *
  6. * and RPi GPIO examples by Gert van Loo & Dom *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  20. ***************************************************************************/
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include <jtag/interface.h>
  25. #include "bitbang.h"
  26. #include <sys/mman.h>
  27. uint32_t bcm2835_peri_base = 0x20000000;
  28. #define BCM2835_GPIO_BASE (bcm2835_peri_base + 0x200000) /* GPIO controller */
  29. #define BCM2835_PADS_GPIO_0_27 (bcm2835_peri_base + 0x100000)
  30. #define BCM2835_PADS_GPIO_0_27_OFFSET (0x2c / 4)
  31. /* GPIO setup macros */
  32. #define MODE_GPIO(g) (*(pio_base+((g)/10))>>(((g)%10)*3) & 7)
  33. #define INP_GPIO(g) do { *(pio_base+((g)/10)) &= ~(7<<(((g)%10)*3)); } while (0)
  34. #define SET_MODE_GPIO(g, m) do { /* clear the mode bits first, then set as necessary */ \
  35. INP_GPIO(g); \
  36. *(pio_base+((g)/10)) |= ((m)<<(((g)%10)*3)); } while (0)
  37. #define OUT_GPIO(g) SET_MODE_GPIO(g, 1)
  38. #define GPIO_SET (*(pio_base+7)) /* sets bits which are 1, ignores bits which are 0 */
  39. #define GPIO_CLR (*(pio_base+10)) /* clears bits which are 1, ignores bits which are 0 */
  40. #define GPIO_LEV (*(pio_base+13)) /* current level of the pin */
  41. static int dev_mem_fd;
  42. static volatile uint32_t *pio_base;
  43. static int bcm2835gpio_read(void);
  44. static void bcm2835gpio_write(int tck, int tms, int tdi);
  45. static void bcm2835gpio_reset(int trst, int srst);
  46. static int bcm2835_swdio_read(void);
  47. static void bcm2835_swdio_drive(bool is_output);
  48. static int bcm2835gpio_init(void);
  49. static int bcm2835gpio_quit(void);
  50. static struct bitbang_interface bcm2835gpio_bitbang = {
  51. .read = bcm2835gpio_read,
  52. .write = bcm2835gpio_write,
  53. .reset = bcm2835gpio_reset,
  54. .swdio_read = bcm2835_swdio_read,
  55. .swdio_drive = bcm2835_swdio_drive,
  56. .blink = NULL
  57. };
  58. /* GPIO numbers for each signal. Negative values are invalid */
  59. static int tck_gpio = -1;
  60. static int tck_gpio_mode;
  61. static int tms_gpio = -1;
  62. static int tms_gpio_mode;
  63. static int tdi_gpio = -1;
  64. static int tdi_gpio_mode;
  65. static int tdo_gpio = -1;
  66. static int tdo_gpio_mode;
  67. static int trst_gpio = -1;
  68. static int trst_gpio_mode;
  69. static int srst_gpio = -1;
  70. static int srst_gpio_mode;
  71. static int swclk_gpio = -1;
  72. static int swclk_gpio_mode;
  73. static int swdio_gpio = -1;
  74. static int swdio_gpio_mode;
  75. /* Transition delay coefficients */
  76. static int speed_coeff = 113714;
  77. static int speed_offset = 28;
  78. static unsigned int jtag_delay;
  79. static int bcm2835gpio_read(void)
  80. {
  81. return !!(GPIO_LEV & 1<<tdo_gpio);
  82. }
  83. static void bcm2835gpio_write(int tck, int tms, int tdi)
  84. {
  85. uint32_t set = tck<<tck_gpio | tms<<tms_gpio | tdi<<tdi_gpio;
  86. uint32_t clear = !tck<<tck_gpio | !tms<<tms_gpio | !tdi<<tdi_gpio;
  87. GPIO_SET = set;
  88. GPIO_CLR = clear;
  89. for (unsigned int i = 0; i < jtag_delay; i++)
  90. asm volatile ("");
  91. }
  92. static void bcm2835gpio_swd_write(int tck, int tms, int tdi)
  93. {
  94. uint32_t set = tck<<swclk_gpio | tdi<<swdio_gpio;
  95. uint32_t clear = !tck<<swclk_gpio | !tdi<<swdio_gpio;
  96. GPIO_SET = set;
  97. GPIO_CLR = clear;
  98. for (unsigned int i = 0; i < jtag_delay; i++)
  99. asm volatile ("");
  100. }
  101. /* (1) assert or (0) deassert reset lines */
  102. static void bcm2835gpio_reset(int trst, int srst)
  103. {
  104. uint32_t set = 0;
  105. uint32_t clear = 0;
  106. if (trst_gpio > 0) {
  107. set |= !trst<<trst_gpio;
  108. clear |= trst<<trst_gpio;
  109. }
  110. if (srst_gpio > 0) {
  111. set |= !srst<<srst_gpio;
  112. clear |= srst<<srst_gpio;
  113. }
  114. GPIO_SET = set;
  115. GPIO_CLR = clear;
  116. }
  117. static void bcm2835_swdio_drive(bool is_output)
  118. {
  119. if (is_output)
  120. OUT_GPIO(swdio_gpio);
  121. else
  122. INP_GPIO(swdio_gpio);
  123. }
  124. static int bcm2835_swdio_read(void)
  125. {
  126. return !!(GPIO_LEV & 1 << swdio_gpio);
  127. }
  128. static int bcm2835gpio_khz(int khz, int *jtag_speed)
  129. {
  130. if (!khz) {
  131. LOG_DEBUG("RCLK not supported");
  132. return ERROR_FAIL;
  133. }
  134. *jtag_speed = speed_coeff/khz - speed_offset;
  135. if (*jtag_speed < 0)
  136. *jtag_speed = 0;
  137. return ERROR_OK;
  138. }
  139. static int bcm2835gpio_speed_div(int speed, int *khz)
  140. {
  141. *khz = speed_coeff/(speed + speed_offset);
  142. return ERROR_OK;
  143. }
  144. static int bcm2835gpio_speed(int speed)
  145. {
  146. jtag_delay = speed;
  147. return ERROR_OK;
  148. }
  149. static int is_gpio_valid(int gpio)
  150. {
  151. return gpio >= 0 && gpio <= 53;
  152. }
  153. COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionums)
  154. {
  155. if (CMD_ARGC == 4) {
  156. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
  157. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpio);
  158. COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], tdi_gpio);
  159. COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], tdo_gpio);
  160. } else if (CMD_ARGC != 0) {
  161. return ERROR_COMMAND_SYNTAX_ERROR;
  162. }
  163. command_print(CMD_CTX,
  164. "BCM2835 GPIO config: tck = %d, tms = %d, tdi = %d, tdo = %d",
  165. tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
  166. return ERROR_OK;
  167. }
  168. COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionum_tck)
  169. {
  170. if (CMD_ARGC == 1)
  171. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
  172. command_print(CMD_CTX, "BCM2835 GPIO config: tck = %d", tck_gpio);
  173. return ERROR_OK;
  174. }
  175. COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionum_tms)
  176. {
  177. if (CMD_ARGC == 1)
  178. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio);
  179. command_print(CMD_CTX, "BCM2835 GPIO config: tms = %d", tms_gpio);
  180. return ERROR_OK;
  181. }
  182. COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionum_tdo)
  183. {
  184. if (CMD_ARGC == 1)
  185. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio);
  186. command_print(CMD_CTX, "BCM2835 GPIO config: tdo = %d", tdo_gpio);
  187. return ERROR_OK;
  188. }
  189. COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionum_tdi)
  190. {
  191. if (CMD_ARGC == 1)
  192. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio);
  193. command_print(CMD_CTX, "BCM2835 GPIO config: tdi = %d", tdi_gpio);
  194. return ERROR_OK;
  195. }
  196. COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionum_srst)
  197. {
  198. if (CMD_ARGC == 1)
  199. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio);
  200. command_print(CMD_CTX, "BCM2835 GPIO config: srst = %d", srst_gpio);
  201. return ERROR_OK;
  202. }
  203. COMMAND_HANDLER(bcm2835gpio_handle_jtag_gpionum_trst)
  204. {
  205. if (CMD_ARGC == 1)
  206. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio);
  207. command_print(CMD_CTX, "BCM2835 GPIO config: trst = %d", trst_gpio);
  208. return ERROR_OK;
  209. }
  210. COMMAND_HANDLER(bcm2835gpio_handle_swd_gpionums)
  211. {
  212. if (CMD_ARGC == 2) {
  213. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
  214. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], swdio_gpio);
  215. } else if (CMD_ARGC != 0) {
  216. return ERROR_COMMAND_SYNTAX_ERROR;
  217. }
  218. command_print(CMD_CTX,
  219. "BCM2835 GPIO nums: swclk = %d, swdio = %d",
  220. swclk_gpio, swdio_gpio);
  221. return ERROR_OK;
  222. }
  223. COMMAND_HANDLER(bcm2835gpio_handle_swd_gpionum_swclk)
  224. {
  225. if (CMD_ARGC == 1)
  226. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
  227. command_print(CMD_CTX, "BCM2835 num: swclk = %d", swclk_gpio);
  228. return ERROR_OK;
  229. }
  230. COMMAND_HANDLER(bcm2835gpio_handle_swd_gpionum_swdio)
  231. {
  232. if (CMD_ARGC == 1)
  233. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpio);
  234. command_print(CMD_CTX, "BCM2835 num: swdio = %d", swdio_gpio);
  235. return ERROR_OK;
  236. }
  237. COMMAND_HANDLER(bcm2835gpio_handle_speed_coeffs)
  238. {
  239. if (CMD_ARGC == 2) {
  240. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], speed_coeff);
  241. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], speed_offset);
  242. }
  243. return ERROR_OK;
  244. }
  245. COMMAND_HANDLER(bcm2835gpio_handle_peripheral_base)
  246. {
  247. if (CMD_ARGC == 1)
  248. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bcm2835_peri_base);
  249. return ERROR_OK;
  250. }
  251. static const struct command_registration bcm2835gpio_command_handlers[] = {
  252. {
  253. .name = "bcm2835gpio_jtag_nums",
  254. .handler = &bcm2835gpio_handle_jtag_gpionums,
  255. .mode = COMMAND_CONFIG,
  256. .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
  257. .usage = "(tck tms tdi tdo)* ",
  258. },
  259. {
  260. .name = "bcm2835gpio_tck_num",
  261. .handler = &bcm2835gpio_handle_jtag_gpionum_tck,
  262. .mode = COMMAND_CONFIG,
  263. .help = "gpio number for tck.",
  264. },
  265. {
  266. .name = "bcm2835gpio_tms_num",
  267. .handler = &bcm2835gpio_handle_jtag_gpionum_tms,
  268. .mode = COMMAND_CONFIG,
  269. .help = "gpio number for tms.",
  270. },
  271. {
  272. .name = "bcm2835gpio_tdo_num",
  273. .handler = &bcm2835gpio_handle_jtag_gpionum_tdo,
  274. .mode = COMMAND_CONFIG,
  275. .help = "gpio number for tdo.",
  276. },
  277. {
  278. .name = "bcm2835gpio_tdi_num",
  279. .handler = &bcm2835gpio_handle_jtag_gpionum_tdi,
  280. .mode = COMMAND_CONFIG,
  281. .help = "gpio number for tdi.",
  282. },
  283. {
  284. .name = "bcm2835gpio_swd_nums",
  285. .handler = &bcm2835gpio_handle_swd_gpionums,
  286. .mode = COMMAND_CONFIG,
  287. .help = "gpio numbers for swclk, swdio. (in that order)",
  288. .usage = "(swclk swdio)* ",
  289. },
  290. {
  291. .name = "bcm2835gpio_swclk_num",
  292. .handler = &bcm2835gpio_handle_swd_gpionum_swclk,
  293. .mode = COMMAND_CONFIG,
  294. .help = "gpio number for swclk.",
  295. },
  296. {
  297. .name = "bcm2835gpio_swdio_num",
  298. .handler = &bcm2835gpio_handle_swd_gpionum_swdio,
  299. .mode = COMMAND_CONFIG,
  300. .help = "gpio number for swdio.",
  301. },
  302. {
  303. .name = "bcm2835gpio_srst_num",
  304. .handler = &bcm2835gpio_handle_jtag_gpionum_srst,
  305. .mode = COMMAND_CONFIG,
  306. .help = "gpio number for srst.",
  307. },
  308. {
  309. .name = "bcm2835gpio_trst_num",
  310. .handler = &bcm2835gpio_handle_jtag_gpionum_trst,
  311. .mode = COMMAND_CONFIG,
  312. .help = "gpio number for trst.",
  313. },
  314. {
  315. .name = "bcm2835gpio_speed_coeffs",
  316. .handler = &bcm2835gpio_handle_speed_coeffs,
  317. .mode = COMMAND_CONFIG,
  318. .help = "SPEED_COEFF and SPEED_OFFSET for delay calculations.",
  319. },
  320. {
  321. .name = "bcm2835gpio_peripheral_base",
  322. .handler = &bcm2835gpio_handle_peripheral_base,
  323. .mode = COMMAND_CONFIG,
  324. .help = "peripheral base to access GPIOs (RPi1 0x20000000, RPi2 0x3F000000).",
  325. },
  326. COMMAND_REGISTRATION_DONE
  327. };
  328. static const char * const bcm2835_transports[] = { "jtag", "swd", NULL };
  329. struct jtag_interface bcm2835gpio_interface = {
  330. .name = "bcm2835gpio",
  331. .supported = DEBUG_CAP_TMS_SEQ,
  332. .execute_queue = bitbang_execute_queue,
  333. .transports = bcm2835_transports,
  334. .swd = &bitbang_swd,
  335. .speed = bcm2835gpio_speed,
  336. .khz = bcm2835gpio_khz,
  337. .speed_div = bcm2835gpio_speed_div,
  338. .commands = bcm2835gpio_command_handlers,
  339. .init = bcm2835gpio_init,
  340. .quit = bcm2835gpio_quit,
  341. };
  342. static bool bcm2835gpio_jtag_mode_possible(void)
  343. {
  344. if (!is_gpio_valid(tck_gpio))
  345. return 0;
  346. if (!is_gpio_valid(tms_gpio))
  347. return 0;
  348. if (!is_gpio_valid(tdi_gpio))
  349. return 0;
  350. if (!is_gpio_valid(tdo_gpio))
  351. return 0;
  352. return 1;
  353. }
  354. static bool bcm2835gpio_swd_mode_possible(void)
  355. {
  356. if (!is_gpio_valid(swclk_gpio))
  357. return 0;
  358. if (!is_gpio_valid(swdio_gpio))
  359. return 0;
  360. return 1;
  361. }
  362. static int bcm2835gpio_init(void)
  363. {
  364. bitbang_interface = &bcm2835gpio_bitbang;
  365. LOG_INFO("BCM2835 GPIO JTAG/SWD bitbang driver");
  366. if (bcm2835gpio_jtag_mode_possible()) {
  367. if (bcm2835gpio_swd_mode_possible())
  368. LOG_INFO("JTAG and SWD modes enabled");
  369. else
  370. LOG_INFO("JTAG only mode enabled (specify swclk and swdio gpio to add SWD mode)");
  371. if (!is_gpio_valid(trst_gpio) && !is_gpio_valid(srst_gpio)) {
  372. LOG_ERROR("Require at least one of trst or srst gpios to be specified");
  373. return ERROR_JTAG_INIT_FAILED;
  374. }
  375. } else if (bcm2835gpio_swd_mode_possible()) {
  376. LOG_INFO("SWD only mode enabled (specify tck, tms, tdi and tdo gpios to add JTAG mode)");
  377. } else {
  378. LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode and/or swclk and swdio gpio for SWD mode");
  379. return ERROR_JTAG_INIT_FAILED;
  380. }
  381. dev_mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
  382. if (dev_mem_fd < 0) {
  383. perror("open");
  384. return ERROR_JTAG_INIT_FAILED;
  385. }
  386. pio_base = mmap(NULL, sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE,
  387. MAP_SHARED, dev_mem_fd, BCM2835_GPIO_BASE);
  388. if (pio_base == MAP_FAILED) {
  389. perror("mmap");
  390. close(dev_mem_fd);
  391. return ERROR_JTAG_INIT_FAILED;
  392. }
  393. static volatile uint32_t *pads_base;
  394. pads_base = mmap(NULL, sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE,
  395. MAP_SHARED, dev_mem_fd, BCM2835_PADS_GPIO_0_27);
  396. if (pads_base == MAP_FAILED) {
  397. perror("mmap");
  398. close(dev_mem_fd);
  399. return ERROR_JTAG_INIT_FAILED;
  400. }
  401. /* set 16mA drive strength */
  402. pads_base[BCM2835_PADS_GPIO_0_27_OFFSET] = 0x5a000018 + 7;
  403. tdo_gpio_mode = MODE_GPIO(tdo_gpio);
  404. tdi_gpio_mode = MODE_GPIO(tdi_gpio);
  405. tck_gpio_mode = MODE_GPIO(tck_gpio);
  406. tms_gpio_mode = MODE_GPIO(tms_gpio);
  407. swclk_gpio_mode = MODE_GPIO(swclk_gpio);
  408. swdio_gpio_mode = MODE_GPIO(swdio_gpio);
  409. /*
  410. * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
  411. * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
  412. */
  413. INP_GPIO(tdo_gpio);
  414. GPIO_CLR = 1<<tdi_gpio | 1<<tck_gpio | 1<<swdio_gpio | 1<<swclk_gpio;
  415. GPIO_SET = 1<<tms_gpio;
  416. OUT_GPIO(tdi_gpio);
  417. OUT_GPIO(tck_gpio);
  418. OUT_GPIO(tms_gpio);
  419. OUT_GPIO(swclk_gpio);
  420. OUT_GPIO(swdio_gpio);
  421. if (trst_gpio != -1) {
  422. trst_gpio_mode = MODE_GPIO(trst_gpio);
  423. GPIO_SET = 1 << trst_gpio;
  424. OUT_GPIO(trst_gpio);
  425. }
  426. if (srst_gpio != -1) {
  427. srst_gpio_mode = MODE_GPIO(srst_gpio);
  428. GPIO_SET = 1 << srst_gpio;
  429. OUT_GPIO(srst_gpio);
  430. }
  431. LOG_DEBUG("saved pinmux settings: tck %d tms %d tdi %d "
  432. "tdo %d trst %d srst %d", tck_gpio_mode, tms_gpio_mode,
  433. tdi_gpio_mode, tdo_gpio_mode, trst_gpio_mode, srst_gpio_mode);
  434. if (swd_mode) {
  435. bcm2835gpio_bitbang.write = bcm2835gpio_swd_write;
  436. bitbang_switch_to_swd();
  437. }
  438. return ERROR_OK;
  439. }
  440. static int bcm2835gpio_quit(void)
  441. {
  442. SET_MODE_GPIO(tdo_gpio, tdo_gpio_mode);
  443. SET_MODE_GPIO(tdi_gpio, tdi_gpio_mode);
  444. SET_MODE_GPIO(tck_gpio, tck_gpio_mode);
  445. SET_MODE_GPIO(tms_gpio, tms_gpio_mode);
  446. SET_MODE_GPIO(swclk_gpio, swclk_gpio_mode);
  447. SET_MODE_GPIO(swdio_gpio, swdio_gpio_mode);
  448. if (trst_gpio != -1)
  449. SET_MODE_GPIO(trst_gpio, trst_gpio_mode);
  450. if (srst_gpio != -1)
  451. SET_MODE_GPIO(srst_gpio, srst_gpio_mode);
  452. return ERROR_OK;
  453. }