]> git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/fpga/fpga.c
fpga: Clean coding style
[people/ms/u-boot.git] / drivers / fpga / 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 /* 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 static 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: CONFIG_FPGA = 0x%x\n", __func__, CONFIG_FPGA);
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 * Generic multiplexing code
192 */
193 int fpga_load(int devnum, const void *buf, size_t bsize)
194 {
195 int ret_val = FPGA_FAIL; /* assume failure */
196 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
197 (char *)__func__);
198
199 if (desc) {
200 switch (desc->devtype) {
201 case fpga_xilinx:
202 #if defined(CONFIG_FPGA_XILINX)
203 ret_val = xilinx_load(desc->devdesc, buf, bsize);
204 #else
205 fpga_no_sup((char *)__func__, "Xilinx devices");
206 #endif
207 break;
208 case fpga_altera:
209 #if defined(CONFIG_FPGA_ALTERA)
210 ret_val = altera_load(desc->devdesc, buf, bsize);
211 #else
212 fpga_no_sup((char *)__func__, "Altera devices");
213 #endif
214 break;
215 case fpga_lattice:
216 #if defined(CONFIG_FPGA_LATTICE)
217 ret_val = lattice_load(desc->devdesc, buf, bsize);
218 #else
219 fpga_no_sup((char *)__func__, "Lattice devices");
220 #endif
221 break;
222 default:
223 printf("%s: Invalid or unsupported device type %d\n",
224 __func__, desc->devtype);
225 }
226 }
227
228 return ret_val;
229 }
230
231 /*
232 * fpga_dump
233 * generic multiplexing code
234 */
235 int fpga_dump(int devnum, const void *buf, size_t bsize)
236 {
237 int ret_val = FPGA_FAIL; /* assume failure */
238 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
239 (char *)__func__);
240
241 if (desc) {
242 switch (desc->devtype) {
243 case fpga_xilinx:
244 #if defined(CONFIG_FPGA_XILINX)
245 ret_val = xilinx_dump(desc->devdesc, buf, bsize);
246 #else
247 fpga_no_sup((char *)__func__, "Xilinx devices");
248 #endif
249 break;
250 case fpga_altera:
251 #if defined(CONFIG_FPGA_ALTERA)
252 ret_val = altera_dump(desc->devdesc, buf, bsize);
253 #else
254 fpga_no_sup((char *)__func__, "Altera devices");
255 #endif
256 break;
257 case fpga_lattice:
258 #if defined(CONFIG_FPGA_LATTICE)
259 ret_val = lattice_dump(desc->devdesc, buf, bsize);
260 #else
261 fpga_no_sup((char *)__func__, "Lattice devices");
262 #endif
263 break;
264 default:
265 printf("%s: Invalid or unsupported device type %d\n",
266 __func__, desc->devtype);
267 }
268 }
269
270 return ret_val;
271 }
272
273 /*
274 * fpga_info
275 * front end to fpga_dev_info. If devnum is invalid, report on all
276 * available devices.
277 */
278 int fpga_info(int devnum)
279 {
280 if (devnum == FPGA_INVALID_DEVICE) {
281 if (next_desc > 0) {
282 int dev;
283
284 for (dev = 0; dev < next_desc; dev++)
285 fpga_dev_info(dev);
286
287 return FPGA_SUCCESS;
288 } else {
289 printf("%s: No FPGA devices available.\n", __func__);
290 return FPGA_FAIL;
291 }
292 }
293
294 return fpga_dev_info(devnum);
295 }