1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2015 - 2016, Xilinx, Inc,
15 #include <zynqmp_firmware.h>
16 #include <asm/cache.h>
17 #include <linux/bitops.h>
18 #include <linux/sizes.h>
19 #include <asm/arch/sys_proto.h>
22 #define DUMMY_WORD 0xffffffff
24 /* Xilinx binary format header */
25 static const u32 bin_format[] = {
26 DUMMY_WORD, /* Dummy words */
42 0x000000bb, /* Sync word */
43 0x11220044, /* Sync word */
46 0xaa995566, /* Sync word */
53 * Load the whole word from unaligned buffer
54 * Keep in your mind that it is byte loading on little-endian system
56 static u32 load_word(const void *buf, u32 swap)
62 if (swap == SWAP_NO) {
63 for (p = 0; p < 4; p++) {
68 for (p = 3; p >= 0; p--) {
77 static u32 check_header(const void *buf)
81 u32 *test = (u32 *)buf;
83 debug("%s: Let's check bitstream header\n", __func__);
85 /* Checking that passing bin is not a bitstream */
86 for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
87 pattern = load_word(&test[i], swap);
90 * Bitstreams in binary format are swapped
91 * compare to regular bistream.
92 * Do not swap dummy word but if swap is done assume
93 * that parsing buffer is binary format
95 if ((__swab32(pattern) != DUMMY_WORD) &&
96 (__swab32(pattern) == bin_format[i])) {
98 debug("%s: data swapped - let's swap\n", __func__);
101 debug("%s: %d/%px: pattern %x/%x bin_format\n", __func__, i,
102 &test[i], pattern, bin_format[i]);
104 debug("%s: Found bitstream header at %px %s swapinng\n", __func__,
105 buf, swap == SWAP_NO ? "without" : "with");
110 static void *check_data(u8 *buf, size_t bsize, u32 *swap)
112 u32 word, p = 0; /* possition */
114 /* Because buf doesn't need to be aligned let's read it by chars */
115 for (p = 0; p < bsize; p++) {
116 word = load_word(&buf[p], SWAP_NO);
117 debug("%s: word %x %x/%px\n", __func__, word, p, &buf[p]);
119 /* Find the first bitstream dummy word */
120 if (word == DUMMY_WORD) {
121 debug("%s: Found dummy word at position %x/%px\n",
122 __func__, p, &buf[p]);
123 *swap = check_header(&buf[p]);
125 /* FIXME add full bitstream checking here */
129 /* Loop can be huge - support CTRL + C */
136 static ulong zynqmp_align_dma_buffer(u32 *buf, u32 len, u32 swap)
141 if ((ulong)buf != ALIGN((ulong)buf, ARCH_DMA_MINALIGN)) {
142 new_buf = (u32 *)ALIGN((ulong)buf, ARCH_DMA_MINALIGN);
145 * This might be dangerous but permits to flash if
146 * ARCH_DMA_MINALIGN is greater than header size
148 if (new_buf > (u32 *)buf) {
149 debug("%s: Aligned buffer is after buffer start\n",
151 new_buf -= ARCH_DMA_MINALIGN;
153 printf("%s: Align buffer at %px to %px(swap %d)\n", __func__,
156 for (i = 0; i < (len/4); i++)
157 new_buf[i] = load_word(&buf[i], swap);
160 } else if ((swap != SWAP_DONE) &&
161 (zynqmp_firmware_version() <= PMUFW_V1_0)) {
162 /* For bitstream which are aligned */
165 printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
168 for (i = 0; i < (len/4); i++)
169 new_buf[i] = load_word(&buf[i], swap);
175 static int zynqmp_validate_bitstream(xilinx_desc *desc, const void *buf,
176 size_t bsize, u32 blocksize, u32 *swap)
181 buf_start = check_data((u8 *)buf, blocksize, swap);
186 /* Check if data is postpone from start */
187 diff = (ulong)buf_start - (ulong)buf;
189 printf("%s: Bitstream is not validated yet (diff %lx)\n",
194 if ((ulong)buf < SZ_1M) {
195 printf("%s: Bitstream has to be placed up to 1MB (%px)\n",
203 #if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
204 static int zynqmp_check_compatible(xilinx_desc *desc, int flags)
207 * If no flags set, the image may be legacy, but we need to
208 * signal caller this situation with specific error code.
213 /* For legacy bitstream images no need for other methods exist */
214 if ((flags & desc->flags) && flags == FPGA_LEGACY)
218 * Other images are handled in secure callback loads(). Check
219 * callback existence besides image type support.
221 if (desc->operations->loads && (flags & desc->flags))
228 static int zynqmp_load(xilinx_desc *desc, const void *buf, size_t bsize,
229 bitstream_type bstype, int flags)
231 ALLOC_CACHE_ALIGN_BUFFER(u32, bsizeptr, 1);
236 u32 bsize_req = (u32)bsize;
237 u32 ret_payload[PAYLOAD_ARG_CNT];
238 #if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
239 struct fpga_secure_info info = { 0 };
241 ret = zynqmp_check_compatible(desc, flags);
243 if (ret != -ENODATA) {
244 puts("Missing loads() operation or unsupported bitstream type\n");
247 /* If flags is not set, the image treats as legacy */
253 break; /* Handle the legacy image later in this function */
254 #if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
255 case FPGA_XILINX_ZYNQMP_DDRAUTH:
256 /* DDR authentication */
257 info.authflag = ZYNQMP_FPGA_AUTH_DDR;
258 info.encflag = FPGA_NO_ENC_OR_NO_AUTH;
259 return desc->operations->loads(desc, buf, bsize, &info);
260 case FPGA_XILINX_ZYNQMP_ENC:
261 /* Encryption using device key */
262 info.authflag = FPGA_NO_ENC_OR_NO_AUTH;
263 info.encflag = FPGA_ENC_DEV_KEY;
264 return desc->operations->loads(desc, buf, bsize, &info);
267 printf("Unsupported bitstream type %d\n", flags);
272 if (zynqmp_firmware_version() <= PMUFW_V1_0) {
273 puts("WARN: PMUFW v1.0 or less is detected\n");
274 puts("WARN: Not all bitstream formats are supported\n");
275 puts("WARN: Please upgrade PMUFW\n");
276 if (zynqmp_validate_bitstream(desc, buf, bsize, bsize, &swap))
278 bsizeptr = (u32 *)&bsize;
279 flush_dcache_range((ulong)bsizeptr,
280 (ulong)bsizeptr + sizeof(size_t));
281 bsize_req = (u32)(uintptr_t)bsizeptr;
282 bstype |= BIT(ZYNQMP_FPGA_BIT_NS);
287 bin_buf = zynqmp_align_dma_buffer((u32 *)buf, bsize, swap);
289 flush_dcache_range(bin_buf, bin_buf + bsize);
291 buf_lo = (u32)bin_buf;
292 buf_hi = upper_32_bits(bin_buf);
294 ret = xilinx_pm_request(PM_FPGA_LOAD, buf_lo, buf_hi,
295 bsize_req, bstype, ret_payload);
297 printf("PL FPGA LOAD failed with err: 0x%08x\n", ret);
302 #if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
303 static int zynqmp_loads(xilinx_desc *desc, const void *buf, size_t bsize,
304 struct fpga_secure_info *fpga_sec_info)
308 u32 ret_payload[PAYLOAD_ARG_CNT];
311 flush_dcache_range((ulong)buf, (ulong)buf +
312 ALIGN(bsize, CONFIG_SYS_CACHELINE_SIZE));
314 if (!fpga_sec_info->encflag)
315 flag |= BIT(ZYNQMP_FPGA_BIT_ENC_DEV_KEY);
317 if (fpga_sec_info->userkey_addr &&
318 fpga_sec_info->encflag == FPGA_ENC_USR_KEY) {
319 flush_dcache_range((ulong)fpga_sec_info->userkey_addr,
320 (ulong)fpga_sec_info->userkey_addr +
322 CONFIG_SYS_CACHELINE_SIZE));
323 flag |= BIT(ZYNQMP_FPGA_BIT_ENC_USR_KEY);
326 if (!fpga_sec_info->authflag)
327 flag |= BIT(ZYNQMP_FPGA_BIT_AUTH_OCM);
329 if (fpga_sec_info->authflag == ZYNQMP_FPGA_AUTH_DDR)
330 flag |= BIT(ZYNQMP_FPGA_BIT_AUTH_DDR);
332 buf_lo = lower_32_bits((ulong)buf);
333 buf_hi = upper_32_bits((ulong)buf);
335 ret = xilinx_pm_request(PM_FPGA_LOAD, buf_lo,
337 (u32)(uintptr_t)fpga_sec_info->userkey_addr,
340 puts("PL FPGA LOAD fail\n");
342 puts("Bitstream successfully loaded\n");
348 static int zynqmp_pcap_info(xilinx_desc *desc)
351 u32 ret_payload[PAYLOAD_ARG_CNT];
353 ret = xilinx_pm_request(PM_FPGA_GET_STATUS, 0, 0, 0,
356 printf("PCAP status\t0x%x\n", ret_payload[1]);
361 static int __maybe_unused zynqmp_str2flag(xilinx_desc *desc, const char *str)
363 if (!strncmp(str, "u-boot,fpga-legacy", 18))
365 #if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
366 if (!strncmp(str, "u-boot,zynqmp-fpga-ddrauth", 26))
367 return FPGA_XILINX_ZYNQMP_DDRAUTH;
369 if (!strncmp(str, "u-boot,zynqmp-fpga-enc", 22))
370 return FPGA_XILINX_ZYNQMP_ENC;
375 struct xilinx_fpga_op zynqmp_op = {
377 .info = zynqmp_pcap_info,
378 #if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE)
379 .loads = zynqmp_loads,
380 .str2flag = zynqmp_str2flag,