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