blob: 9499d1a01444df074baf4dbd978c5537c575b701 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Tien Fong Chee1d675f32017-07-26 13:05:43 +08002/*
3 * Copyright (C) 2017 Intel Corporation <www.intel.com>
Tien Fong Chee1d675f32017-07-26 13:05:43 +08004 */
5
6#include <asm/io.h>
7#include <asm/arch/fpga_manager.h>
8#include <asm/arch/reset_manager.h>
9#include <asm/arch/system_manager.h>
10#include <asm/arch/sdram.h>
11#include <asm/arch/misc.h>
12#include <altera.h>
13#include <common.h>
14#include <errno.h>
15#include <wait_bit.h>
16#include <watchdog.h>
17
18#define CFGWDTH_32 1
19#define MIN_BITSTREAM_SIZECHECK 230
20#define ENCRYPTION_OFFSET 69
21#define COMPRESSION_OFFSET 229
22#define FPGA_TIMEOUT_MSEC 1000 /* timeout in ms */
23#define FPGA_TIMEOUT_CNT 0x1000000
24
Tien Fong Chee1d675f32017-07-26 13:05:43 +080025static const struct socfpga_fpga_manager *fpga_manager_base =
26 (void *)SOCFPGA_FPGAMGRREGS_ADDRESS;
27
28static const struct socfpga_system_manager *system_manager_base =
29 (void *)SOCFPGA_SYSMGR_ADDRESS;
30
31static void fpgamgr_set_cd_ratio(unsigned long ratio);
32
33static uint32_t fpgamgr_get_msel(void)
34{
35 u32 reg;
36
37 reg = readl(&fpga_manager_base->imgcfg_stat);
38 reg = (reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL_SET_MSD) >>
39 ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL0_LSB;
40
41 return reg;
42}
43
44static void fpgamgr_set_cfgwdth(int width)
45{
46 if (width)
47 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
48 ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET_MSK);
49 else
50 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
51 ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET_MSK);
52}
53
54int is_fpgamgr_user_mode(void)
55{
56 return (readl(&fpga_manager_base->imgcfg_stat) &
57 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) != 0;
58}
59
60static int wait_for_user_mode(void)
61{
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010062 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat,
Tien Fong Chee1d675f32017-07-26 13:05:43 +080063 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK,
64 1, FPGA_TIMEOUT_MSEC, false);
65}
66
67static int is_fpgamgr_early_user_mode(void)
68{
69 return (readl(&fpga_manager_base->imgcfg_stat) &
70 ALT_FPGAMGR_IMGCFG_STAT_F2S_EARLY_USERMODE_SET_MSK) != 0;
71}
72
73int fpgamgr_wait_early_user_mode(void)
74{
75 u32 sync_data = 0xffffffff;
76 u32 i = 0;
77 unsigned start = get_timer(0);
78 unsigned long cd_ratio;
79
80 /* Getting existing CDRATIO */
81 cd_ratio = (readl(&fpga_manager_base->imgcfg_ctrl_02) &
82 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK) >>
83 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_LSB;
84
85 /* Using CDRATIO_X1 for better compatibility */
86 fpgamgr_set_cd_ratio(CDRATIO_x1);
87
88 while (!is_fpgamgr_early_user_mode()) {
89 if (get_timer(start) > FPGA_TIMEOUT_MSEC)
90 return -ETIMEDOUT;
91 fpgamgr_program_write((const long unsigned int *)&sync_data,
92 sizeof(sync_data));
93 udelay(FPGA_TIMEOUT_MSEC);
94 i++;
95 }
96
Tien Fong Chee7f3dace2019-05-07 17:42:26 +080097 debug("FPGA: Additional %i sync word needed\n", i);
Tien Fong Chee1d675f32017-07-26 13:05:43 +080098
99 /* restoring original CDRATIO */
100 fpgamgr_set_cd_ratio(cd_ratio);
101
102 return 0;
103}
104
105/* Read f2s_nconfig_pin and f2s_nstatus_pin; loop until de-asserted */
106static int wait_for_nconfig_pin_and_nstatus_pin(void)
107{
108 unsigned long mask = ALT_FPGAMGR_IMGCFG_STAT_F2S_NCONFIG_PIN_SET_MSK |
109 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK;
110
Tien Fong Chee2c0f3ae2017-12-05 15:57:58 +0800111 /*
112 * Poll until f2s_nconfig_pin and f2s_nstatus_pin; loop until
113 * de-asserted, timeout at 1000ms
Tien Fong Chee1d675f32017-07-26 13:05:43 +0800114 */
Tien Fong Chee2c0f3ae2017-12-05 15:57:58 +0800115 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat, mask,
116 true, FPGA_TIMEOUT_MSEC, false);
Tien Fong Chee1d675f32017-07-26 13:05:43 +0800117}
118
119static int wait_for_f2s_nstatus_pin(unsigned long value)
120{
121 /* Poll until f2s to specific value, timeout at 1000ms */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100122 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat,
123 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK,
124 value, FPGA_TIMEOUT_MSEC, false);
Tien Fong Chee1d675f32017-07-26 13:05:43 +0800125}
126
127/* set CD ratio */
128static void fpgamgr_set_cd_ratio(unsigned long ratio)
129{
130 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
131 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK);
132
133 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
134 (ratio << ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_LSB) &
135 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK);
136}
137
138/* get the MSEL value, verify we are set for FPP configuration mode */
139static int fpgamgr_verify_msel(void)
140{
141 u32 msel = fpgamgr_get_msel();
142
143 if (msel & ~BIT(0)) {
144 printf("Fail: read msel=%d\n", msel);
145 return -EPERM;
146 }
147
148 return 0;
149}
150
151/*
152 * Write cdratio and cdwidth based on whether the bitstream is compressed
153 * and/or encoded
154 */
155static int fpgamgr_set_cdratio_cdwidth(unsigned int cfg_width, u32 *rbf_data,
156 size_t rbf_size)
157{
158 unsigned int cd_ratio;
159 bool encrypt, compress;
160
161 /*
162 * According to the bitstream specification,
163 * both encryption and compression status are
164 * in location before offset 230 of the buffer.
165 */
166 if (rbf_size < MIN_BITSTREAM_SIZECHECK)
167 return -EINVAL;
168
169 encrypt = (rbf_data[ENCRYPTION_OFFSET] >> 2) & 3;
170 encrypt = encrypt != 0;
171
172 compress = (rbf_data[COMPRESSION_OFFSET] >> 1) & 1;
173 compress = !compress;
174
Tien Fong Chee7f3dace2019-05-07 17:42:26 +0800175 debug("FPGA: Header word %d = %08x.\n", 69, rbf_data[69]);
176 debug("FPGA: Header word %d = %08x.\n", 229, rbf_data[229]);
177 debug("FPGA: Read from rbf header: encrypt=%d compress=%d.\n", encrypt,
178 compress);
Tien Fong Chee1d675f32017-07-26 13:05:43 +0800179
180 /*
181 * from the register map description of cdratio in imgcfg_ctrl_02:
182 * Normal Configuration : 32bit Passive Parallel
183 * Partial Reconfiguration : 16bit Passive Parallel
184 */
185
186 /*
187 * cd ratio is dependent on cfg width and whether the bitstream
188 * is encrypted and/or compressed.
189 *
190 * | width | encr. | compr. | cd ratio |
191 * | 16 | 0 | 0 | 1 |
192 * | 16 | 0 | 1 | 4 |
193 * | 16 | 1 | 0 | 2 |
194 * | 16 | 1 | 1 | 4 |
195 * | 32 | 0 | 0 | 1 |
196 * | 32 | 0 | 1 | 8 |
197 * | 32 | 1 | 0 | 4 |
198 * | 32 | 1 | 1 | 8 |
199 */
200 if (!compress && !encrypt) {
201 cd_ratio = CDRATIO_x1;
202 } else {
203 if (compress)
204 cd_ratio = CDRATIO_x4;
205 else
206 cd_ratio = CDRATIO_x2;
207
208 /* if 32 bit, double the cd ratio (so register
209 field setting is incremented) */
210 if (cfg_width == CFGWDTH_32)
211 cd_ratio += 1;
212 }
213
214 fpgamgr_set_cfgwdth(cfg_width);
215 fpgamgr_set_cd_ratio(cd_ratio);
216
217 return 0;
218}
219
220static int fpgamgr_reset(void)
221{
222 unsigned long reg;
223
224 /* S2F_NCONFIG = 0 */
225 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
226 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
227
228 /* Wait for f2s_nstatus == 0 */
229 if (wait_for_f2s_nstatus_pin(0))
230 return -ETIME;
231
232 /* S2F_NCONFIG = 1 */
233 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
234 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
235
236 /* Wait for f2s_nstatus == 1 */
237 if (wait_for_f2s_nstatus_pin(1))
238 return -ETIME;
239
240 /* read and confirm f2s_condone_pin = 0 and f2s_condone_oe = 1 */
241 reg = readl(&fpga_manager_base->imgcfg_stat);
242 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) != 0)
243 return -EPERM;
244
245 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_OE_SET_MSK) == 0)
246 return -EPERM;
247
248 return 0;
249}
250
251/* Start the FPGA programming by initialize the FPGA Manager */
252int fpgamgr_program_init(u32 * rbf_data, size_t rbf_size)
253{
254 int ret;
255
256 /* Step 1 */
257 if (fpgamgr_verify_msel())
258 return -EPERM;
259
260 /* Step 2 */
261 if (fpgamgr_set_cdratio_cdwidth(CFGWDTH_32, rbf_data, rbf_size))
262 return -EPERM;
263
264 /*
265 * Step 3:
266 * Make sure no other external devices are trying to interfere with
267 * programming:
268 */
269 if (wait_for_nconfig_pin_and_nstatus_pin())
270 return -ETIME;
271
272 /*
273 * Step 4:
274 * Deassert the signal drives from HPS
275 *
276 * S2F_NCE = 1
277 * S2F_PR_REQUEST = 0
278 * EN_CFG_CTRL = 0
279 * EN_CFG_DATA = 0
280 * S2F_NCONFIG = 1
281 * S2F_NSTATUS_OE = 0
282 * S2F_CONDONE_OE = 0
283 */
284 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
285 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
286
287 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
288 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_PR_REQUEST_SET_MSK);
289
290 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
291 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
292 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
293
294 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
295 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
296
297 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
298 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NSTATUS_OE_SET_MSK |
299 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_CONDONE_OE_SET_MSK);
300
301 /*
302 * Step 5:
303 * Enable overrides
304 * S2F_NENABLE_CONFIG = 0
305 * S2F_NENABLE_NCONFIG = 0
306 */
307 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
308 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NENABLE_CONFIG_SET_MSK);
309 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
310 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NCONFIG_SET_MSK);
311
312 /*
313 * Disable driving signals that HPS doesn't need to drive.
314 * S2F_NENABLE_NSTATUS = 1
315 * S2F_NENABLE_CONDONE = 1
316 */
317 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
318 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NSTATUS_SET_MSK |
319 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_CONDONE_SET_MSK);
320
321 /*
322 * Step 6:
323 * Drive chip select S2F_NCE = 0
324 */
325 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
326 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
327
328 /* Step 7 */
329 if (wait_for_nconfig_pin_and_nstatus_pin())
330 return -ETIME;
331
332 /* Step 8 */
333 ret = fpgamgr_reset();
334
335 if (ret)
336 return ret;
337
338 /*
339 * Step 9:
340 * EN_CFG_CTRL and EN_CFG_DATA = 1
341 */
342 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
343 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
344 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
345
346 return 0;
347}
348
349/* Ensure the FPGA entering config done */
350static int fpgamgr_program_poll_cd(void)
351{
352 unsigned long reg, i;
353
354 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
355 reg = readl(&fpga_manager_base->imgcfg_stat);
356 if (reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK)
357 return 0;
358
359 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK) == 0) {
360 printf("nstatus == 0 while waiting for condone\n");
361 return -EPERM;
362 }
Tien Fong Chee725b1222019-05-07 17:42:27 +0800363 WATCHDOG_RESET();
Tien Fong Chee1d675f32017-07-26 13:05:43 +0800364 }
365
366 if (i == FPGA_TIMEOUT_CNT)
367 return -ETIME;
368
369 return 0;
370}
371
372/* Ensure the FPGA entering user mode */
373static int fpgamgr_program_poll_usermode(void)
374{
375 unsigned long reg;
376 int ret = 0;
377
378 if (fpgamgr_dclkcnt_set(0xf))
379 return -ETIME;
380
381 ret = wait_for_user_mode();
382 if (ret < 0) {
383 printf("%s: Failed to enter user mode with ", __func__);
384 printf("error code %d\n", ret);
385 return ret;
386 }
387
388 /*
389 * Step 14:
390 * Stop DATA path and Dclk
391 * EN_CFG_CTRL and EN_CFG_DATA = 0
392 */
393 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
394 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
395 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
396
397 /*
398 * Step 15:
399 * Disable overrides
400 * S2F_NENABLE_CONFIG = 1
401 * S2F_NENABLE_NCONFIG = 1
402 */
403 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
404 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NENABLE_CONFIG_SET_MSK);
405 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
406 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NCONFIG_SET_MSK);
407
408 /* Disable chip select S2F_NCE = 1 */
409 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
410 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
411
412 /*
413 * Step 16:
414 * Final check
415 */
416 reg = readl(&fpga_manager_base->imgcfg_stat);
417 if (((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) !=
418 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) ||
419 ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) !=
420 ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) ||
421 ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK) !=
422 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK))
423 return -EPERM;
424
425 return 0;
426}
427
428int fpgamgr_program_finish(void)
429{
430 /* Ensure the FPGA entering config done */
431 int status = fpgamgr_program_poll_cd();
432
433 if (status) {
434 printf("FPGA: Poll CD failed with error code %d\n", status);
435 return -EPERM;
436 }
Tien Fong Chee1d675f32017-07-26 13:05:43 +0800437
438 /* Ensure the FPGA entering user mode */
439 status = fpgamgr_program_poll_usermode();
440 if (status) {
441 printf("FPGA: Poll usermode failed with error code %d\n",
442 status);
443 return -EPERM;
444 }
445
446 printf("Full Configuration Succeeded.\n");
447
448 return 0;
449}
450
451/*
452 * FPGA Manager to program the FPGA. This is the interface used by FPGA driver.
453 * Return 0 for sucess, non-zero for error.
454 */
455int socfpga_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size)
456{
Simon Goldschmidt40034482018-10-15 20:35:10 +0200457 int status;
Tien Fong Chee1d675f32017-07-26 13:05:43 +0800458
Tien Fong Chee7f3dace2019-05-07 17:42:26 +0800459 /* Disable all signals from hps peripheral controller to fpga */
Tien Fong Chee1d675f32017-07-26 13:05:43 +0800460 writel(0, &system_manager_base->fpgaintf_en_global);
461
Tien Fong Chee7f3dace2019-05-07 17:42:26 +0800462 /* Disable all axi bridge (hps2fpga, lwhps2fpga & fpga2hps) */
Tien Fong Chee1d675f32017-07-26 13:05:43 +0800463 socfpga_bridges_reset();
464
465 /* Initialize the FPGA Manager */
466 status = fpgamgr_program_init((u32 *)rbf_data, rbf_size);
467 if (status)
468 return status;
469
470 /* Write the RBF data to FPGA Manager */
471 fpgamgr_program_write(rbf_data, rbf_size);
472
473 return fpgamgr_program_finish();
474}