fpga.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 = (image_header_t *)addr;
  113. ulong len;
  114. uchar *data;
  115. char msg[32];
  116. int verify, i;
  117. /*
  118. * Check the image header and data of the net-list
  119. */
  120. if (!image_check_magic (hdr)) {
  121. strcpy (msg, "Bad Image Magic Number");
  122. goto failure;
  123. }
  124. if (!image_check_hcrc (hdr)) {
  125. strcpy (msg, "Bad Image Header CRC");
  126. goto failure;
  127. }
  128. data = (uchar*)image_get_data (hdr);
  129. len = image_get_data_size (hdr);
  130. verify = getenv_verify ();
  131. if (verify) {
  132. if (!image_check_dcrc (hdr)) {
  133. strcpy (msg, "Bad Image Data CRC");
  134. goto failure;
  135. }
  136. }
  137. if (checkall && fpga_get_version(fpga, image_get_name (hdr)) < 0)
  138. return 1;
  139. /* align length */
  140. if (len & 1)
  141. ++len;
  142. /*
  143. * Reset FPGA and wait for completion
  144. */
  145. if (fpga_reset(fpga)) {
  146. strcpy (msg, "Reset Timeout");
  147. goto failure;
  148. }
  149. printf ("(%s)... ", image_get_name (hdr));
  150. /*
  151. * Copy data to FPGA
  152. */
  153. fpga_control (fpga, FPGA_LOAD_MODE);
  154. while (len--) {
  155. *fpga_addr = *data++;
  156. }
  157. fpga_control (fpga, FPGA_READ_MODE);
  158. /*
  159. * Wait for completion and check error status if timeout
  160. */
  161. for (i = 0; i < FPGA_LOAD_TIMEOUT; i++) {
  162. udelay (100);
  163. if (fpga_control (fpga, FPGA_DONE_IS_HIGH))
  164. break;
  165. }
  166. if (i == FPGA_LOAD_TIMEOUT) {
  167. if (fpga_control(fpga, FPGA_INIT_IS_HIGH))
  168. strcpy(msg, "Invalid Size");
  169. else
  170. strcpy(msg, "CRC Error");
  171. goto failure;
  172. }
  173. printf("done\n");
  174. return 0;
  175. failure:
  176. printf("ERROR: %s\n", msg);
  177. return 1;
  178. }
  179. #if defined(CONFIG_CMD_BSP)
  180. /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  181. int do_fpga (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  182. {
  183. ulong addr = 0;
  184. int i;
  185. fpga_t* fpga;
  186. if (argc < 2)
  187. goto failure;
  188. if (strncmp(argv[1], "stat", 4) == 0) { /* status */
  189. if (argc == 2) {
  190. for (i = 0; i < fpga_count; i++) {
  191. fpga_status (&fpga_list[i]);
  192. }
  193. }
  194. else if (argc == 3) {
  195. if ((fpga = fpga_get(argv[2])) == 0)
  196. goto failure;
  197. fpga_status (fpga);
  198. }
  199. else
  200. goto failure;
  201. }
  202. else if (strcmp(argv[1],"load") == 0) { /* load */
  203. if (argc == 3 && fpga_count == 1) {
  204. fpga = &fpga_list[0];
  205. }
  206. else if (argc == 4) {
  207. if ((fpga = fpga_get(argv[2])) == 0)
  208. goto failure;
  209. }
  210. else
  211. goto failure;
  212. addr = simple_strtoul(argv[argc-1], NULL, 16);
  213. printf ("FPGA load %s: addr %08lx: ",
  214. fpga->name, addr);
  215. fpga_load (fpga, addr, 1);
  216. }
  217. else if (strncmp(argv[1], "rese", 4) == 0) { /* reset */
  218. if (argc == 2 && fpga_count == 1) {
  219. fpga = &fpga_list[0];
  220. }
  221. else if (argc == 3) {
  222. if ((fpga = fpga_get(argv[2])) == 0)
  223. goto failure;
  224. }
  225. else
  226. goto failure;
  227. printf ("FPGA reset %s: ", fpga->name);
  228. if (fpga_reset(fpga))
  229. printf ("ERROR: Timeout\n");
  230. else
  231. printf ("done\n");
  232. }
  233. else
  234. goto failure;
  235. return 0;
  236. failure:
  237. printf ("Usage:\n%s\n", cmdtp->usage);
  238. return 1;
  239. }
  240. U_BOOT_CMD(
  241. fpga, 4, 1, do_fpga,
  242. "fpga - access FPGA(s)\n",
  243. "fpga status [name] - print FPGA status\n"
  244. "fpga reset [name] - reset FPGA\n"
  245. "fpga load [name] addr - load FPGA configuration data\n"
  246. );
  247. #endif
  248. /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  249. int fpga_init (void)
  250. {
  251. ulong addr;
  252. ulong new_id, old_id = 0;
  253. image_header_t *hdr;
  254. fpga_t* fpga;
  255. int do_load, i, j;
  256. char name[16], *s;
  257. /*
  258. * Port setup for FPGA control
  259. */
  260. for (i = 0; i < fpga_count; i++) {
  261. fpga_control(&fpga_list[i], FPGA_INIT_PORTS);
  262. }
  263. /*
  264. * Load FPGA(s): a new net-list is loaded if the FPGA is
  265. * empty, Power-on-Reset or the old one is not up-to-date
  266. */
  267. for (i = 0; i < fpga_count; i++) {
  268. fpga = &fpga_list[i];
  269. printf ("%s: ", fpga->name);
  270. for (j = 0; j < strlen(fpga->name); j++)
  271. name[j] = tolower(fpga->name[j]);
  272. name[j] = '\0';
  273. sprintf(name, "%s_addr", name);
  274. addr = 0;
  275. if ((s = getenv(name)) != NULL)
  276. addr = simple_strtoul(s, NULL, 16);
  277. if (!addr) {
  278. printf ("env. variable %s undefined\n", name);
  279. return 1;
  280. }
  281. hdr = (image_header_t *)addr;
  282. if ((new_id = fpga_get_version(fpga, image_get_name (hdr))) == -1)
  283. return 1;
  284. do_load = 1;
  285. if (!power_on_reset() && fpga_control(fpga, FPGA_DONE_IS_HIGH)) {
  286. old_id = fpga_control(fpga, FPGA_GET_ID);
  287. if (new_id == old_id)
  288. do_load = 0;
  289. }
  290. if (do_load) {
  291. printf ("loading ");
  292. fpga_load (fpga, addr, 0);
  293. } else {
  294. printf ("loaded (%08lx)\n", old_id);
  295. }
  296. }
  297. return 0;
  298. }