blob: 4b0334b6beb601568212756a808d22f21dd1507d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk5d3207d2002-08-21 22:08:56 +00002/*
Michal Simekd5dae852013-04-22 15:43:02 +02003 * (C) Copyright 2012-2013, Xilinx, Michal Simek
4 *
wdenk5d3207d2002-08-21 22:08:56 +00005 * (C) Copyright 2002
6 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
7 * Keith Outwater, keith_outwater@mvis.com
wdenk5d3207d2002-08-21 22:08:56 +00008 */
9
10/*
11 * Xilinx FPGA support
12 */
13
14#include <common.h>
Michal Simek6631db42013-04-26 15:04:48 +020015#include <fpga.h>
wdenk5d3207d2002-08-21 22:08:56 +000016#include <virtex2.h>
17#include <spartan2.h>
Wolfgang Denk875c7892005-09-25 16:44:21 +020018#include <spartan3.h>
Michal Simekd5dae852013-04-22 15:43:02 +020019#include <zynqpl.h>
wdenk5d3207d2002-08-21 22:08:56 +000020
wdenk5d3207d2002-08-21 22:08:56 +000021/* Local Static Functions */
Michal Simekf8c1be92014-03-13 12:49:21 +010022static int xilinx_validate(xilinx_desc *desc, char *fn);
wdenk5d3207d2002-08-21 22:08:56 +000023
24/* ------------------------------------------------------------------------- */
25
Goldschmidt Simon8b93a922017-11-10 14:17:41 +000026int fpga_is_partial_data(int devnum, size_t img_len)
27{
28 const fpga_desc * const desc = fpga_get_desc(devnum);
29 xilinx_desc *desc_xilinx = desc->devdesc;
30
31 /* Check datasize against FPGA size */
32 if (img_len >= desc_xilinx->size)
33 return 0;
34
35 /* datasize is smaller, must be partial data */
36 return 1;
37}
38
Michal Simek7a78bd22014-05-02 14:09:30 +020039int fpga_loadbitstream(int devnum, char *fpgadata, size_t size,
40 bitstream_type bstype)
Michal Simek52c20642013-04-26 13:12:07 +020041{
42 unsigned int length;
43 unsigned int swapsize;
Michal Simek52c20642013-04-26 13:12:07 +020044 unsigned char *dataptr;
45 unsigned int i;
Michal Simek6631db42013-04-26 15:04:48 +020046 const fpga_desc *desc;
Michal Simekf8c1be92014-03-13 12:49:21 +010047 xilinx_desc *xdesc;
Michal Simek52c20642013-04-26 13:12:07 +020048
49 dataptr = (unsigned char *)fpgadata;
Michal Simek6631db42013-04-26 15:04:48 +020050 /* Find out fpga_description */
51 desc = fpga_validate(devnum, dataptr, 0, (char *)__func__);
52 /* Assign xilinx device description */
53 xdesc = desc->devdesc;
Michal Simek52c20642013-04-26 13:12:07 +020054
55 /* skip the first bytes of the bitsteam, their meaning is unknown */
56 length = (*dataptr << 8) + *(dataptr + 1);
57 dataptr += 2;
58 dataptr += length;
59
60 /* get design name (identifier, length, string) */
61 length = (*dataptr << 8) + *(dataptr + 1);
62 dataptr += 2;
63 if (*dataptr++ != 0x61) {
64 debug("%s: Design name id not recognized in bitstream\n",
65 __func__);
66 return FPGA_FAIL;
67 }
68
69 length = (*dataptr << 8) + *(dataptr + 1);
70 dataptr += 2;
Siva Durga Prasad Paladugud8639092017-03-02 18:50:11 +053071 printf(" design filename = \"%s\"\n", dataptr);
72 dataptr += length;
Michal Simek52c20642013-04-26 13:12:07 +020073
74 /* get part number (identifier, length, string) */
75 if (*dataptr++ != 0x62) {
76 printf("%s: Part number id not recognized in bitstream\n",
77 __func__);
78 return FPGA_FAIL;
79 }
80
81 length = (*dataptr << 8) + *(dataptr + 1);
82 dataptr += 2;
Michal Simek6631db42013-04-26 15:04:48 +020083
84 if (xdesc->name) {
Siva Durga Prasad Paladugud8639092017-03-02 18:50:11 +053085 i = (ulong)strstr((char *)dataptr, xdesc->name);
Siva Durga Prasad Paladuguf7213262016-01-11 12:30:41 +053086 if (!i) {
Michal Simek6631db42013-04-26 15:04:48 +020087 printf("%s: Wrong bitstream ID for this device\n",
88 __func__);
89 printf("%s: Bitstream ID %s, current device ID %d/%s\n",
Siva Durga Prasad Paladugud8639092017-03-02 18:50:11 +053090 __func__, dataptr, devnum, xdesc->name);
Michal Simek6631db42013-04-26 15:04:48 +020091 return FPGA_FAIL;
92 }
93 } else {
Michal Simekf8c1be92014-03-13 12:49:21 +010094 printf("%s: Please fill correct device ID to xilinx_desc\n",
Michal Simek6631db42013-04-26 15:04:48 +020095 __func__);
96 }
Siva Durga Prasad Paladugud8639092017-03-02 18:50:11 +053097 printf(" part number = \"%s\"\n", dataptr);
98 dataptr += length;
Michal Simek52c20642013-04-26 13:12:07 +020099
100 /* get date (identifier, length, string) */
101 if (*dataptr++ != 0x63) {
102 printf("%s: Date 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 Paladugud8639092017-03-02 18:50:11 +0530109 printf(" date = \"%s\"\n", dataptr);
110 dataptr += length;
Michal Simek52c20642013-04-26 13:12:07 +0200111
112 /* get time (identifier, length, string) */
113 if (*dataptr++ != 0x64) {
114 printf("%s: Time identifier not recognized in bitstream\n",
115 __func__);
116 return FPGA_FAIL;
117 }
118
119 length = (*dataptr << 8) + *(dataptr+1);
120 dataptr += 2;
Siva Durga Prasad Paladugud8639092017-03-02 18:50:11 +0530121 printf(" time = \"%s\"\n", dataptr);
122 dataptr += length;
Michal Simek52c20642013-04-26 13:12:07 +0200123
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 Simek7a78bd22014-05-02 14:09:30 +0200137 return fpga_load(devnum, dataptr, swapsize, bstype);
Michal Simek52c20642013-04-26 13:12:07 +0200138}
139
Michal Simek7a78bd22014-05-02 14:09:30 +0200140int xilinx_load(xilinx_desc *desc, const void *buf, size_t bsize,
141 bitstream_type bstype)
wdenk5d3207d2002-08-21 22:08:56 +0000142{
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200143 if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
wdenk5d3207d2002-08-21 22:08:56 +0000144 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
Michal Simek14cfc4f2014-03-13 13:07:57 +0100145 return FPGA_FAIL;
146 }
wdenk5d3207d2002-08-21 22:08:56 +0000147
Michal Simek6cd68c82014-07-16 10:31:21 +0200148 if (!desc->operations || !desc->operations->load) {
149 printf("%s: Missing load operation\n", __func__);
150 return FPGA_FAIL;
151 }
152
Michal Simek7a78bd22014-05-02 14:09:30 +0200153 return desc->operations->load(desc, buf, bsize, bstype);
wdenk5d3207d2002-08-21 22:08:56 +0000154}
155
Siva Durga Prasad Paladugu1a897662014-03-14 16:35:37 +0530156#if defined(CONFIG_CMD_FPGA_LOADFS)
157int xilinx_loadfs(xilinx_desc *desc, const void *buf, size_t bsize,
158 fpga_fs_info *fpga_fsinfo)
159{
160 if (!xilinx_validate(desc, (char *)__func__)) {
161 printf("%s: Invalid device descriptor\n", __func__);
162 return FPGA_FAIL;
163 }
164
Michal Simek6cd68c82014-07-16 10:31:21 +0200165 if (!desc->operations || !desc->operations->loadfs) {
166 printf("%s: Missing loadfs operation\n", __func__);
Siva Durga Prasad Paladugu1a897662014-03-14 16:35:37 +0530167 return FPGA_FAIL;
Michal Simek6cd68c82014-07-16 10:31:21 +0200168 }
Siva Durga Prasad Paladugu1a897662014-03-14 16:35:37 +0530169
170 return desc->operations->loadfs(desc, buf, bsize, fpga_fsinfo);
171}
172#endif
173
Siva Durga Prasad Paladugua18d09e2018-05-31 15:10:23 +0530174#if defined(CONFIG_CMD_FPGA_LOAD_SECURE)
175int xilinx_loads(xilinx_desc *desc, const void *buf, size_t bsize,
176 struct fpga_secure_info *fpga_sec_info)
177{
178 if (!xilinx_validate(desc, (char *)__func__)) {
179 printf("%s: Invalid device descriptor\n", __func__);
180 return FPGA_FAIL;
181 }
182
183 if (!desc->operations || !desc->operations->loads) {
184 printf("%s: Missing loads operation\n", __func__);
185 return FPGA_FAIL;
186 }
187
188 return desc->operations->loads(desc, buf, bsize, fpga_sec_info);
189}
190#endif
191
Michal Simekf8c1be92014-03-13 12:49:21 +0100192int xilinx_dump(xilinx_desc *desc, const void *buf, size_t bsize)
wdenk5d3207d2002-08-21 22:08:56 +0000193{
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200194 if (!xilinx_validate (desc, (char *)__FUNCTION__)) {
wdenk5d3207d2002-08-21 22:08:56 +0000195 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
Michal Simek14cfc4f2014-03-13 13:07:57 +0100196 return FPGA_FAIL;
197 }
wdenk5d3207d2002-08-21 22:08:56 +0000198
Michal Simek6cd68c82014-07-16 10:31:21 +0200199 if (!desc->operations || !desc->operations->dump) {
200 printf("%s: Missing dump operation\n", __func__);
201 return FPGA_FAIL;
202 }
203
Michal Simek14cfc4f2014-03-13 13:07:57 +0100204 return desc->operations->dump(desc, buf, bsize);
wdenk5d3207d2002-08-21 22:08:56 +0000205}
206
Michal Simekf8c1be92014-03-13 12:49:21 +0100207int xilinx_info(xilinx_desc *desc)
wdenk5d3207d2002-08-21 22:08:56 +0000208{
209 int ret_val = FPGA_FAIL;
210
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200211 if (xilinx_validate (desc, (char *)__FUNCTION__)) {
wdenk5d3207d2002-08-21 22:08:56 +0000212 printf ("Family: \t");
213 switch (desc->family) {
Michal Simekb625b9a2014-03-13 11:23:43 +0100214 case xilinx_spartan2:
wdenk5d3207d2002-08-21 22:08:56 +0000215 printf ("Spartan-II\n");
216 break;
Michal Simek2a6e3862014-03-13 11:28:42 +0100217 case xilinx_spartan3:
Wolfgang Denk875c7892005-09-25 16:44:21 +0200218 printf ("Spartan-III\n");
219 break;
Michal Simekd9071ce2014-03-13 11:33:36 +0100220 case xilinx_virtex2:
wdenk5d3207d2002-08-21 22:08:56 +0000221 printf ("Virtex-II\n");
222 break;
Michal Simekd5dae852013-04-22 15:43:02 +0200223 case xilinx_zynq:
224 printf("Zynq PL\n");
225 break;
Siva Durga Prasad Paladugu6b245012016-01-13 16:25:37 +0530226 case xilinx_zynqmp:
227 printf("ZynqMP PL\n");
228 break;
Siva Durga Prasad Paladugu26e054c2019-08-05 15:54:59 +0530229 case xilinx_versal:
230 printf("Versal PL\n");
231 break;
232 /* Add new family types here */
wdenk5d3207d2002-08-21 22:08:56 +0000233 default:
234 printf ("Unknown family type, %d\n", desc->family);
235 }
236
237 printf ("Interface type:\t");
238 switch (desc->iface) {
239 case slave_serial:
240 printf ("Slave Serial\n");
241 break;
242 case master_serial: /* Not used */
243 printf ("Master Serial\n");
244 break;
245 case slave_parallel:
246 printf ("Slave Parallel\n");
247 break;
248 case jtag_mode: /* Not used */
249 printf ("JTAG Mode\n");
250 break;
251 case slave_selectmap:
252 printf ("Slave SelectMap Mode\n");
253 break;
254 case master_selectmap:
255 printf ("Master SelectMap Mode\n");
256 break;
Michal Simekd5dae852013-04-22 15:43:02 +0200257 case devcfg:
258 printf("Device configuration interface (Zynq)\n");
259 break;
Siva Durga Prasad Paladugu6b245012016-01-13 16:25:37 +0530260 case csu_dma:
261 printf("csu_dma configuration interface (ZynqMP)\n");
262 break;
Siva Durga Prasad Paladugu26e054c2019-08-05 15:54:59 +0530263 case cfi:
264 printf("CFI configuration interface (Versal)\n");
265 break;
wdenk5d3207d2002-08-21 22:08:56 +0000266 /* Add new interface types here */
267 default:
268 printf ("Unsupported interface type, %d\n", desc->iface);
269 }
270
Simon Glassddc94372014-06-07 22:07:58 -0600271 printf("Device Size: \t%zd bytes\n"
272 "Cookie: \t0x%x (%d)\n",
273 desc->size, desc->cookie, desc->cookie);
Michal Simek6631db42013-04-26 15:04:48 +0200274 if (desc->name)
275 printf("Device name: \t%s\n", desc->name);
wdenk5d3207d2002-08-21 22:08:56 +0000276
Michal Simeke136eae2014-07-16 10:36:42 +0200277 if (desc->iface_fns)
wdenk5d3207d2002-08-21 22:08:56 +0000278 printf ("Device Function Table @ 0x%p\n", desc->iface_fns);
Michal Simeke136eae2014-07-16 10:36:42 +0200279 else
wdenk5d3207d2002-08-21 22:08:56 +0000280 printf ("No Device Function Table.\n");
281
Michal Simeke136eae2014-07-16 10:36:42 +0200282 if (desc->operations && desc->operations->info)
283 desc->operations->info(desc);
284
wdenk5d3207d2002-08-21 22:08:56 +0000285 ret_val = FPGA_SUCCESS;
286 } else {
287 printf ("%s: Invalid device descriptor\n", __FUNCTION__);
288 }
289
290 return ret_val;
291}
292
wdenk5d3207d2002-08-21 22:08:56 +0000293/* ------------------------------------------------------------------------- */
294
Michal Simekf8c1be92014-03-13 12:49:21 +0100295static int xilinx_validate(xilinx_desc *desc, char *fn)
wdenk5d3207d2002-08-21 22:08:56 +0000296{
York Sun472d5462013-04-01 11:29:11 -0700297 int ret_val = false;
wdenk5d3207d2002-08-21 22:08:56 +0000298
299 if (desc) {
300 if ((desc->family > min_xilinx_type) &&
301 (desc->family < max_xilinx_type)) {
302 if ((desc->iface > min_xilinx_iface_type) &&
303 (desc->iface < max_xilinx_iface_type)) {
304 if (desc->size) {
York Sun472d5462013-04-01 11:29:11 -0700305 ret_val = true;
wdenk5d3207d2002-08-21 22:08:56 +0000306 } else
307 printf ("%s: NULL part size\n", fn);
308 } else
309 printf ("%s: Invalid Interface type, %d\n",
310 fn, desc->iface);
311 } else
312 printf ("%s: Invalid family type, %d\n", fn, desc->family);
313 } else
314 printf ("%s: NULL descriptor!\n", fn);
315
316 return ret_val;
317}