1*e6e505b9SAlexander Graf /*
2*e6e505b9SAlexander Graf * sunxi DRAM controller initialization
3*e6e505b9SAlexander Graf * (C) Copyright 2012 Henrik Nordstrom <henrik@henriknordstrom.net>
4*e6e505b9SAlexander Graf * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
5*e6e505b9SAlexander Graf *
6*e6e505b9SAlexander Graf * Based on sun4i Linux kernel sources mach-sunxi/pm/standby/dram*.c
7*e6e505b9SAlexander Graf * and earlier U-Boot Allwiner A10 SPL work
8*e6e505b9SAlexander Graf *
9*e6e505b9SAlexander Graf * (C) Copyright 2007-2012
10*e6e505b9SAlexander Graf * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
11*e6e505b9SAlexander Graf * Berg Xing <bergxing@allwinnertech.com>
12*e6e505b9SAlexander Graf * Tom Cubie <tangliang@allwinnertech.com>
13*e6e505b9SAlexander Graf *
14*e6e505b9SAlexander Graf * SPDX-License-Identifier: GPL-2.0+
15*e6e505b9SAlexander Graf */
16*e6e505b9SAlexander Graf
17*e6e505b9SAlexander Graf /*
18*e6e505b9SAlexander Graf * Unfortunately the only documentation we have on the sun7i DRAM
19*e6e505b9SAlexander Graf * controller is Allwinner boot0 + boot1 code, and that code uses
20*e6e505b9SAlexander Graf * magic numbers & shifts with no explanations. Hence this code is
21*e6e505b9SAlexander Graf * rather undocumented and full of magic.
22*e6e505b9SAlexander Graf */
23*e6e505b9SAlexander Graf
24*e6e505b9SAlexander Graf #include <common.h>
25*e6e505b9SAlexander Graf #include <asm/io.h>
26*e6e505b9SAlexander Graf #include <asm/arch/clock.h>
27*e6e505b9SAlexander Graf #include <asm/arch/dram.h>
28*e6e505b9SAlexander Graf #include <asm/arch/timer.h>
29*e6e505b9SAlexander Graf #include <asm/arch/sys_proto.h>
30*e6e505b9SAlexander Graf
31*e6e505b9SAlexander Graf #define CPU_CFG_CHIP_VER(n) ((n) << 6)
32*e6e505b9SAlexander Graf #define CPU_CFG_CHIP_VER_MASK CPU_CFG_CHIP_VER(0x3)
33*e6e505b9SAlexander Graf #define CPU_CFG_CHIP_REV_A 0x0
34*e6e505b9SAlexander Graf #define CPU_CFG_CHIP_REV_C1 0x1
35*e6e505b9SAlexander Graf #define CPU_CFG_CHIP_REV_C2 0x2
36*e6e505b9SAlexander Graf #define CPU_CFG_CHIP_REV_B 0x3
37*e6e505b9SAlexander Graf
38*e6e505b9SAlexander Graf /*
39*e6e505b9SAlexander Graf * Wait up to 1s for mask to be clear in given reg.
40*e6e505b9SAlexander Graf */
await_bits_clear(u32 * reg,u32 mask)41*e6e505b9SAlexander Graf static inline void await_bits_clear(u32 *reg, u32 mask)
42*e6e505b9SAlexander Graf {
43*e6e505b9SAlexander Graf mctl_await_completion(reg, mask, 0);
44*e6e505b9SAlexander Graf }
45*e6e505b9SAlexander Graf
46*e6e505b9SAlexander Graf /*
47*e6e505b9SAlexander Graf * Wait up to 1s for mask to be set in given reg.
48*e6e505b9SAlexander Graf */
await_bits_set(u32 * reg,u32 mask)49*e6e505b9SAlexander Graf static inline void await_bits_set(u32 *reg, u32 mask)
50*e6e505b9SAlexander Graf {
51*e6e505b9SAlexander Graf mctl_await_completion(reg, mask, mask);
52*e6e505b9SAlexander Graf }
53*e6e505b9SAlexander Graf
54*e6e505b9SAlexander Graf /*
55*e6e505b9SAlexander Graf * This performs the external DRAM reset by driving the RESET pin low and
56*e6e505b9SAlexander Graf * then high again. According to the DDR3 spec, the RESET pin needs to be
57*e6e505b9SAlexander Graf * kept low for at least 200 us.
58*e6e505b9SAlexander Graf */
mctl_ddr3_reset(void)59*e6e505b9SAlexander Graf static void mctl_ddr3_reset(void)
60*e6e505b9SAlexander Graf {
61*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram =
62*e6e505b9SAlexander Graf (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
63*e6e505b9SAlexander Graf
64*e6e505b9SAlexander Graf #ifdef CONFIG_MACH_SUN4I
65*e6e505b9SAlexander Graf struct sunxi_timer_reg *timer =
66*e6e505b9SAlexander Graf (struct sunxi_timer_reg *)SUNXI_TIMER_BASE;
67*e6e505b9SAlexander Graf u32 reg_val;
68*e6e505b9SAlexander Graf
69*e6e505b9SAlexander Graf writel(0, &timer->cpu_cfg);
70*e6e505b9SAlexander Graf reg_val = readl(&timer->cpu_cfg);
71*e6e505b9SAlexander Graf
72*e6e505b9SAlexander Graf if ((reg_val & CPU_CFG_CHIP_VER_MASK) !=
73*e6e505b9SAlexander Graf CPU_CFG_CHIP_VER(CPU_CFG_CHIP_REV_A)) {
74*e6e505b9SAlexander Graf setbits_le32(&dram->mcr, DRAM_MCR_RESET);
75*e6e505b9SAlexander Graf udelay(200);
76*e6e505b9SAlexander Graf clrbits_le32(&dram->mcr, DRAM_MCR_RESET);
77*e6e505b9SAlexander Graf } else
78*e6e505b9SAlexander Graf #endif
79*e6e505b9SAlexander Graf {
80*e6e505b9SAlexander Graf clrbits_le32(&dram->mcr, DRAM_MCR_RESET);
81*e6e505b9SAlexander Graf udelay(200);
82*e6e505b9SAlexander Graf setbits_le32(&dram->mcr, DRAM_MCR_RESET);
83*e6e505b9SAlexander Graf }
84*e6e505b9SAlexander Graf /* After the RESET pin is de-asserted, the DDR3 spec requires to wait
85*e6e505b9SAlexander Graf * for additional 500 us before driving the CKE pin (Clock Enable)
86*e6e505b9SAlexander Graf * high. The duration of this delay can be configured in the SDR_IDCR
87*e6e505b9SAlexander Graf * (Initialization Delay Configuration Register) and applied
88*e6e505b9SAlexander Graf * automatically by the DRAM controller during the DDR3 initialization
89*e6e505b9SAlexander Graf * step. But SDR_IDCR has limited range on sun4i/sun5i hardware and
90*e6e505b9SAlexander Graf * can't provide sufficient delay at DRAM clock frequencies higher than
91*e6e505b9SAlexander Graf * 524 MHz (while Allwinner A13 supports DRAM clock frequency up to
92*e6e505b9SAlexander Graf * 533 MHz according to the datasheet). Additionally, there is no
93*e6e505b9SAlexander Graf * official documentation for the SDR_IDCR register anywhere, and
94*e6e505b9SAlexander Graf * there is always a chance that we are interpreting it wrong.
95*e6e505b9SAlexander Graf * Better be safe than sorry, so add an explicit delay here. */
96*e6e505b9SAlexander Graf udelay(500);
97*e6e505b9SAlexander Graf }
98*e6e505b9SAlexander Graf
mctl_set_drive(void)99*e6e505b9SAlexander Graf static void mctl_set_drive(void)
100*e6e505b9SAlexander Graf {
101*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
102*e6e505b9SAlexander Graf
103*e6e505b9SAlexander Graf #ifdef CONFIG_MACH_SUN7I
104*e6e505b9SAlexander Graf clrsetbits_le32(&dram->mcr, DRAM_MCR_MODE_NORM(0x3) | (0x3 << 28),
105*e6e505b9SAlexander Graf #else
106*e6e505b9SAlexander Graf clrsetbits_le32(&dram->mcr, DRAM_MCR_MODE_NORM(0x3),
107*e6e505b9SAlexander Graf #endif
108*e6e505b9SAlexander Graf DRAM_MCR_MODE_EN(0x3) |
109*e6e505b9SAlexander Graf 0xffc);
110*e6e505b9SAlexander Graf }
111*e6e505b9SAlexander Graf
mctl_itm_disable(void)112*e6e505b9SAlexander Graf static void mctl_itm_disable(void)
113*e6e505b9SAlexander Graf {
114*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
115*e6e505b9SAlexander Graf
116*e6e505b9SAlexander Graf clrsetbits_le32(&dram->ccr, DRAM_CCR_INIT, DRAM_CCR_ITM_OFF);
117*e6e505b9SAlexander Graf }
118*e6e505b9SAlexander Graf
mctl_itm_enable(void)119*e6e505b9SAlexander Graf static void mctl_itm_enable(void)
120*e6e505b9SAlexander Graf {
121*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
122*e6e505b9SAlexander Graf
123*e6e505b9SAlexander Graf clrbits_le32(&dram->ccr, DRAM_CCR_ITM_OFF);
124*e6e505b9SAlexander Graf }
125*e6e505b9SAlexander Graf
mctl_itm_reset(void)126*e6e505b9SAlexander Graf static void mctl_itm_reset(void)
127*e6e505b9SAlexander Graf {
128*e6e505b9SAlexander Graf mctl_itm_disable();
129*e6e505b9SAlexander Graf udelay(1); /* ITM reset needs a bit of delay */
130*e6e505b9SAlexander Graf mctl_itm_enable();
131*e6e505b9SAlexander Graf udelay(1);
132*e6e505b9SAlexander Graf }
133*e6e505b9SAlexander Graf
mctl_enable_dll0(u32 phase)134*e6e505b9SAlexander Graf static void mctl_enable_dll0(u32 phase)
135*e6e505b9SAlexander Graf {
136*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
137*e6e505b9SAlexander Graf
138*e6e505b9SAlexander Graf clrsetbits_le32(&dram->dllcr[0], 0x3f << 6,
139*e6e505b9SAlexander Graf ((phase >> 16) & 0x3f) << 6);
140*e6e505b9SAlexander Graf clrsetbits_le32(&dram->dllcr[0], DRAM_DLLCR_NRESET, DRAM_DLLCR_DISABLE);
141*e6e505b9SAlexander Graf udelay(2);
142*e6e505b9SAlexander Graf
143*e6e505b9SAlexander Graf clrbits_le32(&dram->dllcr[0], DRAM_DLLCR_NRESET | DRAM_DLLCR_DISABLE);
144*e6e505b9SAlexander Graf udelay(22);
145*e6e505b9SAlexander Graf
146*e6e505b9SAlexander Graf clrsetbits_le32(&dram->dllcr[0], DRAM_DLLCR_DISABLE, DRAM_DLLCR_NRESET);
147*e6e505b9SAlexander Graf udelay(22);
148*e6e505b9SAlexander Graf }
149*e6e505b9SAlexander Graf
150*e6e505b9SAlexander Graf /* Get the number of DDR byte lanes */
mctl_get_number_of_lanes(void)151*e6e505b9SAlexander Graf static u32 mctl_get_number_of_lanes(void)
152*e6e505b9SAlexander Graf {
153*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
154*e6e505b9SAlexander Graf if ((readl(&dram->dcr) & DRAM_DCR_BUS_WIDTH_MASK) ==
155*e6e505b9SAlexander Graf DRAM_DCR_BUS_WIDTH(DRAM_DCR_BUS_WIDTH_32BIT))
156*e6e505b9SAlexander Graf return 4;
157*e6e505b9SAlexander Graf else
158*e6e505b9SAlexander Graf return 2;
159*e6e505b9SAlexander Graf }
160*e6e505b9SAlexander Graf
161*e6e505b9SAlexander Graf /*
162*e6e505b9SAlexander Graf * Note: This differs from pm/standby in that it checks the bus width
163*e6e505b9SAlexander Graf */
mctl_enable_dllx(u32 phase)164*e6e505b9SAlexander Graf static void mctl_enable_dllx(u32 phase)
165*e6e505b9SAlexander Graf {
166*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
167*e6e505b9SAlexander Graf u32 i, number_of_lanes;
168*e6e505b9SAlexander Graf
169*e6e505b9SAlexander Graf number_of_lanes = mctl_get_number_of_lanes();
170*e6e505b9SAlexander Graf
171*e6e505b9SAlexander Graf for (i = 1; i <= number_of_lanes; i++) {
172*e6e505b9SAlexander Graf clrsetbits_le32(&dram->dllcr[i], 0xf << 14,
173*e6e505b9SAlexander Graf (phase & 0xf) << 14);
174*e6e505b9SAlexander Graf clrsetbits_le32(&dram->dllcr[i], DRAM_DLLCR_NRESET,
175*e6e505b9SAlexander Graf DRAM_DLLCR_DISABLE);
176*e6e505b9SAlexander Graf phase >>= 4;
177*e6e505b9SAlexander Graf }
178*e6e505b9SAlexander Graf udelay(2);
179*e6e505b9SAlexander Graf
180*e6e505b9SAlexander Graf for (i = 1; i <= number_of_lanes; i++)
181*e6e505b9SAlexander Graf clrbits_le32(&dram->dllcr[i], DRAM_DLLCR_NRESET |
182*e6e505b9SAlexander Graf DRAM_DLLCR_DISABLE);
183*e6e505b9SAlexander Graf udelay(22);
184*e6e505b9SAlexander Graf
185*e6e505b9SAlexander Graf for (i = 1; i <= number_of_lanes; i++)
186*e6e505b9SAlexander Graf clrsetbits_le32(&dram->dllcr[i], DRAM_DLLCR_DISABLE,
187*e6e505b9SAlexander Graf DRAM_DLLCR_NRESET);
188*e6e505b9SAlexander Graf udelay(22);
189*e6e505b9SAlexander Graf }
190*e6e505b9SAlexander Graf
191*e6e505b9SAlexander Graf static u32 hpcr_value[32] = {
192*e6e505b9SAlexander Graf #ifdef CONFIG_MACH_SUN5I
193*e6e505b9SAlexander Graf 0, 0, 0, 0,
194*e6e505b9SAlexander Graf 0, 0, 0, 0,
195*e6e505b9SAlexander Graf 0, 0, 0, 0,
196*e6e505b9SAlexander Graf 0, 0, 0, 0,
197*e6e505b9SAlexander Graf 0x1031, 0x1031, 0x0735, 0x1035,
198*e6e505b9SAlexander Graf 0x1035, 0x0731, 0x1031, 0,
199*e6e505b9SAlexander Graf 0x0301, 0x0301, 0x0301, 0x0301,
200*e6e505b9SAlexander Graf 0x0301, 0x0301, 0x0301, 0
201*e6e505b9SAlexander Graf #endif
202*e6e505b9SAlexander Graf #ifdef CONFIG_MACH_SUN4I
203*e6e505b9SAlexander Graf 0x0301, 0x0301, 0x0301, 0x0301,
204*e6e505b9SAlexander Graf 0x0301, 0x0301, 0, 0,
205*e6e505b9SAlexander Graf 0, 0, 0, 0,
206*e6e505b9SAlexander Graf 0, 0, 0, 0,
207*e6e505b9SAlexander Graf 0x1031, 0x1031, 0x0735, 0x5031,
208*e6e505b9SAlexander Graf 0x1035, 0x0731, 0x1031, 0x0735,
209*e6e505b9SAlexander Graf 0x1035, 0x1031, 0x0731, 0x1035,
210*e6e505b9SAlexander Graf 0x1031, 0x0301, 0x0301, 0x0731
211*e6e505b9SAlexander Graf #endif
212*e6e505b9SAlexander Graf #ifdef CONFIG_MACH_SUN7I
213*e6e505b9SAlexander Graf 0x0301, 0x0301, 0x0301, 0x0301,
214*e6e505b9SAlexander Graf 0x0301, 0x0301, 0x0301, 0x0301,
215*e6e505b9SAlexander Graf 0, 0, 0, 0,
216*e6e505b9SAlexander Graf 0, 0, 0, 0,
217*e6e505b9SAlexander Graf 0x1031, 0x1031, 0x0735, 0x1035,
218*e6e505b9SAlexander Graf 0x1035, 0x0731, 0x1031, 0x0735,
219*e6e505b9SAlexander Graf 0x1035, 0x1031, 0x0731, 0x1035,
220*e6e505b9SAlexander Graf 0x0001, 0x1031, 0, 0x1031
221*e6e505b9SAlexander Graf /* last row differs from boot0 source table
222*e6e505b9SAlexander Graf * 0x1031, 0x0301, 0x0301, 0x0731
223*e6e505b9SAlexander Graf * but boot0 code skips #28 and #30, and sets #29 and #31 to the
224*e6e505b9SAlexander Graf * value from #28 entry (0x1031)
225*e6e505b9SAlexander Graf */
226*e6e505b9SAlexander Graf #endif
227*e6e505b9SAlexander Graf };
228*e6e505b9SAlexander Graf
mctl_configure_hostport(void)229*e6e505b9SAlexander Graf static void mctl_configure_hostport(void)
230*e6e505b9SAlexander Graf {
231*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
232*e6e505b9SAlexander Graf u32 i;
233*e6e505b9SAlexander Graf
234*e6e505b9SAlexander Graf for (i = 0; i < 32; i++)
235*e6e505b9SAlexander Graf writel(hpcr_value[i], &dram->hpcr[i]);
236*e6e505b9SAlexander Graf }
237*e6e505b9SAlexander Graf
mctl_setup_dram_clock(u32 clk,u32 mbus_clk)238*e6e505b9SAlexander Graf static void mctl_setup_dram_clock(u32 clk, u32 mbus_clk)
239*e6e505b9SAlexander Graf {
240*e6e505b9SAlexander Graf u32 reg_val;
241*e6e505b9SAlexander Graf struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
242*e6e505b9SAlexander Graf u32 pll5p_clk, pll6x_clk;
243*e6e505b9SAlexander Graf u32 pll5p_div, pll6x_div;
244*e6e505b9SAlexander Graf u32 pll5p_rate, pll6x_rate;
245*e6e505b9SAlexander Graf
246*e6e505b9SAlexander Graf /* setup DRAM PLL */
247*e6e505b9SAlexander Graf reg_val = readl(&ccm->pll5_cfg);
248*e6e505b9SAlexander Graf reg_val &= ~CCM_PLL5_CTRL_M_MASK; /* set M to 0 (x1) */
249*e6e505b9SAlexander Graf reg_val &= ~CCM_PLL5_CTRL_K_MASK; /* set K to 0 (x1) */
250*e6e505b9SAlexander Graf reg_val &= ~CCM_PLL5_CTRL_N_MASK; /* set N to 0 (x0) */
251*e6e505b9SAlexander Graf reg_val &= ~CCM_PLL5_CTRL_P_MASK; /* set P to 0 (x1) */
252*e6e505b9SAlexander Graf #ifdef CONFIG_OLD_SUNXI_KERNEL_COMPAT
253*e6e505b9SAlexander Graf /* Old kernels are hardcoded to P=1 (divide by 2) */
254*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_P(1);
255*e6e505b9SAlexander Graf #endif
256*e6e505b9SAlexander Graf if (clk >= 540 && clk < 552) {
257*e6e505b9SAlexander Graf /* dram = 540MHz */
258*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_M(CCM_PLL5_CTRL_M_X(2));
259*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_K(CCM_PLL5_CTRL_K_X(3));
260*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_N(CCM_PLL5_CTRL_N_X(15));
261*e6e505b9SAlexander Graf } else if (clk >= 512 && clk < 528) {
262*e6e505b9SAlexander Graf /* dram = 512MHz */
263*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_M(CCM_PLL5_CTRL_M_X(3));
264*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_K(CCM_PLL5_CTRL_K_X(4));
265*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_N(CCM_PLL5_CTRL_N_X(16));
266*e6e505b9SAlexander Graf } else if (clk >= 496 && clk < 504) {
267*e6e505b9SAlexander Graf /* dram = 496MHz */
268*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_M(CCM_PLL5_CTRL_M_X(3));
269*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_K(CCM_PLL5_CTRL_K_X(2));
270*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_N(CCM_PLL5_CTRL_N_X(31));
271*e6e505b9SAlexander Graf } else if (clk >= 468 && clk < 480) {
272*e6e505b9SAlexander Graf /* dram = 468MHz */
273*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_M(CCM_PLL5_CTRL_M_X(2));
274*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_K(CCM_PLL5_CTRL_K_X(3));
275*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_N(CCM_PLL5_CTRL_N_X(13));
276*e6e505b9SAlexander Graf } else if (clk >= 396 && clk < 408) {
277*e6e505b9SAlexander Graf /* dram = 396MHz */
278*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_M(CCM_PLL5_CTRL_M_X(2));
279*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_K(CCM_PLL5_CTRL_K_X(3));
280*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_N(CCM_PLL5_CTRL_N_X(11));
281*e6e505b9SAlexander Graf } else {
282*e6e505b9SAlexander Graf /* any other frequency that is a multiple of 24 */
283*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_M(CCM_PLL5_CTRL_M_X(2));
284*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_K(CCM_PLL5_CTRL_K_X(2));
285*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_N(CCM_PLL5_CTRL_N_X(clk / 24));
286*e6e505b9SAlexander Graf }
287*e6e505b9SAlexander Graf reg_val &= ~CCM_PLL5_CTRL_VCO_GAIN; /* PLL VCO Gain off */
288*e6e505b9SAlexander Graf reg_val |= CCM_PLL5_CTRL_EN; /* PLL On */
289*e6e505b9SAlexander Graf writel(reg_val, &ccm->pll5_cfg);
290*e6e505b9SAlexander Graf udelay(5500);
291*e6e505b9SAlexander Graf
292*e6e505b9SAlexander Graf setbits_le32(&ccm->pll5_cfg, CCM_PLL5_CTRL_DDR_CLK);
293*e6e505b9SAlexander Graf
294*e6e505b9SAlexander Graf #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
295*e6e505b9SAlexander Graf /* reset GPS */
296*e6e505b9SAlexander Graf clrbits_le32(&ccm->gps_clk_cfg, CCM_GPS_CTRL_RESET | CCM_GPS_CTRL_GATE);
297*e6e505b9SAlexander Graf setbits_le32(&ccm->ahb_gate0, CCM_AHB_GATE_GPS);
298*e6e505b9SAlexander Graf udelay(1);
299*e6e505b9SAlexander Graf clrbits_le32(&ccm->ahb_gate0, CCM_AHB_GATE_GPS);
300*e6e505b9SAlexander Graf #endif
301*e6e505b9SAlexander Graf
302*e6e505b9SAlexander Graf /* setup MBUS clock */
303*e6e505b9SAlexander Graf if (!mbus_clk)
304*e6e505b9SAlexander Graf mbus_clk = 300;
305*e6e505b9SAlexander Graf
306*e6e505b9SAlexander Graf /* PLL5P and PLL6 are the potential clock sources for MBUS */
307*e6e505b9SAlexander Graf pll6x_clk = clock_get_pll6() / 1000000;
308*e6e505b9SAlexander Graf #ifdef CONFIG_MACH_SUN7I
309*e6e505b9SAlexander Graf pll6x_clk *= 2; /* sun7i uses PLL6*2, sun5i uses just PLL6 */
310*e6e505b9SAlexander Graf #endif
311*e6e505b9SAlexander Graf pll5p_clk = clock_get_pll5p() / 1000000;
312*e6e505b9SAlexander Graf pll6x_div = DIV_ROUND_UP(pll6x_clk, mbus_clk);
313*e6e505b9SAlexander Graf pll5p_div = DIV_ROUND_UP(pll5p_clk, mbus_clk);
314*e6e505b9SAlexander Graf pll6x_rate = pll6x_clk / pll6x_div;
315*e6e505b9SAlexander Graf pll5p_rate = pll5p_clk / pll5p_div;
316*e6e505b9SAlexander Graf
317*e6e505b9SAlexander Graf if (pll6x_div <= 16 && pll6x_rate > pll5p_rate) {
318*e6e505b9SAlexander Graf /* use PLL6 as the MBUS clock source */
319*e6e505b9SAlexander Graf reg_val = CCM_MBUS_CTRL_GATE |
320*e6e505b9SAlexander Graf CCM_MBUS_CTRL_CLK_SRC(CCM_MBUS_CTRL_CLK_SRC_PLL6) |
321*e6e505b9SAlexander Graf CCM_MBUS_CTRL_N(CCM_MBUS_CTRL_N_X(1)) |
322*e6e505b9SAlexander Graf CCM_MBUS_CTRL_M(CCM_MBUS_CTRL_M_X(pll6x_div));
323*e6e505b9SAlexander Graf } else if (pll5p_div <= 16) {
324*e6e505b9SAlexander Graf /* use PLL5P as the MBUS clock source */
325*e6e505b9SAlexander Graf reg_val = CCM_MBUS_CTRL_GATE |
326*e6e505b9SAlexander Graf CCM_MBUS_CTRL_CLK_SRC(CCM_MBUS_CTRL_CLK_SRC_PLL5) |
327*e6e505b9SAlexander Graf CCM_MBUS_CTRL_N(CCM_MBUS_CTRL_N_X(1)) |
328*e6e505b9SAlexander Graf CCM_MBUS_CTRL_M(CCM_MBUS_CTRL_M_X(pll5p_div));
329*e6e505b9SAlexander Graf } else {
330*e6e505b9SAlexander Graf panic("Bad mbus_clk\n");
331*e6e505b9SAlexander Graf }
332*e6e505b9SAlexander Graf writel(reg_val, &ccm->mbus_clk_cfg);
333*e6e505b9SAlexander Graf
334*e6e505b9SAlexander Graf /*
335*e6e505b9SAlexander Graf * open DRAMC AHB & DLL register clock
336*e6e505b9SAlexander Graf * close it first
337*e6e505b9SAlexander Graf */
338*e6e505b9SAlexander Graf #if defined(CONFIG_MACH_SUN5I) || defined(CONFIG_MACH_SUN7I)
339*e6e505b9SAlexander Graf clrbits_le32(&ccm->ahb_gate0, CCM_AHB_GATE_SDRAM | CCM_AHB_GATE_DLL);
340*e6e505b9SAlexander Graf #else
341*e6e505b9SAlexander Graf clrbits_le32(&ccm->ahb_gate0, CCM_AHB_GATE_SDRAM);
342*e6e505b9SAlexander Graf #endif
343*e6e505b9SAlexander Graf udelay(22);
344*e6e505b9SAlexander Graf
345*e6e505b9SAlexander Graf /* then open it */
346*e6e505b9SAlexander Graf #if defined(CONFIG_MACH_SUN5I) || defined(CONFIG_MACH_SUN7I)
347*e6e505b9SAlexander Graf setbits_le32(&ccm->ahb_gate0, CCM_AHB_GATE_SDRAM | CCM_AHB_GATE_DLL);
348*e6e505b9SAlexander Graf #else
349*e6e505b9SAlexander Graf setbits_le32(&ccm->ahb_gate0, CCM_AHB_GATE_SDRAM);
350*e6e505b9SAlexander Graf #endif
351*e6e505b9SAlexander Graf udelay(22);
352*e6e505b9SAlexander Graf }
353*e6e505b9SAlexander Graf
354*e6e505b9SAlexander Graf /*
355*e6e505b9SAlexander Graf * The data from rslrX and rdgrX registers (X=rank) is stored
356*e6e505b9SAlexander Graf * in a single 32-bit value using the following format:
357*e6e505b9SAlexander Graf * bits [31:26] - DQS gating system latency for byte lane 3
358*e6e505b9SAlexander Graf * bits [25:24] - DQS gating phase select for byte lane 3
359*e6e505b9SAlexander Graf * bits [23:18] - DQS gating system latency for byte lane 2
360*e6e505b9SAlexander Graf * bits [17:16] - DQS gating phase select for byte lane 2
361*e6e505b9SAlexander Graf * bits [15:10] - DQS gating system latency for byte lane 1
362*e6e505b9SAlexander Graf * bits [ 9:8 ] - DQS gating phase select for byte lane 1
363*e6e505b9SAlexander Graf * bits [ 7:2 ] - DQS gating system latency for byte lane 0
364*e6e505b9SAlexander Graf * bits [ 1:0 ] - DQS gating phase select for byte lane 0
365*e6e505b9SAlexander Graf */
mctl_set_dqs_gating_delay(int rank,u32 dqs_gating_delay)366*e6e505b9SAlexander Graf static void mctl_set_dqs_gating_delay(int rank, u32 dqs_gating_delay)
367*e6e505b9SAlexander Graf {
368*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
369*e6e505b9SAlexander Graf u32 lane, number_of_lanes = mctl_get_number_of_lanes();
370*e6e505b9SAlexander Graf /* rank0 gating system latency (3 bits per lane: cycles) */
371*e6e505b9SAlexander Graf u32 slr = readl(rank == 0 ? &dram->rslr0 : &dram->rslr1);
372*e6e505b9SAlexander Graf /* rank0 gating phase select (2 bits per lane: 90, 180, 270, 360) */
373*e6e505b9SAlexander Graf u32 dgr = readl(rank == 0 ? &dram->rdgr0 : &dram->rdgr1);
374*e6e505b9SAlexander Graf for (lane = 0; lane < number_of_lanes; lane++) {
375*e6e505b9SAlexander Graf u32 tmp = dqs_gating_delay >> (lane * 8);
376*e6e505b9SAlexander Graf slr &= ~(7 << (lane * 3));
377*e6e505b9SAlexander Graf slr |= ((tmp >> 2) & 7) << (lane * 3);
378*e6e505b9SAlexander Graf dgr &= ~(3 << (lane * 2));
379*e6e505b9SAlexander Graf dgr |= (tmp & 3) << (lane * 2);
380*e6e505b9SAlexander Graf }
381*e6e505b9SAlexander Graf writel(slr, rank == 0 ? &dram->rslr0 : &dram->rslr1);
382*e6e505b9SAlexander Graf writel(dgr, rank == 0 ? &dram->rdgr0 : &dram->rdgr1);
383*e6e505b9SAlexander Graf }
384*e6e505b9SAlexander Graf
dramc_scan_readpipe(void)385*e6e505b9SAlexander Graf static int dramc_scan_readpipe(void)
386*e6e505b9SAlexander Graf {
387*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
388*e6e505b9SAlexander Graf u32 reg_val;
389*e6e505b9SAlexander Graf
390*e6e505b9SAlexander Graf /* data training trigger */
391*e6e505b9SAlexander Graf clrbits_le32(&dram->csr, DRAM_CSR_FAILED);
392*e6e505b9SAlexander Graf setbits_le32(&dram->ccr, DRAM_CCR_DATA_TRAINING);
393*e6e505b9SAlexander Graf
394*e6e505b9SAlexander Graf /* check whether data training process has completed */
395*e6e505b9SAlexander Graf await_bits_clear(&dram->ccr, DRAM_CCR_DATA_TRAINING);
396*e6e505b9SAlexander Graf
397*e6e505b9SAlexander Graf /* check data training result */
398*e6e505b9SAlexander Graf reg_val = readl(&dram->csr);
399*e6e505b9SAlexander Graf if (reg_val & DRAM_CSR_FAILED)
400*e6e505b9SAlexander Graf return -1;
401*e6e505b9SAlexander Graf
402*e6e505b9SAlexander Graf return 0;
403*e6e505b9SAlexander Graf }
404*e6e505b9SAlexander Graf
dramc_clock_output_en(u32 on)405*e6e505b9SAlexander Graf static void dramc_clock_output_en(u32 on)
406*e6e505b9SAlexander Graf {
407*e6e505b9SAlexander Graf #if defined(CONFIG_MACH_SUN5I) || defined(CONFIG_MACH_SUN7I)
408*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
409*e6e505b9SAlexander Graf
410*e6e505b9SAlexander Graf if (on)
411*e6e505b9SAlexander Graf setbits_le32(&dram->mcr, DRAM_MCR_DCLK_OUT);
412*e6e505b9SAlexander Graf else
413*e6e505b9SAlexander Graf clrbits_le32(&dram->mcr, DRAM_MCR_DCLK_OUT);
414*e6e505b9SAlexander Graf #endif
415*e6e505b9SAlexander Graf #ifdef CONFIG_MACH_SUN4I
416*e6e505b9SAlexander Graf struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
417*e6e505b9SAlexander Graf if (on)
418*e6e505b9SAlexander Graf setbits_le32(&ccm->dram_clk_gate, CCM_DRAM_CTRL_DCLK_OUT);
419*e6e505b9SAlexander Graf else
420*e6e505b9SAlexander Graf clrbits_le32(&ccm->dram_clk_gate, CCM_DRAM_CTRL_DCLK_OUT);
421*e6e505b9SAlexander Graf #endif
422*e6e505b9SAlexander Graf }
423*e6e505b9SAlexander Graf
424*e6e505b9SAlexander Graf /* tRFC in nanoseconds for different densities (from the DDR3 spec) */
425*e6e505b9SAlexander Graf static const u16 tRFC_DDR3_table[6] = {
426*e6e505b9SAlexander Graf /* 256Mb 512Mb 1Gb 2Gb 4Gb 8Gb */
427*e6e505b9SAlexander Graf 90, 90, 110, 160, 300, 350
428*e6e505b9SAlexander Graf };
429*e6e505b9SAlexander Graf
dramc_set_autorefresh_cycle(u32 clk,u32 density)430*e6e505b9SAlexander Graf static void dramc_set_autorefresh_cycle(u32 clk, u32 density)
431*e6e505b9SAlexander Graf {
432*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
433*e6e505b9SAlexander Graf u32 tRFC, tREFI;
434*e6e505b9SAlexander Graf
435*e6e505b9SAlexander Graf tRFC = (tRFC_DDR3_table[density] * clk + 999) / 1000;
436*e6e505b9SAlexander Graf tREFI = (7987 * clk) >> 10; /* <= 7.8us */
437*e6e505b9SAlexander Graf
438*e6e505b9SAlexander Graf writel(DRAM_DRR_TREFI(tREFI) | DRAM_DRR_TRFC(tRFC), &dram->drr);
439*e6e505b9SAlexander Graf }
440*e6e505b9SAlexander Graf
441*e6e505b9SAlexander Graf /* Calculate the value for A11, A10, A9 bits in MR0 (write recovery) */
ddr3_write_recovery(u32 clk)442*e6e505b9SAlexander Graf static u32 ddr3_write_recovery(u32 clk)
443*e6e505b9SAlexander Graf {
444*e6e505b9SAlexander Graf u32 twr_ns = 15; /* DDR3 spec says that it is 15ns for all speed bins */
445*e6e505b9SAlexander Graf u32 twr_ck = (twr_ns * clk + 999) / 1000;
446*e6e505b9SAlexander Graf if (twr_ck < 5)
447*e6e505b9SAlexander Graf return 1;
448*e6e505b9SAlexander Graf else if (twr_ck <= 8)
449*e6e505b9SAlexander Graf return twr_ck - 4;
450*e6e505b9SAlexander Graf else if (twr_ck <= 10)
451*e6e505b9SAlexander Graf return 5;
452*e6e505b9SAlexander Graf else
453*e6e505b9SAlexander Graf return 6;
454*e6e505b9SAlexander Graf }
455*e6e505b9SAlexander Graf
456*e6e505b9SAlexander Graf /*
457*e6e505b9SAlexander Graf * If the dram->ppwrsctl (SDR_DPCR) register has the lowest bit set to 1, this
458*e6e505b9SAlexander Graf * means that DRAM is currently in self-refresh mode and retaining the old
459*e6e505b9SAlexander Graf * data. Since we have no idea what to do in this situation yet, just set this
460*e6e505b9SAlexander Graf * register to 0 and initialize DRAM in the same way as on any normal reboot
461*e6e505b9SAlexander Graf * (discarding whatever was stored there).
462*e6e505b9SAlexander Graf *
463*e6e505b9SAlexander Graf * Note: on sun7i hardware, the highest 16 bits need to be set to 0x1651 magic
464*e6e505b9SAlexander Graf * value for this write operation to have any effect. On sun5i hadware this
465*e6e505b9SAlexander Graf * magic value is not necessary. And on sun4i hardware the writes to this
466*e6e505b9SAlexander Graf * register seem to have no effect at all.
467*e6e505b9SAlexander Graf */
mctl_disable_power_save(void)468*e6e505b9SAlexander Graf static void mctl_disable_power_save(void)
469*e6e505b9SAlexander Graf {
470*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
471*e6e505b9SAlexander Graf writel(0x16510000, &dram->ppwrsctl);
472*e6e505b9SAlexander Graf }
473*e6e505b9SAlexander Graf
474*e6e505b9SAlexander Graf /*
475*e6e505b9SAlexander Graf * After the DRAM is powered up or reset, the DDR3 spec requires to wait at
476*e6e505b9SAlexander Graf * least 500 us before driving the CKE pin (Clock Enable) high. The dram->idct
477*e6e505b9SAlexander Graf * (SDR_IDCR) register appears to configure this delay, which gets applied
478*e6e505b9SAlexander Graf * right at the time when the DRAM initialization is activated in the
479*e6e505b9SAlexander Graf * 'mctl_ddr3_initialize' function.
480*e6e505b9SAlexander Graf */
mctl_set_cke_delay(void)481*e6e505b9SAlexander Graf static void mctl_set_cke_delay(void)
482*e6e505b9SAlexander Graf {
483*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
484*e6e505b9SAlexander Graf
485*e6e505b9SAlexander Graf /* The CKE delay is represented in DRAM clock cycles, multiplied by N
486*e6e505b9SAlexander Graf * (where N=2 for sun4i/sun5i and N=3 for sun7i). Here it is set to
487*e6e505b9SAlexander Graf * the maximum possible value 0x1ffff, just like in the Allwinner's
488*e6e505b9SAlexander Graf * boot0 bootloader. The resulting delay value is somewhere between
489*e6e505b9SAlexander Graf * ~0.4 ms (sun5i with 648 MHz DRAM clock speed) and ~1.1 ms (sun7i
490*e6e505b9SAlexander Graf * with 360 MHz DRAM clock speed). */
491*e6e505b9SAlexander Graf setbits_le32(&dram->idcr, 0x1ffff);
492*e6e505b9SAlexander Graf }
493*e6e505b9SAlexander Graf
494*e6e505b9SAlexander Graf /*
495*e6e505b9SAlexander Graf * This triggers the DRAM initialization. It performs sending the mode registers
496*e6e505b9SAlexander Graf * to the DRAM among other things. Very likely the ZQCL command is also getting
497*e6e505b9SAlexander Graf * executed (to do the initial impedance calibration on the DRAM side of the
498*e6e505b9SAlexander Graf * wire). The memory controller and the PHY must be already configured before
499*e6e505b9SAlexander Graf * calling this function.
500*e6e505b9SAlexander Graf */
mctl_ddr3_initialize(void)501*e6e505b9SAlexander Graf static void mctl_ddr3_initialize(void)
502*e6e505b9SAlexander Graf {
503*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
504*e6e505b9SAlexander Graf setbits_le32(&dram->ccr, DRAM_CCR_INIT);
505*e6e505b9SAlexander Graf await_bits_clear(&dram->ccr, DRAM_CCR_INIT);
506*e6e505b9SAlexander Graf }
507*e6e505b9SAlexander Graf
508*e6e505b9SAlexander Graf /*
509*e6e505b9SAlexander Graf * Perform impedance calibration on the DRAM controller side of the wire.
510*e6e505b9SAlexander Graf */
mctl_set_impedance(u32 zq,bool odt_en)511*e6e505b9SAlexander Graf static void mctl_set_impedance(u32 zq, bool odt_en)
512*e6e505b9SAlexander Graf {
513*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
514*e6e505b9SAlexander Graf u32 reg_val;
515*e6e505b9SAlexander Graf u32 zprog = zq & 0xFF, zdata = (zq >> 8) & 0xFFFFF;
516*e6e505b9SAlexander Graf
517*e6e505b9SAlexander Graf #ifndef CONFIG_MACH_SUN7I
518*e6e505b9SAlexander Graf /* Appears that some kind of automatically initiated default
519*e6e505b9SAlexander Graf * ZQ calibration is already in progress at this point on sun4i/sun5i
520*e6e505b9SAlexander Graf * hardware, but not on sun7i. So it is reasonable to wait for its
521*e6e505b9SAlexander Graf * completion before doing anything else. */
522*e6e505b9SAlexander Graf await_bits_set(&dram->zqsr, DRAM_ZQSR_ZDONE);
523*e6e505b9SAlexander Graf #endif
524*e6e505b9SAlexander Graf
525*e6e505b9SAlexander Graf /* ZQ calibration is not really useful unless ODT is enabled */
526*e6e505b9SAlexander Graf if (!odt_en)
527*e6e505b9SAlexander Graf return;
528*e6e505b9SAlexander Graf
529*e6e505b9SAlexander Graf #ifdef CONFIG_MACH_SUN7I
530*e6e505b9SAlexander Graf /* Enabling ODT in SDR_IOCR on sun7i hardware results in a deadlock
531*e6e505b9SAlexander Graf * unless bit 24 is set in SDR_ZQCR1. Not much is known about the
532*e6e505b9SAlexander Graf * SDR_ZQCR1 register, but there are hints indicating that it might
533*e6e505b9SAlexander Graf * be related to periodic impedance re-calibration. This particular
534*e6e505b9SAlexander Graf * magic value is borrowed from the Allwinner boot0 bootloader, and
535*e6e505b9SAlexander Graf * using it helps to avoid troubles */
536*e6e505b9SAlexander Graf writel((1 << 24) | (1 << 1), &dram->zqcr1);
537*e6e505b9SAlexander Graf #endif
538*e6e505b9SAlexander Graf
539*e6e505b9SAlexander Graf /* Needed at least for sun5i, because it does not self clear there */
540*e6e505b9SAlexander Graf clrbits_le32(&dram->zqcr0, DRAM_ZQCR0_ZCAL);
541*e6e505b9SAlexander Graf
542*e6e505b9SAlexander Graf if (zdata) {
543*e6e505b9SAlexander Graf /* Set the user supplied impedance data */
544*e6e505b9SAlexander Graf reg_val = DRAM_ZQCR0_ZDEN | zdata;
545*e6e505b9SAlexander Graf writel(reg_val, &dram->zqcr0);
546*e6e505b9SAlexander Graf /* no need to wait, this takes effect immediately */
547*e6e505b9SAlexander Graf } else {
548*e6e505b9SAlexander Graf /* Do the calibration using the external resistor */
549*e6e505b9SAlexander Graf reg_val = DRAM_ZQCR0_ZCAL | DRAM_ZQCR0_IMP_DIV(zprog);
550*e6e505b9SAlexander Graf writel(reg_val, &dram->zqcr0);
551*e6e505b9SAlexander Graf /* Wait for the new impedance configuration to settle */
552*e6e505b9SAlexander Graf await_bits_set(&dram->zqsr, DRAM_ZQSR_ZDONE);
553*e6e505b9SAlexander Graf }
554*e6e505b9SAlexander Graf
555*e6e505b9SAlexander Graf /* Needed at least for sun5i, because it does not self clear there */
556*e6e505b9SAlexander Graf clrbits_le32(&dram->zqcr0, DRAM_ZQCR0_ZCAL);
557*e6e505b9SAlexander Graf
558*e6e505b9SAlexander Graf /* Set I/O configure register */
559*e6e505b9SAlexander Graf writel(DRAM_IOCR_ODT_EN, &dram->iocr);
560*e6e505b9SAlexander Graf }
561*e6e505b9SAlexander Graf
dramc_init_helper(struct dram_para * para)562*e6e505b9SAlexander Graf static unsigned long dramc_init_helper(struct dram_para *para)
563*e6e505b9SAlexander Graf {
564*e6e505b9SAlexander Graf struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
565*e6e505b9SAlexander Graf u32 reg_val;
566*e6e505b9SAlexander Graf u32 density;
567*e6e505b9SAlexander Graf int ret_val;
568*e6e505b9SAlexander Graf
569*e6e505b9SAlexander Graf /*
570*e6e505b9SAlexander Graf * only single rank DDR3 is supported by this code even though the
571*e6e505b9SAlexander Graf * hardware can theoretically support DDR2 and up to two ranks
572*e6e505b9SAlexander Graf */
573*e6e505b9SAlexander Graf if (para->type != DRAM_MEMORY_TYPE_DDR3 || para->rank_num != 1)
574*e6e505b9SAlexander Graf return 0;
575*e6e505b9SAlexander Graf
576*e6e505b9SAlexander Graf /* setup DRAM relative clock */
577*e6e505b9SAlexander Graf mctl_setup_dram_clock(para->clock, para->mbus_clock);
578*e6e505b9SAlexander Graf
579*e6e505b9SAlexander Graf /* Disable any pad power save control */
580*e6e505b9SAlexander Graf mctl_disable_power_save();
581*e6e505b9SAlexander Graf
582*e6e505b9SAlexander Graf mctl_set_drive();
583*e6e505b9SAlexander Graf
584*e6e505b9SAlexander Graf /* dram clock off */
585*e6e505b9SAlexander Graf dramc_clock_output_en(0);
586*e6e505b9SAlexander Graf
587*e6e505b9SAlexander Graf #ifdef CONFIG_MACH_SUN4I
588*e6e505b9SAlexander Graf /* select dram controller 1 */
589*e6e505b9SAlexander Graf writel(DRAM_CSEL_MAGIC, &dram->csel);
590*e6e505b9SAlexander Graf #endif
591*e6e505b9SAlexander Graf
592*e6e505b9SAlexander Graf mctl_itm_disable();
593*e6e505b9SAlexander Graf mctl_enable_dll0(para->tpr3);
594*e6e505b9SAlexander Graf
595*e6e505b9SAlexander Graf /* configure external DRAM */
596*e6e505b9SAlexander Graf reg_val = DRAM_DCR_TYPE_DDR3;
597*e6e505b9SAlexander Graf reg_val |= DRAM_DCR_IO_WIDTH(para->io_width >> 3);
598*e6e505b9SAlexander Graf
599*e6e505b9SAlexander Graf if (para->density == 256)
600*e6e505b9SAlexander Graf density = DRAM_DCR_CHIP_DENSITY_256M;
601*e6e505b9SAlexander Graf else if (para->density == 512)
602*e6e505b9SAlexander Graf density = DRAM_DCR_CHIP_DENSITY_512M;
603*e6e505b9SAlexander Graf else if (para->density == 1024)
604*e6e505b9SAlexander Graf density = DRAM_DCR_CHIP_DENSITY_1024M;
605*e6e505b9SAlexander Graf else if (para->density == 2048)
606*e6e505b9SAlexander Graf density = DRAM_DCR_CHIP_DENSITY_2048M;
607*e6e505b9SAlexander Graf else if (para->density == 4096)
608*e6e505b9SAlexander Graf density = DRAM_DCR_CHIP_DENSITY_4096M;
609*e6e505b9SAlexander Graf else if (para->density == 8192)
610*e6e505b9SAlexander Graf density = DRAM_DCR_CHIP_DENSITY_8192M;
611*e6e505b9SAlexander Graf else
612*e6e505b9SAlexander Graf density = DRAM_DCR_CHIP_DENSITY_256M;
613*e6e505b9SAlexander Graf
614*e6e505b9SAlexander Graf reg_val |= DRAM_DCR_CHIP_DENSITY(density);
615*e6e505b9SAlexander Graf reg_val |= DRAM_DCR_BUS_WIDTH((para->bus_width >> 3) - 1);
616*e6e505b9SAlexander Graf reg_val |= DRAM_DCR_RANK_SEL(para->rank_num - 1);
617*e6e505b9SAlexander Graf reg_val |= DRAM_DCR_CMD_RANK_ALL;
618*e6e505b9SAlexander Graf reg_val |= DRAM_DCR_MODE(DRAM_DCR_MODE_INTERLEAVE);
619*e6e505b9SAlexander Graf writel(reg_val, &dram->dcr);
620*e6e505b9SAlexander Graf
621*e6e505b9SAlexander Graf dramc_clock_output_en(1);
622*e6e505b9SAlexander Graf
623*e6e505b9SAlexander Graf mctl_set_impedance(para->zq, para->odt_en);
624*e6e505b9SAlexander Graf
625*e6e505b9SAlexander Graf mctl_set_cke_delay();
626*e6e505b9SAlexander Graf
627*e6e505b9SAlexander Graf mctl_ddr3_reset();
628*e6e505b9SAlexander Graf
629*e6e505b9SAlexander Graf udelay(1);
630*e6e505b9SAlexander Graf
631*e6e505b9SAlexander Graf await_bits_clear(&dram->ccr, DRAM_CCR_INIT);
632*e6e505b9SAlexander Graf
633*e6e505b9SAlexander Graf mctl_enable_dllx(para->tpr3);
634*e6e505b9SAlexander Graf
635*e6e505b9SAlexander Graf /* set refresh period */
636*e6e505b9SAlexander Graf dramc_set_autorefresh_cycle(para->clock, density);
637*e6e505b9SAlexander Graf
638*e6e505b9SAlexander Graf /* set timing parameters */
639*e6e505b9SAlexander Graf writel(para->tpr0, &dram->tpr0);
640*e6e505b9SAlexander Graf writel(para->tpr1, &dram->tpr1);
641*e6e505b9SAlexander Graf writel(para->tpr2, &dram->tpr2);
642*e6e505b9SAlexander Graf
643*e6e505b9SAlexander Graf reg_val = DRAM_MR_BURST_LENGTH(0x0);
644*e6e505b9SAlexander Graf #if (defined(CONFIG_MACH_SUN5I) || defined(CONFIG_MACH_SUN7I))
645*e6e505b9SAlexander Graf reg_val |= DRAM_MR_POWER_DOWN;
646*e6e505b9SAlexander Graf #endif
647*e6e505b9SAlexander Graf reg_val |= DRAM_MR_CAS_LAT(para->cas - 4);
648*e6e505b9SAlexander Graf reg_val |= DRAM_MR_WRITE_RECOVERY(ddr3_write_recovery(para->clock));
649*e6e505b9SAlexander Graf writel(reg_val, &dram->mr);
650*e6e505b9SAlexander Graf
651*e6e505b9SAlexander Graf writel(para->emr1, &dram->emr);
652*e6e505b9SAlexander Graf writel(para->emr2, &dram->emr2);
653*e6e505b9SAlexander Graf writel(para->emr3, &dram->emr3);
654*e6e505b9SAlexander Graf
655*e6e505b9SAlexander Graf /* disable drift compensation and set passive DQS window mode */
656*e6e505b9SAlexander Graf clrsetbits_le32(&dram->ccr, DRAM_CCR_DQS_DRIFT_COMP, DRAM_CCR_DQS_GATE);
657*e6e505b9SAlexander Graf
658*e6e505b9SAlexander Graf #ifdef CONFIG_MACH_SUN7I
659*e6e505b9SAlexander Graf /* Command rate timing mode 2T & 1T */
660*e6e505b9SAlexander Graf if (para->tpr4 & 0x1)
661*e6e505b9SAlexander Graf setbits_le32(&dram->ccr, DRAM_CCR_COMMAND_RATE_1T);
662*e6e505b9SAlexander Graf #endif
663*e6e505b9SAlexander Graf /* initialize external DRAM */
664*e6e505b9SAlexander Graf mctl_ddr3_initialize();
665*e6e505b9SAlexander Graf
666*e6e505b9SAlexander Graf /* scan read pipe value */
667*e6e505b9SAlexander Graf mctl_itm_enable();
668*e6e505b9SAlexander Graf
669*e6e505b9SAlexander Graf /* Hardware DQS gate training */
670*e6e505b9SAlexander Graf ret_val = dramc_scan_readpipe();
671*e6e505b9SAlexander Graf
672*e6e505b9SAlexander Graf if (ret_val < 0)
673*e6e505b9SAlexander Graf return 0;
674*e6e505b9SAlexander Graf
675*e6e505b9SAlexander Graf /* allow to override the DQS training results with a custom delay */
676*e6e505b9SAlexander Graf if (para->dqs_gating_delay)
677*e6e505b9SAlexander Graf mctl_set_dqs_gating_delay(0, para->dqs_gating_delay);
678*e6e505b9SAlexander Graf
679*e6e505b9SAlexander Graf /* set the DQS gating window type */
680*e6e505b9SAlexander Graf if (para->active_windowing)
681*e6e505b9SAlexander Graf clrbits_le32(&dram->ccr, DRAM_CCR_DQS_GATE);
682*e6e505b9SAlexander Graf else
683*e6e505b9SAlexander Graf setbits_le32(&dram->ccr, DRAM_CCR_DQS_GATE);
684*e6e505b9SAlexander Graf
685*e6e505b9SAlexander Graf mctl_itm_reset();
686*e6e505b9SAlexander Graf
687*e6e505b9SAlexander Graf /* configure all host port */
688*e6e505b9SAlexander Graf mctl_configure_hostport();
689*e6e505b9SAlexander Graf
690*e6e505b9SAlexander Graf return get_ram_size((long *)PHYS_SDRAM_0, PHYS_SDRAM_0_SIZE);
691*e6e505b9SAlexander Graf }
692*e6e505b9SAlexander Graf
dramc_init(struct dram_para * para)693*e6e505b9SAlexander Graf unsigned long dramc_init(struct dram_para *para)
694*e6e505b9SAlexander Graf {
695*e6e505b9SAlexander Graf unsigned long dram_size, actual_density;
696*e6e505b9SAlexander Graf
697*e6e505b9SAlexander Graf /* If the dram configuration is not provided, use a default */
698*e6e505b9SAlexander Graf if (!para)
699*e6e505b9SAlexander Graf return 0;
700*e6e505b9SAlexander Graf
701*e6e505b9SAlexander Graf /* if everything is known, then autodetection is not necessary */
702*e6e505b9SAlexander Graf if (para->io_width && para->bus_width && para->density)
703*e6e505b9SAlexander Graf return dramc_init_helper(para);
704*e6e505b9SAlexander Graf
705*e6e505b9SAlexander Graf /* try to autodetect the DRAM bus width and density */
706*e6e505b9SAlexander Graf para->io_width = 16;
707*e6e505b9SAlexander Graf para->bus_width = 32;
708*e6e505b9SAlexander Graf #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN5I)
709*e6e505b9SAlexander Graf /* only A0-A14 address lines on A10/A13, limiting max density to 4096 */
710*e6e505b9SAlexander Graf para->density = 4096;
711*e6e505b9SAlexander Graf #else
712*e6e505b9SAlexander Graf /* all A0-A15 address lines on A20, which allow density 8192 */
713*e6e505b9SAlexander Graf para->density = 8192;
714*e6e505b9SAlexander Graf #endif
715*e6e505b9SAlexander Graf
716*e6e505b9SAlexander Graf dram_size = dramc_init_helper(para);
717*e6e505b9SAlexander Graf if (!dram_size) {
718*e6e505b9SAlexander Graf /* if 32-bit bus width failed, try 16-bit bus width instead */
719*e6e505b9SAlexander Graf para->bus_width = 16;
720*e6e505b9SAlexander Graf dram_size = dramc_init_helper(para);
721*e6e505b9SAlexander Graf if (!dram_size) {
722*e6e505b9SAlexander Graf /* if 16-bit bus width also failed, then bail out */
723*e6e505b9SAlexander Graf return dram_size;
724*e6e505b9SAlexander Graf }
725*e6e505b9SAlexander Graf }
726*e6e505b9SAlexander Graf
727*e6e505b9SAlexander Graf /* check if we need to adjust the density */
728*e6e505b9SAlexander Graf actual_density = (dram_size >> 17) * para->io_width / para->bus_width;
729*e6e505b9SAlexander Graf
730*e6e505b9SAlexander Graf if (actual_density != para->density) {
731*e6e505b9SAlexander Graf /* update the density and re-initialize DRAM again */
732*e6e505b9SAlexander Graf para->density = actual_density;
733*e6e505b9SAlexander Graf dram_size = dramc_init_helper(para);
734*e6e505b9SAlexander Graf }
735*e6e505b9SAlexander Graf
736*e6e505b9SAlexander Graf return dram_size;
737*e6e505b9SAlexander Graf }
738