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