blob: f65fcf179cf65b4fd58647e6df482ee3aeabbb2f [file] [log] [blame]
developerf9eea652018-11-15 10:08:03 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * MediaTek DDR3 driver for MT7629 SoC
4 *
5 * Copyright (C) 2018 MediaTek Inc.
6 * Author: Wu Zou <wu.zou@mediatek.com>
7 * Ryder Lee <ryder.lee@mediatek.com>
8 */
9
10#include <clk.h>
11#include <common.h>
12#include <dm.h>
13#include <ram.h>
14#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060015#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
developerfc561e92023-07-19 17:15:47 +080017#include <linux/sizes.h>
developerf9eea652018-11-15 10:08:03 +080018
19/* EMI */
20#define EMI_CONA 0x000
21#define EMI_CONF 0x028
22#define EMI_CONM 0x060
23
24/* DDR PHY */
25#define DDRPHY_PLL1 0x0000
26#define DDRPHY_PLL2 0x0004
27#define DDRPHY_PLL3 0x0008
28#define DDRPHY_PLL4 0x000c
29#define DDRPHY_PLL5 0x0010
30#define DDRPHY_PLL7 0x0018
31#define DDRPHY_B0_DLL_ARPI0 0x0080
32#define DDRPHY_B0_DLL_ARPI1 0x0084
33#define DDRPHY_B0_DLL_ARPI2 0x0088
34#define DDRPHY_B0_DLL_ARPI3 0x008c
35#define DDRPHY_B0_DLL_ARPI4 0x0090
36#define DDRPHY_B0_DLL_ARPI5 0x0094
37#define DDRPHY_B0_DQ2 0x00a0
38#define DDRPHY_B0_DQ3 0x00a4
39#define DDRPHY_B0_DQ4 0x00a8
40#define DDRPHY_B0_DQ5 0x00ac
41#define DDRPHY_B0_DQ6 0x00b0
42#define DDRPHY_B0_DQ7 0x00b4
43#define DDRPHY_B0_DQ8 0x00b8
44#define DDRPHY_B1_DLL_ARPI0 0x0100
45#define DDRPHY_B1_DLL_ARPI1 0x0104
46#define DDRPHY_B1_DLL_ARPI2 0x0108
47#define DDRPHY_B1_DLL_ARPI3 0x010c
48#define DDRPHY_B1_DLL_ARPI4 0x0110
49#define DDRPHY_B1_DLL_ARPI5 0x0114
50#define DDRPHY_B1_DQ2 0x0120
51#define DDRPHY_B1_DQ3 0x0124
52#define DDRPHY_B1_DQ4 0x0128
53#define DDRPHY_B1_DQ5 0x012c
54#define DDRPHY_B1_DQ6 0x0130
55#define DDRPHY_B1_DQ7 0x0134
56#define DDRPHY_B1_DQ8 0x0138
57#define DDRPHY_CA_DLL_ARPI0 0x0180
58#define DDRPHY_CA_DLL_ARPI1 0x0184
59#define DDRPHY_CA_DLL_ARPI2 0x0188
60#define DDRPHY_CA_DLL_ARPI3 0x018c
61#define DDRPHY_CA_DLL_ARPI4 0x0190
62#define DDRPHY_CA_DLL_ARPI5 0x0194
63#define DDRPHY_CA_CMD2 0x01a0
64#define DDRPHY_CA_CMD3 0x01a4
65#define DDRPHY_CA_CMD5 0x01ac
66#define DDRPHY_CA_CMD6 0x01b0
67#define DDRPHY_CA_CMD7 0x01b4
68#define DDRPHY_CA_CMD8 0x01b8
69#define DDRPHY_MISC_VREF_CTRL 0x0264
70#define DDRPHY_MISC_IMP_CTRL0 0x0268
71#define DDRPHY_MISC_IMP_CTRL1 0x026c
72#define DDRPHY_MISC_SHU_OPT 0x0270
73#define DDRPHY_MISC_SPM_CTRL0 0x0274
74#define DDRPHY_MISC_SPM_CTRL1 0x0278
75#define DDRPHY_MISC_SPM_CTRL2 0x027c
76#define DDRPHY_MISC_CG_CTRL0 0x0284
77#define DDRPHY_MISC_CG_CTRL1 0x0288
78#define DDRPHY_MISC_CG_CTRL2 0x028c
79#define DDRPHY_MISC_CG_CTRL4 0x0294
80#define DDRPHY_MISC_CTRL0 0x029c
81#define DDRPHY_MISC_CTRL1 0x02a0
82#define DDRPHY_MISC_CTRL3 0x02a8
83#define DDRPHY_MISC_RXDVS1 0x05e4
84#define DDRPHY_SHU1_B0_DQ4 0x0c10
85#define DDRPHY_SHU1_B0_DQ5 0x0c14
86#define DDRPHY_SHU1_B0_DQ6 0x0c18
87#define DDRPHY_SHU1_B0_DQ7 0x0c1c
88#define DDRPHY_SHU1_B1_DQ4 0x0c90
89#define DDRPHY_SHU1_B1_DQ5 0x0c94
90#define DDRPHY_SHU1_B1_DQ6 0x0c98
91#define DDRPHY_SHU1_B1_DQ7 0x0c9c
92#define DDRPHY_SHU1_CA_CMD2 0x0d08
93#define DDRPHY_SHU1_CA_CMD4 0x0d10
94#define DDRPHY_SHU1_CA_CMD5 0x0d14
95#define DDRPHY_SHU1_CA_CMD6 0x0d18
96#define DDRPHY_SHU1_CA_CMD7 0x0d1c
97#define DDRPHY_SHU1_PLL0 0x0d80
98#define DDRPHY_SHU1_PLL1 0x0d84
99#define DDRPHY_SHU1_PLL4 0x0d90
100#define DDRPHY_SHU1_PLL5 0x0d94
101#define DDRPHY_SHU1_PLL6 0x0d98
102#define DDRPHY_SHU1_PLL7 0x0d9C
103#define DDRPHY_SHU1_PLL8 0x0da0
104#define DDRPHY_SHU1_PLL9 0x0da4
105#define DDRPHY_SHU1_PLL10 0x0da8
106#define DDRPHY_SHU1_PLL11 0x0dac
107#define DDRPHY_SHU1_R0_B0_DQ2 0x0e08
108#define DDRPHY_SHU1_R0_B0_DQ3 0x0e0c
109#define DDRPHY_SHU1_R0_B0_DQ4 0x0e10
110#define DDRPHY_SHU1_R0_B0_DQ5 0x0e14
111#define DDRPHY_SHU1_R0_B0_DQ6 0x0e18
112#define DDRPHY_SHU1_R0_B0_DQ7 0x0e1c
113#define DDRPHY_SHU1_R0_B1_DQ2 0x0e58
114#define DDRPHY_SHU1_R0_B1_DQ3 0x0e5c
115#define DDRPHY_SHU1_R0_B1_DQ4 0x0e60
116#define DDRPHY_SHU1_R0_B1_DQ5 0x0e64
117#define DDRPHY_SHU1_R0_B1_DQ6 0x0e68
118#define DDRPHY_SHU1_R0_B1_DQ7 0x0e6c
119#define DDRPHY_SHU1_R0_CA_CMD9 0x0ec4
120#define DDRPHY_SHU1_R1_B0_DQ2 0x0f08
121#define DDRPHY_SHU1_R1_B0_DQ3 0x0f0c
122#define DDRPHY_SHU1_R1_B0_DQ4 0x0f10
123#define DDRPHY_SHU1_R1_B0_DQ5 0x0f14
124#define DDRPHY_SHU1_R1_B0_DQ6 0x0f18
125#define DDRPHY_SHU1_R1_B0_DQ7 0x0f1c
126#define DDRPHY_SHU1_R1_B1_DQ2 0x0f58
127#define DDRPHY_SHU1_R1_B1_DQ3 0x0f5c
128#define DDRPHY_SHU1_R1_B1_DQ4 0x0f60
129#define DDRPHY_SHU1_R1_B1_DQ5 0x0f64
130#define DDRPHY_SHU1_R1_B1_DQ6 0x0f68
131#define DDRPHY_SHU1_R1_B1_DQ7 0x0f6c
132#define DDRPHY_SHU1_R1_CA_CMD9 0x0fc4
133
134/* DRAMC */
135#define DRAMC_DDRCONF0 0x0000
136#define DRAMC_DRAMCTRL 0x0004
137#define DRAMC_MISCTL0 0x0008
138#define DRAMC_PERFCTL0 0x000c
139#define DRAMC_ARBCTL 0x0010
140#define DRAMC_RSTMASK 0x001c
141#define DRAMC_PADCTRL 0x0020
142#define DRAMC_CKECTRL 0x0024
143#define DRAMC_RKCFG 0x0034
144#define DRAMC_DRAMC_PD_CTRL 0x0038
145#define DRAMC_CLKAR 0x003c
146#define DRAMC_CLKCTRL 0x0040
147#define DRAMC_SREFCTRL 0x0048
148#define DRAMC_REFCTRL0 0x004c
149#define DRAMC_REFCTRL1 0x0050
150#define DRAMC_REFRATRE_FILTER 0x0054
151#define DRAMC_ZQCS 0x0058
152#define DRAMC_MRS 0x005c
153#define DRAMC_SPCMD 0x0060
154#define DRAMC_SPCMDCTRL 0x0064
155#define DRAMC_HW_MRR_FUN 0x0074
156#define DRAMC_TEST2_1 0x0094
157#define DRAMC_TEST2_2 0x0098
158#define DRAMC_TEST2_3 0x009c
159#define DRAMC_TEST2_4 0x00a0
160#define DRAMC_CATRAINING1 0x00b0
161#define DRAMC_DUMMY_RD 0x00d0
162#define DRAMC_SHUCTRL 0x00d4
163#define DRAMC_SHUCTRL2 0x00dc
164#define DRAMC_STBCAL 0x0200
165#define DRAMC_STBCAL1 0x0204
166#define DRAMC_EYESCAN 0x020c
167#define DRAMC_DVFSDLL 0x0210
168#define DRAMC_SHU_ACTIM0 0x0800
169#define DRAMC_SHU_ACTIM1 0x0804
170#define DRAMC_SHU_ACTIM2 0x0808
171#define DRAMC_SHU_ACTIM3 0x080c
172#define DRAMC_SHU_ACTIM4 0x0810
173#define DRAMC_SHU_ACTIM5 0x0814
174#define DRAMC_SHU_ACTIM_XRT 0x081c
175#define DRAMC_SHU_AC_TIME_05T 0x0820
176#define DRAMC_SHU_CONF0 0x0840
177#define DRAMC_SHU_CONF1 0x0844
178#define DRAMC_SHU_CONF2 0x0848
179#define DRAMC_SHU_CONF3 0x084c
180#define DRAMC_SHU_RANKCTL 0x0858
181#define DRAMC_SHU_CKECTRL 0x085c
182#define DRAMC_SHU_ODTCTRL 0x0860
183#define DRAMC_SHU_PIPE 0x0878
184#define DRAMC_SHU_SELPH_CA1 0x0880
185#define DRAMC_SHU_SELPH_CA2 0x0884
186#define DRAMC_SHU_SELPH_CA3 0x0888
187#define DRAMC_SHU_SELPH_CA4 0x088c
188#define DRAMC_SHU_SELPH_CA5 0x0890
189#define DRAMC_SHU_SELPH_CA6 0x0894
190#define DRAMC_SHU_SELPH_CA7 0x0898
191#define DRAMC_SHU_SELPH_CA8 0x089c
192#define DRAMC_SHU_SELPH_DQS0 0x08a0
193#define DRAMC_SHU_SELPH_DQS1 0x08a4
194#define DRAMC_SHU1_DRVING1 0x08a8
195#define DRAMC_SHU1_DRVING2 0x08ac
196#define DRAMC_SHU1_WODT 0x08c0
197#define DRAMC_SHU_SCINTV 0x08c8
198#define DRAMC_SHURK0_DQSCTL 0x0a00
199#define DRAMC_SHURK0_DQSIEN 0x0a04
200#define DRAMC_SHURK0_SELPH_ODTEN0 0x0a1c
201#define DRAMC_SHURK0_SELPH_ODTEN1 0x0a20
202#define DRAMC_SHURK0_SELPH_DQSG0 0x0a24
203#define DRAMC_SHURK0_SELPH_DQSG1 0x0a28
204#define DRAMC_SHURK0_SELPH_DQ0 0x0a2c
205#define DRAMC_SHURK0_SELPH_DQ1 0x0a30
206#define DRAMC_SHURK0_SELPH_DQ2 0x0a34
207#define DRAMC_SHURK0_SELPH_DQ3 0x0a38
208#define DRAMC_SHURK1_DQSCTL 0x0b00
209#define DRAMC_SHURK1_SELPH_ODTEN0 0x0b1c
210#define DRAMC_SHURK1_SELPH_ODTEN1 0x0b20
211#define DRAMC_SHURK1_SELPH_DQSG0 0x0b24
212#define DRAMC_SHURK1_SELPH_DQSG1 0x0b28
213#define DRAMC_SHURK1_SELPH_DQ0 0x0b2c
214#define DRAMC_SHURK1_SELPH_DQ1 0x0b30
215#define DRAMC_SHURK1_SELPH_DQ2 0x0b34
216#define DRAMC_SHURK1_SELPH_DQ3 0x0b38
217#define DRAMC_SHURK2_SELPH_ODTEN0 0x0c1c
218#define DRAMC_SHURK2_SELPH_ODTEN1 0x0c20
219#define DRAMC_SHU_DQSG_RETRY 0x0c54
220
221#define EMI_COL_ADDR_MASK GENMASK(13, 12)
222#define EMI_COL_ADDR_SHIFT 12
223#define WALKING_PATTERN 0x12345678
224#define WALKING_STEP 0x4000000
225
226struct mtk_ddr3_priv {
227 fdt_addr_t emi;
228 fdt_addr_t ddrphy;
229 fdt_addr_t dramc_ao;
230 struct clk phy;
231 struct clk phy_mux;
232 struct clk mem;
233 struct clk mem_mux;
234};
235
236#ifdef CONFIG_SPL_BUILD
237static int mtk_ddr3_rank_size_detect(struct udevice *dev)
238{
239 struct mtk_ddr3_priv *priv = dev_get_priv(dev);
240 int step;
241 u32 start, test;
242
243 /* To detect size, we have to make sure it's single rank
244 * and it has maximum addressing region
245 */
246
Tom Rinibb4dd962022-11-16 13:10:37 -0500247 writel(WALKING_PATTERN, CFG_SYS_SDRAM_BASE);
developerf9eea652018-11-15 10:08:03 +0800248
Tom Rinibb4dd962022-11-16 13:10:37 -0500249 if (readl(CFG_SYS_SDRAM_BASE) != WALKING_PATTERN)
developerf9eea652018-11-15 10:08:03 +0800250 return -EINVAL;
251
252 for (step = 0; step < 5; step++) {
Tom Rinibb4dd962022-11-16 13:10:37 -0500253 writel(~WALKING_PATTERN, CFG_SYS_SDRAM_BASE +
developerf9eea652018-11-15 10:08:03 +0800254 (WALKING_STEP << step));
255
Tom Rinibb4dd962022-11-16 13:10:37 -0500256 start = readl(CFG_SYS_SDRAM_BASE);
257 test = readl(CFG_SYS_SDRAM_BASE + (WALKING_STEP << step));
developerf9eea652018-11-15 10:08:03 +0800258 if ((test != ~WALKING_PATTERN) || test == start)
259 break;
260 }
261
262 step = step ? step - 1 : 3;
263 clrsetbits_le32(priv->emi + EMI_CONA, EMI_COL_ADDR_MASK,
264 step << EMI_COL_ADDR_SHIFT);
265
266 return 0;
267}
268
269static int mtk_ddr3_init(struct udevice *dev)
270{
271 struct mtk_ddr3_priv *priv = dev_get_priv(dev);
272 int ret;
273
274 ret = clk_set_parent(&priv->phy, &priv->phy_mux);
275 if (ret)
276 return ret;
277
278 /* EMI Setting */
279 writel(0x00003010, priv->emi + EMI_CONA);
280 writel(0x00000000, priv->emi + EMI_CONF);
281 writel(0x000006b8, priv->emi + EMI_CONM);
282 /* DQS */
283 writel(0x20c00, priv->dramc_ao + DRAMC_SHU1_DRVING1);
284 /* Clock */
285 writel(0x8320c83, priv->dramc_ao + DRAMC_SHU1_DRVING2);
286
287 /* DDRPHY setting */
288 writel(0x2201, priv->dramc_ao + DRAMC_DRAMCTRL);
289 writel(0x3000000c, priv->dramc_ao + DRAMC_CLKCTRL);
290 writel(0xe08, priv->ddrphy + DDRPHY_CA_CMD5);
291 writel(0x60e, priv->ddrphy + DDRPHY_SHU1_CA_CMD5);
292 writel(0x0, priv->ddrphy + DDRPHY_MISC_SPM_CTRL1);
293 writel(0xffffffff, priv->ddrphy + DDRPHY_MISC_SPM_CTRL0);
294 writel(0xffffffff, priv->ddrphy + DDRPHY_MISC_SPM_CTRL2);
295 writel(0x6003bf, priv->ddrphy + DDRPHY_MISC_CG_CTRL2);
296 writel(0x13300000, priv->ddrphy + DDRPHY_MISC_CG_CTRL4);
297
298 writel(0x1, priv->ddrphy + DDRPHY_SHU1_CA_CMD7);
299 writel(0x21, priv->ddrphy + DDRPHY_SHU1_B0_DQ7);
300 writel(0x1, priv->ddrphy + DDRPHY_SHU1_B1_DQ7);
301 writel(0xfff0, priv->ddrphy + DDRPHY_CA_CMD2);
302 writel(0x0, priv->ddrphy + DDRPHY_B0_DQ2);
303 writel(0x0, priv->ddrphy + DDRPHY_B1_DQ2);
304 writel(0x7, priv->ddrphy + DDRPHY_MISC_RXDVS1);
305 writel(0x10, priv->ddrphy + DDRPHY_PLL3);
306 writel(0x8e8e0000, priv->ddrphy + DDRPHY_MISC_VREF_CTRL);
307 writel(0x2e0040, priv->ddrphy + DDRPHY_MISC_IMP_CTRL0);
308 writel(0x50060e, priv->ddrphy + DDRPHY_SHU1_B0_DQ5);
309 writel(0x50060e, priv->ddrphy + DDRPHY_SHU1_B1_DQ5);
310 udelay(1);
311
312 writel(0x10, priv->ddrphy + DDRPHY_B0_DQ3);
313 writel(0x10, priv->ddrphy + DDRPHY_B1_DQ3);
314 writel(0x3f600, priv->ddrphy + DDRPHY_MISC_CG_CTRL1);
315 writel(0x1010, priv->ddrphy + DDRPHY_B0_DQ4);
316 writel(0x1110e0e, priv->ddrphy + DDRPHY_B0_DQ5);
317 writel(0x10c10d0, priv->ddrphy + DDRPHY_B0_DQ6);
318 writel(0x3110e0e, priv->ddrphy + DDRPHY_B0_DQ5);
319 writel(0x1010, priv->ddrphy + DDRPHY_B1_DQ4);
320 writel(0x1110e0e, priv->ddrphy + DDRPHY_B1_DQ5);
321 writel(0x10c10d0, priv->ddrphy + DDRPHY_B1_DQ6);
322 writel(0x3110e0e, priv->ddrphy + DDRPHY_B1_DQ5);
323 writel(0x7fffffc, priv->ddrphy + DDRPHY_CA_CMD3);
324 writel(0xc0010, priv->ddrphy + DDRPHY_CA_CMD6);
325 writel(0x101, priv->ddrphy + DDRPHY_SHU1_CA_CMD2);
326 writel(0x41e, priv->ddrphy + DDRPHY_B0_DQ3);
327 writel(0x41e, priv->ddrphy + DDRPHY_B1_DQ3);
328 writel(0x180101, priv->ddrphy + DDRPHY_CA_CMD8);
329 writel(0x0, priv->ddrphy + DDRPHY_MISC_IMP_CTRL1);
330 writel(0x11400000, priv->ddrphy + DDRPHY_MISC_CG_CTRL4);
331 writel(0xfff0f0f0, priv->ddrphy + DDRPHY_MISC_SHU_OPT);
332 writel(0x1f, priv->ddrphy + DDRPHY_MISC_CG_CTRL0);
333
334 writel(0x0, priv->ddrphy + DDRPHY_SHU1_CA_CMD6);
335 writel(0x0, priv->ddrphy + DDRPHY_SHU1_B0_DQ6);
336 writel(0x0, priv->ddrphy + DDRPHY_SHU1_B1_DQ6);
337 writel(0x40000, priv->ddrphy + DDRPHY_PLL4);
338 writel(0x0, priv->ddrphy + DDRPHY_PLL1);
339 writel(0x0, priv->ddrphy + DDRPHY_PLL2);
340 writel(0x666008, priv->ddrphy + DDRPHY_CA_DLL_ARPI5);
341 writel(0x80666008, priv->ddrphy + DDRPHY_B0_DLL_ARPI5);
342 writel(0x80666008, priv->ddrphy + DDRPHY_B1_DLL_ARPI5);
343 writel(0x0, priv->ddrphy + DDRPHY_CA_DLL_ARPI0);
344 writel(0x0, priv->ddrphy + DDRPHY_B0_DLL_ARPI0);
345 writel(0x0, priv->ddrphy + DDRPHY_B1_DLL_ARPI0);
346 writel(0x400, priv->ddrphy + DDRPHY_CA_DLL_ARPI2);
347 writel(0x20400, priv->ddrphy + DDRPHY_B0_DLL_ARPI2);
348 writel(0x20400, priv->ddrphy + DDRPHY_B1_DLL_ARPI2);
349 writel(0x0, priv->ddrphy + DDRPHY_SHU1_PLL9);
350 writel(0x0, priv->ddrphy + DDRPHY_SHU1_PLL11);
351 writel(0xf7f, priv->ddrphy + DDRPHY_SHU1_PLL0);
352 writel(0x40000, priv->ddrphy + DDRPHY_SHU1_PLL8);
353 writel(0x40000, priv->ddrphy + DDRPHY_SHU1_PLL10);
354 writel(0xe57800fe, priv->ddrphy + DDRPHY_SHU1_PLL4);
355 writel(0xe57800fe, priv->ddrphy + DDRPHY_SHU1_PLL6);
356
357 writel(0xB5000000, priv->ddrphy + DDRPHY_SHU1_PLL5);
358 writel(0xB5000000, priv->ddrphy + DDRPHY_SHU1_PLL7);
359
360 writel(0x14d0002, priv->ddrphy + DDRPHY_PLL5);
361 writel(0x14d0002, priv->ddrphy + DDRPHY_PLL7);
362 writel(0x80040000, priv->ddrphy + DDRPHY_SHU1_PLL8);
363 writel(0x80040000, priv->ddrphy + DDRPHY_SHU1_PLL10);
364 writel(0xf, priv->ddrphy + DDRPHY_SHU1_PLL1);
365 writel(0x4, priv->ddrphy + DDRPHY_CA_DLL_ARPI0);
366 writel(0x1, priv->ddrphy + DDRPHY_B0_DLL_ARPI0);
367 writel(0x1, priv->ddrphy + DDRPHY_B1_DLL_ARPI0);
368 writel(0x698600, priv->ddrphy + DDRPHY_CA_DLL_ARPI5);
369 writel(0xc0778600, priv->ddrphy + DDRPHY_B0_DLL_ARPI5);
370 writel(0xc0778600, priv->ddrphy + DDRPHY_B1_DLL_ARPI5);
371 writel(0x0, priv->ddrphy + DDRPHY_CA_DLL_ARPI4);
372 writel(0x0, priv->ddrphy + DDRPHY_B0_DLL_ARPI4);
373 writel(0x0, priv->ddrphy + DDRPHY_B1_DLL_ARPI4);
374 writel(0x2ba800, priv->ddrphy + DDRPHY_CA_DLL_ARPI1);
375 writel(0x2ae806, priv->ddrphy + DDRPHY_B0_DLL_ARPI1);
376 writel(0xae806, priv->ddrphy + DDRPHY_B1_DLL_ARPI1);
377 writel(0xba000, priv->ddrphy + DDRPHY_CA_DLL_ARPI3);
378 writel(0x2e800, priv->ddrphy + DDRPHY_B0_DLL_ARPI3);
379 writel(0x2e800, priv->ddrphy + DDRPHY_B1_DLL_ARPI3);
380 writel(0x0, priv->ddrphy + DDRPHY_SHU1_CA_CMD4);
381 writel(0x0, priv->ddrphy + DDRPHY_SHU1_B0_DQ4);
382 writel(0x0, priv->ddrphy + DDRPHY_SHU1_B1_DQ4);
383 writel(0x4, priv->ddrphy + DDRPHY_CA_DLL_ARPI0);
384 writel(0x1, priv->ddrphy + DDRPHY_B0_DLL_ARPI0);
385 writel(0x1, priv->ddrphy + DDRPHY_B1_DLL_ARPI0);
386 writel(0x32cf0000, priv->ddrphy + DDRPHY_SHU1_CA_CMD6);
387 writel(0x32cd0000, priv->ddrphy + DDRPHY_SHU1_B0_DQ6);
388 writel(0x32cd0000, priv->ddrphy + DDRPHY_SHU1_B1_DQ6);
389 writel(0x80010000, priv->ddrphy + DDRPHY_PLL1);
390 writel(0x80000000, priv->ddrphy + DDRPHY_PLL2);
391 udelay(100);
392
393 writel(0xc, priv->ddrphy + DDRPHY_CA_DLL_ARPI0);
394 writel(0x9, priv->ddrphy + DDRPHY_B0_DLL_ARPI0);
395 writel(0x9, priv->ddrphy + DDRPHY_B1_DLL_ARPI0);
396 writel(0xd0000, priv->ddrphy + DDRPHY_PLL4);
397 udelay(1);
398
399 writel(0x82, priv->ddrphy + DDRPHY_MISC_CTRL1);
400 writel(0x2, priv->dramc_ao + DRAMC_DDRCONF0);
401 writel(0x3acf0000, priv->ddrphy + DDRPHY_SHU1_CA_CMD6);
402 writel(0x3acd0000, priv->ddrphy + DDRPHY_SHU1_B0_DQ6);
403 writel(0x3acd0000, priv->ddrphy + DDRPHY_SHU1_B1_DQ6);
404 udelay(1);
405
406 writel(0x0, priv->ddrphy + DDRPHY_CA_DLL_ARPI2);
407 writel(0x0, priv->ddrphy + DDRPHY_B0_DLL_ARPI2);
408 writel(0x0, priv->ddrphy + DDRPHY_B1_DLL_ARPI2);
409 writel(0x80, priv->ddrphy + DDRPHY_MISC_CTRL1);
410 writel(0x0, priv->dramc_ao + DRAMC_DDRCONF0);
411 writel(0x80000000, priv->ddrphy + DDRPHY_PLL1);
412 udelay(1);
413
414 writel(0x698e00, priv->ddrphy + DDRPHY_CA_DLL_ARPI5);
415 udelay(1);
416
417 writel(0xc0778e00, priv->ddrphy + DDRPHY_B0_DLL_ARPI5);
418 udelay(1);
419
420 writel(0xc0778e00, priv->ddrphy + DDRPHY_B1_DLL_ARPI5);
421 udelay(1);
422
423 ret = clk_set_parent(&priv->mem, &priv->mem_mux);
424 if (ret)
425 return ret;
426
427 /* DDR PHY PLL setting */
428 writel(0x51e, priv->ddrphy + DDRPHY_B0_DQ3);
429 writel(0x51e, priv->ddrphy + DDRPHY_B1_DQ3);
430 writel(0x8100008c, priv->ddrphy + DDRPHY_MISC_CTRL1);
431 writel(0x80101, priv->ddrphy + DDRPHY_CA_CMD8);
432 writel(0x100, priv->ddrphy + DDRPHY_CA_CMD7);
433 writel(0x0, priv->ddrphy + DDRPHY_CA_CMD7);
434 writel(0x0, priv->ddrphy + DDRPHY_B0_DQ7);
435 writel(0x0, priv->ddrphy + DDRPHY_B1_DQ7);
436 writel(0x51e, priv->ddrphy + DDRPHY_B0_DQ3);
437 writel(0xff051e, priv->ddrphy + DDRPHY_B1_DQ3);
438 writel(0x0, priv->ddrphy + DDRPHY_B0_DQ2);
439 writel(0x1ff, priv->ddrphy + DDRPHY_B1_DQ2);
440
441 /* Update initial setting */
442 writel(0x5fc, priv->ddrphy + DDRPHY_B0_DQ3);
443 writel(0xff05fc, priv->ddrphy + DDRPHY_B1_DQ3);
444 writel(0x10c12d9, priv->ddrphy + DDRPHY_B0_DQ6);
445 writel(0x10c12d9, priv->ddrphy + DDRPHY_B1_DQ6);
446 writel(0xc0259, priv->ddrphy + DDRPHY_CA_CMD6);
447 writel(0x4000, priv->ddrphy + DDRPHY_B0_DQ2);
448 writel(0x41ff, priv->ddrphy + DDRPHY_B1_DQ2);
449 writel(0x0, priv->ddrphy + DDRPHY_B0_DQ8);
450 writel(0x100, priv->ddrphy + DDRPHY_B1_DQ8);
451 writel(0x3110e0e, priv->ddrphy + DDRPHY_B0_DQ5);
452 writel(0x3110e0e, priv->ddrphy + DDRPHY_B1_DQ5);
453 writel(0x51060e, priv->ddrphy + DDRPHY_SHU1_B0_DQ5);
454 writel(0x51060e, priv->ddrphy + DDRPHY_SHU1_B1_DQ5);
455 writel(0x39eff6, priv->dramc_ao + DRAMC_SHU_SCINTV);
456 writel(0x204ffff, priv->dramc_ao + DRAMC_CLKAR);
457 writel(0x31b1f1cf, priv->dramc_ao + DRAMC_SPCMDCTRL);
458 writel(0x0, priv->dramc_ao + DRAMC_PERFCTL0);
459 writel(0x80000, priv->dramc_ao + DRAMC_PERFCTL0);
460
461 /* Dramc setting PC3 */
462 writel(0x65714001, priv->dramc_ao + DRAMC_REFCTRL0);
463
464 writel(0x11351131, priv->ddrphy + DDRPHY_MISC_CTRL3);
465 writel(0x200600, priv->dramc_ao + DRAMC_SHU_DQSG_RETRY);
466 writel(0x101d007, priv->dramc_ao + DRAMC_SHUCTRL2);
467 writel(0xe090601, priv->dramc_ao + DRAMC_DVFSDLL);
468 writel(0x20003000, priv->dramc_ao + DRAMC_DDRCONF0);
469 writel(0x3900020f, priv->ddrphy + DDRPHY_MISC_CTRL0);
470 writel(0xa20810bf, priv->dramc_ao + DRAMC_SHU_CONF0);
471 writel(0x30050, priv->dramc_ao + DRAMC_SHU_ODTCTRL);
472 writel(0x25712000, priv->dramc_ao + DRAMC_REFCTRL0);
473 writel(0xb0100000, priv->dramc_ao + DRAMC_STBCAL);
474 writel(0x8000000, priv->dramc_ao + DRAMC_SREFCTRL);
475 writel(0xc0000000, priv->dramc_ao + DRAMC_SHU_PIPE);
476 writel(0x731004, priv->dramc_ao + DRAMC_RKCFG);
477 writel(0x8007320f, priv->dramc_ao + DRAMC_SHU_CONF2);
478 writel(0x2a7c0, priv->dramc_ao + DRAMC_SHU_SCINTV);
479 writel(0xc110, priv->dramc_ao + DRAMC_SHUCTRL);
480 writel(0x30000700, priv->dramc_ao + DRAMC_REFCTRL1);
481 writel(0x6543b321, priv->dramc_ao + DRAMC_REFRATRE_FILTER);
482
483 /* Update PCDDR3 default setting */
484 writel(0x0, priv->dramc_ao + DRAMC_SHU_SELPH_CA1);
485 writel(0x0, priv->dramc_ao + DRAMC_SHU_SELPH_CA2);
486 writel(0x0, priv->dramc_ao + DRAMC_SHU_SELPH_CA3);
487 writel(0x0, priv->dramc_ao + DRAMC_SHU_SELPH_CA4);
488 writel(0x10000111, priv->dramc_ao + DRAMC_SHU_SELPH_CA5);
489 writel(0x1000000, priv->dramc_ao + DRAMC_SHU_SELPH_CA6);
490 writel(0x0, priv->dramc_ao + DRAMC_SHU_SELPH_CA7);
491 writel(0x0, priv->dramc_ao + DRAMC_SHU_SELPH_CA8);
492 writel(0x0, priv->ddrphy + DDRPHY_SHU1_R0_CA_CMD9);
493 writel(0x0, priv->ddrphy + DDRPHY_SHU1_R1_CA_CMD9);
494 writel(0x11112222, priv->dramc_ao + DRAMC_SHU_SELPH_DQS0);
495 writel(0x33331111, priv->dramc_ao + DRAMC_SHU_SELPH_DQS1);
496 writel(0x11112222, priv->dramc_ao + DRAMC_SHURK0_SELPH_DQ0);
497 writel(0x11112222, priv->dramc_ao + DRAMC_SHURK0_SELPH_DQ1);
498 writel(0x33331111, priv->dramc_ao + DRAMC_SHURK0_SELPH_DQ2);
499 writel(0x33331111, priv->dramc_ao + DRAMC_SHURK0_SELPH_DQ3);
500 writel(0x11112222, priv->dramc_ao + DRAMC_SHURK1_SELPH_DQ0);
501 writel(0x11112222, priv->dramc_ao + DRAMC_SHURK1_SELPH_DQ1);
502 writel(0x33331111, priv->dramc_ao + DRAMC_SHURK1_SELPH_DQ2);
503 writel(0x33331111, priv->dramc_ao + DRAMC_SHURK1_SELPH_DQ3);
504 writel(0xf0f00, priv->ddrphy + DDRPHY_SHU1_R0_B0_DQ7);
505 writel(0xf0f00, priv->ddrphy + DDRPHY_SHU1_R1_B0_DQ7);
506 writel(0xf0f00, priv->ddrphy + DDRPHY_SHU1_R0_B1_DQ7);
507 writel(0xf0f00, priv->ddrphy + DDRPHY_SHU1_R1_B1_DQ7);
508 writel(0x0, priv->dramc_ao + DRAMC_SHURK0_SELPH_ODTEN0);
509 writel(0x0, priv->dramc_ao + DRAMC_SHURK0_SELPH_ODTEN1);
510 writel(0x0, priv->dramc_ao + DRAMC_SHURK1_SELPH_ODTEN0);
511 writel(0x0, priv->dramc_ao + DRAMC_SHURK1_SELPH_ODTEN1);
512 writel(0x0, priv->dramc_ao + DRAMC_SHURK2_SELPH_ODTEN0);
513 writel(0x66666666, priv->dramc_ao + DRAMC_SHURK2_SELPH_ODTEN1);
514 writel(0x2c000b0f, priv->dramc_ao + DRAMC_SHU_CONF1);
515 writel(0x11111111, priv->dramc_ao + DRAMC_SHURK0_SELPH_DQSG0);
516 writel(0x64646464, priv->dramc_ao + DRAMC_SHURK0_SELPH_DQSG1);
517 writel(0x11111111, priv->dramc_ao + DRAMC_SHURK1_SELPH_DQSG0);
518 writel(0x64646464, priv->dramc_ao + DRAMC_SHURK1_SELPH_DQSG1);
519 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R0_B0_DQ2);
520 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R0_B0_DQ3);
521 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R0_B0_DQ4);
522 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R0_B0_DQ5);
523 writel(0x0, priv->ddrphy + DDRPHY_SHU1_R0_B0_DQ6);
524 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R1_B0_DQ2);
525 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R1_B0_DQ3);
526 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R1_B0_DQ4);
527 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R1_B0_DQ5);
528 writel(0x0, priv->ddrphy + DDRPHY_SHU1_R1_B0_DQ6);
529 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R0_B1_DQ2);
530 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R0_B1_DQ3);
531 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R0_B1_DQ4);
532 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R0_B1_DQ5);
533 writel(0x0, priv->ddrphy + DDRPHY_SHU1_R0_B1_DQ6);
534 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R1_B1_DQ2);
535 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R1_B1_DQ3);
536 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R1_B1_DQ4);
537 writel(0xc0c0c0c, priv->ddrphy + DDRPHY_SHU1_R1_B1_DQ5);
538 writel(0x0, priv->ddrphy + DDRPHY_SHU1_R1_B1_DQ6);
539 writel(0x20000001, priv->dramc_ao + DRAMC_SHU_RANKCTL);
540 writel(0x2, priv->dramc_ao + DRAMC_SHURK0_DQSCTL);
541 writel(0x2, priv->dramc_ao + DRAMC_SHURK1_DQSCTL);
542 writel(0x4020b07, priv->dramc_ao + DRAMC_SHU_ACTIM0);
543 writel(0xb060400, priv->dramc_ao + DRAMC_SHU_ACTIM1);
544 writel(0x8090200, priv->dramc_ao + DRAMC_SHU_ACTIM2);
545 writel(0x810018, priv->dramc_ao + DRAMC_SHU_ACTIM3);
546 writel(0x1e9700ff, priv->dramc_ao + DRAMC_SHU_ACTIM4);
547 writel(0x1000908, priv->dramc_ao + DRAMC_SHU_ACTIM5);
548 writel(0x801040b, priv->dramc_ao + DRAMC_SHU_ACTIM_XRT);
549 writel(0x20000D1, priv->dramc_ao + DRAMC_SHU_AC_TIME_05T);
550 writel(0x80010000, priv->ddrphy + DDRPHY_PLL2);
551 udelay(500);
552
553 writel(0x81080000, priv->dramc_ao + DRAMC_MISCTL0);
554 writel(0xacf13, priv->dramc_ao + DRAMC_PERFCTL0);
555 writel(0xacf12, priv->dramc_ao + DRAMC_PERFCTL0);
556 writel(0x80, priv->dramc_ao + DRAMC_ARBCTL);
557 writel(0x9, priv->dramc_ao + DRAMC_PADCTRL);
558 writel(0x80000107, priv->dramc_ao + DRAMC_DRAMC_PD_CTRL);
559 writel(0x3000000c, priv->dramc_ao + DRAMC_CLKCTRL);
560 writel(0x25714001, priv->dramc_ao + DRAMC_REFCTRL0);
561 writel(0x35b1f1cf, priv->dramc_ao + DRAMC_SPCMDCTRL);
562 writel(0x4300000, priv->dramc_ao + DRAMC_CATRAINING1);
563 writel(0xb0300000, priv->dramc_ao + DRAMC_STBCAL);
564 writel(0x731414, priv->dramc_ao + DRAMC_RKCFG);
565 writel(0x733414, priv->dramc_ao + DRAMC_RKCFG);
566 udelay(20);
567
568 writel(0x80002050, priv->dramc_ao + DRAMC_CKECTRL);
569 udelay(100);
570
571 writel(0x400000, priv->dramc_ao + DRAMC_MRS);
572 writel(0x401800, priv->dramc_ao + DRAMC_MRS);
573 writel(0x1, priv->dramc_ao + DRAMC_SPCMD);
574 writel(0x0, priv->dramc_ao + DRAMC_SPCMD);
575 udelay(100);
576
577 writel(0x601800, priv->dramc_ao + DRAMC_MRS);
578 writel(0x600000, priv->dramc_ao + DRAMC_MRS);
579 writel(0x1, priv->dramc_ao + DRAMC_SPCMD);
580 writel(0x0, priv->dramc_ao + DRAMC_SPCMD);
581 udelay(100);
582
583 writel(0x200000, priv->dramc_ao + DRAMC_MRS);
584 writel(0x200400, priv->dramc_ao + DRAMC_MRS);
585 writel(0x1, priv->dramc_ao + DRAMC_SPCMD);
586 writel(0x0, priv->dramc_ao + DRAMC_SPCMD);
587 udelay(100);
588
589 writel(0x400, priv->dramc_ao + DRAMC_MRS);
590 writel(0x1d7000, priv->dramc_ao + DRAMC_MRS);
591 writel(0x1, priv->dramc_ao + DRAMC_SPCMD);
592 writel(0x0, priv->dramc_ao + DRAMC_SPCMD);
593 udelay(100);
594
595 writel(0x702201, priv->dramc_ao + DRAMC_DRAMCTRL);
596 writel(0x10, priv->dramc_ao + DRAMC_SPCMD);
597 writel(0x0, priv->dramc_ao + DRAMC_SPCMD);
598 writel(0x20, priv->dramc_ao + DRAMC_SPCMD);
599 writel(0x0, priv->dramc_ao + DRAMC_SPCMD);
600 writel(0x1, priv->dramc_ao + DRAMC_HW_MRR_FUN);
601 writel(0x702301, priv->dramc_ao + DRAMC_DRAMCTRL);
602 writel(0x702301, priv->dramc_ao + DRAMC_DRAMCTRL);
603 writel(0xa56, priv->dramc_ao + DRAMC_ZQCS);
604 writel(0xff0000, priv->dramc_ao + DRAMC_SHU_CONF3);
605 writel(0x15b1f1cf, priv->dramc_ao + DRAMC_SPCMDCTRL);
606 writel(0x2cb00b0f, priv->dramc_ao + DRAMC_SHU_CONF1);
607 writel(0x65714001, priv->dramc_ao + DRAMC_REFCTRL0);
608 writel(0x48000000, priv->dramc_ao + DRAMC_SREFCTRL);
609 writel(0xc0000107, priv->dramc_ao + DRAMC_DRAMC_PD_CTRL);
610 writel(0x10002, priv->dramc_ao + DRAMC_EYESCAN);
611 writel(0x15e00, priv->dramc_ao + DRAMC_STBCAL1);
612 writel(0x100000, priv->dramc_ao + DRAMC_TEST2_1);
613 writel(0x4000, priv->dramc_ao + DRAMC_TEST2_2);
614 writel(0x12000480, priv->dramc_ao + DRAMC_TEST2_3);
615 writel(0x301d007, priv->dramc_ao + DRAMC_SHUCTRL2);
616 writel(0x4782321, priv->dramc_ao + DRAMC_DRAMCTRL);
617 writel(0x30210000, priv->dramc_ao + DRAMC_SHU_CKECTRL);
618 writel(0x20000, priv->dramc_ao + DRAMC_DUMMY_RD);
619 writel(0x4080110d, priv->dramc_ao + DRAMC_TEST2_4);
620 writel(0x30000721, priv->dramc_ao + DRAMC_REFCTRL1);
621 writel(0x0, priv->dramc_ao + DRAMC_RSTMASK);
622 writel(0x4782320, priv->dramc_ao + DRAMC_DRAMCTRL);
623 writel(0x80002000, priv->dramc_ao + DRAMC_CKECTRL);
624 writel(0x45714001, priv->dramc_ao + DRAMC_REFCTRL0);
625
626 /* Apply config before calibration */
627 writel(0x120, priv->dramc_ao + DRAMC_DRAMC_PD_CTRL);
628 writel(0x11351131, priv->ddrphy + DDRPHY_MISC_CTRL3);
629 writel(0xffffffff, priv->ddrphy + DDRPHY_MISC_CG_CTRL0);
630 writel(0x2a7fe, priv->dramc_ao + DRAMC_SHU_SCINTV);
631 writel(0xff01ff, priv->dramc_ao + DRAMC_SHU_CONF3);
632 writel(0x4782320, priv->dramc_ao + DRAMC_DRAMCTRL);
633 writel(0xa56, priv->dramc_ao + DRAMC_ZQCS);
634 writel(0x80000000, priv->dramc_ao + DRAMC_SHU1_WODT);
635 writel(0x21, priv->ddrphy + DDRPHY_SHU1_B0_DQ7);
636 writel(0x1, priv->ddrphy + DDRPHY_SHU1_B1_DQ7);
637 writel(0x35b1f1cf, priv->dramc_ao + DRAMC_SPCMDCTRL);
638 writel(0x35b1f1cf, priv->dramc_ao + DRAMC_SPCMDCTRL);
639 writel(0x35b1f1cf, priv->dramc_ao + DRAMC_SPCMDCTRL);
640 writel(0xb0300000, priv->dramc_ao + DRAMC_STBCAL);
641 writel(0xb0300000, priv->dramc_ao + DRAMC_STBCAL);
642 writel(0x10002, priv->dramc_ao + DRAMC_EYESCAN);
643 writel(0x8100008c, priv->ddrphy + DDRPHY_MISC_CTRL1);
644 writel(0x45714001, priv->dramc_ao + DRAMC_REFCTRL0);
645 writel(0xb0300000, priv->dramc_ao + DRAMC_STBCAL);
646 writel(0xb0300000, priv->dramc_ao + DRAMC_STBCAL);
647
648 /* Write leveling */
649 writel(0x1f2e2e00, priv->ddrphy + DDRPHY_SHU1_R0_B0_DQ7);
650 writel(0x202f2f00, priv->ddrphy + DDRPHY_SHU1_R0_B1_DQ7);
651 writel(0x33221100, priv->dramc_ao + DRAMC_SHU_SELPH_DQS1);
652 writel(0x11112222, priv->dramc_ao + DRAMC_SHU_SELPH_DQS0);
653
654 /* RX dqs gating cal */
655 writel(0x11111010, priv->dramc_ao + DRAMC_SHURK0_SELPH_DQSG0);
656 writel(0x20201717, priv->dramc_ao + DRAMC_SHURK0_SELPH_DQSG1);
657 writel(0x1d1f, priv->dramc_ao + DRAMC_SHURK0_DQSIEN);
658
659 /* RX window per-bit cal */
660 writel(0x03030404, priv->ddrphy + DDRPHY_SHU1_R0_B0_DQ2);
661 writel(0x01010303, priv->ddrphy + DDRPHY_SHU1_R0_B0_DQ3);
662 writel(0x01010303, priv->ddrphy + DDRPHY_SHU1_R0_B0_DQ4);
663 writel(0x01010000, priv->ddrphy + DDRPHY_SHU1_R0_B0_DQ5);
664 writel(0x03030606, priv->ddrphy + DDRPHY_SHU1_R0_B1_DQ2);
665 writel(0x02020202, priv->ddrphy + DDRPHY_SHU1_R0_B1_DQ3);
666 writel(0x04040303, priv->ddrphy + DDRPHY_SHU1_R0_B1_DQ4);
667 writel(0x06060101, priv->ddrphy + DDRPHY_SHU1_R0_B1_DQ5);
668
669 /* RX datlat cal */
670 writel(0x28b00a0e, priv->dramc_ao + DRAMC_SHU_CONF1);
671
672 /* TX window per-byte with 2UI cal */
673 writel(0x11112222, priv->dramc_ao + DRAMC_SHURK0_SELPH_DQ0);
674 writel(0x22220000, priv->dramc_ao + DRAMC_SHURK0_SELPH_DQ2);
675 writel(0x11112222, priv->dramc_ao + DRAMC_SHURK0_SELPH_DQ1);
676 writel(0x22220000, priv->dramc_ao + DRAMC_SHURK0_SELPH_DQ3);
677 writel(0x1f2e2e00, priv->ddrphy + DDRPHY_SHU1_R0_B0_DQ7);
678 writel(0x202f2f00, priv->ddrphy + DDRPHY_SHU1_R0_B1_DQ7);
679
680 return mtk_ddr3_rank_size_detect(dev);
681}
682#endif
683
684static int mtk_ddr3_probe(struct udevice *dev)
685{
686 struct mtk_ddr3_priv *priv = dev_get_priv(dev);
687
688 priv->emi = dev_read_addr_index(dev, 0);
689 if (priv->emi == FDT_ADDR_T_NONE)
690 return -EINVAL;
691
692 priv->ddrphy = dev_read_addr_index(dev, 1);
693 if (priv->ddrphy == FDT_ADDR_T_NONE)
694 return -EINVAL;
695
696 priv->dramc_ao = dev_read_addr_index(dev, 2);
697 if (priv->dramc_ao == FDT_ADDR_T_NONE)
698 return -EINVAL;
699
700#ifdef CONFIG_SPL_BUILD
701 int ret;
702
703 ret = clk_get_by_index(dev, 0, &priv->phy);
704 if (ret)
705 return ret;
706
707 ret = clk_get_by_index(dev, 1, &priv->phy_mux);
708 if (ret)
709 return ret;
710
711 ret = clk_get_by_index(dev, 2, &priv->mem);
712 if (ret)
713 return ret;
714
715 ret = clk_get_by_index(dev, 3, &priv->mem_mux);
716 if (ret)
717 return ret;
718
719 ret = mtk_ddr3_init(dev);
720 if (ret)
721 return ret;
722#endif
723 return 0;
724}
725
726static int mtk_ddr3_get_info(struct udevice *dev, struct ram_info *info)
727{
728 struct mtk_ddr3_priv *priv = dev_get_priv(dev);
729 u32 val = readl(priv->emi + EMI_CONA);
730
Tom Rinibb4dd962022-11-16 13:10:37 -0500731 info->base = CFG_SYS_SDRAM_BASE;
developerf9eea652018-11-15 10:08:03 +0800732
733 switch ((val & EMI_COL_ADDR_MASK) >> EMI_COL_ADDR_SHIFT) {
734 case 0:
735 info->size = SZ_128M;
736 break;
737 case 1:
738 info->size = SZ_256M;
739 break;
740 case 2:
741 info->size = SZ_512M;
742 break;
743 case 3:
744 info->size = SZ_1G;
745 break;
746 default:
747 return -EINVAL;
748 }
749
750 return 0;
751}
752
753static struct ram_ops mtk_ddr3_ops = {
754 .get_info = mtk_ddr3_get_info,
755};
756
757static const struct udevice_id mtk_ddr3_ids[] = {
758 { .compatible = "mediatek,mt7629-dramc" },
759 { }
760};
761
762U_BOOT_DRIVER(mediatek_ddr3) = {
763 .name = "mediatek_ddr3",
764 .id = UCLASS_RAM,
765 .of_match = mtk_ddr3_ids,
766 .ops = &mtk_ddr3_ops,
767 .probe = mtk_ddr3_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700768 .priv_auto = sizeof(struct mtk_ddr3_priv),
developerf9eea652018-11-15 10:08:03 +0800769};