xilinx.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * (C) Copyright 2012-2013, Xilinx, Michal Simek
  3. *
  4. * (C) Copyright 2002
  5. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  6. * Keith Outwater, keith_outwater@mvis.com
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. /*
  11. * Xilinx FPGA support
  12. */
  13. #include <common.h>
  14. #include <fpga.h>
  15. #include <virtex2.h>
  16. #include <spartan2.h>
  17. #include <spartan3.h>
  18. #include <zynqpl.h>
  19. #if 0
  20. #define FPGA_DEBUG
  21. #endif
  22. /* Define FPGA_DEBUG to get debug printf's */
  23. #ifdef FPGA_DEBUG
  24. #define PRINTF(fmt,args...) printf (fmt ,##args)
  25. #else
  26. #define PRINTF(fmt,args...)
  27. #endif
  28. /* Local Static Functions */
  29. static int xilinx_validate (Xilinx_desc * desc, char *fn);
  30. /* ------------------------------------------------------------------------- */
  31. int fpga_loadbitstream(int devnum, char *fpgadata, size_t size)
  32. {
  33. unsigned int length;
  34. unsigned int swapsize;
  35. char buffer[80];
  36. unsigned char *dataptr;
  37. unsigned int i;
  38. const fpga_desc *desc;
  39. Xilinx_desc *xdesc;
  40. dataptr = (unsigned char *)fpgadata;
  41. /* Find out fpga_description */
  42. desc = fpga_validate(devnum, dataptr, 0, (char *)__func__);
  43. /* Assign xilinx device description */
  44. xdesc = desc->devdesc;
  45. /* skip the first bytes of the bitsteam, their meaning is unknown */
  46. length = (*dataptr << 8) + *(dataptr + 1);
  47. dataptr += 2;
  48. dataptr += length;
  49. /* get design name (identifier, length, string) */
  50. length = (*dataptr << 8) + *(dataptr + 1);
  51. dataptr += 2;
  52. if (*dataptr++ != 0x61) {
  53. debug("%s: Design name id not recognized in bitstream\n",
  54. __func__);
  55. return FPGA_FAIL;
  56. }
  57. length = (*dataptr << 8) + *(dataptr + 1);
  58. dataptr += 2;
  59. for (i = 0; i < length; i++)
  60. buffer[i] = *dataptr++;
  61. printf(" design filename = \"%s\"\n", buffer);
  62. /* get part number (identifier, length, string) */
  63. if (*dataptr++ != 0x62) {
  64. printf("%s: Part number id not recognized in bitstream\n",
  65. __func__);
  66. return FPGA_FAIL;
  67. }
  68. length = (*dataptr << 8) + *(dataptr + 1);
  69. dataptr += 2;
  70. for (i = 0; i < length; i++)
  71. buffer[i] = *dataptr++;
  72. if (xdesc->name) {
  73. i = strncmp(buffer, xdesc->name, strlen(xdesc->name));
  74. if (i) {
  75. printf("%s: Wrong bitstream ID for this device\n",
  76. __func__);
  77. printf("%s: Bitstream ID %s, current device ID %d/%s\n",
  78. __func__, buffer, devnum, xdesc->name);
  79. return FPGA_FAIL;
  80. }
  81. } else {
  82. printf("%s: Please fill correct device ID to Xilinx_desc\n",
  83. __func__);
  84. }
  85. printf(" part number = \"%s\"\n", buffer);
  86. /* get date (identifier, length, string) */
  87. if (*dataptr++ != 0x63) {
  88. printf("%s: Date identifier not recognized in bitstream\n",
  89. __func__);
  90. return FPGA_FAIL;
  91. }
  92. length = (*dataptr << 8) + *(dataptr+1);
  93. dataptr += 2;
  94. for (i = 0; i < length; i++)
  95. buffer[i] = *dataptr++;
  96. printf(" date = \"%s\"\n", buffer);
  97. /* get time (identifier, length, string) */
  98. if (*dataptr++ != 0x64) {
  99. printf("%s: Time identifier not recognized in bitstream\n",
  100. __func__);
  101. return FPGA_FAIL;
  102. }
  103. length = (*dataptr << 8) + *(dataptr+1);
  104. dataptr += 2;
  105. for (i = 0; i < length; i++)
  106. buffer[i] = *dataptr++;
  107. printf(" time = \"%s\"\n", buffer);
  108. /* get fpga data length (identifier, length) */
  109. if (*dataptr++ != 0x65) {
  110. printf("%s: Data length id not recognized in bitstream\n",
  111. __func__);
  112. return FPGA_FAIL;
  113. }
  114. swapsize = ((unsigned int) *dataptr << 24) +
  115. ((unsigned int) *(dataptr + 1) << 16) +
  116. ((unsigned int) *(dataptr + 2) << 8) +
  117. ((unsigned int) *(dataptr + 3));
  118. dataptr += 4;
  119. printf(" bytes in bitstream = %d\n", swapsize);
  120. return fpga_load(devnum, dataptr, swapsize);
  121. }
  122. int xilinx_load(Xilinx_desc *desc, const void *buf, size_t bsize)
  123. {
  124. int ret_val = FPGA_FAIL; /* assume a failure */
  125. if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
  126. printf ("%s: Invalid device descriptor\n", __FUNCTION__);
  127. } else
  128. switch (desc->family) {
  129. case Xilinx_Spartan2:
  130. #if defined(CONFIG_FPGA_SPARTAN2)
  131. PRINTF ("%s: Launching the Spartan-II Loader...\n",
  132. __FUNCTION__);
  133. ret_val = Spartan2_load (desc, buf, bsize);
  134. #else
  135. printf ("%s: No support for Spartan-II devices.\n",
  136. __FUNCTION__);
  137. #endif
  138. break;
  139. case Xilinx_Spartan3:
  140. #if defined(CONFIG_FPGA_SPARTAN3)
  141. PRINTF ("%s: Launching the Spartan-III Loader...\n",
  142. __FUNCTION__);
  143. ret_val = Spartan3_load (desc, buf, bsize);
  144. #else
  145. printf ("%s: No support for Spartan-III devices.\n",
  146. __FUNCTION__);
  147. #endif
  148. break;
  149. case Xilinx_Virtex2:
  150. #if defined(CONFIG_FPGA_VIRTEX2)
  151. PRINTF ("%s: Launching the Virtex-II Loader...\n",
  152. __FUNCTION__);
  153. ret_val = Virtex2_load (desc, buf, bsize);
  154. #else
  155. printf ("%s: No support for Virtex-II devices.\n",
  156. __FUNCTION__);
  157. #endif
  158. break;
  159. case xilinx_zynq:
  160. #if defined(CONFIG_FPGA_ZYNQPL)
  161. PRINTF("%s: Launching the Zynq PL Loader...\n",
  162. __func__);
  163. ret_val = zynq_load(desc, buf, bsize);
  164. #else
  165. printf("%s: No support for Zynq devices.\n",
  166. __func__);
  167. #endif
  168. break;
  169. default:
  170. printf ("%s: Unsupported family type, %d\n",
  171. __FUNCTION__, desc->family);
  172. }
  173. return ret_val;
  174. }
  175. int xilinx_dump(Xilinx_desc *desc, const void *buf, size_t bsize)
  176. {
  177. int ret_val = FPGA_FAIL; /* assume a failure */
  178. if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
  179. printf ("%s: Invalid device descriptor\n", __FUNCTION__);
  180. } else
  181. switch (desc->family) {
  182. case Xilinx_Spartan2:
  183. #if defined(CONFIG_FPGA_SPARTAN2)
  184. PRINTF ("%s: Launching the Spartan-II Reader...\n",
  185. __FUNCTION__);
  186. ret_val = Spartan2_dump (desc, buf, bsize);
  187. #else
  188. printf ("%s: No support for Spartan-II devices.\n",
  189. __FUNCTION__);
  190. #endif
  191. break;
  192. case Xilinx_Spartan3:
  193. #if defined(CONFIG_FPGA_SPARTAN3)
  194. PRINTF ("%s: Launching the Spartan-III Reader...\n",
  195. __FUNCTION__);
  196. ret_val = Spartan3_dump (desc, buf, bsize);
  197. #else
  198. printf ("%s: No support for Spartan-III devices.\n",
  199. __FUNCTION__);
  200. #endif
  201. break;
  202. case Xilinx_Virtex2:
  203. #if defined( CONFIG_FPGA_VIRTEX2)
  204. PRINTF ("%s: Launching the Virtex-II Reader...\n",
  205. __FUNCTION__);
  206. ret_val = Virtex2_dump (desc, buf, bsize);
  207. #else
  208. printf ("%s: No support for Virtex-II devices.\n",
  209. __FUNCTION__);
  210. #endif
  211. break;
  212. case xilinx_zynq:
  213. #if defined(CONFIG_FPGA_ZYNQPL)
  214. PRINTF("%s: Launching the Zynq PL Reader...\n",
  215. __func__);
  216. ret_val = zynq_dump(desc, buf, bsize);
  217. #else
  218. printf("%s: No support for Zynq devices.\n",
  219. __func__);
  220. #endif
  221. break;
  222. default:
  223. printf ("%s: Unsupported family type, %d\n",
  224. __FUNCTION__, desc->family);
  225. }
  226. return ret_val;
  227. }
  228. int xilinx_info (Xilinx_desc * desc)
  229. {
  230. int ret_val = FPGA_FAIL;
  231. if (xilinx_validate (desc, (char *)__FUNCTION__)) {
  232. printf ("Family: \t");
  233. switch (desc->family) {
  234. case Xilinx_Spartan2:
  235. printf ("Spartan-II\n");
  236. break;
  237. case Xilinx_Spartan3:
  238. printf ("Spartan-III\n");
  239. break;
  240. case Xilinx_Virtex2:
  241. printf ("Virtex-II\n");
  242. break;
  243. case xilinx_zynq:
  244. printf("Zynq PL\n");
  245. break;
  246. /* Add new family types here */
  247. default:
  248. printf ("Unknown family type, %d\n", desc->family);
  249. }
  250. printf ("Interface type:\t");
  251. switch (desc->iface) {
  252. case slave_serial:
  253. printf ("Slave Serial\n");
  254. break;
  255. case master_serial: /* Not used */
  256. printf ("Master Serial\n");
  257. break;
  258. case slave_parallel:
  259. printf ("Slave Parallel\n");
  260. break;
  261. case jtag_mode: /* Not used */
  262. printf ("JTAG Mode\n");
  263. break;
  264. case slave_selectmap:
  265. printf ("Slave SelectMap Mode\n");
  266. break;
  267. case master_selectmap:
  268. printf ("Master SelectMap Mode\n");
  269. break;
  270. case devcfg:
  271. printf("Device configuration interface (Zynq)\n");
  272. break;
  273. /* Add new interface types here */
  274. default:
  275. printf ("Unsupported interface type, %d\n", desc->iface);
  276. }
  277. printf ("Device Size: \t%d bytes\n"
  278. "Cookie: \t0x%x (%d)\n",
  279. desc->size, desc->cookie, desc->cookie);
  280. if (desc->name)
  281. printf("Device name: \t%s\n", desc->name);
  282. if (desc->iface_fns) {
  283. printf ("Device Function Table @ 0x%p\n", desc->iface_fns);
  284. switch (desc->family) {
  285. case Xilinx_Spartan2:
  286. #if defined(CONFIG_FPGA_SPARTAN2)
  287. Spartan2_info (desc);
  288. #else
  289. /* just in case */
  290. printf ("%s: No support for Spartan-II devices.\n",
  291. __FUNCTION__);
  292. #endif
  293. break;
  294. case Xilinx_Spartan3:
  295. #if defined(CONFIG_FPGA_SPARTAN3)
  296. Spartan3_info (desc);
  297. #else
  298. /* just in case */
  299. printf ("%s: No support for Spartan-III devices.\n",
  300. __FUNCTION__);
  301. #endif
  302. break;
  303. case Xilinx_Virtex2:
  304. #if defined(CONFIG_FPGA_VIRTEX2)
  305. Virtex2_info (desc);
  306. #else
  307. /* just in case */
  308. printf ("%s: No support for Virtex-II devices.\n",
  309. __FUNCTION__);
  310. #endif
  311. break;
  312. case xilinx_zynq:
  313. #if defined(CONFIG_FPGA_ZYNQPL)
  314. zynq_info(desc);
  315. #else
  316. /* just in case */
  317. printf("%s: No support for Zynq devices.\n",
  318. __func__);
  319. #endif
  320. /* Add new family types here */
  321. default:
  322. /* we don't need a message here - we give one up above */
  323. ;
  324. }
  325. } else
  326. printf ("No Device Function Table.\n");
  327. ret_val = FPGA_SUCCESS;
  328. } else {
  329. printf ("%s: Invalid device descriptor\n", __FUNCTION__);
  330. }
  331. return ret_val;
  332. }
  333. /* ------------------------------------------------------------------------- */
  334. static int xilinx_validate (Xilinx_desc * desc, char *fn)
  335. {
  336. int ret_val = false;
  337. if (desc) {
  338. if ((desc->family > min_xilinx_type) &&
  339. (desc->family < max_xilinx_type)) {
  340. if ((desc->iface > min_xilinx_iface_type) &&
  341. (desc->iface < max_xilinx_iface_type)) {
  342. if (desc->size) {
  343. ret_val = true;
  344. } else
  345. printf ("%s: NULL part size\n", fn);
  346. } else
  347. printf ("%s: Invalid Interface type, %d\n",
  348. fn, desc->iface);
  349. } else
  350. printf ("%s: Invalid family type, %d\n", fn, desc->family);
  351. } else
  352. printf ("%s: NULL descriptor!\n", fn);
  353. return ret_val;
  354. }