blob: 7f6b6bc73a1f0d2e3250d869863ef4338de9a9e0 [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 static data */
wdenke2211742002-11-02 23:30:20 +000017static int next_desc = FPGA_INVALID_DEVICE;
18static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
19
Michal Simek5125ba12013-04-26 08:46:56 +020020/*
21 * fpga_no_sup
wdenke2211742002-11-02 23:30:20 +000022 * 'no support' message function
23 */
Michal Simek5125ba12013-04-26 08:46:56 +020024static void fpga_no_sup(char *fn, char *msg)
wdenke2211742002-11-02 23:30:20 +000025{
Michal Simek5125ba12013-04-26 08:46:56 +020026 if (fn && msg)
27 printf("%s: No support for %s.\n", fn, msg);
28 else if (msg)
29 printf("No support for %s.\n", msg);
30 else
Robert P. J. Day8d56db92016-07-15 13:44:45 -040031 printf("No FPGA support!\n");
wdenke2211742002-11-02 23:30:20 +000032}
33
34
35/* fpga_get_desc
36 * map a device number to a descriptor
37 */
Michal Simekfbadb762015-01-13 16:09:53 +010038const fpga_desc *const fpga_get_desc(int devnum)
wdenke2211742002-11-02 23:30:20 +000039{
Michal Simek5125ba12013-04-26 08:46:56 +020040 fpga_desc *desc = (fpga_desc *)NULL;
wdenke2211742002-11-02 23:30:20 +000041
Michal Simek5125ba12013-04-26 08:46:56 +020042 if ((devnum >= 0) && (devnum < next_desc)) {
wdenke2211742002-11-02 23:30:20 +000043 desc = &desc_table[devnum];
Michal Simek5125ba12013-04-26 08:46:56 +020044 debug("%s: found fpga descriptor #%d @ 0x%p\n",
45 __func__, devnum, desc);
wdenke2211742002-11-02 23:30:20 +000046 }
47
48 return desc;
49}
50
Michal Simek5125ba12013-04-26 08:46:56 +020051/*
52 * fpga_validate
wdenke2211742002-11-02 23:30:20 +000053 * generic parameter checking code
54 */
Michal Simek6ff890d2013-04-26 15:04:48 +020055const fpga_desc *const fpga_validate(int devnum, const void *buf,
56 size_t bsize, char *fn)
wdenke2211742002-11-02 23:30:20 +000057{
Michal Simek5125ba12013-04-26 08:46:56 +020058 const fpga_desc *desc = fpga_get_desc(devnum);
wdenke2211742002-11-02 23:30:20 +000059
Michal Simek5125ba12013-04-26 08:46:56 +020060 if (!desc)
61 printf("%s: Invalid device number %d\n", fn, devnum);
wdenke2211742002-11-02 23:30:20 +000062
Michal Simek5125ba12013-04-26 08:46:56 +020063 if (!buf) {
64 printf("%s: Null buffer.\n", fn);
wdenke2211742002-11-02 23:30:20 +000065 return (fpga_desc * const)NULL;
66 }
wdenke2211742002-11-02 23:30:20 +000067 return desc;
68}
69
Michal Simek5125ba12013-04-26 08:46:56 +020070/*
71 * fpga_dev_info
wdenke2211742002-11-02 23:30:20 +000072 * generic multiplexing code
73 */
Michal Simek5125ba12013-04-26 08:46:56 +020074static int fpga_dev_info(int devnum)
wdenke2211742002-11-02 23:30:20 +000075{
Michal Simek5125ba12013-04-26 08:46:56 +020076 int ret_val = FPGA_FAIL; /* assume failure */
77 const fpga_desc * const desc = fpga_get_desc(devnum);
wdenke2211742002-11-02 23:30:20 +000078
Michal Simek5125ba12013-04-26 08:46:56 +020079 if (desc) {
80 debug("%s: Device Descriptor @ 0x%p\n",
81 __func__, desc->devdesc);
wdenke2211742002-11-02 23:30:20 +000082
Michal Simek5125ba12013-04-26 08:46:56 +020083 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +000084 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +010085#if defined(CONFIG_FPGA_XILINX)
Michal Simek5125ba12013-04-26 08:46:56 +020086 printf("Xilinx Device\nDescriptor @ 0x%p\n", desc);
87 ret_val = xilinx_info(desc->devdesc);
wdenke2211742002-11-02 23:30:20 +000088#else
Michal Simek5125ba12013-04-26 08:46:56 +020089 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +000090#endif
91 break;
92 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +010093#if defined(CONFIG_FPGA_ALTERA)
Michal Simek5125ba12013-04-26 08:46:56 +020094 printf("Altera Device\nDescriptor @ 0x%p\n", desc);
95 ret_val = altera_info(desc->devdesc);
wdenke2211742002-11-02 23:30:20 +000096#else
Michal Simek5125ba12013-04-26 08:46:56 +020097 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +000098#endif
99 break;
Stefano Babicec65c592010-06-29 11:47:48 +0200100 case fpga_lattice:
Wolfgang Denkace143a2010-10-18 21:32:14 +0200101#if defined(CONFIG_FPGA_LATTICE)
Stefano Babicec65c592010-06-29 11:47:48 +0200102 printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
103 ret_val = lattice_info(desc->devdesc);
Wolfgang Denkace143a2010-10-18 21:32:14 +0200104#else
Michal Simek5125ba12013-04-26 08:46:56 +0200105 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denkace143a2010-10-18 21:32:14 +0200106#endif
Stefano Babicec65c592010-06-29 11:47:48 +0200107 break;
wdenke2211742002-11-02 23:30:20 +0000108 default:
Michal Simek5125ba12013-04-26 08:46:56 +0200109 printf("%s: Invalid or unsupported device type %d\n",
110 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000111 }
112 } else {
Michal Simek5125ba12013-04-26 08:46:56 +0200113 printf("%s: Invalid device number %d\n", __func__, devnum);
wdenke2211742002-11-02 23:30:20 +0000114 }
115
116 return ret_val;
117}
118
Michal Simek5125ba12013-04-26 08:46:56 +0200119/*
Michal Simekb563a6b2016-05-17 14:32:00 +0200120 * fpga_init is usually called from misc_init_r() and MUST be called
wdenke2211742002-11-02 23:30:20 +0000121 * before any of the other fpga functions are used.
122 */
Peter Tysercf8582c2009-09-21 11:20:32 -0500123void fpga_init(void)
wdenke2211742002-11-02 23:30:20 +0000124{
wdenke2211742002-11-02 23:30:20 +0000125 next_desc = 0;
Michal Simek5125ba12013-04-26 08:46:56 +0200126 memset(desc_table, 0, sizeof(desc_table));
wdenke2211742002-11-02 23:30:20 +0000127
Michal Simekaefbf8f2013-04-26 09:38:26 +0200128 debug("%s\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000129}
130
Michal Simek5125ba12013-04-26 08:46:56 +0200131/*
132 * fpga_count
wdenke2211742002-11-02 23:30:20 +0000133 * Basic interface function to get the current number of devices available.
134 */
Michal Simek5125ba12013-04-26 08:46:56 +0200135int fpga_count(void)
wdenke2211742002-11-02 23:30:20 +0000136{
137 return next_desc;
138}
139
Michal Simek5125ba12013-04-26 08:46:56 +0200140/*
141 * fpga_add
Peter Tysercf8582c2009-09-21 11:20:32 -0500142 * Add the device descriptor to the device table.
wdenke2211742002-11-02 23:30:20 +0000143 */
Michal Simek5125ba12013-04-26 08:46:56 +0200144int fpga_add(fpga_type devtype, void *desc)
wdenke2211742002-11-02 23:30:20 +0000145{
146 int devnum = FPGA_INVALID_DEVICE;
147
Michal Simek3529c112018-01-26 13:17:04 +0100148 if (!desc) {
149 printf("%s: NULL device descriptor\n", __func__);
150 return devnum;
151 }
152
Michal Simek5125ba12013-04-26 08:46:56 +0200153 if (next_desc < 0) {
154 printf("%s: FPGA support not initialized!\n", __func__);
155 } else if ((devtype > fpga_min_type) && (devtype < fpga_undefined)) {
Michal Simek3529c112018-01-26 13:17:04 +0100156 if (next_desc < CONFIG_MAX_FPGA_DEVICES) {
157 devnum = next_desc;
158 desc_table[next_desc].devtype = devtype;
159 desc_table[next_desc++].devdesc = desc;
wdenke2211742002-11-02 23:30:20 +0000160 } else {
Michal Simek3529c112018-01-26 13:17:04 +0100161 printf("%s: Exceeded Max FPGA device count\n",
162 __func__);
wdenke2211742002-11-02 23:30:20 +0000163 }
164 } else {
Michal Simek5125ba12013-04-26 08:46:56 +0200165 printf("%s: Unsupported FPGA type %d\n", __func__, devtype);
wdenke2211742002-11-02 23:30:20 +0000166 }
167
168 return devnum;
169}
170
171/*
Goldschmidt Simon9179c812017-11-10 14:17:41 +0000172 * Return 1 if the fpga data is partial.
173 * This is only required for fpga drivers that support bitstream_type.
174 */
175int __weak fpga_is_partial_data(int devnum, size_t img_len)
176{
177 return 0;
178}
179
180/*
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200181 * Convert bitstream data and load into the fpga
182 */
Michal Simek14663652014-05-02 14:09:30 +0200183int __weak fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
184 bitstream_type bstype)
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200185{
186 printf("Bitstream support not implemented for this FPGA device\n");
187 return FPGA_FAIL;
188}
189
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530190#if defined(CONFIG_CMD_FPGA_LOADFS)
191int fpga_fsload(int devnum, const void *buf, size_t size,
192 fpga_fs_info *fpga_fsinfo)
193{
194 int ret_val = FPGA_FAIL; /* assume failure */
195 const fpga_desc *desc = fpga_validate(devnum, buf, size,
196 (char *)__func__);
197
198 if (desc) {
199 switch (desc->devtype) {
200 case fpga_xilinx:
201#if defined(CONFIG_FPGA_XILINX)
202 ret_val = xilinx_loadfs(desc->devdesc, buf, size,
203 fpga_fsinfo);
204#else
205 fpga_no_sup((char *)__func__, "Xilinx devices");
206#endif
207 break;
208 default:
209 printf("%s: Invalid or unsupported device type %d\n",
210 __func__, desc->devtype);
211 }
212 }
213
214 return ret_val;
215}
216#endif
217
Oleksandr Suvorovfbe31bb2022-07-22 17:16:02 +0300218#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
Siva Durga Prasad Paladugucce0cb02018-05-31 15:10:22 +0530219int fpga_loads(int devnum, const void *buf, size_t size,
220 struct fpga_secure_info *fpga_sec_info)
221{
222 int ret_val = FPGA_FAIL;
223
224 const fpga_desc *desc = fpga_validate(devnum, buf, size,
225 (char *)__func__);
226
227 if (desc) {
228 switch (desc->devtype) {
229 case fpga_xilinx:
230#if defined(CONFIG_FPGA_XILINX)
231 ret_val = xilinx_loads(desc->devdesc, buf, size,
232 fpga_sec_info);
233#else
234 fpga_no_sup((char *)__func__, "Xilinx devices");
235#endif
236 break;
237 default:
238 printf("%s: Invalid or unsupported device type %d\n",
239 __func__, desc->devtype);
240 }
241 }
242
243 return ret_val;
244}
245#endif
246
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200247/*
Michal Simek5125ba12013-04-26 08:46:56 +0200248 * Generic multiplexing code
wdenke2211742002-11-02 23:30:20 +0000249 */
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300250int fpga_load(int devnum, const void *buf, size_t bsize, bitstream_type bstype,
251 int flags)
wdenke2211742002-11-02 23:30:20 +0000252{
253 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simek5125ba12013-04-26 08:46:56 +0200254 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
255 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000256
Michal Simek5125ba12013-04-26 08:46:56 +0200257 if (desc) {
258 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000259 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100260#if defined(CONFIG_FPGA_XILINX)
Michal Simek14663652014-05-02 14:09:30 +0200261 ret_val = xilinx_load(desc->devdesc, buf, bsize,
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300262 bstype, flags);
wdenke2211742002-11-02 23:30:20 +0000263#else
Michal Simek5125ba12013-04-26 08:46:56 +0200264 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000265#endif
266 break;
267 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100268#if defined(CONFIG_FPGA_ALTERA)
Michal Simek5125ba12013-04-26 08:46:56 +0200269 ret_val = altera_load(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000270#else
Michal Simek5125ba12013-04-26 08:46:56 +0200271 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000272#endif
273 break;
Stefano Babicec65c592010-06-29 11:47:48 +0200274 case fpga_lattice:
Wolfgang Denkace143a2010-10-18 21:32:14 +0200275#if defined(CONFIG_FPGA_LATTICE)
Stefano Babicec65c592010-06-29 11:47:48 +0200276 ret_val = lattice_load(desc->devdesc, buf, bsize);
Wolfgang Denkace143a2010-10-18 21:32:14 +0200277#else
Michal Simek5125ba12013-04-26 08:46:56 +0200278 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denkace143a2010-10-18 21:32:14 +0200279#endif
Stefano Babicec65c592010-06-29 11:47:48 +0200280 break;
wdenke2211742002-11-02 23:30:20 +0000281 default:
Michal Simek5125ba12013-04-26 08:46:56 +0200282 printf("%s: Invalid or unsupported device type %d\n",
283 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000284 }
285 }
286
287 return ret_val;
288}
289
Michal Simek5125ba12013-04-26 08:46:56 +0200290/*
291 * fpga_dump
wdenke2211742002-11-02 23:30:20 +0000292 * generic multiplexing code
293 */
Wolfgang Denk74f9b382011-07-30 13:33:49 +0000294int fpga_dump(int devnum, const void *buf, size_t bsize)
wdenke2211742002-11-02 23:30:20 +0000295{
296 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simek5125ba12013-04-26 08:46:56 +0200297 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
298 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000299
Michal Simek5125ba12013-04-26 08:46:56 +0200300 if (desc) {
301 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000302 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100303#if defined(CONFIG_FPGA_XILINX)
Michal Simek5125ba12013-04-26 08:46:56 +0200304 ret_val = xilinx_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000305#else
Michal Simek5125ba12013-04-26 08:46:56 +0200306 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000307#endif
308 break;
309 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100310#if defined(CONFIG_FPGA_ALTERA)
Michal Simek5125ba12013-04-26 08:46:56 +0200311 ret_val = altera_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000312#else
Michal Simek5125ba12013-04-26 08:46:56 +0200313 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000314#endif
315 break;
Stefano Babicec65c592010-06-29 11:47:48 +0200316 case fpga_lattice:
Wolfgang Denkace143a2010-10-18 21:32:14 +0200317#if defined(CONFIG_FPGA_LATTICE)
Stefano Babicec65c592010-06-29 11:47:48 +0200318 ret_val = lattice_dump(desc->devdesc, buf, bsize);
Wolfgang Denkace143a2010-10-18 21:32:14 +0200319#else
Michal Simek5125ba12013-04-26 08:46:56 +0200320 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denkace143a2010-10-18 21:32:14 +0200321#endif
Stefano Babicec65c592010-06-29 11:47:48 +0200322 break;
wdenke2211742002-11-02 23:30:20 +0000323 default:
Michal Simek5125ba12013-04-26 08:46:56 +0200324 printf("%s: Invalid or unsupported device type %d\n",
325 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000326 }
327 }
328
329 return ret_val;
330}
331
Michal Simek5125ba12013-04-26 08:46:56 +0200332/*
333 * fpga_info
wdenke2211742002-11-02 23:30:20 +0000334 * front end to fpga_dev_info. If devnum is invalid, report on all
335 * available devices.
336 */
Michal Simek5125ba12013-04-26 08:46:56 +0200337int fpga_info(int devnum)
wdenke2211742002-11-02 23:30:20 +0000338{
Michal Simek5125ba12013-04-26 08:46:56 +0200339 if (devnum == FPGA_INVALID_DEVICE) {
340 if (next_desc > 0) {
wdenke2211742002-11-02 23:30:20 +0000341 int dev;
342
Michal Simek5125ba12013-04-26 08:46:56 +0200343 for (dev = 0; dev < next_desc; dev++)
344 fpga_dev_info(dev);
345
wdenke2211742002-11-02 23:30:20 +0000346 return FPGA_SUCCESS;
347 } else {
Michal Simek5125ba12013-04-26 08:46:56 +0200348 printf("%s: No FPGA devices available.\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000349 return FPGA_FAIL;
350 }
351 }
wdenke2211742002-11-02 23:30:20 +0000352
Michal Simek5125ba12013-04-26 08:46:56 +0200353 return fpga_dev_info(devnum);
354}
Oleksandr Suvorova4d95932022-07-22 17:16:08 +0300355
356#if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
357int fpga_compatible2flag(int devnum, const char *compatible)
358{
359 const fpga_desc * const desc = fpga_get_desc(devnum);
360
361 if (!desc)
362 return 0;
363
364 switch (desc->devtype) {
365#if defined(CONFIG_FPGA_XILINX)
366 case fpga_xilinx:
367 {
368 xilinx_desc *xdesc = (xilinx_desc *)desc->devdesc;
369
370 if (xdesc->operations && xdesc->operations->str2flag)
371 return xdesc->operations->str2flag(xdesc, compatible);
372 }
373#endif
374 default:
375 break;
376 }
377
378 return 0;
379}
380#endif