fpga.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. /*
  25. * Generic FPGA support
  26. */
  27. #include <common.h> /* core U-Boot definitions */
  28. #include <xilinx.h> /* xilinx specific definitions */
  29. #include <altera.h> /* altera specific definitions */
  30. #include <lattice.h>
  31. #if 0
  32. #define FPGA_DEBUG /* define FPGA_DEBUG to get debug messages */
  33. #endif
  34. /* Local definitions */
  35. #ifndef CONFIG_MAX_FPGA_DEVICES
  36. #define CONFIG_MAX_FPGA_DEVICES 5
  37. #endif
  38. /* Enable/Disable debug console messages */
  39. #ifdef FPGA_DEBUG
  40. #define PRINTF(fmt,args...) printf (fmt ,##args)
  41. #else
  42. #define PRINTF(fmt,args...)
  43. #endif
  44. /* Local static data */
  45. static int next_desc = FPGA_INVALID_DEVICE;
  46. static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
  47. /* Local static functions */
  48. static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum );
  49. static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf,
  50. size_t bsize, char *fn );
  51. static int fpga_dev_info( int devnum );
  52. /* ------------------------------------------------------------------------- */
  53. /* fpga_no_sup
  54. * 'no support' message function
  55. */
  56. static void fpga_no_sup( char *fn, char *msg )
  57. {
  58. if ( fn && msg ) {
  59. printf( "%s: No support for %s.\n", fn, msg);
  60. } else if ( msg ) {
  61. printf( "No support for %s.\n", msg);
  62. } else {
  63. printf( "No FPGA suport!\n");
  64. }
  65. }
  66. /* fpga_get_desc
  67. * map a device number to a descriptor
  68. */
  69. static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum )
  70. {
  71. fpga_desc *desc = (fpga_desc * )NULL;
  72. if (( devnum >= 0 ) && (devnum < next_desc )) {
  73. desc = &desc_table[devnum];
  74. PRINTF( "%s: found fpga descriptor #%d @ 0x%p\n",
  75. __FUNCTION__, devnum, desc );
  76. }
  77. return desc;
  78. }
  79. /* fpga_validate
  80. * generic parameter checking code
  81. */
  82. static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf,
  83. size_t bsize, char *fn )
  84. {
  85. fpga_desc * desc = fpga_get_desc( devnum );
  86. if ( !desc ) {
  87. printf( "%s: Invalid device number %d\n", fn, devnum );
  88. }
  89. if ( !buf ) {
  90. printf( "%s: Null buffer.\n", fn );
  91. return (fpga_desc * const)NULL;
  92. }
  93. return desc;
  94. }
  95. /* fpga_dev_info
  96. * generic multiplexing code
  97. */
  98. static int fpga_dev_info( int devnum )
  99. {
  100. int ret_val = FPGA_FAIL; /* assume failure */
  101. const fpga_desc * const desc = fpga_get_desc( devnum );
  102. if ( desc ) {
  103. PRINTF( "%s: Device Descriptor @ 0x%p\n",
  104. __FUNCTION__, desc->devdesc );
  105. switch ( desc->devtype ) {
  106. case fpga_xilinx:
  107. #if defined(CONFIG_FPGA_XILINX)
  108. printf( "Xilinx Device\nDescriptor @ 0x%p\n", desc );
  109. ret_val = xilinx_info( desc->devdesc );
  110. #else
  111. fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
  112. #endif
  113. break;
  114. case fpga_altera:
  115. #if defined(CONFIG_FPGA_ALTERA)
  116. printf( "Altera Device\nDescriptor @ 0x%p\n", desc );
  117. ret_val = altera_info( desc->devdesc );
  118. #else
  119. fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
  120. #endif
  121. break;
  122. case fpga_lattice:
  123. printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
  124. ret_val = lattice_info(desc->devdesc);
  125. break;
  126. default:
  127. printf( "%s: Invalid or unsupported device type %d\n",
  128. __FUNCTION__, desc->devtype );
  129. }
  130. } else {
  131. printf( "%s: Invalid device number %d\n",
  132. __FUNCTION__, devnum );
  133. }
  134. return ret_val;
  135. }
  136. /* ------------------------------------------------------------------------- */
  137. /* fgpa_init is usually called from misc_init_r() and MUST be called
  138. * before any of the other fpga functions are used.
  139. */
  140. void fpga_init(void)
  141. {
  142. next_desc = 0;
  143. memset( desc_table, 0, sizeof(desc_table));
  144. PRINTF( "%s: CONFIG_FPGA = 0x%x\n", __FUNCTION__, CONFIG_FPGA );
  145. }
  146. /* fpga_count
  147. * Basic interface function to get the current number of devices available.
  148. */
  149. int fpga_count( void )
  150. {
  151. return next_desc;
  152. }
  153. /* fpga_add
  154. * Add the device descriptor to the device table.
  155. */
  156. int fpga_add( fpga_type devtype, void *desc )
  157. {
  158. int devnum = FPGA_INVALID_DEVICE;
  159. if ( next_desc < 0 ) {
  160. printf( "%s: FPGA support not initialized!\n", __FUNCTION__ );
  161. } else if (( devtype > fpga_min_type ) && ( devtype < fpga_undefined )) {
  162. if ( desc ) {
  163. if ( next_desc < CONFIG_MAX_FPGA_DEVICES ) {
  164. devnum = next_desc;
  165. desc_table[next_desc].devtype = devtype;
  166. desc_table[next_desc++].devdesc = desc;
  167. } else {
  168. printf( "%s: Exceeded Max FPGA device count\n", __FUNCTION__ );
  169. }
  170. } else {
  171. printf( "%s: NULL device descriptor\n", __FUNCTION__ );
  172. }
  173. } else {
  174. printf( "%s: Unsupported FPGA type %d\n", __FUNCTION__, devtype );
  175. }
  176. return devnum;
  177. }
  178. /*
  179. * Generic multiplexing code
  180. */
  181. int fpga_load( int devnum, void *buf, size_t bsize )
  182. {
  183. int ret_val = FPGA_FAIL; /* assume failure */
  184. fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
  185. if ( desc ) {
  186. switch ( desc->devtype ) {
  187. case fpga_xilinx:
  188. #if defined(CONFIG_FPGA_XILINX)
  189. ret_val = xilinx_load( desc->devdesc, buf, bsize );
  190. #else
  191. fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
  192. #endif
  193. break;
  194. case fpga_altera:
  195. #if defined(CONFIG_FPGA_ALTERA)
  196. ret_val = altera_load( desc->devdesc, buf, bsize );
  197. #else
  198. fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
  199. #endif
  200. break;
  201. case fpga_lattice:
  202. ret_val = lattice_load(desc->devdesc, buf, bsize);
  203. break;
  204. default:
  205. printf( "%s: Invalid or unsupported device type %d\n",
  206. __FUNCTION__, desc->devtype );
  207. }
  208. }
  209. return ret_val;
  210. }
  211. /* fpga_dump
  212. * generic multiplexing code
  213. */
  214. int fpga_dump( int devnum, void *buf, size_t bsize )
  215. {
  216. int ret_val = FPGA_FAIL; /* assume failure */
  217. fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
  218. if ( desc ) {
  219. switch ( desc->devtype ) {
  220. case fpga_xilinx:
  221. #if defined(CONFIG_FPGA_XILINX)
  222. ret_val = xilinx_dump( desc->devdesc, buf, bsize );
  223. #else
  224. fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
  225. #endif
  226. break;
  227. case fpga_altera:
  228. #if defined(CONFIG_FPGA_ALTERA)
  229. ret_val = altera_dump( desc->devdesc, buf, bsize );
  230. #else
  231. fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
  232. #endif
  233. break;
  234. case fpga_lattice:
  235. ret_val = lattice_dump(desc->devdesc, buf, bsize);
  236. break;
  237. default:
  238. printf( "%s: Invalid or unsupported device type %d\n",
  239. __FUNCTION__, desc->devtype );
  240. }
  241. }
  242. return ret_val;
  243. }
  244. /* fpga_info
  245. * front end to fpga_dev_info. If devnum is invalid, report on all
  246. * available devices.
  247. */
  248. int fpga_info( int devnum )
  249. {
  250. if ( devnum == FPGA_INVALID_DEVICE ) {
  251. if ( next_desc > 0 ) {
  252. int dev;
  253. for ( dev = 0; dev < next_desc; dev++ ) {
  254. fpga_dev_info( dev );
  255. }
  256. return FPGA_SUCCESS;
  257. } else {
  258. printf( "%s: No FPGA devices available.\n", __FUNCTION__ );
  259. return FPGA_FAIL;
  260. }
  261. }
  262. else return fpga_dev_info( devnum );
  263. }
  264. /* ------------------------------------------------------------------------- */