blob: 9121d5b779da77d0e19ce1617ee6f86ef07276f3 [file] [log] [blame]
Kever Yang6fc9ebf2018-12-20 11:33:42 +08001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Kever Yang50fb9982017-02-22 16:56:35 +08002/*
3 * (C) Copyright 2016-2017 Rockchip Inc.
4 *
Kever Yang50fb9982017-02-22 16:56:35 +08005 * Adapted from coreboot.
6 */
Philipp Tomsichc69b3092017-05-31 18:16:34 +02007
Kever Yang50fb9982017-02-22 16:56:35 +08008#include <common.h>
9#include <clk.h>
10#include <dm.h>
11#include <dt-structs.h>
12#include <ram.h>
13#include <regmap.h>
14#include <syscon.h>
15#include <asm/io.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080016#include <asm/arch-rockchip/clock.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080017#include <asm/arch-rockchip/cru_rk3399.h>
18#include <asm/arch-rockchip/grf_rk3399.h>
19#include <asm/arch-rockchip/hardware.h>
Jagan Tekiacf8e0f2019-07-15 23:50:57 +053020#include <asm/arch-rockchip/sdram_common.h>
21#include <asm/arch-rockchip/sdram_rk3399.h>
Kever Yang50fb9982017-02-22 16:56:35 +080022#include <linux/err.h>
Philipp Tomsichc69b3092017-05-31 18:16:34 +020023#include <time.h>
Kever Yang50fb9982017-02-22 16:56:35 +080024
Jagan Tekiacf8e0f2019-07-15 23:50:57 +053025#define PRESET_SGRF_HOLD(n) ((0x1 << (6 + 16)) | ((n) << 6))
26#define PRESET_GPIO0_HOLD(n) ((0x1 << (7 + 16)) | ((n) << 7))
27#define PRESET_GPIO1_HOLD(n) ((0x1 << (8 + 16)) | ((n) << 8))
28
29#define PHY_DRV_ODT_HI_Z 0x0
30#define PHY_DRV_ODT_240 0x1
31#define PHY_DRV_ODT_120 0x8
32#define PHY_DRV_ODT_80 0x9
33#define PHY_DRV_ODT_60 0xc
34#define PHY_DRV_ODT_48 0xd
35#define PHY_DRV_ODT_40 0xe
36#define PHY_DRV_ODT_34_3 0xf
37
Jagan Teki5d152172019-07-16 17:27:15 +053038#define PHY_BOOSTP_EN 0x1
39#define PHY_BOOSTN_EN 0x1
Jagan Tekid8681842019-07-16 17:27:16 +053040#define PHY_SLEWP_EN 0x1
41#define PHY_SLEWN_EN 0x1
Jagan Teki65535a22019-07-16 17:27:17 +053042#define PHY_RX_CM_INPUT 0x1
Jagan Teki5d152172019-07-16 17:27:15 +053043
Jagan Tekice75cfb2019-07-15 23:58:43 +053044#define CRU_SFTRST_DDR_CTRL(ch, n) ((0x1 << (8 + 16 + (ch) * 4)) | \
45 ((n) << (8 + (ch) * 4)))
46#define CRU_SFTRST_DDR_PHY(ch, n) ((0x1 << (9 + 16 + (ch) * 4)) | \
47 ((n) << (9 + (ch) * 4)))
Kever Yang50fb9982017-02-22 16:56:35 +080048struct chan_info {
49 struct rk3399_ddr_pctl_regs *pctl;
50 struct rk3399_ddr_pi_regs *pi;
51 struct rk3399_ddr_publ_regs *publ;
52 struct rk3399_msch_regs *msch;
53};
54
55struct dram_info {
Kever Yang7f347842019-04-01 17:20:53 +080056#if defined(CONFIG_TPL_BUILD) || \
57 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Jagan Tekic9151e22019-07-15 23:58:45 +053058 u32 pwrup_srefresh_exit[2];
Kever Yang50fb9982017-02-22 16:56:35 +080059 struct chan_info chan[2];
60 struct clk ddr_clk;
61 struct rk3399_cru *cru;
Jagan Tekic9151e22019-07-15 23:58:45 +053062 struct rk3399_grf_regs *grf;
Kever Yang50fb9982017-02-22 16:56:35 +080063 struct rk3399_pmucru *pmucru;
64 struct rk3399_pmusgrf_regs *pmusgrf;
65 struct rk3399_ddr_cic_regs *cic;
66#endif
67 struct ram_info info;
68 struct rk3399_pmugrf_regs *pmugrf;
69};
70
Kever Yang7f347842019-04-01 17:20:53 +080071#if defined(CONFIG_TPL_BUILD) || \
72 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +080073
74struct rockchip_dmc_plat {
75#if CONFIG_IS_ENABLED(OF_PLATDATA)
76 struct dtd_rockchip_rk3399_dmc dtplat;
77#else
78 struct rk3399_sdram_params sdram_params;
79#endif
80 struct regmap *map;
81};
82
Jagan Tekie3619d12019-07-16 17:27:21 +053083struct io_setting {
84 u32 mhz;
85 u32 mr5;
86 /* dram side */
87 u32 dq_odt;
88 u32 ca_odt;
89 u32 pdds;
90 u32 dq_vref;
91 u32 ca_vref;
92 /* phy side */
93 u32 rd_odt;
94 u32 wr_dq_drv;
95 u32 wr_ca_drv;
96 u32 wr_ckcs_drv;
97 u32 rd_odt_en;
98 u32 rd_vref;
99} lpddr4_io_setting[] = {
100 {
101 50 * MHz,
102 0,
103 /* dram side */
104 0, /* dq_odt; */
105 0, /* ca_odt; */
106 6, /* pdds; */
107 0x72, /* dq_vref; */
108 0x72, /* ca_vref; */
109 /* phy side */
110 PHY_DRV_ODT_HI_Z, /* rd_odt; */
111 PHY_DRV_ODT_40, /* wr_dq_drv; */
112 PHY_DRV_ODT_40, /* wr_ca_drv; */
113 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
114 0, /* rd_odt_en;*/
115 41, /* rd_vref; (unit %, range 3.3% - 48.7%) */
116 },
117 {
118 600 * MHz,
119 0,
120 /* dram side */
121 1, /* dq_odt; */
122 0, /* ca_odt; */
123 6, /* pdds; */
124 0x72, /* dq_vref; */
125 0x72, /* ca_vref; */
126 /* phy side */
127 PHY_DRV_ODT_HI_Z, /* rd_odt; */
128 PHY_DRV_ODT_48, /* wr_dq_drv; */
129 PHY_DRV_ODT_40, /* wr_ca_drv; */
130 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
131 0, /* rd_odt_en; */
132 32, /* rd_vref; (unit %, range 3.3% - 48.7%) */
133 },
134 {
135 800 * MHz,
136 0,
137 /* dram side */
138 1, /* dq_odt; */
139 0, /* ca_odt; */
140 1, /* pdds; */
141 0x72, /* dq_vref; */
142 0x72, /* ca_vref; */
143 /* phy side */
144 PHY_DRV_ODT_40, /* rd_odt; */
145 PHY_DRV_ODT_48, /* wr_dq_drv; */
146 PHY_DRV_ODT_40, /* wr_ca_drv; */
147 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
148 1, /* rd_odt_en; */
149 17, /* rd_vref; (unit %, range 3.3% - 48.7%) */
150 },
151 {
152 933 * MHz,
153 0,
154 /* dram side */
155 3, /* dq_odt; */
156 0, /* ca_odt; */
157 6, /* pdds; */
158 0x59, /* dq_vref; 32% */
159 0x72, /* ca_vref; */
160 /* phy side */
161 PHY_DRV_ODT_HI_Z, /* rd_odt; */
162 PHY_DRV_ODT_48, /* wr_dq_drv; */
163 PHY_DRV_ODT_40, /* wr_ca_drv; */
164 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
165 0, /* rd_odt_en; */
166 32, /* rd_vref; (unit %, range 3.3% - 48.7%) */
167 },
168 {
169 1066 * MHz,
170 0,
171 /* dram side */
172 6, /* dq_odt; */
173 0, /* ca_odt; */
174 1, /* pdds; */
175 0x10, /* dq_vref; */
176 0x72, /* ca_vref; */
177 /* phy side */
178 PHY_DRV_ODT_40, /* rd_odt; */
179 PHY_DRV_ODT_60, /* wr_dq_drv; */
180 PHY_DRV_ODT_40, /* wr_ca_drv; */
181 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
182 1, /* rd_odt_en; */
183 17, /* rd_vref; (unit %, range 3.3% - 48.7%) */
184 },
185};
186
Jagan Tekic9151e22019-07-15 23:58:45 +0530187static void *get_ddrc0_con(struct dram_info *dram, u8 channel)
188{
189 return (channel == 0) ? &dram->grf->ddrc0_con0 : &dram->grf->ddrc0_con1;
190}
191
Kever Yang50fb9982017-02-22 16:56:35 +0800192static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
193{
194 int i;
195
196 for (i = 0; i < n / sizeof(u32); i++) {
197 writel(*src, dest);
198 src++;
199 dest++;
200 }
201}
202
Jagan Tekice75cfb2019-07-15 23:58:43 +0530203static void rkclk_ddr_reset(struct rk3399_cru *cru, u32 channel, u32 ctl,
204 u32 phy)
205{
206 channel &= 0x1;
207 ctl &= 0x1;
208 phy &= 0x1;
209 writel(CRU_SFTRST_DDR_CTRL(channel, ctl) |
210 CRU_SFTRST_DDR_PHY(channel, phy),
211 &cru->softrst_con[4]);
212}
213
214static void phy_pctrl_reset(struct rk3399_cru *cru, u32 channel)
215{
216 rkclk_ddr_reset(cru, channel, 1, 1);
217 udelay(10);
218
219 rkclk_ddr_reset(cru, channel, 1, 0);
220 udelay(10);
221
222 rkclk_ddr_reset(cru, channel, 0, 0);
223 udelay(10);
224}
225
Kever Yang50fb9982017-02-22 16:56:35 +0800226static void phy_dll_bypass_set(struct rk3399_ddr_publ_regs *ddr_publ_regs,
227 u32 freq)
228{
229 u32 *denali_phy = ddr_publ_regs->denali_phy;
230
231 /* From IP spec, only freq small than 125 can enter dll bypass mode */
232 if (freq <= 125) {
233 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
234 setbits_le32(&denali_phy[86], (0x3 << 2) << 8);
235 setbits_le32(&denali_phy[214], (0x3 << 2) << 8);
236 setbits_le32(&denali_phy[342], (0x3 << 2) << 8);
237 setbits_le32(&denali_phy[470], (0x3 << 2) << 8);
238
239 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
240 setbits_le32(&denali_phy[547], (0x3 << 2) << 16);
241 setbits_le32(&denali_phy[675], (0x3 << 2) << 16);
242 setbits_le32(&denali_phy[803], (0x3 << 2) << 16);
243 } else {
244 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
245 clrbits_le32(&denali_phy[86], (0x3 << 2) << 8);
246 clrbits_le32(&denali_phy[214], (0x3 << 2) << 8);
247 clrbits_le32(&denali_phy[342], (0x3 << 2) << 8);
248 clrbits_le32(&denali_phy[470], (0x3 << 2) << 8);
249
250 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
251 clrbits_le32(&denali_phy[547], (0x3 << 2) << 16);
252 clrbits_le32(&denali_phy[675], (0x3 << 2) << 16);
253 clrbits_le32(&denali_phy[803], (0x3 << 2) << 16);
254 }
255}
256
257static void set_memory_map(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530258 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800259{
Jagan Tekia58ff792019-07-15 23:50:58 +0530260 const struct rk3399_sdram_channel *sdram_ch = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +0800261 u32 *denali_ctl = chan->pctl->denali_ctl;
262 u32 *denali_pi = chan->pi->denali_pi;
263 u32 cs_map;
264 u32 reduc;
265 u32 row;
266
267 /* Get row number from ddrconfig setting */
Jagan Teki97867c82019-07-15 23:51:05 +0530268 if (sdram_ch->cap_info.ddrconfig < 2 ||
269 sdram_ch->cap_info.ddrconfig == 4)
Kever Yang50fb9982017-02-22 16:56:35 +0800270 row = 16;
Jagan Teki97867c82019-07-15 23:51:05 +0530271 else if (sdram_ch->cap_info.ddrconfig == 3)
Kever Yang50fb9982017-02-22 16:56:35 +0800272 row = 14;
273 else
274 row = 15;
275
Jagan Teki97867c82019-07-15 23:51:05 +0530276 cs_map = (sdram_ch->cap_info.rank > 1) ? 3 : 1;
277 reduc = (sdram_ch->cap_info.bw == 2) ? 0 : 1;
Kever Yang50fb9982017-02-22 16:56:35 +0800278
279 /* Set the dram configuration to ctrl */
Jagan Teki97867c82019-07-15 23:51:05 +0530280 clrsetbits_le32(&denali_ctl[191], 0xF, (12 - sdram_ch->cap_info.col));
Kever Yang50fb9982017-02-22 16:56:35 +0800281 clrsetbits_le32(&denali_ctl[190], (0x3 << 16) | (0x7 << 24),
Jagan Teki97867c82019-07-15 23:51:05 +0530282 ((3 - sdram_ch->cap_info.bk) << 16) |
Kever Yang50fb9982017-02-22 16:56:35 +0800283 ((16 - row) << 24));
284
285 clrsetbits_le32(&denali_ctl[196], 0x3 | (1 << 16),
286 cs_map | (reduc << 16));
287
288 /* PI_199 PI_COL_DIFF:RW:0:4 */
Jagan Teki97867c82019-07-15 23:51:05 +0530289 clrsetbits_le32(&denali_pi[199], 0xF, (12 - sdram_ch->cap_info.col));
Kever Yang50fb9982017-02-22 16:56:35 +0800290
291 /* PI_155 PI_ROW_DIFF:RW:24:3 PI_BANK_DIFF:RW:16:2 */
292 clrsetbits_le32(&denali_pi[155], (0x3 << 16) | (0x7 << 24),
Jagan Teki97867c82019-07-15 23:51:05 +0530293 ((3 - sdram_ch->cap_info.bk) << 16) |
Kever Yang50fb9982017-02-22 16:56:35 +0800294 ((16 - row) << 24));
Jagan Teki9337cb32019-07-16 17:27:18 +0530295
296 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
297 if (cs_map == 1)
298 cs_map = 0x5;
299 else if (cs_map == 2)
300 cs_map = 0xa;
301 else
302 cs_map = 0xF;
303 }
304
Kever Yang50fb9982017-02-22 16:56:35 +0800305 /* PI_41 PI_CS_MAP:RW:24:4 */
306 clrsetbits_le32(&denali_pi[41], 0xf << 24, cs_map << 24);
Jagan Teki97867c82019-07-15 23:51:05 +0530307 if (sdram_ch->cap_info.rank == 1 && params->base.dramtype == DDR3)
Kever Yang50fb9982017-02-22 16:56:35 +0800308 writel(0x2EC7FFFF, &denali_pi[34]);
309}
310
Jagan Tekib5d46632019-07-16 17:27:07 +0530311static int phy_io_config(const struct chan_info *chan,
312 const struct rk3399_sdram_params *params)
313{
314 u32 *denali_phy = chan->publ->denali_phy;
315 u32 vref_mode_dq, vref_value_dq, vref_mode_ac, vref_value_ac;
316 u32 mode_sel;
317 u32 reg_value;
318 u32 drv_value, odt_value;
319 u32 speed;
320
321 /* vref setting */
322 if (params->base.dramtype == LPDDR4) {
323 /* LPDDR4 */
324 vref_mode_dq = 0x6;
325 vref_value_dq = 0x1f;
326 vref_mode_ac = 0x6;
327 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530328 mode_sel = 0x6;
Jagan Tekib5d46632019-07-16 17:27:07 +0530329 } else if (params->base.dramtype == LPDDR3) {
330 if (params->base.odt == 1) {
331 vref_mode_dq = 0x5; /* LPDDR3 ODT */
332 drv_value = (readl(&denali_phy[6]) >> 12) & 0xf;
333 odt_value = (readl(&denali_phy[6]) >> 4) & 0xf;
334 if (drv_value == PHY_DRV_ODT_48) {
335 switch (odt_value) {
336 case PHY_DRV_ODT_240:
337 vref_value_dq = 0x16;
338 break;
339 case PHY_DRV_ODT_120:
340 vref_value_dq = 0x26;
341 break;
342 case PHY_DRV_ODT_60:
343 vref_value_dq = 0x36;
344 break;
345 default:
346 debug("Invalid ODT value.\n");
347 return -EINVAL;
348 }
349 } else if (drv_value == PHY_DRV_ODT_40) {
350 switch (odt_value) {
351 case PHY_DRV_ODT_240:
352 vref_value_dq = 0x19;
353 break;
354 case PHY_DRV_ODT_120:
355 vref_value_dq = 0x23;
356 break;
357 case PHY_DRV_ODT_60:
358 vref_value_dq = 0x31;
359 break;
360 default:
361 debug("Invalid ODT value.\n");
362 return -EINVAL;
363 }
364 } else if (drv_value == PHY_DRV_ODT_34_3) {
365 switch (odt_value) {
366 case PHY_DRV_ODT_240:
367 vref_value_dq = 0x17;
368 break;
369 case PHY_DRV_ODT_120:
370 vref_value_dq = 0x20;
371 break;
372 case PHY_DRV_ODT_60:
373 vref_value_dq = 0x2e;
374 break;
375 default:
376 debug("Invalid ODT value.\n");
377 return -EINVAL;
378 }
379 } else {
380 debug("Invalid DRV value.\n");
381 return -EINVAL;
382 }
383 } else {
384 vref_mode_dq = 0x2; /* LPDDR3 */
385 vref_value_dq = 0x1f;
386 }
387 vref_mode_ac = 0x2;
388 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530389 mode_sel = 0x0;
Jagan Tekib5d46632019-07-16 17:27:07 +0530390 } else if (params->base.dramtype == DDR3) {
391 /* DDR3L */
392 vref_mode_dq = 0x1;
393 vref_value_dq = 0x1f;
394 vref_mode_ac = 0x1;
395 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530396 mode_sel = 0x1;
Jagan Tekib5d46632019-07-16 17:27:07 +0530397 } else {
398 debug("Unknown DRAM type.\n");
399 return -EINVAL;
400 }
401
402 reg_value = (vref_mode_dq << 9) | (0x1 << 8) | vref_value_dq;
403
404 /* PHY_913 PHY_PAD_VREF_CTRL_DQ_0 12bits offset_8 */
405 clrsetbits_le32(&denali_phy[913], 0xfff << 8, reg_value << 8);
406 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_1 12bits offset_0 */
407 clrsetbits_le32(&denali_phy[914], 0xfff, reg_value);
408 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_2 12bits offset_16 */
409 clrsetbits_le32(&denali_phy[914], 0xfff << 16, reg_value << 16);
410 /* PHY_915 PHY_PAD_VREF_CTRL_DQ_3 12bits offset_0 */
411 clrsetbits_le32(&denali_phy[915], 0xfff, reg_value);
412
413 reg_value = (vref_mode_ac << 9) | (0x1 << 8) | vref_value_ac;
414
415 /* PHY_915 PHY_PAD_VREF_CTRL_AC 12bits offset_16 */
416 clrsetbits_le32(&denali_phy[915], 0xfff << 16, reg_value << 16);
417
Jagan Tekib5d46632019-07-16 17:27:07 +0530418 /* PHY_924 PHY_PAD_FDBK_DRIVE */
419 clrsetbits_le32(&denali_phy[924], 0x7 << 15, mode_sel << 15);
420 /* PHY_926 PHY_PAD_DATA_DRIVE */
421 clrsetbits_le32(&denali_phy[926], 0x7 << 6, mode_sel << 6);
422 /* PHY_927 PHY_PAD_DQS_DRIVE */
423 clrsetbits_le32(&denali_phy[927], 0x7 << 6, mode_sel << 6);
424 /* PHY_928 PHY_PAD_ADDR_DRIVE */
425 clrsetbits_le32(&denali_phy[928], 0x7 << 14, mode_sel << 14);
426 /* PHY_929 PHY_PAD_CLK_DRIVE */
427 clrsetbits_le32(&denali_phy[929], 0x7 << 14, mode_sel << 14);
428 /* PHY_935 PHY_PAD_CKE_DRIVE */
429 clrsetbits_le32(&denali_phy[935], 0x7 << 14, mode_sel << 14);
430 /* PHY_937 PHY_PAD_RST_DRIVE */
431 clrsetbits_le32(&denali_phy[937], 0x7 << 14, mode_sel << 14);
432 /* PHY_939 PHY_PAD_CS_DRIVE */
433 clrsetbits_le32(&denali_phy[939], 0x7 << 14, mode_sel << 14);
434
Jagan Teki5d152172019-07-16 17:27:15 +0530435 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
436 /* BOOSTP_EN & BOOSTN_EN */
437 reg_value = ((PHY_BOOSTP_EN << 4) | PHY_BOOSTN_EN);
438 /* PHY_925 PHY_PAD_FDBK_DRIVE2 */
439 clrsetbits_le32(&denali_phy[925], 0xff << 8, reg_value << 8);
440 /* PHY_926 PHY_PAD_DATA_DRIVE */
441 clrsetbits_le32(&denali_phy[926], 0xff << 12, reg_value << 12);
442 /* PHY_927 PHY_PAD_DQS_DRIVE */
443 clrsetbits_le32(&denali_phy[927], 0xff << 14, reg_value << 14);
444 /* PHY_928 PHY_PAD_ADDR_DRIVE */
445 clrsetbits_le32(&denali_phy[928], 0xff << 20, reg_value << 20);
446 /* PHY_929 PHY_PAD_CLK_DRIVE */
447 clrsetbits_le32(&denali_phy[929], 0xff << 22, reg_value << 22);
448 /* PHY_935 PHY_PAD_CKE_DRIVE */
449 clrsetbits_le32(&denali_phy[935], 0xff << 20, reg_value << 20);
450 /* PHY_937 PHY_PAD_RST_DRIVE */
451 clrsetbits_le32(&denali_phy[937], 0xff << 20, reg_value << 20);
452 /* PHY_939 PHY_PAD_CS_DRIVE */
453 clrsetbits_le32(&denali_phy[939], 0xff << 20, reg_value << 20);
Jagan Tekid8681842019-07-16 17:27:16 +0530454
455 /* SLEWP_EN & SLEWN_EN */
456 reg_value = ((PHY_SLEWP_EN << 3) | PHY_SLEWN_EN);
457 /* PHY_924 PHY_PAD_FDBK_DRIVE */
458 clrsetbits_le32(&denali_phy[924], 0x3f << 8, reg_value << 8);
459 /* PHY_926 PHY_PAD_DATA_DRIVE */
460 clrsetbits_le32(&denali_phy[926], 0x3f, reg_value);
461 /* PHY_927 PHY_PAD_DQS_DRIVE */
462 clrsetbits_le32(&denali_phy[927], 0x3f, reg_value);
463 /* PHY_928 PHY_PAD_ADDR_DRIVE */
464 clrsetbits_le32(&denali_phy[928], 0x3f << 8, reg_value << 8);
465 /* PHY_929 PHY_PAD_CLK_DRIVE */
466 clrsetbits_le32(&denali_phy[929], 0x3f << 8, reg_value << 8);
467 /* PHY_935 PHY_PAD_CKE_DRIVE */
468 clrsetbits_le32(&denali_phy[935], 0x3f << 8, reg_value << 8);
469 /* PHY_937 PHY_PAD_RST_DRIVE */
470 clrsetbits_le32(&denali_phy[937], 0x3f << 8, reg_value << 8);
471 /* PHY_939 PHY_PAD_CS_DRIVE */
472 clrsetbits_le32(&denali_phy[939], 0x3f << 8, reg_value << 8);
Jagan Teki5d152172019-07-16 17:27:15 +0530473 }
474
Jagan Tekib5d46632019-07-16 17:27:07 +0530475 /* speed setting */
476 if (params->base.ddr_freq < 400)
477 speed = 0x0;
478 else if (params->base.ddr_freq < 800)
479 speed = 0x1;
480 else if (params->base.ddr_freq < 1200)
481 speed = 0x2;
482 else
483 speed = 0x3;
484
485 /* PHY_924 PHY_PAD_FDBK_DRIVE */
486 clrsetbits_le32(&denali_phy[924], 0x3 << 21, speed << 21);
487 /* PHY_926 PHY_PAD_DATA_DRIVE */
488 clrsetbits_le32(&denali_phy[926], 0x3 << 9, speed << 9);
489 /* PHY_927 PHY_PAD_DQS_DRIVE */
490 clrsetbits_le32(&denali_phy[927], 0x3 << 9, speed << 9);
491 /* PHY_928 PHY_PAD_ADDR_DRIVE */
492 clrsetbits_le32(&denali_phy[928], 0x3 << 17, speed << 17);
493 /* PHY_929 PHY_PAD_CLK_DRIVE */
494 clrsetbits_le32(&denali_phy[929], 0x3 << 17, speed << 17);
495 /* PHY_935 PHY_PAD_CKE_DRIVE */
496 clrsetbits_le32(&denali_phy[935], 0x3 << 17, speed << 17);
497 /* PHY_937 PHY_PAD_RST_DRIVE */
498 clrsetbits_le32(&denali_phy[937], 0x3 << 17, speed << 17);
499 /* PHY_939 PHY_PAD_CS_DRIVE */
500 clrsetbits_le32(&denali_phy[939], 0x3 << 17, speed << 17);
501
Jagan Teki65535a22019-07-16 17:27:17 +0530502 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
503 /* RX_CM_INPUT */
504 reg_value = PHY_RX_CM_INPUT;
505 /* PHY_924 PHY_PAD_FDBK_DRIVE */
506 clrsetbits_le32(&denali_phy[924], 0x1 << 14, reg_value << 14);
507 /* PHY_926 PHY_PAD_DATA_DRIVE */
508 clrsetbits_le32(&denali_phy[926], 0x1 << 11, reg_value << 11);
509 /* PHY_927 PHY_PAD_DQS_DRIVE */
510 clrsetbits_le32(&denali_phy[927], 0x1 << 13, reg_value << 13);
511 /* PHY_928 PHY_PAD_ADDR_DRIVE */
512 clrsetbits_le32(&denali_phy[928], 0x1 << 19, reg_value << 19);
513 /* PHY_929 PHY_PAD_CLK_DRIVE */
514 clrsetbits_le32(&denali_phy[929], 0x1 << 21, reg_value << 21);
515 /* PHY_935 PHY_PAD_CKE_DRIVE */
516 clrsetbits_le32(&denali_phy[935], 0x1 << 19, reg_value << 19);
517 /* PHY_937 PHY_PAD_RST_DRIVE */
518 clrsetbits_le32(&denali_phy[937], 0x1 << 19, reg_value << 19);
519 /* PHY_939 PHY_PAD_CS_DRIVE */
520 clrsetbits_le32(&denali_phy[939], 0x1 << 19, reg_value << 19);
521 }
522
Jagan Tekib5d46632019-07-16 17:27:07 +0530523 return 0;
524}
525
Kever Yang50fb9982017-02-22 16:56:35 +0800526static void set_ds_odt(const struct chan_info *chan,
Jagan Tekia58ff792019-07-15 23:50:58 +0530527 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800528{
529 u32 *denali_phy = chan->publ->denali_phy;
530
531 u32 tsel_idle_en, tsel_wr_en, tsel_rd_en;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530532 u32 tsel_idle_select_p, tsel_rd_select_p;
533 u32 tsel_idle_select_n, tsel_rd_select_n;
534 u32 tsel_wr_select_dq_p, tsel_wr_select_ca_p;
535 u32 tsel_wr_select_dq_n, tsel_wr_select_ca_n;
Kever Yang50fb9982017-02-22 16:56:35 +0800536 u32 reg_value;
537
Jagan Tekia58ff792019-07-15 23:50:58 +0530538 if (params->base.dramtype == LPDDR4) {
Jagan Tekif676c7c2019-07-15 23:50:56 +0530539 tsel_rd_select_p = PHY_DRV_ODT_HI_Z;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530540 tsel_rd_select_n = PHY_DRV_ODT_240;
541
Jagan Tekif676c7c2019-07-15 23:50:56 +0530542 tsel_idle_select_p = PHY_DRV_ODT_HI_Z;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530543 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800544
Jagan Teki5c3251f2019-07-15 23:51:04 +0530545 tsel_wr_select_dq_p = PHY_DRV_ODT_40;
Jagan Teki36667142019-07-15 23:51:00 +0530546 tsel_wr_select_dq_n = PHY_DRV_ODT_40;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530547
548 tsel_wr_select_ca_p = PHY_DRV_ODT_40;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530549 tsel_wr_select_ca_n = PHY_DRV_ODT_40;
Jagan Tekia58ff792019-07-15 23:50:58 +0530550 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +0800551 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530552 tsel_rd_select_n = PHY_DRV_ODT_HI_Z;
553
Kever Yang50fb9982017-02-22 16:56:35 +0800554 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530555 tsel_idle_select_n = PHY_DRV_ODT_HI_Z;
Kever Yang50fb9982017-02-22 16:56:35 +0800556
Jagan Teki5c3251f2019-07-15 23:51:04 +0530557 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530558 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530559
560 tsel_wr_select_ca_p = PHY_DRV_ODT_48;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530561 tsel_wr_select_ca_n = PHY_DRV_ODT_48;
Kever Yang50fb9982017-02-22 16:56:35 +0800562 } else {
563 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530564 tsel_rd_select_n = PHY_DRV_ODT_240;
565
Kever Yang50fb9982017-02-22 16:56:35 +0800566 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530567 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800568
Jagan Teki5c3251f2019-07-15 23:51:04 +0530569 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530570 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530571
572 tsel_wr_select_ca_p = PHY_DRV_ODT_34_3;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530573 tsel_wr_select_ca_n = PHY_DRV_ODT_34_3;
Kever Yang50fb9982017-02-22 16:56:35 +0800574 }
575
Jagan Tekia58ff792019-07-15 23:50:58 +0530576 if (params->base.odt == 1)
Kever Yang50fb9982017-02-22 16:56:35 +0800577 tsel_rd_en = 1;
578 else
579 tsel_rd_en = 0;
580
581 tsel_wr_en = 0;
582 tsel_idle_en = 0;
583
584 /*
585 * phy_dq_tsel_select_X 24bits DENALI_PHY_6/134/262/390 offset_0
586 * sets termination values for read/idle cycles and drive strength
587 * for write cycles for DQ/DM
588 */
589 reg_value = tsel_rd_select_n | (tsel_rd_select_p << 0x4) |
Jagan Tekib3b34392019-07-15 23:51:01 +0530590 (tsel_wr_select_dq_n << 8) | (tsel_wr_select_dq_p << 12) |
Kever Yang50fb9982017-02-22 16:56:35 +0800591 (tsel_idle_select_n << 16) | (tsel_idle_select_p << 20);
592 clrsetbits_le32(&denali_phy[6], 0xffffff, reg_value);
593 clrsetbits_le32(&denali_phy[134], 0xffffff, reg_value);
594 clrsetbits_le32(&denali_phy[262], 0xffffff, reg_value);
595 clrsetbits_le32(&denali_phy[390], 0xffffff, reg_value);
596
597 /*
598 * phy_dqs_tsel_select_X 24bits DENALI_PHY_7/135/263/391 offset_0
599 * sets termination values for read/idle cycles and drive strength
600 * for write cycles for DQS
601 */
602 clrsetbits_le32(&denali_phy[7], 0xffffff, reg_value);
603 clrsetbits_le32(&denali_phy[135], 0xffffff, reg_value);
604 clrsetbits_le32(&denali_phy[263], 0xffffff, reg_value);
605 clrsetbits_le32(&denali_phy[391], 0xffffff, reg_value);
606
607 /* phy_adr_tsel_select_ 8bits DENALI_PHY_544/672/800 offset_0 */
Jagan Teki7caa3e92019-07-15 23:51:03 +0530608 reg_value = tsel_wr_select_ca_n | (tsel_wr_select_ca_p << 0x4);
Jagan Teki539ffed2019-07-16 17:27:19 +0530609 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
610 /* LPDDR4 these register read always return 0, so
611 * can not use clrsetbits_le32(), need to write32
612 */
613 writel((0x300 << 8) | reg_value, &denali_phy[544]);
614 writel((0x300 << 8) | reg_value, &denali_phy[672]);
615 writel((0x300 << 8) | reg_value, &denali_phy[800]);
616 } else {
617 clrsetbits_le32(&denali_phy[544], 0xff, reg_value);
618 clrsetbits_le32(&denali_phy[672], 0xff, reg_value);
619 clrsetbits_le32(&denali_phy[800], 0xff, reg_value);
620 }
Kever Yang50fb9982017-02-22 16:56:35 +0800621
622 /* phy_pad_addr_drive 8bits DENALI_PHY_928 offset_0 */
623 clrsetbits_le32(&denali_phy[928], 0xff, reg_value);
624
625 /* phy_pad_rst_drive 8bits DENALI_PHY_937 offset_0 */
626 clrsetbits_le32(&denali_phy[937], 0xff, reg_value);
627
628 /* phy_pad_cke_drive 8bits DENALI_PHY_935 offset_0 */
629 clrsetbits_le32(&denali_phy[935], 0xff, reg_value);
630
631 /* phy_pad_cs_drive 8bits DENALI_PHY_939 offset_0 */
632 clrsetbits_le32(&denali_phy[939], 0xff, reg_value);
633
634 /* phy_pad_clk_drive 8bits DENALI_PHY_929 offset_0 */
635 clrsetbits_le32(&denali_phy[929], 0xff, reg_value);
636
637 /* phy_pad_fdbk_drive 23bit DENALI_PHY_924/925 */
638 clrsetbits_le32(&denali_phy[924], 0xff,
Jagan Tekib3b34392019-07-15 23:51:01 +0530639 tsel_wr_select_dq_n | (tsel_wr_select_dq_p << 4));
Kever Yang50fb9982017-02-22 16:56:35 +0800640 clrsetbits_le32(&denali_phy[925], 0xff,
641 tsel_rd_select_n | (tsel_rd_select_p << 4));
642
643 /* phy_dq_tsel_enable_X 3bits DENALI_PHY_5/133/261/389 offset_16 */
644 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
645 << 16;
646 clrsetbits_le32(&denali_phy[5], 0x7 << 16, reg_value);
647 clrsetbits_le32(&denali_phy[133], 0x7 << 16, reg_value);
648 clrsetbits_le32(&denali_phy[261], 0x7 << 16, reg_value);
649 clrsetbits_le32(&denali_phy[389], 0x7 << 16, reg_value);
650
651 /* phy_dqs_tsel_enable_X 3bits DENALI_PHY_6/134/262/390 offset_24 */
652 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
653 << 24;
654 clrsetbits_le32(&denali_phy[6], 0x7 << 24, reg_value);
655 clrsetbits_le32(&denali_phy[134], 0x7 << 24, reg_value);
656 clrsetbits_le32(&denali_phy[262], 0x7 << 24, reg_value);
657 clrsetbits_le32(&denali_phy[390], 0x7 << 24, reg_value);
658
659 /* phy_adr_tsel_enable_ 1bit DENALI_PHY_518/646/774 offset_8 */
660 reg_value = tsel_wr_en << 8;
661 clrsetbits_le32(&denali_phy[518], 0x1 << 8, reg_value);
662 clrsetbits_le32(&denali_phy[646], 0x1 << 8, reg_value);
663 clrsetbits_le32(&denali_phy[774], 0x1 << 8, reg_value);
664
665 /* phy_pad_addr_term tsel 1bit DENALI_PHY_933 offset_17 */
666 reg_value = tsel_wr_en << 17;
667 clrsetbits_le32(&denali_phy[933], 0x1 << 17, reg_value);
668 /*
669 * pad_rst/cke/cs/clk_term tsel 1bits
670 * DENALI_PHY_938/936/940/934 offset_17
671 */
672 clrsetbits_le32(&denali_phy[938], 0x1 << 17, reg_value);
673 clrsetbits_le32(&denali_phy[936], 0x1 << 17, reg_value);
674 clrsetbits_le32(&denali_phy[940], 0x1 << 17, reg_value);
675 clrsetbits_le32(&denali_phy[934], 0x1 << 17, reg_value);
676
677 /* phy_pad_fdbk_term 1bit DENALI_PHY_930 offset_17 */
678 clrsetbits_le32(&denali_phy[930], 0x1 << 17, reg_value);
Jagan Tekib5d46632019-07-16 17:27:07 +0530679
680 phy_io_config(chan, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800681}
682
Jagan Tekic9151e22019-07-15 23:58:45 +0530683static void pctl_start(struct dram_info *dram, u8 channel)
684{
685 const struct chan_info *chan = &dram->chan[channel];
686 u32 *denali_ctl = chan->pctl->denali_ctl;
687 u32 *denali_phy = chan->publ->denali_phy;
688 u32 *ddrc0_con = get_ddrc0_con(dram, channel);
689 u32 count = 0;
690 u32 byte, tmp;
691
692 writel(0x01000000, &ddrc0_con);
693
694 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 0x2 << 24);
695
696 while (!(readl(&denali_ctl[203]) & (1 << 3))) {
697 if (count > 1000) {
698 printf("%s: Failed to init pctl for channel %d\n",
699 __func__, channel);
700 while (1)
701 ;
702 }
703
704 udelay(1);
705 count++;
706 }
707
708 writel(0x01000100, &ddrc0_con);
709
710 for (byte = 0; byte < 4; byte++) {
711 tmp = 0x820;
712 writel((tmp << 16) | tmp, &denali_phy[53 + (128 * byte)]);
713 writel((tmp << 16) | tmp, &denali_phy[54 + (128 * byte)]);
714 writel((tmp << 16) | tmp, &denali_phy[55 + (128 * byte)]);
715 writel((tmp << 16) | tmp, &denali_phy[56 + (128 * byte)]);
716 writel((tmp << 16) | tmp, &denali_phy[57 + (128 * byte)]);
717
718 clrsetbits_le32(&denali_phy[58 + (128 * byte)], 0xffff, tmp);
719 }
720
721 clrsetbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT,
722 dram->pwrup_srefresh_exit[channel]);
723}
724
Jagan Teki4ef5c012019-07-15 23:58:44 +0530725static int pctl_cfg(struct dram_info *dram, const struct chan_info *chan,
726 u32 channel, const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800727{
728 u32 *denali_ctl = chan->pctl->denali_ctl;
729 u32 *denali_pi = chan->pi->denali_pi;
730 u32 *denali_phy = chan->publ->denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +0530731 const u32 *params_ctl = params->pctl_regs.denali_ctl;
732 const u32 *params_phy = params->phy_regs.denali_phy;
Kever Yang50fb9982017-02-22 16:56:35 +0800733 u32 tmp, tmp1, tmp2;
Kever Yang50fb9982017-02-22 16:56:35 +0800734
735 /*
736 * work around controller bug:
737 * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed
738 */
739 copy_to_reg(&denali_ctl[1], &params_ctl[1],
740 sizeof(struct rk3399_ddr_pctl_regs) - 4);
741 writel(params_ctl[0], &denali_ctl[0]);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530742
Jagan Tekicc9da9a2019-07-16 17:27:13 +0530743 /*
744 * two channel init at the same time, then ZQ Cal Start
745 * at the same time, it will use the same RZQ, but cannot
746 * start at the same time.
747 *
748 * So, increase tINIT3 for channel 1, will avoid two
749 * channel ZQ Cal Start at the same time
750 */
751 if (params->base.dramtype == LPDDR4 && channel == 1) {
752 tmp = ((params->base.ddr_freq * MHz + 999) / 1000);
753 tmp1 = readl(&denali_ctl[14]);
754 writel(tmp + tmp1, &denali_ctl[14]);
755 }
756
Jagan Tekia58ff792019-07-15 23:50:58 +0530757 copy_to_reg(denali_pi, &params->pi_regs.denali_pi[0],
Kever Yang50fb9982017-02-22 16:56:35 +0800758 sizeof(struct rk3399_ddr_pi_regs));
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530759
Kever Yang50fb9982017-02-22 16:56:35 +0800760 /* rank count need to set for init */
Jagan Tekia58ff792019-07-15 23:50:58 +0530761 set_memory_map(chan, channel, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800762
Jagan Tekia58ff792019-07-15 23:50:58 +0530763 writel(params->phy_regs.denali_phy[910], &denali_phy[910]);
764 writel(params->phy_regs.denali_phy[911], &denali_phy[911]);
765 writel(params->phy_regs.denali_phy[912], &denali_phy[912]);
Kever Yang50fb9982017-02-22 16:56:35 +0800766
Jagan Tekib49b5dc2019-07-16 17:27:14 +0530767 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
768 writel(params->phy_regs.denali_phy[898], &denali_phy[898]);
769 writel(params->phy_regs.denali_phy[919], &denali_phy[919]);
770 }
771
Jagan Tekic9151e22019-07-15 23:58:45 +0530772 dram->pwrup_srefresh_exit[channel] = readl(&denali_ctl[68]) &
773 PWRUP_SREFRESH_EXIT;
Kever Yang50fb9982017-02-22 16:56:35 +0800774 clrbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT);
775
776 /* PHY_DLL_RST_EN */
777 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 1 << 24);
778
779 setbits_le32(&denali_pi[0], START);
780 setbits_le32(&denali_ctl[0], START);
781
Jagan Teki5e927182019-07-16 17:27:12 +0530782 /**
783 * LPDDR4 use PLL bypass mode for init
784 * not need to wait for the PLL to lock
785 */
786 if (params->base.dramtype != LPDDR4) {
787 /* Waiting for phy DLL lock */
788 while (1) {
789 tmp = readl(&denali_phy[920]);
790 tmp1 = readl(&denali_phy[921]);
791 tmp2 = readl(&denali_phy[922]);
792 if ((((tmp >> 16) & 0x1) == 0x1) &&
793 (((tmp1 >> 16) & 0x1) == 0x1) &&
794 (((tmp1 >> 0) & 0x1) == 0x1) &&
795 (((tmp2 >> 0) & 0x1) == 0x1))
796 break;
797 }
Kever Yang50fb9982017-02-22 16:56:35 +0800798 }
799
800 copy_to_reg(&denali_phy[896], &params_phy[896], (958 - 895) * 4);
801 copy_to_reg(&denali_phy[0], &params_phy[0], (90 - 0 + 1) * 4);
802 copy_to_reg(&denali_phy[128], &params_phy[128], (218 - 128 + 1) * 4);
803 copy_to_reg(&denali_phy[256], &params_phy[256], (346 - 256 + 1) * 4);
804 copy_to_reg(&denali_phy[384], &params_phy[384], (474 - 384 + 1) * 4);
805 copy_to_reg(&denali_phy[512], &params_phy[512], (549 - 512 + 1) * 4);
806 copy_to_reg(&denali_phy[640], &params_phy[640], (677 - 640 + 1) * 4);
807 copy_to_reg(&denali_phy[768], &params_phy[768], (805 - 768 + 1) * 4);
Jagan Tekia58ff792019-07-15 23:50:58 +0530808 set_ds_odt(chan, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800809
810 /*
811 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_84/212/340/468 offset_8
812 * dqs_tsel_wr_end[7:4] add Half cycle
813 */
814 tmp = (readl(&denali_phy[84]) >> 8) & 0xff;
815 clrsetbits_le32(&denali_phy[84], 0xff << 8, (tmp + 0x10) << 8);
816 tmp = (readl(&denali_phy[212]) >> 8) & 0xff;
817 clrsetbits_le32(&denali_phy[212], 0xff << 8, (tmp + 0x10) << 8);
818 tmp = (readl(&denali_phy[340]) >> 8) & 0xff;
819 clrsetbits_le32(&denali_phy[340], 0xff << 8, (tmp + 0x10) << 8);
820 tmp = (readl(&denali_phy[468]) >> 8) & 0xff;
821 clrsetbits_le32(&denali_phy[468], 0xff << 8, (tmp + 0x10) << 8);
822
823 /*
824 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_83/211/339/467 offset_8
825 * dq_tsel_wr_end[7:4] add Half cycle
826 */
827 tmp = (readl(&denali_phy[83]) >> 16) & 0xff;
828 clrsetbits_le32(&denali_phy[83], 0xff << 16, (tmp + 0x10) << 16);
829 tmp = (readl(&denali_phy[211]) >> 16) & 0xff;
830 clrsetbits_le32(&denali_phy[211], 0xff << 16, (tmp + 0x10) << 16);
831 tmp = (readl(&denali_phy[339]) >> 16) & 0xff;
832 clrsetbits_le32(&denali_phy[339], 0xff << 16, (tmp + 0x10) << 16);
833 tmp = (readl(&denali_phy[467]) >> 16) & 0xff;
834 clrsetbits_le32(&denali_phy[467], 0xff << 16, (tmp + 0x10) << 16);
835
Kever Yang50fb9982017-02-22 16:56:35 +0800836 return 0;
837}
838
839static void select_per_cs_training_index(const struct chan_info *chan,
840 u32 rank)
841{
842 u32 *denali_phy = chan->publ->denali_phy;
843
844 /* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */
Jagan Tekif676c7c2019-07-15 23:50:56 +0530845 if ((readl(&denali_phy[84]) >> 16) & 1) {
Kever Yang50fb9982017-02-22 16:56:35 +0800846 /*
847 * PHY_8/136/264/392
848 * phy_per_cs_training_index_X 1bit offset_24
849 */
850 clrsetbits_le32(&denali_phy[8], 0x1 << 24, rank << 24);
851 clrsetbits_le32(&denali_phy[136], 0x1 << 24, rank << 24);
852 clrsetbits_le32(&denali_phy[264], 0x1 << 24, rank << 24);
853 clrsetbits_le32(&denali_phy[392], 0x1 << 24, rank << 24);
854 }
855}
856
857static void override_write_leveling_value(const struct chan_info *chan)
858{
859 u32 *denali_ctl = chan->pctl->denali_ctl;
860 u32 *denali_phy = chan->publ->denali_phy;
861 u32 byte;
862
863 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
864 setbits_le32(&denali_phy[896], 1);
865
866 /*
867 * PHY_8/136/264/392
868 * phy_per_cs_training_multicast_en_X 1bit offset_16
869 */
870 clrsetbits_le32(&denali_phy[8], 0x1 << 16, 1 << 16);
871 clrsetbits_le32(&denali_phy[136], 0x1 << 16, 1 << 16);
872 clrsetbits_le32(&denali_phy[264], 0x1 << 16, 1 << 16);
873 clrsetbits_le32(&denali_phy[392], 0x1 << 16, 1 << 16);
874
875 for (byte = 0; byte < 4; byte++)
876 clrsetbits_le32(&denali_phy[63 + (128 * byte)], 0xffff << 16,
877 0x200 << 16);
878
879 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
880 clrbits_le32(&denali_phy[896], 1);
881
882 /* CTL_200 ctrlupd_req 1bit offset_8 */
883 clrsetbits_le32(&denali_ctl[200], 0x1 << 8, 0x1 << 8);
884}
885
886static int data_training_ca(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530887 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800888{
889 u32 *denali_pi = chan->pi->denali_pi;
890 u32 *denali_phy = chan->publ->denali_phy;
891 u32 i, tmp;
892 u32 obs_0, obs_1, obs_2, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +0530893 u32 rank = params->ch[channel].cap_info.rank;
Jagan Tekibafcc142019-07-15 23:58:41 +0530894 u32 rank_mask;
Kever Yang50fb9982017-02-22 16:56:35 +0800895
Jagan Tekia6079612019-07-15 23:58:40 +0530896 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
897 writel(0x00003f7c, (&denali_pi[175]));
898
Jagan Tekif05675e2019-07-16 17:27:09 +0530899 if (params->base.dramtype == LPDDR4)
900 rank_mask = (rank == 1) ? 0x5 : 0xf;
901 else
902 rank_mask = (rank == 1) ? 0x1 : 0x3;
Jagan Tekibafcc142019-07-15 23:58:41 +0530903
904 for (i = 0; i < 4; i++) {
905 if (!(rank_mask & (1 << i)))
906 continue;
907
Kever Yang50fb9982017-02-22 16:56:35 +0800908 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530909
Kever Yang50fb9982017-02-22 16:56:35 +0800910 /* PI_100 PI_CALVL_EN:RW:8:2 */
911 clrsetbits_le32(&denali_pi[100], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530912
Kever Yang50fb9982017-02-22 16:56:35 +0800913 /* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */
914 clrsetbits_le32(&denali_pi[92],
915 (0x1 << 16) | (0x3 << 24),
916 (0x1 << 16) | (i << 24));
917
918 /* Waiting for training complete */
919 while (1) {
920 /* PI_174 PI_INT_STATUS:RD:8:18 */
921 tmp = readl(&denali_pi[174]) >> 8;
922 /*
923 * check status obs
924 * PHY_532/660/789 phy_adr_calvl_obs1_:0:32
925 */
926 obs_0 = readl(&denali_phy[532]);
927 obs_1 = readl(&denali_phy[660]);
928 obs_2 = readl(&denali_phy[788]);
929 if (((obs_0 >> 30) & 0x3) ||
930 ((obs_1 >> 30) & 0x3) ||
931 ((obs_2 >> 30) & 0x3))
932 obs_err = 1;
933 if ((((tmp >> 11) & 0x1) == 0x1) &&
934 (((tmp >> 13) & 0x1) == 0x1) &&
935 (((tmp >> 5) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +0530936 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800937 break;
938 else if ((((tmp >> 5) & 0x1) == 0x1) ||
939 (obs_err == 1))
940 return -EIO;
941 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530942
Kever Yang50fb9982017-02-22 16:56:35 +0800943 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
944 writel(0x00003f7c, (&denali_pi[175]));
945 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530946
Kever Yang50fb9982017-02-22 16:56:35 +0800947 clrbits_le32(&denali_pi[100], 0x3 << 8);
948
949 return 0;
950}
951
952static int data_training_wl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530953 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800954{
955 u32 *denali_pi = chan->pi->denali_pi;
956 u32 *denali_phy = chan->publ->denali_phy;
957 u32 i, tmp;
958 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +0530959 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +0800960
Jagan Tekia6079612019-07-15 23:58:40 +0530961 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
962 writel(0x00003f7c, (&denali_pi[175]));
963
Kever Yang50fb9982017-02-22 16:56:35 +0800964 for (i = 0; i < rank; i++) {
965 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530966
Kever Yang50fb9982017-02-22 16:56:35 +0800967 /* PI_60 PI_WRLVL_EN:RW:8:2 */
968 clrsetbits_le32(&denali_pi[60], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530969
Kever Yang50fb9982017-02-22 16:56:35 +0800970 /* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */
971 clrsetbits_le32(&denali_pi[59],
972 (0x1 << 8) | (0x3 << 16),
973 (0x1 << 8) | (i << 16));
974
975 /* Waiting for training complete */
976 while (1) {
977 /* PI_174 PI_INT_STATUS:RD:8:18 */
978 tmp = readl(&denali_pi[174]) >> 8;
979
980 /*
981 * check status obs, if error maybe can not
982 * get leveling done PHY_40/168/296/424
983 * phy_wrlvl_status_obs_X:0:13
984 */
985 obs_0 = readl(&denali_phy[40]);
986 obs_1 = readl(&denali_phy[168]);
987 obs_2 = readl(&denali_phy[296]);
988 obs_3 = readl(&denali_phy[424]);
989 if (((obs_0 >> 12) & 0x1) ||
990 ((obs_1 >> 12) & 0x1) ||
991 ((obs_2 >> 12) & 0x1) ||
992 ((obs_3 >> 12) & 0x1))
993 obs_err = 1;
994 if ((((tmp >> 10) & 0x1) == 0x1) &&
995 (((tmp >> 13) & 0x1) == 0x1) &&
996 (((tmp >> 4) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +0530997 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800998 break;
999 else if ((((tmp >> 4) & 0x1) == 0x1) ||
1000 (obs_err == 1))
1001 return -EIO;
1002 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301003
Kever Yang50fb9982017-02-22 16:56:35 +08001004 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1005 writel(0x00003f7c, (&denali_pi[175]));
1006 }
1007
1008 override_write_leveling_value(chan);
1009 clrbits_le32(&denali_pi[60], 0x3 << 8);
1010
1011 return 0;
1012}
1013
1014static int data_training_rg(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301015 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001016{
1017 u32 *denali_pi = chan->pi->denali_pi;
1018 u32 *denali_phy = chan->publ->denali_phy;
1019 u32 i, tmp;
1020 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +05301021 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001022
Jagan Tekia6079612019-07-15 23:58:40 +05301023 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1024 writel(0x00003f7c, (&denali_pi[175]));
1025
Kever Yang50fb9982017-02-22 16:56:35 +08001026 for (i = 0; i < rank; i++) {
1027 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301028
Kever Yang50fb9982017-02-22 16:56:35 +08001029 /* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */
1030 clrsetbits_le32(&denali_pi[80], 0x3 << 24, 0x2 << 24);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301031
Kever Yang50fb9982017-02-22 16:56:35 +08001032 /*
1033 * PI_74 PI_RDLVL_GATE_REQ:WR:16:1
1034 * PI_RDLVL_CS:RW:24:2
1035 */
1036 clrsetbits_le32(&denali_pi[74],
1037 (0x1 << 16) | (0x3 << 24),
1038 (0x1 << 16) | (i << 24));
1039
1040 /* Waiting for training complete */
1041 while (1) {
1042 /* PI_174 PI_INT_STATUS:RD:8:18 */
1043 tmp = readl(&denali_pi[174]) >> 8;
1044
1045 /*
1046 * check status obs
1047 * PHY_43/171/299/427
1048 * PHY_GTLVL_STATUS_OBS_x:16:8
1049 */
1050 obs_0 = readl(&denali_phy[43]);
1051 obs_1 = readl(&denali_phy[171]);
1052 obs_2 = readl(&denali_phy[299]);
1053 obs_3 = readl(&denali_phy[427]);
1054 if (((obs_0 >> (16 + 6)) & 0x3) ||
1055 ((obs_1 >> (16 + 6)) & 0x3) ||
1056 ((obs_2 >> (16 + 6)) & 0x3) ||
1057 ((obs_3 >> (16 + 6)) & 0x3))
1058 obs_err = 1;
1059 if ((((tmp >> 9) & 0x1) == 0x1) &&
1060 (((tmp >> 13) & 0x1) == 0x1) &&
1061 (((tmp >> 3) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +05301062 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001063 break;
1064 else if ((((tmp >> 3) & 0x1) == 0x1) ||
1065 (obs_err == 1))
1066 return -EIO;
1067 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301068
Kever Yang50fb9982017-02-22 16:56:35 +08001069 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1070 writel(0x00003f7c, (&denali_pi[175]));
1071 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301072
Kever Yang50fb9982017-02-22 16:56:35 +08001073 clrbits_le32(&denali_pi[80], 0x3 << 24);
1074
1075 return 0;
1076}
1077
1078static int data_training_rl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301079 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001080{
1081 u32 *denali_pi = chan->pi->denali_pi;
1082 u32 i, tmp;
Jagan Teki97867c82019-07-15 23:51:05 +05301083 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001084
Jagan Tekia6079612019-07-15 23:58:40 +05301085 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1086 writel(0x00003f7c, (&denali_pi[175]));
1087
Kever Yang50fb9982017-02-22 16:56:35 +08001088 for (i = 0; i < rank; i++) {
1089 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301090
Kever Yang50fb9982017-02-22 16:56:35 +08001091 /* PI_80 PI_RDLVL_EN:RW:16:2 */
1092 clrsetbits_le32(&denali_pi[80], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301093
Kever Yang50fb9982017-02-22 16:56:35 +08001094 /* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */
1095 clrsetbits_le32(&denali_pi[74],
1096 (0x1 << 8) | (0x3 << 24),
1097 (0x1 << 8) | (i << 24));
1098
1099 /* Waiting for training complete */
1100 while (1) {
1101 /* PI_174 PI_INT_STATUS:RD:8:18 */
1102 tmp = readl(&denali_pi[174]) >> 8;
1103
1104 /*
1105 * make sure status obs not report error bit
1106 * PHY_46/174/302/430
1107 * phy_rdlvl_status_obs_X:16:8
1108 */
1109 if ((((tmp >> 8) & 0x1) == 0x1) &&
1110 (((tmp >> 13) & 0x1) == 0x1) &&
1111 (((tmp >> 2) & 0x1) == 0x0))
1112 break;
1113 else if (((tmp >> 2) & 0x1) == 0x1)
1114 return -EIO;
1115 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301116
Kever Yang50fb9982017-02-22 16:56:35 +08001117 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1118 writel(0x00003f7c, (&denali_pi[175]));
1119 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301120
Kever Yang50fb9982017-02-22 16:56:35 +08001121 clrbits_le32(&denali_pi[80], 0x3 << 16);
1122
1123 return 0;
1124}
1125
1126static int data_training_wdql(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301127 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001128{
1129 u32 *denali_pi = chan->pi->denali_pi;
1130 u32 i, tmp;
Jagan Teki97867c82019-07-15 23:51:05 +05301131 u32 rank = params->ch[channel].cap_info.rank;
Jagan Teki87723592019-07-15 23:58:42 +05301132 u32 rank_mask;
Kever Yang50fb9982017-02-22 16:56:35 +08001133
Jagan Tekia6079612019-07-15 23:58:40 +05301134 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1135 writel(0x00003f7c, (&denali_pi[175]));
1136
Jagan Tekid7504c02019-07-16 17:27:10 +05301137 if (params->base.dramtype == LPDDR4)
1138 rank_mask = (rank == 1) ? 0x5 : 0xf;
1139 else
1140 rank_mask = (rank == 1) ? 0x1 : 0x3;
Jagan Teki87723592019-07-15 23:58:42 +05301141
1142 for (i = 0; i < 4; i++) {
1143 if (!(rank_mask & (1 << i)))
1144 continue;
1145
Kever Yang50fb9982017-02-22 16:56:35 +08001146 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301147
Kever Yang50fb9982017-02-22 16:56:35 +08001148 /*
1149 * disable PI_WDQLVL_VREF_EN before wdq leveling?
1150 * PI_181 PI_WDQLVL_VREF_EN:RW:8:1
1151 */
1152 clrbits_le32(&denali_pi[181], 0x1 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301153
Kever Yang50fb9982017-02-22 16:56:35 +08001154 /* PI_124 PI_WDQLVL_EN:RW:16:2 */
1155 clrsetbits_le32(&denali_pi[124], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301156
Kever Yang50fb9982017-02-22 16:56:35 +08001157 /* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */
1158 clrsetbits_le32(&denali_pi[121],
1159 (0x1 << 8) | (0x3 << 16),
1160 (0x1 << 8) | (i << 16));
1161
1162 /* Waiting for training complete */
1163 while (1) {
1164 /* PI_174 PI_INT_STATUS:RD:8:18 */
1165 tmp = readl(&denali_pi[174]) >> 8;
1166 if ((((tmp >> 12) & 0x1) == 0x1) &&
1167 (((tmp >> 13) & 0x1) == 0x1) &&
1168 (((tmp >> 6) & 0x1) == 0x0))
1169 break;
1170 else if (((tmp >> 6) & 0x1) == 0x1)
1171 return -EIO;
1172 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301173
Kever Yang50fb9982017-02-22 16:56:35 +08001174 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1175 writel(0x00003f7c, (&denali_pi[175]));
1176 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301177
Kever Yang50fb9982017-02-22 16:56:35 +08001178 clrbits_le32(&denali_pi[124], 0x3 << 16);
1179
1180 return 0;
1181}
1182
1183static int data_training(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301184 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +08001185 u32 training_flag)
1186{
1187 u32 *denali_phy = chan->publ->denali_phy;
Jagan Teki6214ff22019-07-15 23:58:39 +05301188 int ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001189
1190 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1191 setbits_le32(&denali_phy[927], (1 << 22));
1192
1193 if (training_flag == PI_FULL_TRAINING) {
Jagan Tekia58ff792019-07-15 23:50:58 +05301194 if (params->base.dramtype == LPDDR4) {
Kever Yang50fb9982017-02-22 16:56:35 +08001195 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1196 PI_READ_GATE_TRAINING |
1197 PI_READ_LEVELING | PI_WDQ_LEVELING;
Jagan Tekia58ff792019-07-15 23:50:58 +05301198 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +08001199 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1200 PI_READ_GATE_TRAINING;
Jagan Tekia58ff792019-07-15 23:50:58 +05301201 } else if (params->base.dramtype == DDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +08001202 training_flag = PI_WRITE_LEVELING |
1203 PI_READ_GATE_TRAINING |
1204 PI_READ_LEVELING;
1205 }
1206 }
1207
1208 /* ca training(LPDDR4,LPDDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301209 if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING) {
1210 ret = data_training_ca(chan, channel, params);
1211 if (ret < 0) {
1212 debug("%s: data training ca failed\n", __func__);
1213 return ret;
1214 }
1215 }
Kever Yang50fb9982017-02-22 16:56:35 +08001216
1217 /* write leveling(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301218 if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING) {
1219 ret = data_training_wl(chan, channel, params);
1220 if (ret < 0) {
1221 debug("%s: data training wl failed\n", __func__);
1222 return ret;
1223 }
1224 }
Kever Yang50fb9982017-02-22 16:56:35 +08001225
1226 /* read gate training(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301227 if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING) {
1228 ret = data_training_rg(chan, channel, params);
1229 if (ret < 0) {
1230 debug("%s: data training rg failed\n", __func__);
1231 return ret;
1232 }
1233 }
Kever Yang50fb9982017-02-22 16:56:35 +08001234
1235 /* read leveling(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301236 if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING) {
1237 ret = data_training_rl(chan, channel, params);
1238 if (ret < 0) {
1239 debug("%s: data training rl failed\n", __func__);
1240 return ret;
1241 }
1242 }
Kever Yang50fb9982017-02-22 16:56:35 +08001243
1244 /* wdq leveling(LPDDR4 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301245 if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING) {
1246 ret = data_training_wdql(chan, channel, params);
1247 if (ret < 0) {
1248 debug("%s: data training wdql failed\n", __func__);
1249 return ret;
1250 }
1251 }
Kever Yang50fb9982017-02-22 16:56:35 +08001252
1253 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1254 clrbits_le32(&denali_phy[927], (1 << 22));
1255
1256 return 0;
1257}
1258
1259static void set_ddrconfig(const struct chan_info *chan,
Jagan Tekia58ff792019-07-15 23:50:58 +05301260 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +08001261 unsigned char channel, u32 ddrconfig)
1262{
1263 /* only need to set ddrconfig */
1264 struct rk3399_msch_regs *ddr_msch_regs = chan->msch;
1265 unsigned int cs0_cap = 0;
1266 unsigned int cs1_cap = 0;
1267
Jagan Teki97867c82019-07-15 23:51:05 +05301268 cs0_cap = (1 << (params->ch[channel].cap_info.cs0_row
1269 + params->ch[channel].cap_info.col
1270 + params->ch[channel].cap_info.bk
1271 + params->ch[channel].cap_info.bw - 20));
1272 if (params->ch[channel].cap_info.rank > 1)
1273 cs1_cap = cs0_cap >> (params->ch[channel].cap_info.cs0_row
1274 - params->ch[channel].cap_info.cs1_row);
1275 if (params->ch[channel].cap_info.row_3_4) {
Kever Yang50fb9982017-02-22 16:56:35 +08001276 cs0_cap = cs0_cap * 3 / 4;
1277 cs1_cap = cs1_cap * 3 / 4;
1278 }
1279
1280 writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf);
1281 writel(((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8),
1282 &ddr_msch_regs->ddrsize);
1283}
1284
1285static void dram_all_config(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +05301286 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001287{
Jagan Teki2d337122019-07-16 17:27:00 +05301288 u32 sys_reg2 = 0;
Jagan Teki9d8769c2019-07-16 17:27:01 +05301289 u32 sys_reg3 = 0;
Kever Yang50fb9982017-02-22 16:56:35 +08001290 unsigned int channel, idx;
1291
Jagan Teki2d337122019-07-16 17:27:00 +05301292 sys_reg2 |= SYS_REG_ENC_DDRTYPE(params->base.dramtype);
1293 sys_reg2 |= SYS_REG_ENC_NUM_CH(params->base.num_channels);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301294
Kever Yang50fb9982017-02-22 16:56:35 +08001295 for (channel = 0, idx = 0;
Jagan Tekia58ff792019-07-15 23:50:58 +05301296 (idx < params->base.num_channels) && (channel < 2);
Kever Yang50fb9982017-02-22 16:56:35 +08001297 channel++) {
Jagan Tekia58ff792019-07-15 23:50:58 +05301298 const struct rk3399_sdram_channel *info = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +08001299 struct rk3399_msch_regs *ddr_msch_regs;
1300 const struct rk3399_msch_timings *noc_timing;
1301
Jagan Teki97867c82019-07-15 23:51:05 +05301302 if (params->ch[channel].cap_info.col == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001303 continue;
1304 idx++;
Jagan Teki2d337122019-07-16 17:27:00 +05301305 sys_reg2 |= SYS_REG_ENC_ROW_3_4(info->cap_info.row_3_4, channel);
1306 sys_reg2 |= SYS_REG_ENC_CHINFO(channel);
1307 sys_reg2 |= SYS_REG_ENC_RANK(info->cap_info.rank, channel);
1308 sys_reg2 |= SYS_REG_ENC_COL(info->cap_info.col, channel);
1309 sys_reg2 |= SYS_REG_ENC_BK(info->cap_info.bk, channel);
Jagan Teki2d337122019-07-16 17:27:00 +05301310 sys_reg2 |= SYS_REG_ENC_BW(info->cap_info.bw, channel);
1311 sys_reg2 |= SYS_REG_ENC_DBW(info->cap_info.dbw, channel);
Jagan Teki9d8769c2019-07-16 17:27:01 +05301312 SYS_REG_ENC_CS0_ROW(info->cap_info.cs0_row, sys_reg2, sys_reg3, channel);
1313 if (info->cap_info.cs1_row)
1314 SYS_REG_ENC_CS1_ROW(info->cap_info.cs1_row, sys_reg2,
1315 sys_reg3, channel);
1316 sys_reg3 |= SYS_REG_ENC_CS1_COL(info->cap_info.col, channel);
Jagan Teki932dd962019-07-16 17:27:04 +05301317 sys_reg3 |= SYS_REG_ENC_VERSION(DDR_SYS_REG_VERSION);
Kever Yang50fb9982017-02-22 16:56:35 +08001318
1319 ddr_msch_regs = dram->chan[channel].msch;
Jagan Tekia58ff792019-07-15 23:50:58 +05301320 noc_timing = &params->ch[channel].noc_timings;
Kever Yang50fb9982017-02-22 16:56:35 +08001321 writel(noc_timing->ddrtiminga0,
1322 &ddr_msch_regs->ddrtiminga0);
1323 writel(noc_timing->ddrtimingb0,
1324 &ddr_msch_regs->ddrtimingb0);
Jagan Teki5465f9b2019-07-16 17:27:05 +05301325 writel(noc_timing->ddrtimingc0.d32,
Kever Yang50fb9982017-02-22 16:56:35 +08001326 &ddr_msch_regs->ddrtimingc0);
1327 writel(noc_timing->devtodev0,
1328 &ddr_msch_regs->devtodev0);
Jagan Teki264a09f2019-07-16 17:27:06 +05301329 writel(noc_timing->ddrmode.d32,
Kever Yang50fb9982017-02-22 16:56:35 +08001330 &ddr_msch_regs->ddrmode);
1331
Jagan Tekib02c5482019-07-16 17:27:20 +05301332 /**
1333 * rank 1 memory clock disable (dfi_dram_clk_disable = 1)
1334 *
1335 * The hardware for LPDDR4 with
1336 * - CLK0P/N connect to lower 16-bits
1337 * - CLK1P/N connect to higher 16-bits
1338 *
1339 * dfi dram clk is configured via CLK1P/N, so disabling
1340 * dfi dram clk will disable the CLK1P/N as well for lpddr4.
1341 */
1342 if (params->ch[channel].cap_info.rank == 1 &&
1343 params->base.dramtype != LPDDR4)
Kever Yang50fb9982017-02-22 16:56:35 +08001344 setbits_le32(&dram->chan[channel].pctl->denali_ctl[276],
1345 1 << 17);
1346 }
1347
Jagan Teki2d337122019-07-16 17:27:00 +05301348 writel(sys_reg2, &dram->pmugrf->os_reg2);
Jagan Teki9d8769c2019-07-16 17:27:01 +05301349 writel(sys_reg3, &dram->pmugrf->os_reg3);
Kever Yang50fb9982017-02-22 16:56:35 +08001350 rk_clrsetreg(&dram->pmusgrf->soc_con4, 0x1f << 10,
Jagan Tekia58ff792019-07-15 23:50:58 +05301351 params->base.stride << 10);
Kever Yang50fb9982017-02-22 16:56:35 +08001352
1353 /* reboot hold register set */
1354 writel(PRESET_SGRF_HOLD(0) | PRESET_GPIO0_HOLD(1) |
1355 PRESET_GPIO1_HOLD(1),
1356 &dram->pmucru->pmucru_rstnhold_con[1]);
1357 clrsetbits_le32(&dram->cru->glb_rst_con, 0x3, 0x3);
1358}
1359
1360static int switch_to_phy_index1(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +05301361 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001362{
1363 u32 channel;
1364 u32 *denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +05301365 u32 ch_count = params->base.num_channels;
Kever Yang50fb9982017-02-22 16:56:35 +08001366 int ret;
1367 int i = 0;
1368
1369 writel(RK_CLRSETBITS(0x03 << 4 | 1 << 2 | 1,
1370 1 << 4 | 1 << 2 | 1),
1371 &dram->cic->cic_ctrl0);
1372 while (!(readl(&dram->cic->cic_status0) & (1 << 2))) {
1373 mdelay(10);
1374 i++;
1375 if (i > 10) {
1376 debug("index1 frequency change overtime\n");
1377 return -ETIME;
1378 }
1379 }
1380
1381 i = 0;
1382 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &dram->cic->cic_ctrl0);
1383 while (!(readl(&dram->cic->cic_status0) & (1 << 0))) {
1384 mdelay(10);
Heinrich Schuchardt80516592018-03-18 12:10:55 +01001385 i++;
Kever Yang50fb9982017-02-22 16:56:35 +08001386 if (i > 10) {
1387 debug("index1 frequency done overtime\n");
1388 return -ETIME;
1389 }
1390 }
1391
1392 for (channel = 0; channel < ch_count; channel++) {
1393 denali_phy = dram->chan[channel].publ->denali_phy;
1394 clrsetbits_le32(&denali_phy[896], (0x3 << 8) | 1, 1 << 8);
1395 ret = data_training(&dram->chan[channel], channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301396 params, PI_FULL_TRAINING);
Jagan Teki6214ff22019-07-15 23:58:39 +05301397 if (ret < 0) {
Kever Yang50fb9982017-02-22 16:56:35 +08001398 debug("index1 training failed\n");
1399 return ret;
1400 }
1401 }
1402
1403 return 0;
1404}
1405
Jagan Teki2525fae2019-07-15 23:58:52 +05301406static unsigned char calculate_stride(struct rk3399_sdram_params *params)
1407{
1408 unsigned int stride = params->base.stride;
1409 unsigned int channel, chinfo = 0;
1410 unsigned int ch_cap[2] = {0, 0};
1411 u64 cap;
1412
1413 for (channel = 0; channel < 2; channel++) {
1414 unsigned int cs0_cap = 0;
1415 unsigned int cs1_cap = 0;
1416 struct sdram_cap_info *cap_info = &params->ch[channel].cap_info;
1417
1418 if (cap_info->col == 0)
1419 continue;
1420
1421 cs0_cap = (1 << (cap_info->cs0_row + cap_info->col +
1422 cap_info->bk + cap_info->bw - 20));
1423 if (cap_info->rank > 1)
1424 cs1_cap = cs0_cap >> (cap_info->cs0_row
1425 - cap_info->cs1_row);
1426 if (cap_info->row_3_4) {
1427 cs0_cap = cs0_cap * 3 / 4;
1428 cs1_cap = cs1_cap * 3 / 4;
1429 }
1430 ch_cap[channel] = cs0_cap + cs1_cap;
1431 chinfo |= 1 << channel;
1432 }
1433
Jagan Teki874dede2019-07-15 23:58:53 +05301434 /* stride calculation for 1 channel */
1435 if (params->base.num_channels == 1 && chinfo & 1)
1436 return 0x17; /* channel a */
1437
Jagan Teki2525fae2019-07-15 23:58:52 +05301438 /* stride calculation for 2 channels, default gstride type is 256B */
1439 if (ch_cap[0] == ch_cap[1]) {
1440 cap = ch_cap[0] + ch_cap[1];
1441 switch (cap) {
1442 /* 512MB */
1443 case 512:
1444 stride = 0;
1445 break;
1446 /* 1GB */
1447 case 1024:
1448 stride = 0x5;
1449 break;
1450 /*
1451 * 768MB + 768MB same as total 2GB memory
1452 * useful space: 0-768MB 1GB-1792MB
1453 */
1454 case 1536:
1455 /* 2GB */
1456 case 2048:
1457 stride = 0x9;
1458 break;
1459 /* 1536MB + 1536MB */
1460 case 3072:
1461 stride = 0x11;
1462 break;
1463 /* 4GB */
1464 case 4096:
1465 stride = 0xD;
1466 break;
1467 default:
1468 printf("%s: Unable to calculate stride for ", __func__);
1469 print_size((cap * (1 << 20)), " capacity\n");
1470 break;
1471 }
1472 }
1473
Jagan Teki8eed4a42019-07-15 23:58:55 +05301474 sdram_print_stride(stride);
1475
Jagan Teki2525fae2019-07-15 23:58:52 +05301476 return stride;
1477}
1478
Jagan Teki43485e12019-07-15 23:58:54 +05301479static void clear_channel_params(struct rk3399_sdram_params *params, u8 channel)
1480{
1481 params->ch[channel].cap_info.rank = 0;
1482 params->ch[channel].cap_info.col = 0;
1483 params->ch[channel].cap_info.bk = 0;
1484 params->ch[channel].cap_info.bw = 32;
1485 params->ch[channel].cap_info.dbw = 32;
1486 params->ch[channel].cap_info.row_3_4 = 0;
1487 params->ch[channel].cap_info.cs0_row = 0;
1488 params->ch[channel].cap_info.cs1_row = 0;
1489 params->ch[channel].cap_info.ddrconfig = 0;
1490}
1491
1492static int pctl_init(struct dram_info *dram, struct rk3399_sdram_params *params)
1493{
1494 int channel;
1495 int ret;
1496
1497 for (channel = 0; channel < 2; channel++) {
1498 const struct chan_info *chan = &dram->chan[channel];
1499 struct rk3399_cru *cru = dram->cru;
1500 struct rk3399_ddr_publ_regs *publ = chan->publ;
1501
1502 phy_pctrl_reset(cru, channel);
1503 phy_dll_bypass_set(publ, params->base.ddr_freq);
1504
1505 ret = pctl_cfg(dram, chan, channel, params);
1506 if (ret < 0) {
1507 printf("%s: pctl config failed\n", __func__);
1508 return ret;
1509 }
1510
1511 /* start to trigger initialization */
1512 pctl_start(dram, channel);
1513 }
1514
1515 return 0;
1516}
1517
Kever Yang50fb9982017-02-22 16:56:35 +08001518static int sdram_init(struct dram_info *dram,
Jagan Teki2525fae2019-07-15 23:58:52 +05301519 struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001520{
Jagan Tekia58ff792019-07-15 23:50:58 +05301521 unsigned char dramtype = params->base.dramtype;
1522 unsigned int ddr_freq = params->base.ddr_freq;
Jagan Teki43485e12019-07-15 23:58:54 +05301523 u32 training_flag = PI_READ_GATE_TRAINING;
1524 int channel, ch, rank;
Jagan Teki2ef77ed2019-07-15 23:50:59 +05301525 int ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001526
1527 debug("Starting SDRAM initialization...\n");
1528
Philipp Tomsich39dce4a2017-05-31 18:16:35 +02001529 if ((dramtype == DDR3 && ddr_freq > 933) ||
Kever Yang50fb9982017-02-22 16:56:35 +08001530 (dramtype == LPDDR3 && ddr_freq > 933) ||
1531 (dramtype == LPDDR4 && ddr_freq > 800)) {
1532 debug("SDRAM frequency is to high!");
1533 return -E2BIG;
1534 }
1535
Jagan Teki43485e12019-07-15 23:58:54 +05301536 for (ch = 0; ch < 2; ch++) {
1537 params->ch[ch].cap_info.rank = 2;
1538 for (rank = 2; rank != 0; rank--) {
1539 ret = pctl_init(dram, params);
1540 if (ret < 0) {
1541 printf("%s: pctl init failed\n", __func__);
1542 return ret;
1543 }
1544
1545 /* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */
1546 if (dramtype == LPDDR3)
1547 udelay(10);
1548
1549 params->ch[ch].cap_info.rank = rank;
1550
1551 /*
1552 * LPDDR3 CA training msut be trigger before
1553 * other training.
1554 * DDR3 is not have CA training.
1555 */
1556 if (params->base.dramtype == LPDDR3)
1557 training_flag |= PI_CA_TRAINING;
1558
1559 if (!(data_training(&dram->chan[ch], ch,
1560 params, training_flag)))
1561 break;
1562 }
1563 /* Computed rank with associated channel number */
1564 params->ch[ch].cap_info.rank = rank;
1565 }
1566
1567 params->base.num_channels = 0;
Kever Yang50fb9982017-02-22 16:56:35 +08001568 for (channel = 0; channel < 2; channel++) {
1569 const struct chan_info *chan = &dram->chan[channel];
Jagan Teki43485e12019-07-15 23:58:54 +05301570 struct sdram_cap_info *cap_info = &params->ch[channel].cap_info;
1571 u8 training_flag = PI_FULL_TRAINING;
Kever Yang50fb9982017-02-22 16:56:35 +08001572
Jagan Teki43485e12019-07-15 23:58:54 +05301573 if (cap_info->rank == 0) {
1574 clear_channel_params(params, channel);
Kever Yang50fb9982017-02-22 16:56:35 +08001575 continue;
Jagan Teki43485e12019-07-15 23:58:54 +05301576 } else {
1577 params->base.num_channels++;
Kever Yang50fb9982017-02-22 16:56:35 +08001578 }
1579
Jagan Teki43485e12019-07-15 23:58:54 +05301580 debug("Channel ");
1581 debug(channel ? "1: " : "0: ");
Jagan Tekic9151e22019-07-15 23:58:45 +05301582
Jagan Teki43485e12019-07-15 23:58:54 +05301583 /* LPDDR3 should have write and read gate training */
1584 if (params->base.dramtype == LPDDR3)
1585 training_flag = PI_WRITE_LEVELING |
1586 PI_READ_GATE_TRAINING;
Kever Yang50fb9982017-02-22 16:56:35 +08001587
Jagan Teki43485e12019-07-15 23:58:54 +05301588 if (params->base.dramtype != LPDDR4) {
1589 ret = data_training(dram, channel, params,
1590 training_flag);
1591 if (!ret) {
1592 debug("%s: data train failed for channel %d\n",
1593 __func__, ret);
1594 continue;
1595 }
Kever Yang50fb9982017-02-22 16:56:35 +08001596 }
1597
Jagan Teki8eed4a42019-07-15 23:58:55 +05301598 sdram_print_ddr_info(cap_info, &params->base);
1599
Jagan Teki43485e12019-07-15 23:58:54 +05301600 set_ddrconfig(chan, params, channel, cap_info->ddrconfig);
1601 }
1602
1603 if (params->base.num_channels == 0) {
1604 printf("%s: ", __func__);
Jagan Teki8eed4a42019-07-15 23:58:55 +05301605 sdram_print_dram_type(params->base.dramtype);
Jagan Teki43485e12019-07-15 23:58:54 +05301606 printf(" - %dMHz failed!\n", params->base.ddr_freq);
1607 return -EINVAL;
Kever Yang50fb9982017-02-22 16:56:35 +08001608 }
Jagan Teki2525fae2019-07-15 23:58:52 +05301609
1610 params->base.stride = calculate_stride(params);
Jagan Tekia58ff792019-07-15 23:50:58 +05301611 dram_all_config(dram, params);
1612 switch_to_phy_index1(dram, params);
Kever Yang50fb9982017-02-22 16:56:35 +08001613
1614 debug("Finish SDRAM initialization...\n");
1615 return 0;
1616}
1617
1618static int rk3399_dmc_ofdata_to_platdata(struct udevice *dev)
1619{
1620#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1621 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
Kever Yang50fb9982017-02-22 16:56:35 +08001622 int ret;
1623
Philipp Tomsich0250c232017-06-07 18:46:03 +02001624 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
1625 (u32 *)&plat->sdram_params,
1626 sizeof(plat->sdram_params) / sizeof(u32));
Kever Yang50fb9982017-02-22 16:56:35 +08001627 if (ret) {
1628 printf("%s: Cannot read rockchip,sdram-params %d\n",
1629 __func__, ret);
1630 return ret;
1631 }
Masahiro Yamadae4873e32018-04-19 12:14:03 +09001632 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001633 if (ret)
1634 printf("%s: regmap failed %d\n", __func__, ret);
1635
1636#endif
1637 return 0;
1638}
1639
1640#if CONFIG_IS_ENABLED(OF_PLATDATA)
1641static int conv_of_platdata(struct udevice *dev)
1642{
1643 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1644 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1645 int ret;
1646
1647 ret = regmap_init_mem_platdata(dev, dtplat->reg,
Jagan Tekif676c7c2019-07-15 23:50:56 +05301648 ARRAY_SIZE(dtplat->reg) / 2,
1649 &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001650 if (ret)
1651 return ret;
1652
1653 return 0;
1654}
1655#endif
1656
1657static int rk3399_dmc_init(struct udevice *dev)
1658{
1659 struct dram_info *priv = dev_get_priv(dev);
1660 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1661 int ret;
1662#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1663 struct rk3399_sdram_params *params = &plat->sdram_params;
1664#else
1665 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1666 struct rk3399_sdram_params *params =
1667 (void *)dtplat->rockchip_sdram_params;
1668
1669 ret = conv_of_platdata(dev);
1670 if (ret)
1671 return ret;
1672#endif
1673
1674 priv->cic = syscon_get_first_range(ROCKCHIP_SYSCON_CIC);
Jagan Tekic9151e22019-07-15 23:58:45 +05301675 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Kever Yang50fb9982017-02-22 16:56:35 +08001676 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
1677 priv->pmusgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
1678 priv->pmucru = rockchip_get_pmucru();
1679 priv->cru = rockchip_get_cru();
1680 priv->chan[0].pctl = regmap_get_range(plat->map, 0);
1681 priv->chan[0].pi = regmap_get_range(plat->map, 1);
1682 priv->chan[0].publ = regmap_get_range(plat->map, 2);
1683 priv->chan[0].msch = regmap_get_range(plat->map, 3);
1684 priv->chan[1].pctl = regmap_get_range(plat->map, 4);
1685 priv->chan[1].pi = regmap_get_range(plat->map, 5);
1686 priv->chan[1].publ = regmap_get_range(plat->map, 6);
1687 priv->chan[1].msch = regmap_get_range(plat->map, 7);
1688
1689 debug("con reg %p %p %p %p %p %p %p %p\n",
1690 priv->chan[0].pctl, priv->chan[0].pi,
1691 priv->chan[0].publ, priv->chan[0].msch,
1692 priv->chan[1].pctl, priv->chan[1].pi,
1693 priv->chan[1].publ, priv->chan[1].msch);
1694 debug("cru %p, cic %p, grf %p, sgrf %p, pmucru %p\n", priv->cru,
1695 priv->cic, priv->pmugrf, priv->pmusgrf, priv->pmucru);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301696
Kever Yang50fb9982017-02-22 16:56:35 +08001697#if CONFIG_IS_ENABLED(OF_PLATDATA)
1698 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->ddr_clk);
1699#else
1700 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
1701#endif
1702 if (ret) {
1703 printf("%s clk get failed %d\n", __func__, ret);
1704 return ret;
1705 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301706
Kever Yang50fb9982017-02-22 16:56:35 +08001707 ret = clk_set_rate(&priv->ddr_clk, params->base.ddr_freq * MHz);
1708 if (ret < 0) {
1709 printf("%s clk set failed %d\n", __func__, ret);
1710 return ret;
1711 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301712
Kever Yang50fb9982017-02-22 16:56:35 +08001713 ret = sdram_init(priv, params);
1714 if (ret < 0) {
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301715 printf("%s DRAM init failed %d\n", __func__, ret);
Kever Yang50fb9982017-02-22 16:56:35 +08001716 return ret;
1717 }
1718
1719 return 0;
1720}
1721#endif
1722
Kever Yang50fb9982017-02-22 16:56:35 +08001723static int rk3399_dmc_probe(struct udevice *dev)
1724{
Kever Yang7f347842019-04-01 17:20:53 +08001725#if defined(CONFIG_TPL_BUILD) || \
1726 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001727 if (rk3399_dmc_init(dev))
1728 return 0;
1729#else
1730 struct dram_info *priv = dev_get_priv(dev);
1731
1732 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301733 debug("%s: pmugrf = %p\n", __func__, priv->pmugrf);
Kever Yang6c15a542017-06-23 16:11:06 +08001734 priv->info.base = CONFIG_SYS_SDRAM_BASE;
Jagan Tekif676c7c2019-07-15 23:50:56 +05301735 priv->info.size =
1736 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg2);
Kever Yang50fb9982017-02-22 16:56:35 +08001737#endif
1738 return 0;
1739}
1740
1741static int rk3399_dmc_get_info(struct udevice *dev, struct ram_info *info)
1742{
1743 struct dram_info *priv = dev_get_priv(dev);
1744
Kever Yangea61d142017-04-19 16:01:14 +08001745 *info = priv->info;
Kever Yang50fb9982017-02-22 16:56:35 +08001746
1747 return 0;
1748}
1749
1750static struct ram_ops rk3399_dmc_ops = {
1751 .get_info = rk3399_dmc_get_info,
1752};
1753
Kever Yang50fb9982017-02-22 16:56:35 +08001754static const struct udevice_id rk3399_dmc_ids[] = {
1755 { .compatible = "rockchip,rk3399-dmc" },
1756 { }
1757};
1758
1759U_BOOT_DRIVER(dmc_rk3399) = {
1760 .name = "rockchip_rk3399_dmc",
1761 .id = UCLASS_RAM,
1762 .of_match = rk3399_dmc_ids,
1763 .ops = &rk3399_dmc_ops,
Kever Yang7f347842019-04-01 17:20:53 +08001764#if defined(CONFIG_TPL_BUILD) || \
1765 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001766 .ofdata_to_platdata = rk3399_dmc_ofdata_to_platdata,
1767#endif
1768 .probe = rk3399_dmc_probe,
Kever Yang50fb9982017-02-22 16:56:35 +08001769 .priv_auto_alloc_size = sizeof(struct dram_info),
Kever Yang7f347842019-04-01 17:20:53 +08001770#if defined(CONFIG_TPL_BUILD) || \
1771 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001772 .platdata_auto_alloc_size = sizeof(struct rockchip_dmc_plat),
1773#endif
1774};