Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012-2013, Xilinx, Michal Simek |
| 4 | * |
| 5 | * (C) Copyright 2012 |
| 6 | * Joe Hershberger <joe.hershberger@ni.com> |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 9 | #include <config.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 10 | #include <console.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 11 | #include <cpu_func.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 13 | #include <time.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 14 | #include <asm/cache.h> |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 15 | #include <asm/io.h> |
Siva Durga Prasad Paladugu | 9112b4c | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 16 | #include <fs.h> |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 17 | #include <zynqpl.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Alexey Brodkin | 267d8e2 | 2014-02-26 17:47:58 +0400 | [diff] [blame] | 19 | #include <linux/sizes.h> |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 20 | #include <asm/arch/hardware.h> |
| 21 | #include <asm/arch/sys_proto.h> |
| 22 | |
| 23 | #define DEVCFG_CTRL_PCFG_PROG_B 0x40000000 |
Siva Durga Prasad Paladugu | eac0cd1 | 2018-03-06 17:37:09 +0530 | [diff] [blame] | 24 | #define DEVCFG_CTRL_PCFG_AES_EFUSE_MASK 0x00001000 |
Siva Durga Prasad Paladugu | e460352 | 2018-06-26 15:02:19 +0530 | [diff] [blame] | 25 | #define DEVCFG_CTRL_PCAP_RATE_EN_MASK 0x02000000 |
Ibai Erkiaga | 59d9ab7 | 2018-04-05 05:19:27 -0700 | [diff] [blame] | 26 | #define DEVCFG_CTRL_PCFG_AES_EN_MASK 0x00000E00 |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 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 |
Soren Brinkmann | 5577585 | 2013-06-14 17:43:24 -0700 | [diff] [blame] | 36 | #define DEVCFG_MCTRL_PCAP_LPBK 0x00000010 |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 37 | #define DEVCFG_MCTRL_RFIFO_FLUSH 0x00000002 |
| 38 | #define DEVCFG_MCTRL_WFIFO_FLUSH 0x00000001 |
| 39 | |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 40 | #ifndef CFG_SYS_FPGA_WAIT |
| 41 | #define CFG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100 /* 10 ms */ |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 42 | #endif |
| 43 | |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 44 | #ifndef CFG_SYS_FPGA_PROG_TIME |
| 45 | #define CFG_SYS_FPGA_PROG_TIME (CONFIG_SYS_HZ * 4) /* 4 s */ |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 46 | #endif |
| 47 | |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 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()) |
Michal Simek | 15f156a | 2014-04-25 13:51:58 +0200 | [diff] [blame] | 154 | return NULL; |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 155 | } |
Michal Simek | 15f156a | 2014-04-25 13:51:58 +0200 | [diff] [blame] | 156 | return NULL; |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 159 | static int zynq_dma_transfer(u32 srcbuf, u32 srclen, u32 dstbuf, u32 dstlen) |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 160 | { |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 161 | unsigned long ts; |
| 162 | u32 isr_status; |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 163 | |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 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); |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 169 | |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 170 | isr_status = readl(&devcfg_base->int_sts); |
Michal Simek | 94dc92f | 2013-10-04 10:48:59 +0200 | [diff] [blame] | 171 | |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 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)); |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 182 | |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 183 | return FPGA_FAIL; |
Novasys Ingenierie | cbe4b09 | 2013-11-27 09:03:01 +0100 | [diff] [blame] | 184 | } |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 185 | if (get_timer(ts) > CFG_SYS_FPGA_PROG_TIME) { |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 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 | } |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 192 | |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 193 | debug("%s: DMA transfer is done\n", __func__); |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 194 | |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 195 | /* Clear out the DMA status */ |
| 196 | writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts); |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 197 | |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 198 | return FPGA_SUCCESS; |
| 199 | } |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 200 | |
Michal Simek | f46ccf4 | 2014-05-02 14:15:27 +0200 | [diff] [blame] | 201 | static int zynq_dma_xfer_init(bitstream_type bstype) |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 202 | { |
| 203 | u32 status, control, isr_status; |
| 204 | unsigned long ts; |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 205 | |
Soren Brinkmann | 5577585 | 2013-06-14 17:43:24 -0700 | [diff] [blame] | 206 | /* Clear loopback bit */ |
| 207 | clrbits_le32(&devcfg_base->mctrl, DEVCFG_MCTRL_PCAP_LPBK); |
| 208 | |
Siva Durga Prasad Paladugu | c575058 | 2015-12-09 18:46:43 +0530 | [diff] [blame] | 209 | if (bstype != BIT_PARTIAL && bstype != BIT_NONE) { |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 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); |
Siva Durga Prasad Paladugu | eac0cd1 | 2018-03-06 17:37:09 +0530 | [diff] [blame] | 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 | |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 223 | /* Setting PCFG_PROG_B signal to low */ |
| 224 | writel(control & ~DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl); |
| 225 | |
Siva Durga Prasad Paladugu | eac0cd1 | 2018-03-06 17:37:09 +0530 | [diff] [blame] | 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 | |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 233 | /* Polling the PCAP_INIT status for Reset */ |
| 234 | ts = get_timer(0); |
| 235 | while (readl(&devcfg_base->status) & DEVCFG_STATUS_PCFG_INIT) { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 236 | if (get_timer(ts) > CFG_SYS_FPGA_WAIT) { |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 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)) { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 250 | if (get_timer(ts) > CFG_SYS_FPGA_WAIT) { |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 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 | |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 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__); |
Michael Walle | 36eca7c | 2021-02-10 22:42:29 +0100 | [diff] [blame] | 319 | new_buf = (u32 *)((u32)new_buf - ARCH_DMA_MINALIGN); |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 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 | |
Siva Durga Prasad Paladugu | 7857c72 | 2014-03-13 11:57:34 +0530 | [diff] [blame] | 342 | static int zynq_validate_bitstream(xilinx_desc *desc, const void *buf, |
| 343 | size_t bsize, u32 blocksize, u32 *swap, |
Michal Simek | f46ccf4 | 2014-05-02 14:15:27 +0200 | [diff] [blame] | 344 | bitstream_type *bstype) |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 345 | { |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 346 | u32 *buf_start; |
Siva Durga Prasad Paladugu | 7857c72 | 2014-03-13 11:57:34 +0530 | [diff] [blame] | 347 | u32 diff; |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 348 | |
Siva Durga Prasad Paladugu | 7857c72 | 2014-03-13 11:57:34 +0530 | [diff] [blame] | 349 | buf_start = check_data((u8 *)buf, blocksize, swap); |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 350 | |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 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 | |
Michal Simek | f46ccf4 | 2014-05-02 14:15:27 +0200 | [diff] [blame] | 368 | if (zynq_dma_xfer_init(*bstype)) |
Siva Durga Prasad Paladugu | 7857c72 | 2014-03-13 11:57:34 +0530 | [diff] [blame] | 369 | return FPGA_FAIL; |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
Michal Simek | 1466365 | 2014-05-02 14:09:30 +0200 | [diff] [blame] | 374 | static int zynq_load(xilinx_desc *desc, const void *buf, size_t bsize, |
Oleksandr Suvorov | c0806cc | 2022-07-22 17:16:10 +0300 | [diff] [blame] | 375 | bitstream_type bstype, int flags) |
Siva Durga Prasad Paladugu | 7857c72 | 2014-03-13 11:57:34 +0530 | [diff] [blame] | 376 | { |
| 377 | unsigned long ts; /* Timestamp */ |
Siva Durga Prasad Paladugu | 7857c72 | 2014-03-13 11:57:34 +0530 | [diff] [blame] | 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, |
Michal Simek | f46ccf4 | 2014-05-02 14:15:27 +0200 | [diff] [blame] | 385 | &bstype)) |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 386 | return FPGA_FAIL; |
| 387 | |
| 388 | buf = zynq_align_dma_buffer((u32 *)buf, bsize, swap); |
| 389 | |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 390 | debug("%s: Source = 0x%08X\n", __func__, (u32)buf); |
| 391 | debug("%s: Size = %zu\n", __func__, bsize); |
| 392 | |
Jagannadha Sutradharudu Teki | 8cfb246 | 2013-09-20 18:39:47 +0530 | [diff] [blame] | 393 | /* flush(clean & invalidate) d-cache range buf */ |
| 394 | flush_dcache_range((u32)buf, (u32)buf + |
| 395 | roundup(bsize, ARCH_DMA_MINALIGN)); |
| 396 | |
Siva Durga Prasad Paladugu | 2f72618 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 397 | if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0)) |
| 398 | return FPGA_FAIL; |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 399 | |
| 400 | isr_status = readl(&devcfg_base->int_sts); |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 401 | /* Check FPGA configuration completion */ |
| 402 | ts = get_timer(0); |
| 403 | while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 404 | if (get_timer(ts) > CFG_SYS_FPGA_WAIT) { |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 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 | |
Michal Simek | f46ccf4 | 2014-05-02 14:15:27 +0200 | [diff] [blame] | 414 | if (bstype != BIT_PARTIAL) |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 415 | zynq_slcr_devcfg_enable(); |
| 416 | |
Stefan Herbrechtsmeier | fb7fcc2 | 2022-08-08 16:53:31 +0200 | [diff] [blame] | 417 | if (!IS_ENABLED(CONFIG_SPL_BUILD)) |
| 418 | puts("INFO:post config was not run, please run manually if needed\n"); |
Siva Durga Prasad Paladugu | 9bf652d | 2019-03-23 16:01:36 +0530 | [diff] [blame] | 419 | |
Michal Simek | 15d654c | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 420 | return FPGA_SUCCESS; |
| 421 | } |
| 422 | |
Luis Araneda | 4397602 | 2018-07-19 03:10:17 -0400 | [diff] [blame] | 423 | #if defined(CONFIG_CMD_FPGA_LOADFS) && !defined(CONFIG_SPL_BUILD) |
Siva Durga Prasad Paladugu | 9112b4c | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 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; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 430 | loff_t blocksize, actread; |
| 431 | loff_t pos = 0; |
Siva Durga Prasad Paladugu | 9112b4c | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 432 | int fstype; |
Tien Fong Chee | 3b45f6b | 2019-02-15 15:57:07 +0800 | [diff] [blame] | 433 | char *interface, *dev_part; |
| 434 | const char *filename; |
Siva Durga Prasad Paladugu | 9112b4c | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 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 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 445 | if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0) |
Siva Durga Prasad Paladugu | 9112b4c | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 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) { |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 468 | if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0) |
Siva Durga Prasad Paladugu | 9112b4c | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 469 | return FPGA_FAIL; |
| 470 | } else { |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 471 | if (fs_read(filename, (u32) buf, pos, bsize, &actread) < 0) |
Siva Durga Prasad Paladugu | 9112b4c | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 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)) { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 488 | if (get_timer(ts) > CFG_SYS_FPGA_WAIT) { |
Siva Durga Prasad Paladugu | 9112b4c | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 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 | |
Michal Simek | 75fafac | 2014-03-13 13:07:57 +0100 | [diff] [blame] | 505 | struct xilinx_fpga_op zynq_op = { |
| 506 | .load = zynq_load, |
Luis Araneda | 4397602 | 2018-07-19 03:10:17 -0400 | [diff] [blame] | 507 | #if defined(CONFIG_CMD_FPGA_LOADFS) && !defined(CONFIG_SPL_BUILD) |
Siva Durga Prasad Paladugu | 9112b4c | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 508 | .loadfs = zynq_loadfs, |
| 509 | #endif |
Michal Simek | 75fafac | 2014-03-13 13:07:57 +0100 | [diff] [blame] | 510 | }; |
Siva Durga Prasad Paladugu | e460352 | 2018-06-26 15:02:19 +0530 | [diff] [blame] | 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 | */ |
Siva Durga Prasad Paladugu | c575058 | 2015-12-09 18:46:43 +0530 | [diff] [blame] | 517 | int zynq_decrypt_load(u32 srcaddr, u32 srclen, u32 dstaddr, u32 dstlen, |
| 518 | u8 bstype) |
Siva Durga Prasad Paladugu | e460352 | 2018-06-26 15:02:19 +0530 | [diff] [blame] | 519 | { |
T Karthik Reddy | f3b078f | 2019-03-12 20:20:20 +0530 | [diff] [blame] | 520 | u32 isr_status, ts; |
| 521 | |
Siva Durga Prasad Paladugu | e460352 | 2018-06-26 15:02:19 +0530 | [diff] [blame] | 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 | |
Ibai Erkiaga | 59d9ab7 | 2018-04-05 05:19:27 -0700 | [diff] [blame] | 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 | |
Siva Durga Prasad Paladugu | c575058 | 2015-12-09 18:46:43 +0530 | [diff] [blame] | 535 | if (zynq_dma_xfer_init(bstype)) { |
Siva Durga Prasad Paladugu | e460352 | 2018-06-26 15:02:19 +0530 | [diff] [blame] | 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 | */ |
T Karthik Reddy | 1fa7cbe | 2019-03-12 20:20:23 +0530 | [diff] [blame] | 553 | if (bstype == BIT_NONE && dstaddr != 0xFFFFFFFF) |
| 554 | flush_dcache_range((u32)dstaddr, (u32)dstaddr + |
| 555 | roundup(dstlen << 2, ARCH_DMA_MINALIGN)); |
Siva Durga Prasad Paladugu | e460352 | 2018-06-26 15:02:19 +0530 | [diff] [blame] | 556 | |
| 557 | if (zynq_dma_transfer(srcaddr | 1, srclen, dstaddr | 1, dstlen)) |
| 558 | return FPGA_FAIL; |
| 559 | |
T Karthik Reddy | f3b078f | 2019-03-12 20:20:20 +0530 | [diff] [blame] | 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)) { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 565 | if (get_timer(ts) > CFG_SYS_FPGA_WAIT) { |
T Karthik Reddy | f3b078f | 2019-03-12 20:20:20 +0530 | [diff] [blame] | 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 | } |
Siva Durga Prasad Paladugu | e460352 | 2018-06-26 15:02:19 +0530 | [diff] [blame] | 575 | |
| 576 | return FPGA_SUCCESS; |
| 577 | } |
| 578 | #endif |