]> Git Repo - J-u-boot.git/blob - drivers/fpga/zynqpl.c
Merge tag 'u-boot-imx-master-20250127' of https://gitlab.denx.de/u-boot/custodians...
[J-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 <[email protected]>
7  */
8
9 #include <config.h>
10 #include <console.h>
11 #include <cpu_func.h>
12 #include <log.h>
13 #include <time.h>
14 #include <asm/cache.h>
15 #include <asm/io.h>
16 #include <fs.h>
17 #include <zynqpl.h>
18 #include <linux/delay.h>
19 #include <linux/sizes.h>
20 #include <asm/arch/hardware.h>
21 #include <asm/arch/sys_proto.h>
22
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
39
40 #ifndef CFG_SYS_FPGA_WAIT
41 #define CFG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100     /* 10 ms */
42 #endif
43
44 #ifndef CFG_SYS_FPGA_PROG_TIME
45 #define CFG_SYS_FPGA_PROG_TIME  (CONFIG_SYS_HZ * 4) /* 4 s */
46 #endif
47
48 #define DUMMY_WORD      0xffffffff
49
50 /* Xilinx binary format header */
51 static 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  */
74 static 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
95 static 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
133 static 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())
154                         return NULL;
155         }
156         return NULL;
157 }
158
159 static int zynq_dma_transfer(u32 srcbuf, u32 srclen, u32 dstbuf, u32 dstlen)
160 {
161         unsigned long ts;
162         u32 isr_status;
163
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);
169
170         isr_status = readl(&devcfg_base->int_sts);
171
172         /* 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));
182
183                         return FPGA_FAIL;
184                 }
185                 if (get_timer(ts) > CFG_SYS_FPGA_PROG_TIME) {
186                         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         }
192
193         debug("%s: DMA transfer is done\n", __func__);
194
195         /* Clear out the DMA status */
196         writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
197
198         return FPGA_SUCCESS;
199 }
200
201 static int zynq_dma_xfer_init(bitstream_type bstype)
202 {
203         u32 status, control, isr_status;
204         unsigned long ts;
205
206         /* Clear loopback bit */
207         clrbits_le32(&devcfg_base->mctrl, DEVCFG_MCTRL_PCAP_LPBK);
208
209         if (bstype != BIT_PARTIAL && bstype != BIT_NONE) {
210                 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);
215
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
223                 /* Setting PCFG_PROG_B signal to low */
224                 writel(control & ~DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
225
226                 /*
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
233                 /* Polling the PCAP_INIT status for Reset */
234                 ts = get_timer(0);
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",
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)) {
250                         if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
251                                 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
301         return FPGA_SUCCESS;
302 }
303
304 static 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__);
319                         new_buf = (u32 *)((u32)new_buf - ARCH_DMA_MINALIGN);
320                 }
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
342 static int zynq_validate_bitstream(xilinx_desc *desc, const void *buf,
343                                    size_t bsize, u32 blocksize, u32 *swap,
344                                    bitstream_type *bstype)
345 {
346         u32 *buf_start;
347         u32 diff;
348
349         buf_start = check_data((u8 *)buf, blocksize, swap);
350
351         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
368         if (zynq_dma_xfer_init(*bstype))
369                 return FPGA_FAIL;
370
371         return 0;
372 }
373
374 static int zynq_load(xilinx_desc *desc, const void *buf, size_t bsize,
375                      bitstream_type bstype, int flags)
376 {
377         unsigned long ts; /* Timestamp */
378         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,
385                                     &bstype))
386                 return FPGA_FAIL;
387
388         buf = zynq_align_dma_buffer((u32 *)buf, bsize, swap);
389
390         debug("%s: Source = 0x%08X\n", __func__, (u32)buf);
391         debug("%s: Size = %zu\n", __func__, bsize);
392
393         /* flush(clean & invalidate) d-cache range buf */
394         flush_dcache_range((u32)buf, (u32)buf +
395                            roundup(bsize, ARCH_DMA_MINALIGN));
396
397         if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
398                 return FPGA_FAIL;
399
400         isr_status = readl(&devcfg_base->int_sts);
401         /* Check FPGA configuration completion */
402         ts = get_timer(0);
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",
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
414         if (bstype != BIT_PARTIAL)
415                 zynq_slcr_devcfg_enable();
416
417         if (!IS_ENABLED(CONFIG_XPL_BUILD))
418                 puts("INFO:post config was not run, please run manually if needed\n");
419
420         return FPGA_SUCCESS;
421 }
422
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)
426 {
427         unsigned long ts; /* Timestamp */
428         u32 isr_status, swap;
429         u32 partialbit = 0;
430         loff_t blocksize, actread;
431         loff_t pos = 0;
432         int fstype;
433         char *interface, *dev_part;
434         const char *filename;
435
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
445         if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
446                 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) {
468                         if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
469                                 return FPGA_FAIL;
470                 } else {
471                         if (fs_read(filename, (u32) buf, pos, bsize, &actread) < 0)
472                                 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)) {
488                 if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
489                         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
505 struct xilinx_fpga_op zynq_op = {
506         .load = zynq_load,
507 #if defined(CONFIG_CMD_FPGA_LOADFS) && !defined(CONFIG_XPL_BUILD)
508         .loadfs = zynq_loadfs,
509 #endif
510 };
511
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  */
517 int zynq_decrypt_load(u32 srcaddr, u32 srclen, u32 dstaddr, u32 dstlen,
518                       u8 bstype)
519 {
520         u32 isr_status, ts;
521
522         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
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__);
532                 return FPGA_FAIL;
533         }
534
535         if (zynq_dma_xfer_init(bstype)) {
536                 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          */
553         if (bstype == BIT_NONE && dstaddr != 0xFFFFFFFF)
554                 flush_dcache_range((u32)dstaddr, (u32)dstaddr +
555                                    roundup(dstlen << 2, ARCH_DMA_MINALIGN));
556
557         if (zynq_dma_transfer(srcaddr | 1, srclen, dstaddr | 1, dstlen))
558                 return FPGA_FAIL;
559
560         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)) {
565                         if (get_timer(ts) > CFG_SYS_FPGA_WAIT) {
566                                 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         }
575
576         return FPGA_SUCCESS;
577 }
578 #endif
This page took 0.057326 seconds and 4 git commands to generate.