blob: 5659517793e15f3e6c44cc714efc3c1b227bd843 [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 */
wdenke2211742002-11-02 23:30:20 +000049static int next_desc = FPGA_INVALID_DEVICE;
50static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
51
52/* Local static functions */
Wolfgang Denk77ddac92005-10-13 16:45:02 +020053static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum );
54static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf,
wdenk8bde7f72003-06-27 21:31:46 +000055 size_t bsize, char *fn );
wdenke2211742002-11-02 23:30:20 +000056static int fpga_dev_info( int devnum );
57
58
59/* ------------------------------------------------------------------------- */
60
61/* fpga_no_sup
62 * 'no support' message function
63 */
64static void fpga_no_sup( char *fn, char *msg )
65{
66 if ( fn && msg ) {
Matthias Fuchs01335022007-12-27 17:12:34 +010067 printf( "%s: No support for %s.\n", fn, msg);
wdenke2211742002-11-02 23:30:20 +000068 } else if ( msg ) {
Matthias Fuchs01335022007-12-27 17:12:34 +010069 printf( "No support for %s.\n", msg);
wdenke2211742002-11-02 23:30:20 +000070 } else {
Matthias Fuchs01335022007-12-27 17:12:34 +010071 printf( "No FPGA suport!\n");
wdenke2211742002-11-02 23:30:20 +000072 }
73}
74
75
76/* fpga_get_desc
77 * map a device number to a descriptor
78 */
Wolfgang Denk77ddac92005-10-13 16:45:02 +020079static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum )
wdenke2211742002-11-02 23:30:20 +000080{
81 fpga_desc *desc = (fpga_desc * )NULL;
82
83 if (( devnum >= 0 ) && (devnum < next_desc )) {
84 desc = &desc_table[devnum];
85 PRINTF( "%s: found fpga descriptor #%d @ 0x%p\n",
86 __FUNCTION__, devnum, desc );
87 }
88
89 return desc;
90}
91
92
93/* fpga_validate
94 * generic parameter checking code
95 */
Wolfgang Denk77ddac92005-10-13 16:45:02 +020096static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf,
wdenk8bde7f72003-06-27 21:31:46 +000097 size_t bsize, char *fn )
wdenke2211742002-11-02 23:30:20 +000098{
Wolfgang Denk77ddac92005-10-13 16:45:02 +020099 fpga_desc * desc = fpga_get_desc( devnum );
wdenke2211742002-11-02 23:30:20 +0000100
101 if ( !desc ) {
102 printf( "%s: Invalid device number %d\n", fn, devnum );
103 }
104
105 if ( !buf ) {
106 printf( "%s: Null buffer.\n", fn );
107 return (fpga_desc * const)NULL;
108 }
wdenke2211742002-11-02 23:30:20 +0000109 return desc;
110}
111
112
113/* fpga_dev_info
114 * generic multiplexing code
115 */
116static int fpga_dev_info( int devnum )
117{
118 int ret_val = FPGA_FAIL; /* assume failure */
119 const fpga_desc * const desc = fpga_get_desc( devnum );
120
121 if ( desc ) {
122 PRINTF( "%s: Device Descriptor @ 0x%p\n",
123 __FUNCTION__, desc->devdesc );
124
125 switch ( desc->devtype ) {
126 case fpga_xilinx:
Matthias Fuchs01335022007-12-27 17:12:34 +0100127#if defined(CONFIG_FPGA_XILINX)
wdenke2211742002-11-02 23:30:20 +0000128 printf( "Xilinx Device\nDescriptor @ 0x%p\n", desc );
129 ret_val = xilinx_info( desc->devdesc );
130#else
Stefan Roese64cd52e2006-09-18 10:48:03 +0200131 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
wdenke2211742002-11-02 23:30:20 +0000132#endif
133 break;
134 case fpga_altera:
Matthias Fuchs01335022007-12-27 17:12:34 +0100135#if defined(CONFIG_FPGA_ALTERA)
wdenke2211742002-11-02 23:30:20 +0000136 printf( "Altera Device\nDescriptor @ 0x%p\n", desc );
137 ret_val = altera_info( desc->devdesc );
138#else
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200139 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
wdenke2211742002-11-02 23:30:20 +0000140#endif
141 break;
142 default:
143 printf( "%s: Invalid or unsupported device type %d\n",
144 __FUNCTION__, desc->devtype );
145 }
146 } else {
147 printf( "%s: Invalid device number %d\n",
148 __FUNCTION__, devnum );
149 }
150
151 return ret_val;
152}
153
154
wdenke2211742002-11-02 23:30:20 +0000155/* ------------------------------------------------------------------------- */
156/* fgpa_init is usually called from misc_init_r() and MUST be called
157 * before any of the other fpga functions are used.
158 */
Peter Tyser6385b282009-09-21 11:20:32 -0500159void fpga_init(void)
wdenke2211742002-11-02 23:30:20 +0000160{
wdenke2211742002-11-02 23:30:20 +0000161 next_desc = 0;
162 memset( desc_table, 0, sizeof(desc_table));
163
164 PRINTF( "%s: CONFIG_FPGA = 0x%x\n", __FUNCTION__, CONFIG_FPGA );
wdenke2211742002-11-02 23:30:20 +0000165}
166
167/* fpga_count
168 * Basic interface function to get the current number of devices available.
169 */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200170int fpga_count( void )
wdenke2211742002-11-02 23:30:20 +0000171{
172 return next_desc;
173}
174
175/* fpga_add
Peter Tyser6385b282009-09-21 11:20:32 -0500176 * Add the device descriptor to the device table.
wdenke2211742002-11-02 23:30:20 +0000177 */
178int fpga_add( fpga_type devtype, void *desc )
179{
180 int devnum = FPGA_INVALID_DEVICE;
181
182 if ( next_desc < 0 ) {
183 printf( "%s: FPGA support not initialized!\n", __FUNCTION__ );
184 } else if (( devtype > fpga_min_type ) && ( devtype < fpga_undefined )) {
185 if ( desc ) {
186 if ( next_desc < CONFIG_MAX_FPGA_DEVICES ) {
Peter Tyser6385b282009-09-21 11:20:32 -0500187 devnum = next_desc;
188 desc_table[next_desc].devtype = devtype;
189 desc_table[next_desc++].devdesc = desc;
wdenke2211742002-11-02 23:30:20 +0000190 } else {
191 printf( "%s: Exceeded Max FPGA device count\n", __FUNCTION__ );
192 }
193 } else {
194 printf( "%s: NULL device descriptor\n", __FUNCTION__ );
195 }
196 } else {
197 printf( "%s: Unsupported FPGA type %d\n", __FUNCTION__, devtype );
198 }
199
200 return devnum;
201}
202
203/*
204 * Generic multiplexing code
205 */
206int fpga_load( int devnum, void *buf, size_t bsize )
207{
208 int ret_val = FPGA_FAIL; /* assume failure */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200209 fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
wdenke2211742002-11-02 23:30:20 +0000210
211 if ( desc ) {
212 switch ( desc->devtype ) {
213 case fpga_xilinx:
Matthias Fuchs01335022007-12-27 17:12:34 +0100214#if defined(CONFIG_FPGA_XILINX)
wdenke2211742002-11-02 23:30:20 +0000215 ret_val = xilinx_load( desc->devdesc, buf, bsize );
216#else
Stefan Roese64cd52e2006-09-18 10:48:03 +0200217 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
wdenke2211742002-11-02 23:30:20 +0000218#endif
219 break;
220 case fpga_altera:
Matthias Fuchs01335022007-12-27 17:12:34 +0100221#if defined(CONFIG_FPGA_ALTERA)
wdenke2211742002-11-02 23:30:20 +0000222 ret_val = altera_load( desc->devdesc, buf, bsize );
223#else
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200224 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
wdenke2211742002-11-02 23:30:20 +0000225#endif
226 break;
227 default:
228 printf( "%s: Invalid or unsupported device type %d\n",
229 __FUNCTION__, desc->devtype );
230 }
231 }
232
233 return ret_val;
234}
235
236/* fpga_dump
237 * generic multiplexing code
238 */
239int fpga_dump( int devnum, void *buf, size_t bsize )
240{
241 int ret_val = FPGA_FAIL; /* assume failure */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200242 fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
wdenke2211742002-11-02 23:30:20 +0000243
244 if ( desc ) {
245 switch ( desc->devtype ) {
246 case fpga_xilinx:
Matthias Fuchs01335022007-12-27 17:12:34 +0100247#if defined(CONFIG_FPGA_XILINX)
wdenke2211742002-11-02 23:30:20 +0000248 ret_val = xilinx_dump( desc->devdesc, buf, bsize );
249#else
Stefan Roese64cd52e2006-09-18 10:48:03 +0200250 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
wdenke2211742002-11-02 23:30:20 +0000251#endif
252 break;
253 case fpga_altera:
Matthias Fuchs01335022007-12-27 17:12:34 +0100254#if defined(CONFIG_FPGA_ALTERA)
wdenke2211742002-11-02 23:30:20 +0000255 ret_val = altera_dump( desc->devdesc, buf, bsize );
256#else
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200257 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
wdenke2211742002-11-02 23:30:20 +0000258#endif
259 break;
260 default:
261 printf( "%s: Invalid or unsupported device type %d\n",
262 __FUNCTION__, desc->devtype );
263 }
264 }
265
266 return ret_val;
267}
268
269
270/* fpga_info
271 * front end to fpga_dev_info. If devnum is invalid, report on all
272 * available devices.
273 */
274int fpga_info( int devnum )
275{
276 if ( devnum == FPGA_INVALID_DEVICE ) {
277 if ( next_desc > 0 ) {
278 int dev;
279
280 for ( dev = 0; dev < next_desc; dev++ ) {
281 fpga_dev_info( dev );
282 }
283 return FPGA_SUCCESS;
284 } else {
285 printf( "%s: No FPGA devices available.\n", __FUNCTION__ );
286 return FPGA_FAIL;
287 }
288 }
289 else return fpga_dev_info( devnum );
290}
291
292/* ------------------------------------------------------------------------- */