Kever Yang | 6fc9ebf | 2018-12-20 11:33:42 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016-2017 Rockchip Inc. |
| 4 | * |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 5 | * Adapted from coreboot. |
| 6 | */ |
Philipp Tomsich | c69b309 | 2017-05-31 18:16:34 +0200 | [diff] [blame] | 7 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <clk.h> |
| 10 | #include <dm.h> |
| 11 | #include <dt-structs.h> |
| 12 | #include <ram.h> |
| 13 | #include <regmap.h> |
| 14 | #include <syscon.h> |
| 15 | #include <asm/io.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 16 | #include <asm/arch-rockchip/clock.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 17 | #include <asm/arch-rockchip/cru_rk3399.h> |
| 18 | #include <asm/arch-rockchip/grf_rk3399.h> |
| 19 | #include <asm/arch-rockchip/hardware.h> |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 20 | #include <asm/arch-rockchip/sdram_common.h> |
| 21 | #include <asm/arch-rockchip/sdram_rk3399.h> |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 22 | #include <linux/err.h> |
Philipp Tomsich | c69b309 | 2017-05-31 18:16:34 +0200 | [diff] [blame] | 23 | #include <time.h> |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 24 | |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 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)) |
| 28 | |
| 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 |
| 37 | |
Jagan Teki | 5d15217 | 2019-07-16 17:27:15 +0530 | [diff] [blame] | 38 | #define PHY_BOOSTP_EN 0x1 |
| 39 | #define PHY_BOOSTN_EN 0x1 |
Jagan Teki | d868184 | 2019-07-16 17:27:16 +0530 | [diff] [blame] | 40 | #define PHY_SLEWP_EN 0x1 |
| 41 | #define PHY_SLEWN_EN 0x1 |
Jagan Teki | 65535a2 | 2019-07-16 17:27:17 +0530 | [diff] [blame] | 42 | #define PHY_RX_CM_INPUT 0x1 |
Jagan Teki | 5d15217 | 2019-07-16 17:27:15 +0530 | [diff] [blame] | 43 | |
Jagan Teki | ce75cfb | 2019-07-15 23:58:43 +0530 | [diff] [blame] | 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))) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 48 | struct chan_info { |
| 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; |
| 53 | }; |
| 54 | |
| 55 | struct dram_info { |
Kever Yang | 7f34784 | 2019-04-01 17:20:53 +0800 | [diff] [blame] | 56 | #if defined(CONFIG_TPL_BUILD) || \ |
| 57 | (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD)) |
Jagan Teki | c9151e2 | 2019-07-15 23:58:45 +0530 | [diff] [blame] | 58 | u32 pwrup_srefresh_exit[2]; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 59 | struct chan_info chan[2]; |
| 60 | struct clk ddr_clk; |
| 61 | struct rk3399_cru *cru; |
Jagan Teki | c9151e2 | 2019-07-15 23:58:45 +0530 | [diff] [blame] | 62 | struct rk3399_grf_regs *grf; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 63 | struct rk3399_pmucru *pmucru; |
| 64 | struct rk3399_pmusgrf_regs *pmusgrf; |
| 65 | struct rk3399_ddr_cic_regs *cic; |
| 66 | #endif |
| 67 | struct ram_info info; |
| 68 | struct rk3399_pmugrf_regs *pmugrf; |
| 69 | }; |
| 70 | |
Kever Yang | 7f34784 | 2019-04-01 17:20:53 +0800 | [diff] [blame] | 71 | #if defined(CONFIG_TPL_BUILD) || \ |
| 72 | (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD)) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 73 | |
| 74 | struct rockchip_dmc_plat { |
| 75 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 76 | struct dtd_rockchip_rk3399_dmc dtplat; |
| 77 | #else |
| 78 | struct rk3399_sdram_params sdram_params; |
| 79 | #endif |
| 80 | struct regmap *map; |
| 81 | }; |
| 82 | |
Jagan Teki | c9151e2 | 2019-07-15 23:58:45 +0530 | [diff] [blame] | 83 | static void *get_ddrc0_con(struct dram_info *dram, u8 channel) |
| 84 | { |
| 85 | return (channel == 0) ? &dram->grf->ddrc0_con0 : &dram->grf->ddrc0_con1; |
| 86 | } |
| 87 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 88 | static void copy_to_reg(u32 *dest, const u32 *src, u32 n) |
| 89 | { |
| 90 | int i; |
| 91 | |
| 92 | for (i = 0; i < n / sizeof(u32); i++) { |
| 93 | writel(*src, dest); |
| 94 | src++; |
| 95 | dest++; |
| 96 | } |
| 97 | } |
| 98 | |
Jagan Teki | ce75cfb | 2019-07-15 23:58:43 +0530 | [diff] [blame] | 99 | static void rkclk_ddr_reset(struct rk3399_cru *cru, u32 channel, u32 ctl, |
| 100 | u32 phy) |
| 101 | { |
| 102 | channel &= 0x1; |
| 103 | ctl &= 0x1; |
| 104 | phy &= 0x1; |
| 105 | writel(CRU_SFTRST_DDR_CTRL(channel, ctl) | |
| 106 | CRU_SFTRST_DDR_PHY(channel, phy), |
| 107 | &cru->softrst_con[4]); |
| 108 | } |
| 109 | |
| 110 | static void phy_pctrl_reset(struct rk3399_cru *cru, u32 channel) |
| 111 | { |
| 112 | rkclk_ddr_reset(cru, channel, 1, 1); |
| 113 | udelay(10); |
| 114 | |
| 115 | rkclk_ddr_reset(cru, channel, 1, 0); |
| 116 | udelay(10); |
| 117 | |
| 118 | rkclk_ddr_reset(cru, channel, 0, 0); |
| 119 | udelay(10); |
| 120 | } |
| 121 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 122 | static void phy_dll_bypass_set(struct rk3399_ddr_publ_regs *ddr_publ_regs, |
| 123 | u32 freq) |
| 124 | { |
| 125 | u32 *denali_phy = ddr_publ_regs->denali_phy; |
| 126 | |
| 127 | /* From IP spec, only freq small than 125 can enter dll bypass mode */ |
| 128 | if (freq <= 125) { |
| 129 | /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */ |
| 130 | setbits_le32(&denali_phy[86], (0x3 << 2) << 8); |
| 131 | setbits_le32(&denali_phy[214], (0x3 << 2) << 8); |
| 132 | setbits_le32(&denali_phy[342], (0x3 << 2) << 8); |
| 133 | setbits_le32(&denali_phy[470], (0x3 << 2) << 8); |
| 134 | |
| 135 | /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */ |
| 136 | setbits_le32(&denali_phy[547], (0x3 << 2) << 16); |
| 137 | setbits_le32(&denali_phy[675], (0x3 << 2) << 16); |
| 138 | setbits_le32(&denali_phy[803], (0x3 << 2) << 16); |
| 139 | } else { |
| 140 | /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */ |
| 141 | clrbits_le32(&denali_phy[86], (0x3 << 2) << 8); |
| 142 | clrbits_le32(&denali_phy[214], (0x3 << 2) << 8); |
| 143 | clrbits_le32(&denali_phy[342], (0x3 << 2) << 8); |
| 144 | clrbits_le32(&denali_phy[470], (0x3 << 2) << 8); |
| 145 | |
| 146 | /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */ |
| 147 | clrbits_le32(&denali_phy[547], (0x3 << 2) << 16); |
| 148 | clrbits_le32(&denali_phy[675], (0x3 << 2) << 16); |
| 149 | clrbits_le32(&denali_phy[803], (0x3 << 2) << 16); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static void set_memory_map(const struct chan_info *chan, u32 channel, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 154 | const struct rk3399_sdram_params *params) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 155 | { |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 156 | const struct rk3399_sdram_channel *sdram_ch = ¶ms->ch[channel]; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 157 | u32 *denali_ctl = chan->pctl->denali_ctl; |
| 158 | u32 *denali_pi = chan->pi->denali_pi; |
| 159 | u32 cs_map; |
| 160 | u32 reduc; |
| 161 | u32 row; |
| 162 | |
| 163 | /* Get row number from ddrconfig setting */ |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 164 | if (sdram_ch->cap_info.ddrconfig < 2 || |
| 165 | sdram_ch->cap_info.ddrconfig == 4) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 166 | row = 16; |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 167 | else if (sdram_ch->cap_info.ddrconfig == 3) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 168 | row = 14; |
| 169 | else |
| 170 | row = 15; |
| 171 | |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 172 | cs_map = (sdram_ch->cap_info.rank > 1) ? 3 : 1; |
| 173 | reduc = (sdram_ch->cap_info.bw == 2) ? 0 : 1; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 174 | |
| 175 | /* Set the dram configuration to ctrl */ |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 176 | clrsetbits_le32(&denali_ctl[191], 0xF, (12 - sdram_ch->cap_info.col)); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 177 | clrsetbits_le32(&denali_ctl[190], (0x3 << 16) | (0x7 << 24), |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 178 | ((3 - sdram_ch->cap_info.bk) << 16) | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 179 | ((16 - row) << 24)); |
| 180 | |
| 181 | clrsetbits_le32(&denali_ctl[196], 0x3 | (1 << 16), |
| 182 | cs_map | (reduc << 16)); |
| 183 | |
| 184 | /* PI_199 PI_COL_DIFF:RW:0:4 */ |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 185 | clrsetbits_le32(&denali_pi[199], 0xF, (12 - sdram_ch->cap_info.col)); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 186 | |
| 187 | /* PI_155 PI_ROW_DIFF:RW:24:3 PI_BANK_DIFF:RW:16:2 */ |
| 188 | clrsetbits_le32(&denali_pi[155], (0x3 << 16) | (0x7 << 24), |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 189 | ((3 - sdram_ch->cap_info.bk) << 16) | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 190 | ((16 - row) << 24)); |
Jagan Teki | 9337cb3 | 2019-07-16 17:27:18 +0530 | [diff] [blame] | 191 | |
| 192 | if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) { |
| 193 | if (cs_map == 1) |
| 194 | cs_map = 0x5; |
| 195 | else if (cs_map == 2) |
| 196 | cs_map = 0xa; |
| 197 | else |
| 198 | cs_map = 0xF; |
| 199 | } |
| 200 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 201 | /* PI_41 PI_CS_MAP:RW:24:4 */ |
| 202 | clrsetbits_le32(&denali_pi[41], 0xf << 24, cs_map << 24); |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 203 | if (sdram_ch->cap_info.rank == 1 && params->base.dramtype == DDR3) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 204 | writel(0x2EC7FFFF, &denali_pi[34]); |
| 205 | } |
| 206 | |
Jagan Teki | b5d4663 | 2019-07-16 17:27:07 +0530 | [diff] [blame] | 207 | static int phy_io_config(const struct chan_info *chan, |
| 208 | const struct rk3399_sdram_params *params) |
| 209 | { |
| 210 | u32 *denali_phy = chan->publ->denali_phy; |
| 211 | u32 vref_mode_dq, vref_value_dq, vref_mode_ac, vref_value_ac; |
| 212 | u32 mode_sel; |
| 213 | u32 reg_value; |
| 214 | u32 drv_value, odt_value; |
| 215 | u32 speed; |
| 216 | |
| 217 | /* vref setting */ |
| 218 | if (params->base.dramtype == LPDDR4) { |
| 219 | /* LPDDR4 */ |
| 220 | vref_mode_dq = 0x6; |
| 221 | vref_value_dq = 0x1f; |
| 222 | vref_mode_ac = 0x6; |
| 223 | vref_value_ac = 0x1f; |
Jagan Teki | 213b9ba | 2019-07-16 17:27:11 +0530 | [diff] [blame] | 224 | mode_sel = 0x6; |
Jagan Teki | b5d4663 | 2019-07-16 17:27:07 +0530 | [diff] [blame] | 225 | } else if (params->base.dramtype == LPDDR3) { |
| 226 | if (params->base.odt == 1) { |
| 227 | vref_mode_dq = 0x5; /* LPDDR3 ODT */ |
| 228 | drv_value = (readl(&denali_phy[6]) >> 12) & 0xf; |
| 229 | odt_value = (readl(&denali_phy[6]) >> 4) & 0xf; |
| 230 | if (drv_value == PHY_DRV_ODT_48) { |
| 231 | switch (odt_value) { |
| 232 | case PHY_DRV_ODT_240: |
| 233 | vref_value_dq = 0x16; |
| 234 | break; |
| 235 | case PHY_DRV_ODT_120: |
| 236 | vref_value_dq = 0x26; |
| 237 | break; |
| 238 | case PHY_DRV_ODT_60: |
| 239 | vref_value_dq = 0x36; |
| 240 | break; |
| 241 | default: |
| 242 | debug("Invalid ODT value.\n"); |
| 243 | return -EINVAL; |
| 244 | } |
| 245 | } else if (drv_value == PHY_DRV_ODT_40) { |
| 246 | switch (odt_value) { |
| 247 | case PHY_DRV_ODT_240: |
| 248 | vref_value_dq = 0x19; |
| 249 | break; |
| 250 | case PHY_DRV_ODT_120: |
| 251 | vref_value_dq = 0x23; |
| 252 | break; |
| 253 | case PHY_DRV_ODT_60: |
| 254 | vref_value_dq = 0x31; |
| 255 | break; |
| 256 | default: |
| 257 | debug("Invalid ODT value.\n"); |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | } else if (drv_value == PHY_DRV_ODT_34_3) { |
| 261 | switch (odt_value) { |
| 262 | case PHY_DRV_ODT_240: |
| 263 | vref_value_dq = 0x17; |
| 264 | break; |
| 265 | case PHY_DRV_ODT_120: |
| 266 | vref_value_dq = 0x20; |
| 267 | break; |
| 268 | case PHY_DRV_ODT_60: |
| 269 | vref_value_dq = 0x2e; |
| 270 | break; |
| 271 | default: |
| 272 | debug("Invalid ODT value.\n"); |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | } else { |
| 276 | debug("Invalid DRV value.\n"); |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | } else { |
| 280 | vref_mode_dq = 0x2; /* LPDDR3 */ |
| 281 | vref_value_dq = 0x1f; |
| 282 | } |
| 283 | vref_mode_ac = 0x2; |
| 284 | vref_value_ac = 0x1f; |
Jagan Teki | 213b9ba | 2019-07-16 17:27:11 +0530 | [diff] [blame] | 285 | mode_sel = 0x0; |
Jagan Teki | b5d4663 | 2019-07-16 17:27:07 +0530 | [diff] [blame] | 286 | } else if (params->base.dramtype == DDR3) { |
| 287 | /* DDR3L */ |
| 288 | vref_mode_dq = 0x1; |
| 289 | vref_value_dq = 0x1f; |
| 290 | vref_mode_ac = 0x1; |
| 291 | vref_value_ac = 0x1f; |
Jagan Teki | 213b9ba | 2019-07-16 17:27:11 +0530 | [diff] [blame] | 292 | mode_sel = 0x1; |
Jagan Teki | b5d4663 | 2019-07-16 17:27:07 +0530 | [diff] [blame] | 293 | } else { |
| 294 | debug("Unknown DRAM type.\n"); |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | |
| 298 | reg_value = (vref_mode_dq << 9) | (0x1 << 8) | vref_value_dq; |
| 299 | |
| 300 | /* PHY_913 PHY_PAD_VREF_CTRL_DQ_0 12bits offset_8 */ |
| 301 | clrsetbits_le32(&denali_phy[913], 0xfff << 8, reg_value << 8); |
| 302 | /* PHY_914 PHY_PAD_VREF_CTRL_DQ_1 12bits offset_0 */ |
| 303 | clrsetbits_le32(&denali_phy[914], 0xfff, reg_value); |
| 304 | /* PHY_914 PHY_PAD_VREF_CTRL_DQ_2 12bits offset_16 */ |
| 305 | clrsetbits_le32(&denali_phy[914], 0xfff << 16, reg_value << 16); |
| 306 | /* PHY_915 PHY_PAD_VREF_CTRL_DQ_3 12bits offset_0 */ |
| 307 | clrsetbits_le32(&denali_phy[915], 0xfff, reg_value); |
| 308 | |
| 309 | reg_value = (vref_mode_ac << 9) | (0x1 << 8) | vref_value_ac; |
| 310 | |
| 311 | /* PHY_915 PHY_PAD_VREF_CTRL_AC 12bits offset_16 */ |
| 312 | clrsetbits_le32(&denali_phy[915], 0xfff << 16, reg_value << 16); |
| 313 | |
Jagan Teki | b5d4663 | 2019-07-16 17:27:07 +0530 | [diff] [blame] | 314 | /* PHY_924 PHY_PAD_FDBK_DRIVE */ |
| 315 | clrsetbits_le32(&denali_phy[924], 0x7 << 15, mode_sel << 15); |
| 316 | /* PHY_926 PHY_PAD_DATA_DRIVE */ |
| 317 | clrsetbits_le32(&denali_phy[926], 0x7 << 6, mode_sel << 6); |
| 318 | /* PHY_927 PHY_PAD_DQS_DRIVE */ |
| 319 | clrsetbits_le32(&denali_phy[927], 0x7 << 6, mode_sel << 6); |
| 320 | /* PHY_928 PHY_PAD_ADDR_DRIVE */ |
| 321 | clrsetbits_le32(&denali_phy[928], 0x7 << 14, mode_sel << 14); |
| 322 | /* PHY_929 PHY_PAD_CLK_DRIVE */ |
| 323 | clrsetbits_le32(&denali_phy[929], 0x7 << 14, mode_sel << 14); |
| 324 | /* PHY_935 PHY_PAD_CKE_DRIVE */ |
| 325 | clrsetbits_le32(&denali_phy[935], 0x7 << 14, mode_sel << 14); |
| 326 | /* PHY_937 PHY_PAD_RST_DRIVE */ |
| 327 | clrsetbits_le32(&denali_phy[937], 0x7 << 14, mode_sel << 14); |
| 328 | /* PHY_939 PHY_PAD_CS_DRIVE */ |
| 329 | clrsetbits_le32(&denali_phy[939], 0x7 << 14, mode_sel << 14); |
| 330 | |
Jagan Teki | 5d15217 | 2019-07-16 17:27:15 +0530 | [diff] [blame] | 331 | if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) { |
| 332 | /* BOOSTP_EN & BOOSTN_EN */ |
| 333 | reg_value = ((PHY_BOOSTP_EN << 4) | PHY_BOOSTN_EN); |
| 334 | /* PHY_925 PHY_PAD_FDBK_DRIVE2 */ |
| 335 | clrsetbits_le32(&denali_phy[925], 0xff << 8, reg_value << 8); |
| 336 | /* PHY_926 PHY_PAD_DATA_DRIVE */ |
| 337 | clrsetbits_le32(&denali_phy[926], 0xff << 12, reg_value << 12); |
| 338 | /* PHY_927 PHY_PAD_DQS_DRIVE */ |
| 339 | clrsetbits_le32(&denali_phy[927], 0xff << 14, reg_value << 14); |
| 340 | /* PHY_928 PHY_PAD_ADDR_DRIVE */ |
| 341 | clrsetbits_le32(&denali_phy[928], 0xff << 20, reg_value << 20); |
| 342 | /* PHY_929 PHY_PAD_CLK_DRIVE */ |
| 343 | clrsetbits_le32(&denali_phy[929], 0xff << 22, reg_value << 22); |
| 344 | /* PHY_935 PHY_PAD_CKE_DRIVE */ |
| 345 | clrsetbits_le32(&denali_phy[935], 0xff << 20, reg_value << 20); |
| 346 | /* PHY_937 PHY_PAD_RST_DRIVE */ |
| 347 | clrsetbits_le32(&denali_phy[937], 0xff << 20, reg_value << 20); |
| 348 | /* PHY_939 PHY_PAD_CS_DRIVE */ |
| 349 | clrsetbits_le32(&denali_phy[939], 0xff << 20, reg_value << 20); |
Jagan Teki | d868184 | 2019-07-16 17:27:16 +0530 | [diff] [blame] | 350 | |
| 351 | /* SLEWP_EN & SLEWN_EN */ |
| 352 | reg_value = ((PHY_SLEWP_EN << 3) | PHY_SLEWN_EN); |
| 353 | /* PHY_924 PHY_PAD_FDBK_DRIVE */ |
| 354 | clrsetbits_le32(&denali_phy[924], 0x3f << 8, reg_value << 8); |
| 355 | /* PHY_926 PHY_PAD_DATA_DRIVE */ |
| 356 | clrsetbits_le32(&denali_phy[926], 0x3f, reg_value); |
| 357 | /* PHY_927 PHY_PAD_DQS_DRIVE */ |
| 358 | clrsetbits_le32(&denali_phy[927], 0x3f, reg_value); |
| 359 | /* PHY_928 PHY_PAD_ADDR_DRIVE */ |
| 360 | clrsetbits_le32(&denali_phy[928], 0x3f << 8, reg_value << 8); |
| 361 | /* PHY_929 PHY_PAD_CLK_DRIVE */ |
| 362 | clrsetbits_le32(&denali_phy[929], 0x3f << 8, reg_value << 8); |
| 363 | /* PHY_935 PHY_PAD_CKE_DRIVE */ |
| 364 | clrsetbits_le32(&denali_phy[935], 0x3f << 8, reg_value << 8); |
| 365 | /* PHY_937 PHY_PAD_RST_DRIVE */ |
| 366 | clrsetbits_le32(&denali_phy[937], 0x3f << 8, reg_value << 8); |
| 367 | /* PHY_939 PHY_PAD_CS_DRIVE */ |
| 368 | clrsetbits_le32(&denali_phy[939], 0x3f << 8, reg_value << 8); |
Jagan Teki | 5d15217 | 2019-07-16 17:27:15 +0530 | [diff] [blame] | 369 | } |
| 370 | |
Jagan Teki | b5d4663 | 2019-07-16 17:27:07 +0530 | [diff] [blame] | 371 | /* speed setting */ |
| 372 | if (params->base.ddr_freq < 400) |
| 373 | speed = 0x0; |
| 374 | else if (params->base.ddr_freq < 800) |
| 375 | speed = 0x1; |
| 376 | else if (params->base.ddr_freq < 1200) |
| 377 | speed = 0x2; |
| 378 | else |
| 379 | speed = 0x3; |
| 380 | |
| 381 | /* PHY_924 PHY_PAD_FDBK_DRIVE */ |
| 382 | clrsetbits_le32(&denali_phy[924], 0x3 << 21, speed << 21); |
| 383 | /* PHY_926 PHY_PAD_DATA_DRIVE */ |
| 384 | clrsetbits_le32(&denali_phy[926], 0x3 << 9, speed << 9); |
| 385 | /* PHY_927 PHY_PAD_DQS_DRIVE */ |
| 386 | clrsetbits_le32(&denali_phy[927], 0x3 << 9, speed << 9); |
| 387 | /* PHY_928 PHY_PAD_ADDR_DRIVE */ |
| 388 | clrsetbits_le32(&denali_phy[928], 0x3 << 17, speed << 17); |
| 389 | /* PHY_929 PHY_PAD_CLK_DRIVE */ |
| 390 | clrsetbits_le32(&denali_phy[929], 0x3 << 17, speed << 17); |
| 391 | /* PHY_935 PHY_PAD_CKE_DRIVE */ |
| 392 | clrsetbits_le32(&denali_phy[935], 0x3 << 17, speed << 17); |
| 393 | /* PHY_937 PHY_PAD_RST_DRIVE */ |
| 394 | clrsetbits_le32(&denali_phy[937], 0x3 << 17, speed << 17); |
| 395 | /* PHY_939 PHY_PAD_CS_DRIVE */ |
| 396 | clrsetbits_le32(&denali_phy[939], 0x3 << 17, speed << 17); |
| 397 | |
Jagan Teki | 65535a2 | 2019-07-16 17:27:17 +0530 | [diff] [blame] | 398 | if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) { |
| 399 | /* RX_CM_INPUT */ |
| 400 | reg_value = PHY_RX_CM_INPUT; |
| 401 | /* PHY_924 PHY_PAD_FDBK_DRIVE */ |
| 402 | clrsetbits_le32(&denali_phy[924], 0x1 << 14, reg_value << 14); |
| 403 | /* PHY_926 PHY_PAD_DATA_DRIVE */ |
| 404 | clrsetbits_le32(&denali_phy[926], 0x1 << 11, reg_value << 11); |
| 405 | /* PHY_927 PHY_PAD_DQS_DRIVE */ |
| 406 | clrsetbits_le32(&denali_phy[927], 0x1 << 13, reg_value << 13); |
| 407 | /* PHY_928 PHY_PAD_ADDR_DRIVE */ |
| 408 | clrsetbits_le32(&denali_phy[928], 0x1 << 19, reg_value << 19); |
| 409 | /* PHY_929 PHY_PAD_CLK_DRIVE */ |
| 410 | clrsetbits_le32(&denali_phy[929], 0x1 << 21, reg_value << 21); |
| 411 | /* PHY_935 PHY_PAD_CKE_DRIVE */ |
| 412 | clrsetbits_le32(&denali_phy[935], 0x1 << 19, reg_value << 19); |
| 413 | /* PHY_937 PHY_PAD_RST_DRIVE */ |
| 414 | clrsetbits_le32(&denali_phy[937], 0x1 << 19, reg_value << 19); |
| 415 | /* PHY_939 PHY_PAD_CS_DRIVE */ |
| 416 | clrsetbits_le32(&denali_phy[939], 0x1 << 19, reg_value << 19); |
| 417 | } |
| 418 | |
Jagan Teki | b5d4663 | 2019-07-16 17:27:07 +0530 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 422 | static void set_ds_odt(const struct chan_info *chan, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 423 | const struct rk3399_sdram_params *params) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 424 | { |
| 425 | u32 *denali_phy = chan->publ->denali_phy; |
| 426 | |
| 427 | u32 tsel_idle_en, tsel_wr_en, tsel_rd_en; |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 428 | u32 tsel_idle_select_p, tsel_rd_select_p; |
| 429 | u32 tsel_idle_select_n, tsel_rd_select_n; |
| 430 | u32 tsel_wr_select_dq_p, tsel_wr_select_ca_p; |
| 431 | u32 tsel_wr_select_dq_n, tsel_wr_select_ca_n; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 432 | u32 reg_value; |
| 433 | |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 434 | if (params->base.dramtype == LPDDR4) { |
Jagan Teki | f676c7c | 2019-07-15 23:50:56 +0530 | [diff] [blame] | 435 | tsel_rd_select_p = PHY_DRV_ODT_HI_Z; |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 436 | tsel_rd_select_n = PHY_DRV_ODT_240; |
| 437 | |
Jagan Teki | f676c7c | 2019-07-15 23:50:56 +0530 | [diff] [blame] | 438 | tsel_idle_select_p = PHY_DRV_ODT_HI_Z; |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 439 | tsel_idle_select_n = PHY_DRV_ODT_240; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 440 | |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 441 | tsel_wr_select_dq_p = PHY_DRV_ODT_40; |
Jagan Teki | 3666714 | 2019-07-15 23:51:00 +0530 | [diff] [blame] | 442 | tsel_wr_select_dq_n = PHY_DRV_ODT_40; |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 443 | |
| 444 | tsel_wr_select_ca_p = PHY_DRV_ODT_40; |
Jagan Teki | 0fd5efb | 2019-07-15 23:51:02 +0530 | [diff] [blame] | 445 | tsel_wr_select_ca_n = PHY_DRV_ODT_40; |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 446 | } else if (params->base.dramtype == LPDDR3) { |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 447 | tsel_rd_select_p = PHY_DRV_ODT_240; |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 448 | tsel_rd_select_n = PHY_DRV_ODT_HI_Z; |
| 449 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 450 | tsel_idle_select_p = PHY_DRV_ODT_240; |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 451 | tsel_idle_select_n = PHY_DRV_ODT_HI_Z; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 452 | |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 453 | tsel_wr_select_dq_p = PHY_DRV_ODT_34_3; |
Jagan Teki | 3666714 | 2019-07-15 23:51:00 +0530 | [diff] [blame] | 454 | tsel_wr_select_dq_n = PHY_DRV_ODT_34_3; |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 455 | |
| 456 | tsel_wr_select_ca_p = PHY_DRV_ODT_48; |
Jagan Teki | 0fd5efb | 2019-07-15 23:51:02 +0530 | [diff] [blame] | 457 | tsel_wr_select_ca_n = PHY_DRV_ODT_48; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 458 | } else { |
| 459 | tsel_rd_select_p = PHY_DRV_ODT_240; |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 460 | tsel_rd_select_n = PHY_DRV_ODT_240; |
| 461 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 462 | tsel_idle_select_p = PHY_DRV_ODT_240; |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 463 | tsel_idle_select_n = PHY_DRV_ODT_240; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 464 | |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 465 | tsel_wr_select_dq_p = PHY_DRV_ODT_34_3; |
Jagan Teki | 3666714 | 2019-07-15 23:51:00 +0530 | [diff] [blame] | 466 | tsel_wr_select_dq_n = PHY_DRV_ODT_34_3; |
Jagan Teki | 5c3251f | 2019-07-15 23:51:04 +0530 | [diff] [blame] | 467 | |
| 468 | tsel_wr_select_ca_p = PHY_DRV_ODT_34_3; |
Jagan Teki | 0fd5efb | 2019-07-15 23:51:02 +0530 | [diff] [blame] | 469 | tsel_wr_select_ca_n = PHY_DRV_ODT_34_3; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 470 | } |
| 471 | |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 472 | if (params->base.odt == 1) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 473 | tsel_rd_en = 1; |
| 474 | else |
| 475 | tsel_rd_en = 0; |
| 476 | |
| 477 | tsel_wr_en = 0; |
| 478 | tsel_idle_en = 0; |
| 479 | |
| 480 | /* |
| 481 | * phy_dq_tsel_select_X 24bits DENALI_PHY_6/134/262/390 offset_0 |
| 482 | * sets termination values for read/idle cycles and drive strength |
| 483 | * for write cycles for DQ/DM |
| 484 | */ |
| 485 | reg_value = tsel_rd_select_n | (tsel_rd_select_p << 0x4) | |
Jagan Teki | b3b3439 | 2019-07-15 23:51:01 +0530 | [diff] [blame] | 486 | (tsel_wr_select_dq_n << 8) | (tsel_wr_select_dq_p << 12) | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 487 | (tsel_idle_select_n << 16) | (tsel_idle_select_p << 20); |
| 488 | clrsetbits_le32(&denali_phy[6], 0xffffff, reg_value); |
| 489 | clrsetbits_le32(&denali_phy[134], 0xffffff, reg_value); |
| 490 | clrsetbits_le32(&denali_phy[262], 0xffffff, reg_value); |
| 491 | clrsetbits_le32(&denali_phy[390], 0xffffff, reg_value); |
| 492 | |
| 493 | /* |
| 494 | * phy_dqs_tsel_select_X 24bits DENALI_PHY_7/135/263/391 offset_0 |
| 495 | * sets termination values for read/idle cycles and drive strength |
| 496 | * for write cycles for DQS |
| 497 | */ |
| 498 | clrsetbits_le32(&denali_phy[7], 0xffffff, reg_value); |
| 499 | clrsetbits_le32(&denali_phy[135], 0xffffff, reg_value); |
| 500 | clrsetbits_le32(&denali_phy[263], 0xffffff, reg_value); |
| 501 | clrsetbits_le32(&denali_phy[391], 0xffffff, reg_value); |
| 502 | |
| 503 | /* phy_adr_tsel_select_ 8bits DENALI_PHY_544/672/800 offset_0 */ |
Jagan Teki | 7caa3e9 | 2019-07-15 23:51:03 +0530 | [diff] [blame] | 504 | reg_value = tsel_wr_select_ca_n | (tsel_wr_select_ca_p << 0x4); |
Jagan Teki | 539ffed | 2019-07-16 17:27:19 +0530 | [diff] [blame] | 505 | if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) { |
| 506 | /* LPDDR4 these register read always return 0, so |
| 507 | * can not use clrsetbits_le32(), need to write32 |
| 508 | */ |
| 509 | writel((0x300 << 8) | reg_value, &denali_phy[544]); |
| 510 | writel((0x300 << 8) | reg_value, &denali_phy[672]); |
| 511 | writel((0x300 << 8) | reg_value, &denali_phy[800]); |
| 512 | } else { |
| 513 | clrsetbits_le32(&denali_phy[544], 0xff, reg_value); |
| 514 | clrsetbits_le32(&denali_phy[672], 0xff, reg_value); |
| 515 | clrsetbits_le32(&denali_phy[800], 0xff, reg_value); |
| 516 | } |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 517 | |
| 518 | /* phy_pad_addr_drive 8bits DENALI_PHY_928 offset_0 */ |
| 519 | clrsetbits_le32(&denali_phy[928], 0xff, reg_value); |
| 520 | |
| 521 | /* phy_pad_rst_drive 8bits DENALI_PHY_937 offset_0 */ |
| 522 | clrsetbits_le32(&denali_phy[937], 0xff, reg_value); |
| 523 | |
| 524 | /* phy_pad_cke_drive 8bits DENALI_PHY_935 offset_0 */ |
| 525 | clrsetbits_le32(&denali_phy[935], 0xff, reg_value); |
| 526 | |
| 527 | /* phy_pad_cs_drive 8bits DENALI_PHY_939 offset_0 */ |
| 528 | clrsetbits_le32(&denali_phy[939], 0xff, reg_value); |
| 529 | |
| 530 | /* phy_pad_clk_drive 8bits DENALI_PHY_929 offset_0 */ |
| 531 | clrsetbits_le32(&denali_phy[929], 0xff, reg_value); |
| 532 | |
| 533 | /* phy_pad_fdbk_drive 23bit DENALI_PHY_924/925 */ |
| 534 | clrsetbits_le32(&denali_phy[924], 0xff, |
Jagan Teki | b3b3439 | 2019-07-15 23:51:01 +0530 | [diff] [blame] | 535 | tsel_wr_select_dq_n | (tsel_wr_select_dq_p << 4)); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 536 | clrsetbits_le32(&denali_phy[925], 0xff, |
| 537 | tsel_rd_select_n | (tsel_rd_select_p << 4)); |
| 538 | |
| 539 | /* phy_dq_tsel_enable_X 3bits DENALI_PHY_5/133/261/389 offset_16 */ |
| 540 | reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2)) |
| 541 | << 16; |
| 542 | clrsetbits_le32(&denali_phy[5], 0x7 << 16, reg_value); |
| 543 | clrsetbits_le32(&denali_phy[133], 0x7 << 16, reg_value); |
| 544 | clrsetbits_le32(&denali_phy[261], 0x7 << 16, reg_value); |
| 545 | clrsetbits_le32(&denali_phy[389], 0x7 << 16, reg_value); |
| 546 | |
| 547 | /* phy_dqs_tsel_enable_X 3bits DENALI_PHY_6/134/262/390 offset_24 */ |
| 548 | reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2)) |
| 549 | << 24; |
| 550 | clrsetbits_le32(&denali_phy[6], 0x7 << 24, reg_value); |
| 551 | clrsetbits_le32(&denali_phy[134], 0x7 << 24, reg_value); |
| 552 | clrsetbits_le32(&denali_phy[262], 0x7 << 24, reg_value); |
| 553 | clrsetbits_le32(&denali_phy[390], 0x7 << 24, reg_value); |
| 554 | |
| 555 | /* phy_adr_tsel_enable_ 1bit DENALI_PHY_518/646/774 offset_8 */ |
| 556 | reg_value = tsel_wr_en << 8; |
| 557 | clrsetbits_le32(&denali_phy[518], 0x1 << 8, reg_value); |
| 558 | clrsetbits_le32(&denali_phy[646], 0x1 << 8, reg_value); |
| 559 | clrsetbits_le32(&denali_phy[774], 0x1 << 8, reg_value); |
| 560 | |
| 561 | /* phy_pad_addr_term tsel 1bit DENALI_PHY_933 offset_17 */ |
| 562 | reg_value = tsel_wr_en << 17; |
| 563 | clrsetbits_le32(&denali_phy[933], 0x1 << 17, reg_value); |
| 564 | /* |
| 565 | * pad_rst/cke/cs/clk_term tsel 1bits |
| 566 | * DENALI_PHY_938/936/940/934 offset_17 |
| 567 | */ |
| 568 | clrsetbits_le32(&denali_phy[938], 0x1 << 17, reg_value); |
| 569 | clrsetbits_le32(&denali_phy[936], 0x1 << 17, reg_value); |
| 570 | clrsetbits_le32(&denali_phy[940], 0x1 << 17, reg_value); |
| 571 | clrsetbits_le32(&denali_phy[934], 0x1 << 17, reg_value); |
| 572 | |
| 573 | /* phy_pad_fdbk_term 1bit DENALI_PHY_930 offset_17 */ |
| 574 | clrsetbits_le32(&denali_phy[930], 0x1 << 17, reg_value); |
Jagan Teki | b5d4663 | 2019-07-16 17:27:07 +0530 | [diff] [blame] | 575 | |
| 576 | phy_io_config(chan, params); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 577 | } |
| 578 | |
Jagan Teki | c9151e2 | 2019-07-15 23:58:45 +0530 | [diff] [blame] | 579 | static void pctl_start(struct dram_info *dram, u8 channel) |
| 580 | { |
| 581 | const struct chan_info *chan = &dram->chan[channel]; |
| 582 | u32 *denali_ctl = chan->pctl->denali_ctl; |
| 583 | u32 *denali_phy = chan->publ->denali_phy; |
| 584 | u32 *ddrc0_con = get_ddrc0_con(dram, channel); |
| 585 | u32 count = 0; |
| 586 | u32 byte, tmp; |
| 587 | |
| 588 | writel(0x01000000, &ddrc0_con); |
| 589 | |
| 590 | clrsetbits_le32(&denali_phy[957], 0x3 << 24, 0x2 << 24); |
| 591 | |
| 592 | while (!(readl(&denali_ctl[203]) & (1 << 3))) { |
| 593 | if (count > 1000) { |
| 594 | printf("%s: Failed to init pctl for channel %d\n", |
| 595 | __func__, channel); |
| 596 | while (1) |
| 597 | ; |
| 598 | } |
| 599 | |
| 600 | udelay(1); |
| 601 | count++; |
| 602 | } |
| 603 | |
| 604 | writel(0x01000100, &ddrc0_con); |
| 605 | |
| 606 | for (byte = 0; byte < 4; byte++) { |
| 607 | tmp = 0x820; |
| 608 | writel((tmp << 16) | tmp, &denali_phy[53 + (128 * byte)]); |
| 609 | writel((tmp << 16) | tmp, &denali_phy[54 + (128 * byte)]); |
| 610 | writel((tmp << 16) | tmp, &denali_phy[55 + (128 * byte)]); |
| 611 | writel((tmp << 16) | tmp, &denali_phy[56 + (128 * byte)]); |
| 612 | writel((tmp << 16) | tmp, &denali_phy[57 + (128 * byte)]); |
| 613 | |
| 614 | clrsetbits_le32(&denali_phy[58 + (128 * byte)], 0xffff, tmp); |
| 615 | } |
| 616 | |
| 617 | clrsetbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT, |
| 618 | dram->pwrup_srefresh_exit[channel]); |
| 619 | } |
| 620 | |
Jagan Teki | 4ef5c01 | 2019-07-15 23:58:44 +0530 | [diff] [blame] | 621 | static int pctl_cfg(struct dram_info *dram, const struct chan_info *chan, |
| 622 | u32 channel, const struct rk3399_sdram_params *params) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 623 | { |
| 624 | u32 *denali_ctl = chan->pctl->denali_ctl; |
| 625 | u32 *denali_pi = chan->pi->denali_pi; |
| 626 | u32 *denali_phy = chan->publ->denali_phy; |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 627 | const u32 *params_ctl = params->pctl_regs.denali_ctl; |
| 628 | const u32 *params_phy = params->phy_regs.denali_phy; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 629 | u32 tmp, tmp1, tmp2; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 630 | |
| 631 | /* |
| 632 | * work around controller bug: |
| 633 | * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed |
| 634 | */ |
| 635 | copy_to_reg(&denali_ctl[1], ¶ms_ctl[1], |
| 636 | sizeof(struct rk3399_ddr_pctl_regs) - 4); |
| 637 | writel(params_ctl[0], &denali_ctl[0]); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 638 | |
Jagan Teki | cc9da9a | 2019-07-16 17:27:13 +0530 | [diff] [blame] | 639 | /* |
| 640 | * two channel init at the same time, then ZQ Cal Start |
| 641 | * at the same time, it will use the same RZQ, but cannot |
| 642 | * start at the same time. |
| 643 | * |
| 644 | * So, increase tINIT3 for channel 1, will avoid two |
| 645 | * channel ZQ Cal Start at the same time |
| 646 | */ |
| 647 | if (params->base.dramtype == LPDDR4 && channel == 1) { |
| 648 | tmp = ((params->base.ddr_freq * MHz + 999) / 1000); |
| 649 | tmp1 = readl(&denali_ctl[14]); |
| 650 | writel(tmp + tmp1, &denali_ctl[14]); |
| 651 | } |
| 652 | |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 653 | copy_to_reg(denali_pi, ¶ms->pi_regs.denali_pi[0], |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 654 | sizeof(struct rk3399_ddr_pi_regs)); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 655 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 656 | /* rank count need to set for init */ |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 657 | set_memory_map(chan, channel, params); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 658 | |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 659 | writel(params->phy_regs.denali_phy[910], &denali_phy[910]); |
| 660 | writel(params->phy_regs.denali_phy[911], &denali_phy[911]); |
| 661 | writel(params->phy_regs.denali_phy[912], &denali_phy[912]); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 662 | |
Jagan Teki | b49b5dc | 2019-07-16 17:27:14 +0530 | [diff] [blame] | 663 | if (IS_ENABLED(CONFIG_RAM_RK3399_LPDDR4)) { |
| 664 | writel(params->phy_regs.denali_phy[898], &denali_phy[898]); |
| 665 | writel(params->phy_regs.denali_phy[919], &denali_phy[919]); |
| 666 | } |
| 667 | |
Jagan Teki | c9151e2 | 2019-07-15 23:58:45 +0530 | [diff] [blame] | 668 | dram->pwrup_srefresh_exit[channel] = readl(&denali_ctl[68]) & |
| 669 | PWRUP_SREFRESH_EXIT; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 670 | clrbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT); |
| 671 | |
| 672 | /* PHY_DLL_RST_EN */ |
| 673 | clrsetbits_le32(&denali_phy[957], 0x3 << 24, 1 << 24); |
| 674 | |
| 675 | setbits_le32(&denali_pi[0], START); |
| 676 | setbits_le32(&denali_ctl[0], START); |
| 677 | |
Jagan Teki | 5e92718 | 2019-07-16 17:27:12 +0530 | [diff] [blame] | 678 | /** |
| 679 | * LPDDR4 use PLL bypass mode for init |
| 680 | * not need to wait for the PLL to lock |
| 681 | */ |
| 682 | if (params->base.dramtype != LPDDR4) { |
| 683 | /* Waiting for phy DLL lock */ |
| 684 | while (1) { |
| 685 | tmp = readl(&denali_phy[920]); |
| 686 | tmp1 = readl(&denali_phy[921]); |
| 687 | tmp2 = readl(&denali_phy[922]); |
| 688 | if ((((tmp >> 16) & 0x1) == 0x1) && |
| 689 | (((tmp1 >> 16) & 0x1) == 0x1) && |
| 690 | (((tmp1 >> 0) & 0x1) == 0x1) && |
| 691 | (((tmp2 >> 0) & 0x1) == 0x1)) |
| 692 | break; |
| 693 | } |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | copy_to_reg(&denali_phy[896], ¶ms_phy[896], (958 - 895) * 4); |
| 697 | copy_to_reg(&denali_phy[0], ¶ms_phy[0], (90 - 0 + 1) * 4); |
| 698 | copy_to_reg(&denali_phy[128], ¶ms_phy[128], (218 - 128 + 1) * 4); |
| 699 | copy_to_reg(&denali_phy[256], ¶ms_phy[256], (346 - 256 + 1) * 4); |
| 700 | copy_to_reg(&denali_phy[384], ¶ms_phy[384], (474 - 384 + 1) * 4); |
| 701 | copy_to_reg(&denali_phy[512], ¶ms_phy[512], (549 - 512 + 1) * 4); |
| 702 | copy_to_reg(&denali_phy[640], ¶ms_phy[640], (677 - 640 + 1) * 4); |
| 703 | copy_to_reg(&denali_phy[768], ¶ms_phy[768], (805 - 768 + 1) * 4); |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 704 | set_ds_odt(chan, params); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 705 | |
| 706 | /* |
| 707 | * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_84/212/340/468 offset_8 |
| 708 | * dqs_tsel_wr_end[7:4] add Half cycle |
| 709 | */ |
| 710 | tmp = (readl(&denali_phy[84]) >> 8) & 0xff; |
| 711 | clrsetbits_le32(&denali_phy[84], 0xff << 8, (tmp + 0x10) << 8); |
| 712 | tmp = (readl(&denali_phy[212]) >> 8) & 0xff; |
| 713 | clrsetbits_le32(&denali_phy[212], 0xff << 8, (tmp + 0x10) << 8); |
| 714 | tmp = (readl(&denali_phy[340]) >> 8) & 0xff; |
| 715 | clrsetbits_le32(&denali_phy[340], 0xff << 8, (tmp + 0x10) << 8); |
| 716 | tmp = (readl(&denali_phy[468]) >> 8) & 0xff; |
| 717 | clrsetbits_le32(&denali_phy[468], 0xff << 8, (tmp + 0x10) << 8); |
| 718 | |
| 719 | /* |
| 720 | * phy_dqs_tsel_wr_timing_X 8bits DENALI_PHY_83/211/339/467 offset_8 |
| 721 | * dq_tsel_wr_end[7:4] add Half cycle |
| 722 | */ |
| 723 | tmp = (readl(&denali_phy[83]) >> 16) & 0xff; |
| 724 | clrsetbits_le32(&denali_phy[83], 0xff << 16, (tmp + 0x10) << 16); |
| 725 | tmp = (readl(&denali_phy[211]) >> 16) & 0xff; |
| 726 | clrsetbits_le32(&denali_phy[211], 0xff << 16, (tmp + 0x10) << 16); |
| 727 | tmp = (readl(&denali_phy[339]) >> 16) & 0xff; |
| 728 | clrsetbits_le32(&denali_phy[339], 0xff << 16, (tmp + 0x10) << 16); |
| 729 | tmp = (readl(&denali_phy[467]) >> 16) & 0xff; |
| 730 | clrsetbits_le32(&denali_phy[467], 0xff << 16, (tmp + 0x10) << 16); |
| 731 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | static void select_per_cs_training_index(const struct chan_info *chan, |
| 736 | u32 rank) |
| 737 | { |
| 738 | u32 *denali_phy = chan->publ->denali_phy; |
| 739 | |
| 740 | /* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */ |
Jagan Teki | f676c7c | 2019-07-15 23:50:56 +0530 | [diff] [blame] | 741 | if ((readl(&denali_phy[84]) >> 16) & 1) { |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 742 | /* |
| 743 | * PHY_8/136/264/392 |
| 744 | * phy_per_cs_training_index_X 1bit offset_24 |
| 745 | */ |
| 746 | clrsetbits_le32(&denali_phy[8], 0x1 << 24, rank << 24); |
| 747 | clrsetbits_le32(&denali_phy[136], 0x1 << 24, rank << 24); |
| 748 | clrsetbits_le32(&denali_phy[264], 0x1 << 24, rank << 24); |
| 749 | clrsetbits_le32(&denali_phy[392], 0x1 << 24, rank << 24); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | static void override_write_leveling_value(const struct chan_info *chan) |
| 754 | { |
| 755 | u32 *denali_ctl = chan->pctl->denali_ctl; |
| 756 | u32 *denali_phy = chan->publ->denali_phy; |
| 757 | u32 byte; |
| 758 | |
| 759 | /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */ |
| 760 | setbits_le32(&denali_phy[896], 1); |
| 761 | |
| 762 | /* |
| 763 | * PHY_8/136/264/392 |
| 764 | * phy_per_cs_training_multicast_en_X 1bit offset_16 |
| 765 | */ |
| 766 | clrsetbits_le32(&denali_phy[8], 0x1 << 16, 1 << 16); |
| 767 | clrsetbits_le32(&denali_phy[136], 0x1 << 16, 1 << 16); |
| 768 | clrsetbits_le32(&denali_phy[264], 0x1 << 16, 1 << 16); |
| 769 | clrsetbits_le32(&denali_phy[392], 0x1 << 16, 1 << 16); |
| 770 | |
| 771 | for (byte = 0; byte < 4; byte++) |
| 772 | clrsetbits_le32(&denali_phy[63 + (128 * byte)], 0xffff << 16, |
| 773 | 0x200 << 16); |
| 774 | |
| 775 | /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */ |
| 776 | clrbits_le32(&denali_phy[896], 1); |
| 777 | |
| 778 | /* CTL_200 ctrlupd_req 1bit offset_8 */ |
| 779 | clrsetbits_le32(&denali_ctl[200], 0x1 << 8, 0x1 << 8); |
| 780 | } |
| 781 | |
| 782 | static int data_training_ca(const struct chan_info *chan, u32 channel, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 783 | const struct rk3399_sdram_params *params) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 784 | { |
| 785 | u32 *denali_pi = chan->pi->denali_pi; |
| 786 | u32 *denali_phy = chan->publ->denali_phy; |
| 787 | u32 i, tmp; |
| 788 | u32 obs_0, obs_1, obs_2, obs_err = 0; |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 789 | u32 rank = params->ch[channel].cap_info.rank; |
Jagan Teki | bafcc14 | 2019-07-15 23:58:41 +0530 | [diff] [blame] | 790 | u32 rank_mask; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 791 | |
Jagan Teki | a607961 | 2019-07-15 23:58:40 +0530 | [diff] [blame] | 792 | /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */ |
| 793 | writel(0x00003f7c, (&denali_pi[175])); |
| 794 | |
Jagan Teki | f05675e | 2019-07-16 17:27:09 +0530 | [diff] [blame] | 795 | if (params->base.dramtype == LPDDR4) |
| 796 | rank_mask = (rank == 1) ? 0x5 : 0xf; |
| 797 | else |
| 798 | rank_mask = (rank == 1) ? 0x1 : 0x3; |
Jagan Teki | bafcc14 | 2019-07-15 23:58:41 +0530 | [diff] [blame] | 799 | |
| 800 | for (i = 0; i < 4; i++) { |
| 801 | if (!(rank_mask & (1 << i))) |
| 802 | continue; |
| 803 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 804 | select_per_cs_training_index(chan, i); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 805 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 806 | /* PI_100 PI_CALVL_EN:RW:8:2 */ |
| 807 | clrsetbits_le32(&denali_pi[100], 0x3 << 8, 0x2 << 8); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 808 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 809 | /* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */ |
| 810 | clrsetbits_le32(&denali_pi[92], |
| 811 | (0x1 << 16) | (0x3 << 24), |
| 812 | (0x1 << 16) | (i << 24)); |
| 813 | |
| 814 | /* Waiting for training complete */ |
| 815 | while (1) { |
| 816 | /* PI_174 PI_INT_STATUS:RD:8:18 */ |
| 817 | tmp = readl(&denali_pi[174]) >> 8; |
| 818 | /* |
| 819 | * check status obs |
| 820 | * PHY_532/660/789 phy_adr_calvl_obs1_:0:32 |
| 821 | */ |
| 822 | obs_0 = readl(&denali_phy[532]); |
| 823 | obs_1 = readl(&denali_phy[660]); |
| 824 | obs_2 = readl(&denali_phy[788]); |
| 825 | if (((obs_0 >> 30) & 0x3) || |
| 826 | ((obs_1 >> 30) & 0x3) || |
| 827 | ((obs_2 >> 30) & 0x3)) |
| 828 | obs_err = 1; |
| 829 | if ((((tmp >> 11) & 0x1) == 0x1) && |
| 830 | (((tmp >> 13) & 0x1) == 0x1) && |
| 831 | (((tmp >> 5) & 0x1) == 0x0) && |
Jagan Teki | f676c7c | 2019-07-15 23:50:56 +0530 | [diff] [blame] | 832 | obs_err == 0) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 833 | break; |
| 834 | else if ((((tmp >> 5) & 0x1) == 0x1) || |
| 835 | (obs_err == 1)) |
| 836 | return -EIO; |
| 837 | } |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 838 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 839 | /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */ |
| 840 | writel(0x00003f7c, (&denali_pi[175])); |
| 841 | } |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 842 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 843 | clrbits_le32(&denali_pi[100], 0x3 << 8); |
| 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static int data_training_wl(const struct chan_info *chan, u32 channel, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 849 | const struct rk3399_sdram_params *params) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 850 | { |
| 851 | u32 *denali_pi = chan->pi->denali_pi; |
| 852 | u32 *denali_phy = chan->publ->denali_phy; |
| 853 | u32 i, tmp; |
| 854 | u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0; |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 855 | u32 rank = params->ch[channel].cap_info.rank; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 856 | |
Jagan Teki | a607961 | 2019-07-15 23:58:40 +0530 | [diff] [blame] | 857 | /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */ |
| 858 | writel(0x00003f7c, (&denali_pi[175])); |
| 859 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 860 | for (i = 0; i < rank; i++) { |
| 861 | select_per_cs_training_index(chan, i); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 862 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 863 | /* PI_60 PI_WRLVL_EN:RW:8:2 */ |
| 864 | clrsetbits_le32(&denali_pi[60], 0x3 << 8, 0x2 << 8); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 865 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 866 | /* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */ |
| 867 | clrsetbits_le32(&denali_pi[59], |
| 868 | (0x1 << 8) | (0x3 << 16), |
| 869 | (0x1 << 8) | (i << 16)); |
| 870 | |
| 871 | /* Waiting for training complete */ |
| 872 | while (1) { |
| 873 | /* PI_174 PI_INT_STATUS:RD:8:18 */ |
| 874 | tmp = readl(&denali_pi[174]) >> 8; |
| 875 | |
| 876 | /* |
| 877 | * check status obs, if error maybe can not |
| 878 | * get leveling done PHY_40/168/296/424 |
| 879 | * phy_wrlvl_status_obs_X:0:13 |
| 880 | */ |
| 881 | obs_0 = readl(&denali_phy[40]); |
| 882 | obs_1 = readl(&denali_phy[168]); |
| 883 | obs_2 = readl(&denali_phy[296]); |
| 884 | obs_3 = readl(&denali_phy[424]); |
| 885 | if (((obs_0 >> 12) & 0x1) || |
| 886 | ((obs_1 >> 12) & 0x1) || |
| 887 | ((obs_2 >> 12) & 0x1) || |
| 888 | ((obs_3 >> 12) & 0x1)) |
| 889 | obs_err = 1; |
| 890 | if ((((tmp >> 10) & 0x1) == 0x1) && |
| 891 | (((tmp >> 13) & 0x1) == 0x1) && |
| 892 | (((tmp >> 4) & 0x1) == 0x0) && |
Jagan Teki | f676c7c | 2019-07-15 23:50:56 +0530 | [diff] [blame] | 893 | obs_err == 0) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 894 | break; |
| 895 | else if ((((tmp >> 4) & 0x1) == 0x1) || |
| 896 | (obs_err == 1)) |
| 897 | return -EIO; |
| 898 | } |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 899 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 900 | /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */ |
| 901 | writel(0x00003f7c, (&denali_pi[175])); |
| 902 | } |
| 903 | |
| 904 | override_write_leveling_value(chan); |
| 905 | clrbits_le32(&denali_pi[60], 0x3 << 8); |
| 906 | |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | static int data_training_rg(const struct chan_info *chan, u32 channel, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 911 | const struct rk3399_sdram_params *params) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 912 | { |
| 913 | u32 *denali_pi = chan->pi->denali_pi; |
| 914 | u32 *denali_phy = chan->publ->denali_phy; |
| 915 | u32 i, tmp; |
| 916 | u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0; |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 917 | u32 rank = params->ch[channel].cap_info.rank; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 918 | |
Jagan Teki | a607961 | 2019-07-15 23:58:40 +0530 | [diff] [blame] | 919 | /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */ |
| 920 | writel(0x00003f7c, (&denali_pi[175])); |
| 921 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 922 | for (i = 0; i < rank; i++) { |
| 923 | select_per_cs_training_index(chan, i); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 924 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 925 | /* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */ |
| 926 | clrsetbits_le32(&denali_pi[80], 0x3 << 24, 0x2 << 24); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 927 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 928 | /* |
| 929 | * PI_74 PI_RDLVL_GATE_REQ:WR:16:1 |
| 930 | * PI_RDLVL_CS:RW:24:2 |
| 931 | */ |
| 932 | clrsetbits_le32(&denali_pi[74], |
| 933 | (0x1 << 16) | (0x3 << 24), |
| 934 | (0x1 << 16) | (i << 24)); |
| 935 | |
| 936 | /* Waiting for training complete */ |
| 937 | while (1) { |
| 938 | /* PI_174 PI_INT_STATUS:RD:8:18 */ |
| 939 | tmp = readl(&denali_pi[174]) >> 8; |
| 940 | |
| 941 | /* |
| 942 | * check status obs |
| 943 | * PHY_43/171/299/427 |
| 944 | * PHY_GTLVL_STATUS_OBS_x:16:8 |
| 945 | */ |
| 946 | obs_0 = readl(&denali_phy[43]); |
| 947 | obs_1 = readl(&denali_phy[171]); |
| 948 | obs_2 = readl(&denali_phy[299]); |
| 949 | obs_3 = readl(&denali_phy[427]); |
| 950 | if (((obs_0 >> (16 + 6)) & 0x3) || |
| 951 | ((obs_1 >> (16 + 6)) & 0x3) || |
| 952 | ((obs_2 >> (16 + 6)) & 0x3) || |
| 953 | ((obs_3 >> (16 + 6)) & 0x3)) |
| 954 | obs_err = 1; |
| 955 | if ((((tmp >> 9) & 0x1) == 0x1) && |
| 956 | (((tmp >> 13) & 0x1) == 0x1) && |
| 957 | (((tmp >> 3) & 0x1) == 0x0) && |
Jagan Teki | f676c7c | 2019-07-15 23:50:56 +0530 | [diff] [blame] | 958 | obs_err == 0) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 959 | break; |
| 960 | else if ((((tmp >> 3) & 0x1) == 0x1) || |
| 961 | (obs_err == 1)) |
| 962 | return -EIO; |
| 963 | } |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 964 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 965 | /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */ |
| 966 | writel(0x00003f7c, (&denali_pi[175])); |
| 967 | } |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 968 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 969 | clrbits_le32(&denali_pi[80], 0x3 << 24); |
| 970 | |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | static int data_training_rl(const struct chan_info *chan, u32 channel, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 975 | const struct rk3399_sdram_params *params) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 976 | { |
| 977 | u32 *denali_pi = chan->pi->denali_pi; |
| 978 | u32 i, tmp; |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 979 | u32 rank = params->ch[channel].cap_info.rank; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 980 | |
Jagan Teki | a607961 | 2019-07-15 23:58:40 +0530 | [diff] [blame] | 981 | /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */ |
| 982 | writel(0x00003f7c, (&denali_pi[175])); |
| 983 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 984 | for (i = 0; i < rank; i++) { |
| 985 | select_per_cs_training_index(chan, i); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 986 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 987 | /* PI_80 PI_RDLVL_EN:RW:16:2 */ |
| 988 | clrsetbits_le32(&denali_pi[80], 0x3 << 16, 0x2 << 16); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 989 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 990 | /* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */ |
| 991 | clrsetbits_le32(&denali_pi[74], |
| 992 | (0x1 << 8) | (0x3 << 24), |
| 993 | (0x1 << 8) | (i << 24)); |
| 994 | |
| 995 | /* Waiting for training complete */ |
| 996 | while (1) { |
| 997 | /* PI_174 PI_INT_STATUS:RD:8:18 */ |
| 998 | tmp = readl(&denali_pi[174]) >> 8; |
| 999 | |
| 1000 | /* |
| 1001 | * make sure status obs not report error bit |
| 1002 | * PHY_46/174/302/430 |
| 1003 | * phy_rdlvl_status_obs_X:16:8 |
| 1004 | */ |
| 1005 | if ((((tmp >> 8) & 0x1) == 0x1) && |
| 1006 | (((tmp >> 13) & 0x1) == 0x1) && |
| 1007 | (((tmp >> 2) & 0x1) == 0x0)) |
| 1008 | break; |
| 1009 | else if (((tmp >> 2) & 0x1) == 0x1) |
| 1010 | return -EIO; |
| 1011 | } |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1012 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1013 | /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */ |
| 1014 | writel(0x00003f7c, (&denali_pi[175])); |
| 1015 | } |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1016 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1017 | clrbits_le32(&denali_pi[80], 0x3 << 16); |
| 1018 | |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | static int data_training_wdql(const struct chan_info *chan, u32 channel, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1023 | const struct rk3399_sdram_params *params) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1024 | { |
| 1025 | u32 *denali_pi = chan->pi->denali_pi; |
| 1026 | u32 i, tmp; |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 1027 | u32 rank = params->ch[channel].cap_info.rank; |
Jagan Teki | 8772359 | 2019-07-15 23:58:42 +0530 | [diff] [blame] | 1028 | u32 rank_mask; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1029 | |
Jagan Teki | a607961 | 2019-07-15 23:58:40 +0530 | [diff] [blame] | 1030 | /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */ |
| 1031 | writel(0x00003f7c, (&denali_pi[175])); |
| 1032 | |
Jagan Teki | d7504c0 | 2019-07-16 17:27:10 +0530 | [diff] [blame] | 1033 | if (params->base.dramtype == LPDDR4) |
| 1034 | rank_mask = (rank == 1) ? 0x5 : 0xf; |
| 1035 | else |
| 1036 | rank_mask = (rank == 1) ? 0x1 : 0x3; |
Jagan Teki | 8772359 | 2019-07-15 23:58:42 +0530 | [diff] [blame] | 1037 | |
| 1038 | for (i = 0; i < 4; i++) { |
| 1039 | if (!(rank_mask & (1 << i))) |
| 1040 | continue; |
| 1041 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1042 | select_per_cs_training_index(chan, i); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1043 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1044 | /* |
| 1045 | * disable PI_WDQLVL_VREF_EN before wdq leveling? |
| 1046 | * PI_181 PI_WDQLVL_VREF_EN:RW:8:1 |
| 1047 | */ |
| 1048 | clrbits_le32(&denali_pi[181], 0x1 << 8); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1049 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1050 | /* PI_124 PI_WDQLVL_EN:RW:16:2 */ |
| 1051 | clrsetbits_le32(&denali_pi[124], 0x3 << 16, 0x2 << 16); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1052 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1053 | /* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */ |
| 1054 | clrsetbits_le32(&denali_pi[121], |
| 1055 | (0x1 << 8) | (0x3 << 16), |
| 1056 | (0x1 << 8) | (i << 16)); |
| 1057 | |
| 1058 | /* Waiting for training complete */ |
| 1059 | while (1) { |
| 1060 | /* PI_174 PI_INT_STATUS:RD:8:18 */ |
| 1061 | tmp = readl(&denali_pi[174]) >> 8; |
| 1062 | if ((((tmp >> 12) & 0x1) == 0x1) && |
| 1063 | (((tmp >> 13) & 0x1) == 0x1) && |
| 1064 | (((tmp >> 6) & 0x1) == 0x0)) |
| 1065 | break; |
| 1066 | else if (((tmp >> 6) & 0x1) == 0x1) |
| 1067 | return -EIO; |
| 1068 | } |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1069 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1070 | /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */ |
| 1071 | writel(0x00003f7c, (&denali_pi[175])); |
| 1072 | } |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1073 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1074 | clrbits_le32(&denali_pi[124], 0x3 << 16); |
| 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | static int data_training(const struct chan_info *chan, u32 channel, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1080 | const struct rk3399_sdram_params *params, |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1081 | u32 training_flag) |
| 1082 | { |
| 1083 | u32 *denali_phy = chan->publ->denali_phy; |
Jagan Teki | 6214ff2 | 2019-07-15 23:58:39 +0530 | [diff] [blame] | 1084 | int ret; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1085 | |
| 1086 | /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */ |
| 1087 | setbits_le32(&denali_phy[927], (1 << 22)); |
| 1088 | |
| 1089 | if (training_flag == PI_FULL_TRAINING) { |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1090 | if (params->base.dramtype == LPDDR4) { |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1091 | training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING | |
| 1092 | PI_READ_GATE_TRAINING | |
| 1093 | PI_READ_LEVELING | PI_WDQ_LEVELING; |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1094 | } else if (params->base.dramtype == LPDDR3) { |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1095 | training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING | |
| 1096 | PI_READ_GATE_TRAINING; |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1097 | } else if (params->base.dramtype == DDR3) { |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1098 | training_flag = PI_WRITE_LEVELING | |
| 1099 | PI_READ_GATE_TRAINING | |
| 1100 | PI_READ_LEVELING; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | /* ca training(LPDDR4,LPDDR3 support) */ |
Jagan Teki | 6214ff2 | 2019-07-15 23:58:39 +0530 | [diff] [blame] | 1105 | if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING) { |
| 1106 | ret = data_training_ca(chan, channel, params); |
| 1107 | if (ret < 0) { |
| 1108 | debug("%s: data training ca failed\n", __func__); |
| 1109 | return ret; |
| 1110 | } |
| 1111 | } |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1112 | |
| 1113 | /* write leveling(LPDDR4,LPDDR3,DDR3 support) */ |
Jagan Teki | 6214ff2 | 2019-07-15 23:58:39 +0530 | [diff] [blame] | 1114 | if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING) { |
| 1115 | ret = data_training_wl(chan, channel, params); |
| 1116 | if (ret < 0) { |
| 1117 | debug("%s: data training wl failed\n", __func__); |
| 1118 | return ret; |
| 1119 | } |
| 1120 | } |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1121 | |
| 1122 | /* read gate training(LPDDR4,LPDDR3,DDR3 support) */ |
Jagan Teki | 6214ff2 | 2019-07-15 23:58:39 +0530 | [diff] [blame] | 1123 | if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING) { |
| 1124 | ret = data_training_rg(chan, channel, params); |
| 1125 | if (ret < 0) { |
| 1126 | debug("%s: data training rg failed\n", __func__); |
| 1127 | return ret; |
| 1128 | } |
| 1129 | } |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1130 | |
| 1131 | /* read leveling(LPDDR4,LPDDR3,DDR3 support) */ |
Jagan Teki | 6214ff2 | 2019-07-15 23:58:39 +0530 | [diff] [blame] | 1132 | if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING) { |
| 1133 | ret = data_training_rl(chan, channel, params); |
| 1134 | if (ret < 0) { |
| 1135 | debug("%s: data training rl failed\n", __func__); |
| 1136 | return ret; |
| 1137 | } |
| 1138 | } |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1139 | |
| 1140 | /* wdq leveling(LPDDR4 support) */ |
Jagan Teki | 6214ff2 | 2019-07-15 23:58:39 +0530 | [diff] [blame] | 1141 | if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING) { |
| 1142 | ret = data_training_wdql(chan, channel, params); |
| 1143 | if (ret < 0) { |
| 1144 | debug("%s: data training wdql failed\n", __func__); |
| 1145 | return ret; |
| 1146 | } |
| 1147 | } |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1148 | |
| 1149 | /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */ |
| 1150 | clrbits_le32(&denali_phy[927], (1 << 22)); |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | static void set_ddrconfig(const struct chan_info *chan, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1156 | const struct rk3399_sdram_params *params, |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1157 | unsigned char channel, u32 ddrconfig) |
| 1158 | { |
| 1159 | /* only need to set ddrconfig */ |
| 1160 | struct rk3399_msch_regs *ddr_msch_regs = chan->msch; |
| 1161 | unsigned int cs0_cap = 0; |
| 1162 | unsigned int cs1_cap = 0; |
| 1163 | |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 1164 | cs0_cap = (1 << (params->ch[channel].cap_info.cs0_row |
| 1165 | + params->ch[channel].cap_info.col |
| 1166 | + params->ch[channel].cap_info.bk |
| 1167 | + params->ch[channel].cap_info.bw - 20)); |
| 1168 | if (params->ch[channel].cap_info.rank > 1) |
| 1169 | cs1_cap = cs0_cap >> (params->ch[channel].cap_info.cs0_row |
| 1170 | - params->ch[channel].cap_info.cs1_row); |
| 1171 | if (params->ch[channel].cap_info.row_3_4) { |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1172 | cs0_cap = cs0_cap * 3 / 4; |
| 1173 | cs1_cap = cs1_cap * 3 / 4; |
| 1174 | } |
| 1175 | |
| 1176 | writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf); |
| 1177 | writel(((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8), |
| 1178 | &ddr_msch_regs->ddrsize); |
| 1179 | } |
| 1180 | |
| 1181 | static void dram_all_config(struct dram_info *dram, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1182 | const struct rk3399_sdram_params *params) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1183 | { |
Jagan Teki | 2d33712 | 2019-07-16 17:27:00 +0530 | [diff] [blame] | 1184 | u32 sys_reg2 = 0; |
Jagan Teki | 9d8769c | 2019-07-16 17:27:01 +0530 | [diff] [blame] | 1185 | u32 sys_reg3 = 0; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1186 | unsigned int channel, idx; |
| 1187 | |
Jagan Teki | 2d33712 | 2019-07-16 17:27:00 +0530 | [diff] [blame] | 1188 | sys_reg2 |= SYS_REG_ENC_DDRTYPE(params->base.dramtype); |
| 1189 | sys_reg2 |= SYS_REG_ENC_NUM_CH(params->base.num_channels); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1190 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1191 | for (channel = 0, idx = 0; |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1192 | (idx < params->base.num_channels) && (channel < 2); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1193 | channel++) { |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1194 | const struct rk3399_sdram_channel *info = ¶ms->ch[channel]; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1195 | struct rk3399_msch_regs *ddr_msch_regs; |
| 1196 | const struct rk3399_msch_timings *noc_timing; |
| 1197 | |
Jagan Teki | 97867c8 | 2019-07-15 23:51:05 +0530 | [diff] [blame] | 1198 | if (params->ch[channel].cap_info.col == 0) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1199 | continue; |
| 1200 | idx++; |
Jagan Teki | 2d33712 | 2019-07-16 17:27:00 +0530 | [diff] [blame] | 1201 | sys_reg2 |= SYS_REG_ENC_ROW_3_4(info->cap_info.row_3_4, channel); |
| 1202 | sys_reg2 |= SYS_REG_ENC_CHINFO(channel); |
| 1203 | sys_reg2 |= SYS_REG_ENC_RANK(info->cap_info.rank, channel); |
| 1204 | sys_reg2 |= SYS_REG_ENC_COL(info->cap_info.col, channel); |
| 1205 | sys_reg2 |= SYS_REG_ENC_BK(info->cap_info.bk, channel); |
Jagan Teki | 2d33712 | 2019-07-16 17:27:00 +0530 | [diff] [blame] | 1206 | sys_reg2 |= SYS_REG_ENC_BW(info->cap_info.bw, channel); |
| 1207 | sys_reg2 |= SYS_REG_ENC_DBW(info->cap_info.dbw, channel); |
Jagan Teki | 9d8769c | 2019-07-16 17:27:01 +0530 | [diff] [blame] | 1208 | SYS_REG_ENC_CS0_ROW(info->cap_info.cs0_row, sys_reg2, sys_reg3, channel); |
| 1209 | if (info->cap_info.cs1_row) |
| 1210 | SYS_REG_ENC_CS1_ROW(info->cap_info.cs1_row, sys_reg2, |
| 1211 | sys_reg3, channel); |
| 1212 | sys_reg3 |= SYS_REG_ENC_CS1_COL(info->cap_info.col, channel); |
Jagan Teki | 932dd96 | 2019-07-16 17:27:04 +0530 | [diff] [blame] | 1213 | sys_reg3 |= SYS_REG_ENC_VERSION(DDR_SYS_REG_VERSION); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1214 | |
| 1215 | ddr_msch_regs = dram->chan[channel].msch; |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1216 | noc_timing = ¶ms->ch[channel].noc_timings; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1217 | writel(noc_timing->ddrtiminga0, |
| 1218 | &ddr_msch_regs->ddrtiminga0); |
| 1219 | writel(noc_timing->ddrtimingb0, |
| 1220 | &ddr_msch_regs->ddrtimingb0); |
Jagan Teki | 5465f9b | 2019-07-16 17:27:05 +0530 | [diff] [blame] | 1221 | writel(noc_timing->ddrtimingc0.d32, |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1222 | &ddr_msch_regs->ddrtimingc0); |
| 1223 | writel(noc_timing->devtodev0, |
| 1224 | &ddr_msch_regs->devtodev0); |
Jagan Teki | 264a09f | 2019-07-16 17:27:06 +0530 | [diff] [blame] | 1225 | writel(noc_timing->ddrmode.d32, |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1226 | &ddr_msch_regs->ddrmode); |
| 1227 | |
Jagan Teki | b02c548 | 2019-07-16 17:27:20 +0530 | [diff] [blame^] | 1228 | /** |
| 1229 | * rank 1 memory clock disable (dfi_dram_clk_disable = 1) |
| 1230 | * |
| 1231 | * The hardware for LPDDR4 with |
| 1232 | * - CLK0P/N connect to lower 16-bits |
| 1233 | * - CLK1P/N connect to higher 16-bits |
| 1234 | * |
| 1235 | * dfi dram clk is configured via CLK1P/N, so disabling |
| 1236 | * dfi dram clk will disable the CLK1P/N as well for lpddr4. |
| 1237 | */ |
| 1238 | if (params->ch[channel].cap_info.rank == 1 && |
| 1239 | params->base.dramtype != LPDDR4) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1240 | setbits_le32(&dram->chan[channel].pctl->denali_ctl[276], |
| 1241 | 1 << 17); |
| 1242 | } |
| 1243 | |
Jagan Teki | 2d33712 | 2019-07-16 17:27:00 +0530 | [diff] [blame] | 1244 | writel(sys_reg2, &dram->pmugrf->os_reg2); |
Jagan Teki | 9d8769c | 2019-07-16 17:27:01 +0530 | [diff] [blame] | 1245 | writel(sys_reg3, &dram->pmugrf->os_reg3); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1246 | rk_clrsetreg(&dram->pmusgrf->soc_con4, 0x1f << 10, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1247 | params->base.stride << 10); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1248 | |
| 1249 | /* reboot hold register set */ |
| 1250 | writel(PRESET_SGRF_HOLD(0) | PRESET_GPIO0_HOLD(1) | |
| 1251 | PRESET_GPIO1_HOLD(1), |
| 1252 | &dram->pmucru->pmucru_rstnhold_con[1]); |
| 1253 | clrsetbits_le32(&dram->cru->glb_rst_con, 0x3, 0x3); |
| 1254 | } |
| 1255 | |
| 1256 | static int switch_to_phy_index1(struct dram_info *dram, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1257 | const struct rk3399_sdram_params *params) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1258 | { |
| 1259 | u32 channel; |
| 1260 | u32 *denali_phy; |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1261 | u32 ch_count = params->base.num_channels; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1262 | int ret; |
| 1263 | int i = 0; |
| 1264 | |
| 1265 | writel(RK_CLRSETBITS(0x03 << 4 | 1 << 2 | 1, |
| 1266 | 1 << 4 | 1 << 2 | 1), |
| 1267 | &dram->cic->cic_ctrl0); |
| 1268 | while (!(readl(&dram->cic->cic_status0) & (1 << 2))) { |
| 1269 | mdelay(10); |
| 1270 | i++; |
| 1271 | if (i > 10) { |
| 1272 | debug("index1 frequency change overtime\n"); |
| 1273 | return -ETIME; |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | i = 0; |
| 1278 | writel(RK_CLRSETBITS(1 << 1, 1 << 1), &dram->cic->cic_ctrl0); |
| 1279 | while (!(readl(&dram->cic->cic_status0) & (1 << 0))) { |
| 1280 | mdelay(10); |
Heinrich Schuchardt | 8051659 | 2018-03-18 12:10:55 +0100 | [diff] [blame] | 1281 | i++; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1282 | if (i > 10) { |
| 1283 | debug("index1 frequency done overtime\n"); |
| 1284 | return -ETIME; |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | for (channel = 0; channel < ch_count; channel++) { |
| 1289 | denali_phy = dram->chan[channel].publ->denali_phy; |
| 1290 | clrsetbits_le32(&denali_phy[896], (0x3 << 8) | 1, 1 << 8); |
| 1291 | ret = data_training(&dram->chan[channel], channel, |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1292 | params, PI_FULL_TRAINING); |
Jagan Teki | 6214ff2 | 2019-07-15 23:58:39 +0530 | [diff] [blame] | 1293 | if (ret < 0) { |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1294 | debug("index1 training failed\n"); |
| 1295 | return ret; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
Jagan Teki | 2525fae | 2019-07-15 23:58:52 +0530 | [diff] [blame] | 1302 | static unsigned char calculate_stride(struct rk3399_sdram_params *params) |
| 1303 | { |
| 1304 | unsigned int stride = params->base.stride; |
| 1305 | unsigned int channel, chinfo = 0; |
| 1306 | unsigned int ch_cap[2] = {0, 0}; |
| 1307 | u64 cap; |
| 1308 | |
| 1309 | for (channel = 0; channel < 2; channel++) { |
| 1310 | unsigned int cs0_cap = 0; |
| 1311 | unsigned int cs1_cap = 0; |
| 1312 | struct sdram_cap_info *cap_info = ¶ms->ch[channel].cap_info; |
| 1313 | |
| 1314 | if (cap_info->col == 0) |
| 1315 | continue; |
| 1316 | |
| 1317 | cs0_cap = (1 << (cap_info->cs0_row + cap_info->col + |
| 1318 | cap_info->bk + cap_info->bw - 20)); |
| 1319 | if (cap_info->rank > 1) |
| 1320 | cs1_cap = cs0_cap >> (cap_info->cs0_row |
| 1321 | - cap_info->cs1_row); |
| 1322 | if (cap_info->row_3_4) { |
| 1323 | cs0_cap = cs0_cap * 3 / 4; |
| 1324 | cs1_cap = cs1_cap * 3 / 4; |
| 1325 | } |
| 1326 | ch_cap[channel] = cs0_cap + cs1_cap; |
| 1327 | chinfo |= 1 << channel; |
| 1328 | } |
| 1329 | |
Jagan Teki | 874dede | 2019-07-15 23:58:53 +0530 | [diff] [blame] | 1330 | /* stride calculation for 1 channel */ |
| 1331 | if (params->base.num_channels == 1 && chinfo & 1) |
| 1332 | return 0x17; /* channel a */ |
| 1333 | |
Jagan Teki | 2525fae | 2019-07-15 23:58:52 +0530 | [diff] [blame] | 1334 | /* stride calculation for 2 channels, default gstride type is 256B */ |
| 1335 | if (ch_cap[0] == ch_cap[1]) { |
| 1336 | cap = ch_cap[0] + ch_cap[1]; |
| 1337 | switch (cap) { |
| 1338 | /* 512MB */ |
| 1339 | case 512: |
| 1340 | stride = 0; |
| 1341 | break; |
| 1342 | /* 1GB */ |
| 1343 | case 1024: |
| 1344 | stride = 0x5; |
| 1345 | break; |
| 1346 | /* |
| 1347 | * 768MB + 768MB same as total 2GB memory |
| 1348 | * useful space: 0-768MB 1GB-1792MB |
| 1349 | */ |
| 1350 | case 1536: |
| 1351 | /* 2GB */ |
| 1352 | case 2048: |
| 1353 | stride = 0x9; |
| 1354 | break; |
| 1355 | /* 1536MB + 1536MB */ |
| 1356 | case 3072: |
| 1357 | stride = 0x11; |
| 1358 | break; |
| 1359 | /* 4GB */ |
| 1360 | case 4096: |
| 1361 | stride = 0xD; |
| 1362 | break; |
| 1363 | default: |
| 1364 | printf("%s: Unable to calculate stride for ", __func__); |
| 1365 | print_size((cap * (1 << 20)), " capacity\n"); |
| 1366 | break; |
| 1367 | } |
| 1368 | } |
| 1369 | |
Jagan Teki | 8eed4a4 | 2019-07-15 23:58:55 +0530 | [diff] [blame] | 1370 | sdram_print_stride(stride); |
| 1371 | |
Jagan Teki | 2525fae | 2019-07-15 23:58:52 +0530 | [diff] [blame] | 1372 | return stride; |
| 1373 | } |
| 1374 | |
Jagan Teki | 43485e1 | 2019-07-15 23:58:54 +0530 | [diff] [blame] | 1375 | static void clear_channel_params(struct rk3399_sdram_params *params, u8 channel) |
| 1376 | { |
| 1377 | params->ch[channel].cap_info.rank = 0; |
| 1378 | params->ch[channel].cap_info.col = 0; |
| 1379 | params->ch[channel].cap_info.bk = 0; |
| 1380 | params->ch[channel].cap_info.bw = 32; |
| 1381 | params->ch[channel].cap_info.dbw = 32; |
| 1382 | params->ch[channel].cap_info.row_3_4 = 0; |
| 1383 | params->ch[channel].cap_info.cs0_row = 0; |
| 1384 | params->ch[channel].cap_info.cs1_row = 0; |
| 1385 | params->ch[channel].cap_info.ddrconfig = 0; |
| 1386 | } |
| 1387 | |
| 1388 | static int pctl_init(struct dram_info *dram, struct rk3399_sdram_params *params) |
| 1389 | { |
| 1390 | int channel; |
| 1391 | int ret; |
| 1392 | |
| 1393 | for (channel = 0; channel < 2; channel++) { |
| 1394 | const struct chan_info *chan = &dram->chan[channel]; |
| 1395 | struct rk3399_cru *cru = dram->cru; |
| 1396 | struct rk3399_ddr_publ_regs *publ = chan->publ; |
| 1397 | |
| 1398 | phy_pctrl_reset(cru, channel); |
| 1399 | phy_dll_bypass_set(publ, params->base.ddr_freq); |
| 1400 | |
| 1401 | ret = pctl_cfg(dram, chan, channel, params); |
| 1402 | if (ret < 0) { |
| 1403 | printf("%s: pctl config failed\n", __func__); |
| 1404 | return ret; |
| 1405 | } |
| 1406 | |
| 1407 | /* start to trigger initialization */ |
| 1408 | pctl_start(dram, channel); |
| 1409 | } |
| 1410 | |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1414 | static int sdram_init(struct dram_info *dram, |
Jagan Teki | 2525fae | 2019-07-15 23:58:52 +0530 | [diff] [blame] | 1415 | struct rk3399_sdram_params *params) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1416 | { |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1417 | unsigned char dramtype = params->base.dramtype; |
| 1418 | unsigned int ddr_freq = params->base.ddr_freq; |
Jagan Teki | 43485e1 | 2019-07-15 23:58:54 +0530 | [diff] [blame] | 1419 | u32 training_flag = PI_READ_GATE_TRAINING; |
| 1420 | int channel, ch, rank; |
Jagan Teki | 2ef77ed | 2019-07-15 23:50:59 +0530 | [diff] [blame] | 1421 | int ret; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1422 | |
| 1423 | debug("Starting SDRAM initialization...\n"); |
| 1424 | |
Philipp Tomsich | 39dce4a | 2017-05-31 18:16:35 +0200 | [diff] [blame] | 1425 | if ((dramtype == DDR3 && ddr_freq > 933) || |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1426 | (dramtype == LPDDR3 && ddr_freq > 933) || |
| 1427 | (dramtype == LPDDR4 && ddr_freq > 800)) { |
| 1428 | debug("SDRAM frequency is to high!"); |
| 1429 | return -E2BIG; |
| 1430 | } |
| 1431 | |
Jagan Teki | 43485e1 | 2019-07-15 23:58:54 +0530 | [diff] [blame] | 1432 | for (ch = 0; ch < 2; ch++) { |
| 1433 | params->ch[ch].cap_info.rank = 2; |
| 1434 | for (rank = 2; rank != 0; rank--) { |
| 1435 | ret = pctl_init(dram, params); |
| 1436 | if (ret < 0) { |
| 1437 | printf("%s: pctl init failed\n", __func__); |
| 1438 | return ret; |
| 1439 | } |
| 1440 | |
| 1441 | /* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */ |
| 1442 | if (dramtype == LPDDR3) |
| 1443 | udelay(10); |
| 1444 | |
| 1445 | params->ch[ch].cap_info.rank = rank; |
| 1446 | |
| 1447 | /* |
| 1448 | * LPDDR3 CA training msut be trigger before |
| 1449 | * other training. |
| 1450 | * DDR3 is not have CA training. |
| 1451 | */ |
| 1452 | if (params->base.dramtype == LPDDR3) |
| 1453 | training_flag |= PI_CA_TRAINING; |
| 1454 | |
| 1455 | if (!(data_training(&dram->chan[ch], ch, |
| 1456 | params, training_flag))) |
| 1457 | break; |
| 1458 | } |
| 1459 | /* Computed rank with associated channel number */ |
| 1460 | params->ch[ch].cap_info.rank = rank; |
| 1461 | } |
| 1462 | |
| 1463 | params->base.num_channels = 0; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1464 | for (channel = 0; channel < 2; channel++) { |
| 1465 | const struct chan_info *chan = &dram->chan[channel]; |
Jagan Teki | 43485e1 | 2019-07-15 23:58:54 +0530 | [diff] [blame] | 1466 | struct sdram_cap_info *cap_info = ¶ms->ch[channel].cap_info; |
| 1467 | u8 training_flag = PI_FULL_TRAINING; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1468 | |
Jagan Teki | 43485e1 | 2019-07-15 23:58:54 +0530 | [diff] [blame] | 1469 | if (cap_info->rank == 0) { |
| 1470 | clear_channel_params(params, channel); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1471 | continue; |
Jagan Teki | 43485e1 | 2019-07-15 23:58:54 +0530 | [diff] [blame] | 1472 | } else { |
| 1473 | params->base.num_channels++; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1474 | } |
| 1475 | |
Jagan Teki | 43485e1 | 2019-07-15 23:58:54 +0530 | [diff] [blame] | 1476 | debug("Channel "); |
| 1477 | debug(channel ? "1: " : "0: "); |
Jagan Teki | c9151e2 | 2019-07-15 23:58:45 +0530 | [diff] [blame] | 1478 | |
Jagan Teki | 43485e1 | 2019-07-15 23:58:54 +0530 | [diff] [blame] | 1479 | /* LPDDR3 should have write and read gate training */ |
| 1480 | if (params->base.dramtype == LPDDR3) |
| 1481 | training_flag = PI_WRITE_LEVELING | |
| 1482 | PI_READ_GATE_TRAINING; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1483 | |
Jagan Teki | 43485e1 | 2019-07-15 23:58:54 +0530 | [diff] [blame] | 1484 | if (params->base.dramtype != LPDDR4) { |
| 1485 | ret = data_training(dram, channel, params, |
| 1486 | training_flag); |
| 1487 | if (!ret) { |
| 1488 | debug("%s: data train failed for channel %d\n", |
| 1489 | __func__, ret); |
| 1490 | continue; |
| 1491 | } |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1492 | } |
| 1493 | |
Jagan Teki | 8eed4a4 | 2019-07-15 23:58:55 +0530 | [diff] [blame] | 1494 | sdram_print_ddr_info(cap_info, ¶ms->base); |
| 1495 | |
Jagan Teki | 43485e1 | 2019-07-15 23:58:54 +0530 | [diff] [blame] | 1496 | set_ddrconfig(chan, params, channel, cap_info->ddrconfig); |
| 1497 | } |
| 1498 | |
| 1499 | if (params->base.num_channels == 0) { |
| 1500 | printf("%s: ", __func__); |
Jagan Teki | 8eed4a4 | 2019-07-15 23:58:55 +0530 | [diff] [blame] | 1501 | sdram_print_dram_type(params->base.dramtype); |
Jagan Teki | 43485e1 | 2019-07-15 23:58:54 +0530 | [diff] [blame] | 1502 | printf(" - %dMHz failed!\n", params->base.ddr_freq); |
| 1503 | return -EINVAL; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1504 | } |
Jagan Teki | 2525fae | 2019-07-15 23:58:52 +0530 | [diff] [blame] | 1505 | |
| 1506 | params->base.stride = calculate_stride(params); |
Jagan Teki | a58ff79 | 2019-07-15 23:50:58 +0530 | [diff] [blame] | 1507 | dram_all_config(dram, params); |
| 1508 | switch_to_phy_index1(dram, params); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1509 | |
| 1510 | debug("Finish SDRAM initialization...\n"); |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
| 1514 | static int rk3399_dmc_ofdata_to_platdata(struct udevice *dev) |
| 1515 | { |
| 1516 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 1517 | struct rockchip_dmc_plat *plat = dev_get_platdata(dev); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1518 | int ret; |
| 1519 | |
Philipp Tomsich | 0250c23 | 2017-06-07 18:46:03 +0200 | [diff] [blame] | 1520 | ret = dev_read_u32_array(dev, "rockchip,sdram-params", |
| 1521 | (u32 *)&plat->sdram_params, |
| 1522 | sizeof(plat->sdram_params) / sizeof(u32)); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1523 | if (ret) { |
| 1524 | printf("%s: Cannot read rockchip,sdram-params %d\n", |
| 1525 | __func__, ret); |
| 1526 | return ret; |
| 1527 | } |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 1528 | ret = regmap_init_mem(dev_ofnode(dev), &plat->map); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1529 | if (ret) |
| 1530 | printf("%s: regmap failed %d\n", __func__, ret); |
| 1531 | |
| 1532 | #endif |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
| 1536 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 1537 | static int conv_of_platdata(struct udevice *dev) |
| 1538 | { |
| 1539 | struct rockchip_dmc_plat *plat = dev_get_platdata(dev); |
| 1540 | struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat; |
| 1541 | int ret; |
| 1542 | |
| 1543 | ret = regmap_init_mem_platdata(dev, dtplat->reg, |
Jagan Teki | f676c7c | 2019-07-15 23:50:56 +0530 | [diff] [blame] | 1544 | ARRAY_SIZE(dtplat->reg) / 2, |
| 1545 | &plat->map); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1546 | if (ret) |
| 1547 | return ret; |
| 1548 | |
| 1549 | return 0; |
| 1550 | } |
| 1551 | #endif |
| 1552 | |
| 1553 | static int rk3399_dmc_init(struct udevice *dev) |
| 1554 | { |
| 1555 | struct dram_info *priv = dev_get_priv(dev); |
| 1556 | struct rockchip_dmc_plat *plat = dev_get_platdata(dev); |
| 1557 | int ret; |
| 1558 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 1559 | struct rk3399_sdram_params *params = &plat->sdram_params; |
| 1560 | #else |
| 1561 | struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat; |
| 1562 | struct rk3399_sdram_params *params = |
| 1563 | (void *)dtplat->rockchip_sdram_params; |
| 1564 | |
| 1565 | ret = conv_of_platdata(dev); |
| 1566 | if (ret) |
| 1567 | return ret; |
| 1568 | #endif |
| 1569 | |
| 1570 | priv->cic = syscon_get_first_range(ROCKCHIP_SYSCON_CIC); |
Jagan Teki | c9151e2 | 2019-07-15 23:58:45 +0530 | [diff] [blame] | 1571 | priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1572 | priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF); |
| 1573 | priv->pmusgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF); |
| 1574 | priv->pmucru = rockchip_get_pmucru(); |
| 1575 | priv->cru = rockchip_get_cru(); |
| 1576 | priv->chan[0].pctl = regmap_get_range(plat->map, 0); |
| 1577 | priv->chan[0].pi = regmap_get_range(plat->map, 1); |
| 1578 | priv->chan[0].publ = regmap_get_range(plat->map, 2); |
| 1579 | priv->chan[0].msch = regmap_get_range(plat->map, 3); |
| 1580 | priv->chan[1].pctl = regmap_get_range(plat->map, 4); |
| 1581 | priv->chan[1].pi = regmap_get_range(plat->map, 5); |
| 1582 | priv->chan[1].publ = regmap_get_range(plat->map, 6); |
| 1583 | priv->chan[1].msch = regmap_get_range(plat->map, 7); |
| 1584 | |
| 1585 | debug("con reg %p %p %p %p %p %p %p %p\n", |
| 1586 | priv->chan[0].pctl, priv->chan[0].pi, |
| 1587 | priv->chan[0].publ, priv->chan[0].msch, |
| 1588 | priv->chan[1].pctl, priv->chan[1].pi, |
| 1589 | priv->chan[1].publ, priv->chan[1].msch); |
| 1590 | debug("cru %p, cic %p, grf %p, sgrf %p, pmucru %p\n", priv->cru, |
| 1591 | priv->cic, priv->pmugrf, priv->pmusgrf, priv->pmucru); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1592 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1593 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 1594 | ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->ddr_clk); |
| 1595 | #else |
| 1596 | ret = clk_get_by_index(dev, 0, &priv->ddr_clk); |
| 1597 | #endif |
| 1598 | if (ret) { |
| 1599 | printf("%s clk get failed %d\n", __func__, ret); |
| 1600 | return ret; |
| 1601 | } |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1602 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1603 | ret = clk_set_rate(&priv->ddr_clk, params->base.ddr_freq * MHz); |
| 1604 | if (ret < 0) { |
| 1605 | printf("%s clk set failed %d\n", __func__, ret); |
| 1606 | return ret; |
| 1607 | } |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1608 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1609 | ret = sdram_init(priv, params); |
| 1610 | if (ret < 0) { |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1611 | printf("%s DRAM init failed %d\n", __func__, ret); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1612 | return ret; |
| 1613 | } |
| 1614 | |
| 1615 | return 0; |
| 1616 | } |
| 1617 | #endif |
| 1618 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1619 | static int rk3399_dmc_probe(struct udevice *dev) |
| 1620 | { |
Kever Yang | 7f34784 | 2019-04-01 17:20:53 +0800 | [diff] [blame] | 1621 | #if defined(CONFIG_TPL_BUILD) || \ |
| 1622 | (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD)) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1623 | if (rk3399_dmc_init(dev)) |
| 1624 | return 0; |
| 1625 | #else |
| 1626 | struct dram_info *priv = dev_get_priv(dev); |
| 1627 | |
| 1628 | priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF); |
Jagan Teki | acf8e0f | 2019-07-15 23:50:57 +0530 | [diff] [blame] | 1629 | debug("%s: pmugrf = %p\n", __func__, priv->pmugrf); |
Kever Yang | 6c15a54 | 2017-06-23 16:11:06 +0800 | [diff] [blame] | 1630 | priv->info.base = CONFIG_SYS_SDRAM_BASE; |
Jagan Teki | f676c7c | 2019-07-15 23:50:56 +0530 | [diff] [blame] | 1631 | priv->info.size = |
| 1632 | rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg2); |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1633 | #endif |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | static int rk3399_dmc_get_info(struct udevice *dev, struct ram_info *info) |
| 1638 | { |
| 1639 | struct dram_info *priv = dev_get_priv(dev); |
| 1640 | |
Kever Yang | ea61d14 | 2017-04-19 16:01:14 +0800 | [diff] [blame] | 1641 | *info = priv->info; |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1642 | |
| 1643 | return 0; |
| 1644 | } |
| 1645 | |
| 1646 | static struct ram_ops rk3399_dmc_ops = { |
| 1647 | .get_info = rk3399_dmc_get_info, |
| 1648 | }; |
| 1649 | |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1650 | static const struct udevice_id rk3399_dmc_ids[] = { |
| 1651 | { .compatible = "rockchip,rk3399-dmc" }, |
| 1652 | { } |
| 1653 | }; |
| 1654 | |
| 1655 | U_BOOT_DRIVER(dmc_rk3399) = { |
| 1656 | .name = "rockchip_rk3399_dmc", |
| 1657 | .id = UCLASS_RAM, |
| 1658 | .of_match = rk3399_dmc_ids, |
| 1659 | .ops = &rk3399_dmc_ops, |
Kever Yang | 7f34784 | 2019-04-01 17:20:53 +0800 | [diff] [blame] | 1660 | #if defined(CONFIG_TPL_BUILD) || \ |
| 1661 | (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD)) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1662 | .ofdata_to_platdata = rk3399_dmc_ofdata_to_platdata, |
| 1663 | #endif |
| 1664 | .probe = rk3399_dmc_probe, |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1665 | .priv_auto_alloc_size = sizeof(struct dram_info), |
Kever Yang | 7f34784 | 2019-04-01 17:20:53 +0800 | [diff] [blame] | 1666 | #if defined(CONFIG_TPL_BUILD) || \ |
| 1667 | (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD)) |
Kever Yang | 50fb998 | 2017-02-22 16:56:35 +0800 | [diff] [blame] | 1668 | .platdata_auto_alloc_size = sizeof(struct rockchip_dmc_plat), |
| 1669 | #endif |
| 1670 | }; |