xilinx.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. /* Local Static Functions */
  20. static int xilinx_validate(xilinx_desc *desc, char *fn);
  21. /* ------------------------------------------------------------------------- */
  22. int fpga_is_partial_data(int devnum, size_t img_len)
  23. {
  24. const fpga_desc * const desc = fpga_get_desc(devnum);
  25. xilinx_desc *desc_xilinx = desc->devdesc;
  26. /* Check datasize against FPGA size */
  27. if (img_len >= desc_xilinx->size)
  28. return 0;
  29. /* datasize is smaller, must be partial data */
  30. return 1;
  31. }
  32. int fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
  33. bitstream_type bstype)
  34. {
  35. unsigned int length;
  36. unsigned int swapsize;
  37. unsigned char *dataptr;
  38. unsigned int i;
  39. const fpga_desc *desc;
  40. xilinx_desc *xdesc;
  41. dataptr = (unsigned char *)fpgadata;
  42. /* Find out fpga_description */
  43. desc = fpga_validate(devnum, dataptr, 0, (char *)__func__);
  44. /* Assign xilinx device description */
  45. xdesc = desc->devdesc;
  46. /* skip the first bytes of the bitsteam, their meaning is unknown */
  47. length = (*dataptr << 8) + *(dataptr + 1);
  48. dataptr += 2;
  49. dataptr += length;
  50. /* get design name (identifier, length, string) */
  51. length = (*dataptr << 8) + *(dataptr + 1);
  52. dataptr += 2;
  53. if (*dataptr++ != 0x61) {
  54. debug("%s: Design name id not recognized in bitstream\n",
  55. __func__);
  56. return FPGA_FAIL;
  57. }
  58. length = (*dataptr << 8) + *(dataptr + 1);
  59. dataptr += 2;
  60. printf(" design filename = \"%s\"\n", dataptr);
  61. dataptr += length;
  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. if (xdesc->name) {
  71. i = (ulong)strstr((char *)dataptr, xdesc->name);
  72. if (!i) {
  73. printf("%s: Wrong bitstream ID for this device\n",
  74. __func__);
  75. printf("%s: Bitstream ID %s, current device ID %d/%s\n",
  76. __func__, dataptr, devnum, xdesc->name);
  77. return FPGA_FAIL;
  78. }
  79. } else {
  80. printf("%s: Please fill correct device ID to xilinx_desc\n",
  81. __func__);
  82. }
  83. printf(" part number = \"%s\"\n", dataptr);
  84. dataptr += length;
  85. /* get date (identifier, length, string) */
  86. if (*dataptr++ != 0x63) {
  87. printf("%s: Date identifier not recognized in bitstream\n",
  88. __func__);
  89. return FPGA_FAIL;
  90. }
  91. length = (*dataptr << 8) + *(dataptr+1);
  92. dataptr += 2;
  93. printf(" date = \"%s\"\n", dataptr);
  94. dataptr += length;
  95. /* get time (identifier, length, string) */
  96. if (*dataptr++ != 0x64) {
  97. printf("%s: Time identifier not recognized in bitstream\n",
  98. __func__);
  99. return FPGA_FAIL;
  100. }
  101. length = (*dataptr << 8) + *(dataptr+1);
  102. dataptr += 2;
  103. printf(" time = \"%s\"\n", dataptr);
  104. dataptr += length;
  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, bstype);
  118. }
  119. int xilinx_load(xilinx_desc *desc, const void *buf, size_t bsize,
  120. bitstream_type bstype)
  121. {
  122. if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
  123. printf ("%s: Invalid device descriptor\n", __FUNCTION__);
  124. return FPGA_FAIL;
  125. }
  126. if (!desc->operations || !desc->operations->load) {
  127. printf("%s: Missing load operation\n", __func__);
  128. return FPGA_FAIL;
  129. }
  130. return desc->operations->load(desc, buf, bsize, bstype);
  131. }
  132. #if defined(CONFIG_CMD_FPGA_LOADFS)
  133. int xilinx_loadfs(xilinx_desc *desc, const void *buf, size_t bsize,
  134. fpga_fs_info *fpga_fsinfo)
  135. {
  136. if (!xilinx_validate(desc, (char *)__func__)) {
  137. printf("%s: Invalid device descriptor\n", __func__);
  138. return FPGA_FAIL;
  139. }
  140. if (!desc->operations || !desc->operations->loadfs) {
  141. printf("%s: Missing loadfs operation\n", __func__);
  142. return FPGA_FAIL;
  143. }
  144. return desc->operations->loadfs(desc, buf, bsize, fpga_fsinfo);
  145. }
  146. #endif
  147. int xilinx_dump(xilinx_desc *desc, const void *buf, size_t bsize)
  148. {
  149. if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
  150. printf ("%s: Invalid device descriptor\n", __FUNCTION__);
  151. return FPGA_FAIL;
  152. }
  153. if (!desc->operations || !desc->operations->dump) {
  154. printf("%s: Missing dump operation\n", __func__);
  155. return FPGA_FAIL;
  156. }
  157. return desc->operations->dump(desc, buf, bsize);
  158. }
  159. int xilinx_info(xilinx_desc *desc)
  160. {
  161. int ret_val = FPGA_FAIL;
  162. if (xilinx_validate (desc, (char *)__FUNCTION__)) {
  163. printf ("Family: \t");
  164. switch (desc->family) {
  165. case xilinx_spartan2:
  166. printf ("Spartan-II\n");
  167. break;
  168. case xilinx_spartan3:
  169. printf ("Spartan-III\n");
  170. break;
  171. case xilinx_virtex2:
  172. printf ("Virtex-II\n");
  173. break;
  174. case xilinx_zynq:
  175. printf("Zynq PL\n");
  176. break;
  177. case xilinx_zynqmp:
  178. printf("ZynqMP PL\n");
  179. break;
  180. /* Add new family types here */
  181. default:
  182. printf ("Unknown family type, %d\n", desc->family);
  183. }
  184. printf ("Interface type:\t");
  185. switch (desc->iface) {
  186. case slave_serial:
  187. printf ("Slave Serial\n");
  188. break;
  189. case master_serial: /* Not used */
  190. printf ("Master Serial\n");
  191. break;
  192. case slave_parallel:
  193. printf ("Slave Parallel\n");
  194. break;
  195. case jtag_mode: /* Not used */
  196. printf ("JTAG Mode\n");
  197. break;
  198. case slave_selectmap:
  199. printf ("Slave SelectMap Mode\n");
  200. break;
  201. case master_selectmap:
  202. printf ("Master SelectMap Mode\n");
  203. break;
  204. case devcfg:
  205. printf("Device configuration interface (Zynq)\n");
  206. break;
  207. case csu_dma:
  208. printf("csu_dma configuration interface (ZynqMP)\n");
  209. break;
  210. /* Add new interface types here */
  211. default:
  212. printf ("Unsupported interface type, %d\n", desc->iface);
  213. }
  214. printf("Device Size: \t%zd bytes\n"
  215. "Cookie: \t0x%x (%d)\n",
  216. desc->size, desc->cookie, desc->cookie);
  217. if (desc->name)
  218. printf("Device name: \t%s\n", desc->name);
  219. if (desc->iface_fns)
  220. printf ("Device Function Table @ 0x%p\n", desc->iface_fns);
  221. else
  222. printf ("No Device Function Table.\n");
  223. if (desc->operations && desc->operations->info)
  224. desc->operations->info(desc);
  225. ret_val = FPGA_SUCCESS;
  226. } else {
  227. printf ("%s: Invalid device descriptor\n", __FUNCTION__);
  228. }
  229. return ret_val;
  230. }
  231. /* ------------------------------------------------------------------------- */
  232. static int xilinx_validate(xilinx_desc *desc, char *fn)
  233. {
  234. int ret_val = false;
  235. if (desc) {
  236. if ((desc->family > min_xilinx_type) &&
  237. (desc->family < max_xilinx_type)) {
  238. if ((desc->iface > min_xilinx_iface_type) &&
  239. (desc->iface < max_xilinx_iface_type)) {
  240. if (desc->size) {
  241. ret_val = true;
  242. } else
  243. printf ("%s: NULL part size\n", fn);
  244. } else
  245. printf ("%s: Invalid Interface type, %d\n",
  246. fn, desc->iface);
  247. } else
  248. printf ("%s: Invalid family type, %d\n", fn, desc->family);
  249. } else
  250. printf ("%s: NULL descriptor!\n", fn);
  251. return ret_val;
  252. }