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.
 
 
 
 
 
 

287 lines
8.0 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, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #define INCLUDE_JTAG_INTERFACE_H
  24. #include "interface.h"
  25. #include "bitbang.h"
  26. #include <sys/mman.h>
  27. /* AT91RM9200 */
  28. #define AT91C_BASE_SYS (0xfffff000)
  29. /* GPIO assignment */
  30. #define PIOA (0 << 7)
  31. #define PIOB (1 << 7)
  32. #define PIOC (2 << 7)
  33. #define PIOD (3 << 7)
  34. #define PIO_PER (0) /* PIO enable */
  35. #define PIO_OER (4) /* output enable */
  36. #define PIO_ODR (5) /* output disable */
  37. #define PIO_SODR (12) /* set output data */
  38. #define PIO_CODR (13) /* clear output data */
  39. #define PIO_PDSR (15) /* pin data status */
  40. #define PIO_PPUER (25) /* pull-up enable */
  41. #define NC (0) /* not connected */
  42. #define P0 (1 << 0)
  43. #define P1 (1 << 1)
  44. #define P2 (1 << 2)
  45. #define P3 (1 << 3)
  46. #define P4 (1 << 4)
  47. #define P5 (1 << 5)
  48. #define P6 (1 << 6)
  49. #define P7 (1 << 7)
  50. #define P8 (1 << 8)
  51. #define P9 (1 << 9)
  52. #define P10 (1 << 10)
  53. #define P11 (1 << 11)
  54. #define P12 (1 << 12)
  55. #define P13 (1 << 13)
  56. #define P14 (1 << 14)
  57. #define P15 (1 << 15)
  58. #define P16 (1 << 16)
  59. #define P17 (1 << 17)
  60. #define P18 (1 << 18)
  61. #define P19 (1 << 19)
  62. #define P20 (1 << 20)
  63. #define P21 (1 << 21)
  64. #define P22 (1 << 22)
  65. #define P23 (1 << 23)
  66. #define P24 (1 << 24)
  67. #define P25 (1 << 25)
  68. #define P26 (1 << 26)
  69. #define P27 (1 << 27)
  70. #define P28 (1 << 28)
  71. #define P29 (1 << 29)
  72. #define P30 (1 << 30)
  73. #define P31 (1 << 31)
  74. struct device_t
  75. {
  76. char* name;
  77. int TDO_PIO; /* PIO holding TDO */
  78. u32 TDO_MASK; /* TDO bitmask */
  79. int TRST_PIO; /* PIO holding TRST */
  80. u32 TRST_MASK; /* TRST bitmask */
  81. int TMS_PIO; /* PIO holding TMS */
  82. u32 TMS_MASK; /* TMS bitmask */
  83. int TCK_PIO; /* PIO holding TCK */
  84. u32 TCK_MASK; /* TCK bitmask */
  85. int TDI_PIO; /* PIO holding TDI */
  86. u32 TDI_MASK; /* TDI bitmask */
  87. int SRST_PIO; /* PIO holding SRST */
  88. u32 SRST_MASK; /* SRST bitmask */
  89. };
  90. static struct device_t devices[] =
  91. {
  92. { "rea_ecr", PIOD, P27, PIOA, NC, PIOD, P23, PIOD, P24, PIOD, P26, PIOC, P5 },
  93. { .name = NULL },
  94. };
  95. /* configuration */
  96. static char* at91rm9200_device;
  97. /* interface variables
  98. */
  99. static struct device_t* device;
  100. static int dev_mem_fd;
  101. static void *sys_controller;
  102. static u32* pio_base;
  103. /* low level command set
  104. */
  105. static int at91rm9200_read(void);
  106. static void at91rm9200_write(int tck, int tms, int tdi);
  107. static void at91rm9200_reset(int trst, int srst);
  108. static int at91rm9200_speed(int speed);
  109. static int at91rm9200_register_commands(struct command_context_s *cmd_ctx);
  110. static int at91rm9200_init(void);
  111. static int at91rm9200_quit(void);
  112. jtag_interface_t at91rm9200_interface =
  113. {
  114. .name = "at91rm9200",
  115. .execute_queue = bitbang_execute_queue,
  116. .speed = at91rm9200_speed,
  117. .register_commands = at91rm9200_register_commands,
  118. .init = at91rm9200_init,
  119. .quit = at91rm9200_quit,
  120. };
  121. static bitbang_interface_t at91rm9200_bitbang =
  122. {
  123. .read = at91rm9200_read,
  124. .write = at91rm9200_write,
  125. .reset = at91rm9200_reset,
  126. .blink = 0
  127. };
  128. static int at91rm9200_read(void)
  129. {
  130. return (pio_base[device->TDO_PIO + PIO_PDSR] & device->TDO_MASK) != 0;
  131. }
  132. static void at91rm9200_write(int tck, int tms, int tdi)
  133. {
  134. if (tck)
  135. pio_base[device->TCK_PIO + PIO_SODR] = device->TCK_MASK;
  136. else
  137. pio_base[device->TCK_PIO + PIO_CODR] = device->TCK_MASK;
  138. if (tms)
  139. pio_base[device->TMS_PIO + PIO_SODR] = device->TMS_MASK;
  140. else
  141. pio_base[device->TMS_PIO + PIO_CODR] = device->TMS_MASK;
  142. if (tdi)
  143. pio_base[device->TDI_PIO + PIO_SODR] = device->TDI_MASK;
  144. else
  145. pio_base[device->TDI_PIO + PIO_CODR] = device->TDI_MASK;
  146. }
  147. /* (1) assert or (0) deassert reset lines */
  148. static void at91rm9200_reset(int trst, int srst)
  149. {
  150. if (trst == 0)
  151. pio_base[device->TRST_PIO + PIO_SODR] = device->TRST_MASK;
  152. else if (trst == 1)
  153. pio_base[device->TRST_PIO + PIO_CODR] = device->TRST_MASK;
  154. if (srst == 0)
  155. pio_base[device->SRST_PIO + PIO_SODR] = device->SRST_MASK;
  156. else if (srst == 1)
  157. pio_base[device->SRST_PIO + PIO_CODR] = device->SRST_MASK;
  158. }
  159. static int at91rm9200_speed(int speed)
  160. {
  161. return ERROR_OK;
  162. }
  163. static int at91rm9200_handle_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  164. {
  165. if (argc == 0)
  166. return ERROR_OK;
  167. /* only if the device name wasn't overwritten by cmdline */
  168. if (at91rm9200_device == 0)
  169. {
  170. at91rm9200_device = malloc(strlen(args[0]) + sizeof(char));
  171. strcpy(at91rm9200_device, args[0]);
  172. }
  173. return ERROR_OK;
  174. }
  175. static int at91rm9200_register_commands(struct command_context_s *cmd_ctx)
  176. {
  177. register_command(cmd_ctx, NULL, "at91rm9200_device", at91rm9200_handle_device_command,
  178. COMMAND_CONFIG, NULL);
  179. return ERROR_OK;
  180. }
  181. static int at91rm9200_init(void)
  182. {
  183. struct device_t *cur_device;
  184. cur_device = devices;
  185. if (at91rm9200_device == NULL || at91rm9200_device[0] == 0)
  186. {
  187. at91rm9200_device = "rea_ecr";
  188. LOG_WARNING("No at91rm9200 device specified, using default 'rea_ecr'");
  189. }
  190. while (cur_device->name)
  191. {
  192. if (strcmp(cur_device->name, at91rm9200_device) == 0)
  193. {
  194. device = cur_device;
  195. break;
  196. }
  197. cur_device++;
  198. }
  199. if (!device)
  200. {
  201. LOG_ERROR("No matching device found for %s", at91rm9200_device);
  202. return ERROR_JTAG_INIT_FAILED;
  203. }
  204. bitbang_interface = &at91rm9200_bitbang;
  205. dev_mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
  206. if (dev_mem_fd < 0) {
  207. perror("open");
  208. return ERROR_JTAG_INIT_FAILED;
  209. }
  210. sys_controller = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
  211. MAP_SHARED, dev_mem_fd, AT91C_BASE_SYS);
  212. if (sys_controller == MAP_FAILED) {
  213. perror("mmap");
  214. close(dev_mem_fd);
  215. return ERROR_JTAG_INIT_FAILED;
  216. }
  217. pio_base = (u32*)sys_controller + 0x100;
  218. /*
  219. * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
  220. * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
  221. */
  222. pio_base[device->TDI_PIO + PIO_CODR] = device->TDI_MASK;
  223. pio_base[device->TDI_PIO + PIO_OER] = device->TDI_MASK;
  224. pio_base[device->TDI_PIO + PIO_PER] = device->TDI_MASK;
  225. pio_base[device->TCK_PIO + PIO_CODR] = device->TCK_MASK;
  226. pio_base[device->TCK_PIO + PIO_OER] = device->TCK_MASK;
  227. pio_base[device->TCK_PIO + PIO_PER] = device->TCK_MASK;
  228. pio_base[device->TMS_PIO + PIO_SODR] = device->TMS_MASK;
  229. pio_base[device->TMS_PIO + PIO_OER] = device->TMS_MASK;
  230. pio_base[device->TMS_PIO + PIO_PER] = device->TMS_MASK;
  231. pio_base[device->TRST_PIO + PIO_SODR] = device->TRST_MASK;
  232. pio_base[device->TRST_PIO + PIO_OER] = device->TRST_MASK;
  233. pio_base[device->TRST_PIO + PIO_PER] = device->TRST_MASK;
  234. pio_base[device->SRST_PIO + PIO_SODR] = device->SRST_MASK;
  235. pio_base[device->SRST_PIO + PIO_OER] = device->SRST_MASK;
  236. pio_base[device->SRST_PIO + PIO_PER] = device->SRST_MASK;
  237. pio_base[device->TDO_PIO + PIO_ODR] = device->TDO_MASK;
  238. pio_base[device->TDO_PIO + PIO_PPUER] = device->TDO_MASK;
  239. pio_base[device->TDO_PIO + PIO_PER] = device->TDO_MASK;
  240. return ERROR_OK;
  241. }
  242. static int at91rm9200_quit(void)
  243. {
  244. return ERROR_OK;
  245. }