1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2018 Rockchip Electronics Co., Ltd.
7 #include <debug_uart.h>
14 #include <asm/arch-rockchip/clock.h>
15 #include <asm/arch-rockchip/cru_px30.h>
16 #include <asm/arch-rockchip/grf_px30.h>
17 #include <asm/arch-rockchip/hardware.h>
18 #include <asm/arch-rockchip/sdram.h>
19 #include <asm/arch-rockchip/sdram_px30.h>
22 #ifdef CONFIG_TPL_BUILD
23 struct ddr_pctl_regs *pctl;
24 struct ddr_phy_regs *phy;
26 struct msch_regs *msch;
27 struct px30_ddr_grf_regs *ddr_grf;
31 struct px30_pmugrf *pmugrf;
34 #ifdef CONFIG_TPL_BUILD
36 u8 ddr_cfg_2_rbc[] = {
39 * [3] bank(0:4bank,1:8bank)
42 ((5 << 4) | (1 << 3) | 0), /* 0 */
43 ((5 << 4) | (1 << 3) | 1), /* 1 */
44 ((4 << 4) | (1 << 3) | 2), /* 2 */
45 ((3 << 4) | (1 << 3) | 3), /* 3 */
46 ((2 << 4) | (1 << 3) | 4), /* 4 */
47 ((5 << 4) | (0 << 3) | 2), /* 5 */
48 ((4 << 4) | (1 << 3) | 2), /* 6 */
49 /*((0<<3)|3),*/ /* 12 for ddr4 */
50 /*((1<<3)|1),*/ /* 13 B,C exchange for rkvdec */
54 * for ddr4 if ddrconfig=7, upctl should set 7 and noc should
55 * set to 1 for more efficient.
56 * noc ddrconf, upctl addrmap
63 u8 d4_rbc_2_d3_rbc[] = {
72 * row higher than cs should be disabled by set to 0xf
73 * rank addrmap calculate by real cap.
76 /* map0 map1, map2, map3, map4, map5
78 * -------------------------------------------------------
79 * bk2-0 col 5-2 col 9-6 col 11-10 row 11-0
80 * row 15-12 row 17-16 bg1,0
81 * -------------------------------------------------------
85 {0x00060606, 0x00000000, 0x1f1f0000, 0x00001f1f, 0x05050505,
86 0x05050505, 0x00000505, 0x3f3f}, /* 0 */
87 {0x00070707, 0x00000000, 0x1f000000, 0x00001f1f, 0x06060606,
88 0x06060606, 0x06060606, 0x3f3f}, /* 1 */
89 {0x00080808, 0x00000000, 0x00000000, 0x00001f1f, 0x07070707,
90 0x07070707, 0x00000f07, 0x3f3f}, /* 2 */
91 {0x00090909, 0x00000000, 0x00000000, 0x00001f00, 0x08080808,
92 0x08080808, 0x00000f0f, 0x3f3f}, /* 3 */
93 {0x000a0a0a, 0x00000000, 0x00000000, 0x00000000, 0x09090909,
94 0x0f090909, 0x00000f0f, 0x3f3f}, /* 4 */
95 {0x00080808, 0x00000000, 0x00000000, 0x00001f1f, 0x06060606,
96 0x06060606, 0x00000606, 0x3f3f}, /* 5 */
97 {0x00080808, 0x00000000, 0x00000000, 0x00001f1f, 0x07070707,
98 0x07070707, 0x00000f0f, 0x3f3f}, /* 6 */
99 {0x003f0808, 0x00000006, 0x1f1f0000, 0x00001f1f, 0x06060606,
100 0x06060606, 0x00000606, 0x0600}, /* 7 */
101 {0x003f0909, 0x00000007, 0x1f000000, 0x00001f1f, 0x07070707,
102 0x07070707, 0x00000f07, 0x0700}, /* 8 */
103 {0x003f0a0a, 0x01010100, 0x01010101, 0x00001f1f, 0x08080808,
104 0x08080808, 0x00000f0f, 0x0801}, /* 9 */
105 {0x003f0909, 0x01010100, 0x01010101, 0x00001f1f, 0x07070707,
106 0x07070707, 0x00000f07, 0x3f01}, /* 10 */
107 {0x003f0808, 0x00000007, 0x1f000000, 0x00001f1f, 0x06060606,
108 0x06060606, 0x00000606, 0x3f00}, /* 11 */
109 /* when ddr4 12 map to 10, when ddr3 12 unused */
110 {0x003f0909, 0x01010100, 0x01010101, 0x00001f1f, 0x07070707,
111 0x07070707, 0x00000f07, 0x3f01}, /* 10 */
112 {0x00070706, 0x00000000, 0x1f010000, 0x00001f1f, 0x06060606,
113 0x06060606, 0x00000606, 0x3f3f}, /* 13 */
116 #define PMUGRF_BASE_ADDR 0xFF010000
117 #define CRU_BASE_ADDR 0xFF2B0000
118 #define GRF_BASE_ADDR 0xFF140000
119 #define DDRC_BASE_ADDR 0xFF600000
120 #define DDR_PHY_BASE_ADDR 0xFF2A0000
121 #define SERVER_MSCH0_BASE_ADDR 0xFF530000
122 #define DDR_GRF_BASE_ADDR 0xff630000
124 struct dram_info dram_info;
126 struct px30_sdram_params sdram_configs[] = {
127 #include "sdram-px30-ddr3-detect-333.inc"
130 struct ddr_phy_skew skew = {
131 #include "sdram-px30-ddr_skew.inc"
134 static void rkclk_ddr_reset(struct dram_info *dram,
135 u32 ctl_srstn, u32 ctl_psrstn,
136 u32 phy_srstn, u32 phy_psrstn)
138 writel(upctl2_srstn_req(ctl_srstn) | upctl2_psrstn_req(ctl_psrstn) |
139 upctl2_asrstn_req(ctl_srstn),
140 &dram->cru->softrst_con[1]);
141 writel(ddrphy_srstn_req(phy_srstn) | ddrphy_psrstn_req(phy_psrstn),
142 &dram->cru->softrst_con[2]);
145 static void rkclk_set_dpll(struct dram_info *dram, unsigned int hz)
147 unsigned int refdiv, postdiv1, postdiv2, fbdiv;
155 } else if (mhz <= 400) {
158 } else if (mhz <= 600) {
161 } else if (mhz <= 800) {
164 } else if (mhz <= 1600) {
171 fbdiv = (mhz * refdiv * postdiv1 * postdiv2) / 24;
173 writel(DPLL_MODE(CLOCK_FROM_XIN_OSC), &dram->cru->mode);
175 writel(POSTDIV1(postdiv1) | FBDIV(fbdiv), &dram->cru->pll[1].con0);
176 writel(DSMPD(1) | POSTDIV2(postdiv2) | REFDIV(refdiv),
177 &dram->cru->pll[1].con1);
181 if (LOCK(readl(&dram->cru->pll[1].con1)))
186 writel(DPLL_MODE(CLOCK_FROM_PLL), &dram->cru->mode);
189 static void rkclk_configure_ddr(struct dram_info *dram,
190 struct px30_sdram_params *sdram_params)
192 /* for inno ddr phy need 2*freq */
193 rkclk_set_dpll(dram, sdram_params->base.ddr_freq * MHz * 2);
196 /* return ddrconfig value
197 * (-1), find ddrconfig fail
198 * other, the ddrconfig value
199 * only support cs0_row >= cs1_row
201 static unsigned int calculate_ddrconfig(struct px30_sdram_params *sdram_params)
203 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
204 u32 bw, die_bw, col, bank;
209 die_bw = cap_info->dbw;
213 if (sdram_params->base.dramtype == DDR4) {
218 ddrconf = d4_rbc_2_d3_rbc[ddrconf - 7];
220 tmp = ((bank - 2) << 3) | (col + bw - 10);
221 for (i = 0; i < 7; i++)
222 if ((ddr_cfg_2_rbc[i] & 0xf) == tmp) {
227 printascii("calculate ddrconfig error\n");
234 * calculate controller dram address map, and setting to register.
235 * argument sdram_params->ch.ddrconf must be right value before
236 * call this function.
238 static void set_ctl_address_map(struct dram_info *dram,
239 struct px30_sdram_params *sdram_params)
241 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
242 void __iomem *pctl_base = dram->pctl;
243 u32 cs_pst, bg, max_row, ddrconf;
246 if (sdram_params->base.dramtype == DDR4)
248 * DDR4 8bit dram BG = 2(4bank groups),
249 * 16bit dram BG = 1 (2 bank groups)
251 bg = (cap_info->dbw == 0) ? 2 : 1;
255 cs_pst = cap_info->bw + cap_info->col +
256 bg + cap_info->bk + cap_info->cs0_row;
257 if (cs_pst >= 32 || cap_info->rank == 1)
258 writel(0x1f, pctl_base + DDR_PCTL2_ADDRMAP0);
260 writel(cs_pst - 8, pctl_base + DDR_PCTL2_ADDRMAP0);
262 ddrconf = cap_info->ddrconfig;
263 if (sdram_params->base.dramtype == DDR4) {
264 for (i = 0; i < ARRAY_SIZE(d4_rbc_2_d3_rbc); i++) {
265 if (d4_rbc_2_d3_rbc[i] == ddrconf) {
272 sdram_copy_to_reg((u32 *)(pctl_base + DDR_PCTL2_ADDRMAP1),
273 &addrmap[ddrconf][0], 8 * 4);
274 max_row = cs_pst - 1 - 8 - (addrmap[ddrconf][5] & 0xf);
277 printascii("set addrmap fail\n");
278 /* need to disable row ahead of rank by set to 0xf */
279 for (i = 17; i > max_row; i--)
280 clrsetbits_le32(pctl_base + DDR_PCTL2_ADDRMAP6 +
281 ((i - 12) * 8 / 32) * 4,
282 0xf << ((i - 12) * 8 % 32),
283 0xf << ((i - 12) * 8 % 32));
285 if ((sdram_params->base.dramtype == LPDDR3 ||
286 sdram_params->base.dramtype == LPDDR2) &&
288 setbits_le32(pctl_base + DDR_PCTL2_ADDRMAP6, 1 << 31);
289 if (sdram_params->base.dramtype == DDR4 && cap_info->bw != 0x2)
290 setbits_le32(pctl_base + DDR_PCTL2_PCCFG, 1 << 8);
297 int read_mr(struct dram_info *dram, u32 rank, u32 mr_num)
299 void __iomem *ddr_grf_base = dram->ddr_grf;
301 pctl_read_mr(dram->pctl, rank, mr_num);
303 return (readl(ddr_grf_base + DDR_GRF_STATUS(0)) & 0xff);
306 #define MIN(a, b) (((a) > (b)) ? (b) : (a))
307 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
308 static u32 check_rd_gate(struct dram_info *dram)
310 void __iomem *phy_base = dram->phy;
317 bw = (readl(PHY_REG(phy_base, 0x0)) >> 4) & 0xf;
331 for (i = 0; i < bw; i++) {
332 gate[i] = readl(PHY_REG(phy_base, 0xfb + i));
333 max_val = MAX(max_val, gate[i]);
334 min_val = MIN(min_val, gate[i]);
337 if (max_val > 0x80 || min_val < 0x20)
343 static int data_training(struct dram_info *dram, u32 cs, u32 dramtype)
345 void __iomem *pctl_base = dram->pctl;
350 /* disable auto low-power */
351 pwrctl = readl(pctl_base + DDR_PCTL2_PWRCTL);
352 writel(0, pctl_base + DDR_PCTL2_PWRCTL);
354 dis_auto_zq = pctl_dis_zqcs_aref(dram->pctl);
356 ret = phy_data_training(dram->phy, cs, dramtype);
358 pctl_rest_zqcs_aref(dram->pctl, dis_auto_zq);
360 /* restore auto low-power */
361 writel(pwrctl, pctl_base + DDR_PCTL2_PWRCTL);
366 static void dram_set_bw(struct dram_info *dram, u32 bw)
368 phy_dram_set_bw(dram->phy, bw);
371 static void set_ddrconfig(struct dram_info *dram, u32 ddrconfig)
373 writel(ddrconfig | (ddrconfig << 8), &dram->msch->deviceconf);
374 rk_clrsetreg(&dram->grf->soc_noc_con[1], 0x3 << 14, 0 << 14);
377 static void sdram_msch_config(struct msch_regs *msch,
378 struct sdram_msch_timings *noc_timings,
379 struct sdram_cap_info *cap_info,
380 struct sdram_base_params *base)
384 cs_cap[0] = sdram_get_cs_cap(cap_info, 0, base->dramtype);
385 cs_cap[1] = sdram_get_cs_cap(cap_info, 1, base->dramtype);
386 writel(((((cs_cap[1] >> 20) / 64) & 0xff) << 8) |
387 (((cs_cap[0] >> 20) / 64) & 0xff),
390 writel(noc_timings->ddrtiminga0.d32,
392 writel(noc_timings->ddrtimingb0.d32,
394 writel(noc_timings->ddrtimingc0.d32,
396 writel(noc_timings->devtodev0.d32,
398 writel(noc_timings->ddrmode.d32, &msch->ddrmode);
399 writel(noc_timings->ddr4timing.d32,
401 writel(noc_timings->agingx0, &msch->agingx0);
402 writel(noc_timings->agingx0, &msch->aging0);
403 writel(noc_timings->agingx0, &msch->aging1);
404 writel(noc_timings->agingx0, &msch->aging2);
405 writel(noc_timings->agingx0, &msch->aging3);
408 static void dram_all_config(struct dram_info *dram,
409 struct px30_sdram_params *sdram_params)
411 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
415 set_ddrconfig(dram, cap_info->ddrconfig);
416 sdram_org_config(cap_info, &sdram_params->base, &sys_reg2,
418 writel(sys_reg2, &dram->pmugrf->os_reg[2]);
419 writel(sys_reg3, &dram->pmugrf->os_reg[3]);
420 sdram_msch_config(dram->msch, &sdram_params->ch.noc_timings, cap_info,
421 &sdram_params->base);
424 static void enable_low_power(struct dram_info *dram,
425 struct px30_sdram_params *sdram_params)
427 void __iomem *pctl_base = dram->pctl;
428 void __iomem *phy_base = dram->phy;
429 void __iomem *ddr_grf_base = dram->ddr_grf;
433 * bit0: grf_upctl_axi_cg_en = 1 enable upctl2 axi clk auto gating
434 * bit1: grf_upctl_apb_cg_en = 1 ungated axi,core clk for apb access
435 * bit2: grf_upctl_core_cg_en = 1 enable upctl2 core clk auto gating
436 * bit3: grf_selfref_type2_en = 0 disable core clk gating when type2 sr
437 * bit4: grf_upctl_syscreq_cg_en = 1
438 * ungating coreclk when c_sysreq assert
439 * bit8-11: grf_auto_sr_dly = 6
441 writel(0x1f1f0617, &dram->ddr_grf->ddr_grf_con[1]);
443 if (sdram_params->base.dramtype == DDR4)
444 grf_lp_con = (0x7 << 16) | (1 << 1);
445 else if (sdram_params->base.dramtype == DDR3)
446 grf_lp_con = (0x7 << 16) | (1 << 0);
448 grf_lp_con = (0x7 << 16) | (1 << 2);
451 grf_lp_con = grf_lp_con | (0x1 << (9 + 16)) | (0x1 << 9);
452 writel(grf_lp_con, ddr_grf_base + DDR_GRF_LP_CON);
454 /* off digit module clock when enter power down */
455 setbits_le32(PHY_REG(phy_base, 7), 1 << 7);
459 clrbits_le32(pctl_base + DDR_PCTL2_PWRCTL, (1 << 1));
461 setbits_le32(pctl_base + DDR_PCTL2_PWRCTL, (1 << 1));
463 clrbits_le32(pctl_base + DDR_PCTL2_PWRCTL, 1);
465 setbits_le32(pctl_base + DDR_PCTL2_PWRCTL, 1);
466 setbits_le32(pctl_base + DDR_PCTL2_PWRCTL, (1 << 3));
470 * pre_init: 0: pre init for dram cap detect
471 * 1: detect correct cap(except cs1 row)info, than reinit
472 * 2: after reinit, we detect cs1_row, if cs1_row not equal
473 * to cs0_row and cs is in middle on ddrconf map, we need
474 * to reinit dram, than set the correct ddrconf.
476 static int sdram_init_(struct dram_info *dram,
477 struct px30_sdram_params *sdram_params, u32 pre_init)
479 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
480 void __iomem *pctl_base = dram->pctl;
482 rkclk_ddr_reset(dram, 1, 1, 1, 1);
485 * dereset ddr phy psrstn to config pll,
486 * if using phy pll psrstn must be dereset
489 rkclk_ddr_reset(dram, 1, 1, 1, 0);
490 rkclk_configure_ddr(dram, sdram_params);
492 /* release phy srst to provide clk to ctrl */
493 rkclk_ddr_reset(dram, 1, 1, 0, 0);
495 phy_soft_reset(dram->phy);
496 /* release ctrl presetn, and config ctl registers */
497 rkclk_ddr_reset(dram, 1, 0, 0, 0);
498 pctl_cfg(dram->pctl, &sdram_params->pctl_regs, SR_IDLE, PD_IDLE);
499 cap_info->ddrconfig = calculate_ddrconfig(sdram_params);
500 set_ctl_address_map(dram, sdram_params);
501 phy_cfg(dram->phy, &sdram_params->phy_regs, sdram_params->skew,
502 &sdram_params->base, cap_info->bw);
504 /* enable dfi_init_start to init phy after ctl srstn deassert */
505 setbits_le32(pctl_base + DDR_PCTL2_DFIMISC, (1 << 5) | (1 << 4));
507 rkclk_ddr_reset(dram, 0, 0, 0, 0);
508 /* wait for dfi_init_done and dram init complete */
509 while ((readl(pctl_base + DDR_PCTL2_STAT) & 0x7) == 0)
512 if (sdram_params->base.dramtype == LPDDR3)
513 pctl_write_mr(dram->pctl, 3, 11, 3, LPDDR3);
515 /* do ddr gate training */
517 if (data_training(dram, 0, sdram_params->base.dramtype) != 0) {
519 printascii("DTT cs0 error\n");
522 if (check_rd_gate(dram)) {
523 printascii("re training cs0");
524 goto redo_cs0_training;
527 if (sdram_params->base.dramtype == LPDDR3) {
528 if ((read_mr(dram, 1, 8) & 0x3) != 0x3)
530 } else if (sdram_params->base.dramtype == LPDDR2) {
531 if ((read_mr(dram, 1, 8) & 0x3) != 0x0)
534 /* for px30: when 2cs, both 2 cs should be training */
535 if (pre_init != 0 && cap_info->rank == 2) {
537 if (data_training(dram, 1, sdram_params->base.dramtype) != 0) {
538 printascii("DTT cs1 error\n");
541 if (check_rd_gate(dram)) {
542 printascii("re training cs1");
543 goto redo_cs1_training;
547 if (sdram_params->base.dramtype == DDR4)
548 pctl_write_vrefdq(dram->pctl, 0x3, 5670,
549 sdram_params->base.dramtype);
551 dram_all_config(dram, sdram_params);
552 enable_low_power(dram, sdram_params);
557 static int dram_detect_cap(struct dram_info *dram,
558 struct px30_sdram_params *sdram_params,
559 unsigned char channel)
561 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
564 * for ddr3: ddrconf = 3
565 * for ddr4: ddrconf = 12
566 * for lpddr3: ddrconf = 3
574 u32 dram_type = sdram_params->base.dramtype;
576 if (dram_type != DDR4) {
577 /* detect col and bk for ddr3/lpddr3 */
580 if (dram_type == LPDDR2)
585 if (sdram_detect_col(cap_info, coltmp) != 0)
587 sdram_detect_bank(cap_info, coltmp, bktmp);
588 sdram_detect_dbw(cap_info, dram_type);
590 /* detect bg for ddr4 */
599 sdram_detect_bg(cap_info, coltmp);
603 if (sdram_detect_row(cap_info, coltmp, bktmp, rowtmp) != 0)
607 sdram_detect_row_3_4(cap_info, coltmp, bktmp);
609 /* bw and cs detect using data training */
610 if (data_training(dram, 1, dram_type) == 0)
614 cap_info->rank = cs + 1;
616 dram_set_bw(dram, 2);
617 if (data_training(dram, 0, dram_type) == 0)
623 cap_info->cs0_high16bit_row = cap_info->cs0_row;
625 cap_info->cs1_row = cap_info->cs0_row;
626 cap_info->cs1_high16bit_row = cap_info->cs0_row;
628 cap_info->cs1_row = 0;
629 cap_info->cs1_high16bit_row = 0;
637 /* return: 0 = success, other = fail */
638 static int sdram_init_detect(struct dram_info *dram,
639 struct px30_sdram_params *sdram_params)
641 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
646 if (sdram_init_(dram, sdram_params, 0) != 0)
649 if (dram_detect_cap(dram, sdram_params, 0) != 0)
652 /* modify bw, cs related timing */
653 pctl_remodify_sdram_params(&sdram_params->pctl_regs, cap_info,
654 sdram_params->base.dramtype);
655 /* reinit sdram by real dram cap */
656 ret = sdram_init_(dram, sdram_params, 1);
660 /* redetect cs1 row */
661 sdram_detect_cs1_row(cap_info, sdram_params->base.dramtype);
662 if (cap_info->cs1_row) {
663 sys_reg = readl(&dram->pmugrf->os_reg[2]);
664 sys_reg3 = readl(&dram->pmugrf->os_reg[3]);
665 SYS_REG_ENC_CS1_ROW(cap_info->cs1_row,
666 sys_reg, sys_reg3, 0);
667 writel(sys_reg, &dram->pmugrf->os_reg[2]);
668 writel(sys_reg3, &dram->pmugrf->os_reg[3]);
671 ret = sdram_detect_high_row(cap_info);
677 struct px30_sdram_params
678 *get_default_sdram_config(void)
680 sdram_configs[0].skew = &skew;
682 return &sdram_configs[0];
685 /* return: 0 = success, other = fail */
688 struct px30_sdram_params *sdram_params;
691 dram_info.phy = (void *)DDR_PHY_BASE_ADDR;
692 dram_info.pctl = (void *)DDRC_BASE_ADDR;
693 dram_info.grf = (void *)GRF_BASE_ADDR;
694 dram_info.cru = (void *)CRU_BASE_ADDR;
695 dram_info.msch = (void *)SERVER_MSCH0_BASE_ADDR;
696 dram_info.ddr_grf = (void *)DDR_GRF_BASE_ADDR;
697 dram_info.pmugrf = (void *)PMUGRF_BASE_ADDR;
699 sdram_params = get_default_sdram_config();
700 ret = sdram_init_detect(&dram_info, sdram_params);
705 sdram_print_ddr_info(&sdram_params->ch.cap_info, &sdram_params->base);
714 static int px30_dmc_probe(struct udevice *dev)
716 struct dram_info *priv = dev_get_priv(dev);
718 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
719 debug("%s: grf=%p\n", __func__, priv->pmugrf);
720 priv->info.base = CONFIG_SYS_SDRAM_BASE;
722 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg[2]);
727 static int px30_dmc_get_info(struct udevice *dev, struct ram_info *info)
729 struct dram_info *priv = dev_get_priv(dev);
736 static struct ram_ops px30_dmc_ops = {
737 .get_info = px30_dmc_get_info,
740 static const struct udevice_id px30_dmc_ids[] = {
741 { .compatible = "rockchip,px30-dmc" },
745 U_BOOT_DRIVER(dmc_px30) = {
746 .name = "rockchip_px30_dmc",
748 .of_match = px30_dmc_ids,
749 .ops = &px30_dmc_ops,
750 .probe = px30_dmc_probe,
751 .priv_auto_alloc_size = sizeof(struct dram_info),
753 #endif /* CONFIG_TPL_BUILD */