blob: f70bff6ed177aea8cad8d324687ddaa397ae0277 [file] [log] [blame]
wdenke2211742002-11-02 23:30:20 +00001/*
2 * (C) Copyright 2002
3 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 *
23 */
24
Michal Simekf6555d92013-04-26 08:46:56 +020025/* Generic FPGA support */
wdenke2211742002-11-02 23:30:20 +000026#include <common.h> /* core U-Boot definitions */
27#include <xilinx.h> /* xilinx specific definitions */
28#include <altera.h> /* altera specific definitions */
Stefano Babic3b8ac462010-06-29 11:47:48 +020029#include <lattice.h>
wdenke2211742002-11-02 23:30:20 +000030
wdenke2211742002-11-02 23:30:20 +000031/* Local definitions */
32#ifndef CONFIG_MAX_FPGA_DEVICES
33#define CONFIG_MAX_FPGA_DEVICES 5
34#endif
35
wdenke2211742002-11-02 23:30:20 +000036/* Local static data */
wdenke2211742002-11-02 23:30:20 +000037static int next_desc = FPGA_INVALID_DEVICE;
38static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
39
Michal Simekf6555d92013-04-26 08:46:56 +020040/*
41 * fpga_no_sup
wdenke2211742002-11-02 23:30:20 +000042 * 'no support' message function
43 */
Michal Simekf6555d92013-04-26 08:46:56 +020044static void fpga_no_sup(char *fn, char *msg)
wdenke2211742002-11-02 23:30:20 +000045{
Michal Simekf6555d92013-04-26 08:46:56 +020046 if (fn && msg)
47 printf("%s: No support for %s.\n", fn, msg);
48 else if (msg)
49 printf("No support for %s.\n", msg);
50 else
51 printf("No FPGA suport!\n");
wdenke2211742002-11-02 23:30:20 +000052}
53
54
55/* fpga_get_desc
56 * map a device number to a descriptor
57 */
Michal Simekf6555d92013-04-26 08:46:56 +020058static const fpga_desc *const fpga_get_desc(int devnum)
wdenke2211742002-11-02 23:30:20 +000059{
Michal Simekf6555d92013-04-26 08:46:56 +020060 fpga_desc *desc = (fpga_desc *)NULL;
wdenke2211742002-11-02 23:30:20 +000061
Michal Simekf6555d92013-04-26 08:46:56 +020062 if ((devnum >= 0) && (devnum < next_desc)) {
wdenke2211742002-11-02 23:30:20 +000063 desc = &desc_table[devnum];
Michal Simekf6555d92013-04-26 08:46:56 +020064 debug("%s: found fpga descriptor #%d @ 0x%p\n",
65 __func__, devnum, desc);
wdenke2211742002-11-02 23:30:20 +000066 }
67
68 return desc;
69}
70
Michal Simekf6555d92013-04-26 08:46:56 +020071/*
72 * fpga_validate
wdenke2211742002-11-02 23:30:20 +000073 * generic parameter checking code
74 */
Michal Simek6631db42013-04-26 15:04:48 +020075const fpga_desc *const fpga_validate(int devnum, const void *buf,
76 size_t bsize, char *fn)
wdenke2211742002-11-02 23:30:20 +000077{
Michal Simekf6555d92013-04-26 08:46:56 +020078 const fpga_desc *desc = fpga_get_desc(devnum);
wdenke2211742002-11-02 23:30:20 +000079
Michal Simekf6555d92013-04-26 08:46:56 +020080 if (!desc)
81 printf("%s: Invalid device number %d\n", fn, devnum);
wdenke2211742002-11-02 23:30:20 +000082
Michal Simekf6555d92013-04-26 08:46:56 +020083 if (!buf) {
84 printf("%s: Null buffer.\n", fn);
wdenke2211742002-11-02 23:30:20 +000085 return (fpga_desc * const)NULL;
86 }
wdenke2211742002-11-02 23:30:20 +000087 return desc;
88}
89
Michal Simekf6555d92013-04-26 08:46:56 +020090/*
91 * fpga_dev_info
wdenke2211742002-11-02 23:30:20 +000092 * generic multiplexing code
93 */
Michal Simekf6555d92013-04-26 08:46:56 +020094static int fpga_dev_info(int devnum)
wdenke2211742002-11-02 23:30:20 +000095{
Michal Simekf6555d92013-04-26 08:46:56 +020096 int ret_val = FPGA_FAIL; /* assume failure */
97 const fpga_desc * const desc = fpga_get_desc(devnum);
wdenke2211742002-11-02 23:30:20 +000098
Michal Simekf6555d92013-04-26 08:46:56 +020099 if (desc) {
100 debug("%s: Device Descriptor @ 0x%p\n",
101 __func__, desc->devdesc);
wdenke2211742002-11-02 23:30:20 +0000102
Michal Simekf6555d92013-04-26 08:46:56 +0200103 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000104 case fpga_xilinx:
Matthias Fuchs01335022007-12-27 17:12:34 +0100105#if defined(CONFIG_FPGA_XILINX)
Michal Simekf6555d92013-04-26 08:46:56 +0200106 printf("Xilinx Device\nDescriptor @ 0x%p\n", desc);
107 ret_val = xilinx_info(desc->devdesc);
wdenke2211742002-11-02 23:30:20 +0000108#else
Michal Simekf6555d92013-04-26 08:46:56 +0200109 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000110#endif
111 break;
112 case fpga_altera:
Matthias Fuchs01335022007-12-27 17:12:34 +0100113#if defined(CONFIG_FPGA_ALTERA)
Michal Simekf6555d92013-04-26 08:46:56 +0200114 printf("Altera Device\nDescriptor @ 0x%p\n", desc);
115 ret_val = altera_info(desc->devdesc);
wdenke2211742002-11-02 23:30:20 +0000116#else
Michal Simekf6555d92013-04-26 08:46:56 +0200117 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000118#endif
119 break;
Stefano Babic3b8ac462010-06-29 11:47:48 +0200120 case fpga_lattice:
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200121#if defined(CONFIG_FPGA_LATTICE)
Stefano Babic3b8ac462010-06-29 11:47:48 +0200122 printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
123 ret_val = lattice_info(desc->devdesc);
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200124#else
Michal Simekf6555d92013-04-26 08:46:56 +0200125 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200126#endif
Stefano Babic3b8ac462010-06-29 11:47:48 +0200127 break;
wdenke2211742002-11-02 23:30:20 +0000128 default:
Michal Simekf6555d92013-04-26 08:46:56 +0200129 printf("%s: Invalid or unsupported device type %d\n",
130 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000131 }
132 } else {
Michal Simekf6555d92013-04-26 08:46:56 +0200133 printf("%s: Invalid device number %d\n", __func__, devnum);
wdenke2211742002-11-02 23:30:20 +0000134 }
135
136 return ret_val;
137}
138
Michal Simekf6555d92013-04-26 08:46:56 +0200139/*
140 * fgpa_init is usually called from misc_init_r() and MUST be called
wdenke2211742002-11-02 23:30:20 +0000141 * before any of the other fpga functions are used.
142 */
Peter Tyser6385b282009-09-21 11:20:32 -0500143void fpga_init(void)
wdenke2211742002-11-02 23:30:20 +0000144{
wdenke2211742002-11-02 23:30:20 +0000145 next_desc = 0;
Michal Simekf6555d92013-04-26 08:46:56 +0200146 memset(desc_table, 0, sizeof(desc_table));
wdenke2211742002-11-02 23:30:20 +0000147
Michal Simekee976c12013-04-26 09:38:26 +0200148 debug("%s\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000149}
150
Michal Simekf6555d92013-04-26 08:46:56 +0200151/*
152 * fpga_count
wdenke2211742002-11-02 23:30:20 +0000153 * Basic interface function to get the current number of devices available.
154 */
Michal Simekf6555d92013-04-26 08:46:56 +0200155int fpga_count(void)
wdenke2211742002-11-02 23:30:20 +0000156{
157 return next_desc;
158}
159
Michal Simekf6555d92013-04-26 08:46:56 +0200160/*
161 * fpga_add
Peter Tyser6385b282009-09-21 11:20:32 -0500162 * Add the device descriptor to the device table.
wdenke2211742002-11-02 23:30:20 +0000163 */
Michal Simekf6555d92013-04-26 08:46:56 +0200164int fpga_add(fpga_type devtype, void *desc)
wdenke2211742002-11-02 23:30:20 +0000165{
166 int devnum = FPGA_INVALID_DEVICE;
167
Michal Simekf6555d92013-04-26 08:46:56 +0200168 if (next_desc < 0) {
169 printf("%s: FPGA support not initialized!\n", __func__);
170 } else if ((devtype > fpga_min_type) && (devtype < fpga_undefined)) {
171 if (desc) {
172 if (next_desc < CONFIG_MAX_FPGA_DEVICES) {
Peter Tyser6385b282009-09-21 11:20:32 -0500173 devnum = next_desc;
174 desc_table[next_desc].devtype = devtype;
175 desc_table[next_desc++].devdesc = desc;
wdenke2211742002-11-02 23:30:20 +0000176 } else {
Michal Simekf6555d92013-04-26 08:46:56 +0200177 printf("%s: Exceeded Max FPGA device count\n",
178 __func__);
wdenke2211742002-11-02 23:30:20 +0000179 }
180 } else {
Michal Simekf6555d92013-04-26 08:46:56 +0200181 printf("%s: NULL device descriptor\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000182 }
183 } else {
Michal Simekf6555d92013-04-26 08:46:56 +0200184 printf("%s: Unsupported FPGA type %d\n", __func__, devtype);
wdenke2211742002-11-02 23:30:20 +0000185 }
186
187 return devnum;
188}
189
190/*
Michal Simek52c20642013-04-26 13:12:07 +0200191 * Convert bitstream data and load into the fpga
192 */
Michal Simek23f4bd72013-05-01 19:02:02 +0200193int __weak fpga_loadbitstream(int devnum, char *fpgadata, size_t size)
Michal Simek52c20642013-04-26 13:12:07 +0200194{
195 printf("Bitstream support not implemented for this FPGA device\n");
196 return FPGA_FAIL;
197}
198
199/*
Michal Simekf6555d92013-04-26 08:46:56 +0200200 * Generic multiplexing code
wdenke2211742002-11-02 23:30:20 +0000201 */
Wolfgang Denke6a857d2011-07-30 13:33:49 +0000202int fpga_load(int devnum, const void *buf, size_t bsize)
wdenke2211742002-11-02 23:30:20 +0000203{
204 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simekf6555d92013-04-26 08:46:56 +0200205 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
206 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000207
Michal Simekf6555d92013-04-26 08:46:56 +0200208 if (desc) {
209 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000210 case fpga_xilinx:
Matthias Fuchs01335022007-12-27 17:12:34 +0100211#if defined(CONFIG_FPGA_XILINX)
Michal Simekf6555d92013-04-26 08:46:56 +0200212 ret_val = xilinx_load(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000213#else
Michal Simekf6555d92013-04-26 08:46:56 +0200214 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000215#endif
216 break;
217 case fpga_altera:
Matthias Fuchs01335022007-12-27 17:12:34 +0100218#if defined(CONFIG_FPGA_ALTERA)
Michal Simekf6555d92013-04-26 08:46:56 +0200219 ret_val = altera_load(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000220#else
Michal Simekf6555d92013-04-26 08:46:56 +0200221 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000222#endif
223 break;
Stefano Babic3b8ac462010-06-29 11:47:48 +0200224 case fpga_lattice:
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200225#if defined(CONFIG_FPGA_LATTICE)
Stefano Babic3b8ac462010-06-29 11:47:48 +0200226 ret_val = lattice_load(desc->devdesc, buf, bsize);
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200227#else
Michal Simekf6555d92013-04-26 08:46:56 +0200228 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200229#endif
Stefano Babic3b8ac462010-06-29 11:47:48 +0200230 break;
wdenke2211742002-11-02 23:30:20 +0000231 default:
Michal Simekf6555d92013-04-26 08:46:56 +0200232 printf("%s: Invalid or unsupported device type %d\n",
233 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000234 }
235 }
236
237 return ret_val;
238}
239
Michal Simekf6555d92013-04-26 08:46:56 +0200240/*
241 * fpga_dump
wdenke2211742002-11-02 23:30:20 +0000242 * generic multiplexing code
243 */
Wolfgang Denke6a857d2011-07-30 13:33:49 +0000244int fpga_dump(int devnum, const void *buf, size_t bsize)
wdenke2211742002-11-02 23:30:20 +0000245{
246 int ret_val = FPGA_FAIL; /* assume failure */
Michal Simekf6555d92013-04-26 08:46:56 +0200247 const fpga_desc *desc = fpga_validate(devnum, buf, bsize,
248 (char *)__func__);
wdenke2211742002-11-02 23:30:20 +0000249
Michal Simekf6555d92013-04-26 08:46:56 +0200250 if (desc) {
251 switch (desc->devtype) {
wdenke2211742002-11-02 23:30:20 +0000252 case fpga_xilinx:
Matthias Fuchs01335022007-12-27 17:12:34 +0100253#if defined(CONFIG_FPGA_XILINX)
Michal Simekf6555d92013-04-26 08:46:56 +0200254 ret_val = xilinx_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000255#else
Michal Simekf6555d92013-04-26 08:46:56 +0200256 fpga_no_sup((char *)__func__, "Xilinx devices");
wdenke2211742002-11-02 23:30:20 +0000257#endif
258 break;
259 case fpga_altera:
Matthias Fuchs01335022007-12-27 17:12:34 +0100260#if defined(CONFIG_FPGA_ALTERA)
Michal Simekf6555d92013-04-26 08:46:56 +0200261 ret_val = altera_dump(desc->devdesc, buf, bsize);
wdenke2211742002-11-02 23:30:20 +0000262#else
Michal Simekf6555d92013-04-26 08:46:56 +0200263 fpga_no_sup((char *)__func__, "Altera devices");
wdenke2211742002-11-02 23:30:20 +0000264#endif
265 break;
Stefano Babic3b8ac462010-06-29 11:47:48 +0200266 case fpga_lattice:
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200267#if defined(CONFIG_FPGA_LATTICE)
Stefano Babic3b8ac462010-06-29 11:47:48 +0200268 ret_val = lattice_dump(desc->devdesc, buf, bsize);
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200269#else
Michal Simekf6555d92013-04-26 08:46:56 +0200270 fpga_no_sup((char *)__func__, "Lattice devices");
Wolfgang Denk439f6f72010-10-18 21:32:14 +0200271#endif
Stefano Babic3b8ac462010-06-29 11:47:48 +0200272 break;
wdenke2211742002-11-02 23:30:20 +0000273 default:
Michal Simekf6555d92013-04-26 08:46:56 +0200274 printf("%s: Invalid or unsupported device type %d\n",
275 __func__, desc->devtype);
wdenke2211742002-11-02 23:30:20 +0000276 }
277 }
278
279 return ret_val;
280}
281
Michal Simekf6555d92013-04-26 08:46:56 +0200282/*
283 * fpga_info
wdenke2211742002-11-02 23:30:20 +0000284 * front end to fpga_dev_info. If devnum is invalid, report on all
285 * available devices.
286 */
Michal Simekf6555d92013-04-26 08:46:56 +0200287int fpga_info(int devnum)
wdenke2211742002-11-02 23:30:20 +0000288{
Michal Simekf6555d92013-04-26 08:46:56 +0200289 if (devnum == FPGA_INVALID_DEVICE) {
290 if (next_desc > 0) {
wdenke2211742002-11-02 23:30:20 +0000291 int dev;
292
Michal Simekf6555d92013-04-26 08:46:56 +0200293 for (dev = 0; dev < next_desc; dev++)
294 fpga_dev_info(dev);
295
wdenke2211742002-11-02 23:30:20 +0000296 return FPGA_SUCCESS;
297 } else {
Michal Simekf6555d92013-04-26 08:46:56 +0200298 printf("%s: No FPGA devices available.\n", __func__);
wdenke2211742002-11-02 23:30:20 +0000299 return FPGA_FAIL;
300 }
301 }
wdenke2211742002-11-02 23:30:20 +0000302
Michal Simekf6555d92013-04-26 08:46:56 +0200303 return fpga_dev_info(devnum);
304}