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