fpga.c 7.8 KB

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