xref: /openbmc/u-boot/drivers/ddr/fsl/fsl_ddr_gen4.c (revision dda3b610eee9dcd433627202584ded417327dd51)
134e026f9SYork Sun /*
234e026f9SYork Sun  * Copyright 2014 Freescale Semiconductor, Inc.
334e026f9SYork Sun  *
434e026f9SYork Sun  * SPDX-License-Identifier:	GPL-2.0+
534e026f9SYork Sun  */
634e026f9SYork Sun 
734e026f9SYork Sun #include <common.h>
834e026f9SYork Sun #include <asm/io.h>
934e026f9SYork Sun #include <fsl_ddr_sdram.h>
1034e026f9SYork Sun #include <asm/processor.h>
118340e7acSYork Sun #include <fsl_immap.h>
1234e026f9SYork Sun #include <fsl_ddr.h>
1334e026f9SYork Sun 
1434e026f9SYork Sun #if (CONFIG_CHIP_SELECTS_PER_CTRL > 4)
1534e026f9SYork Sun #error Invalid setting for CONFIG_CHIP_SELECTS_PER_CTRL
1634e026f9SYork Sun #endif
1734e026f9SYork Sun 
1834e026f9SYork Sun /*
1934e026f9SYork Sun  * regs has the to-be-set values for DDR controller registers
2034e026f9SYork Sun  * ctrl_num is the DDR controller number
2134e026f9SYork Sun  * step: 0 goes through the initialization in one pass
2234e026f9SYork Sun  *       1 sets registers and returns before enabling controller
2334e026f9SYork Sun  *       2 resumes from step 1 and continues to initialize
2434e026f9SYork Sun  * Dividing the initialization to two steps to deassert DDR reset signal
2534e026f9SYork Sun  * to comply with JEDEC specs for RDIMMs.
2634e026f9SYork Sun  */
2734e026f9SYork Sun void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
2834e026f9SYork Sun 			     unsigned int ctrl_num, int step)
2934e026f9SYork Sun {
3034e026f9SYork Sun 	unsigned int i, bus_width;
3134e026f9SYork Sun 	struct ccsr_ddr __iomem *ddr;
3234e026f9SYork Sun 	u32 temp_sdram_cfg;
3334e026f9SYork Sun 	u32 total_gb_size_per_controller;
3434e026f9SYork Sun 	int timeout;
3534e026f9SYork Sun 
3634e026f9SYork Sun 	switch (ctrl_num) {
3734e026f9SYork Sun 	case 0:
3834e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
3934e026f9SYork Sun 		break;
4034e026f9SYork Sun #if defined(CONFIG_SYS_FSL_DDR2_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 1)
4134e026f9SYork Sun 	case 1:
4234e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR2_ADDR;
4334e026f9SYork Sun 		break;
4434e026f9SYork Sun #endif
4534e026f9SYork Sun #if defined(CONFIG_SYS_FSL_DDR3_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 2)
4634e026f9SYork Sun 	case 2:
4734e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR3_ADDR;
4834e026f9SYork Sun 		break;
4934e026f9SYork Sun #endif
5034e026f9SYork Sun #if defined(CONFIG_SYS_FSL_DDR4_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 3)
5134e026f9SYork Sun 	case 3:
5234e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR4_ADDR;
5334e026f9SYork Sun 		break;
5434e026f9SYork Sun #endif
5534e026f9SYork Sun 	default:
5634e026f9SYork Sun 		printf("%s unexpected ctrl_num = %u\n", __func__, ctrl_num);
5734e026f9SYork Sun 		return;
5834e026f9SYork Sun 	}
5934e026f9SYork Sun 
6034e026f9SYork Sun 	if (step == 2)
6134e026f9SYork Sun 		goto step2;
6234e026f9SYork Sun 
6334e026f9SYork Sun 	if (regs->ddr_eor)
6434e026f9SYork Sun 		ddr_out32(&ddr->eor, regs->ddr_eor);
6534e026f9SYork Sun 
6634e026f9SYork Sun 	ddr_out32(&ddr->sdram_clk_cntl, regs->ddr_sdram_clk_cntl);
6734e026f9SYork Sun 
6834e026f9SYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
6934e026f9SYork Sun 		if (i == 0) {
7034e026f9SYork Sun 			ddr_out32(&ddr->cs0_bnds, regs->cs[i].bnds);
7134e026f9SYork Sun 			ddr_out32(&ddr->cs0_config, regs->cs[i].config);
7234e026f9SYork Sun 			ddr_out32(&ddr->cs0_config_2, regs->cs[i].config_2);
7334e026f9SYork Sun 
7434e026f9SYork Sun 		} else if (i == 1) {
7534e026f9SYork Sun 			ddr_out32(&ddr->cs1_bnds, regs->cs[i].bnds);
7634e026f9SYork Sun 			ddr_out32(&ddr->cs1_config, regs->cs[i].config);
7734e026f9SYork Sun 			ddr_out32(&ddr->cs1_config_2, regs->cs[i].config_2);
7834e026f9SYork Sun 
7934e026f9SYork Sun 		} else if (i == 2) {
8034e026f9SYork Sun 			ddr_out32(&ddr->cs2_bnds, regs->cs[i].bnds);
8134e026f9SYork Sun 			ddr_out32(&ddr->cs2_config, regs->cs[i].config);
8234e026f9SYork Sun 			ddr_out32(&ddr->cs2_config_2, regs->cs[i].config_2);
8334e026f9SYork Sun 
8434e026f9SYork Sun 		} else if (i == 3) {
8534e026f9SYork Sun 			ddr_out32(&ddr->cs3_bnds, regs->cs[i].bnds);
8634e026f9SYork Sun 			ddr_out32(&ddr->cs3_config, regs->cs[i].config);
8734e026f9SYork Sun 			ddr_out32(&ddr->cs3_config_2, regs->cs[i].config_2);
8834e026f9SYork Sun 		}
8934e026f9SYork Sun 	}
9034e026f9SYork Sun 
9134e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_3, regs->timing_cfg_3);
9234e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_0, regs->timing_cfg_0);
9334e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_1, regs->timing_cfg_1);
9434e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_2, regs->timing_cfg_2);
9534e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_4, regs->timing_cfg_4);
9634e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_5, regs->timing_cfg_5);
9734e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_6, regs->timing_cfg_6);
9834e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_7, regs->timing_cfg_7);
9934e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_8, regs->timing_cfg_8);
10034e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_9, regs->timing_cfg_9);
10134e026f9SYork Sun 	ddr_out32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
10234e026f9SYork Sun 	ddr_out32(&ddr->dq_map_0, regs->dq_map_0);
10334e026f9SYork Sun 	ddr_out32(&ddr->dq_map_1, regs->dq_map_1);
10434e026f9SYork Sun 	ddr_out32(&ddr->dq_map_2, regs->dq_map_2);
10534e026f9SYork Sun 	ddr_out32(&ddr->dq_map_3, regs->dq_map_3);
10634e026f9SYork Sun 	ddr_out32(&ddr->sdram_cfg_3, regs->ddr_sdram_cfg_3);
10734e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode, regs->ddr_sdram_mode);
10834e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_2, regs->ddr_sdram_mode_2);
10934e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_3, regs->ddr_sdram_mode_3);
11034e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_4, regs->ddr_sdram_mode_4);
11134e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_5, regs->ddr_sdram_mode_5);
11234e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_6, regs->ddr_sdram_mode_6);
11334e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_7, regs->ddr_sdram_mode_7);
11434e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_8, regs->ddr_sdram_mode_8);
11534e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_9, regs->ddr_sdram_mode_9);
11634e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_10, regs->ddr_sdram_mode_10);
11734e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_11, regs->ddr_sdram_mode_11);
11834e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_12, regs->ddr_sdram_mode_12);
11934e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_13, regs->ddr_sdram_mode_13);
12034e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_14, regs->ddr_sdram_mode_14);
12134e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_15, regs->ddr_sdram_mode_15);
12234e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_16, regs->ddr_sdram_mode_16);
12334e026f9SYork Sun 	ddr_out32(&ddr->sdram_md_cntl, regs->ddr_sdram_md_cntl);
12434e026f9SYork Sun 	ddr_out32(&ddr->sdram_interval, regs->ddr_sdram_interval);
12534e026f9SYork Sun 	ddr_out32(&ddr->sdram_data_init, regs->ddr_data_init);
12634e026f9SYork Sun 	ddr_out32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
12734e026f9SYork Sun #ifndef CONFIG_SYS_FSL_DDR_EMU
12834e026f9SYork Sun 	/*
12934e026f9SYork Sun 	 * Skip these two registers if running on emulator
13034e026f9SYork Sun 	 * because emulator doesn't have skew between bytes.
13134e026f9SYork Sun 	 */
13234e026f9SYork Sun 
13334e026f9SYork Sun 	if (regs->ddr_wrlvl_cntl_2)
13434e026f9SYork Sun 		ddr_out32(&ddr->ddr_wrlvl_cntl_2, regs->ddr_wrlvl_cntl_2);
13534e026f9SYork Sun 	if (regs->ddr_wrlvl_cntl_3)
13634e026f9SYork Sun 		ddr_out32(&ddr->ddr_wrlvl_cntl_3, regs->ddr_wrlvl_cntl_3);
13734e026f9SYork Sun #endif
13834e026f9SYork Sun 
13934e026f9SYork Sun 	ddr_out32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr);
14034e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_1, regs->ddr_sdram_rcw_1);
14134e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_2, regs->ddr_sdram_rcw_2);
14234e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_3, regs->ddr_sdram_rcw_3);
14334e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_4, regs->ddr_sdram_rcw_4);
14434e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_5, regs->ddr_sdram_rcw_5);
14534e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_6, regs->ddr_sdram_rcw_6);
14634e026f9SYork Sun 	ddr_out32(&ddr->ddr_cdr1, regs->ddr_cdr1);
147a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
148a7787b78STang Yuantian 	if (is_warm_boot()) {
149a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2,
150a7787b78STang Yuantian 			  regs->ddr_sdram_cfg_2 & ~SDRAM_CFG2_D_INIT);
151a7787b78STang Yuantian 		ddr_out32(&ddr->init_addr, CONFIG_SYS_SDRAM_BASE);
152a7787b78STang Yuantian 		ddr_out32(&ddr->init_ext_addr, DDR_INIT_ADDR_EXT_UIA);
153a7787b78STang Yuantian 
154a7787b78STang Yuantian 		/* DRAM VRef will not be trained */
155a7787b78STang Yuantian 		ddr_out32(&ddr->ddr_cdr2,
156a7787b78STang Yuantian 			  regs->ddr_cdr2 & ~DDR_CDR2_VREF_TRAIN_EN);
157a7787b78STang Yuantian 	} else
158a7787b78STang Yuantian #endif
159a7787b78STang Yuantian 	{
160a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
161a7787b78STang Yuantian 		ddr_out32(&ddr->init_addr, regs->ddr_init_addr);
162a7787b78STang Yuantian 		ddr_out32(&ddr->init_ext_addr, regs->ddr_init_ext_addr);
16334e026f9SYork Sun 		ddr_out32(&ddr->ddr_cdr2, regs->ddr_cdr2);
164a7787b78STang Yuantian 	}
16534e026f9SYork Sun 	ddr_out32(&ddr->err_disable, regs->err_disable);
16634e026f9SYork Sun 	ddr_out32(&ddr->err_int_en, regs->err_int_en);
16734e026f9SYork Sun 	for (i = 0; i < 32; i++) {
16834e026f9SYork Sun 		if (regs->debug[i]) {
16934e026f9SYork Sun 			debug("Write to debug_%d as %08x\n",
17034e026f9SYork Sun 			      i+1, regs->debug[i]);
17134e026f9SYork Sun 			ddr_out32(&ddr->debug[i], regs->debug[i]);
17234e026f9SYork Sun 		}
17334e026f9SYork Sun 	}
174*dda3b610SYork Sun #ifdef CONFIG_SYS_FSL_ERRATUM_A008378
175*dda3b610SYork Sun 	/* Erratum applies when accumulated ECC is used, or DBI is enabled */
176*dda3b610SYork Sun #define IS_ACC_ECC_EN(v) ((v) & 0x4)
177*dda3b610SYork Sun #define IS_DBI(v) ((((v) >> 12) & 0x3) == 0x2)
178*dda3b610SYork Sun 	if (IS_ACC_ECC_EN(regs->ddr_sdram_cfg) ||
179*dda3b610SYork Sun 	    IS_DBI(regs->ddr_sdram_cfg_3))
180*dda3b610SYork Sun 		ddr_setbits32(ddr->debug[28], 0x9 << 20);
181*dda3b610SYork Sun #endif
18234e026f9SYork Sun 
18334e026f9SYork Sun 	/*
18434e026f9SYork Sun 	 * For RDIMMs, JEDEC spec requires clocks to be stable before reset is
18534e026f9SYork Sun 	 * deasserted. Clocks start when any chip select is enabled and clock
18634e026f9SYork Sun 	 * control register is set. Because all DDR components are connected to
18734e026f9SYork Sun 	 * one reset signal, this needs to be done in two steps. Step 1 is to
18834e026f9SYork Sun 	 * get the clocks started. Step 2 resumes after reset signal is
18934e026f9SYork Sun 	 * deasserted.
19034e026f9SYork Sun 	 */
19134e026f9SYork Sun 	if (step == 1) {
19234e026f9SYork Sun 		udelay(200);
19334e026f9SYork Sun 		return;
19434e026f9SYork Sun 	}
19534e026f9SYork Sun 
19634e026f9SYork Sun step2:
19734e026f9SYork Sun 	/* Set, but do not enable the memory */
19834e026f9SYork Sun 	temp_sdram_cfg = regs->ddr_sdram_cfg;
19934e026f9SYork Sun 	temp_sdram_cfg &= ~(SDRAM_CFG_MEM_EN);
20034e026f9SYork Sun 	ddr_out32(&ddr->sdram_cfg, temp_sdram_cfg);
20134e026f9SYork Sun 
20234e026f9SYork Sun 	/*
20334e026f9SYork Sun 	 * 500 painful micro-seconds must elapse between
20434e026f9SYork Sun 	 * the DDR clock setup and the DDR config enable.
20534e026f9SYork Sun 	 * DDR2 need 200 us, and DDR3 need 500 us from spec,
20634e026f9SYork Sun 	 * we choose the max, that is 500 us for all of case.
20734e026f9SYork Sun 	 */
20834e026f9SYork Sun 	udelay(500);
2098340e7acSYork Sun 	mb();
2108340e7acSYork Sun 	isb();
21134e026f9SYork Sun 
212a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
213a7787b78STang Yuantian 	if (is_warm_boot()) {
214a7787b78STang Yuantian 		/* enter self-refresh */
215a7787b78STang Yuantian 		temp_sdram_cfg = ddr_in32(&ddr->sdram_cfg_2);
216a7787b78STang Yuantian 		temp_sdram_cfg |= SDRAM_CFG2_FRC_SR;
217a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2, temp_sdram_cfg);
218a7787b78STang Yuantian 		/* do board specific memory setup */
219a7787b78STang Yuantian 		board_mem_sleep_setup();
220a7787b78STang Yuantian 
221a7787b78STang Yuantian 		temp_sdram_cfg = (ddr_in32(&ddr->sdram_cfg) | SDRAM_CFG_BI);
222a7787b78STang Yuantian 	} else
223a7787b78STang Yuantian #endif
22434e026f9SYork Sun 		temp_sdram_cfg = ddr_in32(&ddr->sdram_cfg) & ~SDRAM_CFG_BI;
225a7787b78STang Yuantian 	/* Let the controller go */
22634e026f9SYork Sun 	ddr_out32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_MEM_EN);
2278340e7acSYork Sun 	mb();
2288340e7acSYork Sun 	isb();
22934e026f9SYork Sun 
23034e026f9SYork Sun 	total_gb_size_per_controller = 0;
23134e026f9SYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
23234e026f9SYork Sun 		if (!(regs->cs[i].config & 0x80000000))
23334e026f9SYork Sun 			continue;
23434e026f9SYork Sun 		total_gb_size_per_controller += 1 << (
23534e026f9SYork Sun 			((regs->cs[i].config >> 14) & 0x3) + 2 +
23634e026f9SYork Sun 			((regs->cs[i].config >> 8) & 0x7) + 12 +
23734e026f9SYork Sun 			((regs->cs[i].config >> 4) & 0x3) + 0 +
23834e026f9SYork Sun 			((regs->cs[i].config >> 0) & 0x7) + 8 +
23934e026f9SYork Sun 			3 - ((regs->ddr_sdram_cfg >> 19) & 0x3) -
24034e026f9SYork Sun 			26);			/* minus 26 (count of 64M) */
24134e026f9SYork Sun 	}
24234e026f9SYork Sun 	if (fsl_ddr_get_intl3r() & 0x80000000)	/* 3-way interleaving */
24334e026f9SYork Sun 		total_gb_size_per_controller *= 3;
24434e026f9SYork Sun 	else if (regs->cs[0].config & 0x20000000) /* 2-way interleaving */
24534e026f9SYork Sun 		total_gb_size_per_controller <<= 1;
24634e026f9SYork Sun 	/*
24734e026f9SYork Sun 	 * total memory / bus width = transactions needed
24834e026f9SYork Sun 	 * transactions needed / data rate = seconds
24934e026f9SYork Sun 	 * to add plenty of buffer, double the time
25034e026f9SYork Sun 	 * For example, 2GB on 666MT/s 64-bit bus takes about 402ms
25134e026f9SYork Sun 	 * Let's wait for 800ms
25234e026f9SYork Sun 	 */
253f80d6472SYork Sun 	bus_width = 3 - ((ddr_in32(&ddr->sdram_cfg) & SDRAM_CFG_DBW_MASK)
25434e026f9SYork Sun 			>> SDRAM_CFG_DBW_SHIFT);
25534e026f9SYork Sun 	timeout = ((total_gb_size_per_controller << (6 - bus_width)) * 100 /
25634e026f9SYork Sun 		(get_ddr_freq(0) >> 20)) << 2;
25734e026f9SYork Sun 	total_gb_size_per_controller >>= 4;	/* shift down to gb size */
25834e026f9SYork Sun 	debug("total %d GB\n", total_gb_size_per_controller);
25934e026f9SYork Sun 	debug("Need to wait up to %d * 10ms\n", timeout);
26034e026f9SYork Sun 
26134e026f9SYork Sun 	/* Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done.  */
26234e026f9SYork Sun 	while ((ddr_in32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
26334e026f9SYork Sun 		(timeout >= 0)) {
26434e026f9SYork Sun 		udelay(10000);		/* throttle polling rate */
26534e026f9SYork Sun 		timeout--;
26634e026f9SYork Sun 	}
26734e026f9SYork Sun 
26834e026f9SYork Sun 	if (timeout <= 0)
26934e026f9SYork Sun 		printf("Waiting for D_INIT timeout. Memory may not work.\n");
270a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
271a7787b78STang Yuantian 	if (is_warm_boot()) {
272a7787b78STang Yuantian 		/* exit self-refresh */
273a7787b78STang Yuantian 		temp_sdram_cfg = ddr_in32(&ddr->sdram_cfg_2);
274a7787b78STang Yuantian 		temp_sdram_cfg &= ~SDRAM_CFG2_FRC_SR;
275a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2, temp_sdram_cfg);
276a7787b78STang Yuantian 	}
277a7787b78STang Yuantian #endif
27834e026f9SYork Sun }
279