blob: 043b27737d51ccc4d7c50d268332efb71f3f19c4 [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
Kever Yang50fb9982017-02-22 16:56:35 +080038struct chan_info {
39 struct rk3399_ddr_pctl_regs *pctl;
40 struct rk3399_ddr_pi_regs *pi;
41 struct rk3399_ddr_publ_regs *publ;
42 struct rk3399_msch_regs *msch;
43};
44
45struct dram_info {
Kever Yang7f347842019-04-01 17:20:53 +080046#if defined(CONFIG_TPL_BUILD) || \
47 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +080048 struct chan_info chan[2];
49 struct clk ddr_clk;
50 struct rk3399_cru *cru;
51 struct rk3399_pmucru *pmucru;
52 struct rk3399_pmusgrf_regs *pmusgrf;
53 struct rk3399_ddr_cic_regs *cic;
54#endif
55 struct ram_info info;
56 struct rk3399_pmugrf_regs *pmugrf;
57};
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,
Jagan Tekia58ff792019-07-15 23:50:58 +0530114 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800115{
Jagan Tekia58ff792019-07-15 23:50:58 +0530116 const struct rk3399_sdram_channel *sdram_ch = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +0800117 u32 *denali_ctl = chan->pctl->denali_ctl;
118 u32 *denali_pi = chan->pi->denali_pi;
119 u32 cs_map;
120 u32 reduc;
121 u32 row;
122
123 /* Get row number from ddrconfig setting */
124 if (sdram_ch->ddrconfig < 2 || sdram_ch->ddrconfig == 4)
125 row = 16;
126 else if (sdram_ch->ddrconfig == 3)
127 row = 14;
128 else
129 row = 15;
130
131 cs_map = (sdram_ch->rank > 1) ? 3 : 1;
132 reduc = (sdram_ch->bw == 2) ? 0 : 1;
133
134 /* Set the dram configuration to ctrl */
135 clrsetbits_le32(&denali_ctl[191], 0xF, (12 - sdram_ch->col));
136 clrsetbits_le32(&denali_ctl[190], (0x3 << 16) | (0x7 << 24),
137 ((3 - sdram_ch->bk) << 16) |
138 ((16 - row) << 24));
139
140 clrsetbits_le32(&denali_ctl[196], 0x3 | (1 << 16),
141 cs_map | (reduc << 16));
142
143 /* PI_199 PI_COL_DIFF:RW:0:4 */
144 clrsetbits_le32(&denali_pi[199], 0xF, (12 - sdram_ch->col));
145
146 /* PI_155 PI_ROW_DIFF:RW:24:3 PI_BANK_DIFF:RW:16:2 */
147 clrsetbits_le32(&denali_pi[155], (0x3 << 16) | (0x7 << 24),
148 ((3 - sdram_ch->bk) << 16) |
149 ((16 - row) << 24));
150 /* PI_41 PI_CS_MAP:RW:24:4 */
151 clrsetbits_le32(&denali_pi[41], 0xf << 24, cs_map << 24);
Jagan Tekia58ff792019-07-15 23:50:58 +0530152 if (sdram_ch->rank == 1 && params->base.dramtype == DDR3)
Kever Yang50fb9982017-02-22 16:56:35 +0800153 writel(0x2EC7FFFF, &denali_pi[34]);
154}
155
156static void set_ds_odt(const struct chan_info *chan,
Jagan Tekia58ff792019-07-15 23:50:58 +0530157 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800158{
159 u32 *denali_phy = chan->publ->denali_phy;
160
161 u32 tsel_idle_en, tsel_wr_en, tsel_rd_en;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530162 u32 tsel_idle_select_p, tsel_rd_select_p;
163 u32 tsel_idle_select_n, tsel_rd_select_n;
164 u32 tsel_wr_select_dq_p, tsel_wr_select_ca_p;
165 u32 tsel_wr_select_dq_n, tsel_wr_select_ca_n;
Kever Yang50fb9982017-02-22 16:56:35 +0800166 u32 reg_value;
167
Jagan Tekia58ff792019-07-15 23:50:58 +0530168 if (params->base.dramtype == LPDDR4) {
Jagan Tekif676c7c2019-07-15 23:50:56 +0530169 tsel_rd_select_p = PHY_DRV_ODT_HI_Z;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530170 tsel_rd_select_n = PHY_DRV_ODT_240;
171
Jagan Tekif676c7c2019-07-15 23:50:56 +0530172 tsel_idle_select_p = PHY_DRV_ODT_HI_Z;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530173 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800174
Jagan Teki5c3251f2019-07-15 23:51:04 +0530175 tsel_wr_select_dq_p = PHY_DRV_ODT_40;
Jagan Teki36667142019-07-15 23:51:00 +0530176 tsel_wr_select_dq_n = PHY_DRV_ODT_40;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530177
178 tsel_wr_select_ca_p = PHY_DRV_ODT_40;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530179 tsel_wr_select_ca_n = PHY_DRV_ODT_40;
Jagan Tekia58ff792019-07-15 23:50:58 +0530180 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +0800181 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530182 tsel_rd_select_n = PHY_DRV_ODT_HI_Z;
183
Kever Yang50fb9982017-02-22 16:56:35 +0800184 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530185 tsel_idle_select_n = PHY_DRV_ODT_HI_Z;
Kever Yang50fb9982017-02-22 16:56:35 +0800186
Jagan Teki5c3251f2019-07-15 23:51:04 +0530187 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530188 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530189
190 tsel_wr_select_ca_p = PHY_DRV_ODT_48;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530191 tsel_wr_select_ca_n = PHY_DRV_ODT_48;
Kever Yang50fb9982017-02-22 16:56:35 +0800192 } else {
193 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530194 tsel_rd_select_n = PHY_DRV_ODT_240;
195
Kever Yang50fb9982017-02-22 16:56:35 +0800196 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530197 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800198
Jagan Teki5c3251f2019-07-15 23:51:04 +0530199 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530200 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530201
202 tsel_wr_select_ca_p = PHY_DRV_ODT_34_3;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530203 tsel_wr_select_ca_n = PHY_DRV_ODT_34_3;
Kever Yang50fb9982017-02-22 16:56:35 +0800204 }
205
Jagan Tekia58ff792019-07-15 23:50:58 +0530206 if (params->base.odt == 1)
Kever Yang50fb9982017-02-22 16:56:35 +0800207 tsel_rd_en = 1;
208 else
209 tsel_rd_en = 0;
210
211 tsel_wr_en = 0;
212 tsel_idle_en = 0;
213
214 /*
215 * phy_dq_tsel_select_X 24bits DENALI_PHY_6/134/262/390 offset_0
216 * sets termination values for read/idle cycles and drive strength
217 * for write cycles for DQ/DM
218 */
219 reg_value = tsel_rd_select_n | (tsel_rd_select_p << 0x4) |
Jagan Tekib3b34392019-07-15 23:51:01 +0530220 (tsel_wr_select_dq_n << 8) | (tsel_wr_select_dq_p << 12) |
Kever Yang50fb9982017-02-22 16:56:35 +0800221 (tsel_idle_select_n << 16) | (tsel_idle_select_p << 20);
222 clrsetbits_le32(&denali_phy[6], 0xffffff, reg_value);
223 clrsetbits_le32(&denali_phy[134], 0xffffff, reg_value);
224 clrsetbits_le32(&denali_phy[262], 0xffffff, reg_value);
225 clrsetbits_le32(&denali_phy[390], 0xffffff, reg_value);
226
227 /*
228 * phy_dqs_tsel_select_X 24bits DENALI_PHY_7/135/263/391 offset_0
229 * sets termination values for read/idle cycles and drive strength
230 * for write cycles for DQS
231 */
232 clrsetbits_le32(&denali_phy[7], 0xffffff, reg_value);
233 clrsetbits_le32(&denali_phy[135], 0xffffff, reg_value);
234 clrsetbits_le32(&denali_phy[263], 0xffffff, reg_value);
235 clrsetbits_le32(&denali_phy[391], 0xffffff, reg_value);
236
237 /* phy_adr_tsel_select_ 8bits DENALI_PHY_544/672/800 offset_0 */
Jagan Teki7caa3e92019-07-15 23:51:03 +0530238 reg_value = tsel_wr_select_ca_n | (tsel_wr_select_ca_p << 0x4);
Kever Yang50fb9982017-02-22 16:56:35 +0800239 clrsetbits_le32(&denali_phy[544], 0xff, reg_value);
240 clrsetbits_le32(&denali_phy[672], 0xff, reg_value);
241 clrsetbits_le32(&denali_phy[800], 0xff, reg_value);
242
243 /* phy_pad_addr_drive 8bits DENALI_PHY_928 offset_0 */
244 clrsetbits_le32(&denali_phy[928], 0xff, reg_value);
245
246 /* phy_pad_rst_drive 8bits DENALI_PHY_937 offset_0 */
247 clrsetbits_le32(&denali_phy[937], 0xff, reg_value);
248
249 /* phy_pad_cke_drive 8bits DENALI_PHY_935 offset_0 */
250 clrsetbits_le32(&denali_phy[935], 0xff, reg_value);
251
252 /* phy_pad_cs_drive 8bits DENALI_PHY_939 offset_0 */
253 clrsetbits_le32(&denali_phy[939], 0xff, reg_value);
254
255 /* phy_pad_clk_drive 8bits DENALI_PHY_929 offset_0 */
256 clrsetbits_le32(&denali_phy[929], 0xff, reg_value);
257
258 /* phy_pad_fdbk_drive 23bit DENALI_PHY_924/925 */
259 clrsetbits_le32(&denali_phy[924], 0xff,
Jagan Tekib3b34392019-07-15 23:51:01 +0530260 tsel_wr_select_dq_n | (tsel_wr_select_dq_p << 4));
Kever Yang50fb9982017-02-22 16:56:35 +0800261 clrsetbits_le32(&denali_phy[925], 0xff,
262 tsel_rd_select_n | (tsel_rd_select_p << 4));
263
264 /* phy_dq_tsel_enable_X 3bits DENALI_PHY_5/133/261/389 offset_16 */
265 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
266 << 16;
267 clrsetbits_le32(&denali_phy[5], 0x7 << 16, reg_value);
268 clrsetbits_le32(&denali_phy[133], 0x7 << 16, reg_value);
269 clrsetbits_le32(&denali_phy[261], 0x7 << 16, reg_value);
270 clrsetbits_le32(&denali_phy[389], 0x7 << 16, reg_value);
271
272 /* phy_dqs_tsel_enable_X 3bits DENALI_PHY_6/134/262/390 offset_24 */
273 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
274 << 24;
275 clrsetbits_le32(&denali_phy[6], 0x7 << 24, reg_value);
276 clrsetbits_le32(&denali_phy[134], 0x7 << 24, reg_value);
277 clrsetbits_le32(&denali_phy[262], 0x7 << 24, reg_value);
278 clrsetbits_le32(&denali_phy[390], 0x7 << 24, reg_value);
279
280 /* phy_adr_tsel_enable_ 1bit DENALI_PHY_518/646/774 offset_8 */
281 reg_value = tsel_wr_en << 8;
282 clrsetbits_le32(&denali_phy[518], 0x1 << 8, reg_value);
283 clrsetbits_le32(&denali_phy[646], 0x1 << 8, reg_value);
284 clrsetbits_le32(&denali_phy[774], 0x1 << 8, reg_value);
285
286 /* phy_pad_addr_term tsel 1bit DENALI_PHY_933 offset_17 */
287 reg_value = tsel_wr_en << 17;
288 clrsetbits_le32(&denali_phy[933], 0x1 << 17, reg_value);
289 /*
290 * pad_rst/cke/cs/clk_term tsel 1bits
291 * DENALI_PHY_938/936/940/934 offset_17
292 */
293 clrsetbits_le32(&denali_phy[938], 0x1 << 17, reg_value);
294 clrsetbits_le32(&denali_phy[936], 0x1 << 17, reg_value);
295 clrsetbits_le32(&denali_phy[940], 0x1 << 17, reg_value);
296 clrsetbits_le32(&denali_phy[934], 0x1 << 17, reg_value);
297
298 /* phy_pad_fdbk_term 1bit DENALI_PHY_930 offset_17 */
299 clrsetbits_le32(&denali_phy[930], 0x1 << 17, reg_value);
300}
301
302static int phy_io_config(const struct chan_info *chan,
Jagan Tekia58ff792019-07-15 23:50:58 +0530303 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800304{
305 u32 *denali_phy = chan->publ->denali_phy;
306 u32 vref_mode_dq, vref_value_dq, vref_mode_ac, vref_value_ac;
307 u32 mode_sel;
308 u32 reg_value;
309 u32 drv_value, odt_value;
310 u32 speed;
311
312 /* vref setting */
Jagan Tekia58ff792019-07-15 23:50:58 +0530313 if (params->base.dramtype == LPDDR4) {
Kever Yang50fb9982017-02-22 16:56:35 +0800314 /* LPDDR4 */
315 vref_mode_dq = 0x6;
316 vref_value_dq = 0x1f;
317 vref_mode_ac = 0x6;
318 vref_value_ac = 0x1f;
Jagan Tekia58ff792019-07-15 23:50:58 +0530319 } else if (params->base.dramtype == LPDDR3) {
320 if (params->base.odt == 1) {
Kever Yang50fb9982017-02-22 16:56:35 +0800321 vref_mode_dq = 0x5; /* LPDDR3 ODT */
322 drv_value = (readl(&denali_phy[6]) >> 12) & 0xf;
323 odt_value = (readl(&denali_phy[6]) >> 4) & 0xf;
324 if (drv_value == PHY_DRV_ODT_48) {
325 switch (odt_value) {
326 case PHY_DRV_ODT_240:
327 vref_value_dq = 0x16;
328 break;
329 case PHY_DRV_ODT_120:
330 vref_value_dq = 0x26;
331 break;
332 case PHY_DRV_ODT_60:
333 vref_value_dq = 0x36;
334 break;
335 default:
336 debug("Invalid ODT value.\n");
337 return -EINVAL;
338 }
339 } else if (drv_value == PHY_DRV_ODT_40) {
340 switch (odt_value) {
341 case PHY_DRV_ODT_240:
342 vref_value_dq = 0x19;
343 break;
344 case PHY_DRV_ODT_120:
345 vref_value_dq = 0x23;
346 break;
347 case PHY_DRV_ODT_60:
348 vref_value_dq = 0x31;
349 break;
350 default:
351 debug("Invalid ODT value.\n");
352 return -EINVAL;
353 }
354 } else if (drv_value == PHY_DRV_ODT_34_3) {
355 switch (odt_value) {
356 case PHY_DRV_ODT_240:
357 vref_value_dq = 0x17;
358 break;
359 case PHY_DRV_ODT_120:
360 vref_value_dq = 0x20;
361 break;
362 case PHY_DRV_ODT_60:
363 vref_value_dq = 0x2e;
364 break;
365 default:
366 debug("Invalid ODT value.\n");
367 return -EINVAL;
368 }
369 } else {
370 debug("Invalid DRV value.\n");
371 return -EINVAL;
372 }
373 } else {
374 vref_mode_dq = 0x2; /* LPDDR3 */
375 vref_value_dq = 0x1f;
376 }
377 vref_mode_ac = 0x2;
378 vref_value_ac = 0x1f;
Jagan Tekia58ff792019-07-15 23:50:58 +0530379 } else if (params->base.dramtype == DDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +0800380 /* DDR3L */
381 vref_mode_dq = 0x1;
382 vref_value_dq = 0x1f;
383 vref_mode_ac = 0x1;
384 vref_value_ac = 0x1f;
385 } else {
386 debug("Unknown DRAM type.\n");
387 return -EINVAL;
388 }
389
390 reg_value = (vref_mode_dq << 9) | (0x1 << 8) | vref_value_dq;
391
392 /* PHY_913 PHY_PAD_VREF_CTRL_DQ_0 12bits offset_8 */
393 clrsetbits_le32(&denali_phy[913], 0xfff << 8, reg_value << 8);
394 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_1 12bits offset_0 */
395 clrsetbits_le32(&denali_phy[914], 0xfff, reg_value);
396 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_2 12bits offset_16 */
397 clrsetbits_le32(&denali_phy[914], 0xfff << 16, reg_value << 16);
398 /* PHY_915 PHY_PAD_VREF_CTRL_DQ_3 12bits offset_0 */
399 clrsetbits_le32(&denali_phy[915], 0xfff, reg_value);
400
401 reg_value = (vref_mode_ac << 9) | (0x1 << 8) | vref_value_ac;
402
403 /* PHY_915 PHY_PAD_VREF_CTRL_AC 12bits offset_16 */
404 clrsetbits_le32(&denali_phy[915], 0xfff << 16, reg_value << 16);
405
Jagan Tekia58ff792019-07-15 23:50:58 +0530406 if (params->base.dramtype == LPDDR4)
Kever Yang50fb9982017-02-22 16:56:35 +0800407 mode_sel = 0x6;
Jagan Tekia58ff792019-07-15 23:50:58 +0530408 else if (params->base.dramtype == LPDDR3)
Kever Yang50fb9982017-02-22 16:56:35 +0800409 mode_sel = 0x0;
Jagan Tekia58ff792019-07-15 23:50:58 +0530410 else if (params->base.dramtype == DDR3)
Kever Yang50fb9982017-02-22 16:56:35 +0800411 mode_sel = 0x1;
412 else
413 return -EINVAL;
414
415 /* PHY_924 PHY_PAD_FDBK_DRIVE */
416 clrsetbits_le32(&denali_phy[924], 0x7 << 15, mode_sel << 15);
417 /* PHY_926 PHY_PAD_DATA_DRIVE */
418 clrsetbits_le32(&denali_phy[926], 0x7 << 6, mode_sel << 6);
419 /* PHY_927 PHY_PAD_DQS_DRIVE */
420 clrsetbits_le32(&denali_phy[927], 0x7 << 6, mode_sel << 6);
421 /* PHY_928 PHY_PAD_ADDR_DRIVE */
422 clrsetbits_le32(&denali_phy[928], 0x7 << 14, mode_sel << 14);
423 /* PHY_929 PHY_PAD_CLK_DRIVE */
424 clrsetbits_le32(&denali_phy[929], 0x7 << 14, mode_sel << 14);
425 /* PHY_935 PHY_PAD_CKE_DRIVE */
426 clrsetbits_le32(&denali_phy[935], 0x7 << 14, mode_sel << 14);
427 /* PHY_937 PHY_PAD_RST_DRIVE */
428 clrsetbits_le32(&denali_phy[937], 0x7 << 14, mode_sel << 14);
429 /* PHY_939 PHY_PAD_CS_DRIVE */
430 clrsetbits_le32(&denali_phy[939], 0x7 << 14, mode_sel << 14);
431
Kever Yang50fb9982017-02-22 16:56:35 +0800432 /* speed setting */
Jagan Tekia58ff792019-07-15 23:50:58 +0530433 if (params->base.ddr_freq < 400)
Kever Yang50fb9982017-02-22 16:56:35 +0800434 speed = 0x0;
Jagan Tekia58ff792019-07-15 23:50:58 +0530435 else if (params->base.ddr_freq < 800)
Kever Yang50fb9982017-02-22 16:56:35 +0800436 speed = 0x1;
Jagan Tekia58ff792019-07-15 23:50:58 +0530437 else if (params->base.ddr_freq < 1200)
Kever Yang50fb9982017-02-22 16:56:35 +0800438 speed = 0x2;
439 else
440 speed = 0x3;
441
442 /* PHY_924 PHY_PAD_FDBK_DRIVE */
443 clrsetbits_le32(&denali_phy[924], 0x3 << 21, speed << 21);
444 /* PHY_926 PHY_PAD_DATA_DRIVE */
445 clrsetbits_le32(&denali_phy[926], 0x3 << 9, speed << 9);
446 /* PHY_927 PHY_PAD_DQS_DRIVE */
447 clrsetbits_le32(&denali_phy[927], 0x3 << 9, speed << 9);
448 /* PHY_928 PHY_PAD_ADDR_DRIVE */
449 clrsetbits_le32(&denali_phy[928], 0x3 << 17, speed << 17);
450 /* PHY_929 PHY_PAD_CLK_DRIVE */
451 clrsetbits_le32(&denali_phy[929], 0x3 << 17, speed << 17);
452 /* PHY_935 PHY_PAD_CKE_DRIVE */
453 clrsetbits_le32(&denali_phy[935], 0x3 << 17, speed << 17);
454 /* PHY_937 PHY_PAD_RST_DRIVE */
455 clrsetbits_le32(&denali_phy[937], 0x3 << 17, speed << 17);
456 /* PHY_939 PHY_PAD_CS_DRIVE */
457 clrsetbits_le32(&denali_phy[939], 0x3 << 17, speed << 17);
458
459 return 0;
460}
461
462static int pctl_cfg(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530463 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800464{
465 u32 *denali_ctl = chan->pctl->denali_ctl;
466 u32 *denali_pi = chan->pi->denali_pi;
467 u32 *denali_phy = chan->publ->denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +0530468 const u32 *params_ctl = params->pctl_regs.denali_ctl;
469 const u32 *params_phy = params->phy_regs.denali_phy;
Kever Yang50fb9982017-02-22 16:56:35 +0800470 u32 tmp, tmp1, tmp2;
471 u32 pwrup_srefresh_exit;
472 int ret;
Philipp Tomsichc69b3092017-05-31 18:16:34 +0200473 const ulong timeout_ms = 200;
Kever Yang50fb9982017-02-22 16:56:35 +0800474
475 /*
476 * work around controller bug:
477 * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed
478 */
479 copy_to_reg(&denali_ctl[1], &params_ctl[1],
480 sizeof(struct rk3399_ddr_pctl_regs) - 4);
481 writel(params_ctl[0], &denali_ctl[0]);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530482
Jagan Tekia58ff792019-07-15 23:50:58 +0530483 copy_to_reg(denali_pi, &params->pi_regs.denali_pi[0],
Kever Yang50fb9982017-02-22 16:56:35 +0800484 sizeof(struct rk3399_ddr_pi_regs));
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530485
Kever Yang50fb9982017-02-22 16:56:35 +0800486 /* rank count need to set for init */
Jagan Tekia58ff792019-07-15 23:50:58 +0530487 set_memory_map(chan, channel, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800488
Jagan Tekia58ff792019-07-15 23:50:58 +0530489 writel(params->phy_regs.denali_phy[910], &denali_phy[910]);
490 writel(params->phy_regs.denali_phy[911], &denali_phy[911]);
491 writel(params->phy_regs.denali_phy[912], &denali_phy[912]);
Kever Yang50fb9982017-02-22 16:56:35 +0800492
493 pwrup_srefresh_exit = readl(&denali_ctl[68]) & PWRUP_SREFRESH_EXIT;
494 clrbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT);
495
496 /* PHY_DLL_RST_EN */
497 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 1 << 24);
498
499 setbits_le32(&denali_pi[0], START);
500 setbits_le32(&denali_ctl[0], START);
501
Jagan Tekif676c7c2019-07-15 23:50:56 +0530502 /* Waiting for phy DLL lock */
Kever Yang50fb9982017-02-22 16:56:35 +0800503 while (1) {
504 tmp = readl(&denali_phy[920]);
505 tmp1 = readl(&denali_phy[921]);
506 tmp2 = readl(&denali_phy[922]);
507 if ((((tmp >> 16) & 0x1) == 0x1) &&
508 (((tmp1 >> 16) & 0x1) == 0x1) &&
509 (((tmp1 >> 0) & 0x1) == 0x1) &&
510 (((tmp2 >> 0) & 0x1) == 0x1))
511 break;
512 }
513
514 copy_to_reg(&denali_phy[896], &params_phy[896], (958 - 895) * 4);
515 copy_to_reg(&denali_phy[0], &params_phy[0], (90 - 0 + 1) * 4);
516 copy_to_reg(&denali_phy[128], &params_phy[128], (218 - 128 + 1) * 4);
517 copy_to_reg(&denali_phy[256], &params_phy[256], (346 - 256 + 1) * 4);
518 copy_to_reg(&denali_phy[384], &params_phy[384], (474 - 384 + 1) * 4);
519 copy_to_reg(&denali_phy[512], &params_phy[512], (549 - 512 + 1) * 4);
520 copy_to_reg(&denali_phy[640], &params_phy[640], (677 - 640 + 1) * 4);
521 copy_to_reg(&denali_phy[768], &params_phy[768], (805 - 768 + 1) * 4);
Jagan Tekia58ff792019-07-15 23:50:58 +0530522 set_ds_odt(chan, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800523
524 /*
525 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_84/212/340/468 offset_8
526 * dqs_tsel_wr_end[7:4] add Half cycle
527 */
528 tmp = (readl(&denali_phy[84]) >> 8) & 0xff;
529 clrsetbits_le32(&denali_phy[84], 0xff << 8, (tmp + 0x10) << 8);
530 tmp = (readl(&denali_phy[212]) >> 8) & 0xff;
531 clrsetbits_le32(&denali_phy[212], 0xff << 8, (tmp + 0x10) << 8);
532 tmp = (readl(&denali_phy[340]) >> 8) & 0xff;
533 clrsetbits_le32(&denali_phy[340], 0xff << 8, (tmp + 0x10) << 8);
534 tmp = (readl(&denali_phy[468]) >> 8) & 0xff;
535 clrsetbits_le32(&denali_phy[468], 0xff << 8, (tmp + 0x10) << 8);
536
537 /*
538 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_83/211/339/467 offset_8
539 * dq_tsel_wr_end[7:4] add Half cycle
540 */
541 tmp = (readl(&denali_phy[83]) >> 16) & 0xff;
542 clrsetbits_le32(&denali_phy[83], 0xff << 16, (tmp + 0x10) << 16);
543 tmp = (readl(&denali_phy[211]) >> 16) & 0xff;
544 clrsetbits_le32(&denali_phy[211], 0xff << 16, (tmp + 0x10) << 16);
545 tmp = (readl(&denali_phy[339]) >> 16) & 0xff;
546 clrsetbits_le32(&denali_phy[339], 0xff << 16, (tmp + 0x10) << 16);
547 tmp = (readl(&denali_phy[467]) >> 16) & 0xff;
548 clrsetbits_le32(&denali_phy[467], 0xff << 16, (tmp + 0x10) << 16);
549
Jagan Tekia58ff792019-07-15 23:50:58 +0530550 ret = phy_io_config(chan, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800551 if (ret)
552 return ret;
553
554 /* PHY_DLL_RST_EN */
555 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 0x2 << 24);
556
Jagan Tekif676c7c2019-07-15 23:50:56 +0530557 /* Waiting for PHY and DRAM init complete */
Philipp Tomsichc69b3092017-05-31 18:16:34 +0200558 tmp = get_timer(0);
559 do {
560 if (get_timer(tmp) > timeout_ms) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900561 pr_err("DRAM (%s): phy failed to lock within %ld ms\n",
Jagan Tekif676c7c2019-07-15 23:50:56 +0530562 __func__, timeout_ms);
Kever Yang50fb9982017-02-22 16:56:35 +0800563 return -ETIME;
Philipp Tomsichc69b3092017-05-31 18:16:34 +0200564 }
565 } while (!(readl(&denali_ctl[203]) & (1 << 3)));
566 debug("DRAM (%s): phy locked after %ld ms\n", __func__, get_timer(tmp));
Kever Yang50fb9982017-02-22 16:56:35 +0800567
568 clrsetbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT,
569 pwrup_srefresh_exit);
570 return 0;
571}
572
573static void select_per_cs_training_index(const struct chan_info *chan,
574 u32 rank)
575{
576 u32 *denali_phy = chan->publ->denali_phy;
577
578 /* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */
Jagan Tekif676c7c2019-07-15 23:50:56 +0530579 if ((readl(&denali_phy[84]) >> 16) & 1) {
Kever Yang50fb9982017-02-22 16:56:35 +0800580 /*
581 * PHY_8/136/264/392
582 * phy_per_cs_training_index_X 1bit offset_24
583 */
584 clrsetbits_le32(&denali_phy[8], 0x1 << 24, rank << 24);
585 clrsetbits_le32(&denali_phy[136], 0x1 << 24, rank << 24);
586 clrsetbits_le32(&denali_phy[264], 0x1 << 24, rank << 24);
587 clrsetbits_le32(&denali_phy[392], 0x1 << 24, rank << 24);
588 }
589}
590
591static void override_write_leveling_value(const struct chan_info *chan)
592{
593 u32 *denali_ctl = chan->pctl->denali_ctl;
594 u32 *denali_phy = chan->publ->denali_phy;
595 u32 byte;
596
597 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
598 setbits_le32(&denali_phy[896], 1);
599
600 /*
601 * PHY_8/136/264/392
602 * phy_per_cs_training_multicast_en_X 1bit offset_16
603 */
604 clrsetbits_le32(&denali_phy[8], 0x1 << 16, 1 << 16);
605 clrsetbits_le32(&denali_phy[136], 0x1 << 16, 1 << 16);
606 clrsetbits_le32(&denali_phy[264], 0x1 << 16, 1 << 16);
607 clrsetbits_le32(&denali_phy[392], 0x1 << 16, 1 << 16);
608
609 for (byte = 0; byte < 4; byte++)
610 clrsetbits_le32(&denali_phy[63 + (128 * byte)], 0xffff << 16,
611 0x200 << 16);
612
613 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
614 clrbits_le32(&denali_phy[896], 1);
615
616 /* CTL_200 ctrlupd_req 1bit offset_8 */
617 clrsetbits_le32(&denali_ctl[200], 0x1 << 8, 0x1 << 8);
618}
619
620static int data_training_ca(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530621 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800622{
623 u32 *denali_pi = chan->pi->denali_pi;
624 u32 *denali_phy = chan->publ->denali_phy;
625 u32 i, tmp;
626 u32 obs_0, obs_1, obs_2, obs_err = 0;
Jagan Tekia58ff792019-07-15 23:50:58 +0530627 u32 rank = params->ch[channel].rank;
Kever Yang50fb9982017-02-22 16:56:35 +0800628
629 for (i = 0; i < rank; i++) {
630 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530631
Kever Yang50fb9982017-02-22 16:56:35 +0800632 /* PI_100 PI_CALVL_EN:RW:8:2 */
633 clrsetbits_le32(&denali_pi[100], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530634
Kever Yang50fb9982017-02-22 16:56:35 +0800635 /* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */
636 clrsetbits_le32(&denali_pi[92],
637 (0x1 << 16) | (0x3 << 24),
638 (0x1 << 16) | (i << 24));
639
640 /* Waiting for training complete */
641 while (1) {
642 /* PI_174 PI_INT_STATUS:RD:8:18 */
643 tmp = readl(&denali_pi[174]) >> 8;
644 /*
645 * check status obs
646 * PHY_532/660/789 phy_adr_calvl_obs1_:0:32
647 */
648 obs_0 = readl(&denali_phy[532]);
649 obs_1 = readl(&denali_phy[660]);
650 obs_2 = readl(&denali_phy[788]);
651 if (((obs_0 >> 30) & 0x3) ||
652 ((obs_1 >> 30) & 0x3) ||
653 ((obs_2 >> 30) & 0x3))
654 obs_err = 1;
655 if ((((tmp >> 11) & 0x1) == 0x1) &&
656 (((tmp >> 13) & 0x1) == 0x1) &&
657 (((tmp >> 5) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +0530658 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800659 break;
660 else if ((((tmp >> 5) & 0x1) == 0x1) ||
661 (obs_err == 1))
662 return -EIO;
663 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530664
Kever Yang50fb9982017-02-22 16:56:35 +0800665 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
666 writel(0x00003f7c, (&denali_pi[175]));
667 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530668
Kever Yang50fb9982017-02-22 16:56:35 +0800669 clrbits_le32(&denali_pi[100], 0x3 << 8);
670
671 return 0;
672}
673
674static int data_training_wl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530675 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800676{
677 u32 *denali_pi = chan->pi->denali_pi;
678 u32 *denali_phy = chan->publ->denali_phy;
679 u32 i, tmp;
680 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Tekia58ff792019-07-15 23:50:58 +0530681 u32 rank = params->ch[channel].rank;
Kever Yang50fb9982017-02-22 16:56:35 +0800682
683 for (i = 0; i < rank; i++) {
684 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530685
Kever Yang50fb9982017-02-22 16:56:35 +0800686 /* PI_60 PI_WRLVL_EN:RW:8:2 */
687 clrsetbits_le32(&denali_pi[60], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530688
Kever Yang50fb9982017-02-22 16:56:35 +0800689 /* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */
690 clrsetbits_le32(&denali_pi[59],
691 (0x1 << 8) | (0x3 << 16),
692 (0x1 << 8) | (i << 16));
693
694 /* Waiting for training complete */
695 while (1) {
696 /* PI_174 PI_INT_STATUS:RD:8:18 */
697 tmp = readl(&denali_pi[174]) >> 8;
698
699 /*
700 * check status obs, if error maybe can not
701 * get leveling done PHY_40/168/296/424
702 * phy_wrlvl_status_obs_X:0:13
703 */
704 obs_0 = readl(&denali_phy[40]);
705 obs_1 = readl(&denali_phy[168]);
706 obs_2 = readl(&denali_phy[296]);
707 obs_3 = readl(&denali_phy[424]);
708 if (((obs_0 >> 12) & 0x1) ||
709 ((obs_1 >> 12) & 0x1) ||
710 ((obs_2 >> 12) & 0x1) ||
711 ((obs_3 >> 12) & 0x1))
712 obs_err = 1;
713 if ((((tmp >> 10) & 0x1) == 0x1) &&
714 (((tmp >> 13) & 0x1) == 0x1) &&
715 (((tmp >> 4) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +0530716 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800717 break;
718 else if ((((tmp >> 4) & 0x1) == 0x1) ||
719 (obs_err == 1))
720 return -EIO;
721 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530722
Kever Yang50fb9982017-02-22 16:56:35 +0800723 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
724 writel(0x00003f7c, (&denali_pi[175]));
725 }
726
727 override_write_leveling_value(chan);
728 clrbits_le32(&denali_pi[60], 0x3 << 8);
729
730 return 0;
731}
732
733static int data_training_rg(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530734 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800735{
736 u32 *denali_pi = chan->pi->denali_pi;
737 u32 *denali_phy = chan->publ->denali_phy;
738 u32 i, tmp;
739 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Tekia58ff792019-07-15 23:50:58 +0530740 u32 rank = params->ch[channel].rank;
Kever Yang50fb9982017-02-22 16:56:35 +0800741
742 for (i = 0; i < rank; i++) {
743 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530744
Kever Yang50fb9982017-02-22 16:56:35 +0800745 /* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */
746 clrsetbits_le32(&denali_pi[80], 0x3 << 24, 0x2 << 24);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530747
Kever Yang50fb9982017-02-22 16:56:35 +0800748 /*
749 * PI_74 PI_RDLVL_GATE_REQ:WR:16:1
750 * PI_RDLVL_CS:RW:24:2
751 */
752 clrsetbits_le32(&denali_pi[74],
753 (0x1 << 16) | (0x3 << 24),
754 (0x1 << 16) | (i << 24));
755
756 /* Waiting for training complete */
757 while (1) {
758 /* PI_174 PI_INT_STATUS:RD:8:18 */
759 tmp = readl(&denali_pi[174]) >> 8;
760
761 /*
762 * check status obs
763 * PHY_43/171/299/427
764 * PHY_GTLVL_STATUS_OBS_x:16:8
765 */
766 obs_0 = readl(&denali_phy[43]);
767 obs_1 = readl(&denali_phy[171]);
768 obs_2 = readl(&denali_phy[299]);
769 obs_3 = readl(&denali_phy[427]);
770 if (((obs_0 >> (16 + 6)) & 0x3) ||
771 ((obs_1 >> (16 + 6)) & 0x3) ||
772 ((obs_2 >> (16 + 6)) & 0x3) ||
773 ((obs_3 >> (16 + 6)) & 0x3))
774 obs_err = 1;
775 if ((((tmp >> 9) & 0x1) == 0x1) &&
776 (((tmp >> 13) & 0x1) == 0x1) &&
777 (((tmp >> 3) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +0530778 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800779 break;
780 else if ((((tmp >> 3) & 0x1) == 0x1) ||
781 (obs_err == 1))
782 return -EIO;
783 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530784
Kever Yang50fb9982017-02-22 16:56:35 +0800785 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
786 writel(0x00003f7c, (&denali_pi[175]));
787 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530788
Kever Yang50fb9982017-02-22 16:56:35 +0800789 clrbits_le32(&denali_pi[80], 0x3 << 24);
790
791 return 0;
792}
793
794static int data_training_rl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530795 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800796{
797 u32 *denali_pi = chan->pi->denali_pi;
798 u32 i, tmp;
Jagan Tekia58ff792019-07-15 23:50:58 +0530799 u32 rank = params->ch[channel].rank;
Kever Yang50fb9982017-02-22 16:56:35 +0800800
801 for (i = 0; i < rank; i++) {
802 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530803
Kever Yang50fb9982017-02-22 16:56:35 +0800804 /* PI_80 PI_RDLVL_EN:RW:16:2 */
805 clrsetbits_le32(&denali_pi[80], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530806
Kever Yang50fb9982017-02-22 16:56:35 +0800807 /* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */
808 clrsetbits_le32(&denali_pi[74],
809 (0x1 << 8) | (0x3 << 24),
810 (0x1 << 8) | (i << 24));
811
812 /* Waiting for training complete */
813 while (1) {
814 /* PI_174 PI_INT_STATUS:RD:8:18 */
815 tmp = readl(&denali_pi[174]) >> 8;
816
817 /*
818 * make sure status obs not report error bit
819 * PHY_46/174/302/430
820 * phy_rdlvl_status_obs_X:16:8
821 */
822 if ((((tmp >> 8) & 0x1) == 0x1) &&
823 (((tmp >> 13) & 0x1) == 0x1) &&
824 (((tmp >> 2) & 0x1) == 0x0))
825 break;
826 else if (((tmp >> 2) & 0x1) == 0x1)
827 return -EIO;
828 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530829
Kever Yang50fb9982017-02-22 16:56:35 +0800830 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
831 writel(0x00003f7c, (&denali_pi[175]));
832 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530833
Kever Yang50fb9982017-02-22 16:56:35 +0800834 clrbits_le32(&denali_pi[80], 0x3 << 16);
835
836 return 0;
837}
838
839static int data_training_wdql(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530840 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800841{
842 u32 *denali_pi = chan->pi->denali_pi;
843 u32 i, tmp;
Jagan Tekia58ff792019-07-15 23:50:58 +0530844 u32 rank = params->ch[channel].rank;
Kever Yang50fb9982017-02-22 16:56:35 +0800845
846 for (i = 0; i < rank; i++) {
847 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530848
Kever Yang50fb9982017-02-22 16:56:35 +0800849 /*
850 * disable PI_WDQLVL_VREF_EN before wdq leveling?
851 * PI_181 PI_WDQLVL_VREF_EN:RW:8:1
852 */
853 clrbits_le32(&denali_pi[181], 0x1 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530854
Kever Yang50fb9982017-02-22 16:56:35 +0800855 /* PI_124 PI_WDQLVL_EN:RW:16:2 */
856 clrsetbits_le32(&denali_pi[124], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530857
Kever Yang50fb9982017-02-22 16:56:35 +0800858 /* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */
859 clrsetbits_le32(&denali_pi[121],
860 (0x1 << 8) | (0x3 << 16),
861 (0x1 << 8) | (i << 16));
862
863 /* Waiting for training complete */
864 while (1) {
865 /* PI_174 PI_INT_STATUS:RD:8:18 */
866 tmp = readl(&denali_pi[174]) >> 8;
867 if ((((tmp >> 12) & 0x1) == 0x1) &&
868 (((tmp >> 13) & 0x1) == 0x1) &&
869 (((tmp >> 6) & 0x1) == 0x0))
870 break;
871 else if (((tmp >> 6) & 0x1) == 0x1)
872 return -EIO;
873 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530874
Kever Yang50fb9982017-02-22 16:56:35 +0800875 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
876 writel(0x00003f7c, (&denali_pi[175]));
877 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530878
Kever Yang50fb9982017-02-22 16:56:35 +0800879 clrbits_le32(&denali_pi[124], 0x3 << 16);
880
881 return 0;
882}
883
884static int data_training(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530885 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +0800886 u32 training_flag)
887{
888 u32 *denali_phy = chan->publ->denali_phy;
889
890 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
891 setbits_le32(&denali_phy[927], (1 << 22));
892
893 if (training_flag == PI_FULL_TRAINING) {
Jagan Tekia58ff792019-07-15 23:50:58 +0530894 if (params->base.dramtype == LPDDR4) {
Kever Yang50fb9982017-02-22 16:56:35 +0800895 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
896 PI_READ_GATE_TRAINING |
897 PI_READ_LEVELING | PI_WDQ_LEVELING;
Jagan Tekia58ff792019-07-15 23:50:58 +0530898 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +0800899 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
900 PI_READ_GATE_TRAINING;
Jagan Tekia58ff792019-07-15 23:50:58 +0530901 } else if (params->base.dramtype == DDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +0800902 training_flag = PI_WRITE_LEVELING |
903 PI_READ_GATE_TRAINING |
904 PI_READ_LEVELING;
905 }
906 }
907
908 /* ca training(LPDDR4,LPDDR3 support) */
909 if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING)
Jagan Tekia58ff792019-07-15 23:50:58 +0530910 data_training_ca(chan, channel, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800911
912 /* write leveling(LPDDR4,LPDDR3,DDR3 support) */
913 if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING)
Jagan Tekia58ff792019-07-15 23:50:58 +0530914 data_training_wl(chan, channel, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800915
916 /* read gate training(LPDDR4,LPDDR3,DDR3 support) */
917 if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING)
Jagan Tekia58ff792019-07-15 23:50:58 +0530918 data_training_rg(chan, channel, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800919
920 /* read leveling(LPDDR4,LPDDR3,DDR3 support) */
921 if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING)
Jagan Tekia58ff792019-07-15 23:50:58 +0530922 data_training_rl(chan, channel, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800923
924 /* wdq leveling(LPDDR4 support) */
925 if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING)
Jagan Tekia58ff792019-07-15 23:50:58 +0530926 data_training_wdql(chan, channel, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800927
928 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
929 clrbits_le32(&denali_phy[927], (1 << 22));
930
931 return 0;
932}
933
934static void set_ddrconfig(const struct chan_info *chan,
Jagan Tekia58ff792019-07-15 23:50:58 +0530935 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +0800936 unsigned char channel, u32 ddrconfig)
937{
938 /* only need to set ddrconfig */
939 struct rk3399_msch_regs *ddr_msch_regs = chan->msch;
940 unsigned int cs0_cap = 0;
941 unsigned int cs1_cap = 0;
942
Jagan Tekia58ff792019-07-15 23:50:58 +0530943 cs0_cap = (1 << (params->ch[channel].cs0_row
944 + params->ch[channel].col
945 + params->ch[channel].bk
946 + params->ch[channel].bw - 20));
947 if (params->ch[channel].rank > 1)
948 cs1_cap = cs0_cap >> (params->ch[channel].cs0_row
949 - params->ch[channel].cs1_row);
950 if (params->ch[channel].row_3_4) {
Kever Yang50fb9982017-02-22 16:56:35 +0800951 cs0_cap = cs0_cap * 3 / 4;
952 cs1_cap = cs1_cap * 3 / 4;
953 }
954
955 writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf);
956 writel(((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8),
957 &ddr_msch_regs->ddrsize);
958}
959
960static void dram_all_config(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +0530961 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800962{
963 u32 sys_reg = 0;
964 unsigned int channel, idx;
965
Jagan Tekia58ff792019-07-15 23:50:58 +0530966 sys_reg |= params->base.dramtype << SYS_REG_DDRTYPE_SHIFT;
967 sys_reg |= (params->base.num_channels - 1) << SYS_REG_NUM_CH_SHIFT;
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530968
Kever Yang50fb9982017-02-22 16:56:35 +0800969 for (channel = 0, idx = 0;
Jagan Tekia58ff792019-07-15 23:50:58 +0530970 (idx < params->base.num_channels) && (channel < 2);
Kever Yang50fb9982017-02-22 16:56:35 +0800971 channel++) {
Jagan Tekia58ff792019-07-15 23:50:58 +0530972 const struct rk3399_sdram_channel *info = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +0800973 struct rk3399_msch_regs *ddr_msch_regs;
974 const struct rk3399_msch_timings *noc_timing;
975
Jagan Tekia58ff792019-07-15 23:50:58 +0530976 if (params->ch[channel].col == 0)
Kever Yang50fb9982017-02-22 16:56:35 +0800977 continue;
978 idx++;
979 sys_reg |= info->row_3_4 << SYS_REG_ROW_3_4_SHIFT(channel);
980 sys_reg |= 1 << SYS_REG_CHINFO_SHIFT(channel);
981 sys_reg |= (info->rank - 1) << SYS_REG_RANK_SHIFT(channel);
982 sys_reg |= (info->col - 9) << SYS_REG_COL_SHIFT(channel);
983 sys_reg |= info->bk == 3 ? 0 : 1 << SYS_REG_BK_SHIFT(channel);
Jagan Tekif676c7c2019-07-15 23:50:56 +0530984 sys_reg |= (info->cs0_row - 13) <<
985 SYS_REG_CS0_ROW_SHIFT(channel);
986 sys_reg |= (info->cs1_row - 13) <<
987 SYS_REG_CS1_ROW_SHIFT(channel);
Kever Yang50fb9982017-02-22 16:56:35 +0800988 sys_reg |= (2 >> info->bw) << SYS_REG_BW_SHIFT(channel);
989 sys_reg |= (2 >> info->dbw) << SYS_REG_DBW_SHIFT(channel);
990
991 ddr_msch_regs = dram->chan[channel].msch;
Jagan Tekia58ff792019-07-15 23:50:58 +0530992 noc_timing = &params->ch[channel].noc_timings;
Kever Yang50fb9982017-02-22 16:56:35 +0800993 writel(noc_timing->ddrtiminga0,
994 &ddr_msch_regs->ddrtiminga0);
995 writel(noc_timing->ddrtimingb0,
996 &ddr_msch_regs->ddrtimingb0);
997 writel(noc_timing->ddrtimingc0,
998 &ddr_msch_regs->ddrtimingc0);
999 writel(noc_timing->devtodev0,
1000 &ddr_msch_regs->devtodev0);
1001 writel(noc_timing->ddrmode,
1002 &ddr_msch_regs->ddrmode);
1003
1004 /* rank 1 memory clock disable (dfi_dram_clk_disable = 1) */
Jagan Tekia58ff792019-07-15 23:50:58 +05301005 if (params->ch[channel].rank == 1)
Kever Yang50fb9982017-02-22 16:56:35 +08001006 setbits_le32(&dram->chan[channel].pctl->denali_ctl[276],
1007 1 << 17);
1008 }
1009
1010 writel(sys_reg, &dram->pmugrf->os_reg2);
1011 rk_clrsetreg(&dram->pmusgrf->soc_con4, 0x1f << 10,
Jagan Tekia58ff792019-07-15 23:50:58 +05301012 params->base.stride << 10);
Kever Yang50fb9982017-02-22 16:56:35 +08001013
1014 /* reboot hold register set */
1015 writel(PRESET_SGRF_HOLD(0) | PRESET_GPIO0_HOLD(1) |
1016 PRESET_GPIO1_HOLD(1),
1017 &dram->pmucru->pmucru_rstnhold_con[1]);
1018 clrsetbits_le32(&dram->cru->glb_rst_con, 0x3, 0x3);
1019}
1020
1021static int switch_to_phy_index1(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +05301022 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001023{
1024 u32 channel;
1025 u32 *denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +05301026 u32 ch_count = params->base.num_channels;
Kever Yang50fb9982017-02-22 16:56:35 +08001027 int ret;
1028 int i = 0;
1029
1030 writel(RK_CLRSETBITS(0x03 << 4 | 1 << 2 | 1,
1031 1 << 4 | 1 << 2 | 1),
1032 &dram->cic->cic_ctrl0);
1033 while (!(readl(&dram->cic->cic_status0) & (1 << 2))) {
1034 mdelay(10);
1035 i++;
1036 if (i > 10) {
1037 debug("index1 frequency change overtime\n");
1038 return -ETIME;
1039 }
1040 }
1041
1042 i = 0;
1043 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &dram->cic->cic_ctrl0);
1044 while (!(readl(&dram->cic->cic_status0) & (1 << 0))) {
1045 mdelay(10);
Heinrich Schuchardt80516592018-03-18 12:10:55 +01001046 i++;
Kever Yang50fb9982017-02-22 16:56:35 +08001047 if (i > 10) {
1048 debug("index1 frequency done overtime\n");
1049 return -ETIME;
1050 }
1051 }
1052
1053 for (channel = 0; channel < ch_count; channel++) {
1054 denali_phy = dram->chan[channel].publ->denali_phy;
1055 clrsetbits_le32(&denali_phy[896], (0x3 << 8) | 1, 1 << 8);
1056 ret = data_training(&dram->chan[channel], channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301057 params, PI_FULL_TRAINING);
Kever Yang50fb9982017-02-22 16:56:35 +08001058 if (ret) {
1059 debug("index1 training failed\n");
1060 return ret;
1061 }
1062 }
1063
1064 return 0;
1065}
1066
1067static int sdram_init(struct dram_info *dram,
Jagan Tekia58ff792019-07-15 23:50:58 +05301068 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001069{
Jagan Tekia58ff792019-07-15 23:50:58 +05301070 unsigned char dramtype = params->base.dramtype;
1071 unsigned int ddr_freq = params->base.ddr_freq;
Kever Yang50fb9982017-02-22 16:56:35 +08001072 int channel;
Jagan Teki2ef77ed2019-07-15 23:50:59 +05301073 int ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001074
1075 debug("Starting SDRAM initialization...\n");
1076
Philipp Tomsich39dce4a2017-05-31 18:16:35 +02001077 if ((dramtype == DDR3 && ddr_freq > 933) ||
Kever Yang50fb9982017-02-22 16:56:35 +08001078 (dramtype == LPDDR3 && ddr_freq > 933) ||
1079 (dramtype == LPDDR4 && ddr_freq > 800)) {
1080 debug("SDRAM frequency is to high!");
1081 return -E2BIG;
1082 }
1083
1084 for (channel = 0; channel < 2; channel++) {
1085 const struct chan_info *chan = &dram->chan[channel];
1086 struct rk3399_ddr_publ_regs *publ = chan->publ;
1087
1088 phy_dll_bypass_set(publ, ddr_freq);
1089
Jagan Tekia58ff792019-07-15 23:50:58 +05301090 if (channel >= params->base.num_channels)
Kever Yang50fb9982017-02-22 16:56:35 +08001091 continue;
1092
Jagan Teki2ef77ed2019-07-15 23:50:59 +05301093 ret = pctl_cfg(chan, channel, params);
1094 if (ret < 0) {
1095 printf("%s: pctl config failed\n", __func__);
1096 return ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001097 }
1098
1099 /* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */
1100 if (dramtype == LPDDR3)
1101 udelay(10);
1102
Jagan Tekia58ff792019-07-15 23:50:58 +05301103 if (data_training(chan, channel, params, PI_FULL_TRAINING)) {
Kever Yang50fb9982017-02-22 16:56:35 +08001104 printf("SDRAM initialization failed, reset\n");
1105 return -EIO;
1106 }
1107
Jagan Tekia58ff792019-07-15 23:50:58 +05301108 set_ddrconfig(chan, params, channel,
1109 params->ch[channel].ddrconfig);
Kever Yang50fb9982017-02-22 16:56:35 +08001110 }
Jagan Tekia58ff792019-07-15 23:50:58 +05301111 dram_all_config(dram, params);
1112 switch_to_phy_index1(dram, params);
Kever Yang50fb9982017-02-22 16:56:35 +08001113
1114 debug("Finish SDRAM initialization...\n");
1115 return 0;
1116}
1117
1118static int rk3399_dmc_ofdata_to_platdata(struct udevice *dev)
1119{
1120#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1121 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
Kever Yang50fb9982017-02-22 16:56:35 +08001122 int ret;
1123
Philipp Tomsich0250c232017-06-07 18:46:03 +02001124 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
1125 (u32 *)&plat->sdram_params,
1126 sizeof(plat->sdram_params) / sizeof(u32));
Kever Yang50fb9982017-02-22 16:56:35 +08001127 if (ret) {
1128 printf("%s: Cannot read rockchip,sdram-params %d\n",
1129 __func__, ret);
1130 return ret;
1131 }
Masahiro Yamadae4873e32018-04-19 12:14:03 +09001132 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001133 if (ret)
1134 printf("%s: regmap failed %d\n", __func__, ret);
1135
1136#endif
1137 return 0;
1138}
1139
1140#if CONFIG_IS_ENABLED(OF_PLATDATA)
1141static int conv_of_platdata(struct udevice *dev)
1142{
1143 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1144 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1145 int ret;
1146
1147 ret = regmap_init_mem_platdata(dev, dtplat->reg,
Jagan Tekif676c7c2019-07-15 23:50:56 +05301148 ARRAY_SIZE(dtplat->reg) / 2,
1149 &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08001150 if (ret)
1151 return ret;
1152
1153 return 0;
1154}
1155#endif
1156
1157static int rk3399_dmc_init(struct udevice *dev)
1158{
1159 struct dram_info *priv = dev_get_priv(dev);
1160 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1161 int ret;
1162#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1163 struct rk3399_sdram_params *params = &plat->sdram_params;
1164#else
1165 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1166 struct rk3399_sdram_params *params =
1167 (void *)dtplat->rockchip_sdram_params;
1168
1169 ret = conv_of_platdata(dev);
1170 if (ret)
1171 return ret;
1172#endif
1173
1174 priv->cic = syscon_get_first_range(ROCKCHIP_SYSCON_CIC);
1175 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
1176 priv->pmusgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
1177 priv->pmucru = rockchip_get_pmucru();
1178 priv->cru = rockchip_get_cru();
1179 priv->chan[0].pctl = regmap_get_range(plat->map, 0);
1180 priv->chan[0].pi = regmap_get_range(plat->map, 1);
1181 priv->chan[0].publ = regmap_get_range(plat->map, 2);
1182 priv->chan[0].msch = regmap_get_range(plat->map, 3);
1183 priv->chan[1].pctl = regmap_get_range(plat->map, 4);
1184 priv->chan[1].pi = regmap_get_range(plat->map, 5);
1185 priv->chan[1].publ = regmap_get_range(plat->map, 6);
1186 priv->chan[1].msch = regmap_get_range(plat->map, 7);
1187
1188 debug("con reg %p %p %p %p %p %p %p %p\n",
1189 priv->chan[0].pctl, priv->chan[0].pi,
1190 priv->chan[0].publ, priv->chan[0].msch,
1191 priv->chan[1].pctl, priv->chan[1].pi,
1192 priv->chan[1].publ, priv->chan[1].msch);
1193 debug("cru %p, cic %p, grf %p, sgrf %p, pmucru %p\n", priv->cru,
1194 priv->cic, priv->pmugrf, priv->pmusgrf, priv->pmucru);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301195
Kever Yang50fb9982017-02-22 16:56:35 +08001196#if CONFIG_IS_ENABLED(OF_PLATDATA)
1197 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->ddr_clk);
1198#else
1199 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
1200#endif
1201 if (ret) {
1202 printf("%s clk get failed %d\n", __func__, ret);
1203 return ret;
1204 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301205
Kever Yang50fb9982017-02-22 16:56:35 +08001206 ret = clk_set_rate(&priv->ddr_clk, params->base.ddr_freq * MHz);
1207 if (ret < 0) {
1208 printf("%s clk set failed %d\n", __func__, ret);
1209 return ret;
1210 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301211
Kever Yang50fb9982017-02-22 16:56:35 +08001212 ret = sdram_init(priv, params);
1213 if (ret < 0) {
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301214 printf("%s DRAM init failed %d\n", __func__, ret);
Kever Yang50fb9982017-02-22 16:56:35 +08001215 return ret;
1216 }
1217
1218 return 0;
1219}
1220#endif
1221
Kever Yang50fb9982017-02-22 16:56:35 +08001222static int rk3399_dmc_probe(struct udevice *dev)
1223{
Kever Yang7f347842019-04-01 17:20:53 +08001224#if defined(CONFIG_TPL_BUILD) || \
1225 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001226 if (rk3399_dmc_init(dev))
1227 return 0;
1228#else
1229 struct dram_info *priv = dev_get_priv(dev);
1230
1231 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301232 debug("%s: pmugrf = %p\n", __func__, priv->pmugrf);
Kever Yang6c15a542017-06-23 16:11:06 +08001233 priv->info.base = CONFIG_SYS_SDRAM_BASE;
Jagan Tekif676c7c2019-07-15 23:50:56 +05301234 priv->info.size =
1235 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg2);
Kever Yang50fb9982017-02-22 16:56:35 +08001236#endif
1237 return 0;
1238}
1239
1240static int rk3399_dmc_get_info(struct udevice *dev, struct ram_info *info)
1241{
1242 struct dram_info *priv = dev_get_priv(dev);
1243
Kever Yangea61d142017-04-19 16:01:14 +08001244 *info = priv->info;
Kever Yang50fb9982017-02-22 16:56:35 +08001245
1246 return 0;
1247}
1248
1249static struct ram_ops rk3399_dmc_ops = {
1250 .get_info = rk3399_dmc_get_info,
1251};
1252
Kever Yang50fb9982017-02-22 16:56:35 +08001253static const struct udevice_id rk3399_dmc_ids[] = {
1254 { .compatible = "rockchip,rk3399-dmc" },
1255 { }
1256};
1257
1258U_BOOT_DRIVER(dmc_rk3399) = {
1259 .name = "rockchip_rk3399_dmc",
1260 .id = UCLASS_RAM,
1261 .of_match = rk3399_dmc_ids,
1262 .ops = &rk3399_dmc_ops,
Kever Yang7f347842019-04-01 17:20:53 +08001263#if defined(CONFIG_TPL_BUILD) || \
1264 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001265 .ofdata_to_platdata = rk3399_dmc_ofdata_to_platdata,
1266#endif
1267 .probe = rk3399_dmc_probe,
Kever Yang50fb9982017-02-22 16:56:35 +08001268 .priv_auto_alloc_size = sizeof(struct dram_info),
Kever Yang7f347842019-04-01 17:20:53 +08001269#if defined(CONFIG_TPL_BUILD) || \
1270 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Kever Yang50fb9982017-02-22 16:56:35 +08001271 .platdata_auto_alloc_size = sizeof(struct rockchip_dmc_plat),
1272#endif
1273};