2 * (C) Copyright 2012-2013, Xilinx, Michal Simek
5 * Joe Hershberger <joe.hershberger@ni.com>
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <asm/arch/hardware.h>
30 #include <asm/arch/sys_proto.h>
32 #define DEVCFG_CTRL_PCFG_PROG_B 0x40000000
33 #define DEVCFG_ISR_FATAL_ERROR_MASK 0x00740040
34 #define DEVCFG_ISR_ERROR_FLAGS_MASK 0x00340840
35 #define DEVCFG_ISR_RX_FIFO_OV 0x00040000
36 #define DEVCFG_ISR_DMA_DONE 0x00002000
37 #define DEVCFG_ISR_PCFG_DONE 0x00000004
38 #define DEVCFG_STATUS_DMA_CMD_Q_F 0x80000000
39 #define DEVCFG_STATUS_DMA_CMD_Q_E 0x40000000
40 #define DEVCFG_STATUS_DMA_DONE_CNT_MASK 0x30000000
41 #define DEVCFG_STATUS_PCFG_INIT 0x00000010
42 #define DEVCFG_MCTRL_RFIFO_FLUSH 0x00000002
43 #define DEVCFG_MCTRL_WFIFO_FLUSH 0x00000001
45 #ifndef CONFIG_SYS_FPGA_WAIT
46 #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
49 #ifndef CONFIG_SYS_FPGA_PROG_TIME
50 #define CONFIG_SYS_FPGA_PROG_TIME CONFIG_SYS_HZ /* 1 s */
53 int zynq_info(Xilinx_desc *desc)
58 #define DUMMY_WORD 0xffffffff
60 /* Xilinx binary format header */
61 static const u32 bin_format[] = {
62 DUMMY_WORD, /* Dummy words */
70 0x000000bb, /* Sync word */
71 0x11220044, /* Sync word */
74 0xaa995566, /* Sync word */
81 * Load the whole word from unaligned buffer
82 * Keep in your mind that it is byte loading on little-endian system
84 static u32 load_word(const void *buf, u32 swap)
90 if (swap == SWAP_NO) {
91 for (p = 0; p < 4; p++) {
96 for (p = 3; p >= 0; p--) {
105 static u32 check_header(const void *buf)
109 u32 *test = (u32 *)buf;
111 debug("%s: Let's check bitstream header\n", __func__);
113 /* Checking that passing bin is not a bitstream */
114 for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
115 pattern = load_word(&test[i], swap);
118 * Bitstreams in binary format are swapped
119 * compare to regular bistream.
120 * Do not swap dummy word but if swap is done assume
121 * that parsing buffer is binary format
123 if ((__swab32(pattern) != DUMMY_WORD) &&
124 (__swab32(pattern) == bin_format[i])) {
125 pattern = __swab32(pattern);
127 debug("%s: data swapped - let's swap\n", __func__);
130 debug("%s: %d/%x: pattern %x/%x bin_format\n", __func__, i,
131 (u32)&test[i], pattern, bin_format[i]);
132 if (pattern != bin_format[i]) {
133 debug("%s: Bitstream is not recognized\n", __func__);
137 debug("%s: Found bitstream header at %x %s swapinng\n", __func__,
138 (u32)buf, swap == SWAP_NO ? "without" : "with");
143 static void *check_data(u8 *buf, size_t bsize, u32 *swap)
145 u32 word, p = 0; /* possition */
147 /* Because buf doesn't need to be aligned let's read it by chars */
148 for (p = 0; p < bsize; p++) {
149 word = load_word(&buf[p], SWAP_NO);
150 debug("%s: word %x %x/%x\n", __func__, word, p, (u32)&buf[p]);
152 /* Find the first bitstream dummy word */
153 if (word == DUMMY_WORD) {
154 debug("%s: Found dummy word at position %x/%x\n",
155 __func__, p, (u32)&buf[p]);
156 *swap = check_header(&buf[p]);
158 /* FIXME add full bitstream checking here */
162 /* Loop can be huge - support CTRL + C */
170 int zynq_load(Xilinx_desc *desc, const void *buf, size_t bsize)
172 unsigned long ts; /* Timestamp */
174 u32 i, control, isr_status, status, swap, diff;
177 /* Detect if we are going working with partial or full bitstream */
178 if (bsize != desc->size) {
179 printf("%s: Working with partial bitstream\n", __func__);
183 buf_start = check_data((u8 *)buf, bsize, &swap);
187 /* Check if data is postpone from start */
188 diff = (u32)buf_start - (u32)buf;
190 printf("%s: Bitstream is not validated yet (diff %x)\n",
195 if ((u32)buf_start & 0x3) {
196 u32 *new_buf = (u32 *)((u32)buf & ~0x3);
198 printf("%s: Align buffer at %x to %x(swap %d)\n", __func__,
199 (u32)buf_start, (u32)new_buf, swap);
201 for (i = 0; i < (bsize/4); i++)
202 new_buf[i] = load_word(&buf_start[i], swap);
206 } else if (swap != SWAP_DONE) {
207 /* For bitstream which are aligned */
208 u32 *new_buf = (u32 *)buf;
210 printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
213 for (i = 0; i < (bsize/4); i++)
214 new_buf[i] = load_word(&buf_start[i], swap);
220 zynq_slcr_devcfg_disable();
222 /* Setting PCFG_PROG_B signal to high */
223 control = readl(&devcfg_base->ctrl);
224 writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
225 /* Setting PCFG_PROG_B signal to low */
226 writel(control & ~DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
228 /* Polling the PCAP_INIT status for Reset */
230 while (readl(&devcfg_base->status) & DEVCFG_STATUS_PCFG_INIT) {
231 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
232 printf("%s: Timeout wait for INIT to clear\n",
238 /* Setting PCFG_PROG_B signal to high */
239 writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
241 /* Polling the PCAP_INIT status for Set */
243 while (!(readl(&devcfg_base->status) &
244 DEVCFG_STATUS_PCFG_INIT)) {
245 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
246 printf("%s: Timeout wait for INIT to set\n",
253 isr_status = readl(&devcfg_base->int_sts);
255 /* Clear it all, so if Boot ROM comes back, it can proceed */
256 writel(0xFFFFFFFF, &devcfg_base->int_sts);
258 if (isr_status & DEVCFG_ISR_FATAL_ERROR_MASK) {
259 debug("%s: Fatal errors in PCAP 0x%X\n", __func__, isr_status);
261 /* If RX FIFO overflow, need to flush RX FIFO first */
262 if (isr_status & DEVCFG_ISR_RX_FIFO_OV) {
263 writel(DEVCFG_MCTRL_RFIFO_FLUSH, &devcfg_base->mctrl);
264 writel(0xFFFFFFFF, &devcfg_base->int_sts);
269 status = readl(&devcfg_base->status);
271 debug("%s: Status = 0x%08X\n", __func__, status);
273 if (status & DEVCFG_STATUS_DMA_CMD_Q_F) {
274 debug("%s: Error: device busy\n", __func__);
278 debug("%s: Device ready\n", __func__);
280 if (!(status & DEVCFG_STATUS_DMA_CMD_Q_E)) {
281 if (!(readl(&devcfg_base->int_sts) & DEVCFG_ISR_DMA_DONE)) {
282 /* Error state, transfer cannot occur */
283 debug("%s: ISR indicates error\n", __func__);
286 /* Clear out the status */
287 writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
291 if (status & DEVCFG_STATUS_DMA_DONE_CNT_MASK) {
292 /* Clear the count of completed DMA transfers */
293 writel(DEVCFG_STATUS_DMA_DONE_CNT_MASK, &devcfg_base->status);
296 debug("%s: Source = 0x%08X\n", __func__, (u32)buf);
297 debug("%s: Size = %zu\n", __func__, bsize);
299 /* Set up the transfer */
300 writel((u32)buf | 1, &devcfg_base->dma_src_addr);
301 writel(0xFFFFFFFF, &devcfg_base->dma_dst_addr);
302 writel(bsize >> 2, &devcfg_base->dma_src_len);
303 writel(0, &devcfg_base->dma_dst_len);
305 isr_status = readl(&devcfg_base->int_sts);
307 /* Polling the PCAP_INIT status for Set */
309 while (!(isr_status & DEVCFG_ISR_DMA_DONE)) {
310 if (isr_status & DEVCFG_ISR_ERROR_FLAGS_MASK) {
311 debug("%s: Error: isr = 0x%08X\n", __func__,
313 debug("%s: Write count = 0x%08X\n", __func__,
314 readl(&devcfg_base->write_count));
315 debug("%s: Read count = 0x%08X\n", __func__,
316 readl(&devcfg_base->read_count));
320 if (get_timer(ts) > CONFIG_SYS_FPGA_PROG_TIME) {
321 printf("%s: Timeout wait for DMA to complete\n",
325 isr_status = readl(&devcfg_base->int_sts);
328 debug("%s: DMA transfer is done\n", __func__);
330 /* Check FPGA configuration completion */
332 while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
333 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
334 printf("%s: Timeout wait for FPGA to config\n",
338 isr_status = readl(&devcfg_base->int_sts);
341 debug("%s: FPGA config done\n", __func__);
343 /* Clear out the DMA status */
344 writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
347 zynq_slcr_devcfg_enable();
352 int zynq_dump(Xilinx_desc *desc, const void *buf, size_t bsize)