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