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