blob: 57467b4d975aac8ecccdc828b6307243c6ca3af3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek15d654c2013-04-22 15:43:02 +02002/*
3 * (C) Copyright 2012-2013, Xilinx, Michal Simek
4 *
5 * (C) Copyright 2012
6 * Joe Hershberger <joe.hershberger@ni.com>
Michal Simek15d654c2013-04-22 15:43:02 +02007 */
8
Tom Rinidec7ea02024-05-20 13:35:03 -06009#include <config.h>
Simon Glassa73bda42015-11-08 23:47:45 -070010#include <console.h>
Simon Glass1d91ba72019-11-14 12:57:37 -070011#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060013#include <time.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Michal Simek15d654c2013-04-22 15:43:02 +020015#include <asm/io.h>
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +053016#include <fs.h>
Michal Simek15d654c2013-04-22 15:43:02 +020017#include <zynqpl.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Alexey Brodkin267d8e22014-02-26 17:47:58 +040019#include <linux/sizes.h>
Michal Simek15d654c2013-04-22 15:43:02 +020020#include <asm/arch/hardware.h>
21#include <asm/arch/sys_proto.h>
22
23#define DEVCFG_CTRL_PCFG_PROG_B 0x40000000
Siva Durga Prasad Paladugueac0cd12018-03-06 17:37:09 +053024#define DEVCFG_CTRL_PCFG_AES_EFUSE_MASK 0x00001000
Siva Durga Prasad Paladugue4603522018-06-26 15:02:19 +053025#define DEVCFG_CTRL_PCAP_RATE_EN_MASK 0x02000000
Ibai Erkiaga59d9ab72018-04-05 05:19:27 -070026#define DEVCFG_CTRL_PCFG_AES_EN_MASK 0x00000E00
Michal Simek15d654c2013-04-22 15:43:02 +020027#define DEVCFG_ISR_FATAL_ERROR_MASK 0x00740040
28#define DEVCFG_ISR_ERROR_FLAGS_MASK 0x00340840
29#define DEVCFG_ISR_RX_FIFO_OV 0x00040000
30#define DEVCFG_ISR_DMA_DONE 0x00002000
31#define DEVCFG_ISR_PCFG_DONE 0x00000004
32#define DEVCFG_STATUS_DMA_CMD_Q_F 0x80000000
33#define DEVCFG_STATUS_DMA_CMD_Q_E 0x40000000
34#define DEVCFG_STATUS_DMA_DONE_CNT_MASK 0x30000000
35#define DEVCFG_STATUS_PCFG_INIT 0x00000010
Soren Brinkmann55775852013-06-14 17:43:24 -070036#define DEVCFG_MCTRL_PCAP_LPBK 0x00000010
Michal Simek15d654c2013-04-22 15:43:02 +020037#define DEVCFG_MCTRL_RFIFO_FLUSH 0x00000002
38#define DEVCFG_MCTRL_WFIFO_FLUSH 0x00000001
39
Tom Rini6a5dccc2022-11-16 13:10:41 -050040#ifndef CFG_SYS_FPGA_WAIT
41#define CFG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
Michal Simek15d654c2013-04-22 15:43:02 +020042#endif
43
Tom Rini364d0022023-01-10 11:19:45 -050044#ifndef CFG_SYS_FPGA_PROG_TIME
45#define CFG_SYS_FPGA_PROG_TIME (CONFIG_SYS_HZ * 4) /* 4 s */
Michal Simek15d654c2013-04-22 15:43:02 +020046#endif
47
Michal Simek15d654c2013-04-22 15:43:02 +020048#define DUMMY_WORD 0xffffffff
49
50/* Xilinx binary format header */
51static const u32 bin_format[] = {
52 DUMMY_WORD, /* Dummy words */
53 DUMMY_WORD,
54 DUMMY_WORD,
55 DUMMY_WORD,
56 DUMMY_WORD,
57 DUMMY_WORD,
58 DUMMY_WORD,
59 DUMMY_WORD,
60 0x000000bb, /* Sync word */
61 0x11220044, /* Sync word */
62 DUMMY_WORD,
63 DUMMY_WORD,
64 0xaa995566, /* Sync word */
65};
66
67#define SWAP_NO 1
68#define SWAP_DONE 2
69
70/*
71 * Load the whole word from unaligned buffer
72 * Keep in your mind that it is byte loading on little-endian system
73 */
74static u32 load_word(const void *buf, u32 swap)
75{
76 u32 word = 0;
77 u8 *bitc = (u8 *)buf;
78 int p;
79
80 if (swap == SWAP_NO) {
81 for (p = 0; p < 4; p++) {
82 word <<= 8;
83 word |= bitc[p];
84 }
85 } else {
86 for (p = 3; p >= 0; p--) {
87 word <<= 8;
88 word |= bitc[p];
89 }
90 }
91
92 return word;
93}
94
95static u32 check_header(const void *buf)
96{
97 u32 i, pattern;
98 int swap = SWAP_NO;
99 u32 *test = (u32 *)buf;
100
101 debug("%s: Let's check bitstream header\n", __func__);
102
103 /* Checking that passing bin is not a bitstream */
104 for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
105 pattern = load_word(&test[i], swap);
106
107 /*
108 * Bitstreams in binary format are swapped
109 * compare to regular bistream.
110 * Do not swap dummy word but if swap is done assume
111 * that parsing buffer is binary format
112 */
113 if ((__swab32(pattern) != DUMMY_WORD) &&
114 (__swab32(pattern) == bin_format[i])) {
115 pattern = __swab32(pattern);
116 swap = SWAP_DONE;
117 debug("%s: data swapped - let's swap\n", __func__);
118 }
119
120 debug("%s: %d/%x: pattern %x/%x bin_format\n", __func__, i,
121 (u32)&test[i], pattern, bin_format[i]);
122 if (pattern != bin_format[i]) {
123 debug("%s: Bitstream is not recognized\n", __func__);
124 return 0;
125 }
126 }
127 debug("%s: Found bitstream header at %x %s swapinng\n", __func__,
128 (u32)buf, swap == SWAP_NO ? "without" : "with");
129
130 return swap;
131}
132
133static void *check_data(u8 *buf, size_t bsize, u32 *swap)
134{
135 u32 word, p = 0; /* possition */
136
137 /* Because buf doesn't need to be aligned let's read it by chars */
138 for (p = 0; p < bsize; p++) {
139 word = load_word(&buf[p], SWAP_NO);
140 debug("%s: word %x %x/%x\n", __func__, word, p, (u32)&buf[p]);
141
142 /* Find the first bitstream dummy word */
143 if (word == DUMMY_WORD) {
144 debug("%s: Found dummy word at position %x/%x\n",
145 __func__, p, (u32)&buf[p]);
146 *swap = check_header(&buf[p]);
147 if (*swap) {
148 /* FIXME add full bitstream checking here */
149 return &buf[p];
150 }
151 }
152 /* Loop can be huge - support CTRL + C */
153 if (ctrlc())
Michal Simek15f156a2014-04-25 13:51:58 +0200154 return NULL;
Michal Simek15d654c2013-04-22 15:43:02 +0200155 }
Michal Simek15f156a2014-04-25 13:51:58 +0200156 return NULL;
Michal Simek15d654c2013-04-22 15:43:02 +0200157}
158
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530159static int zynq_dma_transfer(u32 srcbuf, u32 srclen, u32 dstbuf, u32 dstlen)
Michal Simek15d654c2013-04-22 15:43:02 +0200160{
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530161 unsigned long ts;
162 u32 isr_status;
Michal Simek15d654c2013-04-22 15:43:02 +0200163
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530164 /* Set up the transfer */
165 writel((u32)srcbuf, &devcfg_base->dma_src_addr);
166 writel(dstbuf, &devcfg_base->dma_dst_addr);
167 writel(srclen, &devcfg_base->dma_src_len);
168 writel(dstlen, &devcfg_base->dma_dst_len);
Michal Simek15d654c2013-04-22 15:43:02 +0200169
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530170 isr_status = readl(&devcfg_base->int_sts);
Michal Simek94dc92f2013-10-04 10:48:59 +0200171
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530172 /* Polling the PCAP_INIT status for Set */
173 ts = get_timer(0);
174 while (!(isr_status & DEVCFG_ISR_DMA_DONE)) {
175 if (isr_status & DEVCFG_ISR_ERROR_FLAGS_MASK) {
176 debug("%s: Error: isr = 0x%08X\n", __func__,
177 isr_status);
178 debug("%s: Write count = 0x%08X\n", __func__,
179 readl(&devcfg_base->write_count));
180 debug("%s: Read count = 0x%08X\n", __func__,
181 readl(&devcfg_base->read_count));
Michal Simek15d654c2013-04-22 15:43:02 +0200182
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530183 return FPGA_FAIL;
Novasys Ingenieriecbe4b092013-11-27 09:03:01 +0100184 }
Tom Rini364d0022023-01-10 11:19:45 -0500185 if (get_timer(ts) > CFG_SYS_FPGA_PROG_TIME) {
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530186 printf("%s: Timeout wait for DMA to complete\n",
187 __func__);
188 return FPGA_FAIL;
189 }
190 isr_status = readl(&devcfg_base->int_sts);
191 }
Michal Simek15d654c2013-04-22 15:43:02 +0200192
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530193 debug("%s: DMA transfer is done\n", __func__);
Michal Simek15d654c2013-04-22 15:43:02 +0200194
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530195 /* Clear out the DMA status */
196 writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
Michal Simek15d654c2013-04-22 15:43:02 +0200197
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530198 return FPGA_SUCCESS;
199}
Michal Simek15d654c2013-04-22 15:43:02 +0200200
Michal Simekf46ccf42014-05-02 14:15:27 +0200201static int zynq_dma_xfer_init(bitstream_type bstype)
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530202{
203 u32 status, control, isr_status;
204 unsigned long ts;
Michal Simek15d654c2013-04-22 15:43:02 +0200205
Soren Brinkmann55775852013-06-14 17:43:24 -0700206 /* Clear loopback bit */
207 clrbits_le32(&devcfg_base->mctrl, DEVCFG_MCTRL_PCAP_LPBK);
208
Siva Durga Prasad Paladuguc5750582015-12-09 18:46:43 +0530209 if (bstype != BIT_PARTIAL && bstype != BIT_NONE) {
Michal Simek15d654c2013-04-22 15:43:02 +0200210 zynq_slcr_devcfg_disable();
211
212 /* Setting PCFG_PROG_B signal to high */
213 control = readl(&devcfg_base->ctrl);
214 writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
Siva Durga Prasad Paladugueac0cd12018-03-06 17:37:09 +0530215
216 /*
217 * Delay is required if AES efuse is selected as
218 * key source.
219 */
220 if (control & DEVCFG_CTRL_PCFG_AES_EFUSE_MASK)
221 mdelay(5);
222
Michal Simek15d654c2013-04-22 15:43:02 +0200223 /* Setting PCFG_PROG_B signal to low */
224 writel(control & ~DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
225
Siva Durga Prasad Paladugueac0cd12018-03-06 17:37:09 +0530226 /*
227 * Delay is required if AES efuse is selected as
228 * key source.
229 */
230 if (control & DEVCFG_CTRL_PCFG_AES_EFUSE_MASK)
231 mdelay(5);
232
Michal Simek15d654c2013-04-22 15:43:02 +0200233 /* Polling the PCAP_INIT status for Reset */
234 ts = get_timer(0);
235 while (readl(&devcfg_base->status) & DEVCFG_STATUS_PCFG_INIT) {
Tom Rini6a5dccc2022-11-16 13:10:41 -0500236 if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
Michal Simek15d654c2013-04-22 15:43:02 +0200237 printf("%s: Timeout wait for INIT to clear\n",
238 __func__);
239 return FPGA_FAIL;
240 }
241 }
242
243 /* Setting PCFG_PROG_B signal to high */
244 writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
245
246 /* Polling the PCAP_INIT status for Set */
247 ts = get_timer(0);
248 while (!(readl(&devcfg_base->status) &
249 DEVCFG_STATUS_PCFG_INIT)) {
Tom Rini6a5dccc2022-11-16 13:10:41 -0500250 if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
Michal Simek15d654c2013-04-22 15:43:02 +0200251 printf("%s: Timeout wait for INIT to set\n",
252 __func__);
253 return FPGA_FAIL;
254 }
255 }
256 }
257
258 isr_status = readl(&devcfg_base->int_sts);
259
260 /* Clear it all, so if Boot ROM comes back, it can proceed */
261 writel(0xFFFFFFFF, &devcfg_base->int_sts);
262
263 if (isr_status & DEVCFG_ISR_FATAL_ERROR_MASK) {
264 debug("%s: Fatal errors in PCAP 0x%X\n", __func__, isr_status);
265
266 /* If RX FIFO overflow, need to flush RX FIFO first */
267 if (isr_status & DEVCFG_ISR_RX_FIFO_OV) {
268 writel(DEVCFG_MCTRL_RFIFO_FLUSH, &devcfg_base->mctrl);
269 writel(0xFFFFFFFF, &devcfg_base->int_sts);
270 }
271 return FPGA_FAIL;
272 }
273
274 status = readl(&devcfg_base->status);
275
276 debug("%s: Status = 0x%08X\n", __func__, status);
277
278 if (status & DEVCFG_STATUS_DMA_CMD_Q_F) {
279 debug("%s: Error: device busy\n", __func__);
280 return FPGA_FAIL;
281 }
282
283 debug("%s: Device ready\n", __func__);
284
285 if (!(status & DEVCFG_STATUS_DMA_CMD_Q_E)) {
286 if (!(readl(&devcfg_base->int_sts) & DEVCFG_ISR_DMA_DONE)) {
287 /* Error state, transfer cannot occur */
288 debug("%s: ISR indicates error\n", __func__);
289 return FPGA_FAIL;
290 } else {
291 /* Clear out the status */
292 writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
293 }
294 }
295
296 if (status & DEVCFG_STATUS_DMA_DONE_CNT_MASK) {
297 /* Clear the count of completed DMA transfers */
298 writel(DEVCFG_STATUS_DMA_DONE_CNT_MASK, &devcfg_base->status);
299 }
300
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530301 return FPGA_SUCCESS;
302}
303
304static u32 *zynq_align_dma_buffer(u32 *buf, u32 len, u32 swap)
305{
306 u32 *new_buf;
307 u32 i;
308
309 if ((u32)buf != ALIGN((u32)buf, ARCH_DMA_MINALIGN)) {
310 new_buf = (u32 *)ALIGN((u32)buf, ARCH_DMA_MINALIGN);
311
312 /*
313 * This might be dangerous but permits to flash if
314 * ARCH_DMA_MINALIGN is greater than header size
315 */
316 if (new_buf > buf) {
317 debug("%s: Aligned buffer is after buffer start\n",
318 __func__);
Michael Walle36eca7c2021-02-10 22:42:29 +0100319 new_buf = (u32 *)((u32)new_buf - ARCH_DMA_MINALIGN);
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530320 }
321 printf("%s: Align buffer at %x to %x(swap %d)\n", __func__,
322 (u32)buf, (u32)new_buf, swap);
323
324 for (i = 0; i < (len/4); i++)
325 new_buf[i] = load_word(&buf[i], swap);
326
327 buf = new_buf;
328 } else if (swap != SWAP_DONE) {
329 /* For bitstream which are aligned */
330 u32 *new_buf = (u32 *)buf;
331
332 printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
333 swap);
334
335 for (i = 0; i < (len/4); i++)
336 new_buf[i] = load_word(&buf[i], swap);
337 }
338
339 return buf;
340}
341
Siva Durga Prasad Paladugu7857c722014-03-13 11:57:34 +0530342static int zynq_validate_bitstream(xilinx_desc *desc, const void *buf,
343 size_t bsize, u32 blocksize, u32 *swap,
Michal Simekf46ccf42014-05-02 14:15:27 +0200344 bitstream_type *bstype)
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530345{
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530346 u32 *buf_start;
Siva Durga Prasad Paladugu7857c722014-03-13 11:57:34 +0530347 u32 diff;
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530348
Siva Durga Prasad Paladugu7857c722014-03-13 11:57:34 +0530349 buf_start = check_data((u8 *)buf, blocksize, swap);
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530350
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530351 if (!buf_start)
352 return FPGA_FAIL;
353
354 /* Check if data is postpone from start */
355 diff = (u32)buf_start - (u32)buf;
356 if (diff) {
357 printf("%s: Bitstream is not validated yet (diff %x)\n",
358 __func__, diff);
359 return FPGA_FAIL;
360 }
361
362 if ((u32)buf < SZ_1M) {
363 printf("%s: Bitstream has to be placed up to 1MB (%x)\n",
364 __func__, (u32)buf);
365 return FPGA_FAIL;
366 }
367
Michal Simekf46ccf42014-05-02 14:15:27 +0200368 if (zynq_dma_xfer_init(*bstype))
Siva Durga Prasad Paladugu7857c722014-03-13 11:57:34 +0530369 return FPGA_FAIL;
370
371 return 0;
372}
373
Michal Simek14663652014-05-02 14:09:30 +0200374static int zynq_load(xilinx_desc *desc, const void *buf, size_t bsize,
Oleksandr Suvorovc0806cc2022-07-22 17:16:10 +0300375 bitstream_type bstype, int flags)
Siva Durga Prasad Paladugu7857c722014-03-13 11:57:34 +0530376{
377 unsigned long ts; /* Timestamp */
Siva Durga Prasad Paladugu7857c722014-03-13 11:57:34 +0530378 u32 isr_status, swap;
379
380 /*
381 * send bsize inplace of blocksize as it was not a bitstream
382 * in chunks
383 */
384 if (zynq_validate_bitstream(desc, buf, bsize, bsize, &swap,
Michal Simekf46ccf42014-05-02 14:15:27 +0200385 &bstype))
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530386 return FPGA_FAIL;
387
388 buf = zynq_align_dma_buffer((u32 *)buf, bsize, swap);
389
Michal Simek15d654c2013-04-22 15:43:02 +0200390 debug("%s: Source = 0x%08X\n", __func__, (u32)buf);
391 debug("%s: Size = %zu\n", __func__, bsize);
392
Jagannadha Sutradharudu Teki8cfb2462013-09-20 18:39:47 +0530393 /* flush(clean & invalidate) d-cache range buf */
394 flush_dcache_range((u32)buf, (u32)buf +
395 roundup(bsize, ARCH_DMA_MINALIGN));
396
Siva Durga Prasad Paladugu2f726182014-03-12 17:09:26 +0530397 if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
398 return FPGA_FAIL;
Michal Simek15d654c2013-04-22 15:43:02 +0200399
400 isr_status = readl(&devcfg_base->int_sts);
Michal Simek15d654c2013-04-22 15:43:02 +0200401 /* Check FPGA configuration completion */
402 ts = get_timer(0);
403 while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
Tom Rini6a5dccc2022-11-16 13:10:41 -0500404 if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
Michal Simek15d654c2013-04-22 15:43:02 +0200405 printf("%s: Timeout wait for FPGA to config\n",
406 __func__);
407 return FPGA_FAIL;
408 }
409 isr_status = readl(&devcfg_base->int_sts);
410 }
411
412 debug("%s: FPGA config done\n", __func__);
413
Michal Simekf46ccf42014-05-02 14:15:27 +0200414 if (bstype != BIT_PARTIAL)
Michal Simek15d654c2013-04-22 15:43:02 +0200415 zynq_slcr_devcfg_enable();
416
Stefan Herbrechtsmeierfb7fcc22022-08-08 16:53:31 +0200417 if (!IS_ENABLED(CONFIG_SPL_BUILD))
418 puts("INFO:post config was not run, please run manually if needed\n");
Siva Durga Prasad Paladugu9bf652d2019-03-23 16:01:36 +0530419
Michal Simek15d654c2013-04-22 15:43:02 +0200420 return FPGA_SUCCESS;
421}
422
Luis Araneda43976022018-07-19 03:10:17 -0400423#if defined(CONFIG_CMD_FPGA_LOADFS) && !defined(CONFIG_SPL_BUILD)
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530424static int zynq_loadfs(xilinx_desc *desc, const void *buf, size_t bsize,
425 fpga_fs_info *fsinfo)
426{
427 unsigned long ts; /* Timestamp */
428 u32 isr_status, swap;
429 u32 partialbit = 0;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800430 loff_t blocksize, actread;
431 loff_t pos = 0;
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530432 int fstype;
Tien Fong Chee3b45f6b2019-02-15 15:57:07 +0800433 char *interface, *dev_part;
434 const char *filename;
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530435
436 blocksize = fsinfo->blocksize;
437 interface = fsinfo->interface;
438 dev_part = fsinfo->dev_part;
439 filename = fsinfo->filename;
440 fstype = fsinfo->fstype;
441
442 if (fs_set_blk_dev(interface, dev_part, fstype))
443 return FPGA_FAIL;
444
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800445 if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530446 return FPGA_FAIL;
447
448 if (zynq_validate_bitstream(desc, buf, bsize, blocksize, &swap,
449 &partialbit))
450 return FPGA_FAIL;
451
452 dcache_disable();
453
454 do {
455 buf = zynq_align_dma_buffer((u32 *)buf, blocksize, swap);
456
457 if (zynq_dma_transfer((u32)buf | 1, blocksize >> 2,
458 0xffffffff, 0))
459 return FPGA_FAIL;
460
461 bsize -= blocksize;
462 pos += blocksize;
463
464 if (fs_set_blk_dev(interface, dev_part, fstype))
465 return FPGA_FAIL;
466
467 if (bsize > blocksize) {
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800468 if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530469 return FPGA_FAIL;
470 } else {
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800471 if (fs_read(filename, (u32) buf, pos, bsize, &actread) < 0)
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530472 return FPGA_FAIL;
473 }
474 } while (bsize > blocksize);
475
476 buf = zynq_align_dma_buffer((u32 *)buf, blocksize, swap);
477
478 if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
479 return FPGA_FAIL;
480
481 dcache_enable();
482
483 isr_status = readl(&devcfg_base->int_sts);
484
485 /* Check FPGA configuration completion */
486 ts = get_timer(0);
487 while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
Tom Rini6a5dccc2022-11-16 13:10:41 -0500488 if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530489 printf("%s: Timeout wait for FPGA to config\n",
490 __func__);
491 return FPGA_FAIL;
492 }
493 isr_status = readl(&devcfg_base->int_sts);
494 }
495
496 debug("%s: FPGA config done\n", __func__);
497
498 if (!partialbit)
499 zynq_slcr_devcfg_enable();
500
501 return FPGA_SUCCESS;
502}
503#endif
504
Michal Simek75fafac2014-03-13 13:07:57 +0100505struct xilinx_fpga_op zynq_op = {
506 .load = zynq_load,
Luis Araneda43976022018-07-19 03:10:17 -0400507#if defined(CONFIG_CMD_FPGA_LOADFS) && !defined(CONFIG_SPL_BUILD)
Siva Durga Prasad Paladugu9112b4c2014-03-14 16:35:37 +0530508 .loadfs = zynq_loadfs,
509#endif
Michal Simek75fafac2014-03-13 13:07:57 +0100510};
Siva Durga Prasad Paladugue4603522018-06-26 15:02:19 +0530511
512#ifdef CONFIG_CMD_ZYNQ_AES
513/*
514 * Load the encrypted image from src addr and decrypt the image and
515 * place it back the decrypted image into dstaddr.
516 */
Siva Durga Prasad Paladuguc5750582015-12-09 18:46:43 +0530517int zynq_decrypt_load(u32 srcaddr, u32 srclen, u32 dstaddr, u32 dstlen,
518 u8 bstype)
Siva Durga Prasad Paladugue4603522018-06-26 15:02:19 +0530519{
T Karthik Reddyf3b078f2019-03-12 20:20:20 +0530520 u32 isr_status, ts;
521
Siva Durga Prasad Paladugue4603522018-06-26 15:02:19 +0530522 if (srcaddr < SZ_1M || dstaddr < SZ_1M) {
523 printf("%s: src and dst addr should be > 1M\n",
524 __func__);
525 return FPGA_FAIL;
526 }
527
Ibai Erkiaga59d9ab72018-04-05 05:19:27 -0700528 /* Check AES engine is enabled */
529 if (!(readl(&devcfg_base->ctrl) &
530 DEVCFG_CTRL_PCFG_AES_EN_MASK)) {
531 printf("%s: AES engine is not enabled\n", __func__);
532 return FPGA_FAIL;
533 }
534
Siva Durga Prasad Paladuguc5750582015-12-09 18:46:43 +0530535 if (zynq_dma_xfer_init(bstype)) {
Siva Durga Prasad Paladugue4603522018-06-26 15:02:19 +0530536 printf("%s: zynq_dma_xfer_init FAIL\n", __func__);
537 return FPGA_FAIL;
538 }
539
540 writel((readl(&devcfg_base->ctrl) | DEVCFG_CTRL_PCAP_RATE_EN_MASK),
541 &devcfg_base->ctrl);
542
543 debug("%s: Source = 0x%08X\n", __func__, (u32)srcaddr);
544 debug("%s: Size = %zu\n", __func__, srclen);
545
546 /* flush(clean & invalidate) d-cache range buf */
547 flush_dcache_range((u32)srcaddr, (u32)srcaddr +
548 roundup(srclen << 2, ARCH_DMA_MINALIGN));
549 /*
550 * Flush destination address range only if image is not
551 * bitstream.
552 */
T Karthik Reddy1fa7cbe2019-03-12 20:20:23 +0530553 if (bstype == BIT_NONE && dstaddr != 0xFFFFFFFF)
554 flush_dcache_range((u32)dstaddr, (u32)dstaddr +
555 roundup(dstlen << 2, ARCH_DMA_MINALIGN));
Siva Durga Prasad Paladugue4603522018-06-26 15:02:19 +0530556
557 if (zynq_dma_transfer(srcaddr | 1, srclen, dstaddr | 1, dstlen))
558 return FPGA_FAIL;
559
T Karthik Reddyf3b078f2019-03-12 20:20:20 +0530560 if (bstype == BIT_FULL) {
561 isr_status = readl(&devcfg_base->int_sts);
562 /* Check FPGA configuration completion */
563 ts = get_timer(0);
564 while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
Tom Rini6a5dccc2022-11-16 13:10:41 -0500565 if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
T Karthik Reddyf3b078f2019-03-12 20:20:20 +0530566 printf("%s: Timeout wait for FPGA to config\n",
567 __func__);
568 return FPGA_FAIL;
569 }
570 isr_status = readl(&devcfg_base->int_sts);
571 }
572 printf("%s: FPGA config done\n", __func__);
573 zynq_slcr_devcfg_enable();
574 }
Siva Durga Prasad Paladugue4603522018-06-26 15:02:19 +0530575
576 return FPGA_SUCCESS;
577}
578#endif