xref: /openbmc/u-boot/drivers/ddr/fsl/fsl_ddr_gen4.c (revision a994b3deb00bf3177cdf9f92060baec4f640f466)
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 
159f9f0093SYork Sun #ifdef CONFIG_SYS_FSL_ERRATUM_A008511
169f9f0093SYork Sun static void set_wait_for_bits_clear(void *ptr, u32 value, u32 bits)
179f9f0093SYork Sun {
189f9f0093SYork Sun 	int timeout = 1000;
199f9f0093SYork Sun 
209f9f0093SYork Sun 	ddr_out32(ptr, value);
219f9f0093SYork Sun 
229f9f0093SYork Sun 	while (ddr_in32(ptr) & bits) {
239f9f0093SYork Sun 		udelay(100);
249f9f0093SYork Sun 		timeout--;
259f9f0093SYork Sun 	}
269f9f0093SYork Sun 	if (timeout <= 0)
279f9f0093SYork Sun 		puts("Error: A007865 wait for clear timeout.\n");
289f9f0093SYork Sun }
299f9f0093SYork Sun #endif /* CONFIG_SYS_FSL_ERRATUM_A008511 */
309f9f0093SYork Sun 
3134e026f9SYork Sun #if (CONFIG_CHIP_SELECTS_PER_CTRL > 4)
3234e026f9SYork Sun #error Invalid setting for CONFIG_CHIP_SELECTS_PER_CTRL
3334e026f9SYork Sun #endif
3434e026f9SYork Sun 
3534e026f9SYork Sun /*
3634e026f9SYork Sun  * regs has the to-be-set values for DDR controller registers
3734e026f9SYork Sun  * ctrl_num is the DDR controller number
3834e026f9SYork Sun  * step: 0 goes through the initialization in one pass
3934e026f9SYork Sun  *       1 sets registers and returns before enabling controller
4034e026f9SYork Sun  *       2 resumes from step 1 and continues to initialize
4134e026f9SYork Sun  * Dividing the initialization to two steps to deassert DDR reset signal
4234e026f9SYork Sun  * to comply with JEDEC specs for RDIMMs.
4334e026f9SYork Sun  */
4434e026f9SYork Sun void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
4534e026f9SYork Sun 			     unsigned int ctrl_num, int step)
4634e026f9SYork Sun {
4734e026f9SYork Sun 	unsigned int i, bus_width;
4834e026f9SYork Sun 	struct ccsr_ddr __iomem *ddr;
4934e026f9SYork Sun 	u32 temp_sdram_cfg;
5034e026f9SYork Sun 	u32 total_gb_size_per_controller;
5134e026f9SYork Sun 	int timeout;
529f9f0093SYork Sun #ifdef CONFIG_SYS_FSL_ERRATUM_A008511
539f9f0093SYork Sun 	u32 temp32, mr6;
547cc07998SYork Sun 	u32 vref_seq1[3] = {0x80, 0x96, 0x16};	/* for range 1 */
557cc07998SYork Sun 	u32 vref_seq2[3] = {0xc0, 0xf0, 0x70};	/* for range 2 */
567cc07998SYork Sun 	u32 *vref_seq = vref_seq1;
579f9f0093SYork Sun #endif
580d3972cfSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009942
590d3972cfSShengzhou Liu 	ulong ddr_freq;
600d3972cfSShengzhou Liu 	u32 tmp;
610d3972cfSShengzhou Liu #endif
624516ff81SYork Sun #ifdef CONFIG_FSL_DDR_BIST
634516ff81SYork Sun 	u32 mtcr, err_detect, err_sbe;
644516ff81SYork Sun 	u32 cs0_bnds, cs1_bnds, cs2_bnds, cs3_bnds, cs0_config;
654516ff81SYork Sun #endif
664516ff81SYork Sun #ifdef CONFIG_FSL_DDR_BIST
674516ff81SYork Sun 	char buffer[CONFIG_SYS_CBSIZE];
684516ff81SYork Sun #endif
6934e026f9SYork Sun 
7034e026f9SYork Sun 	switch (ctrl_num) {
7134e026f9SYork Sun 	case 0:
7234e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
7334e026f9SYork Sun 		break;
7434e026f9SYork Sun #if defined(CONFIG_SYS_FSL_DDR2_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 1)
7534e026f9SYork Sun 	case 1:
7634e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR2_ADDR;
7734e026f9SYork Sun 		break;
7834e026f9SYork Sun #endif
7934e026f9SYork Sun #if defined(CONFIG_SYS_FSL_DDR3_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 2)
8034e026f9SYork Sun 	case 2:
8134e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR3_ADDR;
8234e026f9SYork Sun 		break;
8334e026f9SYork Sun #endif
8434e026f9SYork Sun #if defined(CONFIG_SYS_FSL_DDR4_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 3)
8534e026f9SYork Sun 	case 3:
8634e026f9SYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR4_ADDR;
8734e026f9SYork Sun 		break;
8834e026f9SYork Sun #endif
8934e026f9SYork Sun 	default:
9034e026f9SYork Sun 		printf("%s unexpected ctrl_num = %u\n", __func__, ctrl_num);
9134e026f9SYork Sun 		return;
9234e026f9SYork Sun 	}
9334e026f9SYork Sun 
9434e026f9SYork Sun 	if (step == 2)
9534e026f9SYork Sun 		goto step2;
9634e026f9SYork Sun 
9734e026f9SYork Sun 	if (regs->ddr_eor)
9834e026f9SYork Sun 		ddr_out32(&ddr->eor, regs->ddr_eor);
9934e026f9SYork Sun 
10034e026f9SYork Sun 	ddr_out32(&ddr->sdram_clk_cntl, regs->ddr_sdram_clk_cntl);
10134e026f9SYork Sun 
10234e026f9SYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
10334e026f9SYork Sun 		if (i == 0) {
10434e026f9SYork Sun 			ddr_out32(&ddr->cs0_bnds, regs->cs[i].bnds);
10534e026f9SYork Sun 			ddr_out32(&ddr->cs0_config, regs->cs[i].config);
10634e026f9SYork Sun 			ddr_out32(&ddr->cs0_config_2, regs->cs[i].config_2);
10734e026f9SYork Sun 
10834e026f9SYork Sun 		} else if (i == 1) {
10934e026f9SYork Sun 			ddr_out32(&ddr->cs1_bnds, regs->cs[i].bnds);
11034e026f9SYork Sun 			ddr_out32(&ddr->cs1_config, regs->cs[i].config);
11134e026f9SYork Sun 			ddr_out32(&ddr->cs1_config_2, regs->cs[i].config_2);
11234e026f9SYork Sun 
11334e026f9SYork Sun 		} else if (i == 2) {
11434e026f9SYork Sun 			ddr_out32(&ddr->cs2_bnds, regs->cs[i].bnds);
11534e026f9SYork Sun 			ddr_out32(&ddr->cs2_config, regs->cs[i].config);
11634e026f9SYork Sun 			ddr_out32(&ddr->cs2_config_2, regs->cs[i].config_2);
11734e026f9SYork Sun 
11834e026f9SYork Sun 		} else if (i == 3) {
11934e026f9SYork Sun 			ddr_out32(&ddr->cs3_bnds, regs->cs[i].bnds);
12034e026f9SYork Sun 			ddr_out32(&ddr->cs3_config, regs->cs[i].config);
12134e026f9SYork Sun 			ddr_out32(&ddr->cs3_config_2, regs->cs[i].config_2);
12234e026f9SYork Sun 		}
12334e026f9SYork Sun 	}
12434e026f9SYork Sun 
12534e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_3, regs->timing_cfg_3);
12634e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_0, regs->timing_cfg_0);
12734e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_1, regs->timing_cfg_1);
12834e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_2, regs->timing_cfg_2);
12934e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_4, regs->timing_cfg_4);
13034e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_5, regs->timing_cfg_5);
13134e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_6, regs->timing_cfg_6);
13234e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_7, regs->timing_cfg_7);
13334e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_8, regs->timing_cfg_8);
13434e026f9SYork Sun 	ddr_out32(&ddr->timing_cfg_9, regs->timing_cfg_9);
13534e026f9SYork Sun 	ddr_out32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
13634e026f9SYork Sun 	ddr_out32(&ddr->dq_map_0, regs->dq_map_0);
13734e026f9SYork Sun 	ddr_out32(&ddr->dq_map_1, regs->dq_map_1);
13834e026f9SYork Sun 	ddr_out32(&ddr->dq_map_2, regs->dq_map_2);
13934e026f9SYork Sun 	ddr_out32(&ddr->dq_map_3, regs->dq_map_3);
14034e026f9SYork Sun 	ddr_out32(&ddr->sdram_cfg_3, regs->ddr_sdram_cfg_3);
14134e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode, regs->ddr_sdram_mode);
14234e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_2, regs->ddr_sdram_mode_2);
14334e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_3, regs->ddr_sdram_mode_3);
14434e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_4, regs->ddr_sdram_mode_4);
14534e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_5, regs->ddr_sdram_mode_5);
14634e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_6, regs->ddr_sdram_mode_6);
14734e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_7, regs->ddr_sdram_mode_7);
14834e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_8, regs->ddr_sdram_mode_8);
14934e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_9, regs->ddr_sdram_mode_9);
15034e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_10, regs->ddr_sdram_mode_10);
15134e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_11, regs->ddr_sdram_mode_11);
15234e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_12, regs->ddr_sdram_mode_12);
15334e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_13, regs->ddr_sdram_mode_13);
15434e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_14, regs->ddr_sdram_mode_14);
15534e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_15, regs->ddr_sdram_mode_15);
15634e026f9SYork Sun 	ddr_out32(&ddr->sdram_mode_16, regs->ddr_sdram_mode_16);
15734e026f9SYork Sun 	ddr_out32(&ddr->sdram_md_cntl, regs->ddr_sdram_md_cntl);
158*a994b3deSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009663
159*a994b3deSShengzhou Liu 	ddr_out32(&ddr->sdram_interval,
160*a994b3deSShengzhou Liu 		  regs->ddr_sdram_interval & ~SDRAM_INTERVAL_BSTOPRE);
161*a994b3deSShengzhou Liu #else
16234e026f9SYork Sun 	ddr_out32(&ddr->sdram_interval, regs->ddr_sdram_interval);
163*a994b3deSShengzhou Liu #endif
16434e026f9SYork Sun 	ddr_out32(&ddr->sdram_data_init, regs->ddr_data_init);
16534e026f9SYork Sun 	ddr_out32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
16634e026f9SYork Sun #ifndef CONFIG_SYS_FSL_DDR_EMU
16734e026f9SYork Sun 	/*
16834e026f9SYork Sun 	 * Skip these two registers if running on emulator
16934e026f9SYork Sun 	 * because emulator doesn't have skew between bytes.
17034e026f9SYork Sun 	 */
17134e026f9SYork Sun 
17234e026f9SYork Sun 	if (regs->ddr_wrlvl_cntl_2)
17334e026f9SYork Sun 		ddr_out32(&ddr->ddr_wrlvl_cntl_2, regs->ddr_wrlvl_cntl_2);
17434e026f9SYork Sun 	if (regs->ddr_wrlvl_cntl_3)
17534e026f9SYork Sun 		ddr_out32(&ddr->ddr_wrlvl_cntl_3, regs->ddr_wrlvl_cntl_3);
17634e026f9SYork Sun #endif
17734e026f9SYork Sun 
17834e026f9SYork Sun 	ddr_out32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr);
17934e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_1, regs->ddr_sdram_rcw_1);
18034e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_2, regs->ddr_sdram_rcw_2);
18134e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_3, regs->ddr_sdram_rcw_3);
18234e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_4, regs->ddr_sdram_rcw_4);
18334e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_5, regs->ddr_sdram_rcw_5);
18434e026f9SYork Sun 	ddr_out32(&ddr->ddr_sdram_rcw_6, regs->ddr_sdram_rcw_6);
18534e026f9SYork Sun 	ddr_out32(&ddr->ddr_cdr1, regs->ddr_cdr1);
186a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
187a7787b78STang Yuantian 	if (is_warm_boot()) {
188a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2,
189a7787b78STang Yuantian 			  regs->ddr_sdram_cfg_2 & ~SDRAM_CFG2_D_INIT);
190a7787b78STang Yuantian 		ddr_out32(&ddr->init_addr, CONFIG_SYS_SDRAM_BASE);
191a7787b78STang Yuantian 		ddr_out32(&ddr->init_ext_addr, DDR_INIT_ADDR_EXT_UIA);
192a7787b78STang Yuantian 
193a7787b78STang Yuantian 		/* DRAM VRef will not be trained */
194a7787b78STang Yuantian 		ddr_out32(&ddr->ddr_cdr2,
195a7787b78STang Yuantian 			  regs->ddr_cdr2 & ~DDR_CDR2_VREF_TRAIN_EN);
196a7787b78STang Yuantian 	} else
197a7787b78STang Yuantian #endif
198a7787b78STang Yuantian 	{
199a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
200a7787b78STang Yuantian 		ddr_out32(&ddr->init_addr, regs->ddr_init_addr);
201a7787b78STang Yuantian 		ddr_out32(&ddr->init_ext_addr, regs->ddr_init_ext_addr);
20234e026f9SYork Sun 		ddr_out32(&ddr->ddr_cdr2, regs->ddr_cdr2);
203a7787b78STang Yuantian 	}
20434e026f9SYork Sun 	ddr_out32(&ddr->err_disable, regs->err_disable);
20534e026f9SYork Sun 	ddr_out32(&ddr->err_int_en, regs->err_int_en);
20634e026f9SYork Sun 	for (i = 0; i < 32; i++) {
20734e026f9SYork Sun 		if (regs->debug[i]) {
20834e026f9SYork Sun 			debug("Write to debug_%d as %08x\n",
20934e026f9SYork Sun 			      i+1, regs->debug[i]);
21034e026f9SYork Sun 			ddr_out32(&ddr->debug[i], regs->debug[i]);
21134e026f9SYork Sun 		}
21234e026f9SYork Sun 	}
213dda3b610SYork Sun #ifdef CONFIG_SYS_FSL_ERRATUM_A008378
214dda3b610SYork Sun 	/* Erratum applies when accumulated ECC is used, or DBI is enabled */
215dda3b610SYork Sun #define IS_ACC_ECC_EN(v) ((v) & 0x4)
216dda3b610SYork Sun #define IS_DBI(v) ((((v) >> 12) & 0x3) == 0x2)
217a46b1852SShengzhou Liu 	if (has_erratum_a008378()) {
218dda3b610SYork Sun 		if (IS_ACC_ECC_EN(regs->ddr_sdram_cfg) ||
219dda3b610SYork Sun 		    IS_DBI(regs->ddr_sdram_cfg_3))
220a46b1852SShengzhou Liu 			ddr_setbits32(&ddr->debug[28], 0x9 << 20);
221a46b1852SShengzhou Liu 	}
222dda3b610SYork Sun #endif
22334e026f9SYork Sun 
2249f9f0093SYork Sun #ifdef CONFIG_SYS_FSL_ERRATUM_A008511
2259f9f0093SYork Sun 	/* Part 1 of 2 */
2269f9f0093SYork Sun 	/* This erraum only applies to verion 5.2.0 */
2279f9f0093SYork Sun 	if (fsl_ddr_get_version(ctrl_num) == 0x50200) {
2289f9f0093SYork Sun 		/* Disable DRAM VRef training */
2299f9f0093SYork Sun 		ddr_out32(&ddr->ddr_cdr2,
2309f9f0093SYork Sun 			  regs->ddr_cdr2 & ~DDR_CDR2_VREF_TRAIN_EN);
2319f9f0093SYork Sun 		/* Disable deskew */
2329f9f0093SYork Sun 		ddr_out32(&ddr->debug[28], 0x400);
2339f9f0093SYork Sun 		/* Disable D_INIT */
2349f9f0093SYork Sun 		ddr_out32(&ddr->sdram_cfg_2,
2359f9f0093SYork Sun 			  regs->ddr_sdram_cfg_2 & ~SDRAM_CFG2_D_INIT);
2369f9f0093SYork Sun 		ddr_out32(&ddr->debug[25], 0x9000);
2379f9f0093SYork Sun 	}
2389f9f0093SYork Sun #endif
2390d3972cfSShengzhou Liu 
2400d3972cfSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009942
2410d3972cfSShengzhou Liu 	ddr_freq = get_ddr_freq(ctrl_num) / 1000000;
2420d3972cfSShengzhou Liu 	tmp = ddr_in32(&ddr->debug[28]);
2430d3972cfSShengzhou Liu 	if (ddr_freq <= 1333)
2440d3972cfSShengzhou Liu 		ddr_out32(&ddr->debug[28], tmp | 0x0080006a);
2450d3972cfSShengzhou Liu 	else if (ddr_freq <= 1600)
2460d3972cfSShengzhou Liu 		ddr_out32(&ddr->debug[28], tmp | 0x0070006f);
2470d3972cfSShengzhou Liu 	else if (ddr_freq <= 1867)
2480d3972cfSShengzhou Liu 		ddr_out32(&ddr->debug[28], tmp | 0x00700076);
2490d3972cfSShengzhou Liu 	else if (ddr_freq <= 2133)
2500d3972cfSShengzhou Liu 		ddr_out32(&ddr->debug[28], tmp | 0x0060007b);
2510d3972cfSShengzhou Liu #endif
2520d3972cfSShengzhou Liu 
25334e026f9SYork Sun 	/*
25434e026f9SYork Sun 	 * For RDIMMs, JEDEC spec requires clocks to be stable before reset is
25534e026f9SYork Sun 	 * deasserted. Clocks start when any chip select is enabled and clock
25634e026f9SYork Sun 	 * control register is set. Because all DDR components are connected to
25734e026f9SYork Sun 	 * one reset signal, this needs to be done in two steps. Step 1 is to
25834e026f9SYork Sun 	 * get the clocks started. Step 2 resumes after reset signal is
25934e026f9SYork Sun 	 * deasserted.
26034e026f9SYork Sun 	 */
26134e026f9SYork Sun 	if (step == 1) {
26234e026f9SYork Sun 		udelay(200);
26334e026f9SYork Sun 		return;
26434e026f9SYork Sun 	}
26534e026f9SYork Sun 
26634e026f9SYork Sun step2:
26734e026f9SYork Sun 	/* Set, but do not enable the memory */
26834e026f9SYork Sun 	temp_sdram_cfg = regs->ddr_sdram_cfg;
26934e026f9SYork Sun 	temp_sdram_cfg &= ~(SDRAM_CFG_MEM_EN);
27034e026f9SYork Sun 	ddr_out32(&ddr->sdram_cfg, temp_sdram_cfg);
27134e026f9SYork Sun 
27234e026f9SYork Sun 	/*
27334e026f9SYork Sun 	 * 500 painful micro-seconds must elapse between
27434e026f9SYork Sun 	 * the DDR clock setup and the DDR config enable.
27534e026f9SYork Sun 	 * DDR2 need 200 us, and DDR3 need 500 us from spec,
27634e026f9SYork Sun 	 * we choose the max, that is 500 us for all of case.
27734e026f9SYork Sun 	 */
27834e026f9SYork Sun 	udelay(500);
2798340e7acSYork Sun 	mb();
2808340e7acSYork Sun 	isb();
28134e026f9SYork Sun 
282a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
283a7787b78STang Yuantian 	if (is_warm_boot()) {
284a7787b78STang Yuantian 		/* enter self-refresh */
285a7787b78STang Yuantian 		temp_sdram_cfg = ddr_in32(&ddr->sdram_cfg_2);
286a7787b78STang Yuantian 		temp_sdram_cfg |= SDRAM_CFG2_FRC_SR;
287a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2, temp_sdram_cfg);
288a7787b78STang Yuantian 		/* do board specific memory setup */
289a7787b78STang Yuantian 		board_mem_sleep_setup();
290a7787b78STang Yuantian 
291a7787b78STang Yuantian 		temp_sdram_cfg = (ddr_in32(&ddr->sdram_cfg) | SDRAM_CFG_BI);
292a7787b78STang Yuantian 	} else
293a7787b78STang Yuantian #endif
29434e026f9SYork Sun 		temp_sdram_cfg = ddr_in32(&ddr->sdram_cfg) & ~SDRAM_CFG_BI;
295a7787b78STang Yuantian 	/* Let the controller go */
29634e026f9SYork Sun 	ddr_out32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_MEM_EN);
2978340e7acSYork Sun 	mb();
2988340e7acSYork Sun 	isb();
29934e026f9SYork Sun 
3009f9f0093SYork Sun #ifdef CONFIG_SYS_FSL_ERRATUM_A008511
3019f9f0093SYork Sun 	/* Part 2 of 2 */
3029f9f0093SYork Sun 	/* This erraum only applies to verion 5.2.0 */
3039f9f0093SYork Sun 	if (fsl_ddr_get_version(ctrl_num) == 0x50200) {
3049f9f0093SYork Sun 		/* Wait for idle */
3057cc07998SYork Sun 		timeout = 40;
3069f9f0093SYork Sun 		while (!(ddr_in32(&ddr->debug[1]) & 0x2) &&
3079f9f0093SYork Sun 		       (timeout > 0)) {
3087cc07998SYork Sun 			udelay(1000);
3099f9f0093SYork Sun 			timeout--;
3109f9f0093SYork Sun 		}
3119f9f0093SYork Sun 		if (timeout <= 0) {
3129f9f0093SYork Sun 			printf("Controler %d timeout, debug_2 = %x\n",
3139f9f0093SYork Sun 			       ctrl_num, ddr_in32(&ddr->debug[1]));
3149f9f0093SYork Sun 		}
3157cc07998SYork Sun 
3167cc07998SYork Sun 		/* The vref setting sequence is different for range 2 */
3177cc07998SYork Sun 		if (regs->ddr_cdr2 & DDR_CDR2_VREF_RANGE_2)
3187cc07998SYork Sun 			vref_seq = vref_seq2;
3197cc07998SYork Sun 
3209f9f0093SYork Sun 		/* Set VREF */
3219f9f0093SYork Sun 		for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
3229f9f0093SYork Sun 			if (!(regs->cs[i].config & SDRAM_CS_CONFIG_EN))
3239f9f0093SYork Sun 				continue;
3249f9f0093SYork Sun 
3259f9f0093SYork Sun 			mr6 = (regs->ddr_sdram_mode_10 >> 16)		|
3269f9f0093SYork Sun 				 MD_CNTL_MD_EN				|
3279f9f0093SYork Sun 				 MD_CNTL_CS_SEL(i)			|
3289f9f0093SYork Sun 				 MD_CNTL_MD_SEL(6)			|
3299f9f0093SYork Sun 				 0x00200000;
3307cc07998SYork Sun 			temp32 = mr6 | vref_seq[0];
3319f9f0093SYork Sun 			set_wait_for_bits_clear(&ddr->sdram_md_cntl,
3329f9f0093SYork Sun 						temp32, MD_CNTL_MD_EN);
3339f9f0093SYork Sun 			udelay(1);
3349f9f0093SYork Sun 			debug("MR6 = 0x%08x\n", temp32);
3357cc07998SYork Sun 			temp32 = mr6 | vref_seq[1];
3369f9f0093SYork Sun 			set_wait_for_bits_clear(&ddr->sdram_md_cntl,
3379f9f0093SYork Sun 						temp32, MD_CNTL_MD_EN);
3389f9f0093SYork Sun 			udelay(1);
3399f9f0093SYork Sun 			debug("MR6 = 0x%08x\n", temp32);
3407cc07998SYork Sun 			temp32 = mr6 | vref_seq[2];
3419f9f0093SYork Sun 			set_wait_for_bits_clear(&ddr->sdram_md_cntl,
3429f9f0093SYork Sun 						temp32, MD_CNTL_MD_EN);
3439f9f0093SYork Sun 			udelay(1);
3449f9f0093SYork Sun 			debug("MR6 = 0x%08x\n", temp32);
3459f9f0093SYork Sun 		}
3469f9f0093SYork Sun 		ddr_out32(&ddr->sdram_md_cntl, 0);
3479f9f0093SYork Sun 		ddr_out32(&ddr->debug[28], 0);		/* Enable deskew */
3489f9f0093SYork Sun 		ddr_out32(&ddr->debug[1], 0x400);	/* restart deskew */
3499f9f0093SYork Sun 		/* wait for idle */
3507cc07998SYork Sun 		timeout = 40;
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 		}
3609f9f0093SYork Sun 		/* Restore D_INIT */
3619f9f0093SYork Sun 		ddr_out32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
3629f9f0093SYork Sun 	}
3639f9f0093SYork Sun #endif /* CONFIG_SYS_FSL_ERRATUM_A008511 */
3649f9f0093SYork Sun 
36534e026f9SYork Sun 	total_gb_size_per_controller = 0;
36634e026f9SYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
36734e026f9SYork Sun 		if (!(regs->cs[i].config & 0x80000000))
36834e026f9SYork Sun 			continue;
36934e026f9SYork Sun 		total_gb_size_per_controller += 1 << (
37034e026f9SYork Sun 			((regs->cs[i].config >> 14) & 0x3) + 2 +
37134e026f9SYork Sun 			((regs->cs[i].config >> 8) & 0x7) + 12 +
37234e026f9SYork Sun 			((regs->cs[i].config >> 4) & 0x3) + 0 +
37334e026f9SYork Sun 			((regs->cs[i].config >> 0) & 0x7) + 8 +
37434e026f9SYork Sun 			3 - ((regs->ddr_sdram_cfg >> 19) & 0x3) -
37534e026f9SYork Sun 			26);			/* minus 26 (count of 64M) */
37634e026f9SYork Sun 	}
37734e026f9SYork Sun 	if (fsl_ddr_get_intl3r() & 0x80000000)	/* 3-way interleaving */
37834e026f9SYork Sun 		total_gb_size_per_controller *= 3;
37934e026f9SYork Sun 	else if (regs->cs[0].config & 0x20000000) /* 2-way interleaving */
38034e026f9SYork Sun 		total_gb_size_per_controller <<= 1;
38134e026f9SYork Sun 	/*
38234e026f9SYork Sun 	 * total memory / bus width = transactions needed
38334e026f9SYork Sun 	 * transactions needed / data rate = seconds
38434e026f9SYork Sun 	 * to add plenty of buffer, double the time
38534e026f9SYork Sun 	 * For example, 2GB on 666MT/s 64-bit bus takes about 402ms
38634e026f9SYork Sun 	 * Let's wait for 800ms
38734e026f9SYork Sun 	 */
388f80d6472SYork Sun 	bus_width = 3 - ((ddr_in32(&ddr->sdram_cfg) & SDRAM_CFG_DBW_MASK)
38934e026f9SYork Sun 			>> SDRAM_CFG_DBW_SHIFT);
39034e026f9SYork Sun 	timeout = ((total_gb_size_per_controller << (6 - bus_width)) * 100 /
39103e664d8SYork Sun 		(get_ddr_freq(ctrl_num) >> 20)) << 2;
39234e026f9SYork Sun 	total_gb_size_per_controller >>= 4;	/* shift down to gb size */
39334e026f9SYork Sun 	debug("total %d GB\n", total_gb_size_per_controller);
39434e026f9SYork Sun 	debug("Need to wait up to %d * 10ms\n", timeout);
39534e026f9SYork Sun 
39634e026f9SYork Sun 	/* Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done.  */
39734e026f9SYork Sun 	while ((ddr_in32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
39834e026f9SYork Sun 		(timeout >= 0)) {
39934e026f9SYork Sun 		udelay(10000);		/* throttle polling rate */
40034e026f9SYork Sun 		timeout--;
40134e026f9SYork Sun 	}
40234e026f9SYork Sun 
40334e026f9SYork Sun 	if (timeout <= 0)
40434e026f9SYork Sun 		printf("Waiting for D_INIT timeout. Memory may not work.\n");
405*a994b3deSShengzhou Liu 
406*a994b3deSShengzhou Liu #ifdef CONFIG_SYS_FSL_ERRATUM_A009663
407*a994b3deSShengzhou Liu 	ddr_out32(&ddr->sdram_interval, regs->ddr_sdram_interval);
408*a994b3deSShengzhou Liu #endif
409*a994b3deSShengzhou Liu 
410a7787b78STang Yuantian #ifdef CONFIG_DEEP_SLEEP
411a7787b78STang Yuantian 	if (is_warm_boot()) {
412a7787b78STang Yuantian 		/* exit self-refresh */
413a7787b78STang Yuantian 		temp_sdram_cfg = ddr_in32(&ddr->sdram_cfg_2);
414a7787b78STang Yuantian 		temp_sdram_cfg &= ~SDRAM_CFG2_FRC_SR;
415a7787b78STang Yuantian 		ddr_out32(&ddr->sdram_cfg_2, temp_sdram_cfg);
416a7787b78STang Yuantian 	}
417a7787b78STang Yuantian #endif
4184516ff81SYork Sun 
4194516ff81SYork Sun #ifdef CONFIG_FSL_DDR_BIST
4204516ff81SYork Sun #define BIST_PATTERN1	0xFFFFFFFF
4214516ff81SYork Sun #define BIST_PATTERN2	0x0
4224516ff81SYork Sun #define BIST_CR		0x80010000
4234516ff81SYork Sun #define BIST_CR_EN	0x80000000
4244516ff81SYork Sun #define BIST_CR_STAT	0x00000001
4254516ff81SYork Sun #define CTLR_INTLV_MASK	0x20000000
4264516ff81SYork Sun 	/* Perform build-in test on memory. Three-way interleaving is not yet
4274516ff81SYork Sun 	 * supported by this code. */
4284516ff81SYork Sun 	if (getenv_f("ddr_bist", buffer, CONFIG_SYS_CBSIZE) >= 0) {
4294516ff81SYork Sun 		puts("Running BIST test. This will take a while...");
4304516ff81SYork Sun 		cs0_config = ddr_in32(&ddr->cs0_config);
431da305b9fSYork Sun 		cs0_bnds = ddr_in32(&ddr->cs0_bnds);
432da305b9fSYork Sun 		cs1_bnds = ddr_in32(&ddr->cs1_bnds);
433da305b9fSYork Sun 		cs2_bnds = ddr_in32(&ddr->cs2_bnds);
434da305b9fSYork Sun 		cs3_bnds = ddr_in32(&ddr->cs3_bnds);
4354516ff81SYork Sun 		if (cs0_config & CTLR_INTLV_MASK) {
4364516ff81SYork Sun 			/* set bnds to non-interleaving */
437da305b9fSYork Sun 			ddr_out32(&ddr->cs0_bnds, (cs0_bnds & 0xfffefffe) >> 1);
438da305b9fSYork Sun 			ddr_out32(&ddr->cs1_bnds, (cs1_bnds & 0xfffefffe) >> 1);
439da305b9fSYork Sun 			ddr_out32(&ddr->cs2_bnds, (cs2_bnds & 0xfffefffe) >> 1);
440da305b9fSYork Sun 			ddr_out32(&ddr->cs3_bnds, (cs3_bnds & 0xfffefffe) >> 1);
4414516ff81SYork Sun 		}
4424516ff81SYork Sun 		ddr_out32(&ddr->mtp1, BIST_PATTERN1);
4434516ff81SYork Sun 		ddr_out32(&ddr->mtp2, BIST_PATTERN1);
4444516ff81SYork Sun 		ddr_out32(&ddr->mtp3, BIST_PATTERN2);
4454516ff81SYork Sun 		ddr_out32(&ddr->mtp4, BIST_PATTERN2);
4464516ff81SYork Sun 		ddr_out32(&ddr->mtp5, BIST_PATTERN1);
4474516ff81SYork Sun 		ddr_out32(&ddr->mtp6, BIST_PATTERN1);
4484516ff81SYork Sun 		ddr_out32(&ddr->mtp7, BIST_PATTERN2);
4494516ff81SYork Sun 		ddr_out32(&ddr->mtp8, BIST_PATTERN2);
4504516ff81SYork Sun 		ddr_out32(&ddr->mtp9, BIST_PATTERN1);
4514516ff81SYork Sun 		ddr_out32(&ddr->mtp10, BIST_PATTERN2);
4524516ff81SYork Sun 		mtcr = BIST_CR;
4534516ff81SYork Sun 		ddr_out32(&ddr->mtcr, mtcr);
4544516ff81SYork Sun 		timeout = 100;
4554516ff81SYork Sun 		while (timeout > 0 && (mtcr & BIST_CR_EN)) {
4564516ff81SYork Sun 			mdelay(1000);
4574516ff81SYork Sun 			timeout--;
4584516ff81SYork Sun 			mtcr = ddr_in32(&ddr->mtcr);
4594516ff81SYork Sun 		}
4604516ff81SYork Sun 		if (timeout <= 0)
4614516ff81SYork Sun 			puts("Timeout\n");
4624516ff81SYork Sun 		else
4634516ff81SYork Sun 			puts("Done\n");
4644516ff81SYork Sun 		err_detect = ddr_in32(&ddr->err_detect);
4654516ff81SYork Sun 		err_sbe = ddr_in32(&ddr->err_sbe);
4664516ff81SYork Sun 		if (mtcr & BIST_CR_STAT) {
4674516ff81SYork Sun 			printf("BIST test failed on controller %d.\n",
4684516ff81SYork Sun 			       ctrl_num);
4694516ff81SYork Sun 		}
4704516ff81SYork Sun 		if (err_detect || (err_sbe & 0xffff)) {
4714516ff81SYork Sun 			printf("ECC error detected on controller %d.\n",
4724516ff81SYork Sun 			       ctrl_num);
4734516ff81SYork Sun 		}
4744516ff81SYork Sun 
4754516ff81SYork Sun 		if (cs0_config & CTLR_INTLV_MASK) {
4764516ff81SYork Sun 			/* restore bnds registers */
477da305b9fSYork Sun 			ddr_out32(&ddr->cs0_bnds, cs0_bnds);
478da305b9fSYork Sun 			ddr_out32(&ddr->cs1_bnds, cs1_bnds);
479da305b9fSYork Sun 			ddr_out32(&ddr->cs2_bnds, cs2_bnds);
480da305b9fSYork Sun 			ddr_out32(&ddr->cs3_bnds, cs3_bnds);
4814516ff81SYork Sun 		}
4824516ff81SYork Sun 	}
4834516ff81SYork Sun #endif
48434e026f9SYork Sun }
485