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