1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * (C) Copyright 2016-2017 Rockchip Inc.
5 * Adapted from coreboot.
11 #include <dt-structs.h>
16 #include <asm/arch-rockchip/clock.h>
17 #include <asm/arch-rockchip/cru_rk3399.h>
18 #include <asm/arch-rockchip/grf_rk3399.h>
19 #include <asm/arch-rockchip/hardware.h>
20 #include <asm/arch-rockchip/sdram_common.h>
21 #include <asm/arch-rockchip/sdram_rk3399.h>
22 #include <linux/err.h>
25 #define PRESET_SGRF_HOLD(n) ((0x1 << (6 + 16)) | ((n) << 6))
26 #define PRESET_GPIO0_HOLD(n) ((0x1 << (7 + 16)) | ((n) << 7))
27 #define PRESET_GPIO1_HOLD(n) ((0x1 << (8 + 16)) | ((n) << 8))
29 #define PHY_DRV_ODT_HI_Z 0x0
30 #define PHY_DRV_ODT_240 0x1
31 #define PHY_DRV_ODT_120 0x8
32 #define PHY_DRV_ODT_80 0x9
33 #define PHY_DRV_ODT_60 0xc
34 #define PHY_DRV_ODT_48 0xd
35 #define PHY_DRV_ODT_40 0xe
36 #define PHY_DRV_ODT_34_3 0xf
38 #define PHY_BOOSTP_EN 0x1
39 #define PHY_BOOSTN_EN 0x1
40 #define PHY_SLEWP_EN 0x1
41 #define PHY_SLEWN_EN 0x1
42 #define PHY_RX_CM_INPUT 0x1
43 #define CS0_MR22_VAL 0
44 #define CS1_MR22_VAL 3
46 #define CRU_SFTRST_DDR_CTRL(ch, n) ((0x1 << (8 + 16 + (ch) * 4)) | \
47 ((n) << (8 + (ch) * 4)))
48 #define CRU_SFTRST_DDR_PHY(ch, n) ((0x1 << (9 + 16 + (ch) * 4)) | \
49 ((n) << (9 + (ch) * 4)))
51 struct rk3399_ddr_pctl_regs *pctl;
52 struct rk3399_ddr_pi_regs *pi;
53 struct rk3399_ddr_publ_regs *publ;
54 struct rk3399_msch_regs *msch;
58 #if defined(CONFIG_TPL_BUILD) || \
59 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
60 u32 pwrup_srefresh_exit[2];
61 struct chan_info chan[2];
63 struct rk3399_cru *cru;
64 struct rk3399_grf_regs *grf;
65 struct rk3399_pmucru *pmucru;
66 struct rk3399_pmusgrf_regs *pmusgrf;
67 struct rk3399_ddr_cic_regs *cic;
70 struct rk3399_pmugrf_regs *pmugrf;
73 #if defined(CONFIG_TPL_BUILD) || \
74 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
76 struct rockchip_dmc_plat {
77 #if CONFIG_IS_ENABLED(OF_PLATDATA)
78 struct dtd_rockchip_rk3399_dmc dtplat;
80 struct rk3399_sdram_params sdram_params;
101 } lpddr4_io_setting[] = {
112 PHY_DRV_ODT_HI_Z, /* rd_odt; */
113 PHY_DRV_ODT_40, /* wr_dq_drv; */
114 PHY_DRV_ODT_40, /* wr_ca_drv; */
115 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
117 41, /* rd_vref; (unit %, range 3.3% - 48.7%) */
129 PHY_DRV_ODT_HI_Z, /* rd_odt; */
130 PHY_DRV_ODT_48, /* wr_dq_drv; */
131 PHY_DRV_ODT_40, /* wr_ca_drv; */
132 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
134 32, /* rd_vref; (unit %, range 3.3% - 48.7%) */
146 PHY_DRV_ODT_40, /* rd_odt; */
147 PHY_DRV_ODT_48, /* wr_dq_drv; */
148 PHY_DRV_ODT_40, /* wr_ca_drv; */
149 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
151 17, /* rd_vref; (unit %, range 3.3% - 48.7%) */
160 0x59, /* dq_vref; 32% */
163 PHY_DRV_ODT_HI_Z, /* rd_odt; */
164 PHY_DRV_ODT_48, /* wr_dq_drv; */
165 PHY_DRV_ODT_40, /* wr_ca_drv; */
166 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
168 32, /* rd_vref; (unit %, range 3.3% - 48.7%) */
180 PHY_DRV_ODT_40, /* rd_odt; */
181 PHY_DRV_ODT_60, /* wr_dq_drv; */
182 PHY_DRV_ODT_40, /* wr_ca_drv; */
183 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
185 17, /* rd_vref; (unit %, range 3.3% - 48.7%) */
190 * phy = 0, PHY boot freq
191 * phy = 1, PHY index 0
192 * phy = 2, PHY index 1
194 static struct io_setting *
195 lpddr4_get_io_settings(const struct rk3399_sdram_params *params, u32 mr5)
197 struct io_setting *io = NULL;
200 for (n = 0; n < ARRAY_SIZE(lpddr4_io_setting); n++) {
201 io = &lpddr4_io_setting[n];
204 if (io->mhz >= params->base.ddr_freq &&
208 if (io->mhz >= params->base.ddr_freq)
216 static void *get_ddrc0_con(struct dram_info *dram, u8 channel)
218 return (channel == 0) ? &dram->grf->ddrc0_con0 : &dram->grf->ddrc0_con1;
221 static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
225 for (i = 0; i < n / sizeof(u32); i++) {
232 static void rkclk_ddr_reset(struct rk3399_cru *cru, u32 channel, u32 ctl,
238 writel(CRU_SFTRST_DDR_CTRL(channel, ctl) |
239 CRU_SFTRST_DDR_PHY(channel, phy),
240 &cru->softrst_con[4]);
243 static void phy_pctrl_reset(struct rk3399_cru *cru, u32 channel)
245 rkclk_ddr_reset(cru, channel, 1, 1);
248 rkclk_ddr_reset(cru, channel, 1, 0);
251 rkclk_ddr_reset(cru, channel, 0, 0);
255 static void phy_dll_bypass_set(struct rk3399_ddr_publ_regs *ddr_publ_regs,
258 u32 *denali_phy = ddr_publ_regs->denali_phy;
260 /* From IP spec, only freq small than 125 can enter dll bypass mode */
262 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
263 setbits_le32(&denali_phy[86], (0x3 << 2) << 8);
264 setbits_le32(&denali_phy[214], (0x3 << 2) << 8);
265 setbits_le32(&denali_phy[342], (0x3 << 2) << 8);
266 setbits_le32(&denali_phy[470], (0x3 << 2) << 8);
268 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
269 setbits_le32(&denali_phy[547], (0x3 << 2) << 16);
270 setbits_le32(&denali_phy[675], (0x3 << 2) << 16);
271 setbits_le32(&denali_phy[803], (0x3 << 2) << 16);
273 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
274 clrbits_le32(&denali_phy[86], (0x3 << 2) << 8);
275 clrbits_le32(&denali_phy[214], (0x3 << 2) << 8);
276 clrbits_le32(&denali_phy[342], (0x3 << 2) << 8);
277 clrbits_le32(&denali_phy[470], (0x3 << 2) << 8);
279 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
280 clrbits_le32(&denali_phy[547], (0x3 << 2) << 16);
281 clrbits_le32(&denali_phy[675], (0x3 << 2) << 16);
282 clrbits_le32(&denali_phy[803], (0x3 << 2) << 16);
286 static void set_memory_map(const struct chan_info *chan, u32 channel,
287 const struct rk3399_sdram_params *params)
289 const struct rk3399_sdram_channel *sdram_ch = ¶ms->ch[channel];
290 u32 *denali_ctl = chan->pctl->denali_ctl;
291 u32 *denali_pi = chan->pi->denali_pi;
296 /* Get row number from ddrconfig setting */
297 if (sdram_ch->cap_info.ddrconfig < 2 ||
298 sdram_ch->cap_info.ddrconfig == 4)
300 else if (sdram_ch->cap_info.ddrconfig == 3)
305 cs_map = (sdram_ch->cap_info.rank > 1) ? 3 : 1;
306 reduc = (sdram_ch->cap_info.bw == 2) ? 0 : 1;
308 /* Set the dram configuration to ctrl */
309 clrsetbits_le32(&denali_ctl[191], 0xF, (12 - sdram_ch->cap_info.col));
310 clrsetbits_le32(&denali_ctl[190], (0x3 << 16) | (0x7 << 24),
311 ((3 - sdram_ch->cap_info.bk) << 16) |
314 clrsetbits_le32(&denali_ctl[196], 0x3 | (1 << 16),
315 cs_map | (reduc << 16));
317 /* PI_199 PI_COL_DIFF:RW:0:4 */
318 clrsetbits_le32(&denali_pi[199], 0xF, (12 - sdram_ch->cap_info.col));
320 /* PI_155 PI_ROW_DIFF:RW:24:3 PI_BANK_DIFF:RW:16:2 */
321 clrsetbits_le32(&denali_pi[155], (0x3 << 16) | (0x7 << 24),
322 ((3 - sdram_ch->cap_info.bk) << 16) |
325 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
328 else if (cs_map == 2)
334 /* PI_41 PI_CS_MAP:RW:24:4 */
335 clrsetbits_le32(&denali_pi[41], 0xf << 24, cs_map << 24);
336 if (sdram_ch->cap_info.rank == 1 && params->base.dramtype == DDR3)
337 writel(0x2EC7FFFF, &denali_pi[34]);
340 static int phy_io_config(const struct chan_info *chan,
341 const struct rk3399_sdram_params *params, u32 mr5)
343 u32 *denali_phy = chan->publ->denali_phy;
344 u32 vref_mode_dq, vref_value_dq, vref_mode_ac, vref_value_ac;
347 u32 drv_value, odt_value;
351 if (params->base.dramtype == LPDDR4) {
352 struct io_setting *io = lpddr4_get_io_settings(params, mr5);
353 u32 rd_vref = io->rd_vref * 1000;
355 if (rd_vref < 36700) {
356 /* MODE_LV[2:0] = LPDDR4 (Range 2)*/
358 vref_value_dq = (rd_vref - 3300) / 521;
360 /* MODE_LV[2:0] = LPDDR4 (Range 1)*/
362 vref_value_dq = (rd_vref - 15300) / 521;
365 vref_value_ac = 0x1f;
367 } else if (params->base.dramtype == LPDDR3) {
368 if (params->base.odt == 1) {
369 vref_mode_dq = 0x5; /* LPDDR3 ODT */
370 drv_value = (readl(&denali_phy[6]) >> 12) & 0xf;
371 odt_value = (readl(&denali_phy[6]) >> 4) & 0xf;
372 if (drv_value == PHY_DRV_ODT_48) {
374 case PHY_DRV_ODT_240:
375 vref_value_dq = 0x16;
377 case PHY_DRV_ODT_120:
378 vref_value_dq = 0x26;
381 vref_value_dq = 0x36;
384 debug("Invalid ODT value.\n");
387 } else if (drv_value == PHY_DRV_ODT_40) {
389 case PHY_DRV_ODT_240:
390 vref_value_dq = 0x19;
392 case PHY_DRV_ODT_120:
393 vref_value_dq = 0x23;
396 vref_value_dq = 0x31;
399 debug("Invalid ODT value.\n");
402 } else if (drv_value == PHY_DRV_ODT_34_3) {
404 case PHY_DRV_ODT_240:
405 vref_value_dq = 0x17;
407 case PHY_DRV_ODT_120:
408 vref_value_dq = 0x20;
411 vref_value_dq = 0x2e;
414 debug("Invalid ODT value.\n");
418 debug("Invalid DRV value.\n");
422 vref_mode_dq = 0x2; /* LPDDR3 */
423 vref_value_dq = 0x1f;
426 vref_value_ac = 0x1f;
428 } else if (params->base.dramtype == DDR3) {
431 vref_value_dq = 0x1f;
433 vref_value_ac = 0x1f;
436 debug("Unknown DRAM type.\n");
440 reg_value = (vref_mode_dq << 9) | (0x1 << 8) | vref_value_dq;
442 /* PHY_913 PHY_PAD_VREF_CTRL_DQ_0 12bits offset_8 */
443 clrsetbits_le32(&denali_phy[913], 0xfff << 8, reg_value << 8);
444 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_1 12bits offset_0 */
445 clrsetbits_le32(&denali_phy[914], 0xfff, reg_value);
446 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_2 12bits offset_16 */
447 clrsetbits_le32(&denali_phy[914], 0xfff << 16, reg_value << 16);
448 /* PHY_915 PHY_PAD_VREF_CTRL_DQ_3 12bits offset_0 */
449 clrsetbits_le32(&denali_phy[915], 0xfff, reg_value);
451 reg_value = (vref_mode_ac << 9) | (0x1 << 8) | vref_value_ac;
453 /* PHY_915 PHY_PAD_VREF_CTRL_AC 12bits offset_16 */
454 clrsetbits_le32(&denali_phy[915], 0xfff << 16, reg_value << 16);
456 /* PHY_924 PHY_PAD_FDBK_DRIVE */
457 clrsetbits_le32(&denali_phy[924], 0x7 << 15, mode_sel << 15);
458 /* PHY_926 PHY_PAD_DATA_DRIVE */
459 clrsetbits_le32(&denali_phy[926], 0x7 << 6, mode_sel << 6);
460 /* PHY_927 PHY_PAD_DQS_DRIVE */
461 clrsetbits_le32(&denali_phy[927], 0x7 << 6, mode_sel << 6);
462 /* PHY_928 PHY_PAD_ADDR_DRIVE */
463 clrsetbits_le32(&denali_phy[928], 0x7 << 14, mode_sel << 14);
464 /* PHY_929 PHY_PAD_CLK_DRIVE */
465 clrsetbits_le32(&denali_phy[929], 0x7 << 14, mode_sel << 14);
466 /* PHY_935 PHY_PAD_CKE_DRIVE */
467 clrsetbits_le32(&denali_phy[935], 0x7 << 14, mode_sel << 14);
468 /* PHY_937 PHY_PAD_RST_DRIVE */
469 clrsetbits_le32(&denali_phy[937], 0x7 << 14, mode_sel << 14);
470 /* PHY_939 PHY_PAD_CS_DRIVE */
471 clrsetbits_le32(&denali_phy[939], 0x7 << 14, mode_sel << 14);
473 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
474 /* BOOSTP_EN & BOOSTN_EN */
475 reg_value = ((PHY_BOOSTP_EN << 4) | PHY_BOOSTN_EN);
476 /* PHY_925 PHY_PAD_FDBK_DRIVE2 */
477 clrsetbits_le32(&denali_phy[925], 0xff << 8, reg_value << 8);
478 /* PHY_926 PHY_PAD_DATA_DRIVE */
479 clrsetbits_le32(&denali_phy[926], 0xff << 12, reg_value << 12);
480 /* PHY_927 PHY_PAD_DQS_DRIVE */
481 clrsetbits_le32(&denali_phy[927], 0xff << 14, reg_value << 14);
482 /* PHY_928 PHY_PAD_ADDR_DRIVE */
483 clrsetbits_le32(&denali_phy[928], 0xff << 20, reg_value << 20);
484 /* PHY_929 PHY_PAD_CLK_DRIVE */
485 clrsetbits_le32(&denali_phy[929], 0xff << 22, reg_value << 22);
486 /* PHY_935 PHY_PAD_CKE_DRIVE */
487 clrsetbits_le32(&denali_phy[935], 0xff << 20, reg_value << 20);
488 /* PHY_937 PHY_PAD_RST_DRIVE */
489 clrsetbits_le32(&denali_phy[937], 0xff << 20, reg_value << 20);
490 /* PHY_939 PHY_PAD_CS_DRIVE */
491 clrsetbits_le32(&denali_phy[939], 0xff << 20, reg_value << 20);
493 /* SLEWP_EN & SLEWN_EN */
494 reg_value = ((PHY_SLEWP_EN << 3) | PHY_SLEWN_EN);
495 /* PHY_924 PHY_PAD_FDBK_DRIVE */
496 clrsetbits_le32(&denali_phy[924], 0x3f << 8, reg_value << 8);
497 /* PHY_926 PHY_PAD_DATA_DRIVE */
498 clrsetbits_le32(&denali_phy[926], 0x3f, reg_value);
499 /* PHY_927 PHY_PAD_DQS_DRIVE */
500 clrsetbits_le32(&denali_phy[927], 0x3f, reg_value);
501 /* PHY_928 PHY_PAD_ADDR_DRIVE */
502 clrsetbits_le32(&denali_phy[928], 0x3f << 8, reg_value << 8);
503 /* PHY_929 PHY_PAD_CLK_DRIVE */
504 clrsetbits_le32(&denali_phy[929], 0x3f << 8, reg_value << 8);
505 /* PHY_935 PHY_PAD_CKE_DRIVE */
506 clrsetbits_le32(&denali_phy[935], 0x3f << 8, reg_value << 8);
507 /* PHY_937 PHY_PAD_RST_DRIVE */
508 clrsetbits_le32(&denali_phy[937], 0x3f << 8, reg_value << 8);
509 /* PHY_939 PHY_PAD_CS_DRIVE */
510 clrsetbits_le32(&denali_phy[939], 0x3f << 8, reg_value << 8);
514 if (params->base.ddr_freq < 400)
516 else if (params->base.ddr_freq < 800)
518 else if (params->base.ddr_freq < 1200)
523 /* PHY_924 PHY_PAD_FDBK_DRIVE */
524 clrsetbits_le32(&denali_phy[924], 0x3 << 21, speed << 21);
525 /* PHY_926 PHY_PAD_DATA_DRIVE */
526 clrsetbits_le32(&denali_phy[926], 0x3 << 9, speed << 9);
527 /* PHY_927 PHY_PAD_DQS_DRIVE */
528 clrsetbits_le32(&denali_phy[927], 0x3 << 9, speed << 9);
529 /* PHY_928 PHY_PAD_ADDR_DRIVE */
530 clrsetbits_le32(&denali_phy[928], 0x3 << 17, speed << 17);
531 /* PHY_929 PHY_PAD_CLK_DRIVE */
532 clrsetbits_le32(&denali_phy[929], 0x3 << 17, speed << 17);
533 /* PHY_935 PHY_PAD_CKE_DRIVE */
534 clrsetbits_le32(&denali_phy[935], 0x3 << 17, speed << 17);
535 /* PHY_937 PHY_PAD_RST_DRIVE */
536 clrsetbits_le32(&denali_phy[937], 0x3 << 17, speed << 17);
537 /* PHY_939 PHY_PAD_CS_DRIVE */
538 clrsetbits_le32(&denali_phy[939], 0x3 << 17, speed << 17);
540 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
542 reg_value = PHY_RX_CM_INPUT;
543 /* PHY_924 PHY_PAD_FDBK_DRIVE */
544 clrsetbits_le32(&denali_phy[924], 0x1 << 14, reg_value << 14);
545 /* PHY_926 PHY_PAD_DATA_DRIVE */
546 clrsetbits_le32(&denali_phy[926], 0x1 << 11, reg_value << 11);
547 /* PHY_927 PHY_PAD_DQS_DRIVE */
548 clrsetbits_le32(&denali_phy[927], 0x1 << 13, reg_value << 13);
549 /* PHY_928 PHY_PAD_ADDR_DRIVE */
550 clrsetbits_le32(&denali_phy[928], 0x1 << 19, reg_value << 19);
551 /* PHY_929 PHY_PAD_CLK_DRIVE */
552 clrsetbits_le32(&denali_phy[929], 0x1 << 21, reg_value << 21);
553 /* PHY_935 PHY_PAD_CKE_DRIVE */
554 clrsetbits_le32(&denali_phy[935], 0x1 << 19, reg_value << 19);
555 /* PHY_937 PHY_PAD_RST_DRIVE */
556 clrsetbits_le32(&denali_phy[937], 0x1 << 19, reg_value << 19);
557 /* PHY_939 PHY_PAD_CS_DRIVE */
558 clrsetbits_le32(&denali_phy[939], 0x1 << 19, reg_value << 19);
564 static void set_ds_odt(const struct chan_info *chan,
565 const struct rk3399_sdram_params *params, u32 mr5)
567 u32 *denali_phy = chan->publ->denali_phy;
568 u32 *denali_ctl = chan->pctl->denali_ctl;
569 u32 tsel_idle_en, tsel_wr_en, tsel_rd_en;
570 u32 tsel_idle_select_p, tsel_rd_select_p;
571 u32 tsel_idle_select_n, tsel_rd_select_n;
572 u32 tsel_wr_select_dq_p, tsel_wr_select_ca_p;
573 u32 tsel_wr_select_dq_n, tsel_wr_select_ca_n;
574 u32 tsel_ckcs_select_p, tsel_ckcs_select_n;
575 struct io_setting *io = NULL;
579 if (params->base.dramtype == LPDDR4) {
580 io = lpddr4_get_io_settings(params, mr5);
582 tsel_rd_select_p = PHY_DRV_ODT_HI_Z;
583 tsel_rd_select_n = io->rd_odt;
585 tsel_idle_select_p = PHY_DRV_ODT_HI_Z;
586 tsel_idle_select_n = PHY_DRV_ODT_240;
588 tsel_wr_select_dq_p = io->wr_dq_drv;
589 tsel_wr_select_dq_n = PHY_DRV_ODT_40;
591 tsel_wr_select_ca_p = io->wr_ca_drv;
592 tsel_wr_select_ca_n = PHY_DRV_ODT_40;
594 tsel_ckcs_select_p = io->wr_ckcs_drv;
595 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
596 switch (tsel_rd_select_n) {
597 case PHY_DRV_ODT_240:
600 case PHY_DRV_ODT_120:
615 case PHY_DRV_ODT_34_3:
617 printf("%s: Unable to support LPDDR4 MR22 Soc ODT\n",
620 case PHY_DRV_ODT_HI_Z:
625 } else if (params->base.dramtype == LPDDR3) {
626 tsel_rd_select_p = PHY_DRV_ODT_240;
627 tsel_rd_select_n = PHY_DRV_ODT_HI_Z;
629 tsel_idle_select_p = PHY_DRV_ODT_240;
630 tsel_idle_select_n = PHY_DRV_ODT_HI_Z;
632 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
633 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
635 tsel_wr_select_ca_p = PHY_DRV_ODT_48;
636 tsel_wr_select_ca_n = PHY_DRV_ODT_48;
638 tsel_ckcs_select_p = PHY_DRV_ODT_34_3;
639 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
641 tsel_rd_select_p = PHY_DRV_ODT_240;
642 tsel_rd_select_n = PHY_DRV_ODT_240;
644 tsel_idle_select_p = PHY_DRV_ODT_240;
645 tsel_idle_select_n = PHY_DRV_ODT_240;
647 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
648 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
650 tsel_wr_select_ca_p = PHY_DRV_ODT_34_3;
651 tsel_wr_select_ca_n = PHY_DRV_ODT_34_3;
653 tsel_ckcs_select_p = PHY_DRV_ODT_34_3;
654 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
657 if (params->base.odt == 1) {
660 if (params->base.dramtype == LPDDR4)
661 tsel_rd_en = io->rd_odt_en;
670 clrsetbits_le32(&denali_ctl[145], 0xFF << 16,
671 (soc_odt | (CS0_MR22_VAL << 3)) << 16);
673 clrsetbits_le32(&denali_ctl[146], 0xFF00FF,
674 ((soc_odt | (CS0_MR22_VAL << 3)) << 16) |
675 (soc_odt | (CS0_MR22_VAL << 3)));
677 clrsetbits_le32(&denali_ctl[159], 0xFF << 16,
678 (soc_odt | (CS1_MR22_VAL << 3)) << 16);
680 clrsetbits_le32(&denali_ctl[160], 0xFF00FF,
681 ((soc_odt | (CS1_MR22_VAL << 3)) << 16) |
682 (soc_odt | (CS1_MR22_VAL << 3)));
685 * phy_dq_tsel_select_X 24bits DENALI_PHY_6/134/262/390 offset_0
686 * sets termination values for read/idle cycles and drive strength
687 * for write cycles for DQ/DM
689 reg_value = tsel_rd_select_n | (tsel_rd_select_p << 0x4) |
690 (tsel_wr_select_dq_n << 8) | (tsel_wr_select_dq_p << 12) |
691 (tsel_idle_select_n << 16) | (tsel_idle_select_p << 20);
692 clrsetbits_le32(&denali_phy[6], 0xffffff, reg_value);
693 clrsetbits_le32(&denali_phy[134], 0xffffff, reg_value);
694 clrsetbits_le32(&denali_phy[262], 0xffffff, reg_value);
695 clrsetbits_le32(&denali_phy[390], 0xffffff, reg_value);
698 * phy_dqs_tsel_select_X 24bits DENALI_PHY_7/135/263/391 offset_0
699 * sets termination values for read/idle cycles and drive strength
700 * for write cycles for DQS
702 clrsetbits_le32(&denali_phy[7], 0xffffff, reg_value);
703 clrsetbits_le32(&denali_phy[135], 0xffffff, reg_value);
704 clrsetbits_le32(&denali_phy[263], 0xffffff, reg_value);
705 clrsetbits_le32(&denali_phy[391], 0xffffff, reg_value);
707 /* phy_adr_tsel_select_ 8bits DENALI_PHY_544/672/800 offset_0 */
708 reg_value = tsel_wr_select_ca_n | (tsel_wr_select_ca_p << 0x4);
709 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
710 /* LPDDR4 these register read always return 0, so
711 * can not use clrsetbits_le32(), need to write32
713 writel((0x300 << 8) | reg_value, &denali_phy[544]);
714 writel((0x300 << 8) | reg_value, &denali_phy[672]);
715 writel((0x300 << 8) | reg_value, &denali_phy[800]);
717 clrsetbits_le32(&denali_phy[544], 0xff, reg_value);
718 clrsetbits_le32(&denali_phy[672], 0xff, reg_value);
719 clrsetbits_le32(&denali_phy[800], 0xff, reg_value);
722 /* phy_pad_addr_drive 8bits DENALI_PHY_928 offset_0 */
723 clrsetbits_le32(&denali_phy[928], 0xff, reg_value);
725 /* phy_pad_rst_drive 8bits DENALI_PHY_937 offset_0 */
726 clrsetbits_le32(&denali_phy[937], 0xff, reg_value);
728 /* phy_pad_cke_drive 8bits DENALI_PHY_935 offset_0 */
729 clrsetbits_le32(&denali_phy[935], 0xff, reg_value);
731 /* phy_pad_cs_drive 8bits DENALI_PHY_939 offset_0 */
732 clrsetbits_le32(&denali_phy[939], 0xff,
733 tsel_ckcs_select_n | (tsel_ckcs_select_p << 0x4));
735 /* phy_pad_clk_drive 8bits DENALI_PHY_929 offset_0 */
736 clrsetbits_le32(&denali_phy[929], 0xff,
737 tsel_ckcs_select_n | (tsel_ckcs_select_p << 0x4));
739 /* phy_pad_fdbk_drive 23bit DENALI_PHY_924/925 */
740 clrsetbits_le32(&denali_phy[924], 0xff,
741 tsel_wr_select_dq_n | (tsel_wr_select_dq_p << 4));
742 clrsetbits_le32(&denali_phy[925], 0xff,
743 tsel_rd_select_n | (tsel_rd_select_p << 4));
745 /* phy_dq_tsel_enable_X 3bits DENALI_PHY_5/133/261/389 offset_16 */
746 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
748 clrsetbits_le32(&denali_phy[5], 0x7 << 16, reg_value);
749 clrsetbits_le32(&denali_phy[133], 0x7 << 16, reg_value);
750 clrsetbits_le32(&denali_phy[261], 0x7 << 16, reg_value);
751 clrsetbits_le32(&denali_phy[389], 0x7 << 16, reg_value);
753 /* phy_dqs_tsel_enable_X 3bits DENALI_PHY_6/134/262/390 offset_24 */
754 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
756 clrsetbits_le32(&denali_phy[6], 0x7 << 24, reg_value);
757 clrsetbits_le32(&denali_phy[134], 0x7 << 24, reg_value);
758 clrsetbits_le32(&denali_phy[262], 0x7 << 24, reg_value);
759 clrsetbits_le32(&denali_phy[390], 0x7 << 24, reg_value);
761 /* phy_adr_tsel_enable_ 1bit DENALI_PHY_518/646/774 offset_8 */
762 reg_value = tsel_wr_en << 8;
763 clrsetbits_le32(&denali_phy[518], 0x1 << 8, reg_value);
764 clrsetbits_le32(&denali_phy[646], 0x1 << 8, reg_value);
765 clrsetbits_le32(&denali_phy[774], 0x1 << 8, reg_value);
767 /* phy_pad_addr_term tsel 1bit DENALI_PHY_933 offset_17 */
768 reg_value = tsel_wr_en << 17;
769 clrsetbits_le32(&denali_phy[933], 0x1 << 17, reg_value);
771 * pad_rst/cke/cs/clk_term tsel 1bits
772 * DENALI_PHY_938/936/940/934 offset_17
774 clrsetbits_le32(&denali_phy[938], 0x1 << 17, reg_value);
775 clrsetbits_le32(&denali_phy[936], 0x1 << 17, reg_value);
776 clrsetbits_le32(&denali_phy[940], 0x1 << 17, reg_value);
777 clrsetbits_le32(&denali_phy[934], 0x1 << 17, reg_value);
779 /* phy_pad_fdbk_term 1bit DENALI_PHY_930 offset_17 */
780 clrsetbits_le32(&denali_phy[930], 0x1 << 17, reg_value);
782 phy_io_config(chan, params, mr5);
785 static void pctl_start(struct dram_info *dram, u8 channel)
787 const struct chan_info *chan = &dram->chan[channel];
788 u32 *denali_ctl = chan->pctl->denali_ctl;
789 u32 *denali_phy = chan->publ->denali_phy;
790 u32 *ddrc0_con = get_ddrc0_con(dram, channel);
794 writel(0x01000000, &ddrc0_con);
796 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 0x2 << 24);
798 while (!(readl(&denali_ctl[203]) & (1 << 3))) {
800 printf("%s: Failed to init pctl for channel %d\n",
810 writel(0x01000100, &ddrc0_con);
812 for (byte = 0; byte < 4; byte++) {
814 writel((tmp << 16) | tmp, &denali_phy[53 + (128 * byte)]);
815 writel((tmp << 16) | tmp, &denali_phy[54 + (128 * byte)]);
816 writel((tmp << 16) | tmp, &denali_phy[55 + (128 * byte)]);
817 writel((tmp << 16) | tmp, &denali_phy[56 + (128 * byte)]);
818 writel((tmp << 16) | tmp, &denali_phy[57 + (128 * byte)]);
820 clrsetbits_le32(&denali_phy[58 + (128 * byte)], 0xffff, tmp);
823 clrsetbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT,
824 dram->pwrup_srefresh_exit[channel]);
827 static int pctl_cfg(struct dram_info *dram, const struct chan_info *chan,
828 u32 channel, struct rk3399_sdram_params *params)
830 u32 *denali_ctl = chan->pctl->denali_ctl;
831 u32 *denali_pi = chan->pi->denali_pi;
832 u32 *denali_phy = chan->publ->denali_phy;
833 const u32 *params_ctl = params->pctl_regs.denali_ctl;
834 const u32 *params_phy = params->phy_regs.denali_phy;
838 * work around controller bug:
839 * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed
841 copy_to_reg(&denali_ctl[1], ¶ms_ctl[1],
842 sizeof(struct rk3399_ddr_pctl_regs) - 4);
843 writel(params_ctl[0], &denali_ctl[0]);
846 * two channel init at the same time, then ZQ Cal Start
847 * at the same time, it will use the same RZQ, but cannot
848 * start at the same time.
850 * So, increase tINIT3 for channel 1, will avoid two
851 * channel ZQ Cal Start at the same time
853 if (params->base.dramtype == LPDDR4 && channel == 1) {
854 tmp = ((params->base.ddr_freq * MHz + 999) / 1000);
855 tmp1 = readl(&denali_ctl[14]);
856 writel(tmp + tmp1, &denali_ctl[14]);
859 copy_to_reg(denali_pi, ¶ms->pi_regs.denali_pi[0],
860 sizeof(struct rk3399_ddr_pi_regs));
862 /* rank count need to set for init */
863 set_memory_map(chan, channel, params);
865 writel(params->phy_regs.denali_phy[910], &denali_phy[910]);
866 writel(params->phy_regs.denali_phy[911], &denali_phy[911]);
867 writel(params->phy_regs.denali_phy[912], &denali_phy[912]);
869 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
870 writel(params->phy_regs.denali_phy[898], &denali_phy[898]);
871 writel(params->phy_regs.denali_phy[919], &denali_phy[919]);
874 dram->pwrup_srefresh_exit[channel] = readl(&denali_ctl[68]) &
876 clrbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT);
879 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 1 << 24);
881 setbits_le32(&denali_pi[0], START);
882 setbits_le32(&denali_ctl[0], START);
885 * LPDDR4 use PLL bypass mode for init
886 * not need to wait for the PLL to lock
888 if (params->base.dramtype != LPDDR4) {
889 /* Waiting for phy DLL lock */
891 tmp = readl(&denali_phy[920]);
892 tmp1 = readl(&denali_phy[921]);
893 tmp2 = readl(&denali_phy[922]);
894 if ((((tmp >> 16) & 0x1) == 0x1) &&
895 (((tmp1 >> 16) & 0x1) == 0x1) &&
896 (((tmp1 >> 0) & 0x1) == 0x1) &&
897 (((tmp2 >> 0) & 0x1) == 0x1))
902 copy_to_reg(&denali_phy[896], ¶ms_phy[896], (958 - 895) * 4);
903 copy_to_reg(&denali_phy[0], ¶ms_phy[0], (90 - 0 + 1) * 4);
904 copy_to_reg(&denali_phy[128], ¶ms_phy[128], (218 - 128 + 1) * 4);
905 copy_to_reg(&denali_phy[256], ¶ms_phy[256], (346 - 256 + 1) * 4);
906 copy_to_reg(&denali_phy[384], ¶ms_phy[384], (474 - 384 + 1) * 4);
907 copy_to_reg(&denali_phy[512], ¶ms_phy[512], (549 - 512 + 1) * 4);
908 copy_to_reg(&denali_phy[640], ¶ms_phy[640], (677 - 640 + 1) * 4);
909 copy_to_reg(&denali_phy[768], ¶ms_phy[768], (805 - 768 + 1) * 4);
910 set_ds_odt(chan, params, 0);
913 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_84/212/340/468 offset_8
914 * dqs_tsel_wr_end[7:4] add Half cycle
916 tmp = (readl(&denali_phy[84]) >> 8) & 0xff;
917 clrsetbits_le32(&denali_phy[84], 0xff << 8, (tmp + 0x10) << 8);
918 tmp = (readl(&denali_phy[212]) >> 8) & 0xff;
919 clrsetbits_le32(&denali_phy[212], 0xff << 8, (tmp + 0x10) << 8);
920 tmp = (readl(&denali_phy[340]) >> 8) & 0xff;
921 clrsetbits_le32(&denali_phy[340], 0xff << 8, (tmp + 0x10) << 8);
922 tmp = (readl(&denali_phy[468]) >> 8) & 0xff;
923 clrsetbits_le32(&denali_phy[468], 0xff << 8, (tmp + 0x10) << 8);
926 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_83/211/339/467 offset_8
927 * dq_tsel_wr_end[7:4] add Half cycle
929 tmp = (readl(&denali_phy[83]) >> 16) & 0xff;
930 clrsetbits_le32(&denali_phy[83], 0xff << 16, (tmp + 0x10) << 16);
931 tmp = (readl(&denali_phy[211]) >> 16) & 0xff;
932 clrsetbits_le32(&denali_phy[211], 0xff << 16, (tmp + 0x10) << 16);
933 tmp = (readl(&denali_phy[339]) >> 16) & 0xff;
934 clrsetbits_le32(&denali_phy[339], 0xff << 16, (tmp + 0x10) << 16);
935 tmp = (readl(&denali_phy[467]) >> 16) & 0xff;
936 clrsetbits_le32(&denali_phy[467], 0xff << 16, (tmp + 0x10) << 16);
941 static void select_per_cs_training_index(const struct chan_info *chan,
944 u32 *denali_phy = chan->publ->denali_phy;
946 /* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */
947 if ((readl(&denali_phy[84]) >> 16) & 1) {
950 * phy_per_cs_training_index_X 1bit offset_24
952 clrsetbits_le32(&denali_phy[8], 0x1 << 24, rank << 24);
953 clrsetbits_le32(&denali_phy[136], 0x1 << 24, rank << 24);
954 clrsetbits_le32(&denali_phy[264], 0x1 << 24, rank << 24);
955 clrsetbits_le32(&denali_phy[392], 0x1 << 24, rank << 24);
959 static void override_write_leveling_value(const struct chan_info *chan)
961 u32 *denali_ctl = chan->pctl->denali_ctl;
962 u32 *denali_phy = chan->publ->denali_phy;
965 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
966 setbits_le32(&denali_phy[896], 1);
970 * phy_per_cs_training_multicast_en_X 1bit offset_16
972 clrsetbits_le32(&denali_phy[8], 0x1 << 16, 1 << 16);
973 clrsetbits_le32(&denali_phy[136], 0x1 << 16, 1 << 16);
974 clrsetbits_le32(&denali_phy[264], 0x1 << 16, 1 << 16);
975 clrsetbits_le32(&denali_phy[392], 0x1 << 16, 1 << 16);
977 for (byte = 0; byte < 4; byte++)
978 clrsetbits_le32(&denali_phy[63 + (128 * byte)], 0xffff << 16,
981 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
982 clrbits_le32(&denali_phy[896], 1);
984 /* CTL_200 ctrlupd_req 1bit offset_8 */
985 clrsetbits_le32(&denali_ctl[200], 0x1 << 8, 0x1 << 8);
988 static int data_training_ca(const struct chan_info *chan, u32 channel,
989 const struct rk3399_sdram_params *params)
991 u32 *denali_pi = chan->pi->denali_pi;
992 u32 *denali_phy = chan->publ->denali_phy;
994 u32 obs_0, obs_1, obs_2, obs_err = 0;
995 u32 rank = params->ch[channel].cap_info.rank;
998 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
999 writel(0x00003f7c, (&denali_pi[175]));
1001 if (params->base.dramtype == LPDDR4)
1002 rank_mask = (rank == 1) ? 0x5 : 0xf;
1004 rank_mask = (rank == 1) ? 0x1 : 0x3;
1006 for (i = 0; i < 4; i++) {
1007 if (!(rank_mask & (1 << i)))
1010 select_per_cs_training_index(chan, i);
1012 /* PI_100 PI_CALVL_EN:RW:8:2 */
1013 clrsetbits_le32(&denali_pi[100], 0x3 << 8, 0x2 << 8);
1015 /* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */
1016 clrsetbits_le32(&denali_pi[92],
1017 (0x1 << 16) | (0x3 << 24),
1018 (0x1 << 16) | (i << 24));
1020 /* Waiting for training complete */
1022 /* PI_174 PI_INT_STATUS:RD:8:18 */
1023 tmp = readl(&denali_pi[174]) >> 8;
1026 * PHY_532/660/789 phy_adr_calvl_obs1_:0:32
1028 obs_0 = readl(&denali_phy[532]);
1029 obs_1 = readl(&denali_phy[660]);
1030 obs_2 = readl(&denali_phy[788]);
1031 if (((obs_0 >> 30) & 0x3) ||
1032 ((obs_1 >> 30) & 0x3) ||
1033 ((obs_2 >> 30) & 0x3))
1035 if ((((tmp >> 11) & 0x1) == 0x1) &&
1036 (((tmp >> 13) & 0x1) == 0x1) &&
1037 (((tmp >> 5) & 0x1) == 0x0) &&
1040 else if ((((tmp >> 5) & 0x1) == 0x1) ||
1045 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1046 writel(0x00003f7c, (&denali_pi[175]));
1049 clrbits_le32(&denali_pi[100], 0x3 << 8);
1054 static int data_training_wl(const struct chan_info *chan, u32 channel,
1055 const struct rk3399_sdram_params *params)
1057 u32 *denali_pi = chan->pi->denali_pi;
1058 u32 *denali_phy = chan->publ->denali_phy;
1060 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
1061 u32 rank = params->ch[channel].cap_info.rank;
1063 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1064 writel(0x00003f7c, (&denali_pi[175]));
1066 for (i = 0; i < rank; i++) {
1067 select_per_cs_training_index(chan, i);
1069 /* PI_60 PI_WRLVL_EN:RW:8:2 */
1070 clrsetbits_le32(&denali_pi[60], 0x3 << 8, 0x2 << 8);
1072 /* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */
1073 clrsetbits_le32(&denali_pi[59],
1074 (0x1 << 8) | (0x3 << 16),
1075 (0x1 << 8) | (i << 16));
1077 /* Waiting for training complete */
1079 /* PI_174 PI_INT_STATUS:RD:8:18 */
1080 tmp = readl(&denali_pi[174]) >> 8;
1083 * check status obs, if error maybe can not
1084 * get leveling done PHY_40/168/296/424
1085 * phy_wrlvl_status_obs_X:0:13
1087 obs_0 = readl(&denali_phy[40]);
1088 obs_1 = readl(&denali_phy[168]);
1089 obs_2 = readl(&denali_phy[296]);
1090 obs_3 = readl(&denali_phy[424]);
1091 if (((obs_0 >> 12) & 0x1) ||
1092 ((obs_1 >> 12) & 0x1) ||
1093 ((obs_2 >> 12) & 0x1) ||
1094 ((obs_3 >> 12) & 0x1))
1096 if ((((tmp >> 10) & 0x1) == 0x1) &&
1097 (((tmp >> 13) & 0x1) == 0x1) &&
1098 (((tmp >> 4) & 0x1) == 0x0) &&
1101 else if ((((tmp >> 4) & 0x1) == 0x1) ||
1106 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1107 writel(0x00003f7c, (&denali_pi[175]));
1110 override_write_leveling_value(chan);
1111 clrbits_le32(&denali_pi[60], 0x3 << 8);
1116 static int data_training_rg(const struct chan_info *chan, u32 channel,
1117 const struct rk3399_sdram_params *params)
1119 u32 *denali_pi = chan->pi->denali_pi;
1120 u32 *denali_phy = chan->publ->denali_phy;
1122 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
1123 u32 rank = params->ch[channel].cap_info.rank;
1125 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1126 writel(0x00003f7c, (&denali_pi[175]));
1128 for (i = 0; i < rank; i++) {
1129 select_per_cs_training_index(chan, i);
1131 /* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */
1132 clrsetbits_le32(&denali_pi[80], 0x3 << 24, 0x2 << 24);
1135 * PI_74 PI_RDLVL_GATE_REQ:WR:16:1
1136 * PI_RDLVL_CS:RW:24:2
1138 clrsetbits_le32(&denali_pi[74],
1139 (0x1 << 16) | (0x3 << 24),
1140 (0x1 << 16) | (i << 24));
1142 /* Waiting for training complete */
1144 /* PI_174 PI_INT_STATUS:RD:8:18 */
1145 tmp = readl(&denali_pi[174]) >> 8;
1149 * PHY_43/171/299/427
1150 * PHY_GTLVL_STATUS_OBS_x:16:8
1152 obs_0 = readl(&denali_phy[43]);
1153 obs_1 = readl(&denali_phy[171]);
1154 obs_2 = readl(&denali_phy[299]);
1155 obs_3 = readl(&denali_phy[427]);
1156 if (((obs_0 >> (16 + 6)) & 0x3) ||
1157 ((obs_1 >> (16 + 6)) & 0x3) ||
1158 ((obs_2 >> (16 + 6)) & 0x3) ||
1159 ((obs_3 >> (16 + 6)) & 0x3))
1161 if ((((tmp >> 9) & 0x1) == 0x1) &&
1162 (((tmp >> 13) & 0x1) == 0x1) &&
1163 (((tmp >> 3) & 0x1) == 0x0) &&
1166 else if ((((tmp >> 3) & 0x1) == 0x1) ||
1171 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1172 writel(0x00003f7c, (&denali_pi[175]));
1175 clrbits_le32(&denali_pi[80], 0x3 << 24);
1180 static int data_training_rl(const struct chan_info *chan, u32 channel,
1181 const struct rk3399_sdram_params *params)
1183 u32 *denali_pi = chan->pi->denali_pi;
1185 u32 rank = params->ch[channel].cap_info.rank;
1187 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1188 writel(0x00003f7c, (&denali_pi[175]));
1190 for (i = 0; i < rank; i++) {
1191 select_per_cs_training_index(chan, i);
1193 /* PI_80 PI_RDLVL_EN:RW:16:2 */
1194 clrsetbits_le32(&denali_pi[80], 0x3 << 16, 0x2 << 16);
1196 /* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */
1197 clrsetbits_le32(&denali_pi[74],
1198 (0x1 << 8) | (0x3 << 24),
1199 (0x1 << 8) | (i << 24));
1201 /* Waiting for training complete */
1203 /* PI_174 PI_INT_STATUS:RD:8:18 */
1204 tmp = readl(&denali_pi[174]) >> 8;
1207 * make sure status obs not report error bit
1208 * PHY_46/174/302/430
1209 * phy_rdlvl_status_obs_X:16:8
1211 if ((((tmp >> 8) & 0x1) == 0x1) &&
1212 (((tmp >> 13) & 0x1) == 0x1) &&
1213 (((tmp >> 2) & 0x1) == 0x0))
1215 else if (((tmp >> 2) & 0x1) == 0x1)
1219 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1220 writel(0x00003f7c, (&denali_pi[175]));
1223 clrbits_le32(&denali_pi[80], 0x3 << 16);
1228 static int data_training_wdql(const struct chan_info *chan, u32 channel,
1229 const struct rk3399_sdram_params *params)
1231 u32 *denali_pi = chan->pi->denali_pi;
1233 u32 rank = params->ch[channel].cap_info.rank;
1236 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1237 writel(0x00003f7c, (&denali_pi[175]));
1239 if (params->base.dramtype == LPDDR4)
1240 rank_mask = (rank == 1) ? 0x5 : 0xf;
1242 rank_mask = (rank == 1) ? 0x1 : 0x3;
1244 for (i = 0; i < 4; i++) {
1245 if (!(rank_mask & (1 << i)))
1248 select_per_cs_training_index(chan, i);
1251 * disable PI_WDQLVL_VREF_EN before wdq leveling?
1252 * PI_181 PI_WDQLVL_VREF_EN:RW:8:1
1254 clrbits_le32(&denali_pi[181], 0x1 << 8);
1256 /* PI_124 PI_WDQLVL_EN:RW:16:2 */
1257 clrsetbits_le32(&denali_pi[124], 0x3 << 16, 0x2 << 16);
1259 /* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */
1260 clrsetbits_le32(&denali_pi[121],
1261 (0x1 << 8) | (0x3 << 16),
1262 (0x1 << 8) | (i << 16));
1264 /* Waiting for training complete */
1266 /* PI_174 PI_INT_STATUS:RD:8:18 */
1267 tmp = readl(&denali_pi[174]) >> 8;
1268 if ((((tmp >> 12) & 0x1) == 0x1) &&
1269 (((tmp >> 13) & 0x1) == 0x1) &&
1270 (((tmp >> 6) & 0x1) == 0x0))
1272 else if (((tmp >> 6) & 0x1) == 0x1)
1276 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1277 writel(0x00003f7c, (&denali_pi[175]));
1280 clrbits_le32(&denali_pi[124], 0x3 << 16);
1285 static int data_training(const struct chan_info *chan, u32 channel,
1286 const struct rk3399_sdram_params *params,
1289 u32 *denali_phy = chan->publ->denali_phy;
1292 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1293 setbits_le32(&denali_phy[927], (1 << 22));
1295 if (training_flag == PI_FULL_TRAINING) {
1296 if (params->base.dramtype == LPDDR4) {
1297 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1298 PI_READ_GATE_TRAINING |
1299 PI_READ_LEVELING | PI_WDQ_LEVELING;
1300 } else if (params->base.dramtype == LPDDR3) {
1301 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1302 PI_READ_GATE_TRAINING;
1303 } else if (params->base.dramtype == DDR3) {
1304 training_flag = PI_WRITE_LEVELING |
1305 PI_READ_GATE_TRAINING |
1310 /* ca training(LPDDR4,LPDDR3 support) */
1311 if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING) {
1312 ret = data_training_ca(chan, channel, params);
1314 debug("%s: data training ca failed\n", __func__);
1319 /* write leveling(LPDDR4,LPDDR3,DDR3 support) */
1320 if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING) {
1321 ret = data_training_wl(chan, channel, params);
1323 debug("%s: data training wl failed\n", __func__);
1328 /* read gate training(LPDDR4,LPDDR3,DDR3 support) */
1329 if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING) {
1330 ret = data_training_rg(chan, channel, params);
1332 debug("%s: data training rg failed\n", __func__);
1337 /* read leveling(LPDDR4,LPDDR3,DDR3 support) */
1338 if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING) {
1339 ret = data_training_rl(chan, channel, params);
1341 debug("%s: data training rl failed\n", __func__);
1346 /* wdq leveling(LPDDR4 support) */
1347 if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING) {
1348 ret = data_training_wdql(chan, channel, params);
1350 debug("%s: data training wdql failed\n", __func__);
1355 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1356 clrbits_le32(&denali_phy[927], (1 << 22));
1361 static void set_ddrconfig(const struct chan_info *chan,
1362 const struct rk3399_sdram_params *params,
1363 unsigned char channel, u32 ddrconfig)
1365 /* only need to set ddrconfig */
1366 struct rk3399_msch_regs *ddr_msch_regs = chan->msch;
1367 unsigned int cs0_cap = 0;
1368 unsigned int cs1_cap = 0;
1370 cs0_cap = (1 << (params->ch[channel].cap_info.cs0_row
1371 + params->ch[channel].cap_info.col
1372 + params->ch[channel].cap_info.bk
1373 + params->ch[channel].cap_info.bw - 20));
1374 if (params->ch[channel].cap_info.rank > 1)
1375 cs1_cap = cs0_cap >> (params->ch[channel].cap_info.cs0_row
1376 - params->ch[channel].cap_info.cs1_row);
1377 if (params->ch[channel].cap_info.row_3_4) {
1378 cs0_cap = cs0_cap * 3 / 4;
1379 cs1_cap = cs1_cap * 3 / 4;
1382 writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf);
1383 writel(((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8),
1384 &ddr_msch_regs->ddrsize);
1387 static void dram_all_config(struct dram_info *dram,
1388 const struct rk3399_sdram_params *params)
1392 unsigned int channel, idx;
1394 sys_reg2 |= SYS_REG_ENC_DDRTYPE(params->base.dramtype);
1395 sys_reg2 |= SYS_REG_ENC_NUM_CH(params->base.num_channels);
1397 for (channel = 0, idx = 0;
1398 (idx < params->base.num_channels) && (channel < 2);
1400 const struct rk3399_sdram_channel *info = ¶ms->ch[channel];
1401 struct rk3399_msch_regs *ddr_msch_regs;
1402 const struct rk3399_msch_timings *noc_timing;
1404 if (params->ch[channel].cap_info.col == 0)
1407 sys_reg2 |= SYS_REG_ENC_ROW_3_4(info->cap_info.row_3_4, channel);
1408 sys_reg2 |= SYS_REG_ENC_CHINFO(channel);
1409 sys_reg2 |= SYS_REG_ENC_RANK(info->cap_info.rank, channel);
1410 sys_reg2 |= SYS_REG_ENC_COL(info->cap_info.col, channel);
1411 sys_reg2 |= SYS_REG_ENC_BK(info->cap_info.bk, channel);
1412 sys_reg2 |= SYS_REG_ENC_BW(info->cap_info.bw, channel);
1413 sys_reg2 |= SYS_REG_ENC_DBW(info->cap_info.dbw, channel);
1414 SYS_REG_ENC_CS0_ROW(info->cap_info.cs0_row, sys_reg2, sys_reg3, channel);
1415 if (info->cap_info.cs1_row)
1416 SYS_REG_ENC_CS1_ROW(info->cap_info.cs1_row, sys_reg2,
1418 sys_reg3 |= SYS_REG_ENC_CS1_COL(info->cap_info.col, channel);
1419 sys_reg3 |= SYS_REG_ENC_VERSION(DDR_SYS_REG_VERSION);
1421 ddr_msch_regs = dram->chan[channel].msch;
1422 noc_timing = ¶ms->ch[channel].noc_timings;
1423 writel(noc_timing->ddrtiminga0,
1424 &ddr_msch_regs->ddrtiminga0);
1425 writel(noc_timing->ddrtimingb0,
1426 &ddr_msch_regs->ddrtimingb0);
1427 writel(noc_timing->ddrtimingc0.d32,
1428 &ddr_msch_regs->ddrtimingc0);
1429 writel(noc_timing->devtodev0,
1430 &ddr_msch_regs->devtodev0);
1431 writel(noc_timing->ddrmode.d32,
1432 &ddr_msch_regs->ddrmode);
1435 * rank 1 memory clock disable (dfi_dram_clk_disable = 1)
1437 * The hardware for LPDDR4 with
1438 * - CLK0P/N connect to lower 16-bits
1439 * - CLK1P/N connect to higher 16-bits
1441 * dfi dram clk is configured via CLK1P/N, so disabling
1442 * dfi dram clk will disable the CLK1P/N as well for lpddr4.
1444 if (params->ch[channel].cap_info.rank == 1 &&
1445 params->base.dramtype != LPDDR4)
1446 setbits_le32(&dram->chan[channel].pctl->denali_ctl[276],
1450 writel(sys_reg2, &dram->pmugrf->os_reg2);
1451 writel(sys_reg3, &dram->pmugrf->os_reg3);
1452 rk_clrsetreg(&dram->pmusgrf->soc_con4, 0x1f << 10,
1453 params->base.stride << 10);
1455 /* reboot hold register set */
1456 writel(PRESET_SGRF_HOLD(0) | PRESET_GPIO0_HOLD(1) |
1457 PRESET_GPIO1_HOLD(1),
1458 &dram->pmucru->pmucru_rstnhold_con[1]);
1459 clrsetbits_le32(&dram->cru->glb_rst_con, 0x3, 0x3);
1462 static int switch_to_phy_index1(struct dram_info *dram,
1463 const struct rk3399_sdram_params *params)
1467 u32 ch_count = params->base.num_channels;
1471 writel(RK_CLRSETBITS(0x03 << 4 | 1 << 2 | 1,
1472 1 << 4 | 1 << 2 | 1),
1473 &dram->cic->cic_ctrl0);
1474 while (!(readl(&dram->cic->cic_status0) & (1 << 2))) {
1478 debug("index1 frequency change overtime\n");
1484 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &dram->cic->cic_ctrl0);
1485 while (!(readl(&dram->cic->cic_status0) & (1 << 0))) {
1489 debug("index1 frequency done overtime\n");
1494 for (channel = 0; channel < ch_count; channel++) {
1495 denali_phy = dram->chan[channel].publ->denali_phy;
1496 clrsetbits_le32(&denali_phy[896], (0x3 << 8) | 1, 1 << 8);
1497 ret = data_training(&dram->chan[channel], channel,
1498 params, PI_FULL_TRAINING);
1500 debug("index1 training failed\n");
1508 static unsigned char calculate_stride(struct rk3399_sdram_params *params)
1510 unsigned int stride = params->base.stride;
1511 unsigned int channel, chinfo = 0;
1512 unsigned int ch_cap[2] = {0, 0};
1515 for (channel = 0; channel < 2; channel++) {
1516 unsigned int cs0_cap = 0;
1517 unsigned int cs1_cap = 0;
1518 struct sdram_cap_info *cap_info = ¶ms->ch[channel].cap_info;
1520 if (cap_info->col == 0)
1523 cs0_cap = (1 << (cap_info->cs0_row + cap_info->col +
1524 cap_info->bk + cap_info->bw - 20));
1525 if (cap_info->rank > 1)
1526 cs1_cap = cs0_cap >> (cap_info->cs0_row
1527 - cap_info->cs1_row);
1528 if (cap_info->row_3_4) {
1529 cs0_cap = cs0_cap * 3 / 4;
1530 cs1_cap = cs1_cap * 3 / 4;
1532 ch_cap[channel] = cs0_cap + cs1_cap;
1533 chinfo |= 1 << channel;
1536 /* stride calculation for 1 channel */
1537 if (params->base.num_channels == 1 && chinfo & 1)
1538 return 0x17; /* channel a */
1540 /* stride calculation for 2 channels, default gstride type is 256B */
1541 if (ch_cap[0] == ch_cap[1]) {
1542 cap = ch_cap[0] + ch_cap[1];
1553 * 768MB + 768MB same as total 2GB memory
1554 * useful space: 0-768MB 1GB-1792MB
1561 /* 1536MB + 1536MB */
1570 printf("%s: Unable to calculate stride for ", __func__);
1571 print_size((cap * (1 << 20)), " capacity\n");
1576 sdram_print_stride(stride);
1581 static void clear_channel_params(struct rk3399_sdram_params *params, u8 channel)
1583 params->ch[channel].cap_info.rank = 0;
1584 params->ch[channel].cap_info.col = 0;
1585 params->ch[channel].cap_info.bk = 0;
1586 params->ch[channel].cap_info.bw = 32;
1587 params->ch[channel].cap_info.dbw = 32;
1588 params->ch[channel].cap_info.row_3_4 = 0;
1589 params->ch[channel].cap_info.cs0_row = 0;
1590 params->ch[channel].cap_info.cs1_row = 0;
1591 params->ch[channel].cap_info.ddrconfig = 0;
1594 static int pctl_init(struct dram_info *dram, struct rk3399_sdram_params *params)
1599 for (channel = 0; channel < 2; channel++) {
1600 const struct chan_info *chan = &dram->chan[channel];
1601 struct rk3399_cru *cru = dram->cru;
1602 struct rk3399_ddr_publ_regs *publ = chan->publ;
1604 phy_pctrl_reset(cru, channel);
1605 phy_dll_bypass_set(publ, params->base.ddr_freq);
1607 ret = pctl_cfg(dram, chan, channel, params);
1609 printf("%s: pctl config failed\n", __func__);
1613 /* start to trigger initialization */
1614 pctl_start(dram, channel);
1620 static int sdram_init(struct dram_info *dram,
1621 struct rk3399_sdram_params *params)
1623 unsigned char dramtype = params->base.dramtype;
1624 unsigned int ddr_freq = params->base.ddr_freq;
1625 u32 training_flag = PI_READ_GATE_TRAINING;
1626 int channel, ch, rank;
1629 debug("Starting SDRAM initialization...\n");
1631 if ((dramtype == DDR3 && ddr_freq > 933) ||
1632 (dramtype == LPDDR3 && ddr_freq > 933) ||
1633 (dramtype == LPDDR4 && ddr_freq > 800)) {
1634 debug("SDRAM frequency is to high!");
1638 for (ch = 0; ch < 2; ch++) {
1639 params->ch[ch].cap_info.rank = 2;
1640 for (rank = 2; rank != 0; rank--) {
1641 ret = pctl_init(dram, params);
1643 printf("%s: pctl init failed\n", __func__);
1647 /* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */
1648 if (dramtype == LPDDR3)
1651 params->ch[ch].cap_info.rank = rank;
1654 * LPDDR3 CA training msut be trigger before
1656 * DDR3 is not have CA training.
1658 if (params->base.dramtype == LPDDR3)
1659 training_flag |= PI_CA_TRAINING;
1661 if (!(data_training(&dram->chan[ch], ch,
1662 params, training_flag)))
1665 /* Computed rank with associated channel number */
1666 params->ch[ch].cap_info.rank = rank;
1669 params->base.num_channels = 0;
1670 for (channel = 0; channel < 2; channel++) {
1671 const struct chan_info *chan = &dram->chan[channel];
1672 struct sdram_cap_info *cap_info = ¶ms->ch[channel].cap_info;
1673 u8 training_flag = PI_FULL_TRAINING;
1675 if (cap_info->rank == 0) {
1676 clear_channel_params(params, channel);
1679 params->base.num_channels++;
1683 debug(channel ? "1: " : "0: ");
1685 /* LPDDR3 should have write and read gate training */
1686 if (params->base.dramtype == LPDDR3)
1687 training_flag = PI_WRITE_LEVELING |
1688 PI_READ_GATE_TRAINING;
1690 if (params->base.dramtype != LPDDR4) {
1691 ret = data_training(dram, channel, params,
1694 debug("%s: data train failed for channel %d\n",
1700 sdram_print_ddr_info(cap_info, ¶ms->base);
1702 set_ddrconfig(chan, params, channel, cap_info->ddrconfig);
1705 if (params->base.num_channels == 0) {
1706 printf("%s: ", __func__);
1707 sdram_print_dram_type(params->base.dramtype);
1708 printf(" - %dMHz failed!\n", params->base.ddr_freq);
1712 params->base.stride = calculate_stride(params);
1713 dram_all_config(dram, params);
1714 switch_to_phy_index1(dram, params);
1716 debug("Finish SDRAM initialization...\n");
1720 static int rk3399_dmc_ofdata_to_platdata(struct udevice *dev)
1722 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1723 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1726 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
1727 (u32 *)&plat->sdram_params,
1728 sizeof(plat->sdram_params) / sizeof(u32));
1730 printf("%s: Cannot read rockchip,sdram-params %d\n",
1734 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
1736 printf("%s: regmap failed %d\n", __func__, ret);
1742 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1743 static int conv_of_platdata(struct udevice *dev)
1745 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1746 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1749 ret = regmap_init_mem_platdata(dev, dtplat->reg,
1750 ARRAY_SIZE(dtplat->reg) / 2,
1759 static int rk3399_dmc_init(struct udevice *dev)
1761 struct dram_info *priv = dev_get_priv(dev);
1762 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1764 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1765 struct rk3399_sdram_params *params = &plat->sdram_params;
1767 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1768 struct rk3399_sdram_params *params =
1769 (void *)dtplat->rockchip_sdram_params;
1771 ret = conv_of_platdata(dev);
1776 priv->cic = syscon_get_first_range(ROCKCHIP_SYSCON_CIC);
1777 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
1778 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
1779 priv->pmusgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
1780 priv->pmucru = rockchip_get_pmucru();
1781 priv->cru = rockchip_get_cru();
1782 priv->chan[0].pctl = regmap_get_range(plat->map, 0);
1783 priv->chan[0].pi = regmap_get_range(plat->map, 1);
1784 priv->chan[0].publ = regmap_get_range(plat->map, 2);
1785 priv->chan[0].msch = regmap_get_range(plat->map, 3);
1786 priv->chan[1].pctl = regmap_get_range(plat->map, 4);
1787 priv->chan[1].pi = regmap_get_range(plat->map, 5);
1788 priv->chan[1].publ = regmap_get_range(plat->map, 6);
1789 priv->chan[1].msch = regmap_get_range(plat->map, 7);
1791 debug("con reg %p %p %p %p %p %p %p %p\n",
1792 priv->chan[0].pctl, priv->chan[0].pi,
1793 priv->chan[0].publ, priv->chan[0].msch,
1794 priv->chan[1].pctl, priv->chan[1].pi,
1795 priv->chan[1].publ, priv->chan[1].msch);
1796 debug("cru %p, cic %p, grf %p, sgrf %p, pmucru %p\n", priv->cru,
1797 priv->cic, priv->pmugrf, priv->pmusgrf, priv->pmucru);
1799 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1800 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->ddr_clk);
1802 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
1805 printf("%s clk get failed %d\n", __func__, ret);
1809 ret = clk_set_rate(&priv->ddr_clk, params->base.ddr_freq * MHz);
1811 printf("%s clk set failed %d\n", __func__, ret);
1815 ret = sdram_init(priv, params);
1817 printf("%s DRAM init failed %d\n", __func__, ret);
1825 static int rk3399_dmc_probe(struct udevice *dev)
1827 #if defined(CONFIG_TPL_BUILD) || \
1828 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
1829 if (rk3399_dmc_init(dev))
1832 struct dram_info *priv = dev_get_priv(dev);
1834 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
1835 debug("%s: pmugrf = %p\n", __func__, priv->pmugrf);
1836 priv->info.base = CONFIG_SYS_SDRAM_BASE;
1838 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg2);
1843 static int rk3399_dmc_get_info(struct udevice *dev, struct ram_info *info)
1845 struct dram_info *priv = dev_get_priv(dev);
1852 static struct ram_ops rk3399_dmc_ops = {
1853 .get_info = rk3399_dmc_get_info,
1856 static const struct udevice_id rk3399_dmc_ids[] = {
1857 { .compatible = "rockchip,rk3399-dmc" },
1861 U_BOOT_DRIVER(dmc_rk3399) = {
1862 .name = "rockchip_rk3399_dmc",
1864 .of_match = rk3399_dmc_ids,
1865 .ops = &rk3399_dmc_ops,
1866 #if defined(CONFIG_TPL_BUILD) || \
1867 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
1868 .ofdata_to_platdata = rk3399_dmc_ofdata_to_platdata,
1870 .probe = rk3399_dmc_probe,
1871 .priv_auto_alloc_size = sizeof(struct dram_info),
1872 #if defined(CONFIG_TPL_BUILD) || \
1873 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
1874 .platdata_auto_alloc_size = sizeof(struct rockchip_dmc_plat),