fpga.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * (C) Copyright 2002
  3. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /* Generic FPGA support */
  8. #include <common.h> /* core U-Boot definitions */
  9. #include <xilinx.h> /* xilinx specific definitions */
  10. #include <altera.h> /* altera specific definitions */
  11. #include <lattice.h>
  12. /* Local definitions */
  13. #ifndef CONFIG_MAX_FPGA_DEVICES
  14. #define CONFIG_MAX_FPGA_DEVICES 5
  15. #endif
  16. /* Local static data */
  17. static int next_desc = FPGA_INVALID_DEVICE;
  18. static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
  19. /*
  20. * fpga_no_sup
  21. * 'no support' message function
  22. */
  23. static void fpga_no_sup(char *fn, char *msg)
  24. {
  25. if (fn && msg)
  26. printf("%s: No support for %s.\n", fn, msg);
  27. else if (msg)
  28. printf("No support for %s.\n", msg);
  29. else
  30. printf("No FPGA support!\n");
  31. }
  32. /* fpga_get_desc
  33. * map a device number to a descriptor
  34. */
  35. const fpga_desc *const fpga_get_desc(int devnum)
  36. {
  37. fpga_desc *desc = (fpga_desc *)NULL;
  38. if ((devnum >= 0) && (devnum < next_desc)) {
  39. desc = &desc_table[devnum];
  40. debug("%s: found fpga descriptor #%d @ 0x%p\n",
  41. __func__, devnum, desc);
  42. }
  43. return desc;
  44. }
  45. /*
  46. * fpga_validate
  47. * generic parameter checking code
  48. */
  49. const fpga_desc *const fpga_validate(int devnum, const void *buf,
  50. size_t bsize, char *fn)
  51. {
  52. const fpga_desc *desc = fpga_get_desc(devnum);
  53. if (!desc)
  54. printf("%s: Invalid device number %d\n", fn, devnum);
  55. if (!buf) {
  56. printf("%s: Null buffer.\n", fn);
  57. return (fpga_desc * const)NULL;
  58. }
  59. return desc;
  60. }
  61. /*
  62. * fpga_dev_info
  63. * generic multiplexing code
  64. */
  65. static int fpga_dev_info(int devnum)
  66. {
  67. int ret_val = FPGA_FAIL; /* assume failure */
  68. const fpga_desc * const desc = fpga_get_desc(devnum);
  69. if (desc) {
  70. debug("%s: Device Descriptor @ 0x%p\n",
  71. __func__, desc->devdesc);
  72. switch (desc->devtype) {
  73. case fpga_xilinx:
  74. #if defined(CONFIG_FPGA_XILINX)
  75. printf("Xilinx Device\nDescriptor @ 0x%p\n", desc);
  76. ret_val = xilinx_info(desc->devdesc);
  77. #else
  78. fpga_no_sup((char *)__func__, "Xilinx devices");
  79. #endif
  80. break;
  81. case fpga_altera:
  82. #if defined(CONFIG_FPGA_ALTERA)
  83. printf("Altera Device\nDescriptor @ 0x%p\n", desc);
  84. ret_val = altera_info(desc->devdesc);
  85. #else
  86. fpga_no_sup((char *)__func__, "Altera devices");
  87. #endif
  88. break;
  89. case fpga_lattice:
  90. #if defined(CONFIG_FPGA_LATTICE)
  91. printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
  92. ret_val = lattice_info(desc->devdesc);
  93. #else
  94. fpga_no_sup((char *)__func__, "Lattice devices");
  95. #endif
  96. break;
  97. default:
  98. printf("%s: Invalid or unsupported device type %d\n",
  99. __func__, desc->devtype);
  100. }
  101. } else {
  102. printf("%s: Invalid device number %d\n", __func__, devnum);
  103. }
  104. return ret_val;
  105. }
  106. /*
  107. * fpga_init is usually called from misc_init_r() and MUST be called
  108. * before any of the other fpga functions are used.
  109. */
  110. void fpga_init(void)
  111. {
  112. next_desc = 0;
  113. memset(desc_table, 0, sizeof(desc_table));
  114. debug("%s\n", __func__);
  115. }
  116. /*
  117. * fpga_count
  118. * Basic interface function to get the current number of devices available.
  119. */
  120. int fpga_count(void)
  121. {
  122. return next_desc;
  123. }
  124. /*
  125. * fpga_add
  126. * Add the device descriptor to the device table.
  127. */
  128. int fpga_add(fpga_type devtype, void *desc)
  129. {
  130. int devnum = FPGA_INVALID_DEVICE;
  131. if (!desc) {
  132. printf("%s: NULL device descriptor\n", __func__);
  133. return devnum;
  134. }
  135. if (next_desc < 0) {
  136. printf("%s: FPGA support not initialized!\n", __func__);
  137. } else if ((devtype > fpga_min_type) && (devtype < fpga_undefined)) {
  138. if (next_desc < CONFIG_MAX_FPGA_DEVICES) {
  139. devnum = next_desc;
  140. desc_table[next_desc].devtype = devtype;
  141. desc_table[next_desc++].devdesc = desc;
  142. } else {
  143. printf("%s: Exceeded Max FPGA device count\n",
  144. __func__);
  145. }
  146. } else {
  147. printf("%s: Unsupported FPGA type %d\n", __func__, devtype);
  148. }
  149. return devnum;
  150. }
  151. /*
  152. * Return 1 if the fpga data is partial.
  153. * This is only required for fpga drivers that support bitstream_type.
  154. */
  155. int __weak fpga_is_partial_data(int devnum, size_t img_len)
  156. {
  157. return 0;
  158. }
  159. /*
  160. * Convert bitstream data and load into the fpga
  161. */
  162. int __weak fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
  163. bitstream_type bstype)
  164. {
  165. printf("Bitstream support not implemented for this FPGA device\n");
  166. return FPGA_FAIL;
  167. }
  168. #if defined(CONFIG_CMD_FPGA_LOADFS)
  169. int fpga_fsload(int devnum, const void *buf, size_t size,
  170. fpga_fs_info *fpga_fsinfo)
  171. {
  172. int ret_val = FPGA_FAIL; /* assume failure */
  173. const fpga_desc *desc = fpga_validate(devnum, buf, size,
  174. (char *)__func__);
  175. if (desc) {
  176. switch (desc->devtype) {
  177. case fpga_xilinx:
  178. #if defined(CONFIG_FPGA_XILINX)
  179. ret_val = xilinx_loadfs(desc->devdesc, buf, size,
  180. fpga_fsinfo);
  181. #else
  182. fpga_no_sup((char *)__func__, "Xilinx devices");
  183. #endif
  184. break;
  185. default:
  186. printf("%s: Invalid or unsupported device type %d\n",
  187. __func__, desc->devtype);
  188. }
  189. }
  190. return ret_val;
  191. }
  192. #endif
  193. /*
  194. * Generic multiplexing code
  195. */
  196. int fpga_load(int devnum, const void *buf, size_t bsize, bitstream_type bstype)
  197. {
  198. int ret_val = FPGA_FAIL; /* assume failure */
  199. const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
  200. (char *)__func__);
  201. if (desc) {
  202. switch (desc->devtype) {
  203. case fpga_xilinx:
  204. #if defined(CONFIG_FPGA_XILINX)
  205. ret_val = xilinx_load(desc->devdesc, buf, bsize,
  206. bstype);
  207. #else
  208. fpga_no_sup((char *)__func__, "Xilinx devices");
  209. #endif
  210. break;
  211. case fpga_altera:
  212. #if defined(CONFIG_FPGA_ALTERA)
  213. ret_val = altera_load(desc->devdesc, buf, bsize);
  214. #else
  215. fpga_no_sup((char *)__func__, "Altera devices");
  216. #endif
  217. break;
  218. case fpga_lattice:
  219. #if defined(CONFIG_FPGA_LATTICE)
  220. ret_val = lattice_load(desc->devdesc, buf, bsize);
  221. #else
  222. fpga_no_sup((char *)__func__, "Lattice devices");
  223. #endif
  224. break;
  225. default:
  226. printf("%s: Invalid or unsupported device type %d\n",
  227. __func__, desc->devtype);
  228. }
  229. }
  230. return ret_val;
  231. }
  232. /*
  233. * fpga_dump
  234. * generic multiplexing code
  235. */
  236. int fpga_dump(int devnum, const void *buf, size_t bsize)
  237. {
  238. int ret_val = FPGA_FAIL; /* assume failure */
  239. const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
  240. (char *)__func__);
  241. if (desc) {
  242. switch (desc->devtype) {
  243. case fpga_xilinx:
  244. #if defined(CONFIG_FPGA_XILINX)
  245. ret_val = xilinx_dump(desc->devdesc, buf, bsize);
  246. #else
  247. fpga_no_sup((char *)__func__, "Xilinx devices");
  248. #endif
  249. break;
  250. case fpga_altera:
  251. #if defined(CONFIG_FPGA_ALTERA)
  252. ret_val = altera_dump(desc->devdesc, buf, bsize);
  253. #else
  254. fpga_no_sup((char *)__func__, "Altera devices");
  255. #endif
  256. break;
  257. case fpga_lattice:
  258. #if defined(CONFIG_FPGA_LATTICE)
  259. ret_val = lattice_dump(desc->devdesc, buf, bsize);
  260. #else
  261. fpga_no_sup((char *)__func__, "Lattice devices");
  262. #endif
  263. break;
  264. default:
  265. printf("%s: Invalid or unsupported device type %d\n",
  266. __func__, desc->devtype);
  267. }
  268. }
  269. return ret_val;
  270. }
  271. /*
  272. * fpga_info
  273. * front end to fpga_dev_info. If devnum is invalid, report on all
  274. * available devices.
  275. */
  276. int fpga_info(int devnum)
  277. {
  278. if (devnum == FPGA_INVALID_DEVICE) {
  279. if (next_desc > 0) {
  280. int dev;
  281. for (dev = 0; dev < next_desc; dev++)
  282. fpga_dev_info(dev);
  283. return FPGA_SUCCESS;
  284. } else {
  285. printf("%s: No FPGA devices available.\n", __func__);
  286. return FPGA_FAIL;
  287. }
  288. }
  289. return fpga_dev_info(devnum);
  290. }