xref: /openbmc/u-boot/drivers/fpga/fpga.c (revision 5794619e)
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 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 */
49 static int next_desc = FPGA_INVALID_DEVICE;
50 static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
51 
52 /* Local static functions */
53 static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum );
54 static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf,
55 					 size_t bsize, char *fn );
56 static int fpga_dev_info( int devnum );
57 
58 
59 /* ------------------------------------------------------------------------- */
60 
61 /* fpga_no_sup
62  * 'no support' message function
63  */
64 static void fpga_no_sup( char *fn, char *msg )
65 {
66 	if ( fn && msg ) {
67 		printf( "%s: No support for %s.\n", fn, msg);
68 	} else if ( msg ) {
69 		printf( "No support for %s.\n", msg);
70 	} else {
71 		printf( "No FPGA suport!\n");
72 	}
73 }
74 
75 
76 /* fpga_get_desc
77  *	map a device number to a descriptor
78  */
79 static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum )
80 {
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  */
96 static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf,
97 					 size_t bsize, char *fn )
98 {
99 	fpga_desc * desc = fpga_get_desc( devnum );
100 
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 	}
109 	return desc;
110 }
111 
112 
113 /* fpga_dev_info
114  *	generic multiplexing code
115  */
116 static 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:
127 #if defined(CONFIG_FPGA_XILINX)
128 			printf( "Xilinx Device\nDescriptor @ 0x%p\n", desc );
129 			ret_val = xilinx_info( desc->devdesc );
130 #else
131 			fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
132 #endif
133 			break;
134 		case fpga_altera:
135 #if defined(CONFIG_FPGA_ALTERA)
136 			printf( "Altera Device\nDescriptor @ 0x%p\n", desc );
137 			ret_val = altera_info( desc->devdesc );
138 #else
139 			fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
140 #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 
155 /* ------------------------------------------------------------------------- */
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  */
159 void fpga_init(void)
160 {
161 	next_desc = 0;
162 	memset( desc_table, 0, sizeof(desc_table));
163 
164 	PRINTF( "%s: CONFIG_FPGA = 0x%x\n", __FUNCTION__, CONFIG_FPGA );
165 }
166 
167 /* fpga_count
168  * Basic interface function to get the current number of devices available.
169  */
170 int fpga_count( void )
171 {
172 	return next_desc;
173 }
174 
175 /* fpga_add
176  *	Add the device descriptor to the device table.
177  */
178 int 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 ) {
187 				devnum = next_desc;
188 				desc_table[next_desc].devtype = devtype;
189 				desc_table[next_desc++].devdesc = desc;
190 			} 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  */
206 int fpga_load( int devnum, void *buf, size_t bsize )
207 {
208 	int ret_val = FPGA_FAIL;           /* assume failure */
209 	fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
210 
211 	if ( desc ) {
212 		switch ( desc->devtype ) {
213 		case fpga_xilinx:
214 #if defined(CONFIG_FPGA_XILINX)
215 			ret_val = xilinx_load( desc->devdesc, buf, bsize );
216 #else
217 			fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
218 #endif
219 			break;
220 		case fpga_altera:
221 #if defined(CONFIG_FPGA_ALTERA)
222 			ret_val = altera_load( desc->devdesc, buf, bsize );
223 #else
224 			fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
225 #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  */
239 int fpga_dump( int devnum, void *buf, size_t bsize )
240 {
241 	int ret_val = FPGA_FAIL;           /* assume failure */
242 	fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
243 
244 	if ( desc ) {
245 		switch ( desc->devtype ) {
246 		case fpga_xilinx:
247 #if defined(CONFIG_FPGA_XILINX)
248 			ret_val = xilinx_dump( desc->devdesc, buf, bsize );
249 #else
250 			fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
251 #endif
252 			break;
253 		case fpga_altera:
254 #if defined(CONFIG_FPGA_ALTERA)
255 			ret_val = altera_dump( desc->devdesc, buf, bsize );
256 #else
257 			fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
258 #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  */
274 int 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 /* ------------------------------------------------------------------------- */
293