blob: d958e9ddf7118aeeef49cec555aa75bb3c44d2ee [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 Tekid33056b2019-07-16 17:27:22 +0530187/**
188 * phy = 0, PHY boot freq
189 * phy = 1, PHY index 0
190 * phy = 2, PHY index 1
191 */
192static struct io_setting *
193lpddr4_get_io_settings(const struct rk3399_sdram_params *params, u32 mr5)
194{
195 struct io_setting *io = NULL;
196 u32 n;
197
198 for (n = 0; n < ARRAY_SIZE(lpddr4_io_setting); n++) {
199 io = &lpddr4_io_setting[n];
200
201 if (io->mr5 != 0) {
202 if (io->mhz >= params->base.ddr_freq &&
203 io->mr5 == mr5)
204 break;
205 } else {
206 if (io->mhz >= params->base.ddr_freq)
207 break;
208 }
209 }
210
211 return io;
212}
213
Jagan Tekic9151e22019-07-15 23:58:45 +0530214static void *get_ddrc0_con(struct dram_info *dram, u8 channel)
215{
216 return (channel == 0) ? &dram->grf->ddrc0_con0 : &dram->grf->ddrc0_con1;
217}
218
Kever Yang50fb9982017-02-22 16:56:35 +0800219static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
220{
221 int i;
222
223 for (i = 0; i < n / sizeof(u32); i++) {
224 writel(*src, dest);
225 src++;
226 dest++;
227 }
228}
229
Jagan Tekice75cfb2019-07-15 23:58:43 +0530230static void rkclk_ddr_reset(struct rk3399_cru *cru, u32 channel, u32 ctl,
231 u32 phy)
232{
233 channel &= 0x1;
234 ctl &= 0x1;
235 phy &= 0x1;
236 writel(CRU_SFTRST_DDR_CTRL(channel, ctl) |
237 CRU_SFTRST_DDR_PHY(channel, phy),
238 &cru->softrst_con[4]);
239}
240
241static void phy_pctrl_reset(struct rk3399_cru *cru, u32 channel)
242{
243 rkclk_ddr_reset(cru, channel, 1, 1);
244 udelay(10);
245
246 rkclk_ddr_reset(cru, channel, 1, 0);
247 udelay(10);
248
249 rkclk_ddr_reset(cru, channel, 0, 0);
250 udelay(10);
251}
252
Kever Yang50fb9982017-02-22 16:56:35 +0800253static void phy_dll_bypass_set(struct rk3399_ddr_publ_regs *ddr_publ_regs,
254 u32 freq)
255{
256 u32 *denali_phy = ddr_publ_regs->denali_phy;
257
258 /* From IP spec, only freq small than 125 can enter dll bypass mode */
259 if (freq <= 125) {
260 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
261 setbits_le32(&denali_phy[86], (0x3 << 2) << 8);
262 setbits_le32(&denali_phy[214], (0x3 << 2) << 8);
263 setbits_le32(&denali_phy[342], (0x3 << 2) << 8);
264 setbits_le32(&denali_phy[470], (0x3 << 2) << 8);
265
266 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
267 setbits_le32(&denali_phy[547], (0x3 << 2) << 16);
268 setbits_le32(&denali_phy[675], (0x3 << 2) << 16);
269 setbits_le32(&denali_phy[803], (0x3 << 2) << 16);
270 } else {
271 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
272 clrbits_le32(&denali_phy[86], (0x3 << 2) << 8);
273 clrbits_le32(&denali_phy[214], (0x3 << 2) << 8);
274 clrbits_le32(&denali_phy[342], (0x3 << 2) << 8);
275 clrbits_le32(&denali_phy[470], (0x3 << 2) << 8);
276
277 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
278 clrbits_le32(&denali_phy[547], (0x3 << 2) << 16);
279 clrbits_le32(&denali_phy[675], (0x3 << 2) << 16);
280 clrbits_le32(&denali_phy[803], (0x3 << 2) << 16);
281 }
282}
283
284static void set_memory_map(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530285 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800286{
Jagan Tekia58ff792019-07-15 23:50:58 +0530287 const struct rk3399_sdram_channel *sdram_ch = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +0800288 u32 *denali_ctl = chan->pctl->denali_ctl;
289 u32 *denali_pi = chan->pi->denali_pi;
290 u32 cs_map;
291 u32 reduc;
292 u32 row;
293
294 /* Get row number from ddrconfig setting */
Jagan Teki97867c82019-07-15 23:51:05 +0530295 if (sdram_ch->cap_info.ddrconfig < 2 ||
296 sdram_ch->cap_info.ddrconfig == 4)
Kever Yang50fb9982017-02-22 16:56:35 +0800297 row = 16;
Jagan Teki97867c82019-07-15 23:51:05 +0530298 else if (sdram_ch->cap_info.ddrconfig == 3)
Kever Yang50fb9982017-02-22 16:56:35 +0800299 row = 14;
300 else
301 row = 15;
302
Jagan Teki97867c82019-07-15 23:51:05 +0530303 cs_map = (sdram_ch->cap_info.rank > 1) ? 3 : 1;
304 reduc = (sdram_ch->cap_info.bw == 2) ? 0 : 1;
Kever Yang50fb9982017-02-22 16:56:35 +0800305
306 /* Set the dram configuration to ctrl */
Jagan Teki97867c82019-07-15 23:51:05 +0530307 clrsetbits_le32(&denali_ctl[191], 0xF, (12 - sdram_ch->cap_info.col));
Kever Yang50fb9982017-02-22 16:56:35 +0800308 clrsetbits_le32(&denali_ctl[190], (0x3 << 16) | (0x7 << 24),
Jagan Teki97867c82019-07-15 23:51:05 +0530309 ((3 - sdram_ch->cap_info.bk) << 16) |
Kever Yang50fb9982017-02-22 16:56:35 +0800310 ((16 - row) << 24));
311
312 clrsetbits_le32(&denali_ctl[196], 0x3 | (1 << 16),
313 cs_map | (reduc << 16));
314
315 /* PI_199 PI_COL_DIFF:RW:0:4 */
Jagan Teki97867c82019-07-15 23:51:05 +0530316 clrsetbits_le32(&denali_pi[199], 0xF, (12 - sdram_ch->cap_info.col));
Kever Yang50fb9982017-02-22 16:56:35 +0800317
318 /* PI_155 PI_ROW_DIFF:RW:24:3 PI_BANK_DIFF:RW:16:2 */
319 clrsetbits_le32(&denali_pi[155], (0x3 << 16) | (0x7 << 24),
Jagan Teki97867c82019-07-15 23:51:05 +0530320 ((3 - sdram_ch->cap_info.bk) << 16) |
Kever Yang50fb9982017-02-22 16:56:35 +0800321 ((16 - row) << 24));
Jagan Teki9337cb32019-07-16 17:27:18 +0530322
323 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
324 if (cs_map == 1)
325 cs_map = 0x5;
326 else if (cs_map == 2)
327 cs_map = 0xa;
328 else
329 cs_map = 0xF;
330 }
331
Kever Yang50fb9982017-02-22 16:56:35 +0800332 /* PI_41 PI_CS_MAP:RW:24:4 */
333 clrsetbits_le32(&denali_pi[41], 0xf << 24, cs_map << 24);
Jagan Teki97867c82019-07-15 23:51:05 +0530334 if (sdram_ch->cap_info.rank == 1 && params->base.dramtype == DDR3)
Kever Yang50fb9982017-02-22 16:56:35 +0800335 writel(0x2EC7FFFF, &denali_pi[34]);
336}
337
Jagan Tekib5d46632019-07-16 17:27:07 +0530338static int phy_io_config(const struct chan_info *chan,
339 const struct rk3399_sdram_params *params)
340{
341 u32 *denali_phy = chan->publ->denali_phy;
342 u32 vref_mode_dq, vref_value_dq, vref_mode_ac, vref_value_ac;
343 u32 mode_sel;
344 u32 reg_value;
345 u32 drv_value, odt_value;
346 u32 speed;
347
348 /* vref setting */
349 if (params->base.dramtype == LPDDR4) {
350 /* LPDDR4 */
351 vref_mode_dq = 0x6;
352 vref_value_dq = 0x1f;
353 vref_mode_ac = 0x6;
354 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530355 mode_sel = 0x6;
Jagan Tekib5d46632019-07-16 17:27:07 +0530356 } else if (params->base.dramtype == LPDDR3) {
357 if (params->base.odt == 1) {
358 vref_mode_dq = 0x5; /* LPDDR3 ODT */
359 drv_value = (readl(&denali_phy[6]) >> 12) & 0xf;
360 odt_value = (readl(&denali_phy[6]) >> 4) & 0xf;
361 if (drv_value == PHY_DRV_ODT_48) {
362 switch (odt_value) {
363 case PHY_DRV_ODT_240:
364 vref_value_dq = 0x16;
365 break;
366 case PHY_DRV_ODT_120:
367 vref_value_dq = 0x26;
368 break;
369 case PHY_DRV_ODT_60:
370 vref_value_dq = 0x36;
371 break;
372 default:
373 debug("Invalid ODT value.\n");
374 return -EINVAL;
375 }
376 } else if (drv_value == PHY_DRV_ODT_40) {
377 switch (odt_value) {
378 case PHY_DRV_ODT_240:
379 vref_value_dq = 0x19;
380 break;
381 case PHY_DRV_ODT_120:
382 vref_value_dq = 0x23;
383 break;
384 case PHY_DRV_ODT_60:
385 vref_value_dq = 0x31;
386 break;
387 default:
388 debug("Invalid ODT value.\n");
389 return -EINVAL;
390 }
391 } else if (drv_value == PHY_DRV_ODT_34_3) {
392 switch (odt_value) {
393 case PHY_DRV_ODT_240:
394 vref_value_dq = 0x17;
395 break;
396 case PHY_DRV_ODT_120:
397 vref_value_dq = 0x20;
398 break;
399 case PHY_DRV_ODT_60:
400 vref_value_dq = 0x2e;
401 break;
402 default:
403 debug("Invalid ODT value.\n");
404 return -EINVAL;
405 }
406 } else {
407 debug("Invalid DRV value.\n");
408 return -EINVAL;
409 }
410 } else {
411 vref_mode_dq = 0x2; /* LPDDR3 */
412 vref_value_dq = 0x1f;
413 }
414 vref_mode_ac = 0x2;
415 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530416 mode_sel = 0x0;
Jagan Tekib5d46632019-07-16 17:27:07 +0530417 } else if (params->base.dramtype == DDR3) {
418 /* DDR3L */
419 vref_mode_dq = 0x1;
420 vref_value_dq = 0x1f;
421 vref_mode_ac = 0x1;
422 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530423 mode_sel = 0x1;
Jagan Tekib5d46632019-07-16 17:27:07 +0530424 } else {
425 debug("Unknown DRAM type.\n");
426 return -EINVAL;
427 }
428
429 reg_value = (vref_mode_dq << 9) | (0x1 << 8) | vref_value_dq;
430
431 /* PHY_913 PHY_PAD_VREF_CTRL_DQ_0 12bits offset_8 */
432 clrsetbits_le32(&denali_phy[913], 0xfff << 8, reg_value << 8);
433 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_1 12bits offset_0 */
434 clrsetbits_le32(&denali_phy[914], 0xfff, reg_value);
435 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_2 12bits offset_16 */
436 clrsetbits_le32(&denali_phy[914], 0xfff << 16, reg_value << 16);
437 /* PHY_915 PHY_PAD_VREF_CTRL_DQ_3 12bits offset_0 */
438 clrsetbits_le32(&denali_phy[915], 0xfff, reg_value);
439
440 reg_value = (vref_mode_ac << 9) | (0x1 << 8) | vref_value_ac;
441
442 /* PHY_915 PHY_PAD_VREF_CTRL_AC 12bits offset_16 */
443 clrsetbits_le32(&denali_phy[915], 0xfff << 16, reg_value << 16);
444
Jagan Tekib5d46632019-07-16 17:27:07 +0530445 /* PHY_924 PHY_PAD_FDBK_DRIVE */
446 clrsetbits_le32(&denali_phy[924], 0x7 << 15, mode_sel << 15);
447 /* PHY_926 PHY_PAD_DATA_DRIVE */
448 clrsetbits_le32(&denali_phy[926], 0x7 << 6, mode_sel << 6);
449 /* PHY_927 PHY_PAD_DQS_DRIVE */
450 clrsetbits_le32(&denali_phy[927], 0x7 << 6, mode_sel << 6);
451 /* PHY_928 PHY_PAD_ADDR_DRIVE */
452 clrsetbits_le32(&denali_phy[928], 0x7 << 14, mode_sel << 14);
453 /* PHY_929 PHY_PAD_CLK_DRIVE */
454 clrsetbits_le32(&denali_phy[929], 0x7 << 14, mode_sel << 14);
455 /* PHY_935 PHY_PAD_CKE_DRIVE */
456 clrsetbits_le32(&denali_phy[935], 0x7 << 14, mode_sel << 14);
457 /* PHY_937 PHY_PAD_RST_DRIVE */
458 clrsetbits_le32(&denali_phy[937], 0x7 << 14, mode_sel << 14);
459 /* PHY_939 PHY_PAD_CS_DRIVE */
460 clrsetbits_le32(&denali_phy[939], 0x7 << 14, mode_sel << 14);
461
Jagan Teki5d152172019-07-16 17:27:15 +0530462 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
463 /* BOOSTP_EN & BOOSTN_EN */
464 reg_value = ((PHY_BOOSTP_EN << 4) | PHY_BOOSTN_EN);
465 /* PHY_925 PHY_PAD_FDBK_DRIVE2 */
466 clrsetbits_le32(&denali_phy[925], 0xff << 8, reg_value << 8);
467 /* PHY_926 PHY_PAD_DATA_DRIVE */
468 clrsetbits_le32(&denali_phy[926], 0xff << 12, reg_value << 12);
469 /* PHY_927 PHY_PAD_DQS_DRIVE */
470 clrsetbits_le32(&denali_phy[927], 0xff << 14, reg_value << 14);
471 /* PHY_928 PHY_PAD_ADDR_DRIVE */
472 clrsetbits_le32(&denali_phy[928], 0xff << 20, reg_value << 20);
473 /* PHY_929 PHY_PAD_CLK_DRIVE */
474 clrsetbits_le32(&denali_phy[929], 0xff << 22, reg_value << 22);
475 /* PHY_935 PHY_PAD_CKE_DRIVE */
476 clrsetbits_le32(&denali_phy[935], 0xff << 20, reg_value << 20);
477 /* PHY_937 PHY_PAD_RST_DRIVE */
478 clrsetbits_le32(&denali_phy[937], 0xff << 20, reg_value << 20);
479 /* PHY_939 PHY_PAD_CS_DRIVE */
480 clrsetbits_le32(&denali_phy[939], 0xff << 20, reg_value << 20);
Jagan Tekid8681842019-07-16 17:27:16 +0530481
482 /* SLEWP_EN & SLEWN_EN */
483 reg_value = ((PHY_SLEWP_EN << 3) | PHY_SLEWN_EN);
484 /* PHY_924 PHY_PAD_FDBK_DRIVE */
485 clrsetbits_le32(&denali_phy[924], 0x3f << 8, reg_value << 8);
486 /* PHY_926 PHY_PAD_DATA_DRIVE */
487 clrsetbits_le32(&denali_phy[926], 0x3f, reg_value);
488 /* PHY_927 PHY_PAD_DQS_DRIVE */
489 clrsetbits_le32(&denali_phy[927], 0x3f, reg_value);
490 /* PHY_928 PHY_PAD_ADDR_DRIVE */
491 clrsetbits_le32(&denali_phy[928], 0x3f << 8, reg_value << 8);
492 /* PHY_929 PHY_PAD_CLK_DRIVE */
493 clrsetbits_le32(&denali_phy[929], 0x3f << 8, reg_value << 8);
494 /* PHY_935 PHY_PAD_CKE_DRIVE */
495 clrsetbits_le32(&denali_phy[935], 0x3f << 8, reg_value << 8);
496 /* PHY_937 PHY_PAD_RST_DRIVE */
497 clrsetbits_le32(&denali_phy[937], 0x3f << 8, reg_value << 8);
498 /* PHY_939 PHY_PAD_CS_DRIVE */
499 clrsetbits_le32(&denali_phy[939], 0x3f << 8, reg_value << 8);
Jagan Teki5d152172019-07-16 17:27:15 +0530500 }
501
Jagan Tekib5d46632019-07-16 17:27:07 +0530502 /* speed setting */
503 if (params->base.ddr_freq < 400)
504 speed = 0x0;
505 else if (params->base.ddr_freq < 800)
506 speed = 0x1;
507 else if (params->base.ddr_freq < 1200)
508 speed = 0x2;
509 else
510 speed = 0x3;
511
512 /* PHY_924 PHY_PAD_FDBK_DRIVE */
513 clrsetbits_le32(&denali_phy[924], 0x3 << 21, speed << 21);
514 /* PHY_926 PHY_PAD_DATA_DRIVE */
515 clrsetbits_le32(&denali_phy[926], 0x3 << 9, speed << 9);
516 /* PHY_927 PHY_PAD_DQS_DRIVE */
517 clrsetbits_le32(&denali_phy[927], 0x3 << 9, speed << 9);
518 /* PHY_928 PHY_PAD_ADDR_DRIVE */
519 clrsetbits_le32(&denali_phy[928], 0x3 << 17, speed << 17);
520 /* PHY_929 PHY_PAD_CLK_DRIVE */
521 clrsetbits_le32(&denali_phy[929], 0x3 << 17, speed << 17);
522 /* PHY_935 PHY_PAD_CKE_DRIVE */
523 clrsetbits_le32(&denali_phy[935], 0x3 << 17, speed << 17);
524 /* PHY_937 PHY_PAD_RST_DRIVE */
525 clrsetbits_le32(&denali_phy[937], 0x3 << 17, speed << 17);
526 /* PHY_939 PHY_PAD_CS_DRIVE */
527 clrsetbits_le32(&denali_phy[939], 0x3 << 17, speed << 17);
528
Jagan Teki65535a22019-07-16 17:27:17 +0530529 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
530 /* RX_CM_INPUT */
531 reg_value = PHY_RX_CM_INPUT;
532 /* PHY_924 PHY_PAD_FDBK_DRIVE */
533 clrsetbits_le32(&denali_phy[924], 0x1 << 14, reg_value << 14);
534 /* PHY_926 PHY_PAD_DATA_DRIVE */
535 clrsetbits_le32(&denali_phy[926], 0x1 << 11, reg_value << 11);
536 /* PHY_927 PHY_PAD_DQS_DRIVE */
537 clrsetbits_le32(&denali_phy[927], 0x1 << 13, reg_value << 13);
538 /* PHY_928 PHY_PAD_ADDR_DRIVE */
539 clrsetbits_le32(&denali_phy[928], 0x1 << 19, reg_value << 19);
540 /* PHY_929 PHY_PAD_CLK_DRIVE */
541 clrsetbits_le32(&denali_phy[929], 0x1 << 21, reg_value << 21);
542 /* PHY_935 PHY_PAD_CKE_DRIVE */
543 clrsetbits_le32(&denali_phy[935], 0x1 << 19, reg_value << 19);
544 /* PHY_937 PHY_PAD_RST_DRIVE */
545 clrsetbits_le32(&denali_phy[937], 0x1 << 19, reg_value << 19);
546 /* PHY_939 PHY_PAD_CS_DRIVE */
547 clrsetbits_le32(&denali_phy[939], 0x1 << 19, reg_value << 19);
548 }
549
Jagan Tekib5d46632019-07-16 17:27:07 +0530550 return 0;
551}
552
Kever Yang50fb9982017-02-22 16:56:35 +0800553static void set_ds_odt(const struct chan_info *chan,
Jagan Tekid33056b2019-07-16 17:27:22 +0530554 const struct rk3399_sdram_params *params, u32 mr5)
Kever Yang50fb9982017-02-22 16:56:35 +0800555{
556 u32 *denali_phy = chan->publ->denali_phy;
557
558 u32 tsel_idle_en, tsel_wr_en, tsel_rd_en;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530559 u32 tsel_idle_select_p, tsel_rd_select_p;
560 u32 tsel_idle_select_n, tsel_rd_select_n;
561 u32 tsel_wr_select_dq_p, tsel_wr_select_ca_p;
562 u32 tsel_wr_select_dq_n, tsel_wr_select_ca_n;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530563 u32 tsel_ckcs_select_p, tsel_ckcs_select_n;
Jagan Tekid33056b2019-07-16 17:27:22 +0530564 struct io_setting *io = NULL;
Kever Yang50fb9982017-02-22 16:56:35 +0800565 u32 reg_value;
566
Jagan Tekia58ff792019-07-15 23:50:58 +0530567 if (params->base.dramtype == LPDDR4) {
Jagan Tekid33056b2019-07-16 17:27:22 +0530568 io = lpddr4_get_io_settings(params, mr5);
569
Jagan Tekif676c7c2019-07-15 23:50:56 +0530570 tsel_rd_select_p = PHY_DRV_ODT_HI_Z;
Jagan Tekid33056b2019-07-16 17:27:22 +0530571 tsel_rd_select_n = io->rd_odt;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530572
Jagan Tekif676c7c2019-07-15 23:50:56 +0530573 tsel_idle_select_p = PHY_DRV_ODT_HI_Z;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530574 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800575
Jagan Tekid33056b2019-07-16 17:27:22 +0530576 tsel_wr_select_dq_p = io->wr_dq_drv;
Jagan Teki36667142019-07-15 23:51:00 +0530577 tsel_wr_select_dq_n = PHY_DRV_ODT_40;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530578
Jagan Tekid33056b2019-07-16 17:27:22 +0530579 tsel_wr_select_ca_p = io->wr_ca_drv;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530580 tsel_wr_select_ca_n = PHY_DRV_ODT_40;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530581
582 tsel_ckcs_select_p = io->wr_ckcs_drv;
583 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
Jagan Tekia58ff792019-07-15 23:50:58 +0530584 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +0800585 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530586 tsel_rd_select_n = PHY_DRV_ODT_HI_Z;
587
Kever Yang50fb9982017-02-22 16:56:35 +0800588 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530589 tsel_idle_select_n = PHY_DRV_ODT_HI_Z;
Kever Yang50fb9982017-02-22 16:56:35 +0800590
Jagan Teki5c3251f2019-07-15 23:51:04 +0530591 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530592 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530593
594 tsel_wr_select_ca_p = PHY_DRV_ODT_48;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530595 tsel_wr_select_ca_n = PHY_DRV_ODT_48;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530596
597 tsel_ckcs_select_p = PHY_DRV_ODT_34_3;
598 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
Kever Yang50fb9982017-02-22 16:56:35 +0800599 } else {
600 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530601 tsel_rd_select_n = PHY_DRV_ODT_240;
602
Kever Yang50fb9982017-02-22 16:56:35 +0800603 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530604 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800605
Jagan Teki5c3251f2019-07-15 23:51:04 +0530606 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530607 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530608
609 tsel_wr_select_ca_p = PHY_DRV_ODT_34_3;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530610 tsel_wr_select_ca_n = PHY_DRV_ODT_34_3;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530611
612 tsel_ckcs_select_p = PHY_DRV_ODT_34_3;
613 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
Kever Yang50fb9982017-02-22 16:56:35 +0800614 }
615
Jagan Tekia58ff792019-07-15 23:50:58 +0530616 if (params->base.odt == 1)
Kever Yang50fb9982017-02-22 16:56:35 +0800617 tsel_rd_en = 1;
618 else
619 tsel_rd_en = 0;
620
621 tsel_wr_en = 0;
622 tsel_idle_en = 0;
623
624 /*
625 * phy_dq_tsel_select_X 24bits DENALI_PHY_6/134/262/390 offset_0
626 * sets termination values for read/idle cycles and drive strength
627 * for write cycles for DQ/DM
628 */
629 reg_value = tsel_rd_select_n | (tsel_rd_select_p << 0x4) |
Jagan Tekib3b34392019-07-15 23:51:01 +0530630 (tsel_wr_select_dq_n << 8) | (tsel_wr_select_dq_p << 12) |
Kever Yang50fb9982017-02-22 16:56:35 +0800631 (tsel_idle_select_n << 16) | (tsel_idle_select_p << 20);
632 clrsetbits_le32(&denali_phy[6], 0xffffff, reg_value);
633 clrsetbits_le32(&denali_phy[134], 0xffffff, reg_value);
634 clrsetbits_le32(&denali_phy[262], 0xffffff, reg_value);
635 clrsetbits_le32(&denali_phy[390], 0xffffff, reg_value);
636
637 /*
638 * phy_dqs_tsel_select_X 24bits DENALI_PHY_7/135/263/391 offset_0
639 * sets termination values for read/idle cycles and drive strength
640 * for write cycles for DQS
641 */
642 clrsetbits_le32(&denali_phy[7], 0xffffff, reg_value);
643 clrsetbits_le32(&denali_phy[135], 0xffffff, reg_value);
644 clrsetbits_le32(&denali_phy[263], 0xffffff, reg_value);
645 clrsetbits_le32(&denali_phy[391], 0xffffff, reg_value);
646
647 /* phy_adr_tsel_select_ 8bits DENALI_PHY_544/672/800 offset_0 */
Jagan Teki7caa3e92019-07-15 23:51:03 +0530648 reg_value = tsel_wr_select_ca_n | (tsel_wr_select_ca_p << 0x4);
Jagan Teki539ffed2019-07-16 17:27:19 +0530649 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
650 /* LPDDR4 these register read always return 0, so
651 * can not use clrsetbits_le32(), need to write32
652 */
653 writel((0x300 << 8) | reg_value, &denali_phy[544]);
654 writel((0x300 << 8) | reg_value, &denali_phy[672]);
655 writel((0x300 << 8) | reg_value, &denali_phy[800]);
656 } else {
657 clrsetbits_le32(&denali_phy[544], 0xff, reg_value);
658 clrsetbits_le32(&denali_phy[672], 0xff, reg_value);
659 clrsetbits_le32(&denali_phy[800], 0xff, reg_value);
660 }
Kever Yang50fb9982017-02-22 16:56:35 +0800661
662 /* phy_pad_addr_drive 8bits DENALI_PHY_928 offset_0 */
663 clrsetbits_le32(&denali_phy[928], 0xff, reg_value);
664
665 /* phy_pad_rst_drive 8bits DENALI_PHY_937 offset_0 */
666 clrsetbits_le32(&denali_phy[937], 0xff, reg_value);
667
668 /* phy_pad_cke_drive 8bits DENALI_PHY_935 offset_0 */
669 clrsetbits_le32(&denali_phy[935], 0xff, reg_value);
670
671 /* phy_pad_cs_drive 8bits DENALI_PHY_939 offset_0 */
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530672 clrsetbits_le32(&denali_phy[939], 0xff,
673 tsel_ckcs_select_n | (tsel_ckcs_select_p << 0x4));
Kever Yang50fb9982017-02-22 16:56:35 +0800674
675 /* phy_pad_clk_drive 8bits DENALI_PHY_929 offset_0 */
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530676 clrsetbits_le32(&denali_phy[929], 0xff,
677 tsel_ckcs_select_n | (tsel_ckcs_select_p << 0x4));
Kever Yang50fb9982017-02-22 16:56:35 +0800678
679 /* phy_pad_fdbk_drive 23bit DENALI_PHY_924/925 */
680 clrsetbits_le32(&denali_phy[924], 0xff,
Jagan Tekib3b34392019-07-15 23:51:01 +0530681 tsel_wr_select_dq_n | (tsel_wr_select_dq_p << 4));
Kever Yang50fb9982017-02-22 16:56:35 +0800682 clrsetbits_le32(&denali_phy[925], 0xff,
683 tsel_rd_select_n | (tsel_rd_select_p << 4));
684
685 /* phy_dq_tsel_enable_X 3bits DENALI_PHY_5/133/261/389 offset_16 */
686 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
687 << 16;
688 clrsetbits_le32(&denali_phy[5], 0x7 << 16, reg_value);
689 clrsetbits_le32(&denali_phy[133], 0x7 << 16, reg_value);
690 clrsetbits_le32(&denali_phy[261], 0x7 << 16, reg_value);
691 clrsetbits_le32(&denali_phy[389], 0x7 << 16, reg_value);
692
693 /* phy_dqs_tsel_enable_X 3bits DENALI_PHY_6/134/262/390 offset_24 */
694 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
695 << 24;
696 clrsetbits_le32(&denali_phy[6], 0x7 << 24, reg_value);
697 clrsetbits_le32(&denali_phy[134], 0x7 << 24, reg_value);
698 clrsetbits_le32(&denali_phy[262], 0x7 << 24, reg_value);
699 clrsetbits_le32(&denali_phy[390], 0x7 << 24, reg_value);
700
701 /* phy_adr_tsel_enable_ 1bit DENALI_PHY_518/646/774 offset_8 */
702 reg_value = tsel_wr_en << 8;
703 clrsetbits_le32(&denali_phy[518], 0x1 << 8, reg_value);
704 clrsetbits_le32(&denali_phy[646], 0x1 << 8, reg_value);
705 clrsetbits_le32(&denali_phy[774], 0x1 << 8, reg_value);
706
707 /* phy_pad_addr_term tsel 1bit DENALI_PHY_933 offset_17 */
708 reg_value = tsel_wr_en << 17;
709 clrsetbits_le32(&denali_phy[933], 0x1 << 17, reg_value);
710 /*
711 * pad_rst/cke/cs/clk_term tsel 1bits
712 * DENALI_PHY_938/936/940/934 offset_17
713 */
714 clrsetbits_le32(&denali_phy[938], 0x1 << 17, reg_value);
715 clrsetbits_le32(&denali_phy[936], 0x1 << 17, reg_value);
716 clrsetbits_le32(&denali_phy[940], 0x1 << 17, reg_value);
717 clrsetbits_le32(&denali_phy[934], 0x1 << 17, reg_value);
718
719 /* phy_pad_fdbk_term 1bit DENALI_PHY_930 offset_17 */
720 clrsetbits_le32(&denali_phy[930], 0x1 << 17, reg_value);
Jagan Tekib5d46632019-07-16 17:27:07 +0530721
722 phy_io_config(chan, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800723}
724
Jagan Tekic9151e22019-07-15 23:58:45 +0530725static void pctl_start(struct dram_info *dram, u8 channel)
726{
727 const struct chan_info *chan = &dram->chan[channel];
728 u32 *denali_ctl = chan->pctl->denali_ctl;
729 u32 *denali_phy = chan->publ->denali_phy;
730 u32 *ddrc0_con = get_ddrc0_con(dram, channel);
731 u32 count = 0;
732 u32 byte, tmp;
733
734 writel(0x01000000, &ddrc0_con);
735
736 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 0x2 << 24);
737
738 while (!(readl(&denali_ctl[203]) & (1 << 3))) {
739 if (count > 1000) {
740 printf("%s: Failed to init pctl for channel %d\n",
741 __func__, channel);
742 while (1)
743 ;
744 }
745
746 udelay(1);
747 count++;
748 }
749
750 writel(0x01000100, &ddrc0_con);
751
752 for (byte = 0; byte < 4; byte++) {
753 tmp = 0x820;
754 writel((tmp << 16) | tmp, &denali_phy[53 + (128 * byte)]);
755 writel((tmp << 16) | tmp, &denali_phy[54 + (128 * byte)]);
756 writel((tmp << 16) | tmp, &denali_phy[55 + (128 * byte)]);
757 writel((tmp << 16) | tmp, &denali_phy[56 + (128 * byte)]);
758 writel((tmp << 16) | tmp, &denali_phy[57 + (128 * byte)]);
759
760 clrsetbits_le32(&denali_phy[58 + (128 * byte)], 0xffff, tmp);
761 }
762
763 clrsetbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT,
764 dram->pwrup_srefresh_exit[channel]);
765}
766
Jagan Teki4ef5c012019-07-15 23:58:44 +0530767static int pctl_cfg(struct dram_info *dram, const struct chan_info *chan,
Jagan Tekid33056b2019-07-16 17:27:22 +0530768 u32 channel, struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800769{
770 u32 *denali_ctl = chan->pctl->denali_ctl;
771 u32 *denali_pi = chan->pi->denali_pi;
772 u32 *denali_phy = chan->publ->denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +0530773 const u32 *params_ctl = params->pctl_regs.denali_ctl;
774 const u32 *params_phy = params->phy_regs.denali_phy;
Kever Yang50fb9982017-02-22 16:56:35 +0800775 u32 tmp, tmp1, tmp2;
Kever Yang50fb9982017-02-22 16:56:35 +0800776
777 /*
778 * work around controller bug:
779 * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed
780 */
781 copy_to_reg(&denali_ctl[1], &params_ctl[1],
782 sizeof(struct rk3399_ddr_pctl_regs) - 4);
783 writel(params_ctl[0], &denali_ctl[0]);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530784
Jagan Tekicc9da9a2019-07-16 17:27:13 +0530785 /*
786 * two channel init at the same time, then ZQ Cal Start
787 * at the same time, it will use the same RZQ, but cannot
788 * start at the same time.
789 *
790 * So, increase tINIT3 for channel 1, will avoid two
791 * channel ZQ Cal Start at the same time
792 */
793 if (params->base.dramtype == LPDDR4 && channel == 1) {
794 tmp = ((params->base.ddr_freq * MHz + 999) / 1000);
795 tmp1 = readl(&denali_ctl[14]);
796 writel(tmp + tmp1, &denali_ctl[14]);
797 }
798
Jagan Tekia58ff792019-07-15 23:50:58 +0530799 copy_to_reg(denali_pi, &params->pi_regs.denali_pi[0],
Kever Yang50fb9982017-02-22 16:56:35 +0800800 sizeof(struct rk3399_ddr_pi_regs));
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530801
Kever Yang50fb9982017-02-22 16:56:35 +0800802 /* rank count need to set for init */
Jagan Tekia58ff792019-07-15 23:50:58 +0530803 set_memory_map(chan, channel, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800804
Jagan Tekia58ff792019-07-15 23:50:58 +0530805 writel(params->phy_regs.denali_phy[910], &denali_phy[910]);
806 writel(params->phy_regs.denali_phy[911], &denali_phy[911]);
807 writel(params->phy_regs.denali_phy[912], &denali_phy[912]);
Kever Yang50fb9982017-02-22 16:56:35 +0800808
Jagan Tekib49b5dc2019-07-16 17:27:14 +0530809 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
810 writel(params->phy_regs.denali_phy[898], &denali_phy[898]);
811 writel(params->phy_regs.denali_phy[919], &denali_phy[919]);
812 }
813
Jagan Tekic9151e22019-07-15 23:58:45 +0530814 dram->pwrup_srefresh_exit[channel] = readl(&denali_ctl[68]) &
815 PWRUP_SREFRESH_EXIT;
Kever Yang50fb9982017-02-22 16:56:35 +0800816 clrbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT);
817
818 /* PHY_DLL_RST_EN */
819 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 1 << 24);
820
821 setbits_le32(&denali_pi[0], START);
822 setbits_le32(&denali_ctl[0], START);
823
Jagan Teki5e927182019-07-16 17:27:12 +0530824 /**
825 * LPDDR4 use PLL bypass mode for init
826 * not need to wait for the PLL to lock
827 */
828 if (params->base.dramtype != LPDDR4) {
829 /* Waiting for phy DLL lock */
830 while (1) {
831 tmp = readl(&denali_phy[920]);
832 tmp1 = readl(&denali_phy[921]);
833 tmp2 = readl(&denali_phy[922]);
834 if ((((tmp >> 16) & 0x1) == 0x1) &&
835 (((tmp1 >> 16) & 0x1) == 0x1) &&
836 (((tmp1 >> 0) & 0x1) == 0x1) &&
837 (((tmp2 >> 0) & 0x1) == 0x1))
838 break;
839 }
Kever Yang50fb9982017-02-22 16:56:35 +0800840 }
841
842 copy_to_reg(&denali_phy[896], &params_phy[896], (958 - 895) * 4);
843 copy_to_reg(&denali_phy[0], &params_phy[0], (90 - 0 + 1) * 4);
844 copy_to_reg(&denali_phy[128], &params_phy[128], (218 - 128 + 1) * 4);
845 copy_to_reg(&denali_phy[256], &params_phy[256], (346 - 256 + 1) * 4);
846 copy_to_reg(&denali_phy[384], &params_phy[384], (474 - 384 + 1) * 4);
847 copy_to_reg(&denali_phy[512], &params_phy[512], (549 - 512 + 1) * 4);
848 copy_to_reg(&denali_phy[640], &params_phy[640], (677 - 640 + 1) * 4);
849 copy_to_reg(&denali_phy[768], &params_phy[768], (805 - 768 + 1) * 4);
Jagan Tekid33056b2019-07-16 17:27:22 +0530850 set_ds_odt(chan, params, 0);
Kever Yang50fb9982017-02-22 16:56:35 +0800851
852 /*
853 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_84/212/340/468 offset_8
854 * dqs_tsel_wr_end[7:4] add Half cycle
855 */
856 tmp = (readl(&denali_phy[84]) >> 8) & 0xff;
857 clrsetbits_le32(&denali_phy[84], 0xff << 8, (tmp + 0x10) << 8);
858 tmp = (readl(&denali_phy[212]) >> 8) & 0xff;
859 clrsetbits_le32(&denali_phy[212], 0xff << 8, (tmp + 0x10) << 8);
860 tmp = (readl(&denali_phy[340]) >> 8) & 0xff;
861 clrsetbits_le32(&denali_phy[340], 0xff << 8, (tmp + 0x10) << 8);
862 tmp = (readl(&denali_phy[468]) >> 8) & 0xff;
863 clrsetbits_le32(&denali_phy[468], 0xff << 8, (tmp + 0x10) << 8);
864
865 /*
866 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_83/211/339/467 offset_8
867 * dq_tsel_wr_end[7:4] add Half cycle
868 */
869 tmp = (readl(&denali_phy[83]) >> 16) & 0xff;
870 clrsetbits_le32(&denali_phy[83], 0xff << 16, (tmp + 0x10) << 16);
871 tmp = (readl(&denali_phy[211]) >> 16) & 0xff;
872 clrsetbits_le32(&denali_phy[211], 0xff << 16, (tmp + 0x10) << 16);
873 tmp = (readl(&denali_phy[339]) >> 16) & 0xff;
874 clrsetbits_le32(&denali_phy[339], 0xff << 16, (tmp + 0x10) << 16);
875 tmp = (readl(&denali_phy[467]) >> 16) & 0xff;
876 clrsetbits_le32(&denali_phy[467], 0xff << 16, (tmp + 0x10) << 16);
877
Kever Yang50fb9982017-02-22 16:56:35 +0800878 return 0;
879}
880
881static void select_per_cs_training_index(const struct chan_info *chan,
882 u32 rank)
883{
884 u32 *denali_phy = chan->publ->denali_phy;
885
886 /* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */
Jagan Tekif676c7c2019-07-15 23:50:56 +0530887 if ((readl(&denali_phy[84]) >> 16) & 1) {
Kever Yang50fb9982017-02-22 16:56:35 +0800888 /*
889 * PHY_8/136/264/392
890 * phy_per_cs_training_index_X 1bit offset_24
891 */
892 clrsetbits_le32(&denali_phy[8], 0x1 << 24, rank << 24);
893 clrsetbits_le32(&denali_phy[136], 0x1 << 24, rank << 24);
894 clrsetbits_le32(&denali_phy[264], 0x1 << 24, rank << 24);
895 clrsetbits_le32(&denali_phy[392], 0x1 << 24, rank << 24);
896 }
897}
898
899static void override_write_leveling_value(const struct chan_info *chan)
900{
901 u32 *denali_ctl = chan->pctl->denali_ctl;
902 u32 *denali_phy = chan->publ->denali_phy;
903 u32 byte;
904
905 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
906 setbits_le32(&denali_phy[896], 1);
907
908 /*
909 * PHY_8/136/264/392
910 * phy_per_cs_training_multicast_en_X 1bit offset_16
911 */
912 clrsetbits_le32(&denali_phy[8], 0x1 << 16, 1 << 16);
913 clrsetbits_le32(&denali_phy[136], 0x1 << 16, 1 << 16);
914 clrsetbits_le32(&denali_phy[264], 0x1 << 16, 1 << 16);
915 clrsetbits_le32(&denali_phy[392], 0x1 << 16, 1 << 16);
916
917 for (byte = 0; byte < 4; byte++)
918 clrsetbits_le32(&denali_phy[63 + (128 * byte)], 0xffff << 16,
919 0x200 << 16);
920
921 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
922 clrbits_le32(&denali_phy[896], 1);
923
924 /* CTL_200 ctrlupd_req 1bit offset_8 */
925 clrsetbits_le32(&denali_ctl[200], 0x1 << 8, 0x1 << 8);
926}
927
928static int data_training_ca(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530929 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800930{
931 u32 *denali_pi = chan->pi->denali_pi;
932 u32 *denali_phy = chan->publ->denali_phy;
933 u32 i, tmp;
934 u32 obs_0, obs_1, obs_2, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +0530935 u32 rank = params->ch[channel].cap_info.rank;
Jagan Tekibafcc142019-07-15 23:58:41 +0530936 u32 rank_mask;
Kever Yang50fb9982017-02-22 16:56:35 +0800937
Jagan Tekia6079612019-07-15 23:58:40 +0530938 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
939 writel(0x00003f7c, (&denali_pi[175]));
940
Jagan Tekif05675e2019-07-16 17:27:09 +0530941 if (params->base.dramtype == LPDDR4)
942 rank_mask = (rank == 1) ? 0x5 : 0xf;
943 else
944 rank_mask = (rank == 1) ? 0x1 : 0x3;
Jagan Tekibafcc142019-07-15 23:58:41 +0530945
946 for (i = 0; i < 4; i++) {
947 if (!(rank_mask & (1 << i)))
948 continue;
949
Kever Yang50fb9982017-02-22 16:56:35 +0800950 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530951
Kever Yang50fb9982017-02-22 16:56:35 +0800952 /* PI_100 PI_CALVL_EN:RW:8:2 */
953 clrsetbits_le32(&denali_pi[100], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530954
Kever Yang50fb9982017-02-22 16:56:35 +0800955 /* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */
956 clrsetbits_le32(&denali_pi[92],
957 (0x1 << 16) | (0x3 << 24),
958 (0x1 << 16) | (i << 24));
959
960 /* Waiting for training complete */
961 while (1) {
962 /* PI_174 PI_INT_STATUS:RD:8:18 */
963 tmp = readl(&denali_pi[174]) >> 8;
964 /*
965 * check status obs
966 * PHY_532/660/789 phy_adr_calvl_obs1_:0:32
967 */
968 obs_0 = readl(&denali_phy[532]);
969 obs_1 = readl(&denali_phy[660]);
970 obs_2 = readl(&denali_phy[788]);
971 if (((obs_0 >> 30) & 0x3) ||
972 ((obs_1 >> 30) & 0x3) ||
973 ((obs_2 >> 30) & 0x3))
974 obs_err = 1;
975 if ((((tmp >> 11) & 0x1) == 0x1) &&
976 (((tmp >> 13) & 0x1) == 0x1) &&
977 (((tmp >> 5) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +0530978 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800979 break;
980 else if ((((tmp >> 5) & 0x1) == 0x1) ||
981 (obs_err == 1))
982 return -EIO;
983 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530984
Kever Yang50fb9982017-02-22 16:56:35 +0800985 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
986 writel(0x00003f7c, (&denali_pi[175]));
987 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530988
Kever Yang50fb9982017-02-22 16:56:35 +0800989 clrbits_le32(&denali_pi[100], 0x3 << 8);
990
991 return 0;
992}
993
994static int data_training_wl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530995 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800996{
997 u32 *denali_pi = chan->pi->denali_pi;
998 u32 *denali_phy = chan->publ->denali_phy;
999 u32 i, tmp;
1000 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +05301001 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001002
Jagan Tekia6079612019-07-15 23:58:40 +05301003 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1004 writel(0x00003f7c, (&denali_pi[175]));
1005
Kever Yang50fb9982017-02-22 16:56:35 +08001006 for (i = 0; i < rank; i++) {
1007 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301008
Kever Yang50fb9982017-02-22 16:56:35 +08001009 /* PI_60 PI_WRLVL_EN:RW:8:2 */
1010 clrsetbits_le32(&denali_pi[60], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301011
Kever Yang50fb9982017-02-22 16:56:35 +08001012 /* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */
1013 clrsetbits_le32(&denali_pi[59],
1014 (0x1 << 8) | (0x3 << 16),
1015 (0x1 << 8) | (i << 16));
1016
1017 /* Waiting for training complete */
1018 while (1) {
1019 /* PI_174 PI_INT_STATUS:RD:8:18 */
1020 tmp = readl(&denali_pi[174]) >> 8;
1021
1022 /*
1023 * check status obs, if error maybe can not
1024 * get leveling done PHY_40/168/296/424
1025 * phy_wrlvl_status_obs_X:0:13
1026 */
1027 obs_0 = readl(&denali_phy[40]);
1028 obs_1 = readl(&denali_phy[168]);
1029 obs_2 = readl(&denali_phy[296]);
1030 obs_3 = readl(&denali_phy[424]);
1031 if (((obs_0 >> 12) & 0x1) ||
1032 ((obs_1 >> 12) & 0x1) ||
1033 ((obs_2 >> 12) & 0x1) ||
1034 ((obs_3 >> 12) & 0x1))
1035 obs_err = 1;
1036 if ((((tmp >> 10) & 0x1) == 0x1) &&
1037 (((tmp >> 13) & 0x1) == 0x1) &&
1038 (((tmp >> 4) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +05301039 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001040 break;
1041 else if ((((tmp >> 4) & 0x1) == 0x1) ||
1042 (obs_err == 1))
1043 return -EIO;
1044 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301045
Kever Yang50fb9982017-02-22 16:56:35 +08001046 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1047 writel(0x00003f7c, (&denali_pi[175]));
1048 }
1049
1050 override_write_leveling_value(chan);
1051 clrbits_le32(&denali_pi[60], 0x3 << 8);
1052
1053 return 0;
1054}
1055
1056static int data_training_rg(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301057 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001058{
1059 u32 *denali_pi = chan->pi->denali_pi;
1060 u32 *denali_phy = chan->publ->denali_phy;
1061 u32 i, tmp;
1062 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +05301063 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001064
Jagan Tekia6079612019-07-15 23:58:40 +05301065 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1066 writel(0x00003f7c, (&denali_pi[175]));
1067
Kever Yang50fb9982017-02-22 16:56:35 +08001068 for (i = 0; i < rank; i++) {
1069 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301070
Kever Yang50fb9982017-02-22 16:56:35 +08001071 /* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */
1072 clrsetbits_le32(&denali_pi[80], 0x3 << 24, 0x2 << 24);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301073
Kever Yang50fb9982017-02-22 16:56:35 +08001074 /*
1075 * PI_74 PI_RDLVL_GATE_REQ:WR:16:1
1076 * PI_RDLVL_CS:RW:24:2
1077 */
1078 clrsetbits_le32(&denali_pi[74],
1079 (0x1 << 16) | (0x3 << 24),
1080 (0x1 << 16) | (i << 24));
1081
1082 /* Waiting for training complete */
1083 while (1) {
1084 /* PI_174 PI_INT_STATUS:RD:8:18 */
1085 tmp = readl(&denali_pi[174]) >> 8;
1086
1087 /*
1088 * check status obs
1089 * PHY_43/171/299/427
1090 * PHY_GTLVL_STATUS_OBS_x:16:8
1091 */
1092 obs_0 = readl(&denali_phy[43]);
1093 obs_1 = readl(&denali_phy[171]);
1094 obs_2 = readl(&denali_phy[299]);
1095 obs_3 = readl(&denali_phy[427]);
1096 if (((obs_0 >> (16 + 6)) & 0x3) ||
1097 ((obs_1 >> (16 + 6)) & 0x3) ||
1098 ((obs_2 >> (16 + 6)) & 0x3) ||
1099 ((obs_3 >> (16 + 6)) & 0x3))
1100 obs_err = 1;
1101 if ((((tmp >> 9) & 0x1) == 0x1) &&
1102 (((tmp >> 13) & 0x1) == 0x1) &&
1103 (((tmp >> 3) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +05301104 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001105 break;
1106 else if ((((tmp >> 3) & 0x1) == 0x1) ||
1107 (obs_err == 1))
1108 return -EIO;
1109 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301110
Kever Yang50fb9982017-02-22 16:56:35 +08001111 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1112 writel(0x00003f7c, (&denali_pi[175]));
1113 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301114
Kever Yang50fb9982017-02-22 16:56:35 +08001115 clrbits_le32(&denali_pi[80], 0x3 << 24);
1116
1117 return 0;
1118}
1119
1120static int data_training_rl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301121 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001122{
1123 u32 *denali_pi = chan->pi->denali_pi;
1124 u32 i, tmp;
Jagan Teki97867c82019-07-15 23:51:05 +05301125 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001126
Jagan Tekia6079612019-07-15 23:58:40 +05301127 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1128 writel(0x00003f7c, (&denali_pi[175]));
1129
Kever Yang50fb9982017-02-22 16:56:35 +08001130 for (i = 0; i < rank; i++) {
1131 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301132
Kever Yang50fb9982017-02-22 16:56:35 +08001133 /* PI_80 PI_RDLVL_EN:RW:16:2 */
1134 clrsetbits_le32(&denali_pi[80], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301135
Kever Yang50fb9982017-02-22 16:56:35 +08001136 /* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */
1137 clrsetbits_le32(&denali_pi[74],
1138 (0x1 << 8) | (0x3 << 24),
1139 (0x1 << 8) | (i << 24));
1140
1141 /* Waiting for training complete */
1142 while (1) {
1143 /* PI_174 PI_INT_STATUS:RD:8:18 */
1144 tmp = readl(&denali_pi[174]) >> 8;
1145
1146 /*
1147 * make sure status obs not report error bit
1148 * PHY_46/174/302/430
1149 * phy_rdlvl_status_obs_X:16:8
1150 */
1151 if ((((tmp >> 8) & 0x1) == 0x1) &&
1152 (((tmp >> 13) & 0x1) == 0x1) &&
1153 (((tmp >> 2) & 0x1) == 0x0))
1154 break;
1155 else if (((tmp >> 2) & 0x1) == 0x1)
1156 return -EIO;
1157 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301158
Kever Yang50fb9982017-02-22 16:56:35 +08001159 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1160 writel(0x00003f7c, (&denali_pi[175]));
1161 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301162
Kever Yang50fb9982017-02-22 16:56:35 +08001163 clrbits_le32(&denali_pi[80], 0x3 << 16);
1164
1165 return 0;
1166}
1167
1168static int data_training_wdql(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301169 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001170{
1171 u32 *denali_pi = chan->pi->denali_pi;
1172 u32 i, tmp;
Jagan Teki97867c82019-07-15 23:51:05 +05301173 u32 rank = params->ch[channel].cap_info.rank;
Jagan Teki87723592019-07-15 23:58:42 +05301174 u32 rank_mask;
Kever Yang50fb9982017-02-22 16:56:35 +08001175
Jagan Tekia6079612019-07-15 23:58:40 +05301176 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1177 writel(0x00003f7c, (&denali_pi[175]));
1178
Jagan Tekid7504c02019-07-16 17:27:10 +05301179 if (params->base.dramtype == LPDDR4)
1180 rank_mask = (rank == 1) ? 0x5 : 0xf;
1181 else
1182 rank_mask = (rank == 1) ? 0x1 : 0x3;
Jagan Teki87723592019-07-15 23:58:42 +05301183
1184 for (i = 0; i < 4; i++) {
1185 if (!(rank_mask & (1 << i)))
1186 continue;
1187
Kever Yang50fb9982017-02-22 16:56:35 +08001188 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301189
Kever Yang50fb9982017-02-22 16:56:35 +08001190 /*
1191 * disable PI_WDQLVL_VREF_EN before wdq leveling?
1192 * PI_181 PI_WDQLVL_VREF_EN:RW:8:1
1193 */
1194 clrbits_le32(&denali_pi[181], 0x1 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301195
Kever Yang50fb9982017-02-22 16:56:35 +08001196 /* PI_124 PI_WDQLVL_EN:RW:16:2 */
1197 clrsetbits_le32(&denali_pi[124], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301198
Kever Yang50fb9982017-02-22 16:56:35 +08001199 /* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */
1200 clrsetbits_le32(&denali_pi[121],
1201 (0x1 << 8) | (0x3 << 16),
1202 (0x1 << 8) | (i << 16));
1203
1204 /* Waiting for training complete */
1205 while (1) {
1206 /* PI_174 PI_INT_STATUS:RD:8:18 */
1207 tmp = readl(&denali_pi[174]) >> 8;
1208 if ((((tmp >> 12) & 0x1) == 0x1) &&
1209 (((tmp >> 13) & 0x1) == 0x1) &&
1210 (((tmp >> 6) & 0x1) == 0x0))
1211 break;
1212 else if (((tmp >> 6) & 0x1) == 0x1)
1213 return -EIO;
1214 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301215
Kever Yang50fb9982017-02-22 16:56:35 +08001216 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1217 writel(0x00003f7c, (&denali_pi[175]));
1218 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301219
Kever Yang50fb9982017-02-22 16:56:35 +08001220 clrbits_le32(&denali_pi[124], 0x3 << 16);
1221
1222 return 0;
1223}
1224
1225static int data_training(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301226 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +08001227 u32 training_flag)
1228{
1229 u32 *denali_phy = chan->publ->denali_phy;
Jagan Teki6214ff22019-07-15 23:58:39 +05301230 int ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001231
1232 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1233 setbits_le32(&denali_phy[927], (1 << 22));
1234
1235 if (training_flag == PI_FULL_TRAINING) {
Jagan Tekia58ff792019-07-15 23:50:58 +05301236 if (params->base.dramtype == LPDDR4) {
Kever Yang50fb9982017-02-22 16:56:35 +08001237 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1238 PI_READ_GATE_TRAINING |
1239 PI_READ_LEVELING | PI_WDQ_LEVELING;
Jagan Tekia58ff792019-07-15 23:50:58 +05301240 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +08001241 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1242 PI_READ_GATE_TRAINING;
Jagan Tekia58ff792019-07-15 23:50:58 +05301243 } else if (params->base.dramtype == DDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +08001244 training_flag = PI_WRITE_LEVELING |
1245 PI_READ_GATE_TRAINING |
1246 PI_READ_LEVELING;
1247 }
1248 }
1249
1250 /* ca training(LPDDR4,LPDDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301251 if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING) {
1252 ret = data_training_ca(chan, channel, params);
1253 if (ret < 0) {
1254 debug("%s: data training ca failed\n", __func__);
1255 return ret;
1256 }
1257 }
Kever Yang50fb9982017-02-22 16:56:35 +08001258
1259 /* write leveling(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301260 if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING) {
1261 ret = data_training_wl(chan, channel, params);
1262 if (ret < 0) {
1263 debug("%s: data training wl failed\n", __func__);
1264 return ret;
1265 }
1266 }
Kever Yang50fb9982017-02-22 16:56:35 +08001267
1268 /* read gate training(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301269 if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING) {
1270 ret = data_training_rg(chan, channel, params);
1271 if (ret < 0) {
1272 debug("%s: data training rg failed\n", __func__);
1273 return ret;
1274 }
1275 }
Kever Yang50fb9982017-02-22 16:56:35 +08001276
1277 /* read leveling(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301278 if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING) {
1279 ret = data_training_rl(chan, channel, params);
1280 if (ret < 0) {
1281 debug("%s: data training rl failed\n", __func__);
1282 return ret;
1283 }
1284 }
Kever Yang50fb9982017-02-22 16:56:35 +08001285
1286 /* wdq leveling(LPDDR4 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301287 if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING) {
1288 ret = data_training_wdql(chan, channel, params);
1289 if (ret < 0) {
1290 debug("%s: data training wdql failed\n", __func__);
1291 return ret;
1292 }
1293 }
Kever Yang50fb9982017-02-22 16:56:35 +08001294
1295 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1296 clrbits_le32(&denali_phy[927], (1 << 22));
1297
1298 return 0;
1299}
1300
1301static void set_ddrconfig(const struct chan_info *chan,
Jagan Tekia58ff792019-07-15 23:50:58 +05301302 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +08001303 unsigned char channel, u32 ddrconfig)
1304{
1305 /* only need to set ddrconfig */
1306 struct rk3399_msch_regs *ddr_msch_regs = chan->msch;
1307 unsigned int cs0_cap = 0;
1308 unsigned int cs1_cap = 0;
1309
Jagan Teki97867c82019-07-15 23:51:05 +05301310 cs0_cap = (1 << (params->ch[channel].cap_info.cs0_row
1311 + params->ch[channel].cap_info.col
1312 + params->ch[channel].cap_info.bk
1313 + params->ch[channel].cap_info.bw - 20));
1314 if (params->ch[channel].cap_info.rank > 1)
1315 cs1_cap = cs0_cap >> (params->ch[channel].cap_info.cs0_row
1316 - params->ch[channel].cap_info.cs1_row);
1317 if (params->ch[channel].cap_info.row_3_4) {
Kever Yang50fb9982017-02-22 16:56:35 +08001318 cs0_cap = cs0_cap * 3 / 4;
1319 cs1_cap = cs1_cap * 3 / 4;
1320 }
1321
1322 writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf);
1323 writel(((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8),
1324 &ddr_msch_regs->ddrsize);
1325}
1326
1327static void dram_all_config(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +05301328 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001329{
Jagan Teki2d337122019-07-16 17:27:00 +05301330 u32 sys_reg2 = 0;
Jagan Teki9d8769c2019-07-16 17:27:01 +05301331 u32 sys_reg3 = 0;
Kever Yang50fb9982017-02-22 16:56:35 +08001332 unsigned int channel, idx;
1333
Jagan Teki2d337122019-07-16 17:27:00 +05301334 sys_reg2 |= SYS_REG_ENC_DDRTYPE(params->base.dramtype);
1335 sys_reg2 |= SYS_REG_ENC_NUM_CH(params->base.num_channels);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301336
Kever Yang50fb9982017-02-22 16:56:35 +08001337 for (channel = 0, idx = 0;
Jagan Tekia58ff792019-07-15 23:50:58 +05301338 (idx < params->base.num_channels) && (channel < 2);
Kever Yang50fb9982017-02-22 16:56:35 +08001339 channel++) {
Jagan Tekia58ff792019-07-15 23:50:58 +05301340 const struct rk3399_sdram_channel *info = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +08001341 struct rk3399_msch_regs *ddr_msch_regs;
1342 const struct rk3399_msch_timings *noc_timing;
1343
Jagan Teki97867c82019-07-15 23:51:05 +05301344 if (params->ch[channel].cap_info.col == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001345 continue;
1346 idx++;
Jagan Teki2d337122019-07-16 17:27:00 +05301347 sys_reg2 |= SYS_REG_ENC_ROW_3_4(info->cap_info.row_3_4, channel);
1348 sys_reg2 |= SYS_REG_ENC_CHINFO(channel);
1349 sys_reg2 |= SYS_REG_ENC_RANK(info->cap_info.rank, channel);
1350 sys_reg2 |= SYS_REG_ENC_COL(info->cap_info.col, channel);
1351 sys_reg2 |= SYS_REG_ENC_BK(info->cap_info.bk, channel);
Jagan Teki2d337122019-07-16 17:27:00 +05301352 sys_reg2 |= SYS_REG_ENC_BW(info->cap_info.bw, channel);
1353 sys_reg2 |= SYS_REG_ENC_DBW(info->cap_info.dbw, channel);
Jagan Teki9d8769c2019-07-16 17:27:01 +05301354 SYS_REG_ENC_CS0_ROW(info->cap_info.cs0_row, sys_reg2, sys_reg3, channel);
1355 if (info->cap_info.cs1_row)
1356 SYS_REG_ENC_CS1_ROW(info->cap_info.cs1_row, sys_reg2,
1357 sys_reg3, channel);
1358 sys_reg3 |= SYS_REG_ENC_CS1_COL(info->cap_info.col, channel);
Jagan Teki932dd962019-07-16 17:27:04 +05301359 sys_reg3 |= SYS_REG_ENC_VERSION(DDR_SYS_REG_VERSION);
Kever Yang50fb9982017-02-22 16:56:35 +08001360
1361 ddr_msch_regs = dram->chan[channel].msch;
Jagan Tekia58ff792019-07-15 23:50:58 +05301362 noc_timing = &params->ch[channel].noc_timings;
Kever Yang50fb9982017-02-22 16:56:35 +08001363 writel(noc_timing->ddrtiminga0,
1364 &ddr_msch_regs->ddrtiminga0);
1365 writel(noc_timing->ddrtimingb0,
1366 &ddr_msch_regs->ddrtimingb0);
Jagan Teki5465f9b2019-07-16 17:27:05 +05301367 writel(noc_timing->ddrtimingc0.d32,
Kever Yang50fb9982017-02-22 16:56:35 +08001368 &ddr_msch_regs->ddrtimingc0);
1369 writel(noc_timing->devtodev0,
1370 &ddr_msch_regs->devtodev0);
Jagan Teki264a09f2019-07-16 17:27:06 +05301371 writel(noc_timing->ddrmode.d32,
Kever Yang50fb9982017-02-22 16:56:35 +08001372 &ddr_msch_regs->ddrmode);
1373
Jagan Tekib02c5482019-07-16 17:27:20 +05301374 /**
1375 * rank 1 memory clock disable (dfi_dram_clk_disable = 1)
1376 *
1377 * The hardware for LPDDR4 with
1378 * - CLK0P/N connect to lower 16-bits
1379 * - CLK1P/N connect to higher 16-bits
1380 *
1381 * dfi dram clk is configured via CLK1P/N, so disabling
1382 * dfi dram clk will disable the CLK1P/N as well for lpddr4.
1383 */
1384 if (params->ch[channel].cap_info.rank == 1 &&
1385 params->base.dramtype != LPDDR4)
Kever Yang50fb9982017-02-22 16:56:35 +08001386 setbits_le32(&dram->chan[channel].pctl->denali_ctl[276],
1387 1 << 17);
1388 }
1389
Jagan Teki2d337122019-07-16 17:27:00 +05301390 writel(sys_reg2, &dram->pmugrf->os_reg2);
Jagan Teki9d8769c2019-07-16 17:27:01 +05301391 writel(sys_reg3, &dram->pmugrf->os_reg3);
Kever Yang50fb9982017-02-22 16:56:35 +08001392 rk_clrsetreg(&dram->pmusgrf->soc_con4, 0x1f << 10,
Jagan Tekia58ff792019-07-15 23:50:58 +05301393 params->base.stride << 10);
Kever Yang50fb9982017-02-22 16:56:35 +08001394
1395 /* reboot hold register set */
1396 writel(PRESET_SGRF_HOLD(0) | PRESET_GPIO0_HOLD(1) |
1397 PRESET_GPIO1_HOLD(1),
1398 &dram->pmucru->pmucru_rstnhold_con[1]);
1399 clrsetbits_le32(&dram->cru->glb_rst_con, 0x3, 0x3);
1400}
1401
1402static int switch_to_phy_index1(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +05301403 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001404{
1405 u32 channel;
1406 u32 *denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +05301407 u32 ch_count = params->base.num_channels;
Kever Yang50fb9982017-02-22 16:56:35 +08001408 int ret;
1409 int i = 0;
1410
1411 writel(RK_CLRSETBITS(0x03 << 4 | 1 << 2 | 1,
1412 1 << 4 | 1 << 2 | 1),
1413 &dram->cic->cic_ctrl0);
1414 while (!(readl(&dram->cic->cic_status0) & (1 << 2))) {
1415 mdelay(10);
1416 i++;
1417 if (i > 10) {
1418 debug("index1 frequency change overtime\n");
1419 return -ETIME;
1420 }
1421 }
1422
1423 i = 0;
1424 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &dram->cic->cic_ctrl0);
1425 while (!(readl(&dram->cic->cic_status0) & (1 << 0))) {
1426 mdelay(10);
Heinrich Schuchardt80516592018-03-18 12:10:55 +01001427 i++;
Kever Yang50fb9982017-02-22 16:56:35 +08001428 if (i > 10) {
1429 debug("index1 frequency done overtime\n");
1430 return -ETIME;
1431 }
1432 }
1433
1434 for (channel = 0; channel < ch_count; channel++) {
1435 denali_phy = dram->chan[channel].publ->denali_phy;
1436 clrsetbits_le32(&denali_phy[896], (0x3 << 8) | 1, 1 << 8);
1437 ret = data_training(&dram->chan[channel], channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301438 params, PI_FULL_TRAINING);
Jagan Teki6214ff22019-07-15 23:58:39 +05301439 if (ret < 0) {
Kever Yang50fb9982017-02-22 16:56:35 +08001440 debug("index1 training failed\n");
1441 return ret;
1442 }
1443 }
1444
1445 return 0;
1446}
1447
Jagan Teki2525fae2019-07-15 23:58:52 +05301448static unsigned char calculate_stride(struct rk3399_sdram_params *params)
1449{
1450 unsigned int stride = params->base.stride;
1451 unsigned int channel, chinfo = 0;
1452 unsigned int ch_cap[2] = {0, 0};
1453 u64 cap;
1454
1455 for (channel = 0; channel < 2; channel++) {
1456 unsigned int cs0_cap = 0;
1457 unsigned int cs1_cap = 0;
1458 struct sdram_cap_info *cap_info = &params->ch[channel].cap_info;
1459
1460 if (cap_info->col == 0)
1461 continue;
1462
1463 cs0_cap = (1 << (cap_info->cs0_row + cap_info->col +
1464 cap_info->bk + cap_info->bw - 20));
1465 if (cap_info->rank > 1)
1466 cs1_cap = cs0_cap >> (cap_info->cs0_row
1467 - cap_info->cs1_row);
1468 if (cap_info->row_3_4) {
1469 cs0_cap = cs0_cap * 3 / 4;
1470 cs1_cap = cs1_cap * 3 / 4;
1471 }
1472 ch_cap[channel] = cs0_cap + cs1_cap;
1473 chinfo |= 1 << channel;
1474 }
1475
Jagan Teki874dede2019-07-15 23:58:53 +05301476 /* stride calculation for 1 channel */
1477 if (params->base.num_channels == 1 && chinfo & 1)
1478 return 0x17; /* channel a */
1479
Jagan Teki2525fae2019-07-15 23:58:52 +05301480 /* stride calculation for 2 channels, default gstride type is 256B */
1481 if (ch_cap[0] == ch_cap[1]) {
1482 cap = ch_cap[0] + ch_cap[1];
1483 switch (cap) {
1484 /* 512MB */
1485 case 512:
1486 stride = 0;
1487 break;
1488 /* 1GB */
1489 case 1024:
1490 stride = 0x5;
1491 break;
1492 /*
1493 * 768MB + 768MB same as total 2GB memory
1494 * useful space: 0-768MB 1GB-1792MB
1495 */
1496 case 1536:
1497 /* 2GB */
1498 case 2048:
1499 stride = 0x9;
1500 break;
1501 /* 1536MB + 1536MB */
1502 case 3072:
1503 stride = 0x11;
1504 break;
1505 /* 4GB */
1506 case 4096:
1507 stride = 0xD;
1508 break;
1509 default:
1510 printf("%s: Unable to calculate stride for ", __func__);
1511 print_size((cap * (1 << 20)), " capacity\n");
1512 break;
1513 }
1514 }
1515
Jagan Teki8eed4a42019-07-15 23:58:55 +05301516 sdram_print_stride(stride);
1517
Jagan Teki2525fae2019-07-15 23:58:52 +05301518 return stride;
1519}
1520
Jagan Teki43485e12019-07-15 23:58:54 +05301521static void clear_channel_params(struct rk3399_sdram_params *params, u8 channel)
1522{
1523 params->ch[channel].cap_info.rank = 0;
1524 params->ch[channel].cap_info.col = 0;
1525 params->ch[channel].cap_info.bk = 0;
1526 params->ch[channel].cap_info.bw = 32;
1527 params->ch[channel].cap_info.dbw = 32;
1528 params->ch[channel].cap_info.row_3_4 = 0;
1529 params->ch[channel].cap_info.cs0_row = 0;
1530 params->ch[channel].cap_info.cs1_row = 0;
1531 params->ch[channel].cap_info.ddrconfig = 0;
1532}
1533
1534static int pctl_init(struct dram_info *dram, struct rk3399_sdram_params *params)
1535{
1536 int channel;
1537 int ret;
1538
1539 for (channel = 0; channel < 2; channel++) {
1540 const struct chan_info *chan = &dram->chan[channel];
1541 struct rk3399_cru *cru = dram->cru;
1542 struct rk3399_ddr_publ_regs *publ = chan->publ;
1543
1544 phy_pctrl_reset(cru, channel);
1545 phy_dll_bypass_set(publ, params->base.ddr_freq);
1546
1547 ret = pctl_cfg(dram, chan, channel, params);
1548 if (ret < 0) {
1549 printf("%s: pctl config failed\n", __func__);
1550 return ret;
1551 }
1552
1553 /* start to trigger initialization */
1554 pctl_start(dram, channel);
1555 }
1556
1557 return 0;
1558}
1559
Kever Yang50fb9982017-02-22 16:56:35 +08001560static int sdram_init(struct dram_info *dram,
Jagan Teki2525fae2019-07-15 23:58:52 +05301561 struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001562{
Jagan Tekia58ff792019-07-15 23:50:58 +05301563 unsigned char dramtype = params->base.dramtype;
1564 unsigned int ddr_freq = params->base.ddr_freq;
Jagan Teki43485e12019-07-15 23:58:54 +05301565 u32 training_flag = PI_READ_GATE_TRAINING;
1566 int channel, ch, rank;
Jagan Teki2ef77ed2019-07-15 23:50:59 +05301567 int ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001568
1569 debug("Starting SDRAM initialization...\n");
1570
Philipp Tomsich39dce4a2017-05-31 18:16:35 +02001571 if ((dramtype == DDR3 && ddr_freq > 933) ||
Kever Yang50fb9982017-02-22 16:56:35 +08001572 (dramtype == LPDDR3 && ddr_freq > 933) ||
1573 (dramtype == LPDDR4 && ddr_freq > 800)) {
1574 debug("SDRAM frequency is to high!");
1575 return -E2BIG;
1576 }
1577
Jagan Teki43485e12019-07-15 23:58:54 +05301578 for (ch = 0; ch < 2; ch++) {
1579 params->ch[ch].cap_info.rank = 2;
1580 for (rank = 2; rank != 0; rank--) {
1581 ret = pctl_init(dram, params);
1582 if (ret < 0) {
1583 printf("%s: pctl init failed\n", __func__);
1584 return ret;
1585 }
1586
1587 /* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */
1588 if (dramtype == LPDDR3)
1589 udelay(10);
1590
1591 params->ch[ch].cap_info.rank = rank;
1592
1593 /*
1594 * LPDDR3 CA training msut be trigger before
1595 * other training.
1596 * DDR3 is not have CA training.
1597 */
1598 if (params->base.dramtype == LPDDR3)
1599 training_flag |= PI_CA_TRAINING;
1600
1601 if (!(data_training(&dram->chan[ch], ch,
1602 params, training_flag)))
1603 break;
1604 }
1605 /* Computed rank with associated channel number */
1606 params->ch[ch].cap_info.rank = rank;
1607 }
1608
1609 params->base.num_channels = 0;
Kever Yang50fb9982017-02-22 16:56:35 +08001610 for (channel = 0; channel < 2; channel++) {
1611 const struct chan_info *chan = &dram->chan[channel];
Jagan Teki43485e12019-07-15 23:58:54 +05301612 struct sdram_cap_info *cap_info = &params->ch[channel].cap_info;
1613 u8 training_flag = PI_FULL_TRAINING;
Kever Yang50fb9982017-02-22 16:56:35 +08001614
Jagan Teki43485e12019-07-15 23:58:54 +05301615 if (cap_info->rank == 0) {
1616 clear_channel_params(params, channel);
Kever Yang50fb9982017-02-22 16:56:35 +08001617 continue;
Jagan Teki43485e12019-07-15 23:58:54 +05301618 } else {
1619 params->base.num_channels++;
Kever Yang50fb9982017-02-22 16:56:35 +08001620 }
1621
Jagan Teki43485e12019-07-15 23:58:54 +05301622 debug("Channel ");
1623 debug(channel ? "1: " : "0: ");
Jagan Tekic9151e22019-07-15 23:58:45 +05301624
Jagan Teki43485e12019-07-15 23:58:54 +05301625 /* LPDDR3 should have write and read gate training */
1626 if (params->base.dramtype == LPDDR3)
1627 training_flag = PI_WRITE_LEVELING |
1628 PI_READ_GATE_TRAINING;
Kever Yang50fb9982017-02-22 16:56:35 +08001629
Jagan Teki43485e12019-07-15 23:58:54 +05301630 if (params->base.dramtype != LPDDR4) {
1631 ret = data_training(dram, channel, params,
1632 training_flag);
1633 if (!ret) {
1634 debug("%s: data train failed for channel %d\n",
1635 __func__, ret);
1636 continue;
1637 }
Kever Yang50fb9982017-02-22 16:56:35 +08001638 }
1639
Jagan Teki8eed4a42019-07-15 23:58:55 +05301640 sdram_print_ddr_info(cap_info, &params->base);
1641
Jagan Teki43485e12019-07-15 23:58:54 +05301642 set_ddrconfig(chan, params, channel, cap_info->ddrconfig);
1643 }
1644
1645 if (params->base.num_channels == 0) {
1646 printf("%s: ", __func__);
Jagan Teki8eed4a42019-07-15 23:58:55 +05301647 sdram_print_dram_type(params->base.dramtype);
Jagan Teki43485e12019-07-15 23:58:54 +05301648 printf(" - %dMHz failed!\n", params->base.ddr_freq);
1649 return -EINVAL;
Kever Yang50fb9982017-02-22 16:56:35 +08001650 }
Jagan Teki2525fae2019-07-15 23:58:52 +05301651
1652 params->base.stride = calculate_stride(params);
Jagan Tekia58ff792019-07-15 23:50:58 +05301653 dram_all_config(dram, params);
1654 switch_to_phy_index1(dram, params);
Kever Yang50fb9982017-02-22 16:56:35 +08001655
1656 debug("Finish SDRAM initialization...\n");
1657 return 0;
1658}
1659
1660static int rk3399_dmc_ofdata_to_platdata(struct udevice *dev)
1661{
1662#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1663 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
Kever Yang50fb9982017-02-22 16:56:35 +08001664 int ret;
1665
Philipp Tomsich0250c232017-06-07 18:46:03 +02001666 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
1667 (u32 *)&plat->sdram_params,
1668 sizeof(plat->sdram_params) / sizeof(u32));
Kever Yang50fb9982017-02-22 16:56:35 +08001669 if (ret) {
1670 printf("%s: Cannot read rockchip,sdram-params %d\n",
1671 __func__, ret);
1672 return ret;
1673 }
Masahiro Yamadae4873e32018-04-19 12:14:03 +09001674 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001675 if (ret)
1676 printf("%s: regmap failed %d\n", __func__, ret);
1677
1678#endif
1679 return 0;
1680}
1681
1682#if CONFIG_IS_ENABLED(OF_PLATDATA)
1683static int conv_of_platdata(struct udevice *dev)
1684{
1685 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1686 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1687 int ret;
1688
1689 ret = regmap_init_mem_platdata(dev, dtplat->reg,
Jagan Tekif676c7c2019-07-15 23:50:56 +05301690 ARRAY_SIZE(dtplat->reg) / 2,
1691 &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001692 if (ret)
1693 return ret;
1694
1695 return 0;
1696}
1697#endif
1698
1699static int rk3399_dmc_init(struct udevice *dev)
1700{
1701 struct dram_info *priv = dev_get_priv(dev);
1702 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1703 int ret;
1704#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1705 struct rk3399_sdram_params *params = &plat->sdram_params;
1706#else
1707 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1708 struct rk3399_sdram_params *params =
1709 (void *)dtplat->rockchip_sdram_params;
1710
1711 ret = conv_of_platdata(dev);
1712 if (ret)
1713 return ret;
1714#endif
1715
1716 priv->cic = syscon_get_first_range(ROCKCHIP_SYSCON_CIC);
Jagan Tekic9151e22019-07-15 23:58:45 +05301717 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Kever Yang50fb9982017-02-22 16:56:35 +08001718 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
1719 priv->pmusgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
1720 priv->pmucru = rockchip_get_pmucru();
1721 priv->cru = rockchip_get_cru();
1722 priv->chan[0].pctl = regmap_get_range(plat->map, 0);
1723 priv->chan[0].pi = regmap_get_range(plat->map, 1);
1724 priv->chan[0].publ = regmap_get_range(plat->map, 2);
1725 priv->chan[0].msch = regmap_get_range(plat->map, 3);
1726 priv->chan[1].pctl = regmap_get_range(plat->map, 4);
1727 priv->chan[1].pi = regmap_get_range(plat->map, 5);
1728 priv->chan[1].publ = regmap_get_range(plat->map, 6);
1729 priv->chan[1].msch = regmap_get_range(plat->map, 7);
1730
1731 debug("con reg %p %p %p %p %p %p %p %p\n",
1732 priv->chan[0].pctl, priv->chan[0].pi,
1733 priv->chan[0].publ, priv->chan[0].msch,
1734 priv->chan[1].pctl, priv->chan[1].pi,
1735 priv->chan[1].publ, priv->chan[1].msch);
1736 debug("cru %p, cic %p, grf %p, sgrf %p, pmucru %p\n", priv->cru,
1737 priv->cic, priv->pmugrf, priv->pmusgrf, priv->pmucru);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301738
Kever Yang50fb9982017-02-22 16:56:35 +08001739#if CONFIG_IS_ENABLED(OF_PLATDATA)
1740 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->ddr_clk);
1741#else
1742 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
1743#endif
1744 if (ret) {
1745 printf("%s clk get failed %d\n", __func__, ret);
1746 return ret;
1747 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301748
Kever Yang50fb9982017-02-22 16:56:35 +08001749 ret = clk_set_rate(&priv->ddr_clk, params->base.ddr_freq * MHz);
1750 if (ret < 0) {
1751 printf("%s clk set failed %d\n", __func__, ret);
1752 return ret;
1753 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301754
Kever Yang50fb9982017-02-22 16:56:35 +08001755 ret = sdram_init(priv, params);
1756 if (ret < 0) {
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301757 printf("%s DRAM init failed %d\n", __func__, ret);
Kever Yang50fb9982017-02-22 16:56:35 +08001758 return ret;
1759 }
1760
1761 return 0;
1762}
1763#endif
1764
Kever Yang50fb9982017-02-22 16:56:35 +08001765static int rk3399_dmc_probe(struct udevice *dev)
1766{
Kever Yang7f347842019-04-01 17:20:53 +08001767#if defined(CONFIG_TPL_BUILD) || \
1768 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001769 if (rk3399_dmc_init(dev))
1770 return 0;
1771#else
1772 struct dram_info *priv = dev_get_priv(dev);
1773
1774 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301775 debug("%s: pmugrf = %p\n", __func__, priv->pmugrf);
Kever Yang6c15a542017-06-23 16:11:06 +08001776 priv->info.base = CONFIG_SYS_SDRAM_BASE;
Jagan Tekif676c7c2019-07-15 23:50:56 +05301777 priv->info.size =
1778 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg2);
Kever Yang50fb9982017-02-22 16:56:35 +08001779#endif
1780 return 0;
1781}
1782
1783static int rk3399_dmc_get_info(struct udevice *dev, struct ram_info *info)
1784{
1785 struct dram_info *priv = dev_get_priv(dev);
1786
Kever Yangea61d142017-04-19 16:01:14 +08001787 *info = priv->info;
Kever Yang50fb9982017-02-22 16:56:35 +08001788
1789 return 0;
1790}
1791
1792static struct ram_ops rk3399_dmc_ops = {
1793 .get_info = rk3399_dmc_get_info,
1794};
1795
Kever Yang50fb9982017-02-22 16:56:35 +08001796static const struct udevice_id rk3399_dmc_ids[] = {
1797 { .compatible = "rockchip,rk3399-dmc" },
1798 { }
1799};
1800
1801U_BOOT_DRIVER(dmc_rk3399) = {
1802 .name = "rockchip_rk3399_dmc",
1803 .id = UCLASS_RAM,
1804 .of_match = rk3399_dmc_ids,
1805 .ops = &rk3399_dmc_ops,
Kever Yang7f347842019-04-01 17:20:53 +08001806#if defined(CONFIG_TPL_BUILD) || \
1807 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001808 .ofdata_to_platdata = rk3399_dmc_ofdata_to_platdata,
1809#endif
1810 .probe = rk3399_dmc_probe,
Kever Yang50fb9982017-02-22 16:56:35 +08001811 .priv_auto_alloc_size = sizeof(struct dram_info),
Kever Yang7f347842019-04-01 17:20:53 +08001812#if defined(CONFIG_TPL_BUILD) || \
1813 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001814 .platdata_auto_alloc_size = sizeof(struct rockchip_dmc_plat),
1815#endif
1816};