blob: fe3dfa12335d59d4a737fc005b3ceb1bac71f3b1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenke2211742002-11-02 23:30:20 +00002/*
3 * (C) Copyright 2002
4 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
wdenke2211742002-11-02 23:30:20 +00005 */
6
Michal Simekf6555d92013-04-26 08:46:56 +02007/* Generic FPGA support */
wdenke2211742002-11-02 23:30:20 +00008#include <common.h> /* core U-Boot definitions */
Simon Glass691d7192020-05-10 11:40:02 -06009#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
wdenke2211742002-11-02 23:30:20 +000011#include <xilinx.h> /* xilinx specific definitions */
12#include <altera.h> /* altera specific definitions */
Stefano Babic3b8ac462010-06-29 11:47:48 +020013#include <lattice.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <dm/device_compat.h>
wdenke2211742002-11-02 23:30:20 +000015
wdenke2211742002-11-02 23:30:20 +000016/* Local definitions */
17#ifndef CONFIG_MAX_FPGA_DEVICES
18#define CONFIG_MAX_FPGA_DEVICES 5
19#endif
20
wdenke2211742002-11-02 23:30:20 +000021/* Local static data */
wdenke2211742002-11-02 23:30:20 +000022static int next_desc = FPGA_INVALID_DEVICE;
23static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
24
Michal Simekf6555d92013-04-26 08:46:56 +020025/*
26 * fpga_no_sup
wdenke2211742002-11-02 23:30:20 +000027 * 'no support' message function
28 */
Michal Simekf6555d92013-04-26 08:46:56 +020029static void fpga_no_sup(char *fn, char *msg)
wdenke2211742002-11-02 23:30:20 +000030{
Michal Simekf6555d92013-04-26 08:46:56 +020031 if (fn && msg)
32 printf("%s: No support for %s.\n", fn, msg);
33 else if (msg)
34 printf("No support for %s.\n", msg);
35 else
Robert P. J. Day62a3b7d2016-07-15 13:44:45 -040036 printf("No FPGA support!\n");
wdenke2211742002-11-02 23:30:20 +000037}
38
39
40/* fpga_get_desc
41 * map a device number to a descriptor
42 */
Michal Simekebd322d2015-01-13 16:09:53 +010043const fpga_desc *const fpga_get_desc(int devnum)
wdenke2211742002-11-02 23:30:20 +000044{
Michal Simekf6555d92013-04-26 08:46:56 +020045 fpga_desc *desc = (fpga_desc *)NULL;
wdenke2211742002-11-02 23:30:20 +000046
Michal Simekf6555d92013-04-26 08:46:56 +020047 if ((devnum >= 0) && (devnum < next_desc)) {
wdenke2211742002-11-02 23:30:20 +000048 desc = &desc_table[devnum];
Michal Simekf6555d92013-04-26 08:46:56 +020049 debug("%s: found fpga descriptor #%d @ 0x%p\n",
50 __func__, devnum, desc);
wdenke2211742002-11-02 23:30:20 +000051 }
52
53 return desc;
54}
55
Michal Simekf6555d92013-04-26 08:46:56 +020056/*
57 * fpga_validate
wdenke2211742002-11-02 23:30:20 +000058 * generic parameter checking code
59 */
Michal Simek6631db42013-04-26 15:04:48 +020060const fpga_desc *const fpga_validate(int devnum, const void *buf,
61 size_t bsize, char *fn)
wdenke2211742002-11-02 23:30:20 +000062{
Michal Simekf6555d92013-04-26 08:46:56 +020063 const fpga_desc *desc = fpga_get_desc(devnum);
wdenke2211742002-11-02 23:30:20 +000064
Michal Simekf6555d92013-04-26 08:46:56 +020065 if (!desc)
66 printf("%s: Invalid device number %d\n", fn, devnum);
wdenke2211742002-11-02 23:30:20 +000067
Michal Simekf6555d92013-04-26 08:46:56 +020068 if (!buf) {
69 printf("%s: Null buffer.\n", fn);
wdenke2211742002-11-02 23:30:20 +000070 return (fpga_desc * const)NULL;
71 }
wdenke2211742002-11-02 23:30:20 +000072 return desc;
73}
74
Michal Simekf6555d92013-04-26 08:46:56 +020075/*
76 * fpga_dev_info
wdenke2211742002-11-02 23:30:20 +000077 * generic multiplexing code
78 */
Michal Simekf6555d92013-04-26 08:46:56 +020079static int fpga_dev_info(int devnum)
wdenke2211742002-11-02 23:30:20 +000080{
Michal Simekf6555d92013-04-26 08:46:56 +020081 int ret_val = FPGA_FAIL; /* assume failure */
82 const fpga_desc * const desc = fpga_get_desc(devnum);
wdenke2211742002-11-02 23:30:20 +000083
Michal Simekf6555d92013-04-26 08:46:56 +020084 if (desc) {
85 debug("%s: Device Descriptor @ 0x%p\n",
86 __func__, desc->devdesc);
wdenke2211742002-11-02 23:30:20 +000087
Michal Simekf6555d92013-04-26 08:46:56 +020088 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +000089 case fpga_xilinx:
Matthias Fuchs01335022007-12-27 17:12:34 +010090#if defined(CONFIG_FPGA_XILINX)
Michal Simekf6555d92013-04-26 08:46:56 +020091 printf("Xilinx Device\nDescriptor @ 0x%p\n", desc);
92 ret_val = xilinx_info(desc->devdesc);
wdenke2211742002-11-02 23:30:20 +000093#else
Michal Simekf6555d92013-04-26 08:46:56 +020094 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +000095#endif
96 break;
97 case fpga_altera:
Matthias Fuchs01335022007-12-27 17:12:34 +010098#if defined(CONFIG_FPGA_ALTERA)
Michal Simekf6555d92013-04-26 08:46:56 +020099 printf("Altera Device\nDescriptor @ 0x%p\n", desc);
100 ret_val = altera_info(desc->devdesc);
wdenke2211742002-11-02 23:30:20 +0000101#else
Michal Simekf6555d92013-04-26 08:46:56 +0200102 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000103#endif
104 break;
Stefano Babic3b8ac462010-06-29 11:47:48 +0200105 case fpga_lattice:
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200106#if defined(CONFIG_FPGA_LATTICE)
Stefano Babic3b8ac462010-06-29 11:47:48 +0200107 printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
108 ret_val = lattice_info(desc->devdesc);
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200109#else
Michal Simekf6555d92013-04-26 08:46:56 +0200110 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200111#endif
Stefano Babic3b8ac462010-06-29 11:47:48 +0200112 break;
wdenke2211742002-11-02 23:30:20 +0000113 default:
Michal Simekf6555d92013-04-26 08:46:56 +0200114 printf("%s: Invalid or unsupported device type %d\n",
115 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000116 }
117 } else {
Michal Simekf6555d92013-04-26 08:46:56 +0200118 printf("%s: Invalid device number %d\n", __func__, devnum);
wdenke2211742002-11-02 23:30:20 +0000119 }
120
121 return ret_val;
122}
123
Michal Simekf6555d92013-04-26 08:46:56 +0200124/*
Michal Simek905bca62016-05-17 14:32:00 +0200125 * fpga_init is usually called from misc_init_r() and MUST be called
wdenke2211742002-11-02 23:30:20 +0000126 * before any of the other fpga functions are used.
127 */
Peter Tyser6385b282009-09-21 11:20:32 -0500128void fpga_init(void)
wdenke2211742002-11-02 23:30:20 +0000129{
wdenke2211742002-11-02 23:30:20 +0000130 next_desc = 0;
Michal Simekf6555d92013-04-26 08:46:56 +0200131 memset(desc_table, 0, sizeof(desc_table));
wdenke2211742002-11-02 23:30:20 +0000132
Michal Simekee976c12013-04-26 09:38:26 +0200133 debug("%s\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000134}
135
Michal Simekf6555d92013-04-26 08:46:56 +0200136/*
137 * fpga_count
wdenke2211742002-11-02 23:30:20 +0000138 * Basic interface function to get the current number of devices available.
139 */
Michal Simekf6555d92013-04-26 08:46:56 +0200140int fpga_count(void)
wdenke2211742002-11-02 23:30:20 +0000141{
142 return next_desc;
143}
144
Michal Simekf6555d92013-04-26 08:46:56 +0200145/*
146 * fpga_add
Peter Tyser6385b282009-09-21 11:20:32 -0500147 * Add the device descriptor to the device table.
wdenke2211742002-11-02 23:30:20 +0000148 */
Michal Simekf6555d92013-04-26 08:46:56 +0200149int fpga_add(fpga_type devtype, void *desc)
wdenke2211742002-11-02 23:30:20 +0000150{
151 int devnum = FPGA_INVALID_DEVICE;
152
Michal Simekcda1e3f2018-01-26 13:17:04 +0100153 if (!desc) {
154 printf("%s: NULL device descriptor\n", __func__);
155 return devnum;
156 }
157
Michal Simekf6555d92013-04-26 08:46:56 +0200158 if (next_desc < 0) {
159 printf("%s: FPGA support not initialized!\n", __func__);
160 } else if ((devtype > fpga_min_type) && (devtype < fpga_undefined)) {
Michal Simekcda1e3f2018-01-26 13:17:04 +0100161 if (next_desc < CONFIG_MAX_FPGA_DEVICES) {
162 devnum = next_desc;
163 desc_table[next_desc].devtype = devtype;
164 desc_table[next_desc++].devdesc = desc;
wdenke2211742002-11-02 23:30:20 +0000165 } else {
Michal Simekcda1e3f2018-01-26 13:17:04 +0100166 printf("%s: Exceeded Max FPGA device count\n",
167 __func__);
wdenke2211742002-11-02 23:30:20 +0000168 }
169 } else {
Michal Simekf6555d92013-04-26 08:46:56 +0200170 printf("%s: Unsupported FPGA type %d\n", __func__, devtype);
wdenke2211742002-11-02 23:30:20 +0000171 }
172
173 return devnum;
174}
175
176/*
Goldschmidt Simon8b93a922017-11-10 14:17:41 +0000177 * Return 1 if the fpga data is partial.
178 * This is only required for fpga drivers that support bitstream_type.
179 */
180int __weak fpga_is_partial_data(int devnum, size_t img_len)
181{
182 return 0;
183}
184
185/*
Michal Simek52c20642013-04-26 13:12:07 +0200186 * Convert bitstream data and load into the fpga
187 */
Michal Simek7a78bd22014-05-02 14:09:30 +0200188int __weak fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
189 bitstream_type bstype)
Michal Simek52c20642013-04-26 13:12:07 +0200190{
191 printf("Bitstream support not implemented for this FPGA device\n");
192 return FPGA_FAIL;
193}
194
Siva Durga Prasad Paladugu1a897662014-03-14 16:35:37 +0530195#if defined(CONFIG_CMD_FPGA_LOADFS)
196int fpga_fsload(int devnum, const void *buf, size_t size,
197 fpga_fs_info *fpga_fsinfo)
198{
199 int ret_val = FPGA_FAIL; /* assume failure */
200 const fpga_desc *desc = fpga_validate(devnum, buf, size,
201 (char *)__func__);
202
203 if (desc) {
204 switch (desc->devtype) {
205 case fpga_xilinx:
206#if defined(CONFIG_FPGA_XILINX)
207 ret_val = xilinx_loadfs(desc->devdesc, buf, size,
208 fpga_fsinfo);
209#else
210 fpga_no_sup((char *)__func__, "Xilinx devices");
211#endif
212 break;
213 default:
214 printf("%s: Invalid or unsupported device type %d\n",
215 __func__, desc->devtype);
216 }
217 }
218
219 return ret_val;
220}
221#endif
222
Siva Durga Prasad Paladugucedd48e2018-05-31 15:10:22 +0530223#if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
224int fpga_loads(int devnum, const void *buf, size_t size,
225 struct fpga_secure_info *fpga_sec_info)
226{
227 int ret_val = FPGA_FAIL;
228
229 const fpga_desc *desc = fpga_validate(devnum, buf, size,
230 (char *)__func__);
231
232 if (desc) {
233 switch (desc->devtype) {
234 case fpga_xilinx:
235#if defined(CONFIG_FPGA_XILINX)
236 ret_val = xilinx_loads(desc->devdesc, buf, size,
237 fpga_sec_info);
238#else
239 fpga_no_sup((char *)__func__, "Xilinx devices");
240#endif
241 break;
242 default:
243 printf("%s: Invalid or unsupported device type %d\n",
244 __func__, desc->devtype);
245 }
246 }
247
248 return ret_val;
249}
250#endif
251
Michal Simek52c20642013-04-26 13:12:07 +0200252/*
Michal Simekf6555d92013-04-26 08:46:56 +0200253 * Generic multiplexing code
wdenke2211742002-11-02 23:30:20 +0000254 */
Michal Simek7a78bd22014-05-02 14:09:30 +0200255int fpga_load(int devnum, const void *buf, size_t bsize, bitstream_type bstype)
wdenke2211742002-11-02 23:30:20 +0000256{
257 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simekf6555d92013-04-26 08:46:56 +0200258 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
259 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000260
Michal Simekf6555d92013-04-26 08:46:56 +0200261 if (desc) {
262 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000263 case fpga_xilinx:
Matthias Fuchs01335022007-12-27 17:12:34 +0100264#if defined(CONFIG_FPGA_XILINX)
Michal Simek7a78bd22014-05-02 14:09:30 +0200265 ret_val = xilinx_load(desc->devdesc, buf, bsize,
266 bstype);
wdenke2211742002-11-02 23:30:20 +0000267#else
Michal Simekf6555d92013-04-26 08:46:56 +0200268 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000269#endif
270 break;
271 case fpga_altera:
Matthias Fuchs01335022007-12-27 17:12:34 +0100272#if defined(CONFIG_FPGA_ALTERA)
Michal Simekf6555d92013-04-26 08:46:56 +0200273 ret_val = altera_load(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000274#else
Michal Simekf6555d92013-04-26 08:46:56 +0200275 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000276#endif
277 break;
Stefano Babic3b8ac462010-06-29 11:47:48 +0200278 case fpga_lattice:
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200279#if defined(CONFIG_FPGA_LATTICE)
Stefano Babic3b8ac462010-06-29 11:47:48 +0200280 ret_val = lattice_load(desc->devdesc, buf, bsize);
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200281#else
Michal Simekf6555d92013-04-26 08:46:56 +0200282 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200283#endif
Stefano Babic3b8ac462010-06-29 11:47:48 +0200284 break;
wdenke2211742002-11-02 23:30:20 +0000285 default:
Michal Simekf6555d92013-04-26 08:46:56 +0200286 printf("%s: Invalid or unsupported device type %d\n",
287 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000288 }
289 }
290
291 return ret_val;
292}
293
Michal Simekf6555d92013-04-26 08:46:56 +0200294/*
295 * fpga_dump
wdenke2211742002-11-02 23:30:20 +0000296 * generic multiplexing code
297 */
Wolfgang Denke6a857d2011-07-30 13:33:49 +0000298int fpga_dump(int devnum, const void *buf, size_t bsize)
wdenke2211742002-11-02 23:30:20 +0000299{
300 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simekf6555d92013-04-26 08:46:56 +0200301 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
302 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000303
Michal Simekf6555d92013-04-26 08:46:56 +0200304 if (desc) {
305 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000306 case fpga_xilinx:
Matthias Fuchs01335022007-12-27 17:12:34 +0100307#if defined(CONFIG_FPGA_XILINX)
Michal Simekf6555d92013-04-26 08:46:56 +0200308 ret_val = xilinx_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000309#else
Michal Simekf6555d92013-04-26 08:46:56 +0200310 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000311#endif
312 break;
313 case fpga_altera:
Matthias Fuchs01335022007-12-27 17:12:34 +0100314#if defined(CONFIG_FPGA_ALTERA)
Michal Simekf6555d92013-04-26 08:46:56 +0200315 ret_val = altera_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000316#else
Michal Simekf6555d92013-04-26 08:46:56 +0200317 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000318#endif
319 break;
Stefano Babic3b8ac462010-06-29 11:47:48 +0200320 case fpga_lattice:
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200321#if defined(CONFIG_FPGA_LATTICE)
Stefano Babic3b8ac462010-06-29 11:47:48 +0200322 ret_val = lattice_dump(desc->devdesc, buf, bsize);
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200323#else
Michal Simekf6555d92013-04-26 08:46:56 +0200324 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200325#endif
Stefano Babic3b8ac462010-06-29 11:47:48 +0200326 break;
wdenke2211742002-11-02 23:30:20 +0000327 default:
Michal Simekf6555d92013-04-26 08:46:56 +0200328 printf("%s: Invalid or unsupported device type %d\n",
329 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000330 }
331 }
332
333 return ret_val;
334}
335
Michal Simekf6555d92013-04-26 08:46:56 +0200336/*
337 * fpga_info
wdenke2211742002-11-02 23:30:20 +0000338 * front end to fpga_dev_info. If devnum is invalid, report on all
339 * available devices.
340 */
Michal Simekf6555d92013-04-26 08:46:56 +0200341int fpga_info(int devnum)
wdenke2211742002-11-02 23:30:20 +0000342{
Michal Simekf6555d92013-04-26 08:46:56 +0200343 if (devnum == FPGA_INVALID_DEVICE) {
344 if (next_desc > 0) {
wdenke2211742002-11-02 23:30:20 +0000345 int dev;
346
Michal Simekf6555d92013-04-26 08:46:56 +0200347 for (dev = 0; dev < next_desc; dev++)
348 fpga_dev_info(dev);
349
wdenke2211742002-11-02 23:30:20 +0000350 return FPGA_SUCCESS;
351 } else {
Michal Simekf6555d92013-04-26 08:46:56 +0200352 printf("%s: No FPGA devices available.\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000353 return FPGA_FAIL;
354 }
355 }
wdenke2211742002-11-02 23:30:20 +0000356
Michal Simekf6555d92013-04-26 08:46:56 +0200357 return fpga_dev_info(devnum);
358}