fpga.c 6.8 KB

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