1 // SPDX-License-Identifier: GPL-2.0+
4 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
7 /* Generic FPGA support */
8 #include <common.h> /* core U-Boot definitions */
11 #include <xilinx.h> /* xilinx specific definitions */
12 #include <altera.h> /* altera specific definitions */
14 #include <dm/device_compat.h>
16 /* Local definitions */
17 #ifndef CONFIG_MAX_FPGA_DEVICES
18 #define CONFIG_MAX_FPGA_DEVICES 5
21 /* Local static data */
22 static int next_desc = FPGA_INVALID_DEVICE;
23 static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
27 * 'no support' message function
29 static void fpga_no_sup(char *fn, char *msg)
32 printf("%s: No support for %s.\n", fn, msg);
34 printf("No support for %s.\n", msg);
36 printf("No FPGA support!\n");
41 * map a device number to a descriptor
43 const fpga_desc *const fpga_get_desc(int devnum)
45 fpga_desc *desc = (fpga_desc *)NULL;
47 if ((devnum >= 0) && (devnum < next_desc)) {
48 desc = &desc_table[devnum];
49 debug("%s: found fpga descriptor #%d @ 0x%p\n",
50 __func__, devnum, desc);
58 * generic parameter checking code
60 const fpga_desc *const fpga_validate(int devnum, const void *buf,
61 size_t bsize, char *fn)
63 const fpga_desc *desc = fpga_get_desc(devnum);
66 printf("%s: Invalid device number %d\n", fn, devnum);
69 printf("%s: Null buffer.\n", fn);
70 return (fpga_desc * const)NULL;
77 * generic multiplexing code
79 static int fpga_dev_info(int devnum)
81 int ret_val = FPGA_FAIL; /* assume failure */
82 const fpga_desc * const desc = fpga_get_desc(devnum);
85 debug("%s: Device Descriptor @ 0x%p\n",
86 __func__, desc->devdesc);
88 switch (desc->devtype) {
90 #if defined(CONFIG_FPGA_XILINX)
91 printf("Xilinx Device\nDescriptor @ 0x%p\n", desc);
92 ret_val = xilinx_info(desc->devdesc);
94 fpga_no_sup((char *)__func__, "Xilinx devices");
98 #if defined(CONFIG_FPGA_ALTERA)
99 printf("Altera Device\nDescriptor @ 0x%p\n", desc);
100 ret_val = altera_info(desc->devdesc);
102 fpga_no_sup((char *)__func__, "Altera devices");
106 #if defined(CONFIG_FPGA_LATTICE)
107 printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
108 ret_val = lattice_info(desc->devdesc);
110 fpga_no_sup((char *)__func__, "Lattice devices");
114 printf("%s: Invalid or unsupported device type %d\n",
115 __func__, desc->devtype);
118 printf("%s: Invalid device number %d\n", __func__, devnum);
125 * fpga_init is usually called from misc_init_r() and MUST be called
126 * before any of the other fpga functions are used.
131 memset(desc_table, 0, sizeof(desc_table));
133 debug("%s\n", __func__);
138 * Basic interface function to get the current number of devices available.
147 * Add the device descriptor to the device table.
149 int fpga_add(fpga_type devtype, void *desc)
151 int devnum = FPGA_INVALID_DEVICE;
154 printf("%s: NULL device descriptor\n", __func__);
159 printf("%s: FPGA support not initialized!\n", __func__);
160 } else if ((devtype > fpga_min_type) && (devtype < fpga_undefined)) {
161 if (next_desc < CONFIG_MAX_FPGA_DEVICES) {
163 desc_table[next_desc].devtype = devtype;
164 desc_table[next_desc++].devdesc = desc;
166 printf("%s: Exceeded Max FPGA device count\n",
170 printf("%s: Unsupported FPGA type %d\n", __func__, devtype);
177 * Return 1 if the fpga data is partial.
178 * This is only required for fpga drivers that support bitstream_type.
180 int __weak fpga_is_partial_data(int devnum, size_t img_len)
186 * Convert bitstream data and load into the fpga
188 int __weak fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
189 bitstream_type bstype)
191 printf("Bitstream support not implemented for this FPGA device\n");
195 #if defined(CONFIG_CMD_FPGA_LOADFS)
196 int fpga_fsload(int devnum, const void *buf, size_t size,
197 fpga_fs_info *fpga_fsinfo)
199 int ret_val = FPGA_FAIL; /* assume failure */
200 const fpga_desc *desc = fpga_validate(devnum, buf, size,
204 switch (desc->devtype) {
206 #if defined(CONFIG_FPGA_XILINX)
207 ret_val = xilinx_loadfs(desc->devdesc, buf, size,
210 fpga_no_sup((char *)__func__, "Xilinx devices");
214 printf("%s: Invalid or unsupported device type %d\n",
215 __func__, desc->devtype);
223 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
224 int fpga_loads(int devnum, const void *buf, size_t size,
225 struct fpga_secure_info *fpga_sec_info)
227 int ret_val = FPGA_FAIL;
229 const fpga_desc *desc = fpga_validate(devnum, buf, size,
233 switch (desc->devtype) {
235 #if defined(CONFIG_FPGA_XILINX)
236 ret_val = xilinx_loads(desc->devdesc, buf, size,
239 fpga_no_sup((char *)__func__, "Xilinx devices");
243 printf("%s: Invalid or unsupported device type %d\n",
244 __func__, desc->devtype);
253 * Generic multiplexing code
255 int fpga_load(int devnum, const void *buf, size_t bsize, bitstream_type bstype)
257 int ret_val = FPGA_FAIL; /* assume failure */
258 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
262 switch (desc->devtype) {
264 #if defined(CONFIG_FPGA_XILINX)
265 ret_val = xilinx_load(desc->devdesc, buf, bsize,
268 fpga_no_sup((char *)__func__, "Xilinx devices");
272 #if defined(CONFIG_FPGA_ALTERA)
273 ret_val = altera_load(desc->devdesc, buf, bsize);
275 fpga_no_sup((char *)__func__, "Altera devices");
279 #if defined(CONFIG_FPGA_LATTICE)
280 ret_val = lattice_load(desc->devdesc, buf, bsize);
282 fpga_no_sup((char *)__func__, "Lattice devices");
286 printf("%s: Invalid or unsupported device type %d\n",
287 __func__, desc->devtype);
296 * generic multiplexing code
298 int fpga_dump(int devnum, const void *buf, size_t bsize)
300 int ret_val = FPGA_FAIL; /* assume failure */
301 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
305 switch (desc->devtype) {
307 #if defined(CONFIG_FPGA_XILINX)
308 ret_val = xilinx_dump(desc->devdesc, buf, bsize);
310 fpga_no_sup((char *)__func__, "Xilinx devices");
314 #if defined(CONFIG_FPGA_ALTERA)
315 ret_val = altera_dump(desc->devdesc, buf, bsize);
317 fpga_no_sup((char *)__func__, "Altera devices");
321 #if defined(CONFIG_FPGA_LATTICE)
322 ret_val = lattice_dump(desc->devdesc, buf, bsize);
324 fpga_no_sup((char *)__func__, "Lattice devices");
328 printf("%s: Invalid or unsupported device type %d\n",
329 __func__, desc->devtype);
338 * front end to fpga_dev_info. If devnum is invalid, report on all
341 int fpga_info(int devnum)
343 if (devnum == FPGA_INVALID_DEVICE) {
347 for (dev = 0; dev < next_desc; dev++)
352 printf("%s: No FPGA devices available.\n", __func__);
357 return fpga_dev_info(devnum);