blob: d67123e57ee89cb38a50ff5c44749a100443845e [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);
Jagan Teki940d1252019-07-16 17:27:39 +053077 int (*set_rate)(struct dram_info *dram,
78 const struct rk3399_sdram_params *params);
Jagan Teki9eb935a2019-07-16 17:27:30 +053079};
80
Kever Yang7f347842019-04-01 17:20:53 +080081#if defined(CONFIG_TPL_BUILD) || \
82 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +080083
84struct rockchip_dmc_plat {
85#if CONFIG_IS_ENABLED(OF_PLATDATA)
86 struct dtd_rockchip_rk3399_dmc dtplat;
87#else
88 struct rk3399_sdram_params sdram_params;
89#endif
90 struct regmap *map;
91};
92
Jagan Tekie3619d12019-07-16 17:27:21 +053093struct io_setting {
94 u32 mhz;
95 u32 mr5;
96 /* dram side */
97 u32 dq_odt;
98 u32 ca_odt;
99 u32 pdds;
100 u32 dq_vref;
101 u32 ca_vref;
102 /* phy side */
103 u32 rd_odt;
104 u32 wr_dq_drv;
105 u32 wr_ca_drv;
106 u32 wr_ckcs_drv;
107 u32 rd_odt_en;
108 u32 rd_vref;
109} lpddr4_io_setting[] = {
110 {
111 50 * MHz,
112 0,
113 /* dram side */
114 0, /* dq_odt; */
115 0, /* ca_odt; */
116 6, /* pdds; */
117 0x72, /* dq_vref; */
118 0x72, /* ca_vref; */
119 /* phy side */
120 PHY_DRV_ODT_HI_Z, /* rd_odt; */
121 PHY_DRV_ODT_40, /* wr_dq_drv; */
122 PHY_DRV_ODT_40, /* wr_ca_drv; */
123 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
124 0, /* rd_odt_en;*/
125 41, /* rd_vref; (unit %, range 3.3% - 48.7%) */
126 },
127 {
128 600 * MHz,
129 0,
130 /* dram side */
131 1, /* dq_odt; */
132 0, /* ca_odt; */
133 6, /* pdds; */
134 0x72, /* dq_vref; */
135 0x72, /* ca_vref; */
136 /* phy side */
137 PHY_DRV_ODT_HI_Z, /* rd_odt; */
138 PHY_DRV_ODT_48, /* wr_dq_drv; */
139 PHY_DRV_ODT_40, /* wr_ca_drv; */
140 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
141 0, /* rd_odt_en; */
142 32, /* rd_vref; (unit %, range 3.3% - 48.7%) */
143 },
144 {
145 800 * MHz,
146 0,
147 /* dram side */
148 1, /* dq_odt; */
149 0, /* ca_odt; */
150 1, /* pdds; */
151 0x72, /* dq_vref; */
152 0x72, /* ca_vref; */
153 /* phy side */
154 PHY_DRV_ODT_40, /* rd_odt; */
155 PHY_DRV_ODT_48, /* wr_dq_drv; */
156 PHY_DRV_ODT_40, /* wr_ca_drv; */
157 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
158 1, /* rd_odt_en; */
159 17, /* rd_vref; (unit %, range 3.3% - 48.7%) */
160 },
161 {
162 933 * MHz,
163 0,
164 /* dram side */
165 3, /* dq_odt; */
166 0, /* ca_odt; */
167 6, /* pdds; */
168 0x59, /* dq_vref; 32% */
169 0x72, /* ca_vref; */
170 /* phy side */
171 PHY_DRV_ODT_HI_Z, /* rd_odt; */
172 PHY_DRV_ODT_48, /* wr_dq_drv; */
173 PHY_DRV_ODT_40, /* wr_ca_drv; */
174 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
175 0, /* rd_odt_en; */
176 32, /* rd_vref; (unit %, range 3.3% - 48.7%) */
177 },
178 {
179 1066 * MHz,
180 0,
181 /* dram side */
182 6, /* dq_odt; */
183 0, /* ca_odt; */
184 1, /* pdds; */
185 0x10, /* dq_vref; */
186 0x72, /* ca_vref; */
187 /* phy side */
188 PHY_DRV_ODT_40, /* rd_odt; */
189 PHY_DRV_ODT_60, /* wr_dq_drv; */
190 PHY_DRV_ODT_40, /* wr_ca_drv; */
191 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
192 1, /* rd_odt_en; */
193 17, /* rd_vref; (unit %, range 3.3% - 48.7%) */
194 },
195};
196
Jagan Tekid33056b2019-07-16 17:27:22 +0530197/**
198 * phy = 0, PHY boot freq
199 * phy = 1, PHY index 0
200 * phy = 2, PHY index 1
201 */
202static struct io_setting *
203lpddr4_get_io_settings(const struct rk3399_sdram_params *params, u32 mr5)
204{
205 struct io_setting *io = NULL;
206 u32 n;
207
208 for (n = 0; n < ARRAY_SIZE(lpddr4_io_setting); n++) {
209 io = &lpddr4_io_setting[n];
210
211 if (io->mr5 != 0) {
212 if (io->mhz >= params->base.ddr_freq &&
213 io->mr5 == mr5)
214 break;
215 } else {
216 if (io->mhz >= params->base.ddr_freq)
217 break;
218 }
219 }
220
221 return io;
222}
223
Jagan Tekic9151e22019-07-15 23:58:45 +0530224static void *get_ddrc0_con(struct dram_info *dram, u8 channel)
225{
226 return (channel == 0) ? &dram->grf->ddrc0_con0 : &dram->grf->ddrc0_con1;
227}
228
Kever Yang50fb9982017-02-22 16:56:35 +0800229static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
230{
231 int i;
232
233 for (i = 0; i < n / sizeof(u32); i++) {
234 writel(*src, dest);
235 src++;
236 dest++;
237 }
238}
239
Jagan Tekice75cfb2019-07-15 23:58:43 +0530240static void rkclk_ddr_reset(struct rk3399_cru *cru, u32 channel, u32 ctl,
241 u32 phy)
242{
243 channel &= 0x1;
244 ctl &= 0x1;
245 phy &= 0x1;
246 writel(CRU_SFTRST_DDR_CTRL(channel, ctl) |
247 CRU_SFTRST_DDR_PHY(channel, phy),
248 &cru->softrst_con[4]);
249}
250
251static void phy_pctrl_reset(struct rk3399_cru *cru, u32 channel)
252{
253 rkclk_ddr_reset(cru, channel, 1, 1);
254 udelay(10);
255
256 rkclk_ddr_reset(cru, channel, 1, 0);
257 udelay(10);
258
259 rkclk_ddr_reset(cru, channel, 0, 0);
260 udelay(10);
261}
262
Kever Yang50fb9982017-02-22 16:56:35 +0800263static void phy_dll_bypass_set(struct rk3399_ddr_publ_regs *ddr_publ_regs,
264 u32 freq)
265{
266 u32 *denali_phy = ddr_publ_regs->denali_phy;
267
268 /* From IP spec, only freq small than 125 can enter dll bypass mode */
269 if (freq <= 125) {
270 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
271 setbits_le32(&denali_phy[86], (0x3 << 2) << 8);
272 setbits_le32(&denali_phy[214], (0x3 << 2) << 8);
273 setbits_le32(&denali_phy[342], (0x3 << 2) << 8);
274 setbits_le32(&denali_phy[470], (0x3 << 2) << 8);
275
276 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
277 setbits_le32(&denali_phy[547], (0x3 << 2) << 16);
278 setbits_le32(&denali_phy[675], (0x3 << 2) << 16);
279 setbits_le32(&denali_phy[803], (0x3 << 2) << 16);
280 } else {
281 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
282 clrbits_le32(&denali_phy[86], (0x3 << 2) << 8);
283 clrbits_le32(&denali_phy[214], (0x3 << 2) << 8);
284 clrbits_le32(&denali_phy[342], (0x3 << 2) << 8);
285 clrbits_le32(&denali_phy[470], (0x3 << 2) << 8);
286
287 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
288 clrbits_le32(&denali_phy[547], (0x3 << 2) << 16);
289 clrbits_le32(&denali_phy[675], (0x3 << 2) << 16);
290 clrbits_le32(&denali_phy[803], (0x3 << 2) << 16);
291 }
292}
293
294static void set_memory_map(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530295 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800296{
Jagan Tekia58ff792019-07-15 23:50:58 +0530297 const struct rk3399_sdram_channel *sdram_ch = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +0800298 u32 *denali_ctl = chan->pctl->denali_ctl;
299 u32 *denali_pi = chan->pi->denali_pi;
300 u32 cs_map;
301 u32 reduc;
302 u32 row;
303
304 /* Get row number from ddrconfig setting */
Jagan Teki97867c82019-07-15 23:51:05 +0530305 if (sdram_ch->cap_info.ddrconfig < 2 ||
306 sdram_ch->cap_info.ddrconfig == 4)
Kever Yang50fb9982017-02-22 16:56:35 +0800307 row = 16;
Jagan Teki97867c82019-07-15 23:51:05 +0530308 else if (sdram_ch->cap_info.ddrconfig == 3)
Kever Yang50fb9982017-02-22 16:56:35 +0800309 row = 14;
310 else
311 row = 15;
312
Jagan Teki97867c82019-07-15 23:51:05 +0530313 cs_map = (sdram_ch->cap_info.rank > 1) ? 3 : 1;
314 reduc = (sdram_ch->cap_info.bw == 2) ? 0 : 1;
Kever Yang50fb9982017-02-22 16:56:35 +0800315
316 /* Set the dram configuration to ctrl */
Jagan Teki97867c82019-07-15 23:51:05 +0530317 clrsetbits_le32(&denali_ctl[191], 0xF, (12 - sdram_ch->cap_info.col));
Kever Yang50fb9982017-02-22 16:56:35 +0800318 clrsetbits_le32(&denali_ctl[190], (0x3 << 16) | (0x7 << 24),
Jagan Teki97867c82019-07-15 23:51:05 +0530319 ((3 - sdram_ch->cap_info.bk) << 16) |
Kever Yang50fb9982017-02-22 16:56:35 +0800320 ((16 - row) << 24));
321
322 clrsetbits_le32(&denali_ctl[196], 0x3 | (1 << 16),
323 cs_map | (reduc << 16));
324
325 /* PI_199 PI_COL_DIFF:RW:0:4 */
Jagan Teki97867c82019-07-15 23:51:05 +0530326 clrsetbits_le32(&denali_pi[199], 0xF, (12 - sdram_ch->cap_info.col));
Kever Yang50fb9982017-02-22 16:56:35 +0800327
328 /* PI_155 PI_ROW_DIFF:RW:24:3 PI_BANK_DIFF:RW:16:2 */
329 clrsetbits_le32(&denali_pi[155], (0x3 << 16) | (0x7 << 24),
Jagan Teki97867c82019-07-15 23:51:05 +0530330 ((3 - sdram_ch->cap_info.bk) << 16) |
Kever Yang50fb9982017-02-22 16:56:35 +0800331 ((16 - row) << 24));
Jagan Teki9337cb32019-07-16 17:27:18 +0530332
333 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
334 if (cs_map == 1)
335 cs_map = 0x5;
336 else if (cs_map == 2)
337 cs_map = 0xa;
338 else
339 cs_map = 0xF;
340 }
341
Kever Yang50fb9982017-02-22 16:56:35 +0800342 /* PI_41 PI_CS_MAP:RW:24:4 */
343 clrsetbits_le32(&denali_pi[41], 0xf << 24, cs_map << 24);
Jagan Teki97867c82019-07-15 23:51:05 +0530344 if (sdram_ch->cap_info.rank == 1 && params->base.dramtype == DDR3)
Kever Yang50fb9982017-02-22 16:56:35 +0800345 writel(0x2EC7FFFF, &denali_pi[34]);
346}
347
Jagan Tekib5d46632019-07-16 17:27:07 +0530348static int phy_io_config(const struct chan_info *chan,
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530349 const struct rk3399_sdram_params *params, u32 mr5)
Jagan Tekib5d46632019-07-16 17:27:07 +0530350{
351 u32 *denali_phy = chan->publ->denali_phy;
352 u32 vref_mode_dq, vref_value_dq, vref_mode_ac, vref_value_ac;
353 u32 mode_sel;
354 u32 reg_value;
355 u32 drv_value, odt_value;
356 u32 speed;
357
Jagan Teki59a9a572019-07-16 17:27:27 +0530358 /* vref setting & mode setting */
Jagan Tekib5d46632019-07-16 17:27:07 +0530359 if (params->base.dramtype == LPDDR4) {
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530360 struct io_setting *io = lpddr4_get_io_settings(params, mr5);
361 u32 rd_vref = io->rd_vref * 1000;
362
363 if (rd_vref < 36700) {
364 /* MODE_LV[2:0] = LPDDR4 (Range 2)*/
365 vref_mode_dq = 0x7;
Jagan Teki59a9a572019-07-16 17:27:27 +0530366 /* MODE[2:0]= LPDDR4 Range 2(0.4*VDDQ) */
367 mode_sel = 0x5;
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530368 vref_value_dq = (rd_vref - 3300) / 521;
369 } else {
370 /* MODE_LV[2:0] = LPDDR4 (Range 1)*/
371 vref_mode_dq = 0x6;
Jagan Teki59a9a572019-07-16 17:27:27 +0530372 /* MODE[2:0]= LPDDR4 Range 1(0.33*VDDQ) */
373 mode_sel = 0x4;
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530374 vref_value_dq = (rd_vref - 15300) / 521;
375 }
Jagan Tekib5d46632019-07-16 17:27:07 +0530376 vref_mode_ac = 0x6;
Jagan Tekia5b07192019-07-16 17:27:28 +0530377 /* VDDQ/3/2=16.8% */
378 vref_value_ac = 0x3;
Jagan Tekib5d46632019-07-16 17:27:07 +0530379 } else if (params->base.dramtype == LPDDR3) {
380 if (params->base.odt == 1) {
381 vref_mode_dq = 0x5; /* LPDDR3 ODT */
382 drv_value = (readl(&denali_phy[6]) >> 12) & 0xf;
383 odt_value = (readl(&denali_phy[6]) >> 4) & 0xf;
384 if (drv_value == PHY_DRV_ODT_48) {
385 switch (odt_value) {
386 case PHY_DRV_ODT_240:
387 vref_value_dq = 0x16;
388 break;
389 case PHY_DRV_ODT_120:
390 vref_value_dq = 0x26;
391 break;
392 case PHY_DRV_ODT_60:
393 vref_value_dq = 0x36;
394 break;
395 default:
396 debug("Invalid ODT value.\n");
397 return -EINVAL;
398 }
399 } else if (drv_value == PHY_DRV_ODT_40) {
400 switch (odt_value) {
401 case PHY_DRV_ODT_240:
402 vref_value_dq = 0x19;
403 break;
404 case PHY_DRV_ODT_120:
405 vref_value_dq = 0x23;
406 break;
407 case PHY_DRV_ODT_60:
408 vref_value_dq = 0x31;
409 break;
410 default:
411 debug("Invalid ODT value.\n");
412 return -EINVAL;
413 }
414 } else if (drv_value == PHY_DRV_ODT_34_3) {
415 switch (odt_value) {
416 case PHY_DRV_ODT_240:
417 vref_value_dq = 0x17;
418 break;
419 case PHY_DRV_ODT_120:
420 vref_value_dq = 0x20;
421 break;
422 case PHY_DRV_ODT_60:
423 vref_value_dq = 0x2e;
424 break;
425 default:
426 debug("Invalid ODT value.\n");
427 return -EINVAL;
428 }
429 } else {
430 debug("Invalid DRV value.\n");
431 return -EINVAL;
432 }
433 } else {
434 vref_mode_dq = 0x2; /* LPDDR3 */
435 vref_value_dq = 0x1f;
436 }
437 vref_mode_ac = 0x2;
438 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530439 mode_sel = 0x0;
Jagan Tekib5d46632019-07-16 17:27:07 +0530440 } else if (params->base.dramtype == DDR3) {
441 /* DDR3L */
442 vref_mode_dq = 0x1;
443 vref_value_dq = 0x1f;
444 vref_mode_ac = 0x1;
445 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530446 mode_sel = 0x1;
Jagan Tekib5d46632019-07-16 17:27:07 +0530447 } else {
448 debug("Unknown DRAM type.\n");
449 return -EINVAL;
450 }
451
452 reg_value = (vref_mode_dq << 9) | (0x1 << 8) | vref_value_dq;
453
454 /* PHY_913 PHY_PAD_VREF_CTRL_DQ_0 12bits offset_8 */
455 clrsetbits_le32(&denali_phy[913], 0xfff << 8, reg_value << 8);
456 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_1 12bits offset_0 */
457 clrsetbits_le32(&denali_phy[914], 0xfff, reg_value);
458 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_2 12bits offset_16 */
459 clrsetbits_le32(&denali_phy[914], 0xfff << 16, reg_value << 16);
460 /* PHY_915 PHY_PAD_VREF_CTRL_DQ_3 12bits offset_0 */
461 clrsetbits_le32(&denali_phy[915], 0xfff, reg_value);
462
463 reg_value = (vref_mode_ac << 9) | (0x1 << 8) | vref_value_ac;
464
465 /* PHY_915 PHY_PAD_VREF_CTRL_AC 12bits offset_16 */
466 clrsetbits_le32(&denali_phy[915], 0xfff << 16, reg_value << 16);
467
Jagan Tekib5d46632019-07-16 17:27:07 +0530468 /* PHY_924 PHY_PAD_FDBK_DRIVE */
469 clrsetbits_le32(&denali_phy[924], 0x7 << 15, mode_sel << 15);
470 /* PHY_926 PHY_PAD_DATA_DRIVE */
471 clrsetbits_le32(&denali_phy[926], 0x7 << 6, mode_sel << 6);
472 /* PHY_927 PHY_PAD_DQS_DRIVE */
473 clrsetbits_le32(&denali_phy[927], 0x7 << 6, mode_sel << 6);
474 /* PHY_928 PHY_PAD_ADDR_DRIVE */
475 clrsetbits_le32(&denali_phy[928], 0x7 << 14, mode_sel << 14);
476 /* PHY_929 PHY_PAD_CLK_DRIVE */
477 clrsetbits_le32(&denali_phy[929], 0x7 << 14, mode_sel << 14);
478 /* PHY_935 PHY_PAD_CKE_DRIVE */
479 clrsetbits_le32(&denali_phy[935], 0x7 << 14, mode_sel << 14);
480 /* PHY_937 PHY_PAD_RST_DRIVE */
481 clrsetbits_le32(&denali_phy[937], 0x7 << 14, mode_sel << 14);
482 /* PHY_939 PHY_PAD_CS_DRIVE */
483 clrsetbits_le32(&denali_phy[939], 0x7 << 14, mode_sel << 14);
484
Jagan Teki5d152172019-07-16 17:27:15 +0530485 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
486 /* BOOSTP_EN & BOOSTN_EN */
487 reg_value = ((PHY_BOOSTP_EN << 4) | PHY_BOOSTN_EN);
488 /* PHY_925 PHY_PAD_FDBK_DRIVE2 */
489 clrsetbits_le32(&denali_phy[925], 0xff << 8, reg_value << 8);
490 /* PHY_926 PHY_PAD_DATA_DRIVE */
491 clrsetbits_le32(&denali_phy[926], 0xff << 12, reg_value << 12);
492 /* PHY_927 PHY_PAD_DQS_DRIVE */
493 clrsetbits_le32(&denali_phy[927], 0xff << 14, reg_value << 14);
494 /* PHY_928 PHY_PAD_ADDR_DRIVE */
495 clrsetbits_le32(&denali_phy[928], 0xff << 20, reg_value << 20);
496 /* PHY_929 PHY_PAD_CLK_DRIVE */
497 clrsetbits_le32(&denali_phy[929], 0xff << 22, reg_value << 22);
498 /* PHY_935 PHY_PAD_CKE_DRIVE */
499 clrsetbits_le32(&denali_phy[935], 0xff << 20, reg_value << 20);
500 /* PHY_937 PHY_PAD_RST_DRIVE */
501 clrsetbits_le32(&denali_phy[937], 0xff << 20, reg_value << 20);
502 /* PHY_939 PHY_PAD_CS_DRIVE */
503 clrsetbits_le32(&denali_phy[939], 0xff << 20, reg_value << 20);
Jagan Tekid8681842019-07-16 17:27:16 +0530504
505 /* SLEWP_EN & SLEWN_EN */
506 reg_value = ((PHY_SLEWP_EN << 3) | PHY_SLEWN_EN);
507 /* PHY_924 PHY_PAD_FDBK_DRIVE */
508 clrsetbits_le32(&denali_phy[924], 0x3f << 8, reg_value << 8);
509 /* PHY_926 PHY_PAD_DATA_DRIVE */
510 clrsetbits_le32(&denali_phy[926], 0x3f, reg_value);
511 /* PHY_927 PHY_PAD_DQS_DRIVE */
512 clrsetbits_le32(&denali_phy[927], 0x3f, reg_value);
513 /* PHY_928 PHY_PAD_ADDR_DRIVE */
514 clrsetbits_le32(&denali_phy[928], 0x3f << 8, reg_value << 8);
515 /* PHY_929 PHY_PAD_CLK_DRIVE */
516 clrsetbits_le32(&denali_phy[929], 0x3f << 8, reg_value << 8);
517 /* PHY_935 PHY_PAD_CKE_DRIVE */
518 clrsetbits_le32(&denali_phy[935], 0x3f << 8, reg_value << 8);
519 /* PHY_937 PHY_PAD_RST_DRIVE */
520 clrsetbits_le32(&denali_phy[937], 0x3f << 8, reg_value << 8);
521 /* PHY_939 PHY_PAD_CS_DRIVE */
522 clrsetbits_le32(&denali_phy[939], 0x3f << 8, reg_value << 8);
Jagan Teki5d152172019-07-16 17:27:15 +0530523 }
524
Jagan Tekib5d46632019-07-16 17:27:07 +0530525 /* speed setting */
526 if (params->base.ddr_freq < 400)
527 speed = 0x0;
528 else if (params->base.ddr_freq < 800)
529 speed = 0x1;
530 else if (params->base.ddr_freq < 1200)
531 speed = 0x2;
532 else
533 speed = 0x3;
534
535 /* PHY_924 PHY_PAD_FDBK_DRIVE */
536 clrsetbits_le32(&denali_phy[924], 0x3 << 21, speed << 21);
537 /* PHY_926 PHY_PAD_DATA_DRIVE */
538 clrsetbits_le32(&denali_phy[926], 0x3 << 9, speed << 9);
539 /* PHY_927 PHY_PAD_DQS_DRIVE */
540 clrsetbits_le32(&denali_phy[927], 0x3 << 9, speed << 9);
541 /* PHY_928 PHY_PAD_ADDR_DRIVE */
542 clrsetbits_le32(&denali_phy[928], 0x3 << 17, speed << 17);
543 /* PHY_929 PHY_PAD_CLK_DRIVE */
544 clrsetbits_le32(&denali_phy[929], 0x3 << 17, speed << 17);
545 /* PHY_935 PHY_PAD_CKE_DRIVE */
546 clrsetbits_le32(&denali_phy[935], 0x3 << 17, speed << 17);
547 /* PHY_937 PHY_PAD_RST_DRIVE */
548 clrsetbits_le32(&denali_phy[937], 0x3 << 17, speed << 17);
549 /* PHY_939 PHY_PAD_CS_DRIVE */
550 clrsetbits_le32(&denali_phy[939], 0x3 << 17, speed << 17);
551
Jagan Teki65535a22019-07-16 17:27:17 +0530552 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
553 /* RX_CM_INPUT */
554 reg_value = PHY_RX_CM_INPUT;
555 /* PHY_924 PHY_PAD_FDBK_DRIVE */
556 clrsetbits_le32(&denali_phy[924], 0x1 << 14, reg_value << 14);
557 /* PHY_926 PHY_PAD_DATA_DRIVE */
558 clrsetbits_le32(&denali_phy[926], 0x1 << 11, reg_value << 11);
559 /* PHY_927 PHY_PAD_DQS_DRIVE */
560 clrsetbits_le32(&denali_phy[927], 0x1 << 13, reg_value << 13);
561 /* PHY_928 PHY_PAD_ADDR_DRIVE */
562 clrsetbits_le32(&denali_phy[928], 0x1 << 19, reg_value << 19);
563 /* PHY_929 PHY_PAD_CLK_DRIVE */
564 clrsetbits_le32(&denali_phy[929], 0x1 << 21, reg_value << 21);
565 /* PHY_935 PHY_PAD_CKE_DRIVE */
566 clrsetbits_le32(&denali_phy[935], 0x1 << 19, reg_value << 19);
567 /* PHY_937 PHY_PAD_RST_DRIVE */
568 clrsetbits_le32(&denali_phy[937], 0x1 << 19, reg_value << 19);
569 /* PHY_939 PHY_PAD_CS_DRIVE */
570 clrsetbits_le32(&denali_phy[939], 0x1 << 19, reg_value << 19);
571 }
572
Jagan Tekib5d46632019-07-16 17:27:07 +0530573 return 0;
574}
575
Kever Yang50fb9982017-02-22 16:56:35 +0800576static void set_ds_odt(const struct chan_info *chan,
Jagan Tekid33056b2019-07-16 17:27:22 +0530577 const struct rk3399_sdram_params *params, u32 mr5)
Kever Yang50fb9982017-02-22 16:56:35 +0800578{
579 u32 *denali_phy = chan->publ->denali_phy;
Jagan Teki0cb31122019-07-16 17:27:24 +0530580 u32 *denali_ctl = chan->pctl->denali_ctl;
Kever Yang50fb9982017-02-22 16:56:35 +0800581 u32 tsel_idle_en, tsel_wr_en, tsel_rd_en;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530582 u32 tsel_idle_select_p, tsel_rd_select_p;
583 u32 tsel_idle_select_n, tsel_rd_select_n;
584 u32 tsel_wr_select_dq_p, tsel_wr_select_ca_p;
585 u32 tsel_wr_select_dq_n, tsel_wr_select_ca_n;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530586 u32 tsel_ckcs_select_p, tsel_ckcs_select_n;
Jagan Tekid33056b2019-07-16 17:27:22 +0530587 struct io_setting *io = NULL;
Jagan Teki0cb31122019-07-16 17:27:24 +0530588 u32 soc_odt = 0;
Kever Yang50fb9982017-02-22 16:56:35 +0800589 u32 reg_value;
590
Jagan Tekia58ff792019-07-15 23:50:58 +0530591 if (params->base.dramtype == LPDDR4) {
Jagan Tekid33056b2019-07-16 17:27:22 +0530592 io = lpddr4_get_io_settings(params, mr5);
593
Jagan Tekif676c7c2019-07-15 23:50:56 +0530594 tsel_rd_select_p = PHY_DRV_ODT_HI_Z;
Jagan Tekid33056b2019-07-16 17:27:22 +0530595 tsel_rd_select_n = io->rd_odt;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530596
Jagan Tekif676c7c2019-07-15 23:50:56 +0530597 tsel_idle_select_p = PHY_DRV_ODT_HI_Z;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530598 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800599
Jagan Tekid33056b2019-07-16 17:27:22 +0530600 tsel_wr_select_dq_p = io->wr_dq_drv;
Jagan Teki36667142019-07-15 23:51:00 +0530601 tsel_wr_select_dq_n = PHY_DRV_ODT_40;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530602
Jagan Tekid33056b2019-07-16 17:27:22 +0530603 tsel_wr_select_ca_p = io->wr_ca_drv;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530604 tsel_wr_select_ca_n = PHY_DRV_ODT_40;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530605
606 tsel_ckcs_select_p = io->wr_ckcs_drv;
607 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
Jagan Teki0cb31122019-07-16 17:27:24 +0530608 switch (tsel_rd_select_n) {
609 case PHY_DRV_ODT_240:
610 soc_odt = 1;
611 break;
612 case PHY_DRV_ODT_120:
613 soc_odt = 2;
614 break;
615 case PHY_DRV_ODT_80:
616 soc_odt = 3;
617 break;
618 case PHY_DRV_ODT_60:
619 soc_odt = 4;
620 break;
621 case PHY_DRV_ODT_48:
622 soc_odt = 5;
623 break;
624 case PHY_DRV_ODT_40:
625 soc_odt = 6;
626 break;
627 case PHY_DRV_ODT_34_3:
628 soc_odt = 6;
629 printf("%s: Unable to support LPDDR4 MR22 Soc ODT\n",
630 __func__);
631 break;
632 case PHY_DRV_ODT_HI_Z:
633 default:
634 soc_odt = 0;
635 break;
636 }
Jagan Tekia58ff792019-07-15 23:50:58 +0530637 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +0800638 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530639 tsel_rd_select_n = PHY_DRV_ODT_HI_Z;
640
Kever Yang50fb9982017-02-22 16:56:35 +0800641 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530642 tsel_idle_select_n = PHY_DRV_ODT_HI_Z;
Kever Yang50fb9982017-02-22 16:56:35 +0800643
Jagan Teki5c3251f2019-07-15 23:51:04 +0530644 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530645 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530646
647 tsel_wr_select_ca_p = PHY_DRV_ODT_48;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530648 tsel_wr_select_ca_n = PHY_DRV_ODT_48;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530649
650 tsel_ckcs_select_p = PHY_DRV_ODT_34_3;
651 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
Kever Yang50fb9982017-02-22 16:56:35 +0800652 } else {
653 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530654 tsel_rd_select_n = PHY_DRV_ODT_240;
655
Kever Yang50fb9982017-02-22 16:56:35 +0800656 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530657 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800658
Jagan Teki5c3251f2019-07-15 23:51:04 +0530659 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530660 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530661
662 tsel_wr_select_ca_p = PHY_DRV_ODT_34_3;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530663 tsel_wr_select_ca_n = PHY_DRV_ODT_34_3;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530664
665 tsel_ckcs_select_p = PHY_DRV_ODT_34_3;
666 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
Kever Yang50fb9982017-02-22 16:56:35 +0800667 }
668
Jagan Tekib9584172019-07-16 17:27:25 +0530669 if (params->base.odt == 1) {
Kever Yang50fb9982017-02-22 16:56:35 +0800670 tsel_rd_en = 1;
Jagan Tekib9584172019-07-16 17:27:25 +0530671
672 if (params->base.dramtype == LPDDR4)
673 tsel_rd_en = io->rd_odt_en;
674 } else {
Kever Yang50fb9982017-02-22 16:56:35 +0800675 tsel_rd_en = 0;
Jagan Tekib9584172019-07-16 17:27:25 +0530676 }
Kever Yang50fb9982017-02-22 16:56:35 +0800677
678 tsel_wr_en = 0;
679 tsel_idle_en = 0;
680
Jagan Teki0cb31122019-07-16 17:27:24 +0530681 /* F0_0 */
682 clrsetbits_le32(&denali_ctl[145], 0xFF << 16,
683 (soc_odt | (CS0_MR22_VAL << 3)) << 16);
684 /* F2_0, F1_0 */
685 clrsetbits_le32(&denali_ctl[146], 0xFF00FF,
686 ((soc_odt | (CS0_MR22_VAL << 3)) << 16) |
687 (soc_odt | (CS0_MR22_VAL << 3)));
688 /* F0_1 */
689 clrsetbits_le32(&denali_ctl[159], 0xFF << 16,
690 (soc_odt | (CS1_MR22_VAL << 3)) << 16);
691 /* F2_1, F1_1 */
692 clrsetbits_le32(&denali_ctl[160], 0xFF00FF,
693 ((soc_odt | (CS1_MR22_VAL << 3)) << 16) |
694 (soc_odt | (CS1_MR22_VAL << 3)));
695
Kever Yang50fb9982017-02-22 16:56:35 +0800696 /*
697 * phy_dq_tsel_select_X 24bits DENALI_PHY_6/134/262/390 offset_0
698 * sets termination values for read/idle cycles and drive strength
699 * for write cycles for DQ/DM
700 */
701 reg_value = tsel_rd_select_n | (tsel_rd_select_p << 0x4) |
Jagan Tekib3b34392019-07-15 23:51:01 +0530702 (tsel_wr_select_dq_n << 8) | (tsel_wr_select_dq_p << 12) |
Kever Yang50fb9982017-02-22 16:56:35 +0800703 (tsel_idle_select_n << 16) | (tsel_idle_select_p << 20);
704 clrsetbits_le32(&denali_phy[6], 0xffffff, reg_value);
705 clrsetbits_le32(&denali_phy[134], 0xffffff, reg_value);
706 clrsetbits_le32(&denali_phy[262], 0xffffff, reg_value);
707 clrsetbits_le32(&denali_phy[390], 0xffffff, reg_value);
708
709 /*
710 * phy_dqs_tsel_select_X 24bits DENALI_PHY_7/135/263/391 offset_0
711 * sets termination values for read/idle cycles and drive strength
712 * for write cycles for DQS
713 */
714 clrsetbits_le32(&denali_phy[7], 0xffffff, reg_value);
715 clrsetbits_le32(&denali_phy[135], 0xffffff, reg_value);
716 clrsetbits_le32(&denali_phy[263], 0xffffff, reg_value);
717 clrsetbits_le32(&denali_phy[391], 0xffffff, reg_value);
718
719 /* phy_adr_tsel_select_ 8bits DENALI_PHY_544/672/800 offset_0 */
Jagan Teki7caa3e92019-07-15 23:51:03 +0530720 reg_value = tsel_wr_select_ca_n | (tsel_wr_select_ca_p << 0x4);
Jagan Teki539ffed2019-07-16 17:27:19 +0530721 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
722 /* LPDDR4 these register read always return 0, so
723 * can not use clrsetbits_le32(), need to write32
724 */
725 writel((0x300 << 8) | reg_value, &denali_phy[544]);
726 writel((0x300 << 8) | reg_value, &denali_phy[672]);
727 writel((0x300 << 8) | reg_value, &denali_phy[800]);
728 } else {
729 clrsetbits_le32(&denali_phy[544], 0xff, reg_value);
730 clrsetbits_le32(&denali_phy[672], 0xff, reg_value);
731 clrsetbits_le32(&denali_phy[800], 0xff, reg_value);
732 }
Kever Yang50fb9982017-02-22 16:56:35 +0800733
734 /* phy_pad_addr_drive 8bits DENALI_PHY_928 offset_0 */
735 clrsetbits_le32(&denali_phy[928], 0xff, reg_value);
736
737 /* phy_pad_rst_drive 8bits DENALI_PHY_937 offset_0 */
738 clrsetbits_le32(&denali_phy[937], 0xff, reg_value);
739
740 /* phy_pad_cke_drive 8bits DENALI_PHY_935 offset_0 */
741 clrsetbits_le32(&denali_phy[935], 0xff, reg_value);
742
743 /* phy_pad_cs_drive 8bits DENALI_PHY_939 offset_0 */
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530744 clrsetbits_le32(&denali_phy[939], 0xff,
745 tsel_ckcs_select_n | (tsel_ckcs_select_p << 0x4));
Kever Yang50fb9982017-02-22 16:56:35 +0800746
747 /* phy_pad_clk_drive 8bits DENALI_PHY_929 offset_0 */
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530748 clrsetbits_le32(&denali_phy[929], 0xff,
749 tsel_ckcs_select_n | (tsel_ckcs_select_p << 0x4));
Kever Yang50fb9982017-02-22 16:56:35 +0800750
751 /* phy_pad_fdbk_drive 23bit DENALI_PHY_924/925 */
752 clrsetbits_le32(&denali_phy[924], 0xff,
Jagan Tekib3b34392019-07-15 23:51:01 +0530753 tsel_wr_select_dq_n | (tsel_wr_select_dq_p << 4));
Kever Yang50fb9982017-02-22 16:56:35 +0800754 clrsetbits_le32(&denali_phy[925], 0xff,
755 tsel_rd_select_n | (tsel_rd_select_p << 4));
756
757 /* phy_dq_tsel_enable_X 3bits DENALI_PHY_5/133/261/389 offset_16 */
758 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
759 << 16;
760 clrsetbits_le32(&denali_phy[5], 0x7 << 16, reg_value);
761 clrsetbits_le32(&denali_phy[133], 0x7 << 16, reg_value);
762 clrsetbits_le32(&denali_phy[261], 0x7 << 16, reg_value);
763 clrsetbits_le32(&denali_phy[389], 0x7 << 16, reg_value);
764
765 /* phy_dqs_tsel_enable_X 3bits DENALI_PHY_6/134/262/390 offset_24 */
766 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
767 << 24;
768 clrsetbits_le32(&denali_phy[6], 0x7 << 24, reg_value);
769 clrsetbits_le32(&denali_phy[134], 0x7 << 24, reg_value);
770 clrsetbits_le32(&denali_phy[262], 0x7 << 24, reg_value);
771 clrsetbits_le32(&denali_phy[390], 0x7 << 24, reg_value);
772
773 /* phy_adr_tsel_enable_ 1bit DENALI_PHY_518/646/774 offset_8 */
774 reg_value = tsel_wr_en << 8;
775 clrsetbits_le32(&denali_phy[518], 0x1 << 8, reg_value);
776 clrsetbits_le32(&denali_phy[646], 0x1 << 8, reg_value);
777 clrsetbits_le32(&denali_phy[774], 0x1 << 8, reg_value);
778
779 /* phy_pad_addr_term tsel 1bit DENALI_PHY_933 offset_17 */
780 reg_value = tsel_wr_en << 17;
781 clrsetbits_le32(&denali_phy[933], 0x1 << 17, reg_value);
782 /*
783 * pad_rst/cke/cs/clk_term tsel 1bits
784 * DENALI_PHY_938/936/940/934 offset_17
785 */
786 clrsetbits_le32(&denali_phy[938], 0x1 << 17, reg_value);
787 clrsetbits_le32(&denali_phy[936], 0x1 << 17, reg_value);
788 clrsetbits_le32(&denali_phy[940], 0x1 << 17, reg_value);
789 clrsetbits_le32(&denali_phy[934], 0x1 << 17, reg_value);
790
791 /* phy_pad_fdbk_term 1bit DENALI_PHY_930 offset_17 */
792 clrsetbits_le32(&denali_phy[930], 0x1 << 17, reg_value);
Jagan Tekib5d46632019-07-16 17:27:07 +0530793
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530794 phy_io_config(chan, params, mr5);
Kever Yang50fb9982017-02-22 16:56:35 +0800795}
796
Jagan Tekic9151e22019-07-15 23:58:45 +0530797static void pctl_start(struct dram_info *dram, u8 channel)
798{
799 const struct chan_info *chan = &dram->chan[channel];
800 u32 *denali_ctl = chan->pctl->denali_ctl;
801 u32 *denali_phy = chan->publ->denali_phy;
802 u32 *ddrc0_con = get_ddrc0_con(dram, channel);
803 u32 count = 0;
804 u32 byte, tmp;
805
806 writel(0x01000000, &ddrc0_con);
807
808 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 0x2 << 24);
809
810 while (!(readl(&denali_ctl[203]) & (1 << 3))) {
811 if (count > 1000) {
812 printf("%s: Failed to init pctl for channel %d\n",
813 __func__, channel);
814 while (1)
815 ;
816 }
817
818 udelay(1);
819 count++;
820 }
821
822 writel(0x01000100, &ddrc0_con);
823
824 for (byte = 0; byte < 4; byte++) {
825 tmp = 0x820;
826 writel((tmp << 16) | tmp, &denali_phy[53 + (128 * byte)]);
827 writel((tmp << 16) | tmp, &denali_phy[54 + (128 * byte)]);
828 writel((tmp << 16) | tmp, &denali_phy[55 + (128 * byte)]);
829 writel((tmp << 16) | tmp, &denali_phy[56 + (128 * byte)]);
830 writel((tmp << 16) | tmp, &denali_phy[57 + (128 * byte)]);
831
832 clrsetbits_le32(&denali_phy[58 + (128 * byte)], 0xffff, tmp);
833 }
834
835 clrsetbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT,
836 dram->pwrup_srefresh_exit[channel]);
837}
838
Jagan Teki4ef5c012019-07-15 23:58:44 +0530839static int pctl_cfg(struct dram_info *dram, const struct chan_info *chan,
Jagan Tekid33056b2019-07-16 17:27:22 +0530840 u32 channel, struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800841{
842 u32 *denali_ctl = chan->pctl->denali_ctl;
843 u32 *denali_pi = chan->pi->denali_pi;
844 u32 *denali_phy = chan->publ->denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +0530845 const u32 *params_ctl = params->pctl_regs.denali_ctl;
846 const u32 *params_phy = params->phy_regs.denali_phy;
Kever Yang50fb9982017-02-22 16:56:35 +0800847 u32 tmp, tmp1, tmp2;
Kever Yang50fb9982017-02-22 16:56:35 +0800848
849 /*
850 * work around controller bug:
851 * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed
852 */
853 copy_to_reg(&denali_ctl[1], &params_ctl[1],
854 sizeof(struct rk3399_ddr_pctl_regs) - 4);
855 writel(params_ctl[0], &denali_ctl[0]);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530856
Jagan Tekicc9da9a2019-07-16 17:27:13 +0530857 /*
858 * two channel init at the same time, then ZQ Cal Start
859 * at the same time, it will use the same RZQ, but cannot
860 * start at the same time.
861 *
862 * So, increase tINIT3 for channel 1, will avoid two
863 * channel ZQ Cal Start at the same time
864 */
865 if (params->base.dramtype == LPDDR4 && channel == 1) {
866 tmp = ((params->base.ddr_freq * MHz + 999) / 1000);
867 tmp1 = readl(&denali_ctl[14]);
868 writel(tmp + tmp1, &denali_ctl[14]);
869 }
870
Jagan Tekia58ff792019-07-15 23:50:58 +0530871 copy_to_reg(denali_pi, &params->pi_regs.denali_pi[0],
Kever Yang50fb9982017-02-22 16:56:35 +0800872 sizeof(struct rk3399_ddr_pi_regs));
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530873
Kever Yang50fb9982017-02-22 16:56:35 +0800874 /* rank count need to set for init */
Jagan Tekia58ff792019-07-15 23:50:58 +0530875 set_memory_map(chan, channel, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800876
Jagan Tekia58ff792019-07-15 23:50:58 +0530877 writel(params->phy_regs.denali_phy[910], &denali_phy[910]);
878 writel(params->phy_regs.denali_phy[911], &denali_phy[911]);
879 writel(params->phy_regs.denali_phy[912], &denali_phy[912]);
Kever Yang50fb9982017-02-22 16:56:35 +0800880
Jagan Tekib49b5dc2019-07-16 17:27:14 +0530881 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
882 writel(params->phy_regs.denali_phy[898], &denali_phy[898]);
883 writel(params->phy_regs.denali_phy[919], &denali_phy[919]);
884 }
885
Jagan Tekic9151e22019-07-15 23:58:45 +0530886 dram->pwrup_srefresh_exit[channel] = readl(&denali_ctl[68]) &
887 PWRUP_SREFRESH_EXIT;
Kever Yang50fb9982017-02-22 16:56:35 +0800888 clrbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT);
889
890 /* PHY_DLL_RST_EN */
891 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 1 << 24);
892
893 setbits_le32(&denali_pi[0], START);
894 setbits_le32(&denali_ctl[0], START);
895
Jagan Teki5e927182019-07-16 17:27:12 +0530896 /**
897 * LPDDR4 use PLL bypass mode for init
898 * not need to wait for the PLL to lock
899 */
900 if (params->base.dramtype != LPDDR4) {
901 /* Waiting for phy DLL lock */
902 while (1) {
903 tmp = readl(&denali_phy[920]);
904 tmp1 = readl(&denali_phy[921]);
905 tmp2 = readl(&denali_phy[922]);
906 if ((((tmp >> 16) & 0x1) == 0x1) &&
907 (((tmp1 >> 16) & 0x1) == 0x1) &&
908 (((tmp1 >> 0) & 0x1) == 0x1) &&
909 (((tmp2 >> 0) & 0x1) == 0x1))
910 break;
911 }
Kever Yang50fb9982017-02-22 16:56:35 +0800912 }
913
914 copy_to_reg(&denali_phy[896], &params_phy[896], (958 - 895) * 4);
915 copy_to_reg(&denali_phy[0], &params_phy[0], (90 - 0 + 1) * 4);
916 copy_to_reg(&denali_phy[128], &params_phy[128], (218 - 128 + 1) * 4);
917 copy_to_reg(&denali_phy[256], &params_phy[256], (346 - 256 + 1) * 4);
918 copy_to_reg(&denali_phy[384], &params_phy[384], (474 - 384 + 1) * 4);
919 copy_to_reg(&denali_phy[512], &params_phy[512], (549 - 512 + 1) * 4);
920 copy_to_reg(&denali_phy[640], &params_phy[640], (677 - 640 + 1) * 4);
921 copy_to_reg(&denali_phy[768], &params_phy[768], (805 - 768 + 1) * 4);
Jagan Tekid33056b2019-07-16 17:27:22 +0530922 set_ds_odt(chan, params, 0);
Kever Yang50fb9982017-02-22 16:56:35 +0800923
924 /*
925 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_84/212/340/468 offset_8
926 * dqs_tsel_wr_end[7:4] add Half cycle
927 */
928 tmp = (readl(&denali_phy[84]) >> 8) & 0xff;
929 clrsetbits_le32(&denali_phy[84], 0xff << 8, (tmp + 0x10) << 8);
930 tmp = (readl(&denali_phy[212]) >> 8) & 0xff;
931 clrsetbits_le32(&denali_phy[212], 0xff << 8, (tmp + 0x10) << 8);
932 tmp = (readl(&denali_phy[340]) >> 8) & 0xff;
933 clrsetbits_le32(&denali_phy[340], 0xff << 8, (tmp + 0x10) << 8);
934 tmp = (readl(&denali_phy[468]) >> 8) & 0xff;
935 clrsetbits_le32(&denali_phy[468], 0xff << 8, (tmp + 0x10) << 8);
936
937 /*
938 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_83/211/339/467 offset_8
939 * dq_tsel_wr_end[7:4] add Half cycle
940 */
941 tmp = (readl(&denali_phy[83]) >> 16) & 0xff;
942 clrsetbits_le32(&denali_phy[83], 0xff << 16, (tmp + 0x10) << 16);
943 tmp = (readl(&denali_phy[211]) >> 16) & 0xff;
944 clrsetbits_le32(&denali_phy[211], 0xff << 16, (tmp + 0x10) << 16);
945 tmp = (readl(&denali_phy[339]) >> 16) & 0xff;
946 clrsetbits_le32(&denali_phy[339], 0xff << 16, (tmp + 0x10) << 16);
947 tmp = (readl(&denali_phy[467]) >> 16) & 0xff;
948 clrsetbits_le32(&denali_phy[467], 0xff << 16, (tmp + 0x10) << 16);
949
Kever Yang50fb9982017-02-22 16:56:35 +0800950 return 0;
951}
952
Jagan Teki940d1252019-07-16 17:27:39 +0530953#if !defined(CONFIG_RAM_RK3399_LPDDR4)
Kever Yang50fb9982017-02-22 16:56:35 +0800954static void select_per_cs_training_index(const struct chan_info *chan,
955 u32 rank)
956{
957 u32 *denali_phy = chan->publ->denali_phy;
958
959 /* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */
Jagan Tekif676c7c2019-07-15 23:50:56 +0530960 if ((readl(&denali_phy[84]) >> 16) & 1) {
Kever Yang50fb9982017-02-22 16:56:35 +0800961 /*
962 * PHY_8/136/264/392
963 * phy_per_cs_training_index_X 1bit offset_24
964 */
965 clrsetbits_le32(&denali_phy[8], 0x1 << 24, rank << 24);
966 clrsetbits_le32(&denali_phy[136], 0x1 << 24, rank << 24);
967 clrsetbits_le32(&denali_phy[264], 0x1 << 24, rank << 24);
968 clrsetbits_le32(&denali_phy[392], 0x1 << 24, rank << 24);
969 }
970}
971
972static void override_write_leveling_value(const struct chan_info *chan)
973{
974 u32 *denali_ctl = chan->pctl->denali_ctl;
975 u32 *denali_phy = chan->publ->denali_phy;
976 u32 byte;
977
978 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
979 setbits_le32(&denali_phy[896], 1);
980
981 /*
982 * PHY_8/136/264/392
983 * phy_per_cs_training_multicast_en_X 1bit offset_16
984 */
985 clrsetbits_le32(&denali_phy[8], 0x1 << 16, 1 << 16);
986 clrsetbits_le32(&denali_phy[136], 0x1 << 16, 1 << 16);
987 clrsetbits_le32(&denali_phy[264], 0x1 << 16, 1 << 16);
988 clrsetbits_le32(&denali_phy[392], 0x1 << 16, 1 << 16);
989
990 for (byte = 0; byte < 4; byte++)
991 clrsetbits_le32(&denali_phy[63 + (128 * byte)], 0xffff << 16,
992 0x200 << 16);
993
994 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
995 clrbits_le32(&denali_phy[896], 1);
996
997 /* CTL_200 ctrlupd_req 1bit offset_8 */
998 clrsetbits_le32(&denali_ctl[200], 0x1 << 8, 0x1 << 8);
999}
1000
1001static int data_training_ca(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301002 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001003{
1004 u32 *denali_pi = chan->pi->denali_pi;
1005 u32 *denali_phy = chan->publ->denali_phy;
1006 u32 i, tmp;
1007 u32 obs_0, obs_1, obs_2, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +05301008 u32 rank = params->ch[channel].cap_info.rank;
Jagan Tekibafcc142019-07-15 23:58:41 +05301009 u32 rank_mask;
Kever Yang50fb9982017-02-22 16:56:35 +08001010
Jagan Tekia6079612019-07-15 23:58:40 +05301011 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1012 writel(0x00003f7c, (&denali_pi[175]));
1013
Jagan Tekif05675e2019-07-16 17:27:09 +05301014 if (params->base.dramtype == LPDDR4)
1015 rank_mask = (rank == 1) ? 0x5 : 0xf;
1016 else
1017 rank_mask = (rank == 1) ? 0x1 : 0x3;
Jagan Tekibafcc142019-07-15 23:58:41 +05301018
1019 for (i = 0; i < 4; i++) {
1020 if (!(rank_mask & (1 << i)))
1021 continue;
1022
Kever Yang50fb9982017-02-22 16:56:35 +08001023 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301024
Kever Yang50fb9982017-02-22 16:56:35 +08001025 /* PI_100 PI_CALVL_EN:RW:8:2 */
1026 clrsetbits_le32(&denali_pi[100], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301027
Kever Yang50fb9982017-02-22 16:56:35 +08001028 /* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */
1029 clrsetbits_le32(&denali_pi[92],
1030 (0x1 << 16) | (0x3 << 24),
1031 (0x1 << 16) | (i << 24));
1032
1033 /* Waiting for training complete */
1034 while (1) {
1035 /* PI_174 PI_INT_STATUS:RD:8:18 */
1036 tmp = readl(&denali_pi[174]) >> 8;
1037 /*
1038 * check status obs
1039 * PHY_532/660/789 phy_adr_calvl_obs1_:0:32
1040 */
1041 obs_0 = readl(&denali_phy[532]);
1042 obs_1 = readl(&denali_phy[660]);
1043 obs_2 = readl(&denali_phy[788]);
1044 if (((obs_0 >> 30) & 0x3) ||
1045 ((obs_1 >> 30) & 0x3) ||
1046 ((obs_2 >> 30) & 0x3))
1047 obs_err = 1;
1048 if ((((tmp >> 11) & 0x1) == 0x1) &&
1049 (((tmp >> 13) & 0x1) == 0x1) &&
1050 (((tmp >> 5) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +05301051 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001052 break;
1053 else if ((((tmp >> 5) & 0x1) == 0x1) ||
1054 (obs_err == 1))
1055 return -EIO;
1056 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301057
Kever Yang50fb9982017-02-22 16:56:35 +08001058 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1059 writel(0x00003f7c, (&denali_pi[175]));
1060 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301061
Kever Yang50fb9982017-02-22 16:56:35 +08001062 clrbits_le32(&denali_pi[100], 0x3 << 8);
1063
1064 return 0;
1065}
1066
1067static int data_training_wl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301068 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001069{
1070 u32 *denali_pi = chan->pi->denali_pi;
1071 u32 *denali_phy = chan->publ->denali_phy;
1072 u32 i, tmp;
1073 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +05301074 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001075
Jagan Tekia6079612019-07-15 23:58:40 +05301076 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1077 writel(0x00003f7c, (&denali_pi[175]));
1078
Kever Yang50fb9982017-02-22 16:56:35 +08001079 for (i = 0; i < rank; i++) {
1080 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301081
Kever Yang50fb9982017-02-22 16:56:35 +08001082 /* PI_60 PI_WRLVL_EN:RW:8:2 */
1083 clrsetbits_le32(&denali_pi[60], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301084
Kever Yang50fb9982017-02-22 16:56:35 +08001085 /* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */
1086 clrsetbits_le32(&denali_pi[59],
1087 (0x1 << 8) | (0x3 << 16),
1088 (0x1 << 8) | (i << 16));
1089
1090 /* Waiting for training complete */
1091 while (1) {
1092 /* PI_174 PI_INT_STATUS:RD:8:18 */
1093 tmp = readl(&denali_pi[174]) >> 8;
1094
1095 /*
1096 * check status obs, if error maybe can not
1097 * get leveling done PHY_40/168/296/424
1098 * phy_wrlvl_status_obs_X:0:13
1099 */
1100 obs_0 = readl(&denali_phy[40]);
1101 obs_1 = readl(&denali_phy[168]);
1102 obs_2 = readl(&denali_phy[296]);
1103 obs_3 = readl(&denali_phy[424]);
1104 if (((obs_0 >> 12) & 0x1) ||
1105 ((obs_1 >> 12) & 0x1) ||
1106 ((obs_2 >> 12) & 0x1) ||
1107 ((obs_3 >> 12) & 0x1))
1108 obs_err = 1;
1109 if ((((tmp >> 10) & 0x1) == 0x1) &&
1110 (((tmp >> 13) & 0x1) == 0x1) &&
1111 (((tmp >> 4) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +05301112 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001113 break;
1114 else if ((((tmp >> 4) & 0x1) == 0x1) ||
1115 (obs_err == 1))
1116 return -EIO;
1117 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301118
Kever Yang50fb9982017-02-22 16:56:35 +08001119 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1120 writel(0x00003f7c, (&denali_pi[175]));
1121 }
1122
1123 override_write_leveling_value(chan);
1124 clrbits_le32(&denali_pi[60], 0x3 << 8);
1125
1126 return 0;
1127}
1128
1129static int data_training_rg(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301130 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001131{
1132 u32 *denali_pi = chan->pi->denali_pi;
1133 u32 *denali_phy = chan->publ->denali_phy;
1134 u32 i, tmp;
1135 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +05301136 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001137
Jagan Tekia6079612019-07-15 23:58:40 +05301138 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1139 writel(0x00003f7c, (&denali_pi[175]));
1140
Kever Yang50fb9982017-02-22 16:56:35 +08001141 for (i = 0; i < rank; i++) {
1142 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301143
Kever Yang50fb9982017-02-22 16:56:35 +08001144 /* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */
1145 clrsetbits_le32(&denali_pi[80], 0x3 << 24, 0x2 << 24);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301146
Kever Yang50fb9982017-02-22 16:56:35 +08001147 /*
1148 * PI_74 PI_RDLVL_GATE_REQ:WR:16:1
1149 * PI_RDLVL_CS:RW:24:2
1150 */
1151 clrsetbits_le32(&denali_pi[74],
1152 (0x1 << 16) | (0x3 << 24),
1153 (0x1 << 16) | (i << 24));
1154
1155 /* Waiting for training complete */
1156 while (1) {
1157 /* PI_174 PI_INT_STATUS:RD:8:18 */
1158 tmp = readl(&denali_pi[174]) >> 8;
1159
1160 /*
1161 * check status obs
1162 * PHY_43/171/299/427
1163 * PHY_GTLVL_STATUS_OBS_x:16:8
1164 */
1165 obs_0 = readl(&denali_phy[43]);
1166 obs_1 = readl(&denali_phy[171]);
1167 obs_2 = readl(&denali_phy[299]);
1168 obs_3 = readl(&denali_phy[427]);
1169 if (((obs_0 >> (16 + 6)) & 0x3) ||
1170 ((obs_1 >> (16 + 6)) & 0x3) ||
1171 ((obs_2 >> (16 + 6)) & 0x3) ||
1172 ((obs_3 >> (16 + 6)) & 0x3))
1173 obs_err = 1;
1174 if ((((tmp >> 9) & 0x1) == 0x1) &&
1175 (((tmp >> 13) & 0x1) == 0x1) &&
1176 (((tmp >> 3) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +05301177 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001178 break;
1179 else if ((((tmp >> 3) & 0x1) == 0x1) ||
1180 (obs_err == 1))
1181 return -EIO;
1182 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301183
Kever Yang50fb9982017-02-22 16:56:35 +08001184 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1185 writel(0x00003f7c, (&denali_pi[175]));
1186 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301187
Kever Yang50fb9982017-02-22 16:56:35 +08001188 clrbits_le32(&denali_pi[80], 0x3 << 24);
1189
1190 return 0;
1191}
1192
1193static int data_training_rl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301194 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001195{
1196 u32 *denali_pi = chan->pi->denali_pi;
1197 u32 i, tmp;
Jagan Teki97867c82019-07-15 23:51:05 +05301198 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001199
Jagan Tekia6079612019-07-15 23:58:40 +05301200 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1201 writel(0x00003f7c, (&denali_pi[175]));
1202
Kever Yang50fb9982017-02-22 16:56:35 +08001203 for (i = 0; i < rank; i++) {
1204 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301205
Kever Yang50fb9982017-02-22 16:56:35 +08001206 /* PI_80 PI_RDLVL_EN:RW:16:2 */
1207 clrsetbits_le32(&denali_pi[80], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301208
Kever Yang50fb9982017-02-22 16:56:35 +08001209 /* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */
1210 clrsetbits_le32(&denali_pi[74],
1211 (0x1 << 8) | (0x3 << 24),
1212 (0x1 << 8) | (i << 24));
1213
1214 /* Waiting for training complete */
1215 while (1) {
1216 /* PI_174 PI_INT_STATUS:RD:8:18 */
1217 tmp = readl(&denali_pi[174]) >> 8;
1218
1219 /*
1220 * make sure status obs not report error bit
1221 * PHY_46/174/302/430
1222 * phy_rdlvl_status_obs_X:16:8
1223 */
1224 if ((((tmp >> 8) & 0x1) == 0x1) &&
1225 (((tmp >> 13) & 0x1) == 0x1) &&
1226 (((tmp >> 2) & 0x1) == 0x0))
1227 break;
1228 else if (((tmp >> 2) & 0x1) == 0x1)
1229 return -EIO;
1230 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301231
Kever Yang50fb9982017-02-22 16:56:35 +08001232 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1233 writel(0x00003f7c, (&denali_pi[175]));
1234 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301235
Kever Yang50fb9982017-02-22 16:56:35 +08001236 clrbits_le32(&denali_pi[80], 0x3 << 16);
1237
1238 return 0;
1239}
1240
1241static int data_training_wdql(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301242 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001243{
1244 u32 *denali_pi = chan->pi->denali_pi;
1245 u32 i, tmp;
Jagan Teki97867c82019-07-15 23:51:05 +05301246 u32 rank = params->ch[channel].cap_info.rank;
Jagan Teki87723592019-07-15 23:58:42 +05301247 u32 rank_mask;
Kever Yang50fb9982017-02-22 16:56:35 +08001248
Jagan Tekia6079612019-07-15 23:58:40 +05301249 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1250 writel(0x00003f7c, (&denali_pi[175]));
1251
Jagan Tekid7504c02019-07-16 17:27:10 +05301252 if (params->base.dramtype == LPDDR4)
1253 rank_mask = (rank == 1) ? 0x5 : 0xf;
1254 else
1255 rank_mask = (rank == 1) ? 0x1 : 0x3;
Jagan Teki87723592019-07-15 23:58:42 +05301256
1257 for (i = 0; i < 4; i++) {
1258 if (!(rank_mask & (1 << i)))
1259 continue;
1260
Kever Yang50fb9982017-02-22 16:56:35 +08001261 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301262
Kever Yang50fb9982017-02-22 16:56:35 +08001263 /*
1264 * disable PI_WDQLVL_VREF_EN before wdq leveling?
1265 * PI_181 PI_WDQLVL_VREF_EN:RW:8:1
1266 */
1267 clrbits_le32(&denali_pi[181], 0x1 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301268
Kever Yang50fb9982017-02-22 16:56:35 +08001269 /* PI_124 PI_WDQLVL_EN:RW:16:2 */
1270 clrsetbits_le32(&denali_pi[124], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301271
Kever Yang50fb9982017-02-22 16:56:35 +08001272 /* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */
1273 clrsetbits_le32(&denali_pi[121],
1274 (0x1 << 8) | (0x3 << 16),
1275 (0x1 << 8) | (i << 16));
1276
1277 /* Waiting for training complete */
1278 while (1) {
1279 /* PI_174 PI_INT_STATUS:RD:8:18 */
1280 tmp = readl(&denali_pi[174]) >> 8;
1281 if ((((tmp >> 12) & 0x1) == 0x1) &&
1282 (((tmp >> 13) & 0x1) == 0x1) &&
1283 (((tmp >> 6) & 0x1) == 0x0))
1284 break;
1285 else if (((tmp >> 6) & 0x1) == 0x1)
1286 return -EIO;
1287 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301288
Kever Yang50fb9982017-02-22 16:56:35 +08001289 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1290 writel(0x00003f7c, (&denali_pi[175]));
1291 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301292
Kever Yang50fb9982017-02-22 16:56:35 +08001293 clrbits_le32(&denali_pi[124], 0x3 << 16);
1294
1295 return 0;
1296}
1297
Jagan Teki5ff7abe2019-07-16 17:27:29 +05301298static int data_training(struct dram_info *dram, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301299 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +08001300 u32 training_flag)
1301{
Jagan Teki5ff7abe2019-07-16 17:27:29 +05301302 struct chan_info *chan = &dram->chan[channel];
Kever Yang50fb9982017-02-22 16:56:35 +08001303 u32 *denali_phy = chan->publ->denali_phy;
Jagan Teki6214ff22019-07-15 23:58:39 +05301304 int ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001305
1306 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1307 setbits_le32(&denali_phy[927], (1 << 22));
1308
1309 if (training_flag == PI_FULL_TRAINING) {
Jagan Tekia58ff792019-07-15 23:50:58 +05301310 if (params->base.dramtype == LPDDR4) {
Kever Yang50fb9982017-02-22 16:56:35 +08001311 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1312 PI_READ_GATE_TRAINING |
1313 PI_READ_LEVELING | PI_WDQ_LEVELING;
Jagan Tekia58ff792019-07-15 23:50:58 +05301314 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +08001315 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1316 PI_READ_GATE_TRAINING;
Jagan Tekia58ff792019-07-15 23:50:58 +05301317 } else if (params->base.dramtype == DDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +08001318 training_flag = PI_WRITE_LEVELING |
1319 PI_READ_GATE_TRAINING |
1320 PI_READ_LEVELING;
1321 }
1322 }
1323
1324 /* ca training(LPDDR4,LPDDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301325 if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING) {
1326 ret = data_training_ca(chan, channel, params);
1327 if (ret < 0) {
1328 debug("%s: data training ca failed\n", __func__);
1329 return ret;
1330 }
1331 }
Kever Yang50fb9982017-02-22 16:56:35 +08001332
1333 /* write leveling(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301334 if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING) {
1335 ret = data_training_wl(chan, channel, params);
1336 if (ret < 0) {
1337 debug("%s: data training wl failed\n", __func__);
1338 return ret;
1339 }
1340 }
Kever Yang50fb9982017-02-22 16:56:35 +08001341
1342 /* read gate training(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301343 if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING) {
1344 ret = data_training_rg(chan, channel, params);
1345 if (ret < 0) {
1346 debug("%s: data training rg failed\n", __func__);
1347 return ret;
1348 }
1349 }
Kever Yang50fb9982017-02-22 16:56:35 +08001350
1351 /* read leveling(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301352 if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING) {
1353 ret = data_training_rl(chan, channel, params);
1354 if (ret < 0) {
1355 debug("%s: data training rl failed\n", __func__);
1356 return ret;
1357 }
1358 }
Kever Yang50fb9982017-02-22 16:56:35 +08001359
1360 /* wdq leveling(LPDDR4 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301361 if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING) {
1362 ret = data_training_wdql(chan, channel, params);
1363 if (ret < 0) {
1364 debug("%s: data training wdql failed\n", __func__);
1365 return ret;
1366 }
1367 }
Kever Yang50fb9982017-02-22 16:56:35 +08001368
1369 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1370 clrbits_le32(&denali_phy[927], (1 << 22));
1371
1372 return 0;
1373}
Jagan Teki940d1252019-07-16 17:27:39 +05301374#endif
Kever Yang50fb9982017-02-22 16:56:35 +08001375
1376static void set_ddrconfig(const struct chan_info *chan,
Jagan Tekia58ff792019-07-15 23:50:58 +05301377 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +08001378 unsigned char channel, u32 ddrconfig)
1379{
1380 /* only need to set ddrconfig */
1381 struct rk3399_msch_regs *ddr_msch_regs = chan->msch;
1382 unsigned int cs0_cap = 0;
1383 unsigned int cs1_cap = 0;
1384
Jagan Teki97867c82019-07-15 23:51:05 +05301385 cs0_cap = (1 << (params->ch[channel].cap_info.cs0_row
1386 + params->ch[channel].cap_info.col
1387 + params->ch[channel].cap_info.bk
1388 + params->ch[channel].cap_info.bw - 20));
1389 if (params->ch[channel].cap_info.rank > 1)
1390 cs1_cap = cs0_cap >> (params->ch[channel].cap_info.cs0_row
1391 - params->ch[channel].cap_info.cs1_row);
1392 if (params->ch[channel].cap_info.row_3_4) {
Kever Yang50fb9982017-02-22 16:56:35 +08001393 cs0_cap = cs0_cap * 3 / 4;
1394 cs1_cap = cs1_cap * 3 / 4;
1395 }
1396
1397 writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf);
1398 writel(((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8),
1399 &ddr_msch_regs->ddrsize);
1400}
1401
1402static void dram_all_config(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +05301403 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001404{
Jagan Teki2d337122019-07-16 17:27:00 +05301405 u32 sys_reg2 = 0;
Jagan Teki9d8769c2019-07-16 17:27:01 +05301406 u32 sys_reg3 = 0;
Kever Yang50fb9982017-02-22 16:56:35 +08001407 unsigned int channel, idx;
1408
Jagan Teki2d337122019-07-16 17:27:00 +05301409 sys_reg2 |= SYS_REG_ENC_DDRTYPE(params->base.dramtype);
1410 sys_reg2 |= SYS_REG_ENC_NUM_CH(params->base.num_channels);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301411
Kever Yang50fb9982017-02-22 16:56:35 +08001412 for (channel = 0, idx = 0;
Jagan Tekia58ff792019-07-15 23:50:58 +05301413 (idx < params->base.num_channels) && (channel < 2);
Kever Yang50fb9982017-02-22 16:56:35 +08001414 channel++) {
Jagan Tekia58ff792019-07-15 23:50:58 +05301415 const struct rk3399_sdram_channel *info = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +08001416 struct rk3399_msch_regs *ddr_msch_regs;
1417 const struct rk3399_msch_timings *noc_timing;
1418
Jagan Teki97867c82019-07-15 23:51:05 +05301419 if (params->ch[channel].cap_info.col == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001420 continue;
1421 idx++;
Jagan Teki2d337122019-07-16 17:27:00 +05301422 sys_reg2 |= SYS_REG_ENC_ROW_3_4(info->cap_info.row_3_4, channel);
1423 sys_reg2 |= SYS_REG_ENC_CHINFO(channel);
1424 sys_reg2 |= SYS_REG_ENC_RANK(info->cap_info.rank, channel);
1425 sys_reg2 |= SYS_REG_ENC_COL(info->cap_info.col, channel);
1426 sys_reg2 |= SYS_REG_ENC_BK(info->cap_info.bk, channel);
Jagan Teki2d337122019-07-16 17:27:00 +05301427 sys_reg2 |= SYS_REG_ENC_BW(info->cap_info.bw, channel);
1428 sys_reg2 |= SYS_REG_ENC_DBW(info->cap_info.dbw, channel);
Jagan Teki9d8769c2019-07-16 17:27:01 +05301429 SYS_REG_ENC_CS0_ROW(info->cap_info.cs0_row, sys_reg2, sys_reg3, channel);
1430 if (info->cap_info.cs1_row)
1431 SYS_REG_ENC_CS1_ROW(info->cap_info.cs1_row, sys_reg2,
1432 sys_reg3, channel);
1433 sys_reg3 |= SYS_REG_ENC_CS1_COL(info->cap_info.col, channel);
Jagan Teki932dd962019-07-16 17:27:04 +05301434 sys_reg3 |= SYS_REG_ENC_VERSION(DDR_SYS_REG_VERSION);
Kever Yang50fb9982017-02-22 16:56:35 +08001435
1436 ddr_msch_regs = dram->chan[channel].msch;
Jagan Tekia58ff792019-07-15 23:50:58 +05301437 noc_timing = &params->ch[channel].noc_timings;
Kever Yang50fb9982017-02-22 16:56:35 +08001438 writel(noc_timing->ddrtiminga0,
1439 &ddr_msch_regs->ddrtiminga0);
1440 writel(noc_timing->ddrtimingb0,
1441 &ddr_msch_regs->ddrtimingb0);
Jagan Teki5465f9b2019-07-16 17:27:05 +05301442 writel(noc_timing->ddrtimingc0.d32,
Kever Yang50fb9982017-02-22 16:56:35 +08001443 &ddr_msch_regs->ddrtimingc0);
1444 writel(noc_timing->devtodev0,
1445 &ddr_msch_regs->devtodev0);
Jagan Teki264a09f2019-07-16 17:27:06 +05301446 writel(noc_timing->ddrmode.d32,
Kever Yang50fb9982017-02-22 16:56:35 +08001447 &ddr_msch_regs->ddrmode);
1448
Jagan Tekib02c5482019-07-16 17:27:20 +05301449 /**
1450 * rank 1 memory clock disable (dfi_dram_clk_disable = 1)
1451 *
1452 * The hardware for LPDDR4 with
1453 * - CLK0P/N connect to lower 16-bits
1454 * - CLK1P/N connect to higher 16-bits
1455 *
1456 * dfi dram clk is configured via CLK1P/N, so disabling
1457 * dfi dram clk will disable the CLK1P/N as well for lpddr4.
1458 */
1459 if (params->ch[channel].cap_info.rank == 1 &&
1460 params->base.dramtype != LPDDR4)
Kever Yang50fb9982017-02-22 16:56:35 +08001461 setbits_le32(&dram->chan[channel].pctl->denali_ctl[276],
1462 1 << 17);
1463 }
1464
Jagan Teki2d337122019-07-16 17:27:00 +05301465 writel(sys_reg2, &dram->pmugrf->os_reg2);
Jagan Teki9d8769c2019-07-16 17:27:01 +05301466 writel(sys_reg3, &dram->pmugrf->os_reg3);
Kever Yang50fb9982017-02-22 16:56:35 +08001467 rk_clrsetreg(&dram->pmusgrf->soc_con4, 0x1f << 10,
Jagan Tekia58ff792019-07-15 23:50:58 +05301468 params->base.stride << 10);
Kever Yang50fb9982017-02-22 16:56:35 +08001469
1470 /* reboot hold register set */
1471 writel(PRESET_SGRF_HOLD(0) | PRESET_GPIO0_HOLD(1) |
1472 PRESET_GPIO1_HOLD(1),
1473 &dram->pmucru->pmucru_rstnhold_con[1]);
1474 clrsetbits_le32(&dram->cru->glb_rst_con, 0x3, 0x3);
1475}
1476
Jagan Tekicc117bb2019-07-16 17:27:31 +05301477#if !defined(CONFIG_RAM_RK3399_LPDDR4)
Jagan Teki9eb935a2019-07-16 17:27:30 +05301478static int default_data_training(struct dram_info *dram, u32 channel, u8 rank,
1479 struct rk3399_sdram_params *params)
1480{
1481 u8 training_flag = PI_READ_GATE_TRAINING;
1482
1483 /*
1484 * LPDDR3 CA training msut be trigger before
1485 * other training.
1486 * DDR3 is not have CA training.
1487 */
1488
1489 if (params->base.dramtype == LPDDR3)
1490 training_flag |= PI_CA_TRAINING;
1491
1492 return data_training(dram, channel, params, training_flag);
1493}
1494
Kever Yang50fb9982017-02-22 16:56:35 +08001495static int switch_to_phy_index1(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +05301496 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001497{
1498 u32 channel;
1499 u32 *denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +05301500 u32 ch_count = params->base.num_channels;
Kever Yang50fb9982017-02-22 16:56:35 +08001501 int ret;
1502 int i = 0;
1503
1504 writel(RK_CLRSETBITS(0x03 << 4 | 1 << 2 | 1,
1505 1 << 4 | 1 << 2 | 1),
1506 &dram->cic->cic_ctrl0);
1507 while (!(readl(&dram->cic->cic_status0) & (1 << 2))) {
1508 mdelay(10);
1509 i++;
1510 if (i > 10) {
1511 debug("index1 frequency change overtime\n");
1512 return -ETIME;
1513 }
1514 }
1515
1516 i = 0;
1517 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &dram->cic->cic_ctrl0);
1518 while (!(readl(&dram->cic->cic_status0) & (1 << 0))) {
1519 mdelay(10);
Heinrich Schuchardt80516592018-03-18 12:10:55 +01001520 i++;
Kever Yang50fb9982017-02-22 16:56:35 +08001521 if (i > 10) {
1522 debug("index1 frequency done overtime\n");
1523 return -ETIME;
1524 }
1525 }
1526
1527 for (channel = 0; channel < ch_count; channel++) {
1528 denali_phy = dram->chan[channel].publ->denali_phy;
1529 clrsetbits_le32(&denali_phy[896], (0x3 << 8) | 1, 1 << 8);
Jagan Teki5ff7abe2019-07-16 17:27:29 +05301530 ret = data_training(dram, channel, params, PI_FULL_TRAINING);
Jagan Teki6214ff22019-07-15 23:58:39 +05301531 if (ret < 0) {
Kever Yang50fb9982017-02-22 16:56:35 +08001532 debug("index1 training failed\n");
1533 return ret;
1534 }
1535 }
1536
1537 return 0;
1538}
1539
Jagan Teki940d1252019-07-16 17:27:39 +05301540#else
1541
Jagan Tekicc117bb2019-07-16 17:27:31 +05301542static u32 get_ddr_stride(struct rk3399_pmusgrf_regs *pmusgrf)
1543{
1544 return ((readl(&pmusgrf->soc_con4) >> 10) & 0x1F);
1545}
1546
1547static void set_ddr_stride(struct rk3399_pmusgrf_regs *pmusgrf, u32 stride)
1548{
1549 rk_clrsetreg(&pmusgrf->soc_con4, 0x1f << 10, stride << 10);
1550}
1551
1552static void set_cap_relate_config(const struct chan_info *chan,
1553 struct rk3399_sdram_params *params,
1554 unsigned int channel)
1555{
1556 u32 *denali_ctl = chan->pctl->denali_ctl;
1557 u32 tmp;
1558 struct rk3399_msch_timings *noc_timing;
1559
1560 if (params->base.dramtype == LPDDR3) {
1561 tmp = (8 << params->ch[channel].cap_info.bw) /
1562 (8 << params->ch[channel].cap_info.dbw);
1563
1564 /**
1565 * memdata_ratio
1566 * 1 -> 0, 2 -> 1, 4 -> 2
1567 */
1568 clrsetbits_le32(&denali_ctl[197], 0x7,
1569 (tmp >> 1));
1570 clrsetbits_le32(&denali_ctl[198], 0x7 << 8,
1571 (tmp >> 1) << 8);
1572 }
1573
1574 noc_timing = &params->ch[channel].noc_timings;
1575
1576 /*
1577 * noc timing bw relate timing is 32 bit, and real bw is 16bit
1578 * actually noc reg is setting at function dram_all_config
1579 */
1580 if (params->ch[channel].cap_info.bw == 16 &&
1581 noc_timing->ddrmode.b.mwrsize == 2) {
1582 if (noc_timing->ddrmode.b.burstsize)
1583 noc_timing->ddrmode.b.burstsize -= 1;
1584 noc_timing->ddrmode.b.mwrsize -= 1;
1585 noc_timing->ddrtimingc0.b.burstpenalty *= 2;
1586 noc_timing->ddrtimingc0.b.wrtomwr *= 2;
1587 }
1588}
1589
1590static u32 calculate_ddrconfig(struct rk3399_sdram_params *params, u32 channel)
1591{
1592 unsigned int cs0_row = params->ch[channel].cap_info.cs0_row;
1593 unsigned int col = params->ch[channel].cap_info.col;
1594 unsigned int bw = params->ch[channel].cap_info.bw;
1595 u16 ddr_cfg_2_rbc[] = {
1596 /*
1597 * [6] highest bit col
1598 * [5:3] max row(14+n)
1599 * [2] insertion row
1600 * [1:0] col(9+n),col, data bus 32bit
1601 *
1602 * highbitcol, max_row, insertion_row, col
1603 */
1604 ((0 << 6) | (2 << 3) | (0 << 2) | 0), /* 0 */
1605 ((0 << 6) | (2 << 3) | (0 << 2) | 1), /* 1 */
1606 ((0 << 6) | (1 << 3) | (0 << 2) | 2), /* 2 */
1607 ((0 << 6) | (0 << 3) | (0 << 2) | 3), /* 3 */
1608 ((0 << 6) | (2 << 3) | (1 << 2) | 1), /* 4 */
1609 ((0 << 6) | (1 << 3) | (1 << 2) | 2), /* 5 */
1610 ((1 << 6) | (0 << 3) | (0 << 2) | 2), /* 6 */
1611 ((1 << 6) | (1 << 3) | (0 << 2) | 2), /* 7 */
1612 };
1613 u32 i;
1614
1615 col -= (bw == 2) ? 0 : 1;
1616 col -= 9;
1617
1618 for (i = 0; i < 4; i++) {
1619 if ((col == (ddr_cfg_2_rbc[i] & 0x3)) &&
1620 (cs0_row <= (((ddr_cfg_2_rbc[i] >> 3) & 0x7) + 14)))
1621 break;
1622 }
1623
1624 if (i >= 4)
1625 i = -EINVAL;
1626
1627 return i;
1628}
1629
1630/**
1631 * read mr_num mode register
1632 * rank = 1: cs0
1633 * rank = 2: cs1
1634 */
1635static int read_mr(struct rk3399_ddr_pctl_regs *ddr_pctl_regs, u32 rank,
1636 u32 mr_num, u32 *buf)
1637{
1638 s32 timeout = 100;
1639
1640 writel(((1 << 16) | (((rank == 2) ? 1 : 0) << 8) | mr_num) << 8,
1641 &ddr_pctl_regs->denali_ctl[118]);
1642
1643 while (0 == (readl(&ddr_pctl_regs->denali_ctl[203]) &
1644 ((1 << 21) | (1 << 12)))) {
1645 udelay(1);
1646
1647 if (timeout <= 0) {
1648 printf("%s: pctl timeout!\n", __func__);
1649 return -ETIMEDOUT;
1650 }
1651
1652 timeout--;
1653 }
1654
1655 if (!(readl(&ddr_pctl_regs->denali_ctl[203]) & (1 << 12))) {
1656 *buf = readl(&ddr_pctl_regs->denali_ctl[119]) & 0xFF;
1657 } else {
1658 printf("%s: read mr failed with 0x%x status\n", __func__,
1659 readl(&ddr_pctl_regs->denali_ctl[17]) & 0x3);
1660 *buf = 0;
1661 }
1662
1663 setbits_le32(&ddr_pctl_regs->denali_ctl[205], (1 << 21) | (1 << 12));
1664
1665 return 0;
1666}
1667
1668static int lpddr4_mr_detect(struct dram_info *dram, u32 channel, u8 rank,
1669 struct rk3399_sdram_params *params)
1670{
1671 u64 cs0_cap;
1672 u32 stride;
1673 u32 cs = 0, col = 0, bk = 0, bw = 0, row_3_4 = 0;
1674 u32 cs0_row = 0, cs1_row = 0, ddrconfig = 0;
1675 u32 mr5, mr12, mr14;
1676 struct chan_info *chan = &dram->chan[channel];
1677 struct rk3399_ddr_pctl_regs *ddr_pctl_regs = chan->pctl;
1678 void __iomem *addr = NULL;
1679 int ret = 0;
1680 u32 val;
1681
1682 stride = get_ddr_stride(dram->pmusgrf);
1683
1684 if (params->ch[channel].cap_info.col == 0) {
1685 ret = -EPERM;
1686 goto end;
1687 }
1688
1689 cs = params->ch[channel].cap_info.rank;
1690 col = params->ch[channel].cap_info.col;
1691 bk = params->ch[channel].cap_info.bk;
1692 bw = params->ch[channel].cap_info.bw;
1693 row_3_4 = params->ch[channel].cap_info.row_3_4;
1694 cs0_row = params->ch[channel].cap_info.cs0_row;
1695 cs1_row = params->ch[channel].cap_info.cs1_row;
1696 ddrconfig = params->ch[channel].cap_info.ddrconfig;
1697
1698 /* 2GB */
1699 params->ch[channel].cap_info.rank = 2;
1700 params->ch[channel].cap_info.col = 10;
1701 params->ch[channel].cap_info.bk = 3;
1702 params->ch[channel].cap_info.bw = 2;
1703 params->ch[channel].cap_info.row_3_4 = 0;
1704 params->ch[channel].cap_info.cs0_row = 15;
1705 params->ch[channel].cap_info.cs1_row = 15;
1706 params->ch[channel].cap_info.ddrconfig = 1;
1707
1708 set_memory_map(chan, channel, params);
1709 params->ch[channel].cap_info.ddrconfig =
1710 calculate_ddrconfig(params, channel);
1711 set_ddrconfig(chan, params, channel,
1712 params->ch[channel].cap_info.ddrconfig);
1713 set_cap_relate_config(chan, params, channel);
1714
1715 cs0_cap = (1 << (params->ch[channel].cap_info.bw
1716 + params->ch[channel].cap_info.col
1717 + params->ch[channel].cap_info.bk
1718 + params->ch[channel].cap_info.cs0_row));
1719
1720 if (params->ch[channel].cap_info.row_3_4)
1721 cs0_cap = cs0_cap * 3 / 4;
1722
1723 if (channel == 0)
1724 set_ddr_stride(dram->pmusgrf, 0x17);
1725 else
1726 set_ddr_stride(dram->pmusgrf, 0x18);
1727
1728 /* read and write data to DRAM, avoid be optimized by compiler. */
1729 if (rank == 1)
1730 addr = (void __iomem *)0x100;
1731 else if (rank == 2)
1732 addr = (void __iomem *)(cs0_cap + 0x100);
1733
1734 val = readl(addr);
1735 writel(val + 1, addr);
1736
1737 read_mr(ddr_pctl_regs, rank, 5, &mr5);
1738 read_mr(ddr_pctl_regs, rank, 12, &mr12);
1739 read_mr(ddr_pctl_regs, rank, 14, &mr14);
1740
1741 if (mr5 == 0 || mr12 != 0x4d || mr14 != 0x4d) {
1742 ret = -EINVAL;
1743 goto end;
1744 }
1745end:
1746 params->ch[channel].cap_info.rank = cs;
1747 params->ch[channel].cap_info.col = col;
1748 params->ch[channel].cap_info.bk = bk;
1749 params->ch[channel].cap_info.bw = bw;
1750 params->ch[channel].cap_info.row_3_4 = row_3_4;
1751 params->ch[channel].cap_info.cs0_row = cs0_row;
1752 params->ch[channel].cap_info.cs1_row = cs1_row;
1753 params->ch[channel].cap_info.ddrconfig = ddrconfig;
1754
1755 set_ddr_stride(dram->pmusgrf, stride);
1756
1757 return ret;
1758}
1759#endif /* CONFIG_RAM_RK3399_LPDDR4 */
1760
Jagan Teki2525fae2019-07-15 23:58:52 +05301761static unsigned char calculate_stride(struct rk3399_sdram_params *params)
1762{
1763 unsigned int stride = params->base.stride;
1764 unsigned int channel, chinfo = 0;
1765 unsigned int ch_cap[2] = {0, 0};
1766 u64 cap;
1767
1768 for (channel = 0; channel < 2; channel++) {
1769 unsigned int cs0_cap = 0;
1770 unsigned int cs1_cap = 0;
1771 struct sdram_cap_info *cap_info = &params->ch[channel].cap_info;
1772
1773 if (cap_info->col == 0)
1774 continue;
1775
1776 cs0_cap = (1 << (cap_info->cs0_row + cap_info->col +
1777 cap_info->bk + cap_info->bw - 20));
1778 if (cap_info->rank > 1)
1779 cs1_cap = cs0_cap >> (cap_info->cs0_row
1780 - cap_info->cs1_row);
1781 if (cap_info->row_3_4) {
1782 cs0_cap = cs0_cap * 3 / 4;
1783 cs1_cap = cs1_cap * 3 / 4;
1784 }
1785 ch_cap[channel] = cs0_cap + cs1_cap;
1786 chinfo |= 1 << channel;
1787 }
1788
Jagan Teki874dede2019-07-15 23:58:53 +05301789 /* stride calculation for 1 channel */
1790 if (params->base.num_channels == 1 && chinfo & 1)
1791 return 0x17; /* channel a */
1792
Jagan Teki2525fae2019-07-15 23:58:52 +05301793 /* stride calculation for 2 channels, default gstride type is 256B */
1794 if (ch_cap[0] == ch_cap[1]) {
1795 cap = ch_cap[0] + ch_cap[1];
1796 switch (cap) {
1797 /* 512MB */
1798 case 512:
1799 stride = 0;
1800 break;
1801 /* 1GB */
1802 case 1024:
1803 stride = 0x5;
1804 break;
1805 /*
1806 * 768MB + 768MB same as total 2GB memory
1807 * useful space: 0-768MB 1GB-1792MB
1808 */
1809 case 1536:
1810 /* 2GB */
1811 case 2048:
1812 stride = 0x9;
1813 break;
1814 /* 1536MB + 1536MB */
1815 case 3072:
1816 stride = 0x11;
1817 break;
1818 /* 4GB */
1819 case 4096:
1820 stride = 0xD;
1821 break;
1822 default:
1823 printf("%s: Unable to calculate stride for ", __func__);
1824 print_size((cap * (1 << 20)), " capacity\n");
1825 break;
1826 }
1827 }
1828
Jagan Teki8eed4a42019-07-15 23:58:55 +05301829 sdram_print_stride(stride);
1830
Jagan Teki2525fae2019-07-15 23:58:52 +05301831 return stride;
1832}
1833
Jagan Teki43485e12019-07-15 23:58:54 +05301834static void clear_channel_params(struct rk3399_sdram_params *params, u8 channel)
1835{
1836 params->ch[channel].cap_info.rank = 0;
1837 params->ch[channel].cap_info.col = 0;
1838 params->ch[channel].cap_info.bk = 0;
1839 params->ch[channel].cap_info.bw = 32;
1840 params->ch[channel].cap_info.dbw = 32;
1841 params->ch[channel].cap_info.row_3_4 = 0;
1842 params->ch[channel].cap_info.cs0_row = 0;
1843 params->ch[channel].cap_info.cs1_row = 0;
1844 params->ch[channel].cap_info.ddrconfig = 0;
1845}
1846
1847static int pctl_init(struct dram_info *dram, struct rk3399_sdram_params *params)
1848{
1849 int channel;
1850 int ret;
1851
1852 for (channel = 0; channel < 2; channel++) {
1853 const struct chan_info *chan = &dram->chan[channel];
1854 struct rk3399_cru *cru = dram->cru;
1855 struct rk3399_ddr_publ_regs *publ = chan->publ;
1856
1857 phy_pctrl_reset(cru, channel);
1858 phy_dll_bypass_set(publ, params->base.ddr_freq);
1859
1860 ret = pctl_cfg(dram, chan, channel, params);
1861 if (ret < 0) {
1862 printf("%s: pctl config failed\n", __func__);
1863 return ret;
1864 }
1865
1866 /* start to trigger initialization */
1867 pctl_start(dram, channel);
1868 }
1869
1870 return 0;
1871}
1872
Kever Yang50fb9982017-02-22 16:56:35 +08001873static int sdram_init(struct dram_info *dram,
Jagan Teki2525fae2019-07-15 23:58:52 +05301874 struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001875{
Jagan Tekia58ff792019-07-15 23:50:58 +05301876 unsigned char dramtype = params->base.dramtype;
1877 unsigned int ddr_freq = params->base.ddr_freq;
Jagan Teki43485e12019-07-15 23:58:54 +05301878 int channel, ch, rank;
Jagan Teki2ef77ed2019-07-15 23:50:59 +05301879 int ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001880
1881 debug("Starting SDRAM initialization...\n");
1882
Philipp Tomsich39dce4a2017-05-31 18:16:35 +02001883 if ((dramtype == DDR3 && ddr_freq > 933) ||
Kever Yang50fb9982017-02-22 16:56:35 +08001884 (dramtype == LPDDR3 && ddr_freq > 933) ||
1885 (dramtype == LPDDR4 && ddr_freq > 800)) {
1886 debug("SDRAM frequency is to high!");
1887 return -E2BIG;
1888 }
1889
Jagan Teki43485e12019-07-15 23:58:54 +05301890 for (ch = 0; ch < 2; ch++) {
1891 params->ch[ch].cap_info.rank = 2;
1892 for (rank = 2; rank != 0; rank--) {
1893 ret = pctl_init(dram, params);
1894 if (ret < 0) {
1895 printf("%s: pctl init failed\n", __func__);
1896 return ret;
1897 }
1898
1899 /* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */
1900 if (dramtype == LPDDR3)
1901 udelay(10);
1902
1903 params->ch[ch].cap_info.rank = rank;
1904
Jagan Teki9eb935a2019-07-16 17:27:30 +05301905 ret = dram->ops->data_training(dram, ch, rank, params);
1906 if (!ret) {
1907 debug("%s: data trained for rank %d, ch %d\n",
1908 __func__, rank, ch);
Jagan Teki43485e12019-07-15 23:58:54 +05301909 break;
Jagan Teki9eb935a2019-07-16 17:27:30 +05301910 }
Jagan Teki43485e12019-07-15 23:58:54 +05301911 }
1912 /* Computed rank with associated channel number */
1913 params->ch[ch].cap_info.rank = rank;
1914 }
1915
1916 params->base.num_channels = 0;
Kever Yang50fb9982017-02-22 16:56:35 +08001917 for (channel = 0; channel < 2; channel++) {
1918 const struct chan_info *chan = &dram->chan[channel];
Jagan Teki43485e12019-07-15 23:58:54 +05301919 struct sdram_cap_info *cap_info = &params->ch[channel].cap_info;
1920 u8 training_flag = PI_FULL_TRAINING;
Kever Yang50fb9982017-02-22 16:56:35 +08001921
Jagan Teki43485e12019-07-15 23:58:54 +05301922 if (cap_info->rank == 0) {
1923 clear_channel_params(params, channel);
Kever Yang50fb9982017-02-22 16:56:35 +08001924 continue;
Jagan Teki43485e12019-07-15 23:58:54 +05301925 } else {
1926 params->base.num_channels++;
Kever Yang50fb9982017-02-22 16:56:35 +08001927 }
1928
Jagan Teki43485e12019-07-15 23:58:54 +05301929 debug("Channel ");
1930 debug(channel ? "1: " : "0: ");
Jagan Tekic9151e22019-07-15 23:58:45 +05301931
Jagan Teki43485e12019-07-15 23:58:54 +05301932 /* LPDDR3 should have write and read gate training */
1933 if (params->base.dramtype == LPDDR3)
1934 training_flag = PI_WRITE_LEVELING |
1935 PI_READ_GATE_TRAINING;
Kever Yang50fb9982017-02-22 16:56:35 +08001936
Jagan Teki43485e12019-07-15 23:58:54 +05301937 if (params->base.dramtype != LPDDR4) {
1938 ret = data_training(dram, channel, params,
1939 training_flag);
1940 if (!ret) {
1941 debug("%s: data train failed for channel %d\n",
1942 __func__, ret);
1943 continue;
1944 }
Kever Yang50fb9982017-02-22 16:56:35 +08001945 }
1946
Jagan Teki8eed4a42019-07-15 23:58:55 +05301947 sdram_print_ddr_info(cap_info, &params->base);
1948
Jagan Teki43485e12019-07-15 23:58:54 +05301949 set_ddrconfig(chan, params, channel, cap_info->ddrconfig);
1950 }
1951
1952 if (params->base.num_channels == 0) {
1953 printf("%s: ", __func__);
Jagan Teki8eed4a42019-07-15 23:58:55 +05301954 sdram_print_dram_type(params->base.dramtype);
Jagan Teki43485e12019-07-15 23:58:54 +05301955 printf(" - %dMHz failed!\n", params->base.ddr_freq);
1956 return -EINVAL;
Kever Yang50fb9982017-02-22 16:56:35 +08001957 }
Jagan Teki2525fae2019-07-15 23:58:52 +05301958
1959 params->base.stride = calculate_stride(params);
Jagan Tekia58ff792019-07-15 23:50:58 +05301960 dram_all_config(dram, params);
Jagan Teki940d1252019-07-16 17:27:39 +05301961 dram->ops->set_rate(dram, params);
Kever Yang50fb9982017-02-22 16:56:35 +08001962
1963 debug("Finish SDRAM initialization...\n");
1964 return 0;
1965}
1966
1967static int rk3399_dmc_ofdata_to_platdata(struct udevice *dev)
1968{
1969#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1970 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
Kever Yang50fb9982017-02-22 16:56:35 +08001971 int ret;
1972
Philipp Tomsich0250c232017-06-07 18:46:03 +02001973 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
1974 (u32 *)&plat->sdram_params,
1975 sizeof(plat->sdram_params) / sizeof(u32));
Kever Yang50fb9982017-02-22 16:56:35 +08001976 if (ret) {
1977 printf("%s: Cannot read rockchip,sdram-params %d\n",
1978 __func__, ret);
1979 return ret;
1980 }
Masahiro Yamadae4873e32018-04-19 12:14:03 +09001981 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001982 if (ret)
1983 printf("%s: regmap failed %d\n", __func__, ret);
1984
1985#endif
1986 return 0;
1987}
1988
1989#if CONFIG_IS_ENABLED(OF_PLATDATA)
1990static int conv_of_platdata(struct udevice *dev)
1991{
1992 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1993 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1994 int ret;
1995
1996 ret = regmap_init_mem_platdata(dev, dtplat->reg,
Jagan Tekif676c7c2019-07-15 23:50:56 +05301997 ARRAY_SIZE(dtplat->reg) / 2,
1998 &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001999 if (ret)
2000 return ret;
2001
2002 return 0;
2003}
2004#endif
2005
Jagan Teki9eb935a2019-07-16 17:27:30 +05302006static const struct sdram_rk3399_ops rk3399_ops = {
Jagan Tekicc117bb2019-07-16 17:27:31 +05302007#if !defined(CONFIG_RAM_RK3399_LPDDR4)
Jagan Teki9eb935a2019-07-16 17:27:30 +05302008 .data_training = default_data_training,
Jagan Teki940d1252019-07-16 17:27:39 +05302009 .set_rate = switch_to_phy_index1,
Jagan Tekicc117bb2019-07-16 17:27:31 +05302010#else
2011 .data_training = lpddr4_mr_detect,
2012#endif
Jagan Teki9eb935a2019-07-16 17:27:30 +05302013};
2014
Kever Yang50fb9982017-02-22 16:56:35 +08002015static int rk3399_dmc_init(struct udevice *dev)
2016{
2017 struct dram_info *priv = dev_get_priv(dev);
2018 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
2019 int ret;
2020#if !CONFIG_IS_ENABLED(OF_PLATDATA)
2021 struct rk3399_sdram_params *params = &plat->sdram_params;
2022#else
2023 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
2024 struct rk3399_sdram_params *params =
2025 (void *)dtplat->rockchip_sdram_params;
2026
2027 ret = conv_of_platdata(dev);
2028 if (ret)
2029 return ret;
2030#endif
2031
Jagan Teki9eb935a2019-07-16 17:27:30 +05302032 priv->ops = &rk3399_ops;
Kever Yang50fb9982017-02-22 16:56:35 +08002033 priv->cic = syscon_get_first_range(ROCKCHIP_SYSCON_CIC);
Jagan Tekic9151e22019-07-15 23:58:45 +05302034 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Kever Yang50fb9982017-02-22 16:56:35 +08002035 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
2036 priv->pmusgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
2037 priv->pmucru = rockchip_get_pmucru();
2038 priv->cru = rockchip_get_cru();
2039 priv->chan[0].pctl = regmap_get_range(plat->map, 0);
2040 priv->chan[0].pi = regmap_get_range(plat->map, 1);
2041 priv->chan[0].publ = regmap_get_range(plat->map, 2);
2042 priv->chan[0].msch = regmap_get_range(plat->map, 3);
2043 priv->chan[1].pctl = regmap_get_range(plat->map, 4);
2044 priv->chan[1].pi = regmap_get_range(plat->map, 5);
2045 priv->chan[1].publ = regmap_get_range(plat->map, 6);
2046 priv->chan[1].msch = regmap_get_range(plat->map, 7);
2047
2048 debug("con reg %p %p %p %p %p %p %p %p\n",
2049 priv->chan[0].pctl, priv->chan[0].pi,
2050 priv->chan[0].publ, priv->chan[0].msch,
2051 priv->chan[1].pctl, priv->chan[1].pi,
2052 priv->chan[1].publ, priv->chan[1].msch);
2053 debug("cru %p, cic %p, grf %p, sgrf %p, pmucru %p\n", priv->cru,
2054 priv->cic, priv->pmugrf, priv->pmusgrf, priv->pmucru);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05302055
Kever Yang50fb9982017-02-22 16:56:35 +08002056#if CONFIG_IS_ENABLED(OF_PLATDATA)
2057 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->ddr_clk);
2058#else
2059 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
2060#endif
2061 if (ret) {
2062 printf("%s clk get failed %d\n", __func__, ret);
2063 return ret;
2064 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05302065
Kever Yang50fb9982017-02-22 16:56:35 +08002066 ret = clk_set_rate(&priv->ddr_clk, params->base.ddr_freq * MHz);
2067 if (ret < 0) {
2068 printf("%s clk set failed %d\n", __func__, ret);
2069 return ret;
2070 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05302071
Kever Yang50fb9982017-02-22 16:56:35 +08002072 ret = sdram_init(priv, params);
2073 if (ret < 0) {
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05302074 printf("%s DRAM init failed %d\n", __func__, ret);
Kever Yang50fb9982017-02-22 16:56:35 +08002075 return ret;
2076 }
2077
2078 return 0;
2079}
2080#endif
2081
Kever Yang50fb9982017-02-22 16:56:35 +08002082static int rk3399_dmc_probe(struct udevice *dev)
2083{
Kever Yang7f347842019-04-01 17:20:53 +08002084#if defined(CONFIG_TPL_BUILD) || \
2085 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08002086 if (rk3399_dmc_init(dev))
2087 return 0;
2088#else
2089 struct dram_info *priv = dev_get_priv(dev);
2090
2091 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05302092 debug("%s: pmugrf = %p\n", __func__, priv->pmugrf);
Kever Yang6c15a542017-06-23 16:11:06 +08002093 priv->info.base = CONFIG_SYS_SDRAM_BASE;
Jagan Tekif676c7c2019-07-15 23:50:56 +05302094 priv->info.size =
2095 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg2);
Kever Yang50fb9982017-02-22 16:56:35 +08002096#endif
2097 return 0;
2098}
2099
2100static int rk3399_dmc_get_info(struct udevice *dev, struct ram_info *info)
2101{
2102 struct dram_info *priv = dev_get_priv(dev);
2103
Kever Yangea61d142017-04-19 16:01:14 +08002104 *info = priv->info;
Kever Yang50fb9982017-02-22 16:56:35 +08002105
2106 return 0;
2107}
2108
2109static struct ram_ops rk3399_dmc_ops = {
2110 .get_info = rk3399_dmc_get_info,
2111};
2112
Kever Yang50fb9982017-02-22 16:56:35 +08002113static const struct udevice_id rk3399_dmc_ids[] = {
2114 { .compatible = "rockchip,rk3399-dmc" },
2115 { }
2116};
2117
2118U_BOOT_DRIVER(dmc_rk3399) = {
2119 .name = "rockchip_rk3399_dmc",
2120 .id = UCLASS_RAM,
2121 .of_match = rk3399_dmc_ids,
2122 .ops = &rk3399_dmc_ops,
Kever Yang7f347842019-04-01 17:20:53 +08002123#if defined(CONFIG_TPL_BUILD) || \
2124 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08002125 .ofdata_to_platdata = rk3399_dmc_ofdata_to_platdata,
2126#endif
2127 .probe = rk3399_dmc_probe,
Kever Yang50fb9982017-02-22 16:56:35 +08002128 .priv_auto_alloc_size = sizeof(struct dram_info),
Kever Yang7f347842019-04-01 17:20:53 +08002129#if defined(CONFIG_TPL_BUILD) || \
2130 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08002131 .platdata_auto_alloc_size = sizeof(struct rockchip_dmc_plat),
2132#endif
2133};