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