xilinx.c 9.6 KB

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