xref: /openbmc/u-boot/drivers/ddr/fsl/arm_ddr_gen3.c (revision 5cb27c5d44ac789f0f0583b57c15dc708ca55c69)
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>
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  */
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;
439ac4ffbdSYork Sun #if defined(CONFIG_SYS_FSL_DDR2_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 1)
449ac4ffbdSYork Sun 	case 1:
459ac4ffbdSYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR2_ADDR;
469ac4ffbdSYork Sun 		break;
479ac4ffbdSYork Sun #endif
489ac4ffbdSYork Sun #if defined(CONFIG_SYS_FSL_DDR3_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 2)
499ac4ffbdSYork Sun 	case 2:
509ac4ffbdSYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR3_ADDR;
519ac4ffbdSYork Sun 		break;
529ac4ffbdSYork Sun #endif
539ac4ffbdSYork Sun #if defined(CONFIG_SYS_FSL_DDR4_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 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_cfg_2, regs->ddr_sdram_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->init_addr, regs->ddr_init_addr);
1094e5b1bd0SYork Sun 	ddr_out32(&ddr->init_ext_addr, regs->ddr_init_ext_addr);
1109ac4ffbdSYork Sun 
1114e5b1bd0SYork Sun 	ddr_out32(&ddr->timing_cfg_4, regs->timing_cfg_4);
1124e5b1bd0SYork Sun 	ddr_out32(&ddr->timing_cfg_5, regs->timing_cfg_5);
1134e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
1144e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
1159ac4ffbdSYork Sun #ifndef CONFIG_SYS_FSL_DDR_EMU
1169ac4ffbdSYork Sun 	/*
1179ac4ffbdSYork Sun 	 * Skip these two registers if running on emulator
1189ac4ffbdSYork Sun 	 * because emulator doesn't have skew between bytes.
1199ac4ffbdSYork Sun 	 */
1209ac4ffbdSYork Sun 
1219ac4ffbdSYork Sun 	if (regs->ddr_wrlvl_cntl_2)
1224e5b1bd0SYork Sun 		ddr_out32(&ddr->ddr_wrlvl_cntl_2, regs->ddr_wrlvl_cntl_2);
1239ac4ffbdSYork Sun 	if (regs->ddr_wrlvl_cntl_3)
1244e5b1bd0SYork Sun 		ddr_out32(&ddr->ddr_wrlvl_cntl_3, regs->ddr_wrlvl_cntl_3);
1259ac4ffbdSYork Sun #endif
1269ac4ffbdSYork Sun 
1274e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr);
1284e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_1, regs->ddr_sdram_rcw_1);
1294e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_2, regs->ddr_sdram_rcw_2);
1304e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_cdr1, regs->ddr_cdr1);
1314e5b1bd0SYork Sun 	ddr_out32(&ddr->ddr_cdr2, regs->ddr_cdr2);
1324e5b1bd0SYork Sun 	ddr_out32(&ddr->err_disable, regs->err_disable);
1334e5b1bd0SYork Sun 	ddr_out32(&ddr->err_int_en, regs->err_int_en);
1349ac4ffbdSYork Sun 	for (i = 0; i < 32; i++) {
1359ac4ffbdSYork Sun 		if (regs->debug[i]) {
1369ac4ffbdSYork Sun 			debug("Write to debug_%d as %08x\n", i + 1,
1379ac4ffbdSYork Sun 			      regs->debug[i]);
1384e5b1bd0SYork Sun 			ddr_out32(&ddr->debug[i], regs->debug[i]);
1399ac4ffbdSYork Sun 		}
1409ac4ffbdSYork Sun 	}
1419ac4ffbdSYork Sun 
1429ac4ffbdSYork Sun 	/*
1439ac4ffbdSYork Sun 	 * For RDIMMs, JEDEC spec requires clocks to be stable before reset is
1449ac4ffbdSYork Sun 	 * deasserted. Clocks start when any chip select is enabled and clock
1459ac4ffbdSYork Sun 	 * control register is set. Because all DDR components are connected to
1469ac4ffbdSYork Sun 	 * one reset signal, this needs to be done in two steps. Step 1 is to
1479ac4ffbdSYork Sun 	 * get the clocks started. Step 2 resumes after reset signal is
1489ac4ffbdSYork Sun 	 * deasserted.
1499ac4ffbdSYork Sun 	 */
1509ac4ffbdSYork Sun 	if (step == 1) {
1519ac4ffbdSYork Sun 		udelay(200);
1529ac4ffbdSYork Sun 		return;
1539ac4ffbdSYork Sun 	}
1549ac4ffbdSYork Sun 
1559ac4ffbdSYork Sun step2:
1569ac4ffbdSYork Sun 	/* Set, but do not enable the memory */
1579ac4ffbdSYork Sun 	temp_sdram_cfg = regs->ddr_sdram_cfg;
1589ac4ffbdSYork Sun 	temp_sdram_cfg &= ~(SDRAM_CFG_MEM_EN);
1594e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_cfg, temp_sdram_cfg);
1609ac4ffbdSYork Sun 
1619ac4ffbdSYork Sun 	/*
1629ac4ffbdSYork Sun 	 * 500 painful micro-seconds must elapse between
1639ac4ffbdSYork Sun 	 * the DDR clock setup and the DDR config enable.
1649ac4ffbdSYork Sun 	 * DDR2 need 200 us, and DDR3 need 500 us from spec,
1659ac4ffbdSYork Sun 	 * we choose the max, that is 500 us for all of case.
1669ac4ffbdSYork Sun 	 */
1679ac4ffbdSYork Sun 	udelay(500);
1689ac4ffbdSYork Sun 	asm volatile("dsb sy;isb");
1699ac4ffbdSYork Sun 
1709ac4ffbdSYork Sun 	/* Let the controller go */
1714e5b1bd0SYork Sun 	temp_sdram_cfg = ddr_in32(&ddr->sdram_cfg) & ~SDRAM_CFG_BI;
1724e5b1bd0SYork Sun 	ddr_out32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_MEM_EN);
1739ac4ffbdSYork Sun 	asm volatile("dsb sy;isb");
1749ac4ffbdSYork Sun 
1759ac4ffbdSYork Sun 	total_gb_size_per_controller = 0;
1769ac4ffbdSYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1779ac4ffbdSYork Sun 		if (!(regs->cs[i].config & 0x80000000))
1789ac4ffbdSYork Sun 			continue;
1799ac4ffbdSYork Sun 		total_gb_size_per_controller += 1 << (
1809ac4ffbdSYork Sun 			((regs->cs[i].config >> 14) & 0x3) + 2 +
1819ac4ffbdSYork Sun 			((regs->cs[i].config >> 8) & 0x7) + 12 +
1829ac4ffbdSYork Sun 			((regs->cs[i].config >> 0) & 0x7) + 8 +
1839ac4ffbdSYork Sun 			3 - ((regs->ddr_sdram_cfg >> 19) & 0x3) -
1849ac4ffbdSYork Sun 			26);			/* minus 26 (count of 64M) */
1859ac4ffbdSYork Sun 	}
1869ac4ffbdSYork Sun 	if (regs->cs[0].config & 0x20000000) {
1879ac4ffbdSYork Sun 		/* 2-way interleaving */
1889ac4ffbdSYork Sun 		total_gb_size_per_controller <<= 1;
1899ac4ffbdSYork Sun 	}
1909ac4ffbdSYork Sun 	/*
1919ac4ffbdSYork Sun 	 * total memory / bus width = transactions needed
1929ac4ffbdSYork Sun 	 * transactions needed / data rate = seconds
1939ac4ffbdSYork Sun 	 * to add plenty of buffer, double the time
1949ac4ffbdSYork Sun 	 * For example, 2GB on 666MT/s 64-bit bus takes about 402ms
1959ac4ffbdSYork Sun 	 * Let's wait for 800ms
1969ac4ffbdSYork Sun 	 */
197*5cb27c5dSYork Sun 	bus_width = 3 - ((ddr_in32(&ddr->sdram_cfg) & SDRAM_CFG_DBW_MASK)
1989ac4ffbdSYork Sun 			>> SDRAM_CFG_DBW_SHIFT);
1999ac4ffbdSYork Sun 	timeout = ((total_gb_size_per_controller << (6 - bus_width)) * 100 /
2009ac4ffbdSYork Sun 		(get_ddr_freq(0) >> 20)) << 1;
2019ac4ffbdSYork Sun 	total_gb_size_per_controller >>= 4;	/* shift down to gb size */
2029ac4ffbdSYork Sun 	debug("total %d GB\n", total_gb_size_per_controller);
2039ac4ffbdSYork Sun 	debug("Need to wait up to %d * 10ms\n", timeout);
2049ac4ffbdSYork Sun 
2059ac4ffbdSYork Sun 	/* Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done.  */
2064e5b1bd0SYork Sun 	while ((ddr_in32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
2079ac4ffbdSYork Sun 		(timeout >= 0)) {
2089ac4ffbdSYork Sun 		udelay(10000);		/* throttle polling rate */
2099ac4ffbdSYork Sun 		timeout--;
2109ac4ffbdSYork Sun 	}
2119ac4ffbdSYork Sun 
2129ac4ffbdSYork Sun 	if (timeout <= 0)
2139ac4ffbdSYork Sun 		printf("Waiting for D_INIT timeout. Memory may not work.\n");
2149ac4ffbdSYork Sun }
215