fpga.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 defined(CONFIG_FPGA)
  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 ulong relocation_offset = 0;
  46. static int next_desc = FPGA_INVALID_DEVICE;
  47. static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
  48. /* Local static functions */
  49. static const fpga_desc * const fpga_get_desc( int devnum );
  50. static const fpga_desc * const fpga_validate( int devnum, void *buf,
  51. size_t bsize, char *fn );
  52. static int fpga_dev_info( int devnum );
  53. /* ------------------------------------------------------------------------- */
  54. /* fpga_no_sup
  55. * 'no support' message function
  56. */
  57. static void fpga_no_sup( char *fn, char *msg )
  58. {
  59. if ( fn && msg ) {
  60. printf( "%s: No support for %s. CONFIG_FPGA defined as 0x%x.\n",
  61. fn, msg, CONFIG_FPGA );
  62. } else if ( msg ) {
  63. printf( "No support for %s. CONFIG_FPGA defined as 0x%x.\n",
  64. msg, CONFIG_FPGA );
  65. } else {
  66. printf( "No FPGA suport! CONFIG_FPGA defined as 0x%x.\n",
  67. CONFIG_FPGA );
  68. }
  69. }
  70. /* fpga_get_desc
  71. * map a device number to a descriptor
  72. */
  73. static const fpga_desc * const fpga_get_desc( int devnum )
  74. {
  75. fpga_desc *desc = (fpga_desc * )NULL;
  76. if (( devnum >= 0 ) && (devnum < next_desc )) {
  77. desc = &desc_table[devnum];
  78. PRINTF( "%s: found fpga descriptor #%d @ 0x%p\n",
  79. __FUNCTION__, devnum, desc );
  80. }
  81. return desc;
  82. }
  83. /* fpga_validate
  84. * generic parameter checking code
  85. */
  86. static const fpga_desc * const fpga_validate( int devnum, void *buf,
  87. size_t bsize, char *fn )
  88. {
  89. const fpga_desc * const desc = fpga_get_desc( devnum );
  90. if ( !desc ) {
  91. printf( "%s: Invalid device number %d\n", fn, devnum );
  92. }
  93. if ( !buf ) {
  94. printf( "%s: Null buffer.\n", fn );
  95. return (fpga_desc * const)NULL;
  96. }
  97. if ( !bsize ) {
  98. printf( "%s: Null buffer size.\n", fn );
  99. return (fpga_desc * const)NULL;
  100. }
  101. return desc;
  102. }
  103. /* fpga_dev_info
  104. * generic multiplexing code
  105. */
  106. static int fpga_dev_info( int devnum )
  107. {
  108. int ret_val = FPGA_FAIL; /* assume failure */
  109. const fpga_desc * const desc = fpga_get_desc( devnum );
  110. if ( desc ) {
  111. PRINTF( "%s: Device Descriptor @ 0x%p\n",
  112. __FUNCTION__, desc->devdesc );
  113. switch ( desc->devtype ) {
  114. case fpga_xilinx:
  115. #if CONFIG_FPGA & CFG_FPGA_XILINX
  116. printf( "Xilinx Device\nDescriptor @ 0x%p\n", desc );
  117. ret_val = xilinx_info( desc->devdesc );
  118. #else
  119. fpga_no_sup( __FUNCTION__, "Xilinx devices" );
  120. #endif
  121. break;
  122. case fpga_altera:
  123. #if CONFIG_FPGA & CFG_FPGA_ALTERA
  124. printf( "Altera Device\nDescriptor @ 0x%p\n", desc );
  125. ret_val = altera_info( desc->devdesc );
  126. #else
  127. fpga_no_sup( __FUNCTION__, "Altera devices" );
  128. #endif
  129. break;
  130. default:
  131. printf( "%s: Invalid or unsupported device type %d\n",
  132. __FUNCTION__, desc->devtype );
  133. }
  134. } else {
  135. printf( "%s: Invalid device number %d\n",
  136. __FUNCTION__, devnum );
  137. }
  138. return ret_val;
  139. }
  140. /* fpga_reloc
  141. * generic multiplexing code
  142. */
  143. int fpga_reloc( fpga_type devtype, void *desc, ulong reloc_off )
  144. {
  145. int ret_val = FPGA_FAIL;
  146. PRINTF( "%s: Relocating Device of type %d @ 0x%p with offset %lx\n",
  147. __FUNCTION__, devtype, desc, reloc_off );
  148. switch ( devtype ) {
  149. case fpga_xilinx:
  150. #if CONFIG_FPGA & CFG_FPGA_XILINX
  151. ret_val = xilinx_reloc( desc, reloc_off );
  152. #else
  153. fpga_no_sup( __FUNCTION__, "Xilinx devices" );
  154. #endif
  155. break;
  156. case fpga_altera:
  157. #if CONFIG_FPGA & CFG_FPGA_ALTERA
  158. ret_val = altera_reloc( desc, reloc_off );
  159. #else
  160. fpga_no_sup( __FUNCTION__, "Altera devices" );
  161. #endif
  162. break;
  163. default:
  164. printf( "%s: Invalid or unsupported device type %d\n",
  165. __FUNCTION__, devtype );
  166. }
  167. return ret_val;
  168. }
  169. /* ------------------------------------------------------------------------- */
  170. /* fgpa_init is usually called from misc_init_r() and MUST be called
  171. * before any of the other fpga functions are used.
  172. */
  173. void fpga_init( ulong reloc_off )
  174. {
  175. relocation_offset = reloc_off;
  176. next_desc = 0;
  177. memset( desc_table, 0, sizeof(desc_table));
  178. PRINTF( "%s: CONFIG_FPGA = 0x%x\n", __FUNCTION__, CONFIG_FPGA );
  179. #if 0
  180. PRINTF( "%s: CFG_FPGA_XILINX = 0x%x\n", __FUNCTION__, CFG_FPGA_XILINX );
  181. PRINTF( "%s: CFG_FPGA_ALTERA = 0x%x\n", __FUNCTION__, CFG_FPGA_ALTERA );
  182. #endif
  183. }
  184. /* fpga_count
  185. * Basic interface function to get the current number of devices available.
  186. */
  187. const int fpga_count( void )
  188. {
  189. return next_desc;
  190. }
  191. /* fpga_add
  192. * Attempts to relocate the device/board specific interface code
  193. * to the proper RAM locations and adds the device descriptor to
  194. * the device table.
  195. */
  196. int fpga_add( fpga_type devtype, void *desc )
  197. {
  198. int devnum = FPGA_INVALID_DEVICE;
  199. if ( next_desc < 0 ) {
  200. printf( "%s: FPGA support not initialized!\n", __FUNCTION__ );
  201. } else if (( devtype > fpga_min_type ) && ( devtype < fpga_undefined )) {
  202. if ( desc ) {
  203. if ( next_desc < CONFIG_MAX_FPGA_DEVICES ) {
  204. if ( fpga_reloc( devtype, desc, relocation_offset )
  205. == FPGA_SUCCESS ) {
  206. devnum = next_desc;
  207. desc_table[next_desc].devtype = devtype;
  208. desc_table[next_desc++].devdesc = desc;
  209. } else {
  210. printf( "%s: Unable to relocate device interface table!\n",
  211. __FUNCTION__ );
  212. }
  213. } else {
  214. printf( "%s: Exceeded Max FPGA device count\n", __FUNCTION__ );
  215. }
  216. } else {
  217. printf( "%s: NULL device descriptor\n", __FUNCTION__ );
  218. }
  219. } else {
  220. printf( "%s: Unsupported FPGA type %d\n", __FUNCTION__, devtype );
  221. }
  222. return devnum;
  223. }
  224. /*
  225. * Generic multiplexing code
  226. */
  227. int fpga_load( int devnum, void *buf, size_t bsize )
  228. {
  229. int ret_val = FPGA_FAIL; /* assume failure */
  230. const fpga_desc * const desc = fpga_validate( devnum, buf, bsize, __FUNCTION__ );
  231. if ( desc ) {
  232. switch ( desc->devtype ) {
  233. case fpga_xilinx:
  234. #if CONFIG_FPGA & CFG_FPGA_XILINX
  235. ret_val = xilinx_load( desc->devdesc, buf, bsize );
  236. #else
  237. fpga_no_sup( __FUNCTION__, "Xilinx devices" );
  238. #endif
  239. break;
  240. case fpga_altera:
  241. #if CONFIG_FPGA & CFG_FPGA_ALTERA
  242. ret_val = altera_load( desc->devdesc, buf, bsize );
  243. #else
  244. fpga_no_sup( __FUNCTION__, "Altera devices" );
  245. #endif
  246. break;
  247. default:
  248. printf( "%s: Invalid or unsupported device type %d\n",
  249. __FUNCTION__, desc->devtype );
  250. }
  251. }
  252. return ret_val;
  253. }
  254. /* fpga_dump
  255. * generic multiplexing code
  256. */
  257. int fpga_dump( int devnum, void *buf, size_t bsize )
  258. {
  259. int ret_val = FPGA_FAIL; /* assume failure */
  260. const fpga_desc * const desc = fpga_validate( devnum, buf, bsize, __FUNCTION__ );
  261. if ( desc ) {
  262. switch ( desc->devtype ) {
  263. case fpga_xilinx:
  264. #if CONFIG_FPGA & CFG_FPGA_XILINX
  265. ret_val = xilinx_dump( desc->devdesc, buf, bsize );
  266. #else
  267. fpga_no_sup( __FUNCTION__, "Xilinx devices" );
  268. #endif
  269. break;
  270. case fpga_altera:
  271. #if CONFIG_FPGA & CFG_FPGA_ALTERA
  272. ret_val = altera_dump( desc->devdesc, buf, bsize );
  273. #else
  274. fpga_no_sup( __FUNCTION__, "Altera devices" );
  275. #endif
  276. break;
  277. default:
  278. printf( "%s: Invalid or unsupported device type %d\n",
  279. __FUNCTION__, desc->devtype );
  280. }
  281. }
  282. return ret_val;
  283. }
  284. /* fpga_info
  285. * front end to fpga_dev_info. If devnum is invalid, report on all
  286. * available devices.
  287. */
  288. int fpga_info( int devnum )
  289. {
  290. if ( devnum == FPGA_INVALID_DEVICE ) {
  291. if ( next_desc > 0 ) {
  292. int dev;
  293. for ( dev = 0; dev < next_desc; dev++ ) {
  294. fpga_dev_info( dev );
  295. }
  296. return FPGA_SUCCESS;
  297. } else {
  298. printf( "%s: No FPGA devices available.\n", __FUNCTION__ );
  299. return FPGA_FAIL;
  300. }
  301. }
  302. else return fpga_dev_info( devnum );
  303. }
  304. /* ------------------------------------------------------------------------- */
  305. #endif /* CONFIG_FPGA */