1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2012-2013, Xilinx, Michal Simek
6 * Joe Hershberger <joe.hershberger@ni.com>
14 #include <linux/sizes.h>
15 #include <asm/arch/hardware.h>
16 #include <asm/arch/sys_proto.h>
18 #define DEVCFG_CTRL_PCFG_PROG_B 0x40000000
19 #define DEVCFG_CTRL_PCFG_AES_EFUSE_MASK 0x00001000
20 #define DEVCFG_ISR_FATAL_ERROR_MASK 0x00740040
21 #define DEVCFG_ISR_ERROR_FLAGS_MASK 0x00340840
22 #define DEVCFG_ISR_RX_FIFO_OV 0x00040000
23 #define DEVCFG_ISR_DMA_DONE 0x00002000
24 #define DEVCFG_ISR_PCFG_DONE 0x00000004
25 #define DEVCFG_STATUS_DMA_CMD_Q_F 0x80000000
26 #define DEVCFG_STATUS_DMA_CMD_Q_E 0x40000000
27 #define DEVCFG_STATUS_DMA_DONE_CNT_MASK 0x30000000
28 #define DEVCFG_STATUS_PCFG_INIT 0x00000010
29 #define DEVCFG_MCTRL_PCAP_LPBK 0x00000010
30 #define DEVCFG_MCTRL_RFIFO_FLUSH 0x00000002
31 #define DEVCFG_MCTRL_WFIFO_FLUSH 0x00000001
33 #ifndef CONFIG_SYS_FPGA_WAIT
34 #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
37 #ifndef CONFIG_SYS_FPGA_PROG_TIME
38 #define CONFIG_SYS_FPGA_PROG_TIME (CONFIG_SYS_HZ * 4) /* 4 s */
41 #define DUMMY_WORD 0xffffffff
43 /* Xilinx binary format header */
44 static const u32 bin_format[] = {
45 DUMMY_WORD, /* Dummy words */
53 0x000000bb, /* Sync word */
54 0x11220044, /* Sync word */
57 0xaa995566, /* Sync word */
64 * Load the whole word from unaligned buffer
65 * Keep in your mind that it is byte loading on little-endian system
67 static u32 load_word(const void *buf, u32 swap)
73 if (swap == SWAP_NO) {
74 for (p = 0; p < 4; p++) {
79 for (p = 3; p >= 0; p--) {
88 static u32 check_header(const void *buf)
92 u32 *test = (u32 *)buf;
94 debug("%s: Let's check bitstream header\n", __func__);
96 /* Checking that passing bin is not a bitstream */
97 for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
98 pattern = load_word(&test[i], swap);
101 * Bitstreams in binary format are swapped
102 * compare to regular bistream.
103 * Do not swap dummy word but if swap is done assume
104 * that parsing buffer is binary format
106 if ((__swab32(pattern) != DUMMY_WORD) &&
107 (__swab32(pattern) == bin_format[i])) {
108 pattern = __swab32(pattern);
110 debug("%s: data swapped - let's swap\n", __func__);
113 debug("%s: %d/%x: pattern %x/%x bin_format\n", __func__, i,
114 (u32)&test[i], pattern, bin_format[i]);
115 if (pattern != bin_format[i]) {
116 debug("%s: Bitstream is not recognized\n", __func__);
120 debug("%s: Found bitstream header at %x %s swapinng\n", __func__,
121 (u32)buf, swap == SWAP_NO ? "without" : "with");
126 static void *check_data(u8 *buf, size_t bsize, u32 *swap)
128 u32 word, p = 0; /* possition */
130 /* Because buf doesn't need to be aligned let's read it by chars */
131 for (p = 0; p < bsize; p++) {
132 word = load_word(&buf[p], SWAP_NO);
133 debug("%s: word %x %x/%x\n", __func__, word, p, (u32)&buf[p]);
135 /* Find the first bitstream dummy word */
136 if (word == DUMMY_WORD) {
137 debug("%s: Found dummy word at position %x/%x\n",
138 __func__, p, (u32)&buf[p]);
139 *swap = check_header(&buf[p]);
141 /* FIXME add full bitstream checking here */
145 /* Loop can be huge - support CTRL + C */
152 static int zynq_dma_transfer(u32 srcbuf, u32 srclen, u32 dstbuf, u32 dstlen)
157 /* Set up the transfer */
158 writel((u32)srcbuf, &devcfg_base->dma_src_addr);
159 writel(dstbuf, &devcfg_base->dma_dst_addr);
160 writel(srclen, &devcfg_base->dma_src_len);
161 writel(dstlen, &devcfg_base->dma_dst_len);
163 isr_status = readl(&devcfg_base->int_sts);
165 /* Polling the PCAP_INIT status for Set */
167 while (!(isr_status & DEVCFG_ISR_DMA_DONE)) {
168 if (isr_status & DEVCFG_ISR_ERROR_FLAGS_MASK) {
169 debug("%s: Error: isr = 0x%08X\n", __func__,
171 debug("%s: Write count = 0x%08X\n", __func__,
172 readl(&devcfg_base->write_count));
173 debug("%s: Read count = 0x%08X\n", __func__,
174 readl(&devcfg_base->read_count));
178 if (get_timer(ts) > CONFIG_SYS_FPGA_PROG_TIME) {
179 printf("%s: Timeout wait for DMA to complete\n",
183 isr_status = readl(&devcfg_base->int_sts);
186 debug("%s: DMA transfer is done\n", __func__);
188 /* Clear out the DMA status */
189 writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
194 static int zynq_dma_xfer_init(bitstream_type bstype)
196 u32 status, control, isr_status;
199 /* Clear loopback bit */
200 clrbits_le32(&devcfg_base->mctrl, DEVCFG_MCTRL_PCAP_LPBK);
202 if (bstype != BIT_PARTIAL) {
203 zynq_slcr_devcfg_disable();
205 /* Setting PCFG_PROG_B signal to high */
206 control = readl(&devcfg_base->ctrl);
207 writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
210 * Delay is required if AES efuse is selected as
213 if (control & DEVCFG_CTRL_PCFG_AES_EFUSE_MASK)
216 /* Setting PCFG_PROG_B signal to low */
217 writel(control & ~DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
220 * Delay is required if AES efuse is selected as
223 if (control & DEVCFG_CTRL_PCFG_AES_EFUSE_MASK)
226 /* Polling the PCAP_INIT status for Reset */
228 while (readl(&devcfg_base->status) & DEVCFG_STATUS_PCFG_INIT) {
229 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
230 printf("%s: Timeout wait for INIT to clear\n",
236 /* Setting PCFG_PROG_B signal to high */
237 writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
239 /* Polling the PCAP_INIT status for Set */
241 while (!(readl(&devcfg_base->status) &
242 DEVCFG_STATUS_PCFG_INIT)) {
243 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
244 printf("%s: Timeout wait for INIT to set\n",
251 isr_status = readl(&devcfg_base->int_sts);
253 /* Clear it all, so if Boot ROM comes back, it can proceed */
254 writel(0xFFFFFFFF, &devcfg_base->int_sts);
256 if (isr_status & DEVCFG_ISR_FATAL_ERROR_MASK) {
257 debug("%s: Fatal errors in PCAP 0x%X\n", __func__, isr_status);
259 /* If RX FIFO overflow, need to flush RX FIFO first */
260 if (isr_status & DEVCFG_ISR_RX_FIFO_OV) {
261 writel(DEVCFG_MCTRL_RFIFO_FLUSH, &devcfg_base->mctrl);
262 writel(0xFFFFFFFF, &devcfg_base->int_sts);
267 status = readl(&devcfg_base->status);
269 debug("%s: Status = 0x%08X\n", __func__, status);
271 if (status & DEVCFG_STATUS_DMA_CMD_Q_F) {
272 debug("%s: Error: device busy\n", __func__);
276 debug("%s: Device ready\n", __func__);
278 if (!(status & DEVCFG_STATUS_DMA_CMD_Q_E)) {
279 if (!(readl(&devcfg_base->int_sts) & DEVCFG_ISR_DMA_DONE)) {
280 /* Error state, transfer cannot occur */
281 debug("%s: ISR indicates error\n", __func__);
284 /* Clear out the status */
285 writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
289 if (status & DEVCFG_STATUS_DMA_DONE_CNT_MASK) {
290 /* Clear the count of completed DMA transfers */
291 writel(DEVCFG_STATUS_DMA_DONE_CNT_MASK, &devcfg_base->status);
297 static u32 *zynq_align_dma_buffer(u32 *buf, u32 len, u32 swap)
302 if ((u32)buf != ALIGN((u32)buf, ARCH_DMA_MINALIGN)) {
303 new_buf = (u32 *)ALIGN((u32)buf, ARCH_DMA_MINALIGN);
306 * This might be dangerous but permits to flash if
307 * ARCH_DMA_MINALIGN is greater than header size
310 debug("%s: Aligned buffer is after buffer start\n",
312 new_buf -= ARCH_DMA_MINALIGN;
314 printf("%s: Align buffer at %x to %x(swap %d)\n", __func__,
315 (u32)buf, (u32)new_buf, swap);
317 for (i = 0; i < (len/4); i++)
318 new_buf[i] = load_word(&buf[i], swap);
321 } else if (swap != SWAP_DONE) {
322 /* For bitstream which are aligned */
323 u32 *new_buf = (u32 *)buf;
325 printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
328 for (i = 0; i < (len/4); i++)
329 new_buf[i] = load_word(&buf[i], swap);
335 static int zynq_validate_bitstream(xilinx_desc *desc, const void *buf,
336 size_t bsize, u32 blocksize, u32 *swap,
337 bitstream_type *bstype)
342 buf_start = check_data((u8 *)buf, blocksize, swap);
347 /* Check if data is postpone from start */
348 diff = (u32)buf_start - (u32)buf;
350 printf("%s: Bitstream is not validated yet (diff %x)\n",
355 if ((u32)buf < SZ_1M) {
356 printf("%s: Bitstream has to be placed up to 1MB (%x)\n",
361 if (zynq_dma_xfer_init(*bstype))
367 static int zynq_load(xilinx_desc *desc, const void *buf, size_t bsize,
368 bitstream_type bstype)
370 unsigned long ts; /* Timestamp */
371 u32 isr_status, swap;
374 * send bsize inplace of blocksize as it was not a bitstream
377 if (zynq_validate_bitstream(desc, buf, bsize, bsize, &swap,
381 buf = zynq_align_dma_buffer((u32 *)buf, bsize, swap);
383 debug("%s: Source = 0x%08X\n", __func__, (u32)buf);
384 debug("%s: Size = %zu\n", __func__, bsize);
386 /* flush(clean & invalidate) d-cache range buf */
387 flush_dcache_range((u32)buf, (u32)buf +
388 roundup(bsize, ARCH_DMA_MINALIGN));
390 if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
393 isr_status = readl(&devcfg_base->int_sts);
394 /* Check FPGA configuration completion */
396 while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
397 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
398 printf("%s: Timeout wait for FPGA to config\n",
402 isr_status = readl(&devcfg_base->int_sts);
405 debug("%s: FPGA config done\n", __func__);
407 if (bstype != BIT_PARTIAL)
408 zynq_slcr_devcfg_enable();
413 #if defined(CONFIG_CMD_FPGA_LOADFS)
414 static int zynq_loadfs(xilinx_desc *desc, const void *buf, size_t bsize,
415 fpga_fs_info *fsinfo)
417 unsigned long ts; /* Timestamp */
418 u32 isr_status, swap;
420 loff_t blocksize, actread;
423 char *interface, *dev_part, *filename;
425 blocksize = fsinfo->blocksize;
426 interface = fsinfo->interface;
427 dev_part = fsinfo->dev_part;
428 filename = fsinfo->filename;
429 fstype = fsinfo->fstype;
431 if (fs_set_blk_dev(interface, dev_part, fstype))
434 if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
437 if (zynq_validate_bitstream(desc, buf, bsize, blocksize, &swap,
444 buf = zynq_align_dma_buffer((u32 *)buf, blocksize, swap);
446 if (zynq_dma_transfer((u32)buf | 1, blocksize >> 2,
453 if (fs_set_blk_dev(interface, dev_part, fstype))
456 if (bsize > blocksize) {
457 if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
460 if (fs_read(filename, (u32) buf, pos, bsize, &actread) < 0)
463 } while (bsize > blocksize);
465 buf = zynq_align_dma_buffer((u32 *)buf, blocksize, swap);
467 if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
472 isr_status = readl(&devcfg_base->int_sts);
474 /* Check FPGA configuration completion */
476 while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
477 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
478 printf("%s: Timeout wait for FPGA to config\n",
482 isr_status = readl(&devcfg_base->int_sts);
485 debug("%s: FPGA config done\n", __func__);
488 zynq_slcr_devcfg_enable();
494 struct xilinx_fpga_op zynq_op = {
496 #if defined(CONFIG_CMD_FPGA_LOADFS)
497 .loadfs = zynq_loadfs,