Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 - 2016, Xilinx, Inc, |
| 4 | * Michal Simek <michal.simek@xilinx.com> |
| 5 | * Siva Durga Prasad <siva.durga.paladugu@xilinx.com> |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <console.h> |
| 9 | #include <common.h> |
| 10 | #include <zynqmppl.h> |
Ibai Erkiaga | c8a3efa | 2019-09-27 11:37:01 +0100 | [diff] [blame] | 11 | #include <zynqmp_firmware.h> |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 12 | #include <linux/sizes.h> |
Siva Durga Prasad Paladugu | 4e98589 | 2017-02-17 16:16:01 +0530 | [diff] [blame] | 13 | #include <asm/arch/sys_proto.h> |
Siva Durga Prasad Paladugu | e4d16c2 | 2018-03-15 00:17:24 +0530 | [diff] [blame] | 14 | #include <memalign.h> |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 15 | |
| 16 | #define DUMMY_WORD 0xffffffff |
| 17 | |
| 18 | /* Xilinx binary format header */ |
| 19 | static const u32 bin_format[] = { |
| 20 | DUMMY_WORD, /* Dummy words */ |
| 21 | DUMMY_WORD, |
| 22 | DUMMY_WORD, |
| 23 | DUMMY_WORD, |
| 24 | DUMMY_WORD, |
| 25 | DUMMY_WORD, |
| 26 | DUMMY_WORD, |
| 27 | DUMMY_WORD, |
| 28 | DUMMY_WORD, |
| 29 | DUMMY_WORD, |
| 30 | DUMMY_WORD, |
| 31 | DUMMY_WORD, |
| 32 | DUMMY_WORD, |
| 33 | DUMMY_WORD, |
| 34 | DUMMY_WORD, |
| 35 | DUMMY_WORD, |
| 36 | 0x000000bb, /* Sync word */ |
| 37 | 0x11220044, /* Sync word */ |
| 38 | DUMMY_WORD, |
| 39 | DUMMY_WORD, |
| 40 | 0xaa995566, /* Sync word */ |
| 41 | }; |
| 42 | |
| 43 | #define SWAP_NO 1 |
| 44 | #define SWAP_DONE 2 |
| 45 | |
| 46 | /* |
| 47 | * Load the whole word from unaligned buffer |
| 48 | * Keep in your mind that it is byte loading on little-endian system |
| 49 | */ |
| 50 | static u32 load_word(const void *buf, u32 swap) |
| 51 | { |
| 52 | u32 word = 0; |
| 53 | u8 *bitc = (u8 *)buf; |
| 54 | int p; |
| 55 | |
| 56 | if (swap == SWAP_NO) { |
| 57 | for (p = 0; p < 4; p++) { |
| 58 | word <<= 8; |
| 59 | word |= bitc[p]; |
| 60 | } |
| 61 | } else { |
| 62 | for (p = 3; p >= 0; p--) { |
| 63 | word <<= 8; |
| 64 | word |= bitc[p]; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return word; |
| 69 | } |
| 70 | |
| 71 | static u32 check_header(const void *buf) |
| 72 | { |
| 73 | u32 i, pattern; |
| 74 | int swap = SWAP_NO; |
| 75 | u32 *test = (u32 *)buf; |
| 76 | |
| 77 | debug("%s: Let's check bitstream header\n", __func__); |
| 78 | |
| 79 | /* Checking that passing bin is not a bitstream */ |
| 80 | for (i = 0; i < ARRAY_SIZE(bin_format); i++) { |
| 81 | pattern = load_word(&test[i], swap); |
| 82 | |
| 83 | /* |
| 84 | * Bitstreams in binary format are swapped |
| 85 | * compare to regular bistream. |
| 86 | * Do not swap dummy word but if swap is done assume |
| 87 | * that parsing buffer is binary format |
| 88 | */ |
| 89 | if ((__swab32(pattern) != DUMMY_WORD) && |
| 90 | (__swab32(pattern) == bin_format[i])) { |
| 91 | swap = SWAP_DONE; |
| 92 | debug("%s: data swapped - let's swap\n", __func__); |
| 93 | } |
| 94 | |
| 95 | debug("%s: %d/%px: pattern %x/%x bin_format\n", __func__, i, |
| 96 | &test[i], pattern, bin_format[i]); |
| 97 | } |
| 98 | debug("%s: Found bitstream header at %px %s swapinng\n", __func__, |
| 99 | buf, swap == SWAP_NO ? "without" : "with"); |
| 100 | |
| 101 | return swap; |
| 102 | } |
| 103 | |
| 104 | static void *check_data(u8 *buf, size_t bsize, u32 *swap) |
| 105 | { |
| 106 | u32 word, p = 0; /* possition */ |
| 107 | |
| 108 | /* Because buf doesn't need to be aligned let's read it by chars */ |
| 109 | for (p = 0; p < bsize; p++) { |
| 110 | word = load_word(&buf[p], SWAP_NO); |
| 111 | debug("%s: word %x %x/%px\n", __func__, word, p, &buf[p]); |
| 112 | |
| 113 | /* Find the first bitstream dummy word */ |
| 114 | if (word == DUMMY_WORD) { |
| 115 | debug("%s: Found dummy word at position %x/%px\n", |
| 116 | __func__, p, &buf[p]); |
| 117 | *swap = check_header(&buf[p]); |
| 118 | if (*swap) { |
| 119 | /* FIXME add full bitstream checking here */ |
| 120 | return &buf[p]; |
| 121 | } |
| 122 | } |
| 123 | /* Loop can be huge - support CTRL + C */ |
| 124 | if (ctrlc()) |
| 125 | return NULL; |
| 126 | } |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | static ulong zynqmp_align_dma_buffer(u32 *buf, u32 len, u32 swap) |
| 131 | { |
| 132 | u32 *new_buf; |
| 133 | u32 i; |
| 134 | |
| 135 | if ((ulong)buf != ALIGN((ulong)buf, ARCH_DMA_MINALIGN)) { |
| 136 | new_buf = (u32 *)ALIGN((ulong)buf, ARCH_DMA_MINALIGN); |
| 137 | |
| 138 | /* |
| 139 | * This might be dangerous but permits to flash if |
| 140 | * ARCH_DMA_MINALIGN is greater than header size |
| 141 | */ |
| 142 | if (new_buf > (u32 *)buf) { |
| 143 | debug("%s: Aligned buffer is after buffer start\n", |
| 144 | __func__); |
| 145 | new_buf -= ARCH_DMA_MINALIGN; |
| 146 | } |
| 147 | printf("%s: Align buffer at %px to %px(swap %d)\n", __func__, |
| 148 | buf, new_buf, swap); |
| 149 | |
| 150 | for (i = 0; i < (len/4); i++) |
| 151 | new_buf[i] = load_word(&buf[i], swap); |
| 152 | |
| 153 | buf = new_buf; |
Siva Durga Prasad Paladugu | 62fe313 | 2018-08-21 15:44:50 +0530 | [diff] [blame] | 154 | } else if ((swap != SWAP_DONE) && |
Ibai Erkiaga | 6aa5bc8 | 2019-09-27 11:37:02 +0100 | [diff] [blame^] | 155 | (zynqmp_firmware_version() <= PMUFW_V1_0)) { |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 156 | /* For bitstream which are aligned */ |
Michal Simek | 2712114 | 2019-08-02 12:43:29 +0200 | [diff] [blame] | 157 | new_buf = buf; |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 158 | |
| 159 | printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__, |
| 160 | swap); |
| 161 | |
| 162 | for (i = 0; i < (len/4); i++) |
| 163 | new_buf[i] = load_word(&buf[i], swap); |
| 164 | } |
| 165 | |
| 166 | return (ulong)buf; |
| 167 | } |
| 168 | |
| 169 | static int zynqmp_validate_bitstream(xilinx_desc *desc, const void *buf, |
| 170 | size_t bsize, u32 blocksize, u32 *swap) |
| 171 | { |
| 172 | ulong *buf_start; |
| 173 | ulong diff; |
| 174 | |
| 175 | buf_start = check_data((u8 *)buf, blocksize, swap); |
| 176 | |
| 177 | if (!buf_start) |
| 178 | return FPGA_FAIL; |
| 179 | |
| 180 | /* Check if data is postpone from start */ |
| 181 | diff = (ulong)buf_start - (ulong)buf; |
| 182 | if (diff) { |
| 183 | printf("%s: Bitstream is not validated yet (diff %lx)\n", |
| 184 | __func__, diff); |
| 185 | return FPGA_FAIL; |
| 186 | } |
| 187 | |
| 188 | if ((ulong)buf < SZ_1M) { |
| 189 | printf("%s: Bitstream has to be placed up to 1MB (%px)\n", |
| 190 | __func__, buf); |
| 191 | return FPGA_FAIL; |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 197 | static int zynqmp_load(xilinx_desc *desc, const void *buf, size_t bsize, |
| 198 | bitstream_type bstype) |
| 199 | { |
Siva Durga Prasad Paladugu | e4d16c2 | 2018-03-15 00:17:24 +0530 | [diff] [blame] | 200 | ALLOC_CACHE_ALIGN_BUFFER(u32, bsizeptr, 1); |
Siva Durga Prasad Paladugu | 62fe313 | 2018-08-21 15:44:50 +0530 | [diff] [blame] | 201 | u32 swap = 0; |
Siva Durga Prasad Paladugu | 4e98589 | 2017-02-17 16:16:01 +0530 | [diff] [blame] | 202 | ulong bin_buf; |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 203 | int ret; |
Siva Durga Prasad Paladugu | 4e98589 | 2017-02-17 16:16:01 +0530 | [diff] [blame] | 204 | u32 buf_lo, buf_hi; |
| 205 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
Siva Durga Prasad Paladugu | 62fe313 | 2018-08-21 15:44:50 +0530 | [diff] [blame] | 206 | bool xilfpga_old = false; |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 207 | |
Ibai Erkiaga | 6aa5bc8 | 2019-09-27 11:37:02 +0100 | [diff] [blame^] | 208 | if (zynqmp_firmware_version() <= PMUFW_V1_0) { |
Siva Durga Prasad Paladugu | 62fe313 | 2018-08-21 15:44:50 +0530 | [diff] [blame] | 209 | puts("WARN: PMUFW v1.0 or less is detected\n"); |
| 210 | puts("WARN: Not all bitstream formats are supported\n"); |
| 211 | puts("WARN: Please upgrade PMUFW\n"); |
| 212 | xilfpga_old = true; |
| 213 | if (zynqmp_validate_bitstream(desc, buf, bsize, bsize, &swap)) |
| 214 | return FPGA_FAIL; |
| 215 | bsizeptr = (u32 *)&bsize; |
| 216 | flush_dcache_range((ulong)bsizeptr, |
| 217 | (ulong)bsizeptr + sizeof(size_t)); |
| 218 | bstype |= BIT(ZYNQMP_FPGA_BIT_NS); |
| 219 | } |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 220 | |
| 221 | bin_buf = zynqmp_align_dma_buffer((u32 *)buf, bsize, swap); |
| 222 | |
| 223 | debug("%s called!\n", __func__); |
| 224 | flush_dcache_range(bin_buf, bin_buf + bsize); |
| 225 | |
Siva Durga Prasad Paladugu | 4e98589 | 2017-02-17 16:16:01 +0530 | [diff] [blame] | 226 | buf_lo = (u32)bin_buf; |
| 227 | buf_hi = upper_32_bits(bin_buf); |
Siva Durga Prasad Paladugu | 62fe313 | 2018-08-21 15:44:50 +0530 | [diff] [blame] | 228 | |
| 229 | if (xilfpga_old) |
| 230 | ret = invoke_smc(ZYNQMP_SIP_SVC_PM_FPGA_LOAD, buf_lo, buf_hi, |
| 231 | (u32)(uintptr_t)bsizeptr, bstype, ret_payload); |
| 232 | else |
| 233 | ret = invoke_smc(ZYNQMP_SIP_SVC_PM_FPGA_LOAD, buf_lo, buf_hi, |
| 234 | (u32)bsize, 0, ret_payload); |
| 235 | |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 236 | if (ret) |
Luca Ceresoli | add7f41d | 2019-01-11 17:09:45 +0100 | [diff] [blame] | 237 | puts("PL FPGA LOAD fail\n"); |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
Siva Durga Prasad Paladugu | 6809e81 | 2018-05-31 15:10:23 +0530 | [diff] [blame] | 242 | #if defined(CONFIG_CMD_FPGA_LOAD_SECURE) && !defined(CONFIG_SPL_BUILD) |
| 243 | static int zynqmp_loads(xilinx_desc *desc, const void *buf, size_t bsize, |
| 244 | struct fpga_secure_info *fpga_sec_info) |
| 245 | { |
| 246 | int ret; |
| 247 | u32 buf_lo, buf_hi; |
| 248 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 249 | u8 flag = 0; |
| 250 | |
| 251 | flush_dcache_range((ulong)buf, (ulong)buf + |
| 252 | ALIGN(bsize, CONFIG_SYS_CACHELINE_SIZE)); |
| 253 | |
| 254 | if (!fpga_sec_info->encflag) |
| 255 | flag |= BIT(ZYNQMP_FPGA_BIT_ENC_DEV_KEY); |
| 256 | |
| 257 | if (fpga_sec_info->userkey_addr && |
| 258 | fpga_sec_info->encflag == FPGA_ENC_USR_KEY) { |
| 259 | flush_dcache_range((ulong)fpga_sec_info->userkey_addr, |
| 260 | (ulong)fpga_sec_info->userkey_addr + |
| 261 | ALIGN(KEY_PTR_LEN, |
| 262 | CONFIG_SYS_CACHELINE_SIZE)); |
| 263 | flag |= BIT(ZYNQMP_FPGA_BIT_ENC_USR_KEY); |
| 264 | } |
| 265 | |
| 266 | if (!fpga_sec_info->authflag) |
| 267 | flag |= BIT(ZYNQMP_FPGA_BIT_AUTH_OCM); |
| 268 | |
| 269 | if (fpga_sec_info->authflag == ZYNQMP_FPGA_AUTH_DDR) |
| 270 | flag |= BIT(ZYNQMP_FPGA_BIT_AUTH_DDR); |
| 271 | |
| 272 | buf_lo = lower_32_bits((ulong)buf); |
| 273 | buf_hi = upper_32_bits((ulong)buf); |
| 274 | |
| 275 | ret = invoke_smc(ZYNQMP_SIP_SVC_PM_FPGA_LOAD, buf_lo, buf_hi, |
| 276 | (u32)(uintptr_t)fpga_sec_info->userkey_addr, |
| 277 | flag, ret_payload); |
| 278 | if (ret) |
| 279 | puts("PL FPGA LOAD fail\n"); |
| 280 | else |
| 281 | puts("Bitstream successfully loaded\n"); |
| 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | #endif |
| 286 | |
Nitin Jain | d9361d4 | 2018-02-16 17:29:54 +0530 | [diff] [blame] | 287 | static int zynqmp_pcap_info(xilinx_desc *desc) |
| 288 | { |
| 289 | int ret; |
| 290 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 291 | |
| 292 | ret = invoke_smc(ZYNQMP_SIP_SVC_PM_FPGA_STATUS, 0, 0, 0, |
| 293 | 0, ret_payload); |
| 294 | if (!ret) |
| 295 | printf("PCAP status\t0x%x\n", ret_payload[1]); |
| 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 300 | struct xilinx_fpga_op zynqmp_op = { |
| 301 | .load = zynqmp_load, |
Siva Durga Prasad Paladugu | 6809e81 | 2018-05-31 15:10:23 +0530 | [diff] [blame] | 302 | #if defined CONFIG_CMD_FPGA_LOAD_SECURE |
| 303 | .loads = zynqmp_loads, |
| 304 | #endif |
Nitin Jain | d9361d4 | 2018-02-16 17:29:54 +0530 | [diff] [blame] | 305 | .info = zynqmp_pcap_info, |
Siva Durga Prasad Paladugu | 460fdce | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 306 | }; |