2 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
4 * SPDX-License-Identifier: GPL-2.0
10 #include <dt-bindings/memory/rk3368-dmc.h>
11 #include <dt-structs.h>
16 #include <asm/arch/clock.h>
17 #include <asm/arch/cru_rk3368.h>
18 #include <asm/arch/grf_rk3368.h>
19 #include <asm/arch/ddr_rk3368.h>
20 #include <asm/arch/sdram.h>
21 #include <asm/arch/sdram_common.h>
26 struct rk3368_cru *cru;
27 struct rk3368_grf *grf;
28 struct rk3368_ddr_pctl *pctl;
29 struct rk3368_ddrphy *phy;
30 struct rk3368_pmu_grf *pmugrf;
31 struct rk3368_msch *msch;
34 struct rk3368_sdram_params {
35 #if CONFIG_IS_ENABLED(OF_PLATDATA)
36 struct dtd_rockchip_rk3368_dmc of_plat;
38 struct rk3288_sdram_pctl_timing pctl_timing;
40 struct rk3288_sdram_channel chan;
51 DFI_INIT_START = BIT(0),
52 DFI_DATA_BYTE_DISABLE_EN = BIT(2),
55 DFI_DRAM_CLK_SR_EN = BIT(0),
56 DFI_DRAM_CLK_DPD_EN = BIT(1),
57 ODT_LEN_BL8_W_SHIFT = 16,
60 DFI_PARITY_INTR_EN = BIT(0),
61 DFI_PARITY_EN = BIT(1),
64 TLP_RESP_TIME_SHIFT = 16,
69 RANK0_ODT_WRITE_SEL = BIT(3),
70 RANK1_ODT_WRITE_SEL = BIT(11),
73 HW_LOW_POWER_EN = BIT(0),
90 POWER_UP_START = BIT(0),
93 POWER_UP_DONE = BIT(0),
116 DDR2_DDR3_BL_8 = BIT(0),
118 TFAW_TRRD_MULT4 = (0 << 18),
119 TFAW_TRRD_MULT5 = (1 << 18),
120 TFAW_TRRD_MULT6 = (2 << 18),
123 #define DDR3_MR0_WR(n) \
124 ((n <= 8) ? ((n - 4) << 9) : (((n >> 1) & 0x7) << 9))
125 #define DDR3_MR0_CL(n) \
126 ((((n - 4) & 0x7) << 4) | (((n - 4) & 0x8) >> 2))
127 #define DDR3_MR0_BL8 \
129 #define DDR3_MR0_DLL_RESET \
131 #define DDR3_MR1_RTT120OHM \
132 ((0 << 9) | (1 << 6) | (0 << 2))
133 #define DDR3_MR2_TWL(n) \
134 (((n - 5) & 0x7) << 3)
137 #ifdef CONFIG_TPL_BUILD
139 static void ddr_set_noc_spr_err_stall(struct rk3368_grf *grf, bool enable)
142 rk_setreg(&grf->ddrc0_con0, NOC_RSP_ERR_STALL);
144 rk_clrreg(&grf->ddrc0_con0, NOC_RSP_ERR_STALL);
147 static void ddr_set_ddr3_mode(struct rk3368_grf *grf, bool ddr3_mode)
150 rk_setreg(&grf->ddrc0_con0, MSCH0_MAINDDR3_DDR3);
152 rk_clrreg(&grf->ddrc0_con0, MSCH0_MAINDDR3_DDR3);
155 static void ddrphy_config(struct rk3368_ddrphy *phy,
156 u32 tcl, u32 tal, u32 tcwl)
160 /* Set to DDR3 mode */
161 clrsetbits_le32(&phy->reg[1], 0x3, 0x0);
163 /* DDRPHY_REGB: CL, AL */
164 clrsetbits_le32(&phy->reg[0xb], 0xff, tcl << 4 | tal);
165 /* DDRPHY_REGC: CWL */
166 clrsetbits_le32(&phy->reg[0xc], 0x0f, tcwl);
168 /* Update drive-strength */
169 writel(0xcc, &phy->reg[0x11]);
170 writel(0xaa, &phy->reg[0x16]);
172 * Update NRCOMP/PRCOMP for all 4 channels (for details of all
173 * affected registers refer to the documentation of DDRPHY_REG20
174 * and DDRPHY_REG21 in the RK3368 TRM.
176 for (i = 0; i < 4; ++i) {
177 writel(0xcc, &phy->reg[0x20 + i * 0x10]);
178 writel(0x44, &phy->reg[0x21 + i * 0x10]);
181 /* Enable write-leveling calibration bypass */
182 setbits_le32(&phy->reg[2], BIT(3));
185 static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
189 for (i = 0; i < n / sizeof(u32); i++)
190 writel(*src++, dest++);
193 static void send_command(struct rk3368_ddr_pctl *pctl, u32 rank, u32 cmd)
195 u32 mcmd = START_CMD | cmd | rank;
197 debug("%s: writing %x to MCMD\n", __func__, mcmd);
198 writel(mcmd, &pctl->mcmd);
199 while (readl(&pctl->mcmd) & START_CMD)
203 static void send_mrs(struct rk3368_ddr_pctl *pctl,
204 u32 rank, u32 mr_num, u32 mr_data)
206 u32 mcmd = START_CMD | MRS_CMD | rank | (mr_num << 17) | (mr_data << 4);
208 debug("%s: writing %x to MCMD\n", __func__, mcmd);
209 writel(mcmd, &pctl->mcmd);
210 while (readl(&pctl->mcmd) & START_CMD)
214 static int memory_init(struct rk3368_ddr_pctl *pctl,
215 struct rk3368_sdram_params *params)
218 const ulong timeout_ms = 500;
222 * Power up DRAM by DDR_PCTL_POWCTL[0] register of PCTL and
223 * wait power up DRAM finish with DDR_PCTL_POWSTAT[0] register
226 writel(POWER_UP_START, &pctl->powctl);
230 if (get_timer(tmp) > timeout_ms) {
231 pr_err("%s: POWER_UP_START did not complete in %ld ms\n",
232 __func__, timeout_ms);
235 } while (!(readl(&pctl->powstat) & POWER_UP_DONE));
237 /* Configure MR0 through MR3 */
238 mr[0] = DDR3_MR0_WR(params->pctl_timing.twr) |
239 DDR3_MR0_CL(params->pctl_timing.tcl) |
241 mr[1] = DDR3_MR1_RTT120OHM;
242 mr[2] = DDR3_MR2_TWL(params->pctl_timing.tcwl);
246 * Also see RK3368 Technical Reference Manual:
247 * "16.6.2 Initialization (DDR3 Initialization Sequence)"
249 send_command(pctl, MCMD_RANK0 | MCMD_RANK1, DESELECT_CMD);
251 send_command(pctl, MCMD_RANK0 | MCMD_RANK1, PREA_CMD);
252 send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 2, mr[2]);
253 send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 3, mr[3]);
254 send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 1, mr[1]);
255 send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 0, mr[0]);
256 send_command(pctl, MCMD_RANK0 | MCMD_RANK1, ZQCL_CMD);
261 static void move_to_config_state(struct rk3368_ddr_pctl *pctl)
264 * Also see RK3368 Technical Reference Manual:
265 * "16.6.1 State transition of PCTL (Moving to Config State)"
267 u32 state = readl(&pctl->stat) & PCTL_STAT_MSK;
271 writel(WAKEUP_STATE, &pctl->sctl);
272 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != ACCESS)
278 writel(CFG_STATE, &pctl->sctl);
279 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != CONFIG)
291 static void move_to_access_state(struct rk3368_ddr_pctl *pctl)
294 * Also see RK3368 Technical Reference Manual:
295 * "16.6.1 State transition of PCTL (Moving to Access State)"
297 u32 state = readl(&pctl->stat) & PCTL_STAT_MSK;
301 if (((readl(&pctl->stat) >> LP_TRIG_SHIFT) &
305 writel(WAKEUP_STATE, &pctl->sctl);
306 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != ACCESS)
311 writel(CFG_STATE, &pctl->sctl);
312 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != CONFIG)
317 writel(GO_STATE, &pctl->sctl);
318 while ((readl(&pctl->stat) & PCTL_STAT_MSK) == CONFIG)
330 static void ddrctl_reset(struct rk3368_cru *cru)
332 const u32 ctl_reset = BIT(3) | BIT(2);
333 const u32 phy_reset = BIT(1) | BIT(0);
336 * The PHY reset should be released before the PCTL reset.
338 * Note that the following sequence (including the number of
339 * us to delay between releasing the PHY and PCTL reset) has
340 * been adapted per feedback received from Rockchips, so do
341 * not try to optimise.
343 rk_setreg(&cru->softrst_con[10], ctl_reset | phy_reset);
345 rk_clrreg(&cru->softrst_con[10], phy_reset);
347 rk_clrreg(&cru->softrst_con[10], ctl_reset);
350 static void ddrphy_reset(struct rk3368_ddrphy *ddrphy)
353 * The analog part of the PHY should be release at least 1000
354 * DRAM cycles before the digital part of the PHY (waiting for
355 * 5us will ensure this for a DRAM clock as low as 200MHz).
357 clrbits_le32(&ddrphy->reg[0], BIT(3) | BIT(2));
359 setbits_le32(&ddrphy->reg[0], BIT(2));
361 setbits_le32(&ddrphy->reg[0], BIT(3));
364 static void ddrphy_config_delays(struct rk3368_ddrphy *ddrphy, u32 freq)
368 setbits_le32(&ddrphy->reg[0x13], BIT(4));
369 clrbits_le32(&ddrphy->reg[0x14], BIT(3));
371 setbits_le32(&ddrphy->reg[0x26], BIT(4));
372 clrbits_le32(&ddrphy->reg[0x27], BIT(3));
374 setbits_le32(&ddrphy->reg[0x36], BIT(4));
375 clrbits_le32(&ddrphy->reg[0x37], BIT(3));
377 setbits_le32(&ddrphy->reg[0x46], BIT(4));
378 clrbits_le32(&ddrphy->reg[0x47], BIT(3));
380 setbits_le32(&ddrphy->reg[0x56], BIT(4));
381 clrbits_le32(&ddrphy->reg[0x57], BIT(3));
383 if (freq <= 400000000)
384 setbits_le32(&ddrphy->reg[0xa4], 0x1f);
386 clrbits_le32(&ddrphy->reg[0xa4], 0x1f);
388 if (freq < 681000000)
389 dqs_dll_delay = 3; /* 67.5 degree delay */
391 dqs_dll_delay = 2; /* 45 degree delay */
393 writel(dqs_dll_delay, &ddrphy->reg[0x28]);
394 writel(dqs_dll_delay, &ddrphy->reg[0x38]);
395 writel(dqs_dll_delay, &ddrphy->reg[0x48]);
396 writel(dqs_dll_delay, &ddrphy->reg[0x58]);
399 static int dfi_cfg(struct rk3368_ddr_pctl *pctl)
401 const ulong timeout_ms = 200;
404 writel(DFI_DATA_BYTE_DISABLE_EN, &pctl->dfistcfg0);
406 writel(DFI_DRAM_CLK_SR_EN | DFI_DRAM_CLK_DPD_EN,
408 writel(DFI_PARITY_INTR_EN | DFI_PARITY_EN, &pctl->dfistcfg2);
409 writel(7 << TLP_RESP_TIME_SHIFT | LP_SR_EN | LP_PD_EN,
412 writel(1, &pctl->dfitphyupdtype0);
414 writel(0x1f, &pctl->dfitphyrdlat);
415 writel(0, &pctl->dfitphywrdata);
416 writel(0, &pctl->dfiupdcfg); /* phyupd and ctrlupd disabled */
418 setbits_le32(&pctl->dfistcfg0, DFI_INIT_START);
422 if (get_timer(tmp) > timeout_ms) {
423 pr_err("%s: DFI init did not complete within %ld ms\n",
424 __func__, timeout_ms);
427 } while ((readl(&pctl->dfiststat0) & 1) == 0);
432 static inline u32 ps_to_tCK(const u32 ps, const ulong freq)
434 const ulong MHz = 1000000;
435 return DIV_ROUND_UP(ps * freq, 1000000 * MHz);
438 static inline u32 ns_to_tCK(const u32 ns, const ulong freq)
440 return ps_to_tCK(ns * 1000, freq);
443 static inline u32 tCK_to_ps(const ulong tCK, const ulong freq)
445 const ulong MHz = 1000000;
446 return DIV_ROUND_UP(tCK * 1000000 * MHz, freq);
449 static int pctl_calc_timings(struct rk3368_sdram_params *params,
452 struct rk3288_sdram_pctl_timing *pctl_timing = ¶ms->pctl_timing;
453 const ulong MHz = 1000000;
457 if (params->ddr_speed_bin != DDR3_1600K) {
458 pr_err("%s: unimplemented DDR3 speed bin %d\n",
459 __func__, params->ddr_speed_bin);
463 /* PCTL is clocked at 1/2 the DRAM clock; err on the side of caution */
464 pctl_timing->togcnt1u = DIV_ROUND_UP(freq, 2 * MHz);
465 pctl_timing->togcnt100n = DIV_ROUND_UP(freq / 10, 2 * MHz);
467 pctl_timing->tinit = 200; /* 200 usec */
468 pctl_timing->trsth = 500; /* 500 usec */
469 pctl_timing->trefi = 78; /* 7.8usec = 78 * 100ns */
470 params->trefi_mem_ddr3 = ns_to_tCK(pctl_timing->trefi * 100, freq);
472 if (freq <= (400 * MHz)) {
473 pctl_timing->tcl = 6;
474 pctl_timing->tcwl = 10;
475 } else if (freq <= (533 * MHz)) {
476 pctl_timing->tcl = 8;
477 pctl_timing->tcwl = 6;
478 } else if (freq <= (666 * MHz)) {
479 pctl_timing->tcl = 10;
480 pctl_timing->tcwl = 7;
482 pctl_timing->tcl = 11;
483 pctl_timing->tcwl = 8;
486 pctl_timing->tmrd = 4; /* 4 tCK (all speed bins) */
487 pctl_timing->trfc = ns_to_tCK(350, freq); /* tRFC: 350 (max) @ 8GBit */
488 pctl_timing->trp = max(4u, ps_to_tCK(13750, freq));
491 * READ to WRITE Command Delay = RL + tCCD / 2 + 2tCK - WL
494 pctl_timing->trtw = pctl_timing->tcl + tccd/2 + 2 - pctl_timing->tcwl;
495 pctl_timing->tal = 0;
496 pctl_timing->tras = ps_to_tCK(35000, freq);
497 pctl_timing->trc = ps_to_tCK(48750, freq);
498 pctl_timing->trcd = ps_to_tCK(13750, freq);
499 pctl_timing->trrd = max(4u, ps_to_tCK(7500, freq));
500 pctl_timing->trtp = max(4u, ps_to_tCK(7500, freq));
501 pctl_timing->twr = ps_to_tCK(15000, freq);
502 /* The DDR3 mode-register does only support even values for tWR > 8. */
503 if (pctl_timing->twr > 8)
504 pctl_timing->twr = (pctl_timing->twr + 1) & ~1;
505 pctl_timing->twtr = max(4u, ps_to_tCK(7500, freq));
506 pctl_timing->texsr = 512; /* tEXSR(max) is tDLLLK */
507 pctl_timing->txp = max(3u, ps_to_tCK(6000, freq));
508 pctl_timing->txpdll = max(10u, ps_to_tCK(24000, freq));
509 pctl_timing->tzqcs = max(64u, ps_to_tCK(80000, freq));
510 pctl_timing->tzqcsi = 10000; /* as used by Rockchip */
511 pctl_timing->tdqs = 1; /* fixed for DDR3 */
512 pctl_timing->tcksre = max(5u, ps_to_tCK(10000, freq));
513 pctl_timing->tcksrx = max(5u, ps_to_tCK(10000, freq));
514 pctl_timing->tcke = max(3u, ps_to_tCK(5000, freq));
515 pctl_timing->tmod = max(12u, ps_to_tCK(15000, freq));
516 pctl_timing->trstl = ns_to_tCK(100, freq);
517 pctl_timing->tzqcl = max(256u, ps_to_tCK(320000, freq)); /* tZQoper */
518 pctl_timing->tmrr = 0;
519 pctl_timing->tckesr = pctl_timing->tcke + 1; /* JESD-79: tCKE + 1tCK */
520 pctl_timing->tdpd = 0; /* RK3368 TRM: "allowed values for DDR3: 0" */
524 * The controller can represent tFAW as 4x, 5x or 6x tRRD only.
525 * We want to use the smallest multiplier that satisfies the tFAW
526 * requirements of the given speed-bin. If necessary, we stretch out
527 * tRRD to allow us to operate on a 6x multiplier for tFAW.
529 tfaw_as_ps = 40000; /* 40ns: tFAW for DDR3-1600K, 2KB page-size */
530 if (tCK_to_ps(pctl_timing->trrd * 6, freq) < tfaw_as_ps) {
531 /* If tFAW is > 6 x tRRD, we need to stretch tRRD */
532 pctl_timing->trrd = ps_to_tCK(DIV_ROUND_UP(40000, 6), freq);
533 params->tfaw_mult = TFAW_TRRD_MULT6;
534 } else if (tCK_to_ps(pctl_timing->trrd * 5, freq) < tfaw_as_ps) {
535 params->tfaw_mult = TFAW_TRRD_MULT6;
536 } else if (tCK_to_ps(pctl_timing->trrd * 4, freq) < tfaw_as_ps) {
537 params->tfaw_mult = TFAW_TRRD_MULT5;
539 params->tfaw_mult = TFAW_TRRD_MULT4;
545 static void pctl_cfg(struct rk3368_ddr_pctl *pctl,
546 struct rk3368_sdram_params *params,
547 struct rk3368_grf *grf)
549 /* Configure PCTL timing registers */
550 params->pctl_timing.trefi |= BIT(31); /* see PCTL_TREFI */
551 copy_to_reg(&pctl->togcnt1u, ¶ms->pctl_timing.togcnt1u,
552 sizeof(params->pctl_timing));
553 writel(params->trefi_mem_ddr3, &pctl->trefi_mem_ddr3);
555 /* Set up ODT write selector and ODT write length */
556 writel((RANK0_ODT_WRITE_SEL | RANK1_ODT_WRITE_SEL), &pctl->dfiodtcfg);
557 writel(7 << ODT_LEN_BL8_W_SHIFT, &pctl->dfiodtcfg1);
559 /* Set up the CL/CWL-dependent timings of DFI */
560 writel((params->pctl_timing.tcl - 1) / 2 - 1, &pctl->dfitrddataen);
561 writel((params->pctl_timing.tcwl - 1) / 2 - 1, &pctl->dfitphywrlat);
564 writel(params->tfaw_mult | DDR3_EN | DDR2_DDR3_BL_8, &pctl->mcfg);
565 writel(0x001c0004, &grf->ddrc0_con0);
567 setbits_le32(&pctl->scfg, HW_LOW_POWER_EN);
570 static int ddrphy_data_training(struct rk3368_ddr_pctl *pctl,
571 struct rk3368_ddrphy *ddrphy)
573 const u32 trefi = readl(&pctl->trefi);
574 const ulong timeout_ms = 500;
577 /* disable auto-refresh */
578 writel(0 | BIT(31), &pctl->trefi);
580 clrsetbits_le32(&ddrphy->reg[2], 0x33, 0x20);
581 clrsetbits_le32(&ddrphy->reg[2], 0x33, 0x21);
585 if (get_timer(tmp) > timeout_ms) {
586 pr_err("%s: did not complete within %ld ms\n",
587 __func__, timeout_ms);
590 } while ((readl(&ddrphy->reg[0xff]) & 0xf) != 0xf);
592 send_command(pctl, MCMD_RANK0 | MCMD_RANK1, PREA_CMD);
593 clrsetbits_le32(&ddrphy->reg[2], 0x33, 0x20);
594 /* resume auto-refresh */
595 writel(trefi | BIT(31), &pctl->trefi);
600 static int sdram_col_row_detect(struct udevice *dev)
602 struct dram_info *priv = dev_get_priv(dev);
603 struct rk3368_sdram_params *params = dev_get_platdata(dev);
604 struct rk3368_ddr_pctl *pctl = priv->pctl;
605 struct rk3368_msch *msch = priv->msch;
606 const u32 test_pattern = 0x5aa5f00f;
610 move_to_config_state(pctl);
611 writel(6, &msch->ddrconf);
612 move_to_access_state(pctl);
615 for (col = 11; col >= 9; col--) {
616 writel(0, CONFIG_SYS_SDRAM_BASE);
617 addr = CONFIG_SYS_SDRAM_BASE +
618 (1 << (col + params->chan.bw - 1));
619 writel(test_pattern, addr);
620 if ((readl(addr) == test_pattern) &&
621 (readl(CONFIG_SYS_SDRAM_BASE) == 0))
626 pr_err("%s: col detect error\n", __func__);
630 move_to_config_state(pctl);
631 writel(15, &msch->ddrconf);
632 move_to_access_state(pctl);
635 for (row = 16; row >= 12; row--) {
636 writel(0, CONFIG_SYS_SDRAM_BASE);
637 addr = CONFIG_SYS_SDRAM_BASE + (1 << (row + 15 - 1));
638 writel(test_pattern, addr);
639 if ((readl(addr) == test_pattern) &&
640 (readl(CONFIG_SYS_SDRAM_BASE) == 0))
645 pr_err("%s: row detect error\n", __func__);
650 debug("%s: col %d, row %d\n", __func__, col, row);
651 params->chan.col = col;
652 params->chan.cs0_row = row;
653 params->chan.cs1_row = row;
654 params->chan.row_3_4 = 0;
659 static int msch_niu_config(struct rk3368_msch *msch,
660 struct rk3368_sdram_params *params)
663 const u8 cols = params->chan.col - ((params->chan.bw == 2) ? 0 : 1);
664 const u8 rows = params->chan.cs0_row;
667 * The DDR address-translation table always assumes a 32bit
668 * bus and the comparison below takes care of adjusting for
669 * a 16bit bus (i.e. one column-address is consumed).
675 } ddrconf_table[] = {
677 * C-B-R-D patterns are first. For these we require an
678 * exact match for the columns and rows (as there's
679 * one entry per possible configuration).
681 [0] = { .rows = 13, .columns = 10, .type = DMC_MSCH_CBRD },
682 [1] = { .rows = 14, .columns = 10, .type = DMC_MSCH_CBRD },
683 [2] = { .rows = 15, .columns = 10, .type = DMC_MSCH_CBRD },
684 [3] = { .rows = 16, .columns = 10, .type = DMC_MSCH_CBRD },
685 [4] = { .rows = 14, .columns = 11, .type = DMC_MSCH_CBRD },
686 [5] = { .rows = 15, .columns = 11, .type = DMC_MSCH_CBRD },
687 [6] = { .rows = 16, .columns = 11, .type = DMC_MSCH_CBRD },
688 [7] = { .rows = 13, .columns = 9, .type = DMC_MSCH_CBRD },
689 [8] = { .rows = 14, .columns = 9, .type = DMC_MSCH_CBRD },
690 [9] = { .rows = 15, .columns = 9, .type = DMC_MSCH_CBRD },
691 [10] = { .rows = 16, .columns = 9, .type = DMC_MSCH_CBRD },
693 * 11 through 13 are C-R-B-D patterns. These are
694 * matched for an exact number of columns and to
695 * ensure that the hardware uses at least as many rows
696 * as the pattern requires (i.e. we make sure that
697 * there's no gaps up until we hit the device/chip-select;
698 * however, these patterns can accept up to 16 rows,
699 * as the row-address continues right after the CS
702 [11] = { .rows = 15, .columns = 10, .type = DMC_MSCH_CRBD },
703 [12] = { .rows = 14, .columns = 11, .type = DMC_MSCH_CRBD },
704 [13] = { .rows = 13, .columns = 10, .type = DMC_MSCH_CRBD },
706 * 14 and 15 are catch-all variants using a C-B-D-R
707 * scheme (i.e. alternating the chip-select every time
708 * C-B overflows) and stuffing the remaining C-bits
709 * into the top. Matching needs to make sure that the
710 * number of columns is either an exact match (i.e. we
711 * can use less the the maximum number of rows) -or-
712 * that the columns exceed what is given in this table
713 * and the rows are an exact match (in which case the
714 * remaining C-bits will be stuffed onto the top after
715 * the device/chip-select switches).
717 [14] = { .rows = 16, .columns = 10, .type = DMC_MSCH_CBDR },
718 [15] = { .rows = 16, .columns = 9, .type = DMC_MSCH_CBDR },
722 * For C-B-R-D, we need an exact match (i.e. both for the number of
723 * columns and rows), while for C-B-D-R, only the the number of
724 * columns needs to match.
726 for (i = 0; i < ARRAY_SIZE(ddrconf_table); i++) {
729 /* If this entry if for a different matcher, then skip it */
730 if (ddrconf_table[i].type != params->memory_schedule)
734 * Match according to the rules (exact/inexact/at-least)
735 * documented in the ddrconf_table above.
737 switch (params->memory_schedule) {
739 match = (ddrconf_table[i].columns == cols) &&
740 (ddrconf_table[i].rows == rows);
744 match = (ddrconf_table[i].columns == cols) &&
745 (ddrconf_table[i].rows <= rows);
749 match = (ddrconf_table[i].columns == cols) ||
750 ((ddrconf_table[i].columns <= cols) &&
751 (ddrconf_table[i].rows == rows));
759 debug("%s: setting ddrconf 0x%x\n", __func__, i);
760 writel(i, &msch->ddrconf);
765 pr_err("%s: ddrconf (NIU config) not found\n", __func__);
769 static void dram_all_config(struct udevice *dev)
771 struct dram_info *priv = dev_get_priv(dev);
772 struct rk3368_pmu_grf *pmugrf = priv->pmugrf;
773 struct rk3368_sdram_params *params = dev_get_platdata(dev);
774 const struct rk3288_sdram_channel *info = ¶ms->chan;
778 sys_reg |= DDR3 << SYS_REG_DDRTYPE_SHIFT;
779 sys_reg |= 0 << SYS_REG_NUM_CH_SHIFT;
781 sys_reg |= info->row_3_4 << SYS_REG_ROW_3_4_SHIFT(chan);
782 sys_reg |= 1 << SYS_REG_CHINFO_SHIFT(chan);
783 sys_reg |= (info->rank - 1) << SYS_REG_RANK_SHIFT(chan);
784 sys_reg |= (info->col - 9) << SYS_REG_COL_SHIFT(chan);
785 sys_reg |= info->bk == 3 ? 0 : 1 << SYS_REG_BK_SHIFT(chan);
786 sys_reg |= (info->cs0_row - 13) << SYS_REG_CS0_ROW_SHIFT(chan);
787 sys_reg |= (info->cs1_row - 13) << SYS_REG_CS1_ROW_SHIFT(chan);
788 sys_reg |= (2 >> info->bw) << SYS_REG_BW_SHIFT(chan);
789 sys_reg |= (2 >> info->dbw) << SYS_REG_DBW_SHIFT(chan);
791 writel(sys_reg, &pmugrf->os_reg[2]);
794 static int setup_sdram(struct udevice *dev)
796 struct dram_info *priv = dev_get_priv(dev);
797 struct rk3368_sdram_params *params = dev_get_platdata(dev);
799 struct rk3368_ddr_pctl *pctl = priv->pctl;
800 struct rk3368_ddrphy *ddrphy = priv->phy;
801 struct rk3368_cru *cru = priv->cru;
802 struct rk3368_grf *grf = priv->grf;
803 struct rk3368_msch *msch = priv->msch;
807 /* The input clock (i.e. DPLL) needs to be 2x the DRAM frequency */
808 ret = clk_set_rate(&priv->ddr_clk, 2 * params->ddr_freq);
810 debug("%s: could not set DDR clock: %d\n", __func__, ret);
814 /* Update the read-latency for the RK3368 */
815 writel(0x32, &msch->readlatency);
817 /* Initialise the DDR PCTL and DDR PHY */
819 ddrphy_reset(ddrphy);
820 ddrphy_config_delays(ddrphy, params->ddr_freq);
822 /* Configure relative system information of grf_ddrc0_con0 register */
823 ddr_set_ddr3_mode(grf, true);
824 ddr_set_noc_spr_err_stall(grf, true);
825 /* Calculate timings */
826 pctl_calc_timings(params, params->ddr_freq);
827 /* Initialise the device timings in protocol controller */
828 pctl_cfg(pctl, params, grf);
829 /* Configure AL, CL ... information of PHY registers */
830 ddrphy_config(ddrphy,
831 params->pctl_timing.tcl,
832 params->pctl_timing.tal,
833 params->pctl_timing.tcwl);
835 /* Initialize DRAM and configure with mode-register values */
836 ret = memory_init(pctl, params);
840 move_to_config_state(pctl);
841 /* Perform data-training */
842 ddrphy_data_training(pctl, ddrphy);
843 move_to_access_state(pctl);
845 /* TODO(prt): could detect rank in training... */
846 params->chan.rank = 2;
847 /* TODO(prt): bus width is not auto-detected (yet)... */
848 params->chan.bw = 2; /* 32bit wide bus */
849 params->chan.dbw = params->chan.dbw; /* 32bit wide bus */
851 /* DDR3 is always 8 bank */
853 /* Detect col and row number */
854 ret = sdram_col_row_detect(dev);
858 /* Configure NIU DDR configuration */
859 ret = msch_niu_config(msch, params);
863 /* set up OS_REG to communicate w/ next stage and OS */
864 dram_all_config(dev);
869 printf("DRAM init failed!\n");
874 static int rk3368_dmc_ofdata_to_platdata(struct udevice *dev)
878 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
879 struct rk3368_sdram_params *plat = dev_get_platdata(dev);
881 ret = regmap_init_mem(dev, &plat->map);
889 #if CONFIG_IS_ENABLED(OF_PLATDATA)
890 static int conv_of_platdata(struct udevice *dev)
892 struct rk3368_sdram_params *plat = dev_get_platdata(dev);
893 struct dtd_rockchip_rk3368_dmc *of_plat = &plat->of_plat;
895 plat->ddr_freq = of_plat->rockchip_ddr_frequency;
896 plat->ddr_speed_bin = of_plat->rockchip_ddr_speed_bin;
897 plat->memory_schedule = of_plat->rockchip_memory_schedule;
903 static int rk3368_dmc_probe(struct udevice *dev)
905 #ifdef CONFIG_TPL_BUILD
906 struct rk3368_sdram_params *plat = dev_get_platdata(dev);
907 struct rk3368_ddr_pctl *pctl;
908 struct rk3368_ddrphy *ddrphy;
909 struct rk3368_cru *cru;
910 struct rk3368_grf *grf;
911 struct rk3368_msch *msch;
913 struct udevice *dev_clk;
915 struct dram_info *priv = dev_get_priv(dev);
917 #if CONFIG_IS_ENABLED(OF_PLATDATA)
918 ret = conv_of_platdata(dev);
923 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
924 debug("%s: pmugrf=%p\n", __func__, priv->pmugrf);
926 #ifdef CONFIG_TPL_BUILD
927 pctl = (struct rk3368_ddr_pctl *)plat->of_plat.reg[0];
928 ddrphy = (struct rk3368_ddrphy *)plat->of_plat.reg[2];
929 msch = syscon_get_first_range(ROCKCHIP_SYSCON_MSCH);
930 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
937 ret = rockchip_get_clk(&dev_clk);
940 priv->ddr_clk.id = CLK_DDR;
941 ret = clk_request(dev_clk, &priv->ddr_clk);
945 cru = rockchip_get_cru();
947 if (IS_ERR(priv->cru))
948 return PTR_ERR(priv->cru);
950 ret = setup_sdram(dev);
957 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg[2]);
960 * we use the 0x00000000~0xfdffffff space since 0xff000000~0xffffffff
961 * is SoC register space (i.e. reserved), and 0xfe000000~0xfeffffff is
962 * inaccessible for some IP controller.
964 priv->info.size = min(priv->info.size, (size_t)0xfe000000);
969 static int rk3368_dmc_get_info(struct udevice *dev, struct ram_info *info)
971 struct dram_info *priv = dev_get_priv(dev);
977 static struct ram_ops rk3368_dmc_ops = {
978 .get_info = rk3368_dmc_get_info,
982 static const struct udevice_id rk3368_dmc_ids[] = {
983 { .compatible = "rockchip,rk3368-dmc" },
987 U_BOOT_DRIVER(dmc_rk3368) = {
988 .name = "rockchip_rk3368_dmc",
990 .of_match = rk3368_dmc_ids,
991 .ops = &rk3368_dmc_ops,
992 .probe = rk3368_dmc_probe,
993 .priv_auto_alloc_size = sizeof(struct dram_info),
994 .ofdata_to_platdata = rk3368_dmc_ofdata_to_platdata,
995 .probe = rk3368_dmc_probe,
996 .priv_auto_alloc_size = sizeof(struct dram_info),
997 .platdata_auto_alloc_size = sizeof(struct rk3368_sdram_params),