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