blob: 3e069ab0ef339d25b51929bd02de9cf7fc39e8a6 [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 Tekic9151e22019-07-15 23:58:45 +053083static void *get_ddrc0_con(struct dram_info *dram, u8 channel)
84{
85 return (channel == 0) ? &dram->grf->ddrc0_con0 : &dram->grf->ddrc0_con1;
86}
87
Kever Yang50fb9982017-02-22 16:56:35 +080088static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
89{
90 int i;
91
92 for (i = 0; i < n / sizeof(u32); i++) {
93 writel(*src, dest);
94 src++;
95 dest++;
96 }
97}
98
Jagan Tekice75cfb2019-07-15 23:58:43 +053099static void rkclk_ddr_reset(struct rk3399_cru *cru, u32 channel, u32 ctl,
100 u32 phy)
101{
102 channel &= 0x1;
103 ctl &= 0x1;
104 phy &= 0x1;
105 writel(CRU_SFTRST_DDR_CTRL(channel, ctl) |
106 CRU_SFTRST_DDR_PHY(channel, phy),
107 &cru->softrst_con[4]);
108}
109
110static void phy_pctrl_reset(struct rk3399_cru *cru, u32 channel)
111{
112 rkclk_ddr_reset(cru, channel, 1, 1);
113 udelay(10);
114
115 rkclk_ddr_reset(cru, channel, 1, 0);
116 udelay(10);
117
118 rkclk_ddr_reset(cru, channel, 0, 0);
119 udelay(10);
120}
121
Kever Yang50fb9982017-02-22 16:56:35 +0800122static void phy_dll_bypass_set(struct rk3399_ddr_publ_regs *ddr_publ_regs,
123 u32 freq)
124{
125 u32 *denali_phy = ddr_publ_regs->denali_phy;
126
127 /* From IP spec, only freq small than 125 can enter dll bypass mode */
128 if (freq <= 125) {
129 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
130 setbits_le32(&denali_phy[86], (0x3 << 2) << 8);
131 setbits_le32(&denali_phy[214], (0x3 << 2) << 8);
132 setbits_le32(&denali_phy[342], (0x3 << 2) << 8);
133 setbits_le32(&denali_phy[470], (0x3 << 2) << 8);
134
135 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
136 setbits_le32(&denali_phy[547], (0x3 << 2) << 16);
137 setbits_le32(&denali_phy[675], (0x3 << 2) << 16);
138 setbits_le32(&denali_phy[803], (0x3 << 2) << 16);
139 } else {
140 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
141 clrbits_le32(&denali_phy[86], (0x3 << 2) << 8);
142 clrbits_le32(&denali_phy[214], (0x3 << 2) << 8);
143 clrbits_le32(&denali_phy[342], (0x3 << 2) << 8);
144 clrbits_le32(&denali_phy[470], (0x3 << 2) << 8);
145
146 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
147 clrbits_le32(&denali_phy[547], (0x3 << 2) << 16);
148 clrbits_le32(&denali_phy[675], (0x3 << 2) << 16);
149 clrbits_le32(&denali_phy[803], (0x3 << 2) << 16);
150 }
151}
152
153static void set_memory_map(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530154 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800155{
Jagan Tekia58ff792019-07-15 23:50:58 +0530156 const struct rk3399_sdram_channel *sdram_ch = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +0800157 u32 *denali_ctl = chan->pctl->denali_ctl;
158 u32 *denali_pi = chan->pi->denali_pi;
159 u32 cs_map;
160 u32 reduc;
161 u32 row;
162
163 /* Get row number from ddrconfig setting */
Jagan Teki97867c82019-07-15 23:51:05 +0530164 if (sdram_ch->cap_info.ddrconfig < 2 ||
165 sdram_ch->cap_info.ddrconfig == 4)
Kever Yang50fb9982017-02-22 16:56:35 +0800166 row = 16;
Jagan Teki97867c82019-07-15 23:51:05 +0530167 else if (sdram_ch->cap_info.ddrconfig == 3)
Kever Yang50fb9982017-02-22 16:56:35 +0800168 row = 14;
169 else
170 row = 15;
171
Jagan Teki97867c82019-07-15 23:51:05 +0530172 cs_map = (sdram_ch->cap_info.rank > 1) ? 3 : 1;
173 reduc = (sdram_ch->cap_info.bw == 2) ? 0 : 1;
Kever Yang50fb9982017-02-22 16:56:35 +0800174
175 /* Set the dram configuration to ctrl */
Jagan Teki97867c82019-07-15 23:51:05 +0530176 clrsetbits_le32(&denali_ctl[191], 0xF, (12 - sdram_ch->cap_info.col));
Kever Yang50fb9982017-02-22 16:56:35 +0800177 clrsetbits_le32(&denali_ctl[190], (0x3 << 16) | (0x7 << 24),
Jagan Teki97867c82019-07-15 23:51:05 +0530178 ((3 - sdram_ch->cap_info.bk) << 16) |
Kever Yang50fb9982017-02-22 16:56:35 +0800179 ((16 - row) << 24));
180
181 clrsetbits_le32(&denali_ctl[196], 0x3 | (1 << 16),
182 cs_map | (reduc << 16));
183
184 /* PI_199 PI_COL_DIFF:RW:0:4 */
Jagan Teki97867c82019-07-15 23:51:05 +0530185 clrsetbits_le32(&denali_pi[199], 0xF, (12 - sdram_ch->cap_info.col));
Kever Yang50fb9982017-02-22 16:56:35 +0800186
187 /* PI_155 PI_ROW_DIFF:RW:24:3 PI_BANK_DIFF:RW:16:2 */
188 clrsetbits_le32(&denali_pi[155], (0x3 << 16) | (0x7 << 24),
Jagan Teki97867c82019-07-15 23:51:05 +0530189 ((3 - sdram_ch->cap_info.bk) << 16) |
Kever Yang50fb9982017-02-22 16:56:35 +0800190 ((16 - row) << 24));
Jagan Teki9337cb32019-07-16 17:27:18 +0530191
192 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
193 if (cs_map == 1)
194 cs_map = 0x5;
195 else if (cs_map == 2)
196 cs_map = 0xa;
197 else
198 cs_map = 0xF;
199 }
200
Kever Yang50fb9982017-02-22 16:56:35 +0800201 /* PI_41 PI_CS_MAP:RW:24:4 */
202 clrsetbits_le32(&denali_pi[41], 0xf << 24, cs_map << 24);
Jagan Teki97867c82019-07-15 23:51:05 +0530203 if (sdram_ch->cap_info.rank == 1 && params->base.dramtype == DDR3)
Kever Yang50fb9982017-02-22 16:56:35 +0800204 writel(0x2EC7FFFF, &denali_pi[34]);
205}
206
Jagan Tekib5d46632019-07-16 17:27:07 +0530207static int phy_io_config(const struct chan_info *chan,
208 const struct rk3399_sdram_params *params)
209{
210 u32 *denali_phy = chan->publ->denali_phy;
211 u32 vref_mode_dq, vref_value_dq, vref_mode_ac, vref_value_ac;
212 u32 mode_sel;
213 u32 reg_value;
214 u32 drv_value, odt_value;
215 u32 speed;
216
217 /* vref setting */
218 if (params->base.dramtype == LPDDR4) {
219 /* LPDDR4 */
220 vref_mode_dq = 0x6;
221 vref_value_dq = 0x1f;
222 vref_mode_ac = 0x6;
223 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530224 mode_sel = 0x6;
Jagan Tekib5d46632019-07-16 17:27:07 +0530225 } else if (params->base.dramtype == LPDDR3) {
226 if (params->base.odt == 1) {
227 vref_mode_dq = 0x5; /* LPDDR3 ODT */
228 drv_value = (readl(&denali_phy[6]) >> 12) & 0xf;
229 odt_value = (readl(&denali_phy[6]) >> 4) & 0xf;
230 if (drv_value == PHY_DRV_ODT_48) {
231 switch (odt_value) {
232 case PHY_DRV_ODT_240:
233 vref_value_dq = 0x16;
234 break;
235 case PHY_DRV_ODT_120:
236 vref_value_dq = 0x26;
237 break;
238 case PHY_DRV_ODT_60:
239 vref_value_dq = 0x36;
240 break;
241 default:
242 debug("Invalid ODT value.\n");
243 return -EINVAL;
244 }
245 } else if (drv_value == PHY_DRV_ODT_40) {
246 switch (odt_value) {
247 case PHY_DRV_ODT_240:
248 vref_value_dq = 0x19;
249 break;
250 case PHY_DRV_ODT_120:
251 vref_value_dq = 0x23;
252 break;
253 case PHY_DRV_ODT_60:
254 vref_value_dq = 0x31;
255 break;
256 default:
257 debug("Invalid ODT value.\n");
258 return -EINVAL;
259 }
260 } else if (drv_value == PHY_DRV_ODT_34_3) {
261 switch (odt_value) {
262 case PHY_DRV_ODT_240:
263 vref_value_dq = 0x17;
264 break;
265 case PHY_DRV_ODT_120:
266 vref_value_dq = 0x20;
267 break;
268 case PHY_DRV_ODT_60:
269 vref_value_dq = 0x2e;
270 break;
271 default:
272 debug("Invalid ODT value.\n");
273 return -EINVAL;
274 }
275 } else {
276 debug("Invalid DRV value.\n");
277 return -EINVAL;
278 }
279 } else {
280 vref_mode_dq = 0x2; /* LPDDR3 */
281 vref_value_dq = 0x1f;
282 }
283 vref_mode_ac = 0x2;
284 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530285 mode_sel = 0x0;
Jagan Tekib5d46632019-07-16 17:27:07 +0530286 } else if (params->base.dramtype == DDR3) {
287 /* DDR3L */
288 vref_mode_dq = 0x1;
289 vref_value_dq = 0x1f;
290 vref_mode_ac = 0x1;
291 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530292 mode_sel = 0x1;
Jagan Tekib5d46632019-07-16 17:27:07 +0530293 } else {
294 debug("Unknown DRAM type.\n");
295 return -EINVAL;
296 }
297
298 reg_value = (vref_mode_dq << 9) | (0x1 << 8) | vref_value_dq;
299
300 /* PHY_913 PHY_PAD_VREF_CTRL_DQ_0 12bits offset_8 */
301 clrsetbits_le32(&denali_phy[913], 0xfff << 8, reg_value << 8);
302 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_1 12bits offset_0 */
303 clrsetbits_le32(&denali_phy[914], 0xfff, reg_value);
304 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_2 12bits offset_16 */
305 clrsetbits_le32(&denali_phy[914], 0xfff << 16, reg_value << 16);
306 /* PHY_915 PHY_PAD_VREF_CTRL_DQ_3 12bits offset_0 */
307 clrsetbits_le32(&denali_phy[915], 0xfff, reg_value);
308
309 reg_value = (vref_mode_ac << 9) | (0x1 << 8) | vref_value_ac;
310
311 /* PHY_915 PHY_PAD_VREF_CTRL_AC 12bits offset_16 */
312 clrsetbits_le32(&denali_phy[915], 0xfff << 16, reg_value << 16);
313
Jagan Tekib5d46632019-07-16 17:27:07 +0530314 /* PHY_924 PHY_PAD_FDBK_DRIVE */
315 clrsetbits_le32(&denali_phy[924], 0x7 << 15, mode_sel << 15);
316 /* PHY_926 PHY_PAD_DATA_DRIVE */
317 clrsetbits_le32(&denali_phy[926], 0x7 << 6, mode_sel << 6);
318 /* PHY_927 PHY_PAD_DQS_DRIVE */
319 clrsetbits_le32(&denali_phy[927], 0x7 << 6, mode_sel << 6);
320 /* PHY_928 PHY_PAD_ADDR_DRIVE */
321 clrsetbits_le32(&denali_phy[928], 0x7 << 14, mode_sel << 14);
322 /* PHY_929 PHY_PAD_CLK_DRIVE */
323 clrsetbits_le32(&denali_phy[929], 0x7 << 14, mode_sel << 14);
324 /* PHY_935 PHY_PAD_CKE_DRIVE */
325 clrsetbits_le32(&denali_phy[935], 0x7 << 14, mode_sel << 14);
326 /* PHY_937 PHY_PAD_RST_DRIVE */
327 clrsetbits_le32(&denali_phy[937], 0x7 << 14, mode_sel << 14);
328 /* PHY_939 PHY_PAD_CS_DRIVE */
329 clrsetbits_le32(&denali_phy[939], 0x7 << 14, mode_sel << 14);
330
Jagan Teki5d152172019-07-16 17:27:15 +0530331 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
332 /* BOOSTP_EN & BOOSTN_EN */
333 reg_value = ((PHY_BOOSTP_EN << 4) | PHY_BOOSTN_EN);
334 /* PHY_925 PHY_PAD_FDBK_DRIVE2 */
335 clrsetbits_le32(&denali_phy[925], 0xff << 8, reg_value << 8);
336 /* PHY_926 PHY_PAD_DATA_DRIVE */
337 clrsetbits_le32(&denali_phy[926], 0xff << 12, reg_value << 12);
338 /* PHY_927 PHY_PAD_DQS_DRIVE */
339 clrsetbits_le32(&denali_phy[927], 0xff << 14, reg_value << 14);
340 /* PHY_928 PHY_PAD_ADDR_DRIVE */
341 clrsetbits_le32(&denali_phy[928], 0xff << 20, reg_value << 20);
342 /* PHY_929 PHY_PAD_CLK_DRIVE */
343 clrsetbits_le32(&denali_phy[929], 0xff << 22, reg_value << 22);
344 /* PHY_935 PHY_PAD_CKE_DRIVE */
345 clrsetbits_le32(&denali_phy[935], 0xff << 20, reg_value << 20);
346 /* PHY_937 PHY_PAD_RST_DRIVE */
347 clrsetbits_le32(&denali_phy[937], 0xff << 20, reg_value << 20);
348 /* PHY_939 PHY_PAD_CS_DRIVE */
349 clrsetbits_le32(&denali_phy[939], 0xff << 20, reg_value << 20);
Jagan Tekid8681842019-07-16 17:27:16 +0530350
351 /* SLEWP_EN & SLEWN_EN */
352 reg_value = ((PHY_SLEWP_EN << 3) | PHY_SLEWN_EN);
353 /* PHY_924 PHY_PAD_FDBK_DRIVE */
354 clrsetbits_le32(&denali_phy[924], 0x3f << 8, reg_value << 8);
355 /* PHY_926 PHY_PAD_DATA_DRIVE */
356 clrsetbits_le32(&denali_phy[926], 0x3f, reg_value);
357 /* PHY_927 PHY_PAD_DQS_DRIVE */
358 clrsetbits_le32(&denali_phy[927], 0x3f, reg_value);
359 /* PHY_928 PHY_PAD_ADDR_DRIVE */
360 clrsetbits_le32(&denali_phy[928], 0x3f << 8, reg_value << 8);
361 /* PHY_929 PHY_PAD_CLK_DRIVE */
362 clrsetbits_le32(&denali_phy[929], 0x3f << 8, reg_value << 8);
363 /* PHY_935 PHY_PAD_CKE_DRIVE */
364 clrsetbits_le32(&denali_phy[935], 0x3f << 8, reg_value << 8);
365 /* PHY_937 PHY_PAD_RST_DRIVE */
366 clrsetbits_le32(&denali_phy[937], 0x3f << 8, reg_value << 8);
367 /* PHY_939 PHY_PAD_CS_DRIVE */
368 clrsetbits_le32(&denali_phy[939], 0x3f << 8, reg_value << 8);
Jagan Teki5d152172019-07-16 17:27:15 +0530369 }
370
Jagan Tekib5d46632019-07-16 17:27:07 +0530371 /* speed setting */
372 if (params->base.ddr_freq < 400)
373 speed = 0x0;
374 else if (params->base.ddr_freq < 800)
375 speed = 0x1;
376 else if (params->base.ddr_freq < 1200)
377 speed = 0x2;
378 else
379 speed = 0x3;
380
381 /* PHY_924 PHY_PAD_FDBK_DRIVE */
382 clrsetbits_le32(&denali_phy[924], 0x3 << 21, speed << 21);
383 /* PHY_926 PHY_PAD_DATA_DRIVE */
384 clrsetbits_le32(&denali_phy[926], 0x3 << 9, speed << 9);
385 /* PHY_927 PHY_PAD_DQS_DRIVE */
386 clrsetbits_le32(&denali_phy[927], 0x3 << 9, speed << 9);
387 /* PHY_928 PHY_PAD_ADDR_DRIVE */
388 clrsetbits_le32(&denali_phy[928], 0x3 << 17, speed << 17);
389 /* PHY_929 PHY_PAD_CLK_DRIVE */
390 clrsetbits_le32(&denali_phy[929], 0x3 << 17, speed << 17);
391 /* PHY_935 PHY_PAD_CKE_DRIVE */
392 clrsetbits_le32(&denali_phy[935], 0x3 << 17, speed << 17);
393 /* PHY_937 PHY_PAD_RST_DRIVE */
394 clrsetbits_le32(&denali_phy[937], 0x3 << 17, speed << 17);
395 /* PHY_939 PHY_PAD_CS_DRIVE */
396 clrsetbits_le32(&denali_phy[939], 0x3 << 17, speed << 17);
397
Jagan Teki65535a22019-07-16 17:27:17 +0530398 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
399 /* RX_CM_INPUT */
400 reg_value = PHY_RX_CM_INPUT;
401 /* PHY_924 PHY_PAD_FDBK_DRIVE */
402 clrsetbits_le32(&denali_phy[924], 0x1 << 14, reg_value << 14);
403 /* PHY_926 PHY_PAD_DATA_DRIVE */
404 clrsetbits_le32(&denali_phy[926], 0x1 << 11, reg_value << 11);
405 /* PHY_927 PHY_PAD_DQS_DRIVE */
406 clrsetbits_le32(&denali_phy[927], 0x1 << 13, reg_value << 13);
407 /* PHY_928 PHY_PAD_ADDR_DRIVE */
408 clrsetbits_le32(&denali_phy[928], 0x1 << 19, reg_value << 19);
409 /* PHY_929 PHY_PAD_CLK_DRIVE */
410 clrsetbits_le32(&denali_phy[929], 0x1 << 21, reg_value << 21);
411 /* PHY_935 PHY_PAD_CKE_DRIVE */
412 clrsetbits_le32(&denali_phy[935], 0x1 << 19, reg_value << 19);
413 /* PHY_937 PHY_PAD_RST_DRIVE */
414 clrsetbits_le32(&denali_phy[937], 0x1 << 19, reg_value << 19);
415 /* PHY_939 PHY_PAD_CS_DRIVE */
416 clrsetbits_le32(&denali_phy[939], 0x1 << 19, reg_value << 19);
417 }
418
Jagan Tekib5d46632019-07-16 17:27:07 +0530419 return 0;
420}
421
Kever Yang50fb9982017-02-22 16:56:35 +0800422static void set_ds_odt(const struct chan_info *chan,
Jagan Tekia58ff792019-07-15 23:50:58 +0530423 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800424{
425 u32 *denali_phy = chan->publ->denali_phy;
426
427 u32 tsel_idle_en, tsel_wr_en, tsel_rd_en;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530428 u32 tsel_idle_select_p, tsel_rd_select_p;
429 u32 tsel_idle_select_n, tsel_rd_select_n;
430 u32 tsel_wr_select_dq_p, tsel_wr_select_ca_p;
431 u32 tsel_wr_select_dq_n, tsel_wr_select_ca_n;
Kever Yang50fb9982017-02-22 16:56:35 +0800432 u32 reg_value;
433
Jagan Tekia58ff792019-07-15 23:50:58 +0530434 if (params->base.dramtype == LPDDR4) {
Jagan Tekif676c7c2019-07-15 23:50:56 +0530435 tsel_rd_select_p = PHY_DRV_ODT_HI_Z;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530436 tsel_rd_select_n = PHY_DRV_ODT_240;
437
Jagan Tekif676c7c2019-07-15 23:50:56 +0530438 tsel_idle_select_p = PHY_DRV_ODT_HI_Z;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530439 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800440
Jagan Teki5c3251f2019-07-15 23:51:04 +0530441 tsel_wr_select_dq_p = PHY_DRV_ODT_40;
Jagan Teki36667142019-07-15 23:51:00 +0530442 tsel_wr_select_dq_n = PHY_DRV_ODT_40;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530443
444 tsel_wr_select_ca_p = PHY_DRV_ODT_40;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530445 tsel_wr_select_ca_n = PHY_DRV_ODT_40;
Jagan Tekia58ff792019-07-15 23:50:58 +0530446 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +0800447 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530448 tsel_rd_select_n = PHY_DRV_ODT_HI_Z;
449
Kever Yang50fb9982017-02-22 16:56:35 +0800450 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530451 tsel_idle_select_n = PHY_DRV_ODT_HI_Z;
Kever Yang50fb9982017-02-22 16:56:35 +0800452
Jagan Teki5c3251f2019-07-15 23:51:04 +0530453 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530454 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530455
456 tsel_wr_select_ca_p = PHY_DRV_ODT_48;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530457 tsel_wr_select_ca_n = PHY_DRV_ODT_48;
Kever Yang50fb9982017-02-22 16:56:35 +0800458 } else {
459 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530460 tsel_rd_select_n = PHY_DRV_ODT_240;
461
Kever Yang50fb9982017-02-22 16:56:35 +0800462 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530463 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800464
Jagan Teki5c3251f2019-07-15 23:51:04 +0530465 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530466 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530467
468 tsel_wr_select_ca_p = PHY_DRV_ODT_34_3;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530469 tsel_wr_select_ca_n = PHY_DRV_ODT_34_3;
Kever Yang50fb9982017-02-22 16:56:35 +0800470 }
471
Jagan Tekia58ff792019-07-15 23:50:58 +0530472 if (params->base.odt == 1)
Kever Yang50fb9982017-02-22 16:56:35 +0800473 tsel_rd_en = 1;
474 else
475 tsel_rd_en = 0;
476
477 tsel_wr_en = 0;
478 tsel_idle_en = 0;
479
480 /*
481 * phy_dq_tsel_select_X 24bits DENALI_PHY_6/134/262/390 offset_0
482 * sets termination values for read/idle cycles and drive strength
483 * for write cycles for DQ/DM
484 */
485 reg_value = tsel_rd_select_n | (tsel_rd_select_p << 0x4) |
Jagan Tekib3b34392019-07-15 23:51:01 +0530486 (tsel_wr_select_dq_n << 8) | (tsel_wr_select_dq_p << 12) |
Kever Yang50fb9982017-02-22 16:56:35 +0800487 (tsel_idle_select_n << 16) | (tsel_idle_select_p << 20);
488 clrsetbits_le32(&denali_phy[6], 0xffffff, reg_value);
489 clrsetbits_le32(&denali_phy[134], 0xffffff, reg_value);
490 clrsetbits_le32(&denali_phy[262], 0xffffff, reg_value);
491 clrsetbits_le32(&denali_phy[390], 0xffffff, reg_value);
492
493 /*
494 * phy_dqs_tsel_select_X 24bits DENALI_PHY_7/135/263/391 offset_0
495 * sets termination values for read/idle cycles and drive strength
496 * for write cycles for DQS
497 */
498 clrsetbits_le32(&denali_phy[7], 0xffffff, reg_value);
499 clrsetbits_le32(&denali_phy[135], 0xffffff, reg_value);
500 clrsetbits_le32(&denali_phy[263], 0xffffff, reg_value);
501 clrsetbits_le32(&denali_phy[391], 0xffffff, reg_value);
502
503 /* phy_adr_tsel_select_ 8bits DENALI_PHY_544/672/800 offset_0 */
Jagan Teki7caa3e92019-07-15 23:51:03 +0530504 reg_value = tsel_wr_select_ca_n | (tsel_wr_select_ca_p << 0x4);
Jagan Teki539ffed2019-07-16 17:27:19 +0530505 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
506 /* LPDDR4 these register read always return 0, so
507 * can not use clrsetbits_le32(), need to write32
508 */
509 writel((0x300 << 8) | reg_value, &denali_phy[544]);
510 writel((0x300 << 8) | reg_value, &denali_phy[672]);
511 writel((0x300 << 8) | reg_value, &denali_phy[800]);
512 } else {
513 clrsetbits_le32(&denali_phy[544], 0xff, reg_value);
514 clrsetbits_le32(&denali_phy[672], 0xff, reg_value);
515 clrsetbits_le32(&denali_phy[800], 0xff, reg_value);
516 }
Kever Yang50fb9982017-02-22 16:56:35 +0800517
518 /* phy_pad_addr_drive 8bits DENALI_PHY_928 offset_0 */
519 clrsetbits_le32(&denali_phy[928], 0xff, reg_value);
520
521 /* phy_pad_rst_drive 8bits DENALI_PHY_937 offset_0 */
522 clrsetbits_le32(&denali_phy[937], 0xff, reg_value);
523
524 /* phy_pad_cke_drive 8bits DENALI_PHY_935 offset_0 */
525 clrsetbits_le32(&denali_phy[935], 0xff, reg_value);
526
527 /* phy_pad_cs_drive 8bits DENALI_PHY_939 offset_0 */
528 clrsetbits_le32(&denali_phy[939], 0xff, reg_value);
529
530 /* phy_pad_clk_drive 8bits DENALI_PHY_929 offset_0 */
531 clrsetbits_le32(&denali_phy[929], 0xff, reg_value);
532
533 /* phy_pad_fdbk_drive 23bit DENALI_PHY_924/925 */
534 clrsetbits_le32(&denali_phy[924], 0xff,
Jagan Tekib3b34392019-07-15 23:51:01 +0530535 tsel_wr_select_dq_n | (tsel_wr_select_dq_p << 4));
Kever Yang50fb9982017-02-22 16:56:35 +0800536 clrsetbits_le32(&denali_phy[925], 0xff,
537 tsel_rd_select_n | (tsel_rd_select_p << 4));
538
539 /* phy_dq_tsel_enable_X 3bits DENALI_PHY_5/133/261/389 offset_16 */
540 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
541 << 16;
542 clrsetbits_le32(&denali_phy[5], 0x7 << 16, reg_value);
543 clrsetbits_le32(&denali_phy[133], 0x7 << 16, reg_value);
544 clrsetbits_le32(&denali_phy[261], 0x7 << 16, reg_value);
545 clrsetbits_le32(&denali_phy[389], 0x7 << 16, reg_value);
546
547 /* phy_dqs_tsel_enable_X 3bits DENALI_PHY_6/134/262/390 offset_24 */
548 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
549 << 24;
550 clrsetbits_le32(&denali_phy[6], 0x7 << 24, reg_value);
551 clrsetbits_le32(&denali_phy[134], 0x7 << 24, reg_value);
552 clrsetbits_le32(&denali_phy[262], 0x7 << 24, reg_value);
553 clrsetbits_le32(&denali_phy[390], 0x7 << 24, reg_value);
554
555 /* phy_adr_tsel_enable_ 1bit DENALI_PHY_518/646/774 offset_8 */
556 reg_value = tsel_wr_en << 8;
557 clrsetbits_le32(&denali_phy[518], 0x1 << 8, reg_value);
558 clrsetbits_le32(&denali_phy[646], 0x1 << 8, reg_value);
559 clrsetbits_le32(&denali_phy[774], 0x1 << 8, reg_value);
560
561 /* phy_pad_addr_term tsel 1bit DENALI_PHY_933 offset_17 */
562 reg_value = tsel_wr_en << 17;
563 clrsetbits_le32(&denali_phy[933], 0x1 << 17, reg_value);
564 /*
565 * pad_rst/cke/cs/clk_term tsel 1bits
566 * DENALI_PHY_938/936/940/934 offset_17
567 */
568 clrsetbits_le32(&denali_phy[938], 0x1 << 17, reg_value);
569 clrsetbits_le32(&denali_phy[936], 0x1 << 17, reg_value);
570 clrsetbits_le32(&denali_phy[940], 0x1 << 17, reg_value);
571 clrsetbits_le32(&denali_phy[934], 0x1 << 17, reg_value);
572
573 /* phy_pad_fdbk_term 1bit DENALI_PHY_930 offset_17 */
574 clrsetbits_le32(&denali_phy[930], 0x1 << 17, reg_value);
Jagan Tekib5d46632019-07-16 17:27:07 +0530575
576 phy_io_config(chan, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800577}
578
Jagan Tekic9151e22019-07-15 23:58:45 +0530579static void pctl_start(struct dram_info *dram, u8 channel)
580{
581 const struct chan_info *chan = &dram->chan[channel];
582 u32 *denali_ctl = chan->pctl->denali_ctl;
583 u32 *denali_phy = chan->publ->denali_phy;
584 u32 *ddrc0_con = get_ddrc0_con(dram, channel);
585 u32 count = 0;
586 u32 byte, tmp;
587
588 writel(0x01000000, &ddrc0_con);
589
590 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 0x2 << 24);
591
592 while (!(readl(&denali_ctl[203]) & (1 << 3))) {
593 if (count > 1000) {
594 printf("%s: Failed to init pctl for channel %d\n",
595 __func__, channel);
596 while (1)
597 ;
598 }
599
600 udelay(1);
601 count++;
602 }
603
604 writel(0x01000100, &ddrc0_con);
605
606 for (byte = 0; byte < 4; byte++) {
607 tmp = 0x820;
608 writel((tmp << 16) | tmp, &denali_phy[53 + (128 * byte)]);
609 writel((tmp << 16) | tmp, &denali_phy[54 + (128 * byte)]);
610 writel((tmp << 16) | tmp, &denali_phy[55 + (128 * byte)]);
611 writel((tmp << 16) | tmp, &denali_phy[56 + (128 * byte)]);
612 writel((tmp << 16) | tmp, &denali_phy[57 + (128 * byte)]);
613
614 clrsetbits_le32(&denali_phy[58 + (128 * byte)], 0xffff, tmp);
615 }
616
617 clrsetbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT,
618 dram->pwrup_srefresh_exit[channel]);
619}
620
Jagan Teki4ef5c012019-07-15 23:58:44 +0530621static int pctl_cfg(struct dram_info *dram, const struct chan_info *chan,
622 u32 channel, const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800623{
624 u32 *denali_ctl = chan->pctl->denali_ctl;
625 u32 *denali_pi = chan->pi->denali_pi;
626 u32 *denali_phy = chan->publ->denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +0530627 const u32 *params_ctl = params->pctl_regs.denali_ctl;
628 const u32 *params_phy = params->phy_regs.denali_phy;
Kever Yang50fb9982017-02-22 16:56:35 +0800629 u32 tmp, tmp1, tmp2;
Kever Yang50fb9982017-02-22 16:56:35 +0800630
631 /*
632 * work around controller bug:
633 * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed
634 */
635 copy_to_reg(&denali_ctl[1], &params_ctl[1],
636 sizeof(struct rk3399_ddr_pctl_regs) - 4);
637 writel(params_ctl[0], &denali_ctl[0]);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530638
Jagan Tekicc9da9a2019-07-16 17:27:13 +0530639 /*
640 * two channel init at the same time, then ZQ Cal Start
641 * at the same time, it will use the same RZQ, but cannot
642 * start at the same time.
643 *
644 * So, increase tINIT3 for channel 1, will avoid two
645 * channel ZQ Cal Start at the same time
646 */
647 if (params->base.dramtype == LPDDR4 && channel == 1) {
648 tmp = ((params->base.ddr_freq * MHz + 999) / 1000);
649 tmp1 = readl(&denali_ctl[14]);
650 writel(tmp + tmp1, &denali_ctl[14]);
651 }
652
Jagan Tekia58ff792019-07-15 23:50:58 +0530653 copy_to_reg(denali_pi, &params->pi_regs.denali_pi[0],
Kever Yang50fb9982017-02-22 16:56:35 +0800654 sizeof(struct rk3399_ddr_pi_regs));
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530655
Kever Yang50fb9982017-02-22 16:56:35 +0800656 /* rank count need to set for init */
Jagan Tekia58ff792019-07-15 23:50:58 +0530657 set_memory_map(chan, channel, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800658
Jagan Tekia58ff792019-07-15 23:50:58 +0530659 writel(params->phy_regs.denali_phy[910], &denali_phy[910]);
660 writel(params->phy_regs.denali_phy[911], &denali_phy[911]);
661 writel(params->phy_regs.denali_phy[912], &denali_phy[912]);
Kever Yang50fb9982017-02-22 16:56:35 +0800662
Jagan Tekib49b5dc2019-07-16 17:27:14 +0530663 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
664 writel(params->phy_regs.denali_phy[898], &denali_phy[898]);
665 writel(params->phy_regs.denali_phy[919], &denali_phy[919]);
666 }
667
Jagan Tekic9151e22019-07-15 23:58:45 +0530668 dram->pwrup_srefresh_exit[channel] = readl(&denali_ctl[68]) &
669 PWRUP_SREFRESH_EXIT;
Kever Yang50fb9982017-02-22 16:56:35 +0800670 clrbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT);
671
672 /* PHY_DLL_RST_EN */
673 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 1 << 24);
674
675 setbits_le32(&denali_pi[0], START);
676 setbits_le32(&denali_ctl[0], START);
677
Jagan Teki5e927182019-07-16 17:27:12 +0530678 /**
679 * LPDDR4 use PLL bypass mode for init
680 * not need to wait for the PLL to lock
681 */
682 if (params->base.dramtype != LPDDR4) {
683 /* Waiting for phy DLL lock */
684 while (1) {
685 tmp = readl(&denali_phy[920]);
686 tmp1 = readl(&denali_phy[921]);
687 tmp2 = readl(&denali_phy[922]);
688 if ((((tmp >> 16) & 0x1) == 0x1) &&
689 (((tmp1 >> 16) & 0x1) == 0x1) &&
690 (((tmp1 >> 0) & 0x1) == 0x1) &&
691 (((tmp2 >> 0) & 0x1) == 0x1))
692 break;
693 }
Kever Yang50fb9982017-02-22 16:56:35 +0800694 }
695
696 copy_to_reg(&denali_phy[896], &params_phy[896], (958 - 895) * 4);
697 copy_to_reg(&denali_phy[0], &params_phy[0], (90 - 0 + 1) * 4);
698 copy_to_reg(&denali_phy[128], &params_phy[128], (218 - 128 + 1) * 4);
699 copy_to_reg(&denali_phy[256], &params_phy[256], (346 - 256 + 1) * 4);
700 copy_to_reg(&denali_phy[384], &params_phy[384], (474 - 384 + 1) * 4);
701 copy_to_reg(&denali_phy[512], &params_phy[512], (549 - 512 + 1) * 4);
702 copy_to_reg(&denali_phy[640], &params_phy[640], (677 - 640 + 1) * 4);
703 copy_to_reg(&denali_phy[768], &params_phy[768], (805 - 768 + 1) * 4);
Jagan Tekia58ff792019-07-15 23:50:58 +0530704 set_ds_odt(chan, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800705
706 /*
707 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_84/212/340/468 offset_8
708 * dqs_tsel_wr_end[7:4] add Half cycle
709 */
710 tmp = (readl(&denali_phy[84]) >> 8) & 0xff;
711 clrsetbits_le32(&denali_phy[84], 0xff << 8, (tmp + 0x10) << 8);
712 tmp = (readl(&denali_phy[212]) >> 8) & 0xff;
713 clrsetbits_le32(&denali_phy[212], 0xff << 8, (tmp + 0x10) << 8);
714 tmp = (readl(&denali_phy[340]) >> 8) & 0xff;
715 clrsetbits_le32(&denali_phy[340], 0xff << 8, (tmp + 0x10) << 8);
716 tmp = (readl(&denali_phy[468]) >> 8) & 0xff;
717 clrsetbits_le32(&denali_phy[468], 0xff << 8, (tmp + 0x10) << 8);
718
719 /*
720 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_83/211/339/467 offset_8
721 * dq_tsel_wr_end[7:4] add Half cycle
722 */
723 tmp = (readl(&denali_phy[83]) >> 16) & 0xff;
724 clrsetbits_le32(&denali_phy[83], 0xff << 16, (tmp + 0x10) << 16);
725 tmp = (readl(&denali_phy[211]) >> 16) & 0xff;
726 clrsetbits_le32(&denali_phy[211], 0xff << 16, (tmp + 0x10) << 16);
727 tmp = (readl(&denali_phy[339]) >> 16) & 0xff;
728 clrsetbits_le32(&denali_phy[339], 0xff << 16, (tmp + 0x10) << 16);
729 tmp = (readl(&denali_phy[467]) >> 16) & 0xff;
730 clrsetbits_le32(&denali_phy[467], 0xff << 16, (tmp + 0x10) << 16);
731
Kever Yang50fb9982017-02-22 16:56:35 +0800732 return 0;
733}
734
735static void select_per_cs_training_index(const struct chan_info *chan,
736 u32 rank)
737{
738 u32 *denali_phy = chan->publ->denali_phy;
739
740 /* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */
Jagan Tekif676c7c2019-07-15 23:50:56 +0530741 if ((readl(&denali_phy[84]) >> 16) & 1) {
Kever Yang50fb9982017-02-22 16:56:35 +0800742 /*
743 * PHY_8/136/264/392
744 * phy_per_cs_training_index_X 1bit offset_24
745 */
746 clrsetbits_le32(&denali_phy[8], 0x1 << 24, rank << 24);
747 clrsetbits_le32(&denali_phy[136], 0x1 << 24, rank << 24);
748 clrsetbits_le32(&denali_phy[264], 0x1 << 24, rank << 24);
749 clrsetbits_le32(&denali_phy[392], 0x1 << 24, rank << 24);
750 }
751}
752
753static void override_write_leveling_value(const struct chan_info *chan)
754{
755 u32 *denali_ctl = chan->pctl->denali_ctl;
756 u32 *denali_phy = chan->publ->denali_phy;
757 u32 byte;
758
759 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
760 setbits_le32(&denali_phy[896], 1);
761
762 /*
763 * PHY_8/136/264/392
764 * phy_per_cs_training_multicast_en_X 1bit offset_16
765 */
766 clrsetbits_le32(&denali_phy[8], 0x1 << 16, 1 << 16);
767 clrsetbits_le32(&denali_phy[136], 0x1 << 16, 1 << 16);
768 clrsetbits_le32(&denali_phy[264], 0x1 << 16, 1 << 16);
769 clrsetbits_le32(&denali_phy[392], 0x1 << 16, 1 << 16);
770
771 for (byte = 0; byte < 4; byte++)
772 clrsetbits_le32(&denali_phy[63 + (128 * byte)], 0xffff << 16,
773 0x200 << 16);
774
775 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
776 clrbits_le32(&denali_phy[896], 1);
777
778 /* CTL_200 ctrlupd_req 1bit offset_8 */
779 clrsetbits_le32(&denali_ctl[200], 0x1 << 8, 0x1 << 8);
780}
781
782static int data_training_ca(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530783 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800784{
785 u32 *denali_pi = chan->pi->denali_pi;
786 u32 *denali_phy = chan->publ->denali_phy;
787 u32 i, tmp;
788 u32 obs_0, obs_1, obs_2, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +0530789 u32 rank = params->ch[channel].cap_info.rank;
Jagan Tekibafcc142019-07-15 23:58:41 +0530790 u32 rank_mask;
Kever Yang50fb9982017-02-22 16:56:35 +0800791
Jagan Tekia6079612019-07-15 23:58:40 +0530792 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
793 writel(0x00003f7c, (&denali_pi[175]));
794
Jagan Tekif05675e2019-07-16 17:27:09 +0530795 if (params->base.dramtype == LPDDR4)
796 rank_mask = (rank == 1) ? 0x5 : 0xf;
797 else
798 rank_mask = (rank == 1) ? 0x1 : 0x3;
Jagan Tekibafcc142019-07-15 23:58:41 +0530799
800 for (i = 0; i < 4; i++) {
801 if (!(rank_mask & (1 << i)))
802 continue;
803
Kever Yang50fb9982017-02-22 16:56:35 +0800804 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530805
Kever Yang50fb9982017-02-22 16:56:35 +0800806 /* PI_100 PI_CALVL_EN:RW:8:2 */
807 clrsetbits_le32(&denali_pi[100], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530808
Kever Yang50fb9982017-02-22 16:56:35 +0800809 /* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */
810 clrsetbits_le32(&denali_pi[92],
811 (0x1 << 16) | (0x3 << 24),
812 (0x1 << 16) | (i << 24));
813
814 /* Waiting for training complete */
815 while (1) {
816 /* PI_174 PI_INT_STATUS:RD:8:18 */
817 tmp = readl(&denali_pi[174]) >> 8;
818 /*
819 * check status obs
820 * PHY_532/660/789 phy_adr_calvl_obs1_:0:32
821 */
822 obs_0 = readl(&denali_phy[532]);
823 obs_1 = readl(&denali_phy[660]);
824 obs_2 = readl(&denali_phy[788]);
825 if (((obs_0 >> 30) & 0x3) ||
826 ((obs_1 >> 30) & 0x3) ||
827 ((obs_2 >> 30) & 0x3))
828 obs_err = 1;
829 if ((((tmp >> 11) & 0x1) == 0x1) &&
830 (((tmp >> 13) & 0x1) == 0x1) &&
831 (((tmp >> 5) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +0530832 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800833 break;
834 else if ((((tmp >> 5) & 0x1) == 0x1) ||
835 (obs_err == 1))
836 return -EIO;
837 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530838
Kever Yang50fb9982017-02-22 16:56:35 +0800839 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
840 writel(0x00003f7c, (&denali_pi[175]));
841 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530842
Kever Yang50fb9982017-02-22 16:56:35 +0800843 clrbits_le32(&denali_pi[100], 0x3 << 8);
844
845 return 0;
846}
847
848static int data_training_wl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530849 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800850{
851 u32 *denali_pi = chan->pi->denali_pi;
852 u32 *denali_phy = chan->publ->denali_phy;
853 u32 i, tmp;
854 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +0530855 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +0800856
Jagan Tekia6079612019-07-15 23:58:40 +0530857 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
858 writel(0x00003f7c, (&denali_pi[175]));
859
Kever Yang50fb9982017-02-22 16:56:35 +0800860 for (i = 0; i < rank; i++) {
861 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530862
Kever Yang50fb9982017-02-22 16:56:35 +0800863 /* PI_60 PI_WRLVL_EN:RW:8:2 */
864 clrsetbits_le32(&denali_pi[60], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530865
Kever Yang50fb9982017-02-22 16:56:35 +0800866 /* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */
867 clrsetbits_le32(&denali_pi[59],
868 (0x1 << 8) | (0x3 << 16),
869 (0x1 << 8) | (i << 16));
870
871 /* Waiting for training complete */
872 while (1) {
873 /* PI_174 PI_INT_STATUS:RD:8:18 */
874 tmp = readl(&denali_pi[174]) >> 8;
875
876 /*
877 * check status obs, if error maybe can not
878 * get leveling done PHY_40/168/296/424
879 * phy_wrlvl_status_obs_X:0:13
880 */
881 obs_0 = readl(&denali_phy[40]);
882 obs_1 = readl(&denali_phy[168]);
883 obs_2 = readl(&denali_phy[296]);
884 obs_3 = readl(&denali_phy[424]);
885 if (((obs_0 >> 12) & 0x1) ||
886 ((obs_1 >> 12) & 0x1) ||
887 ((obs_2 >> 12) & 0x1) ||
888 ((obs_3 >> 12) & 0x1))
889 obs_err = 1;
890 if ((((tmp >> 10) & 0x1) == 0x1) &&
891 (((tmp >> 13) & 0x1) == 0x1) &&
892 (((tmp >> 4) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +0530893 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800894 break;
895 else if ((((tmp >> 4) & 0x1) == 0x1) ||
896 (obs_err == 1))
897 return -EIO;
898 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530899
Kever Yang50fb9982017-02-22 16:56:35 +0800900 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
901 writel(0x00003f7c, (&denali_pi[175]));
902 }
903
904 override_write_leveling_value(chan);
905 clrbits_le32(&denali_pi[60], 0x3 << 8);
906
907 return 0;
908}
909
910static int data_training_rg(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530911 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800912{
913 u32 *denali_pi = chan->pi->denali_pi;
914 u32 *denali_phy = chan->publ->denali_phy;
915 u32 i, tmp;
916 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +0530917 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +0800918
Jagan Tekia6079612019-07-15 23:58:40 +0530919 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
920 writel(0x00003f7c, (&denali_pi[175]));
921
Kever Yang50fb9982017-02-22 16:56:35 +0800922 for (i = 0; i < rank; i++) {
923 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530924
Kever Yang50fb9982017-02-22 16:56:35 +0800925 /* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */
926 clrsetbits_le32(&denali_pi[80], 0x3 << 24, 0x2 << 24);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530927
Kever Yang50fb9982017-02-22 16:56:35 +0800928 /*
929 * PI_74 PI_RDLVL_GATE_REQ:WR:16:1
930 * PI_RDLVL_CS:RW:24:2
931 */
932 clrsetbits_le32(&denali_pi[74],
933 (0x1 << 16) | (0x3 << 24),
934 (0x1 << 16) | (i << 24));
935
936 /* Waiting for training complete */
937 while (1) {
938 /* PI_174 PI_INT_STATUS:RD:8:18 */
939 tmp = readl(&denali_pi[174]) >> 8;
940
941 /*
942 * check status obs
943 * PHY_43/171/299/427
944 * PHY_GTLVL_STATUS_OBS_x:16:8
945 */
946 obs_0 = readl(&denali_phy[43]);
947 obs_1 = readl(&denali_phy[171]);
948 obs_2 = readl(&denali_phy[299]);
949 obs_3 = readl(&denali_phy[427]);
950 if (((obs_0 >> (16 + 6)) & 0x3) ||
951 ((obs_1 >> (16 + 6)) & 0x3) ||
952 ((obs_2 >> (16 + 6)) & 0x3) ||
953 ((obs_3 >> (16 + 6)) & 0x3))
954 obs_err = 1;
955 if ((((tmp >> 9) & 0x1) == 0x1) &&
956 (((tmp >> 13) & 0x1) == 0x1) &&
957 (((tmp >> 3) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +0530958 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800959 break;
960 else if ((((tmp >> 3) & 0x1) == 0x1) ||
961 (obs_err == 1))
962 return -EIO;
963 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530964
Kever Yang50fb9982017-02-22 16:56:35 +0800965 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
966 writel(0x00003f7c, (&denali_pi[175]));
967 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530968
Kever Yang50fb9982017-02-22 16:56:35 +0800969 clrbits_le32(&denali_pi[80], 0x3 << 24);
970
971 return 0;
972}
973
974static int data_training_rl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530975 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800976{
977 u32 *denali_pi = chan->pi->denali_pi;
978 u32 i, tmp;
Jagan Teki97867c82019-07-15 23:51:05 +0530979 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +0800980
Jagan Tekia6079612019-07-15 23:58:40 +0530981 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
982 writel(0x00003f7c, (&denali_pi[175]));
983
Kever Yang50fb9982017-02-22 16:56:35 +0800984 for (i = 0; i < rank; i++) {
985 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530986
Kever Yang50fb9982017-02-22 16:56:35 +0800987 /* PI_80 PI_RDLVL_EN:RW:16:2 */
988 clrsetbits_le32(&denali_pi[80], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530989
Kever Yang50fb9982017-02-22 16:56:35 +0800990 /* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */
991 clrsetbits_le32(&denali_pi[74],
992 (0x1 << 8) | (0x3 << 24),
993 (0x1 << 8) | (i << 24));
994
995 /* Waiting for training complete */
996 while (1) {
997 /* PI_174 PI_INT_STATUS:RD:8:18 */
998 tmp = readl(&denali_pi[174]) >> 8;
999
1000 /*
1001 * make sure status obs not report error bit
1002 * PHY_46/174/302/430
1003 * phy_rdlvl_status_obs_X:16:8
1004 */
1005 if ((((tmp >> 8) & 0x1) == 0x1) &&
1006 (((tmp >> 13) & 0x1) == 0x1) &&
1007 (((tmp >> 2) & 0x1) == 0x0))
1008 break;
1009 else if (((tmp >> 2) & 0x1) == 0x1)
1010 return -EIO;
1011 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301012
Kever Yang50fb9982017-02-22 16:56:35 +08001013 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1014 writel(0x00003f7c, (&denali_pi[175]));
1015 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301016
Kever Yang50fb9982017-02-22 16:56:35 +08001017 clrbits_le32(&denali_pi[80], 0x3 << 16);
1018
1019 return 0;
1020}
1021
1022static int data_training_wdql(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301023 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001024{
1025 u32 *denali_pi = chan->pi->denali_pi;
1026 u32 i, tmp;
Jagan Teki97867c82019-07-15 23:51:05 +05301027 u32 rank = params->ch[channel].cap_info.rank;
Jagan Teki87723592019-07-15 23:58:42 +05301028 u32 rank_mask;
Kever Yang50fb9982017-02-22 16:56:35 +08001029
Jagan Tekia6079612019-07-15 23:58:40 +05301030 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1031 writel(0x00003f7c, (&denali_pi[175]));
1032
Jagan Tekid7504c02019-07-16 17:27:10 +05301033 if (params->base.dramtype == LPDDR4)
1034 rank_mask = (rank == 1) ? 0x5 : 0xf;
1035 else
1036 rank_mask = (rank == 1) ? 0x1 : 0x3;
Jagan Teki87723592019-07-15 23:58:42 +05301037
1038 for (i = 0; i < 4; i++) {
1039 if (!(rank_mask & (1 << i)))
1040 continue;
1041
Kever Yang50fb9982017-02-22 16:56:35 +08001042 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301043
Kever Yang50fb9982017-02-22 16:56:35 +08001044 /*
1045 * disable PI_WDQLVL_VREF_EN before wdq leveling?
1046 * PI_181 PI_WDQLVL_VREF_EN:RW:8:1
1047 */
1048 clrbits_le32(&denali_pi[181], 0x1 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301049
Kever Yang50fb9982017-02-22 16:56:35 +08001050 /* PI_124 PI_WDQLVL_EN:RW:16:2 */
1051 clrsetbits_le32(&denali_pi[124], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301052
Kever Yang50fb9982017-02-22 16:56:35 +08001053 /* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */
1054 clrsetbits_le32(&denali_pi[121],
1055 (0x1 << 8) | (0x3 << 16),
1056 (0x1 << 8) | (i << 16));
1057
1058 /* Waiting for training complete */
1059 while (1) {
1060 /* PI_174 PI_INT_STATUS:RD:8:18 */
1061 tmp = readl(&denali_pi[174]) >> 8;
1062 if ((((tmp >> 12) & 0x1) == 0x1) &&
1063 (((tmp >> 13) & 0x1) == 0x1) &&
1064 (((tmp >> 6) & 0x1) == 0x0))
1065 break;
1066 else if (((tmp >> 6) & 0x1) == 0x1)
1067 return -EIO;
1068 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301069
Kever Yang50fb9982017-02-22 16:56:35 +08001070 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1071 writel(0x00003f7c, (&denali_pi[175]));
1072 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301073
Kever Yang50fb9982017-02-22 16:56:35 +08001074 clrbits_le32(&denali_pi[124], 0x3 << 16);
1075
1076 return 0;
1077}
1078
1079static int data_training(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301080 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +08001081 u32 training_flag)
1082{
1083 u32 *denali_phy = chan->publ->denali_phy;
Jagan Teki6214ff22019-07-15 23:58:39 +05301084 int ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001085
1086 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1087 setbits_le32(&denali_phy[927], (1 << 22));
1088
1089 if (training_flag == PI_FULL_TRAINING) {
Jagan Tekia58ff792019-07-15 23:50:58 +05301090 if (params->base.dramtype == LPDDR4) {
Kever Yang50fb9982017-02-22 16:56:35 +08001091 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1092 PI_READ_GATE_TRAINING |
1093 PI_READ_LEVELING | PI_WDQ_LEVELING;
Jagan Tekia58ff792019-07-15 23:50:58 +05301094 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +08001095 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1096 PI_READ_GATE_TRAINING;
Jagan Tekia58ff792019-07-15 23:50:58 +05301097 } else if (params->base.dramtype == DDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +08001098 training_flag = PI_WRITE_LEVELING |
1099 PI_READ_GATE_TRAINING |
1100 PI_READ_LEVELING;
1101 }
1102 }
1103
1104 /* ca training(LPDDR4,LPDDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301105 if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING) {
1106 ret = data_training_ca(chan, channel, params);
1107 if (ret < 0) {
1108 debug("%s: data training ca failed\n", __func__);
1109 return ret;
1110 }
1111 }
Kever Yang50fb9982017-02-22 16:56:35 +08001112
1113 /* write leveling(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301114 if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING) {
1115 ret = data_training_wl(chan, channel, params);
1116 if (ret < 0) {
1117 debug("%s: data training wl failed\n", __func__);
1118 return ret;
1119 }
1120 }
Kever Yang50fb9982017-02-22 16:56:35 +08001121
1122 /* read gate training(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301123 if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING) {
1124 ret = data_training_rg(chan, channel, params);
1125 if (ret < 0) {
1126 debug("%s: data training rg failed\n", __func__);
1127 return ret;
1128 }
1129 }
Kever Yang50fb9982017-02-22 16:56:35 +08001130
1131 /* read leveling(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301132 if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING) {
1133 ret = data_training_rl(chan, channel, params);
1134 if (ret < 0) {
1135 debug("%s: data training rl failed\n", __func__);
1136 return ret;
1137 }
1138 }
Kever Yang50fb9982017-02-22 16:56:35 +08001139
1140 /* wdq leveling(LPDDR4 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301141 if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING) {
1142 ret = data_training_wdql(chan, channel, params);
1143 if (ret < 0) {
1144 debug("%s: data training wdql failed\n", __func__);
1145 return ret;
1146 }
1147 }
Kever Yang50fb9982017-02-22 16:56:35 +08001148
1149 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1150 clrbits_le32(&denali_phy[927], (1 << 22));
1151
1152 return 0;
1153}
1154
1155static void set_ddrconfig(const struct chan_info *chan,
Jagan Tekia58ff792019-07-15 23:50:58 +05301156 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +08001157 unsigned char channel, u32 ddrconfig)
1158{
1159 /* only need to set ddrconfig */
1160 struct rk3399_msch_regs *ddr_msch_regs = chan->msch;
1161 unsigned int cs0_cap = 0;
1162 unsigned int cs1_cap = 0;
1163
Jagan Teki97867c82019-07-15 23:51:05 +05301164 cs0_cap = (1 << (params->ch[channel].cap_info.cs0_row
1165 + params->ch[channel].cap_info.col
1166 + params->ch[channel].cap_info.bk
1167 + params->ch[channel].cap_info.bw - 20));
1168 if (params->ch[channel].cap_info.rank > 1)
1169 cs1_cap = cs0_cap >> (params->ch[channel].cap_info.cs0_row
1170 - params->ch[channel].cap_info.cs1_row);
1171 if (params->ch[channel].cap_info.row_3_4) {
Kever Yang50fb9982017-02-22 16:56:35 +08001172 cs0_cap = cs0_cap * 3 / 4;
1173 cs1_cap = cs1_cap * 3 / 4;
1174 }
1175
1176 writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf);
1177 writel(((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8),
1178 &ddr_msch_regs->ddrsize);
1179}
1180
1181static void dram_all_config(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +05301182 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001183{
Jagan Teki2d337122019-07-16 17:27:00 +05301184 u32 sys_reg2 = 0;
Jagan Teki9d8769c2019-07-16 17:27:01 +05301185 u32 sys_reg3 = 0;
Kever Yang50fb9982017-02-22 16:56:35 +08001186 unsigned int channel, idx;
1187
Jagan Teki2d337122019-07-16 17:27:00 +05301188 sys_reg2 |= SYS_REG_ENC_DDRTYPE(params->base.dramtype);
1189 sys_reg2 |= SYS_REG_ENC_NUM_CH(params->base.num_channels);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301190
Kever Yang50fb9982017-02-22 16:56:35 +08001191 for (channel = 0, idx = 0;
Jagan Tekia58ff792019-07-15 23:50:58 +05301192 (idx < params->base.num_channels) && (channel < 2);
Kever Yang50fb9982017-02-22 16:56:35 +08001193 channel++) {
Jagan Tekia58ff792019-07-15 23:50:58 +05301194 const struct rk3399_sdram_channel *info = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +08001195 struct rk3399_msch_regs *ddr_msch_regs;
1196 const struct rk3399_msch_timings *noc_timing;
1197
Jagan Teki97867c82019-07-15 23:51:05 +05301198 if (params->ch[channel].cap_info.col == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001199 continue;
1200 idx++;
Jagan Teki2d337122019-07-16 17:27:00 +05301201 sys_reg2 |= SYS_REG_ENC_ROW_3_4(info->cap_info.row_3_4, channel);
1202 sys_reg2 |= SYS_REG_ENC_CHINFO(channel);
1203 sys_reg2 |= SYS_REG_ENC_RANK(info->cap_info.rank, channel);
1204 sys_reg2 |= SYS_REG_ENC_COL(info->cap_info.col, channel);
1205 sys_reg2 |= SYS_REG_ENC_BK(info->cap_info.bk, channel);
Jagan Teki2d337122019-07-16 17:27:00 +05301206 sys_reg2 |= SYS_REG_ENC_BW(info->cap_info.bw, channel);
1207 sys_reg2 |= SYS_REG_ENC_DBW(info->cap_info.dbw, channel);
Jagan Teki9d8769c2019-07-16 17:27:01 +05301208 SYS_REG_ENC_CS0_ROW(info->cap_info.cs0_row, sys_reg2, sys_reg3, channel);
1209 if (info->cap_info.cs1_row)
1210 SYS_REG_ENC_CS1_ROW(info->cap_info.cs1_row, sys_reg2,
1211 sys_reg3, channel);
1212 sys_reg3 |= SYS_REG_ENC_CS1_COL(info->cap_info.col, channel);
Jagan Teki932dd962019-07-16 17:27:04 +05301213 sys_reg3 |= SYS_REG_ENC_VERSION(DDR_SYS_REG_VERSION);
Kever Yang50fb9982017-02-22 16:56:35 +08001214
1215 ddr_msch_regs = dram->chan[channel].msch;
Jagan Tekia58ff792019-07-15 23:50:58 +05301216 noc_timing = &params->ch[channel].noc_timings;
Kever Yang50fb9982017-02-22 16:56:35 +08001217 writel(noc_timing->ddrtiminga0,
1218 &ddr_msch_regs->ddrtiminga0);
1219 writel(noc_timing->ddrtimingb0,
1220 &ddr_msch_regs->ddrtimingb0);
Jagan Teki5465f9b2019-07-16 17:27:05 +05301221 writel(noc_timing->ddrtimingc0.d32,
Kever Yang50fb9982017-02-22 16:56:35 +08001222 &ddr_msch_regs->ddrtimingc0);
1223 writel(noc_timing->devtodev0,
1224 &ddr_msch_regs->devtodev0);
Jagan Teki264a09f2019-07-16 17:27:06 +05301225 writel(noc_timing->ddrmode.d32,
Kever Yang50fb9982017-02-22 16:56:35 +08001226 &ddr_msch_regs->ddrmode);
1227
1228 /* rank 1 memory clock disable (dfi_dram_clk_disable = 1) */
Jagan Teki97867c82019-07-15 23:51:05 +05301229 if (params->ch[channel].cap_info.rank == 1)
Kever Yang50fb9982017-02-22 16:56:35 +08001230 setbits_le32(&dram->chan[channel].pctl->denali_ctl[276],
1231 1 << 17);
1232 }
1233
Jagan Teki2d337122019-07-16 17:27:00 +05301234 writel(sys_reg2, &dram->pmugrf->os_reg2);
Jagan Teki9d8769c2019-07-16 17:27:01 +05301235 writel(sys_reg3, &dram->pmugrf->os_reg3);
Kever Yang50fb9982017-02-22 16:56:35 +08001236 rk_clrsetreg(&dram->pmusgrf->soc_con4, 0x1f << 10,
Jagan Tekia58ff792019-07-15 23:50:58 +05301237 params->base.stride << 10);
Kever Yang50fb9982017-02-22 16:56:35 +08001238
1239 /* reboot hold register set */
1240 writel(PRESET_SGRF_HOLD(0) | PRESET_GPIO0_HOLD(1) |
1241 PRESET_GPIO1_HOLD(1),
1242 &dram->pmucru->pmucru_rstnhold_con[1]);
1243 clrsetbits_le32(&dram->cru->glb_rst_con, 0x3, 0x3);
1244}
1245
1246static int switch_to_phy_index1(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +05301247 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001248{
1249 u32 channel;
1250 u32 *denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +05301251 u32 ch_count = params->base.num_channels;
Kever Yang50fb9982017-02-22 16:56:35 +08001252 int ret;
1253 int i = 0;
1254
1255 writel(RK_CLRSETBITS(0x03 << 4 | 1 << 2 | 1,
1256 1 << 4 | 1 << 2 | 1),
1257 &dram->cic->cic_ctrl0);
1258 while (!(readl(&dram->cic->cic_status0) & (1 << 2))) {
1259 mdelay(10);
1260 i++;
1261 if (i > 10) {
1262 debug("index1 frequency change overtime\n");
1263 return -ETIME;
1264 }
1265 }
1266
1267 i = 0;
1268 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &dram->cic->cic_ctrl0);
1269 while (!(readl(&dram->cic->cic_status0) & (1 << 0))) {
1270 mdelay(10);
Heinrich Schuchardt80516592018-03-18 12:10:55 +01001271 i++;
Kever Yang50fb9982017-02-22 16:56:35 +08001272 if (i > 10) {
1273 debug("index1 frequency done overtime\n");
1274 return -ETIME;
1275 }
1276 }
1277
1278 for (channel = 0; channel < ch_count; channel++) {
1279 denali_phy = dram->chan[channel].publ->denali_phy;
1280 clrsetbits_le32(&denali_phy[896], (0x3 << 8) | 1, 1 << 8);
1281 ret = data_training(&dram->chan[channel], channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301282 params, PI_FULL_TRAINING);
Jagan Teki6214ff22019-07-15 23:58:39 +05301283 if (ret < 0) {
Kever Yang50fb9982017-02-22 16:56:35 +08001284 debug("index1 training failed\n");
1285 return ret;
1286 }
1287 }
1288
1289 return 0;
1290}
1291
Jagan Teki2525fae2019-07-15 23:58:52 +05301292static unsigned char calculate_stride(struct rk3399_sdram_params *params)
1293{
1294 unsigned int stride = params->base.stride;
1295 unsigned int channel, chinfo = 0;
1296 unsigned int ch_cap[2] = {0, 0};
1297 u64 cap;
1298
1299 for (channel = 0; channel < 2; channel++) {
1300 unsigned int cs0_cap = 0;
1301 unsigned int cs1_cap = 0;
1302 struct sdram_cap_info *cap_info = &params->ch[channel].cap_info;
1303
1304 if (cap_info->col == 0)
1305 continue;
1306
1307 cs0_cap = (1 << (cap_info->cs0_row + cap_info->col +
1308 cap_info->bk + cap_info->bw - 20));
1309 if (cap_info->rank > 1)
1310 cs1_cap = cs0_cap >> (cap_info->cs0_row
1311 - cap_info->cs1_row);
1312 if (cap_info->row_3_4) {
1313 cs0_cap = cs0_cap * 3 / 4;
1314 cs1_cap = cs1_cap * 3 / 4;
1315 }
1316 ch_cap[channel] = cs0_cap + cs1_cap;
1317 chinfo |= 1 << channel;
1318 }
1319
Jagan Teki874dede2019-07-15 23:58:53 +05301320 /* stride calculation for 1 channel */
1321 if (params->base.num_channels == 1 && chinfo & 1)
1322 return 0x17; /* channel a */
1323
Jagan Teki2525fae2019-07-15 23:58:52 +05301324 /* stride calculation for 2 channels, default gstride type is 256B */
1325 if (ch_cap[0] == ch_cap[1]) {
1326 cap = ch_cap[0] + ch_cap[1];
1327 switch (cap) {
1328 /* 512MB */
1329 case 512:
1330 stride = 0;
1331 break;
1332 /* 1GB */
1333 case 1024:
1334 stride = 0x5;
1335 break;
1336 /*
1337 * 768MB + 768MB same as total 2GB memory
1338 * useful space: 0-768MB 1GB-1792MB
1339 */
1340 case 1536:
1341 /* 2GB */
1342 case 2048:
1343 stride = 0x9;
1344 break;
1345 /* 1536MB + 1536MB */
1346 case 3072:
1347 stride = 0x11;
1348 break;
1349 /* 4GB */
1350 case 4096:
1351 stride = 0xD;
1352 break;
1353 default:
1354 printf("%s: Unable to calculate stride for ", __func__);
1355 print_size((cap * (1 << 20)), " capacity\n");
1356 break;
1357 }
1358 }
1359
Jagan Teki8eed4a42019-07-15 23:58:55 +05301360 sdram_print_stride(stride);
1361
Jagan Teki2525fae2019-07-15 23:58:52 +05301362 return stride;
1363}
1364
Jagan Teki43485e12019-07-15 23:58:54 +05301365static void clear_channel_params(struct rk3399_sdram_params *params, u8 channel)
1366{
1367 params->ch[channel].cap_info.rank = 0;
1368 params->ch[channel].cap_info.col = 0;
1369 params->ch[channel].cap_info.bk = 0;
1370 params->ch[channel].cap_info.bw = 32;
1371 params->ch[channel].cap_info.dbw = 32;
1372 params->ch[channel].cap_info.row_3_4 = 0;
1373 params->ch[channel].cap_info.cs0_row = 0;
1374 params->ch[channel].cap_info.cs1_row = 0;
1375 params->ch[channel].cap_info.ddrconfig = 0;
1376}
1377
1378static int pctl_init(struct dram_info *dram, struct rk3399_sdram_params *params)
1379{
1380 int channel;
1381 int ret;
1382
1383 for (channel = 0; channel < 2; channel++) {
1384 const struct chan_info *chan = &dram->chan[channel];
1385 struct rk3399_cru *cru = dram->cru;
1386 struct rk3399_ddr_publ_regs *publ = chan->publ;
1387
1388 phy_pctrl_reset(cru, channel);
1389 phy_dll_bypass_set(publ, params->base.ddr_freq);
1390
1391 ret = pctl_cfg(dram, chan, channel, params);
1392 if (ret < 0) {
1393 printf("%s: pctl config failed\n", __func__);
1394 return ret;
1395 }
1396
1397 /* start to trigger initialization */
1398 pctl_start(dram, channel);
1399 }
1400
1401 return 0;
1402}
1403
Kever Yang50fb9982017-02-22 16:56:35 +08001404static int sdram_init(struct dram_info *dram,
Jagan Teki2525fae2019-07-15 23:58:52 +05301405 struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001406{
Jagan Tekia58ff792019-07-15 23:50:58 +05301407 unsigned char dramtype = params->base.dramtype;
1408 unsigned int ddr_freq = params->base.ddr_freq;
Jagan Teki43485e12019-07-15 23:58:54 +05301409 u32 training_flag = PI_READ_GATE_TRAINING;
1410 int channel, ch, rank;
Jagan Teki2ef77ed2019-07-15 23:50:59 +05301411 int ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001412
1413 debug("Starting SDRAM initialization...\n");
1414
Philipp Tomsich39dce4a2017-05-31 18:16:35 +02001415 if ((dramtype == DDR3 && ddr_freq > 933) ||
Kever Yang50fb9982017-02-22 16:56:35 +08001416 (dramtype == LPDDR3 && ddr_freq > 933) ||
1417 (dramtype == LPDDR4 && ddr_freq > 800)) {
1418 debug("SDRAM frequency is to high!");
1419 return -E2BIG;
1420 }
1421
Jagan Teki43485e12019-07-15 23:58:54 +05301422 for (ch = 0; ch < 2; ch++) {
1423 params->ch[ch].cap_info.rank = 2;
1424 for (rank = 2; rank != 0; rank--) {
1425 ret = pctl_init(dram, params);
1426 if (ret < 0) {
1427 printf("%s: pctl init failed\n", __func__);
1428 return ret;
1429 }
1430
1431 /* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */
1432 if (dramtype == LPDDR3)
1433 udelay(10);
1434
1435 params->ch[ch].cap_info.rank = rank;
1436
1437 /*
1438 * LPDDR3 CA training msut be trigger before
1439 * other training.
1440 * DDR3 is not have CA training.
1441 */
1442 if (params->base.dramtype == LPDDR3)
1443 training_flag |= PI_CA_TRAINING;
1444
1445 if (!(data_training(&dram->chan[ch], ch,
1446 params, training_flag)))
1447 break;
1448 }
1449 /* Computed rank with associated channel number */
1450 params->ch[ch].cap_info.rank = rank;
1451 }
1452
1453 params->base.num_channels = 0;
Kever Yang50fb9982017-02-22 16:56:35 +08001454 for (channel = 0; channel < 2; channel++) {
1455 const struct chan_info *chan = &dram->chan[channel];
Jagan Teki43485e12019-07-15 23:58:54 +05301456 struct sdram_cap_info *cap_info = &params->ch[channel].cap_info;
1457 u8 training_flag = PI_FULL_TRAINING;
Kever Yang50fb9982017-02-22 16:56:35 +08001458
Jagan Teki43485e12019-07-15 23:58:54 +05301459 if (cap_info->rank == 0) {
1460 clear_channel_params(params, channel);
Kever Yang50fb9982017-02-22 16:56:35 +08001461 continue;
Jagan Teki43485e12019-07-15 23:58:54 +05301462 } else {
1463 params->base.num_channels++;
Kever Yang50fb9982017-02-22 16:56:35 +08001464 }
1465
Jagan Teki43485e12019-07-15 23:58:54 +05301466 debug("Channel ");
1467 debug(channel ? "1: " : "0: ");
Jagan Tekic9151e22019-07-15 23:58:45 +05301468
Jagan Teki43485e12019-07-15 23:58:54 +05301469 /* LPDDR3 should have write and read gate training */
1470 if (params->base.dramtype == LPDDR3)
1471 training_flag = PI_WRITE_LEVELING |
1472 PI_READ_GATE_TRAINING;
Kever Yang50fb9982017-02-22 16:56:35 +08001473
Jagan Teki43485e12019-07-15 23:58:54 +05301474 if (params->base.dramtype != LPDDR4) {
1475 ret = data_training(dram, channel, params,
1476 training_flag);
1477 if (!ret) {
1478 debug("%s: data train failed for channel %d\n",
1479 __func__, ret);
1480 continue;
1481 }
Kever Yang50fb9982017-02-22 16:56:35 +08001482 }
1483
Jagan Teki8eed4a42019-07-15 23:58:55 +05301484 sdram_print_ddr_info(cap_info, &params->base);
1485
Jagan Teki43485e12019-07-15 23:58:54 +05301486 set_ddrconfig(chan, params, channel, cap_info->ddrconfig);
1487 }
1488
1489 if (params->base.num_channels == 0) {
1490 printf("%s: ", __func__);
Jagan Teki8eed4a42019-07-15 23:58:55 +05301491 sdram_print_dram_type(params->base.dramtype);
Jagan Teki43485e12019-07-15 23:58:54 +05301492 printf(" - %dMHz failed!\n", params->base.ddr_freq);
1493 return -EINVAL;
Kever Yang50fb9982017-02-22 16:56:35 +08001494 }
Jagan Teki2525fae2019-07-15 23:58:52 +05301495
1496 params->base.stride = calculate_stride(params);
Jagan Tekia58ff792019-07-15 23:50:58 +05301497 dram_all_config(dram, params);
1498 switch_to_phy_index1(dram, params);
Kever Yang50fb9982017-02-22 16:56:35 +08001499
1500 debug("Finish SDRAM initialization...\n");
1501 return 0;
1502}
1503
1504static int rk3399_dmc_ofdata_to_platdata(struct udevice *dev)
1505{
1506#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1507 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
Kever Yang50fb9982017-02-22 16:56:35 +08001508 int ret;
1509
Philipp Tomsich0250c232017-06-07 18:46:03 +02001510 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
1511 (u32 *)&plat->sdram_params,
1512 sizeof(plat->sdram_params) / sizeof(u32));
Kever Yang50fb9982017-02-22 16:56:35 +08001513 if (ret) {
1514 printf("%s: Cannot read rockchip,sdram-params %d\n",
1515 __func__, ret);
1516 return ret;
1517 }
Masahiro Yamadae4873e32018-04-19 12:14:03 +09001518 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001519 if (ret)
1520 printf("%s: regmap failed %d\n", __func__, ret);
1521
1522#endif
1523 return 0;
1524}
1525
1526#if CONFIG_IS_ENABLED(OF_PLATDATA)
1527static int conv_of_platdata(struct udevice *dev)
1528{
1529 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1530 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1531 int ret;
1532
1533 ret = regmap_init_mem_platdata(dev, dtplat->reg,
Jagan Tekif676c7c2019-07-15 23:50:56 +05301534 ARRAY_SIZE(dtplat->reg) / 2,
1535 &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001536 if (ret)
1537 return ret;
1538
1539 return 0;
1540}
1541#endif
1542
1543static int rk3399_dmc_init(struct udevice *dev)
1544{
1545 struct dram_info *priv = dev_get_priv(dev);
1546 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1547 int ret;
1548#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1549 struct rk3399_sdram_params *params = &plat->sdram_params;
1550#else
1551 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1552 struct rk3399_sdram_params *params =
1553 (void *)dtplat->rockchip_sdram_params;
1554
1555 ret = conv_of_platdata(dev);
1556 if (ret)
1557 return ret;
1558#endif
1559
1560 priv->cic = syscon_get_first_range(ROCKCHIP_SYSCON_CIC);
Jagan Tekic9151e22019-07-15 23:58:45 +05301561 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Kever Yang50fb9982017-02-22 16:56:35 +08001562 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
1563 priv->pmusgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
1564 priv->pmucru = rockchip_get_pmucru();
1565 priv->cru = rockchip_get_cru();
1566 priv->chan[0].pctl = regmap_get_range(plat->map, 0);
1567 priv->chan[0].pi = regmap_get_range(plat->map, 1);
1568 priv->chan[0].publ = regmap_get_range(plat->map, 2);
1569 priv->chan[0].msch = regmap_get_range(plat->map, 3);
1570 priv->chan[1].pctl = regmap_get_range(plat->map, 4);
1571 priv->chan[1].pi = regmap_get_range(plat->map, 5);
1572 priv->chan[1].publ = regmap_get_range(plat->map, 6);
1573 priv->chan[1].msch = regmap_get_range(plat->map, 7);
1574
1575 debug("con reg %p %p %p %p %p %p %p %p\n",
1576 priv->chan[0].pctl, priv->chan[0].pi,
1577 priv->chan[0].publ, priv->chan[0].msch,
1578 priv->chan[1].pctl, priv->chan[1].pi,
1579 priv->chan[1].publ, priv->chan[1].msch);
1580 debug("cru %p, cic %p, grf %p, sgrf %p, pmucru %p\n", priv->cru,
1581 priv->cic, priv->pmugrf, priv->pmusgrf, priv->pmucru);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301582
Kever Yang50fb9982017-02-22 16:56:35 +08001583#if CONFIG_IS_ENABLED(OF_PLATDATA)
1584 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->ddr_clk);
1585#else
1586 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
1587#endif
1588 if (ret) {
1589 printf("%s clk get failed %d\n", __func__, ret);
1590 return ret;
1591 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301592
Kever Yang50fb9982017-02-22 16:56:35 +08001593 ret = clk_set_rate(&priv->ddr_clk, params->base.ddr_freq * MHz);
1594 if (ret < 0) {
1595 printf("%s clk set failed %d\n", __func__, ret);
1596 return ret;
1597 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301598
Kever Yang50fb9982017-02-22 16:56:35 +08001599 ret = sdram_init(priv, params);
1600 if (ret < 0) {
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301601 printf("%s DRAM init failed %d\n", __func__, ret);
Kever Yang50fb9982017-02-22 16:56:35 +08001602 return ret;
1603 }
1604
1605 return 0;
1606}
1607#endif
1608
Kever Yang50fb9982017-02-22 16:56:35 +08001609static int rk3399_dmc_probe(struct udevice *dev)
1610{
Kever Yang7f347842019-04-01 17:20:53 +08001611#if defined(CONFIG_TPL_BUILD) || \
1612 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001613 if (rk3399_dmc_init(dev))
1614 return 0;
1615#else
1616 struct dram_info *priv = dev_get_priv(dev);
1617
1618 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301619 debug("%s: pmugrf = %p\n", __func__, priv->pmugrf);
Kever Yang6c15a542017-06-23 16:11:06 +08001620 priv->info.base = CONFIG_SYS_SDRAM_BASE;
Jagan Tekif676c7c2019-07-15 23:50:56 +05301621 priv->info.size =
1622 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg2);
Kever Yang50fb9982017-02-22 16:56:35 +08001623#endif
1624 return 0;
1625}
1626
1627static int rk3399_dmc_get_info(struct udevice *dev, struct ram_info *info)
1628{
1629 struct dram_info *priv = dev_get_priv(dev);
1630
Kever Yangea61d142017-04-19 16:01:14 +08001631 *info = priv->info;
Kever Yang50fb9982017-02-22 16:56:35 +08001632
1633 return 0;
1634}
1635
1636static struct ram_ops rk3399_dmc_ops = {
1637 .get_info = rk3399_dmc_get_info,
1638};
1639
Kever Yang50fb9982017-02-22 16:56:35 +08001640static const struct udevice_id rk3399_dmc_ids[] = {
1641 { .compatible = "rockchip,rk3399-dmc" },
1642 { }
1643};
1644
1645U_BOOT_DRIVER(dmc_rk3399) = {
1646 .name = "rockchip_rk3399_dmc",
1647 .id = UCLASS_RAM,
1648 .of_match = rk3399_dmc_ids,
1649 .ops = &rk3399_dmc_ops,
Kever Yang7f347842019-04-01 17:20:53 +08001650#if defined(CONFIG_TPL_BUILD) || \
1651 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001652 .ofdata_to_platdata = rk3399_dmc_ofdata_to_platdata,
1653#endif
1654 .probe = rk3399_dmc_probe,
Kever Yang50fb9982017-02-22 16:56:35 +08001655 .priv_auto_alloc_size = sizeof(struct dram_info),
Kever Yang7f347842019-04-01 17:20:53 +08001656#if defined(CONFIG_TPL_BUILD) || \
1657 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001658 .platdata_auto_alloc_size = sizeof(struct rockchip_dmc_plat),
1659#endif
1660};