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
44 #define CRU_SFTRST_DDR_CTRL(ch, n) ((0x1 << (8 + 16 + (ch) * 4)) | \
45 ((n) << (8 + (ch) * 4)))
46 #define CRU_SFTRST_DDR_PHY(ch, n) ((0x1 << (9 + 16 + (ch) * 4)) | \
47 ((n) << (9 + (ch) * 4)))
49 struct rk3399_ddr_pctl_regs *pctl;
50 struct rk3399_ddr_pi_regs *pi;
51 struct rk3399_ddr_publ_regs *publ;
52 struct rk3399_msch_regs *msch;
56 #if defined(CONFIG_TPL_BUILD) || \
57 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
58 u32 pwrup_srefresh_exit[2];
59 struct chan_info chan[2];
61 struct rk3399_cru *cru;
62 struct rk3399_grf_regs *grf;
63 struct rk3399_pmucru *pmucru;
64 struct rk3399_pmusgrf_regs *pmusgrf;
65 struct rk3399_ddr_cic_regs *cic;
68 struct rk3399_pmugrf_regs *pmugrf;
71 #if defined(CONFIG_TPL_BUILD) || \
72 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
74 struct rockchip_dmc_plat {
75 #if CONFIG_IS_ENABLED(OF_PLATDATA)
76 struct dtd_rockchip_rk3399_dmc dtplat;
78 struct rk3399_sdram_params sdram_params;
99 } lpddr4_io_setting[] = {
110 PHY_DRV_ODT_HI_Z, /* rd_odt; */
111 PHY_DRV_ODT_40, /* wr_dq_drv; */
112 PHY_DRV_ODT_40, /* wr_ca_drv; */
113 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
115 41, /* rd_vref; (unit %, range 3.3% - 48.7%) */
127 PHY_DRV_ODT_HI_Z, /* rd_odt; */
128 PHY_DRV_ODT_48, /* wr_dq_drv; */
129 PHY_DRV_ODT_40, /* wr_ca_drv; */
130 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
132 32, /* rd_vref; (unit %, range 3.3% - 48.7%) */
144 PHY_DRV_ODT_40, /* rd_odt; */
145 PHY_DRV_ODT_48, /* wr_dq_drv; */
146 PHY_DRV_ODT_40, /* wr_ca_drv; */
147 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
149 17, /* rd_vref; (unit %, range 3.3% - 48.7%) */
158 0x59, /* dq_vref; 32% */
161 PHY_DRV_ODT_HI_Z, /* rd_odt; */
162 PHY_DRV_ODT_48, /* wr_dq_drv; */
163 PHY_DRV_ODT_40, /* wr_ca_drv; */
164 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
166 32, /* rd_vref; (unit %, range 3.3% - 48.7%) */
178 PHY_DRV_ODT_40, /* rd_odt; */
179 PHY_DRV_ODT_60, /* wr_dq_drv; */
180 PHY_DRV_ODT_40, /* wr_ca_drv; */
181 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
183 17, /* rd_vref; (unit %, range 3.3% - 48.7%) */
188 * phy = 0, PHY boot freq
189 * phy = 1, PHY index 0
190 * phy = 2, PHY index 1
192 static struct io_setting *
193 lpddr4_get_io_settings(const struct rk3399_sdram_params *params, u32 mr5)
195 struct io_setting *io = NULL;
198 for (n = 0; n < ARRAY_SIZE(lpddr4_io_setting); n++) {
199 io = &lpddr4_io_setting[n];
202 if (io->mhz >= params->base.ddr_freq &&
206 if (io->mhz >= params->base.ddr_freq)
214 static void *get_ddrc0_con(struct dram_info *dram, u8 channel)
216 return (channel == 0) ? &dram->grf->ddrc0_con0 : &dram->grf->ddrc0_con1;
219 static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
223 for (i = 0; i < n / sizeof(u32); i++) {
230 static void rkclk_ddr_reset(struct rk3399_cru *cru, u32 channel, u32 ctl,
236 writel(CRU_SFTRST_DDR_CTRL(channel, ctl) |
237 CRU_SFTRST_DDR_PHY(channel, phy),
238 &cru->softrst_con[4]);
241 static void phy_pctrl_reset(struct rk3399_cru *cru, u32 channel)
243 rkclk_ddr_reset(cru, channel, 1, 1);
246 rkclk_ddr_reset(cru, channel, 1, 0);
249 rkclk_ddr_reset(cru, channel, 0, 0);
253 static void phy_dll_bypass_set(struct rk3399_ddr_publ_regs *ddr_publ_regs,
256 u32 *denali_phy = ddr_publ_regs->denali_phy;
258 /* From IP spec, only freq small than 125 can enter dll bypass mode */
260 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
261 setbits_le32(&denali_phy[86], (0x3 << 2) << 8);
262 setbits_le32(&denali_phy[214], (0x3 << 2) << 8);
263 setbits_le32(&denali_phy[342], (0x3 << 2) << 8);
264 setbits_le32(&denali_phy[470], (0x3 << 2) << 8);
266 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
267 setbits_le32(&denali_phy[547], (0x3 << 2) << 16);
268 setbits_le32(&denali_phy[675], (0x3 << 2) << 16);
269 setbits_le32(&denali_phy[803], (0x3 << 2) << 16);
271 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
272 clrbits_le32(&denali_phy[86], (0x3 << 2) << 8);
273 clrbits_le32(&denali_phy[214], (0x3 << 2) << 8);
274 clrbits_le32(&denali_phy[342], (0x3 << 2) << 8);
275 clrbits_le32(&denali_phy[470], (0x3 << 2) << 8);
277 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
278 clrbits_le32(&denali_phy[547], (0x3 << 2) << 16);
279 clrbits_le32(&denali_phy[675], (0x3 << 2) << 16);
280 clrbits_le32(&denali_phy[803], (0x3 << 2) << 16);
284 static void set_memory_map(const struct chan_info *chan, u32 channel,
285 const struct rk3399_sdram_params *params)
287 const struct rk3399_sdram_channel *sdram_ch = ¶ms->ch[channel];
288 u32 *denali_ctl = chan->pctl->denali_ctl;
289 u32 *denali_pi = chan->pi->denali_pi;
294 /* Get row number from ddrconfig setting */
295 if (sdram_ch->cap_info.ddrconfig < 2 ||
296 sdram_ch->cap_info.ddrconfig == 4)
298 else if (sdram_ch->cap_info.ddrconfig == 3)
303 cs_map = (sdram_ch->cap_info.rank > 1) ? 3 : 1;
304 reduc = (sdram_ch->cap_info.bw == 2) ? 0 : 1;
306 /* Set the dram configuration to ctrl */
307 clrsetbits_le32(&denali_ctl[191], 0xF, (12 - sdram_ch->cap_info.col));
308 clrsetbits_le32(&denali_ctl[190], (0x3 << 16) | (0x7 << 24),
309 ((3 - sdram_ch->cap_info.bk) << 16) |
312 clrsetbits_le32(&denali_ctl[196], 0x3 | (1 << 16),
313 cs_map | (reduc << 16));
315 /* PI_199 PI_COL_DIFF:RW:0:4 */
316 clrsetbits_le32(&denali_pi[199], 0xF, (12 - sdram_ch->cap_info.col));
318 /* PI_155 PI_ROW_DIFF:RW:24:3 PI_BANK_DIFF:RW:16:2 */
319 clrsetbits_le32(&denali_pi[155], (0x3 << 16) | (0x7 << 24),
320 ((3 - sdram_ch->cap_info.bk) << 16) |
323 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
326 else if (cs_map == 2)
332 /* PI_41 PI_CS_MAP:RW:24:4 */
333 clrsetbits_le32(&denali_pi[41], 0xf << 24, cs_map << 24);
334 if (sdram_ch->cap_info.rank == 1 && params->base.dramtype == DDR3)
335 writel(0x2EC7FFFF, &denali_pi[34]);
338 static int phy_io_config(const struct chan_info *chan,
339 const struct rk3399_sdram_params *params)
341 u32 *denali_phy = chan->publ->denali_phy;
342 u32 vref_mode_dq, vref_value_dq, vref_mode_ac, vref_value_ac;
345 u32 drv_value, odt_value;
349 if (params->base.dramtype == LPDDR4) {
352 vref_value_dq = 0x1f;
354 vref_value_ac = 0x1f;
356 } else if (params->base.dramtype == LPDDR3) {
357 if (params->base.odt == 1) {
358 vref_mode_dq = 0x5; /* LPDDR3 ODT */
359 drv_value = (readl(&denali_phy[6]) >> 12) & 0xf;
360 odt_value = (readl(&denali_phy[6]) >> 4) & 0xf;
361 if (drv_value == PHY_DRV_ODT_48) {
363 case PHY_DRV_ODT_240:
364 vref_value_dq = 0x16;
366 case PHY_DRV_ODT_120:
367 vref_value_dq = 0x26;
370 vref_value_dq = 0x36;
373 debug("Invalid ODT value.\n");
376 } else if (drv_value == PHY_DRV_ODT_40) {
378 case PHY_DRV_ODT_240:
379 vref_value_dq = 0x19;
381 case PHY_DRV_ODT_120:
382 vref_value_dq = 0x23;
385 vref_value_dq = 0x31;
388 debug("Invalid ODT value.\n");
391 } else if (drv_value == PHY_DRV_ODT_34_3) {
393 case PHY_DRV_ODT_240:
394 vref_value_dq = 0x17;
396 case PHY_DRV_ODT_120:
397 vref_value_dq = 0x20;
400 vref_value_dq = 0x2e;
403 debug("Invalid ODT value.\n");
407 debug("Invalid DRV value.\n");
411 vref_mode_dq = 0x2; /* LPDDR3 */
412 vref_value_dq = 0x1f;
415 vref_value_ac = 0x1f;
417 } else if (params->base.dramtype == DDR3) {
420 vref_value_dq = 0x1f;
422 vref_value_ac = 0x1f;
425 debug("Unknown DRAM type.\n");
429 reg_value = (vref_mode_dq << 9) | (0x1 << 8) | vref_value_dq;
431 /* PHY_913 PHY_PAD_VREF_CTRL_DQ_0 12bits offset_8 */
432 clrsetbits_le32(&denali_phy[913], 0xfff << 8, reg_value << 8);
433 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_1 12bits offset_0 */
434 clrsetbits_le32(&denali_phy[914], 0xfff, reg_value);
435 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_2 12bits offset_16 */
436 clrsetbits_le32(&denali_phy[914], 0xfff << 16, reg_value << 16);
437 /* PHY_915 PHY_PAD_VREF_CTRL_DQ_3 12bits offset_0 */
438 clrsetbits_le32(&denali_phy[915], 0xfff, reg_value);
440 reg_value = (vref_mode_ac << 9) | (0x1 << 8) | vref_value_ac;
442 /* PHY_915 PHY_PAD_VREF_CTRL_AC 12bits offset_16 */
443 clrsetbits_le32(&denali_phy[915], 0xfff << 16, reg_value << 16);
445 /* PHY_924 PHY_PAD_FDBK_DRIVE */
446 clrsetbits_le32(&denali_phy[924], 0x7 << 15, mode_sel << 15);
447 /* PHY_926 PHY_PAD_DATA_DRIVE */
448 clrsetbits_le32(&denali_phy[926], 0x7 << 6, mode_sel << 6);
449 /* PHY_927 PHY_PAD_DQS_DRIVE */
450 clrsetbits_le32(&denali_phy[927], 0x7 << 6, mode_sel << 6);
451 /* PHY_928 PHY_PAD_ADDR_DRIVE */
452 clrsetbits_le32(&denali_phy[928], 0x7 << 14, mode_sel << 14);
453 /* PHY_929 PHY_PAD_CLK_DRIVE */
454 clrsetbits_le32(&denali_phy[929], 0x7 << 14, mode_sel << 14);
455 /* PHY_935 PHY_PAD_CKE_DRIVE */
456 clrsetbits_le32(&denali_phy[935], 0x7 << 14, mode_sel << 14);
457 /* PHY_937 PHY_PAD_RST_DRIVE */
458 clrsetbits_le32(&denali_phy[937], 0x7 << 14, mode_sel << 14);
459 /* PHY_939 PHY_PAD_CS_DRIVE */
460 clrsetbits_le32(&denali_phy[939], 0x7 << 14, mode_sel << 14);
462 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
463 /* BOOSTP_EN & BOOSTN_EN */
464 reg_value = ((PHY_BOOSTP_EN << 4) | PHY_BOOSTN_EN);
465 /* PHY_925 PHY_PAD_FDBK_DRIVE2 */
466 clrsetbits_le32(&denali_phy[925], 0xff << 8, reg_value << 8);
467 /* PHY_926 PHY_PAD_DATA_DRIVE */
468 clrsetbits_le32(&denali_phy[926], 0xff << 12, reg_value << 12);
469 /* PHY_927 PHY_PAD_DQS_DRIVE */
470 clrsetbits_le32(&denali_phy[927], 0xff << 14, reg_value << 14);
471 /* PHY_928 PHY_PAD_ADDR_DRIVE */
472 clrsetbits_le32(&denali_phy[928], 0xff << 20, reg_value << 20);
473 /* PHY_929 PHY_PAD_CLK_DRIVE */
474 clrsetbits_le32(&denali_phy[929], 0xff << 22, reg_value << 22);
475 /* PHY_935 PHY_PAD_CKE_DRIVE */
476 clrsetbits_le32(&denali_phy[935], 0xff << 20, reg_value << 20);
477 /* PHY_937 PHY_PAD_RST_DRIVE */
478 clrsetbits_le32(&denali_phy[937], 0xff << 20, reg_value << 20);
479 /* PHY_939 PHY_PAD_CS_DRIVE */
480 clrsetbits_le32(&denali_phy[939], 0xff << 20, reg_value << 20);
482 /* SLEWP_EN & SLEWN_EN */
483 reg_value = ((PHY_SLEWP_EN << 3) | PHY_SLEWN_EN);
484 /* PHY_924 PHY_PAD_FDBK_DRIVE */
485 clrsetbits_le32(&denali_phy[924], 0x3f << 8, reg_value << 8);
486 /* PHY_926 PHY_PAD_DATA_DRIVE */
487 clrsetbits_le32(&denali_phy[926], 0x3f, reg_value);
488 /* PHY_927 PHY_PAD_DQS_DRIVE */
489 clrsetbits_le32(&denali_phy[927], 0x3f, reg_value);
490 /* PHY_928 PHY_PAD_ADDR_DRIVE */
491 clrsetbits_le32(&denali_phy[928], 0x3f << 8, reg_value << 8);
492 /* PHY_929 PHY_PAD_CLK_DRIVE */
493 clrsetbits_le32(&denali_phy[929], 0x3f << 8, reg_value << 8);
494 /* PHY_935 PHY_PAD_CKE_DRIVE */
495 clrsetbits_le32(&denali_phy[935], 0x3f << 8, reg_value << 8);
496 /* PHY_937 PHY_PAD_RST_DRIVE */
497 clrsetbits_le32(&denali_phy[937], 0x3f << 8, reg_value << 8);
498 /* PHY_939 PHY_PAD_CS_DRIVE */
499 clrsetbits_le32(&denali_phy[939], 0x3f << 8, reg_value << 8);
503 if (params->base.ddr_freq < 400)
505 else if (params->base.ddr_freq < 800)
507 else if (params->base.ddr_freq < 1200)
512 /* PHY_924 PHY_PAD_FDBK_DRIVE */
513 clrsetbits_le32(&denali_phy[924], 0x3 << 21, speed << 21);
514 /* PHY_926 PHY_PAD_DATA_DRIVE */
515 clrsetbits_le32(&denali_phy[926], 0x3 << 9, speed << 9);
516 /* PHY_927 PHY_PAD_DQS_DRIVE */
517 clrsetbits_le32(&denali_phy[927], 0x3 << 9, speed << 9);
518 /* PHY_928 PHY_PAD_ADDR_DRIVE */
519 clrsetbits_le32(&denali_phy[928], 0x3 << 17, speed << 17);
520 /* PHY_929 PHY_PAD_CLK_DRIVE */
521 clrsetbits_le32(&denali_phy[929], 0x3 << 17, speed << 17);
522 /* PHY_935 PHY_PAD_CKE_DRIVE */
523 clrsetbits_le32(&denali_phy[935], 0x3 << 17, speed << 17);
524 /* PHY_937 PHY_PAD_RST_DRIVE */
525 clrsetbits_le32(&denali_phy[937], 0x3 << 17, speed << 17);
526 /* PHY_939 PHY_PAD_CS_DRIVE */
527 clrsetbits_le32(&denali_phy[939], 0x3 << 17, speed << 17);
529 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
531 reg_value = PHY_RX_CM_INPUT;
532 /* PHY_924 PHY_PAD_FDBK_DRIVE */
533 clrsetbits_le32(&denali_phy[924], 0x1 << 14, reg_value << 14);
534 /* PHY_926 PHY_PAD_DATA_DRIVE */
535 clrsetbits_le32(&denali_phy[926], 0x1 << 11, reg_value << 11);
536 /* PHY_927 PHY_PAD_DQS_DRIVE */
537 clrsetbits_le32(&denali_phy[927], 0x1 << 13, reg_value << 13);
538 /* PHY_928 PHY_PAD_ADDR_DRIVE */
539 clrsetbits_le32(&denali_phy[928], 0x1 << 19, reg_value << 19);
540 /* PHY_929 PHY_PAD_CLK_DRIVE */
541 clrsetbits_le32(&denali_phy[929], 0x1 << 21, reg_value << 21);
542 /* PHY_935 PHY_PAD_CKE_DRIVE */
543 clrsetbits_le32(&denali_phy[935], 0x1 << 19, reg_value << 19);
544 /* PHY_937 PHY_PAD_RST_DRIVE */
545 clrsetbits_le32(&denali_phy[937], 0x1 << 19, reg_value << 19);
546 /* PHY_939 PHY_PAD_CS_DRIVE */
547 clrsetbits_le32(&denali_phy[939], 0x1 << 19, reg_value << 19);
553 static void set_ds_odt(const struct chan_info *chan,
554 const struct rk3399_sdram_params *params, u32 mr5)
556 u32 *denali_phy = chan->publ->denali_phy;
558 u32 tsel_idle_en, tsel_wr_en, tsel_rd_en;
559 u32 tsel_idle_select_p, tsel_rd_select_p;
560 u32 tsel_idle_select_n, tsel_rd_select_n;
561 u32 tsel_wr_select_dq_p, tsel_wr_select_ca_p;
562 u32 tsel_wr_select_dq_n, tsel_wr_select_ca_n;
563 u32 tsel_ckcs_select_p, tsel_ckcs_select_n;
564 struct io_setting *io = NULL;
567 if (params->base.dramtype == LPDDR4) {
568 io = lpddr4_get_io_settings(params, mr5);
570 tsel_rd_select_p = PHY_DRV_ODT_HI_Z;
571 tsel_rd_select_n = io->rd_odt;
573 tsel_idle_select_p = PHY_DRV_ODT_HI_Z;
574 tsel_idle_select_n = PHY_DRV_ODT_240;
576 tsel_wr_select_dq_p = io->wr_dq_drv;
577 tsel_wr_select_dq_n = PHY_DRV_ODT_40;
579 tsel_wr_select_ca_p = io->wr_ca_drv;
580 tsel_wr_select_ca_n = PHY_DRV_ODT_40;
582 tsel_ckcs_select_p = io->wr_ckcs_drv;
583 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
584 } else if (params->base.dramtype == LPDDR3) {
585 tsel_rd_select_p = PHY_DRV_ODT_240;
586 tsel_rd_select_n = PHY_DRV_ODT_HI_Z;
588 tsel_idle_select_p = PHY_DRV_ODT_240;
589 tsel_idle_select_n = PHY_DRV_ODT_HI_Z;
591 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
592 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
594 tsel_wr_select_ca_p = PHY_DRV_ODT_48;
595 tsel_wr_select_ca_n = PHY_DRV_ODT_48;
597 tsel_ckcs_select_p = PHY_DRV_ODT_34_3;
598 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
600 tsel_rd_select_p = PHY_DRV_ODT_240;
601 tsel_rd_select_n = PHY_DRV_ODT_240;
603 tsel_idle_select_p = PHY_DRV_ODT_240;
604 tsel_idle_select_n = PHY_DRV_ODT_240;
606 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
607 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
609 tsel_wr_select_ca_p = PHY_DRV_ODT_34_3;
610 tsel_wr_select_ca_n = PHY_DRV_ODT_34_3;
612 tsel_ckcs_select_p = PHY_DRV_ODT_34_3;
613 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
616 if (params->base.odt == 1)
625 * phy_dq_tsel_select_X 24bits DENALI_PHY_6/134/262/390 offset_0
626 * sets termination values for read/idle cycles and drive strength
627 * for write cycles for DQ/DM
629 reg_value = tsel_rd_select_n | (tsel_rd_select_p << 0x4) |
630 (tsel_wr_select_dq_n << 8) | (tsel_wr_select_dq_p << 12) |
631 (tsel_idle_select_n << 16) | (tsel_idle_select_p << 20);
632 clrsetbits_le32(&denali_phy[6], 0xffffff, reg_value);
633 clrsetbits_le32(&denali_phy[134], 0xffffff, reg_value);
634 clrsetbits_le32(&denali_phy[262], 0xffffff, reg_value);
635 clrsetbits_le32(&denali_phy[390], 0xffffff, reg_value);
638 * phy_dqs_tsel_select_X 24bits DENALI_PHY_7/135/263/391 offset_0
639 * sets termination values for read/idle cycles and drive strength
640 * for write cycles for DQS
642 clrsetbits_le32(&denali_phy[7], 0xffffff, reg_value);
643 clrsetbits_le32(&denali_phy[135], 0xffffff, reg_value);
644 clrsetbits_le32(&denali_phy[263], 0xffffff, reg_value);
645 clrsetbits_le32(&denali_phy[391], 0xffffff, reg_value);
647 /* phy_adr_tsel_select_ 8bits DENALI_PHY_544/672/800 offset_0 */
648 reg_value = tsel_wr_select_ca_n | (tsel_wr_select_ca_p << 0x4);
649 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
650 /* LPDDR4 these register read always return 0, so
651 * can not use clrsetbits_le32(), need to write32
653 writel((0x300 << 8) | reg_value, &denali_phy[544]);
654 writel((0x300 << 8) | reg_value, &denali_phy[672]);
655 writel((0x300 << 8) | reg_value, &denali_phy[800]);
657 clrsetbits_le32(&denali_phy[544], 0xff, reg_value);
658 clrsetbits_le32(&denali_phy[672], 0xff, reg_value);
659 clrsetbits_le32(&denali_phy[800], 0xff, reg_value);
662 /* phy_pad_addr_drive 8bits DENALI_PHY_928 offset_0 */
663 clrsetbits_le32(&denali_phy[928], 0xff, reg_value);
665 /* phy_pad_rst_drive 8bits DENALI_PHY_937 offset_0 */
666 clrsetbits_le32(&denali_phy[937], 0xff, reg_value);
668 /* phy_pad_cke_drive 8bits DENALI_PHY_935 offset_0 */
669 clrsetbits_le32(&denali_phy[935], 0xff, reg_value);
671 /* phy_pad_cs_drive 8bits DENALI_PHY_939 offset_0 */
672 clrsetbits_le32(&denali_phy[939], 0xff,
673 tsel_ckcs_select_n | (tsel_ckcs_select_p << 0x4));
675 /* phy_pad_clk_drive 8bits DENALI_PHY_929 offset_0 */
676 clrsetbits_le32(&denali_phy[929], 0xff,
677 tsel_ckcs_select_n | (tsel_ckcs_select_p << 0x4));
679 /* phy_pad_fdbk_drive 23bit DENALI_PHY_924/925 */
680 clrsetbits_le32(&denali_phy[924], 0xff,
681 tsel_wr_select_dq_n | (tsel_wr_select_dq_p << 4));
682 clrsetbits_le32(&denali_phy[925], 0xff,
683 tsel_rd_select_n | (tsel_rd_select_p << 4));
685 /* phy_dq_tsel_enable_X 3bits DENALI_PHY_5/133/261/389 offset_16 */
686 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
688 clrsetbits_le32(&denali_phy[5], 0x7 << 16, reg_value);
689 clrsetbits_le32(&denali_phy[133], 0x7 << 16, reg_value);
690 clrsetbits_le32(&denali_phy[261], 0x7 << 16, reg_value);
691 clrsetbits_le32(&denali_phy[389], 0x7 << 16, reg_value);
693 /* phy_dqs_tsel_enable_X 3bits DENALI_PHY_6/134/262/390 offset_24 */
694 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
696 clrsetbits_le32(&denali_phy[6], 0x7 << 24, reg_value);
697 clrsetbits_le32(&denali_phy[134], 0x7 << 24, reg_value);
698 clrsetbits_le32(&denali_phy[262], 0x7 << 24, reg_value);
699 clrsetbits_le32(&denali_phy[390], 0x7 << 24, reg_value);
701 /* phy_adr_tsel_enable_ 1bit DENALI_PHY_518/646/774 offset_8 */
702 reg_value = tsel_wr_en << 8;
703 clrsetbits_le32(&denali_phy[518], 0x1 << 8, reg_value);
704 clrsetbits_le32(&denali_phy[646], 0x1 << 8, reg_value);
705 clrsetbits_le32(&denali_phy[774], 0x1 << 8, reg_value);
707 /* phy_pad_addr_term tsel 1bit DENALI_PHY_933 offset_17 */
708 reg_value = tsel_wr_en << 17;
709 clrsetbits_le32(&denali_phy[933], 0x1 << 17, reg_value);
711 * pad_rst/cke/cs/clk_term tsel 1bits
712 * DENALI_PHY_938/936/940/934 offset_17
714 clrsetbits_le32(&denali_phy[938], 0x1 << 17, reg_value);
715 clrsetbits_le32(&denali_phy[936], 0x1 << 17, reg_value);
716 clrsetbits_le32(&denali_phy[940], 0x1 << 17, reg_value);
717 clrsetbits_le32(&denali_phy[934], 0x1 << 17, reg_value);
719 /* phy_pad_fdbk_term 1bit DENALI_PHY_930 offset_17 */
720 clrsetbits_le32(&denali_phy[930], 0x1 << 17, reg_value);
722 phy_io_config(chan, params);
725 static void pctl_start(struct dram_info *dram, u8 channel)
727 const struct chan_info *chan = &dram->chan[channel];
728 u32 *denali_ctl = chan->pctl->denali_ctl;
729 u32 *denali_phy = chan->publ->denali_phy;
730 u32 *ddrc0_con = get_ddrc0_con(dram, channel);
734 writel(0x01000000, &ddrc0_con);
736 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 0x2 << 24);
738 while (!(readl(&denali_ctl[203]) & (1 << 3))) {
740 printf("%s: Failed to init pctl for channel %d\n",
750 writel(0x01000100, &ddrc0_con);
752 for (byte = 0; byte < 4; byte++) {
754 writel((tmp << 16) | tmp, &denali_phy[53 + (128 * byte)]);
755 writel((tmp << 16) | tmp, &denali_phy[54 + (128 * byte)]);
756 writel((tmp << 16) | tmp, &denali_phy[55 + (128 * byte)]);
757 writel((tmp << 16) | tmp, &denali_phy[56 + (128 * byte)]);
758 writel((tmp << 16) | tmp, &denali_phy[57 + (128 * byte)]);
760 clrsetbits_le32(&denali_phy[58 + (128 * byte)], 0xffff, tmp);
763 clrsetbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT,
764 dram->pwrup_srefresh_exit[channel]);
767 static int pctl_cfg(struct dram_info *dram, const struct chan_info *chan,
768 u32 channel, struct rk3399_sdram_params *params)
770 u32 *denali_ctl = chan->pctl->denali_ctl;
771 u32 *denali_pi = chan->pi->denali_pi;
772 u32 *denali_phy = chan->publ->denali_phy;
773 const u32 *params_ctl = params->pctl_regs.denali_ctl;
774 const u32 *params_phy = params->phy_regs.denali_phy;
778 * work around controller bug:
779 * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed
781 copy_to_reg(&denali_ctl[1], ¶ms_ctl[1],
782 sizeof(struct rk3399_ddr_pctl_regs) - 4);
783 writel(params_ctl[0], &denali_ctl[0]);
786 * two channel init at the same time, then ZQ Cal Start
787 * at the same time, it will use the same RZQ, but cannot
788 * start at the same time.
790 * So, increase tINIT3 for channel 1, will avoid two
791 * channel ZQ Cal Start at the same time
793 if (params->base.dramtype == LPDDR4 && channel == 1) {
794 tmp = ((params->base.ddr_freq * MHz + 999) / 1000);
795 tmp1 = readl(&denali_ctl[14]);
796 writel(tmp + tmp1, &denali_ctl[14]);
799 copy_to_reg(denali_pi, ¶ms->pi_regs.denali_pi[0],
800 sizeof(struct rk3399_ddr_pi_regs));
802 /* rank count need to set for init */
803 set_memory_map(chan, channel, params);
805 writel(params->phy_regs.denali_phy[910], &denali_phy[910]);
806 writel(params->phy_regs.denali_phy[911], &denali_phy[911]);
807 writel(params->phy_regs.denali_phy[912], &denali_phy[912]);
809 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
810 writel(params->phy_regs.denali_phy[898], &denali_phy[898]);
811 writel(params->phy_regs.denali_phy[919], &denali_phy[919]);
814 dram->pwrup_srefresh_exit[channel] = readl(&denali_ctl[68]) &
816 clrbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT);
819 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 1 << 24);
821 setbits_le32(&denali_pi[0], START);
822 setbits_le32(&denali_ctl[0], START);
825 * LPDDR4 use PLL bypass mode for init
826 * not need to wait for the PLL to lock
828 if (params->base.dramtype != LPDDR4) {
829 /* Waiting for phy DLL lock */
831 tmp = readl(&denali_phy[920]);
832 tmp1 = readl(&denali_phy[921]);
833 tmp2 = readl(&denali_phy[922]);
834 if ((((tmp >> 16) & 0x1) == 0x1) &&
835 (((tmp1 >> 16) & 0x1) == 0x1) &&
836 (((tmp1 >> 0) & 0x1) == 0x1) &&
837 (((tmp2 >> 0) & 0x1) == 0x1))
842 copy_to_reg(&denali_phy[896], ¶ms_phy[896], (958 - 895) * 4);
843 copy_to_reg(&denali_phy[0], ¶ms_phy[0], (90 - 0 + 1) * 4);
844 copy_to_reg(&denali_phy[128], ¶ms_phy[128], (218 - 128 + 1) * 4);
845 copy_to_reg(&denali_phy[256], ¶ms_phy[256], (346 - 256 + 1) * 4);
846 copy_to_reg(&denali_phy[384], ¶ms_phy[384], (474 - 384 + 1) * 4);
847 copy_to_reg(&denali_phy[512], ¶ms_phy[512], (549 - 512 + 1) * 4);
848 copy_to_reg(&denali_phy[640], ¶ms_phy[640], (677 - 640 + 1) * 4);
849 copy_to_reg(&denali_phy[768], ¶ms_phy[768], (805 - 768 + 1) * 4);
850 set_ds_odt(chan, params, 0);
853 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_84/212/340/468 offset_8
854 * dqs_tsel_wr_end[7:4] add Half cycle
856 tmp = (readl(&denali_phy[84]) >> 8) & 0xff;
857 clrsetbits_le32(&denali_phy[84], 0xff << 8, (tmp + 0x10) << 8);
858 tmp = (readl(&denali_phy[212]) >> 8) & 0xff;
859 clrsetbits_le32(&denali_phy[212], 0xff << 8, (tmp + 0x10) << 8);
860 tmp = (readl(&denali_phy[340]) >> 8) & 0xff;
861 clrsetbits_le32(&denali_phy[340], 0xff << 8, (tmp + 0x10) << 8);
862 tmp = (readl(&denali_phy[468]) >> 8) & 0xff;
863 clrsetbits_le32(&denali_phy[468], 0xff << 8, (tmp + 0x10) << 8);
866 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_83/211/339/467 offset_8
867 * dq_tsel_wr_end[7:4] add Half cycle
869 tmp = (readl(&denali_phy[83]) >> 16) & 0xff;
870 clrsetbits_le32(&denali_phy[83], 0xff << 16, (tmp + 0x10) << 16);
871 tmp = (readl(&denali_phy[211]) >> 16) & 0xff;
872 clrsetbits_le32(&denali_phy[211], 0xff << 16, (tmp + 0x10) << 16);
873 tmp = (readl(&denali_phy[339]) >> 16) & 0xff;
874 clrsetbits_le32(&denali_phy[339], 0xff << 16, (tmp + 0x10) << 16);
875 tmp = (readl(&denali_phy[467]) >> 16) & 0xff;
876 clrsetbits_le32(&denali_phy[467], 0xff << 16, (tmp + 0x10) << 16);
881 static void select_per_cs_training_index(const struct chan_info *chan,
884 u32 *denali_phy = chan->publ->denali_phy;
886 /* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */
887 if ((readl(&denali_phy[84]) >> 16) & 1) {
890 * phy_per_cs_training_index_X 1bit offset_24
892 clrsetbits_le32(&denali_phy[8], 0x1 << 24, rank << 24);
893 clrsetbits_le32(&denali_phy[136], 0x1 << 24, rank << 24);
894 clrsetbits_le32(&denali_phy[264], 0x1 << 24, rank << 24);
895 clrsetbits_le32(&denali_phy[392], 0x1 << 24, rank << 24);
899 static void override_write_leveling_value(const struct chan_info *chan)
901 u32 *denali_ctl = chan->pctl->denali_ctl;
902 u32 *denali_phy = chan->publ->denali_phy;
905 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
906 setbits_le32(&denali_phy[896], 1);
910 * phy_per_cs_training_multicast_en_X 1bit offset_16
912 clrsetbits_le32(&denali_phy[8], 0x1 << 16, 1 << 16);
913 clrsetbits_le32(&denali_phy[136], 0x1 << 16, 1 << 16);
914 clrsetbits_le32(&denali_phy[264], 0x1 << 16, 1 << 16);
915 clrsetbits_le32(&denali_phy[392], 0x1 << 16, 1 << 16);
917 for (byte = 0; byte < 4; byte++)
918 clrsetbits_le32(&denali_phy[63 + (128 * byte)], 0xffff << 16,
921 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
922 clrbits_le32(&denali_phy[896], 1);
924 /* CTL_200 ctrlupd_req 1bit offset_8 */
925 clrsetbits_le32(&denali_ctl[200], 0x1 << 8, 0x1 << 8);
928 static int data_training_ca(const struct chan_info *chan, u32 channel,
929 const struct rk3399_sdram_params *params)
931 u32 *denali_pi = chan->pi->denali_pi;
932 u32 *denali_phy = chan->publ->denali_phy;
934 u32 obs_0, obs_1, obs_2, obs_err = 0;
935 u32 rank = params->ch[channel].cap_info.rank;
938 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
939 writel(0x00003f7c, (&denali_pi[175]));
941 if (params->base.dramtype == LPDDR4)
942 rank_mask = (rank == 1) ? 0x5 : 0xf;
944 rank_mask = (rank == 1) ? 0x1 : 0x3;
946 for (i = 0; i < 4; i++) {
947 if (!(rank_mask & (1 << i)))
950 select_per_cs_training_index(chan, i);
952 /* PI_100 PI_CALVL_EN:RW:8:2 */
953 clrsetbits_le32(&denali_pi[100], 0x3 << 8, 0x2 << 8);
955 /* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */
956 clrsetbits_le32(&denali_pi[92],
957 (0x1 << 16) | (0x3 << 24),
958 (0x1 << 16) | (i << 24));
960 /* Waiting for training complete */
962 /* PI_174 PI_INT_STATUS:RD:8:18 */
963 tmp = readl(&denali_pi[174]) >> 8;
966 * PHY_532/660/789 phy_adr_calvl_obs1_:0:32
968 obs_0 = readl(&denali_phy[532]);
969 obs_1 = readl(&denali_phy[660]);
970 obs_2 = readl(&denali_phy[788]);
971 if (((obs_0 >> 30) & 0x3) ||
972 ((obs_1 >> 30) & 0x3) ||
973 ((obs_2 >> 30) & 0x3))
975 if ((((tmp >> 11) & 0x1) == 0x1) &&
976 (((tmp >> 13) & 0x1) == 0x1) &&
977 (((tmp >> 5) & 0x1) == 0x0) &&
980 else if ((((tmp >> 5) & 0x1) == 0x1) ||
985 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
986 writel(0x00003f7c, (&denali_pi[175]));
989 clrbits_le32(&denali_pi[100], 0x3 << 8);
994 static int data_training_wl(const struct chan_info *chan, u32 channel,
995 const struct rk3399_sdram_params *params)
997 u32 *denali_pi = chan->pi->denali_pi;
998 u32 *denali_phy = chan->publ->denali_phy;
1000 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
1001 u32 rank = params->ch[channel].cap_info.rank;
1003 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1004 writel(0x00003f7c, (&denali_pi[175]));
1006 for (i = 0; i < rank; i++) {
1007 select_per_cs_training_index(chan, i);
1009 /* PI_60 PI_WRLVL_EN:RW:8:2 */
1010 clrsetbits_le32(&denali_pi[60], 0x3 << 8, 0x2 << 8);
1012 /* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */
1013 clrsetbits_le32(&denali_pi[59],
1014 (0x1 << 8) | (0x3 << 16),
1015 (0x1 << 8) | (i << 16));
1017 /* Waiting for training complete */
1019 /* PI_174 PI_INT_STATUS:RD:8:18 */
1020 tmp = readl(&denali_pi[174]) >> 8;
1023 * check status obs, if error maybe can not
1024 * get leveling done PHY_40/168/296/424
1025 * phy_wrlvl_status_obs_X:0:13
1027 obs_0 = readl(&denali_phy[40]);
1028 obs_1 = readl(&denali_phy[168]);
1029 obs_2 = readl(&denali_phy[296]);
1030 obs_3 = readl(&denali_phy[424]);
1031 if (((obs_0 >> 12) & 0x1) ||
1032 ((obs_1 >> 12) & 0x1) ||
1033 ((obs_2 >> 12) & 0x1) ||
1034 ((obs_3 >> 12) & 0x1))
1036 if ((((tmp >> 10) & 0x1) == 0x1) &&
1037 (((tmp >> 13) & 0x1) == 0x1) &&
1038 (((tmp >> 4) & 0x1) == 0x0) &&
1041 else if ((((tmp >> 4) & 0x1) == 0x1) ||
1046 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1047 writel(0x00003f7c, (&denali_pi[175]));
1050 override_write_leveling_value(chan);
1051 clrbits_le32(&denali_pi[60], 0x3 << 8);
1056 static int data_training_rg(const struct chan_info *chan, u32 channel,
1057 const struct rk3399_sdram_params *params)
1059 u32 *denali_pi = chan->pi->denali_pi;
1060 u32 *denali_phy = chan->publ->denali_phy;
1062 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
1063 u32 rank = params->ch[channel].cap_info.rank;
1065 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1066 writel(0x00003f7c, (&denali_pi[175]));
1068 for (i = 0; i < rank; i++) {
1069 select_per_cs_training_index(chan, i);
1071 /* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */
1072 clrsetbits_le32(&denali_pi[80], 0x3 << 24, 0x2 << 24);
1075 * PI_74 PI_RDLVL_GATE_REQ:WR:16:1
1076 * PI_RDLVL_CS:RW:24:2
1078 clrsetbits_le32(&denali_pi[74],
1079 (0x1 << 16) | (0x3 << 24),
1080 (0x1 << 16) | (i << 24));
1082 /* Waiting for training complete */
1084 /* PI_174 PI_INT_STATUS:RD:8:18 */
1085 tmp = readl(&denali_pi[174]) >> 8;
1089 * PHY_43/171/299/427
1090 * PHY_GTLVL_STATUS_OBS_x:16:8
1092 obs_0 = readl(&denali_phy[43]);
1093 obs_1 = readl(&denali_phy[171]);
1094 obs_2 = readl(&denali_phy[299]);
1095 obs_3 = readl(&denali_phy[427]);
1096 if (((obs_0 >> (16 + 6)) & 0x3) ||
1097 ((obs_1 >> (16 + 6)) & 0x3) ||
1098 ((obs_2 >> (16 + 6)) & 0x3) ||
1099 ((obs_3 >> (16 + 6)) & 0x3))
1101 if ((((tmp >> 9) & 0x1) == 0x1) &&
1102 (((tmp >> 13) & 0x1) == 0x1) &&
1103 (((tmp >> 3) & 0x1) == 0x0) &&
1106 else if ((((tmp >> 3) & 0x1) == 0x1) ||
1111 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1112 writel(0x00003f7c, (&denali_pi[175]));
1115 clrbits_le32(&denali_pi[80], 0x3 << 24);
1120 static int data_training_rl(const struct chan_info *chan, u32 channel,
1121 const struct rk3399_sdram_params *params)
1123 u32 *denali_pi = chan->pi->denali_pi;
1125 u32 rank = params->ch[channel].cap_info.rank;
1127 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1128 writel(0x00003f7c, (&denali_pi[175]));
1130 for (i = 0; i < rank; i++) {
1131 select_per_cs_training_index(chan, i);
1133 /* PI_80 PI_RDLVL_EN:RW:16:2 */
1134 clrsetbits_le32(&denali_pi[80], 0x3 << 16, 0x2 << 16);
1136 /* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */
1137 clrsetbits_le32(&denali_pi[74],
1138 (0x1 << 8) | (0x3 << 24),
1139 (0x1 << 8) | (i << 24));
1141 /* Waiting for training complete */
1143 /* PI_174 PI_INT_STATUS:RD:8:18 */
1144 tmp = readl(&denali_pi[174]) >> 8;
1147 * make sure status obs not report error bit
1148 * PHY_46/174/302/430
1149 * phy_rdlvl_status_obs_X:16:8
1151 if ((((tmp >> 8) & 0x1) == 0x1) &&
1152 (((tmp >> 13) & 0x1) == 0x1) &&
1153 (((tmp >> 2) & 0x1) == 0x0))
1155 else if (((tmp >> 2) & 0x1) == 0x1)
1159 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1160 writel(0x00003f7c, (&denali_pi[175]));
1163 clrbits_le32(&denali_pi[80], 0x3 << 16);
1168 static int data_training_wdql(const struct chan_info *chan, u32 channel,
1169 const struct rk3399_sdram_params *params)
1171 u32 *denali_pi = chan->pi->denali_pi;
1173 u32 rank = params->ch[channel].cap_info.rank;
1176 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1177 writel(0x00003f7c, (&denali_pi[175]));
1179 if (params->base.dramtype == LPDDR4)
1180 rank_mask = (rank == 1) ? 0x5 : 0xf;
1182 rank_mask = (rank == 1) ? 0x1 : 0x3;
1184 for (i = 0; i < 4; i++) {
1185 if (!(rank_mask & (1 << i)))
1188 select_per_cs_training_index(chan, i);
1191 * disable PI_WDQLVL_VREF_EN before wdq leveling?
1192 * PI_181 PI_WDQLVL_VREF_EN:RW:8:1
1194 clrbits_le32(&denali_pi[181], 0x1 << 8);
1196 /* PI_124 PI_WDQLVL_EN:RW:16:2 */
1197 clrsetbits_le32(&denali_pi[124], 0x3 << 16, 0x2 << 16);
1199 /* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */
1200 clrsetbits_le32(&denali_pi[121],
1201 (0x1 << 8) | (0x3 << 16),
1202 (0x1 << 8) | (i << 16));
1204 /* Waiting for training complete */
1206 /* PI_174 PI_INT_STATUS:RD:8:18 */
1207 tmp = readl(&denali_pi[174]) >> 8;
1208 if ((((tmp >> 12) & 0x1) == 0x1) &&
1209 (((tmp >> 13) & 0x1) == 0x1) &&
1210 (((tmp >> 6) & 0x1) == 0x0))
1212 else if (((tmp >> 6) & 0x1) == 0x1)
1216 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1217 writel(0x00003f7c, (&denali_pi[175]));
1220 clrbits_le32(&denali_pi[124], 0x3 << 16);
1225 static int data_training(const struct chan_info *chan, u32 channel,
1226 const struct rk3399_sdram_params *params,
1229 u32 *denali_phy = chan->publ->denali_phy;
1232 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1233 setbits_le32(&denali_phy[927], (1 << 22));
1235 if (training_flag == PI_FULL_TRAINING) {
1236 if (params->base.dramtype == LPDDR4) {
1237 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1238 PI_READ_GATE_TRAINING |
1239 PI_READ_LEVELING | PI_WDQ_LEVELING;
1240 } else if (params->base.dramtype == LPDDR3) {
1241 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1242 PI_READ_GATE_TRAINING;
1243 } else if (params->base.dramtype == DDR3) {
1244 training_flag = PI_WRITE_LEVELING |
1245 PI_READ_GATE_TRAINING |
1250 /* ca training(LPDDR4,LPDDR3 support) */
1251 if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING) {
1252 ret = data_training_ca(chan, channel, params);
1254 debug("%s: data training ca failed\n", __func__);
1259 /* write leveling(LPDDR4,LPDDR3,DDR3 support) */
1260 if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING) {
1261 ret = data_training_wl(chan, channel, params);
1263 debug("%s: data training wl failed\n", __func__);
1268 /* read gate training(LPDDR4,LPDDR3,DDR3 support) */
1269 if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING) {
1270 ret = data_training_rg(chan, channel, params);
1272 debug("%s: data training rg failed\n", __func__);
1277 /* read leveling(LPDDR4,LPDDR3,DDR3 support) */
1278 if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING) {
1279 ret = data_training_rl(chan, channel, params);
1281 debug("%s: data training rl failed\n", __func__);
1286 /* wdq leveling(LPDDR4 support) */
1287 if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING) {
1288 ret = data_training_wdql(chan, channel, params);
1290 debug("%s: data training wdql failed\n", __func__);
1295 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1296 clrbits_le32(&denali_phy[927], (1 << 22));
1301 static void set_ddrconfig(const struct chan_info *chan,
1302 const struct rk3399_sdram_params *params,
1303 unsigned char channel, u32 ddrconfig)
1305 /* only need to set ddrconfig */
1306 struct rk3399_msch_regs *ddr_msch_regs = chan->msch;
1307 unsigned int cs0_cap = 0;
1308 unsigned int cs1_cap = 0;
1310 cs0_cap = (1 << (params->ch[channel].cap_info.cs0_row
1311 + params->ch[channel].cap_info.col
1312 + params->ch[channel].cap_info.bk
1313 + params->ch[channel].cap_info.bw - 20));
1314 if (params->ch[channel].cap_info.rank > 1)
1315 cs1_cap = cs0_cap >> (params->ch[channel].cap_info.cs0_row
1316 - params->ch[channel].cap_info.cs1_row);
1317 if (params->ch[channel].cap_info.row_3_4) {
1318 cs0_cap = cs0_cap * 3 / 4;
1319 cs1_cap = cs1_cap * 3 / 4;
1322 writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf);
1323 writel(((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8),
1324 &ddr_msch_regs->ddrsize);
1327 static void dram_all_config(struct dram_info *dram,
1328 const struct rk3399_sdram_params *params)
1332 unsigned int channel, idx;
1334 sys_reg2 |= SYS_REG_ENC_DDRTYPE(params->base.dramtype);
1335 sys_reg2 |= SYS_REG_ENC_NUM_CH(params->base.num_channels);
1337 for (channel = 0, idx = 0;
1338 (idx < params->base.num_channels) && (channel < 2);
1340 const struct rk3399_sdram_channel *info = ¶ms->ch[channel];
1341 struct rk3399_msch_regs *ddr_msch_regs;
1342 const struct rk3399_msch_timings *noc_timing;
1344 if (params->ch[channel].cap_info.col == 0)
1347 sys_reg2 |= SYS_REG_ENC_ROW_3_4(info->cap_info.row_3_4, channel);
1348 sys_reg2 |= SYS_REG_ENC_CHINFO(channel);
1349 sys_reg2 |= SYS_REG_ENC_RANK(info->cap_info.rank, channel);
1350 sys_reg2 |= SYS_REG_ENC_COL(info->cap_info.col, channel);
1351 sys_reg2 |= SYS_REG_ENC_BK(info->cap_info.bk, channel);
1352 sys_reg2 |= SYS_REG_ENC_BW(info->cap_info.bw, channel);
1353 sys_reg2 |= SYS_REG_ENC_DBW(info->cap_info.dbw, channel);
1354 SYS_REG_ENC_CS0_ROW(info->cap_info.cs0_row, sys_reg2, sys_reg3, channel);
1355 if (info->cap_info.cs1_row)
1356 SYS_REG_ENC_CS1_ROW(info->cap_info.cs1_row, sys_reg2,
1358 sys_reg3 |= SYS_REG_ENC_CS1_COL(info->cap_info.col, channel);
1359 sys_reg3 |= SYS_REG_ENC_VERSION(DDR_SYS_REG_VERSION);
1361 ddr_msch_regs = dram->chan[channel].msch;
1362 noc_timing = ¶ms->ch[channel].noc_timings;
1363 writel(noc_timing->ddrtiminga0,
1364 &ddr_msch_regs->ddrtiminga0);
1365 writel(noc_timing->ddrtimingb0,
1366 &ddr_msch_regs->ddrtimingb0);
1367 writel(noc_timing->ddrtimingc0.d32,
1368 &ddr_msch_regs->ddrtimingc0);
1369 writel(noc_timing->devtodev0,
1370 &ddr_msch_regs->devtodev0);
1371 writel(noc_timing->ddrmode.d32,
1372 &ddr_msch_regs->ddrmode);
1375 * rank 1 memory clock disable (dfi_dram_clk_disable = 1)
1377 * The hardware for LPDDR4 with
1378 * - CLK0P/N connect to lower 16-bits
1379 * - CLK1P/N connect to higher 16-bits
1381 * dfi dram clk is configured via CLK1P/N, so disabling
1382 * dfi dram clk will disable the CLK1P/N as well for lpddr4.
1384 if (params->ch[channel].cap_info.rank == 1 &&
1385 params->base.dramtype != LPDDR4)
1386 setbits_le32(&dram->chan[channel].pctl->denali_ctl[276],
1390 writel(sys_reg2, &dram->pmugrf->os_reg2);
1391 writel(sys_reg3, &dram->pmugrf->os_reg3);
1392 rk_clrsetreg(&dram->pmusgrf->soc_con4, 0x1f << 10,
1393 params->base.stride << 10);
1395 /* reboot hold register set */
1396 writel(PRESET_SGRF_HOLD(0) | PRESET_GPIO0_HOLD(1) |
1397 PRESET_GPIO1_HOLD(1),
1398 &dram->pmucru->pmucru_rstnhold_con[1]);
1399 clrsetbits_le32(&dram->cru->glb_rst_con, 0x3, 0x3);
1402 static int switch_to_phy_index1(struct dram_info *dram,
1403 const struct rk3399_sdram_params *params)
1407 u32 ch_count = params->base.num_channels;
1411 writel(RK_CLRSETBITS(0x03 << 4 | 1 << 2 | 1,
1412 1 << 4 | 1 << 2 | 1),
1413 &dram->cic->cic_ctrl0);
1414 while (!(readl(&dram->cic->cic_status0) & (1 << 2))) {
1418 debug("index1 frequency change overtime\n");
1424 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &dram->cic->cic_ctrl0);
1425 while (!(readl(&dram->cic->cic_status0) & (1 << 0))) {
1429 debug("index1 frequency done overtime\n");
1434 for (channel = 0; channel < ch_count; channel++) {
1435 denali_phy = dram->chan[channel].publ->denali_phy;
1436 clrsetbits_le32(&denali_phy[896], (0x3 << 8) | 1, 1 << 8);
1437 ret = data_training(&dram->chan[channel], channel,
1438 params, PI_FULL_TRAINING);
1440 debug("index1 training failed\n");
1448 static unsigned char calculate_stride(struct rk3399_sdram_params *params)
1450 unsigned int stride = params->base.stride;
1451 unsigned int channel, chinfo = 0;
1452 unsigned int ch_cap[2] = {0, 0};
1455 for (channel = 0; channel < 2; channel++) {
1456 unsigned int cs0_cap = 0;
1457 unsigned int cs1_cap = 0;
1458 struct sdram_cap_info *cap_info = ¶ms->ch[channel].cap_info;
1460 if (cap_info->col == 0)
1463 cs0_cap = (1 << (cap_info->cs0_row + cap_info->col +
1464 cap_info->bk + cap_info->bw - 20));
1465 if (cap_info->rank > 1)
1466 cs1_cap = cs0_cap >> (cap_info->cs0_row
1467 - cap_info->cs1_row);
1468 if (cap_info->row_3_4) {
1469 cs0_cap = cs0_cap * 3 / 4;
1470 cs1_cap = cs1_cap * 3 / 4;
1472 ch_cap[channel] = cs0_cap + cs1_cap;
1473 chinfo |= 1 << channel;
1476 /* stride calculation for 1 channel */
1477 if (params->base.num_channels == 1 && chinfo & 1)
1478 return 0x17; /* channel a */
1480 /* stride calculation for 2 channels, default gstride type is 256B */
1481 if (ch_cap[0] == ch_cap[1]) {
1482 cap = ch_cap[0] + ch_cap[1];
1493 * 768MB + 768MB same as total 2GB memory
1494 * useful space: 0-768MB 1GB-1792MB
1501 /* 1536MB + 1536MB */
1510 printf("%s: Unable to calculate stride for ", __func__);
1511 print_size((cap * (1 << 20)), " capacity\n");
1516 sdram_print_stride(stride);
1521 static void clear_channel_params(struct rk3399_sdram_params *params, u8 channel)
1523 params->ch[channel].cap_info.rank = 0;
1524 params->ch[channel].cap_info.col = 0;
1525 params->ch[channel].cap_info.bk = 0;
1526 params->ch[channel].cap_info.bw = 32;
1527 params->ch[channel].cap_info.dbw = 32;
1528 params->ch[channel].cap_info.row_3_4 = 0;
1529 params->ch[channel].cap_info.cs0_row = 0;
1530 params->ch[channel].cap_info.cs1_row = 0;
1531 params->ch[channel].cap_info.ddrconfig = 0;
1534 static int pctl_init(struct dram_info *dram, struct rk3399_sdram_params *params)
1539 for (channel = 0; channel < 2; channel++) {
1540 const struct chan_info *chan = &dram->chan[channel];
1541 struct rk3399_cru *cru = dram->cru;
1542 struct rk3399_ddr_publ_regs *publ = chan->publ;
1544 phy_pctrl_reset(cru, channel);
1545 phy_dll_bypass_set(publ, params->base.ddr_freq);
1547 ret = pctl_cfg(dram, chan, channel, params);
1549 printf("%s: pctl config failed\n", __func__);
1553 /* start to trigger initialization */
1554 pctl_start(dram, channel);
1560 static int sdram_init(struct dram_info *dram,
1561 struct rk3399_sdram_params *params)
1563 unsigned char dramtype = params->base.dramtype;
1564 unsigned int ddr_freq = params->base.ddr_freq;
1565 u32 training_flag = PI_READ_GATE_TRAINING;
1566 int channel, ch, rank;
1569 debug("Starting SDRAM initialization...\n");
1571 if ((dramtype == DDR3 && ddr_freq > 933) ||
1572 (dramtype == LPDDR3 && ddr_freq > 933) ||
1573 (dramtype == LPDDR4 && ddr_freq > 800)) {
1574 debug("SDRAM frequency is to high!");
1578 for (ch = 0; ch < 2; ch++) {
1579 params->ch[ch].cap_info.rank = 2;
1580 for (rank = 2; rank != 0; rank--) {
1581 ret = pctl_init(dram, params);
1583 printf("%s: pctl init failed\n", __func__);
1587 /* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */
1588 if (dramtype == LPDDR3)
1591 params->ch[ch].cap_info.rank = rank;
1594 * LPDDR3 CA training msut be trigger before
1596 * DDR3 is not have CA training.
1598 if (params->base.dramtype == LPDDR3)
1599 training_flag |= PI_CA_TRAINING;
1601 if (!(data_training(&dram->chan[ch], ch,
1602 params, training_flag)))
1605 /* Computed rank with associated channel number */
1606 params->ch[ch].cap_info.rank = rank;
1609 params->base.num_channels = 0;
1610 for (channel = 0; channel < 2; channel++) {
1611 const struct chan_info *chan = &dram->chan[channel];
1612 struct sdram_cap_info *cap_info = ¶ms->ch[channel].cap_info;
1613 u8 training_flag = PI_FULL_TRAINING;
1615 if (cap_info->rank == 0) {
1616 clear_channel_params(params, channel);
1619 params->base.num_channels++;
1623 debug(channel ? "1: " : "0: ");
1625 /* LPDDR3 should have write and read gate training */
1626 if (params->base.dramtype == LPDDR3)
1627 training_flag = PI_WRITE_LEVELING |
1628 PI_READ_GATE_TRAINING;
1630 if (params->base.dramtype != LPDDR4) {
1631 ret = data_training(dram, channel, params,
1634 debug("%s: data train failed for channel %d\n",
1640 sdram_print_ddr_info(cap_info, ¶ms->base);
1642 set_ddrconfig(chan, params, channel, cap_info->ddrconfig);
1645 if (params->base.num_channels == 0) {
1646 printf("%s: ", __func__);
1647 sdram_print_dram_type(params->base.dramtype);
1648 printf(" - %dMHz failed!\n", params->base.ddr_freq);
1652 params->base.stride = calculate_stride(params);
1653 dram_all_config(dram, params);
1654 switch_to_phy_index1(dram, params);
1656 debug("Finish SDRAM initialization...\n");
1660 static int rk3399_dmc_ofdata_to_platdata(struct udevice *dev)
1662 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1663 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1666 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
1667 (u32 *)&plat->sdram_params,
1668 sizeof(plat->sdram_params) / sizeof(u32));
1670 printf("%s: Cannot read rockchip,sdram-params %d\n",
1674 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
1676 printf("%s: regmap failed %d\n", __func__, ret);
1682 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1683 static int conv_of_platdata(struct udevice *dev)
1685 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1686 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1689 ret = regmap_init_mem_platdata(dev, dtplat->reg,
1690 ARRAY_SIZE(dtplat->reg) / 2,
1699 static int rk3399_dmc_init(struct udevice *dev)
1701 struct dram_info *priv = dev_get_priv(dev);
1702 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1704 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1705 struct rk3399_sdram_params *params = &plat->sdram_params;
1707 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1708 struct rk3399_sdram_params *params =
1709 (void *)dtplat->rockchip_sdram_params;
1711 ret = conv_of_platdata(dev);
1716 priv->cic = syscon_get_first_range(ROCKCHIP_SYSCON_CIC);
1717 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
1718 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
1719 priv->pmusgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
1720 priv->pmucru = rockchip_get_pmucru();
1721 priv->cru = rockchip_get_cru();
1722 priv->chan[0].pctl = regmap_get_range(plat->map, 0);
1723 priv->chan[0].pi = regmap_get_range(plat->map, 1);
1724 priv->chan[0].publ = regmap_get_range(plat->map, 2);
1725 priv->chan[0].msch = regmap_get_range(plat->map, 3);
1726 priv->chan[1].pctl = regmap_get_range(plat->map, 4);
1727 priv->chan[1].pi = regmap_get_range(plat->map, 5);
1728 priv->chan[1].publ = regmap_get_range(plat->map, 6);
1729 priv->chan[1].msch = regmap_get_range(plat->map, 7);
1731 debug("con reg %p %p %p %p %p %p %p %p\n",
1732 priv->chan[0].pctl, priv->chan[0].pi,
1733 priv->chan[0].publ, priv->chan[0].msch,
1734 priv->chan[1].pctl, priv->chan[1].pi,
1735 priv->chan[1].publ, priv->chan[1].msch);
1736 debug("cru %p, cic %p, grf %p, sgrf %p, pmucru %p\n", priv->cru,
1737 priv->cic, priv->pmugrf, priv->pmusgrf, priv->pmucru);
1739 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1740 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->ddr_clk);
1742 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
1745 printf("%s clk get failed %d\n", __func__, ret);
1749 ret = clk_set_rate(&priv->ddr_clk, params->base.ddr_freq * MHz);
1751 printf("%s clk set failed %d\n", __func__, ret);
1755 ret = sdram_init(priv, params);
1757 printf("%s DRAM init failed %d\n", __func__, ret);
1765 static int rk3399_dmc_probe(struct udevice *dev)
1767 #if defined(CONFIG_TPL_BUILD) || \
1768 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
1769 if (rk3399_dmc_init(dev))
1772 struct dram_info *priv = dev_get_priv(dev);
1774 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
1775 debug("%s: pmugrf = %p\n", __func__, priv->pmugrf);
1776 priv->info.base = CONFIG_SYS_SDRAM_BASE;
1778 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg2);
1783 static int rk3399_dmc_get_info(struct udevice *dev, struct ram_info *info)
1785 struct dram_info *priv = dev_get_priv(dev);
1792 static struct ram_ops rk3399_dmc_ops = {
1793 .get_info = rk3399_dmc_get_info,
1796 static const struct udevice_id rk3399_dmc_ids[] = {
1797 { .compatible = "rockchip,rk3399-dmc" },
1801 U_BOOT_DRIVER(dmc_rk3399) = {
1802 .name = "rockchip_rk3399_dmc",
1804 .of_match = rk3399_dmc_ids,
1805 .ops = &rk3399_dmc_ops,
1806 #if defined(CONFIG_TPL_BUILD) || \
1807 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
1808 .ofdata_to_platdata = rk3399_dmc_ofdata_to_platdata,
1810 .probe = rk3399_dmc_probe,
1811 .priv_auto_alloc_size = sizeof(struct dram_info),
1812 #if defined(CONFIG_TPL_BUILD) || \
1813 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
1814 .platdata_auto_alloc_size = sizeof(struct rockchip_dmc_plat),