3 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 * Generic FPGA support
28 #include <common.h> /* core U-Boot definitions */
29 #include <xilinx.h> /* xilinx specific definitions */
30 #include <altera.h> /* altera specific definitions */
33 #define FPGA_DEBUG /* define FPGA_DEBUG to get debug messages */
36 /* Local definitions */
37 #ifndef CONFIG_MAX_FPGA_DEVICES
38 #define CONFIG_MAX_FPGA_DEVICES 5
41 /* Enable/Disable debug console messages */
43 #define PRINTF(fmt,args...) printf (fmt ,##args)
45 #define PRINTF(fmt,args...)
48 /* Local static data */
49 static ulong relocation_offset = 0;
50 static int next_desc = FPGA_INVALID_DEVICE;
51 static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
53 /* Local static functions */
54 static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum );
55 static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf,
56 size_t bsize, char *fn );
57 static int fpga_dev_info( int devnum );
60 /* ------------------------------------------------------------------------- */
63 * 'no support' message function
65 static void fpga_no_sup( char *fn, char *msg )
68 printf( "%s: No support for %s.\n", fn, msg);
70 printf( "No support for %s.\n", msg);
72 printf( "No FPGA suport!\n");
78 * map a device number to a descriptor
80 static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum )
82 fpga_desc *desc = (fpga_desc * )NULL;
84 if (( devnum >= 0 ) && (devnum < next_desc )) {
85 desc = &desc_table[devnum];
86 PRINTF( "%s: found fpga descriptor #%d @ 0x%p\n",
87 __FUNCTION__, devnum, desc );
95 * generic parameter checking code
97 static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf,
98 size_t bsize, char *fn )
100 fpga_desc * desc = fpga_get_desc( devnum );
103 printf( "%s: Invalid device number %d\n", fn, devnum );
107 printf( "%s: Null buffer.\n", fn );
108 return (fpga_desc * const)NULL;
115 * generic multiplexing code
117 static int fpga_dev_info( int devnum )
119 int ret_val = FPGA_FAIL; /* assume failure */
120 const fpga_desc * const desc = fpga_get_desc( devnum );
123 PRINTF( "%s: Device Descriptor @ 0x%p\n",
124 __FUNCTION__, desc->devdesc );
126 switch ( desc->devtype ) {
128 #if defined(CONFIG_FPGA_XILINX)
129 printf( "Xilinx Device\nDescriptor @ 0x%p\n", desc );
130 ret_val = xilinx_info( desc->devdesc );
132 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
136 #if defined(CONFIG_FPGA_ALTERA)
137 printf( "Altera Device\nDescriptor @ 0x%p\n", desc );
138 ret_val = altera_info( desc->devdesc );
140 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
144 printf( "%s: Invalid or unsupported device type %d\n",
145 __FUNCTION__, desc->devtype );
148 printf( "%s: Invalid device number %d\n",
149 __FUNCTION__, devnum );
157 * generic multiplexing code
159 int fpga_reloc( fpga_type devtype, void *desc, ulong reloc_off )
161 int ret_val = FPGA_FAIL;
163 PRINTF( "%s: Relocating Device of type %d @ 0x%p with offset %lx\n",
164 __FUNCTION__, devtype, desc, reloc_off );
168 #if defined(CONFIG_FPGA_XILINX)
169 ret_val = xilinx_reloc( desc, reloc_off );
171 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
175 #if defined(CONFIG_FPGA_ALTERA)
176 ret_val = altera_reloc( desc, reloc_off );
178 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
182 printf( "%s: Invalid or unsupported device type %d\n",
183 __FUNCTION__, devtype );
189 /* ------------------------------------------------------------------------- */
190 /* fgpa_init is usually called from misc_init_r() and MUST be called
191 * before any of the other fpga functions are used.
193 void fpga_init( ulong reloc_off )
195 relocation_offset = reloc_off;
197 memset( desc_table, 0, sizeof(desc_table));
199 PRINTF( "%s: CONFIG_FPGA = 0x%x\n", __FUNCTION__, CONFIG_FPGA );
203 * Basic interface function to get the current number of devices available.
205 int fpga_count( void )
211 * Attempts to relocate the device/board specific interface code
212 * to the proper RAM locations and adds the device descriptor to
215 int fpga_add( fpga_type devtype, void *desc )
217 int devnum = FPGA_INVALID_DEVICE;
219 if ( next_desc < 0 ) {
220 printf( "%s: FPGA support not initialized!\n", __FUNCTION__ );
221 } else if (( devtype > fpga_min_type ) && ( devtype < fpga_undefined )) {
223 if ( next_desc < CONFIG_MAX_FPGA_DEVICES ) {
224 if ( fpga_reloc( devtype, desc, relocation_offset )
227 desc_table[next_desc].devtype = devtype;
228 desc_table[next_desc++].devdesc = desc;
230 printf( "%s: Unable to relocate device interface table!\n",
234 printf( "%s: Exceeded Max FPGA device count\n", __FUNCTION__ );
237 printf( "%s: NULL device descriptor\n", __FUNCTION__ );
240 printf( "%s: Unsupported FPGA type %d\n", __FUNCTION__, devtype );
247 * Generic multiplexing code
249 int fpga_load( int devnum, void *buf, size_t bsize )
251 int ret_val = FPGA_FAIL; /* assume failure */
252 fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
255 switch ( desc->devtype ) {
257 #if defined(CONFIG_FPGA_XILINX)
258 ret_val = xilinx_load( desc->devdesc, buf, bsize );
260 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
264 #if defined(CONFIG_FPGA_ALTERA)
265 ret_val = altera_load( desc->devdesc, buf, bsize );
267 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
271 printf( "%s: Invalid or unsupported device type %d\n",
272 __FUNCTION__, desc->devtype );
280 * generic multiplexing code
282 int fpga_dump( int devnum, void *buf, size_t bsize )
284 int ret_val = FPGA_FAIL; /* assume failure */
285 fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
288 switch ( desc->devtype ) {
290 #if defined(CONFIG_FPGA_XILINX)
291 ret_val = xilinx_dump( desc->devdesc, buf, bsize );
293 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
297 #if defined(CONFIG_FPGA_ALTERA)
298 ret_val = altera_dump( desc->devdesc, buf, bsize );
300 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
304 printf( "%s: Invalid or unsupported device type %d\n",
305 __FUNCTION__, desc->devtype );
314 * front end to fpga_dev_info. If devnum is invalid, report on all
317 int fpga_info( int devnum )
319 if ( devnum == FPGA_INVALID_DEVICE ) {
320 if ( next_desc > 0 ) {
323 for ( dev = 0; dev < next_desc; dev++ ) {
324 fpga_dev_info( dev );
328 printf( "%s: No FPGA devices available.\n", __FUNCTION__ );
332 else return fpga_dev_info( devnum );
335 /* ------------------------------------------------------------------------- */