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