1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * (C) Copyright 2017 Rockchip Electronics Co., Ltd.
7 #include <debug_uart.h>
9 #include <dt-structs.h>
15 #include <asm/arch-rockchip/clock.h>
16 #include <asm/arch-rockchip/cru_rk3328.h>
17 #include <asm/arch-rockchip/grf_rk3328.h>
18 #include <asm/arch-rockchip/sdram.h>
19 #include <asm/arch-rockchip/sdram_rk3328.h>
20 #include <asm/arch-rockchip/uart.h>
23 #ifdef CONFIG_TPL_BUILD
24 struct ddr_pctl_regs *pctl;
25 struct ddr_phy_regs *phy;
27 struct rk3328_cru *cru;
28 struct msch_regs *msch;
29 struct rk3328_ddr_grf_regs *ddr_grf;
32 struct rk3328_grf_regs *grf;
35 #ifdef CONFIG_TPL_BUILD
37 struct rk3328_sdram_channel sdram_ch;
39 struct rockchip_dmc_plat {
40 #if CONFIG_IS_ENABLED(OF_PLATDATA)
41 struct dtd_rockchip_rk3328_dmc dtplat;
43 struct rk3328_sdram_params sdram_params;
48 #if CONFIG_IS_ENABLED(OF_PLATDATA)
49 static int conv_of_platdata(struct udevice *dev)
51 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
52 struct dtd_rockchip_rk3328_dmc *dtplat = &plat->dtplat;
55 ret = regmap_init_mem_platdata(dev, dtplat->reg,
56 ARRAY_SIZE(dtplat->reg) / 2,
65 static void rkclk_ddr_reset(struct dram_info *dram,
66 u32 ctl_srstn, u32 ctl_psrstn,
67 u32 phy_srstn, u32 phy_psrstn)
69 writel(ddrctrl_srstn_req(ctl_srstn) | ddrctrl_psrstn_req(ctl_psrstn) |
70 ddrphy_srstn_req(phy_srstn) | ddrphy_psrstn_req(phy_psrstn),
71 &dram->cru->softrst_con[5]);
72 writel(ddrctrl_asrstn_req(ctl_srstn), &dram->cru->softrst_con[9]);
75 static void rkclk_set_dpll(struct dram_info *dram, unsigned int hz)
77 unsigned int refdiv, postdiv1, postdiv2, fbdiv;
85 } else if (mhz <= 400) {
88 } else if (mhz <= 600) {
91 } else if (mhz <= 800) {
94 } else if (mhz <= 1600) {
101 fbdiv = (mhz * refdiv * postdiv1 * postdiv2) / 24;
103 writel(((0x1 << 4) << 16) | (0 << 4), &dram->cru->mode_con);
104 writel(POSTDIV1(postdiv1) | FBDIV(fbdiv), &dram->cru->dpll_con[0]);
105 writel(DSMPD(1) | POSTDIV2(postdiv2) | REFDIV(refdiv),
106 &dram->cru->dpll_con[1]);
110 if (LOCK(readl(&dram->cru->dpll_con[1])))
115 writel(((0x1 << 4) << 16) | (1 << 4), &dram->cru->mode_con);
118 static void rkclk_configure_ddr(struct dram_info *dram,
119 struct rk3328_sdram_params *sdram_params)
121 void __iomem *phy_base = dram->phy;
123 /* choose DPLL for ddr clk source */
124 clrbits_le32(PHY_REG(phy_base, 0xef), 1 << 7);
126 /* for inno ddr phy need 2*freq */
127 rkclk_set_dpll(dram, sdram_params->base.ddr_freq * MHZ * 2);
130 /* return ddrconfig value
131 * (-1), find ddrconfig fail
132 * other, the ddrconfig value
133 * only support cs0_row >= cs1_row
135 static u32 calculate_ddrconfig(struct rk3328_sdram_params *sdram_params)
137 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
138 u32 cs, bw, die_bw, col, row, bank;
145 die_bw = cap_info->dbw;
147 row = cap_info->cs0_row;
148 cs1_row = cap_info->cs1_row;
151 if (sdram_params->base.dramtype == DDR4) {
152 /* when DDR_TEST, CS always at MSB position for easy test */
153 if (cs == 2 && row == cs1_row) {
154 /* include 2cs cap both 2^n or both (2^n - 2^(n-2)) */
155 tmp = ((row - 13) << 3) | (1 << 2) | (bw & 0x2) |
157 for (i = 17; i < 21; i++) {
159 (ddr4_cfg_2_rbc[i - 10] & 0x7)) &&
161 (ddr4_cfg_2_rbc[i - 10] & 0x3c))) {
168 tmp = ((cs - 1) << 6) | ((row - 13) << 3) | (bw & 0x2) | die_bw;
169 for (i = 10; i < 17; i++) {
170 if (((tmp & 0x7) == (ddr4_cfg_2_rbc[i - 10] & 0x7)) &&
171 ((tmp & 0x3c) <= (ddr4_cfg_2_rbc[i - 10] & 0x3c)) &&
172 ((tmp & 0x40) <= (ddr4_cfg_2_rbc[i - 10] & 0x40))) {
183 /* when DDR_TEST, CS always at MSB position for easy test */
184 if (cs == 2 && row == cs1_row) {
185 /* include 2cs cap both 2^n or both (2^n - 2^(n-2)) */
186 for (i = 5; i < 8; i++) {
187 if ((bw + col - 11) == (ddr_cfg_2_rbc[i] &
195 tmp = ((row - 13) << 4) | (1 << 2) | ((bw + col - 11) << 0);
196 for (i = 0; i < 5; i++)
197 if (((tmp & 0xf) == (ddr_cfg_2_rbc[i] & 0xf)) &&
198 ((tmp & 0x30) <= (ddr_cfg_2_rbc[i] & 0x30))) {
206 printf("calculate ddrconfig error\n");
212 * calculate controller dram address map, and setting to register.
213 * argument sdram_ch.ddrconf must be right value before
214 * call this function.
216 static void set_ctl_address_map(struct dram_info *dram,
217 struct rk3328_sdram_params *sdram_params)
219 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
220 void __iomem *pctl_base = dram->pctl;
222 sdram_copy_to_reg((u32 *)(pctl_base + DDR_PCTL2_ADDRMAP0),
223 &addrmap[cap_info->ddrconfig][0], 9 * 4);
224 if (sdram_params->base.dramtype == LPDDR3 && cap_info->row_3_4)
225 setbits_le32(pctl_base + DDR_PCTL2_ADDRMAP6, 1 << 31);
226 if (sdram_params->base.dramtype == DDR4 && cap_info->bw == 0x1)
227 setbits_le32(pctl_base + DDR_PCTL2_PCCFG, 1 << 8);
229 if (cap_info->rank == 1)
230 clrsetbits_le32(pctl_base + DDR_PCTL2_ADDRMAP0, 0x1f, 0x1f);
233 static int data_training(struct dram_info *dram, u32 cs, u32 dramtype)
235 void __iomem *pctl_base = dram->pctl;
240 /* disable auto low-power */
241 pwrctl = readl(pctl_base + DDR_PCTL2_PWRCTL);
242 writel(0, pctl_base + DDR_PCTL2_PWRCTL);
244 dis_auto_zq = pctl_dis_zqcs_aref(dram->pctl);
246 ret = phy_data_training(dram->phy, cs, dramtype);
248 pctl_rest_zqcs_aref(dram->pctl, dis_auto_zq);
250 /* restore auto low-power */
251 writel(pwrctl, pctl_base + DDR_PCTL2_PWRCTL);
256 static void rx_deskew_switch_adjust(struct dram_info *dram)
260 void __iomem *phy_base = dram->phy;
262 for (i = 0; i < 4; i++)
263 gate_val = MAX(readl(PHY_REG(phy_base, 0xfb + i)), gate_val);
265 deskew_val = (gate_val >> 3) + 1;
266 deskew_val = (deskew_val > 0x1f) ? 0x1f : deskew_val;
267 clrsetbits_le32(PHY_REG(phy_base, 0x6e), 0xc, (deskew_val & 0x3) << 2);
268 clrsetbits_le32(PHY_REG(phy_base, 0x6f), 0x7 << 4,
269 (deskew_val & 0x1c) << 2);
272 static void tx_deskew_switch_adjust(struct dram_info *dram)
274 void __iomem *phy_base = dram->phy;
276 clrsetbits_le32(PHY_REG(phy_base, 0x6e), 0x3, 1);
279 static void set_ddrconfig(struct dram_info *dram, u32 ddrconfig)
281 writel(ddrconfig, &dram->msch->ddrconf);
284 static void sdram_msch_config(struct msch_regs *msch,
285 struct sdram_msch_timings *noc_timings)
287 writel(noc_timings->ddrtiming.d32, &msch->ddrtiming);
289 writel(noc_timings->ddrmode.d32, &msch->ddrmode);
290 writel(noc_timings->readlatency, &msch->readlatency);
292 writel(noc_timings->activate.d32, &msch->activate);
293 writel(noc_timings->devtodev.d32, &msch->devtodev);
294 writel(noc_timings->ddr4timing.d32, &msch->ddr4_timing);
295 writel(noc_timings->agingx0, &msch->aging0);
296 writel(noc_timings->agingx0, &msch->aging1);
297 writel(noc_timings->agingx0, &msch->aging2);
298 writel(noc_timings->agingx0, &msch->aging3);
299 writel(noc_timings->agingx0, &msch->aging4);
300 writel(noc_timings->agingx0, &msch->aging5);
303 static void dram_all_config(struct dram_info *dram,
304 struct rk3328_sdram_params *sdram_params)
306 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
310 set_ddrconfig(dram, cap_info->ddrconfig);
311 sdram_org_config(cap_info, &sdram_params->base, &sys_reg2,
313 writel(sys_reg2, &dram->grf->os_reg[2]);
314 writel(sys_reg3, &dram->grf->os_reg[3]);
316 sdram_msch_config(dram->msch, &sdram_ch.noc_timings);
319 static void enable_low_power(struct dram_info *dram,
320 struct rk3328_sdram_params *sdram_params)
322 void __iomem *pctl_base = dram->pctl;
324 /* enable upctl2 axi clock auto gating */
325 writel(0x00800000, &dram->ddr_grf->ddr_grf_con[0]);
326 writel(0x20012001, &dram->ddr_grf->ddr_grf_con[2]);
327 /* enable upctl2 core clock auto gating */
328 writel(0x001e001a, &dram->ddr_grf->ddr_grf_con[2]);
331 clrbits_le32(pctl_base + DDR_PCTL2_PWRCTL, (1 << 1));
333 setbits_le32(pctl_base + DDR_PCTL2_PWRCTL, (1 << 1));
335 clrbits_le32(pctl_base + DDR_PCTL2_PWRCTL, 1);
337 setbits_le32(pctl_base + DDR_PCTL2_PWRCTL, 1);
338 setbits_le32(pctl_base + DDR_PCTL2_PWRCTL, (1 << 3));
341 static int sdram_init(struct dram_info *dram,
342 struct rk3328_sdram_params *sdram_params, u32 pre_init)
344 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
345 void __iomem *pctl_base = dram->pctl;
347 rkclk_ddr_reset(dram, 1, 1, 1, 1);
350 * dereset ddr phy psrstn to config pll,
351 * if using phy pll psrstn must be dereset
354 rkclk_ddr_reset(dram, 1, 1, 1, 0);
355 rkclk_configure_ddr(dram, sdram_params);
357 /* release phy srst to provide clk to ctrl */
358 rkclk_ddr_reset(dram, 1, 1, 0, 0);
360 phy_soft_reset(dram->phy);
361 /* release ctrl presetn, and config ctl registers */
362 rkclk_ddr_reset(dram, 1, 0, 0, 0);
363 pctl_cfg(dram->pctl, &sdram_params->pctl_regs, SR_IDLE, PD_IDLE);
364 cap_info->ddrconfig = calculate_ddrconfig(sdram_params);
365 set_ctl_address_map(dram, sdram_params);
366 phy_cfg(dram->phy, &sdram_params->phy_regs, &sdram_params->skew,
367 &sdram_params->base, cap_info->bw);
369 /* enable dfi_init_start to init phy after ctl srstn deassert */
370 setbits_le32(pctl_base + DDR_PCTL2_DFIMISC, (1 << 5) | (1 << 4));
371 rkclk_ddr_reset(dram, 0, 0, 0, 0);
372 /* wait for dfi_init_done and dram init complete */
373 while ((readl(pctl_base + DDR_PCTL2_STAT) & 0x7) == 0)
376 /* do ddr gate training */
377 if (data_training(dram, 0, sdram_params->base.dramtype) != 0) {
378 printf("data training error\n");
382 if (sdram_params->base.dramtype == DDR4)
383 pctl_write_vrefdq(dram->pctl, 0x3, 5670,
384 sdram_params->base.dramtype);
387 rx_deskew_switch_adjust(dram);
388 tx_deskew_switch_adjust(dram);
391 dram_all_config(dram, sdram_params);
392 enable_low_power(dram, sdram_params);
397 static u64 dram_detect_cap(struct dram_info *dram,
398 struct rk3328_sdram_params *sdram_params,
399 unsigned char channel)
401 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
404 * for ddr3: ddrconf = 3
405 * for ddr4: ddrconf = 12
406 * for lpddr3: ddrconf = 3
414 u32 dram_type = sdram_params->base.dramtype;
416 if (dram_type != DDR4) {
417 /* detect col and bk for ddr3/lpddr3 */
422 if (sdram_detect_col(cap_info, coltmp) != 0)
424 sdram_detect_bank(cap_info, coltmp, bktmp);
425 sdram_detect_dbw(cap_info, dram_type);
427 /* detect bg for ddr4 */
436 sdram_detect_bg(cap_info, coltmp);
440 if (sdram_detect_row(cap_info, coltmp, bktmp, rowtmp) != 0)
444 sdram_detect_row_3_4(cap_info, coltmp, bktmp);
446 /* bw and cs detect using data training */
447 if (data_training(dram, 1, dram_type) == 0)
451 cap_info->rank = cs + 1;
456 cap_info->cs0_high16bit_row = cap_info->cs0_row;
458 cap_info->cs1_row = cap_info->cs0_row;
459 cap_info->cs1_high16bit_row = cap_info->cs0_row;
461 cap_info->cs1_row = 0;
462 cap_info->cs1_high16bit_row = 0;
470 static int sdram_init_detect(struct dram_info *dram,
471 struct rk3328_sdram_params *sdram_params)
475 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
477 debug("Starting SDRAM initialization...\n");
479 memcpy(&sdram_ch, &sdram_params->ch,
480 sizeof(struct rk3328_sdram_channel));
482 sdram_init(dram, sdram_params, 0);
483 dram_detect_cap(dram, sdram_params, 0);
485 /* modify bw, cs related timing */
486 pctl_remodify_sdram_params(&sdram_params->pctl_regs, cap_info,
487 sdram_params->base.dramtype);
489 if (cap_info->bw == 2)
490 sdram_ch.noc_timings.ddrtiming.b.bwratio = 0;
492 sdram_ch.noc_timings.ddrtiming.b.bwratio = 1;
494 /* reinit sdram by real dram cap */
495 sdram_init(dram, sdram_params, 1);
497 /* redetect cs1 row */
498 sdram_detect_cs1_row(cap_info, sdram_params->base.dramtype);
499 if (cap_info->cs1_row) {
500 sys_reg = readl(&dram->grf->os_reg[2]);
501 sys_reg3 = readl(&dram->grf->os_reg[3]);
502 SYS_REG_ENC_CS1_ROW(cap_info->cs1_row,
503 sys_reg, sys_reg3, 0);
504 writel(sys_reg, &dram->grf->os_reg[2]);
505 writel(sys_reg3, &dram->grf->os_reg[3]);
508 sdram_print_ddr_info(&sdram_params->ch.cap_info, &sdram_params->base);
513 static int rk3328_dmc_init(struct udevice *dev)
515 struct dram_info *priv = dev_get_priv(dev);
516 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
519 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
520 struct rk3328_sdram_params *params = &plat->sdram_params;
522 struct dtd_rockchip_rk3328_dmc *dtplat = &plat->dtplat;
523 struct rk3328_sdram_params *params =
524 (void *)dtplat->rockchip_sdram_params;
526 ret = conv_of_platdata(dev);
530 priv->phy = regmap_get_range(plat->map, 0);
531 priv->pctl = regmap_get_range(plat->map, 1);
532 priv->grf = regmap_get_range(plat->map, 2);
533 priv->cru = regmap_get_range(plat->map, 3);
534 priv->msch = regmap_get_range(plat->map, 4);
535 priv->ddr_grf = regmap_get_range(plat->map, 5);
537 debug("%s phy %p pctrl %p grf %p cru %p msch %p ddr_grf %p\n",
538 __func__, priv->phy, priv->pctl, priv->grf, priv->cru,
539 priv->msch, priv->ddr_grf);
540 ret = sdram_init_detect(priv, params);
542 printf("%s DRAM init failed%d\n", __func__, ret);
549 static int rk3328_dmc_ofdata_to_platdata(struct udevice *dev)
551 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
552 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
555 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
556 (u32 *)&plat->sdram_params,
557 sizeof(plat->sdram_params) / sizeof(u32));
559 printf("%s: Cannot read rockchip,sdram-params %d\n",
563 ret = regmap_init_mem(dev, &plat->map);
565 printf("%s: regmap failed %d\n", __func__, ret);
572 static int rk3328_dmc_probe(struct udevice *dev)
574 #ifdef CONFIG_TPL_BUILD
575 if (rk3328_dmc_init(dev))
578 struct dram_info *priv = dev_get_priv(dev);
580 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
581 debug("%s: grf=%p\n", __func__, priv->grf);
582 priv->info.base = CONFIG_SYS_SDRAM_BASE;
583 priv->info.size = rockchip_sdram_size(
584 (phys_addr_t)&priv->grf->os_reg[2]);
589 static int rk3328_dmc_get_info(struct udevice *dev, struct ram_info *info)
591 struct dram_info *priv = dev_get_priv(dev);
598 static struct ram_ops rk3328_dmc_ops = {
599 .get_info = rk3328_dmc_get_info,
602 static const struct udevice_id rk3328_dmc_ids[] = {
603 { .compatible = "rockchip,rk3328-dmc" },
607 U_BOOT_DRIVER(dmc_rk3328) = {
608 .name = "rockchip_rk3328_dmc",
610 .of_match = rk3328_dmc_ids,
611 .ops = &rk3328_dmc_ops,
612 #ifdef CONFIG_TPL_BUILD
613 .ofdata_to_platdata = rk3328_dmc_ofdata_to_platdata,
615 .probe = rk3328_dmc_probe,
616 .priv_auto_alloc_size = sizeof(struct dram_info),
617 #ifdef CONFIG_TPL_BUILD
618 .platdata_auto_alloc_size = sizeof(struct rockchip_dmc_plat),