fpga.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. /*
  193. * Generic multiplexing code
  194. */
  195. int fpga_load(int devnum, const void *buf, size_t bsize, bitstream_type bstype)
  196. {
  197. int ret_val = FPGA_FAIL; /* assume failure */
  198. const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
  199. (char *)__func__);
  200. if (desc) {
  201. switch (desc->devtype) {
  202. case fpga_xilinx:
  203. #if defined(CONFIG_FPGA_XILINX)
  204. ret_val = xilinx_load(desc->devdesc, buf, bsize,
  205. bstype);
  206. #else
  207. fpga_no_sup((char *)__func__, "Xilinx devices");
  208. #endif
  209. break;
  210. case fpga_altera:
  211. #if defined(CONFIG_FPGA_ALTERA)
  212. ret_val = altera_load(desc->devdesc, buf, bsize);
  213. #else
  214. fpga_no_sup((char *)__func__, "Altera devices");
  215. #endif
  216. break;
  217. case fpga_lattice:
  218. #if defined(CONFIG_FPGA_LATTICE)
  219. ret_val = lattice_load(desc->devdesc, buf, bsize);
  220. #else
  221. fpga_no_sup((char *)__func__, "Lattice devices");
  222. #endif
  223. break;
  224. default:
  225. printf("%s: Invalid or unsupported device type %d\n",
  226. __func__, desc->devtype);
  227. }
  228. }
  229. return ret_val;
  230. }
  231. /*
  232. * fpga_dump
  233. * generic multiplexing code
  234. */
  235. int fpga_dump(int devnum, const void *buf, size_t bsize)
  236. {
  237. int ret_val = FPGA_FAIL; /* assume failure */
  238. const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
  239. (char *)__func__);
  240. if (desc) {
  241. switch (desc->devtype) {
  242. case fpga_xilinx:
  243. #if defined(CONFIG_FPGA_XILINX)
  244. ret_val = xilinx_dump(desc->devdesc, buf, bsize);
  245. #else
  246. fpga_no_sup((char *)__func__, "Xilinx devices");
  247. #endif
  248. break;
  249. case fpga_altera:
  250. #if defined(CONFIG_FPGA_ALTERA)
  251. ret_val = altera_dump(desc->devdesc, buf, bsize);
  252. #else
  253. fpga_no_sup((char *)__func__, "Altera devices");
  254. #endif
  255. break;
  256. case fpga_lattice:
  257. #if defined(CONFIG_FPGA_LATTICE)
  258. ret_val = lattice_dump(desc->devdesc, buf, bsize);
  259. #else
  260. fpga_no_sup((char *)__func__, "Lattice devices");
  261. #endif
  262. break;
  263. default:
  264. printf("%s: Invalid or unsupported device type %d\n",
  265. __func__, desc->devtype);
  266. }
  267. }
  268. return ret_val;
  269. }
  270. /*
  271. * fpga_info
  272. * front end to fpga_dev_info. If devnum is invalid, report on all
  273. * available devices.
  274. */
  275. int fpga_info(int devnum)
  276. {
  277. if (devnum == FPGA_INVALID_DEVICE) {
  278. if (next_desc > 0) {
  279. int dev;
  280. for (dev = 0; dev < next_desc; dev++)
  281. fpga_dev_info(dev);
  282. return FPGA_SUCCESS;
  283. } else {
  284. printf("%s: No FPGA devices available.\n", __func__);
  285. return FPGA_FAIL;
  286. }
  287. }
  288. return fpga_dev_info(devnum);
  289. }