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