blob: 55bdf9e7cf290e4cd405cdc2ece0c3c59d291177 [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>
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 Simek5125ba12013-04-26 08:46:56 +020022/*
23 * fpga_no_sup
wdenke2211742002-11-02 23:30:20 +000024 * 'no support' message function
25 */
Michal Simek5125ba12013-04-26 08:46:56 +020026static void fpga_no_sup(char *fn, char *msg)
wdenke2211742002-11-02 23:30:20 +000027{
Michal Simek5125ba12013-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. Day8d56db92016-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 Simekfbadb762015-01-13 16:09:53 +010040const fpga_desc *const fpga_get_desc(int devnum)
wdenke2211742002-11-02 23:30:20 +000041{
Michal Simek5125ba12013-04-26 08:46:56 +020042 fpga_desc *desc = (fpga_desc *)NULL;
wdenke2211742002-11-02 23:30:20 +000043
Michal Simek5125ba12013-04-26 08:46:56 +020044 if ((devnum >= 0) && (devnum < next_desc)) {
wdenke2211742002-11-02 23:30:20 +000045 desc = &desc_table[devnum];
Michal Simek5125ba12013-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 Simek5125ba12013-04-26 08:46:56 +020053/*
54 * fpga_validate
wdenke2211742002-11-02 23:30:20 +000055 * generic parameter checking code
56 */
Michal Simek6ff890d2013-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 Simek5125ba12013-04-26 08:46:56 +020060 const fpga_desc *desc = fpga_get_desc(devnum);
wdenke2211742002-11-02 23:30:20 +000061
Michal Simek5125ba12013-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 Simek5125ba12013-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 Simek5125ba12013-04-26 08:46:56 +020072/*
73 * fpga_dev_info
wdenke2211742002-11-02 23:30:20 +000074 * generic multiplexing code
75 */
Michal Simek5125ba12013-04-26 08:46:56 +020076static int fpga_dev_info(int devnum)
wdenke2211742002-11-02 23:30:20 +000077{
Michal Simek5125ba12013-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 Simek5125ba12013-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 Simek5125ba12013-04-26 08:46:56 +020085 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +000086 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +010087#if defined(CONFIG_FPGA_XILINX)
Michal Simek5125ba12013-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 Simek5125ba12013-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 Fuchsa4400872007-12-27 17:12:34 +010095#if defined(CONFIG_FPGA_ALTERA)
Michal Simek5125ba12013-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 Simek5125ba12013-04-26 08:46:56 +020099 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000100#endif
101 break;
Stefano Babicec65c592010-06-29 11:47:48 +0200102 case fpga_lattice:
Wolfgang Denkace143a2010-10-18 21:32:14 +0200103#if defined(CONFIG_FPGA_LATTICE)
Stefano Babicec65c592010-06-29 11:47:48 +0200104 printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
105 ret_val = lattice_info(desc->devdesc);
Wolfgang Denkace143a2010-10-18 21:32:14 +0200106#else
Michal Simek5125ba12013-04-26 08:46:56 +0200107 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denkace143a2010-10-18 21:32:14 +0200108#endif
Stefano Babicec65c592010-06-29 11:47:48 +0200109 break;
wdenke2211742002-11-02 23:30:20 +0000110 default:
Michal Simek5125ba12013-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 Simek5125ba12013-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 Simek5125ba12013-04-26 08:46:56 +0200121/*
Michal Simekb563a6b2016-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 Tysercf8582c2009-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 Simek5125ba12013-04-26 08:46:56 +0200128 memset(desc_table, 0, sizeof(desc_table));
wdenke2211742002-11-02 23:30:20 +0000129
Michal Simekaefbf8f2013-04-26 09:38:26 +0200130 debug("%s\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000131}
132
Michal Simek5125ba12013-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 Simek5125ba12013-04-26 08:46:56 +0200137int fpga_count(void)
wdenke2211742002-11-02 23:30:20 +0000138{
139 return next_desc;
140}
141
Michal Simek5125ba12013-04-26 08:46:56 +0200142/*
143 * fpga_add
Peter Tysercf8582c2009-09-21 11:20:32 -0500144 * Add the device descriptor to the device table.
wdenke2211742002-11-02 23:30:20 +0000145 */
Michal Simek5125ba12013-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 Simek3529c112018-01-26 13:17:04 +0100150 if (!desc) {
151 printf("%s: NULL device descriptor\n", __func__);
152 return devnum;
153 }
154
Michal Simek5125ba12013-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 Simek3529c112018-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 Simek3529c112018-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 Simek5125ba12013-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 Simon9179c812017-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 Simekcd5a1cf2013-04-26 13:12:07 +0200183 * Convert bitstream data and load into the fpga
184 */
Michal Simek14663652014-05-02 14:09:30 +0200185int __weak fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
186 bitstream_type bstype)
Michal Simekcd5a1cf2013-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 Paladugu9112b4c2014-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
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200220/*
Michal Simek5125ba12013-04-26 08:46:56 +0200221 * Generic multiplexing code
wdenke2211742002-11-02 23:30:20 +0000222 */
Michal Simek14663652014-05-02 14:09:30 +0200223int fpga_load(int devnum, const void *buf, size_t bsize, bitstream_type bstype)
wdenke2211742002-11-02 23:30:20 +0000224{
225 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simek5125ba12013-04-26 08:46:56 +0200226 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
227 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000228
Michal Simek5125ba12013-04-26 08:46:56 +0200229 if (desc) {
230 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000231 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100232#if defined(CONFIG_FPGA_XILINX)
Michal Simek14663652014-05-02 14:09:30 +0200233 ret_val = xilinx_load(desc->devdesc, buf, bsize,
234 bstype);
wdenke2211742002-11-02 23:30:20 +0000235#else
Michal Simek5125ba12013-04-26 08:46:56 +0200236 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000237#endif
238 break;
239 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100240#if defined(CONFIG_FPGA_ALTERA)
Michal Simek5125ba12013-04-26 08:46:56 +0200241 ret_val = altera_load(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000242#else
Michal Simek5125ba12013-04-26 08:46:56 +0200243 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000244#endif
245 break;
Stefano Babicec65c592010-06-29 11:47:48 +0200246 case fpga_lattice:
Wolfgang Denkace143a2010-10-18 21:32:14 +0200247#if defined(CONFIG_FPGA_LATTICE)
Stefano Babicec65c592010-06-29 11:47:48 +0200248 ret_val = lattice_load(desc->devdesc, buf, bsize);
Wolfgang Denkace143a2010-10-18 21:32:14 +0200249#else
Michal Simek5125ba12013-04-26 08:46:56 +0200250 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denkace143a2010-10-18 21:32:14 +0200251#endif
Stefano Babicec65c592010-06-29 11:47:48 +0200252 break;
wdenke2211742002-11-02 23:30:20 +0000253 default:
Michal Simek5125ba12013-04-26 08:46:56 +0200254 printf("%s: Invalid or unsupported device type %d\n",
255 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000256 }
257 }
258
259 return ret_val;
260}
261
Michal Simek5125ba12013-04-26 08:46:56 +0200262/*
263 * fpga_dump
wdenke2211742002-11-02 23:30:20 +0000264 * generic multiplexing code
265 */
Wolfgang Denk74f9b382011-07-30 13:33:49 +0000266int fpga_dump(int devnum, const void *buf, size_t bsize)
wdenke2211742002-11-02 23:30:20 +0000267{
268 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simek5125ba12013-04-26 08:46:56 +0200269 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
270 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000271
Michal Simek5125ba12013-04-26 08:46:56 +0200272 if (desc) {
273 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000274 case fpga_xilinx:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100275#if defined(CONFIG_FPGA_XILINX)
Michal Simek5125ba12013-04-26 08:46:56 +0200276 ret_val = xilinx_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000277#else
Michal Simek5125ba12013-04-26 08:46:56 +0200278 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000279#endif
280 break;
281 case fpga_altera:
Matthias Fuchsa4400872007-12-27 17:12:34 +0100282#if defined(CONFIG_FPGA_ALTERA)
Michal Simek5125ba12013-04-26 08:46:56 +0200283 ret_val = altera_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000284#else
Michal Simek5125ba12013-04-26 08:46:56 +0200285 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000286#endif
287 break;
Stefano Babicec65c592010-06-29 11:47:48 +0200288 case fpga_lattice:
Wolfgang Denkace143a2010-10-18 21:32:14 +0200289#if defined(CONFIG_FPGA_LATTICE)
Stefano Babicec65c592010-06-29 11:47:48 +0200290 ret_val = lattice_dump(desc->devdesc, buf, bsize);
Wolfgang Denkace143a2010-10-18 21:32:14 +0200291#else
Michal Simek5125ba12013-04-26 08:46:56 +0200292 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denkace143a2010-10-18 21:32:14 +0200293#endif
Stefano Babicec65c592010-06-29 11:47:48 +0200294 break;
wdenke2211742002-11-02 23:30:20 +0000295 default:
Michal Simek5125ba12013-04-26 08:46:56 +0200296 printf("%s: Invalid or unsupported device type %d\n",
297 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000298 }
299 }
300
301 return ret_val;
302}
303
Michal Simek5125ba12013-04-26 08:46:56 +0200304/*
305 * fpga_info
wdenke2211742002-11-02 23:30:20 +0000306 * front end to fpga_dev_info. If devnum is invalid, report on all
307 * available devices.
308 */
Michal Simek5125ba12013-04-26 08:46:56 +0200309int fpga_info(int devnum)
wdenke2211742002-11-02 23:30:20 +0000310{
Michal Simek5125ba12013-04-26 08:46:56 +0200311 if (devnum == FPGA_INVALID_DEVICE) {
312 if (next_desc > 0) {
wdenke2211742002-11-02 23:30:20 +0000313 int dev;
314
Michal Simek5125ba12013-04-26 08:46:56 +0200315 for (dev = 0; dev < next_desc; dev++)
316 fpga_dev_info(dev);
317
wdenke2211742002-11-02 23:30:20 +0000318 return FPGA_SUCCESS;
319 } else {
Michal Simek5125ba12013-04-26 08:46:56 +0200320 printf("%s: No FPGA devices available.\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000321 return FPGA_FAIL;
322 }
323 }
wdenke2211742002-11-02 23:30:20 +0000324
Michal Simek5125ba12013-04-26 08:46:56 +0200325 return fpga_dev_info(devnum);
326}