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>
13 #include <asm/arch/sys_proto.h>
15 #define DUMMY_WORD 0xffffffff
17 /* Xilinx binary format header */
18 static const u32 bin_format
[] = {
19 DUMMY_WORD
, /* Dummy words */
35 0x000000bb, /* Sync word */
36 0x11220044, /* Sync word */
39 0xaa995566, /* Sync word */
46 * Load the whole word from unaligned buffer
47 * Keep in your mind that it is byte loading on little-endian system
49 static u32
load_word(const void *buf
, u32 swap
)
55 if (swap
== SWAP_NO
) {
56 for (p
= 0; p
< 4; p
++) {
61 for (p
= 3; p
>= 0; p
--) {
70 static u32
check_header(const void *buf
)
74 u32
*test
= (u32
*)buf
;
76 debug("%s: Let's check bitstream header\n", __func__
);
78 /* Checking that passing bin is not a bitstream */
79 for (i
= 0; i
< ARRAY_SIZE(bin_format
); i
++) {
80 pattern
= load_word(&test
[i
], swap
);
83 * Bitstreams in binary format are swapped
84 * compare to regular bistream.
85 * Do not swap dummy word but if swap is done assume
86 * that parsing buffer is binary format
88 if ((__swab32(pattern
) != DUMMY_WORD
) &&
89 (__swab32(pattern
) == bin_format
[i
])) {
91 debug("%s: data swapped - let's swap\n", __func__
);
94 debug("%s: %d/%px: pattern %x/%x bin_format\n", __func__
, i
,
95 &test
[i
], pattern
, bin_format
[i
]);
97 debug("%s: Found bitstream header at %px %s swapinng\n", __func__
,
98 buf
, swap
== SWAP_NO
? "without" : "with");
103 static void *check_data(u8
*buf
, size_t bsize
, u32
*swap
)
105 u32 word
, p
= 0; /* possition */
107 /* Because buf doesn't need to be aligned let's read it by chars */
108 for (p
= 0; p
< bsize
; p
++) {
109 word
= load_word(&buf
[p
], SWAP_NO
);
110 debug("%s: word %x %x/%px\n", __func__
, word
, p
, &buf
[p
]);
112 /* Find the first bitstream dummy word */
113 if (word
== DUMMY_WORD
) {
114 debug("%s: Found dummy word at position %x/%px\n",
115 __func__
, p
, &buf
[p
]);
116 *swap
= check_header(&buf
[p
]);
118 /* FIXME add full bitstream checking here */
122 /* Loop can be huge - support CTRL + C */
129 static ulong
zynqmp_align_dma_buffer(u32
*buf
, u32 len
, u32 swap
)
134 if ((ulong
)buf
!= ALIGN((ulong
)buf
, ARCH_DMA_MINALIGN
)) {
135 new_buf
= (u32
*)ALIGN((ulong
)buf
, ARCH_DMA_MINALIGN
);
138 * This might be dangerous but permits to flash if
139 * ARCH_DMA_MINALIGN is greater than header size
141 if (new_buf
> (u32
*)buf
) {
142 debug("%s: Aligned buffer is after buffer start\n",
144 new_buf
-= ARCH_DMA_MINALIGN
;
146 printf("%s: Align buffer at %px to %px(swap %d)\n", __func__
,
149 for (i
= 0; i
< (len
/4); i
++)
150 new_buf
[i
] = load_word(&buf
[i
], swap
);
153 } else if (swap
!= SWAP_DONE
) {
154 /* For bitstream which are aligned */
155 u32
*new_buf
= (u32
*)buf
;
157 printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__
,
160 for (i
= 0; i
< (len
/4); i
++)
161 new_buf
[i
] = load_word(&buf
[i
], swap
);
167 static int zynqmp_validate_bitstream(xilinx_desc
*desc
, const void *buf
,
168 size_t bsize
, u32 blocksize
, u32
*swap
)
173 buf_start
= check_data((u8
*)buf
, blocksize
, swap
);
178 /* Check if data is postpone from start */
179 diff
= (ulong
)buf_start
- (ulong
)buf
;
181 printf("%s: Bitstream is not validated yet (diff %lx)\n",
186 if ((ulong
)buf
< SZ_1M
) {
187 printf("%s: Bitstream has to be placed up to 1MB (%px)\n",
195 static int zynqmp_load(xilinx_desc
*desc
, const void *buf
, size_t bsize
,
196 bitstream_type bstype
)
202 u32 ret_payload
[PAYLOAD_ARG_CNT
];
204 if (zynqmp_validate_bitstream(desc
, buf
, bsize
, bsize
, &swap
))
207 bin_buf
= zynqmp_align_dma_buffer((u32
*)buf
, bsize
, swap
);
209 debug("%s called!\n", __func__
);
210 flush_dcache_range(bin_buf
, bin_buf
+ bsize
);
213 bsize
= bsize
/ 4 + 1;
217 buf_lo
= (u32
)bin_buf
;
218 buf_hi
= upper_32_bits(bin_buf
);
219 ret
= invoke_smc(ZYNQMP_SIP_SVC_PM_FPGA_LOAD
, buf_lo
, buf_hi
, bsize
,
220 bstype
, ret_payload
);
222 debug("PL FPGA LOAD fail\n");
227 struct xilinx_fpga_op zynqmp_op
= {