Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/ |
| 4 | * |
| 5 | * Texas Instruments' K3 SD Host Controller Interface |
| 6 | */ |
| 7 | |
| 8 | #include <clk.h> |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <malloc.h> |
| 12 | #include <power-domain.h> |
Faiz Abbas | ce142ff | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 13 | #include <regmap.h> |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 14 | #include <sdhci.h> |
| 15 | |
Faiz Abbas | ce142ff | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 16 | /* CTL_CFG Registers */ |
| 17 | #define CTL_CFG_2 0x14 |
| 18 | |
| 19 | #define SLOTTYPE_MASK GENMASK(31, 30) |
| 20 | #define SLOTTYPE_EMBEDDED BIT(30) |
| 21 | |
| 22 | /* PHY Registers */ |
| 23 | #define PHY_CTRL1 0x100 |
| 24 | #define PHY_CTRL2 0x104 |
| 25 | #define PHY_CTRL3 0x108 |
| 26 | #define PHY_CTRL4 0x10C |
| 27 | #define PHY_CTRL5 0x110 |
| 28 | #define PHY_CTRL6 0x114 |
| 29 | #define PHY_STAT1 0x130 |
| 30 | #define PHY_STAT2 0x134 |
| 31 | |
| 32 | #define IOMUX_ENABLE_SHIFT 31 |
| 33 | #define IOMUX_ENABLE_MASK BIT(IOMUX_ENABLE_SHIFT) |
| 34 | #define OTAPDLYENA_SHIFT 20 |
| 35 | #define OTAPDLYENA_MASK BIT(OTAPDLYENA_SHIFT) |
| 36 | #define OTAPDLYSEL_SHIFT 12 |
| 37 | #define OTAPDLYSEL_MASK GENMASK(15, 12) |
| 38 | #define STRBSEL_SHIFT 24 |
| 39 | #define STRBSEL_MASK GENMASK(27, 24) |
| 40 | #define SEL50_SHIFT 8 |
| 41 | #define SEL50_MASK BIT(SEL50_SHIFT) |
| 42 | #define SEL100_SHIFT 9 |
| 43 | #define SEL100_MASK BIT(SEL100_SHIFT) |
| 44 | #define DLL_TRIM_ICP_SHIFT 4 |
| 45 | #define DLL_TRIM_ICP_MASK GENMASK(7, 4) |
| 46 | #define DR_TY_SHIFT 20 |
| 47 | #define DR_TY_MASK GENMASK(22, 20) |
| 48 | #define ENDLL_SHIFT 1 |
| 49 | #define ENDLL_MASK BIT(ENDLL_SHIFT) |
| 50 | #define DLLRDY_SHIFT 0 |
| 51 | #define DLLRDY_MASK BIT(DLLRDY_SHIFT) |
| 52 | #define PDB_SHIFT 0 |
| 53 | #define PDB_MASK BIT(PDB_SHIFT) |
| 54 | #define CALDONE_SHIFT 1 |
| 55 | #define CALDONE_MASK BIT(CALDONE_SHIFT) |
| 56 | #define RETRIM_SHIFT 17 |
| 57 | #define RETRIM_MASK BIT(RETRIM_SHIFT) |
| 58 | |
| 59 | #define DRIVER_STRENGTH_50_OHM 0x0 |
| 60 | #define DRIVER_STRENGTH_33_OHM 0x1 |
| 61 | #define DRIVER_STRENGTH_66_OHM 0x2 |
| 62 | #define DRIVER_STRENGTH_100_OHM 0x3 |
| 63 | #define DRIVER_STRENGTH_40_OHM 0x4 |
| 64 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 65 | #define AM654_SDHCI_MIN_FREQ 400000 |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 66 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 67 | struct am654_sdhci_plat { |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 68 | struct mmc_config cfg; |
| 69 | struct mmc mmc; |
Faiz Abbas | ce142ff | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 70 | struct regmap *base; |
| 71 | bool non_removable; |
| 72 | u32 otap_del_sel; |
| 73 | u32 trm_icp; |
| 74 | u32 drv_strength; |
| 75 | bool dll_on; |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 76 | }; |
| 77 | |
Faiz Abbas | f605807 | 2019-06-11 00:43:41 +0530 | [diff] [blame] | 78 | static void am654_sdhci_set_control_reg(struct sdhci_host *host) |
| 79 | { |
| 80 | struct mmc *mmc = (struct mmc *)host->mmc; |
| 81 | u32 reg; |
| 82 | |
| 83 | if (IS_SD(host->mmc) && |
| 84 | mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) { |
| 85 | reg = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 86 | reg |= SDHCI_CTRL_VDD_180; |
| 87 | sdhci_writew(host, reg, SDHCI_HOST_CONTROL2); |
| 88 | } |
| 89 | |
| 90 | sdhci_set_uhs_timing(host); |
| 91 | } |
| 92 | |
Faiz Abbas | ce142ff | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 93 | static int am654_sdhci_set_ios_post(struct sdhci_host *host) |
| 94 | { |
| 95 | struct udevice *dev = host->mmc->dev; |
| 96 | struct am654_sdhci_plat *plat = dev_get_platdata(dev); |
| 97 | unsigned int speed = host->mmc->clock; |
| 98 | int sel50, sel100; |
| 99 | u32 mask, val; |
| 100 | int ret; |
| 101 | |
| 102 | /* Reset SD Clock Enable */ |
| 103 | val = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 104 | val &= ~SDHCI_CLOCK_CARD_EN; |
| 105 | sdhci_writew(host, val, SDHCI_CLOCK_CONTROL); |
| 106 | |
| 107 | /* power off phy */ |
| 108 | if (plat->dll_on) { |
| 109 | regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK, 0); |
| 110 | |
| 111 | plat->dll_on = false; |
| 112 | } |
| 113 | |
| 114 | /* restart clock */ |
| 115 | sdhci_set_clock(host->mmc, speed); |
| 116 | |
| 117 | /* switch phy back on */ |
| 118 | if (speed > AM654_SDHCI_MIN_FREQ) { |
| 119 | mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK; |
| 120 | val = (1 << OTAPDLYENA_SHIFT) | |
| 121 | (plat->otap_del_sel << OTAPDLYSEL_SHIFT); |
| 122 | regmap_update_bits(plat->base, PHY_CTRL4, mask, val); |
| 123 | switch (speed) { |
| 124 | case 200000000: |
| 125 | sel50 = 0; |
| 126 | sel100 = 0; |
| 127 | break; |
| 128 | case 100000000: |
| 129 | sel50 = 0; |
| 130 | sel100 = 1; |
| 131 | break; |
| 132 | default: |
| 133 | sel50 = 1; |
| 134 | sel100 = 0; |
| 135 | } |
| 136 | |
| 137 | /* Configure PHY DLL frequency */ |
| 138 | mask = SEL50_MASK | SEL100_MASK; |
| 139 | val = (sel50 << SEL50_SHIFT) | (sel100 << SEL100_SHIFT); |
| 140 | regmap_update_bits(plat->base, PHY_CTRL5, mask, val); |
| 141 | |
| 142 | /* Enable DLL */ |
| 143 | regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK, |
| 144 | 0x1 << ENDLL_SHIFT); |
| 145 | /* |
| 146 | * Poll for DLL ready. Use a one second timeout. |
| 147 | * Works in all experiments done so far |
| 148 | */ |
| 149 | ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, val, |
| 150 | val & DLLRDY_MASK, 1000, 1000000); |
| 151 | if (ret) |
| 152 | return ret; |
| 153 | |
| 154 | plat->dll_on = true; |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | const struct sdhci_ops am654_sdhci_ops = { |
Faiz Abbas | f605807 | 2019-06-11 00:43:41 +0530 | [diff] [blame] | 161 | .set_ios_post = &am654_sdhci_set_ios_post, |
| 162 | .set_control_reg = &am654_sdhci_set_control_reg, |
Faiz Abbas | ce142ff | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | int am654_sdhci_init(struct am654_sdhci_plat *plat) |
| 166 | { |
| 167 | u32 ctl_cfg_2 = 0; |
| 168 | u32 mask, val; |
| 169 | int ret; |
| 170 | |
| 171 | /* Reset OTAP to default value */ |
| 172 | mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK; |
| 173 | regmap_update_bits(plat->base, PHY_CTRL4, mask, 0x0); |
| 174 | |
| 175 | regmap_read(plat->base, PHY_STAT1, &val); |
| 176 | if (~val & CALDONE_MASK) { |
| 177 | /* Calibrate IO lines */ |
| 178 | regmap_update_bits(plat->base, PHY_CTRL1, PDB_MASK, PDB_MASK); |
| 179 | ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, val, |
| 180 | val & CALDONE_MASK, 1, 20); |
| 181 | if (ret) |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | /* Configure DLL TRIM */ |
| 186 | mask = DLL_TRIM_ICP_MASK; |
| 187 | val = plat->trm_icp << DLL_TRIM_ICP_SHIFT; |
| 188 | |
| 189 | /* Configure DLL driver strength */ |
| 190 | mask |= DR_TY_MASK; |
| 191 | val |= plat->drv_strength << DR_TY_SHIFT; |
| 192 | regmap_update_bits(plat->base, PHY_CTRL1, mask, val); |
| 193 | |
| 194 | /* Enable pins by setting IO mux to 0 */ |
| 195 | regmap_update_bits(plat->base, PHY_CTRL1, IOMUX_ENABLE_MASK, 0); |
| 196 | |
| 197 | /* Set slot type based on SD or eMMC */ |
| 198 | if (plat->non_removable) |
| 199 | ctl_cfg_2 = SLOTTYPE_EMBEDDED; |
| 200 | |
| 201 | regmap_update_bits(plat->base, CTL_CFG_2, SLOTTYPE_MASK, ctl_cfg_2); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 206 | static int am654_sdhci_probe(struct udevice *dev) |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 207 | { |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 208 | struct am654_sdhci_plat *plat = dev_get_platdata(dev); |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 209 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 210 | struct sdhci_host *host = dev_get_priv(dev); |
Faiz Abbas | ce142ff | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 211 | struct mmc_config *cfg = &plat->cfg; |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 212 | struct power_domain sdhci_pwrdmn; |
| 213 | struct clk clk; |
| 214 | unsigned long clock; |
| 215 | int ret; |
| 216 | |
| 217 | ret = power_domain_get_by_index(dev, &sdhci_pwrdmn, 0); |
Andreas Dannenberg | 70942db | 2019-06-04 17:55:44 -0500 | [diff] [blame] | 218 | if (!ret) { |
| 219 | ret = power_domain_on(&sdhci_pwrdmn); |
| 220 | if (ret) { |
| 221 | dev_err(dev, "Power domain on failed (%d)\n", ret); |
| 222 | return ret; |
| 223 | } |
| 224 | } else if (ret != -ENOENT && ret != -ENODEV && ret != -ENOSYS) { |
| 225 | dev_err(dev, "failed to get power domain (%d)\n", ret); |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | ret = clk_get_by_index(dev, 0, &clk); |
| 230 | if (ret) { |
| 231 | dev_err(dev, "failed to get clock\n"); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | clock = clk_get_rate(&clk); |
| 236 | if (IS_ERR_VALUE(clock)) { |
| 237 | dev_err(dev, "failed to get rate\n"); |
| 238 | return clock; |
| 239 | } |
| 240 | |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 241 | host->max_clk = clock; |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 242 | host->mmc = &plat->mmc; |
Faiz Abbas | ce142ff | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 243 | host->mmc->dev = dev; |
| 244 | ret = sdhci_setup_cfg(cfg, host, cfg->f_max, |
| 245 | AM654_SDHCI_MIN_FREQ); |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 246 | if (ret) |
| 247 | return ret; |
Faiz Abbas | ce142ff | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 248 | host->ops = &am654_sdhci_ops; |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 249 | host->mmc->priv = host; |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 250 | upriv->mmc = host->mmc; |
| 251 | |
Faiz Abbas | ce142ff | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 252 | regmap_init_mem_index(dev_ofnode(dev), &plat->base, 1); |
| 253 | |
| 254 | am654_sdhci_init(plat); |
| 255 | |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 256 | return sdhci_probe(dev); |
| 257 | } |
| 258 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 259 | static int am654_sdhci_ofdata_to_platdata(struct udevice *dev) |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 260 | { |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 261 | struct am654_sdhci_plat *plat = dev_get_platdata(dev); |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 262 | struct sdhci_host *host = dev_get_priv(dev); |
Faiz Abbas | ce142ff | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 263 | struct mmc_config *cfg = &plat->cfg; |
| 264 | u32 drv_strength; |
| 265 | int ret; |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 266 | |
| 267 | host->name = dev->name; |
| 268 | host->ioaddr = (void *)dev_read_addr(dev); |
Faiz Abbas | ce142ff | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 269 | plat->non_removable = dev_read_bool(dev, "non-removable"); |
| 270 | |
| 271 | ret = dev_read_u32(dev, "ti,trm-icp", &plat->trm_icp); |
| 272 | if (ret) |
| 273 | return ret; |
| 274 | |
| 275 | ret = dev_read_u32(dev, "ti,otap-del-sel", &plat->otap_del_sel); |
| 276 | if (ret) |
| 277 | return ret; |
| 278 | |
| 279 | ret = dev_read_u32(dev, "ti,driver-strength-ohm", &drv_strength); |
| 280 | if (ret) |
| 281 | return ret; |
| 282 | |
| 283 | switch (drv_strength) { |
| 284 | case 50: |
| 285 | plat->drv_strength = DRIVER_STRENGTH_50_OHM; |
| 286 | break; |
| 287 | case 33: |
| 288 | plat->drv_strength = DRIVER_STRENGTH_33_OHM; |
| 289 | break; |
| 290 | case 66: |
| 291 | plat->drv_strength = DRIVER_STRENGTH_66_OHM; |
| 292 | break; |
| 293 | case 100: |
| 294 | plat->drv_strength = DRIVER_STRENGTH_100_OHM; |
| 295 | break; |
| 296 | case 40: |
| 297 | plat->drv_strength = DRIVER_STRENGTH_40_OHM; |
| 298 | break; |
| 299 | default: |
| 300 | dev_err(dev, "Invalid driver strength\n"); |
| 301 | return -EINVAL; |
| 302 | } |
| 303 | |
| 304 | ret = mmc_of_parse(dev, cfg); |
| 305 | if (ret) |
| 306 | return ret; |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 311 | static int am654_sdhci_bind(struct udevice *dev) |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 312 | { |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 313 | struct am654_sdhci_plat *plat = dev_get_platdata(dev); |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 314 | |
| 315 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 316 | } |
| 317 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 318 | static const struct udevice_id am654_sdhci_ids[] = { |
| 319 | { .compatible = "ti,am654-sdhci-5.1" }, |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 320 | { } |
| 321 | }; |
| 322 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 323 | U_BOOT_DRIVER(am654_sdhci_drv) = { |
| 324 | .name = "am654_sdhci", |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 325 | .id = UCLASS_MMC, |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 326 | .of_match = am654_sdhci_ids, |
| 327 | .ofdata_to_platdata = am654_sdhci_ofdata_to_platdata, |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 328 | .ops = &sdhci_ops, |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 329 | .bind = am654_sdhci_bind, |
| 330 | .probe = am654_sdhci_probe, |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 331 | .priv_auto_alloc_size = sizeof(struct sdhci_host), |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 332 | .platdata_auto_alloc_size = sizeof(struct am654_sdhci_plat), |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 333 | }; |