blob: 541e4a4b1e58d23b2b9e7fc8f321fe88c0ad4251 [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>
17#include <asm/arch-rockchip/sdram_common.h>
18#include <asm/arch-rockchip/sdram_rk3399.h>
19#include <asm/arch-rockchip/cru_rk3399.h>
20#include <asm/arch-rockchip/grf_rk3399.h>
21#include <asm/arch-rockchip/hardware.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
Kever Yang50fb9982017-02-22 16:56:35 +080025struct chan_info {
26 struct rk3399_ddr_pctl_regs *pctl;
27 struct rk3399_ddr_pi_regs *pi;
28 struct rk3399_ddr_publ_regs *publ;
29 struct rk3399_msch_regs *msch;
30};
31
32struct dram_info {
Kever Yang7f347842019-04-01 17:20:53 +080033#if defined(CONFIG_TPL_BUILD) || \
34 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +080035 struct chan_info chan[2];
36 struct clk ddr_clk;
37 struct rk3399_cru *cru;
38 struct rk3399_pmucru *pmucru;
39 struct rk3399_pmusgrf_regs *pmusgrf;
40 struct rk3399_ddr_cic_regs *cic;
41#endif
42 struct ram_info info;
43 struct rk3399_pmugrf_regs *pmugrf;
44};
45
Kever Yang50fb9982017-02-22 16:56:35 +080046#define PRESET_SGRF_HOLD(n) ((0x1 << (6 + 16)) | ((n) << 6))
47#define PRESET_GPIO0_HOLD(n) ((0x1 << (7 + 16)) | ((n) << 7))
48#define PRESET_GPIO1_HOLD(n) ((0x1 << (8 + 16)) | ((n) << 8))
49
Jagan Tekif676c7c2019-07-15 23:50:56 +053050#define PHY_DRV_ODT_HI_Z 0x0
Kever Yang50fb9982017-02-22 16:56:35 +080051#define PHY_DRV_ODT_240 0x1
52#define PHY_DRV_ODT_120 0x8
53#define PHY_DRV_ODT_80 0x9
54#define PHY_DRV_ODT_60 0xc
55#define PHY_DRV_ODT_48 0xd
56#define PHY_DRV_ODT_40 0xe
57#define PHY_DRV_ODT_34_3 0xf
58
Kever Yang7f347842019-04-01 17:20:53 +080059#if defined(CONFIG_TPL_BUILD) || \
60 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +080061
62struct rockchip_dmc_plat {
63#if CONFIG_IS_ENABLED(OF_PLATDATA)
64 struct dtd_rockchip_rk3399_dmc dtplat;
65#else
66 struct rk3399_sdram_params sdram_params;
67#endif
68 struct regmap *map;
69};
70
71static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
72{
73 int i;
74
75 for (i = 0; i < n / sizeof(u32); i++) {
76 writel(*src, dest);
77 src++;
78 dest++;
79 }
80}
81
82static void phy_dll_bypass_set(struct rk3399_ddr_publ_regs *ddr_publ_regs,
83 u32 freq)
84{
85 u32 *denali_phy = ddr_publ_regs->denali_phy;
86
87 /* From IP spec, only freq small than 125 can enter dll bypass mode */
88 if (freq <= 125) {
89 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
90 setbits_le32(&denali_phy[86], (0x3 << 2) << 8);
91 setbits_le32(&denali_phy[214], (0x3 << 2) << 8);
92 setbits_le32(&denali_phy[342], (0x3 << 2) << 8);
93 setbits_le32(&denali_phy[470], (0x3 << 2) << 8);
94
95 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
96 setbits_le32(&denali_phy[547], (0x3 << 2) << 16);
97 setbits_le32(&denali_phy[675], (0x3 << 2) << 16);
98 setbits_le32(&denali_phy[803], (0x3 << 2) << 16);
99 } else {
100 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
101 clrbits_le32(&denali_phy[86], (0x3 << 2) << 8);
102 clrbits_le32(&denali_phy[214], (0x3 << 2) << 8);
103 clrbits_le32(&denali_phy[342], (0x3 << 2) << 8);
104 clrbits_le32(&denali_phy[470], (0x3 << 2) << 8);
105
106 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
107 clrbits_le32(&denali_phy[547], (0x3 << 2) << 16);
108 clrbits_le32(&denali_phy[675], (0x3 << 2) << 16);
109 clrbits_le32(&denali_phy[803], (0x3 << 2) << 16);
110 }
111}
112
113static void set_memory_map(const struct chan_info *chan, u32 channel,
114 const struct rk3399_sdram_params *sdram_params)
115{
116 const struct rk3399_sdram_channel *sdram_ch =
117 &sdram_params->ch[channel];
118 u32 *denali_ctl = chan->pctl->denali_ctl;
119 u32 *denali_pi = chan->pi->denali_pi;
120 u32 cs_map;
121 u32 reduc;
122 u32 row;
123
124 /* Get row number from ddrconfig setting */
125 if (sdram_ch->ddrconfig < 2 || sdram_ch->ddrconfig == 4)
126 row = 16;
127 else if (sdram_ch->ddrconfig == 3)
128 row = 14;
129 else
130 row = 15;
131
132 cs_map = (sdram_ch->rank > 1) ? 3 : 1;
133 reduc = (sdram_ch->bw == 2) ? 0 : 1;
134
135 /* Set the dram configuration to ctrl */
136 clrsetbits_le32(&denali_ctl[191], 0xF, (12 - sdram_ch->col));
137 clrsetbits_le32(&denali_ctl[190], (0x3 << 16) | (0x7 << 24),
138 ((3 - sdram_ch->bk) << 16) |
139 ((16 - row) << 24));
140
141 clrsetbits_le32(&denali_ctl[196], 0x3 | (1 << 16),
142 cs_map | (reduc << 16));
143
144 /* PI_199 PI_COL_DIFF:RW:0:4 */
145 clrsetbits_le32(&denali_pi[199], 0xF, (12 - sdram_ch->col));
146
147 /* PI_155 PI_ROW_DIFF:RW:24:3 PI_BANK_DIFF:RW:16:2 */
148 clrsetbits_le32(&denali_pi[155], (0x3 << 16) | (0x7 << 24),
149 ((3 - sdram_ch->bk) << 16) |
150 ((16 - row) << 24));
151 /* PI_41 PI_CS_MAP:RW:24:4 */
152 clrsetbits_le32(&denali_pi[41], 0xf << 24, cs_map << 24);
Jagan Tekif676c7c2019-07-15 23:50:56 +0530153 if (sdram_ch->rank == 1 && sdram_params->base.dramtype == DDR3)
Kever Yang50fb9982017-02-22 16:56:35 +0800154 writel(0x2EC7FFFF, &denali_pi[34]);
155}
156
157static void set_ds_odt(const struct chan_info *chan,
158 const struct rk3399_sdram_params *sdram_params)
159{
160 u32 *denali_phy = chan->publ->denali_phy;
161
162 u32 tsel_idle_en, tsel_wr_en, tsel_rd_en;
163 u32 tsel_idle_select_p, tsel_wr_select_p, tsel_rd_select_p;
164 u32 ca_tsel_wr_select_p, ca_tsel_wr_select_n;
165 u32 tsel_idle_select_n, tsel_wr_select_n, tsel_rd_select_n;
166 u32 reg_value;
167
168 if (sdram_params->base.dramtype == LPDDR4) {
Jagan Tekif676c7c2019-07-15 23:50:56 +0530169 tsel_rd_select_p = PHY_DRV_ODT_HI_Z;
Kever Yang50fb9982017-02-22 16:56:35 +0800170 tsel_wr_select_p = PHY_DRV_ODT_40;
171 ca_tsel_wr_select_p = PHY_DRV_ODT_40;
Jagan Tekif676c7c2019-07-15 23:50:56 +0530172 tsel_idle_select_p = PHY_DRV_ODT_HI_Z;
Kever Yang50fb9982017-02-22 16:56:35 +0800173
174 tsel_rd_select_n = PHY_DRV_ODT_240;
175 tsel_wr_select_n = PHY_DRV_ODT_40;
176 ca_tsel_wr_select_n = PHY_DRV_ODT_40;
177 tsel_idle_select_n = PHY_DRV_ODT_240;
178 } else if (sdram_params->base.dramtype == LPDDR3) {
179 tsel_rd_select_p = PHY_DRV_ODT_240;
180 tsel_wr_select_p = PHY_DRV_ODT_34_3;
181 ca_tsel_wr_select_p = PHY_DRV_ODT_48;
182 tsel_idle_select_p = PHY_DRV_ODT_240;
183
Jagan Tekif676c7c2019-07-15 23:50:56 +0530184 tsel_rd_select_n = PHY_DRV_ODT_HI_Z;
Kever Yang50fb9982017-02-22 16:56:35 +0800185 tsel_wr_select_n = PHY_DRV_ODT_34_3;
186 ca_tsel_wr_select_n = PHY_DRV_ODT_48;
Jagan Tekif676c7c2019-07-15 23:50:56 +0530187 tsel_idle_select_n = PHY_DRV_ODT_HI_Z;
Kever Yang50fb9982017-02-22 16:56:35 +0800188 } else {
189 tsel_rd_select_p = PHY_DRV_ODT_240;
190 tsel_wr_select_p = PHY_DRV_ODT_34_3;
191 ca_tsel_wr_select_p = PHY_DRV_ODT_34_3;
192 tsel_idle_select_p = PHY_DRV_ODT_240;
193
194 tsel_rd_select_n = PHY_DRV_ODT_240;
195 tsel_wr_select_n = PHY_DRV_ODT_34_3;
196 ca_tsel_wr_select_n = PHY_DRV_ODT_34_3;
197 tsel_idle_select_n = PHY_DRV_ODT_240;
198 }
199
200 if (sdram_params->base.odt == 1)
201 tsel_rd_en = 1;
202 else
203 tsel_rd_en = 0;
204
205 tsel_wr_en = 0;
206 tsel_idle_en = 0;
207
208 /*
209 * phy_dq_tsel_select_X 24bits DENALI_PHY_6/134/262/390 offset_0
210 * sets termination values for read/idle cycles and drive strength
211 * for write cycles for DQ/DM
212 */
213 reg_value = tsel_rd_select_n | (tsel_rd_select_p << 0x4) |
214 (tsel_wr_select_n << 8) | (tsel_wr_select_p << 12) |
215 (tsel_idle_select_n << 16) | (tsel_idle_select_p << 20);
216 clrsetbits_le32(&denali_phy[6], 0xffffff, reg_value);
217 clrsetbits_le32(&denali_phy[134], 0xffffff, reg_value);
218 clrsetbits_le32(&denali_phy[262], 0xffffff, reg_value);
219 clrsetbits_le32(&denali_phy[390], 0xffffff, reg_value);
220
221 /*
222 * phy_dqs_tsel_select_X 24bits DENALI_PHY_7/135/263/391 offset_0
223 * sets termination values for read/idle cycles and drive strength
224 * for write cycles for DQS
225 */
226 clrsetbits_le32(&denali_phy[7], 0xffffff, reg_value);
227 clrsetbits_le32(&denali_phy[135], 0xffffff, reg_value);
228 clrsetbits_le32(&denali_phy[263], 0xffffff, reg_value);
229 clrsetbits_le32(&denali_phy[391], 0xffffff, reg_value);
230
231 /* phy_adr_tsel_select_ 8bits DENALI_PHY_544/672/800 offset_0 */
232 reg_value = ca_tsel_wr_select_n | (ca_tsel_wr_select_p << 0x4);
233 clrsetbits_le32(&denali_phy[544], 0xff, reg_value);
234 clrsetbits_le32(&denali_phy[672], 0xff, reg_value);
235 clrsetbits_le32(&denali_phy[800], 0xff, reg_value);
236
237 /* phy_pad_addr_drive 8bits DENALI_PHY_928 offset_0 */
238 clrsetbits_le32(&denali_phy[928], 0xff, reg_value);
239
240 /* phy_pad_rst_drive 8bits DENALI_PHY_937 offset_0 */
241 clrsetbits_le32(&denali_phy[937], 0xff, reg_value);
242
243 /* phy_pad_cke_drive 8bits DENALI_PHY_935 offset_0 */
244 clrsetbits_le32(&denali_phy[935], 0xff, reg_value);
245
246 /* phy_pad_cs_drive 8bits DENALI_PHY_939 offset_0 */
247 clrsetbits_le32(&denali_phy[939], 0xff, reg_value);
248
249 /* phy_pad_clk_drive 8bits DENALI_PHY_929 offset_0 */
250 clrsetbits_le32(&denali_phy[929], 0xff, reg_value);
251
252 /* phy_pad_fdbk_drive 23bit DENALI_PHY_924/925 */
253 clrsetbits_le32(&denali_phy[924], 0xff,
254 tsel_wr_select_n | (tsel_wr_select_p << 4));
255 clrsetbits_le32(&denali_phy[925], 0xff,
256 tsel_rd_select_n | (tsel_rd_select_p << 4));
257
258 /* phy_dq_tsel_enable_X 3bits DENALI_PHY_5/133/261/389 offset_16 */
259 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
260 << 16;
261 clrsetbits_le32(&denali_phy[5], 0x7 << 16, reg_value);
262 clrsetbits_le32(&denali_phy[133], 0x7 << 16, reg_value);
263 clrsetbits_le32(&denali_phy[261], 0x7 << 16, reg_value);
264 clrsetbits_le32(&denali_phy[389], 0x7 << 16, reg_value);
265
266 /* phy_dqs_tsel_enable_X 3bits DENALI_PHY_6/134/262/390 offset_24 */
267 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
268 << 24;
269 clrsetbits_le32(&denali_phy[6], 0x7 << 24, reg_value);
270 clrsetbits_le32(&denali_phy[134], 0x7 << 24, reg_value);
271 clrsetbits_le32(&denali_phy[262], 0x7 << 24, reg_value);
272 clrsetbits_le32(&denali_phy[390], 0x7 << 24, reg_value);
273
274 /* phy_adr_tsel_enable_ 1bit DENALI_PHY_518/646/774 offset_8 */
275 reg_value = tsel_wr_en << 8;
276 clrsetbits_le32(&denali_phy[518], 0x1 << 8, reg_value);
277 clrsetbits_le32(&denali_phy[646], 0x1 << 8, reg_value);
278 clrsetbits_le32(&denali_phy[774], 0x1 << 8, reg_value);
279
280 /* phy_pad_addr_term tsel 1bit DENALI_PHY_933 offset_17 */
281 reg_value = tsel_wr_en << 17;
282 clrsetbits_le32(&denali_phy[933], 0x1 << 17, reg_value);
283 /*
284 * pad_rst/cke/cs/clk_term tsel 1bits
285 * DENALI_PHY_938/936/940/934 offset_17
286 */
287 clrsetbits_le32(&denali_phy[938], 0x1 << 17, reg_value);
288 clrsetbits_le32(&denali_phy[936], 0x1 << 17, reg_value);
289 clrsetbits_le32(&denali_phy[940], 0x1 << 17, reg_value);
290 clrsetbits_le32(&denali_phy[934], 0x1 << 17, reg_value);
291
292 /* phy_pad_fdbk_term 1bit DENALI_PHY_930 offset_17 */
293 clrsetbits_le32(&denali_phy[930], 0x1 << 17, reg_value);
294}
295
296static int phy_io_config(const struct chan_info *chan,
Jagan Tekif676c7c2019-07-15 23:50:56 +0530297 const struct rk3399_sdram_params *sdram_params)
Kever Yang50fb9982017-02-22 16:56:35 +0800298{
299 u32 *denali_phy = chan->publ->denali_phy;
300 u32 vref_mode_dq, vref_value_dq, vref_mode_ac, vref_value_ac;
301 u32 mode_sel;
302 u32 reg_value;
303 u32 drv_value, odt_value;
304 u32 speed;
305
306 /* vref setting */
307 if (sdram_params->base.dramtype == LPDDR4) {
308 /* LPDDR4 */
309 vref_mode_dq = 0x6;
310 vref_value_dq = 0x1f;
311 vref_mode_ac = 0x6;
312 vref_value_ac = 0x1f;
313 } else if (sdram_params->base.dramtype == LPDDR3) {
314 if (sdram_params->base.odt == 1) {
315 vref_mode_dq = 0x5; /* LPDDR3 ODT */
316 drv_value = (readl(&denali_phy[6]) >> 12) & 0xf;
317 odt_value = (readl(&denali_phy[6]) >> 4) & 0xf;
318 if (drv_value == PHY_DRV_ODT_48) {
319 switch (odt_value) {
320 case PHY_DRV_ODT_240:
321 vref_value_dq = 0x16;
322 break;
323 case PHY_DRV_ODT_120:
324 vref_value_dq = 0x26;
325 break;
326 case PHY_DRV_ODT_60:
327 vref_value_dq = 0x36;
328 break;
329 default:
330 debug("Invalid ODT value.\n");
331 return -EINVAL;
332 }
333 } else if (drv_value == PHY_DRV_ODT_40) {
334 switch (odt_value) {
335 case PHY_DRV_ODT_240:
336 vref_value_dq = 0x19;
337 break;
338 case PHY_DRV_ODT_120:
339 vref_value_dq = 0x23;
340 break;
341 case PHY_DRV_ODT_60:
342 vref_value_dq = 0x31;
343 break;
344 default:
345 debug("Invalid ODT value.\n");
346 return -EINVAL;
347 }
348 } else if (drv_value == PHY_DRV_ODT_34_3) {
349 switch (odt_value) {
350 case PHY_DRV_ODT_240:
351 vref_value_dq = 0x17;
352 break;
353 case PHY_DRV_ODT_120:
354 vref_value_dq = 0x20;
355 break;
356 case PHY_DRV_ODT_60:
357 vref_value_dq = 0x2e;
358 break;
359 default:
360 debug("Invalid ODT value.\n");
361 return -EINVAL;
362 }
363 } else {
364 debug("Invalid DRV value.\n");
365 return -EINVAL;
366 }
367 } else {
368 vref_mode_dq = 0x2; /* LPDDR3 */
369 vref_value_dq = 0x1f;
370 }
371 vref_mode_ac = 0x2;
372 vref_value_ac = 0x1f;
373 } else if (sdram_params->base.dramtype == DDR3) {
374 /* DDR3L */
375 vref_mode_dq = 0x1;
376 vref_value_dq = 0x1f;
377 vref_mode_ac = 0x1;
378 vref_value_ac = 0x1f;
379 } else {
380 debug("Unknown DRAM type.\n");
381 return -EINVAL;
382 }
383
384 reg_value = (vref_mode_dq << 9) | (0x1 << 8) | vref_value_dq;
385
386 /* PHY_913 PHY_PAD_VREF_CTRL_DQ_0 12bits offset_8 */
387 clrsetbits_le32(&denali_phy[913], 0xfff << 8, reg_value << 8);
388 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_1 12bits offset_0 */
389 clrsetbits_le32(&denali_phy[914], 0xfff, reg_value);
390 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_2 12bits offset_16 */
391 clrsetbits_le32(&denali_phy[914], 0xfff << 16, reg_value << 16);
392 /* PHY_915 PHY_PAD_VREF_CTRL_DQ_3 12bits offset_0 */
393 clrsetbits_le32(&denali_phy[915], 0xfff, reg_value);
394
395 reg_value = (vref_mode_ac << 9) | (0x1 << 8) | vref_value_ac;
396
397 /* PHY_915 PHY_PAD_VREF_CTRL_AC 12bits offset_16 */
398 clrsetbits_le32(&denali_phy[915], 0xfff << 16, reg_value << 16);
399
400 if (sdram_params->base.dramtype == LPDDR4)
401 mode_sel = 0x6;
402 else if (sdram_params->base.dramtype == LPDDR3)
403 mode_sel = 0x0;
404 else if (sdram_params->base.dramtype == DDR3)
405 mode_sel = 0x1;
406 else
407 return -EINVAL;
408
409 /* PHY_924 PHY_PAD_FDBK_DRIVE */
410 clrsetbits_le32(&denali_phy[924], 0x7 << 15, mode_sel << 15);
411 /* PHY_926 PHY_PAD_DATA_DRIVE */
412 clrsetbits_le32(&denali_phy[926], 0x7 << 6, mode_sel << 6);
413 /* PHY_927 PHY_PAD_DQS_DRIVE */
414 clrsetbits_le32(&denali_phy[927], 0x7 << 6, mode_sel << 6);
415 /* PHY_928 PHY_PAD_ADDR_DRIVE */
416 clrsetbits_le32(&denali_phy[928], 0x7 << 14, mode_sel << 14);
417 /* PHY_929 PHY_PAD_CLK_DRIVE */
418 clrsetbits_le32(&denali_phy[929], 0x7 << 14, mode_sel << 14);
419 /* PHY_935 PHY_PAD_CKE_DRIVE */
420 clrsetbits_le32(&denali_phy[935], 0x7 << 14, mode_sel << 14);
421 /* PHY_937 PHY_PAD_RST_DRIVE */
422 clrsetbits_le32(&denali_phy[937], 0x7 << 14, mode_sel << 14);
423 /* PHY_939 PHY_PAD_CS_DRIVE */
424 clrsetbits_le32(&denali_phy[939], 0x7 << 14, mode_sel << 14);
425
Kever Yang50fb9982017-02-22 16:56:35 +0800426 /* speed setting */
427 if (sdram_params->base.ddr_freq < 400)
428 speed = 0x0;
429 else if (sdram_params->base.ddr_freq < 800)
430 speed = 0x1;
431 else if (sdram_params->base.ddr_freq < 1200)
432 speed = 0x2;
433 else
434 speed = 0x3;
435
436 /* PHY_924 PHY_PAD_FDBK_DRIVE */
437 clrsetbits_le32(&denali_phy[924], 0x3 << 21, speed << 21);
438 /* PHY_926 PHY_PAD_DATA_DRIVE */
439 clrsetbits_le32(&denali_phy[926], 0x3 << 9, speed << 9);
440 /* PHY_927 PHY_PAD_DQS_DRIVE */
441 clrsetbits_le32(&denali_phy[927], 0x3 << 9, speed << 9);
442 /* PHY_928 PHY_PAD_ADDR_DRIVE */
443 clrsetbits_le32(&denali_phy[928], 0x3 << 17, speed << 17);
444 /* PHY_929 PHY_PAD_CLK_DRIVE */
445 clrsetbits_le32(&denali_phy[929], 0x3 << 17, speed << 17);
446 /* PHY_935 PHY_PAD_CKE_DRIVE */
447 clrsetbits_le32(&denali_phy[935], 0x3 << 17, speed << 17);
448 /* PHY_937 PHY_PAD_RST_DRIVE */
449 clrsetbits_le32(&denali_phy[937], 0x3 << 17, speed << 17);
450 /* PHY_939 PHY_PAD_CS_DRIVE */
451 clrsetbits_le32(&denali_phy[939], 0x3 << 17, speed << 17);
452
453 return 0;
454}
455
456static int pctl_cfg(const struct chan_info *chan, u32 channel,
457 const struct rk3399_sdram_params *sdram_params)
458{
459 u32 *denali_ctl = chan->pctl->denali_ctl;
460 u32 *denali_pi = chan->pi->denali_pi;
461 u32 *denali_phy = chan->publ->denali_phy;
462 const u32 *params_ctl = sdram_params->pctl_regs.denali_ctl;
463 const u32 *params_phy = sdram_params->phy_regs.denali_phy;
464 u32 tmp, tmp1, tmp2;
465 u32 pwrup_srefresh_exit;
466 int ret;
Philipp Tomsichc69b3092017-05-31 18:16:34 +0200467 const ulong timeout_ms = 200;
Kever Yang50fb9982017-02-22 16:56:35 +0800468
469 /*
470 * work around controller bug:
471 * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed
472 */
473 copy_to_reg(&denali_ctl[1], &params_ctl[1],
474 sizeof(struct rk3399_ddr_pctl_regs) - 4);
475 writel(params_ctl[0], &denali_ctl[0]);
476 copy_to_reg(denali_pi, &sdram_params->pi_regs.denali_pi[0],
477 sizeof(struct rk3399_ddr_pi_regs));
478 /* rank count need to set for init */
479 set_memory_map(chan, channel, sdram_params);
480
481 writel(sdram_params->phy_regs.denali_phy[910], &denali_phy[910]);
482 writel(sdram_params->phy_regs.denali_phy[911], &denali_phy[911]);
483 writel(sdram_params->phy_regs.denali_phy[912], &denali_phy[912]);
484
485 pwrup_srefresh_exit = readl(&denali_ctl[68]) & PWRUP_SREFRESH_EXIT;
486 clrbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT);
487
488 /* PHY_DLL_RST_EN */
489 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 1 << 24);
490
491 setbits_le32(&denali_pi[0], START);
492 setbits_le32(&denali_ctl[0], START);
493
Jagan Tekif676c7c2019-07-15 23:50:56 +0530494 /* Waiting for phy DLL lock */
Kever Yang50fb9982017-02-22 16:56:35 +0800495 while (1) {
496 tmp = readl(&denali_phy[920]);
497 tmp1 = readl(&denali_phy[921]);
498 tmp2 = readl(&denali_phy[922]);
499 if ((((tmp >> 16) & 0x1) == 0x1) &&
500 (((tmp1 >> 16) & 0x1) == 0x1) &&
501 (((tmp1 >> 0) & 0x1) == 0x1) &&
502 (((tmp2 >> 0) & 0x1) == 0x1))
503 break;
504 }
505
506 copy_to_reg(&denali_phy[896], &params_phy[896], (958 - 895) * 4);
507 copy_to_reg(&denali_phy[0], &params_phy[0], (90 - 0 + 1) * 4);
508 copy_to_reg(&denali_phy[128], &params_phy[128], (218 - 128 + 1) * 4);
509 copy_to_reg(&denali_phy[256], &params_phy[256], (346 - 256 + 1) * 4);
510 copy_to_reg(&denali_phy[384], &params_phy[384], (474 - 384 + 1) * 4);
511 copy_to_reg(&denali_phy[512], &params_phy[512], (549 - 512 + 1) * 4);
512 copy_to_reg(&denali_phy[640], &params_phy[640], (677 - 640 + 1) * 4);
513 copy_to_reg(&denali_phy[768], &params_phy[768], (805 - 768 + 1) * 4);
514 set_ds_odt(chan, sdram_params);
515
516 /*
517 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_84/212/340/468 offset_8
518 * dqs_tsel_wr_end[7:4] add Half cycle
519 */
520 tmp = (readl(&denali_phy[84]) >> 8) & 0xff;
521 clrsetbits_le32(&denali_phy[84], 0xff << 8, (tmp + 0x10) << 8);
522 tmp = (readl(&denali_phy[212]) >> 8) & 0xff;
523 clrsetbits_le32(&denali_phy[212], 0xff << 8, (tmp + 0x10) << 8);
524 tmp = (readl(&denali_phy[340]) >> 8) & 0xff;
525 clrsetbits_le32(&denali_phy[340], 0xff << 8, (tmp + 0x10) << 8);
526 tmp = (readl(&denali_phy[468]) >> 8) & 0xff;
527 clrsetbits_le32(&denali_phy[468], 0xff << 8, (tmp + 0x10) << 8);
528
529 /*
530 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_83/211/339/467 offset_8
531 * dq_tsel_wr_end[7:4] add Half cycle
532 */
533 tmp = (readl(&denali_phy[83]) >> 16) & 0xff;
534 clrsetbits_le32(&denali_phy[83], 0xff << 16, (tmp + 0x10) << 16);
535 tmp = (readl(&denali_phy[211]) >> 16) & 0xff;
536 clrsetbits_le32(&denali_phy[211], 0xff << 16, (tmp + 0x10) << 16);
537 tmp = (readl(&denali_phy[339]) >> 16) & 0xff;
538 clrsetbits_le32(&denali_phy[339], 0xff << 16, (tmp + 0x10) << 16);
539 tmp = (readl(&denali_phy[467]) >> 16) & 0xff;
540 clrsetbits_le32(&denali_phy[467], 0xff << 16, (tmp + 0x10) << 16);
541
542 ret = phy_io_config(chan, sdram_params);
543 if (ret)
544 return ret;
545
546 /* PHY_DLL_RST_EN */
547 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 0x2 << 24);
548
Jagan Tekif676c7c2019-07-15 23:50:56 +0530549 /* Waiting for PHY and DRAM init complete */
Philipp Tomsichc69b3092017-05-31 18:16:34 +0200550 tmp = get_timer(0);
551 do {
552 if (get_timer(tmp) > timeout_ms) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900553 pr_err("DRAM (%s): phy failed to lock within %ld ms\n",
Jagan Tekif676c7c2019-07-15 23:50:56 +0530554 __func__, timeout_ms);
Kever Yang50fb9982017-02-22 16:56:35 +0800555 return -ETIME;
Philipp Tomsichc69b3092017-05-31 18:16:34 +0200556 }
557 } while (!(readl(&denali_ctl[203]) & (1 << 3)));
558 debug("DRAM (%s): phy locked after %ld ms\n", __func__, get_timer(tmp));
Kever Yang50fb9982017-02-22 16:56:35 +0800559
560 clrsetbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT,
561 pwrup_srefresh_exit);
562 return 0;
563}
564
565static void select_per_cs_training_index(const struct chan_info *chan,
566 u32 rank)
567{
568 u32 *denali_phy = chan->publ->denali_phy;
569
570 /* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */
Jagan Tekif676c7c2019-07-15 23:50:56 +0530571 if ((readl(&denali_phy[84]) >> 16) & 1) {
Kever Yang50fb9982017-02-22 16:56:35 +0800572 /*
573 * PHY_8/136/264/392
574 * phy_per_cs_training_index_X 1bit offset_24
575 */
576 clrsetbits_le32(&denali_phy[8], 0x1 << 24, rank << 24);
577 clrsetbits_le32(&denali_phy[136], 0x1 << 24, rank << 24);
578 clrsetbits_le32(&denali_phy[264], 0x1 << 24, rank << 24);
579 clrsetbits_le32(&denali_phy[392], 0x1 << 24, rank << 24);
580 }
581}
582
583static void override_write_leveling_value(const struct chan_info *chan)
584{
585 u32 *denali_ctl = chan->pctl->denali_ctl;
586 u32 *denali_phy = chan->publ->denali_phy;
587 u32 byte;
588
589 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
590 setbits_le32(&denali_phy[896], 1);
591
592 /*
593 * PHY_8/136/264/392
594 * phy_per_cs_training_multicast_en_X 1bit offset_16
595 */
596 clrsetbits_le32(&denali_phy[8], 0x1 << 16, 1 << 16);
597 clrsetbits_le32(&denali_phy[136], 0x1 << 16, 1 << 16);
598 clrsetbits_le32(&denali_phy[264], 0x1 << 16, 1 << 16);
599 clrsetbits_le32(&denali_phy[392], 0x1 << 16, 1 << 16);
600
601 for (byte = 0; byte < 4; byte++)
602 clrsetbits_le32(&denali_phy[63 + (128 * byte)], 0xffff << 16,
603 0x200 << 16);
604
605 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
606 clrbits_le32(&denali_phy[896], 1);
607
608 /* CTL_200 ctrlupd_req 1bit offset_8 */
609 clrsetbits_le32(&denali_ctl[200], 0x1 << 8, 0x1 << 8);
610}
611
612static int data_training_ca(const struct chan_info *chan, u32 channel,
613 const struct rk3399_sdram_params *sdram_params)
614{
615 u32 *denali_pi = chan->pi->denali_pi;
616 u32 *denali_phy = chan->publ->denali_phy;
617 u32 i, tmp;
618 u32 obs_0, obs_1, obs_2, obs_err = 0;
619 u32 rank = sdram_params->ch[channel].rank;
620
621 for (i = 0; i < rank; i++) {
622 select_per_cs_training_index(chan, i);
623 /* PI_100 PI_CALVL_EN:RW:8:2 */
624 clrsetbits_le32(&denali_pi[100], 0x3 << 8, 0x2 << 8);
625 /* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */
626 clrsetbits_le32(&denali_pi[92],
627 (0x1 << 16) | (0x3 << 24),
628 (0x1 << 16) | (i << 24));
629
630 /* Waiting for training complete */
631 while (1) {
632 /* PI_174 PI_INT_STATUS:RD:8:18 */
633 tmp = readl(&denali_pi[174]) >> 8;
634 /*
635 * check status obs
636 * PHY_532/660/789 phy_adr_calvl_obs1_:0:32
637 */
638 obs_0 = readl(&denali_phy[532]);
639 obs_1 = readl(&denali_phy[660]);
640 obs_2 = readl(&denali_phy[788]);
641 if (((obs_0 >> 30) & 0x3) ||
642 ((obs_1 >> 30) & 0x3) ||
643 ((obs_2 >> 30) & 0x3))
644 obs_err = 1;
645 if ((((tmp >> 11) & 0x1) == 0x1) &&
646 (((tmp >> 13) & 0x1) == 0x1) &&
647 (((tmp >> 5) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +0530648 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800649 break;
650 else if ((((tmp >> 5) & 0x1) == 0x1) ||
651 (obs_err == 1))
652 return -EIO;
653 }
654 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
655 writel(0x00003f7c, (&denali_pi[175]));
656 }
657 clrbits_le32(&denali_pi[100], 0x3 << 8);
658
659 return 0;
660}
661
662static int data_training_wl(const struct chan_info *chan, u32 channel,
663 const struct rk3399_sdram_params *sdram_params)
664{
665 u32 *denali_pi = chan->pi->denali_pi;
666 u32 *denali_phy = chan->publ->denali_phy;
667 u32 i, tmp;
668 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
669 u32 rank = sdram_params->ch[channel].rank;
670
671 for (i = 0; i < rank; i++) {
672 select_per_cs_training_index(chan, i);
673 /* PI_60 PI_WRLVL_EN:RW:8:2 */
674 clrsetbits_le32(&denali_pi[60], 0x3 << 8, 0x2 << 8);
675 /* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */
676 clrsetbits_le32(&denali_pi[59],
677 (0x1 << 8) | (0x3 << 16),
678 (0x1 << 8) | (i << 16));
679
680 /* Waiting for training complete */
681 while (1) {
682 /* PI_174 PI_INT_STATUS:RD:8:18 */
683 tmp = readl(&denali_pi[174]) >> 8;
684
685 /*
686 * check status obs, if error maybe can not
687 * get leveling done PHY_40/168/296/424
688 * phy_wrlvl_status_obs_X:0:13
689 */
690 obs_0 = readl(&denali_phy[40]);
691 obs_1 = readl(&denali_phy[168]);
692 obs_2 = readl(&denali_phy[296]);
693 obs_3 = readl(&denali_phy[424]);
694 if (((obs_0 >> 12) & 0x1) ||
695 ((obs_1 >> 12) & 0x1) ||
696 ((obs_2 >> 12) & 0x1) ||
697 ((obs_3 >> 12) & 0x1))
698 obs_err = 1;
699 if ((((tmp >> 10) & 0x1) == 0x1) &&
700 (((tmp >> 13) & 0x1) == 0x1) &&
701 (((tmp >> 4) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +0530702 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800703 break;
704 else if ((((tmp >> 4) & 0x1) == 0x1) ||
705 (obs_err == 1))
706 return -EIO;
707 }
708 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
709 writel(0x00003f7c, (&denali_pi[175]));
710 }
711
712 override_write_leveling_value(chan);
713 clrbits_le32(&denali_pi[60], 0x3 << 8);
714
715 return 0;
716}
717
718static int data_training_rg(const struct chan_info *chan, u32 channel,
719 const struct rk3399_sdram_params *sdram_params)
720{
721 u32 *denali_pi = chan->pi->denali_pi;
722 u32 *denali_phy = chan->publ->denali_phy;
723 u32 i, tmp;
724 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
725 u32 rank = sdram_params->ch[channel].rank;
726
727 for (i = 0; i < rank; i++) {
728 select_per_cs_training_index(chan, i);
729 /* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */
730 clrsetbits_le32(&denali_pi[80], 0x3 << 24, 0x2 << 24);
731 /*
732 * PI_74 PI_RDLVL_GATE_REQ:WR:16:1
733 * PI_RDLVL_CS:RW:24:2
734 */
735 clrsetbits_le32(&denali_pi[74],
736 (0x1 << 16) | (0x3 << 24),
737 (0x1 << 16) | (i << 24));
738
739 /* Waiting for training complete */
740 while (1) {
741 /* PI_174 PI_INT_STATUS:RD:8:18 */
742 tmp = readl(&denali_pi[174]) >> 8;
743
744 /*
745 * check status obs
746 * PHY_43/171/299/427
747 * PHY_GTLVL_STATUS_OBS_x:16:8
748 */
749 obs_0 = readl(&denali_phy[43]);
750 obs_1 = readl(&denali_phy[171]);
751 obs_2 = readl(&denali_phy[299]);
752 obs_3 = readl(&denali_phy[427]);
753 if (((obs_0 >> (16 + 6)) & 0x3) ||
754 ((obs_1 >> (16 + 6)) & 0x3) ||
755 ((obs_2 >> (16 + 6)) & 0x3) ||
756 ((obs_3 >> (16 + 6)) & 0x3))
757 obs_err = 1;
758 if ((((tmp >> 9) & 0x1) == 0x1) &&
759 (((tmp >> 13) & 0x1) == 0x1) &&
760 (((tmp >> 3) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +0530761 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800762 break;
763 else if ((((tmp >> 3) & 0x1) == 0x1) ||
764 (obs_err == 1))
765 return -EIO;
766 }
767 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
768 writel(0x00003f7c, (&denali_pi[175]));
769 }
770 clrbits_le32(&denali_pi[80], 0x3 << 24);
771
772 return 0;
773}
774
775static int data_training_rl(const struct chan_info *chan, u32 channel,
776 const struct rk3399_sdram_params *sdram_params)
777{
778 u32 *denali_pi = chan->pi->denali_pi;
779 u32 i, tmp;
780 u32 rank = sdram_params->ch[channel].rank;
781
782 for (i = 0; i < rank; i++) {
783 select_per_cs_training_index(chan, i);
784 /* PI_80 PI_RDLVL_EN:RW:16:2 */
785 clrsetbits_le32(&denali_pi[80], 0x3 << 16, 0x2 << 16);
786 /* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */
787 clrsetbits_le32(&denali_pi[74],
788 (0x1 << 8) | (0x3 << 24),
789 (0x1 << 8) | (i << 24));
790
791 /* Waiting for training complete */
792 while (1) {
793 /* PI_174 PI_INT_STATUS:RD:8:18 */
794 tmp = readl(&denali_pi[174]) >> 8;
795
796 /*
797 * make sure status obs not report error bit
798 * PHY_46/174/302/430
799 * phy_rdlvl_status_obs_X:16:8
800 */
801 if ((((tmp >> 8) & 0x1) == 0x1) &&
802 (((tmp >> 13) & 0x1) == 0x1) &&
803 (((tmp >> 2) & 0x1) == 0x0))
804 break;
805 else if (((tmp >> 2) & 0x1) == 0x1)
806 return -EIO;
807 }
808 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
809 writel(0x00003f7c, (&denali_pi[175]));
810 }
811 clrbits_le32(&denali_pi[80], 0x3 << 16);
812
813 return 0;
814}
815
816static int data_training_wdql(const struct chan_info *chan, u32 channel,
817 const struct rk3399_sdram_params *sdram_params)
818{
819 u32 *denali_pi = chan->pi->denali_pi;
820 u32 i, tmp;
821 u32 rank = sdram_params->ch[channel].rank;
822
823 for (i = 0; i < rank; i++) {
824 select_per_cs_training_index(chan, i);
825 /*
826 * disable PI_WDQLVL_VREF_EN before wdq leveling?
827 * PI_181 PI_WDQLVL_VREF_EN:RW:8:1
828 */
829 clrbits_le32(&denali_pi[181], 0x1 << 8);
830 /* PI_124 PI_WDQLVL_EN:RW:16:2 */
831 clrsetbits_le32(&denali_pi[124], 0x3 << 16, 0x2 << 16);
832 /* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */
833 clrsetbits_le32(&denali_pi[121],
834 (0x1 << 8) | (0x3 << 16),
835 (0x1 << 8) | (i << 16));
836
837 /* Waiting for training complete */
838 while (1) {
839 /* PI_174 PI_INT_STATUS:RD:8:18 */
840 tmp = readl(&denali_pi[174]) >> 8;
841 if ((((tmp >> 12) & 0x1) == 0x1) &&
842 (((tmp >> 13) & 0x1) == 0x1) &&
843 (((tmp >> 6) & 0x1) == 0x0))
844 break;
845 else if (((tmp >> 6) & 0x1) == 0x1)
846 return -EIO;
847 }
848 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
849 writel(0x00003f7c, (&denali_pi[175]));
850 }
851 clrbits_le32(&denali_pi[124], 0x3 << 16);
852
853 return 0;
854}
855
856static int data_training(const struct chan_info *chan, u32 channel,
857 const struct rk3399_sdram_params *sdram_params,
858 u32 training_flag)
859{
860 u32 *denali_phy = chan->publ->denali_phy;
861
862 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
863 setbits_le32(&denali_phy[927], (1 << 22));
864
865 if (training_flag == PI_FULL_TRAINING) {
866 if (sdram_params->base.dramtype == LPDDR4) {
867 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
868 PI_READ_GATE_TRAINING |
869 PI_READ_LEVELING | PI_WDQ_LEVELING;
870 } else if (sdram_params->base.dramtype == LPDDR3) {
871 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
872 PI_READ_GATE_TRAINING;
873 } else if (sdram_params->base.dramtype == DDR3) {
874 training_flag = PI_WRITE_LEVELING |
875 PI_READ_GATE_TRAINING |
876 PI_READ_LEVELING;
877 }
878 }
879
880 /* ca training(LPDDR4,LPDDR3 support) */
881 if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING)
882 data_training_ca(chan, channel, sdram_params);
883
884 /* write leveling(LPDDR4,LPDDR3,DDR3 support) */
885 if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING)
886 data_training_wl(chan, channel, sdram_params);
887
888 /* read gate training(LPDDR4,LPDDR3,DDR3 support) */
889 if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING)
890 data_training_rg(chan, channel, sdram_params);
891
892 /* read leveling(LPDDR4,LPDDR3,DDR3 support) */
893 if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING)
894 data_training_rl(chan, channel, sdram_params);
895
896 /* wdq leveling(LPDDR4 support) */
897 if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING)
898 data_training_wdql(chan, channel, sdram_params);
899
900 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
901 clrbits_le32(&denali_phy[927], (1 << 22));
902
903 return 0;
904}
905
906static void set_ddrconfig(const struct chan_info *chan,
907 const struct rk3399_sdram_params *sdram_params,
908 unsigned char channel, u32 ddrconfig)
909{
910 /* only need to set ddrconfig */
911 struct rk3399_msch_regs *ddr_msch_regs = chan->msch;
912 unsigned int cs0_cap = 0;
913 unsigned int cs1_cap = 0;
914
915 cs0_cap = (1 << (sdram_params->ch[channel].cs0_row
916 + sdram_params->ch[channel].col
917 + sdram_params->ch[channel].bk
918 + sdram_params->ch[channel].bw - 20));
919 if (sdram_params->ch[channel].rank > 1)
920 cs1_cap = cs0_cap >> (sdram_params->ch[channel].cs0_row
921 - sdram_params->ch[channel].cs1_row);
922 if (sdram_params->ch[channel].row_3_4) {
923 cs0_cap = cs0_cap * 3 / 4;
924 cs1_cap = cs1_cap * 3 / 4;
925 }
926
927 writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf);
928 writel(((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8),
929 &ddr_msch_regs->ddrsize);
930}
931
932static void dram_all_config(struct dram_info *dram,
933 const struct rk3399_sdram_params *sdram_params)
934{
935 u32 sys_reg = 0;
936 unsigned int channel, idx;
937
938 sys_reg |= sdram_params->base.dramtype << SYS_REG_DDRTYPE_SHIFT;
939 sys_reg |= (sdram_params->base.num_channels - 1)
940 << SYS_REG_NUM_CH_SHIFT;
941 for (channel = 0, idx = 0;
942 (idx < sdram_params->base.num_channels) && (channel < 2);
943 channel++) {
944 const struct rk3399_sdram_channel *info =
945 &sdram_params->ch[channel];
946 struct rk3399_msch_regs *ddr_msch_regs;
947 const struct rk3399_msch_timings *noc_timing;
948
949 if (sdram_params->ch[channel].col == 0)
950 continue;
951 idx++;
952 sys_reg |= info->row_3_4 << SYS_REG_ROW_3_4_SHIFT(channel);
953 sys_reg |= 1 << SYS_REG_CHINFO_SHIFT(channel);
954 sys_reg |= (info->rank - 1) << SYS_REG_RANK_SHIFT(channel);
955 sys_reg |= (info->col - 9) << SYS_REG_COL_SHIFT(channel);
956 sys_reg |= info->bk == 3 ? 0 : 1 << SYS_REG_BK_SHIFT(channel);
Jagan Tekif676c7c2019-07-15 23:50:56 +0530957 sys_reg |= (info->cs0_row - 13) <<
958 SYS_REG_CS0_ROW_SHIFT(channel);
959 sys_reg |= (info->cs1_row - 13) <<
960 SYS_REG_CS1_ROW_SHIFT(channel);
Kever Yang50fb9982017-02-22 16:56:35 +0800961 sys_reg |= (2 >> info->bw) << SYS_REG_BW_SHIFT(channel);
962 sys_reg |= (2 >> info->dbw) << SYS_REG_DBW_SHIFT(channel);
963
964 ddr_msch_regs = dram->chan[channel].msch;
965 noc_timing = &sdram_params->ch[channel].noc_timings;
966 writel(noc_timing->ddrtiminga0,
967 &ddr_msch_regs->ddrtiminga0);
968 writel(noc_timing->ddrtimingb0,
969 &ddr_msch_regs->ddrtimingb0);
970 writel(noc_timing->ddrtimingc0,
971 &ddr_msch_regs->ddrtimingc0);
972 writel(noc_timing->devtodev0,
973 &ddr_msch_regs->devtodev0);
974 writel(noc_timing->ddrmode,
975 &ddr_msch_regs->ddrmode);
976
977 /* rank 1 memory clock disable (dfi_dram_clk_disable = 1) */
978 if (sdram_params->ch[channel].rank == 1)
979 setbits_le32(&dram->chan[channel].pctl->denali_ctl[276],
980 1 << 17);
981 }
982
983 writel(sys_reg, &dram->pmugrf->os_reg2);
984 rk_clrsetreg(&dram->pmusgrf->soc_con4, 0x1f << 10,
985 sdram_params->base.stride << 10);
986
987 /* reboot hold register set */
988 writel(PRESET_SGRF_HOLD(0) | PRESET_GPIO0_HOLD(1) |
989 PRESET_GPIO1_HOLD(1),
990 &dram->pmucru->pmucru_rstnhold_con[1]);
991 clrsetbits_le32(&dram->cru->glb_rst_con, 0x3, 0x3);
992}
993
994static int switch_to_phy_index1(struct dram_info *dram,
Jagan Tekif676c7c2019-07-15 23:50:56 +0530995 const struct rk3399_sdram_params *sdram_params)
Kever Yang50fb9982017-02-22 16:56:35 +0800996{
997 u32 channel;
998 u32 *denali_phy;
999 u32 ch_count = sdram_params->base.num_channels;
1000 int ret;
1001 int i = 0;
1002
1003 writel(RK_CLRSETBITS(0x03 << 4 | 1 << 2 | 1,
1004 1 << 4 | 1 << 2 | 1),
1005 &dram->cic->cic_ctrl0);
1006 while (!(readl(&dram->cic->cic_status0) & (1 << 2))) {
1007 mdelay(10);
1008 i++;
1009 if (i > 10) {
1010 debug("index1 frequency change overtime\n");
1011 return -ETIME;
1012 }
1013 }
1014
1015 i = 0;
1016 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &dram->cic->cic_ctrl0);
1017 while (!(readl(&dram->cic->cic_status0) & (1 << 0))) {
1018 mdelay(10);
Heinrich Schuchardt80516592018-03-18 12:10:55 +01001019 i++;
Kever Yang50fb9982017-02-22 16:56:35 +08001020 if (i > 10) {
1021 debug("index1 frequency done overtime\n");
1022 return -ETIME;
1023 }
1024 }
1025
1026 for (channel = 0; channel < ch_count; channel++) {
1027 denali_phy = dram->chan[channel].publ->denali_phy;
1028 clrsetbits_le32(&denali_phy[896], (0x3 << 8) | 1, 1 << 8);
1029 ret = data_training(&dram->chan[channel], channel,
Jagan Tekif676c7c2019-07-15 23:50:56 +05301030 sdram_params, PI_FULL_TRAINING);
Kever Yang50fb9982017-02-22 16:56:35 +08001031 if (ret) {
1032 debug("index1 training failed\n");
1033 return ret;
1034 }
1035 }
1036
1037 return 0;
1038}
1039
1040static int sdram_init(struct dram_info *dram,
1041 const struct rk3399_sdram_params *sdram_params)
1042{
1043 unsigned char dramtype = sdram_params->base.dramtype;
1044 unsigned int ddr_freq = sdram_params->base.ddr_freq;
1045 int channel;
1046
1047 debug("Starting SDRAM initialization...\n");
1048
Philipp Tomsich39dce4a2017-05-31 18:16:35 +02001049 if ((dramtype == DDR3 && ddr_freq > 933) ||
Kever Yang50fb9982017-02-22 16:56:35 +08001050 (dramtype == LPDDR3 && ddr_freq > 933) ||
1051 (dramtype == LPDDR4 && ddr_freq > 800)) {
1052 debug("SDRAM frequency is to high!");
1053 return -E2BIG;
1054 }
1055
1056 for (channel = 0; channel < 2; channel++) {
1057 const struct chan_info *chan = &dram->chan[channel];
1058 struct rk3399_ddr_publ_regs *publ = chan->publ;
1059
1060 phy_dll_bypass_set(publ, ddr_freq);
1061
1062 if (channel >= sdram_params->base.num_channels)
1063 continue;
1064
1065 if (pctl_cfg(chan, channel, sdram_params) != 0) {
1066 printf("pctl_cfg fail, reset\n");
1067 return -EIO;
1068 }
1069
1070 /* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */
1071 if (dramtype == LPDDR3)
1072 udelay(10);
1073
1074 if (data_training(chan, channel,
1075 sdram_params, PI_FULL_TRAINING)) {
1076 printf("SDRAM initialization failed, reset\n");
1077 return -EIO;
1078 }
1079
1080 set_ddrconfig(chan, sdram_params, channel,
1081 sdram_params->ch[channel].ddrconfig);
1082 }
1083 dram_all_config(dram, sdram_params);
1084 switch_to_phy_index1(dram, sdram_params);
1085
1086 debug("Finish SDRAM initialization...\n");
1087 return 0;
1088}
1089
1090static int rk3399_dmc_ofdata_to_platdata(struct udevice *dev)
1091{
1092#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1093 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
Kever Yang50fb9982017-02-22 16:56:35 +08001094 int ret;
1095
Philipp Tomsich0250c232017-06-07 18:46:03 +02001096 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
1097 (u32 *)&plat->sdram_params,
1098 sizeof(plat->sdram_params) / sizeof(u32));
Kever Yang50fb9982017-02-22 16:56:35 +08001099 if (ret) {
1100 printf("%s: Cannot read rockchip,sdram-params %d\n",
1101 __func__, ret);
1102 return ret;
1103 }
Masahiro Yamadae4873e32018-04-19 12:14:03 +09001104 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001105 if (ret)
1106 printf("%s: regmap failed %d\n", __func__, ret);
1107
1108#endif
1109 return 0;
1110}
1111
1112#if CONFIG_IS_ENABLED(OF_PLATDATA)
1113static int conv_of_platdata(struct udevice *dev)
1114{
1115 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1116 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1117 int ret;
1118
1119 ret = regmap_init_mem_platdata(dev, dtplat->reg,
Jagan Tekif676c7c2019-07-15 23:50:56 +05301120 ARRAY_SIZE(dtplat->reg) / 2,
1121 &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001122 if (ret)
1123 return ret;
1124
1125 return 0;
1126}
1127#endif
1128
1129static int rk3399_dmc_init(struct udevice *dev)
1130{
1131 struct dram_info *priv = dev_get_priv(dev);
1132 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1133 int ret;
1134#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1135 struct rk3399_sdram_params *params = &plat->sdram_params;
1136#else
1137 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1138 struct rk3399_sdram_params *params =
1139 (void *)dtplat->rockchip_sdram_params;
1140
1141 ret = conv_of_platdata(dev);
1142 if (ret)
1143 return ret;
1144#endif
1145
1146 priv->cic = syscon_get_first_range(ROCKCHIP_SYSCON_CIC);
1147 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
1148 priv->pmusgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
1149 priv->pmucru = rockchip_get_pmucru();
1150 priv->cru = rockchip_get_cru();
1151 priv->chan[0].pctl = regmap_get_range(plat->map, 0);
1152 priv->chan[0].pi = regmap_get_range(plat->map, 1);
1153 priv->chan[0].publ = regmap_get_range(plat->map, 2);
1154 priv->chan[0].msch = regmap_get_range(plat->map, 3);
1155 priv->chan[1].pctl = regmap_get_range(plat->map, 4);
1156 priv->chan[1].pi = regmap_get_range(plat->map, 5);
1157 priv->chan[1].publ = regmap_get_range(plat->map, 6);
1158 priv->chan[1].msch = regmap_get_range(plat->map, 7);
1159
1160 debug("con reg %p %p %p %p %p %p %p %p\n",
1161 priv->chan[0].pctl, priv->chan[0].pi,
1162 priv->chan[0].publ, priv->chan[0].msch,
1163 priv->chan[1].pctl, priv->chan[1].pi,
1164 priv->chan[1].publ, priv->chan[1].msch);
1165 debug("cru %p, cic %p, grf %p, sgrf %p, pmucru %p\n", priv->cru,
1166 priv->cic, priv->pmugrf, priv->pmusgrf, priv->pmucru);
1167#if CONFIG_IS_ENABLED(OF_PLATDATA)
1168 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->ddr_clk);
1169#else
1170 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
1171#endif
1172 if (ret) {
1173 printf("%s clk get failed %d\n", __func__, ret);
1174 return ret;
1175 }
1176 ret = clk_set_rate(&priv->ddr_clk, params->base.ddr_freq * MHz);
1177 if (ret < 0) {
1178 printf("%s clk set failed %d\n", __func__, ret);
1179 return ret;
1180 }
1181 ret = sdram_init(priv, params);
1182 if (ret < 0) {
1183 printf("%s DRAM init failed%d\n", __func__, ret);
1184 return ret;
1185 }
1186
1187 return 0;
1188}
1189#endif
1190
Kever Yang50fb9982017-02-22 16:56:35 +08001191static int rk3399_dmc_probe(struct udevice *dev)
1192{
Kever Yang7f347842019-04-01 17:20:53 +08001193#if defined(CONFIG_TPL_BUILD) || \
1194 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001195 if (rk3399_dmc_init(dev))
1196 return 0;
1197#else
1198 struct dram_info *priv = dev_get_priv(dev);
1199
1200 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
1201 debug("%s: pmugrf=%p\n", __func__, priv->pmugrf);
Kever Yang6c15a542017-06-23 16:11:06 +08001202 priv->info.base = CONFIG_SYS_SDRAM_BASE;
Jagan Tekif676c7c2019-07-15 23:50:56 +05301203 priv->info.size =
1204 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg2);
Kever Yang50fb9982017-02-22 16:56:35 +08001205#endif
1206 return 0;
1207}
1208
1209static int rk3399_dmc_get_info(struct udevice *dev, struct ram_info *info)
1210{
1211 struct dram_info *priv = dev_get_priv(dev);
1212
Kever Yangea61d142017-04-19 16:01:14 +08001213 *info = priv->info;
Kever Yang50fb9982017-02-22 16:56:35 +08001214
1215 return 0;
1216}
1217
1218static struct ram_ops rk3399_dmc_ops = {
1219 .get_info = rk3399_dmc_get_info,
1220};
1221
Kever Yang50fb9982017-02-22 16:56:35 +08001222static const struct udevice_id rk3399_dmc_ids[] = {
1223 { .compatible = "rockchip,rk3399-dmc" },
1224 { }
1225};
1226
1227U_BOOT_DRIVER(dmc_rk3399) = {
1228 .name = "rockchip_rk3399_dmc",
1229 .id = UCLASS_RAM,
1230 .of_match = rk3399_dmc_ids,
1231 .ops = &rk3399_dmc_ops,
Kever Yang7f347842019-04-01 17:20:53 +08001232#if defined(CONFIG_TPL_BUILD) || \
1233 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001234 .ofdata_to_platdata = rk3399_dmc_ofdata_to_platdata,
1235#endif
1236 .probe = rk3399_dmc_probe,
Kever Yang50fb9982017-02-22 16:56:35 +08001237 .priv_auto_alloc_size = sizeof(struct dram_info),
Kever Yang7f347842019-04-01 17:20:53 +08001238#if defined(CONFIG_TPL_BUILD) || \
1239 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001240 .platdata_auto_alloc_size = sizeof(struct rockchip_dmc_plat),
1241#endif
1242};