Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | d5dae85 | 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 | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 10 | #include <console.h> |
Simon Glass | 9edefc2 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 11 | #include <cpu_func.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 13 | #include <asm/cache.h> |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
Siva Durga Prasad Paladugu | 1a89766 | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 15 | #include <fs.h> |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 16 | #include <zynqpl.h> |
Alexey Brodkin | 1ace402 | 2014-02-26 17:47:58 +0400 | [diff] [blame] | 17 | #include <linux/sizes.h> |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 18 | #include <asm/arch/hardware.h> |
| 19 | #include <asm/arch/sys_proto.h> |
| 20 | |
| 21 | #define DEVCFG_CTRL_PCFG_PROG_B 0x40000000 |
Siva Durga Prasad Paladugu | 71723aa | 2018-03-06 17:37:09 +0530 | [diff] [blame] | 22 | #define DEVCFG_CTRL_PCFG_AES_EFUSE_MASK 0x00001000 |
Siva Durga Prasad Paladugu | 37e3a36 | 2018-06-26 15:02:19 +0530 | [diff] [blame] | 23 | #define DEVCFG_CTRL_PCAP_RATE_EN_MASK 0x02000000 |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 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 |
Soren Brinkmann | 5f93227 | 2013-06-14 17:43:24 -0700 | [diff] [blame] | 33 | #define DEVCFG_MCTRL_PCAP_LPBK 0x00000010 |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 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 |
Michal Simek | fd2b10b | 2013-06-17 13:54:07 +0200 | [diff] [blame] | 42 | #define CONFIG_SYS_FPGA_PROG_TIME (CONFIG_SYS_HZ * 4) /* 4 s */ |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 43 | #endif |
| 44 | |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 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()) |
Michal Simek | 42a74a0 | 2014-04-25 13:51:58 +0200 | [diff] [blame] | 151 | return NULL; |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 152 | } |
Michal Simek | 42a74a0 | 2014-04-25 13:51:58 +0200 | [diff] [blame] | 153 | return NULL; |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 156 | static int zynq_dma_transfer(u32 srcbuf, u32 srclen, u32 dstbuf, u32 dstlen) |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 157 | { |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 158 | unsigned long ts; |
| 159 | u32 isr_status; |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 160 | |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 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); |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 166 | |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 167 | isr_status = readl(&devcfg_base->int_sts); |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 168 | |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 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)); |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 179 | |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 180 | return FPGA_FAIL; |
Novasys Ingenierie | c83a35f | 2013-11-27 09:03:01 +0100 | [diff] [blame] | 181 | } |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 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); |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 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 | |
Michal Simek | 5b815c9 | 2014-05-02 14:15:27 +0200 | [diff] [blame] | 198 | static int zynq_dma_xfer_init(bitstream_type bstype) |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 199 | { |
| 200 | u32 status, control, isr_status; |
| 201 | unsigned long ts; |
| 202 | |
Soren Brinkmann | 5f93227 | 2013-06-14 17:43:24 -0700 | [diff] [blame] | 203 | /* Clear loopback bit */ |
| 204 | clrbits_le32(&devcfg_base->mctrl, DEVCFG_MCTRL_PCAP_LPBK); |
| 205 | |
Michal Simek | 5b815c9 | 2014-05-02 14:15:27 +0200 | [diff] [blame] | 206 | if (bstype != BIT_PARTIAL) { |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 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); |
Siva Durga Prasad Paladugu | 71723aa | 2018-03-06 17:37:09 +0530 | [diff] [blame] | 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 | |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 220 | /* Setting PCFG_PROG_B signal to low */ |
| 221 | writel(control & ~DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl); |
| 222 | |
Siva Durga Prasad Paladugu | 71723aa | 2018-03-06 17:37:09 +0530 | [diff] [blame] | 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 | |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 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 | |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 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 | |
Siva Durga Prasad Paladugu | 3108185 | 2014-03-13 11:57:34 +0530 | [diff] [blame] | 339 | static int zynq_validate_bitstream(xilinx_desc *desc, const void *buf, |
| 340 | size_t bsize, u32 blocksize, u32 *swap, |
Michal Simek | 5b815c9 | 2014-05-02 14:15:27 +0200 | [diff] [blame] | 341 | bitstream_type *bstype) |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 342 | { |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 343 | u32 *buf_start; |
Siva Durga Prasad Paladugu | 3108185 | 2014-03-13 11:57:34 +0530 | [diff] [blame] | 344 | u32 diff; |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 345 | |
Siva Durga Prasad Paladugu | 3108185 | 2014-03-13 11:57:34 +0530 | [diff] [blame] | 346 | buf_start = check_data((u8 *)buf, blocksize, swap); |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 347 | |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 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 | |
Michal Simek | 5b815c9 | 2014-05-02 14:15:27 +0200 | [diff] [blame] | 365 | if (zynq_dma_xfer_init(*bstype)) |
Siva Durga Prasad Paladugu | 3108185 | 2014-03-13 11:57:34 +0530 | [diff] [blame] | 366 | return FPGA_FAIL; |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
Michal Simek | 7a78bd2 | 2014-05-02 14:09:30 +0200 | [diff] [blame] | 371 | static int zynq_load(xilinx_desc *desc, const void *buf, size_t bsize, |
| 372 | bitstream_type bstype) |
Siva Durga Prasad Paladugu | 3108185 | 2014-03-13 11:57:34 +0530 | [diff] [blame] | 373 | { |
| 374 | unsigned long ts; /* Timestamp */ |
Siva Durga Prasad Paladugu | 3108185 | 2014-03-13 11:57:34 +0530 | [diff] [blame] | 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, |
Michal Simek | 5b815c9 | 2014-05-02 14:15:27 +0200 | [diff] [blame] | 382 | &bstype)) |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 383 | return FPGA_FAIL; |
| 384 | |
| 385 | buf = zynq_align_dma_buffer((u32 *)buf, bsize, swap); |
| 386 | |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 387 | debug("%s: Source = 0x%08X\n", __func__, (u32)buf); |
| 388 | debug("%s: Size = %zu\n", __func__, bsize); |
| 389 | |
Jagannadha Sutradharudu Teki | ec4b73f | 2013-09-20 18:39:47 +0530 | [diff] [blame] | 390 | /* flush(clean & invalidate) d-cache range buf */ |
| 391 | flush_dcache_range((u32)buf, (u32)buf + |
| 392 | roundup(bsize, ARCH_DMA_MINALIGN)); |
| 393 | |
Siva Durga Prasad Paladugu | a0735a3 | 2014-03-12 17:09:26 +0530 | [diff] [blame] | 394 | if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0)) |
| 395 | return FPGA_FAIL; |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 396 | |
| 397 | isr_status = readl(&devcfg_base->int_sts); |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 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 | |
Michal Simek | 5b815c9 | 2014-05-02 14:15:27 +0200 | [diff] [blame] | 411 | if (bstype != BIT_PARTIAL) |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 412 | zynq_slcr_devcfg_enable(); |
| 413 | |
Siva Durga Prasad Paladugu | 31f7ce7 | 2019-03-23 16:01:36 +0530 | [diff] [blame] | 414 | puts("INFO:post config was not run, please run manually if needed\n"); |
| 415 | |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 416 | return FPGA_SUCCESS; |
| 417 | } |
| 418 | |
Luis Araneda | d600c4f | 2018-07-19 03:10:17 -0400 | [diff] [blame] | 419 | #if defined(CONFIG_CMD_FPGA_LOADFS) && !defined(CONFIG_SPL_BUILD) |
Siva Durga Prasad Paladugu | 1a89766 | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 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; |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 426 | loff_t blocksize, actread; |
| 427 | loff_t pos = 0; |
Siva Durga Prasad Paladugu | 1a89766 | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 428 | int fstype; |
Tien Fong Chee | 3003c44 | 2019-02-15 15:57:07 +0800 | [diff] [blame] | 429 | char *interface, *dev_part; |
| 430 | const char *filename; |
Siva Durga Prasad Paladugu | 1a89766 | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 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 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 441 | if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0) |
Siva Durga Prasad Paladugu | 1a89766 | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 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) { |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 464 | if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0) |
Siva Durga Prasad Paladugu | 1a89766 | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 465 | return FPGA_FAIL; |
| 466 | } else { |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 467 | if (fs_read(filename, (u32) buf, pos, bsize, &actread) < 0) |
Siva Durga Prasad Paladugu | 1a89766 | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 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 | |
Michal Simek | 14cfc4f | 2014-03-13 13:07:57 +0100 | [diff] [blame] | 501 | struct xilinx_fpga_op zynq_op = { |
| 502 | .load = zynq_load, |
Luis Araneda | d600c4f | 2018-07-19 03:10:17 -0400 | [diff] [blame] | 503 | #if defined(CONFIG_CMD_FPGA_LOADFS) && !defined(CONFIG_SPL_BUILD) |
Siva Durga Prasad Paladugu | 1a89766 | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 504 | .loadfs = zynq_loadfs, |
| 505 | #endif |
Michal Simek | 14cfc4f | 2014-03-13 13:07:57 +0100 | [diff] [blame] | 506 | }; |
Siva Durga Prasad Paladugu | 37e3a36 | 2018-06-26 15:02:19 +0530 | [diff] [blame] | 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 |