fpga.c 7.1 KB

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