fpga.c 7.3 KB

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