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 */
9 #include <xilinx.h> /* xilinx specific definitions */
10 #include <altera.h> /* altera specific definitions */
13 /* Local definitions */
14 #ifndef CONFIG_MAX_FPGA_DEVICES
15 #define CONFIG_MAX_FPGA_DEVICES 5
18 /* Local static data */
19 static int next_desc = FPGA_INVALID_DEVICE;
20 static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
24 * 'no support' message function
26 static void fpga_no_sup(char *fn, char *msg)
29 printf("%s: No support for %s.\n", fn, msg);
31 printf("No support for %s.\n", msg);
33 printf("No FPGA support!\n");
38 * map a device number to a descriptor
40 const fpga_desc *const fpga_get_desc(int devnum)
42 fpga_desc *desc = (fpga_desc *)NULL;
44 if ((devnum >= 0) && (devnum < next_desc)) {
45 desc = &desc_table[devnum];
46 debug("%s: found fpga descriptor #%d @ 0x%p\n",
47 __func__, devnum, desc);
55 * generic parameter checking code
57 const fpga_desc *const fpga_validate(int devnum, const void *buf,
58 size_t bsize, char *fn)
60 const fpga_desc *desc = fpga_get_desc(devnum);
63 printf("%s: Invalid device number %d\n", fn, devnum);
66 printf("%s: Null buffer.\n", fn);
67 return (fpga_desc * const)NULL;
74 * generic multiplexing code
76 static int fpga_dev_info(int devnum)
78 int ret_val = FPGA_FAIL; /* assume failure */
79 const fpga_desc * const desc = fpga_get_desc(devnum);
82 debug("%s: Device Descriptor @ 0x%p\n",
83 __func__, desc->devdesc);
85 switch (desc->devtype) {
87 #if defined(CONFIG_FPGA_XILINX)
88 printf("Xilinx Device\nDescriptor @ 0x%p\n", desc);
89 ret_val = xilinx_info(desc->devdesc);
91 fpga_no_sup((char *)__func__, "Xilinx devices");
95 #if defined(CONFIG_FPGA_ALTERA)
96 printf("Altera Device\nDescriptor @ 0x%p\n", desc);
97 ret_val = altera_info(desc->devdesc);
99 fpga_no_sup((char *)__func__, "Altera devices");
103 #if defined(CONFIG_FPGA_LATTICE)
104 printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
105 ret_val = lattice_info(desc->devdesc);
107 fpga_no_sup((char *)__func__, "Lattice devices");
111 printf("%s: Invalid or unsupported device type %d\n",
112 __func__, desc->devtype);
115 printf("%s: Invalid device number %d\n", __func__, devnum);
122 * fpga_init is usually called from misc_init_r() and MUST be called
123 * before any of the other fpga functions are used.
128 memset(desc_table, 0, sizeof(desc_table));
130 debug("%s\n", __func__);
135 * Basic interface function to get the current number of devices available.
144 * Add the device descriptor to the device table.
146 int fpga_add(fpga_type devtype, void *desc)
148 int devnum = FPGA_INVALID_DEVICE;
151 printf("%s: NULL device descriptor\n", __func__);
156 printf("%s: FPGA support not initialized!\n", __func__);
157 } else if ((devtype > fpga_min_type) && (devtype < fpga_undefined)) {
158 if (next_desc < CONFIG_MAX_FPGA_DEVICES) {
160 desc_table[next_desc].devtype = devtype;
161 desc_table[next_desc++].devdesc = desc;
163 printf("%s: Exceeded Max FPGA device count\n",
167 printf("%s: Unsupported FPGA type %d\n", __func__, devtype);
174 * Return 1 if the fpga data is partial.
175 * This is only required for fpga drivers that support bitstream_type.
177 int __weak fpga_is_partial_data(int devnum, size_t img_len)
183 * Convert bitstream data and load into the fpga
185 int __weak fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
186 bitstream_type bstype)
188 printf("Bitstream support not implemented for this FPGA device\n");
192 #if defined(CONFIG_CMD_FPGA_LOADFS)
193 int fpga_fsload(int devnum, const void *buf, size_t size,
194 fpga_fs_info *fpga_fsinfo)
196 int ret_val = FPGA_FAIL; /* assume failure */
197 const fpga_desc *desc = fpga_validate(devnum, buf, size,
201 switch (desc->devtype) {
203 #if defined(CONFIG_FPGA_XILINX)
204 ret_val = xilinx_loadfs(desc->devdesc, buf, size,
207 fpga_no_sup((char *)__func__, "Xilinx devices");
211 printf("%s: Invalid or unsupported device type %d\n",
212 __func__, desc->devtype);
220 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
221 int fpga_loads(int devnum, const void *buf, size_t size,
222 struct fpga_secure_info *fpga_sec_info)
224 int ret_val = FPGA_FAIL;
226 const fpga_desc *desc = fpga_validate(devnum, buf, size,
230 switch (desc->devtype) {
232 #if defined(CONFIG_FPGA_XILINX)
233 ret_val = xilinx_loads(desc->devdesc, buf, size,
236 fpga_no_sup((char *)__func__, "Xilinx devices");
240 printf("%s: Invalid or unsupported device type %d\n",
241 __func__, desc->devtype);
250 * Generic multiplexing code
252 int fpga_load(int devnum, const void *buf, size_t bsize, bitstream_type bstype)
254 int ret_val = FPGA_FAIL; /* assume failure */
255 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
259 switch (desc->devtype) {
261 #if defined(CONFIG_FPGA_XILINX)
262 ret_val = xilinx_load(desc->devdesc, buf, bsize,
265 fpga_no_sup((char *)__func__, "Xilinx devices");
269 #if defined(CONFIG_FPGA_ALTERA)
270 ret_val = altera_load(desc->devdesc, buf, bsize);
272 fpga_no_sup((char *)__func__, "Altera devices");
276 #if defined(CONFIG_FPGA_LATTICE)
277 ret_val = lattice_load(desc->devdesc, buf, bsize);
279 fpga_no_sup((char *)__func__, "Lattice devices");
283 printf("%s: Invalid or unsupported device type %d\n",
284 __func__, desc->devtype);
293 * generic multiplexing code
295 int fpga_dump(int devnum, const void *buf, size_t bsize)
297 int ret_val = FPGA_FAIL; /* assume failure */
298 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
302 switch (desc->devtype) {
304 #if defined(CONFIG_FPGA_XILINX)
305 ret_val = xilinx_dump(desc->devdesc, buf, bsize);
307 fpga_no_sup((char *)__func__, "Xilinx devices");
311 #if defined(CONFIG_FPGA_ALTERA)
312 ret_val = altera_dump(desc->devdesc, buf, bsize);
314 fpga_no_sup((char *)__func__, "Altera devices");
318 #if defined(CONFIG_FPGA_LATTICE)
319 ret_val = lattice_dump(desc->devdesc, buf, bsize);
321 fpga_no_sup((char *)__func__, "Lattice devices");
325 printf("%s: Invalid or unsupported device type %d\n",
326 __func__, desc->devtype);
335 * front end to fpga_dev_info. If devnum is invalid, report on all
338 int fpga_info(int devnum)
340 if (devnum == FPGA_INVALID_DEVICE) {
344 for (dev = 0; dev < next_desc; dev++)
349 printf("%s: No FPGA devices available.\n", __func__);
354 return fpga_dev_info(devnum);