fpga.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Grandegger, DENX Software Engineering, wg@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (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, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <linux/ctype.h>
  26. #include <common.h>
  27. #include "fpga.h"
  28. int power_on_reset(void);
  29. /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  30. static int fpga_get_version(fpga_t* fpga, char* name)
  31. {
  32. char vname[12];
  33. /*
  34. * Net-list string format:
  35. * "vvvvvvvvddddddddn...".
  36. * Version Date Name
  37. * "0000000322042002PUMA" = PUMA version 3 from 22.04.2002.
  38. */
  39. if (strlen(name) < (16 + strlen(fpga->name)))
  40. goto failure;
  41. /* Check FPGA name */
  42. if (strcmp(&name[16], fpga->name) != 0)
  43. goto failure;
  44. /* Get version number */
  45. memcpy(vname, name, 8);
  46. vname[8] = '\0';
  47. return simple_strtoul(vname, NULL, 16);
  48. failure:
  49. printf("Image name %s is invalid\n", name);
  50. return -1;
  51. }
  52. /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  53. static fpga_t* fpga_get(char* fpga_name)
  54. {
  55. char name[FPGA_NAME_LEN];
  56. int i;
  57. if (strlen(fpga_name) >= FPGA_NAME_LEN)
  58. goto failure;
  59. for (i = 0; i < strlen(fpga_name); i++)
  60. name[i] = toupper(fpga_name[i]);
  61. name[i] = '\0';
  62. for (i = 0; i < fpga_count; i++) {
  63. if (strcmp(name, fpga_list[i].name) == 0)
  64. return &fpga_list[i];
  65. }
  66. failure:
  67. printf("FPGA: name %s is invalid\n", fpga_name);
  68. return NULL;
  69. }
  70. /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  71. static void fpga_status (fpga_t* fpga)
  72. {
  73. /* Check state */
  74. if (fpga_control(fpga, FPGA_DONE_IS_HIGH))
  75. printf ("%s is loaded (%08lx)\n",
  76. fpga->name, fpga_control(fpga, FPGA_GET_ID));
  77. else
  78. printf ("%s is NOT loaded\n", fpga->name);
  79. }
  80. /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  81. #define FPGA_RESET_TIMEOUT 100 /* = 10 ms */
  82. static int fpga_reset (fpga_t* fpga)
  83. {
  84. int i;
  85. /* Set PROG to low and wait til INIT goes low */
  86. fpga_control(fpga, FPGA_PROG_SET_LOW);
  87. for (i = 0; i < FPGA_RESET_TIMEOUT; i++) {
  88. udelay (100);
  89. if (!fpga_control(fpga, FPGA_INIT_IS_HIGH))
  90. break;
  91. }
  92. if (i == FPGA_RESET_TIMEOUT)
  93. goto failure;
  94. /* Set PROG to high and wait til INIT goes high */
  95. fpga_control(fpga, FPGA_PROG_SET_HIGH);
  96. for (i = 0; i < FPGA_RESET_TIMEOUT; i++) {
  97. udelay (100);
  98. if (fpga_control(fpga, FPGA_INIT_IS_HIGH))
  99. break;
  100. }
  101. if (i == FPGA_RESET_TIMEOUT)
  102. goto failure;
  103. return 0;
  104. failure:
  105. return 1;
  106. }
  107. /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  108. #define FPGA_LOAD_TIMEOUT 100 /* = 10 ms */
  109. static int fpga_load (fpga_t* fpga, ulong addr, int checkall)
  110. {
  111. volatile uchar *fpga_addr = (volatile uchar *)fpga->conf_base;
  112. image_header_t hdr;
  113. ulong len, checksum;
  114. uchar *data = (uchar *)&hdr;
  115. char *s, msg[32];
  116. int verify, i;
  117. /*
  118. * Check the image header and data of the net-list
  119. */
  120. memcpy (&hdr, (char *)addr, sizeof(image_header_t));
  121. if (hdr.ih_magic != IH_MAGIC) {
  122. strcpy (msg, "Bad Image Magic Number");
  123. goto failure;
  124. }
  125. len = sizeof(image_header_t);
  126. checksum = hdr.ih_hcrc;
  127. hdr.ih_hcrc = 0;
  128. if (crc32 (0, data, len) != checksum) {
  129. strcpy (msg, "Bad Image Header CRC");
  130. goto failure;
  131. }
  132. data = (uchar*)(addr + sizeof(image_header_t));
  133. len = hdr.ih_size;
  134. s = getenv ("verify");
  135. verify = (s && (*s == 'n')) ? 0 : 1;
  136. if (verify) {
  137. if (crc32 (0, data, len) != hdr.ih_dcrc) {
  138. strcpy (msg, "Bad Image Data CRC");
  139. goto failure;
  140. }
  141. }
  142. if (checkall && fpga_get_version(fpga, hdr.ih_name) < 0)
  143. return 1;
  144. /* align length */
  145. if (len & 1)
  146. ++len;
  147. /*
  148. * Reset FPGA and wait for completion
  149. */
  150. if (fpga_reset(fpga)) {
  151. strcpy (msg, "Reset Timeout");
  152. goto failure;
  153. }
  154. printf ("(%s)... ", hdr.ih_name);
  155. /*
  156. * Copy data to FPGA
  157. */
  158. fpga_control (fpga, FPGA_LOAD_MODE);
  159. while (len--) {
  160. *fpga_addr = *data++;
  161. }
  162. fpga_control (fpga, FPGA_READ_MODE);
  163. /*
  164. * Wait for completion and check error status if timeout
  165. */
  166. for (i = 0; i < FPGA_LOAD_TIMEOUT; i++) {
  167. udelay (100);
  168. if (fpga_control (fpga, FPGA_DONE_IS_HIGH))
  169. break;
  170. }
  171. if (i == FPGA_LOAD_TIMEOUT) {
  172. if (fpga_control(fpga, FPGA_INIT_IS_HIGH))
  173. strcpy(msg, "Invalid Size");
  174. else
  175. strcpy(msg, "CRC Error");
  176. goto failure;
  177. }
  178. printf("done\n");
  179. return 0;
  180. failure:
  181. printf("ERROR: %s\n", msg);
  182. return 1;
  183. }
  184. #if (CONFIG_COMMANDS & CFG_CMD_BSP)
  185. /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  186. int do_fpga (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  187. {
  188. ulong addr = 0;
  189. int i;
  190. fpga_t* fpga;
  191. if (argc < 2)
  192. goto failure;
  193. if (strncmp(argv[1], "stat", 4) == 0) { /* status */
  194. if (argc == 2) {
  195. for (i = 0; i < fpga_count; i++) {
  196. fpga_status (&fpga_list[i]);
  197. }
  198. }
  199. else if (argc == 3) {
  200. if ((fpga = fpga_get(argv[2])) == 0)
  201. goto failure;
  202. fpga_status (fpga);
  203. }
  204. else
  205. goto failure;
  206. }
  207. else if (strcmp(argv[1],"load") == 0) { /* load */
  208. if (argc == 3 && fpga_count == 1) {
  209. fpga = &fpga_list[0];
  210. }
  211. else if (argc == 4) {
  212. if ((fpga = fpga_get(argv[2])) == 0)
  213. goto failure;
  214. }
  215. else
  216. goto failure;
  217. addr = simple_strtoul(argv[argc-1], NULL, 16);
  218. printf ("FPGA load %s: addr %08lx: ",
  219. fpga->name, addr);
  220. fpga_load (fpga, addr, 1);
  221. }
  222. else if (strncmp(argv[1], "rese", 4) == 0) { /* reset */
  223. if (argc == 2 && fpga_count == 1) {
  224. fpga = &fpga_list[0];
  225. }
  226. else if (argc == 3) {
  227. if ((fpga = fpga_get(argv[2])) == 0)
  228. goto failure;
  229. }
  230. else
  231. goto failure;
  232. printf ("FPGA reset %s: ", fpga->name);
  233. if (fpga_reset(fpga))
  234. printf ("ERROR: Timeout\n");
  235. else
  236. printf ("done\n");
  237. }
  238. else
  239. goto failure;
  240. return 0;
  241. failure:
  242. printf ("Usage:\n%s\n", cmdtp->usage);
  243. return 1;
  244. }
  245. U_BOOT_CMD(
  246. fpga, 4, 1, do_fpga,
  247. "fpga - access FPGA(s)\n",
  248. "fpga status [name] - print FPGA status\n"
  249. "fpga reset [name] - reset FPGA\n"
  250. "fpga load [name] addr - load FPGA configuration data\n"
  251. );
  252. #endif /* CONFIG_COMMANDS & CFG_CMD_BSP */
  253. /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  254. int fpga_init (void)
  255. {
  256. ulong addr;
  257. ulong new_id, old_id = 0;
  258. image_header_t *hdr;
  259. fpga_t* fpga;
  260. int do_load, i, j;
  261. char name[16], *s;
  262. /*
  263. * Port setup for FPGA control
  264. */
  265. for (i = 0; i < fpga_count; i++) {
  266. fpga_control(&fpga_list[i], FPGA_INIT_PORTS);
  267. }
  268. /*
  269. * Load FPGA(s): a new net-list is loaded if the FPGA is
  270. * empty, Power-on-Reset or the old one is not up-to-date
  271. */
  272. for (i = 0; i < fpga_count; i++) {
  273. fpga = &fpga_list[i];
  274. printf ("%s: ", fpga->name);
  275. for (j = 0; j < strlen(fpga->name); j++)
  276. name[j] = tolower(fpga->name[j]);
  277. name[j] = '\0';
  278. sprintf(name, "%s_addr", name);
  279. addr = 0;
  280. if ((s = getenv(name)) != NULL)
  281. addr = simple_strtoul(s, NULL, 16);
  282. if (!addr) {
  283. printf ("env. variable %s undefined\n", name);
  284. return 1;
  285. }
  286. hdr = (image_header_t *)addr;
  287. if ((new_id = fpga_get_version(fpga, hdr->ih_name)) == -1)
  288. return 1;
  289. do_load = 1;
  290. if (!power_on_reset() && fpga_control(fpga, FPGA_DONE_IS_HIGH)) {
  291. old_id = fpga_control(fpga, FPGA_GET_ID);
  292. if (new_id == old_id)
  293. do_load = 0;
  294. }
  295. if (do_load) {
  296. printf ("loading ");
  297. fpga_load (fpga, addr, 0);
  298. } else {
  299. printf ("loaded (%08lx)\n", old_id);
  300. }
  301. }
  302. return 0;
  303. }