1 // SPDX-License-Identifier: GPL-2.0+
7 /* Generic FPGA support */
10 #include <xilinx.h> /* xilinx specific definitions */
11 #include <altera.h> /* altera specific definitions */
13 #include <dm/device_compat.h>
15 /* Local static data */
16 static int next_desc = FPGA_INVALID_DEVICE;
17 static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
21 * 'no support' message function
23 static void fpga_no_sup(char *fn, char *msg)
26 printf("%s: No support for %s.\n", fn, msg);
28 printf("No support for %s.\n", msg);
30 printf("No FPGA support!\n");
35 * map a device number to a descriptor
37 const fpga_desc *const fpga_get_desc(int devnum)
39 fpga_desc *desc = (fpga_desc *)NULL;
41 if ((devnum >= 0) && (devnum < next_desc)) {
42 desc = &desc_table[devnum];
43 debug("%s: found fpga descriptor #%d @ 0x%p\n",
44 __func__, devnum, desc);
52 * generic parameter checking code
54 const fpga_desc *const fpga_validate(int devnum, const void *buf,
55 size_t bsize, char *fn)
57 const fpga_desc *desc = fpga_get_desc(devnum);
60 printf("%s: Invalid device number %d\n", fn, devnum);
63 printf("%s: Null buffer.\n", fn);
64 return (fpga_desc * const)NULL;
71 * generic multiplexing code
73 static int fpga_dev_info(int devnum)
75 int ret_val = FPGA_FAIL; /* assume failure */
76 const fpga_desc * const desc = fpga_get_desc(devnum);
79 debug("%s: Device Descriptor @ 0x%p\n",
80 __func__, desc->devdesc);
82 switch (desc->devtype) {
84 #if defined(CONFIG_FPGA_XILINX)
85 printf("Xilinx Device\nDescriptor @ 0x%p\n", desc);
86 ret_val = xilinx_info(desc->devdesc);
88 fpga_no_sup((char *)__func__, "Xilinx devices");
92 #if defined(CONFIG_FPGA_ALTERA)
93 printf("Altera Device\nDescriptor @ 0x%p\n", desc);
94 ret_val = altera_info(desc->devdesc);
96 fpga_no_sup((char *)__func__, "Altera devices");
100 #if defined(CONFIG_FPGA_LATTICE)
101 printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
102 ret_val = lattice_info(desc->devdesc);
104 fpga_no_sup((char *)__func__, "Lattice devices");
108 printf("%s: Invalid or unsupported device type %d\n",
109 __func__, desc->devtype);
112 printf("%s: Invalid device number %d\n", __func__, devnum);
119 * fpga_init is usually called from misc_init_r() and MUST be called
120 * before any of the other fpga functions are used.
125 memset(desc_table, 0, sizeof(desc_table));
127 debug("%s\n", __func__);
132 * Basic interface function to get the current number of devices available.
141 * Add the device descriptor to the device table.
143 int fpga_add(fpga_type devtype, void *desc)
145 int devnum = FPGA_INVALID_DEVICE;
148 printf("%s: NULL device descriptor\n", __func__);
153 printf("%s: FPGA support not initialized!\n", __func__);
154 } else if ((devtype > fpga_min_type) && (devtype < fpga_undefined)) {
155 if (next_desc < CONFIG_MAX_FPGA_DEVICES) {
157 desc_table[next_desc].devtype = devtype;
158 desc_table[next_desc++].devdesc = desc;
160 printf("%s: Exceeded Max FPGA device count\n",
164 printf("%s: Unsupported FPGA type %d\n", __func__, devtype);
171 * Return 1 if the fpga data is partial.
172 * This is only required for fpga drivers that support bitstream_type.
174 int __weak fpga_is_partial_data(int devnum, size_t img_len)
180 * Convert bitstream data and load into the fpga
182 int __weak fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
183 bitstream_type bstype)
185 printf("Bitstream support not implemented for this FPGA device\n");
189 #if defined(CONFIG_CMD_FPGA_LOADFS)
190 int fpga_fsload(int devnum, const void *buf, size_t size,
191 fpga_fs_info *fpga_fsinfo)
193 int ret_val = FPGA_FAIL; /* assume failure */
194 const fpga_desc *desc = fpga_validate(devnum, buf, size,
198 switch (desc->devtype) {
200 #if defined(CONFIG_FPGA_XILINX)
201 ret_val = xilinx_loadfs(desc->devdesc, buf, size,
204 fpga_no_sup((char *)__func__, "Xilinx devices");
208 printf("%s: Invalid or unsupported device type %d\n",
209 __func__, desc->devtype);
217 #if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
218 int fpga_loads(int devnum, const void *buf, size_t size,
219 struct fpga_secure_info *fpga_sec_info)
221 int ret_val = FPGA_FAIL;
223 const fpga_desc *desc = fpga_validate(devnum, buf, size,
227 switch (desc->devtype) {
229 #if defined(CONFIG_FPGA_XILINX)
230 ret_val = xilinx_loads(desc->devdesc, buf, size,
233 fpga_no_sup((char *)__func__, "Xilinx devices");
237 printf("%s: Invalid or unsupported device type %d\n",
238 __func__, desc->devtype);
246 static int fpga_load_event_notify(const void *buf, size_t bsize, int result)
248 if (CONFIG_IS_ENABLED(EVENT)) {
249 struct event_fpga_load load = {
255 return event_notify(EVT_FPGA_LOAD, &load, sizeof(load));
262 * Generic multiplexing code
264 int fpga_load(int devnum, const void *buf, size_t bsize, bitstream_type bstype,
267 int ret_val = FPGA_FAIL; /* assume failure */
269 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
273 switch (desc->devtype) {
275 #if defined(CONFIG_FPGA_XILINX)
276 ret_val = xilinx_load(desc->devdesc, buf, bsize,
279 fpga_no_sup((char *)__func__, "Xilinx devices");
283 #if defined(CONFIG_FPGA_ALTERA)
284 ret_val = altera_load(desc->devdesc, buf, bsize);
286 fpga_no_sup((char *)__func__, "Altera devices");
290 #if defined(CONFIG_FPGA_LATTICE)
291 ret_val = lattice_load(desc->devdesc, buf, bsize);
293 fpga_no_sup((char *)__func__, "Lattice devices");
297 printf("%s: Invalid or unsupported device type %d\n",
298 __func__, desc->devtype);
302 ret_notify = fpga_load_event_notify(buf, bsize, ret_val);
311 * generic multiplexing code
313 int fpga_dump(int devnum, const void *buf, size_t bsize)
315 int ret_val = FPGA_FAIL; /* assume failure */
316 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
320 switch (desc->devtype) {
322 #if defined(CONFIG_FPGA_XILINX)
323 ret_val = xilinx_dump(desc->devdesc, buf, bsize);
325 fpga_no_sup((char *)__func__, "Xilinx devices");
329 #if defined(CONFIG_FPGA_ALTERA)
330 ret_val = altera_dump(desc->devdesc, buf, bsize);
332 fpga_no_sup((char *)__func__, "Altera devices");
336 #if defined(CONFIG_FPGA_LATTICE)
337 ret_val = lattice_dump(desc->devdesc, buf, bsize);
339 fpga_no_sup((char *)__func__, "Lattice devices");
343 printf("%s: Invalid or unsupported device type %d\n",
344 __func__, desc->devtype);
353 * front end to fpga_dev_info. If devnum is invalid, report on all
356 int fpga_info(int devnum)
358 if (devnum == FPGA_INVALID_DEVICE) {
362 for (dev = 0; dev < next_desc; dev++)
367 printf("%s: No FPGA devices available.\n", __func__);
372 return fpga_dev_info(devnum);
375 #if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
376 int fpga_compatible2flag(int devnum, const char *compatible)
378 const fpga_desc * const desc = fpga_get_desc(devnum);
383 switch (desc->devtype) {
384 #if defined(CONFIG_FPGA_XILINX)
387 xilinx_desc *xdesc = (xilinx_desc *)desc->devdesc;
389 if (xdesc->operations && xdesc->operations->str2flag)
390 return xdesc->operations->str2flag(xdesc, compatible);