blob: 496aa2f6eb750cf066946e315e3dbe7b9ec1de4a [file] [log] [blame]
wdenk5d3207d2002-08-21 22:08:56 +00001/*
Michal Simekd5dae852013-04-22 15:43:02 +02002 * (C) Copyright 2012-2013, Xilinx, Michal Simek
3 *
wdenk5d3207d2002-08-21 22:08:56 +00004 * (C) Copyright 2002
5 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
6 * Keith Outwater, keith_outwater@mvis.com
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 *
26 */
27
28/*
29 * Xilinx FPGA support
30 */
31
32#include <common.h>
33#include <virtex2.h>
34#include <spartan2.h>
Wolfgang Denk875c7892005-09-25 16:44:21 +020035#include <spartan3.h>
Michal Simekd5dae852013-04-22 15:43:02 +020036#include <zynqpl.h>
wdenk5d3207d2002-08-21 22:08:56 +000037
wdenk5d3207d2002-08-21 22:08:56 +000038#if 0
39#define FPGA_DEBUG
40#endif
41
42/* Define FPGA_DEBUG to get debug printf's */
43#ifdef FPGA_DEBUG
44#define PRINTF(fmt,args...) printf (fmt ,##args)
45#else
46#define PRINTF(fmt,args...)
47#endif
48
49/* Local Static Functions */
50static int xilinx_validate (Xilinx_desc * desc, char *fn);
51
52/* ------------------------------------------------------------------------- */
53
Michal Simek23f4bd72013-05-01 19:02:02 +020054int fpga_loadbitstream(int devnum, char *fpgadata, size_t size)
Michal Simek52c20642013-04-26 13:12:07 +020055{
56 unsigned int length;
57 unsigned int swapsize;
58 char buffer[80];
59 unsigned char *dataptr;
60 unsigned int i;
61
62 dataptr = (unsigned char *)fpgadata;
63
64 /* skip the first bytes of the bitsteam, their meaning is unknown */
65 length = (*dataptr << 8) + *(dataptr + 1);
66 dataptr += 2;
67 dataptr += length;
68
69 /* get design name (identifier, length, string) */
70 length = (*dataptr << 8) + *(dataptr + 1);
71 dataptr += 2;
72 if (*dataptr++ != 0x61) {
73 debug("%s: Design name id not recognized in bitstream\n",
74 __func__);
75 return FPGA_FAIL;
76 }
77
78 length = (*dataptr << 8) + *(dataptr + 1);
79 dataptr += 2;
80 for (i = 0; i < length; i++)
81 buffer[i] = *dataptr++;
82
83 printf(" design filename = \"%s\"\n", buffer);
84
85 /* get part number (identifier, length, string) */
86 if (*dataptr++ != 0x62) {
87 printf("%s: Part number id not recognized in bitstream\n",
88 __func__);
89 return FPGA_FAIL;
90 }
91
92 length = (*dataptr << 8) + *(dataptr + 1);
93 dataptr += 2;
94 for (i = 0; i < length; i++)
95 buffer[i] = *dataptr++;
96 printf(" part number = \"%s\"\n", buffer);
97
98 /* get date (identifier, length, string) */
99 if (*dataptr++ != 0x63) {
100 printf("%s: Date identifier not recognized in bitstream\n",
101 __func__);
102 return FPGA_FAIL;
103 }
104
105 length = (*dataptr << 8) + *(dataptr+1);
106 dataptr += 2;
107 for (i = 0; i < length; i++)
108 buffer[i] = *dataptr++;
109 printf(" date = \"%s\"\n", buffer);
110
111 /* get time (identifier, length, string) */
112 if (*dataptr++ != 0x64) {
113 printf("%s: Time identifier not recognized in bitstream\n",
114 __func__);
115 return FPGA_FAIL;
116 }
117
118 length = (*dataptr << 8) + *(dataptr+1);
119 dataptr += 2;
120 for (i = 0; i < length; i++)
121 buffer[i] = *dataptr++;
122 printf(" time = \"%s\"\n", buffer);
123
124 /* get fpga data length (identifier, length) */
125 if (*dataptr++ != 0x65) {
126 printf("%s: Data length id not recognized in bitstream\n",
127 __func__);
128 return FPGA_FAIL;
129 }
130 swapsize = ((unsigned int) *dataptr << 24) +
131 ((unsigned int) *(dataptr + 1) << 16) +
132 ((unsigned int) *(dataptr + 2) << 8) +
133 ((unsigned int) *(dataptr + 3));
134 dataptr += 4;
135 printf(" bytes in bitstream = %d\n", swapsize);
136
Michal Simek23f4bd72013-05-01 19:02:02 +0200137 return fpga_load(devnum, dataptr, swapsize);
Michal Simek52c20642013-04-26 13:12:07 +0200138}
139
Wolfgang Denke6a857d2011-07-30 13:33:49 +0000140int xilinx_load(Xilinx_desc *desc, const void *buf, size_t bsize)
wdenk5d3207d2002-08-21 22:08:56 +0000141{
142 int ret_val = FPGA_FAIL; /* assume a failure */
143
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200144 if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
wdenk5d3207d2002-08-21 22:08:56 +0000145 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
146 } else
147 switch (desc->family) {
148 case Xilinx_Spartan2:
Matthias Fuchs01335022007-12-27 17:12:34 +0100149#if defined(CONFIG_FPGA_SPARTAN2)
wdenk5d3207d2002-08-21 22:08:56 +0000150 PRINTF ("%s: Launching the Spartan-II Loader...\n",
151 __FUNCTION__);
152 ret_val = Spartan2_load (desc, buf, bsize);
153#else
154 printf ("%s: No support for Spartan-II devices.\n",
155 __FUNCTION__);
156#endif
157 break;
Wolfgang Denk875c7892005-09-25 16:44:21 +0200158 case Xilinx_Spartan3:
Matthias Fuchs01335022007-12-27 17:12:34 +0100159#if defined(CONFIG_FPGA_SPARTAN3)
Wolfgang Denk875c7892005-09-25 16:44:21 +0200160 PRINTF ("%s: Launching the Spartan-III Loader...\n",
161 __FUNCTION__);
162 ret_val = Spartan3_load (desc, buf, bsize);
163#else
164 printf ("%s: No support for Spartan-III devices.\n",
165 __FUNCTION__);
166#endif
167 break;
wdenk5d3207d2002-08-21 22:08:56 +0000168 case Xilinx_Virtex2:
Matthias Fuchs01335022007-12-27 17:12:34 +0100169#if defined(CONFIG_FPGA_VIRTEX2)
wdenk5d3207d2002-08-21 22:08:56 +0000170 PRINTF ("%s: Launching the Virtex-II Loader...\n",
171 __FUNCTION__);
172 ret_val = Virtex2_load (desc, buf, bsize);
173#else
174 printf ("%s: No support for Virtex-II devices.\n",
175 __FUNCTION__);
176#endif
177 break;
Michal Simekd5dae852013-04-22 15:43:02 +0200178 case xilinx_zynq:
179#if defined(CONFIG_FPGA_ZYNQPL)
180 PRINTF("%s: Launching the Zynq PL Loader...\n",
181 __func__);
182 ret_val = zynq_load(desc, buf, bsize);
183#else
184 printf("%s: No support for Zynq devices.\n",
185 __func__);
186#endif
187 break;
wdenk5d3207d2002-08-21 22:08:56 +0000188
189 default:
190 printf ("%s: Unsupported family type, %d\n",
191 __FUNCTION__, desc->family);
192 }
193
194 return ret_val;
195}
196
Wolfgang Denke6a857d2011-07-30 13:33:49 +0000197int xilinx_dump(Xilinx_desc *desc, const void *buf, size_t bsize)
wdenk5d3207d2002-08-21 22:08:56 +0000198{
199 int ret_val = FPGA_FAIL; /* assume a failure */
200
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200201 if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
wdenk5d3207d2002-08-21 22:08:56 +0000202 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
203 } else
204 switch (desc->family) {
205 case Xilinx_Spartan2:
Matthias Fuchs01335022007-12-27 17:12:34 +0100206#if defined(CONFIG_FPGA_SPARTAN2)
wdenk5d3207d2002-08-21 22:08:56 +0000207 PRINTF ("%s: Launching the Spartan-II Reader...\n",
208 __FUNCTION__);
209 ret_val = Spartan2_dump (desc, buf, bsize);
210#else
211 printf ("%s: No support for Spartan-II devices.\n",
212 __FUNCTION__);
213#endif
214 break;
Wolfgang Denk875c7892005-09-25 16:44:21 +0200215 case Xilinx_Spartan3:
Matthias Fuchs01335022007-12-27 17:12:34 +0100216#if defined(CONFIG_FPGA_SPARTAN3)
Wolfgang Denk875c7892005-09-25 16:44:21 +0200217 PRINTF ("%s: Launching the Spartan-III Reader...\n",
218 __FUNCTION__);
219 ret_val = Spartan3_dump (desc, buf, bsize);
220#else
221 printf ("%s: No support for Spartan-III devices.\n",
222 __FUNCTION__);
223#endif
224 break;
wdenk5d3207d2002-08-21 22:08:56 +0000225 case Xilinx_Virtex2:
Matthias Fuchs01335022007-12-27 17:12:34 +0100226#if defined( CONFIG_FPGA_VIRTEX2)
wdenk5d3207d2002-08-21 22:08:56 +0000227 PRINTF ("%s: Launching the Virtex-II Reader...\n",
228 __FUNCTION__);
229 ret_val = Virtex2_dump (desc, buf, bsize);
230#else
231 printf ("%s: No support for Virtex-II devices.\n",
232 __FUNCTION__);
233#endif
234 break;
Michal Simekd5dae852013-04-22 15:43:02 +0200235 case xilinx_zynq:
236#if defined(CONFIG_FPGA_ZYNQPL)
237 PRINTF("%s: Launching the Zynq PL Reader...\n",
238 __func__);
239 ret_val = zynq_dump(desc, buf, bsize);
240#else
241 printf("%s: No support for Zynq devices.\n",
242 __func__);
243#endif
244 break;
wdenk5d3207d2002-08-21 22:08:56 +0000245
246 default:
247 printf ("%s: Unsupported family type, %d\n",
248 __FUNCTION__, desc->family);
249 }
250
251 return ret_val;
252}
253
254int xilinx_info (Xilinx_desc * desc)
255{
256 int ret_val = FPGA_FAIL;
257
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200258 if (xilinx_validate (desc, (char *)__FUNCTION__)) {
wdenk5d3207d2002-08-21 22:08:56 +0000259 printf ("Family: \t");
260 switch (desc->family) {
261 case Xilinx_Spartan2:
262 printf ("Spartan-II\n");
263 break;
Wolfgang Denk875c7892005-09-25 16:44:21 +0200264 case Xilinx_Spartan3:
265 printf ("Spartan-III\n");
266 break;
wdenk5d3207d2002-08-21 22:08:56 +0000267 case Xilinx_Virtex2:
268 printf ("Virtex-II\n");
269 break;
Michal Simekd5dae852013-04-22 15:43:02 +0200270 case xilinx_zynq:
271 printf("Zynq PL\n");
272 break;
wdenk5d3207d2002-08-21 22:08:56 +0000273 /* Add new family types here */
274 default:
275 printf ("Unknown family type, %d\n", desc->family);
276 }
277
278 printf ("Interface type:\t");
279 switch (desc->iface) {
280 case slave_serial:
281 printf ("Slave Serial\n");
282 break;
283 case master_serial: /* Not used */
284 printf ("Master Serial\n");
285 break;
286 case slave_parallel:
287 printf ("Slave Parallel\n");
288 break;
289 case jtag_mode: /* Not used */
290 printf ("JTAG Mode\n");
291 break;
292 case slave_selectmap:
293 printf ("Slave SelectMap Mode\n");
294 break;
295 case master_selectmap:
296 printf ("Master SelectMap Mode\n");
297 break;
Michal Simekd5dae852013-04-22 15:43:02 +0200298 case devcfg:
299 printf("Device configuration interface (Zynq)\n");
300 break;
wdenk5d3207d2002-08-21 22:08:56 +0000301 /* Add new interface types here */
302 default:
303 printf ("Unsupported interface type, %d\n", desc->iface);
304 }
305
306 printf ("Device Size: \t%d bytes\n"
307 "Cookie: \t0x%x (%d)\n",
308 desc->size, desc->cookie, desc->cookie);
309
310 if (desc->iface_fns) {
311 printf ("Device Function Table @ 0x%p\n", desc->iface_fns);
312 switch (desc->family) {
313 case Xilinx_Spartan2:
Matthias Fuchs01335022007-12-27 17:12:34 +0100314#if defined(CONFIG_FPGA_SPARTAN2)
wdenk5d3207d2002-08-21 22:08:56 +0000315 Spartan2_info (desc);
316#else
317 /* just in case */
318 printf ("%s: No support for Spartan-II devices.\n",
319 __FUNCTION__);
320#endif
321 break;
Wolfgang Denk875c7892005-09-25 16:44:21 +0200322 case Xilinx_Spartan3:
Matthias Fuchs01335022007-12-27 17:12:34 +0100323#if defined(CONFIG_FPGA_SPARTAN3)
Wolfgang Denk875c7892005-09-25 16:44:21 +0200324 Spartan3_info (desc);
325#else
326 /* just in case */
327 printf ("%s: No support for Spartan-III devices.\n",
328 __FUNCTION__);
329#endif
330 break;
wdenk5d3207d2002-08-21 22:08:56 +0000331 case Xilinx_Virtex2:
Matthias Fuchs01335022007-12-27 17:12:34 +0100332#if defined(CONFIG_FPGA_VIRTEX2)
wdenk5d3207d2002-08-21 22:08:56 +0000333 Virtex2_info (desc);
334#else
335 /* just in case */
336 printf ("%s: No support for Virtex-II devices.\n",
337 __FUNCTION__);
338#endif
339 break;
Michal Simekd5dae852013-04-22 15:43:02 +0200340 case xilinx_zynq:
341#if defined(CONFIG_FPGA_ZYNQPL)
342 zynq_info(desc);
343#else
344 /* just in case */
345 printf("%s: No support for Zynq devices.\n",
346 __func__);
347#endif
wdenk5d3207d2002-08-21 22:08:56 +0000348 /* Add new family types here */
349 default:
350 /* we don't need a message here - we give one up above */
wdenka8c7c702003-12-06 19:49:23 +0000351 ;
wdenk5d3207d2002-08-21 22:08:56 +0000352 }
353 } else
354 printf ("No Device Function Table.\n");
355
356 ret_val = FPGA_SUCCESS;
357 } else {
358 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
359 }
360
361 return ret_val;
362}
363
wdenk5d3207d2002-08-21 22:08:56 +0000364/* ------------------------------------------------------------------------- */
365
366static int xilinx_validate (Xilinx_desc * desc, char *fn)
367{
York Sun472d5462013-04-01 11:29:11 -0700368 int ret_val = false;
wdenk5d3207d2002-08-21 22:08:56 +0000369
370 if (desc) {
371 if ((desc->family > min_xilinx_type) &&
372 (desc->family < max_xilinx_type)) {
373 if ((desc->iface > min_xilinx_iface_type) &&
374 (desc->iface < max_xilinx_iface_type)) {
375 if (desc->size) {
York Sun472d5462013-04-01 11:29:11 -0700376 ret_val = true;
wdenk5d3207d2002-08-21 22:08:56 +0000377 } else
378 printf ("%s: NULL part size\n", fn);
379 } else
380 printf ("%s: Invalid Interface type, %d\n",
381 fn, desc->iface);
382 } else
383 printf ("%s: Invalid family type, %d\n", fn, desc->family);
384 } else
385 printf ("%s: NULL descriptor!\n", fn);
386
387 return ret_val;
388}