blob: b450a1b1be0a8f1d439fcbb9499ed761af0d8219 [file] [log] [blame]
Tom Rini4606fc72018-05-20 09:47:45 -04001// SPDX-License-Identifier: GPL-2.0
Tien Fong Chee402735b2017-12-05 15:58:02 +08002/*
3 * Copyright (C) 2017 Intel Corporation <www.intel.com>
Tien Fong Chee402735b2017-12-05 15:58:02 +08004 */
5
6#include <common.h>
7#include <errno.h>
8#include <fdtdec.h>
9#include <malloc.h>
10#include <wait_bit.h>
11#include <watchdog.h>
12#include <asm/io.h>
13#include <asm/arch/fpga_manager.h>
14#include <asm/arch/misc.h>
15#include <asm/arch/reset_manager.h>
16#include <asm/arch/sdram.h>
17#include <linux/kernel.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21static void sdram_mmr_init(void);
22static u64 sdram_size_calc(void);
23
24/* FAWBANK - Number of Bank of a given device involved in the FAW period. */
25#define ARRIA10_SDR_ACTIVATE_FAWBANK (0x1)
26
27#define ARRIA_DDR_CONFIG(A, B, C, R) \
28 (((A) << 24) | ((B) << 16) | ((C) << 8) | (R))
29#define DDR_CONFIG_ELEMENTS ARRAY_SIZE(ddr_config)
30#define DDR_REG_SEQ2CORE 0xFFD0507C
31#define DDR_REG_CORE2SEQ 0xFFD05078
32#define DDR_READ_LATENCY_DELAY 40
33#define DDR_SIZE_2GB_HEX 0x80000000
Tien Fong Chee402735b2017-12-05 15:58:02 +080034
35#define IO48_MMR_DRAMSTS 0xFFCFA0EC
36#define IO48_MMR_NIOS2_RESERVE0 0xFFCFA110
37#define IO48_MMR_NIOS2_RESERVE1 0xFFCFA114
38#define IO48_MMR_NIOS2_RESERVE2 0xFFCFA118
39
40#define SEQ2CORE_MASK 0xF
41#define CORE2SEQ_INT_REQ 0xF
42#define SEQ2CORE_INT_RESP_BIT 3
43
44static const struct socfpga_ecc_hmc *socfpga_ecc_hmc_base =
45 (void *)SOCFPGA_SDR_ADDRESS;
46static const struct socfpga_noc_ddr_scheduler *socfpga_noc_ddr_scheduler_base =
47 (void *)SOCFPGA_SDR_SCHEDULER_ADDRESS;
48static const struct socfpga_noc_fw_ddr_mpu_fpga2sdram
49 *socfpga_noc_fw_ddr_mpu_fpga2sdram_base =
50 (void *)SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS;
51static const struct socfpga_noc_fw_ddr_l3 *socfpga_noc_fw_ddr_l3_base =
52 (void *)SOCFPGA_SDR_FIREWALL_L3_ADDRESS;
53static const struct socfpga_io48_mmr *socfpga_io48_mmr_base =
54 (void *)SOCFPGA_HMC_MMR_IO48_ADDRESS;
55
56/* The following are the supported configurations */
57static u32 ddr_config[] = {
58 /* Chip - Row - Bank - Column Style */
59 /* All Types */
60 ARRIA_DDR_CONFIG(0, 3, 10, 12),
61 ARRIA_DDR_CONFIG(0, 3, 10, 13),
62 ARRIA_DDR_CONFIG(0, 3, 10, 14),
63 ARRIA_DDR_CONFIG(0, 3, 10, 15),
64 ARRIA_DDR_CONFIG(0, 3, 10, 16),
65 ARRIA_DDR_CONFIG(0, 3, 10, 17),
66 /* LPDDR x16 */
67 ARRIA_DDR_CONFIG(0, 3, 11, 14),
68 ARRIA_DDR_CONFIG(0, 3, 11, 15),
69 ARRIA_DDR_CONFIG(0, 3, 11, 16),
70 ARRIA_DDR_CONFIG(0, 3, 12, 15),
71 /* DDR4 Only */
72 ARRIA_DDR_CONFIG(0, 4, 10, 14),
73 ARRIA_DDR_CONFIG(0, 4, 10, 15),
74 ARRIA_DDR_CONFIG(0, 4, 10, 16),
75 ARRIA_DDR_CONFIG(0, 4, 10, 17), /* 14 */
76 /* Chip - Bank - Row - Column Style */
77 ARRIA_DDR_CONFIG(1, 3, 10, 12),
78 ARRIA_DDR_CONFIG(1, 3, 10, 13),
79 ARRIA_DDR_CONFIG(1, 3, 10, 14),
80 ARRIA_DDR_CONFIG(1, 3, 10, 15),
81 ARRIA_DDR_CONFIG(1, 3, 10, 16),
82 ARRIA_DDR_CONFIG(1, 3, 10, 17),
83 ARRIA_DDR_CONFIG(1, 3, 11, 14),
84 ARRIA_DDR_CONFIG(1, 3, 11, 15),
85 ARRIA_DDR_CONFIG(1, 3, 11, 16),
86 ARRIA_DDR_CONFIG(1, 3, 12, 15),
87 /* DDR4 Only */
88 ARRIA_DDR_CONFIG(1, 4, 10, 14),
89 ARRIA_DDR_CONFIG(1, 4, 10, 15),
90 ARRIA_DDR_CONFIG(1, 4, 10, 16),
91 ARRIA_DDR_CONFIG(1, 4, 10, 17),
92};
93
94static int match_ddr_conf(u32 ddr_conf)
95{
96 int i;
97
98 for (i = 0; i < DDR_CONFIG_ELEMENTS; i++) {
99 if (ddr_conf == ddr_config[i])
100 return i;
101 }
102 return 0;
103}
104
105/* Check whether SDRAM is successfully Calibrated */
106static int is_sdram_cal_success(void)
107{
108 return readl(&socfpga_ecc_hmc_base->ddrcalstat);
109}
110
111static unsigned char ddr_get_bit(u32 ereg, unsigned char bit)
112{
113 u32 reg = readl(ereg);
114
115 return (reg & BIT(bit)) ? 1 : 0;
116}
117
118static unsigned char ddr_wait_bit(u32 ereg, u32 bit,
119 u32 expected, u32 timeout_usec)
120{
121 u32 tmr;
122
123 for (tmr = 0; tmr < timeout_usec; tmr += 100) {
124 udelay(100);
125 WATCHDOG_RESET();
126 if (ddr_get_bit(ereg, bit) == expected)
127 return 0;
128 }
129
130 return 1;
131}
132
133static int emif_clear(void)
134{
Tien Fong Chee402735b2017-12-05 15:58:02 +0800135 writel(0, DDR_REG_CORE2SEQ);
136
Marek Vasuta9c04702019-03-08 19:11:55 +0100137 return wait_for_bit_le32((u32 *)DDR_REG_SEQ2CORE,
138 SEQ2CORE_MASK, 0, 1000, 0);
Tien Fong Chee402735b2017-12-05 15:58:02 +0800139}
140
141static int emif_reset(void)
142{
143 u32 c2s, s2c;
Marek Vasuta9c04702019-03-08 19:11:55 +0100144 int ret;
Tien Fong Chee402735b2017-12-05 15:58:02 +0800145
146 c2s = readl(DDR_REG_CORE2SEQ);
147 s2c = readl(DDR_REG_SEQ2CORE);
148
149 debug("c2s=%08x s2c=%08x nr0=%08x nr1=%08x nr2=%08x dst=%08x\n",
150 c2s, s2c, readl(IO48_MMR_NIOS2_RESERVE0),
151 readl(IO48_MMR_NIOS2_RESERVE1),
152 readl(IO48_MMR_NIOS2_RESERVE2),
153 readl(IO48_MMR_DRAMSTS));
154
Marek Vasuta9c04702019-03-08 19:11:55 +0100155 if (s2c & SEQ2CORE_MASK) {
156 ret = emif_clear();
157 if (ret) {
158 debug("failed emif_clear()\n");
159 return -EPERM;
160 }
Tien Fong Chee402735b2017-12-05 15:58:02 +0800161 }
162
163 writel(CORE2SEQ_INT_REQ, DDR_REG_CORE2SEQ);
164
165 if (ddr_wait_bit(DDR_REG_SEQ2CORE, SEQ2CORE_INT_RESP_BIT, 0, 1000000)) {
166 debug("emif_reset failed to see interrupt acknowledge\n");
167 return -EPERM;
168 } else {
169 debug("emif_reset interrupt acknowledged\n");
170 }
171
Marek Vasuta9c04702019-03-08 19:11:55 +0100172 ret = emif_clear();
173 if (ret) {
Tien Fong Chee402735b2017-12-05 15:58:02 +0800174 debug("emif_clear() failed\n");
175 return -EPERM;
176 }
177 debug("emif_reset interrupt cleared\n");
178
179 debug("nr0=%08x nr1=%08x nr2=%08x\n",
180 readl(IO48_MMR_NIOS2_RESERVE0),
181 readl(IO48_MMR_NIOS2_RESERVE1),
182 readl(IO48_MMR_NIOS2_RESERVE2));
183
184 return 0;
185}
186
187static int ddr_setup(void)
188{
189 int i, j, ddr_setup_complete = 0;
190
191 /* Try 3 times to do a calibration */
192 for (i = 0; (i < 3) && !ddr_setup_complete; i++) {
193 WATCHDOG_RESET();
194
195 /* A delay to wait for calibration bit to set */
196 for (j = 0; (j < 10) && !ddr_setup_complete; j++) {
197 mdelay(500);
198 ddr_setup_complete = is_sdram_cal_success();
199 }
200
201 if (!ddr_setup_complete)
202 if (emif_reset())
203 puts("Error: Failed to reset EMIF\n");
204 }
205
206 /* After 3 times trying calibration */
207 if (!ddr_setup_complete) {
208 puts("Error: Could Not Calibrate SDRAM\n");
209 return -EPERM;
210 }
211
212 return 0;
213}
214
Marek Vasut36938972018-05-28 17:22:47 +0200215static int sdram_is_ecc_enabled(void)
216{
217 return !!(readl(&socfpga_ecc_hmc_base->eccctrl) &
218 ALT_ECC_HMC_OCP_ECCCTL_ECC_EN_SET_MSK);
219}
220
221/* Initialize SDRAM ECC bits to avoid false DBE */
222static void sdram_init_ecc_bits(u32 size)
223{
224 icache_enable();
225
226 memset(0, 0, 0x8000);
227 gd->arch.tlb_addr = 0x4000;
228 gd->arch.tlb_size = PGTABLE_SIZE;
229
230 dcache_enable();
231
232 printf("DDRCAL: Scrubbing ECC RAM (%i MiB).\n", size >> 20);
233 memset((void *)0x8000, 0, size - 0x8000);
234 flush_dcache_all();
235 printf("DDRCAL: Scrubbing ECC RAM done.\n");
236 dcache_disable();
237}
238
Tien Fong Chee402735b2017-12-05 15:58:02 +0800239/* Function to startup the SDRAM*/
240static int sdram_startup(void)
241{
242 /* Release NOC ddr scheduler from reset */
243 socfpga_reset_deassert_noc_ddr_scheduler();
244
245 /* Bringup the DDR (calibration and configuration) */
246 return ddr_setup();
247}
248
249static u64 sdram_size_calc(void)
250{
251 u32 dramaddrw = readl(&socfpga_io48_mmr_base->dramaddrw);
252
253 u64 size = BIT(((dramaddrw &
254 IO48_MMR_DRAMADDRW_CFG_CS_ADDR_WIDTH_MASK) >>
255 IO48_MMR_DRAMADDRW_CFG_CS_ADDR_WIDTH_SHIFT) +
256 ((dramaddrw &
257 IO48_MMR_DRAMADDRW_CFG_BANK_GROUP_ADDR_WIDTH_MASK) >>
258 IO48_MMR_DRAMADDRW_CFG_BANK_GROUP_ADDR_WIDTH_SHIFT) +
259 ((dramaddrw &
260 IO48_MMR_DRAMADDRW_CFG_BANK_ADDR_WIDTH_MASK) >>
261 IO48_MMR_DRAMADDRW_CFG_BANK_ADDR_WIDTH_SHIFT) +
262 ((dramaddrw &
263 IO48_MMR_DRAMADDRW_CFG_ROW_ADDR_WIDTH_MASK) >>
264 IO48_MMR_DRAMADDRW_CFG_ROW_ADDR_WIDTH_SHIFT) +
265 (dramaddrw & IO48_MMR_DRAMADDRW_CFG_COL_ADDR_WIDTH_MASK));
266
267 size *= (2 << (readl(&socfpga_ecc_hmc_base->ddrioctrl) &
268 ALT_ECC_HMC_OCP_DDRIOCTRL_IO_SIZE_MSK));
269
Marek Vasut6b440502019-03-06 17:18:22 +0100270 debug("SDRAM size=%llu\n", size);
Tien Fong Chee402735b2017-12-05 15:58:02 +0800271
272 return size;
273}
274
275/* Function to initialize SDRAM MMR and NOC DDR scheduler*/
276static void sdram_mmr_init(void)
277{
278 u32 update_value, io48_value;
279 u32 ctrlcfg0 = readl(&socfpga_io48_mmr_base->ctrlcfg0);
280 u32 ctrlcfg1 = readl(&socfpga_io48_mmr_base->ctrlcfg1);
281 u32 dramaddrw = readl(&socfpga_io48_mmr_base->dramaddrw);
282 u32 caltim0 = readl(&socfpga_io48_mmr_base->caltiming0);
283 u32 caltim1 = readl(&socfpga_io48_mmr_base->caltiming1);
284 u32 caltim2 = readl(&socfpga_io48_mmr_base->caltiming2);
285 u32 caltim3 = readl(&socfpga_io48_mmr_base->caltiming3);
286 u32 caltim4 = readl(&socfpga_io48_mmr_base->caltiming4);
287 u32 caltim9 = readl(&socfpga_io48_mmr_base->caltiming9);
288 u32 ddrioctl;
289
290 /*
291 * Configure the DDR IO size [0xFFCFB008]
292 * niosreserve0: Used to indicate DDR width &
293 * bit[7:0] = Number of data bits (0x20 for 32bit)
294 * bit[8] = 1 if user-mode OCT is present
295 * bit[9] = 1 if warm reset compiled into EMIF Cal Code
296 * bit[10] = 1 if warm reset is on during generation in EMIF Cal
297 * niosreserve1: IP ADCDS version encoded as 16 bit value
298 * bit[2:0] = Variant (0=not special,1=FAE beta, 2=Customer beta,
299 * 3=EAP, 4-6 are reserved)
300 * bit[5:3] = Service Pack # (e.g. 1)
301 * bit[9:6] = Minor Release #
302 * bit[14:10] = Major Release #
303 */
Marek Vasute2bc6b12019-03-05 18:37:02 +0100304 if ((readl(&socfpga_io48_mmr_base->niosreserve1) >> 6) & 0x1FF) {
Tien Fong Chee402735b2017-12-05 15:58:02 +0800305 update_value = readl(&socfpga_io48_mmr_base->niosreserve0);
306 writel(((update_value & 0xFF) >> 5),
307 &socfpga_ecc_hmc_base->ddrioctrl);
308 }
309
310 ddrioctl = readl(&socfpga_ecc_hmc_base->ddrioctrl);
311
312 /* Set the DDR Configuration [0xFFD12400] */
313 io48_value = ARRIA_DDR_CONFIG(
314 ((ctrlcfg1 &
315 IO48_MMR_CTRLCFG1_ADDR_ORDER_MASK) >>
316 IO48_MMR_CTRLCFG1_ADDR_ORDER_SHIFT),
317 ((dramaddrw &
318 IO48_MMR_DRAMADDRW_CFG_BANK_ADDR_WIDTH_MASK) >>
319 IO48_MMR_DRAMADDRW_CFG_BANK_ADDR_WIDTH_SHIFT) +
320 ((dramaddrw &
321 IO48_MMR_DRAMADDRW_CFG_BANK_GROUP_ADDR_WIDTH_MASK) >>
322 IO48_MMR_DRAMADDRW_CFG_BANK_GROUP_ADDR_WIDTH_SHIFT),
323 (dramaddrw &
324 IO48_MMR_DRAMADDRW_CFG_COL_ADDR_WIDTH_MASK),
325 ((dramaddrw &
326 IO48_MMR_DRAMADDRW_CFG_ROW_ADDR_WIDTH_MASK) >>
327 IO48_MMR_DRAMADDRW_CFG_ROW_ADDR_WIDTH_SHIFT));
328
329 update_value = match_ddr_conf(io48_value);
330 if (update_value)
331 writel(update_value,
332 &socfpga_noc_ddr_scheduler_base->ddr_t_main_scheduler_ddrconf);
333
334 /*
335 * Configure DDR timing [0xFFD1240C]
336 * RDTOMISS = tRTP + tRP + tRCD - BL/2
337 * WRTOMISS = WL + tWR + tRP + tRCD and
338 * WL = RL + BL/2 + 2 - rd-to-wr ; tWR = 15ns so...
339 * First part of equation is in memory clock units so divide by 2
340 * for HMC clock units. 1066MHz is close to 1ns so use 15 directly.
341 * WRTOMISS = ((RL + BL/2 + 2 + tWR) >> 1)- rd-to-wr + tRP + tRCD
342 */
343 u32 ctrlcfg0_cfg_ctrl_burst_len =
344 (ctrlcfg0 & IO48_MMR_CTRLCFG0_CTRL_BURST_LENGTH_MASK) >>
345 IO48_MMR_CTRLCFG0_CTRL_BURST_LENGTH_SHIFT;
346
347 u32 caltim0_cfg_act_to_rdwr = caltim0 &
348 IO48_MMR_CALTIMING0_CFG_ACT_TO_RDWR_MASK;
349
350 u32 caltim0_cfg_act_to_act =
351 (caltim0 & IO48_MMR_CALTIMING0_CFG_ACT_TO_ACT_MASK) >>
352 IO48_MMR_CALTIMING0_CFG_ACT_TO_ACT_SHIFT;
353
354 u32 caltim0_cfg_act_to_act_db =
355 (caltim0 &
356 IO48_MMR_CALTIMING0_CFG_ACT_TO_ACT_DIFF_BANK_MASK) >>
357 IO48_MMR_CALTIMING0_CFG_ACT_TO_ACT_DIFF_BANK_SHIFT;
358
359 u32 caltim1_cfg_rd_to_wr =
360 (caltim1 & IO48_MMR_CALTIMING1_CFG_RD_TO_WR_MASK) >>
361 IO48_MMR_CALTIMING1_CFG_RD_TO_WR_SHIFT;
362
363 u32 caltim1_cfg_rd_to_rd_dc =
364 (caltim1 & IO48_MMR_CALTIMING1_CFG_RD_TO_RD_DC_MASK) >>
365 IO48_MMR_CALTIMING1_CFG_RD_TO_RD_DC_SHIFT;
366
367 u32 caltim1_cfg_rd_to_wr_dc =
368 (caltim1 & IO48_MMR_CALTIMING1_CFG_RD_TO_WR_DIFF_CHIP_MASK) >>
369 IO48_MMR_CALTIMING1_CFG_RD_TO_WR_DIFF_CHIP_SHIFT;
370
371 u32 caltim2_cfg_rd_to_pch =
372 (caltim2 & IO48_MMR_CALTIMING2_CFG_RD_TO_PCH_MASK) >>
373 IO48_MMR_CALTIMING2_CFG_RD_TO_PCH_SHIFT;
374
375 u32 caltim3_cfg_wr_to_rd =
376 (caltim3 & IO48_MMR_CALTIMING3_CFG_WR_TO_RD_MASK) >>
377 IO48_MMR_CALTIMING3_CFG_WR_TO_RD_SHIFT;
378
379 u32 caltim3_cfg_wr_to_rd_dc =
380 (caltim3 & IO48_MMR_CALTIMING3_CFG_WR_TO_RD_DIFF_CHIP_MASK) >>
381 IO48_MMR_CALTIMING3_CFG_WR_TO_RD_DIFF_CHIP_SHIFT;
382
383 u32 caltim4_cfg_pch_to_valid =
384 (caltim4 & IO48_MMR_CALTIMING4_CFG_PCH_TO_VALID_MASK) >>
385 IO48_MMR_CALTIMING4_CFG_PCH_TO_VALID_SHIFT;
386
387 u32 caltim9_cfg_4_act_to_act = caltim9 &
388 IO48_MMR_CALTIMING9_CFG_WR_4_ACT_TO_ACT_MASK;
389
390 update_value = (caltim2_cfg_rd_to_pch + caltim4_cfg_pch_to_valid +
391 caltim0_cfg_act_to_rdwr -
392 (ctrlcfg0_cfg_ctrl_burst_len >> 2));
393
Marek Vasute2bc6b12019-03-05 18:37:02 +0100394 io48_value = ((((readl(&socfpga_io48_mmr_base->dramtiming0) &
Tien Fong Chee402735b2017-12-05 15:58:02 +0800395 ALT_IO48_DRAMTIME_MEM_READ_LATENCY_MASK) + 2 + 15 +
396 (ctrlcfg0_cfg_ctrl_burst_len >> 1)) >> 1) -
397 /* Up to here was in memory cycles so divide by 2 */
398 caltim1_cfg_rd_to_wr + caltim0_cfg_act_to_rdwr +
399 caltim4_cfg_pch_to_valid);
400
401 writel(((caltim0_cfg_act_to_act <<
402 ALT_NOC_MPU_DDR_T_SCHED_DDRTIMING_ACTTOACT_LSB) |
403 (update_value <<
404 ALT_NOC_MPU_DDR_T_SCHED_DDRTIMING_RDTOMISS_LSB) |
405 (io48_value <<
406 ALT_NOC_MPU_DDR_T_SCHED_DDRTIMING_WRTOMISS_LSB) |
407 ((ctrlcfg0_cfg_ctrl_burst_len >> 2) <<
408 ALT_NOC_MPU_DDR_T_SCHED_DDRTIMING_BURSTLEN_LSB) |
409 (caltim1_cfg_rd_to_wr <<
410 ALT_NOC_MPU_DDR_T_SCHED_DDRTIMING_RDTOWR_LSB) |
411 (caltim3_cfg_wr_to_rd <<
412 ALT_NOC_MPU_DDR_T_SCHED_DDRTIMING_WRTORD_LSB) |
413 (((ddrioctl == 1) ? 1 : 0) <<
414 ALT_NOC_MPU_DDR_T_SCHED_DDRTIMING_BWRATIO_LSB)),
415 &socfpga_noc_ddr_scheduler_base->
416 ddr_t_main_scheduler_ddrtiming);
417
418 /* Configure DDR mode [0xFFD12410] [precharge = 0] */
419 writel(((ddrioctl ? 0 : 1) <<
420 ALT_NOC_MPU_DDR_T_SCHED_DDRMOD_BWRATIOEXTENDED_LSB),
421 &socfpga_noc_ddr_scheduler_base->ddr_t_main_scheduler_ddrmode);
422
423 /* Configure the read latency [0xFFD12414] */
Marek Vasute2bc6b12019-03-05 18:37:02 +0100424 writel(((readl(&socfpga_io48_mmr_base->dramtiming0) &
Tien Fong Chee402735b2017-12-05 15:58:02 +0800425 ALT_IO48_DRAMTIME_MEM_READ_LATENCY_MASK) >> 1) +
426 DDR_READ_LATENCY_DELAY,
427 &socfpga_noc_ddr_scheduler_base->
428 ddr_t_main_scheduler_readlatency);
429
430 /*
431 * Configuring timing values concerning activate commands
432 * [0xFFD12438] [FAWBANK alway 1 because always 4 bank DDR]
433 */
434 writel(((caltim0_cfg_act_to_act_db <<
435 ALT_NOC_MPU_DDR_T_SCHED_ACTIVATE_RRD_LSB) |
436 (caltim9_cfg_4_act_to_act <<
437 ALT_NOC_MPU_DDR_T_SCHED_ACTIVATE_FAW_LSB) |
438 (ARRIA10_SDR_ACTIVATE_FAWBANK <<
439 ALT_NOC_MPU_DDR_T_SCHED_ACTIVATE_FAWBANK_LSB)),
440 &socfpga_noc_ddr_scheduler_base->ddr_t_main_scheduler_activate);
441
442 /*
443 * Configuring timing values concerning device to device data bus
444 * ownership change [0xFFD1243C]
445 */
446 writel(((caltim1_cfg_rd_to_rd_dc <<
447 ALT_NOC_MPU_DDR_T_SCHED_DEVTODEV_BUSRDTORD_LSB) |
448 (caltim1_cfg_rd_to_wr_dc <<
449 ALT_NOC_MPU_DDR_T_SCHED_DEVTODEV_BUSRDTOWR_LSB) |
450 (caltim3_cfg_wr_to_rd_dc <<
451 ALT_NOC_MPU_DDR_T_SCHED_DEVTODEV_BUSWRTORD_LSB)),
452 &socfpga_noc_ddr_scheduler_base->ddr_t_main_scheduler_devtodev);
453
454 /* Enable or disable the SDRAM ECC */
455 if (ctrlcfg1 & IO48_MMR_CTRLCFG1_CTRL_ENABLE_ECC) {
456 setbits_le32(&socfpga_ecc_hmc_base->eccctrl,
457 (ALT_ECC_HMC_OCP_ECCCTL_AWB_CNT_RST_SET_MSK |
458 ALT_ECC_HMC_OCP_ECCCTL_CNT_RST_SET_MSK |
459 ALT_ECC_HMC_OCP_ECCCTL_ECC_EN_SET_MSK));
460 clrbits_le32(&socfpga_ecc_hmc_base->eccctrl,
461 (ALT_ECC_HMC_OCP_ECCCTL_AWB_CNT_RST_SET_MSK |
462 ALT_ECC_HMC_OCP_ECCCTL_CNT_RST_SET_MSK));
463 setbits_le32(&socfpga_ecc_hmc_base->eccctrl2,
464 (ALT_ECC_HMC_OCP_ECCCTL2_RMW_EN_SET_MSK |
465 ALT_ECC_HMC_OCP_ECCCTL2_AWB_EN_SET_MSK));
466 } else {
467 clrbits_le32(&socfpga_ecc_hmc_base->eccctrl,
468 (ALT_ECC_HMC_OCP_ECCCTL_AWB_CNT_RST_SET_MSK |
469 ALT_ECC_HMC_OCP_ECCCTL_CNT_RST_SET_MSK |
470 ALT_ECC_HMC_OCP_ECCCTL_ECC_EN_SET_MSK));
471 clrbits_le32(&socfpga_ecc_hmc_base->eccctrl2,
472 (ALT_ECC_HMC_OCP_ECCCTL2_RMW_EN_SET_MSK |
473 ALT_ECC_HMC_OCP_ECCCTL2_AWB_EN_SET_MSK));
474 }
475}
476
477struct firewall_entry {
478 const char *prop_name;
479 const u32 cfg_addr;
480 const u32 en_addr;
481 const u32 en_bit;
482};
483#define FW_MPU_FPGA_ADDRESS \
484 ((const struct socfpga_noc_fw_ddr_mpu_fpga2sdram *)\
485 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS)
486
487#define SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(ADDR) \
488 (SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS + \
489 offsetof(struct socfpga_noc_fw_ddr_mpu_fpga2sdram, ADDR))
490
491#define SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(ADDR) \
492 (SOCFPGA_SDR_FIREWALL_L3_ADDRESS + \
493 offsetof(struct socfpga_noc_fw_ddr_l3, ADDR))
494
495const struct firewall_entry firewall_table[] = {
496 {
497 "mpu0",
498 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(mpuregion0addr),
499 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
500 ALT_NOC_FW_DDR_SCR_EN_MPUREG0EN_SET_MSK
501 },
502 {
503 "mpu1",
504 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS +
505 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(mpuregion1addr),
506 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
507 ALT_NOC_FW_DDR_SCR_EN_MPUREG1EN_SET_MSK
508 },
509 {
510 "mpu2",
511 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(mpuregion2addr),
512 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
513 ALT_NOC_FW_DDR_SCR_EN_MPUREG2EN_SET_MSK
514 },
515 {
516 "mpu3",
517 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(mpuregion3addr),
518 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
519 ALT_NOC_FW_DDR_SCR_EN_MPUREG3EN_SET_MSK
520 },
521 {
522 "l3-0",
523 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(hpsregion0addr),
524 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(enable),
525 ALT_NOC_FW_DDR_SCR_EN_HPSREG0EN_SET_MSK
526 },
527 {
528 "l3-1",
529 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(hpsregion1addr),
530 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(enable),
531 ALT_NOC_FW_DDR_SCR_EN_HPSREG1EN_SET_MSK
532 },
533 {
534 "l3-2",
535 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(hpsregion2addr),
536 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(enable),
537 ALT_NOC_FW_DDR_SCR_EN_HPSREG2EN_SET_MSK
538 },
539 {
540 "l3-3",
541 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(hpsregion3addr),
542 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(enable),
543 ALT_NOC_FW_DDR_SCR_EN_HPSREG3EN_SET_MSK
544 },
545 {
546 "l3-4",
547 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(hpsregion4addr),
548 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(enable),
549 ALT_NOC_FW_DDR_SCR_EN_HPSREG4EN_SET_MSK
550 },
551 {
552 "l3-5",
553 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(hpsregion5addr),
554 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(enable),
555 ALT_NOC_FW_DDR_SCR_EN_HPSREG5EN_SET_MSK
556 },
557 {
558 "l3-6",
559 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(hpsregion6addr),
560 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(enable),
561 ALT_NOC_FW_DDR_SCR_EN_HPSREG6EN_SET_MSK
562 },
563 {
564 "l3-7",
565 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(hpsregion7addr),
566 SOCFPGA_SDR_FIREWALL_L3_ADDRESS_OFFSET(enable),
567 ALT_NOC_FW_DDR_SCR_EN_HPSREG7EN_SET_MSK
568 },
569 {
570 "fpga2sdram0-0",
571 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET
572 (fpga2sdram0region0addr),
573 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
574 ALT_NOC_FW_DDR_SCR_EN_F2SDR0REG0EN_SET_MSK
575 },
576 {
577 "fpga2sdram0-1",
578 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET
579 (fpga2sdram0region1addr),
580 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
581 ALT_NOC_FW_DDR_SCR_EN_F2SDR0REG1EN_SET_MSK
582 },
583 {
584 "fpga2sdram0-2",
585 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET
586 (fpga2sdram0region2addr),
587 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
588 ALT_NOC_FW_DDR_SCR_EN_F2SDR0REG2EN_SET_MSK
589 },
590 {
591 "fpga2sdram0-3",
592 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET
593 (fpga2sdram0region3addr),
594 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
595 ALT_NOC_FW_DDR_SCR_EN_F2SDR0REG3EN_SET_MSK
596 },
597 {
598 "fpga2sdram1-0",
599 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET
600 (fpga2sdram1region0addr),
601 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
602 ALT_NOC_FW_DDR_SCR_EN_F2SDR1REG0EN_SET_MSK
603 },
604 {
605 "fpga2sdram1-1",
606 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET
607 (fpga2sdram1region1addr),
608 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
609 ALT_NOC_FW_DDR_SCR_EN_F2SDR1REG1EN_SET_MSK
610 },
611 {
612 "fpga2sdram1-2",
613 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET
614 (fpga2sdram1region2addr),
615 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
616 ALT_NOC_FW_DDR_SCR_EN_F2SDR1REG2EN_SET_MSK
617 },
618 {
619 "fpga2sdram1-3",
620 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET
621 (fpga2sdram1region3addr),
622 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
623 ALT_NOC_FW_DDR_SCR_EN_F2SDR1REG3EN_SET_MSK
624 },
625 {
626 "fpga2sdram2-0",
627 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET
628 (fpga2sdram2region0addr),
629 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
630 ALT_NOC_FW_DDR_SCR_EN_F2SDR2REG0EN_SET_MSK
631 },
632 {
633 "fpga2sdram2-1",
634 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET
635 (fpga2sdram2region1addr),
636 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
637 ALT_NOC_FW_DDR_SCR_EN_F2SDR2REG1EN_SET_MSK
638 },
639 {
640 "fpga2sdram2-2",
641 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET
642 (fpga2sdram2region2addr),
643 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
644 ALT_NOC_FW_DDR_SCR_EN_F2SDR2REG2EN_SET_MSK
645 },
646 {
647 "fpga2sdram2-3",
648 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET
649 (fpga2sdram2region3addr),
650 SOCFPGA_SDR_FIREWALL_MPU_FPGA_ADDRESS_OFFSET(enable),
651 ALT_NOC_FW_DDR_SCR_EN_F2SDR2REG3EN_SET_MSK
652 },
653
654};
655
656static int of_sdram_firewall_setup(const void *blob)
657{
658 int child, i, node, ret;
659 u32 start_end[2];
660 char name[32];
661
662 node = fdtdec_next_compatible(blob, 0, COMPAT_ALTERA_SOCFPGA_NOC);
663 if (node < 0)
664 return -ENXIO;
665
666 child = fdt_first_subnode(blob, node);
667 if (child < 0)
668 return -ENXIO;
669
670 /* set to default state */
671 writel(0, &socfpga_noc_fw_ddr_mpu_fpga2sdram_base->enable);
672 writel(0, &socfpga_noc_fw_ddr_l3_base->enable);
673
674
675 for (i = 0; i < ARRAY_SIZE(firewall_table); i++) {
676 sprintf(name, "%s", firewall_table[i].prop_name);
677 ret = fdtdec_get_int_array(blob, child, name,
678 start_end, 2);
679 if (ret) {
680 sprintf(name, "altr,%s", firewall_table[i].prop_name);
681 ret = fdtdec_get_int_array(blob, child, name,
682 start_end, 2);
683 if (ret)
684 continue;
685 }
686
687 writel((start_end[0] & ALT_NOC_FW_DDR_ADDR_MASK) |
688 (start_end[1] << ALT_NOC_FW_DDR_END_ADDR_LSB),
689 firewall_table[i].cfg_addr);
690 setbits_le32(firewall_table[i].en_addr,
691 firewall_table[i].en_bit);
692 }
693
694 return 0;
695}
696
697int ddr_calibration_sequence(void)
698{
699 WATCHDOG_RESET();
700
701 /* Check to see if SDRAM cal was success */
702 if (sdram_startup()) {
703 puts("DDRCAL: Failed\n");
704 return -EPERM;
705 }
706
707 puts("DDRCAL: Success\n");
708
709 WATCHDOG_RESET();
710
711 /* initialize the MMR register */
712 sdram_mmr_init();
713
714 /* assigning the SDRAM size */
715 u64 size = sdram_size_calc();
716
717 /*
718 * If size is less than zero, this is invalid/weird value from
719 * calculation, use default Config size.
720 * Up to 2GB is supported, 2GB would be used if more than that.
721 */
722 if (size <= 0)
723 gd->ram_size = PHYS_SDRAM_1_SIZE;
724 else if (DDR_SIZE_2GB_HEX <= size)
725 gd->ram_size = DDR_SIZE_2GB_HEX;
726 else
727 gd->ram_size = (u32)size;
728
729 /* setup the dram info within bd */
730 dram_init_banksize();
731
732 if (of_sdram_firewall_setup(gd->fdt_blob))
733 puts("FW: Error Configuring Firewall\n");
734
Marek Vasut36938972018-05-28 17:22:47 +0200735 if (sdram_is_ecc_enabled())
736 sdram_init_ecc_bits(gd->ram_size);
737
Tien Fong Chee402735b2017-12-05 15:58:02 +0800738 return 0;
739}