1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2012-2013, Xilinx, Michal Simek
14 #include <asm/cache.h>
18 #include <linux/delay.h>
19 #include <linux/sizes.h>
20 #include <asm/arch/hardware.h>
21 #include <asm/arch/sys_proto.h>
23 #define DEVCFG_CTRL_PCFG_PROG_B 0x40000000
24 #define DEVCFG_CTRL_PCFG_AES_EFUSE_MASK 0x00001000
25 #define DEVCFG_CTRL_PCAP_RATE_EN_MASK 0x02000000
26 #define DEVCFG_CTRL_PCFG_AES_EN_MASK 0x00000E00
27 #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
36 #define DEVCFG_MCTRL_PCAP_LPBK 0x00000010
37 #define DEVCFG_MCTRL_RFIFO_FLUSH 0x00000002
38 #define DEVCFG_MCTRL_WFIFO_FLUSH 0x00000001
40 #ifndef CFG_SYS_FPGA_WAIT
41 #define CFG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
44 #ifndef CFG_SYS_FPGA_PROG_TIME
45 #define CFG_SYS_FPGA_PROG_TIME (CONFIG_SYS_HZ * 4) /* 4 s */
48 #define DUMMY_WORD 0xffffffff
50 /* Xilinx binary format header */
51 static const u32 bin_format[] = {
52 DUMMY_WORD, /* Dummy words */
60 0x000000bb, /* Sync word */
61 0x11220044, /* Sync word */
64 0xaa995566, /* Sync word */
71 * Load the whole word from unaligned buffer
72 * Keep in your mind that it is byte loading on little-endian system
74 static u32 load_word(const void *buf, u32 swap)
80 if (swap == SWAP_NO) {
81 for (p = 0; p < 4; p++) {
86 for (p = 3; p >= 0; p--) {
95 static u32 check_header(const void *buf)
99 u32 *test = (u32 *)buf;
101 debug("%s: Let's check bitstream header\n", __func__);
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);
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
113 if ((__swab32(pattern) != DUMMY_WORD) &&
114 (__swab32(pattern) == bin_format[i])) {
115 pattern = __swab32(pattern);
117 debug("%s: data swapped - let's swap\n", __func__);
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__);
127 debug("%s: Found bitstream header at %x %s swapinng\n", __func__,
128 (u32)buf, swap == SWAP_NO ? "without" : "with");
133 static void *check_data(u8 *buf, size_t bsize, u32 *swap)
135 u32 word, p = 0; /* possition */
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]);
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]);
148 /* FIXME add full bitstream checking here */
152 /* Loop can be huge - support CTRL + C */
159 static int zynq_dma_transfer(u32 srcbuf, u32 srclen, u32 dstbuf, u32 dstlen)
164 /* 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);
170 isr_status = readl(&devcfg_base->int_sts);
172 /* Polling the PCAP_INIT status for Set */
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__,
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));
185 if (get_timer(ts) > CFG_SYS_FPGA_PROG_TIME) {
186 printf("%s: Timeout wait for DMA to complete\n",
190 isr_status = readl(&devcfg_base->int_sts);
193 debug("%s: DMA transfer is done\n", __func__);
195 /* Clear out the DMA status */
196 writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
201 static int zynq_dma_xfer_init(bitstream_type bstype)
203 u32 status, control, isr_status;
206 /* Clear loopback bit */
207 clrbits_le32(&devcfg_base->mctrl, DEVCFG_MCTRL_PCAP_LPBK);
209 if (bstype != BIT_PARTIAL && bstype != BIT_NONE) {
210 zynq_slcr_devcfg_disable();
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);
217 * Delay is required if AES efuse is selected as
220 if (control & DEVCFG_CTRL_PCFG_AES_EFUSE_MASK)
223 /* Setting PCFG_PROG_B signal to low */
224 writel(control & ~DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
227 * Delay is required if AES efuse is selected as
230 if (control & DEVCFG_CTRL_PCFG_AES_EFUSE_MASK)
233 /* Polling the PCAP_INIT status for Reset */
235 while (readl(&devcfg_base->status) & DEVCFG_STATUS_PCFG_INIT) {
236 if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
237 printf("%s: Timeout wait for INIT to clear\n",
243 /* Setting PCFG_PROG_B signal to high */
244 writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
246 /* Polling the PCAP_INIT status for Set */
248 while (!(readl(&devcfg_base->status) &
249 DEVCFG_STATUS_PCFG_INIT)) {
250 if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
251 printf("%s: Timeout wait for INIT to set\n",
258 isr_status = readl(&devcfg_base->int_sts);
260 /* Clear it all, so if Boot ROM comes back, it can proceed */
261 writel(0xFFFFFFFF, &devcfg_base->int_sts);
263 if (isr_status & DEVCFG_ISR_FATAL_ERROR_MASK) {
264 debug("%s: Fatal errors in PCAP 0x%X\n", __func__, isr_status);
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);
274 status = readl(&devcfg_base->status);
276 debug("%s: Status = 0x%08X\n", __func__, status);
278 if (status & DEVCFG_STATUS_DMA_CMD_Q_F) {
279 debug("%s: Error: device busy\n", __func__);
283 debug("%s: Device ready\n", __func__);
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__);
291 /* Clear out the status */
292 writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
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);
304 static u32 *zynq_align_dma_buffer(u32 *buf, u32 len, u32 swap)
309 if ((u32)buf != ALIGN((u32)buf, ARCH_DMA_MINALIGN)) {
310 new_buf = (u32 *)ALIGN((u32)buf, ARCH_DMA_MINALIGN);
313 * This might be dangerous but permits to flash if
314 * ARCH_DMA_MINALIGN is greater than header size
317 debug("%s: Aligned buffer is after buffer start\n",
319 new_buf = (u32 *)((u32)new_buf - ARCH_DMA_MINALIGN);
321 printf("%s: Align buffer at %x to %x(swap %d)\n", __func__,
322 (u32)buf, (u32)new_buf, swap);
324 for (i = 0; i < (len/4); i++)
325 new_buf[i] = load_word(&buf[i], swap);
328 } else if (swap != SWAP_DONE) {
329 /* For bitstream which are aligned */
330 u32 *new_buf = (u32 *)buf;
332 printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
335 for (i = 0; i < (len/4); i++)
336 new_buf[i] = load_word(&buf[i], swap);
342 static int zynq_validate_bitstream(xilinx_desc *desc, const void *buf,
343 size_t bsize, u32 blocksize, u32 *swap,
344 bitstream_type *bstype)
349 buf_start = check_data((u8 *)buf, blocksize, swap);
354 /* Check if data is postpone from start */
355 diff = (u32)buf_start - (u32)buf;
357 printf("%s: Bitstream is not validated yet (diff %x)\n",
362 if ((u32)buf < SZ_1M) {
363 printf("%s: Bitstream has to be placed up to 1MB (%x)\n",
368 if (zynq_dma_xfer_init(*bstype))
374 static int zynq_load(xilinx_desc *desc, const void *buf, size_t bsize,
375 bitstream_type bstype, int flags)
377 unsigned long ts; /* Timestamp */
378 u32 isr_status, swap;
381 * send bsize inplace of blocksize as it was not a bitstream
384 if (zynq_validate_bitstream(desc, buf, bsize, bsize, &swap,
388 buf = zynq_align_dma_buffer((u32 *)buf, bsize, swap);
390 debug("%s: Source = 0x%08X\n", __func__, (u32)buf);
391 debug("%s: Size = %zu\n", __func__, bsize);
393 /* flush(clean & invalidate) d-cache range buf */
394 flush_dcache_range((u32)buf, (u32)buf +
395 roundup(bsize, ARCH_DMA_MINALIGN));
397 if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
400 isr_status = readl(&devcfg_base->int_sts);
401 /* Check FPGA configuration completion */
403 while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
404 if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
405 printf("%s: Timeout wait for FPGA to config\n",
409 isr_status = readl(&devcfg_base->int_sts);
412 debug("%s: FPGA config done\n", __func__);
414 if (bstype != BIT_PARTIAL)
415 zynq_slcr_devcfg_enable();
417 if (!IS_ENABLED(CONFIG_XPL_BUILD))
418 puts("INFO:post config was not run, please run manually if needed\n");
423 #if defined(CONFIG_CMD_FPGA_LOADFS) && !defined(CONFIG_XPL_BUILD)
424 static int zynq_loadfs(xilinx_desc *desc, const void *buf, size_t bsize,
425 fpga_fs_info *fsinfo)
427 unsigned long ts; /* Timestamp */
428 u32 isr_status, swap;
430 loff_t blocksize, actread;
433 char *interface, *dev_part;
434 const char *filename;
436 blocksize = fsinfo->blocksize;
437 interface = fsinfo->interface;
438 dev_part = fsinfo->dev_part;
439 filename = fsinfo->filename;
440 fstype = fsinfo->fstype;
442 if (fs_set_blk_dev(interface, dev_part, fstype))
445 if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
448 if (zynq_validate_bitstream(desc, buf, bsize, blocksize, &swap,
455 buf = zynq_align_dma_buffer((u32 *)buf, blocksize, swap);
457 if (zynq_dma_transfer((u32)buf | 1, blocksize >> 2,
464 if (fs_set_blk_dev(interface, dev_part, fstype))
467 if (bsize > blocksize) {
468 if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
471 if (fs_read(filename, (u32) buf, pos, bsize, &actread) < 0)
474 } while (bsize > blocksize);
476 buf = zynq_align_dma_buffer((u32 *)buf, blocksize, swap);
478 if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
483 isr_status = readl(&devcfg_base->int_sts);
485 /* Check FPGA configuration completion */
487 while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
488 if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
489 printf("%s: Timeout wait for FPGA to config\n",
493 isr_status = readl(&devcfg_base->int_sts);
496 debug("%s: FPGA config done\n", __func__);
499 zynq_slcr_devcfg_enable();
505 struct xilinx_fpga_op zynq_op = {
507 #if defined(CONFIG_CMD_FPGA_LOADFS) && !defined(CONFIG_XPL_BUILD)
508 .loadfs = zynq_loadfs,
512 #ifdef CONFIG_CMD_ZYNQ_AES
514 * Load the encrypted image from src addr and decrypt the image and
515 * place it back the decrypted image into dstaddr.
517 int zynq_decrypt_load(u32 srcaddr, u32 srclen, u32 dstaddr, u32 dstlen,
522 if (srcaddr < SZ_1M || dstaddr < SZ_1M) {
523 printf("%s: src and dst addr should be > 1M\n",
528 /* 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__);
535 if (zynq_dma_xfer_init(bstype)) {
536 printf("%s: zynq_dma_xfer_init FAIL\n", __func__);
540 writel((readl(&devcfg_base->ctrl) | DEVCFG_CTRL_PCAP_RATE_EN_MASK),
543 debug("%s: Source = 0x%08X\n", __func__, (u32)srcaddr);
544 debug("%s: Size = %zu\n", __func__, srclen);
546 /* flush(clean & invalidate) d-cache range buf */
547 flush_dcache_range((u32)srcaddr, (u32)srcaddr +
548 roundup(srclen << 2, ARCH_DMA_MINALIGN));
550 * Flush destination address range only if image is not
553 if (bstype == BIT_NONE && dstaddr != 0xFFFFFFFF)
554 flush_dcache_range((u32)dstaddr, (u32)dstaddr +
555 roundup(dstlen << 2, ARCH_DMA_MINALIGN));
557 if (zynq_dma_transfer(srcaddr | 1, srclen, dstaddr | 1, dstlen))
560 if (bstype == BIT_FULL) {
561 isr_status = readl(&devcfg_base->int_sts);
562 /* Check FPGA configuration completion */
564 while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
565 if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
566 printf("%s: Timeout wait for FPGA to config\n",
570 isr_status = readl(&devcfg_base->int_sts);
572 printf("%s: FPGA config done\n", __func__);
573 zynq_slcr_devcfg_enable();