1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
11 #include <dt-bindings/memory/rk3368-dmc.h>
12 #include <dt-structs.h>
17 #include <asm/arch-rockchip/clock.h>
18 #include <asm/arch-rockchip/cru_rk3368.h>
19 #include <asm/arch-rockchip/grf_rk3368.h>
20 #include <asm/arch-rockchip/ddr_rk3368.h>
21 #include <asm/arch-rockchip/sdram.h>
22 #include <asm/arch-rockchip/sdram_rk3288.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
29 struct rk3368_cru *cru;
30 struct rk3368_grf *grf;
31 struct rk3368_ddr_pctl *pctl;
32 struct rk3368_ddrphy *phy;
33 struct rk3368_pmu_grf *pmugrf;
34 struct rk3368_msch *msch;
37 struct rk3368_sdram_params {
38 #if CONFIG_IS_ENABLED(OF_PLATDATA)
39 struct dtd_rockchip_rk3368_dmc of_plat;
41 struct rk3288_sdram_pctl_timing pctl_timing;
43 struct rk3288_sdram_channel chan;
54 DFI_INIT_START = BIT(0),
55 DFI_DATA_BYTE_DISABLE_EN = BIT(2),
58 DFI_DRAM_CLK_SR_EN = BIT(0),
59 DFI_DRAM_CLK_DPD_EN = BIT(1),
60 ODT_LEN_BL8_W_SHIFT = 16,
63 DFI_PARITY_INTR_EN = BIT(0),
64 DFI_PARITY_EN = BIT(1),
67 TLP_RESP_TIME_SHIFT = 16,
72 RANK0_ODT_WRITE_SEL = BIT(3),
73 RANK1_ODT_WRITE_SEL = BIT(11),
76 HW_LOW_POWER_EN = BIT(0),
93 POWER_UP_START = BIT(0),
96 POWER_UP_DONE = BIT(0),
119 DDR2_DDR3_BL_8 = BIT(0),
121 TFAW_TRRD_MULT4 = (0 << 18),
122 TFAW_TRRD_MULT5 = (1 << 18),
123 TFAW_TRRD_MULT6 = (2 << 18),
126 #define DDR3_MR0_WR(n) \
127 ((n <= 8) ? ((n - 4) << 9) : (((n >> 1) & 0x7) << 9))
128 #define DDR3_MR0_CL(n) \
129 ((((n - 4) & 0x7) << 4) | (((n - 4) & 0x8) >> 2))
130 #define DDR3_MR0_BL8 \
132 #define DDR3_MR0_DLL_RESET \
134 #define DDR3_MR1_RTT120OHM \
135 ((0 << 9) | (1 << 6) | (0 << 2))
136 #define DDR3_MR2_TWL(n) \
137 (((n - 5) & 0x7) << 3)
140 #ifdef CONFIG_TPL_BUILD
142 static void ddr_set_noc_spr_err_stall(struct rk3368_grf *grf, bool enable)
145 rk_setreg(&grf->ddrc0_con0, NOC_RSP_ERR_STALL);
147 rk_clrreg(&grf->ddrc0_con0, NOC_RSP_ERR_STALL);
150 static void ddr_set_ddr3_mode(struct rk3368_grf *grf, bool ddr3_mode)
153 rk_setreg(&grf->ddrc0_con0, MSCH0_MAINDDR3_DDR3);
155 rk_clrreg(&grf->ddrc0_con0, MSCH0_MAINDDR3_DDR3);
158 static void ddrphy_config(struct rk3368_ddrphy *phy,
159 u32 tcl, u32 tal, u32 tcwl)
163 /* Set to DDR3 mode */
164 clrsetbits_le32(&phy->reg[1], 0x3, 0x0);
166 /* DDRPHY_REGB: CL, AL */
167 clrsetbits_le32(&phy->reg[0xb], 0xff, tcl << 4 | tal);
168 /* DDRPHY_REGC: CWL */
169 clrsetbits_le32(&phy->reg[0xc], 0x0f, tcwl);
171 /* Update drive-strength */
172 writel(0xcc, &phy->reg[0x11]);
173 writel(0xaa, &phy->reg[0x16]);
175 * Update NRCOMP/PRCOMP for all 4 channels (for details of all
176 * affected registers refer to the documentation of DDRPHY_REG20
177 * and DDRPHY_REG21 in the RK3368 TRM.
179 for (i = 0; i < 4; ++i) {
180 writel(0xcc, &phy->reg[0x20 + i * 0x10]);
181 writel(0x44, &phy->reg[0x21 + i * 0x10]);
184 /* Enable write-leveling calibration bypass */
185 setbits_le32(&phy->reg[2], BIT(3));
188 static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
192 for (i = 0; i < n / sizeof(u32); i++)
193 writel(*src++, dest++);
196 static void send_command(struct rk3368_ddr_pctl *pctl, u32 rank, u32 cmd)
198 u32 mcmd = START_CMD | cmd | rank;
200 debug("%s: writing %x to MCMD\n", __func__, mcmd);
201 writel(mcmd, &pctl->mcmd);
202 while (readl(&pctl->mcmd) & START_CMD)
206 static void send_mrs(struct rk3368_ddr_pctl *pctl,
207 u32 rank, u32 mr_num, u32 mr_data)
209 u32 mcmd = START_CMD | MRS_CMD | rank | (mr_num << 17) | (mr_data << 4);
211 debug("%s: writing %x to MCMD\n", __func__, mcmd);
212 writel(mcmd, &pctl->mcmd);
213 while (readl(&pctl->mcmd) & START_CMD)
217 static int memory_init(struct rk3368_ddr_pctl *pctl,
218 struct rk3368_sdram_params *params)
221 const ulong timeout_ms = 500;
225 * Power up DRAM by DDR_PCTL_POWCTL[0] register of PCTL and
226 * wait power up DRAM finish with DDR_PCTL_POWSTAT[0] register
229 writel(POWER_UP_START, &pctl->powctl);
233 if (get_timer(tmp) > timeout_ms) {
234 pr_err("%s: POWER_UP_START did not complete in %ld ms\n",
235 __func__, timeout_ms);
238 } while (!(readl(&pctl->powstat) & POWER_UP_DONE));
240 /* Configure MR0 through MR3 */
241 mr[0] = DDR3_MR0_WR(params->pctl_timing.twr) |
242 DDR3_MR0_CL(params->pctl_timing.tcl) |
244 mr[1] = DDR3_MR1_RTT120OHM;
245 mr[2] = DDR3_MR2_TWL(params->pctl_timing.tcwl);
249 * Also see RK3368 Technical Reference Manual:
250 * "16.6.2 Initialization (DDR3 Initialization Sequence)"
252 send_command(pctl, MCMD_RANK0 | MCMD_RANK1, DESELECT_CMD);
254 send_command(pctl, MCMD_RANK0 | MCMD_RANK1, PREA_CMD);
255 send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 2, mr[2]);
256 send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 3, mr[3]);
257 send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 1, mr[1]);
258 send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 0, mr[0]);
259 send_command(pctl, MCMD_RANK0 | MCMD_RANK1, ZQCL_CMD);
264 static void move_to_config_state(struct rk3368_ddr_pctl *pctl)
267 * Also see RK3368 Technical Reference Manual:
268 * "16.6.1 State transition of PCTL (Moving to Config State)"
270 u32 state = readl(&pctl->stat) & PCTL_STAT_MSK;
274 writel(WAKEUP_STATE, &pctl->sctl);
275 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != ACCESS)
281 writel(CFG_STATE, &pctl->sctl);
282 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != CONFIG)
294 static void move_to_access_state(struct rk3368_ddr_pctl *pctl)
297 * Also see RK3368 Technical Reference Manual:
298 * "16.6.1 State transition of PCTL (Moving to Access State)"
300 u32 state = readl(&pctl->stat) & PCTL_STAT_MSK;
304 if (((readl(&pctl->stat) >> LP_TRIG_SHIFT) &
308 writel(WAKEUP_STATE, &pctl->sctl);
309 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != ACCESS)
314 writel(CFG_STATE, &pctl->sctl);
315 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != CONFIG)
320 writel(GO_STATE, &pctl->sctl);
321 while ((readl(&pctl->stat) & PCTL_STAT_MSK) == CONFIG)
333 static void ddrctl_reset(struct rk3368_cru *cru)
335 const u32 ctl_reset = BIT(3) | BIT(2);
336 const u32 phy_reset = BIT(1) | BIT(0);
339 * The PHY reset should be released before the PCTL reset.
341 * Note that the following sequence (including the number of
342 * us to delay between releasing the PHY and PCTL reset) has
343 * been adapted per feedback received from Rockchips, so do
344 * not try to optimise.
346 rk_setreg(&cru->softrst_con[10], ctl_reset | phy_reset);
348 rk_clrreg(&cru->softrst_con[10], phy_reset);
350 rk_clrreg(&cru->softrst_con[10], ctl_reset);
353 static void ddrphy_reset(struct rk3368_ddrphy *ddrphy)
356 * The analog part of the PHY should be release at least 1000
357 * DRAM cycles before the digital part of the PHY (waiting for
358 * 5us will ensure this for a DRAM clock as low as 200MHz).
360 clrbits_le32(&ddrphy->reg[0], BIT(3) | BIT(2));
362 setbits_le32(&ddrphy->reg[0], BIT(2));
364 setbits_le32(&ddrphy->reg[0], BIT(3));
367 static void ddrphy_config_delays(struct rk3368_ddrphy *ddrphy, u32 freq)
371 setbits_le32(&ddrphy->reg[0x13], BIT(4));
372 clrbits_le32(&ddrphy->reg[0x14], BIT(3));
374 setbits_le32(&ddrphy->reg[0x26], BIT(4));
375 clrbits_le32(&ddrphy->reg[0x27], BIT(3));
377 setbits_le32(&ddrphy->reg[0x36], BIT(4));
378 clrbits_le32(&ddrphy->reg[0x37], BIT(3));
380 setbits_le32(&ddrphy->reg[0x46], BIT(4));
381 clrbits_le32(&ddrphy->reg[0x47], BIT(3));
383 setbits_le32(&ddrphy->reg[0x56], BIT(4));
384 clrbits_le32(&ddrphy->reg[0x57], BIT(3));
386 if (freq <= 400000000)
387 setbits_le32(&ddrphy->reg[0xa4], 0x1f);
389 clrbits_le32(&ddrphy->reg[0xa4], 0x1f);
391 if (freq < 681000000)
392 dqs_dll_delay = 3; /* 67.5 degree delay */
394 dqs_dll_delay = 2; /* 45 degree delay */
396 writel(dqs_dll_delay, &ddrphy->reg[0x28]);
397 writel(dqs_dll_delay, &ddrphy->reg[0x38]);
398 writel(dqs_dll_delay, &ddrphy->reg[0x48]);
399 writel(dqs_dll_delay, &ddrphy->reg[0x58]);
402 static int dfi_cfg(struct rk3368_ddr_pctl *pctl)
404 const ulong timeout_ms = 200;
407 writel(DFI_DATA_BYTE_DISABLE_EN, &pctl->dfistcfg0);
409 writel(DFI_DRAM_CLK_SR_EN | DFI_DRAM_CLK_DPD_EN,
411 writel(DFI_PARITY_INTR_EN | DFI_PARITY_EN, &pctl->dfistcfg2);
412 writel(7 << TLP_RESP_TIME_SHIFT | LP_SR_EN | LP_PD_EN,
415 writel(1, &pctl->dfitphyupdtype0);
417 writel(0x1f, &pctl->dfitphyrdlat);
418 writel(0, &pctl->dfitphywrdata);
419 writel(0, &pctl->dfiupdcfg); /* phyupd and ctrlupd disabled */
421 setbits_le32(&pctl->dfistcfg0, DFI_INIT_START);
425 if (get_timer(tmp) > timeout_ms) {
426 pr_err("%s: DFI init did not complete within %ld ms\n",
427 __func__, timeout_ms);
430 } while ((readl(&pctl->dfiststat0) & 1) == 0);
435 static inline u32 ps_to_tCK(const u32 ps, const ulong freq)
437 const ulong MHz = 1000000;
438 return DIV_ROUND_UP(ps * freq, 1000000 * MHz);
441 static inline u32 ns_to_tCK(const u32 ns, const ulong freq)
443 return ps_to_tCK(ns * 1000, freq);
446 static inline u32 tCK_to_ps(const ulong tCK, const ulong freq)
448 const ulong MHz = 1000000;
449 return DIV_ROUND_UP(tCK * 1000000 * MHz, freq);
452 static int pctl_calc_timings(struct rk3368_sdram_params *params,
455 struct rk3288_sdram_pctl_timing *pctl_timing = ¶ms->pctl_timing;
456 const ulong MHz = 1000000;
460 if (params->ddr_speed_bin != DDR3_1600K) {
461 pr_err("%s: unimplemented DDR3 speed bin %d\n",
462 __func__, params->ddr_speed_bin);
466 /* PCTL is clocked at 1/2 the DRAM clock; err on the side of caution */
467 pctl_timing->togcnt1u = DIV_ROUND_UP(freq, 2 * MHz);
468 pctl_timing->togcnt100n = DIV_ROUND_UP(freq / 10, 2 * MHz);
470 pctl_timing->tinit = 200; /* 200 usec */
471 pctl_timing->trsth = 500; /* 500 usec */
472 pctl_timing->trefi = 78; /* 7.8usec = 78 * 100ns */
473 params->trefi_mem_ddr3 = ns_to_tCK(pctl_timing->trefi * 100, freq);
475 if (freq <= (400 * MHz)) {
476 pctl_timing->tcl = 6;
477 pctl_timing->tcwl = 10;
478 } else if (freq <= (533 * MHz)) {
479 pctl_timing->tcl = 8;
480 pctl_timing->tcwl = 6;
481 } else if (freq <= (666 * MHz)) {
482 pctl_timing->tcl = 10;
483 pctl_timing->tcwl = 7;
485 pctl_timing->tcl = 11;
486 pctl_timing->tcwl = 8;
489 pctl_timing->tmrd = 4; /* 4 tCK (all speed bins) */
490 pctl_timing->trfc = ns_to_tCK(350, freq); /* tRFC: 350 (max) @ 8GBit */
491 pctl_timing->trp = max(4u, ps_to_tCK(13750, freq));
494 * READ to WRITE Command Delay = RL + tCCD / 2 + 2tCK - WL
497 pctl_timing->trtw = pctl_timing->tcl + tccd/2 + 2 - pctl_timing->tcwl;
498 pctl_timing->tal = 0;
499 pctl_timing->tras = ps_to_tCK(35000, freq);
500 pctl_timing->trc = ps_to_tCK(48750, freq);
501 pctl_timing->trcd = ps_to_tCK(13750, freq);
502 pctl_timing->trrd = max(4u, ps_to_tCK(7500, freq));
503 pctl_timing->trtp = max(4u, ps_to_tCK(7500, freq));
504 pctl_timing->twr = ps_to_tCK(15000, freq);
505 /* The DDR3 mode-register does only support even values for tWR > 8. */
506 if (pctl_timing->twr > 8)
507 pctl_timing->twr = (pctl_timing->twr + 1) & ~1;
508 pctl_timing->twtr = max(4u, ps_to_tCK(7500, freq));
509 pctl_timing->texsr = 512; /* tEXSR(max) is tDLLLK */
510 pctl_timing->txp = max(3u, ps_to_tCK(6000, freq));
511 pctl_timing->txpdll = max(10u, ps_to_tCK(24000, freq));
512 pctl_timing->tzqcs = max(64u, ps_to_tCK(80000, freq));
513 pctl_timing->tzqcsi = 10000; /* as used by Rockchip */
514 pctl_timing->tdqs = 1; /* fixed for DDR3 */
515 pctl_timing->tcksre = max(5u, ps_to_tCK(10000, freq));
516 pctl_timing->tcksrx = max(5u, ps_to_tCK(10000, freq));
517 pctl_timing->tcke = max(3u, ps_to_tCK(5000, freq));
518 pctl_timing->tmod = max(12u, ps_to_tCK(15000, freq));
519 pctl_timing->trstl = ns_to_tCK(100, freq);
520 pctl_timing->tzqcl = max(256u, ps_to_tCK(320000, freq)); /* tZQoper */
521 pctl_timing->tmrr = 0;
522 pctl_timing->tckesr = pctl_timing->tcke + 1; /* JESD-79: tCKE + 1tCK */
523 pctl_timing->tdpd = 0; /* RK3368 TRM: "allowed values for DDR3: 0" */
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.
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;
542 params->tfaw_mult = TFAW_TRRD_MULT4;
548 static void pctl_cfg(struct rk3368_ddr_pctl *pctl,
549 struct rk3368_sdram_params *params,
550 struct rk3368_grf *grf)
552 /* Configure PCTL timing registers */
553 params->pctl_timing.trefi |= BIT(31); /* see PCTL_TREFI */
554 copy_to_reg(&pctl->togcnt1u, ¶ms->pctl_timing.togcnt1u,
555 sizeof(params->pctl_timing));
556 writel(params->trefi_mem_ddr3, &pctl->trefi_mem_ddr3);
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);
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);
567 writel(params->tfaw_mult | DDR3_EN | DDR2_DDR3_BL_8, &pctl->mcfg);
568 writel(0x001c0004, &grf->ddrc0_con0);
570 setbits_le32(&pctl->scfg, HW_LOW_POWER_EN);
573 static int ddrphy_data_training(struct rk3368_ddr_pctl *pctl,
574 struct rk3368_ddrphy *ddrphy)
576 const u32 trefi = readl(&pctl->trefi);
577 const ulong timeout_ms = 500;
580 /* disable auto-refresh */
581 writel(0 | BIT(31), &pctl->trefi);
583 clrsetbits_le32(&ddrphy->reg[2], 0x33, 0x20);
584 clrsetbits_le32(&ddrphy->reg[2], 0x33, 0x21);
588 if (get_timer(tmp) > timeout_ms) {
589 pr_err("%s: did not complete within %ld ms\n",
590 __func__, timeout_ms);
593 } while ((readl(&ddrphy->reg[0xff]) & 0xf) != 0xf);
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);
603 static int sdram_col_row_detect(struct udevice *dev)
605 struct dram_info *priv = dev_get_priv(dev);
606 struct rk3368_sdram_params *params = dev_get_platdata(dev);
607 struct rk3368_ddr_pctl *pctl = priv->pctl;
608 struct rk3368_msch *msch = priv->msch;
609 const u32 test_pattern = 0x5aa5f00f;
613 move_to_config_state(pctl);
614 writel(6, &msch->ddrconf);
615 move_to_access_state(pctl);
618 for (col = 11; col >= 9; col--) {
619 writel(0, CONFIG_SYS_SDRAM_BASE);
620 addr = CONFIG_SYS_SDRAM_BASE +
621 (1 << (col + params->chan.bw - 1));
622 writel(test_pattern, addr);
623 if ((readl(addr) == test_pattern) &&
624 (readl(CONFIG_SYS_SDRAM_BASE) == 0))
629 pr_err("%s: col detect error\n", __func__);
633 move_to_config_state(pctl);
634 writel(15, &msch->ddrconf);
635 move_to_access_state(pctl);
638 for (row = 16; row >= 12; row--) {
639 writel(0, CONFIG_SYS_SDRAM_BASE);
640 addr = CONFIG_SYS_SDRAM_BASE + (1 << (row + 15 - 1));
641 writel(test_pattern, addr);
642 if ((readl(addr) == test_pattern) &&
643 (readl(CONFIG_SYS_SDRAM_BASE) == 0))
648 pr_err("%s: row detect error\n", __func__);
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;
662 static int msch_niu_config(struct rk3368_msch *msch,
663 struct rk3368_sdram_params *params)
666 const u8 cols = params->chan.col - ((params->chan.bw == 2) ? 0 : 1);
667 const u8 rows = params->chan.cs0_row;
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).
678 } ddrconf_table[] = {
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).
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 },
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
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 },
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).
720 [14] = { .rows = 16, .columns = 10, .type = DMC_MSCH_CBDR },
721 [15] = { .rows = 16, .columns = 9, .type = DMC_MSCH_CBDR },
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.
729 for (i = 0; i < ARRAY_SIZE(ddrconf_table); i++) {
732 /* If this entry if for a different matcher, then skip it */
733 if (ddrconf_table[i].type != params->memory_schedule)
737 * Match according to the rules (exact/inexact/at-least)
738 * documented in the ddrconf_table above.
740 switch (params->memory_schedule) {
742 match = (ddrconf_table[i].columns == cols) &&
743 (ddrconf_table[i].rows == rows);
747 match = (ddrconf_table[i].columns == cols) &&
748 (ddrconf_table[i].rows <= rows);
752 match = (ddrconf_table[i].columns == cols) ||
753 ((ddrconf_table[i].columns <= cols) &&
754 (ddrconf_table[i].rows == rows));
762 debug("%s: setting ddrconf 0x%x\n", __func__, i);
763 writel(i, &msch->ddrconf);
768 pr_err("%s: ddrconf (NIU config) not found\n", __func__);
772 static void dram_all_config(struct udevice *dev)
774 struct dram_info *priv = dev_get_priv(dev);
775 struct rk3368_pmu_grf *pmugrf = priv->pmugrf;
776 struct rk3368_sdram_params *params = dev_get_platdata(dev);
777 const struct rk3288_sdram_channel *info = ¶ms->chan;
781 sys_reg |= DDR3 << SYS_REG_DDRTYPE_SHIFT;
782 sys_reg |= 0 << SYS_REG_NUM_CH_SHIFT;
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);
794 writel(sys_reg, &pmugrf->os_reg[2]);
797 static int setup_sdram(struct udevice *dev)
799 struct dram_info *priv = dev_get_priv(dev);
800 struct rk3368_sdram_params *params = dev_get_platdata(dev);
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;
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);
813 debug("%s: could not set DDR clock: %d\n", __func__, ret);
817 /* Update the read-latency for the RK3368 */
818 writel(0x32, &msch->readlatency);
820 /* Initialise the DDR PCTL and DDR PHY */
822 ddrphy_reset(ddrphy);
823 ddrphy_config_delays(ddrphy, params->ddr_freq);
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);
838 /* Initialize DRAM and configure with mode-register values */
839 ret = memory_init(pctl, params);
843 move_to_config_state(pctl);
844 /* Perform data-training */
845 ddrphy_data_training(pctl, ddrphy);
846 move_to_access_state(pctl);
848 /* TODO(prt): could detect rank in training... */
849 #ifdef CONFIG_TARGET_EVB_PX5
850 params->chan.rank = 1;
852 params->chan.rank = 2;
854 /* 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 */
858 /* DDR3 is always 8 bank */
860 /* Detect col and row number */
861 ret = sdram_col_row_detect(dev);
865 /* Configure NIU DDR configuration */
866 ret = msch_niu_config(msch, params);
870 /* set up OS_REG to communicate w/ next stage and OS */
871 dram_all_config(dev);
876 printf("DRAM init failed!\n");
881 static int rk3368_dmc_ofdata_to_platdata(struct udevice *dev)
885 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
886 struct rk3368_sdram_params *plat = dev_get_platdata(dev);
888 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
896 #if CONFIG_IS_ENABLED(OF_PLATDATA)
897 static int conv_of_platdata(struct udevice *dev)
899 struct rk3368_sdram_params *plat = dev_get_platdata(dev);
900 struct dtd_rockchip_rk3368_dmc *of_plat = &plat->of_plat;
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;
910 static int rk3368_dmc_probe(struct udevice *dev)
912 #ifdef CONFIG_TPL_BUILD
913 struct rk3368_sdram_params *plat = dev_get_platdata(dev);
914 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;
920 struct udevice *dev_clk;
922 struct dram_info *priv = dev_get_priv(dev);
924 #if CONFIG_IS_ENABLED(OF_PLATDATA)
925 ret = conv_of_platdata(dev);
930 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
931 debug("%s: pmugrf=%p\n", __func__, priv->pmugrf);
933 #ifdef CONFIG_TPL_BUILD
934 pctl = (struct rk3368_ddr_pctl *)plat->of_plat.reg[0];
935 ddrphy = (struct rk3368_ddrphy *)plat->of_plat.reg[2];
936 msch = syscon_get_first_range(ROCKCHIP_SYSCON_MSCH);
937 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
944 ret = rockchip_get_clk(&dev_clk);
947 priv->ddr_clk.id = CLK_DDR;
948 ret = clk_request(dev_clk, &priv->ddr_clk);
952 cru = rockchip_get_cru();
954 if (IS_ERR(priv->cru))
955 return PTR_ERR(priv->cru);
957 ret = setup_sdram(dev);
964 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg[2]);
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.
971 priv->info.size = min(priv->info.size, (size_t)0xfe000000);
976 static int rk3368_dmc_get_info(struct udevice *dev, struct ram_info *info)
978 struct dram_info *priv = dev_get_priv(dev);
984 static struct ram_ops rk3368_dmc_ops = {
985 .get_info = rk3368_dmc_get_info,
989 static const struct udevice_id rk3368_dmc_ids[] = {
990 { .compatible = "rockchip,rk3368-dmc" },
994 U_BOOT_DRIVER(dmc_rk3368) = {
995 .name = "rockchip_rk3368_dmc",
997 .of_match = rk3368_dmc_ids,
998 .ops = &rk3368_dmc_ops,
999 .probe = rk3368_dmc_probe,
1000 .priv_auto_alloc_size = sizeof(struct dram_info),
1001 .ofdata_to_platdata = rk3368_dmc_ofdata_to_platdata,
1002 .probe = rk3368_dmc_probe,
1003 .priv_auto_alloc_size = sizeof(struct dram_info),
1004 .platdata_auto_alloc_size = sizeof(struct rk3368_sdram_params),