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 struct io_setting *io = NULL;
566 if (params->base.dramtype == LPDDR4) {
567 io = lpddr4_get_io_settings(params, mr5);
569 tsel_rd_select_p = PHY_DRV_ODT_HI_Z;
570 tsel_rd_select_n = io->rd_odt;
572 tsel_idle_select_p = PHY_DRV_ODT_HI_Z;
573 tsel_idle_select_n = PHY_DRV_ODT_240;
575 tsel_wr_select_dq_p = io->wr_dq_drv;
576 tsel_wr_select_dq_n = PHY_DRV_ODT_40;
578 tsel_wr_select_ca_p = io->wr_ca_drv;
579 tsel_wr_select_ca_n = PHY_DRV_ODT_40;
580 } else if (params->base.dramtype == LPDDR3) {
581 tsel_rd_select_p = PHY_DRV_ODT_240;
582 tsel_rd_select_n = PHY_DRV_ODT_HI_Z;
584 tsel_idle_select_p = PHY_DRV_ODT_240;
585 tsel_idle_select_n = PHY_DRV_ODT_HI_Z;
587 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
588 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
590 tsel_wr_select_ca_p = PHY_DRV_ODT_48;
591 tsel_wr_select_ca_n = PHY_DRV_ODT_48;
593 tsel_rd_select_p = PHY_DRV_ODT_240;
594 tsel_rd_select_n = PHY_DRV_ODT_240;
596 tsel_idle_select_p = PHY_DRV_ODT_240;
597 tsel_idle_select_n = PHY_DRV_ODT_240;
599 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
600 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
602 tsel_wr_select_ca_p = PHY_DRV_ODT_34_3;
603 tsel_wr_select_ca_n = PHY_DRV_ODT_34_3;
606 if (params->base.odt == 1)
615 * phy_dq_tsel_select_X 24bits DENALI_PHY_6/134/262/390 offset_0
616 * sets termination values for read/idle cycles and drive strength
617 * for write cycles for DQ/DM
619 reg_value = tsel_rd_select_n | (tsel_rd_select_p << 0x4) |
620 (tsel_wr_select_dq_n << 8) | (tsel_wr_select_dq_p << 12) |
621 (tsel_idle_select_n << 16) | (tsel_idle_select_p << 20);
622 clrsetbits_le32(&denali_phy[6], 0xffffff, reg_value);
623 clrsetbits_le32(&denali_phy[134], 0xffffff, reg_value);
624 clrsetbits_le32(&denali_phy[262], 0xffffff, reg_value);
625 clrsetbits_le32(&denali_phy[390], 0xffffff, reg_value);
628 * phy_dqs_tsel_select_X 24bits DENALI_PHY_7/135/263/391 offset_0
629 * sets termination values for read/idle cycles and drive strength
630 * for write cycles for DQS
632 clrsetbits_le32(&denali_phy[7], 0xffffff, reg_value);
633 clrsetbits_le32(&denali_phy[135], 0xffffff, reg_value);
634 clrsetbits_le32(&denali_phy[263], 0xffffff, reg_value);
635 clrsetbits_le32(&denali_phy[391], 0xffffff, reg_value);
637 /* phy_adr_tsel_select_ 8bits DENALI_PHY_544/672/800 offset_0 */
638 reg_value = tsel_wr_select_ca_n | (tsel_wr_select_ca_p << 0x4);
639 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
640 /* LPDDR4 these register read always return 0, so
641 * can not use clrsetbits_le32(), need to write32
643 writel((0x300 << 8) | reg_value, &denali_phy[544]);
644 writel((0x300 << 8) | reg_value, &denali_phy[672]);
645 writel((0x300 << 8) | reg_value, &denali_phy[800]);
647 clrsetbits_le32(&denali_phy[544], 0xff, reg_value);
648 clrsetbits_le32(&denali_phy[672], 0xff, reg_value);
649 clrsetbits_le32(&denali_phy[800], 0xff, reg_value);
652 /* phy_pad_addr_drive 8bits DENALI_PHY_928 offset_0 */
653 clrsetbits_le32(&denali_phy[928], 0xff, reg_value);
655 /* phy_pad_rst_drive 8bits DENALI_PHY_937 offset_0 */
656 clrsetbits_le32(&denali_phy[937], 0xff, reg_value);
658 /* phy_pad_cke_drive 8bits DENALI_PHY_935 offset_0 */
659 clrsetbits_le32(&denali_phy[935], 0xff, reg_value);
661 /* phy_pad_cs_drive 8bits DENALI_PHY_939 offset_0 */
662 clrsetbits_le32(&denali_phy[939], 0xff, reg_value);
664 /* phy_pad_clk_drive 8bits DENALI_PHY_929 offset_0 */
665 clrsetbits_le32(&denali_phy[929], 0xff, reg_value);
667 /* phy_pad_fdbk_drive 23bit DENALI_PHY_924/925 */
668 clrsetbits_le32(&denali_phy[924], 0xff,
669 tsel_wr_select_dq_n | (tsel_wr_select_dq_p << 4));
670 clrsetbits_le32(&denali_phy[925], 0xff,
671 tsel_rd_select_n | (tsel_rd_select_p << 4));
673 /* phy_dq_tsel_enable_X 3bits DENALI_PHY_5/133/261/389 offset_16 */
674 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
676 clrsetbits_le32(&denali_phy[5], 0x7 << 16, reg_value);
677 clrsetbits_le32(&denali_phy[133], 0x7 << 16, reg_value);
678 clrsetbits_le32(&denali_phy[261], 0x7 << 16, reg_value);
679 clrsetbits_le32(&denali_phy[389], 0x7 << 16, reg_value);
681 /* phy_dqs_tsel_enable_X 3bits DENALI_PHY_6/134/262/390 offset_24 */
682 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
684 clrsetbits_le32(&denali_phy[6], 0x7 << 24, reg_value);
685 clrsetbits_le32(&denali_phy[134], 0x7 << 24, reg_value);
686 clrsetbits_le32(&denali_phy[262], 0x7 << 24, reg_value);
687 clrsetbits_le32(&denali_phy[390], 0x7 << 24, reg_value);
689 /* phy_adr_tsel_enable_ 1bit DENALI_PHY_518/646/774 offset_8 */
690 reg_value = tsel_wr_en << 8;
691 clrsetbits_le32(&denali_phy[518], 0x1 << 8, reg_value);
692 clrsetbits_le32(&denali_phy[646], 0x1 << 8, reg_value);
693 clrsetbits_le32(&denali_phy[774], 0x1 << 8, reg_value);
695 /* phy_pad_addr_term tsel 1bit DENALI_PHY_933 offset_17 */
696 reg_value = tsel_wr_en << 17;
697 clrsetbits_le32(&denali_phy[933], 0x1 << 17, reg_value);
699 * pad_rst/cke/cs/clk_term tsel 1bits
700 * DENALI_PHY_938/936/940/934 offset_17
702 clrsetbits_le32(&denali_phy[938], 0x1 << 17, reg_value);
703 clrsetbits_le32(&denali_phy[936], 0x1 << 17, reg_value);
704 clrsetbits_le32(&denali_phy[940], 0x1 << 17, reg_value);
705 clrsetbits_le32(&denali_phy[934], 0x1 << 17, reg_value);
707 /* phy_pad_fdbk_term 1bit DENALI_PHY_930 offset_17 */
708 clrsetbits_le32(&denali_phy[930], 0x1 << 17, reg_value);
710 phy_io_config(chan, params);
713 static void pctl_start(struct dram_info *dram, u8 channel)
715 const struct chan_info *chan = &dram->chan[channel];
716 u32 *denali_ctl = chan->pctl->denali_ctl;
717 u32 *denali_phy = chan->publ->denali_phy;
718 u32 *ddrc0_con = get_ddrc0_con(dram, channel);
722 writel(0x01000000, &ddrc0_con);
724 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 0x2 << 24);
726 while (!(readl(&denali_ctl[203]) & (1 << 3))) {
728 printf("%s: Failed to init pctl for channel %d\n",
738 writel(0x01000100, &ddrc0_con);
740 for (byte = 0; byte < 4; byte++) {
742 writel((tmp << 16) | tmp, &denali_phy[53 + (128 * byte)]);
743 writel((tmp << 16) | tmp, &denali_phy[54 + (128 * byte)]);
744 writel((tmp << 16) | tmp, &denali_phy[55 + (128 * byte)]);
745 writel((tmp << 16) | tmp, &denali_phy[56 + (128 * byte)]);
746 writel((tmp << 16) | tmp, &denali_phy[57 + (128 * byte)]);
748 clrsetbits_le32(&denali_phy[58 + (128 * byte)], 0xffff, tmp);
751 clrsetbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT,
752 dram->pwrup_srefresh_exit[channel]);
755 static int pctl_cfg(struct dram_info *dram, const struct chan_info *chan,
756 u32 channel, struct rk3399_sdram_params *params)
758 u32 *denali_ctl = chan->pctl->denali_ctl;
759 u32 *denali_pi = chan->pi->denali_pi;
760 u32 *denali_phy = chan->publ->denali_phy;
761 const u32 *params_ctl = params->pctl_regs.denali_ctl;
762 const u32 *params_phy = params->phy_regs.denali_phy;
766 * work around controller bug:
767 * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed
769 copy_to_reg(&denali_ctl[1], ¶ms_ctl[1],
770 sizeof(struct rk3399_ddr_pctl_regs) - 4);
771 writel(params_ctl[0], &denali_ctl[0]);
774 * two channel init at the same time, then ZQ Cal Start
775 * at the same time, it will use the same RZQ, but cannot
776 * start at the same time.
778 * So, increase tINIT3 for channel 1, will avoid two
779 * channel ZQ Cal Start at the same time
781 if (params->base.dramtype == LPDDR4 && channel == 1) {
782 tmp = ((params->base.ddr_freq * MHz + 999) / 1000);
783 tmp1 = readl(&denali_ctl[14]);
784 writel(tmp + tmp1, &denali_ctl[14]);
787 copy_to_reg(denali_pi, ¶ms->pi_regs.denali_pi[0],
788 sizeof(struct rk3399_ddr_pi_regs));
790 /* rank count need to set for init */
791 set_memory_map(chan, channel, params);
793 writel(params->phy_regs.denali_phy[910], &denali_phy[910]);
794 writel(params->phy_regs.denali_phy[911], &denali_phy[911]);
795 writel(params->phy_regs.denali_phy[912], &denali_phy[912]);
797 if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) {
798 writel(params->phy_regs.denali_phy[898], &denali_phy[898]);
799 writel(params->phy_regs.denali_phy[919], &denali_phy[919]);
802 dram->pwrup_srefresh_exit[channel] = readl(&denali_ctl[68]) &
804 clrbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT);
807 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 1 << 24);
809 setbits_le32(&denali_pi[0], START);
810 setbits_le32(&denali_ctl[0], START);
813 * LPDDR4 use PLL bypass mode for init
814 * not need to wait for the PLL to lock
816 if (params->base.dramtype != LPDDR4) {
817 /* Waiting for phy DLL lock */
819 tmp = readl(&denali_phy[920]);
820 tmp1 = readl(&denali_phy[921]);
821 tmp2 = readl(&denali_phy[922]);
822 if ((((tmp >> 16) & 0x1) == 0x1) &&
823 (((tmp1 >> 16) & 0x1) == 0x1) &&
824 (((tmp1 >> 0) & 0x1) == 0x1) &&
825 (((tmp2 >> 0) & 0x1) == 0x1))
830 copy_to_reg(&denali_phy[896], ¶ms_phy[896], (958 - 895) * 4);
831 copy_to_reg(&denali_phy[0], ¶ms_phy[0], (90 - 0 + 1) * 4);
832 copy_to_reg(&denali_phy[128], ¶ms_phy[128], (218 - 128 + 1) * 4);
833 copy_to_reg(&denali_phy[256], ¶ms_phy[256], (346 - 256 + 1) * 4);
834 copy_to_reg(&denali_phy[384], ¶ms_phy[384], (474 - 384 + 1) * 4);
835 copy_to_reg(&denali_phy[512], ¶ms_phy[512], (549 - 512 + 1) * 4);
836 copy_to_reg(&denali_phy[640], ¶ms_phy[640], (677 - 640 + 1) * 4);
837 copy_to_reg(&denali_phy[768], ¶ms_phy[768], (805 - 768 + 1) * 4);
838 set_ds_odt(chan, params, 0);
841 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_84/212/340/468 offset_8
842 * dqs_tsel_wr_end[7:4] add Half cycle
844 tmp = (readl(&denali_phy[84]) >> 8) & 0xff;
845 clrsetbits_le32(&denali_phy[84], 0xff << 8, (tmp + 0x10) << 8);
846 tmp = (readl(&denali_phy[212]) >> 8) & 0xff;
847 clrsetbits_le32(&denali_phy[212], 0xff << 8, (tmp + 0x10) << 8);
848 tmp = (readl(&denali_phy[340]) >> 8) & 0xff;
849 clrsetbits_le32(&denali_phy[340], 0xff << 8, (tmp + 0x10) << 8);
850 tmp = (readl(&denali_phy[468]) >> 8) & 0xff;
851 clrsetbits_le32(&denali_phy[468], 0xff << 8, (tmp + 0x10) << 8);
854 * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_83/211/339/467 offset_8
855 * dq_tsel_wr_end[7:4] add Half cycle
857 tmp = (readl(&denali_phy[83]) >> 16) & 0xff;
858 clrsetbits_le32(&denali_phy[83], 0xff << 16, (tmp + 0x10) << 16);
859 tmp = (readl(&denali_phy[211]) >> 16) & 0xff;
860 clrsetbits_le32(&denali_phy[211], 0xff << 16, (tmp + 0x10) << 16);
861 tmp = (readl(&denali_phy[339]) >> 16) & 0xff;
862 clrsetbits_le32(&denali_phy[339], 0xff << 16, (tmp + 0x10) << 16);
863 tmp = (readl(&denali_phy[467]) >> 16) & 0xff;
864 clrsetbits_le32(&denali_phy[467], 0xff << 16, (tmp + 0x10) << 16);
869 static void select_per_cs_training_index(const struct chan_info *chan,
872 u32 *denali_phy = chan->publ->denali_phy;
874 /* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */
875 if ((readl(&denali_phy[84]) >> 16) & 1) {
878 * phy_per_cs_training_index_X 1bit offset_24
880 clrsetbits_le32(&denali_phy[8], 0x1 << 24, rank << 24);
881 clrsetbits_le32(&denali_phy[136], 0x1 << 24, rank << 24);
882 clrsetbits_le32(&denali_phy[264], 0x1 << 24, rank << 24);
883 clrsetbits_le32(&denali_phy[392], 0x1 << 24, rank << 24);
887 static void override_write_leveling_value(const struct chan_info *chan)
889 u32 *denali_ctl = chan->pctl->denali_ctl;
890 u32 *denali_phy = chan->publ->denali_phy;
893 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
894 setbits_le32(&denali_phy[896], 1);
898 * phy_per_cs_training_multicast_en_X 1bit offset_16
900 clrsetbits_le32(&denali_phy[8], 0x1 << 16, 1 << 16);
901 clrsetbits_le32(&denali_phy[136], 0x1 << 16, 1 << 16);
902 clrsetbits_le32(&denali_phy[264], 0x1 << 16, 1 << 16);
903 clrsetbits_le32(&denali_phy[392], 0x1 << 16, 1 << 16);
905 for (byte = 0; byte < 4; byte++)
906 clrsetbits_le32(&denali_phy[63 + (128 * byte)], 0xffff << 16,
909 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
910 clrbits_le32(&denali_phy[896], 1);
912 /* CTL_200 ctrlupd_req 1bit offset_8 */
913 clrsetbits_le32(&denali_ctl[200], 0x1 << 8, 0x1 << 8);
916 static int data_training_ca(const struct chan_info *chan, u32 channel,
917 const struct rk3399_sdram_params *params)
919 u32 *denali_pi = chan->pi->denali_pi;
920 u32 *denali_phy = chan->publ->denali_phy;
922 u32 obs_0, obs_1, obs_2, obs_err = 0;
923 u32 rank = params->ch[channel].cap_info.rank;
926 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
927 writel(0x00003f7c, (&denali_pi[175]));
929 if (params->base.dramtype == LPDDR4)
930 rank_mask = (rank == 1) ? 0x5 : 0xf;
932 rank_mask = (rank == 1) ? 0x1 : 0x3;
934 for (i = 0; i < 4; i++) {
935 if (!(rank_mask & (1 << i)))
938 select_per_cs_training_index(chan, i);
940 /* PI_100 PI_CALVL_EN:RW:8:2 */
941 clrsetbits_le32(&denali_pi[100], 0x3 << 8, 0x2 << 8);
943 /* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */
944 clrsetbits_le32(&denali_pi[92],
945 (0x1 << 16) | (0x3 << 24),
946 (0x1 << 16) | (i << 24));
948 /* Waiting for training complete */
950 /* PI_174 PI_INT_STATUS:RD:8:18 */
951 tmp = readl(&denali_pi[174]) >> 8;
954 * PHY_532/660/789 phy_adr_calvl_obs1_:0:32
956 obs_0 = readl(&denali_phy[532]);
957 obs_1 = readl(&denali_phy[660]);
958 obs_2 = readl(&denali_phy[788]);
959 if (((obs_0 >> 30) & 0x3) ||
960 ((obs_1 >> 30) & 0x3) ||
961 ((obs_2 >> 30) & 0x3))
963 if ((((tmp >> 11) & 0x1) == 0x1) &&
964 (((tmp >> 13) & 0x1) == 0x1) &&
965 (((tmp >> 5) & 0x1) == 0x0) &&
968 else if ((((tmp >> 5) & 0x1) == 0x1) ||
973 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
974 writel(0x00003f7c, (&denali_pi[175]));
977 clrbits_le32(&denali_pi[100], 0x3 << 8);
982 static int data_training_wl(const struct chan_info *chan, u32 channel,
983 const struct rk3399_sdram_params *params)
985 u32 *denali_pi = chan->pi->denali_pi;
986 u32 *denali_phy = chan->publ->denali_phy;
988 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
989 u32 rank = params->ch[channel].cap_info.rank;
991 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
992 writel(0x00003f7c, (&denali_pi[175]));
994 for (i = 0; i < rank; i++) {
995 select_per_cs_training_index(chan, i);
997 /* PI_60 PI_WRLVL_EN:RW:8:2 */
998 clrsetbits_le32(&denali_pi[60], 0x3 << 8, 0x2 << 8);
1000 /* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */
1001 clrsetbits_le32(&denali_pi[59],
1002 (0x1 << 8) | (0x3 << 16),
1003 (0x1 << 8) | (i << 16));
1005 /* Waiting for training complete */
1007 /* PI_174 PI_INT_STATUS:RD:8:18 */
1008 tmp = readl(&denali_pi[174]) >> 8;
1011 * check status obs, if error maybe can not
1012 * get leveling done PHY_40/168/296/424
1013 * phy_wrlvl_status_obs_X:0:13
1015 obs_0 = readl(&denali_phy[40]);
1016 obs_1 = readl(&denali_phy[168]);
1017 obs_2 = readl(&denali_phy[296]);
1018 obs_3 = readl(&denali_phy[424]);
1019 if (((obs_0 >> 12) & 0x1) ||
1020 ((obs_1 >> 12) & 0x1) ||
1021 ((obs_2 >> 12) & 0x1) ||
1022 ((obs_3 >> 12) & 0x1))
1024 if ((((tmp >> 10) & 0x1) == 0x1) &&
1025 (((tmp >> 13) & 0x1) == 0x1) &&
1026 (((tmp >> 4) & 0x1) == 0x0) &&
1029 else if ((((tmp >> 4) & 0x1) == 0x1) ||
1034 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1035 writel(0x00003f7c, (&denali_pi[175]));
1038 override_write_leveling_value(chan);
1039 clrbits_le32(&denali_pi[60], 0x3 << 8);
1044 static int data_training_rg(const struct chan_info *chan, u32 channel,
1045 const struct rk3399_sdram_params *params)
1047 u32 *denali_pi = chan->pi->denali_pi;
1048 u32 *denali_phy = chan->publ->denali_phy;
1050 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
1051 u32 rank = params->ch[channel].cap_info.rank;
1053 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1054 writel(0x00003f7c, (&denali_pi[175]));
1056 for (i = 0; i < rank; i++) {
1057 select_per_cs_training_index(chan, i);
1059 /* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */
1060 clrsetbits_le32(&denali_pi[80], 0x3 << 24, 0x2 << 24);
1063 * PI_74 PI_RDLVL_GATE_REQ:WR:16:1
1064 * PI_RDLVL_CS:RW:24:2
1066 clrsetbits_le32(&denali_pi[74],
1067 (0x1 << 16) | (0x3 << 24),
1068 (0x1 << 16) | (i << 24));
1070 /* Waiting for training complete */
1072 /* PI_174 PI_INT_STATUS:RD:8:18 */
1073 tmp = readl(&denali_pi[174]) >> 8;
1077 * PHY_43/171/299/427
1078 * PHY_GTLVL_STATUS_OBS_x:16:8
1080 obs_0 = readl(&denali_phy[43]);
1081 obs_1 = readl(&denali_phy[171]);
1082 obs_2 = readl(&denali_phy[299]);
1083 obs_3 = readl(&denali_phy[427]);
1084 if (((obs_0 >> (16 + 6)) & 0x3) ||
1085 ((obs_1 >> (16 + 6)) & 0x3) ||
1086 ((obs_2 >> (16 + 6)) & 0x3) ||
1087 ((obs_3 >> (16 + 6)) & 0x3))
1089 if ((((tmp >> 9) & 0x1) == 0x1) &&
1090 (((tmp >> 13) & 0x1) == 0x1) &&
1091 (((tmp >> 3) & 0x1) == 0x0) &&
1094 else if ((((tmp >> 3) & 0x1) == 0x1) ||
1099 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1100 writel(0x00003f7c, (&denali_pi[175]));
1103 clrbits_le32(&denali_pi[80], 0x3 << 24);
1108 static int data_training_rl(const struct chan_info *chan, u32 channel,
1109 const struct rk3399_sdram_params *params)
1111 u32 *denali_pi = chan->pi->denali_pi;
1113 u32 rank = params->ch[channel].cap_info.rank;
1115 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1116 writel(0x00003f7c, (&denali_pi[175]));
1118 for (i = 0; i < rank; i++) {
1119 select_per_cs_training_index(chan, i);
1121 /* PI_80 PI_RDLVL_EN:RW:16:2 */
1122 clrsetbits_le32(&denali_pi[80], 0x3 << 16, 0x2 << 16);
1124 /* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */
1125 clrsetbits_le32(&denali_pi[74],
1126 (0x1 << 8) | (0x3 << 24),
1127 (0x1 << 8) | (i << 24));
1129 /* Waiting for training complete */
1131 /* PI_174 PI_INT_STATUS:RD:8:18 */
1132 tmp = readl(&denali_pi[174]) >> 8;
1135 * make sure status obs not report error bit
1136 * PHY_46/174/302/430
1137 * phy_rdlvl_status_obs_X:16:8
1139 if ((((tmp >> 8) & 0x1) == 0x1) &&
1140 (((tmp >> 13) & 0x1) == 0x1) &&
1141 (((tmp >> 2) & 0x1) == 0x0))
1143 else if (((tmp >> 2) & 0x1) == 0x1)
1147 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1148 writel(0x00003f7c, (&denali_pi[175]));
1151 clrbits_le32(&denali_pi[80], 0x3 << 16);
1156 static int data_training_wdql(const struct chan_info *chan, u32 channel,
1157 const struct rk3399_sdram_params *params)
1159 u32 *denali_pi = chan->pi->denali_pi;
1161 u32 rank = params->ch[channel].cap_info.rank;
1164 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1165 writel(0x00003f7c, (&denali_pi[175]));
1167 if (params->base.dramtype == LPDDR4)
1168 rank_mask = (rank == 1) ? 0x5 : 0xf;
1170 rank_mask = (rank == 1) ? 0x1 : 0x3;
1172 for (i = 0; i < 4; i++) {
1173 if (!(rank_mask & (1 << i)))
1176 select_per_cs_training_index(chan, i);
1179 * disable PI_WDQLVL_VREF_EN before wdq leveling?
1180 * PI_181 PI_WDQLVL_VREF_EN:RW:8:1
1182 clrbits_le32(&denali_pi[181], 0x1 << 8);
1184 /* PI_124 PI_WDQLVL_EN:RW:16:2 */
1185 clrsetbits_le32(&denali_pi[124], 0x3 << 16, 0x2 << 16);
1187 /* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */
1188 clrsetbits_le32(&denali_pi[121],
1189 (0x1 << 8) | (0x3 << 16),
1190 (0x1 << 8) | (i << 16));
1192 /* Waiting for training complete */
1194 /* PI_174 PI_INT_STATUS:RD:8:18 */
1195 tmp = readl(&denali_pi[174]) >> 8;
1196 if ((((tmp >> 12) & 0x1) == 0x1) &&
1197 (((tmp >> 13) & 0x1) == 0x1) &&
1198 (((tmp >> 6) & 0x1) == 0x0))
1200 else if (((tmp >> 6) & 0x1) == 0x1)
1204 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1205 writel(0x00003f7c, (&denali_pi[175]));
1208 clrbits_le32(&denali_pi[124], 0x3 << 16);
1213 static int data_training(const struct chan_info *chan, u32 channel,
1214 const struct rk3399_sdram_params *params,
1217 u32 *denali_phy = chan->publ->denali_phy;
1220 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1221 setbits_le32(&denali_phy[927], (1 << 22));
1223 if (training_flag == PI_FULL_TRAINING) {
1224 if (params->base.dramtype == LPDDR4) {
1225 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1226 PI_READ_GATE_TRAINING |
1227 PI_READ_LEVELING | PI_WDQ_LEVELING;
1228 } else if (params->base.dramtype == LPDDR3) {
1229 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1230 PI_READ_GATE_TRAINING;
1231 } else if (params->base.dramtype == DDR3) {
1232 training_flag = PI_WRITE_LEVELING |
1233 PI_READ_GATE_TRAINING |
1238 /* ca training(LPDDR4,LPDDR3 support) */
1239 if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING) {
1240 ret = data_training_ca(chan, channel, params);
1242 debug("%s: data training ca failed\n", __func__);
1247 /* write leveling(LPDDR4,LPDDR3,DDR3 support) */
1248 if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING) {
1249 ret = data_training_wl(chan, channel, params);
1251 debug("%s: data training wl failed\n", __func__);
1256 /* read gate training(LPDDR4,LPDDR3,DDR3 support) */
1257 if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING) {
1258 ret = data_training_rg(chan, channel, params);
1260 debug("%s: data training rg failed\n", __func__);
1265 /* read leveling(LPDDR4,LPDDR3,DDR3 support) */
1266 if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING) {
1267 ret = data_training_rl(chan, channel, params);
1269 debug("%s: data training rl failed\n", __func__);
1274 /* wdq leveling(LPDDR4 support) */
1275 if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING) {
1276 ret = data_training_wdql(chan, channel, params);
1278 debug("%s: data training wdql failed\n", __func__);
1283 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1284 clrbits_le32(&denali_phy[927], (1 << 22));
1289 static void set_ddrconfig(const struct chan_info *chan,
1290 const struct rk3399_sdram_params *params,
1291 unsigned char channel, u32 ddrconfig)
1293 /* only need to set ddrconfig */
1294 struct rk3399_msch_regs *ddr_msch_regs = chan->msch;
1295 unsigned int cs0_cap = 0;
1296 unsigned int cs1_cap = 0;
1298 cs0_cap = (1 << (params->ch[channel].cap_info.cs0_row
1299 + params->ch[channel].cap_info.col
1300 + params->ch[channel].cap_info.bk
1301 + params->ch[channel].cap_info.bw - 20));
1302 if (params->ch[channel].cap_info.rank > 1)
1303 cs1_cap = cs0_cap >> (params->ch[channel].cap_info.cs0_row
1304 - params->ch[channel].cap_info.cs1_row);
1305 if (params->ch[channel].cap_info.row_3_4) {
1306 cs0_cap = cs0_cap * 3 / 4;
1307 cs1_cap = cs1_cap * 3 / 4;
1310 writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf);
1311 writel(((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8),
1312 &ddr_msch_regs->ddrsize);
1315 static void dram_all_config(struct dram_info *dram,
1316 const struct rk3399_sdram_params *params)
1320 unsigned int channel, idx;
1322 sys_reg2 |= SYS_REG_ENC_DDRTYPE(params->base.dramtype);
1323 sys_reg2 |= SYS_REG_ENC_NUM_CH(params->base.num_channels);
1325 for (channel = 0, idx = 0;
1326 (idx < params->base.num_channels) && (channel < 2);
1328 const struct rk3399_sdram_channel *info = ¶ms->ch[channel];
1329 struct rk3399_msch_regs *ddr_msch_regs;
1330 const struct rk3399_msch_timings *noc_timing;
1332 if (params->ch[channel].cap_info.col == 0)
1335 sys_reg2 |= SYS_REG_ENC_ROW_3_4(info->cap_info.row_3_4, channel);
1336 sys_reg2 |= SYS_REG_ENC_CHINFO(channel);
1337 sys_reg2 |= SYS_REG_ENC_RANK(info->cap_info.rank, channel);
1338 sys_reg2 |= SYS_REG_ENC_COL(info->cap_info.col, channel);
1339 sys_reg2 |= SYS_REG_ENC_BK(info->cap_info.bk, channel);
1340 sys_reg2 |= SYS_REG_ENC_BW(info->cap_info.bw, channel);
1341 sys_reg2 |= SYS_REG_ENC_DBW(info->cap_info.dbw, channel);
1342 SYS_REG_ENC_CS0_ROW(info->cap_info.cs0_row, sys_reg2, sys_reg3, channel);
1343 if (info->cap_info.cs1_row)
1344 SYS_REG_ENC_CS1_ROW(info->cap_info.cs1_row, sys_reg2,
1346 sys_reg3 |= SYS_REG_ENC_CS1_COL(info->cap_info.col, channel);
1347 sys_reg3 |= SYS_REG_ENC_VERSION(DDR_SYS_REG_VERSION);
1349 ddr_msch_regs = dram->chan[channel].msch;
1350 noc_timing = ¶ms->ch[channel].noc_timings;
1351 writel(noc_timing->ddrtiminga0,
1352 &ddr_msch_regs->ddrtiminga0);
1353 writel(noc_timing->ddrtimingb0,
1354 &ddr_msch_regs->ddrtimingb0);
1355 writel(noc_timing->ddrtimingc0.d32,
1356 &ddr_msch_regs->ddrtimingc0);
1357 writel(noc_timing->devtodev0,
1358 &ddr_msch_regs->devtodev0);
1359 writel(noc_timing->ddrmode.d32,
1360 &ddr_msch_regs->ddrmode);
1363 * rank 1 memory clock disable (dfi_dram_clk_disable = 1)
1365 * The hardware for LPDDR4 with
1366 * - CLK0P/N connect to lower 16-bits
1367 * - CLK1P/N connect to higher 16-bits
1369 * dfi dram clk is configured via CLK1P/N, so disabling
1370 * dfi dram clk will disable the CLK1P/N as well for lpddr4.
1372 if (params->ch[channel].cap_info.rank == 1 &&
1373 params->base.dramtype != LPDDR4)
1374 setbits_le32(&dram->chan[channel].pctl->denali_ctl[276],
1378 writel(sys_reg2, &dram->pmugrf->os_reg2);
1379 writel(sys_reg3, &dram->pmugrf->os_reg3);
1380 rk_clrsetreg(&dram->pmusgrf->soc_con4, 0x1f << 10,
1381 params->base.stride << 10);
1383 /* reboot hold register set */
1384 writel(PRESET_SGRF_HOLD(0) | PRESET_GPIO0_HOLD(1) |
1385 PRESET_GPIO1_HOLD(1),
1386 &dram->pmucru->pmucru_rstnhold_con[1]);
1387 clrsetbits_le32(&dram->cru->glb_rst_con, 0x3, 0x3);
1390 static int switch_to_phy_index1(struct dram_info *dram,
1391 const struct rk3399_sdram_params *params)
1395 u32 ch_count = params->base.num_channels;
1399 writel(RK_CLRSETBITS(0x03 << 4 | 1 << 2 | 1,
1400 1 << 4 | 1 << 2 | 1),
1401 &dram->cic->cic_ctrl0);
1402 while (!(readl(&dram->cic->cic_status0) & (1 << 2))) {
1406 debug("index1 frequency change overtime\n");
1412 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &dram->cic->cic_ctrl0);
1413 while (!(readl(&dram->cic->cic_status0) & (1 << 0))) {
1417 debug("index1 frequency done overtime\n");
1422 for (channel = 0; channel < ch_count; channel++) {
1423 denali_phy = dram->chan[channel].publ->denali_phy;
1424 clrsetbits_le32(&denali_phy[896], (0x3 << 8) | 1, 1 << 8);
1425 ret = data_training(&dram->chan[channel], channel,
1426 params, PI_FULL_TRAINING);
1428 debug("index1 training failed\n");
1436 static unsigned char calculate_stride(struct rk3399_sdram_params *params)
1438 unsigned int stride = params->base.stride;
1439 unsigned int channel, chinfo = 0;
1440 unsigned int ch_cap[2] = {0, 0};
1443 for (channel = 0; channel < 2; channel++) {
1444 unsigned int cs0_cap = 0;
1445 unsigned int cs1_cap = 0;
1446 struct sdram_cap_info *cap_info = ¶ms->ch[channel].cap_info;
1448 if (cap_info->col == 0)
1451 cs0_cap = (1 << (cap_info->cs0_row + cap_info->col +
1452 cap_info->bk + cap_info->bw - 20));
1453 if (cap_info->rank > 1)
1454 cs1_cap = cs0_cap >> (cap_info->cs0_row
1455 - cap_info->cs1_row);
1456 if (cap_info->row_3_4) {
1457 cs0_cap = cs0_cap * 3 / 4;
1458 cs1_cap = cs1_cap * 3 / 4;
1460 ch_cap[channel] = cs0_cap + cs1_cap;
1461 chinfo |= 1 << channel;
1464 /* stride calculation for 1 channel */
1465 if (params->base.num_channels == 1 && chinfo & 1)
1466 return 0x17; /* channel a */
1468 /* stride calculation for 2 channels, default gstride type is 256B */
1469 if (ch_cap[0] == ch_cap[1]) {
1470 cap = ch_cap[0] + ch_cap[1];
1481 * 768MB + 768MB same as total 2GB memory
1482 * useful space: 0-768MB 1GB-1792MB
1489 /* 1536MB + 1536MB */
1498 printf("%s: Unable to calculate stride for ", __func__);
1499 print_size((cap * (1 << 20)), " capacity\n");
1504 sdram_print_stride(stride);
1509 static void clear_channel_params(struct rk3399_sdram_params *params, u8 channel)
1511 params->ch[channel].cap_info.rank = 0;
1512 params->ch[channel].cap_info.col = 0;
1513 params->ch[channel].cap_info.bk = 0;
1514 params->ch[channel].cap_info.bw = 32;
1515 params->ch[channel].cap_info.dbw = 32;
1516 params->ch[channel].cap_info.row_3_4 = 0;
1517 params->ch[channel].cap_info.cs0_row = 0;
1518 params->ch[channel].cap_info.cs1_row = 0;
1519 params->ch[channel].cap_info.ddrconfig = 0;
1522 static int pctl_init(struct dram_info *dram, struct rk3399_sdram_params *params)
1527 for (channel = 0; channel < 2; channel++) {
1528 const struct chan_info *chan = &dram->chan[channel];
1529 struct rk3399_cru *cru = dram->cru;
1530 struct rk3399_ddr_publ_regs *publ = chan->publ;
1532 phy_pctrl_reset(cru, channel);
1533 phy_dll_bypass_set(publ, params->base.ddr_freq);
1535 ret = pctl_cfg(dram, chan, channel, params);
1537 printf("%s: pctl config failed\n", __func__);
1541 /* start to trigger initialization */
1542 pctl_start(dram, channel);
1548 static int sdram_init(struct dram_info *dram,
1549 struct rk3399_sdram_params *params)
1551 unsigned char dramtype = params->base.dramtype;
1552 unsigned int ddr_freq = params->base.ddr_freq;
1553 u32 training_flag = PI_READ_GATE_TRAINING;
1554 int channel, ch, rank;
1557 debug("Starting SDRAM initialization...\n");
1559 if ((dramtype == DDR3 && ddr_freq > 933) ||
1560 (dramtype == LPDDR3 && ddr_freq > 933) ||
1561 (dramtype == LPDDR4 && ddr_freq > 800)) {
1562 debug("SDRAM frequency is to high!");
1566 for (ch = 0; ch < 2; ch++) {
1567 params->ch[ch].cap_info.rank = 2;
1568 for (rank = 2; rank != 0; rank--) {
1569 ret = pctl_init(dram, params);
1571 printf("%s: pctl init failed\n", __func__);
1575 /* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */
1576 if (dramtype == LPDDR3)
1579 params->ch[ch].cap_info.rank = rank;
1582 * LPDDR3 CA training msut be trigger before
1584 * DDR3 is not have CA training.
1586 if (params->base.dramtype == LPDDR3)
1587 training_flag |= PI_CA_TRAINING;
1589 if (!(data_training(&dram->chan[ch], ch,
1590 params, training_flag)))
1593 /* Computed rank with associated channel number */
1594 params->ch[ch].cap_info.rank = rank;
1597 params->base.num_channels = 0;
1598 for (channel = 0; channel < 2; channel++) {
1599 const struct chan_info *chan = &dram->chan[channel];
1600 struct sdram_cap_info *cap_info = ¶ms->ch[channel].cap_info;
1601 u8 training_flag = PI_FULL_TRAINING;
1603 if (cap_info->rank == 0) {
1604 clear_channel_params(params, channel);
1607 params->base.num_channels++;
1611 debug(channel ? "1: " : "0: ");
1613 /* LPDDR3 should have write and read gate training */
1614 if (params->base.dramtype == LPDDR3)
1615 training_flag = PI_WRITE_LEVELING |
1616 PI_READ_GATE_TRAINING;
1618 if (params->base.dramtype != LPDDR4) {
1619 ret = data_training(dram, channel, params,
1622 debug("%s: data train failed for channel %d\n",
1628 sdram_print_ddr_info(cap_info, ¶ms->base);
1630 set_ddrconfig(chan, params, channel, cap_info->ddrconfig);
1633 if (params->base.num_channels == 0) {
1634 printf("%s: ", __func__);
1635 sdram_print_dram_type(params->base.dramtype);
1636 printf(" - %dMHz failed!\n", params->base.ddr_freq);
1640 params->base.stride = calculate_stride(params);
1641 dram_all_config(dram, params);
1642 switch_to_phy_index1(dram, params);
1644 debug("Finish SDRAM initialization...\n");
1648 static int rk3399_dmc_ofdata_to_platdata(struct udevice *dev)
1650 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1651 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1654 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
1655 (u32 *)&plat->sdram_params,
1656 sizeof(plat->sdram_params) / sizeof(u32));
1658 printf("%s: Cannot read rockchip,sdram-params %d\n",
1662 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
1664 printf("%s: regmap failed %d\n", __func__, ret);
1670 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1671 static int conv_of_platdata(struct udevice *dev)
1673 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1674 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1677 ret = regmap_init_mem_platdata(dev, dtplat->reg,
1678 ARRAY_SIZE(dtplat->reg) / 2,
1687 static int rk3399_dmc_init(struct udevice *dev)
1689 struct dram_info *priv = dev_get_priv(dev);
1690 struct rockchip_dmc_plat *plat = dev_get_platdata(dev);
1692 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1693 struct rk3399_sdram_params *params = &plat->sdram_params;
1695 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
1696 struct rk3399_sdram_params *params =
1697 (void *)dtplat->rockchip_sdram_params;
1699 ret = conv_of_platdata(dev);
1704 priv->cic = syscon_get_first_range(ROCKCHIP_SYSCON_CIC);
1705 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
1706 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
1707 priv->pmusgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
1708 priv->pmucru = rockchip_get_pmucru();
1709 priv->cru = rockchip_get_cru();
1710 priv->chan[0].pctl = regmap_get_range(plat->map, 0);
1711 priv->chan[0].pi = regmap_get_range(plat->map, 1);
1712 priv->chan[0].publ = regmap_get_range(plat->map, 2);
1713 priv->chan[0].msch = regmap_get_range(plat->map, 3);
1714 priv->chan[1].pctl = regmap_get_range(plat->map, 4);
1715 priv->chan[1].pi = regmap_get_range(plat->map, 5);
1716 priv->chan[1].publ = regmap_get_range(plat->map, 6);
1717 priv->chan[1].msch = regmap_get_range(plat->map, 7);
1719 debug("con reg %p %p %p %p %p %p %p %p\n",
1720 priv->chan[0].pctl, priv->chan[0].pi,
1721 priv->chan[0].publ, priv->chan[0].msch,
1722 priv->chan[1].pctl, priv->chan[1].pi,
1723 priv->chan[1].publ, priv->chan[1].msch);
1724 debug("cru %p, cic %p, grf %p, sgrf %p, pmucru %p\n", priv->cru,
1725 priv->cic, priv->pmugrf, priv->pmusgrf, priv->pmucru);
1727 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1728 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->ddr_clk);
1730 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
1733 printf("%s clk get failed %d\n", __func__, ret);
1737 ret = clk_set_rate(&priv->ddr_clk, params->base.ddr_freq * MHz);
1739 printf("%s clk set failed %d\n", __func__, ret);
1743 ret = sdram_init(priv, params);
1745 printf("%s DRAM init failed %d\n", __func__, ret);
1753 static int rk3399_dmc_probe(struct udevice *dev)
1755 #if defined(CONFIG_TPL_BUILD) || \
1756 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
1757 if (rk3399_dmc_init(dev))
1760 struct dram_info *priv = dev_get_priv(dev);
1762 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
1763 debug("%s: pmugrf = %p\n", __func__, priv->pmugrf);
1764 priv->info.base = CONFIG_SYS_SDRAM_BASE;
1766 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg2);
1771 static int rk3399_dmc_get_info(struct udevice *dev, struct ram_info *info)
1773 struct dram_info *priv = dev_get_priv(dev);
1780 static struct ram_ops rk3399_dmc_ops = {
1781 .get_info = rk3399_dmc_get_info,
1784 static const struct udevice_id rk3399_dmc_ids[] = {
1785 { .compatible = "rockchip,rk3399-dmc" },
1789 U_BOOT_DRIVER(dmc_rk3399) = {
1790 .name = "rockchip_rk3399_dmc",
1792 .of_match = rk3399_dmc_ids,
1793 .ops = &rk3399_dmc_ops,
1794 #if defined(CONFIG_TPL_BUILD) || \
1795 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
1796 .ofdata_to_platdata = rk3399_dmc_ofdata_to_platdata,
1798 .probe = rk3399_dmc_probe,
1799 .priv_auto_alloc_size = sizeof(struct dram_info),
1800 #if defined(CONFIG_TPL_BUILD) || \
1801 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
1802 .platdata_auto_alloc_size = sizeof(struct rockchip_dmc_plat),