fpga.c 8.3 KB

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