blob: 941f30010a5437dccd3737960204ec4888ea4ebb [file] [log] [blame]
wdenk5d3207d2002-08-21 22:08:56 +00001/*
Michal Simek15d654c2013-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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk5d3207d2002-08-21 22:08:56 +00009 */
10
11/*
12 * Xilinx FPGA support
13 */
14
15#include <common.h>
Michal Simek6ff890d2013-04-26 15:04:48 +020016#include <fpga.h>
wdenk5d3207d2002-08-21 22:08:56 +000017#include <virtex2.h>
18#include <spartan2.h>
Wolfgang Denkc38e70c2005-09-25 16:44:21 +020019#include <spartan3.h>
Michal Simek15d654c2013-04-22 15:43:02 +020020#include <zynqpl.h>
wdenk5d3207d2002-08-21 22:08:56 +000021
wdenk5d3207d2002-08-21 22:08:56 +000022/* Local Static Functions */
Michal Simek25e1e2e2014-03-13 12:49:21 +010023static int xilinx_validate(xilinx_desc *desc, char *fn);
wdenk5d3207d2002-08-21 22:08:56 +000024
25/* ------------------------------------------------------------------------- */
26
Michal Simek14663652014-05-02 14:09:30 +020027int fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
28 bitstream_type bstype)
Michal Simekcd5a1cf2013-04-26 13:12:07 +020029{
30 unsigned int length;
31 unsigned int swapsize;
Michal Simekcd5a1cf2013-04-26 13:12:07 +020032 unsigned char *dataptr;
33 unsigned int i;
Michal Simek6ff890d2013-04-26 15:04:48 +020034 const fpga_desc *desc;
Michal Simek25e1e2e2014-03-13 12:49:21 +010035 xilinx_desc *xdesc;
Michal Simekcd5a1cf2013-04-26 13:12:07 +020036
37 dataptr = (unsigned char *)fpgadata;
Michal Simek6ff890d2013-04-26 15:04:48 +020038 /* Find out fpga_description */
39 desc = fpga_validate(devnum, dataptr, 0, (char *)__func__);
40 /* Assign xilinx device description */
41 xdesc = desc->devdesc;
Michal Simekcd5a1cf2013-04-26 13:12:07 +020042
43 /* skip the first bytes of the bitsteam, their meaning is unknown */
44 length = (*dataptr << 8) + *(dataptr + 1);
45 dataptr += 2;
46 dataptr += length;
47
48 /* get design name (identifier, length, string) */
49 length = (*dataptr << 8) + *(dataptr + 1);
50 dataptr += 2;
51 if (*dataptr++ != 0x61) {
52 debug("%s: Design name id not recognized in bitstream\n",
53 __func__);
54 return FPGA_FAIL;
55 }
56
57 length = (*dataptr << 8) + *(dataptr + 1);
58 dataptr += 2;
Siva Durga Prasad Paladugu3da614c2017-03-02 18:50:11 +053059 printf(" design filename = \"%s\"\n", dataptr);
60 dataptr += length;
Michal Simekcd5a1cf2013-04-26 13:12:07 +020061
62 /* get part number (identifier, length, string) */
63 if (*dataptr++ != 0x62) {
64 printf("%s: Part number id not recognized in bitstream\n",
65 __func__);
66 return FPGA_FAIL;
67 }
68
69 length = (*dataptr << 8) + *(dataptr + 1);
70 dataptr += 2;
Michal Simek6ff890d2013-04-26 15:04:48 +020071
72 if (xdesc->name) {
Siva Durga Prasad Paladugu3da614c2017-03-02 18:50:11 +053073 i = (ulong)strstr((char *)dataptr, xdesc->name);
Siva Durga Prasad Paladugu64b97092016-01-11 12:30:41 +053074 if (!i) {
Michal Simek6ff890d2013-04-26 15:04:48 +020075 printf("%s: Wrong bitstream ID for this device\n",
76 __func__);
77 printf("%s: Bitstream ID %s, current device ID %d/%s\n",
Siva Durga Prasad Paladugu3da614c2017-03-02 18:50:11 +053078 __func__, dataptr, devnum, xdesc->name);
Michal Simek6ff890d2013-04-26 15:04:48 +020079 return FPGA_FAIL;
80 }
81 } else {
Michal Simek25e1e2e2014-03-13 12:49:21 +010082 printf("%s: Please fill correct device ID to xilinx_desc\n",
Michal Simek6ff890d2013-04-26 15:04:48 +020083 __func__);
84 }
Siva Durga Prasad Paladugu3da614c2017-03-02 18:50:11 +053085 printf(" part number = \"%s\"\n", dataptr);
86 dataptr += length;
Michal Simekcd5a1cf2013-04-26 13:12:07 +020087
88 /* get date (identifier, length, string) */
89 if (*dataptr++ != 0x63) {
90 printf("%s: Date identifier not recognized in bitstream\n",
91 __func__);
92 return FPGA_FAIL;
93 }
94
95 length = (*dataptr << 8) + *(dataptr+1);
96 dataptr += 2;
Siva Durga Prasad Paladugu3da614c2017-03-02 18:50:11 +053097 printf(" date = \"%s\"\n", dataptr);
98 dataptr += length;
Michal Simekcd5a1cf2013-04-26 13:12:07 +020099
100 /* get time (identifier, length, string) */
101 if (*dataptr++ != 0x64) {
102 printf("%s: Time identifier not recognized in bitstream\n",
103 __func__);
104 return FPGA_FAIL;
105 }
106
107 length = (*dataptr << 8) + *(dataptr+1);
108 dataptr += 2;
Siva Durga Prasad Paladugu3da614c2017-03-02 18:50:11 +0530109 printf(" time = \"%s\"\n", dataptr);
110 dataptr += length;
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200111
112 /* get fpga data length (identifier, length) */
113 if (*dataptr++ != 0x65) {
114 printf("%s: Data length id not recognized in bitstream\n",
115 __func__);
116 return FPGA_FAIL;
117 }
118 swapsize = ((unsigned int) *dataptr << 24) +
119 ((unsigned int) *(dataptr + 1) << 16) +
120 ((unsigned int) *(dataptr + 2) << 8) +
121 ((unsigned int) *(dataptr + 3));
122 dataptr += 4;
123 printf(" bytes in bitstream = %d\n", swapsize);
124
Michal Simek14663652014-05-02 14:09:30 +0200125 return fpga_load(devnum, dataptr, swapsize, bstype);
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200126}
127
Michal Simek14663652014-05-02 14:09:30 +0200128int xilinx_load(xilinx_desc *desc, const void *buf, size_t bsize,
129 bitstream_type bstype)
wdenk5d3207d2002-08-21 22:08:56 +0000130{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200131 if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
wdenk5d3207d2002-08-21 22:08:56 +0000132 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
Michal Simek75fafac2014-03-13 13:07:57 +0100133 return FPGA_FAIL;
134 }
wdenk5d3207d2002-08-21 22:08:56 +0000135
Michal Simek1c1350d2014-07-16 10:31:21 +0200136 if (!desc->operations || !desc->operations->load) {
137 printf("%s: Missing load operation\n", __func__);
138 return FPGA_FAIL;
139 }
140
Michal Simek14663652014-05-02 14:09:30 +0200141 return desc->operations->load(desc, buf, bsize, bstype);
wdenk5d3207d2002-08-21 22:08:56 +0000142}
143
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530144#if defined(CONFIG_CMD_FPGA_LOADFS)
145int xilinx_loadfs(xilinx_desc *desc, const void *buf, size_t bsize,
146 fpga_fs_info *fpga_fsinfo)
147{
148 if (!xilinx_validate(desc, (char *)__func__)) {
149 printf("%s: Invalid device descriptor\n", __func__);
150 return FPGA_FAIL;
151 }
152
Michal Simek1c1350d2014-07-16 10:31:21 +0200153 if (!desc->operations || !desc->operations->loadfs) {
154 printf("%s: Missing loadfs operation\n", __func__);
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530155 return FPGA_FAIL;
Michal Simek1c1350d2014-07-16 10:31:21 +0200156 }
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530157
158 return desc->operations->loadfs(desc, buf, bsize, fpga_fsinfo);
159}
160#endif
161
Michal Simek25e1e2e2014-03-13 12:49:21 +0100162int xilinx_dump(xilinx_desc *desc, const void *buf, size_t bsize)
wdenk5d3207d2002-08-21 22:08:56 +0000163{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200164 if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
wdenk5d3207d2002-08-21 22:08:56 +0000165 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
Michal Simek75fafac2014-03-13 13:07:57 +0100166 return FPGA_FAIL;
167 }
wdenk5d3207d2002-08-21 22:08:56 +0000168
Michal Simek1c1350d2014-07-16 10:31:21 +0200169 if (!desc->operations || !desc->operations->dump) {
170 printf("%s: Missing dump operation\n", __func__);
171 return FPGA_FAIL;
172 }
173
Michal Simek75fafac2014-03-13 13:07:57 +0100174 return desc->operations->dump(desc, buf, bsize);
wdenk5d3207d2002-08-21 22:08:56 +0000175}
176
Michal Simek25e1e2e2014-03-13 12:49:21 +0100177int xilinx_info(xilinx_desc *desc)
wdenk5d3207d2002-08-21 22:08:56 +0000178{
179 int ret_val = FPGA_FAIL;
180
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200181 if (xilinx_validate (desc, (char *)__FUNCTION__)) {
wdenk5d3207d2002-08-21 22:08:56 +0000182 printf ("Family: \t");
183 switch (desc->family) {
Michal Simek5206cca2014-03-13 11:23:43 +0100184 case xilinx_spartan2:
wdenk5d3207d2002-08-21 22:08:56 +0000185 printf ("Spartan-II\n");
186 break;
Michal Simek2091a0c2014-03-13 11:28:42 +0100187 case xilinx_spartan3:
Wolfgang Denkc38e70c2005-09-25 16:44:21 +0200188 printf ("Spartan-III\n");
189 break;
Michal Simek90258882014-03-13 11:33:36 +0100190 case xilinx_virtex2:
wdenk5d3207d2002-08-21 22:08:56 +0000191 printf ("Virtex-II\n");
192 break;
Michal Simek15d654c2013-04-22 15:43:02 +0200193 case xilinx_zynq:
194 printf("Zynq PL\n");
195 break;
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530196 case xilinx_zynqmp:
197 printf("ZynqMP PL\n");
198 break;
wdenk5d3207d2002-08-21 22:08:56 +0000199 /* Add new family types here */
200 default:
201 printf ("Unknown family type, %d\n", desc->family);
202 }
203
204 printf ("Interface type:\t");
205 switch (desc->iface) {
206 case slave_serial:
207 printf ("Slave Serial\n");
208 break;
209 case master_serial: /* Not used */
210 printf ("Master Serial\n");
211 break;
212 case slave_parallel:
213 printf ("Slave Parallel\n");
214 break;
215 case jtag_mode: /* Not used */
216 printf ("JTAG Mode\n");
217 break;
218 case slave_selectmap:
219 printf ("Slave SelectMap Mode\n");
220 break;
221 case master_selectmap:
222 printf ("Master SelectMap Mode\n");
223 break;
Michal Simek15d654c2013-04-22 15:43:02 +0200224 case devcfg:
225 printf("Device configuration interface (Zynq)\n");
226 break;
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530227 case csu_dma:
228 printf("csu_dma configuration interface (ZynqMP)\n");
229 break;
wdenk5d3207d2002-08-21 22:08:56 +0000230 /* Add new interface types here */
231 default:
232 printf ("Unsupported interface type, %d\n", desc->iface);
233 }
234
Simon Glass660031e2014-06-07 22:07:58 -0600235 printf("Device Size: \t%zd bytes\n"
236 "Cookie: \t0x%x (%d)\n",
237 desc->size, desc->cookie, desc->cookie);
Michal Simek6ff890d2013-04-26 15:04:48 +0200238 if (desc->name)
239 printf("Device name: \t%s\n", desc->name);
wdenk5d3207d2002-08-21 22:08:56 +0000240
Michal Simek35d9ad22014-07-16 10:36:42 +0200241 if (desc->iface_fns)
wdenk5d3207d2002-08-21 22:08:56 +0000242 printf ("Device Function Table @ 0x%p\n", desc->iface_fns);
Michal Simek35d9ad22014-07-16 10:36:42 +0200243 else
wdenk5d3207d2002-08-21 22:08:56 +0000244 printf ("No Device Function Table.\n");
245
Michal Simek35d9ad22014-07-16 10:36:42 +0200246 if (desc->operations && desc->operations->info)
247 desc->operations->info(desc);
248
wdenk5d3207d2002-08-21 22:08:56 +0000249 ret_val = FPGA_SUCCESS;
250 } else {
251 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
252 }
253
254 return ret_val;
255}
256
wdenk5d3207d2002-08-21 22:08:56 +0000257/* ------------------------------------------------------------------------- */
258
Michal Simek25e1e2e2014-03-13 12:49:21 +0100259static int xilinx_validate(xilinx_desc *desc, char *fn)
wdenk5d3207d2002-08-21 22:08:56 +0000260{
York Sun4a598092013-04-01 11:29:11 -0700261 int ret_val = false;
wdenk5d3207d2002-08-21 22:08:56 +0000262
263 if (desc) {
264 if ((desc->family > min_xilinx_type) &&
265 (desc->family < max_xilinx_type)) {
266 if ((desc->iface > min_xilinx_iface_type) &&
267 (desc->iface < max_xilinx_iface_type)) {
268 if (desc->size) {
York Sun4a598092013-04-01 11:29:11 -0700269 ret_val = true;
wdenk5d3207d2002-08-21 22:08:56 +0000270 } else
271 printf ("%s: NULL part size\n", fn);
272 } else
273 printf ("%s: Invalid Interface type, %d\n",
274 fn, desc->iface);
275 } else
276 printf ("%s: Invalid family type, %d\n", fn, desc->family);
277 } else
278 printf ("%s: NULL descriptor!\n", fn);
279
280 return ret_val;
281}