Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Texas Instruments' K3 DDRSS driver |
| 4 | * |
| 5 | * Copyright (C) 2020-2021 Texas Instruments Incorporated - http://www.ti.com/ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <clk.h> |
| 10 | #include <dm.h> |
| 11 | #include <dm/device_compat.h> |
| 12 | #include <ram.h> |
| 13 | #include <hang.h> |
| 14 | #include <log.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <power-domain.h> |
| 17 | #include <wait_bit.h> |
Lokesh Vutla | dd01c63 | 2021-05-11 10:22:13 -0500 | [diff] [blame] | 18 | #include <power/regulator.h> |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 19 | |
| 20 | #include "lpddr4_obj_if.h" |
| 21 | #include "lpddr4_if.h" |
| 22 | #include "lpddr4_structs_if.h" |
| 23 | #include "lpddr4_ctl_regs.h" |
| 24 | |
| 25 | #define SRAM_MAX 512 |
| 26 | |
| 27 | #define CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS 0x80 |
| 28 | #define CTRLMMR_DDR4_FSP_CLKCHNG_ACK_OFFS 0xc0 |
| 29 | |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 30 | #define DDRSS_V2A_R1_MAT_REG 0x0020 |
| 31 | #define DDRSS_ECC_CTRL_REG 0x0120 |
Dave Gerlach | 2c861a9 | 2021-05-11 10:22:12 -0500 | [diff] [blame] | 32 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 33 | #define SINGLE_DDR_SUBSYSTEM 0x1 |
| 34 | #define MULTI_DDR_SUBSYSTEM 0x2 |
| 35 | |
Aswath Govindraju | 6324bc7 | 2022-01-25 20:56:30 +0530 | [diff] [blame^] | 36 | #define MULTI_DDR_CFG0 0x00114100 |
| 37 | #define MULTI_DDR_CFG1 0x00114104 |
| 38 | #define DDR_CFG_LOAD 0x00114110 |
| 39 | |
| 40 | enum intrlv_gran { |
| 41 | GRAN_128B, |
| 42 | GRAN_512B, |
| 43 | GRAN_2KB, |
| 44 | GRAN_4KB, |
| 45 | GRAN_16KB, |
| 46 | GRAN_32KB, |
| 47 | GRAN_512KB, |
| 48 | GRAN_1GB, |
| 49 | GRAN_1_5GB, |
| 50 | GRAN_2GB, |
| 51 | GRAN_3GB, |
| 52 | GRAN_4GB, |
| 53 | GRAN_6GB, |
| 54 | GRAN_8GB, |
| 55 | GRAN_16GB |
| 56 | }; |
| 57 | |
| 58 | enum intrlv_size { |
| 59 | SIZE_0, |
| 60 | SIZE_128MB, |
| 61 | SIZE_256MB, |
| 62 | SIZE_512MB, |
| 63 | SIZE_1GB, |
| 64 | SIZE_2GB, |
| 65 | SIZE_3GB, |
| 66 | SIZE_4GB, |
| 67 | SIZE_6GB, |
| 68 | SIZE_8GB, |
| 69 | SIZE_12GB, |
| 70 | SIZE_16GB, |
| 71 | SIZE_32GB |
| 72 | }; |
| 73 | |
| 74 | struct k3_ddrss_data { |
| 75 | u32 flags; |
| 76 | }; |
| 77 | |
| 78 | enum ecc_enable { |
| 79 | DISABLE_ALL = 0, |
| 80 | ENABLE_0, |
| 81 | ENABLE_1, |
| 82 | ENABLE_ALL |
| 83 | }; |
| 84 | |
| 85 | enum emif_config { |
| 86 | INTERLEAVE_ALL = 0, |
| 87 | SEPR0, |
| 88 | SEPR1 |
| 89 | }; |
| 90 | |
| 91 | enum emif_active { |
| 92 | EMIF_0 = 1, |
| 93 | EMIF_1, |
| 94 | EMIF_ALL |
| 95 | }; |
| 96 | |
| 97 | struct k3_msmc { |
| 98 | enum intrlv_gran gran; |
| 99 | enum intrlv_size size; |
| 100 | enum ecc_enable enable; |
| 101 | enum emif_config config; |
| 102 | enum emif_active active; |
| 103 | }; |
| 104 | |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 105 | struct k3_ddrss_desc { |
| 106 | struct udevice *dev; |
| 107 | void __iomem *ddrss_ss_cfg; |
| 108 | void __iomem *ddrss_ctrl_mmr; |
| 109 | struct power_domain ddrcfg_pwrdmn; |
| 110 | struct power_domain ddrdata_pwrdmn; |
| 111 | struct clk ddr_clk; |
| 112 | struct clk osc_clk; |
| 113 | u32 ddr_freq1; |
| 114 | u32 ddr_freq2; |
| 115 | u32 ddr_fhs_cnt; |
Lokesh Vutla | dd01c63 | 2021-05-11 10:22:13 -0500 | [diff] [blame] | 116 | struct udevice *vtt_supply; |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 117 | u32 instance; |
| 118 | lpddr4_obj *driverdt; |
| 119 | lpddr4_config config; |
| 120 | lpddr4_privatedata pd; |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 121 | }; |
| 122 | |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 123 | struct reginitdata { |
| 124 | u32 ctl_regs[LPDDR4_INTR_CTL_REG_COUNT]; |
| 125 | u16 ctl_regs_offs[LPDDR4_INTR_CTL_REG_COUNT]; |
| 126 | u32 pi_regs[LPDDR4_INTR_PHY_INDEP_REG_COUNT]; |
| 127 | u16 pi_regs_offs[LPDDR4_INTR_PHY_INDEP_REG_COUNT]; |
| 128 | u32 phy_regs[LPDDR4_INTR_PHY_REG_COUNT]; |
| 129 | u16 phy_regs_offs[LPDDR4_INTR_PHY_REG_COUNT]; |
| 130 | }; |
| 131 | |
| 132 | #define TH_MACRO_EXP(fld, str) (fld##str) |
| 133 | |
| 134 | #define TH_FLD_MASK(fld) TH_MACRO_EXP(fld, _MASK) |
| 135 | #define TH_FLD_SHIFT(fld) TH_MACRO_EXP(fld, _SHIFT) |
| 136 | #define TH_FLD_WIDTH(fld) TH_MACRO_EXP(fld, _WIDTH) |
| 137 | #define TH_FLD_WOCLR(fld) TH_MACRO_EXP(fld, _WOCLR) |
| 138 | #define TH_FLD_WOSET(fld) TH_MACRO_EXP(fld, _WOSET) |
| 139 | |
| 140 | #define str(s) #s |
| 141 | #define xstr(s) str(s) |
| 142 | |
| 143 | #define CTL_SHIFT 11 |
| 144 | #define PHY_SHIFT 11 |
| 145 | #define PI_SHIFT 10 |
| 146 | |
| 147 | #define DENALI_CTL_0_DRAM_CLASS_DDR4 0xA |
| 148 | #define DENALI_CTL_0_DRAM_CLASS_LPDDR4 0xB |
| 149 | |
| 150 | #define TH_OFFSET_FROM_REG(REG, SHIFT, offset) do {\ |
| 151 | char *i, *pstr = xstr(REG); offset = 0;\ |
| 152 | for (i = &pstr[SHIFT]; *i != '\0'; ++i) {\ |
| 153 | offset = offset * 10 + (*i - '0'); } \ |
| 154 | } while (0) |
| 155 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 156 | static u32 k3_lpddr4_read_ddr_type(const lpddr4_privatedata *pd) |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 157 | { |
| 158 | u32 status = 0U; |
| 159 | u32 offset = 0U; |
| 160 | u32 regval = 0U; |
| 161 | u32 dram_class = 0U; |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 162 | struct k3_ddrss_desc *ddrss = (struct k3_ddrss_desc *)pd->ddr_instance; |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 163 | |
| 164 | TH_OFFSET_FROM_REG(LPDDR4__DRAM_CLASS__REG, CTL_SHIFT, offset); |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 165 | status = ddrss->driverdt->readreg(pd, LPDDR4_CTL_REGS, offset, ®val); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 166 | if (status > 0U) { |
| 167 | printf("%s: Failed to read DRAM_CLASS\n", __func__); |
| 168 | hang(); |
| 169 | } |
| 170 | |
| 171 | dram_class = ((regval & TH_FLD_MASK(LPDDR4__DRAM_CLASS__FLD)) >> |
| 172 | TH_FLD_SHIFT(LPDDR4__DRAM_CLASS__FLD)); |
| 173 | return dram_class; |
| 174 | } |
| 175 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 176 | static void k3_lpddr4_freq_update(struct k3_ddrss_desc *ddrss) |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 177 | { |
| 178 | unsigned int req_type, counter; |
| 179 | |
| 180 | for (counter = 0; counter < ddrss->ddr_fhs_cnt; counter++) { |
| 181 | if (wait_for_bit_le32(ddrss->ddrss_ctrl_mmr + |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 182 | CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS + ddrss->instance * 0x10, 0x80, |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 183 | true, 10000, false)) { |
| 184 | printf("Timeout during frequency handshake\n"); |
| 185 | hang(); |
| 186 | } |
| 187 | |
| 188 | req_type = readl(ddrss->ddrss_ctrl_mmr + |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 189 | CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS + ddrss->instance * 0x10) & 0x03; |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 190 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 191 | debug("%s: received freq change req: req type = %d, req no. = %d, instance = %d\n", |
| 192 | __func__, req_type, counter, ddrss->instance); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 193 | |
| 194 | if (req_type == 1) |
| 195 | clk_set_rate(&ddrss->ddr_clk, ddrss->ddr_freq1); |
| 196 | else if (req_type == 2) |
| 197 | clk_set_rate(&ddrss->ddr_clk, ddrss->ddr_freq2); |
| 198 | else if (req_type == 0) |
| 199 | /* Put DDR pll in bypass mode */ |
| 200 | clk_set_rate(&ddrss->ddr_clk, |
| 201 | clk_get_rate(&ddrss->osc_clk)); |
| 202 | else |
| 203 | printf("%s: Invalid freq request type\n", __func__); |
| 204 | |
| 205 | writel(0x1, ddrss->ddrss_ctrl_mmr + |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 206 | CTRLMMR_DDR4_FSP_CLKCHNG_ACK_OFFS + ddrss->instance * 0x10); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 207 | if (wait_for_bit_le32(ddrss->ddrss_ctrl_mmr + |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 208 | CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS + ddrss->instance * 0x10, 0x80, |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 209 | false, 10, false)) { |
| 210 | printf("Timeout during frequency handshake\n"); |
| 211 | hang(); |
| 212 | } |
| 213 | writel(0x0, ddrss->ddrss_ctrl_mmr + |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 214 | CTRLMMR_DDR4_FSP_CLKCHNG_ACK_OFFS + ddrss->instance * 0x10); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 218 | static void k3_lpddr4_ack_freq_upd_req(const lpddr4_privatedata *pd) |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 219 | { |
| 220 | u32 dram_class; |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 221 | struct k3_ddrss_desc *ddrss = (struct k3_ddrss_desc *)pd->ddr_instance; |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 222 | |
| 223 | debug("--->>> LPDDR4 Initialization is in progress ... <<<---\n"); |
| 224 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 225 | dram_class = k3_lpddr4_read_ddr_type(pd); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 226 | |
| 227 | switch (dram_class) { |
| 228 | case DENALI_CTL_0_DRAM_CLASS_DDR4: |
| 229 | break; |
| 230 | case DENALI_CTL_0_DRAM_CLASS_LPDDR4: |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 231 | k3_lpddr4_freq_update(ddrss); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 232 | break; |
| 233 | default: |
| 234 | printf("Unrecognized dram_class cannot update frequency!\n"); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | static int k3_ddrss_init_freq(struct k3_ddrss_desc *ddrss) |
| 239 | { |
| 240 | u32 dram_class; |
| 241 | int ret; |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 242 | lpddr4_privatedata *pd = &ddrss->pd; |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 243 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 244 | dram_class = k3_lpddr4_read_ddr_type(pd); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 245 | |
| 246 | switch (dram_class) { |
| 247 | case DENALI_CTL_0_DRAM_CLASS_DDR4: |
| 248 | /* Set to ddr_freq1 from DT for DDR4 */ |
| 249 | ret = clk_set_rate(&ddrss->ddr_clk, ddrss->ddr_freq1); |
| 250 | break; |
| 251 | case DENALI_CTL_0_DRAM_CLASS_LPDDR4: |
| 252 | /* Set to bypass frequency for LPDDR4*/ |
| 253 | ret = clk_set_rate(&ddrss->ddr_clk, clk_get_rate(&ddrss->osc_clk)); |
| 254 | break; |
| 255 | default: |
| 256 | ret = -EINVAL; |
| 257 | printf("Unrecognized dram_class cannot init frequency!\n"); |
| 258 | } |
| 259 | |
| 260 | if (ret < 0) |
| 261 | dev_err(ddrss->dev, "ddr clk init failed: %d\n", ret); |
| 262 | else |
| 263 | ret = 0; |
| 264 | |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | static void k3_lpddr4_info_handler(const lpddr4_privatedata *pd, |
| 269 | lpddr4_infotype infotype) |
| 270 | { |
| 271 | if (infotype == LPDDR4_DRV_SOC_PLL_UPDATE) |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 272 | k3_lpddr4_ack_freq_upd_req(pd); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static int k3_ddrss_power_on(struct k3_ddrss_desc *ddrss) |
| 276 | { |
| 277 | int ret; |
| 278 | |
| 279 | debug("%s(ddrss=%p)\n", __func__, ddrss); |
| 280 | |
| 281 | ret = power_domain_on(&ddrss->ddrcfg_pwrdmn); |
| 282 | if (ret) { |
| 283 | dev_err(ddrss->dev, "power_domain_on() failed: %d\n", ret); |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | ret = power_domain_on(&ddrss->ddrdata_pwrdmn); |
| 288 | if (ret) { |
| 289 | dev_err(ddrss->dev, "power_domain_on() failed: %d\n", ret); |
| 290 | return ret; |
| 291 | } |
| 292 | |
Lokesh Vutla | dd01c63 | 2021-05-11 10:22:13 -0500 | [diff] [blame] | 293 | ret = device_get_supply_regulator(ddrss->dev, "vtt-supply", |
| 294 | &ddrss->vtt_supply); |
| 295 | if (ret) { |
| 296 | dev_dbg(ddrss->dev, "vtt-supply not found.\n"); |
| 297 | } else { |
| 298 | ret = regulator_set_value(ddrss->vtt_supply, 3300000); |
| 299 | if (ret) |
| 300 | return ret; |
| 301 | dev_dbg(ddrss->dev, "VTT regulator enabled, volt = %d\n", |
| 302 | regulator_get_value(ddrss->vtt_supply)); |
| 303 | } |
| 304 | |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int k3_ddrss_ofdata_to_priv(struct udevice *dev) |
| 309 | { |
| 310 | struct k3_ddrss_desc *ddrss = dev_get_priv(dev); |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 311 | struct k3_ddrss_data *ddrss_data = (struct k3_ddrss_data *)dev_get_driver_data(dev); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 312 | phys_addr_t reg; |
| 313 | int ret; |
| 314 | |
| 315 | debug("%s(dev=%p)\n", __func__, dev); |
| 316 | |
| 317 | reg = dev_read_addr_name(dev, "cfg"); |
| 318 | if (reg == FDT_ADDR_T_NONE) { |
| 319 | dev_err(dev, "No reg property for DDRSS wrapper logic\n"); |
| 320 | return -EINVAL; |
| 321 | } |
| 322 | ddrss->ddrss_ss_cfg = (void *)reg; |
| 323 | |
| 324 | reg = dev_read_addr_name(dev, "ctrl_mmr_lp4"); |
| 325 | if (reg == FDT_ADDR_T_NONE) { |
| 326 | dev_err(dev, "No reg property for CTRL MMR\n"); |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | ddrss->ddrss_ctrl_mmr = (void *)reg; |
| 330 | |
| 331 | ret = power_domain_get_by_index(dev, &ddrss->ddrcfg_pwrdmn, 0); |
| 332 | if (ret) { |
| 333 | dev_err(dev, "power_domain_get() failed: %d\n", ret); |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | ret = power_domain_get_by_index(dev, &ddrss->ddrdata_pwrdmn, 1); |
| 338 | if (ret) { |
| 339 | dev_err(dev, "power_domain_get() failed: %d\n", ret); |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | ret = clk_get_by_index(dev, 0, &ddrss->ddr_clk); |
| 344 | if (ret) |
| 345 | dev_err(dev, "clk get failed%d\n", ret); |
| 346 | |
| 347 | ret = clk_get_by_index(dev, 1, &ddrss->osc_clk); |
| 348 | if (ret) |
| 349 | dev_err(dev, "clk get failed for osc clk %d\n", ret); |
| 350 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 351 | /* Reading instance number for multi ddr subystems */ |
| 352 | if (ddrss_data->flags & MULTI_DDR_SUBSYSTEM) { |
| 353 | ret = dev_read_u32(dev, "instance", &ddrss->instance); |
| 354 | if (ret) { |
| 355 | dev_err(dev, "missing instance property"); |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | } else { |
| 359 | ddrss->instance = 0; |
| 360 | } |
| 361 | |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 362 | ret = dev_read_u32(dev, "ti,ddr-freq1", &ddrss->ddr_freq1); |
| 363 | if (ret) |
| 364 | dev_err(dev, "ddr freq1 not populated %d\n", ret); |
| 365 | |
| 366 | ret = dev_read_u32(dev, "ti,ddr-freq2", &ddrss->ddr_freq2); |
| 367 | if (ret) |
| 368 | dev_err(dev, "ddr freq2 not populated %d\n", ret); |
| 369 | |
| 370 | ret = dev_read_u32(dev, "ti,ddr-fhs-cnt", &ddrss->ddr_fhs_cnt); |
| 371 | if (ret) |
| 372 | dev_err(dev, "ddr fhs cnt not populated %d\n", ret); |
| 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 377 | void k3_lpddr4_probe(struct k3_ddrss_desc *ddrss) |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 378 | { |
| 379 | u32 status = 0U; |
| 380 | u16 configsize = 0U; |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 381 | lpddr4_config *config = &ddrss->config; |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 382 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 383 | status = ddrss->driverdt->probe(config, &configsize); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 384 | |
| 385 | if ((status != 0) || (configsize != sizeof(lpddr4_privatedata)) |
| 386 | || (configsize > SRAM_MAX)) { |
| 387 | printf("%s: FAIL\n", __func__); |
| 388 | hang(); |
| 389 | } else { |
| 390 | debug("%s: PASS\n", __func__); |
| 391 | } |
| 392 | } |
| 393 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 394 | void k3_lpddr4_init(struct k3_ddrss_desc *ddrss) |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 395 | { |
| 396 | u32 status = 0U; |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 397 | lpddr4_config *config = &ddrss->config; |
| 398 | lpddr4_obj *driverdt = ddrss->driverdt; |
| 399 | lpddr4_privatedata *pd = &ddrss->pd; |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 400 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 401 | if ((sizeof(*pd) != sizeof(lpddr4_privatedata)) || (sizeof(*pd) > SRAM_MAX)) { |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 402 | printf("%s: FAIL\n", __func__); |
| 403 | hang(); |
| 404 | } |
| 405 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 406 | config->ctlbase = (struct lpddr4_ctlregs_s *)ddrss->ddrss_ss_cfg; |
| 407 | config->infohandler = (lpddr4_infocallback) k3_lpddr4_info_handler; |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 408 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 409 | status = driverdt->init(pd, config); |
| 410 | |
| 411 | /* linking ddr instance to lpddr4 */ |
| 412 | pd->ddr_instance = (void *)ddrss; |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 413 | |
| 414 | if ((status > 0U) || |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 415 | (pd->ctlbase != (struct lpddr4_ctlregs_s *)config->ctlbase) || |
| 416 | (pd->ctlinterrupthandler != config->ctlinterrupthandler) || |
| 417 | (pd->phyindepinterrupthandler != config->phyindepinterrupthandler)) { |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 418 | printf("%s: FAIL\n", __func__); |
| 419 | hang(); |
| 420 | } else { |
| 421 | debug("%s: PASS\n", __func__); |
| 422 | } |
| 423 | } |
| 424 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 425 | void populate_data_array_from_dt(struct k3_ddrss_desc *ddrss, |
| 426 | struct reginitdata *reginit_data) |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 427 | { |
| 428 | int ret, i; |
| 429 | |
| 430 | ret = dev_read_u32_array(ddrss->dev, "ti,ctl-data", |
| 431 | (u32 *)reginit_data->ctl_regs, |
| 432 | LPDDR4_INTR_CTL_REG_COUNT); |
| 433 | if (ret) |
| 434 | printf("Error reading ctrl data %d\n", ret); |
| 435 | |
| 436 | for (i = 0; i < LPDDR4_INTR_CTL_REG_COUNT; i++) |
| 437 | reginit_data->ctl_regs_offs[i] = i; |
| 438 | |
| 439 | ret = dev_read_u32_array(ddrss->dev, "ti,pi-data", |
| 440 | (u32 *)reginit_data->pi_regs, |
| 441 | LPDDR4_INTR_PHY_INDEP_REG_COUNT); |
| 442 | if (ret) |
| 443 | printf("Error reading PI data\n"); |
| 444 | |
| 445 | for (i = 0; i < LPDDR4_INTR_PHY_INDEP_REG_COUNT; i++) |
| 446 | reginit_data->pi_regs_offs[i] = i; |
| 447 | |
| 448 | ret = dev_read_u32_array(ddrss->dev, "ti,phy-data", |
| 449 | (u32 *)reginit_data->phy_regs, |
| 450 | LPDDR4_INTR_PHY_REG_COUNT); |
| 451 | if (ret) |
| 452 | printf("Error reading PHY data %d\n", ret); |
| 453 | |
| 454 | for (i = 0; i < LPDDR4_INTR_PHY_REG_COUNT; i++) |
| 455 | reginit_data->phy_regs_offs[i] = i; |
| 456 | } |
| 457 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 458 | void k3_lpddr4_hardware_reg_init(struct k3_ddrss_desc *ddrss) |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 459 | { |
| 460 | u32 status = 0U; |
| 461 | struct reginitdata reginitdata; |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 462 | lpddr4_obj *driverdt = ddrss->driverdt; |
| 463 | lpddr4_privatedata *pd = &ddrss->pd; |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 464 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 465 | populate_data_array_from_dt(ddrss, ®initdata); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 466 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 467 | status = driverdt->writectlconfig(pd, reginitdata.ctl_regs, |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 468 | reginitdata.ctl_regs_offs, |
| 469 | LPDDR4_INTR_CTL_REG_COUNT); |
| 470 | if (!status) |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 471 | status = driverdt->writephyindepconfig(pd, reginitdata.pi_regs, |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 472 | reginitdata.pi_regs_offs, |
| 473 | LPDDR4_INTR_PHY_INDEP_REG_COUNT); |
| 474 | if (!status) |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 475 | status = driverdt->writephyconfig(pd, reginitdata.phy_regs, |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 476 | reginitdata.phy_regs_offs, |
| 477 | LPDDR4_INTR_PHY_REG_COUNT); |
| 478 | if (status) { |
| 479 | printf("%s: FAIL\n", __func__); |
| 480 | hang(); |
| 481 | } |
| 482 | } |
| 483 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 484 | void k3_lpddr4_start(struct k3_ddrss_desc *ddrss) |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 485 | { |
| 486 | u32 status = 0U; |
| 487 | u32 regval = 0U; |
| 488 | u32 offset = 0U; |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 489 | lpddr4_obj *driverdt = ddrss->driverdt; |
| 490 | lpddr4_privatedata *pd = &ddrss->pd; |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 491 | |
| 492 | TH_OFFSET_FROM_REG(LPDDR4__START__REG, CTL_SHIFT, offset); |
| 493 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 494 | status = driverdt->readreg(pd, LPDDR4_CTL_REGS, offset, ®val); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 495 | if ((status > 0U) || ((regval & TH_FLD_MASK(LPDDR4__START__FLD)) != 0U)) { |
| 496 | printf("%s: Pre start FAIL\n", __func__); |
| 497 | hang(); |
| 498 | } |
| 499 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 500 | status = driverdt->start(pd); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 501 | if (status > 0U) { |
| 502 | printf("%s: FAIL\n", __func__); |
| 503 | hang(); |
| 504 | } |
| 505 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 506 | status = driverdt->readreg(pd, LPDDR4_CTL_REGS, offset, ®val); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 507 | if ((status > 0U) || ((regval & TH_FLD_MASK(LPDDR4__START__FLD)) != 1U)) { |
| 508 | printf("%s: Post start FAIL\n", __func__); |
| 509 | hang(); |
| 510 | } else { |
| 511 | debug("%s: Post start PASS\n", __func__); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | static int k3_ddrss_probe(struct udevice *dev) |
| 516 | { |
| 517 | int ret; |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 518 | struct k3_ddrss_desc *ddrss = dev_get_priv(dev); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 519 | |
| 520 | debug("%s(dev=%p)\n", __func__, dev); |
| 521 | |
| 522 | ret = k3_ddrss_ofdata_to_priv(dev); |
| 523 | if (ret) |
| 524 | return ret; |
| 525 | |
| 526 | ddrss->dev = dev; |
| 527 | ret = k3_ddrss_power_on(ddrss); |
| 528 | if (ret) |
| 529 | return ret; |
| 530 | |
Dave Gerlach | 2c861a9 | 2021-05-11 10:22:12 -0500 | [diff] [blame] | 531 | #ifdef CONFIG_K3_AM64_DDRSS |
| 532 | |
| 533 | writel(0x000001EF, ddrss->ddrss_ss_cfg + DDRSS_V2A_R1_MAT_REG); |
| 534 | writel(0x0, ddrss->ddrss_ss_cfg + DDRSS_ECC_CTRL_REG); |
| 535 | #endif |
| 536 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 537 | ddrss->driverdt = lpddr4_getinstance(); |
| 538 | |
| 539 | k3_lpddr4_probe(ddrss); |
| 540 | k3_lpddr4_init(ddrss); |
| 541 | k3_lpddr4_hardware_reg_init(ddrss); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 542 | |
| 543 | ret = k3_ddrss_init_freq(ddrss); |
| 544 | if (ret) |
| 545 | return ret; |
| 546 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 547 | k3_lpddr4_start(ddrss); |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 548 | |
| 549 | return ret; |
| 550 | } |
| 551 | |
| 552 | static int k3_ddrss_get_info(struct udevice *dev, struct ram_info *info) |
| 553 | { |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static struct ram_ops k3_ddrss_ops = { |
| 558 | .get_info = k3_ddrss_get_info, |
| 559 | }; |
| 560 | |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 561 | static const struct k3_ddrss_data k3_data = { |
| 562 | .flags = SINGLE_DDR_SUBSYSTEM, |
| 563 | }; |
| 564 | |
| 565 | static const struct k3_ddrss_data j721s2_data = { |
| 566 | .flags = MULTI_DDR_SUBSYSTEM, |
| 567 | }; |
| 568 | |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 569 | static const struct udevice_id k3_ddrss_ids[] = { |
Aswath Govindraju | b232cb4 | 2022-01-25 20:56:29 +0530 | [diff] [blame] | 570 | {.compatible = "ti,am64-ddrss", .data = (ulong)&k3_data, }, |
| 571 | {.compatible = "ti,j721e-ddrss", .data = (ulong)&k3_data, }, |
| 572 | {.compatible = "ti,j721s2-ddrss", .data = (ulong)&j721s2_data, }, |
Dave Gerlach | d712b36 | 2021-05-11 10:22:11 -0500 | [diff] [blame] | 573 | {} |
| 574 | }; |
| 575 | |
| 576 | U_BOOT_DRIVER(k3_ddrss) = { |
| 577 | .name = "k3_ddrss", |
| 578 | .id = UCLASS_RAM, |
| 579 | .of_match = k3_ddrss_ids, |
| 580 | .ops = &k3_ddrss_ops, |
| 581 | .probe = k3_ddrss_probe, |
| 582 | .priv_auto = sizeof(struct k3_ddrss_desc), |
| 583 | }; |
Aswath Govindraju | 6324bc7 | 2022-01-25 20:56:30 +0530 | [diff] [blame^] | 584 | |
| 585 | static int k3_msmc_set_config(struct k3_msmc *msmc) |
| 586 | { |
| 587 | u32 ddr_cfg0 = 0; |
| 588 | u32 ddr_cfg1 = 0; |
| 589 | |
| 590 | ddr_cfg0 |= msmc->gran << 24; |
| 591 | ddr_cfg0 |= msmc->size << 16; |
| 592 | /* heartbeat_per, bit[4:0] setting to 3 is advisable */ |
| 593 | ddr_cfg0 |= 3; |
| 594 | |
| 595 | /* Program MULTI_DDR_CFG0 */ |
| 596 | writel(ddr_cfg0, MULTI_DDR_CFG0); |
| 597 | |
| 598 | ddr_cfg1 |= msmc->enable << 16; |
| 599 | ddr_cfg1 |= msmc->config << 8; |
| 600 | ddr_cfg1 |= msmc->active; |
| 601 | |
| 602 | /* Program MULTI_DDR_CFG1 */ |
| 603 | writel(ddr_cfg1, MULTI_DDR_CFG1); |
| 604 | |
| 605 | /* Program DDR_CFG_LOAD */ |
| 606 | writel(0x60000000, DDR_CFG_LOAD); |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static int k3_msmc_probe(struct udevice *dev) |
| 612 | { |
| 613 | struct k3_msmc *msmc = dev_get_priv(dev); |
| 614 | int ret = 0; |
| 615 | |
| 616 | /* Read the granular size from DT */ |
| 617 | ret = dev_read_u32(dev, "intrlv-gran", &msmc->gran); |
| 618 | if (ret) { |
| 619 | dev_err(dev, "missing intrlv-gran property"); |
| 620 | return -EINVAL; |
| 621 | } |
| 622 | |
| 623 | /* Read the interleave region from DT */ |
| 624 | ret = dev_read_u32(dev, "intrlv-size", &msmc->size); |
| 625 | if (ret) { |
| 626 | dev_err(dev, "missing intrlv-size property"); |
| 627 | return -EINVAL; |
| 628 | } |
| 629 | |
| 630 | /* Read ECC enable config */ |
| 631 | ret = dev_read_u32(dev, "ecc-enable", &msmc->enable); |
| 632 | if (ret) { |
| 633 | dev_err(dev, "missing ecc-enable property"); |
| 634 | return -EINVAL; |
| 635 | } |
| 636 | |
| 637 | /* Read EMIF configuration */ |
| 638 | ret = dev_read_u32(dev, "emif-config", &msmc->config); |
| 639 | if (ret) { |
| 640 | dev_err(dev, "missing emif-config property"); |
| 641 | return -EINVAL; |
| 642 | } |
| 643 | |
| 644 | /* Read EMIF active */ |
| 645 | ret = dev_read_u32(dev, "emif-active", &msmc->active); |
| 646 | if (ret) { |
| 647 | dev_err(dev, "missing emif-active property"); |
| 648 | return -EINVAL; |
| 649 | } |
| 650 | |
| 651 | ret = k3_msmc_set_config(msmc); |
| 652 | if (ret) { |
| 653 | dev_err(dev, "error setting msmc config"); |
| 654 | return -EINVAL; |
| 655 | } |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static const struct udevice_id k3_msmc_ids[] = { |
| 661 | { .compatible = "ti,j721s2-msmc"}, |
| 662 | {} |
| 663 | }; |
| 664 | |
| 665 | U_BOOT_DRIVER(k3_msmc) = { |
| 666 | .name = "k3_msmc", |
| 667 | .of_match = k3_msmc_ids, |
| 668 | .id = UCLASS_MISC, |
| 669 | .probe = k3_msmc_probe, |
| 670 | .priv_auto = sizeof(struct k3_msmc), |
| 671 | .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF, |
| 672 | }; |