blob: b995bcc008b13e5585f3ea09f746bb3a67a52386 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +05302/*
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 Paladugu460fdce2016-01-13 16:25:37 +05306 */
7
8#include <console.h>
9#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +053012#include <zynqmppl.h>
Ibai Erkiagac8a3efa2019-09-27 11:37:01 +010013#include <zynqmp_firmware.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +053015#include <linux/sizes.h>
Siva Durga Prasad Paladugu4e985892017-02-17 16:16:01 +053016#include <asm/arch/sys_proto.h>
Siva Durga Prasad Paladugue4d16c22018-03-15 00:17:24 +053017#include <memalign.h>
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +053018
19#define DUMMY_WORD 0xffffffff
20
21/* Xilinx binary format header */
22static const u32 bin_format[] = {
23 DUMMY_WORD, /* Dummy words */
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 DUMMY_WORD,
37 DUMMY_WORD,
38 DUMMY_WORD,
39 0x000000bb, /* Sync word */
40 0x11220044, /* Sync word */
41 DUMMY_WORD,
42 DUMMY_WORD,
43 0xaa995566, /* Sync word */
44};
45
46#define SWAP_NO 1
47#define SWAP_DONE 2
48
49/*
50 * Load the whole word from unaligned buffer
51 * Keep in your mind that it is byte loading on little-endian system
52 */
53static u32 load_word(const void *buf, u32 swap)
54{
55 u32 word = 0;
56 u8 *bitc = (u8 *)buf;
57 int p;
58
59 if (swap == SWAP_NO) {
60 for (p = 0; p < 4; p++) {
61 word <<= 8;
62 word |= bitc[p];
63 }
64 } else {
65 for (p = 3; p >= 0; p--) {
66 word <<= 8;
67 word |= bitc[p];
68 }
69 }
70
71 return word;
72}
73
74static u32 check_header(const void *buf)
75{
76 u32 i, pattern;
77 int swap = SWAP_NO;
78 u32 *test = (u32 *)buf;
79
80 debug("%s: Let's check bitstream header\n", __func__);
81
82 /* Checking that passing bin is not a bitstream */
83 for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
84 pattern = load_word(&test[i], swap);
85
86 /*
87 * Bitstreams in binary format are swapped
88 * compare to regular bistream.
89 * Do not swap dummy word but if swap is done assume
90 * that parsing buffer is binary format
91 */
92 if ((__swab32(pattern) != DUMMY_WORD) &&
93 (__swab32(pattern) == bin_format[i])) {
94 swap = SWAP_DONE;
95 debug("%s: data swapped - let's swap\n", __func__);
96 }
97
98 debug("%s: %d/%px: pattern %x/%x bin_format\n", __func__, i,
99 &test[i], pattern, bin_format[i]);
100 }
101 debug("%s: Found bitstream header at %px %s swapinng\n", __func__,
102 buf, swap == SWAP_NO ? "without" : "with");
103
104 return swap;
105}
106
107static void *check_data(u8 *buf, size_t bsize, u32 *swap)
108{
109 u32 word, p = 0; /* possition */
110
111 /* Because buf doesn't need to be aligned let's read it by chars */
112 for (p = 0; p < bsize; p++) {
113 word = load_word(&buf[p], SWAP_NO);
114 debug("%s: word %x %x/%px\n", __func__, word, p, &buf[p]);
115
116 /* Find the first bitstream dummy word */
117 if (word == DUMMY_WORD) {
118 debug("%s: Found dummy word at position %x/%px\n",
119 __func__, p, &buf[p]);
120 *swap = check_header(&buf[p]);
121 if (*swap) {
122 /* FIXME add full bitstream checking here */
123 return &buf[p];
124 }
125 }
126 /* Loop can be huge - support CTRL + C */
127 if (ctrlc())
128 return NULL;
129 }
130 return NULL;
131}
132
133static ulong zynqmp_align_dma_buffer(u32 *buf, u32 len, u32 swap)
134{
135 u32 *new_buf;
136 u32 i;
137
138 if ((ulong)buf != ALIGN((ulong)buf, ARCH_DMA_MINALIGN)) {
139 new_buf = (u32 *)ALIGN((ulong)buf, ARCH_DMA_MINALIGN);
140
141 /*
142 * This might be dangerous but permits to flash if
143 * ARCH_DMA_MINALIGN is greater than header size
144 */
145 if (new_buf > (u32 *)buf) {
146 debug("%s: Aligned buffer is after buffer start\n",
147 __func__);
148 new_buf -= ARCH_DMA_MINALIGN;
149 }
150 printf("%s: Align buffer at %px to %px(swap %d)\n", __func__,
151 buf, new_buf, swap);
152
153 for (i = 0; i < (len/4); i++)
154 new_buf[i] = load_word(&buf[i], swap);
155
156 buf = new_buf;
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530157 } else if ((swap != SWAP_DONE) &&
Ibai Erkiaga6aa5bc82019-09-27 11:37:02 +0100158 (zynqmp_firmware_version() <= PMUFW_V1_0)) {
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530159 /* For bitstream which are aligned */
Michal Simek27121142019-08-02 12:43:29 +0200160 new_buf = buf;
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530161
162 printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
163 swap);
164
165 for (i = 0; i < (len/4); i++)
166 new_buf[i] = load_word(&buf[i], swap);
167 }
168
169 return (ulong)buf;
170}
171
172static int zynqmp_validate_bitstream(xilinx_desc *desc, const void *buf,
173 size_t bsize, u32 blocksize, u32 *swap)
174{
175 ulong *buf_start;
176 ulong diff;
177
178 buf_start = check_data((u8 *)buf, blocksize, swap);
179
180 if (!buf_start)
181 return FPGA_FAIL;
182
183 /* Check if data is postpone from start */
184 diff = (ulong)buf_start - (ulong)buf;
185 if (diff) {
186 printf("%s: Bitstream is not validated yet (diff %lx)\n",
187 __func__, diff);
188 return FPGA_FAIL;
189 }
190
191 if ((ulong)buf < SZ_1M) {
192 printf("%s: Bitstream has to be placed up to 1MB (%px)\n",
193 __func__, buf);
194 return FPGA_FAIL;
195 }
196
197 return 0;
198}
199
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530200static int zynqmp_load(xilinx_desc *desc, const void *buf, size_t bsize,
201 bitstream_type bstype)
202{
Siva Durga Prasad Paladugue4d16c22018-03-15 00:17:24 +0530203 ALLOC_CACHE_ALIGN_BUFFER(u32, bsizeptr, 1);
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530204 u32 swap = 0;
Siva Durga Prasad Paladugu4e985892017-02-17 16:16:01 +0530205 ulong bin_buf;
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530206 int ret;
Siva Durga Prasad Paladugu4e985892017-02-17 16:16:01 +0530207 u32 buf_lo, buf_hi;
208 u32 ret_payload[PAYLOAD_ARG_CNT];
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530209 bool xilfpga_old = false;
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530210
Ibai Erkiaga6aa5bc82019-09-27 11:37:02 +0100211 if (zynqmp_firmware_version() <= PMUFW_V1_0) {
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530212 puts("WARN: PMUFW v1.0 or less is detected\n");
213 puts("WARN: Not all bitstream formats are supported\n");
214 puts("WARN: Please upgrade PMUFW\n");
215 xilfpga_old = true;
216 if (zynqmp_validate_bitstream(desc, buf, bsize, bsize, &swap))
217 return FPGA_FAIL;
218 bsizeptr = (u32 *)&bsize;
219 flush_dcache_range((ulong)bsizeptr,
220 (ulong)bsizeptr + sizeof(size_t));
221 bstype |= BIT(ZYNQMP_FPGA_BIT_NS);
222 }
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530223
224 bin_buf = zynqmp_align_dma_buffer((u32 *)buf, bsize, swap);
225
226 debug("%s called!\n", __func__);
227 flush_dcache_range(bin_buf, bin_buf + bsize);
228
Siva Durga Prasad Paladugu4e985892017-02-17 16:16:01 +0530229 buf_lo = (u32)bin_buf;
230 buf_hi = upper_32_bits(bin_buf);
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530231
232 if (xilfpga_old)
Michal Simek4c3de372019-10-04 15:35:45 +0200233 ret = xilinx_pm_request(ZYNQMP_SIP_SVC_PM_FPGA_LOAD, buf_lo,
234 buf_hi, (u32)(uintptr_t)bsizeptr,
235 bstype, ret_payload);
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530236 else
Michal Simek4c3de372019-10-04 15:35:45 +0200237 ret = xilinx_pm_request(ZYNQMP_SIP_SVC_PM_FPGA_LOAD, buf_lo,
238 buf_hi, (u32)bsize, 0, ret_payload);
Siva Durga Prasad Paladugu62fe3132018-08-21 15:44:50 +0530239
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530240 if (ret)
Luca Ceresoliadd7f41d2019-01-11 17:09:45 +0100241 puts("PL FPGA LOAD fail\n");
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530242
243 return ret;
244}
245
Siva Durga Prasad Paladugu6809e812018-05-31 15:10:23 +0530246#if defined(CONFIG_CMD_FPGA_LOAD_SECURE) && !defined(CONFIG_SPL_BUILD)
247static int zynqmp_loads(xilinx_desc *desc, const void *buf, size_t bsize,
248 struct fpga_secure_info *fpga_sec_info)
249{
250 int ret;
251 u32 buf_lo, buf_hi;
252 u32 ret_payload[PAYLOAD_ARG_CNT];
253 u8 flag = 0;
254
255 flush_dcache_range((ulong)buf, (ulong)buf +
256 ALIGN(bsize, CONFIG_SYS_CACHELINE_SIZE));
257
258 if (!fpga_sec_info->encflag)
259 flag |= BIT(ZYNQMP_FPGA_BIT_ENC_DEV_KEY);
260
261 if (fpga_sec_info->userkey_addr &&
262 fpga_sec_info->encflag == FPGA_ENC_USR_KEY) {
263 flush_dcache_range((ulong)fpga_sec_info->userkey_addr,
264 (ulong)fpga_sec_info->userkey_addr +
265 ALIGN(KEY_PTR_LEN,
266 CONFIG_SYS_CACHELINE_SIZE));
267 flag |= BIT(ZYNQMP_FPGA_BIT_ENC_USR_KEY);
268 }
269
270 if (!fpga_sec_info->authflag)
271 flag |= BIT(ZYNQMP_FPGA_BIT_AUTH_OCM);
272
273 if (fpga_sec_info->authflag == ZYNQMP_FPGA_AUTH_DDR)
274 flag |= BIT(ZYNQMP_FPGA_BIT_AUTH_DDR);
275
276 buf_lo = lower_32_bits((ulong)buf);
277 buf_hi = upper_32_bits((ulong)buf);
278
Michal Simek4c3de372019-10-04 15:35:45 +0200279 ret = xilinx_pm_request(ZYNQMP_SIP_SVC_PM_FPGA_LOAD, buf_lo,
280 buf_hi,
Siva Durga Prasad Paladugu6809e812018-05-31 15:10:23 +0530281 (u32)(uintptr_t)fpga_sec_info->userkey_addr,
282 flag, ret_payload);
283 if (ret)
284 puts("PL FPGA LOAD fail\n");
285 else
286 puts("Bitstream successfully loaded\n");
287
288 return ret;
289}
290#endif
291
Nitin Jaind9361d42018-02-16 17:29:54 +0530292static int zynqmp_pcap_info(xilinx_desc *desc)
293{
294 int ret;
295 u32 ret_payload[PAYLOAD_ARG_CNT];
296
Michal Simek4c3de372019-10-04 15:35:45 +0200297 ret = xilinx_pm_request(ZYNQMP_SIP_SVC_PM_FPGA_STATUS, 0, 0, 0,
298 0, ret_payload);
Nitin Jaind9361d42018-02-16 17:29:54 +0530299 if (!ret)
300 printf("PCAP status\t0x%x\n", ret_payload[1]);
301
302 return ret;
303}
304
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530305struct xilinx_fpga_op zynqmp_op = {
306 .load = zynqmp_load,
Siva Durga Prasad Paladugu6809e812018-05-31 15:10:23 +0530307#if defined CONFIG_CMD_FPGA_LOAD_SECURE
308 .loads = zynqmp_loads,
309#endif
Nitin Jaind9361d42018-02-16 17:29:54 +0530310 .info = zynqmp_pcap_info,
Siva Durga Prasad Paladugu460fdce2016-01-13 16:25:37 +0530311};