1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2018 Rockchip Electronics Co., Ltd.
9 #include <asm/arch-rockchip/sdram.h>
10 #include <asm/arch-rockchip/sdram_pctl_px30.h>
11 #include <linux/delay.h>
17 void pctl_read_mr(void __iomem *pctl_base, u32 rank, u32 mr_num)
19 writel((rank << 4) | (1 << 0), pctl_base + DDR_PCTL2_MRCTRL0);
20 writel((mr_num << 8), pctl_base + DDR_PCTL2_MRCTRL1);
21 setbits_le32(pctl_base + DDR_PCTL2_MRCTRL0, 1u << 31);
22 while (readl(pctl_base + DDR_PCTL2_MRCTRL0) & (1u << 31))
24 while (readl(pctl_base + DDR_PCTL2_MRSTAT) & MR_WR_BUSY)
31 * note: be careful of keep mr original val
33 int pctl_write_mr(void __iomem *pctl_base, u32 rank, u32 mr_num, u32 arg,
36 while (readl(pctl_base + DDR_PCTL2_MRSTAT) & MR_WR_BUSY)
38 if (dramtype == DDR3 || dramtype == DDR4) {
39 writel((mr_num << 12) | (rank << 4) | (0 << 0),
40 pctl_base + DDR_PCTL2_MRCTRL0);
41 writel(arg, pctl_base + DDR_PCTL2_MRCTRL1);
43 writel((rank << 4) | (0 << 0),
44 pctl_base + DDR_PCTL2_MRCTRL0);
45 writel((mr_num << 8) | (arg & 0xff),
46 pctl_base + DDR_PCTL2_MRCTRL1);
49 setbits_le32(pctl_base + DDR_PCTL2_MRCTRL0, 1u << 31);
50 while (readl(pctl_base + DDR_PCTL2_MRCTRL0) & (1u << 31))
52 while (readl(pctl_base + DDR_PCTL2_MRSTAT) & MR_WR_BUSY)
59 * rank : 1:cs0, 2:cs1, 3:cs0&cs1
60 * vrefrate: 4500: 45%,
62 int pctl_write_vrefdq(void __iomem *pctl_base, u32 rank, u32 vrefrate,
68 if (dramtype != DDR4 || vrefrate < 4500 ||
72 tccd_l = (readl(pctl_base + DDR_PCTL2_DRAMTMG4) >> 16) & 0xf;
73 tccd_l = (tccd_l - 4) << 10;
75 if (vrefrate > 7500) {
77 value = ((vrefrate - 6000) / 65) | tccd_l;
80 value = ((vrefrate - 4500) / 65) | tccd_l | (1 << 6);
83 dis_auto_zq = pctl_dis_zqcs_aref(pctl_base);
85 /* enable vrefdq calibratin */
86 pctl_write_mr(pctl_base, rank, 6, value | (1 << 7), dramtype);
87 udelay(1);/* tvrefdqe */
88 /* write vrefdq value */
89 pctl_write_mr(pctl_base, rank, 6, value | (1 << 7), dramtype);
90 udelay(1);/* tvref_time */
91 pctl_write_mr(pctl_base, rank, 6, value | (0 << 7), dramtype);
92 udelay(1);/* tvrefdqx */
94 pctl_rest_zqcs_aref(pctl_base, dis_auto_zq);
99 static int upctl2_update_ref_reg(void __iomem *pctl_base)
103 ret = readl(pctl_base + DDR_PCTL2_RFSHCTL3) ^ (1 << 1);
104 writel(ret, pctl_base + DDR_PCTL2_RFSHCTL3);
109 u32 pctl_dis_zqcs_aref(void __iomem *pctl_base)
114 if (!(readl(pctl_base + DDR_PCTL2_ZQCTL0) &
117 setbits_le32(pctl_base + DDR_PCTL2_ZQCTL0, 1 << 31);
120 /* disable auto refresh */
121 setbits_le32(pctl_base + DDR_PCTL2_RFSHCTL3, 1);
123 upctl2_update_ref_reg(pctl_base);
128 void pctl_rest_zqcs_aref(void __iomem *pctl_base, u32 dis_auto_zq)
132 clrbits_le32(pctl_base + DDR_PCTL2_ZQCTL0, 1 << 31);
134 /* restore auto refresh */
135 clrbits_le32(pctl_base + DDR_PCTL2_RFSHCTL3, 1);
137 upctl2_update_ref_reg(pctl_base);
140 u32 pctl_remodify_sdram_params(struct ddr_pctl_regs *pctl_regs,
141 struct sdram_cap_info *cap_info,
144 u32 tmp = 0, tmp_adr = 0, i;
146 for (i = 0; pctl_regs->pctl[i][0] != 0xFFFFFFFF; i++) {
147 if (pctl_regs->pctl[i][0] == 0) {
148 tmp = pctl_regs->pctl[i][1];/* MSTR */
153 tmp &= ~((3ul << 30) | (3ul << 24) | (3ul << 12));
155 switch (cap_info->dbw) {
169 * If DDR3 or DDR4 MSTR.active_ranks=1,
170 * it will gate memory clock when enter power down.
171 * Force set active_ranks to 3 to workaround it.
173 if (cap_info->rank == 2 || dram_type == DDR3 ||
179 tmp |= (2 - cap_info->bw) << 12;
181 pctl_regs->pctl[tmp_adr][1] = tmp;
186 int pctl_cfg(void __iomem *pctl_base, struct ddr_pctl_regs *pctl_regs,
187 u32 sr_idle, u32 pd_idle)
191 for (i = 0; pctl_regs->pctl[i][0] != 0xFFFFFFFF; i++) {
192 writel(pctl_regs->pctl[i][1],
193 pctl_base + pctl_regs->pctl[i][0]);
195 clrsetbits_le32(pctl_base + DDR_PCTL2_PWRTMG,
197 ((sr_idle & 0xff) << 16) | (pd_idle & 0x1f));
199 clrsetbits_le32(pctl_base + DDR_PCTL2_HWLPCTL,
203 setbits_le32(pctl_base + DDR_PCTL2_ZQCTL0, 1u << 31);