xref: /openbmc/u-boot/drivers/ddr/fsl/fsl_ddr_gen4.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
234e026f9SYork Sun /*
39f9f0093SYork Sun  * Copyright 2014-2015 Freescale Semiconductor, Inc.
434e026f9SYork Sun  */
534e026f9SYork Sun 
634e026f9SYork Sun #include <common.h>
734e026f9SYork Sun #include <asm/io.h>
834e026f9SYork Sun #include <fsl_ddr_sdram.h>
934e026f9SYork Sun #include <asm/processor.h>
108340e7acSYork Sun #include <fsl_immap.h>
1134e026f9SYork Sun #include <fsl_ddr.h>
12a46b1852SShengzhou Liu #include <fsl_errata.h>
13457e51cfSSimon Glass #if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3) || \
14457e51cfSSimon Glass 	defined(CONFIG_ARM)
156e2941d7SSimon Glass #include <asm/arch/clock.h>
166e2941d7SSimon Glass #endif
1734e026f9SYork Sun 
18944537c5SYork Sun #define CTLR_INTLV_MASK	0x20000000
19944537c5SYork Sun 
20dd8e740cSShengzhou Liu #if defined(CONFIG_SYS_FSL_ERRATUM_A008511) | \
21dd8e740cSShengzhou Liu 	defined(CONFIG_SYS_FSL_ERRATUM_A009803)
set_wait_for_bits_clear(void * ptr,u32 value,u32 bits)229f9f0093SYork Sun static void set_wait_for_bits_clear(void *ptr, u32 value, u32 bits)
239f9f0093SYork Sun {
249f9f0093SYork Sun 	int timeout = 1000;
259f9f0093SYork Sun 
269f9f0093SYork Sun 	ddr_out32(ptr, value);
279f9f0093SYork Sun 
289f9f0093SYork Sun 	while (ddr_in32(ptr) & bits) {
299f9f0093SYork Sun 		udelay(100);
309f9f0093SYork Sun 		timeout--;
319f9f0093SYork Sun 	}
329f9f0093SYork Sun 	if (timeout <= 0)
33dd8e740cSShengzhou Liu 		puts("Error: wait for clear timeout.\n");
349f9f0093SYork Sun }
35dd8e740cSShengzhou Liu #endif
369f9f0093SYork Sun 
3734e026f9SYork Sun #if (CONFIG_CHIP_SELECTS_PER_CTRL > 4)
3834e026f9SYork Sun #error Invalid setting for CONFIG_CHIP_SELECTS_PER_CTRL
3934e026f9SYork Sun #endif
4034e026f9SYork Sun 
4134e026f9SYork Sun /*
4234e026f9SYork Sun  * regs has the to-be-set values for DDR controller registers
4334e026f9SYork Sun  * ctrl_num is the DDR controller number
4434e026f9SYork Sun  * step: 0 goes through the initialization in one pass
4534e026f9SYork Sun  *       1 sets registers and returns before enabling controller
4634e026f9SYork Sun  *       2 resumes from step 1 and continues to initialize
4734e026f9SYork Sun  * Dividing the initialization to two steps to deassert DDR reset signal
4834e026f9SYork Sun  * to comply with JEDEC specs for RDIMMs.
4934e026f9SYork Sun  */
fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t * regs,unsigned int ctrl_num,int step)5034e026f9SYork Sun void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
5134e026f9SYork Sun 			     unsigned int ctrl_num, int step)
5234e026f9SYork Sun {
5334e026f9SYork Sun 	unsigned int i, bus_width;
5434e026f9SYork Sun 	struct ccsr_ddr __iomem *ddr;
555a17b8b5SShengzhou Liu 	u32 temp32;
5634e026f9SYork Sun 	u32 total_gb_size_per_controller;
5734e026f9SYork Sun 	int timeout;
58944537c5SYork Sun 	int mod_bnds = 0;
592f0dcf2dSShaohui Xie 
609f9f0093SYork Sun #ifdef CONFIG_SYS_FSL_ERRATUM_A008511
612f0dcf2dSShaohui Xie 	u32 mr6;
627cc07998SYork Sun 	u32 vref_seq1[3] = {0x80, 0x96, 0x16};	/* for range 1 */
637cc07998SYork Sun 	u32 vref_seq2[3] = {0xc0, 0xf0, 0x70};	/* for range 2 */
647cc07998SYork Sun 	u32 *vref_seq = vref_seq1;
659f9f0093SYork Sun #endif
664516ff81SYork Sun #ifdef CONFIG_FSL_DDR_BIST
674516ff81SYork Sun 	u32 mtcr, err_detect, err_sbe;
684516ff81SYork Sun 	u32 cs0_bnds, cs1_bnds, cs2_bnds, cs3_bnds, cs0_config;
694516ff81SYork Sun #endif
704516ff81SYork Sun #ifdef CONFIG_FSL_DDR_BIST
714516ff81SYork Sun 	char buffer[CONFIG_SYS_CBSIZE];
724516ff81SYork Sun #endif
7334e026f9SYork Sun 	switch (ctrl_num) {
7434e026f9SYork Sun 	case 0:
7534e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
7634e026f9SYork Sun 		break;
7751370d56SYork Sun #if defined(CONFIG_SYS_FSL_DDR2_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 1)
7834e026f9SYork Sun 	case 1:
7934e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR2_ADDR;
8034e026f9SYork Sun 		break;
8134e026f9SYork Sun #endif
8251370d56SYork Sun #if defined(CONFIG_SYS_FSL_DDR3_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 2)
8334e026f9SYork Sun 	case 2:
8434e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR3_ADDR;
8534e026f9SYork Sun 		break;
8634e026f9SYork Sun #endif
8751370d56SYork Sun #if defined(CONFIG_SYS_FSL_DDR4_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 3)
8834e026f9SYork Sun 	case 3:
8934e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR4_ADDR;
9034e026f9SYork Sun 		break;
9134e026f9SYork Sun #endif
9234e026f9SYork Sun 	default:
9334e026f9SYork Sun 		printf("%s unexpected ctrl_num = %u\n", __func__, ctrl_num);
9434e026f9SYork Sun 		return;
9534e026f9SYork Sun 	}
96944537c5SYork Sun 	mod_bnds = regs->cs[0].config & CTLR_INTLV_MASK;
9734e026f9SYork Sun 
9834e026f9SYork Sun 	if (step == 2)
9934e026f9SYork Sun 		goto step2;
10034e026f9SYork Sun 
101554d33f3SRajesh Bhagat 	/* Set cdr1 first in case 0.9v VDD is enabled for some SoCs*/
102554d33f3SRajesh Bhagat 	ddr_out32(&ddr->ddr_cdr1, regs->ddr_cdr1);
103554d33f3SRajesh Bhagat 
10434e026f9SYork Sun 	if (regs->ddr_eor)
10534e026f9SYork Sun 		ddr_out32(&ddr->eor, regs->ddr_eor);
10634e026f9SYork Sun 
10734e026f9SYork Sun 	ddr_out32(&ddr->sdram_clk_cntl, regs->ddr_sdram_clk_cntl);
10834e026f9SYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
10934e026f9SYork Sun 		if (i == 0) {
110944537c5SYork Sun 			if (mod_bnds) {
111944537c5SYork Sun 				debug("modified bnds\n");
112944537c5SYork Sun 				ddr_out32(&ddr->cs0_bnds,
113944537c5SYork Sun 					  (regs->cs[i].bnds & 0xfffefffe) >> 1);
114944537c5SYork Sun 				ddr_out32(&ddr->cs0_config,
115944537c5SYork Sun 					  (regs->cs[i].config &
116944537c5SYork Sun 					   ~CTLR_INTLV_MASK));
117944537c5SYork Sun 			} else {
11834e026f9SYork Sun 				ddr_out32(&ddr->cs0_bnds, regs->cs[i].bnds);
11934e026f9SYork Sun 				ddr_out32(&ddr->cs0_config, regs->cs[i].config);
120944537c5SYork Sun 			}
12134e026f9SYork Sun 			ddr_out32(&ddr->cs0_config_2, regs->cs[i].config_2);
12234e026f9SYork Sun 
12334e026f9SYork Sun 		} else if (i == 1) {
124944537c5SYork Sun 			if (mod_bnds) {
125944537c5SYork Sun 				ddr_out32(&ddr->cs1_bnds,
126944537c5SYork Sun 					  (regs->cs[i].bnds & 0xfffefffe) >> 1);
127944537c5SYork Sun 			} else {
12834e026f9SYork Sun 				ddr_out32(&ddr->cs1_bnds, regs->cs[i].bnds);
129944537c5SYork Sun 			}
13034e026f9SYork Sun 			ddr_out32(&ddr->cs1_config, regs->cs[i].config);
13134e026f9SYork Sun 			ddr_out32(&ddr->cs1_config_2, regs->cs[i].config_2);
13234e026f9SYork Sun 
13334e026f9SYork Sun 		} else if (i == 2) {
134944537c5SYork Sun 			if (mod_bnds) {
135944537c5SYork Sun 				ddr_out32(&ddr->cs2_bnds,
136944537c5SYork Sun 					  (regs->cs[i].bnds & 0xfffefffe) >> 1);
137944537c5SYork Sun 			} else {
13834e026f9SYork Sun 				ddr_out32(&ddr->cs2_bnds, regs->cs[i].bnds);
139944537c5SYork Sun 			}
14034e026f9SYork Sun 			ddr_out32(&ddr->cs2_config, regs->cs[i].config);
14134e026f9SYork Sun 			ddr_out32(&ddr->cs2_config_2, regs->cs[i].config_2);
14234e026f9SYork Sun 
14334e026f9SYork Sun 		} else if (i == 3) {
144944537c5SYork Sun 			if (mod_bnds) {
145944537c5SYork Sun 				ddr_out32(&ddr->cs3_bnds,
146944537c5SYork Sun 					  (regs->cs[i].bnds & 0xfffefffe) >> 1);
147944537c5SYork Sun 			} else {
14834e026f9SYork Sun 				ddr_out32(&ddr->cs3_bnds, regs->cs[i].bnds);
149944537c5SYork Sun 			}
15034e026f9SYork Sun 			ddr_out32(&ddr->cs3_config, regs->cs[i].config);
15134e026f9SYork Sun 			ddr_out32(&ddr->cs3_config_2, regs->cs[i].config_2);
15234e026f9SYork Sun 		}
15334e026f9SYork Sun 	}
15434e026f9SYork Sun 
15534e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_3, regs->timing_cfg_3);
15634e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_0, regs->timing_cfg_0);
15734e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_1, regs->timing_cfg_1);
15834e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_2, regs->timing_cfg_2);
15934e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_4, regs->timing_cfg_4);
16034e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_5, regs->timing_cfg_5);
16134e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_6, regs->timing_cfg_6);
16234e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_7, regs->timing_cfg_7);
16334e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_8, regs->timing_cfg_8);
16434e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_9, regs->timing_cfg_9);
16534e026f9SYork Sun 	ddr_out32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
16634e026f9SYork Sun 	ddr_out32(&ddr->dq_map_0, regs->dq_map_0);
16734e026f9SYork Sun 	ddr_out32(&ddr->dq_map_1, regs->dq_map_1);
16834e026f9SYork Sun 	ddr_out32(&ddr->dq_map_2, regs->dq_map_2);
16934e026f9SYork Sun 	ddr_out32(&ddr->dq_map_3, regs->dq_map_3);
17034e026f9SYork Sun 	ddr_out32(&ddr->sdram_cfg_3, regs->ddr_sdram_cfg_3);
17134e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode, regs->ddr_sdram_mode);
17234e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_2, regs->ddr_sdram_mode_2);
17334e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_3, regs->ddr_sdram_mode_3);
17434e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_4, regs->ddr_sdram_mode_4);
17534e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_5, regs->ddr_sdram_mode_5);
17634e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_6, regs->ddr_sdram_mode_6);
17734e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_7, regs->ddr_sdram_mode_7);
17834e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_8, regs->ddr_sdram_mode_8);
17934e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_9, regs->ddr_sdram_mode_9);
18034e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_10, regs->ddr_sdram_mode_10);
18134e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_11, regs->ddr_sdram_mode_11);
18234e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_12, regs->ddr_sdram_mode_12);
18334e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_13, regs->ddr_sdram_mode_13);
18434e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_14, regs->ddr_sdram_mode_14);
18534e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_15, regs->ddr_sdram_mode_15);
18634e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_16, regs->ddr_sdram_mode_16);
18734e026f9SYork Sun 	ddr_out32(&ddr->sdram_md_cntl, regs->ddr_sdram_md_cntl);
188a994b3deSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009663
189a994b3deSShengzhou Liu 	ddr_out32(&ddr->sdram_interval,
190a994b3deSShengzhou Liu 		  regs->ddr_sdram_interval & ~SDRAM_INTERVAL_BSTOPRE);
191a994b3deSShengzhou Liu #else
19234e026f9SYork Sun 	ddr_out32(&ddr->sdram_interval, regs->ddr_sdram_interval);
193a994b3deSShengzhou Liu #endif
19434e026f9SYork Sun 	ddr_out32(&ddr->sdram_data_init, regs->ddr_data_init);
19534e026f9SYork Sun 	ddr_out32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
19634e026f9SYork Sun #ifndef CONFIG_SYS_FSL_DDR_EMU
19734e026f9SYork Sun 	/*
19834e026f9SYork Sun 	 * Skip these two registers if running on emulator
19934e026f9SYork Sun 	 * because emulator doesn't have skew between bytes.
20034e026f9SYork Sun 	 */
20134e026f9SYork Sun 
20234e026f9SYork Sun 	if (regs->ddr_wrlvl_cntl_2)
20334e026f9SYork Sun 		ddr_out32(&ddr->ddr_wrlvl_cntl_2, regs->ddr_wrlvl_cntl_2);
20434e026f9SYork Sun 	if (regs->ddr_wrlvl_cntl_3)
20534e026f9SYork Sun 		ddr_out32(&ddr->ddr_wrlvl_cntl_3, regs->ddr_wrlvl_cntl_3);
20634e026f9SYork Sun #endif
20734e026f9SYork Sun 
20834e026f9SYork Sun 	ddr_out32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr);
20934e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_1, regs->ddr_sdram_rcw_1);
21034e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_2, regs->ddr_sdram_rcw_2);
21134e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_3, regs->ddr_sdram_rcw_3);
21234e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_4, regs->ddr_sdram_rcw_4);
21334e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_5, regs->ddr_sdram_rcw_5);
21434e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_6, regs->ddr_sdram_rcw_6);
215a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
216a7787b78STang Yuantian 	if (is_warm_boot()) {
217a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2,
218a7787b78STang Yuantian 			  regs->ddr_sdram_cfg_2 & ~SDRAM_CFG2_D_INIT);
219a7787b78STang Yuantian 		ddr_out32(&ddr->init_addr, CONFIG_SYS_SDRAM_BASE);
220a7787b78STang Yuantian 		ddr_out32(&ddr->init_ext_addr, DDR_INIT_ADDR_EXT_UIA);
221a7787b78STang Yuantian 
222a7787b78STang Yuantian 		/* DRAM VRef will not be trained */
223a7787b78STang Yuantian 		ddr_out32(&ddr->ddr_cdr2,
224a7787b78STang Yuantian 			  regs->ddr_cdr2 & ~DDR_CDR2_VREF_TRAIN_EN);
225a7787b78STang Yuantian 	} else
226a7787b78STang Yuantian #endif
227a7787b78STang Yuantian 	{
228a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
229a7787b78STang Yuantian 		ddr_out32(&ddr->init_addr, regs->ddr_init_addr);
230a7787b78STang Yuantian 		ddr_out32(&ddr->init_ext_addr, regs->ddr_init_ext_addr);
23134e026f9SYork Sun 		ddr_out32(&ddr->ddr_cdr2, regs->ddr_cdr2);
232a7787b78STang Yuantian 	}
233dd8e740cSShengzhou Liu 
234dd8e740cSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009803
235dd8e740cSShengzhou Liu 	/* part 1 of 2 */
236d3674046SShengzhou Liu 	if (regs->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN) {
237dd8e740cSShengzhou Liu 		if (regs->ddr_sdram_cfg & SDRAM_CFG_RD_EN) { /* for RDIMM */
238dd8e740cSShengzhou Liu 			ddr_out32(&ddr->ddr_sdram_rcw_2,
239d46ec0bbSYork Sun 				  regs->ddr_sdram_rcw_2 & ~0xf0);
240dd8e740cSShengzhou Liu 		}
241d3674046SShengzhou Liu 		ddr_out32(&ddr->err_disable, regs->err_disable |
242d3674046SShengzhou Liu 			  DDR_ERR_DISABLE_APED);
243d3674046SShengzhou Liu 	}
244dd8e740cSShengzhou Liu #else
24534e026f9SYork Sun 	ddr_out32(&ddr->err_disable, regs->err_disable);
246dd8e740cSShengzhou Liu #endif
24734e026f9SYork Sun 	ddr_out32(&ddr->err_int_en, regs->err_int_en);
248b406731aSYork Sun 	for (i = 0; i < 64; i++) {
24934e026f9SYork Sun 		if (regs->debug[i]) {
25034e026f9SYork Sun 			debug("Write to debug_%d as %08x\n",
25134e026f9SYork Sun 			      i+1, regs->debug[i]);
25234e026f9SYork Sun 			ddr_out32(&ddr->debug[i], regs->debug[i]);
25334e026f9SYork Sun 		}
25434e026f9SYork Sun 	}
25534e026f9SYork Sun 
2569f9f0093SYork Sun #ifdef CONFIG_SYS_FSL_ERRATUM_A008511
2579f9f0093SYork Sun 	/* Part 1 of 2 */
2589f9f0093SYork Sun 	if (fsl_ddr_get_version(ctrl_num) == 0x50200) {
2599f9f0093SYork Sun 		/* Disable DRAM VRef training */
2609f9f0093SYork Sun 		ddr_out32(&ddr->ddr_cdr2,
2619f9f0093SYork Sun 			  regs->ddr_cdr2 & ~DDR_CDR2_VREF_TRAIN_EN);
2624a68489eSShengzhou Liu 		/* disable transmit bit deskew */
2634a68489eSShengzhou Liu 		temp32 = ddr_in32(&ddr->debug[28]);
2644a68489eSShengzhou Liu 		temp32 |= DDR_TX_BD_DIS;
2654a68489eSShengzhou Liu 		ddr_out32(&ddr->debug[28], temp32);
2664baa38c5SYork Sun 		ddr_out32(&ddr->debug[25], 0x9000);
2674baa38c5SYork Sun 	} else if (fsl_ddr_get_version(ctrl_num) == 0x50201) {
2684baa38c5SYork Sun 		/* Output enable forced off */
2694baa38c5SYork Sun 		ddr_out32(&ddr->debug[37], 1 << 31);
2704baa38c5SYork Sun 		/* Enable Vref training */
2714baa38c5SYork Sun 		ddr_out32(&ddr->ddr_cdr2,
2724baa38c5SYork Sun 			  regs->ddr_cdr2 | DDR_CDR2_VREF_TRAIN_EN);
2734baa38c5SYork Sun 	} else {
2744baa38c5SYork Sun 		debug("Erratum A008511 doesn't apply.\n");
2754baa38c5SYork Sun 	}
2764baa38c5SYork Sun #endif
2774baa38c5SYork Sun 
2784baa38c5SYork Sun #if defined(CONFIG_SYS_FSL_ERRATUM_A009803) || \
2794baa38c5SYork Sun 	defined(CONFIG_SYS_FSL_ERRATUM_A008511)
2809f9f0093SYork Sun 	/* Disable D_INIT */
2819f9f0093SYork Sun 	ddr_out32(&ddr->sdram_cfg_2,
2829f9f0093SYork Sun 		  regs->ddr_sdram_cfg_2 & ~SDRAM_CFG2_D_INIT);
2839f9f0093SYork Sun #endif
2840d3972cfSShengzhou Liu 
2855fc62fe5SShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009801
2865fc62fe5SShengzhou Liu 	temp32 = ddr_in32(&ddr->debug[25]);
2875fc62fe5SShengzhou Liu 	temp32 &= ~DDR_CAS_TO_PRE_SUB_MASK;
2885fc62fe5SShengzhou Liu 	temp32 |= 9 << DDR_CAS_TO_PRE_SUB_SHIFT;
2895fc62fe5SShengzhou Liu 	ddr_out32(&ddr->debug[25], temp32);
2905fc62fe5SShengzhou Liu #endif
2915fc62fe5SShengzhou Liu 
292019a147bSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A010165
2935a17b8b5SShengzhou Liu 	temp32 = get_ddr_freq(ctrl_num) / 1000000;
2945a17b8b5SShengzhou Liu 	if ((temp32 > 1900) && (temp32 < 2300)) {
2955a17b8b5SShengzhou Liu 		temp32 = ddr_in32(&ddr->debug[28]);
2965a17b8b5SShengzhou Liu 		ddr_out32(&ddr->debug[28], temp32 | 0x000a0000);
297019a147bSShengzhou Liu 	}
298019a147bSShengzhou Liu #endif
29934e026f9SYork Sun 	/*
30034e026f9SYork Sun 	 * For RDIMMs, JEDEC spec requires clocks to be stable before reset is
30134e026f9SYork Sun 	 * deasserted. Clocks start when any chip select is enabled and clock
30234e026f9SYork Sun 	 * control register is set. Because all DDR components are connected to
30334e026f9SYork Sun 	 * one reset signal, this needs to be done in two steps. Step 1 is to
30434e026f9SYork Sun 	 * get the clocks started. Step 2 resumes after reset signal is
30534e026f9SYork Sun 	 * deasserted.
30634e026f9SYork Sun 	 */
30734e026f9SYork Sun 	if (step == 1) {
30834e026f9SYork Sun 		udelay(200);
30934e026f9SYork Sun 		return;
31034e026f9SYork Sun 	}
31134e026f9SYork Sun 
31234e026f9SYork Sun step2:
31334e026f9SYork Sun 	/* Set, but do not enable the memory */
3145a17b8b5SShengzhou Liu 	temp32 = regs->ddr_sdram_cfg;
3155a17b8b5SShengzhou Liu 	temp32 &= ~(SDRAM_CFG_MEM_EN);
3165a17b8b5SShengzhou Liu 	ddr_out32(&ddr->sdram_cfg, temp32);
31734e026f9SYork Sun 
31834e026f9SYork Sun 	/*
31934e026f9SYork Sun 	 * 500 painful micro-seconds must elapse between
32034e026f9SYork Sun 	 * the DDR clock setup and the DDR config enable.
32134e026f9SYork Sun 	 * DDR2 need 200 us, and DDR3 need 500 us from spec,
32234e026f9SYork Sun 	 * we choose the max, that is 500 us for all of case.
32334e026f9SYork Sun 	 */
32434e026f9SYork Sun 	udelay(500);
3258340e7acSYork Sun 	mb();
3268340e7acSYork Sun 	isb();
32734e026f9SYork Sun 
328a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
329a7787b78STang Yuantian 	if (is_warm_boot()) {
330a7787b78STang Yuantian 		/* enter self-refresh */
3315a17b8b5SShengzhou Liu 		temp32 = ddr_in32(&ddr->sdram_cfg_2);
3325a17b8b5SShengzhou Liu 		temp32 |= SDRAM_CFG2_FRC_SR;
3335a17b8b5SShengzhou Liu 		ddr_out32(&ddr->sdram_cfg_2, temp32);
334a7787b78STang Yuantian 		/* do board specific memory setup */
335a7787b78STang Yuantian 		board_mem_sleep_setup();
336a7787b78STang Yuantian 
3375a17b8b5SShengzhou Liu 		temp32 = (ddr_in32(&ddr->sdram_cfg) | SDRAM_CFG_BI);
338a7787b78STang Yuantian 	} else
339a7787b78STang Yuantian #endif
3405a17b8b5SShengzhou Liu 		temp32 = ddr_in32(&ddr->sdram_cfg) & ~SDRAM_CFG_BI;
341a7787b78STang Yuantian 	/* Let the controller go */
3425a17b8b5SShengzhou Liu 	ddr_out32(&ddr->sdram_cfg, temp32 | SDRAM_CFG_MEM_EN);
3438340e7acSYork Sun 	mb();
3448340e7acSYork Sun 	isb();
34534e026f9SYork Sun 
346dd8e740cSShengzhou Liu #if defined(CONFIG_SYS_FSL_ERRATUM_A008511) || \
347dd8e740cSShengzhou Liu 	defined(CONFIG_SYS_FSL_ERRATUM_A009803)
3489f9f0093SYork Sun 	/* Part 2 of 2 */
3497cc07998SYork Sun 	timeout = 40;
3504baa38c5SYork Sun 	/* Wait for idle. D_INIT needs to be cleared earlier, or timeout */
3519f9f0093SYork Sun 	while (!(ddr_in32(&ddr->debug[1]) & 0x2) &&
3529f9f0093SYork Sun 	       (timeout > 0)) {
3537cc07998SYork Sun 		udelay(1000);
3549f9f0093SYork Sun 		timeout--;
3559f9f0093SYork Sun 	}
3569f9f0093SYork Sun 	if (timeout <= 0) {
3579f9f0093SYork Sun 		printf("Controler %d timeout, debug_2 = %x\n",
3589f9f0093SYork Sun 		       ctrl_num, ddr_in32(&ddr->debug[1]));
3599f9f0093SYork Sun 	}
3607cc07998SYork Sun 
361dd8e740cSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A008511
3624baa38c5SYork Sun 	/* This erraum only applies to verion 5.2.0 */
3634baa38c5SYork Sun 	if (fsl_ddr_get_version(ctrl_num) == 0x50200) {
3647cc07998SYork Sun 		/* The vref setting sequence is different for range 2 */
3657cc07998SYork Sun 		if (regs->ddr_cdr2 & DDR_CDR2_VREF_RANGE_2)
3667cc07998SYork Sun 			vref_seq = vref_seq2;
3677cc07998SYork Sun 
3689f9f0093SYork Sun 		/* Set VREF */
3699f9f0093SYork Sun 		for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
3709f9f0093SYork Sun 			if (!(regs->cs[i].config & SDRAM_CS_CONFIG_EN))
3719f9f0093SYork Sun 				continue;
3729f9f0093SYork Sun 
3739f9f0093SYork Sun 			mr6 = (regs->ddr_sdram_mode_10 >> 16)		|
3749f9f0093SYork Sun 				 MD_CNTL_MD_EN				|
3759f9f0093SYork Sun 				 MD_CNTL_CS_SEL(i)			|
3769f9f0093SYork Sun 				 MD_CNTL_MD_SEL(6)			|
3779f9f0093SYork Sun 				 0x00200000;
3787cc07998SYork Sun 			temp32 = mr6 | vref_seq[0];
3799f9f0093SYork Sun 			set_wait_for_bits_clear(&ddr->sdram_md_cntl,
3809f9f0093SYork Sun 						temp32, MD_CNTL_MD_EN);
3819f9f0093SYork Sun 			udelay(1);
3829f9f0093SYork Sun 			debug("MR6 = 0x%08x\n", temp32);
3837cc07998SYork Sun 			temp32 = mr6 | vref_seq[1];
3849f9f0093SYork Sun 			set_wait_for_bits_clear(&ddr->sdram_md_cntl,
3859f9f0093SYork Sun 						temp32, MD_CNTL_MD_EN);
3869f9f0093SYork Sun 			udelay(1);
3879f9f0093SYork Sun 			debug("MR6 = 0x%08x\n", temp32);
3887cc07998SYork Sun 			temp32 = mr6 | vref_seq[2];
3899f9f0093SYork Sun 			set_wait_for_bits_clear(&ddr->sdram_md_cntl,
3909f9f0093SYork Sun 						temp32, MD_CNTL_MD_EN);
3919f9f0093SYork Sun 			udelay(1);
3929f9f0093SYork Sun 			debug("MR6 = 0x%08x\n", temp32);
3939f9f0093SYork Sun 		}
3949f9f0093SYork Sun 		ddr_out32(&ddr->sdram_md_cntl, 0);
3954a68489eSShengzhou Liu 		temp32 = ddr_in32(&ddr->debug[28]);
3964a68489eSShengzhou Liu 		temp32 &= ~DDR_TX_BD_DIS; /* Enable deskew */
3974a68489eSShengzhou Liu 		ddr_out32(&ddr->debug[28], temp32);
3989f9f0093SYork Sun 		ddr_out32(&ddr->debug[1], 0x400);	/* restart deskew */
3999f9f0093SYork Sun 		/* wait for idle */
4007cc07998SYork Sun 		timeout = 40;
4019f9f0093SYork Sun 		while (!(ddr_in32(&ddr->debug[1]) & 0x2) &&
4029f9f0093SYork Sun 		       (timeout > 0)) {
4037cc07998SYork Sun 			udelay(1000);
4049f9f0093SYork Sun 			timeout--;
4059f9f0093SYork Sun 		}
4069f9f0093SYork Sun 		if (timeout <= 0) {
4079f9f0093SYork Sun 			printf("Controler %d timeout, debug_2 = %x\n",
4089f9f0093SYork Sun 			       ctrl_num, ddr_in32(&ddr->debug[1]));
4099f9f0093SYork Sun 		}
4104baa38c5SYork Sun 	}
4119f9f0093SYork Sun #endif /* CONFIG_SYS_FSL_ERRATUM_A008511 */
4129f9f0093SYork Sun 
413dd8e740cSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009803
414d3674046SShengzhou Liu 	if (regs->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN) {
415dd8e740cSShengzhou Liu 		/* if it's RDIMM */
416dd8e740cSShengzhou Liu 		if (regs->ddr_sdram_cfg & SDRAM_CFG_RD_EN) {
417dd8e740cSShengzhou Liu 			for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
418dd8e740cSShengzhou Liu 				if (!(regs->cs[i].config & SDRAM_CS_CONFIG_EN))
419dd8e740cSShengzhou Liu 					continue;
420dd8e740cSShengzhou Liu 				set_wait_for_bits_clear(&ddr->sdram_md_cntl,
421dd8e740cSShengzhou Liu 							MD_CNTL_MD_EN |
422dd8e740cSShengzhou Liu 							MD_CNTL_CS_SEL(i) |
423dd8e740cSShengzhou Liu 							0x070000ed,
424dd8e740cSShengzhou Liu 							MD_CNTL_MD_EN);
425dd8e740cSShengzhou Liu 				udelay(1);
426dd8e740cSShengzhou Liu 			}
427dd8e740cSShengzhou Liu 		}
428dd8e740cSShengzhou Liu 
429dd8e740cSShengzhou Liu 		ddr_out32(&ddr->err_disable,
430dd8e740cSShengzhou Liu 			  regs->err_disable & ~DDR_ERR_DISABLE_APED);
431d3674046SShengzhou Liu 	}
432dd8e740cSShengzhou Liu #endif
4334baa38c5SYork Sun 	/* Restore D_INIT */
4344baa38c5SYork Sun 	ddr_out32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
435dd8e740cSShengzhou Liu #endif
436dd8e740cSShengzhou Liu 
43734e026f9SYork Sun 	total_gb_size_per_controller = 0;
43834e026f9SYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
43934e026f9SYork Sun 		if (!(regs->cs[i].config & 0x80000000))
44034e026f9SYork Sun 			continue;
44134e026f9SYork Sun 		total_gb_size_per_controller += 1 << (
44234e026f9SYork Sun 			((regs->cs[i].config >> 14) & 0x3) + 2 +
44334e026f9SYork Sun 			((regs->cs[i].config >> 8) & 0x7) + 12 +
44434e026f9SYork Sun 			((regs->cs[i].config >> 4) & 0x3) + 0 +
44534e026f9SYork Sun 			((regs->cs[i].config >> 0) & 0x7) + 8 +
446944537c5SYork Sun 			((regs->ddr_sdram_cfg_3 >> 4) & 0x3) +
44734e026f9SYork Sun 			3 - ((regs->ddr_sdram_cfg >> 19) & 0x3) -
44834e026f9SYork Sun 			26);			/* minus 26 (count of 64M) */
44934e026f9SYork Sun 	}
45034e026f9SYork Sun 	/*
45134e026f9SYork Sun 	 * total memory / bus width = transactions needed
45234e026f9SYork Sun 	 * transactions needed / data rate = seconds
45334e026f9SYork Sun 	 * to add plenty of buffer, double the time
45434e026f9SYork Sun 	 * For example, 2GB on 666MT/s 64-bit bus takes about 402ms
45534e026f9SYork Sun 	 * Let's wait for 800ms
45634e026f9SYork Sun 	 */
457f80d6472SYork Sun 	bus_width = 3 - ((ddr_in32(&ddr->sdram_cfg) & SDRAM_CFG_DBW_MASK)
45834e026f9SYork Sun 			>> SDRAM_CFG_DBW_SHIFT);
45934e026f9SYork Sun 	timeout = ((total_gb_size_per_controller << (6 - bus_width)) * 100 /
46003e664d8SYork Sun 		(get_ddr_freq(ctrl_num) >> 20)) << 2;
46134e026f9SYork Sun 	total_gb_size_per_controller >>= 4;	/* shift down to gb size */
46234e026f9SYork Sun 	debug("total %d GB\n", total_gb_size_per_controller);
46334e026f9SYork Sun 	debug("Need to wait up to %d * 10ms\n", timeout);
46434e026f9SYork Sun 
46534e026f9SYork Sun 	/* Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done.  */
46634e026f9SYork Sun 	while ((ddr_in32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
46734e026f9SYork Sun 		(timeout >= 0)) {
46834e026f9SYork Sun 		udelay(10000);		/* throttle polling rate */
46934e026f9SYork Sun 		timeout--;
47034e026f9SYork Sun 	}
47134e026f9SYork Sun 
47234e026f9SYork Sun 	if (timeout <= 0)
47334e026f9SYork Sun 		printf("Waiting for D_INIT timeout. Memory may not work.\n");
474a994b3deSShengzhou Liu 
475944537c5SYork Sun 	if (mod_bnds) {
476944537c5SYork Sun 		debug("Reset to original bnds\n");
477944537c5SYork Sun 		ddr_out32(&ddr->cs0_bnds, regs->cs[0].bnds);
478944537c5SYork Sun #if (CONFIG_CHIP_SELECTS_PER_CTRL > 1)
479944537c5SYork Sun 		ddr_out32(&ddr->cs1_bnds, regs->cs[1].bnds);
480944537c5SYork Sun #if (CONFIG_CHIP_SELECTS_PER_CTRL > 2)
481944537c5SYork Sun 		ddr_out32(&ddr->cs2_bnds, regs->cs[2].bnds);
482944537c5SYork Sun #if (CONFIG_CHIP_SELECTS_PER_CTRL > 3)
483944537c5SYork Sun 		ddr_out32(&ddr->cs3_bnds, regs->cs[3].bnds);
484944537c5SYork Sun #endif
485944537c5SYork Sun #endif
486944537c5SYork Sun #endif
487944537c5SYork Sun 		ddr_out32(&ddr->cs0_config, regs->cs[0].config);
488944537c5SYork Sun 	}
489944537c5SYork Sun 
490a994b3deSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009663
491a994b3deSShengzhou Liu 	ddr_out32(&ddr->sdram_interval, regs->ddr_sdram_interval);
492a994b3deSShengzhou Liu #endif
493a994b3deSShengzhou Liu 
494a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
495a7787b78STang Yuantian 	if (is_warm_boot()) {
496a7787b78STang Yuantian 		/* exit self-refresh */
4975a17b8b5SShengzhou Liu 		temp32 = ddr_in32(&ddr->sdram_cfg_2);
4985a17b8b5SShengzhou Liu 		temp32 &= ~SDRAM_CFG2_FRC_SR;
4995a17b8b5SShengzhou Liu 		ddr_out32(&ddr->sdram_cfg_2, temp32);
500a7787b78STang Yuantian 	}
501a7787b78STang Yuantian #endif
5024516ff81SYork Sun 
5034516ff81SYork Sun #ifdef CONFIG_FSL_DDR_BIST
5044516ff81SYork Sun #define BIST_PATTERN1	0xFFFFFFFF
5054516ff81SYork Sun #define BIST_PATTERN2	0x0
5064516ff81SYork Sun #define BIST_CR		0x80010000
5074516ff81SYork Sun #define BIST_CR_EN	0x80000000
5084516ff81SYork Sun #define BIST_CR_STAT	0x00000001
5094516ff81SYork Sun 	/* Perform build-in test on memory. Three-way interleaving is not yet
5104516ff81SYork Sun 	 * supported by this code. */
51100caae6dSSimon Glass 	if (env_get_f("ddr_bist", buffer, CONFIG_SYS_CBSIZE) >= 0) {
5124516ff81SYork Sun 		puts("Running BIST test. This will take a while...");
5134516ff81SYork Sun 		cs0_config = ddr_in32(&ddr->cs0_config);
514da305b9fSYork Sun 		cs0_bnds = ddr_in32(&ddr->cs0_bnds);
515da305b9fSYork Sun 		cs1_bnds = ddr_in32(&ddr->cs1_bnds);
516da305b9fSYork Sun 		cs2_bnds = ddr_in32(&ddr->cs2_bnds);
517da305b9fSYork Sun 		cs3_bnds = ddr_in32(&ddr->cs3_bnds);
5184516ff81SYork Sun 		if (cs0_config & CTLR_INTLV_MASK) {
5194516ff81SYork Sun 			/* set bnds to non-interleaving */
520da305b9fSYork Sun 			ddr_out32(&ddr->cs0_bnds, (cs0_bnds & 0xfffefffe) >> 1);
521da305b9fSYork Sun 			ddr_out32(&ddr->cs1_bnds, (cs1_bnds & 0xfffefffe) >> 1);
522da305b9fSYork Sun 			ddr_out32(&ddr->cs2_bnds, (cs2_bnds & 0xfffefffe) >> 1);
523da305b9fSYork Sun 			ddr_out32(&ddr->cs3_bnds, (cs3_bnds & 0xfffefffe) >> 1);
5244516ff81SYork Sun 		}
5254516ff81SYork Sun 		ddr_out32(&ddr->mtp1, BIST_PATTERN1);
5264516ff81SYork Sun 		ddr_out32(&ddr->mtp2, BIST_PATTERN1);
5274516ff81SYork Sun 		ddr_out32(&ddr->mtp3, BIST_PATTERN2);
5284516ff81SYork Sun 		ddr_out32(&ddr->mtp4, BIST_PATTERN2);
5294516ff81SYork Sun 		ddr_out32(&ddr->mtp5, BIST_PATTERN1);
5304516ff81SYork Sun 		ddr_out32(&ddr->mtp6, BIST_PATTERN1);
5314516ff81SYork Sun 		ddr_out32(&ddr->mtp7, BIST_PATTERN2);
5324516ff81SYork Sun 		ddr_out32(&ddr->mtp8, BIST_PATTERN2);
5334516ff81SYork Sun 		ddr_out32(&ddr->mtp9, BIST_PATTERN1);
5344516ff81SYork Sun 		ddr_out32(&ddr->mtp10, BIST_PATTERN2);
5354516ff81SYork Sun 		mtcr = BIST_CR;
5364516ff81SYork Sun 		ddr_out32(&ddr->mtcr, mtcr);
5374516ff81SYork Sun 		timeout = 100;
5384516ff81SYork Sun 		while (timeout > 0 && (mtcr & BIST_CR_EN)) {
5394516ff81SYork Sun 			mdelay(1000);
5404516ff81SYork Sun 			timeout--;
5414516ff81SYork Sun 			mtcr = ddr_in32(&ddr->mtcr);
5424516ff81SYork Sun 		}
5434516ff81SYork Sun 		if (timeout <= 0)
5444516ff81SYork Sun 			puts("Timeout\n");
5454516ff81SYork Sun 		else
5464516ff81SYork Sun 			puts("Done\n");
5474516ff81SYork Sun 		err_detect = ddr_in32(&ddr->err_detect);
5484516ff81SYork Sun 		err_sbe = ddr_in32(&ddr->err_sbe);
5494516ff81SYork Sun 		if (mtcr & BIST_CR_STAT) {
5504516ff81SYork Sun 			printf("BIST test failed on controller %d.\n",
5514516ff81SYork Sun 			       ctrl_num);
5524516ff81SYork Sun 		}
5534516ff81SYork Sun 		if (err_detect || (err_sbe & 0xffff)) {
5544516ff81SYork Sun 			printf("ECC error detected on controller %d.\n",
5554516ff81SYork Sun 			       ctrl_num);
5564516ff81SYork Sun 		}
5574516ff81SYork Sun 
5584516ff81SYork Sun 		if (cs0_config & CTLR_INTLV_MASK) {
5594516ff81SYork Sun 			/* restore bnds registers */
560da305b9fSYork Sun 			ddr_out32(&ddr->cs0_bnds, cs0_bnds);
561da305b9fSYork Sun 			ddr_out32(&ddr->cs1_bnds, cs1_bnds);
562da305b9fSYork Sun 			ddr_out32(&ddr->cs2_bnds, cs2_bnds);
563da305b9fSYork Sun 			ddr_out32(&ddr->cs3_bnds, cs3_bnds);
5644516ff81SYork Sun 		}
5654516ff81SYork Sun 	}
5664516ff81SYork Sun #endif
56734e026f9SYork Sun }
568