fpga.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 (next_desc < 0) {
  132. printf("%s: FPGA support not initialized!\n", __func__);
  133. } else if ((devtype > fpga_min_type) && (devtype < fpga_undefined)) {
  134. if (desc) {
  135. if (next_desc < CONFIG_MAX_FPGA_DEVICES) {
  136. devnum = next_desc;
  137. desc_table[next_desc].devtype = devtype;
  138. desc_table[next_desc++].devdesc = desc;
  139. } else {
  140. printf("%s: Exceeded Max FPGA device count\n",
  141. __func__);
  142. }
  143. } else {
  144. printf("%s: NULL device descriptor\n", __func__);
  145. }
  146. } else {
  147. printf("%s: Unsupported FPGA type %d\n", __func__, devtype);
  148. }
  149. return devnum;
  150. }
  151. /*
  152. * Convert bitstream data and load into the fpga
  153. */
  154. int __weak fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
  155. bitstream_type bstype)
  156. {
  157. printf("Bitstream support not implemented for this FPGA device\n");
  158. return FPGA_FAIL;
  159. }
  160. #if defined(CONFIG_CMD_FPGA_LOADFS)
  161. int fpga_fsload(int devnum, const void *buf, size_t size,
  162. fpga_fs_info *fpga_fsinfo)
  163. {
  164. int ret_val = FPGA_FAIL; /* assume failure */
  165. const fpga_desc *desc = fpga_validate(devnum, buf, size,
  166. (char *)__func__);
  167. if (desc) {
  168. switch (desc->devtype) {
  169. case fpga_xilinx:
  170. #if defined(CONFIG_FPGA_XILINX)
  171. ret_val = xilinx_loadfs(desc->devdesc, buf, size,
  172. fpga_fsinfo);
  173. #else
  174. fpga_no_sup((char *)__func__, "Xilinx devices");
  175. #endif
  176. break;
  177. default:
  178. printf("%s: Invalid or unsupported device type %d\n",
  179. __func__, desc->devtype);
  180. }
  181. }
  182. return ret_val;
  183. }
  184. #endif
  185. /*
  186. * Generic multiplexing code
  187. */
  188. int fpga_load(int devnum, const void *buf, size_t bsize, bitstream_type bstype)
  189. {
  190. int ret_val = FPGA_FAIL; /* assume failure */
  191. const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
  192. (char *)__func__);
  193. if (desc) {
  194. switch (desc->devtype) {
  195. case fpga_xilinx:
  196. #if defined(CONFIG_FPGA_XILINX)
  197. ret_val = xilinx_load(desc->devdesc, buf, bsize,
  198. bstype);
  199. #else
  200. fpga_no_sup((char *)__func__, "Xilinx devices");
  201. #endif
  202. break;
  203. case fpga_altera:
  204. #if defined(CONFIG_FPGA_ALTERA)
  205. ret_val = altera_load(desc->devdesc, buf, bsize);
  206. #else
  207. fpga_no_sup((char *)__func__, "Altera devices");
  208. #endif
  209. break;
  210. case fpga_lattice:
  211. #if defined(CONFIG_FPGA_LATTICE)
  212. ret_val = lattice_load(desc->devdesc, buf, bsize);
  213. #else
  214. fpga_no_sup((char *)__func__, "Lattice devices");
  215. #endif
  216. break;
  217. default:
  218. printf("%s: Invalid or unsupported device type %d\n",
  219. __func__, desc->devtype);
  220. }
  221. }
  222. return ret_val;
  223. }
  224. /*
  225. * fpga_dump
  226. * generic multiplexing code
  227. */
  228. int fpga_dump(int devnum, const void *buf, size_t bsize)
  229. {
  230. int ret_val = FPGA_FAIL; /* assume failure */
  231. const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
  232. (char *)__func__);
  233. if (desc) {
  234. switch (desc->devtype) {
  235. case fpga_xilinx:
  236. #if defined(CONFIG_FPGA_XILINX)
  237. ret_val = xilinx_dump(desc->devdesc, buf, bsize);
  238. #else
  239. fpga_no_sup((char *)__func__, "Xilinx devices");
  240. #endif
  241. break;
  242. case fpga_altera:
  243. #if defined(CONFIG_FPGA_ALTERA)
  244. ret_val = altera_dump(desc->devdesc, buf, bsize);
  245. #else
  246. fpga_no_sup((char *)__func__, "Altera devices");
  247. #endif
  248. break;
  249. case fpga_lattice:
  250. #if defined(CONFIG_FPGA_LATTICE)
  251. ret_val = lattice_dump(desc->devdesc, buf, bsize);
  252. #else
  253. fpga_no_sup((char *)__func__, "Lattice devices");
  254. #endif
  255. break;
  256. default:
  257. printf("%s: Invalid or unsupported device type %d\n",
  258. __func__, desc->devtype);
  259. }
  260. }
  261. return ret_val;
  262. }
  263. /*
  264. * fpga_info
  265. * front end to fpga_dev_info. If devnum is invalid, report on all
  266. * available devices.
  267. */
  268. int fpga_info(int devnum)
  269. {
  270. if (devnum == FPGA_INVALID_DEVICE) {
  271. if (next_desc > 0) {
  272. int dev;
  273. for (dev = 0; dev < next_desc; dev++)
  274. fpga_dev_info(dev);
  275. return FPGA_SUCCESS;
  276. } else {
  277. printf("%s: No FPGA devices available.\n", __func__);
  278. return FPGA_FAIL;
  279. }
  280. }
  281. return fpga_dev_info(devnum);
  282. }