fpga.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 suport!\n");
  31. }
  32. /* fpga_get_desc
  33. * map a device number to a descriptor
  34. */
  35. static 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. * fgpa_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. {
  156. printf("Bitstream support not implemented for this FPGA device\n");
  157. return FPGA_FAIL;
  158. }
  159. /*
  160. * Generic multiplexing code
  161. */
  162. int fpga_load(int devnum, const void *buf, size_t bsize)
  163. {
  164. int ret_val = FPGA_FAIL; /* assume failure */
  165. const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
  166. (char *)__func__);
  167. if (desc) {
  168. switch (desc->devtype) {
  169. case fpga_xilinx:
  170. #if defined(CONFIG_FPGA_XILINX)
  171. ret_val = xilinx_load(desc->devdesc, buf, bsize);
  172. #else
  173. fpga_no_sup((char *)__func__, "Xilinx devices");
  174. #endif
  175. break;
  176. case fpga_altera:
  177. #if defined(CONFIG_FPGA_ALTERA)
  178. ret_val = altera_load(desc->devdesc, buf, bsize);
  179. #else
  180. fpga_no_sup((char *)__func__, "Altera devices");
  181. #endif
  182. break;
  183. case fpga_lattice:
  184. #if defined(CONFIG_FPGA_LATTICE)
  185. ret_val = lattice_load(desc->devdesc, buf, bsize);
  186. #else
  187. fpga_no_sup((char *)__func__, "Lattice devices");
  188. #endif
  189. break;
  190. default:
  191. printf("%s: Invalid or unsupported device type %d\n",
  192. __func__, desc->devtype);
  193. }
  194. }
  195. return ret_val;
  196. }
  197. /*
  198. * fpga_dump
  199. * generic multiplexing code
  200. */
  201. int fpga_dump(int devnum, const void *buf, size_t bsize)
  202. {
  203. int ret_val = FPGA_FAIL; /* assume failure */
  204. const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
  205. (char *)__func__);
  206. if (desc) {
  207. switch (desc->devtype) {
  208. case fpga_xilinx:
  209. #if defined(CONFIG_FPGA_XILINX)
  210. ret_val = xilinx_dump(desc->devdesc, buf, bsize);
  211. #else
  212. fpga_no_sup((char *)__func__, "Xilinx devices");
  213. #endif
  214. break;
  215. case fpga_altera:
  216. #if defined(CONFIG_FPGA_ALTERA)
  217. ret_val = altera_dump(desc->devdesc, buf, bsize);
  218. #else
  219. fpga_no_sup((char *)__func__, "Altera devices");
  220. #endif
  221. break;
  222. case fpga_lattice:
  223. #if defined(CONFIG_FPGA_LATTICE)
  224. ret_val = lattice_dump(desc->devdesc, buf, bsize);
  225. #else
  226. fpga_no_sup((char *)__func__, "Lattice devices");
  227. #endif
  228. break;
  229. default:
  230. printf("%s: Invalid or unsupported device type %d\n",
  231. __func__, desc->devtype);
  232. }
  233. }
  234. return ret_val;
  235. }
  236. /*
  237. * fpga_info
  238. * front end to fpga_dev_info. If devnum is invalid, report on all
  239. * available devices.
  240. */
  241. int fpga_info(int devnum)
  242. {
  243. if (devnum == FPGA_INVALID_DEVICE) {
  244. if (next_desc > 0) {
  245. int dev;
  246. for (dev = 0; dev < next_desc; dev++)
  247. fpga_dev_info(dev);
  248. return FPGA_SUCCESS;
  249. } else {
  250. printf("%s: No FPGA devices available.\n", __func__);
  251. return FPGA_FAIL;
  252. }
  253. }
  254. return fpga_dev_info(devnum);
  255. }