blob: 4db5c0a91e98f5ff76fd433580fef7d8e3b8aefc [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 */
Simon Glass97589732020-05-10 11:40:02 -06009#include <init.h>
Simon Glass0f2af882020-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 Babicec65c592010-06-29 11:47:48 +020013#include <lattice.h>
Simon Glass9bc15642020-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 Simek5125ba12013-04-26 08:46:56 +020025/*
26 * fpga_no_sup
wdenke2211742002-11-02 23:30:20 +000027 * 'no support' message function
28 */
Michal Simek5125ba12013-04-26 08:46:56 +020029static void fpga_no_sup(char *fn, char *msg)
wdenke2211742002-11-02 23:30:20 +000030{
Michal Simek5125ba12013-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. Day8d56db92016-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 Simekfbadb762015-01-13 16:09:53 +010043const fpga_desc *const fpga_get_desc(int devnum)
wdenke2211742002-11-02 23:30:20 +000044{
Michal Simek5125ba12013-04-26 08:46:56 +020045 fpga_desc *desc = (fpga_desc *)NULL;
wdenke2211742002-11-02 23:30:20 +000046
Michal Simek5125ba12013-04-26 08:46:56 +020047 if ((devnum >= 0) && (devnum < next_desc)) {
wdenke2211742002-11-02 23:30:20 +000048 desc = &desc_table[devnum];
Michal Simek5125ba12013-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 Simek5125ba12013-04-26 08:46:56 +020056/*
57 * fpga_validate
wdenke2211742002-11-02 23:30:20 +000058 * generic parameter checking code
59 */
Michal Simek6ff890d2013-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 Simek5125ba12013-04-26 08:46:56 +020063 const fpga_desc *desc = fpga_get_desc(devnum);
wdenke2211742002-11-02 23:30:20 +000064
Michal Simek5125ba12013-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 Simek5125ba12013-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 Simek5125ba12013-04-26 08:46:56 +020075/*
76 * fpga_dev_info
wdenke2211742002-11-02 23:30:20 +000077 * generic multiplexing code
78 */
Michal Simek5125ba12013-04-26 08:46:56 +020079static int fpga_dev_info(int devnum)
wdenke2211742002-11-02 23:30:20 +000080{
Michal Simek5125ba12013-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 Simek5125ba12013-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 Simek5125ba12013-04-26 08:46:56 +020088 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +000089 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +010090#if defined(CONFIG_FPGA_XILINX)
Michal Simek5125ba12013-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 Simek5125ba12013-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 Fuchsa4400872007-12-27 17:12:34 +010098#if defined(CONFIG_FPGA_ALTERA)
Michal Simek5125ba12013-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 Simek5125ba12013-04-26 08:46:56 +0200102 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000103#endif
104 break;
Stefano Babicec65c592010-06-29 11:47:48 +0200105 case fpga_lattice:
Wolfgang Denkace143a2010-10-18 21:32:14 +0200106#if defined(CONFIG_FPGA_LATTICE)
Stefano Babicec65c592010-06-29 11:47:48 +0200107 printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
108 ret_val = lattice_info(desc->devdesc);
Wolfgang Denkace143a2010-10-18 21:32:14 +0200109#else
Michal Simek5125ba12013-04-26 08:46:56 +0200110 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denkace143a2010-10-18 21:32:14 +0200111#endif
Stefano Babicec65c592010-06-29 11:47:48 +0200112 break;
wdenke2211742002-11-02 23:30:20 +0000113 default:
Michal Simek5125ba12013-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 Simek5125ba12013-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 Simek5125ba12013-04-26 08:46:56 +0200124/*
Michal Simekb563a6b2016-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 Tysercf8582c2009-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 Simek5125ba12013-04-26 08:46:56 +0200131 memset(desc_table, 0, sizeof(desc_table));
wdenke2211742002-11-02 23:30:20 +0000132
Michal Simekaefbf8f2013-04-26 09:38:26 +0200133 debug("%s\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000134}
135
Michal Simek5125ba12013-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 Simek5125ba12013-04-26 08:46:56 +0200140int fpga_count(void)
wdenke2211742002-11-02 23:30:20 +0000141{
142 return next_desc;
143}
144
Michal Simek5125ba12013-04-26 08:46:56 +0200145/*
146 * fpga_add
Peter Tysercf8582c2009-09-21 11:20:32 -0500147 * Add the device descriptor to the device table.
wdenke2211742002-11-02 23:30:20 +0000148 */
Michal Simek5125ba12013-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 Simek3529c112018-01-26 13:17:04 +0100153 if (!desc) {
154 printf("%s: NULL device descriptor\n", __func__);
155 return devnum;
156 }
157
Michal Simek5125ba12013-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 Simek3529c112018-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 Simek3529c112018-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 Simek5125ba12013-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 Simon9179c812017-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 Simekcd5a1cf2013-04-26 13:12:07 +0200186 * Convert bitstream data and load into the fpga
187 */
Michal Simek14663652014-05-02 14:09:30 +0200188int __weak fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
189 bitstream_type bstype)
Michal Simekcd5a1cf2013-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 Paladugu9112b4c2014-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
Oleksandr Suvorovfbe31bb2022-07-22 17:16:02 +0300223#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
Siva Durga Prasad Paladugucce0cb02018-05-31 15:10:22 +0530224int 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 Simekcd5a1cf2013-04-26 13:12:07 +0200252/*
Michal Simek5125ba12013-04-26 08:46:56 +0200253 * Generic multiplexing code
wdenke2211742002-11-02 23:30:20 +0000254 */
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300255int fpga_load(int devnum, const void *buf, size_t bsize, bitstream_type bstype,
256 int flags)
wdenke2211742002-11-02 23:30:20 +0000257{
258 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simek5125ba12013-04-26 08:46:56 +0200259 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
260 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000261
Michal Simek5125ba12013-04-26 08:46:56 +0200262 if (desc) {
263 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000264 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100265#if defined(CONFIG_FPGA_XILINX)
Michal Simek14663652014-05-02 14:09:30 +0200266 ret_val = xilinx_load(desc->devdesc, buf, bsize,
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300267 bstype, flags);
wdenke2211742002-11-02 23:30:20 +0000268#else
Michal Simek5125ba12013-04-26 08:46:56 +0200269 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000270#endif
271 break;
272 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100273#if defined(CONFIG_FPGA_ALTERA)
Michal Simek5125ba12013-04-26 08:46:56 +0200274 ret_val = altera_load(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000275#else
Michal Simek5125ba12013-04-26 08:46:56 +0200276 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000277#endif
278 break;
Stefano Babicec65c592010-06-29 11:47:48 +0200279 case fpga_lattice:
Wolfgang Denkace143a2010-10-18 21:32:14 +0200280#if defined(CONFIG_FPGA_LATTICE)
Stefano Babicec65c592010-06-29 11:47:48 +0200281 ret_val = lattice_load(desc->devdesc, buf, bsize);
Wolfgang Denkace143a2010-10-18 21:32:14 +0200282#else
Michal Simek5125ba12013-04-26 08:46:56 +0200283 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denkace143a2010-10-18 21:32:14 +0200284#endif
Stefano Babicec65c592010-06-29 11:47:48 +0200285 break;
wdenke2211742002-11-02 23:30:20 +0000286 default:
Michal Simek5125ba12013-04-26 08:46:56 +0200287 printf("%s: Invalid or unsupported device type %d\n",
288 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000289 }
290 }
291
292 return ret_val;
293}
294
Michal Simek5125ba12013-04-26 08:46:56 +0200295/*
296 * fpga_dump
wdenke2211742002-11-02 23:30:20 +0000297 * generic multiplexing code
298 */
Wolfgang Denk74f9b382011-07-30 13:33:49 +0000299int fpga_dump(int devnum, const void *buf, size_t bsize)
wdenke2211742002-11-02 23:30:20 +0000300{
301 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simek5125ba12013-04-26 08:46:56 +0200302 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
303 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000304
Michal Simek5125ba12013-04-26 08:46:56 +0200305 if (desc) {
306 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000307 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100308#if defined(CONFIG_FPGA_XILINX)
Michal Simek5125ba12013-04-26 08:46:56 +0200309 ret_val = xilinx_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000310#else
Michal Simek5125ba12013-04-26 08:46:56 +0200311 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000312#endif
313 break;
314 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100315#if defined(CONFIG_FPGA_ALTERA)
Michal Simek5125ba12013-04-26 08:46:56 +0200316 ret_val = altera_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000317#else
Michal Simek5125ba12013-04-26 08:46:56 +0200318 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000319#endif
320 break;
Stefano Babicec65c592010-06-29 11:47:48 +0200321 case fpga_lattice:
Wolfgang Denkace143a2010-10-18 21:32:14 +0200322#if defined(CONFIG_FPGA_LATTICE)
Stefano Babicec65c592010-06-29 11:47:48 +0200323 ret_val = lattice_dump(desc->devdesc, buf, bsize);
Wolfgang Denkace143a2010-10-18 21:32:14 +0200324#else
Michal Simek5125ba12013-04-26 08:46:56 +0200325 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denkace143a2010-10-18 21:32:14 +0200326#endif
Stefano Babicec65c592010-06-29 11:47:48 +0200327 break;
wdenke2211742002-11-02 23:30:20 +0000328 default:
Michal Simek5125ba12013-04-26 08:46:56 +0200329 printf("%s: Invalid or unsupported device type %d\n",
330 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000331 }
332 }
333
334 return ret_val;
335}
336
Michal Simek5125ba12013-04-26 08:46:56 +0200337/*
338 * fpga_info
wdenke2211742002-11-02 23:30:20 +0000339 * front end to fpga_dev_info. If devnum is invalid, report on all
340 * available devices.
341 */
Michal Simek5125ba12013-04-26 08:46:56 +0200342int fpga_info(int devnum)
wdenke2211742002-11-02 23:30:20 +0000343{
Michal Simek5125ba12013-04-26 08:46:56 +0200344 if (devnum == FPGA_INVALID_DEVICE) {
345 if (next_desc > 0) {
wdenke2211742002-11-02 23:30:20 +0000346 int dev;
347
Michal Simek5125ba12013-04-26 08:46:56 +0200348 for (dev = 0; dev < next_desc; dev++)
349 fpga_dev_info(dev);
350
wdenke2211742002-11-02 23:30:20 +0000351 return FPGA_SUCCESS;
352 } else {
Michal Simek5125ba12013-04-26 08:46:56 +0200353 printf("%s: No FPGA devices available.\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000354 return FPGA_FAIL;
355 }
356 }
wdenke2211742002-11-02 23:30:20 +0000357
Michal Simek5125ba12013-04-26 08:46:56 +0200358 return fpga_dev_info(devnum);
359}
Oleksandr Suvorova4d95932022-07-22 17:16:08 +0300360
361#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
362int fpga_compatible2flag(int devnum, const char *compatible)
363{
364 const fpga_desc * const desc = fpga_get_desc(devnum);
365
366 if (!desc)
367 return 0;
368
369 switch (desc->devtype) {
370#if defined(CONFIG_FPGA_XILINX)
371 case fpga_xilinx:
372 {
373 xilinx_desc *xdesc = (xilinx_desc *)desc->devdesc;
374
375 if (xdesc->operations && xdesc->operations->str2flag)
376 return xdesc->operations->str2flag(xdesc, compatible);
377 }
378#endif
379 default:
380 break;
381 }
382
383 return 0;
384}
385#endif