blob: 7e8bd7eae881d23c641eb1869b099db19c82f652 [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 */
9#include <xilinx.h> /* xilinx specific definitions */
10#include <altera.h> /* altera specific definitions */
Stefano Babic3b8ac462010-06-29 11:47:48 +020011#include <lattice.h>
wdenke2211742002-11-02 23:30:20 +000012
wdenke2211742002-11-02 23:30:20 +000013/* Local definitions */
14#ifndef CONFIG_MAX_FPGA_DEVICES
15#define CONFIG_MAX_FPGA_DEVICES 5
16#endif
17
wdenke2211742002-11-02 23:30:20 +000018/* Local static data */
wdenke2211742002-11-02 23:30:20 +000019static int next_desc = FPGA_INVALID_DEVICE;
20static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
21
Michal Simekf6555d92013-04-26 08:46:56 +020022/*
23 * fpga_no_sup
wdenke2211742002-11-02 23:30:20 +000024 * 'no support' message function
25 */
Michal Simekf6555d92013-04-26 08:46:56 +020026static void fpga_no_sup(char *fn, char *msg)
wdenke2211742002-11-02 23:30:20 +000027{
Michal Simekf6555d92013-04-26 08:46:56 +020028 if (fn && msg)
29 printf("%s: No support for %s.\n", fn, msg);
30 else if (msg)
31 printf("No support for %s.\n", msg);
32 else
Robert P. J. Day62a3b7d2016-07-15 13:44:45 -040033 printf("No FPGA support!\n");
wdenke2211742002-11-02 23:30:20 +000034}
35
36
37/* fpga_get_desc
38 * map a device number to a descriptor
39 */
Michal Simekebd322d2015-01-13 16:09:53 +010040const fpga_desc *const fpga_get_desc(int devnum)
wdenke2211742002-11-02 23:30:20 +000041{
Michal Simekf6555d92013-04-26 08:46:56 +020042 fpga_desc *desc = (fpga_desc *)NULL;
wdenke2211742002-11-02 23:30:20 +000043
Michal Simekf6555d92013-04-26 08:46:56 +020044 if ((devnum >= 0) && (devnum < next_desc)) {
wdenke2211742002-11-02 23:30:20 +000045 desc = &desc_table[devnum];
Michal Simekf6555d92013-04-26 08:46:56 +020046 debug("%s: found fpga descriptor #%d @ 0x%p\n",
47 __func__, devnum, desc);
wdenke2211742002-11-02 23:30:20 +000048 }
49
50 return desc;
51}
52
Michal Simekf6555d92013-04-26 08:46:56 +020053/*
54 * fpga_validate
wdenke2211742002-11-02 23:30:20 +000055 * generic parameter checking code
56 */
Michal Simek6631db42013-04-26 15:04:48 +020057const fpga_desc *const fpga_validate(int devnum, const void *buf,
58 size_t bsize, char *fn)
wdenke2211742002-11-02 23:30:20 +000059{
Michal Simekf6555d92013-04-26 08:46:56 +020060 const fpga_desc *desc = fpga_get_desc(devnum);
wdenke2211742002-11-02 23:30:20 +000061
Michal Simekf6555d92013-04-26 08:46:56 +020062 if (!desc)
63 printf("%s: Invalid device number %d\n", fn, devnum);
wdenke2211742002-11-02 23:30:20 +000064
Michal Simekf6555d92013-04-26 08:46:56 +020065 if (!buf) {
66 printf("%s: Null buffer.\n", fn);
wdenke2211742002-11-02 23:30:20 +000067 return (fpga_desc * const)NULL;
68 }
wdenke2211742002-11-02 23:30:20 +000069 return desc;
70}
71
Michal Simekf6555d92013-04-26 08:46:56 +020072/*
73 * fpga_dev_info
wdenke2211742002-11-02 23:30:20 +000074 * generic multiplexing code
75 */
Michal Simekf6555d92013-04-26 08:46:56 +020076static int fpga_dev_info(int devnum)
wdenke2211742002-11-02 23:30:20 +000077{
Michal Simekf6555d92013-04-26 08:46:56 +020078 int ret_val = FPGA_FAIL; /* assume failure */
79 const fpga_desc * const desc = fpga_get_desc(devnum);
wdenke2211742002-11-02 23:30:20 +000080
Michal Simekf6555d92013-04-26 08:46:56 +020081 if (desc) {
82 debug("%s: Device Descriptor @ 0x%p\n",
83 __func__, desc->devdesc);
wdenke2211742002-11-02 23:30:20 +000084
Michal Simekf6555d92013-04-26 08:46:56 +020085 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +000086 case fpga_xilinx:
Matthias Fuchs01335022007-12-27 17:12:34 +010087#if defined(CONFIG_FPGA_XILINX)
Michal Simekf6555d92013-04-26 08:46:56 +020088 printf("Xilinx Device\nDescriptor @ 0x%p\n", desc);
89 ret_val = xilinx_info(desc->devdesc);
wdenke2211742002-11-02 23:30:20 +000090#else
Michal Simekf6555d92013-04-26 08:46:56 +020091 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +000092#endif
93 break;
94 case fpga_altera:
Matthias Fuchs01335022007-12-27 17:12:34 +010095#if defined(CONFIG_FPGA_ALTERA)
Michal Simekf6555d92013-04-26 08:46:56 +020096 printf("Altera Device\nDescriptor @ 0x%p\n", desc);
97 ret_val = altera_info(desc->devdesc);
wdenke2211742002-11-02 23:30:20 +000098#else
Michal Simekf6555d92013-04-26 08:46:56 +020099 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000100#endif
101 break;
Stefano Babic3b8ac462010-06-29 11:47:48 +0200102 case fpga_lattice:
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200103#if defined(CONFIG_FPGA_LATTICE)
Stefano Babic3b8ac462010-06-29 11:47:48 +0200104 printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
105 ret_val = lattice_info(desc->devdesc);
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200106#else
Michal Simekf6555d92013-04-26 08:46:56 +0200107 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200108#endif
Stefano Babic3b8ac462010-06-29 11:47:48 +0200109 break;
wdenke2211742002-11-02 23:30:20 +0000110 default:
Michal Simekf6555d92013-04-26 08:46:56 +0200111 printf("%s: Invalid or unsupported device type %d\n",
112 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000113 }
114 } else {
Michal Simekf6555d92013-04-26 08:46:56 +0200115 printf("%s: Invalid device number %d\n", __func__, devnum);
wdenke2211742002-11-02 23:30:20 +0000116 }
117
118 return ret_val;
119}
120
Michal Simekf6555d92013-04-26 08:46:56 +0200121/*
Michal Simek905bca62016-05-17 14:32:00 +0200122 * fpga_init is usually called from misc_init_r() and MUST be called
wdenke2211742002-11-02 23:30:20 +0000123 * before any of the other fpga functions are used.
124 */
Peter Tyser6385b282009-09-21 11:20:32 -0500125void fpga_init(void)
wdenke2211742002-11-02 23:30:20 +0000126{
wdenke2211742002-11-02 23:30:20 +0000127 next_desc = 0;
Michal Simekf6555d92013-04-26 08:46:56 +0200128 memset(desc_table, 0, sizeof(desc_table));
wdenke2211742002-11-02 23:30:20 +0000129
Michal Simekee976c12013-04-26 09:38:26 +0200130 debug("%s\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000131}
132
Michal Simekf6555d92013-04-26 08:46:56 +0200133/*
134 * fpga_count
wdenke2211742002-11-02 23:30:20 +0000135 * Basic interface function to get the current number of devices available.
136 */
Michal Simekf6555d92013-04-26 08:46:56 +0200137int fpga_count(void)
wdenke2211742002-11-02 23:30:20 +0000138{
139 return next_desc;
140}
141
Michal Simekf6555d92013-04-26 08:46:56 +0200142/*
143 * fpga_add
Peter Tyser6385b282009-09-21 11:20:32 -0500144 * Add the device descriptor to the device table.
wdenke2211742002-11-02 23:30:20 +0000145 */
Michal Simekf6555d92013-04-26 08:46:56 +0200146int fpga_add(fpga_type devtype, void *desc)
wdenke2211742002-11-02 23:30:20 +0000147{
148 int devnum = FPGA_INVALID_DEVICE;
149
Michal Simekcda1e3f2018-01-26 13:17:04 +0100150 if (!desc) {
151 printf("%s: NULL device descriptor\n", __func__);
152 return devnum;
153 }
154
Michal Simekf6555d92013-04-26 08:46:56 +0200155 if (next_desc < 0) {
156 printf("%s: FPGA support not initialized!\n", __func__);
157 } else if ((devtype > fpga_min_type) && (devtype < fpga_undefined)) {
Michal Simekcda1e3f2018-01-26 13:17:04 +0100158 if (next_desc < CONFIG_MAX_FPGA_DEVICES) {
159 devnum = next_desc;
160 desc_table[next_desc].devtype = devtype;
161 desc_table[next_desc++].devdesc = desc;
wdenke2211742002-11-02 23:30:20 +0000162 } else {
Michal Simekcda1e3f2018-01-26 13:17:04 +0100163 printf("%s: Exceeded Max FPGA device count\n",
164 __func__);
wdenke2211742002-11-02 23:30:20 +0000165 }
166 } else {
Michal Simekf6555d92013-04-26 08:46:56 +0200167 printf("%s: Unsupported FPGA type %d\n", __func__, devtype);
wdenke2211742002-11-02 23:30:20 +0000168 }
169
170 return devnum;
171}
172
173/*
Goldschmidt Simon8b93a922017-11-10 14:17:41 +0000174 * Return 1 if the fpga data is partial.
175 * This is only required for fpga drivers that support bitstream_type.
176 */
177int __weak fpga_is_partial_data(int devnum, size_t img_len)
178{
179 return 0;
180}
181
182/*
Michal Simek52c20642013-04-26 13:12:07 +0200183 * Convert bitstream data and load into the fpga
184 */
Michal Simek7a78bd22014-05-02 14:09:30 +0200185int __weak fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
186 bitstream_type bstype)
Michal Simek52c20642013-04-26 13:12:07 +0200187{
188 printf("Bitstream support not implemented for this FPGA device\n");
189 return FPGA_FAIL;
190}
191
Siva Durga Prasad Paladugu1a897662014-03-14 16:35:37 +0530192#if defined(CONFIG_CMD_FPGA_LOADFS)
193int fpga_fsload(int devnum, const void *buf, size_t size,
194 fpga_fs_info *fpga_fsinfo)
195{
196 int ret_val = FPGA_FAIL; /* assume failure */
197 const fpga_desc *desc = fpga_validate(devnum, buf, size,
198 (char *)__func__);
199
200 if (desc) {
201 switch (desc->devtype) {
202 case fpga_xilinx:
203#if defined(CONFIG_FPGA_XILINX)
204 ret_val = xilinx_loadfs(desc->devdesc, buf, size,
205 fpga_fsinfo);
206#else
207 fpga_no_sup((char *)__func__, "Xilinx devices");
208#endif
209 break;
210 default:
211 printf("%s: Invalid or unsupported device type %d\n",
212 __func__, desc->devtype);
213 }
214 }
215
216 return ret_val;
217}
218#endif
219
Siva Durga Prasad Paladugucedd48e2018-05-31 15:10:22 +0530220#if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
221int fpga_loads(int devnum, const void *buf, size_t size,
222 struct fpga_secure_info *fpga_sec_info)
223{
224 int ret_val = FPGA_FAIL;
225
226 const fpga_desc *desc = fpga_validate(devnum, buf, size,
227 (char *)__func__);
228
229 if (desc) {
230 switch (desc->devtype) {
231 case fpga_xilinx:
232#if defined(CONFIG_FPGA_XILINX)
233 ret_val = xilinx_loads(desc->devdesc, buf, size,
234 fpga_sec_info);
235#else
236 fpga_no_sup((char *)__func__, "Xilinx devices");
237#endif
238 break;
239 default:
240 printf("%s: Invalid or unsupported device type %d\n",
241 __func__, desc->devtype);
242 }
243 }
244
245 return ret_val;
246}
247#endif
248
Michal Simek52c20642013-04-26 13:12:07 +0200249/*
Michal Simekf6555d92013-04-26 08:46:56 +0200250 * Generic multiplexing code
wdenke2211742002-11-02 23:30:20 +0000251 */
Michal Simek7a78bd22014-05-02 14:09:30 +0200252int fpga_load(int devnum, const void *buf, size_t bsize, bitstream_type bstype)
wdenke2211742002-11-02 23:30:20 +0000253{
254 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simekf6555d92013-04-26 08:46:56 +0200255 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
256 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000257
Michal Simekf6555d92013-04-26 08:46:56 +0200258 if (desc) {
259 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000260 case fpga_xilinx:
Matthias Fuchs01335022007-12-27 17:12:34 +0100261#if defined(CONFIG_FPGA_XILINX)
Michal Simek7a78bd22014-05-02 14:09:30 +0200262 ret_val = xilinx_load(desc->devdesc, buf, bsize,
263 bstype);
wdenke2211742002-11-02 23:30:20 +0000264#else
Michal Simekf6555d92013-04-26 08:46:56 +0200265 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000266#endif
267 break;
268 case fpga_altera:
Matthias Fuchs01335022007-12-27 17:12:34 +0100269#if defined(CONFIG_FPGA_ALTERA)
Michal Simekf6555d92013-04-26 08:46:56 +0200270 ret_val = altera_load(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000271#else
Michal Simekf6555d92013-04-26 08:46:56 +0200272 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000273#endif
274 break;
Stefano Babic3b8ac462010-06-29 11:47:48 +0200275 case fpga_lattice:
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200276#if defined(CONFIG_FPGA_LATTICE)
Stefano Babic3b8ac462010-06-29 11:47:48 +0200277 ret_val = lattice_load(desc->devdesc, buf, bsize);
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200278#else
Michal Simekf6555d92013-04-26 08:46:56 +0200279 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200280#endif
Stefano Babic3b8ac462010-06-29 11:47:48 +0200281 break;
wdenke2211742002-11-02 23:30:20 +0000282 default:
Michal Simekf6555d92013-04-26 08:46:56 +0200283 printf("%s: Invalid or unsupported device type %d\n",
284 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000285 }
286 }
287
288 return ret_val;
289}
290
Michal Simekf6555d92013-04-26 08:46:56 +0200291/*
292 * fpga_dump
wdenke2211742002-11-02 23:30:20 +0000293 * generic multiplexing code
294 */
Wolfgang Denke6a857d2011-07-30 13:33:49 +0000295int fpga_dump(int devnum, const void *buf, size_t bsize)
wdenke2211742002-11-02 23:30:20 +0000296{
297 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simekf6555d92013-04-26 08:46:56 +0200298 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
299 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000300
Michal Simekf6555d92013-04-26 08:46:56 +0200301 if (desc) {
302 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000303 case fpga_xilinx:
Matthias Fuchs01335022007-12-27 17:12:34 +0100304#if defined(CONFIG_FPGA_XILINX)
Michal Simekf6555d92013-04-26 08:46:56 +0200305 ret_val = xilinx_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000306#else
Michal Simekf6555d92013-04-26 08:46:56 +0200307 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000308#endif
309 break;
310 case fpga_altera:
Matthias Fuchs01335022007-12-27 17:12:34 +0100311#if defined(CONFIG_FPGA_ALTERA)
Michal Simekf6555d92013-04-26 08:46:56 +0200312 ret_val = altera_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000313#else
Michal Simekf6555d92013-04-26 08:46:56 +0200314 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000315#endif
316 break;
Stefano Babic3b8ac462010-06-29 11:47:48 +0200317 case fpga_lattice:
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200318#if defined(CONFIG_FPGA_LATTICE)
Stefano Babic3b8ac462010-06-29 11:47:48 +0200319 ret_val = lattice_dump(desc->devdesc, buf, bsize);
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200320#else
Michal Simekf6555d92013-04-26 08:46:56 +0200321 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200322#endif
Stefano Babic3b8ac462010-06-29 11:47:48 +0200323 break;
wdenke2211742002-11-02 23:30:20 +0000324 default:
Michal Simekf6555d92013-04-26 08:46:56 +0200325 printf("%s: Invalid or unsupported device type %d\n",
326 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000327 }
328 }
329
330 return ret_val;
331}
332
Michal Simekf6555d92013-04-26 08:46:56 +0200333/*
334 * fpga_info
wdenke2211742002-11-02 23:30:20 +0000335 * front end to fpga_dev_info. If devnum is invalid, report on all
336 * available devices.
337 */
Michal Simekf6555d92013-04-26 08:46:56 +0200338int fpga_info(int devnum)
wdenke2211742002-11-02 23:30:20 +0000339{
Michal Simekf6555d92013-04-26 08:46:56 +0200340 if (devnum == FPGA_INVALID_DEVICE) {
341 if (next_desc > 0) {
wdenke2211742002-11-02 23:30:20 +0000342 int dev;
343
Michal Simekf6555d92013-04-26 08:46:56 +0200344 for (dev = 0; dev < next_desc; dev++)
345 fpga_dev_info(dev);
346
wdenke2211742002-11-02 23:30:20 +0000347 return FPGA_SUCCESS;
348 } else {
Michal Simekf6555d92013-04-26 08:46:56 +0200349 printf("%s: No FPGA devices available.\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000350 return FPGA_FAIL;
351 }
352 }
wdenke2211742002-11-02 23:30:20 +0000353
Michal Simekf6555d92013-04-26 08:46:56 +0200354 return fpga_dev_info(devnum);
355}