2 * (C) Copyright 2012-2013, Xilinx, Michal Simek
5 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
6 * Keith Outwater, keith_outwater@mvis.com
8 * SPDX-License-Identifier: GPL-2.0+
26 /* Define FPGA_DEBUG to get debug printf's */
28 #define PRINTF(fmt,args...) printf (fmt ,##args)
30 #define PRINTF(fmt,args...)
33 /* Local Static Functions */
34 static int xilinx_validate (Xilinx_desc * desc, char *fn);
36 /* ------------------------------------------------------------------------- */
38 int fpga_loadbitstream(int devnum, char *fpgadata, size_t size)
41 unsigned int swapsize;
43 unsigned char *dataptr;
45 const fpga_desc *desc;
48 dataptr = (unsigned char *)fpgadata;
49 /* Find out fpga_description */
50 desc = fpga_validate(devnum, dataptr, 0, (char *)__func__);
51 /* Assign xilinx device description */
52 xdesc = desc->devdesc;
54 /* skip the first bytes of the bitsteam, their meaning is unknown */
55 length = (*dataptr << 8) + *(dataptr + 1);
59 /* get design name (identifier, length, string) */
60 length = (*dataptr << 8) + *(dataptr + 1);
62 if (*dataptr++ != 0x61) {
63 debug("%s: Design name id not recognized in bitstream\n",
68 length = (*dataptr << 8) + *(dataptr + 1);
70 for (i = 0; i < length; i++)
71 buffer[i] = *dataptr++;
73 printf(" design filename = \"%s\"\n", buffer);
75 /* get part number (identifier, length, string) */
76 if (*dataptr++ != 0x62) {
77 printf("%s: Part number id not recognized in bitstream\n",
82 length = (*dataptr << 8) + *(dataptr + 1);
84 for (i = 0; i < length; i++)
85 buffer[i] = *dataptr++;
88 i = strncmp(buffer, xdesc->name, strlen(xdesc->name));
90 printf("%s: Wrong bitstream ID for this device\n",
92 printf("%s: Bitstream ID %s, current device ID %d/%s\n",
93 __func__, buffer, devnum, xdesc->name);
97 printf("%s: Please fill correct device ID to Xilinx_desc\n",
100 printf(" part number = \"%s\"\n", buffer);
102 /* get date (identifier, length, string) */
103 if (*dataptr++ != 0x63) {
104 printf("%s: Date identifier not recognized in bitstream\n",
109 length = (*dataptr << 8) + *(dataptr+1);
111 for (i = 0; i < length; i++)
112 buffer[i] = *dataptr++;
113 printf(" date = \"%s\"\n", buffer);
115 /* get time (identifier, length, string) */
116 if (*dataptr++ != 0x64) {
117 printf("%s: Time identifier not recognized in bitstream\n",
122 length = (*dataptr << 8) + *(dataptr+1);
124 for (i = 0; i < length; i++)
125 buffer[i] = *dataptr++;
126 printf(" time = \"%s\"\n", buffer);
128 /* get fpga data length (identifier, length) */
129 if (*dataptr++ != 0x65) {
130 printf("%s: Data length id not recognized in bitstream\n",
134 swapsize = ((unsigned int) *dataptr << 24) +
135 ((unsigned int) *(dataptr + 1) << 16) +
136 ((unsigned int) *(dataptr + 2) << 8) +
137 ((unsigned int) *(dataptr + 3));
139 printf(" bytes in bitstream = %d\n", swapsize);
141 return fpga_load(devnum, dataptr, swapsize);
144 int xilinx_load(Xilinx_desc *desc, const void *buf, size_t bsize)
146 int ret_val = FPGA_FAIL; /* assume a failure */
148 if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
149 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
151 switch (desc->family) {
152 case xilinx_spartan2:
153 #if defined(CONFIG_FPGA_SPARTAN2)
154 PRINTF ("%s: Launching the Spartan-II Loader...\n",
156 ret_val = spartan2_load(desc, buf, bsize);
158 printf ("%s: No support for Spartan-II devices.\n",
162 case xilinx_spartan3:
163 #if defined(CONFIG_FPGA_SPARTAN3)
164 PRINTF ("%s: Launching the Spartan-III Loader...\n",
166 ret_val = spartan3_load(desc, buf, bsize);
168 printf ("%s: No support for Spartan-III devices.\n",
173 #if defined(CONFIG_FPGA_VIRTEX2)
174 PRINTF ("%s: Launching the Virtex-II Loader...\n",
176 ret_val = virtex2_load(desc, buf, bsize);
178 printf ("%s: No support for Virtex-II devices.\n",
183 #if defined(CONFIG_FPGA_ZYNQPL)
184 PRINTF("%s: Launching the Zynq PL Loader...\n",
186 ret_val = zynq_load(desc, buf, bsize);
188 printf("%s: No support for Zynq devices.\n",
194 printf ("%s: Unsupported family type, %d\n",
195 __FUNCTION__, desc->family);
201 int xilinx_dump(Xilinx_desc *desc, const void *buf, size_t bsize)
203 int ret_val = FPGA_FAIL; /* assume a failure */
205 if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
206 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
208 switch (desc->family) {
209 case xilinx_spartan2:
210 #if defined(CONFIG_FPGA_SPARTAN2)
211 PRINTF ("%s: Launching the Spartan-II Reader...\n",
213 ret_val = spartan2_dump(desc, buf, bsize);
215 printf ("%s: No support for Spartan-II devices.\n",
219 case xilinx_spartan3:
220 #if defined(CONFIG_FPGA_SPARTAN3)
221 PRINTF ("%s: Launching the Spartan-III Reader...\n",
223 ret_val = spartan3_dump(desc, buf, bsize);
225 printf ("%s: No support for Spartan-III devices.\n",
230 #if defined( CONFIG_FPGA_VIRTEX2)
231 PRINTF ("%s: Launching the Virtex-II Reader...\n",
233 ret_val = virtex2_dump(desc, buf, bsize);
235 printf ("%s: No support for Virtex-II devices.\n",
240 #if defined(CONFIG_FPGA_ZYNQPL)
241 PRINTF("%s: Launching the Zynq PL Reader...\n",
243 ret_val = zynq_dump(desc, buf, bsize);
245 printf("%s: No support for Zynq devices.\n",
251 printf ("%s: Unsupported family type, %d\n",
252 __FUNCTION__, desc->family);
258 int xilinx_info (Xilinx_desc * desc)
260 int ret_val = FPGA_FAIL;
262 if (xilinx_validate (desc, (char *)__FUNCTION__)) {
263 printf ("Family: \t");
264 switch (desc->family) {
265 case xilinx_spartan2:
266 printf ("Spartan-II\n");
268 case xilinx_spartan3:
269 printf ("Spartan-III\n");
272 printf ("Virtex-II\n");
277 /* Add new family types here */
279 printf ("Unknown family type, %d\n", desc->family);
282 printf ("Interface type:\t");
283 switch (desc->iface) {
285 printf ("Slave Serial\n");
287 case master_serial: /* Not used */
288 printf ("Master Serial\n");
291 printf ("Slave Parallel\n");
293 case jtag_mode: /* Not used */
294 printf ("JTAG Mode\n");
296 case slave_selectmap:
297 printf ("Slave SelectMap Mode\n");
299 case master_selectmap:
300 printf ("Master SelectMap Mode\n");
303 printf("Device configuration interface (Zynq)\n");
305 /* Add new interface types here */
307 printf ("Unsupported interface type, %d\n", desc->iface);
310 printf ("Device Size: \t%d bytes\n"
311 "Cookie: \t0x%x (%d)\n",
312 desc->size, desc->cookie, desc->cookie);
314 printf("Device name: \t%s\n", desc->name);
316 if (desc->iface_fns) {
317 printf ("Device Function Table @ 0x%p\n", desc->iface_fns);
318 switch (desc->family) {
319 case xilinx_spartan2:
320 #if defined(CONFIG_FPGA_SPARTAN2)
324 printf ("%s: No support for Spartan-II devices.\n",
328 case xilinx_spartan3:
329 #if defined(CONFIG_FPGA_SPARTAN3)
333 printf ("%s: No support for Spartan-III devices.\n",
338 #if defined(CONFIG_FPGA_VIRTEX2)
342 printf ("%s: No support for Virtex-II devices.\n",
347 #if defined(CONFIG_FPGA_ZYNQPL)
351 printf("%s: No support for Zynq devices.\n",
354 /* Add new family types here */
356 /* we don't need a message here - we give one up above */
360 printf ("No Device Function Table.\n");
362 ret_val = FPGA_SUCCESS;
364 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
370 /* ------------------------------------------------------------------------- */
372 static int xilinx_validate (Xilinx_desc * desc, char *fn)
377 if ((desc->family > min_xilinx_type) &&
378 (desc->family < max_xilinx_type)) {
379 if ((desc->iface > min_xilinx_iface_type) &&
380 (desc->iface < max_xilinx_iface_type)) {
384 printf ("%s: NULL part size\n", fn);
386 printf ("%s: Invalid Interface type, %d\n",
389 printf ("%s: Invalid family type, %d\n", fn, desc->family);
391 printf ("%s: NULL descriptor!\n", fn);