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.
 
 
 
 
 
 

278 lines
7.9 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2006 by Anders Larsen *
  3. * al@alarsen.net *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <jtag/interface.h>
  22. #include "bitbang.h"
  23. #include <sys/mman.h>
  24. /* AT91RM9200 */
  25. #define AT91C_BASE_SYS (0xfffff000)
  26. /* GPIO assignment */
  27. #define PIOA (0 << 7)
  28. #define PIOB (1 << 7)
  29. #define PIOC (2 << 7)
  30. #define PIOD (3 << 7)
  31. #define PIO_PER (0) /* PIO enable */
  32. #define PIO_OER (4) /* output enable */
  33. #define PIO_ODR (5) /* output disable */
  34. #define PIO_SODR (12) /* set output data */
  35. #define PIO_CODR (13) /* clear output data */
  36. #define PIO_PDSR (15) /* pin data status */
  37. #define PIO_PPUER (25) /* pull-up enable */
  38. #define NC (0) /* not connected */
  39. #define P0 (1 << 0)
  40. #define P1 (1 << 1)
  41. #define P2 (1 << 2)
  42. #define P3 (1 << 3)
  43. #define P4 (1 << 4)
  44. #define P5 (1 << 5)
  45. #define P6 (1 << 6)
  46. #define P7 (1 << 7)
  47. #define P8 (1 << 8)
  48. #define P9 (1 << 9)
  49. #define P10 (1 << 10)
  50. #define P11 (1 << 11)
  51. #define P12 (1 << 12)
  52. #define P13 (1 << 13)
  53. #define P14 (1 << 14)
  54. #define P15 (1 << 15)
  55. #define P16 (1 << 16)
  56. #define P17 (1 << 17)
  57. #define P18 (1 << 18)
  58. #define P19 (1 << 19)
  59. #define P20 (1 << 20)
  60. #define P21 (1 << 21)
  61. #define P22 (1 << 22)
  62. #define P23 (1 << 23)
  63. #define P24 (1 << 24)
  64. #define P25 (1 << 25)
  65. #define P26 (1 << 26)
  66. #define P27 (1 << 27)
  67. #define P28 (1 << 28)
  68. #define P29 (1 << 29)
  69. #define P30 (1 << 30)
  70. #define P31 (1 << 31)
  71. struct device_t {
  72. const char *name;
  73. int TDO_PIO; /* PIO holding TDO */
  74. uint32_t TDO_MASK; /* TDO bitmask */
  75. int TRST_PIO; /* PIO holding TRST */
  76. uint32_t TRST_MASK; /* TRST bitmask */
  77. int TMS_PIO; /* PIO holding TMS */
  78. uint32_t TMS_MASK; /* TMS bitmask */
  79. int TCK_PIO; /* PIO holding TCK */
  80. uint32_t TCK_MASK; /* TCK bitmask */
  81. int TDI_PIO; /* PIO holding TDI */
  82. uint32_t TDI_MASK; /* TDI bitmask */
  83. int SRST_PIO; /* PIO holding SRST */
  84. uint32_t SRST_MASK; /* SRST bitmask */
  85. };
  86. static const struct device_t devices[] = {
  87. { "rea_ecr", PIOD, P27, PIOA, NC, PIOD, P23, PIOD, P24, PIOD, P26, PIOC, P5 },
  88. { .name = NULL },
  89. };
  90. /* configuration */
  91. static char *at91rm9200_device;
  92. /* interface variables
  93. */
  94. static const struct device_t *device;
  95. static int dev_mem_fd;
  96. static void *sys_controller;
  97. static uint32_t *pio_base;
  98. /* low level command set
  99. */
  100. static bb_value_t at91rm9200_read(void);
  101. static int at91rm9200_write(int tck, int tms, int tdi);
  102. static int at91rm9200_init(void);
  103. static int at91rm9200_quit(void);
  104. static struct bitbang_interface at91rm9200_bitbang = {
  105. .read = at91rm9200_read,
  106. .write = at91rm9200_write,
  107. .blink = 0
  108. };
  109. static bb_value_t at91rm9200_read(void)
  110. {
  111. return (pio_base[device->TDO_PIO + PIO_PDSR] & device->TDO_MASK) ? BB_HIGH : BB_LOW;
  112. }
  113. static int at91rm9200_write(int tck, int tms, int tdi)
  114. {
  115. if (tck)
  116. pio_base[device->TCK_PIO + PIO_SODR] = device->TCK_MASK;
  117. else
  118. pio_base[device->TCK_PIO + PIO_CODR] = device->TCK_MASK;
  119. if (tms)
  120. pio_base[device->TMS_PIO + PIO_SODR] = device->TMS_MASK;
  121. else
  122. pio_base[device->TMS_PIO + PIO_CODR] = device->TMS_MASK;
  123. if (tdi)
  124. pio_base[device->TDI_PIO + PIO_SODR] = device->TDI_MASK;
  125. else
  126. pio_base[device->TDI_PIO + PIO_CODR] = device->TDI_MASK;
  127. return ERROR_OK;
  128. }
  129. /* (1) assert or (0) deassert reset lines */
  130. static int at91rm9200_reset(int trst, int srst)
  131. {
  132. if (trst == 0)
  133. pio_base[device->TRST_PIO + PIO_SODR] = device->TRST_MASK;
  134. else if (trst == 1)
  135. pio_base[device->TRST_PIO + PIO_CODR] = device->TRST_MASK;
  136. if (srst == 0)
  137. pio_base[device->SRST_PIO + PIO_SODR] = device->SRST_MASK;
  138. else if (srst == 1)
  139. pio_base[device->SRST_PIO + PIO_CODR] = device->SRST_MASK;
  140. return ERROR_OK;
  141. }
  142. COMMAND_HANDLER(at91rm9200_handle_device_command)
  143. {
  144. if (CMD_ARGC == 0)
  145. return ERROR_COMMAND_SYNTAX_ERROR;
  146. /* only if the device name wasn't overwritten by cmdline */
  147. if (at91rm9200_device == 0) {
  148. at91rm9200_device = malloc(strlen(CMD_ARGV[0]) + sizeof(char));
  149. strcpy(at91rm9200_device, CMD_ARGV[0]);
  150. }
  151. return ERROR_OK;
  152. }
  153. static const struct command_registration at91rm9200_command_handlers[] = {
  154. {
  155. .name = "at91rm9200_device",
  156. .handler = &at91rm9200_handle_device_command,
  157. .mode = COMMAND_CONFIG,
  158. .help = "Set at91rm9200 device [default \"rea_ecr\"]",
  159. .usage = "<device>",
  160. },
  161. COMMAND_REGISTRATION_DONE
  162. };
  163. static struct jtag_interface at91rm9200_interface = {
  164. .execute_queue = bitbang_execute_queue,
  165. };
  166. struct adapter_driver at91rm9200_adapter_driver = {
  167. .name = "at91rm9200",
  168. .transports = jtag_only,
  169. .commands = at91rm9200_command_handlers,
  170. .init = at91rm9200_init,
  171. .quit = at91rm9200_quit,
  172. .reset = at91rm9200_reset,
  173. .jtag_ops = &at91rm9200_interface,
  174. };
  175. static int at91rm9200_init(void)
  176. {
  177. const struct device_t *cur_device;
  178. cur_device = devices;
  179. if (!at91rm9200_device || at91rm9200_device[0] == 0) {
  180. at91rm9200_device = "rea_ecr";
  181. LOG_WARNING("No at91rm9200 device specified, using default 'rea_ecr'");
  182. }
  183. while (cur_device->name) {
  184. if (strcmp(cur_device->name, at91rm9200_device) == 0) {
  185. device = cur_device;
  186. break;
  187. }
  188. cur_device++;
  189. }
  190. if (!device) {
  191. LOG_ERROR("No matching device found for %s", at91rm9200_device);
  192. return ERROR_JTAG_INIT_FAILED;
  193. }
  194. bitbang_interface = &at91rm9200_bitbang;
  195. dev_mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
  196. if (dev_mem_fd < 0) {
  197. LOG_ERROR("open: %s", strerror(errno));
  198. return ERROR_JTAG_INIT_FAILED;
  199. }
  200. sys_controller = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
  201. MAP_SHARED, dev_mem_fd, AT91C_BASE_SYS);
  202. if (sys_controller == MAP_FAILED) {
  203. LOG_ERROR("mmap: %s", strerror(errno));
  204. close(dev_mem_fd);
  205. return ERROR_JTAG_INIT_FAILED;
  206. }
  207. pio_base = (uint32_t *)sys_controller + 0x100;
  208. /*
  209. * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
  210. * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
  211. */
  212. pio_base[device->TDI_PIO + PIO_CODR] = device->TDI_MASK;
  213. pio_base[device->TDI_PIO + PIO_OER] = device->TDI_MASK;
  214. pio_base[device->TDI_PIO + PIO_PER] = device->TDI_MASK;
  215. pio_base[device->TCK_PIO + PIO_CODR] = device->TCK_MASK;
  216. pio_base[device->TCK_PIO + PIO_OER] = device->TCK_MASK;
  217. pio_base[device->TCK_PIO + PIO_PER] = device->TCK_MASK;
  218. pio_base[device->TMS_PIO + PIO_SODR] = device->TMS_MASK;
  219. pio_base[device->TMS_PIO + PIO_OER] = device->TMS_MASK;
  220. pio_base[device->TMS_PIO + PIO_PER] = device->TMS_MASK;
  221. pio_base[device->TRST_PIO + PIO_SODR] = device->TRST_MASK;
  222. pio_base[device->TRST_PIO + PIO_OER] = device->TRST_MASK;
  223. pio_base[device->TRST_PIO + PIO_PER] = device->TRST_MASK;
  224. pio_base[device->SRST_PIO + PIO_SODR] = device->SRST_MASK;
  225. pio_base[device->SRST_PIO + PIO_OER] = device->SRST_MASK;
  226. pio_base[device->SRST_PIO + PIO_PER] = device->SRST_MASK;
  227. pio_base[device->TDO_PIO + PIO_ODR] = device->TDO_MASK;
  228. pio_base[device->TDO_PIO + PIO_PPUER] = device->TDO_MASK;
  229. pio_base[device->TDO_PIO + PIO_PER] = device->TDO_MASK;
  230. return ERROR_OK;
  231. }
  232. static int at91rm9200_quit(void)
  233. {
  234. return ERROR_OK;
  235. }