Lokesh Vutla | bc9979f | 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 | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 13 | #include <regmap.h> |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 14 | #include <sdhci.h> |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 15 | #include <soc.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 19 | |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 20 | /* CTL_CFG Registers */ |
| 21 | #define CTL_CFG_2 0x14 |
| 22 | |
| 23 | #define SLOTTYPE_MASK GENMASK(31, 30) |
| 24 | #define SLOTTYPE_EMBEDDED BIT(30) |
| 25 | |
| 26 | /* PHY Registers */ |
| 27 | #define PHY_CTRL1 0x100 |
| 28 | #define PHY_CTRL2 0x104 |
| 29 | #define PHY_CTRL3 0x108 |
| 30 | #define PHY_CTRL4 0x10C |
| 31 | #define PHY_CTRL5 0x110 |
| 32 | #define PHY_CTRL6 0x114 |
| 33 | #define PHY_STAT1 0x130 |
| 34 | #define PHY_STAT2 0x134 |
| 35 | |
| 36 | #define IOMUX_ENABLE_SHIFT 31 |
| 37 | #define IOMUX_ENABLE_MASK BIT(IOMUX_ENABLE_SHIFT) |
| 38 | #define OTAPDLYENA_SHIFT 20 |
| 39 | #define OTAPDLYENA_MASK BIT(OTAPDLYENA_SHIFT) |
| 40 | #define OTAPDLYSEL_SHIFT 12 |
| 41 | #define OTAPDLYSEL_MASK GENMASK(15, 12) |
| 42 | #define STRBSEL_SHIFT 24 |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 43 | #define STRBSEL_4BIT_MASK GENMASK(27, 24) |
| 44 | #define STRBSEL_8BIT_MASK GENMASK(31, 24) |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 45 | #define SEL50_SHIFT 8 |
| 46 | #define SEL50_MASK BIT(SEL50_SHIFT) |
| 47 | #define SEL100_SHIFT 9 |
| 48 | #define SEL100_MASK BIT(SEL100_SHIFT) |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 49 | #define FREQSEL_SHIFT 8 |
| 50 | #define FREQSEL_MASK GENMASK(10, 8) |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 51 | #define DLL_TRIM_ICP_SHIFT 4 |
| 52 | #define DLL_TRIM_ICP_MASK GENMASK(7, 4) |
| 53 | #define DR_TY_SHIFT 20 |
| 54 | #define DR_TY_MASK GENMASK(22, 20) |
| 55 | #define ENDLL_SHIFT 1 |
| 56 | #define ENDLL_MASK BIT(ENDLL_SHIFT) |
| 57 | #define DLLRDY_SHIFT 0 |
| 58 | #define DLLRDY_MASK BIT(DLLRDY_SHIFT) |
| 59 | #define PDB_SHIFT 0 |
| 60 | #define PDB_MASK BIT(PDB_SHIFT) |
| 61 | #define CALDONE_SHIFT 1 |
| 62 | #define CALDONE_MASK BIT(CALDONE_SHIFT) |
| 63 | #define RETRIM_SHIFT 17 |
| 64 | #define RETRIM_MASK BIT(RETRIM_SHIFT) |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 65 | #define SELDLYTXCLK_SHIFT 17 |
| 66 | #define SELDLYTXCLK_MASK BIT(SELDLYTXCLK_SHIFT) |
| 67 | #define SELDLYRXCLK_SHIFT 16 |
| 68 | #define SELDLYRXCLK_MASK BIT(SELDLYRXCLK_SHIFT) |
| 69 | #define ITAPDLYSEL_SHIFT 0 |
| 70 | #define ITAPDLYSEL_MASK GENMASK(4, 0) |
| 71 | #define ITAPDLYENA_SHIFT 8 |
| 72 | #define ITAPDLYENA_MASK BIT(ITAPDLYENA_SHIFT) |
| 73 | #define ITAPCHGWIN_SHIFT 9 |
| 74 | #define ITAPCHGWIN_MASK BIT(ITAPCHGWIN_SHIFT) |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 75 | |
| 76 | #define DRIVER_STRENGTH_50_OHM 0x0 |
| 77 | #define DRIVER_STRENGTH_33_OHM 0x1 |
| 78 | #define DRIVER_STRENGTH_66_OHM 0x2 |
| 79 | #define DRIVER_STRENGTH_100_OHM 0x3 |
| 80 | #define DRIVER_STRENGTH_40_OHM 0x4 |
| 81 | |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 82 | #define AM654_SDHCI_MIN_FREQ 400000 |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 83 | #define CLOCK_TOO_SLOW_HZ 50000000 |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 84 | |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 85 | struct am654_sdhci_plat { |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 86 | struct mmc_config cfg; |
| 87 | struct mmc mmc; |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 88 | struct regmap *base; |
| 89 | bool non_removable; |
Faiz Abbas | 7101e12 | 2020-07-29 07:03:41 +0530 | [diff] [blame] | 90 | u32 otap_del_sel[MMC_MODES_END]; |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 91 | u32 itap_del_sel[MMC_MODES_END]; |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 92 | u32 trm_icp; |
| 93 | u32 drv_strength; |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 94 | u32 strb_sel; |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 95 | u32 flags; |
Faiz Abbas | b7f57bb | 2021-02-04 15:10:48 +0530 | [diff] [blame] | 96 | #define DLL_PRESENT BIT(0) |
| 97 | #define IOMUX_PRESENT BIT(1) |
| 98 | #define FREQSEL_2_BIT BIT(2) |
| 99 | #define STRBSEL_4_BIT BIT(3) |
Faiz Abbas | 947e8f3 | 2021-02-04 15:10:49 +0530 | [diff] [blame] | 100 | #define DLL_CALIB BIT(4) |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 101 | }; |
| 102 | |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 103 | struct timing_data { |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 104 | const char *otap_binding; |
| 105 | const char *itap_binding; |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 106 | u32 capability; |
| 107 | }; |
| 108 | |
| 109 | static const struct timing_data td[] = { |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 110 | [MMC_LEGACY] = {"ti,otap-del-sel-legacy", |
| 111 | "ti,itap-del-sel-legacy", |
| 112 | 0}, |
| 113 | [MMC_HS] = {"ti,otap-del-sel-mmc-hs", |
| 114 | "ti,itap-del-sel-mms-hs", |
| 115 | MMC_CAP(MMC_HS)}, |
| 116 | [SD_HS] = {"ti,otap-del-sel-sd-hs", |
| 117 | "ti,itap-del-sel-sd-hs", |
| 118 | MMC_CAP(SD_HS)}, |
| 119 | [UHS_SDR12] = {"ti,otap-del-sel-sdr12", |
| 120 | "ti,itap-del-sel-sdr12", |
| 121 | MMC_CAP(UHS_SDR12)}, |
| 122 | [UHS_SDR25] = {"ti,otap-del-sel-sdr25", |
| 123 | "ti,itap-del-sel-sdr25", |
| 124 | MMC_CAP(UHS_SDR25)}, |
| 125 | [UHS_SDR50] = {"ti,otap-del-sel-sdr50", |
| 126 | NULL, |
| 127 | MMC_CAP(UHS_SDR50)}, |
| 128 | [UHS_SDR104] = {"ti,otap-del-sel-sdr104", |
| 129 | NULL, |
| 130 | MMC_CAP(UHS_SDR104)}, |
| 131 | [UHS_DDR50] = {"ti,otap-del-sel-ddr50", |
| 132 | NULL, |
| 133 | MMC_CAP(UHS_DDR50)}, |
| 134 | [MMC_DDR_52] = {"ti,otap-del-sel-ddr52", |
| 135 | "ti,itap-del-sel-ddr52", |
| 136 | MMC_CAP(MMC_DDR_52)}, |
| 137 | [MMC_HS_200] = {"ti,otap-del-sel-hs200", |
| 138 | NULL, |
| 139 | MMC_CAP(MMC_HS_200)}, |
| 140 | [MMC_HS_400] = {"ti,otap-del-sel-hs400", |
| 141 | NULL, |
| 142 | MMC_CAP(MMC_HS_400)}, |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 143 | }; |
| 144 | |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 145 | struct am654_driver_data { |
| 146 | const struct sdhci_ops *ops; |
| 147 | u32 flags; |
| 148 | }; |
| 149 | |
Faiz Abbas | 7eecee6 | 2019-06-11 00:43:41 +0530 | [diff] [blame] | 150 | static void am654_sdhci_set_control_reg(struct sdhci_host *host) |
| 151 | { |
| 152 | struct mmc *mmc = (struct mmc *)host->mmc; |
| 153 | u32 reg; |
| 154 | |
| 155 | if (IS_SD(host->mmc) && |
| 156 | mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) { |
| 157 | reg = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 158 | reg |= SDHCI_CTRL_VDD_180; |
| 159 | sdhci_writew(host, reg, SDHCI_HOST_CONTROL2); |
| 160 | } |
| 161 | |
| 162 | sdhci_set_uhs_timing(host); |
| 163 | } |
| 164 | |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 165 | static int am654_sdhci_setup_dll(struct am654_sdhci_plat *plat, |
| 166 | unsigned int speed) |
| 167 | { |
| 168 | int sel50, sel100, freqsel; |
| 169 | u32 mask, val; |
| 170 | int ret; |
| 171 | |
| 172 | /* Disable delay chain mode */ |
| 173 | regmap_update_bits(plat->base, PHY_CTRL5, |
| 174 | SELDLYTXCLK_MASK | SELDLYRXCLK_MASK, 0); |
| 175 | |
| 176 | if (plat->flags & FREQSEL_2_BIT) { |
| 177 | switch (speed) { |
| 178 | case 200000000: |
| 179 | sel50 = 0; |
| 180 | sel100 = 0; |
| 181 | break; |
| 182 | case 100000000: |
| 183 | sel50 = 0; |
| 184 | sel100 = 1; |
| 185 | break; |
| 186 | default: |
| 187 | sel50 = 1; |
| 188 | sel100 = 0; |
| 189 | } |
| 190 | |
| 191 | /* Configure PHY DLL frequency */ |
| 192 | mask = SEL50_MASK | SEL100_MASK; |
| 193 | val = (sel50 << SEL50_SHIFT) | (sel100 << SEL100_SHIFT); |
| 194 | regmap_update_bits(plat->base, PHY_CTRL5, mask, val); |
| 195 | } else { |
| 196 | switch (speed) { |
| 197 | case 200000000: |
| 198 | freqsel = 0x0; |
| 199 | break; |
| 200 | default: |
| 201 | freqsel = 0x4; |
| 202 | } |
| 203 | regmap_update_bits(plat->base, PHY_CTRL5, FREQSEL_MASK, |
| 204 | freqsel << FREQSEL_SHIFT); |
| 205 | } |
| 206 | |
| 207 | /* Configure DLL TRIM */ |
| 208 | mask = DLL_TRIM_ICP_MASK; |
| 209 | val = plat->trm_icp << DLL_TRIM_ICP_SHIFT; |
| 210 | |
| 211 | /* Configure DLL driver strength */ |
| 212 | mask |= DR_TY_MASK; |
| 213 | val |= plat->drv_strength << DR_TY_SHIFT; |
| 214 | regmap_update_bits(plat->base, PHY_CTRL1, mask, val); |
| 215 | |
| 216 | /* Enable DLL */ |
| 217 | regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK, |
| 218 | 0x1 << ENDLL_SHIFT); |
| 219 | /* |
| 220 | * Poll for DLL ready. Use a one second timeout. |
| 221 | * Works in all experiments done so far |
| 222 | */ |
| 223 | ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, val, |
| 224 | val & DLLRDY_MASK, 1000, 1000000); |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static void am654_sdhci_write_itapdly(struct am654_sdhci_plat *plat, |
| 230 | u32 itapdly) |
| 231 | { |
| 232 | /* Set ITAPCHGWIN before writing to ITAPDLY */ |
| 233 | regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK, |
| 234 | 1 << ITAPCHGWIN_SHIFT); |
| 235 | regmap_update_bits(plat->base, PHY_CTRL4, ITAPDLYSEL_MASK, |
| 236 | itapdly << ITAPDLYSEL_SHIFT); |
| 237 | regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK, 0); |
| 238 | } |
| 239 | |
| 240 | static void am654_sdhci_setup_delay_chain(struct am654_sdhci_plat *plat, |
| 241 | int mode) |
| 242 | { |
| 243 | u32 mask, val; |
| 244 | |
| 245 | val = 1 << SELDLYTXCLK_SHIFT | 1 << SELDLYRXCLK_SHIFT; |
| 246 | mask = SELDLYTXCLK_MASK | SELDLYRXCLK_MASK; |
| 247 | regmap_update_bits(plat->base, PHY_CTRL5, mask, val); |
| 248 | |
| 249 | am654_sdhci_write_itapdly(plat, plat->itap_del_sel[mode]); |
| 250 | } |
| 251 | |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 252 | static int am654_sdhci_set_ios_post(struct sdhci_host *host) |
| 253 | { |
| 254 | struct udevice *dev = host->mmc->dev; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 255 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 256 | unsigned int speed = host->mmc->clock; |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 257 | int mode = host->mmc->selected_mode; |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 258 | u32 otap_del_sel; |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 259 | u32 mask, val; |
| 260 | int ret; |
| 261 | |
| 262 | /* Reset SD Clock Enable */ |
| 263 | val = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 264 | val &= ~SDHCI_CLOCK_CARD_EN; |
| 265 | sdhci_writew(host, val, SDHCI_CLOCK_CONTROL); |
| 266 | |
Faiz Abbas | 2c45a2c | 2021-02-04 15:10:47 +0530 | [diff] [blame] | 267 | regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK, 0); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 268 | |
| 269 | /* restart clock */ |
| 270 | sdhci_set_clock(host->mmc, speed); |
| 271 | |
| 272 | /* switch phy back on */ |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 273 | otap_del_sel = plat->otap_del_sel[mode]; |
| 274 | mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK; |
| 275 | val = (1 << OTAPDLYENA_SHIFT) | |
| 276 | (otap_del_sel << OTAPDLYSEL_SHIFT); |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 277 | |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 278 | /* Write to STRBSEL for HS400 speed mode */ |
| 279 | if (host->mmc->selected_mode == MMC_HS_400) { |
| 280 | if (plat->flags & STRBSEL_4_BIT) |
| 281 | mask |= STRBSEL_4BIT_MASK; |
| 282 | else |
| 283 | mask |= STRBSEL_8BIT_MASK; |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 284 | |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 285 | val |= plat->strb_sel << STRBSEL_SHIFT; |
| 286 | } |
Faiz Abbas | 947e8f3 | 2021-02-04 15:10:49 +0530 | [diff] [blame] | 287 | |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 288 | regmap_update_bits(plat->base, PHY_CTRL4, mask, val); |
Faiz Abbas | 947e8f3 | 2021-02-04 15:10:49 +0530 | [diff] [blame] | 289 | |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 290 | if (mode > UHS_SDR25 && speed >= CLOCK_TOO_SLOW_HZ) { |
| 291 | ret = am654_sdhci_setup_dll(plat, speed); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 292 | if (ret) |
| 293 | return ret; |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 294 | } else { |
| 295 | am654_sdhci_setup_delay_chain(plat, mode); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 301 | int am654_sdhci_init(struct am654_sdhci_plat *plat) |
| 302 | { |
| 303 | u32 ctl_cfg_2 = 0; |
| 304 | u32 mask, val; |
| 305 | int ret; |
| 306 | |
| 307 | /* Reset OTAP to default value */ |
| 308 | mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK; |
| 309 | regmap_update_bits(plat->base, PHY_CTRL4, mask, 0x0); |
| 310 | |
Faiz Abbas | 947e8f3 | 2021-02-04 15:10:49 +0530 | [diff] [blame] | 311 | if (plat->flags & DLL_CALIB) { |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 312 | regmap_read(plat->base, PHY_STAT1, &val); |
| 313 | if (~val & CALDONE_MASK) { |
| 314 | /* Calibrate IO lines */ |
| 315 | regmap_update_bits(plat->base, PHY_CTRL1, PDB_MASK, |
| 316 | PDB_MASK); |
| 317 | ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, |
| 318 | val, val & CALDONE_MASK, |
| 319 | 1, 20); |
| 320 | if (ret) |
| 321 | return ret; |
| 322 | } |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 323 | } |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 324 | |
| 325 | /* Enable pins by setting IO mux to 0 */ |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 326 | if (plat->flags & IOMUX_PRESENT) |
| 327 | regmap_update_bits(plat->base, PHY_CTRL1, IOMUX_ENABLE_MASK, 0); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 328 | |
| 329 | /* Set slot type based on SD or eMMC */ |
| 330 | if (plat->non_removable) |
| 331 | ctl_cfg_2 = SLOTTYPE_EMBEDDED; |
| 332 | |
| 333 | regmap_update_bits(plat->base, CTL_CFG_2, SLOTTYPE_MASK, ctl_cfg_2); |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 338 | #define MAX_SDCD_DEBOUNCE_TIME 2000 |
| 339 | static int am654_sdhci_deferred_probe(struct sdhci_host *host) |
| 340 | { |
| 341 | struct udevice *dev = host->mmc->dev; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 342 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 343 | unsigned long start; |
| 344 | int val; |
| 345 | |
| 346 | /* |
| 347 | * The controller takes about 1 second to debounce the card detect line |
| 348 | * and doesn't let us power on until that time is up. Instead of waiting |
| 349 | * for 1 second at every stage, poll on the CARD_PRESENT bit upto a |
| 350 | * maximum of 2 seconds to be safe.. |
| 351 | */ |
| 352 | start = get_timer(0); |
| 353 | do { |
| 354 | if (get_timer(start) > MAX_SDCD_DEBOUNCE_TIME) |
| 355 | return -ENOMEDIUM; |
| 356 | |
| 357 | val = mmc_getcd(host->mmc); |
| 358 | } while (!val); |
| 359 | |
| 360 | am654_sdhci_init(plat); |
| 361 | |
| 362 | return sdhci_probe(dev); |
| 363 | } |
| 364 | |
| 365 | const struct sdhci_ops am654_sdhci_ops = { |
| 366 | .deferred_probe = am654_sdhci_deferred_probe, |
| 367 | .set_ios_post = &am654_sdhci_set_ios_post, |
| 368 | .set_control_reg = &am654_sdhci_set_control_reg, |
| 369 | }; |
| 370 | |
| 371 | const struct am654_driver_data am654_drv_data = { |
| 372 | .ops = &am654_sdhci_ops, |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 373 | .flags = DLL_PRESENT | IOMUX_PRESENT | FREQSEL_2_BIT | STRBSEL_4_BIT, |
| 374 | }; |
| 375 | |
| 376 | const struct am654_driver_data am654_sr1_drv_data = { |
| 377 | .ops = &am654_sdhci_ops, |
Faiz Abbas | 947e8f3 | 2021-02-04 15:10:49 +0530 | [diff] [blame] | 378 | .flags = IOMUX_PRESENT | FREQSEL_2_BIT | DLL_PRESENT | DLL_CALIB | |
| 379 | STRBSEL_4_BIT, |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | const struct am654_driver_data j721e_8bit_drv_data = { |
| 383 | .ops = &am654_sdhci_ops, |
Faiz Abbas | 947e8f3 | 2021-02-04 15:10:49 +0530 | [diff] [blame] | 384 | .flags = DLL_PRESENT | DLL_CALIB, |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | static int j721e_4bit_sdhci_set_ios_post(struct sdhci_host *host) |
| 388 | { |
| 389 | struct udevice *dev = host->mmc->dev; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 390 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 391 | u32 otap_del_sel, mask, val; |
| 392 | |
| 393 | otap_del_sel = plat->otap_del_sel[host->mmc->selected_mode]; |
| 394 | mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK; |
| 395 | val = (1 << OTAPDLYENA_SHIFT) | (otap_del_sel << OTAPDLYSEL_SHIFT); |
| 396 | regmap_update_bits(plat->base, PHY_CTRL4, mask, val); |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | const struct sdhci_ops j721e_4bit_sdhci_ops = { |
| 402 | .deferred_probe = am654_sdhci_deferred_probe, |
| 403 | .set_ios_post = &j721e_4bit_sdhci_set_ios_post, |
| 404 | }; |
| 405 | |
| 406 | const struct am654_driver_data j721e_4bit_drv_data = { |
| 407 | .ops = &j721e_4bit_sdhci_ops, |
| 408 | .flags = IOMUX_PRESENT, |
| 409 | }; |
| 410 | |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 411 | const struct soc_attr am654_sdhci_soc_attr[] = { |
| 412 | { .family = "AM65X", .revision = "SR1.0", .data = &am654_sr1_drv_data}, |
| 413 | {/* sentinel */} |
| 414 | }; |
| 415 | |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 416 | static int sdhci_am654_get_otap_delay(struct udevice *dev, |
| 417 | struct mmc_config *cfg) |
| 418 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 419 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 420 | int ret; |
| 421 | int i; |
| 422 | |
| 423 | /* ti,otap-del-sel-legacy is mandatory */ |
| 424 | ret = dev_read_u32(dev, "ti,otap-del-sel-legacy", |
| 425 | &plat->otap_del_sel[0]); |
| 426 | if (ret) |
| 427 | return ret; |
| 428 | /* |
| 429 | * Remove the corresponding capability if an otap-del-sel |
| 430 | * value is not found |
| 431 | */ |
| 432 | for (i = MMC_HS; i <= MMC_HS_400; i++) { |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 433 | ret = dev_read_u32(dev, td[i].otap_binding, |
| 434 | &plat->otap_del_sel[i]); |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 435 | if (ret) { |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 436 | dev_dbg(dev, "Couldn't find %s\n", td[i].otap_binding); |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 437 | /* |
| 438 | * Remove the corresponding capability |
| 439 | * if an otap-del-sel value is not found |
| 440 | */ |
| 441 | cfg->host_caps &= ~td[i].capability; |
| 442 | } |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame^] | 443 | |
| 444 | if (td[i].itap_binding) |
| 445 | dev_read_u32(dev, td[i].itap_binding, |
| 446 | &plat->itap_del_sel[i]); |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 452 | static int am654_sdhci_probe(struct udevice *dev) |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 453 | { |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 454 | struct am654_driver_data *drv_data = |
| 455 | (struct am654_driver_data *)dev_get_driver_data(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 456 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 457 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 458 | struct sdhci_host *host = dev_get_priv(dev); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 459 | struct mmc_config *cfg = &plat->cfg; |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 460 | const struct soc_attr *soc; |
| 461 | const struct am654_driver_data *soc_drv_data; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 462 | struct clk clk; |
| 463 | unsigned long clock; |
| 464 | int ret; |
| 465 | |
Faiz Abbas | dc2bcc2 | 2020-01-16 19:42:18 +0530 | [diff] [blame] | 466 | ret = clk_get_by_name(dev, "clk_xin", &clk); |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 467 | if (ret) { |
| 468 | dev_err(dev, "failed to get clock\n"); |
| 469 | return ret; |
| 470 | } |
| 471 | |
| 472 | clock = clk_get_rate(&clk); |
| 473 | if (IS_ERR_VALUE(clock)) { |
| 474 | dev_err(dev, "failed to get rate\n"); |
| 475 | return clock; |
| 476 | } |
| 477 | |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 478 | host->max_clk = clock; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 479 | host->mmc = &plat->mmc; |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 480 | host->mmc->dev = dev; |
| 481 | ret = sdhci_setup_cfg(cfg, host, cfg->f_max, |
| 482 | AM654_SDHCI_MIN_FREQ); |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 483 | if (ret) |
| 484 | return ret; |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 485 | |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 486 | ret = sdhci_am654_get_otap_delay(dev, cfg); |
| 487 | if (ret) |
| 488 | return ret; |
| 489 | |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 490 | host->ops = drv_data->ops; |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 491 | |
| 492 | /* Update ops based on SoC revision */ |
| 493 | soc = soc_device_match(am654_sdhci_soc_attr); |
| 494 | if (soc && soc->data) { |
| 495 | soc_drv_data = soc->data; |
| 496 | host->ops = soc_drv_data->ops; |
| 497 | } |
| 498 | |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 499 | host->mmc->priv = host; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 500 | upriv->mmc = host->mmc; |
| 501 | |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 502 | regmap_init_mem_index(dev_ofnode(dev), &plat->base, 1); |
| 503 | |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 504 | return 0; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 505 | } |
| 506 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 507 | static int am654_sdhci_of_to_plat(struct udevice *dev) |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 508 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 509 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 510 | struct sdhci_host *host = dev_get_priv(dev); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 511 | struct mmc_config *cfg = &plat->cfg; |
| 512 | u32 drv_strength; |
| 513 | int ret; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 514 | |
| 515 | host->name = dev->name; |
| 516 | host->ioaddr = (void *)dev_read_addr(dev); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 517 | plat->non_removable = dev_read_bool(dev, "non-removable"); |
| 518 | |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 519 | if (plat->flags & DLL_PRESENT) { |
| 520 | ret = dev_read_u32(dev, "ti,trm-icp", &plat->trm_icp); |
| 521 | if (ret) |
| 522 | return ret; |
| 523 | |
| 524 | ret = dev_read_u32(dev, "ti,driver-strength-ohm", |
| 525 | &drv_strength); |
| 526 | if (ret) |
| 527 | return ret; |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 528 | |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 529 | switch (drv_strength) { |
| 530 | case 50: |
| 531 | plat->drv_strength = DRIVER_STRENGTH_50_OHM; |
| 532 | break; |
| 533 | case 33: |
| 534 | plat->drv_strength = DRIVER_STRENGTH_33_OHM; |
| 535 | break; |
| 536 | case 66: |
| 537 | plat->drv_strength = DRIVER_STRENGTH_66_OHM; |
| 538 | break; |
| 539 | case 100: |
| 540 | plat->drv_strength = DRIVER_STRENGTH_100_OHM; |
| 541 | break; |
| 542 | case 40: |
| 543 | plat->drv_strength = DRIVER_STRENGTH_40_OHM; |
| 544 | break; |
| 545 | default: |
| 546 | dev_err(dev, "Invalid driver strength\n"); |
| 547 | return -EINVAL; |
| 548 | } |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | ret = mmc_of_parse(dev, cfg); |
| 552 | if (ret) |
| 553 | return ret; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 558 | static int am654_sdhci_bind(struct udevice *dev) |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 559 | { |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 560 | struct am654_driver_data *drv_data = |
| 561 | (struct am654_driver_data *)dev_get_driver_data(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 562 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 563 | const struct soc_attr *soc; |
| 564 | const struct am654_driver_data *soc_drv_data; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 565 | |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 566 | plat->flags = drv_data->flags; |
| 567 | |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 568 | /* Update flags based on SoC revision */ |
| 569 | soc = soc_device_match(am654_sdhci_soc_attr); |
| 570 | if (soc && soc->data) { |
| 571 | soc_drv_data = soc->data; |
| 572 | plat->flags = soc_drv_data->flags; |
| 573 | } |
| 574 | |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 575 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 576 | } |
| 577 | |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 578 | static const struct udevice_id am654_sdhci_ids[] = { |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 579 | { |
| 580 | .compatible = "ti,am654-sdhci-5.1", |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 581 | .data = (ulong)&am654_drv_data, |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 582 | }, |
| 583 | { |
| 584 | .compatible = "ti,j721e-sdhci-8bit", |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 585 | .data = (ulong)&j721e_8bit_drv_data, |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 586 | }, |
| 587 | { |
| 588 | .compatible = "ti,j721e-sdhci-4bit", |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 589 | .data = (ulong)&j721e_4bit_drv_data, |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 590 | }, |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 591 | { } |
| 592 | }; |
| 593 | |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 594 | U_BOOT_DRIVER(am654_sdhci_drv) = { |
| 595 | .name = "am654_sdhci", |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 596 | .id = UCLASS_MMC, |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 597 | .of_match = am654_sdhci_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 598 | .of_to_plat = am654_sdhci_of_to_plat, |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 599 | .ops = &sdhci_ops, |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 600 | .bind = am654_sdhci_bind, |
| 601 | .probe = am654_sdhci_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 602 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 603 | .plat_auto = sizeof(struct am654_sdhci_plat), |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 604 | }; |