xref: /openbmc/u-boot/drivers/ddr/fsl/arm_ddr_gen3.c (revision 9ac4ffbde1a5015c9929ee8578d3811b716e2fd3)
1*9ac4ffbdSYork Sun /*
2*9ac4ffbdSYork Sun  * Copyright 2013 Freescale Semiconductor, Inc.
3*9ac4ffbdSYork Sun  *
4*9ac4ffbdSYork Sun  * SPDX-License-Identifier:	GPL-2.0+
5*9ac4ffbdSYork Sun  *
6*9ac4ffbdSYork Sun  * Derived from mpc85xx_ddr_gen3.c, removed all workarounds
7*9ac4ffbdSYork Sun  */
8*9ac4ffbdSYork Sun 
9*9ac4ffbdSYork Sun #include <common.h>
10*9ac4ffbdSYork Sun #include <asm/io.h>
11*9ac4ffbdSYork Sun #include <fsl_ddr_sdram.h>
12*9ac4ffbdSYork Sun #include <asm/processor.h>
13*9ac4ffbdSYork Sun #include <fsl_immap.h>
14*9ac4ffbdSYork Sun 
15*9ac4ffbdSYork Sun #if (CONFIG_CHIP_SELECTS_PER_CTRL > 4)
16*9ac4ffbdSYork Sun #error Invalid setting for CONFIG_CHIP_SELECTS_PER_CTRL
17*9ac4ffbdSYork Sun #endif
18*9ac4ffbdSYork Sun 
19*9ac4ffbdSYork Sun 
20*9ac4ffbdSYork Sun /*
21*9ac4ffbdSYork Sun  * regs has the to-be-set values for DDR controller registers
22*9ac4ffbdSYork Sun  * ctrl_num is the DDR controller number
23*9ac4ffbdSYork Sun  * step: 0 goes through the initialization in one pass
24*9ac4ffbdSYork Sun  *       1 sets registers and returns before enabling controller
25*9ac4ffbdSYork Sun  *       2 resumes from step 1 and continues to initialize
26*9ac4ffbdSYork Sun  * Dividing the initialization to two steps to deassert DDR reset signal
27*9ac4ffbdSYork Sun  * to comply with JEDEC specs for RDIMMs.
28*9ac4ffbdSYork Sun  */
29*9ac4ffbdSYork Sun void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
30*9ac4ffbdSYork Sun 			     unsigned int ctrl_num, int step)
31*9ac4ffbdSYork Sun {
32*9ac4ffbdSYork Sun 	unsigned int i, bus_width;
33*9ac4ffbdSYork Sun 	struct ccsr_ddr __iomem *ddr;
34*9ac4ffbdSYork Sun 	u32 temp_sdram_cfg;
35*9ac4ffbdSYork Sun 	u32 total_gb_size_per_controller;
36*9ac4ffbdSYork Sun 	int timeout;
37*9ac4ffbdSYork Sun 
38*9ac4ffbdSYork Sun 	switch (ctrl_num) {
39*9ac4ffbdSYork Sun 	case 0:
40*9ac4ffbdSYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
41*9ac4ffbdSYork Sun 		break;
42*9ac4ffbdSYork Sun #if defined(CONFIG_SYS_FSL_DDR2_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 1)
43*9ac4ffbdSYork Sun 	case 1:
44*9ac4ffbdSYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR2_ADDR;
45*9ac4ffbdSYork Sun 		break;
46*9ac4ffbdSYork Sun #endif
47*9ac4ffbdSYork Sun #if defined(CONFIG_SYS_FSL_DDR3_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 2)
48*9ac4ffbdSYork Sun 	case 2:
49*9ac4ffbdSYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR3_ADDR;
50*9ac4ffbdSYork Sun 		break;
51*9ac4ffbdSYork Sun #endif
52*9ac4ffbdSYork Sun #if defined(CONFIG_SYS_FSL_DDR4_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 3)
53*9ac4ffbdSYork Sun 	case 3:
54*9ac4ffbdSYork Sun 		ddr = (void *)CONFIG_SYS_FSL_DDR4_ADDR;
55*9ac4ffbdSYork Sun 		break;
56*9ac4ffbdSYork Sun #endif
57*9ac4ffbdSYork Sun 	default:
58*9ac4ffbdSYork Sun 		printf("%s unexpected ctrl_num = %u\n", __func__, ctrl_num);
59*9ac4ffbdSYork Sun 		return;
60*9ac4ffbdSYork Sun 	}
61*9ac4ffbdSYork Sun 
62*9ac4ffbdSYork Sun 	if (step == 2)
63*9ac4ffbdSYork Sun 		goto step2;
64*9ac4ffbdSYork Sun 
65*9ac4ffbdSYork Sun 	if (regs->ddr_eor)
66*9ac4ffbdSYork Sun 		out_be32(&ddr->eor, regs->ddr_eor);
67*9ac4ffbdSYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
68*9ac4ffbdSYork Sun 		if (i == 0) {
69*9ac4ffbdSYork Sun 			out_be32(&ddr->cs0_bnds, regs->cs[i].bnds);
70*9ac4ffbdSYork Sun 			out_be32(&ddr->cs0_config, regs->cs[i].config);
71*9ac4ffbdSYork Sun 			out_be32(&ddr->cs0_config_2, regs->cs[i].config_2);
72*9ac4ffbdSYork Sun 
73*9ac4ffbdSYork Sun 		} else if (i == 1) {
74*9ac4ffbdSYork Sun 			out_be32(&ddr->cs1_bnds, regs->cs[i].bnds);
75*9ac4ffbdSYork Sun 			out_be32(&ddr->cs1_config, regs->cs[i].config);
76*9ac4ffbdSYork Sun 			out_be32(&ddr->cs1_config_2, regs->cs[i].config_2);
77*9ac4ffbdSYork Sun 
78*9ac4ffbdSYork Sun 		} else if (i == 2) {
79*9ac4ffbdSYork Sun 			out_be32(&ddr->cs2_bnds, regs->cs[i].bnds);
80*9ac4ffbdSYork Sun 			out_be32(&ddr->cs2_config, regs->cs[i].config);
81*9ac4ffbdSYork Sun 			out_be32(&ddr->cs2_config_2, regs->cs[i].config_2);
82*9ac4ffbdSYork Sun 
83*9ac4ffbdSYork Sun 		} else if (i == 3) {
84*9ac4ffbdSYork Sun 			out_be32(&ddr->cs3_bnds, regs->cs[i].bnds);
85*9ac4ffbdSYork Sun 			out_be32(&ddr->cs3_config, regs->cs[i].config);
86*9ac4ffbdSYork Sun 			out_be32(&ddr->cs3_config_2, regs->cs[i].config_2);
87*9ac4ffbdSYork Sun 		}
88*9ac4ffbdSYork Sun 	}
89*9ac4ffbdSYork Sun 
90*9ac4ffbdSYork Sun 	out_be32(&ddr->timing_cfg_3, regs->timing_cfg_3);
91*9ac4ffbdSYork Sun 	out_be32(&ddr->timing_cfg_0, regs->timing_cfg_0);
92*9ac4ffbdSYork Sun 	out_be32(&ddr->timing_cfg_1, regs->timing_cfg_1);
93*9ac4ffbdSYork Sun 	out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2);
94*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
95*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_mode, regs->ddr_sdram_mode);
96*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_mode_2, regs->ddr_sdram_mode_2);
97*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_mode_3, regs->ddr_sdram_mode_3);
98*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_mode_4, regs->ddr_sdram_mode_4);
99*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_mode_5, regs->ddr_sdram_mode_5);
100*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_mode_6, regs->ddr_sdram_mode_6);
101*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_mode_7, regs->ddr_sdram_mode_7);
102*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_mode_8, regs->ddr_sdram_mode_8);
103*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_md_cntl, regs->ddr_sdram_md_cntl);
104*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval);
105*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_data_init, regs->ddr_data_init);
106*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_clk_cntl, regs->ddr_sdram_clk_cntl);
107*9ac4ffbdSYork Sun 	out_be32(&ddr->init_addr, regs->ddr_init_addr);
108*9ac4ffbdSYork Sun 	out_be32(&ddr->init_ext_addr, regs->ddr_init_ext_addr);
109*9ac4ffbdSYork Sun 
110*9ac4ffbdSYork Sun 	out_be32(&ddr->timing_cfg_4, regs->timing_cfg_4);
111*9ac4ffbdSYork Sun 	out_be32(&ddr->timing_cfg_5, regs->timing_cfg_5);
112*9ac4ffbdSYork Sun 	out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
113*9ac4ffbdSYork Sun 	out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
114*9ac4ffbdSYork Sun #ifndef CONFIG_SYS_FSL_DDR_EMU
115*9ac4ffbdSYork Sun 	/*
116*9ac4ffbdSYork Sun 	 * Skip these two registers if running on emulator
117*9ac4ffbdSYork Sun 	 * because emulator doesn't have skew between bytes.
118*9ac4ffbdSYork Sun 	 */
119*9ac4ffbdSYork Sun 
120*9ac4ffbdSYork Sun 	if (regs->ddr_wrlvl_cntl_2)
121*9ac4ffbdSYork Sun 		out_be32(&ddr->ddr_wrlvl_cntl_2, regs->ddr_wrlvl_cntl_2);
122*9ac4ffbdSYork Sun 	if (regs->ddr_wrlvl_cntl_3)
123*9ac4ffbdSYork Sun 		out_be32(&ddr->ddr_wrlvl_cntl_3, regs->ddr_wrlvl_cntl_3);
124*9ac4ffbdSYork Sun #endif
125*9ac4ffbdSYork Sun 
126*9ac4ffbdSYork Sun 	out_be32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr);
127*9ac4ffbdSYork Sun 	out_be32(&ddr->ddr_sdram_rcw_1, regs->ddr_sdram_rcw_1);
128*9ac4ffbdSYork Sun 	out_be32(&ddr->ddr_sdram_rcw_2, regs->ddr_sdram_rcw_2);
129*9ac4ffbdSYork Sun 	out_be32(&ddr->ddr_cdr1, regs->ddr_cdr1);
130*9ac4ffbdSYork Sun 	out_be32(&ddr->ddr_cdr2, regs->ddr_cdr2);
131*9ac4ffbdSYork Sun 	out_be32(&ddr->err_disable, regs->err_disable);
132*9ac4ffbdSYork Sun 	out_be32(&ddr->err_int_en, regs->err_int_en);
133*9ac4ffbdSYork Sun 	for (i = 0; i < 32; i++) {
134*9ac4ffbdSYork Sun 		if (regs->debug[i]) {
135*9ac4ffbdSYork Sun 			debug("Write to debug_%d as %08x\n", i + 1,
136*9ac4ffbdSYork Sun 			      regs->debug[i]);
137*9ac4ffbdSYork Sun 			out_be32(&ddr->debug[i], regs->debug[i]);
138*9ac4ffbdSYork Sun 		}
139*9ac4ffbdSYork Sun 	}
140*9ac4ffbdSYork Sun 
141*9ac4ffbdSYork Sun 	/*
142*9ac4ffbdSYork Sun 	 * For RDIMMs, JEDEC spec requires clocks to be stable before reset is
143*9ac4ffbdSYork Sun 	 * deasserted. Clocks start when any chip select is enabled and clock
144*9ac4ffbdSYork Sun 	 * control register is set. Because all DDR components are connected to
145*9ac4ffbdSYork Sun 	 * one reset signal, this needs to be done in two steps. Step 1 is to
146*9ac4ffbdSYork Sun 	 * get the clocks started. Step 2 resumes after reset signal is
147*9ac4ffbdSYork Sun 	 * deasserted.
148*9ac4ffbdSYork Sun 	 */
149*9ac4ffbdSYork Sun 	if (step == 1) {
150*9ac4ffbdSYork Sun 		udelay(200);
151*9ac4ffbdSYork Sun 		return;
152*9ac4ffbdSYork Sun 	}
153*9ac4ffbdSYork Sun 
154*9ac4ffbdSYork Sun step2:
155*9ac4ffbdSYork Sun 	/* Set, but do not enable the memory */
156*9ac4ffbdSYork Sun 	temp_sdram_cfg = regs->ddr_sdram_cfg;
157*9ac4ffbdSYork Sun 	temp_sdram_cfg &= ~(SDRAM_CFG_MEM_EN);
158*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_cfg, temp_sdram_cfg);
159*9ac4ffbdSYork Sun 
160*9ac4ffbdSYork Sun 	/*
161*9ac4ffbdSYork Sun 	 * 500 painful micro-seconds must elapse between
162*9ac4ffbdSYork Sun 	 * the DDR clock setup and the DDR config enable.
163*9ac4ffbdSYork Sun 	 * DDR2 need 200 us, and DDR3 need 500 us from spec,
164*9ac4ffbdSYork Sun 	 * we choose the max, that is 500 us for all of case.
165*9ac4ffbdSYork Sun 	 */
166*9ac4ffbdSYork Sun 	udelay(500);
167*9ac4ffbdSYork Sun 	asm volatile("dsb sy;isb");
168*9ac4ffbdSYork Sun 
169*9ac4ffbdSYork Sun 	/* Let the controller go */
170*9ac4ffbdSYork Sun 	temp_sdram_cfg = in_be32(&ddr->sdram_cfg) & ~SDRAM_CFG_BI;
171*9ac4ffbdSYork Sun 	out_be32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_MEM_EN);
172*9ac4ffbdSYork Sun 	asm volatile("dsb sy;isb");
173*9ac4ffbdSYork Sun 
174*9ac4ffbdSYork Sun 	total_gb_size_per_controller = 0;
175*9ac4ffbdSYork Sun 	for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
176*9ac4ffbdSYork Sun 		if (!(regs->cs[i].config & 0x80000000))
177*9ac4ffbdSYork Sun 			continue;
178*9ac4ffbdSYork Sun 		total_gb_size_per_controller += 1 << (
179*9ac4ffbdSYork Sun 			((regs->cs[i].config >> 14) & 0x3) + 2 +
180*9ac4ffbdSYork Sun 			((regs->cs[i].config >> 8) & 0x7) + 12 +
181*9ac4ffbdSYork Sun 			((regs->cs[i].config >> 0) & 0x7) + 8 +
182*9ac4ffbdSYork Sun 			3 - ((regs->ddr_sdram_cfg >> 19) & 0x3) -
183*9ac4ffbdSYork Sun 			26);			/* minus 26 (count of 64M) */
184*9ac4ffbdSYork Sun 	}
185*9ac4ffbdSYork Sun 	if (regs->cs[0].config & 0x20000000) {
186*9ac4ffbdSYork Sun 		/* 2-way interleaving */
187*9ac4ffbdSYork Sun 		total_gb_size_per_controller <<= 1;
188*9ac4ffbdSYork Sun 	}
189*9ac4ffbdSYork Sun 	/*
190*9ac4ffbdSYork Sun 	 * total memory / bus width = transactions needed
191*9ac4ffbdSYork Sun 	 * transactions needed / data rate = seconds
192*9ac4ffbdSYork Sun 	 * to add plenty of buffer, double the time
193*9ac4ffbdSYork Sun 	 * For example, 2GB on 666MT/s 64-bit bus takes about 402ms
194*9ac4ffbdSYork Sun 	 * Let's wait for 800ms
195*9ac4ffbdSYork Sun 	 */
196*9ac4ffbdSYork Sun 	bus_width = 3 - ((ddr->sdram_cfg & SDRAM_CFG_DBW_MASK)
197*9ac4ffbdSYork Sun 			>> SDRAM_CFG_DBW_SHIFT);
198*9ac4ffbdSYork Sun 	timeout = ((total_gb_size_per_controller << (6 - bus_width)) * 100 /
199*9ac4ffbdSYork Sun 		(get_ddr_freq(0) >> 20)) << 1;
200*9ac4ffbdSYork Sun 	total_gb_size_per_controller >>= 4;	/* shift down to gb size */
201*9ac4ffbdSYork Sun 	debug("total %d GB\n", total_gb_size_per_controller);
202*9ac4ffbdSYork Sun 	debug("Need to wait up to %d * 10ms\n", timeout);
203*9ac4ffbdSYork Sun 
204*9ac4ffbdSYork Sun 	/* Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done.  */
205*9ac4ffbdSYork Sun 	while ((in_be32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
206*9ac4ffbdSYork Sun 		(timeout >= 0)) {
207*9ac4ffbdSYork Sun 		udelay(10000);		/* throttle polling rate */
208*9ac4ffbdSYork Sun 		timeout--;
209*9ac4ffbdSYork Sun 	}
210*9ac4ffbdSYork Sun 
211*9ac4ffbdSYork Sun 	if (timeout <= 0)
212*9ac4ffbdSYork Sun 		printf("Waiting for D_INIT timeout. Memory may not work.\n");
213*9ac4ffbdSYork Sun }
214