blob: 67a6c300c788a2e96eff924bdf9996d275463c1a [file] [log] [blame]
wdenke2211742002-11-02 23:30:20 +00001/*
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
wdenke2211742002-11-02 23:30:20 +000032#if 0
33#define FPGA_DEBUG /* define FPGA_DEBUG to get debug messages */
34#endif
35
36/* Local definitions */
37#ifndef CONFIG_MAX_FPGA_DEVICES
38#define CONFIG_MAX_FPGA_DEVICES 5
39#endif
40
41/* Enable/Disable debug console messages */
42#ifdef FPGA_DEBUG
43#define PRINTF(fmt,args...) printf (fmt ,##args)
44#else
45#define PRINTF(fmt,args...)
46#endif
47
48/* Local static data */
49static ulong relocation_offset = 0;
50static int next_desc = FPGA_INVALID_DEVICE;
51static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
52
53/* Local static functions */
Wolfgang Denk7fb52662005-10-13 16:45:02 +020054static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum );
55static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf,
wdenk57b2d802003-06-27 21:31:46 +000056 size_t bsize, char *fn );
wdenke2211742002-11-02 23:30:20 +000057static int fpga_dev_info( int devnum );
58
59
60/* ------------------------------------------------------------------------- */
61
62/* fpga_no_sup
63 * 'no support' message function
64 */
65static void fpga_no_sup( char *fn, char *msg )
66{
67 if ( fn && msg ) {
Matthias Fuchsa4400872007-12-27 17:12:34 +010068 printf( "%s: No support for %s.\n", fn, msg);
wdenke2211742002-11-02 23:30:20 +000069 } else if ( msg ) {
Matthias Fuchsa4400872007-12-27 17:12:34 +010070 printf( "No support for %s.\n", msg);
wdenke2211742002-11-02 23:30:20 +000071 } else {
Matthias Fuchsa4400872007-12-27 17:12:34 +010072 printf( "No FPGA suport!\n");
wdenke2211742002-11-02 23:30:20 +000073 }
74}
75
76
77/* fpga_get_desc
78 * map a device number to a descriptor
79 */
Wolfgang Denk7fb52662005-10-13 16:45:02 +020080static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum )
wdenke2211742002-11-02 23:30:20 +000081{
82 fpga_desc *desc = (fpga_desc * )NULL;
83
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 );
88 }
89
90 return desc;
91}
92
93
94/* fpga_validate
95 * generic parameter checking code
96 */
Wolfgang Denk7fb52662005-10-13 16:45:02 +020097static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf,
wdenk57b2d802003-06-27 21:31:46 +000098 size_t bsize, char *fn )
wdenke2211742002-11-02 23:30:20 +000099{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200100 fpga_desc * desc = fpga_get_desc( devnum );
wdenke2211742002-11-02 23:30:20 +0000101
102 if ( !desc ) {
103 printf( "%s: Invalid device number %d\n", fn, devnum );
104 }
105
106 if ( !buf ) {
107 printf( "%s: Null buffer.\n", fn );
108 return (fpga_desc * const)NULL;
109 }
wdenke2211742002-11-02 23:30:20 +0000110 return desc;
111}
112
113
114/* fpga_dev_info
115 * generic multiplexing code
116 */
117static int fpga_dev_info( int devnum )
118{
119 int ret_val = FPGA_FAIL; /* assume failure */
120 const fpga_desc * const desc = fpga_get_desc( devnum );
121
122 if ( desc ) {
123 PRINTF( "%s: Device Descriptor @ 0x%p\n",
124 __FUNCTION__, desc->devdesc );
125
126 switch ( desc->devtype ) {
127 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100128#if defined(CONFIG_FPGA_XILINX)
wdenke2211742002-11-02 23:30:20 +0000129 printf( "Xilinx Device\nDescriptor @ 0x%p\n", desc );
130 ret_val = xilinx_info( desc->devdesc );
131#else
Stefan Roese0b294432006-09-18 10:48:03 +0200132 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
wdenke2211742002-11-02 23:30:20 +0000133#endif
134 break;
135 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100136#if defined(CONFIG_FPGA_ALTERA)
wdenke2211742002-11-02 23:30:20 +0000137 printf( "Altera Device\nDescriptor @ 0x%p\n", desc );
138 ret_val = altera_info( desc->devdesc );
139#else
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200140 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
wdenke2211742002-11-02 23:30:20 +0000141#endif
142 break;
143 default:
144 printf( "%s: Invalid or unsupported device type %d\n",
145 __FUNCTION__, desc->devtype );
146 }
147 } else {
148 printf( "%s: Invalid device number %d\n",
149 __FUNCTION__, devnum );
150 }
151
152 return ret_val;
153}
154
155
156/* fpga_reloc
157 * generic multiplexing code
158 */
159int fpga_reloc( fpga_type devtype, void *desc, ulong reloc_off )
160{
161 int ret_val = FPGA_FAIL;
162
163 PRINTF( "%s: Relocating Device of type %d @ 0x%p with offset %lx\n",
164 __FUNCTION__, devtype, desc, reloc_off );
165
166 switch ( devtype ) {
167 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100168#if defined(CONFIG_FPGA_XILINX)
wdenke2211742002-11-02 23:30:20 +0000169 ret_val = xilinx_reloc( desc, reloc_off );
170#else
Stefan Roese0b294432006-09-18 10:48:03 +0200171 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
wdenke2211742002-11-02 23:30:20 +0000172#endif
173 break;
174 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100175#if defined(CONFIG_FPGA_ALTERA)
wdenke2211742002-11-02 23:30:20 +0000176 ret_val = altera_reloc( desc, reloc_off );
177#else
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200178 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
wdenke2211742002-11-02 23:30:20 +0000179#endif
180 break;
181 default:
182 printf( "%s: Invalid or unsupported device type %d\n",
183 __FUNCTION__, devtype );
184 }
185
186 return ret_val;
187}
188
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.
192 */
193void fpga_init( ulong reloc_off )
194{
195 relocation_offset = reloc_off;
196 next_desc = 0;
197 memset( desc_table, 0, sizeof(desc_table));
198
199 PRINTF( "%s: CONFIG_FPGA = 0x%x\n", __FUNCTION__, CONFIG_FPGA );
wdenke2211742002-11-02 23:30:20 +0000200}
201
202/* fpga_count
203 * Basic interface function to get the current number of devices available.
204 */
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200205int fpga_count( void )
wdenke2211742002-11-02 23:30:20 +0000206{
207 return next_desc;
208}
209
210/* fpga_add
211 * Attempts to relocate the device/board specific interface code
212 * to the proper RAM locations and adds the device descriptor to
213 * the device table.
214 */
215int fpga_add( fpga_type devtype, void *desc )
216{
217 int devnum = FPGA_INVALID_DEVICE;
218
219 if ( next_desc < 0 ) {
220 printf( "%s: FPGA support not initialized!\n", __FUNCTION__ );
221 } else if (( devtype > fpga_min_type ) && ( devtype < fpga_undefined )) {
222 if ( desc ) {
223 if ( next_desc < CONFIG_MAX_FPGA_DEVICES ) {
224 if ( fpga_reloc( devtype, desc, relocation_offset )
225 == FPGA_SUCCESS ) {
226 devnum = next_desc;
227 desc_table[next_desc].devtype = devtype;
228 desc_table[next_desc++].devdesc = desc;
229 } else {
230 printf( "%s: Unable to relocate device interface table!\n",
231 __FUNCTION__ );
232 }
233 } else {
234 printf( "%s: Exceeded Max FPGA device count\n", __FUNCTION__ );
235 }
236 } else {
237 printf( "%s: NULL device descriptor\n", __FUNCTION__ );
238 }
239 } else {
240 printf( "%s: Unsupported FPGA type %d\n", __FUNCTION__, devtype );
241 }
242
243 return devnum;
244}
245
246/*
247 * Generic multiplexing code
248 */
249int fpga_load( int devnum, void *buf, size_t bsize )
250{
251 int ret_val = FPGA_FAIL; /* assume failure */
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200252 fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
wdenke2211742002-11-02 23:30:20 +0000253
254 if ( desc ) {
255 switch ( desc->devtype ) {
256 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100257#if defined(CONFIG_FPGA_XILINX)
wdenke2211742002-11-02 23:30:20 +0000258 ret_val = xilinx_load( desc->devdesc, buf, bsize );
259#else
Stefan Roese0b294432006-09-18 10:48:03 +0200260 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
wdenke2211742002-11-02 23:30:20 +0000261#endif
262 break;
263 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100264#if defined(CONFIG_FPGA_ALTERA)
wdenke2211742002-11-02 23:30:20 +0000265 ret_val = altera_load( desc->devdesc, buf, bsize );
266#else
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200267 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
wdenke2211742002-11-02 23:30:20 +0000268#endif
269 break;
270 default:
271 printf( "%s: Invalid or unsupported device type %d\n",
272 __FUNCTION__, desc->devtype );
273 }
274 }
275
276 return ret_val;
277}
278
279/* fpga_dump
280 * generic multiplexing code
281 */
282int fpga_dump( int devnum, void *buf, size_t bsize )
283{
284 int ret_val = FPGA_FAIL; /* assume failure */
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200285 fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
wdenke2211742002-11-02 23:30:20 +0000286
287 if ( desc ) {
288 switch ( desc->devtype ) {
289 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100290#if defined(CONFIG_FPGA_XILINX)
wdenke2211742002-11-02 23:30:20 +0000291 ret_val = xilinx_dump( desc->devdesc, buf, bsize );
292#else
Stefan Roese0b294432006-09-18 10:48:03 +0200293 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
wdenke2211742002-11-02 23:30:20 +0000294#endif
295 break;
296 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100297#if defined(CONFIG_FPGA_ALTERA)
wdenke2211742002-11-02 23:30:20 +0000298 ret_val = altera_dump( desc->devdesc, buf, bsize );
299#else
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200300 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
wdenke2211742002-11-02 23:30:20 +0000301#endif
302 break;
303 default:
304 printf( "%s: Invalid or unsupported device type %d\n",
305 __FUNCTION__, desc->devtype );
306 }
307 }
308
309 return ret_val;
310}
311
312
313/* fpga_info
314 * front end to fpga_dev_info. If devnum is invalid, report on all
315 * available devices.
316 */
317int fpga_info( int devnum )
318{
319 if ( devnum == FPGA_INVALID_DEVICE ) {
320 if ( next_desc > 0 ) {
321 int dev;
322
323 for ( dev = 0; dev < next_desc; dev++ ) {
324 fpga_dev_info( dev );
325 }
326 return FPGA_SUCCESS;
327 } else {
328 printf( "%s: No FPGA devices available.\n", __FUNCTION__ );
329 return FPGA_FAIL;
330 }
331 }
332 else return fpga_dev_info( devnum );
333}
334
335/* ------------------------------------------------------------------------- */