e9bf3a618032685ec6fb0cfd94f01880402e1576
[oweals/u-boot.git] / drivers / fpga / zynqpl.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2012-2013, Xilinx, Michal Simek
4  *
5  * (C) Copyright 2012
6  * Joe Hershberger <joe.hershberger@ni.com>
7  */
8
9 #include <common.h>
10 #include <console.h>
11 #include <cpu_func.h>
12 #include <log.h>
13 #include <asm/cache.h>
14 #include <asm/io.h>
15 #include <fs.h>
16 #include <zynqpl.h>
17 #include <linux/sizes.h>
18 #include <asm/arch/hardware.h>
19 #include <asm/arch/sys_proto.h>
20
21 #define DEVCFG_CTRL_PCFG_PROG_B         0x40000000
22 #define DEVCFG_CTRL_PCFG_AES_EFUSE_MASK 0x00001000
23 #define DEVCFG_CTRL_PCAP_RATE_EN_MASK   0x02000000
24 #define DEVCFG_ISR_FATAL_ERROR_MASK     0x00740040
25 #define DEVCFG_ISR_ERROR_FLAGS_MASK     0x00340840
26 #define DEVCFG_ISR_RX_FIFO_OV           0x00040000
27 #define DEVCFG_ISR_DMA_DONE             0x00002000
28 #define DEVCFG_ISR_PCFG_DONE            0x00000004
29 #define DEVCFG_STATUS_DMA_CMD_Q_F       0x80000000
30 #define DEVCFG_STATUS_DMA_CMD_Q_E       0x40000000
31 #define DEVCFG_STATUS_DMA_DONE_CNT_MASK 0x30000000
32 #define DEVCFG_STATUS_PCFG_INIT         0x00000010
33 #define DEVCFG_MCTRL_PCAP_LPBK          0x00000010
34 #define DEVCFG_MCTRL_RFIFO_FLUSH        0x00000002
35 #define DEVCFG_MCTRL_WFIFO_FLUSH        0x00000001
36
37 #ifndef CONFIG_SYS_FPGA_WAIT
38 #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100  /* 10 ms */
39 #endif
40
41 #ifndef CONFIG_SYS_FPGA_PROG_TIME
42 #define CONFIG_SYS_FPGA_PROG_TIME       (CONFIG_SYS_HZ * 4) /* 4 s */
43 #endif
44
45 #define DUMMY_WORD      0xffffffff
46
47 /* Xilinx binary format header */
48 static const u32 bin_format[] = {
49         DUMMY_WORD, /* Dummy words */
50         DUMMY_WORD,
51         DUMMY_WORD,
52         DUMMY_WORD,
53         DUMMY_WORD,
54         DUMMY_WORD,
55         DUMMY_WORD,
56         DUMMY_WORD,
57         0x000000bb, /* Sync word */
58         0x11220044, /* Sync word */
59         DUMMY_WORD,
60         DUMMY_WORD,
61         0xaa995566, /* Sync word */
62 };
63
64 #define SWAP_NO         1
65 #define SWAP_DONE       2
66
67 /*
68  * Load the whole word from unaligned buffer
69  * Keep in your mind that it is byte loading on little-endian system
70  */
71 static u32 load_word(const void *buf, u32 swap)
72 {
73         u32 word = 0;
74         u8 *bitc = (u8 *)buf;
75         int p;
76
77         if (swap == SWAP_NO) {
78                 for (p = 0; p < 4; p++) {
79                         word <<= 8;
80                         word |= bitc[p];
81                 }
82         } else {
83                 for (p = 3; p >= 0; p--) {
84                         word <<= 8;
85                         word |= bitc[p];
86                 }
87         }
88
89         return word;
90 }
91
92 static u32 check_header(const void *buf)
93 {
94         u32 i, pattern;
95         int swap = SWAP_NO;
96         u32 *test = (u32 *)buf;
97
98         debug("%s: Let's check bitstream header\n", __func__);
99
100         /* Checking that passing bin is not a bitstream */
101         for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
102                 pattern = load_word(&test[i], swap);
103
104                 /*
105                  * Bitstreams in binary format are swapped
106                  * compare to regular bistream.
107                  * Do not swap dummy word but if swap is done assume
108                  * that parsing buffer is binary format
109                  */
110                 if ((__swab32(pattern) != DUMMY_WORD) &&
111                     (__swab32(pattern) == bin_format[i])) {
112                         pattern = __swab32(pattern);
113                         swap = SWAP_DONE;
114                         debug("%s: data swapped - let's swap\n", __func__);
115                 }
116
117                 debug("%s: %d/%x: pattern %x/%x bin_format\n", __func__, i,
118                       (u32)&test[i], pattern, bin_format[i]);
119                 if (pattern != bin_format[i]) {
120                         debug("%s: Bitstream is not recognized\n", __func__);
121                         return 0;
122                 }
123         }
124         debug("%s: Found bitstream header at %x %s swapinng\n", __func__,
125               (u32)buf, swap == SWAP_NO ? "without" : "with");
126
127         return swap;
128 }
129
130 static void *check_data(u8 *buf, size_t bsize, u32 *swap)
131 {
132         u32 word, p = 0; /* possition */
133
134         /* Because buf doesn't need to be aligned let's read it by chars */
135         for (p = 0; p < bsize; p++) {
136                 word = load_word(&buf[p], SWAP_NO);
137                 debug("%s: word %x %x/%x\n", __func__, word, p, (u32)&buf[p]);
138
139                 /* Find the first bitstream dummy word */
140                 if (word == DUMMY_WORD) {
141                         debug("%s: Found dummy word at position %x/%x\n",
142                               __func__, p, (u32)&buf[p]);
143                         *swap = check_header(&buf[p]);
144                         if (*swap) {
145                                 /* FIXME add full bitstream checking here */
146                                 return &buf[p];
147                         }
148                 }
149                 /* Loop can be huge - support CTRL + C */
150                 if (ctrlc())
151                         return NULL;
152         }
153         return NULL;
154 }
155
156 static int zynq_dma_transfer(u32 srcbuf, u32 srclen, u32 dstbuf, u32 dstlen)
157 {
158         unsigned long ts;
159         u32 isr_status;
160
161         /* Set up the transfer */
162         writel((u32)srcbuf, &devcfg_base->dma_src_addr);
163         writel(dstbuf, &devcfg_base->dma_dst_addr);
164         writel(srclen, &devcfg_base->dma_src_len);
165         writel(dstlen, &devcfg_base->dma_dst_len);
166
167         isr_status = readl(&devcfg_base->int_sts);
168
169         /* Polling the PCAP_INIT status for Set */
170         ts = get_timer(0);
171         while (!(isr_status & DEVCFG_ISR_DMA_DONE)) {
172                 if (isr_status & DEVCFG_ISR_ERROR_FLAGS_MASK) {
173                         debug("%s: Error: isr = 0x%08X\n", __func__,
174                               isr_status);
175                         debug("%s: Write count = 0x%08X\n", __func__,
176                               readl(&devcfg_base->write_count));
177                         debug("%s: Read count = 0x%08X\n", __func__,
178                               readl(&devcfg_base->read_count));
179
180                         return FPGA_FAIL;
181                 }
182                 if (get_timer(ts) > CONFIG_SYS_FPGA_PROG_TIME) {
183                         printf("%s: Timeout wait for DMA to complete\n",
184                                __func__);
185                         return FPGA_FAIL;
186                 }
187                 isr_status = readl(&devcfg_base->int_sts);
188         }
189
190         debug("%s: DMA transfer is done\n", __func__);
191
192         /* Clear out the DMA status */
193         writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
194
195         return FPGA_SUCCESS;
196 }
197
198 static int zynq_dma_xfer_init(bitstream_type bstype)
199 {
200         u32 status, control, isr_status;
201         unsigned long ts;
202
203         /* Clear loopback bit */
204         clrbits_le32(&devcfg_base->mctrl, DEVCFG_MCTRL_PCAP_LPBK);
205
206         if (bstype != BIT_PARTIAL) {
207                 zynq_slcr_devcfg_disable();
208
209                 /* Setting PCFG_PROG_B signal to high */
210                 control = readl(&devcfg_base->ctrl);
211                 writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
212
213                 /*
214                  * Delay is required if AES efuse is selected as
215                  * key source.
216                  */
217                 if (control & DEVCFG_CTRL_PCFG_AES_EFUSE_MASK)
218                         mdelay(5);
219
220                 /* Setting PCFG_PROG_B signal to low */
221                 writel(control & ~DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
222
223                 /*
224                  * Delay is required if AES efuse is selected as
225                  * key source.
226                  */
227                 if (control & DEVCFG_CTRL_PCFG_AES_EFUSE_MASK)
228                         mdelay(5);
229
230                 /* Polling the PCAP_INIT status for Reset */
231                 ts = get_timer(0);
232                 while (readl(&devcfg_base->status) & DEVCFG_STATUS_PCFG_INIT) {
233                         if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
234                                 printf("%s: Timeout wait for INIT to clear\n",
235                                        __func__);
236                                 return FPGA_FAIL;
237                         }
238                 }
239
240                 /* Setting PCFG_PROG_B signal to high */
241                 writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
242
243                 /* Polling the PCAP_INIT status for Set */
244                 ts = get_timer(0);
245                 while (!(readl(&devcfg_base->status) &
246                         DEVCFG_STATUS_PCFG_INIT)) {
247                         if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
248                                 printf("%s: Timeout wait for INIT to set\n",
249                                        __func__);
250                                 return FPGA_FAIL;
251                         }
252                 }
253         }
254
255         isr_status = readl(&devcfg_base->int_sts);
256
257         /* Clear it all, so if Boot ROM comes back, it can proceed */
258         writel(0xFFFFFFFF, &devcfg_base->int_sts);
259
260         if (isr_status & DEVCFG_ISR_FATAL_ERROR_MASK) {
261                 debug("%s: Fatal errors in PCAP 0x%X\n", __func__, isr_status);
262
263                 /* If RX FIFO overflow, need to flush RX FIFO first */
264                 if (isr_status & DEVCFG_ISR_RX_FIFO_OV) {
265                         writel(DEVCFG_MCTRL_RFIFO_FLUSH, &devcfg_base->mctrl);
266                         writel(0xFFFFFFFF, &devcfg_base->int_sts);
267                 }
268                 return FPGA_FAIL;
269         }
270
271         status = readl(&devcfg_base->status);
272
273         debug("%s: Status = 0x%08X\n", __func__, status);
274
275         if (status & DEVCFG_STATUS_DMA_CMD_Q_F) {
276                 debug("%s: Error: device busy\n", __func__);
277                 return FPGA_FAIL;
278         }
279
280         debug("%s: Device ready\n", __func__);
281
282         if (!(status & DEVCFG_STATUS_DMA_CMD_Q_E)) {
283                 if (!(readl(&devcfg_base->int_sts) & DEVCFG_ISR_DMA_DONE)) {
284                         /* Error state, transfer cannot occur */
285                         debug("%s: ISR indicates error\n", __func__);
286                         return FPGA_FAIL;
287                 } else {
288                         /* Clear out the status */
289                         writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
290                 }
291         }
292
293         if (status & DEVCFG_STATUS_DMA_DONE_CNT_MASK) {
294                 /* Clear the count of completed DMA transfers */
295                 writel(DEVCFG_STATUS_DMA_DONE_CNT_MASK, &devcfg_base->status);
296         }
297
298         return FPGA_SUCCESS;
299 }
300
301 static u32 *zynq_align_dma_buffer(u32 *buf, u32 len, u32 swap)
302 {
303         u32 *new_buf;
304         u32 i;
305
306         if ((u32)buf != ALIGN((u32)buf, ARCH_DMA_MINALIGN)) {
307                 new_buf = (u32 *)ALIGN((u32)buf, ARCH_DMA_MINALIGN);
308
309                 /*
310                  * This might be dangerous but permits to flash if
311                  * ARCH_DMA_MINALIGN is greater than header size
312                  */
313                 if (new_buf > buf) {
314                         debug("%s: Aligned buffer is after buffer start\n",
315                               __func__);
316                         new_buf -= ARCH_DMA_MINALIGN;
317                 }
318                 printf("%s: Align buffer at %x to %x(swap %d)\n", __func__,
319                        (u32)buf, (u32)new_buf, swap);
320
321                 for (i = 0; i < (len/4); i++)
322                         new_buf[i] = load_word(&buf[i], swap);
323
324                 buf = new_buf;
325         } else if (swap != SWAP_DONE) {
326                 /* For bitstream which are aligned */
327                 u32 *new_buf = (u32 *)buf;
328
329                 printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
330                        swap);
331
332                 for (i = 0; i < (len/4); i++)
333                         new_buf[i] = load_word(&buf[i], swap);
334         }
335
336         return buf;
337 }
338
339 static int zynq_validate_bitstream(xilinx_desc *desc, const void *buf,
340                                    size_t bsize, u32 blocksize, u32 *swap,
341                                    bitstream_type *bstype)
342 {
343         u32 *buf_start;
344         u32 diff;
345
346         buf_start = check_data((u8 *)buf, blocksize, swap);
347
348         if (!buf_start)
349                 return FPGA_FAIL;
350
351         /* Check if data is postpone from start */
352         diff = (u32)buf_start - (u32)buf;
353         if (diff) {
354                 printf("%s: Bitstream is not validated yet (diff %x)\n",
355                        __func__, diff);
356                 return FPGA_FAIL;
357         }
358
359         if ((u32)buf < SZ_1M) {
360                 printf("%s: Bitstream has to be placed up to 1MB (%x)\n",
361                        __func__, (u32)buf);
362                 return FPGA_FAIL;
363         }
364
365         if (zynq_dma_xfer_init(*bstype))
366                 return FPGA_FAIL;
367
368         return 0;
369 }
370
371 static int zynq_load(xilinx_desc *desc, const void *buf, size_t bsize,
372                      bitstream_type bstype)
373 {
374         unsigned long ts; /* Timestamp */
375         u32 isr_status, swap;
376
377         /*
378          * send bsize inplace of blocksize as it was not a bitstream
379          * in chunks
380          */
381         if (zynq_validate_bitstream(desc, buf, bsize, bsize, &swap,
382                                     &bstype))
383                 return FPGA_FAIL;
384
385         buf = zynq_align_dma_buffer((u32 *)buf, bsize, swap);
386
387         debug("%s: Source = 0x%08X\n", __func__, (u32)buf);
388         debug("%s: Size = %zu\n", __func__, bsize);
389
390         /* flush(clean & invalidate) d-cache range buf */
391         flush_dcache_range((u32)buf, (u32)buf +
392                            roundup(bsize, ARCH_DMA_MINALIGN));
393
394         if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
395                 return FPGA_FAIL;
396
397         isr_status = readl(&devcfg_base->int_sts);
398         /* Check FPGA configuration completion */
399         ts = get_timer(0);
400         while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
401                 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
402                         printf("%s: Timeout wait for FPGA to config\n",
403                                __func__);
404                         return FPGA_FAIL;
405                 }
406                 isr_status = readl(&devcfg_base->int_sts);
407         }
408
409         debug("%s: FPGA config done\n", __func__);
410
411         if (bstype != BIT_PARTIAL)
412                 zynq_slcr_devcfg_enable();
413
414         puts("INFO:post config was not run, please run manually if needed\n");
415
416         return FPGA_SUCCESS;
417 }
418
419 #if defined(CONFIG_CMD_FPGA_LOADFS) && !defined(CONFIG_SPL_BUILD)
420 static int zynq_loadfs(xilinx_desc *desc, const void *buf, size_t bsize,
421                        fpga_fs_info *fsinfo)
422 {
423         unsigned long ts; /* Timestamp */
424         u32 isr_status, swap;
425         u32 partialbit = 0;
426         loff_t blocksize, actread;
427         loff_t pos = 0;
428         int fstype;
429         char *interface, *dev_part;
430         const char *filename;
431
432         blocksize = fsinfo->blocksize;
433         interface = fsinfo->interface;
434         dev_part = fsinfo->dev_part;
435         filename = fsinfo->filename;
436         fstype = fsinfo->fstype;
437
438         if (fs_set_blk_dev(interface, dev_part, fstype))
439                 return FPGA_FAIL;
440
441         if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
442                 return FPGA_FAIL;
443
444         if (zynq_validate_bitstream(desc, buf, bsize, blocksize, &swap,
445                                     &partialbit))
446                 return FPGA_FAIL;
447
448         dcache_disable();
449
450         do {
451                 buf = zynq_align_dma_buffer((u32 *)buf, blocksize, swap);
452
453                 if (zynq_dma_transfer((u32)buf | 1, blocksize >> 2,
454                                       0xffffffff, 0))
455                         return FPGA_FAIL;
456
457                 bsize -= blocksize;
458                 pos   += blocksize;
459
460                 if (fs_set_blk_dev(interface, dev_part, fstype))
461                         return FPGA_FAIL;
462
463                 if (bsize > blocksize) {
464                         if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
465                                 return FPGA_FAIL;
466                 } else {
467                         if (fs_read(filename, (u32) buf, pos, bsize, &actread) < 0)
468                                 return FPGA_FAIL;
469                 }
470         } while (bsize > blocksize);
471
472         buf = zynq_align_dma_buffer((u32 *)buf, blocksize, swap);
473
474         if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
475                 return FPGA_FAIL;
476
477         dcache_enable();
478
479         isr_status = readl(&devcfg_base->int_sts);
480
481         /* Check FPGA configuration completion */
482         ts = get_timer(0);
483         while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
484                 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
485                         printf("%s: Timeout wait for FPGA to config\n",
486                                __func__);
487                         return FPGA_FAIL;
488                 }
489                 isr_status = readl(&devcfg_base->int_sts);
490         }
491
492         debug("%s: FPGA config done\n", __func__);
493
494         if (!partialbit)
495                 zynq_slcr_devcfg_enable();
496
497         return FPGA_SUCCESS;
498 }
499 #endif
500
501 struct xilinx_fpga_op zynq_op = {
502         .load = zynq_load,
503 #if defined(CONFIG_CMD_FPGA_LOADFS) && !defined(CONFIG_SPL_BUILD)
504         .loadfs = zynq_loadfs,
505 #endif
506 };
507
508 #ifdef CONFIG_CMD_ZYNQ_AES
509 /*
510  * Load the encrypted image from src addr and decrypt the image and
511  * place it back the decrypted image into dstaddr.
512  */
513 int zynq_decrypt_load(u32 srcaddr, u32 srclen, u32 dstaddr, u32 dstlen)
514 {
515         if (srcaddr < SZ_1M || dstaddr < SZ_1M) {
516                 printf("%s: src and dst addr should be > 1M\n",
517                        __func__);
518                 return FPGA_FAIL;
519         }
520
521         if (zynq_dma_xfer_init(BIT_NONE)) {
522                 printf("%s: zynq_dma_xfer_init FAIL\n", __func__);
523                 return FPGA_FAIL;
524         }
525
526         writel((readl(&devcfg_base->ctrl) | DEVCFG_CTRL_PCAP_RATE_EN_MASK),
527                &devcfg_base->ctrl);
528
529         debug("%s: Source = 0x%08X\n", __func__, (u32)srcaddr);
530         debug("%s: Size = %zu\n", __func__, srclen);
531
532         /* flush(clean & invalidate) d-cache range buf */
533         flush_dcache_range((u32)srcaddr, (u32)srcaddr +
534                         roundup(srclen << 2, ARCH_DMA_MINALIGN));
535         /*
536          * Flush destination address range only if image is not
537          * bitstream.
538          */
539         flush_dcache_range((u32)dstaddr, (u32)dstaddr +
540                            roundup(dstlen << 2, ARCH_DMA_MINALIGN));
541
542         if (zynq_dma_transfer(srcaddr | 1, srclen, dstaddr | 1, dstlen))
543                 return FPGA_FAIL;
544
545         writel((readl(&devcfg_base->ctrl) & ~DEVCFG_CTRL_PCAP_RATE_EN_MASK),
546                &devcfg_base->ctrl);
547
548         return FPGA_SUCCESS;
549 }
550 #endif