2 * (C) Copyright 2015 - 2016, Xilinx, Inc,
3 * Michal Simek <michal.simek@xilinx.com>
4 * Siva Durga Prasad <siva.durga.paladugu@xilinx.com>
6 * SPDX-License-Identifier: GPL-2.0
12 #include <linux/sizes.h>
14 #define DUMMY_WORD 0xffffffff
16 /* Xilinx binary format header */
17 static const u32 bin_format[] = {
18 DUMMY_WORD, /* Dummy words */
34 0x000000bb, /* Sync word */
35 0x11220044, /* Sync word */
38 0xaa995566, /* Sync word */
45 * Load the whole word from unaligned buffer
46 * Keep in your mind that it is byte loading on little-endian system
48 static u32 load_word(const void *buf, u32 swap)
54 if (swap == SWAP_NO) {
55 for (p = 0; p < 4; p++) {
60 for (p = 3; p >= 0; p--) {
69 static u32 check_header(const void *buf)
73 u32 *test = (u32 *)buf;
75 debug("%s: Let's check bitstream header\n", __func__);
77 /* Checking that passing bin is not a bitstream */
78 for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
79 pattern = load_word(&test[i], swap);
82 * Bitstreams in binary format are swapped
83 * compare to regular bistream.
84 * Do not swap dummy word but if swap is done assume
85 * that parsing buffer is binary format
87 if ((__swab32(pattern) != DUMMY_WORD) &&
88 (__swab32(pattern) == bin_format[i])) {
90 debug("%s: data swapped - let's swap\n", __func__);
93 debug("%s: %d/%px: pattern %x/%x bin_format\n", __func__, i,
94 &test[i], pattern, bin_format[i]);
96 debug("%s: Found bitstream header at %px %s swapinng\n", __func__,
97 buf, swap == SWAP_NO ? "without" : "with");
102 static void *check_data(u8 *buf, size_t bsize, u32 *swap)
104 u32 word, p = 0; /* possition */
106 /* Because buf doesn't need to be aligned let's read it by chars */
107 for (p = 0; p < bsize; p++) {
108 word = load_word(&buf[p], SWAP_NO);
109 debug("%s: word %x %x/%px\n", __func__, word, p, &buf[p]);
111 /* Find the first bitstream dummy word */
112 if (word == DUMMY_WORD) {
113 debug("%s: Found dummy word at position %x/%px\n",
114 __func__, p, &buf[p]);
115 *swap = check_header(&buf[p]);
117 /* FIXME add full bitstream checking here */
121 /* Loop can be huge - support CTRL + C */
128 static ulong zynqmp_align_dma_buffer(u32 *buf, u32 len, u32 swap)
133 if ((ulong)buf != ALIGN((ulong)buf, ARCH_DMA_MINALIGN)) {
134 new_buf = (u32 *)ALIGN((ulong)buf, ARCH_DMA_MINALIGN);
137 * This might be dangerous but permits to flash if
138 * ARCH_DMA_MINALIGN is greater than header size
140 if (new_buf > (u32 *)buf) {
141 debug("%s: Aligned buffer is after buffer start\n",
143 new_buf -= ARCH_DMA_MINALIGN;
145 printf("%s: Align buffer at %px to %px(swap %d)\n", __func__,
148 for (i = 0; i < (len/4); i++)
149 new_buf[i] = load_word(&buf[i], swap);
152 } else if (swap != SWAP_DONE) {
153 /* For bitstream which are aligned */
154 u32 *new_buf = (u32 *)buf;
156 printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
159 for (i = 0; i < (len/4); i++)
160 new_buf[i] = load_word(&buf[i], swap);
166 static int zynqmp_validate_bitstream(xilinx_desc *desc, const void *buf,
167 size_t bsize, u32 blocksize, u32 *swap)
172 buf_start = check_data((u8 *)buf, blocksize, swap);
177 /* Check if data is postpone from start */
178 diff = (ulong)buf_start - (ulong)buf;
180 printf("%s: Bitstream is not validated yet (diff %lx)\n",
185 if ((ulong)buf < SZ_1M) {
186 printf("%s: Bitstream has to be placed up to 1MB (%px)\n",
194 static int invoke_smc(ulong id, ulong reg0, ulong reg1, ulong reg2)
207 static int zynqmp_load(xilinx_desc *desc, const void *buf, size_t bsize,
208 bitstream_type bstype)
211 ulong bin_buf, flags;
214 if (zynqmp_validate_bitstream(desc, buf, bsize, bsize, &swap))
217 bin_buf = zynqmp_align_dma_buffer((u32 *)buf, bsize, swap);
219 debug("%s called!\n", __func__);
220 flush_dcache_range(bin_buf, bin_buf + bsize);
223 bsize = bsize / 4 + 1;
227 flags = (u32)bsize | ((u64)bstype << 32);
229 ret = invoke_smc(ZYNQMP_SIP_SVC_PM_FPGA_LOAD, bin_buf, flags, 0);
231 debug("PL FPGA LOAD fail\n");
236 struct xilinx_fpga_op zynqmp_op = {