blob: ab087275fe249ef079a47675f286950ce9399cee [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Philipp Tomsichd21a4d82017-06-23 00:12:05 +02002/*
3 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
Philipp Tomsichd21a4d82017-06-23 00:12:05 +02004 */
5
Tom Rinidec7ea02024-05-20 13:35:03 -06006#include <config.h>
Philipp Tomsichd21a4d82017-06-23 00:12:05 +02007#include <clk.h>
8#include <dm.h>
Simon Glassf11478f2019-12-28 10:45:07 -07009#include <hang.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Philipp Tomsichd21a4d82017-06-23 00:12:05 +020011#include <dt-bindings/memory/rk3368-dmc.h>
12#include <dt-structs.h>
13#include <ram.h>
14#include <regmap.h>
15#include <syscon.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080016#include <asm/arch-rockchip/clock.h>
17#include <asm/arch-rockchip/cru_rk3368.h>
18#include <asm/arch-rockchip/grf_rk3368.h>
Quentin Schulz5e38edb2024-03-11 13:01:56 +010019#include <asm/arch-rockchip/hardware.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080020#include <asm/arch-rockchip/ddr_rk3368.h>
Kever Yange47db832019-11-15 11:04:33 +080021#include <asm/arch-rockchip/sdram.h>
Kever Yangcdbb38a2019-11-15 11:04:32 +080022#include <asm/arch-rockchip/sdram_rk3288.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060023#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060024#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070025#include <linux/err.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060026#include <linux/printk.h>
Philipp Tomsichd21a4d82017-06-23 00:12:05 +020027
Philipp Tomsichd21a4d82017-06-23 00:12:05 +020028struct dram_info {
29 struct ram_info info;
30 struct clk ddr_clk;
31 struct rk3368_cru *cru;
32 struct rk3368_grf *grf;
33 struct rk3368_ddr_pctl *pctl;
34 struct rk3368_ddrphy *phy;
35 struct rk3368_pmu_grf *pmugrf;
36 struct rk3368_msch *msch;
37};
38
39struct rk3368_sdram_params {
40#if CONFIG_IS_ENABLED(OF_PLATDATA)
41 struct dtd_rockchip_rk3368_dmc of_plat;
42#endif
43 struct rk3288_sdram_pctl_timing pctl_timing;
44 u32 trefi_mem_ddr3;
45 struct rk3288_sdram_channel chan;
46 struct regmap *map;
47 u32 ddr_freq;
48 u32 memory_schedule;
49 u32 ddr_speed_bin;
50 u32 tfaw_mult;
51};
52
53/* PTCL bits */
54enum {
55 /* PCTL_DFISTCFG0 */
56 DFI_INIT_START = BIT(0),
57 DFI_DATA_BYTE_DISABLE_EN = BIT(2),
58
59 /* PCTL_DFISTCFG1 */
60 DFI_DRAM_CLK_SR_EN = BIT(0),
61 DFI_DRAM_CLK_DPD_EN = BIT(1),
62 ODT_LEN_BL8_W_SHIFT = 16,
63
64 /* PCTL_DFISTCFG2 */
65 DFI_PARITY_INTR_EN = BIT(0),
66 DFI_PARITY_EN = BIT(1),
67
68 /* PCTL_DFILPCFG0 */
69 TLP_RESP_TIME_SHIFT = 16,
70 LP_SR_EN = BIT(8),
71 LP_PD_EN = BIT(0),
72
73 /* PCTL_DFIODTCFG */
74 RANK0_ODT_WRITE_SEL = BIT(3),
75 RANK1_ODT_WRITE_SEL = BIT(11),
76
77 /* PCTL_SCFG */
78 HW_LOW_POWER_EN = BIT(0),
79
80 /* PCTL_MCMD */
81 START_CMD = BIT(31),
82 MCMD_RANK0 = BIT(20),
83 MCMD_RANK1 = BIT(21),
84 DESELECT_CMD = 0,
85 PREA_CMD,
86 REF_CMD,
87 MRS_CMD,
88 ZQCS_CMD,
89 ZQCL_CMD,
90 RSTL_CMD,
91 MRR_CMD = 8,
92 DPDE_CMD,
93
94 /* PCTL_POWCTL */
95 POWER_UP_START = BIT(0),
96
97 /* PCTL_POWSTAT */
98 POWER_UP_DONE = BIT(0),
99
100 /* PCTL_SCTL */
101 INIT_STATE = 0,
102 CFG_STATE,
103 GO_STATE,
104 SLEEP_STATE,
105 WAKEUP_STATE,
106
107 /* PCTL_STAT */
108 LP_TRIG_SHIFT = 4,
109 LP_TRIG_MASK = 7,
110 PCTL_STAT_MSK = 7,
111 INIT_MEM = 0,
112 CONFIG,
Tom Rini364d0022023-01-10 11:19:45 -0500113 CFG_REQ,
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200114 ACCESS,
115 ACCESS_REQ,
116 LOW_POWER,
117 LOW_POWER_ENTRY_REQ,
118 LOW_POWER_EXIT_REQ,
119
120 /* PCTL_MCFG */
121 DDR2_DDR3_BL_8 = BIT(0),
122 DDR3_EN = BIT(5),
123 TFAW_TRRD_MULT4 = (0 << 18),
124 TFAW_TRRD_MULT5 = (1 << 18),
125 TFAW_TRRD_MULT6 = (2 << 18),
126};
127
128#define DDR3_MR0_WR(n) \
129 ((n <= 8) ? ((n - 4) << 9) : (((n >> 1) & 0x7) << 9))
130#define DDR3_MR0_CL(n) \
131 ((((n - 4) & 0x7) << 4) | (((n - 4) & 0x8) >> 2))
132#define DDR3_MR0_BL8 \
133 (0 << 0)
134#define DDR3_MR0_DLL_RESET \
135 (1 << 8)
136#define DDR3_MR1_RTT120OHM \
137 ((0 << 9) | (1 << 6) | (0 << 2))
138#define DDR3_MR2_TWL(n) \
139 (((n - 5) & 0x7) << 3)
140
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200141#ifdef CONFIG_TPL_BUILD
142
143static void ddr_set_noc_spr_err_stall(struct rk3368_grf *grf, bool enable)
144{
145 if (enable)
146 rk_setreg(&grf->ddrc0_con0, NOC_RSP_ERR_STALL);
147 else
148 rk_clrreg(&grf->ddrc0_con0, NOC_RSP_ERR_STALL);
149}
150
151static void ddr_set_ddr3_mode(struct rk3368_grf *grf, bool ddr3_mode)
152{
153 if (ddr3_mode)
154 rk_setreg(&grf->ddrc0_con0, MSCH0_MAINDDR3_DDR3);
155 else
156 rk_clrreg(&grf->ddrc0_con0, MSCH0_MAINDDR3_DDR3);
157}
158
159static void ddrphy_config(struct rk3368_ddrphy *phy,
160 u32 tcl, u32 tal, u32 tcwl)
161{
162 int i;
163
164 /* Set to DDR3 mode */
165 clrsetbits_le32(&phy->reg[1], 0x3, 0x0);
166
167 /* DDRPHY_REGB: CL, AL */
168 clrsetbits_le32(&phy->reg[0xb], 0xff, tcl << 4 | tal);
169 /* DDRPHY_REGC: CWL */
170 clrsetbits_le32(&phy->reg[0xc], 0x0f, tcwl);
171
172 /* Update drive-strength */
173 writel(0xcc, &phy->reg[0x11]);
174 writel(0xaa, &phy->reg[0x16]);
175 /*
176 * Update NRCOMP/PRCOMP for all 4 channels (for details of all
177 * affected registers refer to the documentation of DDRPHY_REG20
178 * and DDRPHY_REG21 in the RK3368 TRM.
179 */
180 for (i = 0; i < 4; ++i) {
181 writel(0xcc, &phy->reg[0x20 + i * 0x10]);
182 writel(0x44, &phy->reg[0x21 + i * 0x10]);
183 }
184
185 /* Enable write-leveling calibration bypass */
186 setbits_le32(&phy->reg[2], BIT(3));
187}
188
189static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
190{
191 int i;
192
193 for (i = 0; i < n / sizeof(u32); i++)
194 writel(*src++, dest++);
195}
196
197static void send_command(struct rk3368_ddr_pctl *pctl, u32 rank, u32 cmd)
198{
199 u32 mcmd = START_CMD | cmd | rank;
200
201 debug("%s: writing %x to MCMD\n", __func__, mcmd);
202 writel(mcmd, &pctl->mcmd);
203 while (readl(&pctl->mcmd) & START_CMD)
204 /* spin */;
205}
206
207static void send_mrs(struct rk3368_ddr_pctl *pctl,
208 u32 rank, u32 mr_num, u32 mr_data)
209{
210 u32 mcmd = START_CMD | MRS_CMD | rank | (mr_num << 17) | (mr_data << 4);
211
212 debug("%s: writing %x to MCMD\n", __func__, mcmd);
213 writel(mcmd, &pctl->mcmd);
214 while (readl(&pctl->mcmd) & START_CMD)
215 /* spin */;
216}
217
218static int memory_init(struct rk3368_ddr_pctl *pctl,
219 struct rk3368_sdram_params *params)
220{
221 u32 mr[4];
222 const ulong timeout_ms = 500;
223 ulong tmp;
224
225 /*
226 * Power up DRAM by DDR_PCTL_POWCTL[0] register of PCTL and
227 * wait power up DRAM finish with DDR_PCTL_POWSTAT[0] register
228 * of PCTL.
229 */
230 writel(POWER_UP_START, &pctl->powctl);
231
232 tmp = get_timer(0);
233 do {
234 if (get_timer(tmp) > timeout_ms) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900235 pr_err("%s: POWER_UP_START did not complete in %ld ms\n",
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200236 __func__, timeout_ms);
237 return -ETIME;
238 }
239 } while (!(readl(&pctl->powstat) & POWER_UP_DONE));
240
241 /* Configure MR0 through MR3 */
242 mr[0] = DDR3_MR0_WR(params->pctl_timing.twr) |
243 DDR3_MR0_CL(params->pctl_timing.tcl) |
244 DDR3_MR0_DLL_RESET;
245 mr[1] = DDR3_MR1_RTT120OHM;
246 mr[2] = DDR3_MR2_TWL(params->pctl_timing.tcwl);
247 mr[3] = 0;
248
249 /*
250 * Also see RK3368 Technical Reference Manual:
251 * "16.6.2 Initialization (DDR3 Initialization Sequence)"
252 */
253 send_command(pctl, MCMD_RANK0 | MCMD_RANK1, DESELECT_CMD);
254 udelay(1);
255 send_command(pctl, MCMD_RANK0 | MCMD_RANK1, PREA_CMD);
256 send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 2, mr[2]);
257 send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 3, mr[3]);
258 send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 1, mr[1]);
259 send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 0, mr[0]);
260 send_command(pctl, MCMD_RANK0 | MCMD_RANK1, ZQCL_CMD);
261
262 return 0;
263}
264
265static void move_to_config_state(struct rk3368_ddr_pctl *pctl)
266{
267 /*
268 * Also see RK3368 Technical Reference Manual:
269 * "16.6.1 State transition of PCTL (Moving to Config State)"
270 */
271 u32 state = readl(&pctl->stat) & PCTL_STAT_MSK;
272
273 switch (state) {
274 case LOW_POWER:
275 writel(WAKEUP_STATE, &pctl->sctl);
276 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != ACCESS)
277 /* spin */;
278
279 /* fall-through */
280 case ACCESS:
281 case INIT_MEM:
282 writel(CFG_STATE, &pctl->sctl);
283 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != CONFIG)
284 /* spin */;
285 break;
286
287 case CONFIG:
288 return;
289
290 default:
291 break;
292 }
293}
294
295static void move_to_access_state(struct rk3368_ddr_pctl *pctl)
296{
297 /*
298 * Also see RK3368 Technical Reference Manual:
299 * "16.6.1 State transition of PCTL (Moving to Access State)"
300 */
301 u32 state = readl(&pctl->stat) & PCTL_STAT_MSK;
302
303 switch (state) {
304 case LOW_POWER:
305 if (((readl(&pctl->stat) >> LP_TRIG_SHIFT) &
306 LP_TRIG_MASK) == 1)
307 return;
308
309 writel(WAKEUP_STATE, &pctl->sctl);
310 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != ACCESS)
311 /* spin */;
312
313 /* fall-through */
314 case INIT_MEM:
315 writel(CFG_STATE, &pctl->sctl);
316 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != CONFIG)
317 /* spin */;
318
319 /* fall-through */
320 case CONFIG:
321 writel(GO_STATE, &pctl->sctl);
322 while ((readl(&pctl->stat) & PCTL_STAT_MSK) == CONFIG)
323 /* spin */;
324 break;
325
326 case ACCESS:
327 return;
328
329 default:
330 break;
331 }
332}
333
334static void ddrctl_reset(struct rk3368_cru *cru)
335{
336 const u32 ctl_reset = BIT(3) | BIT(2);
337 const u32 phy_reset = BIT(1) | BIT(0);
338
339 /*
340 * The PHY reset should be released before the PCTL reset.
341 *
342 * Note that the following sequence (including the number of
343 * us to delay between releasing the PHY and PCTL reset) has
344 * been adapted per feedback received from Rockchips, so do
345 * not try to optimise.
346 */
347 rk_setreg(&cru->softrst_con[10], ctl_reset | phy_reset);
348 udelay(1);
349 rk_clrreg(&cru->softrst_con[10], phy_reset);
350 udelay(5);
351 rk_clrreg(&cru->softrst_con[10], ctl_reset);
352}
353
354static void ddrphy_reset(struct rk3368_ddrphy *ddrphy)
355{
356 /*
357 * The analog part of the PHY should be release at least 1000
358 * DRAM cycles before the digital part of the PHY (waiting for
359 * 5us will ensure this for a DRAM clock as low as 200MHz).
360 */
361 clrbits_le32(&ddrphy->reg[0], BIT(3) | BIT(2));
362 udelay(1);
363 setbits_le32(&ddrphy->reg[0], BIT(2));
364 udelay(5);
365 setbits_le32(&ddrphy->reg[0], BIT(3));
366}
367
368static void ddrphy_config_delays(struct rk3368_ddrphy *ddrphy, u32 freq)
369{
370 u32 dqs_dll_delay;
371
372 setbits_le32(&ddrphy->reg[0x13], BIT(4));
373 clrbits_le32(&ddrphy->reg[0x14], BIT(3));
374
375 setbits_le32(&ddrphy->reg[0x26], BIT(4));
376 clrbits_le32(&ddrphy->reg[0x27], BIT(3));
377
378 setbits_le32(&ddrphy->reg[0x36], BIT(4));
379 clrbits_le32(&ddrphy->reg[0x37], BIT(3));
380
381 setbits_le32(&ddrphy->reg[0x46], BIT(4));
382 clrbits_le32(&ddrphy->reg[0x47], BIT(3));
383
384 setbits_le32(&ddrphy->reg[0x56], BIT(4));
385 clrbits_le32(&ddrphy->reg[0x57], BIT(3));
386
387 if (freq <= 400000000)
388 setbits_le32(&ddrphy->reg[0xa4], 0x1f);
389 else
390 clrbits_le32(&ddrphy->reg[0xa4], 0x1f);
391
392 if (freq < 681000000)
393 dqs_dll_delay = 3; /* 67.5 degree delay */
394 else
395 dqs_dll_delay = 2; /* 45 degree delay */
396
397 writel(dqs_dll_delay, &ddrphy->reg[0x28]);
398 writel(dqs_dll_delay, &ddrphy->reg[0x38]);
399 writel(dqs_dll_delay, &ddrphy->reg[0x48]);
400 writel(dqs_dll_delay, &ddrphy->reg[0x58]);
401}
402
403static int dfi_cfg(struct rk3368_ddr_pctl *pctl)
404{
405 const ulong timeout_ms = 200;
406 ulong tmp;
407
408 writel(DFI_DATA_BYTE_DISABLE_EN, &pctl->dfistcfg0);
409
410 writel(DFI_DRAM_CLK_SR_EN | DFI_DRAM_CLK_DPD_EN,
411 &pctl->dfistcfg1);
412 writel(DFI_PARITY_INTR_EN | DFI_PARITY_EN, &pctl->dfistcfg2);
413 writel(7 << TLP_RESP_TIME_SHIFT | LP_SR_EN | LP_PD_EN,
414 &pctl->dfilpcfg0);
415
416 writel(1, &pctl->dfitphyupdtype0);
417
418 writel(0x1f, &pctl->dfitphyrdlat);
419 writel(0, &pctl->dfitphywrdata);
420 writel(0, &pctl->dfiupdcfg); /* phyupd and ctrlupd disabled */
421
422 setbits_le32(&pctl->dfistcfg0, DFI_INIT_START);
423
424 tmp = get_timer(0);
425 do {
426 if (get_timer(tmp) > timeout_ms) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900427 pr_err("%s: DFI init did not complete within %ld ms\n",
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200428 __func__, timeout_ms);
429 return -ETIME;
430 }
431 } while ((readl(&pctl->dfiststat0) & 1) == 0);
432
433 return 0;
434}
435
436static inline u32 ps_to_tCK(const u32 ps, const ulong freq)
437{
438 const ulong MHz = 1000000;
439 return DIV_ROUND_UP(ps * freq, 1000000 * MHz);
440}
441
442static inline u32 ns_to_tCK(const u32 ns, const ulong freq)
443{
444 return ps_to_tCK(ns * 1000, freq);
445}
446
447static inline u32 tCK_to_ps(const ulong tCK, const ulong freq)
448{
449 const ulong MHz = 1000000;
450 return DIV_ROUND_UP(tCK * 1000000 * MHz, freq);
451}
452
453static int pctl_calc_timings(struct rk3368_sdram_params *params,
454 ulong freq)
455{
456 struct rk3288_sdram_pctl_timing *pctl_timing = &params->pctl_timing;
457 const ulong MHz = 1000000;
458 u32 tccd;
459 u32 tfaw_as_ps;
460
461 if (params->ddr_speed_bin != DDR3_1600K) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900462 pr_err("%s: unimplemented DDR3 speed bin %d\n",
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200463 __func__, params->ddr_speed_bin);
464 return -1;
465 }
466
467 /* PCTL is clocked at 1/2 the DRAM clock; err on the side of caution */
468 pctl_timing->togcnt1u = DIV_ROUND_UP(freq, 2 * MHz);
469 pctl_timing->togcnt100n = DIV_ROUND_UP(freq / 10, 2 * MHz);
470
471 pctl_timing->tinit = 200; /* 200 usec */
472 pctl_timing->trsth = 500; /* 500 usec */
473 pctl_timing->trefi = 78; /* 7.8usec = 78 * 100ns */
474 params->trefi_mem_ddr3 = ns_to_tCK(pctl_timing->trefi * 100, freq);
475
476 if (freq <= (400 * MHz)) {
477 pctl_timing->tcl = 6;
478 pctl_timing->tcwl = 10;
479 } else if (freq <= (533 * MHz)) {
480 pctl_timing->tcl = 8;
481 pctl_timing->tcwl = 6;
482 } else if (freq <= (666 * MHz)) {
483 pctl_timing->tcl = 10;
484 pctl_timing->tcwl = 7;
485 } else {
486 pctl_timing->tcl = 11;
487 pctl_timing->tcwl = 8;
488 }
489
490 pctl_timing->tmrd = 4; /* 4 tCK (all speed bins) */
491 pctl_timing->trfc = ns_to_tCK(350, freq); /* tRFC: 350 (max) @ 8GBit */
492 pctl_timing->trp = max(4u, ps_to_tCK(13750, freq));
493 /*
494 * JESD-79:
495 * READ to WRITE Command Delay = RL + tCCD / 2 + 2tCK - WL
496 */
497 tccd = 4;
498 pctl_timing->trtw = pctl_timing->tcl + tccd/2 + 2 - pctl_timing->tcwl;
499 pctl_timing->tal = 0;
500 pctl_timing->tras = ps_to_tCK(35000, freq);
501 pctl_timing->trc = ps_to_tCK(48750, freq);
502 pctl_timing->trcd = ps_to_tCK(13750, freq);
503 pctl_timing->trrd = max(4u, ps_to_tCK(7500, freq));
504 pctl_timing->trtp = max(4u, ps_to_tCK(7500, freq));
505 pctl_timing->twr = ps_to_tCK(15000, freq);
506 /* The DDR3 mode-register does only support even values for tWR > 8. */
507 if (pctl_timing->twr > 8)
508 pctl_timing->twr = (pctl_timing->twr + 1) & ~1;
509 pctl_timing->twtr = max(4u, ps_to_tCK(7500, freq));
510 pctl_timing->texsr = 512; /* tEXSR(max) is tDLLLK */
511 pctl_timing->txp = max(3u, ps_to_tCK(6000, freq));
512 pctl_timing->txpdll = max(10u, ps_to_tCK(24000, freq));
513 pctl_timing->tzqcs = max(64u, ps_to_tCK(80000, freq));
514 pctl_timing->tzqcsi = 10000; /* as used by Rockchip */
515 pctl_timing->tdqs = 1; /* fixed for DDR3 */
516 pctl_timing->tcksre = max(5u, ps_to_tCK(10000, freq));
517 pctl_timing->tcksrx = max(5u, ps_to_tCK(10000, freq));
518 pctl_timing->tcke = max(3u, ps_to_tCK(5000, freq));
519 pctl_timing->tmod = max(12u, ps_to_tCK(15000, freq));
520 pctl_timing->trstl = ns_to_tCK(100, freq);
521 pctl_timing->tzqcl = max(256u, ps_to_tCK(320000, freq)); /* tZQoper */
522 pctl_timing->tmrr = 0;
523 pctl_timing->tckesr = pctl_timing->tcke + 1; /* JESD-79: tCKE + 1tCK */
524 pctl_timing->tdpd = 0; /* RK3368 TRM: "allowed values for DDR3: 0" */
525
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200526 /*
527 * The controller can represent tFAW as 4x, 5x or 6x tRRD only.
528 * We want to use the smallest multiplier that satisfies the tFAW
529 * requirements of the given speed-bin. If necessary, we stretch out
530 * tRRD to allow us to operate on a 6x multiplier for tFAW.
531 */
532 tfaw_as_ps = 40000; /* 40ns: tFAW for DDR3-1600K, 2KB page-size */
533 if (tCK_to_ps(pctl_timing->trrd * 6, freq) < tfaw_as_ps) {
534 /* If tFAW is > 6 x tRRD, we need to stretch tRRD */
535 pctl_timing->trrd = ps_to_tCK(DIV_ROUND_UP(40000, 6), freq);
536 params->tfaw_mult = TFAW_TRRD_MULT6;
537 } else if (tCK_to_ps(pctl_timing->trrd * 5, freq) < tfaw_as_ps) {
538 params->tfaw_mult = TFAW_TRRD_MULT6;
539 } else if (tCK_to_ps(pctl_timing->trrd * 4, freq) < tfaw_as_ps) {
540 params->tfaw_mult = TFAW_TRRD_MULT5;
541 } else {
542 params->tfaw_mult = TFAW_TRRD_MULT4;
543 }
544
545 return 0;
546}
547
548static void pctl_cfg(struct rk3368_ddr_pctl *pctl,
549 struct rk3368_sdram_params *params,
550 struct rk3368_grf *grf)
551{
552 /* Configure PCTL timing registers */
553 params->pctl_timing.trefi |= BIT(31); /* see PCTL_TREFI */
554 copy_to_reg(&pctl->togcnt1u, &params->pctl_timing.togcnt1u,
555 sizeof(params->pctl_timing));
556 writel(params->trefi_mem_ddr3, &pctl->trefi_mem_ddr3);
557
558 /* Set up ODT write selector and ODT write length */
559 writel((RANK0_ODT_WRITE_SEL | RANK1_ODT_WRITE_SEL), &pctl->dfiodtcfg);
560 writel(7 << ODT_LEN_BL8_W_SHIFT, &pctl->dfiodtcfg1);
561
562 /* Set up the CL/CWL-dependent timings of DFI */
563 writel((params->pctl_timing.tcl - 1) / 2 - 1, &pctl->dfitrddataen);
564 writel((params->pctl_timing.tcwl - 1) / 2 - 1, &pctl->dfitphywrlat);
565
566 /* DDR3 */
567 writel(params->tfaw_mult | DDR3_EN | DDR2_DDR3_BL_8, &pctl->mcfg);
568 writel(0x001c0004, &grf->ddrc0_con0);
569
570 setbits_le32(&pctl->scfg, HW_LOW_POWER_EN);
571}
572
573static int ddrphy_data_training(struct rk3368_ddr_pctl *pctl,
574 struct rk3368_ddrphy *ddrphy)
575{
576 const u32 trefi = readl(&pctl->trefi);
577 const ulong timeout_ms = 500;
578 ulong tmp;
579
580 /* disable auto-refresh */
581 writel(0 | BIT(31), &pctl->trefi);
582
583 clrsetbits_le32(&ddrphy->reg[2], 0x33, 0x20);
584 clrsetbits_le32(&ddrphy->reg[2], 0x33, 0x21);
585
586 tmp = get_timer(0);
587 do {
588 if (get_timer(tmp) > timeout_ms) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900589 pr_err("%s: did not complete within %ld ms\n",
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200590 __func__, timeout_ms);
591 return -ETIME;
592 }
593 } while ((readl(&ddrphy->reg[0xff]) & 0xf) != 0xf);
594
595 send_command(pctl, MCMD_RANK0 | MCMD_RANK1, PREA_CMD);
596 clrsetbits_le32(&ddrphy->reg[2], 0x33, 0x20);
597 /* resume auto-refresh */
598 writel(trefi | BIT(31), &pctl->trefi);
599
600 return 0;
601}
602
603static int sdram_col_row_detect(struct udevice *dev)
604{
605 struct dram_info *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700606 struct rk3368_sdram_params *params = dev_get_plat(dev);
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200607 struct rk3368_ddr_pctl *pctl = priv->pctl;
608 struct rk3368_msch *msch = priv->msch;
609 const u32 test_pattern = 0x5aa5f00f;
610 int row, col;
611 uintptr_t addr;
612
613 move_to_config_state(pctl);
614 writel(6, &msch->ddrconf);
615 move_to_access_state(pctl);
616
617 /* Detect col */
618 for (col = 11; col >= 9; col--) {
Tom Rinibb4dd962022-11-16 13:10:37 -0500619 writel(0, CFG_SYS_SDRAM_BASE);
620 addr = CFG_SYS_SDRAM_BASE +
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200621 (1 << (col + params->chan.bw - 1));
622 writel(test_pattern, addr);
623 if ((readl(addr) == test_pattern) &&
Tom Rinibb4dd962022-11-16 13:10:37 -0500624 (readl(CFG_SYS_SDRAM_BASE) == 0))
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200625 break;
626 }
627
628 if (col == 8) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900629 pr_err("%s: col detect error\n", __func__);
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200630 return -EINVAL;
631 }
632
633 move_to_config_state(pctl);
634 writel(15, &msch->ddrconf);
635 move_to_access_state(pctl);
636
637 /* Detect row*/
638 for (row = 16; row >= 12; row--) {
Tom Rinibb4dd962022-11-16 13:10:37 -0500639 writel(0, CFG_SYS_SDRAM_BASE);
640 addr = CFG_SYS_SDRAM_BASE + (1 << (row + 15 - 1));
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200641 writel(test_pattern, addr);
642 if ((readl(addr) == test_pattern) &&
Tom Rinibb4dd962022-11-16 13:10:37 -0500643 (readl(CFG_SYS_SDRAM_BASE) == 0))
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200644 break;
645 }
646
647 if (row == 11) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900648 pr_err("%s: row detect error\n", __func__);
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200649 return -EINVAL;
650 }
651
652 /* Record results */
653 debug("%s: col %d, row %d\n", __func__, col, row);
654 params->chan.col = col;
655 params->chan.cs0_row = row;
656 params->chan.cs1_row = row;
657 params->chan.row_3_4 = 0;
658
659 return 0;
660}
661
662static int msch_niu_config(struct rk3368_msch *msch,
663 struct rk3368_sdram_params *params)
664{
665 int i;
666 const u8 cols = params->chan.col - ((params->chan.bw == 2) ? 0 : 1);
667 const u8 rows = params->chan.cs0_row;
668
669 /*
670 * The DDR address-translation table always assumes a 32bit
671 * bus and the comparison below takes care of adjusting for
672 * a 16bit bus (i.e. one column-address is consumed).
673 */
674 const struct {
675 u8 rows;
676 u8 columns;
677 u8 type;
678 } ddrconf_table[] = {
679 /*
680 * C-B-R-D patterns are first. For these we require an
681 * exact match for the columns and rows (as there's
682 * one entry per possible configuration).
683 */
684 [0] = { .rows = 13, .columns = 10, .type = DMC_MSCH_CBRD },
685 [1] = { .rows = 14, .columns = 10, .type = DMC_MSCH_CBRD },
686 [2] = { .rows = 15, .columns = 10, .type = DMC_MSCH_CBRD },
687 [3] = { .rows = 16, .columns = 10, .type = DMC_MSCH_CBRD },
688 [4] = { .rows = 14, .columns = 11, .type = DMC_MSCH_CBRD },
689 [5] = { .rows = 15, .columns = 11, .type = DMC_MSCH_CBRD },
690 [6] = { .rows = 16, .columns = 11, .type = DMC_MSCH_CBRD },
691 [7] = { .rows = 13, .columns = 9, .type = DMC_MSCH_CBRD },
692 [8] = { .rows = 14, .columns = 9, .type = DMC_MSCH_CBRD },
693 [9] = { .rows = 15, .columns = 9, .type = DMC_MSCH_CBRD },
694 [10] = { .rows = 16, .columns = 9, .type = DMC_MSCH_CBRD },
695 /*
696 * 11 through 13 are C-R-B-D patterns. These are
697 * matched for an exact number of columns and to
698 * ensure that the hardware uses at least as many rows
699 * as the pattern requires (i.e. we make sure that
700 * there's no gaps up until we hit the device/chip-select;
701 * however, these patterns can accept up to 16 rows,
702 * as the row-address continues right after the CS
703 * switching)
704 */
705 [11] = { .rows = 15, .columns = 10, .type = DMC_MSCH_CRBD },
706 [12] = { .rows = 14, .columns = 11, .type = DMC_MSCH_CRBD },
707 [13] = { .rows = 13, .columns = 10, .type = DMC_MSCH_CRBD },
708 /*
709 * 14 and 15 are catch-all variants using a C-B-D-R
710 * scheme (i.e. alternating the chip-select every time
711 * C-B overflows) and stuffing the remaining C-bits
712 * into the top. Matching needs to make sure that the
713 * number of columns is either an exact match (i.e. we
714 * can use less the the maximum number of rows) -or-
715 * that the columns exceed what is given in this table
716 * and the rows are an exact match (in which case the
717 * remaining C-bits will be stuffed onto the top after
718 * the device/chip-select switches).
719 */
720 [14] = { .rows = 16, .columns = 10, .type = DMC_MSCH_CBDR },
721 [15] = { .rows = 16, .columns = 9, .type = DMC_MSCH_CBDR },
722 };
723
724 /*
725 * For C-B-R-D, we need an exact match (i.e. both for the number of
726 * columns and rows), while for C-B-D-R, only the the number of
727 * columns needs to match.
728 */
729 for (i = 0; i < ARRAY_SIZE(ddrconf_table); i++) {
730 bool match = false;
731
732 /* If this entry if for a different matcher, then skip it */
733 if (ddrconf_table[i].type != params->memory_schedule)
734 continue;
735
736 /*
737 * Match according to the rules (exact/inexact/at-least)
738 * documented in the ddrconf_table above.
739 */
740 switch (params->memory_schedule) {
741 case DMC_MSCH_CBRD:
742 match = (ddrconf_table[i].columns == cols) &&
743 (ddrconf_table[i].rows == rows);
744 break;
745
746 case DMC_MSCH_CRBD:
747 match = (ddrconf_table[i].columns == cols) &&
748 (ddrconf_table[i].rows <= rows);
749 break;
750
751 case DMC_MSCH_CBDR:
752 match = (ddrconf_table[i].columns == cols) ||
753 ((ddrconf_table[i].columns <= cols) &&
754 (ddrconf_table[i].rows == rows));
755 break;
756
757 default:
758 break;
759 }
760
761 if (match) {
762 debug("%s: setting ddrconf 0x%x\n", __func__, i);
763 writel(i, &msch->ddrconf);
764 return 0;
765 }
766 }
767
Masahiro Yamada81e10422017-09-16 14:10:41 +0900768 pr_err("%s: ddrconf (NIU config) not found\n", __func__);
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200769 return -EINVAL;
770}
771
772static void dram_all_config(struct udevice *dev)
773{
774 struct dram_info *priv = dev_get_priv(dev);
775 struct rk3368_pmu_grf *pmugrf = priv->pmugrf;
Simon Glassfa20e932020-12-03 16:55:20 -0700776 struct rk3368_sdram_params *params = dev_get_plat(dev);
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200777 const struct rk3288_sdram_channel *info = &params->chan;
778 u32 sys_reg = 0;
779 const int chan = 0;
780
781 sys_reg |= DDR3 << SYS_REG_DDRTYPE_SHIFT;
782 sys_reg |= 0 << SYS_REG_NUM_CH_SHIFT;
783
784 sys_reg |= info->row_3_4 << SYS_REG_ROW_3_4_SHIFT(chan);
785 sys_reg |= 1 << SYS_REG_CHINFO_SHIFT(chan);
786 sys_reg |= (info->rank - 1) << SYS_REG_RANK_SHIFT(chan);
787 sys_reg |= (info->col - 9) << SYS_REG_COL_SHIFT(chan);
788 sys_reg |= info->bk == 3 ? 0 : 1 << SYS_REG_BK_SHIFT(chan);
789 sys_reg |= (info->cs0_row - 13) << SYS_REG_CS0_ROW_SHIFT(chan);
790 sys_reg |= (info->cs1_row - 13) << SYS_REG_CS1_ROW_SHIFT(chan);
791 sys_reg |= (2 >> info->bw) << SYS_REG_BW_SHIFT(chan);
792 sys_reg |= (2 >> info->dbw) << SYS_REG_DBW_SHIFT(chan);
793
794 writel(sys_reg, &pmugrf->os_reg[2]);
795}
796
797static int setup_sdram(struct udevice *dev)
798{
799 struct dram_info *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700800 struct rk3368_sdram_params *params = dev_get_plat(dev);
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200801
802 struct rk3368_ddr_pctl *pctl = priv->pctl;
803 struct rk3368_ddrphy *ddrphy = priv->phy;
804 struct rk3368_cru *cru = priv->cru;
805 struct rk3368_grf *grf = priv->grf;
806 struct rk3368_msch *msch = priv->msch;
807
808 int ret;
809
810 /* The input clock (i.e. DPLL) needs to be 2x the DRAM frequency */
811 ret = clk_set_rate(&priv->ddr_clk, 2 * params->ddr_freq);
812 if (ret < 0) {
813 debug("%s: could not set DDR clock: %d\n", __func__, ret);
814 return ret;
815 }
816
817 /* Update the read-latency for the RK3368 */
818 writel(0x32, &msch->readlatency);
819
820 /* Initialise the DDR PCTL and DDR PHY */
821 ddrctl_reset(cru);
822 ddrphy_reset(ddrphy);
823 ddrphy_config_delays(ddrphy, params->ddr_freq);
824 dfi_cfg(pctl);
825 /* Configure relative system information of grf_ddrc0_con0 register */
826 ddr_set_ddr3_mode(grf, true);
827 ddr_set_noc_spr_err_stall(grf, true);
828 /* Calculate timings */
829 pctl_calc_timings(params, params->ddr_freq);
830 /* Initialise the device timings in protocol controller */
831 pctl_cfg(pctl, params, grf);
832 /* Configure AL, CL ... information of PHY registers */
833 ddrphy_config(ddrphy,
834 params->pctl_timing.tcl,
835 params->pctl_timing.tal,
836 params->pctl_timing.tcwl);
837
838 /* Initialize DRAM and configure with mode-register values */
839 ret = memory_init(pctl, params);
840 if (ret)
841 goto error;
842
843 move_to_config_state(pctl);
844 /* Perform data-training */
845 ddrphy_data_training(pctl, ddrphy);
846 move_to_access_state(pctl);
847
848 /* TODO(prt): could detect rank in training... */
Kever Yang24b65002019-03-29 22:48:29 +0800849#ifdef CONFIG_TARGET_EVB_PX5
850 params->chan.rank = 1;
851#else
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200852 params->chan.rank = 2;
Kever Yang24b65002019-03-29 22:48:29 +0800853#endif
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200854 /* TODO(prt): bus width is not auto-detected (yet)... */
855 params->chan.bw = 2; /* 32bit wide bus */
856 params->chan.dbw = params->chan.dbw; /* 32bit wide bus */
857
858 /* DDR3 is always 8 bank */
859 params->chan.bk = 3;
860 /* Detect col and row number */
861 ret = sdram_col_row_detect(dev);
862 if (ret)
863 goto error;
864
865 /* Configure NIU DDR configuration */
866 ret = msch_niu_config(msch, params);
867 if (ret)
868 goto error;
869
870 /* set up OS_REG to communicate w/ next stage and OS */
871 dram_all_config(dev);
872
873 return 0;
874
875error:
876 printf("DRAM init failed!\n");
877 hang();
878}
879#endif
880
Simon Glassaad29ae2020-12-03 16:55:21 -0700881static int rk3368_dmc_of_to_plat(struct udevice *dev)
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200882{
883 int ret = 0;
884
Simon Glass6d70ba02021-08-07 07:24:06 -0600885 if (CONFIG_IS_ENABLED(OF_REAL)) {
886 struct rk3368_sdram_params *plat = dev_get_plat(dev);
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200887
Simon Glass6d70ba02021-08-07 07:24:06 -0600888 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
889 if (ret)
890 return ret;
891 }
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200892
893 return ret;
894}
895
896#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -0700897static int conv_of_plat(struct udevice *dev)
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200898{
Simon Glassfa20e932020-12-03 16:55:20 -0700899 struct rk3368_sdram_params *plat = dev_get_plat(dev);
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200900 struct dtd_rockchip_rk3368_dmc *of_plat = &plat->of_plat;
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200901
902 plat->ddr_freq = of_plat->rockchip_ddr_frequency;
903 plat->ddr_speed_bin = of_plat->rockchip_ddr_speed_bin;
904 plat->memory_schedule = of_plat->rockchip_memory_schedule;
905
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200906 return 0;
907}
908#endif
909
910static int rk3368_dmc_probe(struct udevice *dev)
911{
912#ifdef CONFIG_TPL_BUILD
Simon Glassfa20e932020-12-03 16:55:20 -0700913 struct rk3368_sdram_params *plat = dev_get_plat(dev);
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200914 struct rk3368_ddr_pctl *pctl;
915 struct rk3368_ddrphy *ddrphy;
916 struct rk3368_cru *cru;
917 struct rk3368_grf *grf;
918 struct rk3368_msch *msch;
919 int ret;
920 struct udevice *dev_clk;
921#endif
922 struct dram_info *priv = dev_get_priv(dev);
923
924#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -0700925 ret = conv_of_plat(dev);
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200926 if (ret)
927 return ret;
928#endif
929
930 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
931 debug("%s: pmugrf=%p\n", __func__, priv->pmugrf);
932
933#ifdef CONFIG_TPL_BUILD
Philipp Tomsich4e2fe8f2017-08-14 19:05:32 +0200934 pctl = (struct rk3368_ddr_pctl *)plat->of_plat.reg[0];
935 ddrphy = (struct rk3368_ddrphy *)plat->of_plat.reg[2];
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200936 msch = syscon_get_first_range(ROCKCHIP_SYSCON_MSCH);
937 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
938
939 priv->pctl = pctl;
940 priv->phy = ddrphy;
941 priv->msch = msch;
942 priv->grf = grf;
943
944 ret = rockchip_get_clk(&dev_clk);
945 if (ret)
946 return ret;
947 priv->ddr_clk.id = CLK_DDR;
948 ret = clk_request(dev_clk, &priv->ddr_clk);
949 if (ret)
950 return ret;
951
952 cru = rockchip_get_cru();
953 priv->cru = cru;
954 if (IS_ERR(priv->cru))
955 return PTR_ERR(priv->cru);
956
957 ret = setup_sdram(dev);
958 if (ret)
959 return ret;
960#endif
961
962 priv->info.base = 0;
963 priv->info.size =
964 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg[2]);
965
966 /*
967 * we use the 0x00000000~0xfdffffff space since 0xff000000~0xffffffff
968 * is SoC register space (i.e. reserved), and 0xfe000000~0xfeffffff is
969 * inaccessible for some IP controller.
970 */
971 priv->info.size = min(priv->info.size, (size_t)0xfe000000);
972
973 return 0;
974}
975
976static int rk3368_dmc_get_info(struct udevice *dev, struct ram_info *info)
977{
978 struct dram_info *priv = dev_get_priv(dev);
979
980 *info = priv->info;
981 return 0;
982}
983
984static struct ram_ops rk3368_dmc_ops = {
985 .get_info = rk3368_dmc_get_info,
986};
987
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200988static const struct udevice_id rk3368_dmc_ids[] = {
989 { .compatible = "rockchip,rk3368-dmc" },
990 { }
991};
992
Walter Lozano2901ac62020-06-25 01:10:04 -0300993U_BOOT_DRIVER(rockchip_rk3368_dmc) = {
Philipp Tomsichd21a4d82017-06-23 00:12:05 +0200994 .name = "rockchip_rk3368_dmc",
995 .id = UCLASS_RAM,
996 .of_match = rk3368_dmc_ids,
997 .ops = &rk3368_dmc_ops,
998 .probe = rk3368_dmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700999 .priv_auto = sizeof(struct dram_info),
Simon Glassaad29ae2020-12-03 16:55:21 -07001000 .of_to_plat = rk3368_dmc_of_to_plat,
Philipp Tomsichd21a4d82017-06-23 00:12:05 +02001001 .probe = rk3368_dmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001002 .priv_auto = sizeof(struct dram_info),
Simon Glass71fa5b42020-12-03 16:55:18 -07001003 .plat_auto = sizeof(struct rk3368_sdram_params),
Philipp Tomsichd21a4d82017-06-23 00:12:05 +02001004};