xref: /openbmc/u-boot/drivers/ddr/fsl/fsl_ddr_gen4.c (revision 51370d561842ae7438337e77a93177e13796ac45)
134e026f9SYork Sun /*
29f9f0093SYork Sun  * Copyright 2014-2015 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>
13a46b1852SShengzhou Liu #include <fsl_errata.h>
1434e026f9SYork Sun 
15dd8e740cSShengzhou Liu #if defined(CONFIG_SYS_FSL_ERRATUM_A008511) | \
16dd8e740cSShengzhou Liu 	defined(CONFIG_SYS_FSL_ERRATUM_A009803)
179f9f0093SYork Sun static void set_wait_for_bits_clear(void *ptr, u32 value, u32 bits)
189f9f0093SYork Sun {
199f9f0093SYork Sun 	int timeout = 1000;
209f9f0093SYork Sun 
219f9f0093SYork Sun 	ddr_out32(ptr, value);
229f9f0093SYork Sun 
239f9f0093SYork Sun 	while (ddr_in32(ptr) & bits) {
249f9f0093SYork Sun 		udelay(100);
259f9f0093SYork Sun 		timeout--;
269f9f0093SYork Sun 	}
279f9f0093SYork Sun 	if (timeout <= 0)
28dd8e740cSShengzhou Liu 		puts("Error: wait for clear timeout.\n");
299f9f0093SYork Sun }
30dd8e740cSShengzhou Liu #endif
319f9f0093SYork Sun 
3234e026f9SYork Sun #if (CONFIG_CHIP_SELECTS_PER_CTRL > 4)
3334e026f9SYork Sun #error Invalid setting for CONFIG_CHIP_SELECTS_PER_CTRL
3434e026f9SYork Sun #endif
3534e026f9SYork Sun 
3634e026f9SYork Sun /*
3734e026f9SYork Sun  * regs has the to-be-set values for DDR controller registers
3834e026f9SYork Sun  * ctrl_num is the DDR controller number
3934e026f9SYork Sun  * step: 0 goes through the initialization in one pass
4034e026f9SYork Sun  *       1 sets registers and returns before enabling controller
4134e026f9SYork Sun  *       2 resumes from step 1 and continues to initialize
4234e026f9SYork Sun  * Dividing the initialization to two steps to deassert DDR reset signal
4334e026f9SYork Sun  * to comply with JEDEC specs for RDIMMs.
4434e026f9SYork Sun  */
4534e026f9SYork Sun void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
4634e026f9SYork Sun 			     unsigned int ctrl_num, int step)
4734e026f9SYork Sun {
4834e026f9SYork Sun 	unsigned int i, bus_width;
4934e026f9SYork Sun 	struct ccsr_ddr __iomem *ddr;
505a17b8b5SShengzhou Liu 	u32 temp32;
5134e026f9SYork Sun 	u32 total_gb_size_per_controller;
5234e026f9SYork Sun 	int timeout;
532f0dcf2dSShaohui Xie 
549f9f0093SYork Sun #ifdef CONFIG_SYS_FSL_ERRATUM_A008511
552f0dcf2dSShaohui Xie 	u32 mr6;
567cc07998SYork Sun 	u32 vref_seq1[3] = {0x80, 0x96, 0x16};	/* for range 1 */
577cc07998SYork Sun 	u32 vref_seq2[3] = {0xc0, 0xf0, 0x70};	/* for range 2 */
587cc07998SYork Sun 	u32 *vref_seq = vref_seq1;
599f9f0093SYork Sun #endif
604516ff81SYork Sun #ifdef CONFIG_FSL_DDR_BIST
614516ff81SYork Sun 	u32 mtcr, err_detect, err_sbe;
624516ff81SYork Sun 	u32 cs0_bnds, cs1_bnds, cs2_bnds, cs3_bnds, cs0_config;
634516ff81SYork Sun #endif
644516ff81SYork Sun #ifdef CONFIG_FSL_DDR_BIST
654516ff81SYork Sun 	char buffer[CONFIG_SYS_CBSIZE];
664516ff81SYork Sun #endif
6734e026f9SYork Sun 	switch (ctrl_num) {
6834e026f9SYork Sun 	case 0:
6934e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
7034e026f9SYork Sun 		break;
71*51370d56SYork Sun #if defined(CONFIG_SYS_FSL_DDR2_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 1)
7234e026f9SYork Sun 	case 1:
7334e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR2_ADDR;
7434e026f9SYork Sun 		break;
7534e026f9SYork Sun #endif
76*51370d56SYork Sun #if defined(CONFIG_SYS_FSL_DDR3_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 2)
7734e026f9SYork Sun 	case 2:
7834e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR3_ADDR;
7934e026f9SYork Sun 		break;
8034e026f9SYork Sun #endif
81*51370d56SYork Sun #if defined(CONFIG_SYS_FSL_DDR4_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 3)
8234e026f9SYork Sun 	case 3:
8334e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR4_ADDR;
8434e026f9SYork Sun 		break;
8534e026f9SYork Sun #endif
8634e026f9SYork Sun 	default:
8734e026f9SYork Sun 		printf("%s unexpected ctrl_num = %u\n", __func__, ctrl_num);
8834e026f9SYork Sun 		return;
8934e026f9SYork Sun 	}
9034e026f9SYork Sun 
9134e026f9SYork Sun 	if (step == 2)
9234e026f9SYork Sun 		goto step2;
9334e026f9SYork Sun 
9434e026f9SYork Sun 	if (regs->ddr_eor)
9534e026f9SYork Sun 		ddr_out32(&ddr->eor, regs->ddr_eor);
9634e026f9SYork Sun 
9734e026f9SYork Sun 	ddr_out32(&ddr->sdram_clk_cntl, regs->ddr_sdram_clk_cntl);
9834e026f9SYork Sun 
9934e026f9SYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
10034e026f9SYork Sun 		if (i == 0) {
10134e026f9SYork Sun 			ddr_out32(&ddr->cs0_bnds, regs->cs[i].bnds);
10234e026f9SYork Sun 			ddr_out32(&ddr->cs0_config, regs->cs[i].config);
10334e026f9SYork Sun 			ddr_out32(&ddr->cs0_config_2, regs->cs[i].config_2);
10434e026f9SYork Sun 
10534e026f9SYork Sun 		} else if (i == 1) {
10634e026f9SYork Sun 			ddr_out32(&ddr->cs1_bnds, regs->cs[i].bnds);
10734e026f9SYork Sun 			ddr_out32(&ddr->cs1_config, regs->cs[i].config);
10834e026f9SYork Sun 			ddr_out32(&ddr->cs1_config_2, regs->cs[i].config_2);
10934e026f9SYork Sun 
11034e026f9SYork Sun 		} else if (i == 2) {
11134e026f9SYork Sun 			ddr_out32(&ddr->cs2_bnds, regs->cs[i].bnds);
11234e026f9SYork Sun 			ddr_out32(&ddr->cs2_config, regs->cs[i].config);
11334e026f9SYork Sun 			ddr_out32(&ddr->cs2_config_2, regs->cs[i].config_2);
11434e026f9SYork Sun 
11534e026f9SYork Sun 		} else if (i == 3) {
11634e026f9SYork Sun 			ddr_out32(&ddr->cs3_bnds, regs->cs[i].bnds);
11734e026f9SYork Sun 			ddr_out32(&ddr->cs3_config, regs->cs[i].config);
11834e026f9SYork Sun 			ddr_out32(&ddr->cs3_config_2, regs->cs[i].config_2);
11934e026f9SYork Sun 		}
12034e026f9SYork Sun 	}
12134e026f9SYork Sun 
12234e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_3, regs->timing_cfg_3);
12334e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_0, regs->timing_cfg_0);
12434e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_1, regs->timing_cfg_1);
12534e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_2, regs->timing_cfg_2);
12634e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_4, regs->timing_cfg_4);
12734e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_5, regs->timing_cfg_5);
12834e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_6, regs->timing_cfg_6);
12934e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_7, regs->timing_cfg_7);
13034e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_8, regs->timing_cfg_8);
13134e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_9, regs->timing_cfg_9);
13234e026f9SYork Sun 	ddr_out32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
13334e026f9SYork Sun 	ddr_out32(&ddr->dq_map_0, regs->dq_map_0);
13434e026f9SYork Sun 	ddr_out32(&ddr->dq_map_1, regs->dq_map_1);
13534e026f9SYork Sun 	ddr_out32(&ddr->dq_map_2, regs->dq_map_2);
13634e026f9SYork Sun 	ddr_out32(&ddr->dq_map_3, regs->dq_map_3);
13734e026f9SYork Sun 	ddr_out32(&ddr->sdram_cfg_3, regs->ddr_sdram_cfg_3);
13834e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode, regs->ddr_sdram_mode);
13934e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_2, regs->ddr_sdram_mode_2);
14034e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_3, regs->ddr_sdram_mode_3);
14134e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_4, regs->ddr_sdram_mode_4);
14234e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_5, regs->ddr_sdram_mode_5);
14334e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_6, regs->ddr_sdram_mode_6);
14434e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_7, regs->ddr_sdram_mode_7);
14534e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_8, regs->ddr_sdram_mode_8);
14634e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_9, regs->ddr_sdram_mode_9);
14734e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_10, regs->ddr_sdram_mode_10);
14834e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_11, regs->ddr_sdram_mode_11);
14934e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_12, regs->ddr_sdram_mode_12);
15034e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_13, regs->ddr_sdram_mode_13);
15134e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_14, regs->ddr_sdram_mode_14);
15234e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_15, regs->ddr_sdram_mode_15);
15334e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_16, regs->ddr_sdram_mode_16);
15434e026f9SYork Sun 	ddr_out32(&ddr->sdram_md_cntl, regs->ddr_sdram_md_cntl);
155a994b3deSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009663
156a994b3deSShengzhou Liu 	ddr_out32(&ddr->sdram_interval,
157a994b3deSShengzhou Liu 		  regs->ddr_sdram_interval & ~SDRAM_INTERVAL_BSTOPRE);
158a994b3deSShengzhou Liu #else
15934e026f9SYork Sun 	ddr_out32(&ddr->sdram_interval, regs->ddr_sdram_interval);
160a994b3deSShengzhou Liu #endif
16134e026f9SYork Sun 	ddr_out32(&ddr->sdram_data_init, regs->ddr_data_init);
16234e026f9SYork Sun 	ddr_out32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
16334e026f9SYork Sun #ifndef CONFIG_SYS_FSL_DDR_EMU
16434e026f9SYork Sun 	/*
16534e026f9SYork Sun 	 * Skip these two registers if running on emulator
16634e026f9SYork Sun 	 * because emulator doesn't have skew between bytes.
16734e026f9SYork Sun 	 */
16834e026f9SYork Sun 
16934e026f9SYork Sun 	if (regs->ddr_wrlvl_cntl_2)
17034e026f9SYork Sun 		ddr_out32(&ddr->ddr_wrlvl_cntl_2, regs->ddr_wrlvl_cntl_2);
17134e026f9SYork Sun 	if (regs->ddr_wrlvl_cntl_3)
17234e026f9SYork Sun 		ddr_out32(&ddr->ddr_wrlvl_cntl_3, regs->ddr_wrlvl_cntl_3);
17334e026f9SYork Sun #endif
17434e026f9SYork Sun 
17534e026f9SYork Sun 	ddr_out32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr);
17634e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_1, regs->ddr_sdram_rcw_1);
17734e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_2, regs->ddr_sdram_rcw_2);
17834e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_3, regs->ddr_sdram_rcw_3);
17934e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_4, regs->ddr_sdram_rcw_4);
18034e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_5, regs->ddr_sdram_rcw_5);
18134e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_6, regs->ddr_sdram_rcw_6);
18234e026f9SYork Sun 	ddr_out32(&ddr->ddr_cdr1, regs->ddr_cdr1);
183a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
184a7787b78STang Yuantian 	if (is_warm_boot()) {
185a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2,
186a7787b78STang Yuantian 			  regs->ddr_sdram_cfg_2 & ~SDRAM_CFG2_D_INIT);
187a7787b78STang Yuantian 		ddr_out32(&ddr->init_addr, CONFIG_SYS_SDRAM_BASE);
188a7787b78STang Yuantian 		ddr_out32(&ddr->init_ext_addr, DDR_INIT_ADDR_EXT_UIA);
189a7787b78STang Yuantian 
190a7787b78STang Yuantian 		/* DRAM VRef will not be trained */
191a7787b78STang Yuantian 		ddr_out32(&ddr->ddr_cdr2,
192a7787b78STang Yuantian 			  regs->ddr_cdr2 & ~DDR_CDR2_VREF_TRAIN_EN);
193a7787b78STang Yuantian 	} else
194a7787b78STang Yuantian #endif
195a7787b78STang Yuantian 	{
196a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
197a7787b78STang Yuantian 		ddr_out32(&ddr->init_addr, regs->ddr_init_addr);
198a7787b78STang Yuantian 		ddr_out32(&ddr->init_ext_addr, regs->ddr_init_ext_addr);
19934e026f9SYork Sun 		ddr_out32(&ddr->ddr_cdr2, regs->ddr_cdr2);
200a7787b78STang Yuantian 	}
201dd8e740cSShengzhou Liu 
202dd8e740cSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009803
203dd8e740cSShengzhou Liu 	/* part 1 of 2 */
204d3674046SShengzhou Liu 	if (regs->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN) {
205dd8e740cSShengzhou Liu 		if (regs->ddr_sdram_cfg & SDRAM_CFG_RD_EN) { /* for RDIMM */
206dd8e740cSShengzhou Liu 			ddr_out32(&ddr->ddr_sdram_rcw_2,
207dd8e740cSShengzhou Liu 				  regs->ddr_sdram_rcw_2 & ~0x0f000000);
208dd8e740cSShengzhou Liu 		}
209d3674046SShengzhou Liu 		ddr_out32(&ddr->err_disable, regs->err_disable |
210d3674046SShengzhou Liu 			  DDR_ERR_DISABLE_APED);
211d3674046SShengzhou Liu 	}
212dd8e740cSShengzhou Liu #else
21334e026f9SYork Sun 	ddr_out32(&ddr->err_disable, regs->err_disable);
214dd8e740cSShengzhou Liu #endif
21534e026f9SYork Sun 	ddr_out32(&ddr->err_int_en, regs->err_int_en);
216b406731aSYork Sun 	for (i = 0; i < 64; i++) {
21734e026f9SYork Sun 		if (regs->debug[i]) {
21834e026f9SYork Sun 			debug("Write to debug_%d as %08x\n",
21934e026f9SYork Sun 			      i+1, regs->debug[i]);
22034e026f9SYork Sun 			ddr_out32(&ddr->debug[i], regs->debug[i]);
22134e026f9SYork Sun 		}
22234e026f9SYork Sun 	}
22334e026f9SYork Sun 
2249f9f0093SYork Sun #ifdef CONFIG_SYS_FSL_ERRATUM_A008511
2259f9f0093SYork Sun 	/* Part 1 of 2 */
2269f9f0093SYork Sun 	if (fsl_ddr_get_version(ctrl_num) == 0x50200) {
2279f9f0093SYork Sun 		/* Disable DRAM VRef training */
2289f9f0093SYork Sun 		ddr_out32(&ddr->ddr_cdr2,
2299f9f0093SYork Sun 			  regs->ddr_cdr2 & ~DDR_CDR2_VREF_TRAIN_EN);
2304a68489eSShengzhou Liu 		/* disable transmit bit deskew */
2314a68489eSShengzhou Liu 		temp32 = ddr_in32(&ddr->debug[28]);
2324a68489eSShengzhou Liu 		temp32 |= DDR_TX_BD_DIS;
2334a68489eSShengzhou Liu 		ddr_out32(&ddr->debug[28], temp32);
2344baa38c5SYork Sun 		ddr_out32(&ddr->debug[25], 0x9000);
2354baa38c5SYork Sun 	} else if (fsl_ddr_get_version(ctrl_num) == 0x50201) {
2364baa38c5SYork Sun 		/* Output enable forced off */
2374baa38c5SYork Sun 		ddr_out32(&ddr->debug[37], 1 << 31);
2384baa38c5SYork Sun 		/* Enable Vref training */
2394baa38c5SYork Sun 		ddr_out32(&ddr->ddr_cdr2,
2404baa38c5SYork Sun 			  regs->ddr_cdr2 | DDR_CDR2_VREF_TRAIN_EN);
2414baa38c5SYork Sun 	} else {
2424baa38c5SYork Sun 		debug("Erratum A008511 doesn't apply.\n");
2434baa38c5SYork Sun 	}
2444baa38c5SYork Sun #endif
2454baa38c5SYork Sun 
2464baa38c5SYork Sun #if defined(CONFIG_SYS_FSL_ERRATUM_A009803) || \
2474baa38c5SYork Sun 	defined(CONFIG_SYS_FSL_ERRATUM_A008511)
2489f9f0093SYork Sun 	/* Disable D_INIT */
2499f9f0093SYork Sun 	ddr_out32(&ddr->sdram_cfg_2,
2509f9f0093SYork Sun 		  regs->ddr_sdram_cfg_2 & ~SDRAM_CFG2_D_INIT);
2519f9f0093SYork Sun #endif
2520d3972cfSShengzhou Liu 
2535fc62fe5SShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009801
2545fc62fe5SShengzhou Liu 	temp32 = ddr_in32(&ddr->debug[25]);
2555fc62fe5SShengzhou Liu 	temp32 &= ~DDR_CAS_TO_PRE_SUB_MASK;
2565fc62fe5SShengzhou Liu 	temp32 |= 9 << DDR_CAS_TO_PRE_SUB_SHIFT;
2575fc62fe5SShengzhou Liu 	ddr_out32(&ddr->debug[25], temp32);
2585fc62fe5SShengzhou Liu #endif
2595fc62fe5SShengzhou Liu 
260019a147bSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A010165
2615a17b8b5SShengzhou Liu 	temp32 = get_ddr_freq(ctrl_num) / 1000000;
2625a17b8b5SShengzhou Liu 	if ((temp32 > 1900) && (temp32 < 2300)) {
2635a17b8b5SShengzhou Liu 		temp32 = ddr_in32(&ddr->debug[28]);
2645a17b8b5SShengzhou Liu 		ddr_out32(&ddr->debug[28], temp32 | 0x000a0000);
265019a147bSShengzhou Liu 	}
266019a147bSShengzhou Liu #endif
26734e026f9SYork Sun 	/*
26834e026f9SYork Sun 	 * For RDIMMs, JEDEC spec requires clocks to be stable before reset is
26934e026f9SYork Sun 	 * deasserted. Clocks start when any chip select is enabled and clock
27034e026f9SYork Sun 	 * control register is set. Because all DDR components are connected to
27134e026f9SYork Sun 	 * one reset signal, this needs to be done in two steps. Step 1 is to
27234e026f9SYork Sun 	 * get the clocks started. Step 2 resumes after reset signal is
27334e026f9SYork Sun 	 * deasserted.
27434e026f9SYork Sun 	 */
27534e026f9SYork Sun 	if (step == 1) {
27634e026f9SYork Sun 		udelay(200);
27734e026f9SYork Sun 		return;
27834e026f9SYork Sun 	}
27934e026f9SYork Sun 
28034e026f9SYork Sun step2:
28134e026f9SYork Sun 	/* Set, but do not enable the memory */
2825a17b8b5SShengzhou Liu 	temp32 = regs->ddr_sdram_cfg;
2835a17b8b5SShengzhou Liu 	temp32 &= ~(SDRAM_CFG_MEM_EN);
2845a17b8b5SShengzhou Liu 	ddr_out32(&ddr->sdram_cfg, temp32);
28534e026f9SYork Sun 
28634e026f9SYork Sun 	/*
28734e026f9SYork Sun 	 * 500 painful micro-seconds must elapse between
28834e026f9SYork Sun 	 * the DDR clock setup and the DDR config enable.
28934e026f9SYork Sun 	 * DDR2 need 200 us, and DDR3 need 500 us from spec,
29034e026f9SYork Sun 	 * we choose the max, that is 500 us for all of case.
29134e026f9SYork Sun 	 */
29234e026f9SYork Sun 	udelay(500);
2938340e7acSYork Sun 	mb();
2948340e7acSYork Sun 	isb();
29534e026f9SYork Sun 
296a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
297a7787b78STang Yuantian 	if (is_warm_boot()) {
298a7787b78STang Yuantian 		/* enter self-refresh */
2995a17b8b5SShengzhou Liu 		temp32 = ddr_in32(&ddr->sdram_cfg_2);
3005a17b8b5SShengzhou Liu 		temp32 |= SDRAM_CFG2_FRC_SR;
3015a17b8b5SShengzhou Liu 		ddr_out32(&ddr->sdram_cfg_2, temp32);
302a7787b78STang Yuantian 		/* do board specific memory setup */
303a7787b78STang Yuantian 		board_mem_sleep_setup();
304a7787b78STang Yuantian 
3055a17b8b5SShengzhou Liu 		temp32 = (ddr_in32(&ddr->sdram_cfg) | SDRAM_CFG_BI);
306a7787b78STang Yuantian 	} else
307a7787b78STang Yuantian #endif
3085a17b8b5SShengzhou Liu 		temp32 = ddr_in32(&ddr->sdram_cfg) & ~SDRAM_CFG_BI;
309a7787b78STang Yuantian 	/* Let the controller go */
3105a17b8b5SShengzhou Liu 	ddr_out32(&ddr->sdram_cfg, temp32 | SDRAM_CFG_MEM_EN);
3118340e7acSYork Sun 	mb();
3128340e7acSYork Sun 	isb();
31334e026f9SYork Sun 
314dd8e740cSShengzhou Liu #if defined(CONFIG_SYS_FSL_ERRATUM_A008511) || \
315dd8e740cSShengzhou Liu 	defined(CONFIG_SYS_FSL_ERRATUM_A009803)
3169f9f0093SYork Sun 	/* Part 2 of 2 */
3177cc07998SYork Sun 	timeout = 40;
3184baa38c5SYork Sun 	/* Wait for idle. D_INIT needs to be cleared earlier, or timeout */
3199f9f0093SYork Sun 	while (!(ddr_in32(&ddr->debug[1]) & 0x2) &&
3209f9f0093SYork Sun 	       (timeout > 0)) {
3217cc07998SYork Sun 		udelay(1000);
3229f9f0093SYork Sun 		timeout--;
3239f9f0093SYork Sun 	}
3249f9f0093SYork Sun 	if (timeout <= 0) {
3259f9f0093SYork Sun 		printf("Controler %d timeout, debug_2 = %x\n",
3269f9f0093SYork Sun 		       ctrl_num, ddr_in32(&ddr->debug[1]));
3279f9f0093SYork Sun 	}
3287cc07998SYork Sun 
329dd8e740cSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A008511
3304baa38c5SYork Sun 	/* This erraum only applies to verion 5.2.0 */
3314baa38c5SYork Sun 	if (fsl_ddr_get_version(ctrl_num) == 0x50200) {
3327cc07998SYork Sun 		/* The vref setting sequence is different for range 2 */
3337cc07998SYork Sun 		if (regs->ddr_cdr2 & DDR_CDR2_VREF_RANGE_2)
3347cc07998SYork Sun 			vref_seq = vref_seq2;
3357cc07998SYork Sun 
3369f9f0093SYork Sun 		/* Set VREF */
3379f9f0093SYork Sun 		for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
3389f9f0093SYork Sun 			if (!(regs->cs[i].config & SDRAM_CS_CONFIG_EN))
3399f9f0093SYork Sun 				continue;
3409f9f0093SYork Sun 
3419f9f0093SYork Sun 			mr6 = (regs->ddr_sdram_mode_10 >> 16)		|
3429f9f0093SYork Sun 				 MD_CNTL_MD_EN				|
3439f9f0093SYork Sun 				 MD_CNTL_CS_SEL(i)			|
3449f9f0093SYork Sun 				 MD_CNTL_MD_SEL(6)			|
3459f9f0093SYork Sun 				 0x00200000;
3467cc07998SYork Sun 			temp32 = mr6 | vref_seq[0];
3479f9f0093SYork Sun 			set_wait_for_bits_clear(&ddr->sdram_md_cntl,
3489f9f0093SYork Sun 						temp32, MD_CNTL_MD_EN);
3499f9f0093SYork Sun 			udelay(1);
3509f9f0093SYork Sun 			debug("MR6 = 0x%08x\n", temp32);
3517cc07998SYork Sun 			temp32 = mr6 | vref_seq[1];
3529f9f0093SYork Sun 			set_wait_for_bits_clear(&ddr->sdram_md_cntl,
3539f9f0093SYork Sun 						temp32, MD_CNTL_MD_EN);
3549f9f0093SYork Sun 			udelay(1);
3559f9f0093SYork Sun 			debug("MR6 = 0x%08x\n", temp32);
3567cc07998SYork Sun 			temp32 = mr6 | vref_seq[2];
3579f9f0093SYork Sun 			set_wait_for_bits_clear(&ddr->sdram_md_cntl,
3589f9f0093SYork Sun 						temp32, MD_CNTL_MD_EN);
3599f9f0093SYork Sun 			udelay(1);
3609f9f0093SYork Sun 			debug("MR6 = 0x%08x\n", temp32);
3619f9f0093SYork Sun 		}
3629f9f0093SYork Sun 		ddr_out32(&ddr->sdram_md_cntl, 0);
3634a68489eSShengzhou Liu 		temp32 = ddr_in32(&ddr->debug[28]);
3644a68489eSShengzhou Liu 		temp32 &= ~DDR_TX_BD_DIS; /* Enable deskew */
3654a68489eSShengzhou Liu 		ddr_out32(&ddr->debug[28], temp32);
3669f9f0093SYork Sun 		ddr_out32(&ddr->debug[1], 0x400);	/* restart deskew */
3679f9f0093SYork Sun 		/* wait for idle */
3687cc07998SYork Sun 		timeout = 40;
3699f9f0093SYork Sun 		while (!(ddr_in32(&ddr->debug[1]) & 0x2) &&
3709f9f0093SYork Sun 		       (timeout > 0)) {
3717cc07998SYork Sun 			udelay(1000);
3729f9f0093SYork Sun 			timeout--;
3739f9f0093SYork Sun 		}
3749f9f0093SYork Sun 		if (timeout <= 0) {
3759f9f0093SYork Sun 			printf("Controler %d timeout, debug_2 = %x\n",
3769f9f0093SYork Sun 			       ctrl_num, ddr_in32(&ddr->debug[1]));
3779f9f0093SYork Sun 		}
3784baa38c5SYork Sun 	}
3799f9f0093SYork Sun #endif /* CONFIG_SYS_FSL_ERRATUM_A008511 */
3809f9f0093SYork Sun 
381dd8e740cSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009803
382d3674046SShengzhou Liu 	if (regs->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN) {
383dd8e740cSShengzhou Liu 		/* if it's RDIMM */
384dd8e740cSShengzhou Liu 		if (regs->ddr_sdram_cfg & SDRAM_CFG_RD_EN) {
385dd8e740cSShengzhou Liu 			for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
386dd8e740cSShengzhou Liu 				if (!(regs->cs[i].config & SDRAM_CS_CONFIG_EN))
387dd8e740cSShengzhou Liu 					continue;
388dd8e740cSShengzhou Liu 				set_wait_for_bits_clear(&ddr->sdram_md_cntl,
389dd8e740cSShengzhou Liu 							MD_CNTL_MD_EN |
390dd8e740cSShengzhou Liu 							MD_CNTL_CS_SEL(i) |
391dd8e740cSShengzhou Liu 							0x070000ed,
392dd8e740cSShengzhou Liu 							MD_CNTL_MD_EN);
393dd8e740cSShengzhou Liu 				udelay(1);
394dd8e740cSShengzhou Liu 			}
395dd8e740cSShengzhou Liu 		}
396dd8e740cSShengzhou Liu 
397dd8e740cSShengzhou Liu 		ddr_out32(&ddr->err_disable,
398dd8e740cSShengzhou Liu 			  regs->err_disable & ~DDR_ERR_DISABLE_APED);
399d3674046SShengzhou Liu 	}
400dd8e740cSShengzhou Liu #endif
4014baa38c5SYork Sun 	/* Restore D_INIT */
4024baa38c5SYork Sun 	ddr_out32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
403dd8e740cSShengzhou Liu #endif
404dd8e740cSShengzhou Liu 
40534e026f9SYork Sun 	total_gb_size_per_controller = 0;
40634e026f9SYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
40734e026f9SYork Sun 		if (!(regs->cs[i].config & 0x80000000))
40834e026f9SYork Sun 			continue;
40934e026f9SYork Sun 		total_gb_size_per_controller += 1 << (
41034e026f9SYork Sun 			((regs->cs[i].config >> 14) & 0x3) + 2 +
41134e026f9SYork Sun 			((regs->cs[i].config >> 8) & 0x7) + 12 +
41234e026f9SYork Sun 			((regs->cs[i].config >> 4) & 0x3) + 0 +
41334e026f9SYork Sun 			((regs->cs[i].config >> 0) & 0x7) + 8 +
41434e026f9SYork Sun 			3 - ((regs->ddr_sdram_cfg >> 19) & 0x3) -
41534e026f9SYork Sun 			26);			/* minus 26 (count of 64M) */
41634e026f9SYork Sun 	}
41734e026f9SYork Sun 	if (fsl_ddr_get_intl3r() & 0x80000000)	/* 3-way interleaving */
41834e026f9SYork Sun 		total_gb_size_per_controller *= 3;
41934e026f9SYork Sun 	else if (regs->cs[0].config & 0x20000000) /* 2-way interleaving */
42034e026f9SYork Sun 		total_gb_size_per_controller <<= 1;
42134e026f9SYork Sun 	/*
42234e026f9SYork Sun 	 * total memory / bus width = transactions needed
42334e026f9SYork Sun 	 * transactions needed / data rate = seconds
42434e026f9SYork Sun 	 * to add plenty of buffer, double the time
42534e026f9SYork Sun 	 * For example, 2GB on 666MT/s 64-bit bus takes about 402ms
42634e026f9SYork Sun 	 * Let's wait for 800ms
42734e026f9SYork Sun 	 */
428f80d6472SYork Sun 	bus_width = 3 - ((ddr_in32(&ddr->sdram_cfg) & SDRAM_CFG_DBW_MASK)
42934e026f9SYork Sun 			>> SDRAM_CFG_DBW_SHIFT);
43034e026f9SYork Sun 	timeout = ((total_gb_size_per_controller << (6 - bus_width)) * 100 /
43103e664d8SYork Sun 		(get_ddr_freq(ctrl_num) >> 20)) << 2;
43234e026f9SYork Sun 	total_gb_size_per_controller >>= 4;	/* shift down to gb size */
43334e026f9SYork Sun 	debug("total %d GB\n", total_gb_size_per_controller);
43434e026f9SYork Sun 	debug("Need to wait up to %d * 10ms\n", timeout);
43534e026f9SYork Sun 
43634e026f9SYork Sun 	/* Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done.  */
43734e026f9SYork Sun 	while ((ddr_in32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
43834e026f9SYork Sun 		(timeout >= 0)) {
43934e026f9SYork Sun 		udelay(10000);		/* throttle polling rate */
44034e026f9SYork Sun 		timeout--;
44134e026f9SYork Sun 	}
44234e026f9SYork Sun 
44334e026f9SYork Sun 	if (timeout <= 0)
44434e026f9SYork Sun 		printf("Waiting for D_INIT timeout. Memory may not work.\n");
445a994b3deSShengzhou Liu 
446a994b3deSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009663
447a994b3deSShengzhou Liu 	ddr_out32(&ddr->sdram_interval, regs->ddr_sdram_interval);
448a994b3deSShengzhou Liu #endif
449a994b3deSShengzhou Liu 
450a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
451a7787b78STang Yuantian 	if (is_warm_boot()) {
452a7787b78STang Yuantian 		/* exit self-refresh */
4535a17b8b5SShengzhou Liu 		temp32 = ddr_in32(&ddr->sdram_cfg_2);
4545a17b8b5SShengzhou Liu 		temp32 &= ~SDRAM_CFG2_FRC_SR;
4555a17b8b5SShengzhou Liu 		ddr_out32(&ddr->sdram_cfg_2, temp32);
456a7787b78STang Yuantian 	}
457a7787b78STang Yuantian #endif
4584516ff81SYork Sun 
4594516ff81SYork Sun #ifdef CONFIG_FSL_DDR_BIST
4604516ff81SYork Sun #define BIST_PATTERN1	0xFFFFFFFF
4614516ff81SYork Sun #define BIST_PATTERN2	0x0
4624516ff81SYork Sun #define BIST_CR		0x80010000
4634516ff81SYork Sun #define BIST_CR_EN	0x80000000
4644516ff81SYork Sun #define BIST_CR_STAT	0x00000001
4654516ff81SYork Sun #define CTLR_INTLV_MASK	0x20000000
4664516ff81SYork Sun 	/* Perform build-in test on memory. Three-way interleaving is not yet
4674516ff81SYork Sun 	 * supported by this code. */
4684516ff81SYork Sun 	if (getenv_f("ddr_bist", buffer, CONFIG_SYS_CBSIZE) >= 0) {
4694516ff81SYork Sun 		puts("Running BIST test. This will take a while...");
4704516ff81SYork Sun 		cs0_config = ddr_in32(&ddr->cs0_config);
471da305b9fSYork Sun 		cs0_bnds = ddr_in32(&ddr->cs0_bnds);
472da305b9fSYork Sun 		cs1_bnds = ddr_in32(&ddr->cs1_bnds);
473da305b9fSYork Sun 		cs2_bnds = ddr_in32(&ddr->cs2_bnds);
474da305b9fSYork Sun 		cs3_bnds = ddr_in32(&ddr->cs3_bnds);
4754516ff81SYork Sun 		if (cs0_config & CTLR_INTLV_MASK) {
4764516ff81SYork Sun 			/* set bnds to non-interleaving */
477da305b9fSYork Sun 			ddr_out32(&ddr->cs0_bnds, (cs0_bnds & 0xfffefffe) >> 1);
478da305b9fSYork Sun 			ddr_out32(&ddr->cs1_bnds, (cs1_bnds & 0xfffefffe) >> 1);
479da305b9fSYork Sun 			ddr_out32(&ddr->cs2_bnds, (cs2_bnds & 0xfffefffe) >> 1);
480da305b9fSYork Sun 			ddr_out32(&ddr->cs3_bnds, (cs3_bnds & 0xfffefffe) >> 1);
4814516ff81SYork Sun 		}
4824516ff81SYork Sun 		ddr_out32(&ddr->mtp1, BIST_PATTERN1);
4834516ff81SYork Sun 		ddr_out32(&ddr->mtp2, BIST_PATTERN1);
4844516ff81SYork Sun 		ddr_out32(&ddr->mtp3, BIST_PATTERN2);
4854516ff81SYork Sun 		ddr_out32(&ddr->mtp4, BIST_PATTERN2);
4864516ff81SYork Sun 		ddr_out32(&ddr->mtp5, BIST_PATTERN1);
4874516ff81SYork Sun 		ddr_out32(&ddr->mtp6, BIST_PATTERN1);
4884516ff81SYork Sun 		ddr_out32(&ddr->mtp7, BIST_PATTERN2);
4894516ff81SYork Sun 		ddr_out32(&ddr->mtp8, BIST_PATTERN2);
4904516ff81SYork Sun 		ddr_out32(&ddr->mtp9, BIST_PATTERN1);
4914516ff81SYork Sun 		ddr_out32(&ddr->mtp10, BIST_PATTERN2);
4924516ff81SYork Sun 		mtcr = BIST_CR;
4934516ff81SYork Sun 		ddr_out32(&ddr->mtcr, mtcr);
4944516ff81SYork Sun 		timeout = 100;
4954516ff81SYork Sun 		while (timeout > 0 && (mtcr & BIST_CR_EN)) {
4964516ff81SYork Sun 			mdelay(1000);
4974516ff81SYork Sun 			timeout--;
4984516ff81SYork Sun 			mtcr = ddr_in32(&ddr->mtcr);
4994516ff81SYork Sun 		}
5004516ff81SYork Sun 		if (timeout <= 0)
5014516ff81SYork Sun 			puts("Timeout\n");
5024516ff81SYork Sun 		else
5034516ff81SYork Sun 			puts("Done\n");
5044516ff81SYork Sun 		err_detect = ddr_in32(&ddr->err_detect);
5054516ff81SYork Sun 		err_sbe = ddr_in32(&ddr->err_sbe);
5064516ff81SYork Sun 		if (mtcr & BIST_CR_STAT) {
5074516ff81SYork Sun 			printf("BIST test failed on controller %d.\n",
5084516ff81SYork Sun 			       ctrl_num);
5094516ff81SYork Sun 		}
5104516ff81SYork Sun 		if (err_detect || (err_sbe & 0xffff)) {
5114516ff81SYork Sun 			printf("ECC error detected on controller %d.\n",
5124516ff81SYork Sun 			       ctrl_num);
5134516ff81SYork Sun 		}
5144516ff81SYork Sun 
5154516ff81SYork Sun 		if (cs0_config & CTLR_INTLV_MASK) {
5164516ff81SYork Sun 			/* restore bnds registers */
517da305b9fSYork Sun 			ddr_out32(&ddr->cs0_bnds, cs0_bnds);
518da305b9fSYork Sun 			ddr_out32(&ddr->cs1_bnds, cs1_bnds);
519da305b9fSYork Sun 			ddr_out32(&ddr->cs2_bnds, cs2_bnds);
520da305b9fSYork Sun 			ddr_out32(&ddr->cs3_bnds, cs3_bnds);
5214516ff81SYork Sun 		}
5224516ff81SYork Sun 	}
5234516ff81SYork Sun #endif
52434e026f9SYork Sun }
525