blob: fe79f31cdb374a041c50f2c6a310a76487ba8538 [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 Teki0cb31122019-07-16 17:27:24 +053043#define CS0_MR22_VAL 0
44#define CS1_MR22_VAL 3
Jagan Teki5d152172019-07-16 17:27:15 +053045
Jagan Tekice75cfb2019-07-15 23:58:43 +053046#define CRU_SFTRST_DDR_CTRL(ch, n) ((0x1 << (8 + 16 + (ch) * 4)) | \
47 ((n) << (8 + (ch) * 4)))
48#define CRU_SFTRST_DDR_PHY(ch, n) ((0x1 << (9 + 16 + (ch) * 4)) | \
49 ((n) << (9 + (ch) * 4)))
Kever Yang50fb9982017-02-22 16:56:35 +080050struct chan_info {
51 struct rk3399_ddr_pctl_regs *pctl;
52 struct rk3399_ddr_pi_regs *pi;
53 struct rk3399_ddr_publ_regs *publ;
54 struct rk3399_msch_regs *msch;
55};
56
57struct dram_info {
Kever Yang7f347842019-04-01 17:20:53 +080058#if defined(CONFIG_TPL_BUILD) || \
59 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Jagan Tekic9151e22019-07-15 23:58:45 +053060 u32 pwrup_srefresh_exit[2];
Kever Yang50fb9982017-02-22 16:56:35 +080061 struct chan_info chan[2];
62 struct clk ddr_clk;
63 struct rk3399_cru *cru;
Jagan Tekic9151e22019-07-15 23:58:45 +053064 struct rk3399_grf_regs *grf;
Kever Yang50fb9982017-02-22 16:56:35 +080065 struct rk3399_pmucru *pmucru;
66 struct rk3399_pmusgrf_regs *pmusgrf;
67 struct rk3399_ddr_cic_regs *cic;
Jagan Teki9eb935a2019-07-16 17:27:30 +053068 const struct sdram_rk3399_ops *ops;
Kever Yang50fb9982017-02-22 16:56:35 +080069#endif
70 struct ram_info info;
71 struct rk3399_pmugrf_regs *pmugrf;
72};
73
Jagan Teki9eb935a2019-07-16 17:27:30 +053074struct sdram_rk3399_ops {
75 int (*data_training)(struct dram_info *dram, u32 channel, u8 rank,
76 struct rk3399_sdram_params *sdram);
77};
78
Kever Yang7f347842019-04-01 17:20:53 +080079#if defined(CONFIG_TPL_BUILD) || \
80 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +080081
82struct rockchip_dmc_plat {
83#if CONFIG_IS_ENABLED(OF_PLATDATA)
84 struct dtd_rockchip_rk3399_dmc dtplat;
85#else
86 struct rk3399_sdram_params sdram_params;
87#endif
88 struct regmap *map;
89};
90
Jagan Tekie3619d12019-07-16 17:27:21 +053091struct io_setting {
92 u32 mhz;
93 u32 mr5;
94 /* dram side */
95 u32 dq_odt;
96 u32 ca_odt;
97 u32 pdds;
98 u32 dq_vref;
99 u32 ca_vref;
100 /* phy side */
101 u32 rd_odt;
102 u32 wr_dq_drv;
103 u32 wr_ca_drv;
104 u32 wr_ckcs_drv;
105 u32 rd_odt_en;
106 u32 rd_vref;
107} lpddr4_io_setting[] = {
108 {
109 50 * MHz,
110 0,
111 /* dram side */
112 0, /* dq_odt; */
113 0, /* ca_odt; */
114 6, /* pdds; */
115 0x72, /* dq_vref; */
116 0x72, /* ca_vref; */
117 /* phy side */
118 PHY_DRV_ODT_HI_Z, /* rd_odt; */
119 PHY_DRV_ODT_40, /* wr_dq_drv; */
120 PHY_DRV_ODT_40, /* wr_ca_drv; */
121 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
122 0, /* rd_odt_en;*/
123 41, /* rd_vref; (unit %, range 3.3% - 48.7%) */
124 },
125 {
126 600 * MHz,
127 0,
128 /* dram side */
129 1, /* dq_odt; */
130 0, /* ca_odt; */
131 6, /* pdds; */
132 0x72, /* dq_vref; */
133 0x72, /* ca_vref; */
134 /* phy side */
135 PHY_DRV_ODT_HI_Z, /* rd_odt; */
136 PHY_DRV_ODT_48, /* wr_dq_drv; */
137 PHY_DRV_ODT_40, /* wr_ca_drv; */
138 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
139 0, /* rd_odt_en; */
140 32, /* rd_vref; (unit %, range 3.3% - 48.7%) */
141 },
142 {
143 800 * MHz,
144 0,
145 /* dram side */
146 1, /* dq_odt; */
147 0, /* ca_odt; */
148 1, /* pdds; */
149 0x72, /* dq_vref; */
150 0x72, /* ca_vref; */
151 /* phy side */
152 PHY_DRV_ODT_40, /* rd_odt; */
153 PHY_DRV_ODT_48, /* wr_dq_drv; */
154 PHY_DRV_ODT_40, /* wr_ca_drv; */
155 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
156 1, /* rd_odt_en; */
157 17, /* rd_vref; (unit %, range 3.3% - 48.7%) */
158 },
159 {
160 933 * MHz,
161 0,
162 /* dram side */
163 3, /* dq_odt; */
164 0, /* ca_odt; */
165 6, /* pdds; */
166 0x59, /* dq_vref; 32% */
167 0x72, /* ca_vref; */
168 /* phy side */
169 PHY_DRV_ODT_HI_Z, /* rd_odt; */
170 PHY_DRV_ODT_48, /* wr_dq_drv; */
171 PHY_DRV_ODT_40, /* wr_ca_drv; */
172 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
173 0, /* rd_odt_en; */
174 32, /* rd_vref; (unit %, range 3.3% - 48.7%) */
175 },
176 {
177 1066 * MHz,
178 0,
179 /* dram side */
180 6, /* dq_odt; */
181 0, /* ca_odt; */
182 1, /* pdds; */
183 0x10, /* dq_vref; */
184 0x72, /* ca_vref; */
185 /* phy side */
186 PHY_DRV_ODT_40, /* rd_odt; */
187 PHY_DRV_ODT_60, /* wr_dq_drv; */
188 PHY_DRV_ODT_40, /* wr_ca_drv; */
189 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
190 1, /* rd_odt_en; */
191 17, /* rd_vref; (unit %, range 3.3% - 48.7%) */
192 },
193};
194
Jagan Tekid33056b2019-07-16 17:27:22 +0530195/**
196 * phy = 0, PHY boot freq
197 * phy = 1, PHY index 0
198 * phy = 2, PHY index 1
199 */
200static struct io_setting *
201lpddr4_get_io_settings(const struct rk3399_sdram_params *params, u32 mr5)
202{
203 struct io_setting *io = NULL;
204 u32 n;
205
206 for (n = 0; n < ARRAY_SIZE(lpddr4_io_setting); n++) {
207 io = &lpddr4_io_setting[n];
208
209 if (io->mr5 != 0) {
210 if (io->mhz >= params->base.ddr_freq &&
211 io->mr5 == mr5)
212 break;
213 } else {
214 if (io->mhz >= params->base.ddr_freq)
215 break;
216 }
217 }
218
219 return io;
220}
221
Jagan Tekic9151e22019-07-15 23:58:45 +0530222static void *get_ddrc0_con(struct dram_info *dram, u8 channel)
223{
224 return (channel == 0) ? &dram->grf->ddrc0_con0 : &dram->grf->ddrc0_con1;
225}
226
Kever Yang50fb9982017-02-22 16:56:35 +0800227static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
228{
229 int i;
230
231 for (i = 0; i < n / sizeof(u32); i++) {
232 writel(*src, dest);
233 src++;
234 dest++;
235 }
236}
237
Jagan Tekice75cfb2019-07-15 23:58:43 +0530238static void rkclk_ddr_reset(struct rk3399_cru *cru, u32 channel, u32 ctl,
239 u32 phy)
240{
241 channel &= 0x1;
242 ctl &= 0x1;
243 phy &= 0x1;
244 writel(CRU_SFTRST_DDR_CTRL(channel, ctl) |
245 CRU_SFTRST_DDR_PHY(channel, phy),
246 &cru->softrst_con[4]);
247}
248
249static void phy_pctrl_reset(struct rk3399_cru *cru, u32 channel)
250{
251 rkclk_ddr_reset(cru, channel, 1, 1);
252 udelay(10);
253
254 rkclk_ddr_reset(cru, channel, 1, 0);
255 udelay(10);
256
257 rkclk_ddr_reset(cru, channel, 0, 0);
258 udelay(10);
259}
260
Kever Yang50fb9982017-02-22 16:56:35 +0800261static void phy_dll_bypass_set(struct rk3399_ddr_publ_regs *ddr_publ_regs,
262 u32 freq)
263{
264 u32 *denali_phy = ddr_publ_regs->denali_phy;
265
266 /* From IP spec, only freq small than 125 can enter dll bypass mode */
267 if (freq <= 125) {
268 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
269 setbits_le32(&denali_phy[86], (0x3 << 2) << 8);
270 setbits_le32(&denali_phy[214], (0x3 << 2) << 8);
271 setbits_le32(&denali_phy[342], (0x3 << 2) << 8);
272 setbits_le32(&denali_phy[470], (0x3 << 2) << 8);
273
274 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
275 setbits_le32(&denali_phy[547], (0x3 << 2) << 16);
276 setbits_le32(&denali_phy[675], (0x3 << 2) << 16);
277 setbits_le32(&denali_phy[803], (0x3 << 2) << 16);
278 } else {
279 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
280 clrbits_le32(&denali_phy[86], (0x3 << 2) << 8);
281 clrbits_le32(&denali_phy[214], (0x3 << 2) << 8);
282 clrbits_le32(&denali_phy[342], (0x3 << 2) << 8);
283 clrbits_le32(&denali_phy[470], (0x3 << 2) << 8);
284
285 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
286 clrbits_le32(&denali_phy[547], (0x3 << 2) << 16);
287 clrbits_le32(&denali_phy[675], (0x3 << 2) << 16);
288 clrbits_le32(&denali_phy[803], (0x3 << 2) << 16);
289 }
290}
291
292static void set_memory_map(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530293 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800294{
Jagan Tekia58ff792019-07-15 23:50:58 +0530295 const struct rk3399_sdram_channel *sdram_ch = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +0800296 u32 *denali_ctl = chan->pctl->denali_ctl;
297 u32 *denali_pi = chan->pi->denali_pi;
298 u32 cs_map;
299 u32 reduc;
300 u32 row;
301
302 /* Get row number from ddrconfig setting */
Jagan Teki97867c82019-07-15 23:51:05 +0530303 if (sdram_ch->cap_info.ddrconfig < 2 ||
304 sdram_ch->cap_info.ddrconfig == 4)
Kever Yang50fb9982017-02-22 16:56:35 +0800305 row = 16;
Jagan Teki97867c82019-07-15 23:51:05 +0530306 else if (sdram_ch->cap_info.ddrconfig == 3)
Kever Yang50fb9982017-02-22 16:56:35 +0800307 row = 14;
308 else
309 row = 15;
310
Jagan Teki97867c82019-07-15 23:51:05 +0530311 cs_map = (sdram_ch->cap_info.rank > 1) ? 3 : 1;
312 reduc = (sdram_ch->cap_info.bw == 2) ? 0 : 1;
Kever Yang50fb9982017-02-22 16:56:35 +0800313
314 /* Set the dram configuration to ctrl */
Jagan Teki97867c82019-07-15 23:51:05 +0530315 clrsetbits_le32(&denali_ctl[191], 0xF, (12 - sdram_ch->cap_info.col));
Kever Yang50fb9982017-02-22 16:56:35 +0800316 clrsetbits_le32(&denali_ctl[190], (0x3 << 16) | (0x7 << 24),
Jagan Teki97867c82019-07-15 23:51:05 +0530317 ((3 - sdram_ch->cap_info.bk) << 16) |
Kever Yang50fb9982017-02-22 16:56:35 +0800318 ((16 - row) << 24));
319
320 clrsetbits_le32(&denali_ctl[196], 0x3 | (1 << 16),
321 cs_map | (reduc << 16));
322
323 /* PI_199 PI_COL_DIFF:RW:0:4 */
Jagan Teki97867c82019-07-15 23:51:05 +0530324 clrsetbits_le32(&denali_pi[199], 0xF, (12 - sdram_ch->cap_info.col));
Kever Yang50fb9982017-02-22 16:56:35 +0800325
326 /* PI_155 PI_ROW_DIFF:RW:24:3 PI_BANK_DIFF:RW:16:2 */
327 clrsetbits_le32(&denali_pi[155], (0x3 << 16) | (0x7 << 24),
Jagan Teki97867c82019-07-15 23:51:05 +0530328 ((3 - sdram_ch->cap_info.bk) << 16) |
Kever Yang50fb9982017-02-22 16:56:35 +0800329 ((16 - row) << 24));
Jagan Teki9337cb32019-07-16 17:27:18 +0530330
331 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
332 if (cs_map == 1)
333 cs_map = 0x5;
334 else if (cs_map == 2)
335 cs_map = 0xa;
336 else
337 cs_map = 0xF;
338 }
339
Kever Yang50fb9982017-02-22 16:56:35 +0800340 /* PI_41 PI_CS_MAP:RW:24:4 */
341 clrsetbits_le32(&denali_pi[41], 0xf << 24, cs_map << 24);
Jagan Teki97867c82019-07-15 23:51:05 +0530342 if (sdram_ch->cap_info.rank == 1 && params->base.dramtype == DDR3)
Kever Yang50fb9982017-02-22 16:56:35 +0800343 writel(0x2EC7FFFF, &denali_pi[34]);
344}
345
Jagan Tekib5d46632019-07-16 17:27:07 +0530346static int phy_io_config(const struct chan_info *chan,
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530347 const struct rk3399_sdram_params *params, u32 mr5)
Jagan Tekib5d46632019-07-16 17:27:07 +0530348{
349 u32 *denali_phy = chan->publ->denali_phy;
350 u32 vref_mode_dq, vref_value_dq, vref_mode_ac, vref_value_ac;
351 u32 mode_sel;
352 u32 reg_value;
353 u32 drv_value, odt_value;
354 u32 speed;
355
Jagan Teki59a9a572019-07-16 17:27:27 +0530356 /* vref setting & mode setting */
Jagan Tekib5d46632019-07-16 17:27:07 +0530357 if (params->base.dramtype == LPDDR4) {
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530358 struct io_setting *io = lpddr4_get_io_settings(params, mr5);
359 u32 rd_vref = io->rd_vref * 1000;
360
361 if (rd_vref < 36700) {
362 /* MODE_LV[2:0] = LPDDR4 (Range 2)*/
363 vref_mode_dq = 0x7;
Jagan Teki59a9a572019-07-16 17:27:27 +0530364 /* MODE[2:0]= LPDDR4 Range 2(0.4*VDDQ) */
365 mode_sel = 0x5;
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530366 vref_value_dq = (rd_vref - 3300) / 521;
367 } else {
368 /* MODE_LV[2:0] = LPDDR4 (Range 1)*/
369 vref_mode_dq = 0x6;
Jagan Teki59a9a572019-07-16 17:27:27 +0530370 /* MODE[2:0]= LPDDR4 Range 1(0.33*VDDQ) */
371 mode_sel = 0x4;
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530372 vref_value_dq = (rd_vref - 15300) / 521;
373 }
Jagan Tekib5d46632019-07-16 17:27:07 +0530374 vref_mode_ac = 0x6;
Jagan Tekia5b07192019-07-16 17:27:28 +0530375 /* VDDQ/3/2=16.8% */
376 vref_value_ac = 0x3;
Jagan Tekib5d46632019-07-16 17:27:07 +0530377 } else if (params->base.dramtype == LPDDR3) {
378 if (params->base.odt == 1) {
379 vref_mode_dq = 0x5; /* LPDDR3 ODT */
380 drv_value = (readl(&denali_phy[6]) >> 12) & 0xf;
381 odt_value = (readl(&denali_phy[6]) >> 4) & 0xf;
382 if (drv_value == PHY_DRV_ODT_48) {
383 switch (odt_value) {
384 case PHY_DRV_ODT_240:
385 vref_value_dq = 0x16;
386 break;
387 case PHY_DRV_ODT_120:
388 vref_value_dq = 0x26;
389 break;
390 case PHY_DRV_ODT_60:
391 vref_value_dq = 0x36;
392 break;
393 default:
394 debug("Invalid ODT value.\n");
395 return -EINVAL;
396 }
397 } else if (drv_value == PHY_DRV_ODT_40) {
398 switch (odt_value) {
399 case PHY_DRV_ODT_240:
400 vref_value_dq = 0x19;
401 break;
402 case PHY_DRV_ODT_120:
403 vref_value_dq = 0x23;
404 break;
405 case PHY_DRV_ODT_60:
406 vref_value_dq = 0x31;
407 break;
408 default:
409 debug("Invalid ODT value.\n");
410 return -EINVAL;
411 }
412 } else if (drv_value == PHY_DRV_ODT_34_3) {
413 switch (odt_value) {
414 case PHY_DRV_ODT_240:
415 vref_value_dq = 0x17;
416 break;
417 case PHY_DRV_ODT_120:
418 vref_value_dq = 0x20;
419 break;
420 case PHY_DRV_ODT_60:
421 vref_value_dq = 0x2e;
422 break;
423 default:
424 debug("Invalid ODT value.\n");
425 return -EINVAL;
426 }
427 } else {
428 debug("Invalid DRV value.\n");
429 return -EINVAL;
430 }
431 } else {
432 vref_mode_dq = 0x2; /* LPDDR3 */
433 vref_value_dq = 0x1f;
434 }
435 vref_mode_ac = 0x2;
436 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530437 mode_sel = 0x0;
Jagan Tekib5d46632019-07-16 17:27:07 +0530438 } else if (params->base.dramtype == DDR3) {
439 /* DDR3L */
440 vref_mode_dq = 0x1;
441 vref_value_dq = 0x1f;
442 vref_mode_ac = 0x1;
443 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530444 mode_sel = 0x1;
Jagan Tekib5d46632019-07-16 17:27:07 +0530445 } else {
446 debug("Unknown DRAM type.\n");
447 return -EINVAL;
448 }
449
450 reg_value = (vref_mode_dq << 9) | (0x1 << 8) | vref_value_dq;
451
452 /* PHY_913 PHY_PAD_VREF_CTRL_DQ_0 12bits offset_8 */
453 clrsetbits_le32(&denali_phy[913], 0xfff << 8, reg_value << 8);
454 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_1 12bits offset_0 */
455 clrsetbits_le32(&denali_phy[914], 0xfff, reg_value);
456 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_2 12bits offset_16 */
457 clrsetbits_le32(&denali_phy[914], 0xfff << 16, reg_value << 16);
458 /* PHY_915 PHY_PAD_VREF_CTRL_DQ_3 12bits offset_0 */
459 clrsetbits_le32(&denali_phy[915], 0xfff, reg_value);
460
461 reg_value = (vref_mode_ac << 9) | (0x1 << 8) | vref_value_ac;
462
463 /* PHY_915 PHY_PAD_VREF_CTRL_AC 12bits offset_16 */
464 clrsetbits_le32(&denali_phy[915], 0xfff << 16, reg_value << 16);
465
Jagan Tekib5d46632019-07-16 17:27:07 +0530466 /* PHY_924 PHY_PAD_FDBK_DRIVE */
467 clrsetbits_le32(&denali_phy[924], 0x7 << 15, mode_sel << 15);
468 /* PHY_926 PHY_PAD_DATA_DRIVE */
469 clrsetbits_le32(&denali_phy[926], 0x7 << 6, mode_sel << 6);
470 /* PHY_927 PHY_PAD_DQS_DRIVE */
471 clrsetbits_le32(&denali_phy[927], 0x7 << 6, mode_sel << 6);
472 /* PHY_928 PHY_PAD_ADDR_DRIVE */
473 clrsetbits_le32(&denali_phy[928], 0x7 << 14, mode_sel << 14);
474 /* PHY_929 PHY_PAD_CLK_DRIVE */
475 clrsetbits_le32(&denali_phy[929], 0x7 << 14, mode_sel << 14);
476 /* PHY_935 PHY_PAD_CKE_DRIVE */
477 clrsetbits_le32(&denali_phy[935], 0x7 << 14, mode_sel << 14);
478 /* PHY_937 PHY_PAD_RST_DRIVE */
479 clrsetbits_le32(&denali_phy[937], 0x7 << 14, mode_sel << 14);
480 /* PHY_939 PHY_PAD_CS_DRIVE */
481 clrsetbits_le32(&denali_phy[939], 0x7 << 14, mode_sel << 14);
482
Jagan Teki5d152172019-07-16 17:27:15 +0530483 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
484 /* BOOSTP_EN & BOOSTN_EN */
485 reg_value = ((PHY_BOOSTP_EN << 4) | PHY_BOOSTN_EN);
486 /* PHY_925 PHY_PAD_FDBK_DRIVE2 */
487 clrsetbits_le32(&denali_phy[925], 0xff << 8, reg_value << 8);
488 /* PHY_926 PHY_PAD_DATA_DRIVE */
489 clrsetbits_le32(&denali_phy[926], 0xff << 12, reg_value << 12);
490 /* PHY_927 PHY_PAD_DQS_DRIVE */
491 clrsetbits_le32(&denali_phy[927], 0xff << 14, reg_value << 14);
492 /* PHY_928 PHY_PAD_ADDR_DRIVE */
493 clrsetbits_le32(&denali_phy[928], 0xff << 20, reg_value << 20);
494 /* PHY_929 PHY_PAD_CLK_DRIVE */
495 clrsetbits_le32(&denali_phy[929], 0xff << 22, reg_value << 22);
496 /* PHY_935 PHY_PAD_CKE_DRIVE */
497 clrsetbits_le32(&denali_phy[935], 0xff << 20, reg_value << 20);
498 /* PHY_937 PHY_PAD_RST_DRIVE */
499 clrsetbits_le32(&denali_phy[937], 0xff << 20, reg_value << 20);
500 /* PHY_939 PHY_PAD_CS_DRIVE */
501 clrsetbits_le32(&denali_phy[939], 0xff << 20, reg_value << 20);
Jagan Tekid8681842019-07-16 17:27:16 +0530502
503 /* SLEWP_EN & SLEWN_EN */
504 reg_value = ((PHY_SLEWP_EN << 3) | PHY_SLEWN_EN);
505 /* PHY_924 PHY_PAD_FDBK_DRIVE */
506 clrsetbits_le32(&denali_phy[924], 0x3f << 8, reg_value << 8);
507 /* PHY_926 PHY_PAD_DATA_DRIVE */
508 clrsetbits_le32(&denali_phy[926], 0x3f, reg_value);
509 /* PHY_927 PHY_PAD_DQS_DRIVE */
510 clrsetbits_le32(&denali_phy[927], 0x3f, reg_value);
511 /* PHY_928 PHY_PAD_ADDR_DRIVE */
512 clrsetbits_le32(&denali_phy[928], 0x3f << 8, reg_value << 8);
513 /* PHY_929 PHY_PAD_CLK_DRIVE */
514 clrsetbits_le32(&denali_phy[929], 0x3f << 8, reg_value << 8);
515 /* PHY_935 PHY_PAD_CKE_DRIVE */
516 clrsetbits_le32(&denali_phy[935], 0x3f << 8, reg_value << 8);
517 /* PHY_937 PHY_PAD_RST_DRIVE */
518 clrsetbits_le32(&denali_phy[937], 0x3f << 8, reg_value << 8);
519 /* PHY_939 PHY_PAD_CS_DRIVE */
520 clrsetbits_le32(&denali_phy[939], 0x3f << 8, reg_value << 8);
Jagan Teki5d152172019-07-16 17:27:15 +0530521 }
522
Jagan Tekib5d46632019-07-16 17:27:07 +0530523 /* speed setting */
524 if (params->base.ddr_freq < 400)
525 speed = 0x0;
526 else if (params->base.ddr_freq < 800)
527 speed = 0x1;
528 else if (params->base.ddr_freq < 1200)
529 speed = 0x2;
530 else
531 speed = 0x3;
532
533 /* PHY_924 PHY_PAD_FDBK_DRIVE */
534 clrsetbits_le32(&denali_phy[924], 0x3 << 21, speed << 21);
535 /* PHY_926 PHY_PAD_DATA_DRIVE */
536 clrsetbits_le32(&denali_phy[926], 0x3 << 9, speed << 9);
537 /* PHY_927 PHY_PAD_DQS_DRIVE */
538 clrsetbits_le32(&denali_phy[927], 0x3 << 9, speed << 9);
539 /* PHY_928 PHY_PAD_ADDR_DRIVE */
540 clrsetbits_le32(&denali_phy[928], 0x3 << 17, speed << 17);
541 /* PHY_929 PHY_PAD_CLK_DRIVE */
542 clrsetbits_le32(&denali_phy[929], 0x3 << 17, speed << 17);
543 /* PHY_935 PHY_PAD_CKE_DRIVE */
544 clrsetbits_le32(&denali_phy[935], 0x3 << 17, speed << 17);
545 /* PHY_937 PHY_PAD_RST_DRIVE */
546 clrsetbits_le32(&denali_phy[937], 0x3 << 17, speed << 17);
547 /* PHY_939 PHY_PAD_CS_DRIVE */
548 clrsetbits_le32(&denali_phy[939], 0x3 << 17, speed << 17);
549
Jagan Teki65535a22019-07-16 17:27:17 +0530550 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
551 /* RX_CM_INPUT */
552 reg_value = PHY_RX_CM_INPUT;
553 /* PHY_924 PHY_PAD_FDBK_DRIVE */
554 clrsetbits_le32(&denali_phy[924], 0x1 << 14, reg_value << 14);
555 /* PHY_926 PHY_PAD_DATA_DRIVE */
556 clrsetbits_le32(&denali_phy[926], 0x1 << 11, reg_value << 11);
557 /* PHY_927 PHY_PAD_DQS_DRIVE */
558 clrsetbits_le32(&denali_phy[927], 0x1 << 13, reg_value << 13);
559 /* PHY_928 PHY_PAD_ADDR_DRIVE */
560 clrsetbits_le32(&denali_phy[928], 0x1 << 19, reg_value << 19);
561 /* PHY_929 PHY_PAD_CLK_DRIVE */
562 clrsetbits_le32(&denali_phy[929], 0x1 << 21, reg_value << 21);
563 /* PHY_935 PHY_PAD_CKE_DRIVE */
564 clrsetbits_le32(&denali_phy[935], 0x1 << 19, reg_value << 19);
565 /* PHY_937 PHY_PAD_RST_DRIVE */
566 clrsetbits_le32(&denali_phy[937], 0x1 << 19, reg_value << 19);
567 /* PHY_939 PHY_PAD_CS_DRIVE */
568 clrsetbits_le32(&denali_phy[939], 0x1 << 19, reg_value << 19);
569 }
570
Jagan Tekib5d46632019-07-16 17:27:07 +0530571 return 0;
572}
573
Kever Yang50fb9982017-02-22 16:56:35 +0800574static void set_ds_odt(const struct chan_info *chan,
Jagan Tekid33056b2019-07-16 17:27:22 +0530575 const struct rk3399_sdram_params *params, u32 mr5)
Kever Yang50fb9982017-02-22 16:56:35 +0800576{
577 u32 *denali_phy = chan->publ->denali_phy;
Jagan Teki0cb31122019-07-16 17:27:24 +0530578 u32 *denali_ctl = chan->pctl->denali_ctl;
Kever Yang50fb9982017-02-22 16:56:35 +0800579 u32 tsel_idle_en, tsel_wr_en, tsel_rd_en;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530580 u32 tsel_idle_select_p, tsel_rd_select_p;
581 u32 tsel_idle_select_n, tsel_rd_select_n;
582 u32 tsel_wr_select_dq_p, tsel_wr_select_ca_p;
583 u32 tsel_wr_select_dq_n, tsel_wr_select_ca_n;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530584 u32 tsel_ckcs_select_p, tsel_ckcs_select_n;
Jagan Tekid33056b2019-07-16 17:27:22 +0530585 struct io_setting *io = NULL;
Jagan Teki0cb31122019-07-16 17:27:24 +0530586 u32 soc_odt = 0;
Kever Yang50fb9982017-02-22 16:56:35 +0800587 u32 reg_value;
588
Jagan Tekia58ff792019-07-15 23:50:58 +0530589 if (params->base.dramtype == LPDDR4) {
Jagan Tekid33056b2019-07-16 17:27:22 +0530590 io = lpddr4_get_io_settings(params, mr5);
591
Jagan Tekif676c7c2019-07-15 23:50:56 +0530592 tsel_rd_select_p = PHY_DRV_ODT_HI_Z;
Jagan Tekid33056b2019-07-16 17:27:22 +0530593 tsel_rd_select_n = io->rd_odt;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530594
Jagan Tekif676c7c2019-07-15 23:50:56 +0530595 tsel_idle_select_p = PHY_DRV_ODT_HI_Z;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530596 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800597
Jagan Tekid33056b2019-07-16 17:27:22 +0530598 tsel_wr_select_dq_p = io->wr_dq_drv;
Jagan Teki36667142019-07-15 23:51:00 +0530599 tsel_wr_select_dq_n = PHY_DRV_ODT_40;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530600
Jagan Tekid33056b2019-07-16 17:27:22 +0530601 tsel_wr_select_ca_p = io->wr_ca_drv;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530602 tsel_wr_select_ca_n = PHY_DRV_ODT_40;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530603
604 tsel_ckcs_select_p = io->wr_ckcs_drv;
605 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
Jagan Teki0cb31122019-07-16 17:27:24 +0530606 switch (tsel_rd_select_n) {
607 case PHY_DRV_ODT_240:
608 soc_odt = 1;
609 break;
610 case PHY_DRV_ODT_120:
611 soc_odt = 2;
612 break;
613 case PHY_DRV_ODT_80:
614 soc_odt = 3;
615 break;
616 case PHY_DRV_ODT_60:
617 soc_odt = 4;
618 break;
619 case PHY_DRV_ODT_48:
620 soc_odt = 5;
621 break;
622 case PHY_DRV_ODT_40:
623 soc_odt = 6;
624 break;
625 case PHY_DRV_ODT_34_3:
626 soc_odt = 6;
627 printf("%s: Unable to support LPDDR4 MR22 Soc ODT\n",
628 __func__);
629 break;
630 case PHY_DRV_ODT_HI_Z:
631 default:
632 soc_odt = 0;
633 break;
634 }
Jagan Tekia58ff792019-07-15 23:50:58 +0530635 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +0800636 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530637 tsel_rd_select_n = PHY_DRV_ODT_HI_Z;
638
Kever Yang50fb9982017-02-22 16:56:35 +0800639 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530640 tsel_idle_select_n = PHY_DRV_ODT_HI_Z;
Kever Yang50fb9982017-02-22 16:56:35 +0800641
Jagan Teki5c3251f2019-07-15 23:51:04 +0530642 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530643 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530644
645 tsel_wr_select_ca_p = PHY_DRV_ODT_48;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530646 tsel_wr_select_ca_n = PHY_DRV_ODT_48;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530647
648 tsel_ckcs_select_p = PHY_DRV_ODT_34_3;
649 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
Kever Yang50fb9982017-02-22 16:56:35 +0800650 } else {
651 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530652 tsel_rd_select_n = PHY_DRV_ODT_240;
653
Kever Yang50fb9982017-02-22 16:56:35 +0800654 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530655 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800656
Jagan Teki5c3251f2019-07-15 23:51:04 +0530657 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530658 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530659
660 tsel_wr_select_ca_p = PHY_DRV_ODT_34_3;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530661 tsel_wr_select_ca_n = PHY_DRV_ODT_34_3;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530662
663 tsel_ckcs_select_p = PHY_DRV_ODT_34_3;
664 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
Kever Yang50fb9982017-02-22 16:56:35 +0800665 }
666
Jagan Tekib9584172019-07-16 17:27:25 +0530667 if (params->base.odt == 1) {
Kever Yang50fb9982017-02-22 16:56:35 +0800668 tsel_rd_en = 1;
Jagan Tekib9584172019-07-16 17:27:25 +0530669
670 if (params->base.dramtype == LPDDR4)
671 tsel_rd_en = io->rd_odt_en;
672 } else {
Kever Yang50fb9982017-02-22 16:56:35 +0800673 tsel_rd_en = 0;
Jagan Tekib9584172019-07-16 17:27:25 +0530674 }
Kever Yang50fb9982017-02-22 16:56:35 +0800675
676 tsel_wr_en = 0;
677 tsel_idle_en = 0;
678
Jagan Teki0cb31122019-07-16 17:27:24 +0530679 /* F0_0 */
680 clrsetbits_le32(&denali_ctl[145], 0xFF << 16,
681 (soc_odt | (CS0_MR22_VAL << 3)) << 16);
682 /* F2_0, F1_0 */
683 clrsetbits_le32(&denali_ctl[146], 0xFF00FF,
684 ((soc_odt | (CS0_MR22_VAL << 3)) << 16) |
685 (soc_odt | (CS0_MR22_VAL << 3)));
686 /* F0_1 */
687 clrsetbits_le32(&denali_ctl[159], 0xFF << 16,
688 (soc_odt | (CS1_MR22_VAL << 3)) << 16);
689 /* F2_1, F1_1 */
690 clrsetbits_le32(&denali_ctl[160], 0xFF00FF,
691 ((soc_odt | (CS1_MR22_VAL << 3)) << 16) |
692 (soc_odt | (CS1_MR22_VAL << 3)));
693
Kever Yang50fb9982017-02-22 16:56:35 +0800694 /*
695 * phy_dq_tsel_select_X 24bits DENALI_PHY_6/134/262/390 offset_0
696 * sets termination values for read/idle cycles and drive strength
697 * for write cycles for DQ/DM
698 */
699 reg_value = tsel_rd_select_n | (tsel_rd_select_p << 0x4) |
Jagan Tekib3b34392019-07-15 23:51:01 +0530700 (tsel_wr_select_dq_n << 8) | (tsel_wr_select_dq_p << 12) |
Kever Yang50fb9982017-02-22 16:56:35 +0800701 (tsel_idle_select_n << 16) | (tsel_idle_select_p << 20);
702 clrsetbits_le32(&denali_phy[6], 0xffffff, reg_value);
703 clrsetbits_le32(&denali_phy[134], 0xffffff, reg_value);
704 clrsetbits_le32(&denali_phy[262], 0xffffff, reg_value);
705 clrsetbits_le32(&denali_phy[390], 0xffffff, reg_value);
706
707 /*
708 * phy_dqs_tsel_select_X 24bits DENALI_PHY_7/135/263/391 offset_0
709 * sets termination values for read/idle cycles and drive strength
710 * for write cycles for DQS
711 */
712 clrsetbits_le32(&denali_phy[7], 0xffffff, reg_value);
713 clrsetbits_le32(&denali_phy[135], 0xffffff, reg_value);
714 clrsetbits_le32(&denali_phy[263], 0xffffff, reg_value);
715 clrsetbits_le32(&denali_phy[391], 0xffffff, reg_value);
716
717 /* phy_adr_tsel_select_ 8bits DENALI_PHY_544/672/800 offset_0 */
Jagan Teki7caa3e92019-07-15 23:51:03 +0530718 reg_value = tsel_wr_select_ca_n | (tsel_wr_select_ca_p << 0x4);
Jagan Teki539ffed2019-07-16 17:27:19 +0530719 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
720 /* LPDDR4 these register read always return 0, so
721 * can not use clrsetbits_le32(), need to write32
722 */
723 writel((0x300 << 8) | reg_value, &denali_phy[544]);
724 writel((0x300 << 8) | reg_value, &denali_phy[672]);
725 writel((0x300 << 8) | reg_value, &denali_phy[800]);
726 } else {
727 clrsetbits_le32(&denali_phy[544], 0xff, reg_value);
728 clrsetbits_le32(&denali_phy[672], 0xff, reg_value);
729 clrsetbits_le32(&denali_phy[800], 0xff, reg_value);
730 }
Kever Yang50fb9982017-02-22 16:56:35 +0800731
732 /* phy_pad_addr_drive 8bits DENALI_PHY_928 offset_0 */
733 clrsetbits_le32(&denali_phy[928], 0xff, reg_value);
734
735 /* phy_pad_rst_drive 8bits DENALI_PHY_937 offset_0 */
736 clrsetbits_le32(&denali_phy[937], 0xff, reg_value);
737
738 /* phy_pad_cke_drive 8bits DENALI_PHY_935 offset_0 */
739 clrsetbits_le32(&denali_phy[935], 0xff, reg_value);
740
741 /* phy_pad_cs_drive 8bits DENALI_PHY_939 offset_0 */
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530742 clrsetbits_le32(&denali_phy[939], 0xff,
743 tsel_ckcs_select_n | (tsel_ckcs_select_p << 0x4));
Kever Yang50fb9982017-02-22 16:56:35 +0800744
745 /* phy_pad_clk_drive 8bits DENALI_PHY_929 offset_0 */
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530746 clrsetbits_le32(&denali_phy[929], 0xff,
747 tsel_ckcs_select_n | (tsel_ckcs_select_p << 0x4));
Kever Yang50fb9982017-02-22 16:56:35 +0800748
749 /* phy_pad_fdbk_drive 23bit DENALI_PHY_924/925 */
750 clrsetbits_le32(&denali_phy[924], 0xff,
Jagan Tekib3b34392019-07-15 23:51:01 +0530751 tsel_wr_select_dq_n | (tsel_wr_select_dq_p << 4));
Kever Yang50fb9982017-02-22 16:56:35 +0800752 clrsetbits_le32(&denali_phy[925], 0xff,
753 tsel_rd_select_n | (tsel_rd_select_p << 4));
754
755 /* phy_dq_tsel_enable_X 3bits DENALI_PHY_5/133/261/389 offset_16 */
756 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
757 << 16;
758 clrsetbits_le32(&denali_phy[5], 0x7 << 16, reg_value);
759 clrsetbits_le32(&denali_phy[133], 0x7 << 16, reg_value);
760 clrsetbits_le32(&denali_phy[261], 0x7 << 16, reg_value);
761 clrsetbits_le32(&denali_phy[389], 0x7 << 16, reg_value);
762
763 /* phy_dqs_tsel_enable_X 3bits DENALI_PHY_6/134/262/390 offset_24 */
764 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
765 << 24;
766 clrsetbits_le32(&denali_phy[6], 0x7 << 24, reg_value);
767 clrsetbits_le32(&denali_phy[134], 0x7 << 24, reg_value);
768 clrsetbits_le32(&denali_phy[262], 0x7 << 24, reg_value);
769 clrsetbits_le32(&denali_phy[390], 0x7 << 24, reg_value);
770
771 /* phy_adr_tsel_enable_ 1bit DENALI_PHY_518/646/774 offset_8 */
772 reg_value = tsel_wr_en << 8;
773 clrsetbits_le32(&denali_phy[518], 0x1 << 8, reg_value);
774 clrsetbits_le32(&denali_phy[646], 0x1 << 8, reg_value);
775 clrsetbits_le32(&denali_phy[774], 0x1 << 8, reg_value);
776
777 /* phy_pad_addr_term tsel 1bit DENALI_PHY_933 offset_17 */
778 reg_value = tsel_wr_en << 17;
779 clrsetbits_le32(&denali_phy[933], 0x1 << 17, reg_value);
780 /*
781 * pad_rst/cke/cs/clk_term tsel 1bits
782 * DENALI_PHY_938/936/940/934 offset_17
783 */
784 clrsetbits_le32(&denali_phy[938], 0x1 << 17, reg_value);
785 clrsetbits_le32(&denali_phy[936], 0x1 << 17, reg_value);
786 clrsetbits_le32(&denali_phy[940], 0x1 << 17, reg_value);
787 clrsetbits_le32(&denali_phy[934], 0x1 << 17, reg_value);
788
789 /* phy_pad_fdbk_term 1bit DENALI_PHY_930 offset_17 */
790 clrsetbits_le32(&denali_phy[930], 0x1 << 17, reg_value);
Jagan Tekib5d46632019-07-16 17:27:07 +0530791
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530792 phy_io_config(chan, params, mr5);
Kever Yang50fb9982017-02-22 16:56:35 +0800793}
794
Jagan Tekic9151e22019-07-15 23:58:45 +0530795static void pctl_start(struct dram_info *dram, u8 channel)
796{
797 const struct chan_info *chan = &dram->chan[channel];
798 u32 *denali_ctl = chan->pctl->denali_ctl;
799 u32 *denali_phy = chan->publ->denali_phy;
800 u32 *ddrc0_con = get_ddrc0_con(dram, channel);
801 u32 count = 0;
802 u32 byte, tmp;
803
804 writel(0x01000000, &ddrc0_con);
805
806 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 0x2 << 24);
807
808 while (!(readl(&denali_ctl[203]) & (1 << 3))) {
809 if (count > 1000) {
810 printf("%s: Failed to init pctl for channel %d\n",
811 __func__, channel);
812 while (1)
813 ;
814 }
815
816 udelay(1);
817 count++;
818 }
819
820 writel(0x01000100, &ddrc0_con);
821
822 for (byte = 0; byte < 4; byte++) {
823 tmp = 0x820;
824 writel((tmp << 16) | tmp, &denali_phy[53 + (128 * byte)]);
825 writel((tmp << 16) | tmp, &denali_phy[54 + (128 * byte)]);
826 writel((tmp << 16) | tmp, &denali_phy[55 + (128 * byte)]);
827 writel((tmp << 16) | tmp, &denali_phy[56 + (128 * byte)]);
828 writel((tmp << 16) | tmp, &denali_phy[57 + (128 * byte)]);
829
830 clrsetbits_le32(&denali_phy[58 + (128 * byte)], 0xffff, tmp);
831 }
832
833 clrsetbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT,
834 dram->pwrup_srefresh_exit[channel]);
835}
836
Jagan Teki4ef5c012019-07-15 23:58:44 +0530837static int pctl_cfg(struct dram_info *dram, const struct chan_info *chan,
Jagan Tekid33056b2019-07-16 17:27:22 +0530838 u32 channel, struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800839{
840 u32 *denali_ctl = chan->pctl->denali_ctl;
841 u32 *denali_pi = chan->pi->denali_pi;
842 u32 *denali_phy = chan->publ->denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +0530843 const u32 *params_ctl = params->pctl_regs.denali_ctl;
844 const u32 *params_phy = params->phy_regs.denali_phy;
Kever Yang50fb9982017-02-22 16:56:35 +0800845 u32 tmp, tmp1, tmp2;
Kever Yang50fb9982017-02-22 16:56:35 +0800846
847 /*
848 * work around controller bug:
849 * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed
850 */
851 copy_to_reg(&denali_ctl[1], &params_ctl[1],
852 sizeof(struct rk3399_ddr_pctl_regs) - 4);
853 writel(params_ctl[0], &denali_ctl[0]);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530854
Jagan Tekicc9da9a2019-07-16 17:27:13 +0530855 /*
856 * two channel init at the same time, then ZQ Cal Start
857 * at the same time, it will use the same RZQ, but cannot
858 * start at the same time.
859 *
860 * So, increase tINIT3 for channel 1, will avoid two
861 * channel ZQ Cal Start at the same time
862 */
863 if (params->base.dramtype == LPDDR4 && channel == 1) {
864 tmp = ((params->base.ddr_freq * MHz + 999) / 1000);
865 tmp1 = readl(&denali_ctl[14]);
866 writel(tmp + tmp1, &denali_ctl[14]);
867 }
868
Jagan Tekia58ff792019-07-15 23:50:58 +0530869 copy_to_reg(denali_pi, &params->pi_regs.denali_pi[0],
Kever Yang50fb9982017-02-22 16:56:35 +0800870 sizeof(struct rk3399_ddr_pi_regs));
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530871
Kever Yang50fb9982017-02-22 16:56:35 +0800872 /* rank count need to set for init */
Jagan Tekia58ff792019-07-15 23:50:58 +0530873 set_memory_map(chan, channel, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800874
Jagan Tekia58ff792019-07-15 23:50:58 +0530875 writel(params->phy_regs.denali_phy[910], &denali_phy[910]);
876 writel(params->phy_regs.denali_phy[911], &denali_phy[911]);
877 writel(params->phy_regs.denali_phy[912], &denali_phy[912]);
Kever Yang50fb9982017-02-22 16:56:35 +0800878
Jagan Tekib49b5dc2019-07-16 17:27:14 +0530879 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
880 writel(params->phy_regs.denali_phy[898], &denali_phy[898]);
881 writel(params->phy_regs.denali_phy[919], &denali_phy[919]);
882 }
883
Jagan Tekic9151e22019-07-15 23:58:45 +0530884 dram->pwrup_srefresh_exit[channel] = readl(&denali_ctl[68]) &
885 PWRUP_SREFRESH_EXIT;
Kever Yang50fb9982017-02-22 16:56:35 +0800886 clrbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT);
887
888 /* PHY_DLL_RST_EN */
889 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 1 << 24);
890
891 setbits_le32(&denali_pi[0], START);
892 setbits_le32(&denali_ctl[0], START);
893
Jagan Teki5e927182019-07-16 17:27:12 +0530894 /**
895 * LPDDR4 use PLL bypass mode for init
896 * not need to wait for the PLL to lock
897 */
898 if (params->base.dramtype != LPDDR4) {
899 /* Waiting for phy DLL lock */
900 while (1) {
901 tmp = readl(&denali_phy[920]);
902 tmp1 = readl(&denali_phy[921]);
903 tmp2 = readl(&denali_phy[922]);
904 if ((((tmp >> 16) & 0x1) == 0x1) &&
905 (((tmp1 >> 16) & 0x1) == 0x1) &&
906 (((tmp1 >> 0) & 0x1) == 0x1) &&
907 (((tmp2 >> 0) & 0x1) == 0x1))
908 break;
909 }
Kever Yang50fb9982017-02-22 16:56:35 +0800910 }
911
912 copy_to_reg(&denali_phy[896], &params_phy[896], (958 - 895) * 4);
913 copy_to_reg(&denali_phy[0], &params_phy[0], (90 - 0 + 1) * 4);
914 copy_to_reg(&denali_phy[128], &params_phy[128], (218 - 128 + 1) * 4);
915 copy_to_reg(&denali_phy[256], &params_phy[256], (346 - 256 + 1) * 4);
916 copy_to_reg(&denali_phy[384], &params_phy[384], (474 - 384 + 1) * 4);
917 copy_to_reg(&denali_phy[512], &params_phy[512], (549 - 512 + 1) * 4);
918 copy_to_reg(&denali_phy[640], &params_phy[640], (677 - 640 + 1) * 4);
919 copy_to_reg(&denali_phy[768], &params_phy[768], (805 - 768 + 1) * 4);
Jagan Tekid33056b2019-07-16 17:27:22 +0530920 set_ds_odt(chan, params, 0);
Kever Yang50fb9982017-02-22 16:56:35 +0800921
922 /*
923 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_84/212/340/468 offset_8
924 * dqs_tsel_wr_end[7:4] add Half cycle
925 */
926 tmp = (readl(&denali_phy[84]) >> 8) & 0xff;
927 clrsetbits_le32(&denali_phy[84], 0xff << 8, (tmp + 0x10) << 8);
928 tmp = (readl(&denali_phy[212]) >> 8) & 0xff;
929 clrsetbits_le32(&denali_phy[212], 0xff << 8, (tmp + 0x10) << 8);
930 tmp = (readl(&denali_phy[340]) >> 8) & 0xff;
931 clrsetbits_le32(&denali_phy[340], 0xff << 8, (tmp + 0x10) << 8);
932 tmp = (readl(&denali_phy[468]) >> 8) & 0xff;
933 clrsetbits_le32(&denali_phy[468], 0xff << 8, (tmp + 0x10) << 8);
934
935 /*
936 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_83/211/339/467 offset_8
937 * dq_tsel_wr_end[7:4] add Half cycle
938 */
939 tmp = (readl(&denali_phy[83]) >> 16) & 0xff;
940 clrsetbits_le32(&denali_phy[83], 0xff << 16, (tmp + 0x10) << 16);
941 tmp = (readl(&denali_phy[211]) >> 16) & 0xff;
942 clrsetbits_le32(&denali_phy[211], 0xff << 16, (tmp + 0x10) << 16);
943 tmp = (readl(&denali_phy[339]) >> 16) & 0xff;
944 clrsetbits_le32(&denali_phy[339], 0xff << 16, (tmp + 0x10) << 16);
945 tmp = (readl(&denali_phy[467]) >> 16) & 0xff;
946 clrsetbits_le32(&denali_phy[467], 0xff << 16, (tmp + 0x10) << 16);
947
Kever Yang50fb9982017-02-22 16:56:35 +0800948 return 0;
949}
950
951static void select_per_cs_training_index(const struct chan_info *chan,
952 u32 rank)
953{
954 u32 *denali_phy = chan->publ->denali_phy;
955
956 /* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */
Jagan Tekif676c7c2019-07-15 23:50:56 +0530957 if ((readl(&denali_phy[84]) >> 16) & 1) {
Kever Yang50fb9982017-02-22 16:56:35 +0800958 /*
959 * PHY_8/136/264/392
960 * phy_per_cs_training_index_X 1bit offset_24
961 */
962 clrsetbits_le32(&denali_phy[8], 0x1 << 24, rank << 24);
963 clrsetbits_le32(&denali_phy[136], 0x1 << 24, rank << 24);
964 clrsetbits_le32(&denali_phy[264], 0x1 << 24, rank << 24);
965 clrsetbits_le32(&denali_phy[392], 0x1 << 24, rank << 24);
966 }
967}
968
969static void override_write_leveling_value(const struct chan_info *chan)
970{
971 u32 *denali_ctl = chan->pctl->denali_ctl;
972 u32 *denali_phy = chan->publ->denali_phy;
973 u32 byte;
974
975 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
976 setbits_le32(&denali_phy[896], 1);
977
978 /*
979 * PHY_8/136/264/392
980 * phy_per_cs_training_multicast_en_X 1bit offset_16
981 */
982 clrsetbits_le32(&denali_phy[8], 0x1 << 16, 1 << 16);
983 clrsetbits_le32(&denali_phy[136], 0x1 << 16, 1 << 16);
984 clrsetbits_le32(&denali_phy[264], 0x1 << 16, 1 << 16);
985 clrsetbits_le32(&denali_phy[392], 0x1 << 16, 1 << 16);
986
987 for (byte = 0; byte < 4; byte++)
988 clrsetbits_le32(&denali_phy[63 + (128 * byte)], 0xffff << 16,
989 0x200 << 16);
990
991 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
992 clrbits_le32(&denali_phy[896], 1);
993
994 /* CTL_200 ctrlupd_req 1bit offset_8 */
995 clrsetbits_le32(&denali_ctl[200], 0x1 << 8, 0x1 << 8);
996}
997
998static int data_training_ca(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530999 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001000{
1001 u32 *denali_pi = chan->pi->denali_pi;
1002 u32 *denali_phy = chan->publ->denali_phy;
1003 u32 i, tmp;
1004 u32 obs_0, obs_1, obs_2, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +05301005 u32 rank = params->ch[channel].cap_info.rank;
Jagan Tekibafcc142019-07-15 23:58:41 +05301006 u32 rank_mask;
Kever Yang50fb9982017-02-22 16:56:35 +08001007
Jagan Tekia6079612019-07-15 23:58:40 +05301008 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1009 writel(0x00003f7c, (&denali_pi[175]));
1010
Jagan Tekif05675e2019-07-16 17:27:09 +05301011 if (params->base.dramtype == LPDDR4)
1012 rank_mask = (rank == 1) ? 0x5 : 0xf;
1013 else
1014 rank_mask = (rank == 1) ? 0x1 : 0x3;
Jagan Tekibafcc142019-07-15 23:58:41 +05301015
1016 for (i = 0; i < 4; i++) {
1017 if (!(rank_mask & (1 << i)))
1018 continue;
1019
Kever Yang50fb9982017-02-22 16:56:35 +08001020 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301021
Kever Yang50fb9982017-02-22 16:56:35 +08001022 /* PI_100 PI_CALVL_EN:RW:8:2 */
1023 clrsetbits_le32(&denali_pi[100], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301024
Kever Yang50fb9982017-02-22 16:56:35 +08001025 /* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */
1026 clrsetbits_le32(&denali_pi[92],
1027 (0x1 << 16) | (0x3 << 24),
1028 (0x1 << 16) | (i << 24));
1029
1030 /* Waiting for training complete */
1031 while (1) {
1032 /* PI_174 PI_INT_STATUS:RD:8:18 */
1033 tmp = readl(&denali_pi[174]) >> 8;
1034 /*
1035 * check status obs
1036 * PHY_532/660/789 phy_adr_calvl_obs1_:0:32
1037 */
1038 obs_0 = readl(&denali_phy[532]);
1039 obs_1 = readl(&denali_phy[660]);
1040 obs_2 = readl(&denali_phy[788]);
1041 if (((obs_0 >> 30) & 0x3) ||
1042 ((obs_1 >> 30) & 0x3) ||
1043 ((obs_2 >> 30) & 0x3))
1044 obs_err = 1;
1045 if ((((tmp >> 11) & 0x1) == 0x1) &&
1046 (((tmp >> 13) & 0x1) == 0x1) &&
1047 (((tmp >> 5) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +05301048 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001049 break;
1050 else if ((((tmp >> 5) & 0x1) == 0x1) ||
1051 (obs_err == 1))
1052 return -EIO;
1053 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301054
Kever Yang50fb9982017-02-22 16:56:35 +08001055 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1056 writel(0x00003f7c, (&denali_pi[175]));
1057 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301058
Kever Yang50fb9982017-02-22 16:56:35 +08001059 clrbits_le32(&denali_pi[100], 0x3 << 8);
1060
1061 return 0;
1062}
1063
1064static int data_training_wl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301065 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001066{
1067 u32 *denali_pi = chan->pi->denali_pi;
1068 u32 *denali_phy = chan->publ->denali_phy;
1069 u32 i, tmp;
1070 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +05301071 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001072
Jagan Tekia6079612019-07-15 23:58:40 +05301073 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1074 writel(0x00003f7c, (&denali_pi[175]));
1075
Kever Yang50fb9982017-02-22 16:56:35 +08001076 for (i = 0; i < rank; i++) {
1077 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301078
Kever Yang50fb9982017-02-22 16:56:35 +08001079 /* PI_60 PI_WRLVL_EN:RW:8:2 */
1080 clrsetbits_le32(&denali_pi[60], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301081
Kever Yang50fb9982017-02-22 16:56:35 +08001082 /* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */
1083 clrsetbits_le32(&denali_pi[59],
1084 (0x1 << 8) | (0x3 << 16),
1085 (0x1 << 8) | (i << 16));
1086
1087 /* Waiting for training complete */
1088 while (1) {
1089 /* PI_174 PI_INT_STATUS:RD:8:18 */
1090 tmp = readl(&denali_pi[174]) >> 8;
1091
1092 /*
1093 * check status obs, if error maybe can not
1094 * get leveling done PHY_40/168/296/424
1095 * phy_wrlvl_status_obs_X:0:13
1096 */
1097 obs_0 = readl(&denali_phy[40]);
1098 obs_1 = readl(&denali_phy[168]);
1099 obs_2 = readl(&denali_phy[296]);
1100 obs_3 = readl(&denali_phy[424]);
1101 if (((obs_0 >> 12) & 0x1) ||
1102 ((obs_1 >> 12) & 0x1) ||
1103 ((obs_2 >> 12) & 0x1) ||
1104 ((obs_3 >> 12) & 0x1))
1105 obs_err = 1;
1106 if ((((tmp >> 10) & 0x1) == 0x1) &&
1107 (((tmp >> 13) & 0x1) == 0x1) &&
1108 (((tmp >> 4) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +05301109 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001110 break;
1111 else if ((((tmp >> 4) & 0x1) == 0x1) ||
1112 (obs_err == 1))
1113 return -EIO;
1114 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301115
Kever Yang50fb9982017-02-22 16:56:35 +08001116 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1117 writel(0x00003f7c, (&denali_pi[175]));
1118 }
1119
1120 override_write_leveling_value(chan);
1121 clrbits_le32(&denali_pi[60], 0x3 << 8);
1122
1123 return 0;
1124}
1125
1126static int data_training_rg(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301127 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001128{
1129 u32 *denali_pi = chan->pi->denali_pi;
1130 u32 *denali_phy = chan->publ->denali_phy;
1131 u32 i, tmp;
1132 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +05301133 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001134
Jagan Tekia6079612019-07-15 23:58:40 +05301135 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1136 writel(0x00003f7c, (&denali_pi[175]));
1137
Kever Yang50fb9982017-02-22 16:56:35 +08001138 for (i = 0; i < rank; i++) {
1139 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301140
Kever Yang50fb9982017-02-22 16:56:35 +08001141 /* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */
1142 clrsetbits_le32(&denali_pi[80], 0x3 << 24, 0x2 << 24);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301143
Kever Yang50fb9982017-02-22 16:56:35 +08001144 /*
1145 * PI_74 PI_RDLVL_GATE_REQ:WR:16:1
1146 * PI_RDLVL_CS:RW:24:2
1147 */
1148 clrsetbits_le32(&denali_pi[74],
1149 (0x1 << 16) | (0x3 << 24),
1150 (0x1 << 16) | (i << 24));
1151
1152 /* Waiting for training complete */
1153 while (1) {
1154 /* PI_174 PI_INT_STATUS:RD:8:18 */
1155 tmp = readl(&denali_pi[174]) >> 8;
1156
1157 /*
1158 * check status obs
1159 * PHY_43/171/299/427
1160 * PHY_GTLVL_STATUS_OBS_x:16:8
1161 */
1162 obs_0 = readl(&denali_phy[43]);
1163 obs_1 = readl(&denali_phy[171]);
1164 obs_2 = readl(&denali_phy[299]);
1165 obs_3 = readl(&denali_phy[427]);
1166 if (((obs_0 >> (16 + 6)) & 0x3) ||
1167 ((obs_1 >> (16 + 6)) & 0x3) ||
1168 ((obs_2 >> (16 + 6)) & 0x3) ||
1169 ((obs_3 >> (16 + 6)) & 0x3))
1170 obs_err = 1;
1171 if ((((tmp >> 9) & 0x1) == 0x1) &&
1172 (((tmp >> 13) & 0x1) == 0x1) &&
1173 (((tmp >> 3) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +05301174 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001175 break;
1176 else if ((((tmp >> 3) & 0x1) == 0x1) ||
1177 (obs_err == 1))
1178 return -EIO;
1179 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301180
Kever Yang50fb9982017-02-22 16:56:35 +08001181 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1182 writel(0x00003f7c, (&denali_pi[175]));
1183 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301184
Kever Yang50fb9982017-02-22 16:56:35 +08001185 clrbits_le32(&denali_pi[80], 0x3 << 24);
1186
1187 return 0;
1188}
1189
1190static int data_training_rl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301191 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001192{
1193 u32 *denali_pi = chan->pi->denali_pi;
1194 u32 i, tmp;
Jagan Teki97867c82019-07-15 23:51:05 +05301195 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001196
Jagan Tekia6079612019-07-15 23:58:40 +05301197 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1198 writel(0x00003f7c, (&denali_pi[175]));
1199
Kever Yang50fb9982017-02-22 16:56:35 +08001200 for (i = 0; i < rank; i++) {
1201 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301202
Kever Yang50fb9982017-02-22 16:56:35 +08001203 /* PI_80 PI_RDLVL_EN:RW:16:2 */
1204 clrsetbits_le32(&denali_pi[80], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301205
Kever Yang50fb9982017-02-22 16:56:35 +08001206 /* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */
1207 clrsetbits_le32(&denali_pi[74],
1208 (0x1 << 8) | (0x3 << 24),
1209 (0x1 << 8) | (i << 24));
1210
1211 /* Waiting for training complete */
1212 while (1) {
1213 /* PI_174 PI_INT_STATUS:RD:8:18 */
1214 tmp = readl(&denali_pi[174]) >> 8;
1215
1216 /*
1217 * make sure status obs not report error bit
1218 * PHY_46/174/302/430
1219 * phy_rdlvl_status_obs_X:16:8
1220 */
1221 if ((((tmp >> 8) & 0x1) == 0x1) &&
1222 (((tmp >> 13) & 0x1) == 0x1) &&
1223 (((tmp >> 2) & 0x1) == 0x0))
1224 break;
1225 else if (((tmp >> 2) & 0x1) == 0x1)
1226 return -EIO;
1227 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301228
Kever Yang50fb9982017-02-22 16:56:35 +08001229 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1230 writel(0x00003f7c, (&denali_pi[175]));
1231 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301232
Kever Yang50fb9982017-02-22 16:56:35 +08001233 clrbits_le32(&denali_pi[80], 0x3 << 16);
1234
1235 return 0;
1236}
1237
1238static int data_training_wdql(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301239 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001240{
1241 u32 *denali_pi = chan->pi->denali_pi;
1242 u32 i, tmp;
Jagan Teki97867c82019-07-15 23:51:05 +05301243 u32 rank = params->ch[channel].cap_info.rank;
Jagan Teki87723592019-07-15 23:58:42 +05301244 u32 rank_mask;
Kever Yang50fb9982017-02-22 16:56:35 +08001245
Jagan Tekia6079612019-07-15 23:58:40 +05301246 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1247 writel(0x00003f7c, (&denali_pi[175]));
1248
Jagan Tekid7504c02019-07-16 17:27:10 +05301249 if (params->base.dramtype == LPDDR4)
1250 rank_mask = (rank == 1) ? 0x5 : 0xf;
1251 else
1252 rank_mask = (rank == 1) ? 0x1 : 0x3;
Jagan Teki87723592019-07-15 23:58:42 +05301253
1254 for (i = 0; i < 4; i++) {
1255 if (!(rank_mask & (1 << i)))
1256 continue;
1257
Kever Yang50fb9982017-02-22 16:56:35 +08001258 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301259
Kever Yang50fb9982017-02-22 16:56:35 +08001260 /*
1261 * disable PI_WDQLVL_VREF_EN before wdq leveling?
1262 * PI_181 PI_WDQLVL_VREF_EN:RW:8:1
1263 */
1264 clrbits_le32(&denali_pi[181], 0x1 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301265
Kever Yang50fb9982017-02-22 16:56:35 +08001266 /* PI_124 PI_WDQLVL_EN:RW:16:2 */
1267 clrsetbits_le32(&denali_pi[124], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301268
Kever Yang50fb9982017-02-22 16:56:35 +08001269 /* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */
1270 clrsetbits_le32(&denali_pi[121],
1271 (0x1 << 8) | (0x3 << 16),
1272 (0x1 << 8) | (i << 16));
1273
1274 /* Waiting for training complete */
1275 while (1) {
1276 /* PI_174 PI_INT_STATUS:RD:8:18 */
1277 tmp = readl(&denali_pi[174]) >> 8;
1278 if ((((tmp >> 12) & 0x1) == 0x1) &&
1279 (((tmp >> 13) & 0x1) == 0x1) &&
1280 (((tmp >> 6) & 0x1) == 0x0))
1281 break;
1282 else if (((tmp >> 6) & 0x1) == 0x1)
1283 return -EIO;
1284 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301285
Kever Yang50fb9982017-02-22 16:56:35 +08001286 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1287 writel(0x00003f7c, (&denali_pi[175]));
1288 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301289
Kever Yang50fb9982017-02-22 16:56:35 +08001290 clrbits_le32(&denali_pi[124], 0x3 << 16);
1291
1292 return 0;
1293}
1294
Jagan Teki5ff7abe2019-07-16 17:27:29 +05301295static int data_training(struct dram_info *dram, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301296 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +08001297 u32 training_flag)
1298{
Jagan Teki5ff7abe2019-07-16 17:27:29 +05301299 struct chan_info *chan = &dram->chan[channel];
Kever Yang50fb9982017-02-22 16:56:35 +08001300 u32 *denali_phy = chan->publ->denali_phy;
Jagan Teki6214ff22019-07-15 23:58:39 +05301301 int ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001302
1303 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1304 setbits_le32(&denali_phy[927], (1 << 22));
1305
1306 if (training_flag == PI_FULL_TRAINING) {
Jagan Tekia58ff792019-07-15 23:50:58 +05301307 if (params->base.dramtype == LPDDR4) {
Kever Yang50fb9982017-02-22 16:56:35 +08001308 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1309 PI_READ_GATE_TRAINING |
1310 PI_READ_LEVELING | PI_WDQ_LEVELING;
Jagan Tekia58ff792019-07-15 23:50:58 +05301311 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +08001312 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1313 PI_READ_GATE_TRAINING;
Jagan Tekia58ff792019-07-15 23:50:58 +05301314 } else if (params->base.dramtype == DDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +08001315 training_flag = PI_WRITE_LEVELING |
1316 PI_READ_GATE_TRAINING |
1317 PI_READ_LEVELING;
1318 }
1319 }
1320
1321 /* ca training(LPDDR4,LPDDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301322 if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING) {
1323 ret = data_training_ca(chan, channel, params);
1324 if (ret < 0) {
1325 debug("%s: data training ca failed\n", __func__);
1326 return ret;
1327 }
1328 }
Kever Yang50fb9982017-02-22 16:56:35 +08001329
1330 /* write leveling(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301331 if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING) {
1332 ret = data_training_wl(chan, channel, params);
1333 if (ret < 0) {
1334 debug("%s: data training wl failed\n", __func__);
1335 return ret;
1336 }
1337 }
Kever Yang50fb9982017-02-22 16:56:35 +08001338
1339 /* read gate training(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301340 if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING) {
1341 ret = data_training_rg(chan, channel, params);
1342 if (ret < 0) {
1343 debug("%s: data training rg failed\n", __func__);
1344 return ret;
1345 }
1346 }
Kever Yang50fb9982017-02-22 16:56:35 +08001347
1348 /* read leveling(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301349 if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING) {
1350 ret = data_training_rl(chan, channel, params);
1351 if (ret < 0) {
1352 debug("%s: data training rl failed\n", __func__);
1353 return ret;
1354 }
1355 }
Kever Yang50fb9982017-02-22 16:56:35 +08001356
1357 /* wdq leveling(LPDDR4 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301358 if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING) {
1359 ret = data_training_wdql(chan, channel, params);
1360 if (ret < 0) {
1361 debug("%s: data training wdql failed\n", __func__);
1362 return ret;
1363 }
1364 }
Kever Yang50fb9982017-02-22 16:56:35 +08001365
1366 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1367 clrbits_le32(&denali_phy[927], (1 << 22));
1368
1369 return 0;
1370}
1371
1372static void set_ddrconfig(const struct chan_info *chan,
Jagan Tekia58ff792019-07-15 23:50:58 +05301373 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +08001374 unsigned char channel, u32 ddrconfig)
1375{
1376 /* only need to set ddrconfig */
1377 struct rk3399_msch_regs *ddr_msch_regs = chan->msch;
1378 unsigned int cs0_cap = 0;
1379 unsigned int cs1_cap = 0;
1380
Jagan Teki97867c82019-07-15 23:51:05 +05301381 cs0_cap = (1 << (params->ch[channel].cap_info.cs0_row
1382 + params->ch[channel].cap_info.col
1383 + params->ch[channel].cap_info.bk
1384 + params->ch[channel].cap_info.bw - 20));
1385 if (params->ch[channel].cap_info.rank > 1)
1386 cs1_cap = cs0_cap >> (params->ch[channel].cap_info.cs0_row
1387 - params->ch[channel].cap_info.cs1_row);
1388 if (params->ch[channel].cap_info.row_3_4) {
Kever Yang50fb9982017-02-22 16:56:35 +08001389 cs0_cap = cs0_cap * 3 / 4;
1390 cs1_cap = cs1_cap * 3 / 4;
1391 }
1392
1393 writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf);
1394 writel(((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8),
1395 &ddr_msch_regs->ddrsize);
1396}
1397
1398static void dram_all_config(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +05301399 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001400{
Jagan Teki2d337122019-07-16 17:27:00 +05301401 u32 sys_reg2 = 0;
Jagan Teki9d8769c2019-07-16 17:27:01 +05301402 u32 sys_reg3 = 0;
Kever Yang50fb9982017-02-22 16:56:35 +08001403 unsigned int channel, idx;
1404
Jagan Teki2d337122019-07-16 17:27:00 +05301405 sys_reg2 |= SYS_REG_ENC_DDRTYPE(params->base.dramtype);
1406 sys_reg2 |= SYS_REG_ENC_NUM_CH(params->base.num_channels);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301407
Kever Yang50fb9982017-02-22 16:56:35 +08001408 for (channel = 0, idx = 0;
Jagan Tekia58ff792019-07-15 23:50:58 +05301409 (idx < params->base.num_channels) && (channel < 2);
Kever Yang50fb9982017-02-22 16:56:35 +08001410 channel++) {
Jagan Tekia58ff792019-07-15 23:50:58 +05301411 const struct rk3399_sdram_channel *info = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +08001412 struct rk3399_msch_regs *ddr_msch_regs;
1413 const struct rk3399_msch_timings *noc_timing;
1414
Jagan Teki97867c82019-07-15 23:51:05 +05301415 if (params->ch[channel].cap_info.col == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001416 continue;
1417 idx++;
Jagan Teki2d337122019-07-16 17:27:00 +05301418 sys_reg2 |= SYS_REG_ENC_ROW_3_4(info->cap_info.row_3_4, channel);
1419 sys_reg2 |= SYS_REG_ENC_CHINFO(channel);
1420 sys_reg2 |= SYS_REG_ENC_RANK(info->cap_info.rank, channel);
1421 sys_reg2 |= SYS_REG_ENC_COL(info->cap_info.col, channel);
1422 sys_reg2 |= SYS_REG_ENC_BK(info->cap_info.bk, channel);
Jagan Teki2d337122019-07-16 17:27:00 +05301423 sys_reg2 |= SYS_REG_ENC_BW(info->cap_info.bw, channel);
1424 sys_reg2 |= SYS_REG_ENC_DBW(info->cap_info.dbw, channel);
Jagan Teki9d8769c2019-07-16 17:27:01 +05301425 SYS_REG_ENC_CS0_ROW(info->cap_info.cs0_row, sys_reg2, sys_reg3, channel);
1426 if (info->cap_info.cs1_row)
1427 SYS_REG_ENC_CS1_ROW(info->cap_info.cs1_row, sys_reg2,
1428 sys_reg3, channel);
1429 sys_reg3 |= SYS_REG_ENC_CS1_COL(info->cap_info.col, channel);
Jagan Teki932dd962019-07-16 17:27:04 +05301430 sys_reg3 |= SYS_REG_ENC_VERSION(DDR_SYS_REG_VERSION);
Kever Yang50fb9982017-02-22 16:56:35 +08001431
1432 ddr_msch_regs = dram->chan[channel].msch;
Jagan Tekia58ff792019-07-15 23:50:58 +05301433 noc_timing = &params->ch[channel].noc_timings;
Kever Yang50fb9982017-02-22 16:56:35 +08001434 writel(noc_timing->ddrtiminga0,
1435 &ddr_msch_regs->ddrtiminga0);
1436 writel(noc_timing->ddrtimingb0,
1437 &ddr_msch_regs->ddrtimingb0);
Jagan Teki5465f9b2019-07-16 17:27:05 +05301438 writel(noc_timing->ddrtimingc0.d32,
Kever Yang50fb9982017-02-22 16:56:35 +08001439 &ddr_msch_regs->ddrtimingc0);
1440 writel(noc_timing->devtodev0,
1441 &ddr_msch_regs->devtodev0);
Jagan Teki264a09f2019-07-16 17:27:06 +05301442 writel(noc_timing->ddrmode.d32,
Kever Yang50fb9982017-02-22 16:56:35 +08001443 &ddr_msch_regs->ddrmode);
1444
Jagan Tekib02c5482019-07-16 17:27:20 +05301445 /**
1446 * rank 1 memory clock disable (dfi_dram_clk_disable = 1)
1447 *
1448 * The hardware for LPDDR4 with
1449 * - CLK0P/N connect to lower 16-bits
1450 * - CLK1P/N connect to higher 16-bits
1451 *
1452 * dfi dram clk is configured via CLK1P/N, so disabling
1453 * dfi dram clk will disable the CLK1P/N as well for lpddr4.
1454 */
1455 if (params->ch[channel].cap_info.rank == 1 &&
1456 params->base.dramtype != LPDDR4)
Kever Yang50fb9982017-02-22 16:56:35 +08001457 setbits_le32(&dram->chan[channel].pctl->denali_ctl[276],
1458 1 << 17);
1459 }
1460
Jagan Teki2d337122019-07-16 17:27:00 +05301461 writel(sys_reg2, &dram->pmugrf->os_reg2);
Jagan Teki9d8769c2019-07-16 17:27:01 +05301462 writel(sys_reg3, &dram->pmugrf->os_reg3);
Kever Yang50fb9982017-02-22 16:56:35 +08001463 rk_clrsetreg(&dram->pmusgrf->soc_con4, 0x1f << 10,
Jagan Tekia58ff792019-07-15 23:50:58 +05301464 params->base.stride << 10);
Kever Yang50fb9982017-02-22 16:56:35 +08001465
1466 /* reboot hold register set */
1467 writel(PRESET_SGRF_HOLD(0) | PRESET_GPIO0_HOLD(1) |
1468 PRESET_GPIO1_HOLD(1),
1469 &dram->pmucru->pmucru_rstnhold_con[1]);
1470 clrsetbits_le32(&dram->cru->glb_rst_con, 0x3, 0x3);
1471}
1472
Jagan Tekicc117bb2019-07-16 17:27:31 +05301473#if !defined(CONFIG_RAM_RK3399_LPDDR4)
Jagan Teki9eb935a2019-07-16 17:27:30 +05301474static int default_data_training(struct dram_info *dram, u32 channel, u8 rank,
1475 struct rk3399_sdram_params *params)
1476{
1477 u8 training_flag = PI_READ_GATE_TRAINING;
1478
1479 /*
1480 * LPDDR3 CA training msut be trigger before
1481 * other training.
1482 * DDR3 is not have CA training.
1483 */
1484
1485 if (params->base.dramtype == LPDDR3)
1486 training_flag |= PI_CA_TRAINING;
1487
1488 return data_training(dram, channel, params, training_flag);
1489}
Jagan Tekicc117bb2019-07-16 17:27:31 +05301490#endif
Jagan Teki9eb935a2019-07-16 17:27:30 +05301491
Kever Yang50fb9982017-02-22 16:56:35 +08001492static int switch_to_phy_index1(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +05301493 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001494{
1495 u32 channel;
1496 u32 *denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +05301497 u32 ch_count = params->base.num_channels;
Kever Yang50fb9982017-02-22 16:56:35 +08001498 int ret;
1499 int i = 0;
1500
1501 writel(RK_CLRSETBITS(0x03 << 4 | 1 << 2 | 1,
1502 1 << 4 | 1 << 2 | 1),
1503 &dram->cic->cic_ctrl0);
1504 while (!(readl(&dram->cic->cic_status0) & (1 << 2))) {
1505 mdelay(10);
1506 i++;
1507 if (i > 10) {
1508 debug("index1 frequency change overtime\n");
1509 return -ETIME;
1510 }
1511 }
1512
1513 i = 0;
1514 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &dram->cic->cic_ctrl0);
1515 while (!(readl(&dram->cic->cic_status0) & (1 << 0))) {
1516 mdelay(10);
Heinrich Schuchardt80516592018-03-18 12:10:55 +01001517 i++;
Kever Yang50fb9982017-02-22 16:56:35 +08001518 if (i > 10) {
1519 debug("index1 frequency done overtime\n");
1520 return -ETIME;
1521 }
1522 }
1523
1524 for (channel = 0; channel < ch_count; channel++) {
1525 denali_phy = dram->chan[channel].publ->denali_phy;
1526 clrsetbits_le32(&denali_phy[896], (0x3 << 8) | 1, 1 << 8);
Jagan Teki5ff7abe2019-07-16 17:27:29 +05301527 ret = data_training(dram, channel, params, PI_FULL_TRAINING);
Jagan Teki6214ff22019-07-15 23:58:39 +05301528 if (ret < 0) {
Kever Yang50fb9982017-02-22 16:56:35 +08001529 debug("index1 training failed\n");
1530 return ret;
1531 }
1532 }
1533
1534 return 0;
1535}
1536
Jagan Tekicc117bb2019-07-16 17:27:31 +05301537#if defined(CONFIG_RAM_RK3399_LPDDR4)
1538static u32 get_ddr_stride(struct rk3399_pmusgrf_regs *pmusgrf)
1539{
1540 return ((readl(&pmusgrf->soc_con4) >> 10) & 0x1F);
1541}
1542
1543static void set_ddr_stride(struct rk3399_pmusgrf_regs *pmusgrf, u32 stride)
1544{
1545 rk_clrsetreg(&pmusgrf->soc_con4, 0x1f << 10, stride << 10);
1546}
1547
1548static void set_cap_relate_config(const struct chan_info *chan,
1549 struct rk3399_sdram_params *params,
1550 unsigned int channel)
1551{
1552 u32 *denali_ctl = chan->pctl->denali_ctl;
1553 u32 tmp;
1554 struct rk3399_msch_timings *noc_timing;
1555
1556 if (params->base.dramtype == LPDDR3) {
1557 tmp = (8 << params->ch[channel].cap_info.bw) /
1558 (8 << params->ch[channel].cap_info.dbw);
1559
1560 /**
1561 * memdata_ratio
1562 * 1 -> 0, 2 -> 1, 4 -> 2
1563 */
1564 clrsetbits_le32(&denali_ctl[197], 0x7,
1565 (tmp >> 1));
1566 clrsetbits_le32(&denali_ctl[198], 0x7 << 8,
1567 (tmp >> 1) << 8);
1568 }
1569
1570 noc_timing = &params->ch[channel].noc_timings;
1571
1572 /*
1573 * noc timing bw relate timing is 32 bit, and real bw is 16bit
1574 * actually noc reg is setting at function dram_all_config
1575 */
1576 if (params->ch[channel].cap_info.bw == 16 &&
1577 noc_timing->ddrmode.b.mwrsize == 2) {
1578 if (noc_timing->ddrmode.b.burstsize)
1579 noc_timing->ddrmode.b.burstsize -= 1;
1580 noc_timing->ddrmode.b.mwrsize -= 1;
1581 noc_timing->ddrtimingc0.b.burstpenalty *= 2;
1582 noc_timing->ddrtimingc0.b.wrtomwr *= 2;
1583 }
1584}
1585
1586static u32 calculate_ddrconfig(struct rk3399_sdram_params *params, u32 channel)
1587{
1588 unsigned int cs0_row = params->ch[channel].cap_info.cs0_row;
1589 unsigned int col = params->ch[channel].cap_info.col;
1590 unsigned int bw = params->ch[channel].cap_info.bw;
1591 u16 ddr_cfg_2_rbc[] = {
1592 /*
1593 * [6] highest bit col
1594 * [5:3] max row(14+n)
1595 * [2] insertion row
1596 * [1:0] col(9+n),col, data bus 32bit
1597 *
1598 * highbitcol, max_row, insertion_row, col
1599 */
1600 ((0 << 6) | (2 << 3) | (0 << 2) | 0), /* 0 */
1601 ((0 << 6) | (2 << 3) | (0 << 2) | 1), /* 1 */
1602 ((0 << 6) | (1 << 3) | (0 << 2) | 2), /* 2 */
1603 ((0 << 6) | (0 << 3) | (0 << 2) | 3), /* 3 */
1604 ((0 << 6) | (2 << 3) | (1 << 2) | 1), /* 4 */
1605 ((0 << 6) | (1 << 3) | (1 << 2) | 2), /* 5 */
1606 ((1 << 6) | (0 << 3) | (0 << 2) | 2), /* 6 */
1607 ((1 << 6) | (1 << 3) | (0 << 2) | 2), /* 7 */
1608 };
1609 u32 i;
1610
1611 col -= (bw == 2) ? 0 : 1;
1612 col -= 9;
1613
1614 for (i = 0; i < 4; i++) {
1615 if ((col == (ddr_cfg_2_rbc[i] & 0x3)) &&
1616 (cs0_row <= (((ddr_cfg_2_rbc[i] >> 3) & 0x7) + 14)))
1617 break;
1618 }
1619
1620 if (i >= 4)
1621 i = -EINVAL;
1622
1623 return i;
1624}
1625
1626/**
1627 * read mr_num mode register
1628 * rank = 1: cs0
1629 * rank = 2: cs1
1630 */
1631static int read_mr(struct rk3399_ddr_pctl_regs *ddr_pctl_regs, u32 rank,
1632 u32 mr_num, u32 *buf)
1633{
1634 s32 timeout = 100;
1635
1636 writel(((1 << 16) | (((rank == 2) ? 1 : 0) << 8) | mr_num) << 8,
1637 &ddr_pctl_regs->denali_ctl[118]);
1638
1639 while (0 == (readl(&ddr_pctl_regs->denali_ctl[203]) &
1640 ((1 << 21) | (1 << 12)))) {
1641 udelay(1);
1642
1643 if (timeout <= 0) {
1644 printf("%s: pctl timeout!\n", __func__);
1645 return -ETIMEDOUT;
1646 }
1647
1648 timeout--;
1649 }
1650
1651 if (!(readl(&ddr_pctl_regs->denali_ctl[203]) & (1 << 12))) {
1652 *buf = readl(&ddr_pctl_regs->denali_ctl[119]) & 0xFF;
1653 } else {
1654 printf("%s: read mr failed with 0x%x status\n", __func__,
1655 readl(&ddr_pctl_regs->denali_ctl[17]) & 0x3);
1656 *buf = 0;
1657 }
1658
1659 setbits_le32(&ddr_pctl_regs->denali_ctl[205], (1 << 21) | (1 << 12));
1660
1661 return 0;
1662}
1663
1664static int lpddr4_mr_detect(struct dram_info *dram, u32 channel, u8 rank,
1665 struct rk3399_sdram_params *params)
1666{
1667 u64 cs0_cap;
1668 u32 stride;
1669 u32 cs = 0, col = 0, bk = 0, bw = 0, row_3_4 = 0;
1670 u32 cs0_row = 0, cs1_row = 0, ddrconfig = 0;
1671 u32 mr5, mr12, mr14;
1672 struct chan_info *chan = &dram->chan[channel];
1673 struct rk3399_ddr_pctl_regs *ddr_pctl_regs = chan->pctl;
1674 void __iomem *addr = NULL;
1675 int ret = 0;
1676 u32 val;
1677
1678 stride = get_ddr_stride(dram->pmusgrf);
1679
1680 if (params->ch[channel].cap_info.col == 0) {
1681 ret = -EPERM;
1682 goto end;
1683 }
1684
1685 cs = params->ch[channel].cap_info.rank;
1686 col = params->ch[channel].cap_info.col;
1687 bk = params->ch[channel].cap_info.bk;
1688 bw = params->ch[channel].cap_info.bw;
1689 row_3_4 = params->ch[channel].cap_info.row_3_4;
1690 cs0_row = params->ch[channel].cap_info.cs0_row;
1691 cs1_row = params->ch[channel].cap_info.cs1_row;
1692 ddrconfig = params->ch[channel].cap_info.ddrconfig;
1693
1694 /* 2GB */
1695 params->ch[channel].cap_info.rank = 2;
1696 params->ch[channel].cap_info.col = 10;
1697 params->ch[channel].cap_info.bk = 3;
1698 params->ch[channel].cap_info.bw = 2;
1699 params->ch[channel].cap_info.row_3_4 = 0;
1700 params->ch[channel].cap_info.cs0_row = 15;
1701 params->ch[channel].cap_info.cs1_row = 15;
1702 params->ch[channel].cap_info.ddrconfig = 1;
1703
1704 set_memory_map(chan, channel, params);
1705 params->ch[channel].cap_info.ddrconfig =
1706 calculate_ddrconfig(params, channel);
1707 set_ddrconfig(chan, params, channel,
1708 params->ch[channel].cap_info.ddrconfig);
1709 set_cap_relate_config(chan, params, channel);
1710
1711 cs0_cap = (1 << (params->ch[channel].cap_info.bw
1712 + params->ch[channel].cap_info.col
1713 + params->ch[channel].cap_info.bk
1714 + params->ch[channel].cap_info.cs0_row));
1715
1716 if (params->ch[channel].cap_info.row_3_4)
1717 cs0_cap = cs0_cap * 3 / 4;
1718
1719 if (channel == 0)
1720 set_ddr_stride(dram->pmusgrf, 0x17);
1721 else
1722 set_ddr_stride(dram->pmusgrf, 0x18);
1723
1724 /* read and write data to DRAM, avoid be optimized by compiler. */
1725 if (rank == 1)
1726 addr = (void __iomem *)0x100;
1727 else if (rank == 2)
1728 addr = (void __iomem *)(cs0_cap + 0x100);
1729
1730 val = readl(addr);
1731 writel(val + 1, addr);
1732
1733 read_mr(ddr_pctl_regs, rank, 5, &mr5);
1734 read_mr(ddr_pctl_regs, rank, 12, &mr12);
1735 read_mr(ddr_pctl_regs, rank, 14, &mr14);
1736
1737 if (mr5 == 0 || mr12 != 0x4d || mr14 != 0x4d) {
1738 ret = -EINVAL;
1739 goto end;
1740 }
1741end:
1742 params->ch[channel].cap_info.rank = cs;
1743 params->ch[channel].cap_info.col = col;
1744 params->ch[channel].cap_info.bk = bk;
1745 params->ch[channel].cap_info.bw = bw;
1746 params->ch[channel].cap_info.row_3_4 = row_3_4;
1747 params->ch[channel].cap_info.cs0_row = cs0_row;
1748 params->ch[channel].cap_info.cs1_row = cs1_row;
1749 params->ch[channel].cap_info.ddrconfig = ddrconfig;
1750
1751 set_ddr_stride(dram->pmusgrf, stride);
1752
1753 return ret;
1754}
1755#endif /* CONFIG_RAM_RK3399_LPDDR4 */
1756
Jagan Teki2525fae2019-07-15 23:58:52 +05301757static unsigned char calculate_stride(struct rk3399_sdram_params *params)
1758{
1759 unsigned int stride = params->base.stride;
1760 unsigned int channel, chinfo = 0;
1761 unsigned int ch_cap[2] = {0, 0};
1762 u64 cap;
1763
1764 for (channel = 0; channel < 2; channel++) {
1765 unsigned int cs0_cap = 0;
1766 unsigned int cs1_cap = 0;
1767 struct sdram_cap_info *cap_info = &params->ch[channel].cap_info;
1768
1769 if (cap_info->col == 0)
1770 continue;
1771
1772 cs0_cap = (1 << (cap_info->cs0_row + cap_info->col +
1773 cap_info->bk + cap_info->bw - 20));
1774 if (cap_info->rank > 1)
1775 cs1_cap = cs0_cap >> (cap_info->cs0_row
1776 - cap_info->cs1_row);
1777 if (cap_info->row_3_4) {
1778 cs0_cap = cs0_cap * 3 / 4;
1779 cs1_cap = cs1_cap * 3 / 4;
1780 }
1781 ch_cap[channel] = cs0_cap + cs1_cap;
1782 chinfo |= 1 << channel;
1783 }
1784
Jagan Teki874dede2019-07-15 23:58:53 +05301785 /* stride calculation for 1 channel */
1786 if (params->base.num_channels == 1 && chinfo & 1)
1787 return 0x17; /* channel a */
1788
Jagan Teki2525fae2019-07-15 23:58:52 +05301789 /* stride calculation for 2 channels, default gstride type is 256B */
1790 if (ch_cap[0] == ch_cap[1]) {
1791 cap = ch_cap[0] + ch_cap[1];
1792 switch (cap) {
1793 /* 512MB */
1794 case 512:
1795 stride = 0;
1796 break;
1797 /* 1GB */
1798 case 1024:
1799 stride = 0x5;
1800 break;
1801 /*
1802 * 768MB + 768MB same as total 2GB memory
1803 * useful space: 0-768MB 1GB-1792MB
1804 */
1805 case 1536:
1806 /* 2GB */
1807 case 2048:
1808 stride = 0x9;
1809 break;
1810 /* 1536MB + 1536MB */
1811 case 3072:
1812 stride = 0x11;
1813 break;
1814 /* 4GB */
1815 case 4096:
1816 stride = 0xD;
1817 break;
1818 default:
1819 printf("%s: Unable to calculate stride for ", __func__);
1820 print_size((cap * (1 << 20)), " capacity\n");
1821 break;
1822 }
1823 }
1824
Jagan Teki8eed4a42019-07-15 23:58:55 +05301825 sdram_print_stride(stride);
1826
Jagan Teki2525fae2019-07-15 23:58:52 +05301827 return stride;
1828}
1829
Jagan Teki43485e12019-07-15 23:58:54 +05301830static void clear_channel_params(struct rk3399_sdram_params *params, u8 channel)
1831{
1832 params->ch[channel].cap_info.rank = 0;
1833 params->ch[channel].cap_info.col = 0;
1834 params->ch[channel].cap_info.bk = 0;
1835 params->ch[channel].cap_info.bw = 32;
1836 params->ch[channel].cap_info.dbw = 32;
1837 params->ch[channel].cap_info.row_3_4 = 0;
1838 params->ch[channel].cap_info.cs0_row = 0;
1839 params->ch[channel].cap_info.cs1_row = 0;
1840 params->ch[channel].cap_info.ddrconfig = 0;
1841}
1842
1843static int pctl_init(struct dram_info *dram, struct rk3399_sdram_params *params)
1844{
1845 int channel;
1846 int ret;
1847
1848 for (channel = 0; channel < 2; channel++) {
1849 const struct chan_info *chan = &dram->chan[channel];
1850 struct rk3399_cru *cru = dram->cru;
1851 struct rk3399_ddr_publ_regs *publ = chan->publ;
1852
1853 phy_pctrl_reset(cru, channel);
1854 phy_dll_bypass_set(publ, params->base.ddr_freq);
1855
1856 ret = pctl_cfg(dram, chan, channel, params);
1857 if (ret < 0) {
1858 printf("%s: pctl config failed\n", __func__);
1859 return ret;
1860 }
1861
1862 /* start to trigger initialization */
1863 pctl_start(dram, channel);
1864 }
1865
1866 return 0;
1867}
1868
Kever Yang50fb9982017-02-22 16:56:35 +08001869static int sdram_init(struct dram_info *dram,
Jagan Teki2525fae2019-07-15 23:58:52 +05301870 struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001871{
Jagan Tekia58ff792019-07-15 23:50:58 +05301872 unsigned char dramtype = params->base.dramtype;
1873 unsigned int ddr_freq = params->base.ddr_freq;
Jagan Teki43485e12019-07-15 23:58:54 +05301874 int channel, ch, rank;
Jagan Teki2ef77ed2019-07-15 23:50:59 +05301875 int ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001876
1877 debug("Starting SDRAM initialization...\n");
1878
Philipp Tomsich39dce4a2017-05-31 18:16:35 +02001879 if ((dramtype == DDR3 && ddr_freq > 933) ||
Kever Yang50fb9982017-02-22 16:56:35 +08001880 (dramtype == LPDDR3 && ddr_freq > 933) ||
1881 (dramtype == LPDDR4 && ddr_freq > 800)) {
1882 debug("SDRAM frequency is to high!");
1883 return -E2BIG;
1884 }
1885
Jagan Teki43485e12019-07-15 23:58:54 +05301886 for (ch = 0; ch < 2; ch++) {
1887 params->ch[ch].cap_info.rank = 2;
1888 for (rank = 2; rank != 0; rank--) {
1889 ret = pctl_init(dram, params);
1890 if (ret < 0) {
1891 printf("%s: pctl init failed\n", __func__);
1892 return ret;
1893 }
1894
1895 /* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */
1896 if (dramtype == LPDDR3)
1897 udelay(10);
1898
1899 params->ch[ch].cap_info.rank = rank;
1900
Jagan Teki9eb935a2019-07-16 17:27:30 +05301901 ret = dram->ops->data_training(dram, ch, rank, params);
1902 if (!ret) {
1903 debug("%s: data trained for rank %d, ch %d\n",
1904 __func__, rank, ch);
Jagan Teki43485e12019-07-15 23:58:54 +05301905 break;
Jagan Teki9eb935a2019-07-16 17:27:30 +05301906 }
Jagan Teki43485e12019-07-15 23:58:54 +05301907 }
1908 /* Computed rank with associated channel number */
1909 params->ch[ch].cap_info.rank = rank;
1910 }
1911
1912 params->base.num_channels = 0;
Kever Yang50fb9982017-02-22 16:56:35 +08001913 for (channel = 0; channel < 2; channel++) {
1914 const struct chan_info *chan = &dram->chan[channel];
Jagan Teki43485e12019-07-15 23:58:54 +05301915 struct sdram_cap_info *cap_info = &params->ch[channel].cap_info;
1916 u8 training_flag = PI_FULL_TRAINING;
Kever Yang50fb9982017-02-22 16:56:35 +08001917
Jagan Teki43485e12019-07-15 23:58:54 +05301918 if (cap_info->rank == 0) {
1919 clear_channel_params(params, channel);
Kever Yang50fb9982017-02-22 16:56:35 +08001920 continue;
Jagan Teki43485e12019-07-15 23:58:54 +05301921 } else {
1922 params->base.num_channels++;
Kever Yang50fb9982017-02-22 16:56:35 +08001923 }
1924
Jagan Teki43485e12019-07-15 23:58:54 +05301925 debug("Channel ");
1926 debug(channel ? "1: " : "0: ");
Jagan Tekic9151e22019-07-15 23:58:45 +05301927
Jagan Teki43485e12019-07-15 23:58:54 +05301928 /* LPDDR3 should have write and read gate training */
1929 if (params->base.dramtype == LPDDR3)
1930 training_flag = PI_WRITE_LEVELING |
1931 PI_READ_GATE_TRAINING;
Kever Yang50fb9982017-02-22 16:56:35 +08001932
Jagan Teki43485e12019-07-15 23:58:54 +05301933 if (params->base.dramtype != LPDDR4) {
1934 ret = data_training(dram, channel, params,
1935 training_flag);
1936 if (!ret) {
1937 debug("%s: data train failed for channel %d\n",
1938 __func__, ret);
1939 continue;
1940 }
Kever Yang50fb9982017-02-22 16:56:35 +08001941 }
1942
Jagan Teki8eed4a42019-07-15 23:58:55 +05301943 sdram_print_ddr_info(cap_info, &params->base);
1944
Jagan Teki43485e12019-07-15 23:58:54 +05301945 set_ddrconfig(chan, params, channel, cap_info->ddrconfig);
1946 }
1947
1948 if (params->base.num_channels == 0) {
1949 printf("%s: ", __func__);
Jagan Teki8eed4a42019-07-15 23:58:55 +05301950 sdram_print_dram_type(params->base.dramtype);
Jagan Teki43485e12019-07-15 23:58:54 +05301951 printf(" - %dMHz failed!\n", params->base.ddr_freq);
1952 return -EINVAL;
Kever Yang50fb9982017-02-22 16:56:35 +08001953 }
Jagan Teki2525fae2019-07-15 23:58:52 +05301954
1955 params->base.stride = calculate_stride(params);
Jagan Tekia58ff792019-07-15 23:50:58 +05301956 dram_all_config(dram, params);
1957 switch_to_phy_index1(dram, params);
Kever Yang50fb9982017-02-22 16:56:35 +08001958
1959 debug("Finish SDRAM initialization...\n");
1960 return 0;
1961}
1962
1963static int rk3399_dmc_ofdata_to_platdata(struct udevice *dev)
1964{
1965#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1966 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
Kever Yang50fb9982017-02-22 16:56:35 +08001967 int ret;
1968
Philipp Tomsich0250c232017-06-07 18:46:03 +02001969 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
1970 (u32 *)&plat->sdram_params,
1971 sizeof(plat->sdram_params) / sizeof(u32));
Kever Yang50fb9982017-02-22 16:56:35 +08001972 if (ret) {
1973 printf("%s: Cannot read rockchip,sdram-params %d\n",
1974 __func__, ret);
1975 return ret;
1976 }
Masahiro Yamadae4873e32018-04-19 12:14:03 +09001977 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001978 if (ret)
1979 printf("%s: regmap failed %d\n", __func__, ret);
1980
1981#endif
1982 return 0;
1983}
1984
1985#if CONFIG_IS_ENABLED(OF_PLATDATA)
1986static int conv_of_platdata(struct udevice *dev)
1987{
1988 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1989 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1990 int ret;
1991
1992 ret = regmap_init_mem_platdata(dev, dtplat->reg,
Jagan Tekif676c7c2019-07-15 23:50:56 +05301993 ARRAY_SIZE(dtplat->reg) / 2,
1994 &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001995 if (ret)
1996 return ret;
1997
1998 return 0;
1999}
2000#endif
2001
Jagan Teki9eb935a2019-07-16 17:27:30 +05302002static const struct sdram_rk3399_ops rk3399_ops = {
Jagan Tekicc117bb2019-07-16 17:27:31 +05302003#if !defined(CONFIG_RAM_RK3399_LPDDR4)
Jagan Teki9eb935a2019-07-16 17:27:30 +05302004 .data_training = default_data_training,
Jagan Tekicc117bb2019-07-16 17:27:31 +05302005#else
2006 .data_training = lpddr4_mr_detect,
2007#endif
Jagan Teki9eb935a2019-07-16 17:27:30 +05302008};
2009
Kever Yang50fb9982017-02-22 16:56:35 +08002010static int rk3399_dmc_init(struct udevice *dev)
2011{
2012 struct dram_info *priv = dev_get_priv(dev);
2013 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
2014 int ret;
2015#if !CONFIG_IS_ENABLED(OF_PLATDATA)
2016 struct rk3399_sdram_params *params = &plat->sdram_params;
2017#else
2018 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
2019 struct rk3399_sdram_params *params =
2020 (void *)dtplat->rockchip_sdram_params;
2021
2022 ret = conv_of_platdata(dev);
2023 if (ret)
2024 return ret;
2025#endif
2026
Jagan Teki9eb935a2019-07-16 17:27:30 +05302027 priv->ops = &rk3399_ops;
Kever Yang50fb9982017-02-22 16:56:35 +08002028 priv->cic = syscon_get_first_range(ROCKCHIP_SYSCON_CIC);
Jagan Tekic9151e22019-07-15 23:58:45 +05302029 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Kever Yang50fb9982017-02-22 16:56:35 +08002030 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
2031 priv->pmusgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
2032 priv->pmucru = rockchip_get_pmucru();
2033 priv->cru = rockchip_get_cru();
2034 priv->chan[0].pctl = regmap_get_range(plat->map, 0);
2035 priv->chan[0].pi = regmap_get_range(plat->map, 1);
2036 priv->chan[0].publ = regmap_get_range(plat->map, 2);
2037 priv->chan[0].msch = regmap_get_range(plat->map, 3);
2038 priv->chan[1].pctl = regmap_get_range(plat->map, 4);
2039 priv->chan[1].pi = regmap_get_range(plat->map, 5);
2040 priv->chan[1].publ = regmap_get_range(plat->map, 6);
2041 priv->chan[1].msch = regmap_get_range(plat->map, 7);
2042
2043 debug("con reg %p %p %p %p %p %p %p %p\n",
2044 priv->chan[0].pctl, priv->chan[0].pi,
2045 priv->chan[0].publ, priv->chan[0].msch,
2046 priv->chan[1].pctl, priv->chan[1].pi,
2047 priv->chan[1].publ, priv->chan[1].msch);
2048 debug("cru %p, cic %p, grf %p, sgrf %p, pmucru %p\n", priv->cru,
2049 priv->cic, priv->pmugrf, priv->pmusgrf, priv->pmucru);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05302050
Kever Yang50fb9982017-02-22 16:56:35 +08002051#if CONFIG_IS_ENABLED(OF_PLATDATA)
2052 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->ddr_clk);
2053#else
2054 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
2055#endif
2056 if (ret) {
2057 printf("%s clk get failed %d\n", __func__, ret);
2058 return ret;
2059 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05302060
Kever Yang50fb9982017-02-22 16:56:35 +08002061 ret = clk_set_rate(&priv->ddr_clk, params->base.ddr_freq * MHz);
2062 if (ret < 0) {
2063 printf("%s clk set failed %d\n", __func__, ret);
2064 return ret;
2065 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05302066
Kever Yang50fb9982017-02-22 16:56:35 +08002067 ret = sdram_init(priv, params);
2068 if (ret < 0) {
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05302069 printf("%s DRAM init failed %d\n", __func__, ret);
Kever Yang50fb9982017-02-22 16:56:35 +08002070 return ret;
2071 }
2072
2073 return 0;
2074}
2075#endif
2076
Kever Yang50fb9982017-02-22 16:56:35 +08002077static int rk3399_dmc_probe(struct udevice *dev)
2078{
Kever Yang7f347842019-04-01 17:20:53 +08002079#if defined(CONFIG_TPL_BUILD) || \
2080 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08002081 if (rk3399_dmc_init(dev))
2082 return 0;
2083#else
2084 struct dram_info *priv = dev_get_priv(dev);
2085
2086 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05302087 debug("%s: pmugrf = %p\n", __func__, priv->pmugrf);
Kever Yang6c15a542017-06-23 16:11:06 +08002088 priv->info.base = CONFIG_SYS_SDRAM_BASE;
Jagan Tekif676c7c2019-07-15 23:50:56 +05302089 priv->info.size =
2090 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg2);
Kever Yang50fb9982017-02-22 16:56:35 +08002091#endif
2092 return 0;
2093}
2094
2095static int rk3399_dmc_get_info(struct udevice *dev, struct ram_info *info)
2096{
2097 struct dram_info *priv = dev_get_priv(dev);
2098
Kever Yangea61d142017-04-19 16:01:14 +08002099 *info = priv->info;
Kever Yang50fb9982017-02-22 16:56:35 +08002100
2101 return 0;
2102}
2103
2104static struct ram_ops rk3399_dmc_ops = {
2105 .get_info = rk3399_dmc_get_info,
2106};
2107
Kever Yang50fb9982017-02-22 16:56:35 +08002108static const struct udevice_id rk3399_dmc_ids[] = {
2109 { .compatible = "rockchip,rk3399-dmc" },
2110 { }
2111};
2112
2113U_BOOT_DRIVER(dmc_rk3399) = {
2114 .name = "rockchip_rk3399_dmc",
2115 .id = UCLASS_RAM,
2116 .of_match = rk3399_dmc_ids,
2117 .ops = &rk3399_dmc_ops,
Kever Yang7f347842019-04-01 17:20:53 +08002118#if defined(CONFIG_TPL_BUILD) || \
2119 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08002120 .ofdata_to_platdata = rk3399_dmc_ofdata_to_platdata,
2121#endif
2122 .probe = rk3399_dmc_probe,
Kever Yang50fb9982017-02-22 16:56:35 +08002123 .priv_auto_alloc_size = sizeof(struct dram_info),
Kever Yang7f347842019-04-01 17:20:53 +08002124#if defined(CONFIG_TPL_BUILD) || \
2125 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08002126 .platdata_auto_alloc_size = sizeof(struct rockchip_dmc_plat),
2127#endif
2128};