xref: /openbmc/u-boot/drivers/ddr/fsl/arm_ddr_gen3.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
29ac4ffbdSYork Sun /*
39ac4ffbdSYork Sun  * Copyright 2013 Freescale Semiconductor, Inc.
49ac4ffbdSYork Sun  *
59ac4ffbdSYork Sun  * Derived from mpc85xx_ddr_gen3.c, removed all workarounds
69ac4ffbdSYork Sun  */
79ac4ffbdSYork Sun 
89ac4ffbdSYork Sun #include <common.h>
99ac4ffbdSYork Sun #include <asm/io.h>
109ac4ffbdSYork Sun #include <fsl_ddr_sdram.h>
119ac4ffbdSYork Sun #include <asm/processor.h>
129ac4ffbdSYork Sun #include <fsl_immap.h>
134e5b1bd0SYork Sun #include <fsl_ddr.h>
146e2941d7SSimon Glass #include <asm/arch/clock.h>
159ac4ffbdSYork Sun 
169ac4ffbdSYork Sun #if (CONFIG_CHIP_SELECTS_PER_CTRL > 4)
179ac4ffbdSYork Sun #error Invalid setting for CONFIG_CHIP_SELECTS_PER_CTRL
189ac4ffbdSYork Sun #endif
199ac4ffbdSYork Sun 
209ac4ffbdSYork Sun 
219ac4ffbdSYork Sun /*
229ac4ffbdSYork Sun  * regs has the to-be-set values for DDR controller registers
239ac4ffbdSYork Sun  * ctrl_num is the DDR controller number
249ac4ffbdSYork Sun  * step: 0 goes through the initialization in one pass
259ac4ffbdSYork Sun  *       1 sets registers and returns before enabling controller
269ac4ffbdSYork Sun  *       2 resumes from step 1 and continues to initialize
279ac4ffbdSYork Sun  * Dividing the initialization to two steps to deassert DDR reset signal
289ac4ffbdSYork Sun  * to comply with JEDEC specs for RDIMMs.
299ac4ffbdSYork Sun  */
fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t * regs,unsigned int ctrl_num,int step)309ac4ffbdSYork Sun void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
319ac4ffbdSYork Sun 			     unsigned int ctrl_num, int step)
329ac4ffbdSYork Sun {
339ac4ffbdSYork Sun 	unsigned int i, bus_width;
349ac4ffbdSYork Sun 	struct ccsr_ddr __iomem *ddr;
359ac4ffbdSYork Sun 	u32 temp_sdram_cfg;
369ac4ffbdSYork Sun 	u32 total_gb_size_per_controller;
379ac4ffbdSYork Sun 	int timeout;
389ac4ffbdSYork Sun 
399ac4ffbdSYork Sun 	switch (ctrl_num) {
409ac4ffbdSYork Sun 	case 0:
419ac4ffbdSYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
429ac4ffbdSYork Sun 		break;
4351370d56SYork Sun #if defined(CONFIG_SYS_FSL_DDR2_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 1)
449ac4ffbdSYork Sun 	case 1:
459ac4ffbdSYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR2_ADDR;
469ac4ffbdSYork Sun 		break;
479ac4ffbdSYork Sun #endif
4851370d56SYork Sun #if defined(CONFIG_SYS_FSL_DDR3_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 2)
499ac4ffbdSYork Sun 	case 2:
509ac4ffbdSYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR3_ADDR;
519ac4ffbdSYork Sun 		break;
529ac4ffbdSYork Sun #endif
5351370d56SYork Sun #if defined(CONFIG_SYS_FSL_DDR4_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 3)
549ac4ffbdSYork Sun 	case 3:
559ac4ffbdSYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR4_ADDR;
569ac4ffbdSYork Sun 		break;
579ac4ffbdSYork Sun #endif
589ac4ffbdSYork Sun 	default:
599ac4ffbdSYork Sun 		printf("%s unexpected ctrl_num = %u\n", __func__, ctrl_num);
609ac4ffbdSYork Sun 		return;
619ac4ffbdSYork Sun 	}
629ac4ffbdSYork Sun 
639ac4ffbdSYork Sun 	if (step == 2)
649ac4ffbdSYork Sun 		goto step2;
659ac4ffbdSYork Sun 
669ac4ffbdSYork Sun 	if (regs->ddr_eor)
674e5b1bd0SYork Sun 		ddr_out32(&ddr->eor, regs->ddr_eor);
689ac4ffbdSYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
699ac4ffbdSYork Sun 		if (i == 0) {
704e5b1bd0SYork Sun 			ddr_out32(&ddr->cs0_bnds, regs->cs[i].bnds);
714e5b1bd0SYork Sun 			ddr_out32(&ddr->cs0_config, regs->cs[i].config);
724e5b1bd0SYork Sun 			ddr_out32(&ddr->cs0_config_2, regs->cs[i].config_2);
739ac4ffbdSYork Sun 
749ac4ffbdSYork Sun 		} else if (i == 1) {
754e5b1bd0SYork Sun 			ddr_out32(&ddr->cs1_bnds, regs->cs[i].bnds);
764e5b1bd0SYork Sun 			ddr_out32(&ddr->cs1_config, regs->cs[i].config);
774e5b1bd0SYork Sun 			ddr_out32(&ddr->cs1_config_2, regs->cs[i].config_2);
789ac4ffbdSYork Sun 
799ac4ffbdSYork Sun 		} else if (i == 2) {
804e5b1bd0SYork Sun 			ddr_out32(&ddr->cs2_bnds, regs->cs[i].bnds);
814e5b1bd0SYork Sun 			ddr_out32(&ddr->cs2_config, regs->cs[i].config);
824e5b1bd0SYork Sun 			ddr_out32(&ddr->cs2_config_2, regs->cs[i].config_2);
839ac4ffbdSYork Sun 
849ac4ffbdSYork Sun 		} else if (i == 3) {
854e5b1bd0SYork Sun 			ddr_out32(&ddr->cs3_bnds, regs->cs[i].bnds);
864e5b1bd0SYork Sun 			ddr_out32(&ddr->cs3_config, regs->cs[i].config);
874e5b1bd0SYork Sun 			ddr_out32(&ddr->cs3_config_2, regs->cs[i].config_2);
889ac4ffbdSYork Sun 		}
899ac4ffbdSYork Sun 	}
909ac4ffbdSYork Sun 
914e5b1bd0SYork Sun 	ddr_out32(&ddr->timing_cfg_3, regs->timing_cfg_3);
924e5b1bd0SYork Sun 	ddr_out32(&ddr->timing_cfg_0, regs->timing_cfg_0);
934e5b1bd0SYork Sun 	ddr_out32(&ddr->timing_cfg_1, regs->timing_cfg_1);
944e5b1bd0SYork Sun 	ddr_out32(&ddr->timing_cfg_2, regs->timing_cfg_2);
954e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_mode, regs->ddr_sdram_mode);
964e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_mode_2, regs->ddr_sdram_mode_2);
974e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_mode_3, regs->ddr_sdram_mode_3);
984e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_mode_4, regs->ddr_sdram_mode_4);
994e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_mode_5, regs->ddr_sdram_mode_5);
1004e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_mode_6, regs->ddr_sdram_mode_6);
1014e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_mode_7, regs->ddr_sdram_mode_7);
1024e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_mode_8, regs->ddr_sdram_mode_8);
1034e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_md_cntl, regs->ddr_sdram_md_cntl);
1044e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_interval, regs->ddr_sdram_interval);
1054e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_data_init, regs->ddr_data_init);
1064e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_clk_cntl, regs->ddr_sdram_clk_cntl);
1074e5b1bd0SYork Sun 	ddr_out32(&ddr->timing_cfg_4, regs->timing_cfg_4);
1084e5b1bd0SYork Sun 	ddr_out32(&ddr->timing_cfg_5, regs->timing_cfg_5);
1094e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
1104e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
1119ac4ffbdSYork Sun #ifndef CONFIG_SYS_FSL_DDR_EMU
1129ac4ffbdSYork Sun 	/*
1139ac4ffbdSYork Sun 	 * Skip these two registers if running on emulator
1149ac4ffbdSYork Sun 	 * because emulator doesn't have skew between bytes.
1159ac4ffbdSYork Sun 	 */
1169ac4ffbdSYork Sun 
1179ac4ffbdSYork Sun 	if (regs->ddr_wrlvl_cntl_2)
1184e5b1bd0SYork Sun 		ddr_out32(&ddr->ddr_wrlvl_cntl_2, regs->ddr_wrlvl_cntl_2);
1199ac4ffbdSYork Sun 	if (regs->ddr_wrlvl_cntl_3)
1204e5b1bd0SYork Sun 		ddr_out32(&ddr->ddr_wrlvl_cntl_3, regs->ddr_wrlvl_cntl_3);
1219ac4ffbdSYork Sun #endif
1229ac4ffbdSYork Sun 
1234e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr);
1244e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_1, regs->ddr_sdram_rcw_1);
1254e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_2, regs->ddr_sdram_rcw_2);
1264e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_cdr1, regs->ddr_cdr1);
127a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
128a7787b78STang Yuantian 	if (is_warm_boot()) {
129a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2,
130a7787b78STang Yuantian 			  regs->ddr_sdram_cfg_2 & ~SDRAM_CFG2_D_INIT);
131a7787b78STang Yuantian 		ddr_out32(&ddr->init_addr, CONFIG_SYS_SDRAM_BASE);
132a7787b78STang Yuantian 		ddr_out32(&ddr->init_ext_addr, DDR_INIT_ADDR_EXT_UIA);
133a7787b78STang Yuantian 
134a7787b78STang Yuantian 		/* DRAM VRef will not be trained */
135a7787b78STang Yuantian 		ddr_out32(&ddr->ddr_cdr2,
136a7787b78STang Yuantian 			  regs->ddr_cdr2 & ~DDR_CDR2_VREF_TRAIN_EN);
137a7787b78STang Yuantian 	} else
138a7787b78STang Yuantian #endif
139a7787b78STang Yuantian 	{
140a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
141a7787b78STang Yuantian 		ddr_out32(&ddr->init_addr, regs->ddr_init_addr);
142a7787b78STang Yuantian 		ddr_out32(&ddr->init_ext_addr, regs->ddr_init_ext_addr);
1434e5b1bd0SYork Sun 		ddr_out32(&ddr->ddr_cdr2, regs->ddr_cdr2);
144a7787b78STang Yuantian 	}
1454e5b1bd0SYork Sun 	ddr_out32(&ddr->err_disable, regs->err_disable);
1464e5b1bd0SYork Sun 	ddr_out32(&ddr->err_int_en, regs->err_int_en);
1479ac4ffbdSYork Sun 	for (i = 0; i < 32; i++) {
1489ac4ffbdSYork Sun 		if (regs->debug[i]) {
1499ac4ffbdSYork Sun 			debug("Write to debug_%d as %08x\n", i + 1,
1509ac4ffbdSYork Sun 			      regs->debug[i]);
1514e5b1bd0SYork Sun 			ddr_out32(&ddr->debug[i], regs->debug[i]);
1529ac4ffbdSYork Sun 		}
1539ac4ffbdSYork Sun 	}
1549ac4ffbdSYork Sun 
1559ac4ffbdSYork Sun 	/*
1569ac4ffbdSYork Sun 	 * For RDIMMs, JEDEC spec requires clocks to be stable before reset is
1579ac4ffbdSYork Sun 	 * deasserted. Clocks start when any chip select is enabled and clock
1589ac4ffbdSYork Sun 	 * control register is set. Because all DDR components are connected to
1599ac4ffbdSYork Sun 	 * one reset signal, this needs to be done in two steps. Step 1 is to
1609ac4ffbdSYork Sun 	 * get the clocks started. Step 2 resumes after reset signal is
1619ac4ffbdSYork Sun 	 * deasserted.
1629ac4ffbdSYork Sun 	 */
1639ac4ffbdSYork Sun 	if (step == 1) {
1649ac4ffbdSYork Sun 		udelay(200);
1659ac4ffbdSYork Sun 		return;
1669ac4ffbdSYork Sun 	}
1679ac4ffbdSYork Sun 
1689ac4ffbdSYork Sun step2:
1699ac4ffbdSYork Sun 	/* Set, but do not enable the memory */
1709ac4ffbdSYork Sun 	temp_sdram_cfg = regs->ddr_sdram_cfg;
1719ac4ffbdSYork Sun 	temp_sdram_cfg &= ~(SDRAM_CFG_MEM_EN);
1724e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_cfg, temp_sdram_cfg);
1739ac4ffbdSYork Sun 
1749ac4ffbdSYork Sun 	/*
1759ac4ffbdSYork Sun 	 * 500 painful micro-seconds must elapse between
1769ac4ffbdSYork Sun 	 * the DDR clock setup and the DDR config enable.
1779ac4ffbdSYork Sun 	 * DDR2 need 200 us, and DDR3 need 500 us from spec,
1789ac4ffbdSYork Sun 	 * we choose the max, that is 500 us for all of case.
1799ac4ffbdSYork Sun 	 */
1809ac4ffbdSYork Sun 	udelay(500);
1819ac4ffbdSYork Sun 	asm volatile("dsb sy;isb");
1829ac4ffbdSYork Sun 
183a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
184a7787b78STang Yuantian 	if (is_warm_boot()) {
185a7787b78STang Yuantian 		/* enter self-refresh */
186a7787b78STang Yuantian 		temp_sdram_cfg = ddr_in32(&ddr->sdram_cfg_2);
187a7787b78STang Yuantian 		temp_sdram_cfg |= SDRAM_CFG2_FRC_SR;
188a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2, temp_sdram_cfg);
189a7787b78STang Yuantian 		/* do board specific memory setup */
190a7787b78STang Yuantian 		board_mem_sleep_setup();
191a7787b78STang Yuantian 
192a7787b78STang Yuantian 		temp_sdram_cfg = (ddr_in32(&ddr->sdram_cfg) | SDRAM_CFG_BI);
193a7787b78STang Yuantian 	} else
194a7787b78STang Yuantian #endif
1954e5b1bd0SYork Sun 		temp_sdram_cfg = ddr_in32(&ddr->sdram_cfg) & ~SDRAM_CFG_BI;
196a7787b78STang Yuantian 	/* Let the controller go */
1974e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_MEM_EN);
1989ac4ffbdSYork Sun 	asm volatile("dsb sy;isb");
1999ac4ffbdSYork Sun 
2009ac4ffbdSYork Sun 	total_gb_size_per_controller = 0;
2019ac4ffbdSYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
2029ac4ffbdSYork Sun 		if (!(regs->cs[i].config & 0x80000000))
2039ac4ffbdSYork Sun 			continue;
2049ac4ffbdSYork Sun 		total_gb_size_per_controller += 1 << (
2059ac4ffbdSYork Sun 			((regs->cs[i].config >> 14) & 0x3) + 2 +
2069ac4ffbdSYork Sun 			((regs->cs[i].config >> 8) & 0x7) + 12 +
2079ac4ffbdSYork Sun 			((regs->cs[i].config >> 0) & 0x7) + 8 +
2089ac4ffbdSYork Sun 			3 - ((regs->ddr_sdram_cfg >> 19) & 0x3) -
2099ac4ffbdSYork Sun 			26);			/* minus 26 (count of 64M) */
2109ac4ffbdSYork Sun 	}
2119ac4ffbdSYork Sun 	if (regs->cs[0].config & 0x20000000) {
2129ac4ffbdSYork Sun 		/* 2-way interleaving */
2139ac4ffbdSYork Sun 		total_gb_size_per_controller <<= 1;
2149ac4ffbdSYork Sun 	}
2159ac4ffbdSYork Sun 	/*
2169ac4ffbdSYork Sun 	 * total memory / bus width = transactions needed
2179ac4ffbdSYork Sun 	 * transactions needed / data rate = seconds
2189ac4ffbdSYork Sun 	 * to add plenty of buffer, double the time
2199ac4ffbdSYork Sun 	 * For example, 2GB on 666MT/s 64-bit bus takes about 402ms
2209ac4ffbdSYork Sun 	 * Let's wait for 800ms
2219ac4ffbdSYork Sun 	 */
2225cb27c5dSYork Sun 	bus_width = 3 - ((ddr_in32(&ddr->sdram_cfg) & SDRAM_CFG_DBW_MASK)
2239ac4ffbdSYork Sun 			>> SDRAM_CFG_DBW_SHIFT);
2249ac4ffbdSYork Sun 	timeout = ((total_gb_size_per_controller << (6 - bus_width)) * 100 /
22503e664d8SYork Sun 		(get_ddr_freq(ctrl_num) >> 20)) << 1;
2269ac4ffbdSYork Sun 	total_gb_size_per_controller >>= 4;	/* shift down to gb size */
2279ac4ffbdSYork Sun 	debug("total %d GB\n", total_gb_size_per_controller);
2289ac4ffbdSYork Sun 	debug("Need to wait up to %d * 10ms\n", timeout);
2299ac4ffbdSYork Sun 
2309ac4ffbdSYork Sun 	/* Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done.  */
2314e5b1bd0SYork Sun 	while ((ddr_in32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
2329ac4ffbdSYork Sun 		(timeout >= 0)) {
2339ac4ffbdSYork Sun 		udelay(10000);		/* throttle polling rate */
2349ac4ffbdSYork Sun 		timeout--;
2359ac4ffbdSYork Sun 	}
2369ac4ffbdSYork Sun 
2379ac4ffbdSYork Sun 	if (timeout <= 0)
2389ac4ffbdSYork Sun 		printf("Waiting for D_INIT timeout. Memory may not work.\n");
239a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
240a7787b78STang Yuantian 	if (is_warm_boot()) {
241a7787b78STang Yuantian 		/* exit self-refresh */
242a7787b78STang Yuantian 		temp_sdram_cfg = ddr_in32(&ddr->sdram_cfg_2);
243a7787b78STang Yuantian 		temp_sdram_cfg &= ~SDRAM_CFG2_FRC_SR;
244a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2, temp_sdram_cfg);
245a7787b78STang Yuantian 	}
246a7787b78STang Yuantian #endif
2479ac4ffbdSYork Sun }
248