blob: 0917871d49b8c6319d867a53da52136c6f70d805 [file] [log] [blame]
Tom Rini10e47792018-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 Simek5125ba12013-04-26 08:46:56 +02007/* Generic FPGA support */
wdenke2211742002-11-02 23:30:20 +00008#include <common.h> /* core U-Boot definitions */
9#include <xilinx.h> /* xilinx specific definitions */
10#include <altera.h> /* altera specific definitions */
Stefano Babicec65c592010-06-29 11:47:48 +020011#include <lattice.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <dm/device_compat.h>
wdenke2211742002-11-02 23:30:20 +000013
wdenke2211742002-11-02 23:30:20 +000014/* Local definitions */
15#ifndef CONFIG_MAX_FPGA_DEVICES
16#define CONFIG_MAX_FPGA_DEVICES 5
17#endif
18
wdenke2211742002-11-02 23:30:20 +000019/* Local static data */
wdenke2211742002-11-02 23:30:20 +000020static int next_desc = FPGA_INVALID_DEVICE;
21static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
22
Michal Simek5125ba12013-04-26 08:46:56 +020023/*
24 * fpga_no_sup
wdenke2211742002-11-02 23:30:20 +000025 * 'no support' message function
26 */
Michal Simek5125ba12013-04-26 08:46:56 +020027static void fpga_no_sup(char *fn, char *msg)
wdenke2211742002-11-02 23:30:20 +000028{
Michal Simek5125ba12013-04-26 08:46:56 +020029 if (fn && msg)
30 printf("%s: No support for %s.\n", fn, msg);
31 else if (msg)
32 printf("No support for %s.\n", msg);
33 else
Robert P. J. Day8d56db92016-07-15 13:44:45 -040034 printf("No FPGA support!\n");
wdenke2211742002-11-02 23:30:20 +000035}
36
37
38/* fpga_get_desc
39 * map a device number to a descriptor
40 */
Michal Simekfbadb762015-01-13 16:09:53 +010041const fpga_desc *const fpga_get_desc(int devnum)
wdenke2211742002-11-02 23:30:20 +000042{
Michal Simek5125ba12013-04-26 08:46:56 +020043 fpga_desc *desc = (fpga_desc *)NULL;
wdenke2211742002-11-02 23:30:20 +000044
Michal Simek5125ba12013-04-26 08:46:56 +020045 if ((devnum >= 0) && (devnum < next_desc)) {
wdenke2211742002-11-02 23:30:20 +000046 desc = &desc_table[devnum];
Michal Simek5125ba12013-04-26 08:46:56 +020047 debug("%s: found fpga descriptor #%d @ 0x%p\n",
48 __func__, devnum, desc);
wdenke2211742002-11-02 23:30:20 +000049 }
50
51 return desc;
52}
53
Michal Simek5125ba12013-04-26 08:46:56 +020054/*
55 * fpga_validate
wdenke2211742002-11-02 23:30:20 +000056 * generic parameter checking code
57 */
Michal Simek6ff890d2013-04-26 15:04:48 +020058const fpga_desc *const fpga_validate(int devnum, const void *buf,
59 size_t bsize, char *fn)
wdenke2211742002-11-02 23:30:20 +000060{
Michal Simek5125ba12013-04-26 08:46:56 +020061 const fpga_desc *desc = fpga_get_desc(devnum);
wdenke2211742002-11-02 23:30:20 +000062
Michal Simek5125ba12013-04-26 08:46:56 +020063 if (!desc)
64 printf("%s: Invalid device number %d\n", fn, devnum);
wdenke2211742002-11-02 23:30:20 +000065
Michal Simek5125ba12013-04-26 08:46:56 +020066 if (!buf) {
67 printf("%s: Null buffer.\n", fn);
wdenke2211742002-11-02 23:30:20 +000068 return (fpga_desc * const)NULL;
69 }
wdenke2211742002-11-02 23:30:20 +000070 return desc;
71}
72
Michal Simek5125ba12013-04-26 08:46:56 +020073/*
74 * fpga_dev_info
wdenke2211742002-11-02 23:30:20 +000075 * generic multiplexing code
76 */
Michal Simek5125ba12013-04-26 08:46:56 +020077static int fpga_dev_info(int devnum)
wdenke2211742002-11-02 23:30:20 +000078{
Michal Simek5125ba12013-04-26 08:46:56 +020079 int ret_val = FPGA_FAIL; /* assume failure */
80 const fpga_desc * const desc = fpga_get_desc(devnum);
wdenke2211742002-11-02 23:30:20 +000081
Michal Simek5125ba12013-04-26 08:46:56 +020082 if (desc) {
83 debug("%s: Device Descriptor @ 0x%p\n",
84 __func__, desc->devdesc);
wdenke2211742002-11-02 23:30:20 +000085
Michal Simek5125ba12013-04-26 08:46:56 +020086 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +000087 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +010088#if defined(CONFIG_FPGA_XILINX)
Michal Simek5125ba12013-04-26 08:46:56 +020089 printf("Xilinx Device\nDescriptor @ 0x%p\n", desc);
90 ret_val = xilinx_info(desc->devdesc);
wdenke2211742002-11-02 23:30:20 +000091#else
Michal Simek5125ba12013-04-26 08:46:56 +020092 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +000093#endif
94 break;
95 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +010096#if defined(CONFIG_FPGA_ALTERA)
Michal Simek5125ba12013-04-26 08:46:56 +020097 printf("Altera Device\nDescriptor @ 0x%p\n", desc);
98 ret_val = altera_info(desc->devdesc);
wdenke2211742002-11-02 23:30:20 +000099#else
Michal Simek5125ba12013-04-26 08:46:56 +0200100 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000101#endif
102 break;
Stefano Babicec65c592010-06-29 11:47:48 +0200103 case fpga_lattice:
Wolfgang Denkace143a2010-10-18 21:32:14 +0200104#if defined(CONFIG_FPGA_LATTICE)
Stefano Babicec65c592010-06-29 11:47:48 +0200105 printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
106 ret_val = lattice_info(desc->devdesc);
Wolfgang Denkace143a2010-10-18 21:32:14 +0200107#else
Michal Simek5125ba12013-04-26 08:46:56 +0200108 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denkace143a2010-10-18 21:32:14 +0200109#endif
Stefano Babicec65c592010-06-29 11:47:48 +0200110 break;
wdenke2211742002-11-02 23:30:20 +0000111 default:
Michal Simek5125ba12013-04-26 08:46:56 +0200112 printf("%s: Invalid or unsupported device type %d\n",
113 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000114 }
115 } else {
Michal Simek5125ba12013-04-26 08:46:56 +0200116 printf("%s: Invalid device number %d\n", __func__, devnum);
wdenke2211742002-11-02 23:30:20 +0000117 }
118
119 return ret_val;
120}
121
Michal Simek5125ba12013-04-26 08:46:56 +0200122/*
Michal Simekb563a6b2016-05-17 14:32:00 +0200123 * fpga_init is usually called from misc_init_r() and MUST be called
wdenke2211742002-11-02 23:30:20 +0000124 * before any of the other fpga functions are used.
125 */
Peter Tysercf8582c2009-09-21 11:20:32 -0500126void fpga_init(void)
wdenke2211742002-11-02 23:30:20 +0000127{
wdenke2211742002-11-02 23:30:20 +0000128 next_desc = 0;
Michal Simek5125ba12013-04-26 08:46:56 +0200129 memset(desc_table, 0, sizeof(desc_table));
wdenke2211742002-11-02 23:30:20 +0000130
Michal Simekaefbf8f2013-04-26 09:38:26 +0200131 debug("%s\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000132}
133
Michal Simek5125ba12013-04-26 08:46:56 +0200134/*
135 * fpga_count
wdenke2211742002-11-02 23:30:20 +0000136 * Basic interface function to get the current number of devices available.
137 */
Michal Simek5125ba12013-04-26 08:46:56 +0200138int fpga_count(void)
wdenke2211742002-11-02 23:30:20 +0000139{
140 return next_desc;
141}
142
Michal Simek5125ba12013-04-26 08:46:56 +0200143/*
144 * fpga_add
Peter Tysercf8582c2009-09-21 11:20:32 -0500145 * Add the device descriptor to the device table.
wdenke2211742002-11-02 23:30:20 +0000146 */
Michal Simek5125ba12013-04-26 08:46:56 +0200147int fpga_add(fpga_type devtype, void *desc)
wdenke2211742002-11-02 23:30:20 +0000148{
149 int devnum = FPGA_INVALID_DEVICE;
150
Michal Simek3529c112018-01-26 13:17:04 +0100151 if (!desc) {
152 printf("%s: NULL device descriptor\n", __func__);
153 return devnum;
154 }
155
Michal Simek5125ba12013-04-26 08:46:56 +0200156 if (next_desc < 0) {
157 printf("%s: FPGA support not initialized!\n", __func__);
158 } else if ((devtype > fpga_min_type) && (devtype < fpga_undefined)) {
Michal Simek3529c112018-01-26 13:17:04 +0100159 if (next_desc < CONFIG_MAX_FPGA_DEVICES) {
160 devnum = next_desc;
161 desc_table[next_desc].devtype = devtype;
162 desc_table[next_desc++].devdesc = desc;
wdenke2211742002-11-02 23:30:20 +0000163 } else {
Michal Simek3529c112018-01-26 13:17:04 +0100164 printf("%s: Exceeded Max FPGA device count\n",
165 __func__);
wdenke2211742002-11-02 23:30:20 +0000166 }
167 } else {
Michal Simek5125ba12013-04-26 08:46:56 +0200168 printf("%s: Unsupported FPGA type %d\n", __func__, devtype);
wdenke2211742002-11-02 23:30:20 +0000169 }
170
171 return devnum;
172}
173
174/*
Goldschmidt Simon9179c812017-11-10 14:17:41 +0000175 * Return 1 if the fpga data is partial.
176 * This is only required for fpga drivers that support bitstream_type.
177 */
178int __weak fpga_is_partial_data(int devnum, size_t img_len)
179{
180 return 0;
181}
182
183/*
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200184 * Convert bitstream data and load into the fpga
185 */
Michal Simek14663652014-05-02 14:09:30 +0200186int __weak fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
187 bitstream_type bstype)
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200188{
189 printf("Bitstream support not implemented for this FPGA device\n");
190 return FPGA_FAIL;
191}
192
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530193#if defined(CONFIG_CMD_FPGA_LOADFS)
194int fpga_fsload(int devnum, const void *buf, size_t size,
195 fpga_fs_info *fpga_fsinfo)
196{
197 int ret_val = FPGA_FAIL; /* assume failure */
198 const fpga_desc *desc = fpga_validate(devnum, buf, size,
199 (char *)__func__);
200
201 if (desc) {
202 switch (desc->devtype) {
203 case fpga_xilinx:
204#if defined(CONFIG_FPGA_XILINX)
205 ret_val = xilinx_loadfs(desc->devdesc, buf, size,
206 fpga_fsinfo);
207#else
208 fpga_no_sup((char *)__func__, "Xilinx devices");
209#endif
210 break;
211 default:
212 printf("%s: Invalid or unsupported device type %d\n",
213 __func__, desc->devtype);
214 }
215 }
216
217 return ret_val;
218}
219#endif
220
Siva Durga Prasad Paladugucce0cb02018-05-31 15:10:22 +0530221#if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
222int fpga_loads(int devnum, const void *buf, size_t size,
223 struct fpga_secure_info *fpga_sec_info)
224{
225 int ret_val = FPGA_FAIL;
226
227 const fpga_desc *desc = fpga_validate(devnum, buf, size,
228 (char *)__func__);
229
230 if (desc) {
231 switch (desc->devtype) {
232 case fpga_xilinx:
233#if defined(CONFIG_FPGA_XILINX)
234 ret_val = xilinx_loads(desc->devdesc, buf, size,
235 fpga_sec_info);
236#else
237 fpga_no_sup((char *)__func__, "Xilinx devices");
238#endif
239 break;
240 default:
241 printf("%s: Invalid or unsupported device type %d\n",
242 __func__, desc->devtype);
243 }
244 }
245
246 return ret_val;
247}
248#endif
249
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200250/*
Michal Simek5125ba12013-04-26 08:46:56 +0200251 * Generic multiplexing code
wdenke2211742002-11-02 23:30:20 +0000252 */
Michal Simek14663652014-05-02 14:09:30 +0200253int fpga_load(int devnum, const void *buf, size_t bsize, bitstream_type bstype)
wdenke2211742002-11-02 23:30:20 +0000254{
255 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simek5125ba12013-04-26 08:46:56 +0200256 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
257 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000258
Michal Simek5125ba12013-04-26 08:46:56 +0200259 if (desc) {
260 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000261 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100262#if defined(CONFIG_FPGA_XILINX)
Michal Simek14663652014-05-02 14:09:30 +0200263 ret_val = xilinx_load(desc->devdesc, buf, bsize,
264 bstype);
wdenke2211742002-11-02 23:30:20 +0000265#else
Michal Simek5125ba12013-04-26 08:46:56 +0200266 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000267#endif
268 break;
269 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100270#if defined(CONFIG_FPGA_ALTERA)
Michal Simek5125ba12013-04-26 08:46:56 +0200271 ret_val = altera_load(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000272#else
Michal Simek5125ba12013-04-26 08:46:56 +0200273 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000274#endif
275 break;
Stefano Babicec65c592010-06-29 11:47:48 +0200276 case fpga_lattice:
Wolfgang Denkace143a2010-10-18 21:32:14 +0200277#if defined(CONFIG_FPGA_LATTICE)
Stefano Babicec65c592010-06-29 11:47:48 +0200278 ret_val = lattice_load(desc->devdesc, buf, bsize);
Wolfgang Denkace143a2010-10-18 21:32:14 +0200279#else
Michal Simek5125ba12013-04-26 08:46:56 +0200280 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denkace143a2010-10-18 21:32:14 +0200281#endif
Stefano Babicec65c592010-06-29 11:47:48 +0200282 break;
wdenke2211742002-11-02 23:30:20 +0000283 default:
Michal Simek5125ba12013-04-26 08:46:56 +0200284 printf("%s: Invalid or unsupported device type %d\n",
285 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000286 }
287 }
288
289 return ret_val;
290}
291
Michal Simek5125ba12013-04-26 08:46:56 +0200292/*
293 * fpga_dump
wdenke2211742002-11-02 23:30:20 +0000294 * generic multiplexing code
295 */
Wolfgang Denk74f9b382011-07-30 13:33:49 +0000296int fpga_dump(int devnum, const void *buf, size_t bsize)
wdenke2211742002-11-02 23:30:20 +0000297{
298 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simek5125ba12013-04-26 08:46:56 +0200299 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
300 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000301
Michal Simek5125ba12013-04-26 08:46:56 +0200302 if (desc) {
303 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000304 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100305#if defined(CONFIG_FPGA_XILINX)
Michal Simek5125ba12013-04-26 08:46:56 +0200306 ret_val = xilinx_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000307#else
Michal Simek5125ba12013-04-26 08:46:56 +0200308 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000309#endif
310 break;
311 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100312#if defined(CONFIG_FPGA_ALTERA)
Michal Simek5125ba12013-04-26 08:46:56 +0200313 ret_val = altera_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000314#else
Michal Simek5125ba12013-04-26 08:46:56 +0200315 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000316#endif
317 break;
Stefano Babicec65c592010-06-29 11:47:48 +0200318 case fpga_lattice:
Wolfgang Denkace143a2010-10-18 21:32:14 +0200319#if defined(CONFIG_FPGA_LATTICE)
Stefano Babicec65c592010-06-29 11:47:48 +0200320 ret_val = lattice_dump(desc->devdesc, buf, bsize);
Wolfgang Denkace143a2010-10-18 21:32:14 +0200321#else
Michal Simek5125ba12013-04-26 08:46:56 +0200322 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denkace143a2010-10-18 21:32:14 +0200323#endif
Stefano Babicec65c592010-06-29 11:47:48 +0200324 break;
wdenke2211742002-11-02 23:30:20 +0000325 default:
Michal Simek5125ba12013-04-26 08:46:56 +0200326 printf("%s: Invalid or unsupported device type %d\n",
327 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000328 }
329 }
330
331 return ret_val;
332}
333
Michal Simek5125ba12013-04-26 08:46:56 +0200334/*
335 * fpga_info
wdenke2211742002-11-02 23:30:20 +0000336 * front end to fpga_dev_info. If devnum is invalid, report on all
337 * available devices.
338 */
Michal Simek5125ba12013-04-26 08:46:56 +0200339int fpga_info(int devnum)
wdenke2211742002-11-02 23:30:20 +0000340{
Michal Simek5125ba12013-04-26 08:46:56 +0200341 if (devnum == FPGA_INVALID_DEVICE) {
342 if (next_desc > 0) {
wdenke2211742002-11-02 23:30:20 +0000343 int dev;
344
Michal Simek5125ba12013-04-26 08:46:56 +0200345 for (dev = 0; dev < next_desc; dev++)
346 fpga_dev_info(dev);
347
wdenke2211742002-11-02 23:30:20 +0000348 return FPGA_SUCCESS;
349 } else {
Michal Simek5125ba12013-04-26 08:46:56 +0200350 printf("%s: No FPGA devices available.\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000351 return FPGA_FAIL;
352 }
353 }
wdenke2211742002-11-02 23:30:20 +0000354
Michal Simek5125ba12013-04-26 08:46:56 +0200355 return fpga_dev_info(devnum);
356}