blob: 3c057609697fe51ef51c7037b663b17f3a2ff94a [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
Goldschmidt Simon9179c812017-11-10 14:17:41 +000027int fpga_is_partial_data(int devnum, size_t img_len)
28{
29 const fpga_desc * const desc = fpga_get_desc(devnum);
30 xilinx_desc *desc_xilinx = desc->devdesc;
31
32 /* Check datasize against FPGA size */
33 if (img_len >= desc_xilinx->size)
34 return 0;
35
36 /* datasize is smaller, must be partial data */
37 return 1;
38}
39
Michal Simek14663652014-05-02 14:09:30 +020040int fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
41 bitstream_type bstype)
Michal Simekcd5a1cf2013-04-26 13:12:07 +020042{
43 unsigned int length;
44 unsigned int swapsize;
Michal Simekcd5a1cf2013-04-26 13:12:07 +020045 unsigned char *dataptr;
46 unsigned int i;
Michal Simek6ff890d2013-04-26 15:04:48 +020047 const fpga_desc *desc;
Michal Simek25e1e2e2014-03-13 12:49:21 +010048 xilinx_desc *xdesc;
Michal Simekcd5a1cf2013-04-26 13:12:07 +020049
50 dataptr = (unsigned char *)fpgadata;
Michal Simek6ff890d2013-04-26 15:04:48 +020051 /* Find out fpga_description */
52 desc = fpga_validate(devnum, dataptr, 0, (char *)__func__);
53 /* Assign xilinx device description */
54 xdesc = desc->devdesc;
Michal Simekcd5a1cf2013-04-26 13:12:07 +020055
56 /* skip the first bytes of the bitsteam, their meaning is unknown */
57 length = (*dataptr << 8) + *(dataptr + 1);
58 dataptr += 2;
59 dataptr += length;
60
61 /* get design name (identifier, length, string) */
62 length = (*dataptr << 8) + *(dataptr + 1);
63 dataptr += 2;
64 if (*dataptr++ != 0x61) {
65 debug("%s: Design name id not recognized in bitstream\n",
66 __func__);
67 return FPGA_FAIL;
68 }
69
70 length = (*dataptr << 8) + *(dataptr + 1);
71 dataptr += 2;
Siva Durga Prasad Paladugu3da614c2017-03-02 18:50:11 +053072 printf(" design filename = \"%s\"\n", dataptr);
73 dataptr += length;
Michal Simekcd5a1cf2013-04-26 13:12:07 +020074
75 /* get part number (identifier, length, string) */
76 if (*dataptr++ != 0x62) {
77 printf("%s: Part number id not recognized in bitstream\n",
78 __func__);
79 return FPGA_FAIL;
80 }
81
82 length = (*dataptr << 8) + *(dataptr + 1);
83 dataptr += 2;
Michal Simek6ff890d2013-04-26 15:04:48 +020084
85 if (xdesc->name) {
Siva Durga Prasad Paladugu3da614c2017-03-02 18:50:11 +053086 i = (ulong)strstr((char *)dataptr, xdesc->name);
Siva Durga Prasad Paladugu64b97092016-01-11 12:30:41 +053087 if (!i) {
Michal Simek6ff890d2013-04-26 15:04:48 +020088 printf("%s: Wrong bitstream ID for this device\n",
89 __func__);
90 printf("%s: Bitstream ID %s, current device ID %d/%s\n",
Siva Durga Prasad Paladugu3da614c2017-03-02 18:50:11 +053091 __func__, dataptr, devnum, xdesc->name);
Michal Simek6ff890d2013-04-26 15:04:48 +020092 return FPGA_FAIL;
93 }
94 } else {
Michal Simek25e1e2e2014-03-13 12:49:21 +010095 printf("%s: Please fill correct device ID to xilinx_desc\n",
Michal Simek6ff890d2013-04-26 15:04:48 +020096 __func__);
97 }
Siva Durga Prasad Paladugu3da614c2017-03-02 18:50:11 +053098 printf(" part number = \"%s\"\n", dataptr);
99 dataptr += length;
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200100
101 /* get date (identifier, length, string) */
102 if (*dataptr++ != 0x63) {
103 printf("%s: Date identifier not recognized in bitstream\n",
104 __func__);
105 return FPGA_FAIL;
106 }
107
108 length = (*dataptr << 8) + *(dataptr+1);
109 dataptr += 2;
Siva Durga Prasad Paladugu3da614c2017-03-02 18:50:11 +0530110 printf(" date = \"%s\"\n", dataptr);
111 dataptr += length;
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200112
113 /* get time (identifier, length, string) */
114 if (*dataptr++ != 0x64) {
115 printf("%s: Time identifier not recognized in bitstream\n",
116 __func__);
117 return FPGA_FAIL;
118 }
119
120 length = (*dataptr << 8) + *(dataptr+1);
121 dataptr += 2;
Siva Durga Prasad Paladugu3da614c2017-03-02 18:50:11 +0530122 printf(" time = \"%s\"\n", dataptr);
123 dataptr += length;
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200124
125 /* get fpga data length (identifier, length) */
126 if (*dataptr++ != 0x65) {
127 printf("%s: Data length id not recognized in bitstream\n",
128 __func__);
129 return FPGA_FAIL;
130 }
131 swapsize = ((unsigned int) *dataptr << 24) +
132 ((unsigned int) *(dataptr + 1) << 16) +
133 ((unsigned int) *(dataptr + 2) << 8) +
134 ((unsigned int) *(dataptr + 3));
135 dataptr += 4;
136 printf(" bytes in bitstream = %d\n", swapsize);
137
Michal Simek14663652014-05-02 14:09:30 +0200138 return fpga_load(devnum, dataptr, swapsize, bstype);
Michal Simekcd5a1cf2013-04-26 13:12:07 +0200139}
140
Michal Simek14663652014-05-02 14:09:30 +0200141int xilinx_load(xilinx_desc *desc, const void *buf, size_t bsize,
142 bitstream_type bstype)
wdenk5d3207d2002-08-21 22:08:56 +0000143{
Wolfgang Denk7fb52662005-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__);
Michal Simek75fafac2014-03-13 13:07:57 +0100146 return FPGA_FAIL;
147 }
wdenk5d3207d2002-08-21 22:08:56 +0000148
Michal Simek1c1350d2014-07-16 10:31:21 +0200149 if (!desc->operations || !desc->operations->load) {
150 printf("%s: Missing load operation\n", __func__);
151 return FPGA_FAIL;
152 }
153
Michal Simek14663652014-05-02 14:09:30 +0200154 return desc->operations->load(desc, buf, bsize, bstype);
wdenk5d3207d2002-08-21 22:08:56 +0000155}
156
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530157#if defined(CONFIG_CMD_FPGA_LOADFS)
158int xilinx_loadfs(xilinx_desc *desc, const void *buf, size_t bsize,
159 fpga_fs_info *fpga_fsinfo)
160{
161 if (!xilinx_validate(desc, (char *)__func__)) {
162 printf("%s: Invalid device descriptor\n", __func__);
163 return FPGA_FAIL;
164 }
165
Michal Simek1c1350d2014-07-16 10:31:21 +0200166 if (!desc->operations || !desc->operations->loadfs) {
167 printf("%s: Missing loadfs operation\n", __func__);
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530168 return FPGA_FAIL;
Michal Simek1c1350d2014-07-16 10:31:21 +0200169 }
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530170
171 return desc->operations->loadfs(desc, buf, bsize, fpga_fsinfo);
172}
173#endif
174
Michal Simek25e1e2e2014-03-13 12:49:21 +0100175int xilinx_dump(xilinx_desc *desc, const void *buf, size_t bsize)
wdenk5d3207d2002-08-21 22:08:56 +0000176{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200177 if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
wdenk5d3207d2002-08-21 22:08:56 +0000178 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
Michal Simek75fafac2014-03-13 13:07:57 +0100179 return FPGA_FAIL;
180 }
wdenk5d3207d2002-08-21 22:08:56 +0000181
Michal Simek1c1350d2014-07-16 10:31:21 +0200182 if (!desc->operations || !desc->operations->dump) {
183 printf("%s: Missing dump operation\n", __func__);
184 return FPGA_FAIL;
185 }
186
Michal Simek75fafac2014-03-13 13:07:57 +0100187 return desc->operations->dump(desc, buf, bsize);
wdenk5d3207d2002-08-21 22:08:56 +0000188}
189
Michal Simek25e1e2e2014-03-13 12:49:21 +0100190int xilinx_info(xilinx_desc *desc)
wdenk5d3207d2002-08-21 22:08:56 +0000191{
192 int ret_val = FPGA_FAIL;
193
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200194 if (xilinx_validate (desc, (char *)__FUNCTION__)) {
wdenk5d3207d2002-08-21 22:08:56 +0000195 printf ("Family: \t");
196 switch (desc->family) {
Michal Simek5206cca2014-03-13 11:23:43 +0100197 case xilinx_spartan2:
wdenk5d3207d2002-08-21 22:08:56 +0000198 printf ("Spartan-II\n");
199 break;
Michal Simek2091a0c2014-03-13 11:28:42 +0100200 case xilinx_spartan3:
Wolfgang Denkc38e70c2005-09-25 16:44:21 +0200201 printf ("Spartan-III\n");
202 break;
Michal Simek90258882014-03-13 11:33:36 +0100203 case xilinx_virtex2:
wdenk5d3207d2002-08-21 22:08:56 +0000204 printf ("Virtex-II\n");
205 break;
Michal Simek15d654c2013-04-22 15:43:02 +0200206 case xilinx_zynq:
207 printf("Zynq PL\n");
208 break;
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530209 case xilinx_zynqmp:
210 printf("ZynqMP PL\n");
211 break;
wdenk5d3207d2002-08-21 22:08:56 +0000212 /* Add new family types here */
213 default:
214 printf ("Unknown family type, %d\n", desc->family);
215 }
216
217 printf ("Interface type:\t");
218 switch (desc->iface) {
219 case slave_serial:
220 printf ("Slave Serial\n");
221 break;
222 case master_serial: /* Not used */
223 printf ("Master Serial\n");
224 break;
225 case slave_parallel:
226 printf ("Slave Parallel\n");
227 break;
228 case jtag_mode: /* Not used */
229 printf ("JTAG Mode\n");
230 break;
231 case slave_selectmap:
232 printf ("Slave SelectMap Mode\n");
233 break;
234 case master_selectmap:
235 printf ("Master SelectMap Mode\n");
236 break;
Michal Simek15d654c2013-04-22 15:43:02 +0200237 case devcfg:
238 printf("Device configuration interface (Zynq)\n");
239 break;
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530240 case csu_dma:
241 printf("csu_dma configuration interface (ZynqMP)\n");
242 break;
wdenk5d3207d2002-08-21 22:08:56 +0000243 /* Add new interface types here */
244 default:
245 printf ("Unsupported interface type, %d\n", desc->iface);
246 }
247
Simon Glass660031e2014-06-07 22:07:58 -0600248 printf("Device Size: \t%zd bytes\n"
249 "Cookie: \t0x%x (%d)\n",
250 desc->size, desc->cookie, desc->cookie);
Michal Simek6ff890d2013-04-26 15:04:48 +0200251 if (desc->name)
252 printf("Device name: \t%s\n", desc->name);
wdenk5d3207d2002-08-21 22:08:56 +0000253
Michal Simek35d9ad22014-07-16 10:36:42 +0200254 if (desc->iface_fns)
wdenk5d3207d2002-08-21 22:08:56 +0000255 printf ("Device Function Table @ 0x%p\n", desc->iface_fns);
Michal Simek35d9ad22014-07-16 10:36:42 +0200256 else
wdenk5d3207d2002-08-21 22:08:56 +0000257 printf ("No Device Function Table.\n");
258
Michal Simek35d9ad22014-07-16 10:36:42 +0200259 if (desc->operations && desc->operations->info)
260 desc->operations->info(desc);
261
wdenk5d3207d2002-08-21 22:08:56 +0000262 ret_val = FPGA_SUCCESS;
263 } else {
264 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
265 }
266
267 return ret_val;
268}
269
wdenk5d3207d2002-08-21 22:08:56 +0000270/* ------------------------------------------------------------------------- */
271
Michal Simek25e1e2e2014-03-13 12:49:21 +0100272static int xilinx_validate(xilinx_desc *desc, char *fn)
wdenk5d3207d2002-08-21 22:08:56 +0000273{
York Sun4a598092013-04-01 11:29:11 -0700274 int ret_val = false;
wdenk5d3207d2002-08-21 22:08:56 +0000275
276 if (desc) {
277 if ((desc->family > min_xilinx_type) &&
278 (desc->family < max_xilinx_type)) {
279 if ((desc->iface > min_xilinx_iface_type) &&
280 (desc->iface < max_xilinx_iface_type)) {
281 if (desc->size) {
York Sun4a598092013-04-01 11:29:11 -0700282 ret_val = true;
wdenk5d3207d2002-08-21 22:08:56 +0000283 } else
284 printf ("%s: NULL part size\n", fn);
285 } else
286 printf ("%s: Invalid Interface type, %d\n",
287 fn, desc->iface);
288 } else
289 printf ("%s: Invalid family type, %d\n", fn, desc->family);
290 } else
291 printf ("%s: NULL descriptor!\n", fn);
292
293 return ret_val;
294}