1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2015 - 2016, Xilinx, Inc,
12 #include <zynqmp_firmware.h>
13 #include <asm/cache.h>
14 #include <linux/sizes.h>
15 #include <asm/arch/sys_proto.h>
18 #define DUMMY_WORD 0xffffffff
20 /* Xilinx binary format header */
21 static const u32 bin_format[] = {
22 DUMMY_WORD, /* Dummy words */
38 0x000000bb, /* Sync word */
39 0x11220044, /* Sync word */
42 0xaa995566, /* Sync word */
49 * Load the whole word from unaligned buffer
50 * Keep in your mind that it is byte loading on little-endian system
52 static u32 load_word(const void *buf, u32 swap)
58 if (swap == SWAP_NO) {
59 for (p = 0; p < 4; p++) {
64 for (p = 3; p >= 0; p--) {
73 static u32 check_header(const void *buf)
77 u32 *test = (u32 *)buf;
79 debug("%s: Let's check bitstream header\n", __func__);
81 /* Checking that passing bin is not a bitstream */
82 for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
83 pattern = load_word(&test[i], swap);
86 * Bitstreams in binary format are swapped
87 * compare to regular bistream.
88 * Do not swap dummy word but if swap is done assume
89 * that parsing buffer is binary format
91 if ((__swab32(pattern) != DUMMY_WORD) &&
92 (__swab32(pattern) == bin_format[i])) {
94 debug("%s: data swapped - let's swap\n", __func__);
97 debug("%s: %d/%px: pattern %x/%x bin_format\n", __func__, i,
98 &test[i], pattern, bin_format[i]);
100 debug("%s: Found bitstream header at %px %s swapinng\n", __func__,
101 buf, swap == SWAP_NO ? "without" : "with");
106 static void *check_data(u8 *buf, size_t bsize, u32 *swap)
108 u32 word, p = 0; /* possition */
110 /* Because buf doesn't need to be aligned let's read it by chars */
111 for (p = 0; p < bsize; p++) {
112 word = load_word(&buf[p], SWAP_NO);
113 debug("%s: word %x %x/%px\n", __func__, word, p, &buf[p]);
115 /* Find the first bitstream dummy word */
116 if (word == DUMMY_WORD) {
117 debug("%s: Found dummy word at position %x/%px\n",
118 __func__, p, &buf[p]);
119 *swap = check_header(&buf[p]);
121 /* FIXME add full bitstream checking here */
125 /* Loop can be huge - support CTRL + C */
132 static ulong zynqmp_align_dma_buffer(u32 *buf, u32 len, u32 swap)
137 if ((ulong)buf != ALIGN((ulong)buf, ARCH_DMA_MINALIGN)) {
138 new_buf = (u32 *)ALIGN((ulong)buf, ARCH_DMA_MINALIGN);
141 * This might be dangerous but permits to flash if
142 * ARCH_DMA_MINALIGN is greater than header size
144 if (new_buf > (u32 *)buf) {
145 debug("%s: Aligned buffer is after buffer start\n",
147 new_buf -= ARCH_DMA_MINALIGN;
149 printf("%s: Align buffer at %px to %px(swap %d)\n", __func__,
152 for (i = 0; i < (len/4); i++)
153 new_buf[i] = load_word(&buf[i], swap);
156 } else if ((swap != SWAP_DONE) &&
157 (zynqmp_firmware_version() <= PMUFW_V1_0)) {
158 /* For bitstream which are aligned */
161 printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
164 for (i = 0; i < (len/4); i++)
165 new_buf[i] = load_word(&buf[i], swap);
171 static int zynqmp_validate_bitstream(xilinx_desc *desc, const void *buf,
172 size_t bsize, u32 blocksize, u32 *swap)
177 buf_start = check_data((u8 *)buf, blocksize, swap);
182 /* Check if data is postpone from start */
183 diff = (ulong)buf_start - (ulong)buf;
185 printf("%s: Bitstream is not validated yet (diff %lx)\n",
190 if ((ulong)buf < SZ_1M) {
191 printf("%s: Bitstream has to be placed up to 1MB (%px)\n",
199 static int zynqmp_load(xilinx_desc *desc, const void *buf, size_t bsize,
200 bitstream_type bstype)
202 ALLOC_CACHE_ALIGN_BUFFER(u32, bsizeptr, 1);
207 u32 ret_payload[PAYLOAD_ARG_CNT];
208 bool xilfpga_old = false;
210 if (zynqmp_firmware_version() <= PMUFW_V1_0) {
211 puts("WARN: PMUFW v1.0 or less is detected\n");
212 puts("WARN: Not all bitstream formats are supported\n");
213 puts("WARN: Please upgrade PMUFW\n");
215 if (zynqmp_validate_bitstream(desc, buf, bsize, bsize, &swap))
217 bsizeptr = (u32 *)&bsize;
218 flush_dcache_range((ulong)bsizeptr,
219 (ulong)bsizeptr + sizeof(size_t));
220 bstype |= BIT(ZYNQMP_FPGA_BIT_NS);
223 bin_buf = zynqmp_align_dma_buffer((u32 *)buf, bsize, swap);
225 debug("%s called!\n", __func__);
226 flush_dcache_range(bin_buf, bin_buf + bsize);
228 buf_lo = (u32)bin_buf;
229 buf_hi = upper_32_bits(bin_buf);
232 ret = xilinx_pm_request(ZYNQMP_SIP_SVC_PM_FPGA_LOAD, buf_lo,
233 buf_hi, (u32)(uintptr_t)bsizeptr,
234 bstype, ret_payload);
236 ret = xilinx_pm_request(ZYNQMP_SIP_SVC_PM_FPGA_LOAD, buf_lo,
237 buf_hi, (u32)bsize, 0, ret_payload);
240 puts("PL FPGA LOAD fail\n");
245 #if defined(CONFIG_CMD_FPGA_LOAD_SECURE) && !defined(CONFIG_SPL_BUILD)
246 static int zynqmp_loads(xilinx_desc *desc, const void *buf, size_t bsize,
247 struct fpga_secure_info *fpga_sec_info)
251 u32 ret_payload[PAYLOAD_ARG_CNT];
254 flush_dcache_range((ulong)buf, (ulong)buf +
255 ALIGN(bsize, CONFIG_SYS_CACHELINE_SIZE));
257 if (!fpga_sec_info->encflag)
258 flag |= BIT(ZYNQMP_FPGA_BIT_ENC_DEV_KEY);
260 if (fpga_sec_info->userkey_addr &&
261 fpga_sec_info->encflag == FPGA_ENC_USR_KEY) {
262 flush_dcache_range((ulong)fpga_sec_info->userkey_addr,
263 (ulong)fpga_sec_info->userkey_addr +
265 CONFIG_SYS_CACHELINE_SIZE));
266 flag |= BIT(ZYNQMP_FPGA_BIT_ENC_USR_KEY);
269 if (!fpga_sec_info->authflag)
270 flag |= BIT(ZYNQMP_FPGA_BIT_AUTH_OCM);
272 if (fpga_sec_info->authflag == ZYNQMP_FPGA_AUTH_DDR)
273 flag |= BIT(ZYNQMP_FPGA_BIT_AUTH_DDR);
275 buf_lo = lower_32_bits((ulong)buf);
276 buf_hi = upper_32_bits((ulong)buf);
278 ret = xilinx_pm_request(ZYNQMP_SIP_SVC_PM_FPGA_LOAD, buf_lo,
280 (u32)(uintptr_t)fpga_sec_info->userkey_addr,
283 puts("PL FPGA LOAD fail\n");
285 puts("Bitstream successfully loaded\n");
291 static int zynqmp_pcap_info(xilinx_desc *desc)
294 u32 ret_payload[PAYLOAD_ARG_CNT];
296 ret = xilinx_pm_request(ZYNQMP_SIP_SVC_PM_FPGA_STATUS, 0, 0, 0,
299 printf("PCAP status\t0x%x\n", ret_payload[1]);
304 struct xilinx_fpga_op zynqmp_op = {
306 #if defined CONFIG_CMD_FPGA_LOAD_SECURE
307 .loads = zynqmp_loads,
309 .info = zynqmp_pcap_info,