2 * (C) Copyright 2012-2013, Xilinx, Michal Simek
5 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
6 * Keith Outwater, keith_outwater@mvis.com
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
43 /* Define FPGA_DEBUG to get debug printf's */
45 #define PRINTF(fmt,args...) printf (fmt ,##args)
47 #define PRINTF(fmt,args...)
50 /* Local Static Functions */
51 static int xilinx_validate (Xilinx_desc * desc, char *fn);
53 /* ------------------------------------------------------------------------- */
55 int fpga_loadbitstream(int devnum, char *fpgadata, size_t size)
58 unsigned int swapsize;
60 unsigned char *dataptr;
62 const fpga_desc *desc;
65 dataptr = (unsigned char *)fpgadata;
66 /* Find out fpga_description */
67 desc = fpga_validate(devnum, dataptr, 0, (char *)__func__);
68 /* Assign xilinx device description */
69 xdesc = desc->devdesc;
71 /* skip the first bytes of the bitsteam, their meaning is unknown */
72 length = (*dataptr << 8) + *(dataptr + 1);
76 /* get design name (identifier, length, string) */
77 length = (*dataptr << 8) + *(dataptr + 1);
79 if (*dataptr++ != 0x61) {
80 debug("%s: Design name id not recognized in bitstream\n",
85 length = (*dataptr << 8) + *(dataptr + 1);
87 for (i = 0; i < length; i++)
88 buffer[i] = *dataptr++;
90 printf(" design filename = \"%s\"\n", buffer);
92 /* get part number (identifier, length, string) */
93 if (*dataptr++ != 0x62) {
94 printf("%s: Part number id not recognized in bitstream\n",
99 length = (*dataptr << 8) + *(dataptr + 1);
101 for (i = 0; i < length; i++)
102 buffer[i] = *dataptr++;
105 i = strncmp(buffer, xdesc->name, strlen(xdesc->name));
107 printf("%s: Wrong bitstream ID for this device\n",
109 printf("%s: Bitstream ID %s, current device ID %d/%s\n",
110 __func__, buffer, devnum, xdesc->name);
114 printf("%s: Please fill correct device ID to Xilinx_desc\n",
117 printf(" part number = \"%s\"\n", buffer);
119 /* get date (identifier, length, string) */
120 if (*dataptr++ != 0x63) {
121 printf("%s: Date identifier not recognized in bitstream\n",
126 length = (*dataptr << 8) + *(dataptr+1);
128 for (i = 0; i < length; i++)
129 buffer[i] = *dataptr++;
130 printf(" date = \"%s\"\n", buffer);
132 /* get time (identifier, length, string) */
133 if (*dataptr++ != 0x64) {
134 printf("%s: Time identifier not recognized in bitstream\n",
139 length = (*dataptr << 8) + *(dataptr+1);
141 for (i = 0; i < length; i++)
142 buffer[i] = *dataptr++;
143 printf(" time = \"%s\"\n", buffer);
145 /* get fpga data length (identifier, length) */
146 if (*dataptr++ != 0x65) {
147 printf("%s: Data length id not recognized in bitstream\n",
151 swapsize = ((unsigned int) *dataptr << 24) +
152 ((unsigned int) *(dataptr + 1) << 16) +
153 ((unsigned int) *(dataptr + 2) << 8) +
154 ((unsigned int) *(dataptr + 3));
156 printf(" bytes in bitstream = %d\n", swapsize);
158 return fpga_load(devnum, dataptr, swapsize);
161 int xilinx_load(Xilinx_desc *desc, const void *buf, size_t bsize)
163 int ret_val = FPGA_FAIL; /* assume a failure */
165 if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
166 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
168 switch (desc->family) {
169 case Xilinx_Spartan2:
170 #if defined(CONFIG_FPGA_SPARTAN2)
171 PRINTF ("%s: Launching the Spartan-II Loader...\n",
173 ret_val = Spartan2_load (desc, buf, bsize);
175 printf ("%s: No support for Spartan-II devices.\n",
179 case Xilinx_Spartan3:
180 #if defined(CONFIG_FPGA_SPARTAN3)
181 PRINTF ("%s: Launching the Spartan-III Loader...\n",
183 ret_val = Spartan3_load (desc, buf, bsize);
185 printf ("%s: No support for Spartan-III devices.\n",
190 #if defined(CONFIG_FPGA_VIRTEX2)
191 PRINTF ("%s: Launching the Virtex-II Loader...\n",
193 ret_val = Virtex2_load (desc, buf, bsize);
195 printf ("%s: No support for Virtex-II devices.\n",
200 #if defined(CONFIG_FPGA_ZYNQPL)
201 PRINTF("%s: Launching the Zynq PL Loader...\n",
203 ret_val = zynq_load(desc, buf, bsize);
205 printf("%s: No support for Zynq devices.\n",
211 printf ("%s: Unsupported family type, %d\n",
212 __FUNCTION__, desc->family);
218 int xilinx_dump(Xilinx_desc *desc, const void *buf, size_t bsize)
220 int ret_val = FPGA_FAIL; /* assume a failure */
222 if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
223 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
225 switch (desc->family) {
226 case Xilinx_Spartan2:
227 #if defined(CONFIG_FPGA_SPARTAN2)
228 PRINTF ("%s: Launching the Spartan-II Reader...\n",
230 ret_val = Spartan2_dump (desc, buf, bsize);
232 printf ("%s: No support for Spartan-II devices.\n",
236 case Xilinx_Spartan3:
237 #if defined(CONFIG_FPGA_SPARTAN3)
238 PRINTF ("%s: Launching the Spartan-III Reader...\n",
240 ret_val = Spartan3_dump (desc, buf, bsize);
242 printf ("%s: No support for Spartan-III devices.\n",
247 #if defined( CONFIG_FPGA_VIRTEX2)
248 PRINTF ("%s: Launching the Virtex-II Reader...\n",
250 ret_val = Virtex2_dump (desc, buf, bsize);
252 printf ("%s: No support for Virtex-II devices.\n",
257 #if defined(CONFIG_FPGA_ZYNQPL)
258 PRINTF("%s: Launching the Zynq PL Reader...\n",
260 ret_val = zynq_dump(desc, buf, bsize);
262 printf("%s: No support for Zynq devices.\n",
268 printf ("%s: Unsupported family type, %d\n",
269 __FUNCTION__, desc->family);
275 int xilinx_info (Xilinx_desc * desc)
277 int ret_val = FPGA_FAIL;
279 if (xilinx_validate (desc, (char *)__FUNCTION__)) {
280 printf ("Family: \t");
281 switch (desc->family) {
282 case Xilinx_Spartan2:
283 printf ("Spartan-II\n");
285 case Xilinx_Spartan3:
286 printf ("Spartan-III\n");
289 printf ("Virtex-II\n");
294 /* Add new family types here */
296 printf ("Unknown family type, %d\n", desc->family);
299 printf ("Interface type:\t");
300 switch (desc->iface) {
302 printf ("Slave Serial\n");
304 case master_serial: /* Not used */
305 printf ("Master Serial\n");
308 printf ("Slave Parallel\n");
310 case jtag_mode: /* Not used */
311 printf ("JTAG Mode\n");
313 case slave_selectmap:
314 printf ("Slave SelectMap Mode\n");
316 case master_selectmap:
317 printf ("Master SelectMap Mode\n");
320 printf("Device configuration interface (Zynq)\n");
322 /* Add new interface types here */
324 printf ("Unsupported interface type, %d\n", desc->iface);
327 printf ("Device Size: \t%d bytes\n"
328 "Cookie: \t0x%x (%d)\n",
329 desc->size, desc->cookie, desc->cookie);
331 printf("Device name: \t%s\n", desc->name);
333 if (desc->iface_fns) {
334 printf ("Device Function Table @ 0x%p\n", desc->iface_fns);
335 switch (desc->family) {
336 case Xilinx_Spartan2:
337 #if defined(CONFIG_FPGA_SPARTAN2)
338 Spartan2_info (desc);
341 printf ("%s: No support for Spartan-II devices.\n",
345 case Xilinx_Spartan3:
346 #if defined(CONFIG_FPGA_SPARTAN3)
347 Spartan3_info (desc);
350 printf ("%s: No support for Spartan-III devices.\n",
355 #if defined(CONFIG_FPGA_VIRTEX2)
359 printf ("%s: No support for Virtex-II devices.\n",
364 #if defined(CONFIG_FPGA_ZYNQPL)
368 printf("%s: No support for Zynq devices.\n",
371 /* Add new family types here */
373 /* we don't need a message here - we give one up above */
377 printf ("No Device Function Table.\n");
379 ret_val = FPGA_SUCCESS;
381 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
387 /* ------------------------------------------------------------------------- */
389 static int xilinx_validate (Xilinx_desc * desc, char *fn)
394 if ((desc->family > min_xilinx_type) &&
395 (desc->family < max_xilinx_type)) {
396 if ((desc->iface > min_xilinx_iface_type) &&
397 (desc->iface < max_xilinx_iface_type)) {
401 printf ("%s: NULL part size\n", fn);
403 printf ("%s: Invalid Interface type, %d\n",
406 printf ("%s: Invalid family type, %d\n", fn, desc->family);
408 printf ("%s: NULL descriptor!\n", fn);