xref: /openbmc/u-boot/drivers/ddr/altera/sdram_s10.c (revision 0eee446e)
10bc28b7cSLey Foon Tan // SPDX-License-Identifier: GPL-2.0
20bc28b7cSLey Foon Tan /*
30bc28b7cSLey Foon Tan  * Copyright (C) 2016-2018 Intel Corporation <www.intel.com>
40bc28b7cSLey Foon Tan  *
50bc28b7cSLey Foon Tan  */
60bc28b7cSLey Foon Tan 
70bc28b7cSLey Foon Tan #include <common.h>
80bc28b7cSLey Foon Tan #include <errno.h>
90bc28b7cSLey Foon Tan #include <div64.h>
100bc28b7cSLey Foon Tan #include <asm/io.h>
110bc28b7cSLey Foon Tan #include <wait_bit.h>
120bc28b7cSLey Foon Tan #include <asm/arch/firewall_s10.h>
130bc28b7cSLey Foon Tan #include <asm/arch/sdram_s10.h>
140bc28b7cSLey Foon Tan #include <asm/arch/system_manager.h>
150bc28b7cSLey Foon Tan #include <asm/arch/reset_manager.h>
160bc28b7cSLey Foon Tan 
170bc28b7cSLey Foon Tan DECLARE_GLOBAL_DATA_PTR;
180bc28b7cSLey Foon Tan 
190bc28b7cSLey Foon Tan static const struct socfpga_system_manager *sysmgr_regs =
200bc28b7cSLey Foon Tan 		(void *)SOCFPGA_SYSMGR_ADDRESS;
210bc28b7cSLey Foon Tan 
220bc28b7cSLey Foon Tan #define DDR_CONFIG(A, B, C, R)	(((A) << 24) | ((B) << 16) | ((C) << 8) | (R))
230bc28b7cSLey Foon Tan 
240bc28b7cSLey Foon Tan /* The followring are the supported configurations */
250bc28b7cSLey Foon Tan u32 ddr_config[] = {
260bc28b7cSLey Foon Tan 	/* DDR_CONFIG(Address order,Bank,Column,Row) */
270bc28b7cSLey Foon Tan 	/* List for DDR3 or LPDDR3 (pinout order > chip, row, bank, column) */
280bc28b7cSLey Foon Tan 	DDR_CONFIG(0, 3, 10, 12),
290bc28b7cSLey Foon Tan 	DDR_CONFIG(0, 3,  9, 13),
300bc28b7cSLey Foon Tan 	DDR_CONFIG(0, 3, 10, 13),
310bc28b7cSLey Foon Tan 	DDR_CONFIG(0, 3,  9, 14),
320bc28b7cSLey Foon Tan 	DDR_CONFIG(0, 3, 10, 14),
330bc28b7cSLey Foon Tan 	DDR_CONFIG(0, 3, 10, 15),
340bc28b7cSLey Foon Tan 	DDR_CONFIG(0, 3, 11, 14),
350bc28b7cSLey Foon Tan 	DDR_CONFIG(0, 3, 11, 15),
360bc28b7cSLey Foon Tan 	DDR_CONFIG(0, 3, 10, 16),
370bc28b7cSLey Foon Tan 	DDR_CONFIG(0, 3, 11, 16),
380bc28b7cSLey Foon Tan 	DDR_CONFIG(0, 3, 12, 15),	/* 0xa */
390bc28b7cSLey Foon Tan 	/* List for DDR4 only (pinout order > chip, bank, row, column) */
400bc28b7cSLey Foon Tan 	DDR_CONFIG(1, 3, 10, 14),
410bc28b7cSLey Foon Tan 	DDR_CONFIG(1, 4, 10, 14),
420bc28b7cSLey Foon Tan 	DDR_CONFIG(1, 3, 10, 15),
430bc28b7cSLey Foon Tan 	DDR_CONFIG(1, 4, 10, 15),
440bc28b7cSLey Foon Tan 	DDR_CONFIG(1, 3, 10, 16),
450bc28b7cSLey Foon Tan 	DDR_CONFIG(1, 4, 10, 16),
460bc28b7cSLey Foon Tan 	DDR_CONFIG(1, 3, 10, 17),
470bc28b7cSLey Foon Tan 	DDR_CONFIG(1, 4, 10, 17),
480bc28b7cSLey Foon Tan };
490bc28b7cSLey Foon Tan 
hmc_readl(u32 reg)500bc28b7cSLey Foon Tan static u32 hmc_readl(u32 reg)
510bc28b7cSLey Foon Tan {
520bc28b7cSLey Foon Tan 	return readl(((void __iomem *)SOCFPGA_HMC_MMR_IO48_ADDRESS + (reg)));
530bc28b7cSLey Foon Tan }
540bc28b7cSLey Foon Tan 
hmc_ecc_readl(u32 reg)550bc28b7cSLey Foon Tan static u32 hmc_ecc_readl(u32 reg)
560bc28b7cSLey Foon Tan {
570bc28b7cSLey Foon Tan 	return readl((void __iomem *)SOCFPGA_SDR_ADDRESS + (reg));
580bc28b7cSLey Foon Tan }
590bc28b7cSLey Foon Tan 
hmc_ecc_writel(u32 data,u32 reg)600bc28b7cSLey Foon Tan static u32 hmc_ecc_writel(u32 data, u32 reg)
610bc28b7cSLey Foon Tan {
620bc28b7cSLey Foon Tan 	return writel(data, (void __iomem *)SOCFPGA_SDR_ADDRESS + (reg));
630bc28b7cSLey Foon Tan }
640bc28b7cSLey Foon Tan 
ddr_sch_writel(u32 data,u32 reg)650bc28b7cSLey Foon Tan static u32 ddr_sch_writel(u32 data, u32 reg)
660bc28b7cSLey Foon Tan {
670bc28b7cSLey Foon Tan 	return writel(data,
680bc28b7cSLey Foon Tan 		      (void __iomem *)SOCFPGA_SDR_SCHEDULER_ADDRESS + (reg));
690bc28b7cSLey Foon Tan }
700bc28b7cSLey Foon Tan 
match_ddr_conf(u32 ddr_conf)710bc28b7cSLey Foon Tan int match_ddr_conf(u32 ddr_conf)
720bc28b7cSLey Foon Tan {
730bc28b7cSLey Foon Tan 	int i;
740bc28b7cSLey Foon Tan 
750bc28b7cSLey Foon Tan 	for (i = 0; i < ARRAY_SIZE(ddr_config); i++) {
760bc28b7cSLey Foon Tan 		if (ddr_conf == ddr_config[i])
770bc28b7cSLey Foon Tan 			return i;
780bc28b7cSLey Foon Tan 	}
790bc28b7cSLey Foon Tan 	return 0;
800bc28b7cSLey Foon Tan }
810bc28b7cSLey Foon Tan 
emif_clear(void)820bc28b7cSLey Foon Tan static int emif_clear(void)
830bc28b7cSLey Foon Tan {
840bc28b7cSLey Foon Tan 	hmc_ecc_writel(0, RSTHANDSHAKECTRL);
850bc28b7cSLey Foon Tan 
860bc28b7cSLey Foon Tan 	return wait_for_bit_le32((const void *)(SOCFPGA_SDR_ADDRESS +
870bc28b7cSLey Foon Tan 				 RSTHANDSHAKESTAT),
880bc28b7cSLey Foon Tan 				 DDR_HMC_RSTHANDSHAKE_MASK,
890bc28b7cSLey Foon Tan 				 false, 1000, false);
900bc28b7cSLey Foon Tan }
910bc28b7cSLey Foon Tan 
emif_reset(void)920bc28b7cSLey Foon Tan static int emif_reset(void)
930bc28b7cSLey Foon Tan {
940bc28b7cSLey Foon Tan 	u32 c2s, s2c, ret;
950bc28b7cSLey Foon Tan 
960bc28b7cSLey Foon Tan 	c2s = hmc_ecc_readl(RSTHANDSHAKECTRL) & DDR_HMC_RSTHANDSHAKE_MASK;
970bc28b7cSLey Foon Tan 	s2c = hmc_ecc_readl(RSTHANDSHAKESTAT) & DDR_HMC_RSTHANDSHAKE_MASK;
980bc28b7cSLey Foon Tan 
990bc28b7cSLey Foon Tan 	debug("DDR: c2s=%08x s2c=%08x nr0=%08x nr1=%08x nr2=%08x dst=%08x\n",
1000bc28b7cSLey Foon Tan 	      c2s, s2c, hmc_readl(NIOSRESERVED0), hmc_readl(NIOSRESERVED1),
1010bc28b7cSLey Foon Tan 	      hmc_readl(NIOSRESERVED2), hmc_readl(DRAMSTS));
1020bc28b7cSLey Foon Tan 
1030bc28b7cSLey Foon Tan 	if (s2c && emif_clear()) {
1040bc28b7cSLey Foon Tan 		printf("DDR: emif_clear() failed\n");
1050bc28b7cSLey Foon Tan 		return -1;
1060bc28b7cSLey Foon Tan 	}
1070bc28b7cSLey Foon Tan 
1080bc28b7cSLey Foon Tan 	debug("DDR: Triggerring emif reset\n");
1090bc28b7cSLey Foon Tan 	hmc_ecc_writel(DDR_HMC_CORE2SEQ_INT_REQ, RSTHANDSHAKECTRL);
1100bc28b7cSLey Foon Tan 
1110bc28b7cSLey Foon Tan 	/* if seq2core[3] = 0, we are good */
1120bc28b7cSLey Foon Tan 	ret = wait_for_bit_le32((const void *)(SOCFPGA_SDR_ADDRESS +
1130bc28b7cSLey Foon Tan 				 RSTHANDSHAKESTAT),
1140bc28b7cSLey Foon Tan 				 DDR_HMC_SEQ2CORE_INT_RESP_MASK,
1150bc28b7cSLey Foon Tan 				 false, 1000, false);
1160bc28b7cSLey Foon Tan 	if (ret) {
1170bc28b7cSLey Foon Tan 		printf("DDR: failed to get ack from EMIF\n");
1180bc28b7cSLey Foon Tan 		return ret;
1190bc28b7cSLey Foon Tan 	}
1200bc28b7cSLey Foon Tan 
1210bc28b7cSLey Foon Tan 	ret = emif_clear();
1220bc28b7cSLey Foon Tan 	if (ret) {
1230bc28b7cSLey Foon Tan 		printf("DDR: emif_clear() failed\n");
1240bc28b7cSLey Foon Tan 		return ret;
1250bc28b7cSLey Foon Tan 	}
1260bc28b7cSLey Foon Tan 
1270bc28b7cSLey Foon Tan 	debug("DDR: %s triggered successly\n", __func__);
1280bc28b7cSLey Foon Tan 	return 0;
1290bc28b7cSLey Foon Tan }
1300bc28b7cSLey Foon Tan 
poll_hmc_clock_status(void)1310bc28b7cSLey Foon Tan static int poll_hmc_clock_status(void)
1320bc28b7cSLey Foon Tan {
1330bc28b7cSLey Foon Tan 	return wait_for_bit_le32(&sysmgr_regs->hmc_clk,
1340bc28b7cSLey Foon Tan 				 SYSMGR_HMC_CLK_STATUS_MSK, true, 1000, false);
1350bc28b7cSLey Foon Tan }
1360bc28b7cSLey Foon Tan 
1370bc28b7cSLey Foon Tan /**
1380bc28b7cSLey Foon Tan  * sdram_mmr_init_full() - Function to initialize SDRAM MMR
1390bc28b7cSLey Foon Tan  *
1400bc28b7cSLey Foon Tan  * Initialize the SDRAM MMR.
1410bc28b7cSLey Foon Tan  */
sdram_mmr_init_full(unsigned int unused)1420bc28b7cSLey Foon Tan int sdram_mmr_init_full(unsigned int unused)
1430bc28b7cSLey Foon Tan {
1440bc28b7cSLey Foon Tan 	u32 update_value, io48_value, ddrioctl;
1450bc28b7cSLey Foon Tan 	u32 i;
1460bc28b7cSLey Foon Tan 	int ret;
1470bc28b7cSLey Foon Tan 
1480bc28b7cSLey Foon Tan 	/* Enable access to DDR from CPU master */
1490bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_DDRREG),
1500bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1510bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE0),
1520bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1530bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1A),
1540bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1550bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1B),
1560bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1570bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1C),
1580bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1590bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1D),
1600bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1610bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_CPU0_MPRT_ADBASE_MEMSPACE1E),
1620bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1630bc28b7cSLey Foon Tan 
1640bc28b7cSLey Foon Tan 	/* Enable access to DDR from IO master */
1650bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE0),
1660bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1670bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1A),
1680bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1690bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1B),
1700bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1710bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1C),
1720bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1730bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1D),
1740bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1750bc28b7cSLey Foon Tan 	clrbits_le32(CCU_REG_ADDR(CCU_IOM_MPRT_ADBASE_MEMSPACE1E),
1760bc28b7cSLey Foon Tan 		     CCU_ADBASE_DI_MASK);
1770bc28b7cSLey Foon Tan 
1780bc28b7cSLey Foon Tan 	/* this enables nonsecure access to DDR */
1790bc28b7cSLey Foon Tan 	/* mpuregion0addr_limit */
1800bc28b7cSLey Foon Tan 	FW_MPU_DDR_SCR_WRITEL(0xFFFF0000, FW_MPU_DDR_SCR_MPUREGION0ADDR_LIMIT);
1810bc28b7cSLey Foon Tan 	FW_MPU_DDR_SCR_WRITEL(0x1F, FW_MPU_DDR_SCR_MPUREGION0ADDR_LIMITEXT);
1820bc28b7cSLey Foon Tan 
1830bc28b7cSLey Foon Tan 	/* nonmpuregion0addr_limit */
1840bc28b7cSLey Foon Tan 	FW_MPU_DDR_SCR_WRITEL(0xFFFF0000,
1850bc28b7cSLey Foon Tan 			      FW_MPU_DDR_SCR_NONMPUREGION0ADDR_LIMIT);
1860bc28b7cSLey Foon Tan 	FW_MPU_DDR_SCR_WRITEL(0x1F, FW_MPU_DDR_SCR_NONMPUREGION0ADDR_LIMITEXT);
1870bc28b7cSLey Foon Tan 
1880bc28b7cSLey Foon Tan 	/* Enable mpuregion0enable and nonmpuregion0enable */
1890bc28b7cSLey Foon Tan 	FW_MPU_DDR_SCR_WRITEL(MPUREGION0_ENABLE | NONMPUREGION0_ENABLE,
1900bc28b7cSLey Foon Tan 			      FW_MPU_DDR_SCR_EN_SET);
1910bc28b7cSLey Foon Tan 
1920bc28b7cSLey Foon Tan 	/* Ensure HMC clock is running */
1930bc28b7cSLey Foon Tan 	if (poll_hmc_clock_status()) {
1940bc28b7cSLey Foon Tan 		puts("DDR: Error as HMC clock not running\n");
1950bc28b7cSLey Foon Tan 		return -1;
1960bc28b7cSLey Foon Tan 	}
1970bc28b7cSLey Foon Tan 
1980bc28b7cSLey Foon Tan 	/* release DDR scheduler from reset */
1990bc28b7cSLey Foon Tan 	socfpga_per_reset(SOCFPGA_RESET(SDR), 0);
2000bc28b7cSLey Foon Tan 
2010bc28b7cSLey Foon Tan 	/* Try 3 times to do a calibration */
2020bc28b7cSLey Foon Tan 	for (i = 0; i < 3; i++) {
2030bc28b7cSLey Foon Tan 		ret = wait_for_bit_le32((const void *)(SOCFPGA_SDR_ADDRESS +
2040bc28b7cSLey Foon Tan 					DDRCALSTAT),
2050bc28b7cSLey Foon Tan 					DDR_HMC_DDRCALSTAT_CAL_MSK, true, 1000,
2060bc28b7cSLey Foon Tan 					false);
2070bc28b7cSLey Foon Tan 		if (!ret)
2080bc28b7cSLey Foon Tan 			break;
2090bc28b7cSLey Foon Tan 
2100bc28b7cSLey Foon Tan 		emif_reset();
2110bc28b7cSLey Foon Tan 	}
2120bc28b7cSLey Foon Tan 
2130bc28b7cSLey Foon Tan 	if (ret) {
2140bc28b7cSLey Foon Tan 		puts("DDR: Error as SDRAM calibration failed\n");
2150bc28b7cSLey Foon Tan 		return -1;
2160bc28b7cSLey Foon Tan 	}
2170bc28b7cSLey Foon Tan 	debug("DDR: Calibration success\n");
2180bc28b7cSLey Foon Tan 
2190bc28b7cSLey Foon Tan 	u32 ctrlcfg0 = hmc_readl(CTRLCFG0);
2200bc28b7cSLey Foon Tan 	u32 ctrlcfg1 = hmc_readl(CTRLCFG1);
2210bc28b7cSLey Foon Tan 	u32 dramaddrw = hmc_readl(DRAMADDRW);
2220bc28b7cSLey Foon Tan 	u32 dramtim0 = hmc_readl(DRAMTIMING0);
2230bc28b7cSLey Foon Tan 	u32 caltim0 = hmc_readl(CALTIMING0);
2240bc28b7cSLey Foon Tan 	u32 caltim1 = hmc_readl(CALTIMING1);
2250bc28b7cSLey Foon Tan 	u32 caltim2 = hmc_readl(CALTIMING2);
2260bc28b7cSLey Foon Tan 	u32 caltim3 = hmc_readl(CALTIMING3);
2270bc28b7cSLey Foon Tan 	u32 caltim4 = hmc_readl(CALTIMING4);
2280bc28b7cSLey Foon Tan 	u32 caltim9 = hmc_readl(CALTIMING9);
2290bc28b7cSLey Foon Tan 
2300bc28b7cSLey Foon Tan 	/*
2310bc28b7cSLey Foon Tan 	 * Configure the DDR IO size [0xFFCFB008]
2320bc28b7cSLey Foon Tan 	 * niosreserve0: Used to indicate DDR width &
2330bc28b7cSLey Foon Tan 	 *	bit[7:0] = Number of data bits (bit[6:5] 0x01=32bit, 0x10=64bit)
2340bc28b7cSLey Foon Tan 	 *	bit[8]   = 1 if user-mode OCT is present
2350bc28b7cSLey Foon Tan 	 *	bit[9]   = 1 if warm reset compiled into EMIF Cal Code
2360bc28b7cSLey Foon Tan 	 *	bit[10]  = 1 if warm reset is on during generation in EMIF Cal
2370bc28b7cSLey Foon Tan 	 * niosreserve1: IP ADCDS version encoded as 16 bit value
2380bc28b7cSLey Foon Tan 	 *	bit[2:0] = Variant (0=not special,1=FAE beta, 2=Customer beta,
2390bc28b7cSLey Foon Tan 	 *			    3=EAP, 4-6 are reserved)
2400bc28b7cSLey Foon Tan 	 *	bit[5:3] = Service Pack # (e.g. 1)
2410bc28b7cSLey Foon Tan 	 *	bit[9:6] = Minor Release #
2420bc28b7cSLey Foon Tan 	 *	bit[14:10] = Major Release #
2430bc28b7cSLey Foon Tan 	 */
2440bc28b7cSLey Foon Tan 	update_value = hmc_readl(NIOSRESERVED0);
2450bc28b7cSLey Foon Tan 	hmc_ecc_writel(((update_value & 0xFF) >> 5), DDRIOCTRL);
2460bc28b7cSLey Foon Tan 	ddrioctl = hmc_ecc_readl(DDRIOCTRL);
2470bc28b7cSLey Foon Tan 
2480bc28b7cSLey Foon Tan 	/* enable HPS interface to HMC */
2490bc28b7cSLey Foon Tan 	hmc_ecc_writel(DDR_HMC_HPSINTFCSEL_ENABLE_MASK, HPSINTFCSEL);
2500bc28b7cSLey Foon Tan 
2510bc28b7cSLey Foon Tan 	/* Set the DDR Configuration */
2520bc28b7cSLey Foon Tan 	io48_value = DDR_CONFIG(CTRLCFG1_CFG_ADDR_ORDER(ctrlcfg1),
2530bc28b7cSLey Foon Tan 				(DRAMADDRW_CFG_BANK_ADDR_WIDTH(dramaddrw) +
2540bc28b7cSLey Foon Tan 				 DRAMADDRW_CFG_BANK_GRP_ADDR_WIDTH(dramaddrw)),
2550bc28b7cSLey Foon Tan 				DRAMADDRW_CFG_COL_ADDR_WIDTH(dramaddrw),
2560bc28b7cSLey Foon Tan 				DRAMADDRW_CFG_ROW_ADDR_WIDTH(dramaddrw));
2570bc28b7cSLey Foon Tan 
2580bc28b7cSLey Foon Tan 	update_value = match_ddr_conf(io48_value);
2590bc28b7cSLey Foon Tan 	if (update_value)
2600bc28b7cSLey Foon Tan 		ddr_sch_writel(update_value, DDR_SCH_DDRCONF);
2610bc28b7cSLey Foon Tan 
2620bc28b7cSLey Foon Tan 	/* Configure HMC dramaddrw */
2630bc28b7cSLey Foon Tan 	hmc_ecc_writel(hmc_readl(DRAMADDRW), DRAMADDRWIDTH);
2640bc28b7cSLey Foon Tan 
2650bc28b7cSLey Foon Tan 	/*
2660bc28b7cSLey Foon Tan 	 * Configure DDR timing
2670bc28b7cSLey Foon Tan 	 *  RDTOMISS = tRTP + tRP + tRCD - BL/2
2680bc28b7cSLey Foon Tan 	 *  WRTOMISS = WL + tWR + tRP + tRCD and
2690bc28b7cSLey Foon Tan 	 *    WL = RL + BL/2 + 2 - rd-to-wr ; tWR = 15ns  so...
2700bc28b7cSLey Foon Tan 	 *  First part of equation is in memory clock units so divide by 2
2710bc28b7cSLey Foon Tan 	 *  for HMC clock units. 1066MHz is close to 1ns so use 15 directly.
2720bc28b7cSLey Foon Tan 	 *  WRTOMISS = ((RL + BL/2 + 2 + tWR) >> 1)- rd-to-wr + tRP + tRCD
2730bc28b7cSLey Foon Tan 	 */
2740bc28b7cSLey Foon Tan 	u32 burst_len = CTRLCFG0_CFG_CTRL_BURST_LEN(ctrlcfg0);
2750bc28b7cSLey Foon Tan 
2760bc28b7cSLey Foon Tan 	update_value = CALTIMING2_CFG_RD_TO_WR_PCH(caltim2) +
2770bc28b7cSLey Foon Tan 		       CALTIMING4_CFG_PCH_TO_VALID(caltim4) +
2780bc28b7cSLey Foon Tan 		       CALTIMING0_CFG_ACT_TO_RDWR(caltim0) -
2790bc28b7cSLey Foon Tan 		       (burst_len >> 2);
2800bc28b7cSLey Foon Tan 	io48_value = (((DRAMTIMING0_CFG_TCL(dramtim0) + 2 + DDR_TWR +
2810bc28b7cSLey Foon Tan 		       (burst_len >> 1)) >> 1) -
2820bc28b7cSLey Foon Tan 		      /* Up to here was in memory cycles so divide by 2 */
2830bc28b7cSLey Foon Tan 		      CALTIMING1_CFG_RD_TO_WR(caltim1) +
2840bc28b7cSLey Foon Tan 		      CALTIMING0_CFG_ACT_TO_RDWR(caltim0) +
2850bc28b7cSLey Foon Tan 		      CALTIMING4_CFG_PCH_TO_VALID(caltim4));
2860bc28b7cSLey Foon Tan 
2870bc28b7cSLey Foon Tan 	ddr_sch_writel(((CALTIMING0_CFG_ACT_TO_ACT(caltim0) <<
2880bc28b7cSLey Foon Tan 			 DDR_SCH_DDRTIMING_ACTTOACT_OFF) |
2890bc28b7cSLey Foon Tan 			(update_value << DDR_SCH_DDRTIMING_RDTOMISS_OFF) |
2900bc28b7cSLey Foon Tan 			(io48_value << DDR_SCH_DDRTIMING_WRTOMISS_OFF) |
2910bc28b7cSLey Foon Tan 			((burst_len >> 2) << DDR_SCH_DDRTIMING_BURSTLEN_OFF) |
2920bc28b7cSLey Foon Tan 			(CALTIMING1_CFG_RD_TO_WR(caltim1) <<
2930bc28b7cSLey Foon Tan 			 DDR_SCH_DDRTIMING_RDTOWR_OFF) |
2940bc28b7cSLey Foon Tan 			(CALTIMING3_CFG_WR_TO_RD(caltim3) <<
2950bc28b7cSLey Foon Tan 			 DDR_SCH_DDRTIMING_WRTORD_OFF) |
2960bc28b7cSLey Foon Tan 			(((ddrioctl == 1) ? 1 : 0) <<
2970bc28b7cSLey Foon Tan 			 DDR_SCH_DDRTIMING_BWRATIO_OFF)),
2980bc28b7cSLey Foon Tan 			DDR_SCH_DDRTIMING);
2990bc28b7cSLey Foon Tan 
3000bc28b7cSLey Foon Tan 	/* Configure DDR mode [precharge = 0] */
3010bc28b7cSLey Foon Tan 	ddr_sch_writel(((ddrioctl ? 0 : 1) <<
3020bc28b7cSLey Foon Tan 			 DDR_SCH_DDRMOD_BWRATIOEXTENDED_OFF),
3030bc28b7cSLey Foon Tan 			DDR_SCH_DDRMODE);
3040bc28b7cSLey Foon Tan 
3050bc28b7cSLey Foon Tan 	/* Configure the read latency */
3060bc28b7cSLey Foon Tan 	ddr_sch_writel((DRAMTIMING0_CFG_TCL(dramtim0) >> 1) +
3070bc28b7cSLey Foon Tan 			DDR_READ_LATENCY_DELAY,
3080bc28b7cSLey Foon Tan 			DDR_SCH_READ_LATENCY);
3090bc28b7cSLey Foon Tan 
3100bc28b7cSLey Foon Tan 	/*
3110bc28b7cSLey Foon Tan 	 * Configuring timing values concerning activate commands
3120bc28b7cSLey Foon Tan 	 * [FAWBANK alway 1 because always 4 bank DDR]
3130bc28b7cSLey Foon Tan 	 */
3140bc28b7cSLey Foon Tan 	ddr_sch_writel(((CALTIMING0_CFG_ACT_TO_ACT_DB(caltim0) <<
3150bc28b7cSLey Foon Tan 			 DDR_SCH_ACTIVATE_RRD_OFF) |
3160bc28b7cSLey Foon Tan 			(CALTIMING9_CFG_4_ACT_TO_ACT(caltim9) <<
3170bc28b7cSLey Foon Tan 			 DDR_SCH_ACTIVATE_FAW_OFF) |
3180bc28b7cSLey Foon Tan 			(DDR_ACTIVATE_FAWBANK <<
3190bc28b7cSLey Foon Tan 			 DDR_SCH_ACTIVATE_FAWBANK_OFF)),
3200bc28b7cSLey Foon Tan 			DDR_SCH_ACTIVATE);
3210bc28b7cSLey Foon Tan 
3220bc28b7cSLey Foon Tan 	/*
3230bc28b7cSLey Foon Tan 	 * Configuring timing values concerning device to device data bus
3240bc28b7cSLey Foon Tan 	 * ownership change
3250bc28b7cSLey Foon Tan 	 */
3260bc28b7cSLey Foon Tan 	ddr_sch_writel(((CALTIMING1_CFG_RD_TO_RD_DC(caltim1) <<
3270bc28b7cSLey Foon Tan 			 DDR_SCH_DEVTODEV_BUSRDTORD_OFF) |
3280bc28b7cSLey Foon Tan 			(CALTIMING1_CFG_RD_TO_WR_DC(caltim1) <<
3290bc28b7cSLey Foon Tan 			 DDR_SCH_DEVTODEV_BUSRDTOWR_OFF) |
3300bc28b7cSLey Foon Tan 			(CALTIMING3_CFG_WR_TO_RD_DC(caltim3) <<
3310bc28b7cSLey Foon Tan 			 DDR_SCH_DEVTODEV_BUSWRTORD_OFF)),
3320bc28b7cSLey Foon Tan 			DDR_SCH_DEVTODEV);
3330bc28b7cSLey Foon Tan 
3340bc28b7cSLey Foon Tan 	/* assigning the SDRAM size */
3350bc28b7cSLey Foon Tan 	unsigned long long size = sdram_calculate_size();
3360bc28b7cSLey Foon Tan 	/* If the size is invalid, use default Config size */
3370bc28b7cSLey Foon Tan 	if (size <= 0)
3380bc28b7cSLey Foon Tan 		gd->ram_size = PHYS_SDRAM_1_SIZE;
3390bc28b7cSLey Foon Tan 	else
3400bc28b7cSLey Foon Tan 		gd->ram_size = size;
3410bc28b7cSLey Foon Tan 
3420bc28b7cSLey Foon Tan 	/* Enable or disable the SDRAM ECC */
3430bc28b7cSLey Foon Tan 	if (CTRLCFG1_CFG_CTRL_EN_ECC(ctrlcfg1)) {
3440bc28b7cSLey Foon Tan 		setbits_le32(SOCFPGA_SDR_ADDRESS + ECCCTRL1,
3450bc28b7cSLey Foon Tan 			     (DDR_HMC_ECCCTL_AWB_CNT_RST_SET_MSK |
3460bc28b7cSLey Foon Tan 			      DDR_HMC_ECCCTL_CNT_RST_SET_MSK |
3470bc28b7cSLey Foon Tan 			      DDR_HMC_ECCCTL_ECC_EN_SET_MSK));
3480bc28b7cSLey Foon Tan 		clrbits_le32(SOCFPGA_SDR_ADDRESS + ECCCTRL1,
3490bc28b7cSLey Foon Tan 			     (DDR_HMC_ECCCTL_AWB_CNT_RST_SET_MSK |
3500bc28b7cSLey Foon Tan 			      DDR_HMC_ECCCTL_CNT_RST_SET_MSK));
3510bc28b7cSLey Foon Tan 		setbits_le32(SOCFPGA_SDR_ADDRESS + ECCCTRL2,
3520bc28b7cSLey Foon Tan 			     (DDR_HMC_ECCCTL2_RMW_EN_SET_MSK |
3530bc28b7cSLey Foon Tan 			      DDR_HMC_ECCCTL2_AWB_EN_SET_MSK));
3540bc28b7cSLey Foon Tan 	} else {
3550bc28b7cSLey Foon Tan 		clrbits_le32(SOCFPGA_SDR_ADDRESS + ECCCTRL1,
3560bc28b7cSLey Foon Tan 			     (DDR_HMC_ECCCTL_AWB_CNT_RST_SET_MSK |
3570bc28b7cSLey Foon Tan 			      DDR_HMC_ECCCTL_CNT_RST_SET_MSK |
3580bc28b7cSLey Foon Tan 			      DDR_HMC_ECCCTL_ECC_EN_SET_MSK));
3590bc28b7cSLey Foon Tan 		clrbits_le32(SOCFPGA_SDR_ADDRESS + ECCCTRL2,
3600bc28b7cSLey Foon Tan 			     (DDR_HMC_ECCCTL2_RMW_EN_SET_MSK |
3610bc28b7cSLey Foon Tan 			      DDR_HMC_ECCCTL2_AWB_EN_SET_MSK));
3620bc28b7cSLey Foon Tan 	}
3630bc28b7cSLey Foon Tan 
3640bc28b7cSLey Foon Tan 	debug("DDR: HMC init success\n");
3650bc28b7cSLey Foon Tan 	return 0;
3660bc28b7cSLey Foon Tan }
3670bc28b7cSLey Foon Tan 
3680bc28b7cSLey Foon Tan /**
3690bc28b7cSLey Foon Tan  * sdram_calculate_size() - Calculate SDRAM size
3700bc28b7cSLey Foon Tan  *
3710bc28b7cSLey Foon Tan  * Calculate SDRAM device size based on SDRAM controller parameters.
3720bc28b7cSLey Foon Tan  * Size is specified in bytes.
3730bc28b7cSLey Foon Tan  */
sdram_calculate_size(void)374*02d8d325SDalon Westergreen phys_size_t sdram_calculate_size(void)
3750bc28b7cSLey Foon Tan {
3760bc28b7cSLey Foon Tan 	u32 dramaddrw = hmc_readl(DRAMADDRW);
3770bc28b7cSLey Foon Tan 
378*02d8d325SDalon Westergreen 	phys_size_t size = 1 << (DRAMADDRW_CFG_CS_ADDR_WIDTH(dramaddrw) +
3790bc28b7cSLey Foon Tan 			 DRAMADDRW_CFG_BANK_GRP_ADDR_WIDTH(dramaddrw) +
3800bc28b7cSLey Foon Tan 			 DRAMADDRW_CFG_BANK_ADDR_WIDTH(dramaddrw) +
3810bc28b7cSLey Foon Tan 			 DRAMADDRW_CFG_ROW_ADDR_WIDTH(dramaddrw) +
3820bc28b7cSLey Foon Tan 			 DRAMADDRW_CFG_COL_ADDR_WIDTH(dramaddrw));
3830bc28b7cSLey Foon Tan 
3840bc28b7cSLey Foon Tan 	size *= (2 << (hmc_ecc_readl(DDRIOCTRL) &
3850bc28b7cSLey Foon Tan 			DDR_HMC_DDRIOCTRL_IOSIZE_MSK));
3860bc28b7cSLey Foon Tan 
3870bc28b7cSLey Foon Tan 	return size;
3880bc28b7cSLey Foon Tan }
389