Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Nishanth Menon | eaa39c6 | 2023-11-01 15:56:03 -0500 | [diff] [blame] | 3 | * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/ |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 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> |
Faiz Abbas | 7a7e2c7 | 2021-02-04 15:10:53 +0530 | [diff] [blame] | 12 | #include <mmc.h> |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 13 | #include <power-domain.h> |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 14 | #include <regmap.h> |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 15 | #include <sdhci.h> |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 16 | #include <soc.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 20 | |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 21 | /* CTL_CFG Registers */ |
| 22 | #define CTL_CFG_2 0x14 |
| 23 | |
| 24 | #define SLOTTYPE_MASK GENMASK(31, 30) |
| 25 | #define SLOTTYPE_EMBEDDED BIT(30) |
| 26 | |
| 27 | /* PHY Registers */ |
| 28 | #define PHY_CTRL1 0x100 |
| 29 | #define PHY_CTRL2 0x104 |
| 30 | #define PHY_CTRL3 0x108 |
| 31 | #define PHY_CTRL4 0x10C |
| 32 | #define PHY_CTRL5 0x110 |
| 33 | #define PHY_CTRL6 0x114 |
| 34 | #define PHY_STAT1 0x130 |
| 35 | #define PHY_STAT2 0x134 |
| 36 | |
| 37 | #define IOMUX_ENABLE_SHIFT 31 |
| 38 | #define IOMUX_ENABLE_MASK BIT(IOMUX_ENABLE_SHIFT) |
| 39 | #define OTAPDLYENA_SHIFT 20 |
| 40 | #define OTAPDLYENA_MASK BIT(OTAPDLYENA_SHIFT) |
| 41 | #define OTAPDLYSEL_SHIFT 12 |
| 42 | #define OTAPDLYSEL_MASK GENMASK(15, 12) |
| 43 | #define STRBSEL_SHIFT 24 |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 44 | #define STRBSEL_4BIT_MASK GENMASK(27, 24) |
| 45 | #define STRBSEL_8BIT_MASK GENMASK(31, 24) |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 46 | #define SEL50_SHIFT 8 |
| 47 | #define SEL50_MASK BIT(SEL50_SHIFT) |
| 48 | #define SEL100_SHIFT 9 |
| 49 | #define SEL100_MASK BIT(SEL100_SHIFT) |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 50 | #define FREQSEL_SHIFT 8 |
| 51 | #define FREQSEL_MASK GENMASK(10, 8) |
Faiz Abbas | c73f04e | 2021-02-04 15:10:52 +0530 | [diff] [blame] | 52 | #define CLKBUFSEL_SHIFT 0 |
| 53 | #define CLKBUFSEL_MASK GENMASK(2, 0) |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 54 | #define DLL_TRIM_ICP_SHIFT 4 |
| 55 | #define DLL_TRIM_ICP_MASK GENMASK(7, 4) |
| 56 | #define DR_TY_SHIFT 20 |
| 57 | #define DR_TY_MASK GENMASK(22, 20) |
| 58 | #define ENDLL_SHIFT 1 |
| 59 | #define ENDLL_MASK BIT(ENDLL_SHIFT) |
| 60 | #define DLLRDY_SHIFT 0 |
| 61 | #define DLLRDY_MASK BIT(DLLRDY_SHIFT) |
| 62 | #define PDB_SHIFT 0 |
| 63 | #define PDB_MASK BIT(PDB_SHIFT) |
| 64 | #define CALDONE_SHIFT 1 |
| 65 | #define CALDONE_MASK BIT(CALDONE_SHIFT) |
| 66 | #define RETRIM_SHIFT 17 |
| 67 | #define RETRIM_MASK BIT(RETRIM_SHIFT) |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 68 | #define SELDLYTXCLK_SHIFT 17 |
| 69 | #define SELDLYTXCLK_MASK BIT(SELDLYTXCLK_SHIFT) |
| 70 | #define SELDLYRXCLK_SHIFT 16 |
| 71 | #define SELDLYRXCLK_MASK BIT(SELDLYRXCLK_SHIFT) |
| 72 | #define ITAPDLYSEL_SHIFT 0 |
| 73 | #define ITAPDLYSEL_MASK GENMASK(4, 0) |
| 74 | #define ITAPDLYENA_SHIFT 8 |
| 75 | #define ITAPDLYENA_MASK BIT(ITAPDLYENA_SHIFT) |
| 76 | #define ITAPCHGWIN_SHIFT 9 |
| 77 | #define ITAPCHGWIN_MASK BIT(ITAPCHGWIN_SHIFT) |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 78 | |
| 79 | #define DRIVER_STRENGTH_50_OHM 0x0 |
| 80 | #define DRIVER_STRENGTH_33_OHM 0x1 |
| 81 | #define DRIVER_STRENGTH_66_OHM 0x2 |
| 82 | #define DRIVER_STRENGTH_100_OHM 0x3 |
| 83 | #define DRIVER_STRENGTH_40_OHM 0x4 |
| 84 | |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 85 | #define AM654_SDHCI_MIN_FREQ 400000 |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 86 | #define CLOCK_TOO_SLOW_HZ 50000000 |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 87 | |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 88 | struct am654_sdhci_plat { |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 89 | struct mmc_config cfg; |
| 90 | struct mmc mmc; |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 91 | struct regmap *base; |
| 92 | bool non_removable; |
Faiz Abbas | 7101e12 | 2020-07-29 07:03:41 +0530 | [diff] [blame] | 93 | u32 otap_del_sel[MMC_MODES_END]; |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 94 | u32 itap_del_sel[MMC_MODES_END]; |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 95 | u32 trm_icp; |
| 96 | u32 drv_strength; |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 97 | u32 strb_sel; |
Faiz Abbas | c73f04e | 2021-02-04 15:10:52 +0530 | [diff] [blame] | 98 | u32 clkbuf_sel; |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 99 | u32 flags; |
Judith Mendez | 81c1d44 | 2024-04-18 14:00:56 -0500 | [diff] [blame] | 100 | bool dll_enable; |
Faiz Abbas | b7f57bb | 2021-02-04 15:10:48 +0530 | [diff] [blame] | 101 | #define DLL_PRESENT BIT(0) |
| 102 | #define IOMUX_PRESENT BIT(1) |
| 103 | #define FREQSEL_2_BIT BIT(2) |
| 104 | #define STRBSEL_4_BIT BIT(3) |
Faiz Abbas | 947e8f3 | 2021-02-04 15:10:49 +0530 | [diff] [blame] | 105 | #define DLL_CALIB BIT(4) |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 106 | }; |
| 107 | |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 108 | struct timing_data { |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 109 | const char *otap_binding; |
| 110 | const char *itap_binding; |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 111 | u32 capability; |
| 112 | }; |
| 113 | |
Judith Mendez | 81c1d44 | 2024-04-18 14:00:56 -0500 | [diff] [blame] | 114 | struct window { |
| 115 | u8 start; |
| 116 | u8 end; |
| 117 | u8 length; |
| 118 | }; |
| 119 | |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 120 | static const struct timing_data td[] = { |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 121 | [MMC_LEGACY] = {"ti,otap-del-sel-legacy", |
| 122 | "ti,itap-del-sel-legacy", |
| 123 | 0}, |
| 124 | [MMC_HS] = {"ti,otap-del-sel-mmc-hs", |
| 125 | "ti,itap-del-sel-mms-hs", |
| 126 | MMC_CAP(MMC_HS)}, |
| 127 | [SD_HS] = {"ti,otap-del-sel-sd-hs", |
| 128 | "ti,itap-del-sel-sd-hs", |
| 129 | MMC_CAP(SD_HS)}, |
| 130 | [UHS_SDR12] = {"ti,otap-del-sel-sdr12", |
| 131 | "ti,itap-del-sel-sdr12", |
| 132 | MMC_CAP(UHS_SDR12)}, |
| 133 | [UHS_SDR25] = {"ti,otap-del-sel-sdr25", |
| 134 | "ti,itap-del-sel-sdr25", |
| 135 | MMC_CAP(UHS_SDR25)}, |
| 136 | [UHS_SDR50] = {"ti,otap-del-sel-sdr50", |
| 137 | NULL, |
| 138 | MMC_CAP(UHS_SDR50)}, |
| 139 | [UHS_SDR104] = {"ti,otap-del-sel-sdr104", |
| 140 | NULL, |
| 141 | MMC_CAP(UHS_SDR104)}, |
| 142 | [UHS_DDR50] = {"ti,otap-del-sel-ddr50", |
| 143 | NULL, |
| 144 | MMC_CAP(UHS_DDR50)}, |
| 145 | [MMC_DDR_52] = {"ti,otap-del-sel-ddr52", |
| 146 | "ti,itap-del-sel-ddr52", |
| 147 | MMC_CAP(MMC_DDR_52)}, |
| 148 | [MMC_HS_200] = {"ti,otap-del-sel-hs200", |
| 149 | NULL, |
| 150 | MMC_CAP(MMC_HS_200)}, |
| 151 | [MMC_HS_400] = {"ti,otap-del-sel-hs400", |
| 152 | NULL, |
| 153 | MMC_CAP(MMC_HS_400)}, |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 154 | }; |
| 155 | |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 156 | struct am654_driver_data { |
| 157 | const struct sdhci_ops *ops; |
| 158 | u32 flags; |
| 159 | }; |
| 160 | |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 161 | static int am654_sdhci_setup_dll(struct am654_sdhci_plat *plat, |
| 162 | unsigned int speed) |
| 163 | { |
| 164 | int sel50, sel100, freqsel; |
| 165 | u32 mask, val; |
| 166 | int ret; |
| 167 | |
| 168 | /* Disable delay chain mode */ |
| 169 | regmap_update_bits(plat->base, PHY_CTRL5, |
| 170 | SELDLYTXCLK_MASK | SELDLYRXCLK_MASK, 0); |
| 171 | |
| 172 | if (plat->flags & FREQSEL_2_BIT) { |
| 173 | switch (speed) { |
| 174 | case 200000000: |
| 175 | sel50 = 0; |
| 176 | sel100 = 0; |
| 177 | break; |
| 178 | case 100000000: |
| 179 | sel50 = 0; |
| 180 | sel100 = 1; |
| 181 | break; |
| 182 | default: |
| 183 | sel50 = 1; |
| 184 | sel100 = 0; |
| 185 | } |
| 186 | |
| 187 | /* Configure PHY DLL frequency */ |
| 188 | mask = SEL50_MASK | SEL100_MASK; |
| 189 | val = (sel50 << SEL50_SHIFT) | (sel100 << SEL100_SHIFT); |
| 190 | regmap_update_bits(plat->base, PHY_CTRL5, mask, val); |
| 191 | } else { |
| 192 | switch (speed) { |
| 193 | case 200000000: |
| 194 | freqsel = 0x0; |
| 195 | break; |
| 196 | default: |
| 197 | freqsel = 0x4; |
| 198 | } |
| 199 | regmap_update_bits(plat->base, PHY_CTRL5, FREQSEL_MASK, |
| 200 | freqsel << FREQSEL_SHIFT); |
| 201 | } |
| 202 | |
| 203 | /* Configure DLL TRIM */ |
| 204 | mask = DLL_TRIM_ICP_MASK; |
| 205 | val = plat->trm_icp << DLL_TRIM_ICP_SHIFT; |
| 206 | |
| 207 | /* Configure DLL driver strength */ |
| 208 | mask |= DR_TY_MASK; |
| 209 | val |= plat->drv_strength << DR_TY_SHIFT; |
| 210 | regmap_update_bits(plat->base, PHY_CTRL1, mask, val); |
| 211 | |
| 212 | /* Enable DLL */ |
| 213 | regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK, |
| 214 | 0x1 << ENDLL_SHIFT); |
| 215 | /* |
| 216 | * Poll for DLL ready. Use a one second timeout. |
| 217 | * Works in all experiments done so far |
| 218 | */ |
| 219 | ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, val, |
| 220 | val & DLLRDY_MASK, 1000, 1000000); |
| 221 | |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | static void am654_sdhci_write_itapdly(struct am654_sdhci_plat *plat, |
| 226 | u32 itapdly) |
| 227 | { |
| 228 | /* Set ITAPCHGWIN before writing to ITAPDLY */ |
| 229 | regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK, |
| 230 | 1 << ITAPCHGWIN_SHIFT); |
| 231 | regmap_update_bits(plat->base, PHY_CTRL4, ITAPDLYSEL_MASK, |
| 232 | itapdly << ITAPDLYSEL_SHIFT); |
| 233 | regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK, 0); |
| 234 | } |
| 235 | |
| 236 | static void am654_sdhci_setup_delay_chain(struct am654_sdhci_plat *plat, |
| 237 | int mode) |
| 238 | { |
| 239 | u32 mask, val; |
| 240 | |
| 241 | val = 1 << SELDLYTXCLK_SHIFT | 1 << SELDLYRXCLK_SHIFT; |
| 242 | mask = SELDLYTXCLK_MASK | SELDLYRXCLK_MASK; |
| 243 | regmap_update_bits(plat->base, PHY_CTRL5, mask, val); |
| 244 | |
| 245 | am654_sdhci_write_itapdly(plat, plat->itap_del_sel[mode]); |
| 246 | } |
| 247 | |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 248 | static int am654_sdhci_set_ios_post(struct sdhci_host *host) |
| 249 | { |
| 250 | struct udevice *dev = host->mmc->dev; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 251 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 252 | unsigned int speed = host->mmc->clock; |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 253 | int mode = host->mmc->selected_mode; |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 254 | u32 otap_del_sel; |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 255 | u32 mask, val; |
| 256 | int ret; |
| 257 | |
| 258 | /* Reset SD Clock Enable */ |
| 259 | val = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 260 | val &= ~SDHCI_CLOCK_CARD_EN; |
| 261 | sdhci_writew(host, val, SDHCI_CLOCK_CONTROL); |
| 262 | |
Faiz Abbas | 2c45a2c | 2021-02-04 15:10:47 +0530 | [diff] [blame] | 263 | regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK, 0); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 264 | |
| 265 | /* restart clock */ |
| 266 | sdhci_set_clock(host->mmc, speed); |
| 267 | |
| 268 | /* switch phy back on */ |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 269 | otap_del_sel = plat->otap_del_sel[mode]; |
| 270 | mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK; |
| 271 | val = (1 << OTAPDLYENA_SHIFT) | |
| 272 | (otap_del_sel << OTAPDLYSEL_SHIFT); |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 273 | |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 274 | /* Write to STRBSEL for HS400 speed mode */ |
| 275 | if (host->mmc->selected_mode == MMC_HS_400) { |
| 276 | if (plat->flags & STRBSEL_4_BIT) |
| 277 | mask |= STRBSEL_4BIT_MASK; |
| 278 | else |
| 279 | mask |= STRBSEL_8BIT_MASK; |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 280 | |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 281 | val |= plat->strb_sel << STRBSEL_SHIFT; |
| 282 | } |
Faiz Abbas | 947e8f3 | 2021-02-04 15:10:49 +0530 | [diff] [blame] | 283 | |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 284 | regmap_update_bits(plat->base, PHY_CTRL4, mask, val); |
Faiz Abbas | 947e8f3 | 2021-02-04 15:10:49 +0530 | [diff] [blame] | 285 | |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 286 | if (mode > UHS_SDR25 && speed >= CLOCK_TOO_SLOW_HZ) { |
| 287 | ret = am654_sdhci_setup_dll(plat, speed); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 288 | if (ret) |
| 289 | return ret; |
Judith Mendez | 81c1d44 | 2024-04-18 14:00:56 -0500 | [diff] [blame] | 290 | |
| 291 | plat->dll_enable = true; |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 292 | } else { |
| 293 | am654_sdhci_setup_delay_chain(plat, mode); |
Judith Mendez | 81c1d44 | 2024-04-18 14:00:56 -0500 | [diff] [blame] | 294 | plat->dll_enable = false; |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 295 | } |
| 296 | |
Faiz Abbas | c73f04e | 2021-02-04 15:10:52 +0530 | [diff] [blame] | 297 | regmap_update_bits(plat->base, PHY_CTRL5, CLKBUFSEL_MASK, |
| 298 | plat->clkbuf_sel); |
| 299 | |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 303 | int am654_sdhci_init(struct am654_sdhci_plat *plat) |
| 304 | { |
| 305 | u32 ctl_cfg_2 = 0; |
| 306 | u32 mask, val; |
| 307 | int ret; |
| 308 | |
| 309 | /* Reset OTAP to default value */ |
| 310 | mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK; |
| 311 | regmap_update_bits(plat->base, PHY_CTRL4, mask, 0x0); |
| 312 | |
Faiz Abbas | 947e8f3 | 2021-02-04 15:10:49 +0530 | [diff] [blame] | 313 | if (plat->flags & DLL_CALIB) { |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 314 | regmap_read(plat->base, PHY_STAT1, &val); |
| 315 | if (~val & CALDONE_MASK) { |
| 316 | /* Calibrate IO lines */ |
| 317 | regmap_update_bits(plat->base, PHY_CTRL1, PDB_MASK, |
| 318 | PDB_MASK); |
| 319 | ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, |
| 320 | val, val & CALDONE_MASK, |
| 321 | 1, 20); |
| 322 | if (ret) |
| 323 | return ret; |
| 324 | } |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 325 | } |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 326 | |
| 327 | /* Enable pins by setting IO mux to 0 */ |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 328 | if (plat->flags & IOMUX_PRESENT) |
| 329 | regmap_update_bits(plat->base, PHY_CTRL1, IOMUX_ENABLE_MASK, 0); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 330 | |
| 331 | /* Set slot type based on SD or eMMC */ |
| 332 | if (plat->non_removable) |
| 333 | ctl_cfg_2 = SLOTTYPE_EMBEDDED; |
| 334 | |
| 335 | regmap_update_bits(plat->base, CTL_CFG_2, SLOTTYPE_MASK, ctl_cfg_2); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 340 | #define MAX_SDCD_DEBOUNCE_TIME 2000 |
| 341 | static int am654_sdhci_deferred_probe(struct sdhci_host *host) |
| 342 | { |
| 343 | struct udevice *dev = host->mmc->dev; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 344 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 345 | unsigned long start; |
| 346 | int val; |
| 347 | |
| 348 | /* |
| 349 | * The controller takes about 1 second to debounce the card detect line |
| 350 | * and doesn't let us power on until that time is up. Instead of waiting |
| 351 | * for 1 second at every stage, poll on the CARD_PRESENT bit upto a |
| 352 | * maximum of 2 seconds to be safe.. |
| 353 | */ |
| 354 | start = get_timer(0); |
| 355 | do { |
| 356 | if (get_timer(start) > MAX_SDCD_DEBOUNCE_TIME) |
| 357 | return -ENOMEDIUM; |
| 358 | |
| 359 | val = mmc_getcd(host->mmc); |
| 360 | } while (!val); |
| 361 | |
| 362 | am654_sdhci_init(plat); |
| 363 | |
| 364 | return sdhci_probe(dev); |
| 365 | } |
| 366 | |
Faiz Abbas | 36c8c5c | 2021-02-04 15:10:54 +0530 | [diff] [blame] | 367 | static void am654_sdhci_write_b(struct sdhci_host *host, u8 val, int reg) |
| 368 | { |
| 369 | if (reg == SDHCI_HOST_CONTROL) { |
| 370 | switch (host->mmc->selected_mode) { |
| 371 | /* |
| 372 | * According to the data manual, HISPD bit |
| 373 | * should not be set in these speed modes. |
| 374 | */ |
| 375 | case SD_HS: |
| 376 | case MMC_HS: |
| 377 | case UHS_SDR12: |
| 378 | case UHS_SDR25: |
| 379 | val &= ~SDHCI_CTRL_HISPD; |
| 380 | default: |
| 381 | break; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | writeb(val, host->ioaddr + reg); |
| 386 | } |
Faiz Abbas | 7a7e2c7 | 2021-02-04 15:10:53 +0530 | [diff] [blame] | 387 | #ifdef MMC_SUPPORTS_TUNING |
Judith Mendez | 81c1d44 | 2024-04-18 14:00:56 -0500 | [diff] [blame] | 388 | #define ITAPDLY_LENGTH 32 |
| 389 | #define ITAPDLY_LAST_INDEX (ITAPDLY_LENGTH - 1) |
| 390 | |
| 391 | static u32 am654_sdhci_calculate_itap(struct udevice *dev, struct window |
| 392 | *fail_window, u8 num_fails, bool circular_buffer) |
| 393 | { |
| 394 | u8 itap = 0, start_fail = 0, end_fail = 0, pass_length = 0; |
| 395 | u8 first_fail_start = 0, last_fail_end = 0; |
| 396 | struct window pass_window = {0, 0, 0}; |
| 397 | int prev_fail_end = -1; |
| 398 | u8 i; |
| 399 | |
| 400 | if (!num_fails) |
| 401 | return ITAPDLY_LAST_INDEX >> 1; |
| 402 | |
| 403 | if (fail_window->length == ITAPDLY_LENGTH) { |
| 404 | dev_err(dev, "No passing ITAPDLY, return 0\n"); |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | first_fail_start = fail_window->start; |
| 409 | last_fail_end = fail_window[num_fails - 1].end; |
| 410 | |
| 411 | for (i = 0; i < num_fails; i++) { |
| 412 | start_fail = fail_window[i].start; |
| 413 | end_fail = fail_window[i].end; |
| 414 | pass_length = start_fail - (prev_fail_end + 1); |
| 415 | |
| 416 | if (pass_length > pass_window.length) { |
| 417 | pass_window.start = prev_fail_end + 1; |
| 418 | pass_window.length = pass_length; |
| 419 | } |
| 420 | prev_fail_end = end_fail; |
| 421 | } |
| 422 | |
| 423 | if (!circular_buffer) |
| 424 | pass_length = ITAPDLY_LAST_INDEX - last_fail_end; |
| 425 | else |
| 426 | pass_length = ITAPDLY_LAST_INDEX - last_fail_end + first_fail_start; |
| 427 | |
| 428 | if (pass_length > pass_window.length) { |
| 429 | pass_window.start = last_fail_end + 1; |
| 430 | pass_window.length = pass_length; |
| 431 | } |
| 432 | |
| 433 | if (!circular_buffer) |
| 434 | itap = pass_window.start + (pass_window.length >> 1); |
| 435 | else |
| 436 | itap = (pass_window.start + (pass_window.length >> 1)) % ITAPDLY_LENGTH; |
| 437 | |
| 438 | return (itap > ITAPDLY_LAST_INDEX) ? ITAPDLY_LAST_INDEX >> 1 : itap; |
| 439 | } |
| 440 | |
Faiz Abbas | 7a7e2c7 | 2021-02-04 15:10:53 +0530 | [diff] [blame] | 441 | static int am654_sdhci_execute_tuning(struct mmc *mmc, u8 opcode) |
| 442 | { |
| 443 | struct udevice *dev = mmc->dev; |
| 444 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Judith Mendez | 81c1d44 | 2024-04-18 14:00:56 -0500 | [diff] [blame] | 445 | struct window fail_window[ITAPDLY_LENGTH]; |
| 446 | u8 curr_pass, itap; |
| 447 | u8 fail_index = 0; |
| 448 | u8 prev_pass = 1; |
| 449 | |
| 450 | memset(fail_window, 0, sizeof(fail_window)); |
Faiz Abbas | 7a7e2c7 | 2021-02-04 15:10:53 +0530 | [diff] [blame] | 451 | |
| 452 | /* Enable ITAPDLY */ |
| 453 | regmap_update_bits(plat->base, PHY_CTRL4, ITAPDLYENA_MASK, |
| 454 | 1 << ITAPDLYENA_SHIFT); |
| 455 | |
Judith Mendez | 81c1d44 | 2024-04-18 14:00:56 -0500 | [diff] [blame] | 456 | for (itap = 0; itap < ITAPDLY_LENGTH; itap++) { |
Faiz Abbas | 7a7e2c7 | 2021-02-04 15:10:53 +0530 | [diff] [blame] | 457 | am654_sdhci_write_itapdly(plat, itap); |
| 458 | |
Judith Mendez | 81c1d44 | 2024-04-18 14:00:56 -0500 | [diff] [blame] | 459 | curr_pass = !mmc_send_tuning(mmc, opcode); |
| 460 | |
| 461 | if (!curr_pass && prev_pass) |
| 462 | fail_window[fail_index].start = itap; |
Faiz Abbas | 7a7e2c7 | 2021-02-04 15:10:53 +0530 | [diff] [blame] | 463 | |
Judith Mendez | 81c1d44 | 2024-04-18 14:00:56 -0500 | [diff] [blame] | 464 | if (!curr_pass) { |
| 465 | fail_window[fail_index].end = itap; |
| 466 | fail_window[fail_index].length++; |
| 467 | } |
| 468 | |
| 469 | if (curr_pass && !prev_pass) |
| 470 | fail_index++; |
Faiz Abbas | 7a7e2c7 | 2021-02-04 15:10:53 +0530 | [diff] [blame] | 471 | |
Judith Mendez | 81c1d44 | 2024-04-18 14:00:56 -0500 | [diff] [blame] | 472 | prev_pass = curr_pass; |
Faiz Abbas | 7a7e2c7 | 2021-02-04 15:10:53 +0530 | [diff] [blame] | 473 | } |
Judith Mendez | 81c1d44 | 2024-04-18 14:00:56 -0500 | [diff] [blame] | 474 | |
| 475 | if (fail_window[fail_index].length != 0) |
| 476 | fail_index++; |
| 477 | |
| 478 | itap = am654_sdhci_calculate_itap(dev, fail_window, fail_index, |
| 479 | plat->dll_enable); |
| 480 | |
Faiz Abbas | 7a7e2c7 | 2021-02-04 15:10:53 +0530 | [diff] [blame] | 481 | am654_sdhci_write_itapdly(plat, itap); |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | #endif |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 486 | const struct sdhci_ops am654_sdhci_ops = { |
Faiz Abbas | 7a7e2c7 | 2021-02-04 15:10:53 +0530 | [diff] [blame] | 487 | #ifdef MMC_SUPPORTS_TUNING |
| 488 | .platform_execute_tuning = am654_sdhci_execute_tuning, |
| 489 | #endif |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 490 | .deferred_probe = am654_sdhci_deferred_probe, |
| 491 | .set_ios_post = &am654_sdhci_set_ios_post, |
Faiz Abbas | 9c10cfe | 2021-02-04 15:10:55 +0530 | [diff] [blame] | 492 | .set_control_reg = sdhci_set_control_reg, |
Faiz Abbas | 36c8c5c | 2021-02-04 15:10:54 +0530 | [diff] [blame] | 493 | .write_b = am654_sdhci_write_b, |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | const struct am654_driver_data am654_drv_data = { |
| 497 | .ops = &am654_sdhci_ops, |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 498 | .flags = DLL_PRESENT | IOMUX_PRESENT | FREQSEL_2_BIT | STRBSEL_4_BIT, |
| 499 | }; |
| 500 | |
| 501 | const struct am654_driver_data am654_sr1_drv_data = { |
| 502 | .ops = &am654_sdhci_ops, |
Faiz Abbas | 947e8f3 | 2021-02-04 15:10:49 +0530 | [diff] [blame] | 503 | .flags = IOMUX_PRESENT | FREQSEL_2_BIT | DLL_PRESENT | DLL_CALIB | |
| 504 | STRBSEL_4_BIT, |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 505 | }; |
| 506 | |
| 507 | const struct am654_driver_data j721e_8bit_drv_data = { |
| 508 | .ops = &am654_sdhci_ops, |
Faiz Abbas | 947e8f3 | 2021-02-04 15:10:49 +0530 | [diff] [blame] | 509 | .flags = DLL_PRESENT | DLL_CALIB, |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 510 | }; |
| 511 | |
| 512 | static int j721e_4bit_sdhci_set_ios_post(struct sdhci_host *host) |
| 513 | { |
| 514 | struct udevice *dev = host->mmc->dev; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 515 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Nitin Yadav | 83db1f9 | 2024-04-18 14:00:57 -0500 | [diff] [blame^] | 516 | int mode = host->mmc->selected_mode; |
| 517 | u32 otap_del_sel; |
| 518 | u32 itap_del_sel; |
| 519 | u32 mask, val; |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 520 | |
Nitin Yadav | 83db1f9 | 2024-04-18 14:00:57 -0500 | [diff] [blame^] | 521 | otap_del_sel = plat->otap_del_sel[mode]; |
| 522 | |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 523 | mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK; |
Nitin Yadav | 83db1f9 | 2024-04-18 14:00:57 -0500 | [diff] [blame^] | 524 | val = (1 << OTAPDLYENA_SHIFT) | |
| 525 | (otap_del_sel << OTAPDLYSEL_SHIFT); |
| 526 | |
| 527 | itap_del_sel = plat->itap_del_sel[mode]; |
| 528 | |
| 529 | mask |= ITAPDLYENA_MASK | ITAPDLYSEL_MASK; |
| 530 | val |= (1 << ITAPDLYENA_SHIFT) | |
| 531 | (itap_del_sel << ITAPDLYSEL_SHIFT); |
| 532 | |
| 533 | regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK, |
| 534 | 1 << ITAPCHGWIN_SHIFT); |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 535 | regmap_update_bits(plat->base, PHY_CTRL4, mask, val); |
Nitin Yadav | 83db1f9 | 2024-04-18 14:00:57 -0500 | [diff] [blame^] | 536 | regmap_update_bits(plat->base, PHY_CTRL4, ITAPCHGWIN_MASK, 0); |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 537 | |
Faiz Abbas | c73f04e | 2021-02-04 15:10:52 +0530 | [diff] [blame] | 538 | regmap_update_bits(plat->base, PHY_CTRL5, CLKBUFSEL_MASK, |
| 539 | plat->clkbuf_sel); |
| 540 | |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | const struct sdhci_ops j721e_4bit_sdhci_ops = { |
Faiz Abbas | 7a7e2c7 | 2021-02-04 15:10:53 +0530 | [diff] [blame] | 545 | #ifdef MMC_SUPPORTS_TUNING |
| 546 | .platform_execute_tuning = am654_sdhci_execute_tuning, |
| 547 | #endif |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 548 | .deferred_probe = am654_sdhci_deferred_probe, |
| 549 | .set_ios_post = &j721e_4bit_sdhci_set_ios_post, |
Faiz Abbas | 9c10cfe | 2021-02-04 15:10:55 +0530 | [diff] [blame] | 550 | .set_control_reg = sdhci_set_control_reg, |
Faiz Abbas | 36c8c5c | 2021-02-04 15:10:54 +0530 | [diff] [blame] | 551 | .write_b = am654_sdhci_write_b, |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 552 | }; |
| 553 | |
| 554 | const struct am654_driver_data j721e_4bit_drv_data = { |
| 555 | .ops = &j721e_4bit_sdhci_ops, |
| 556 | .flags = IOMUX_PRESENT, |
| 557 | }; |
| 558 | |
Dave Gerlach | 057d6af | 2021-04-23 11:27:40 -0500 | [diff] [blame] | 559 | static const struct am654_driver_data sdhci_am64_8bit_drvdata = { |
| 560 | .ops = &am654_sdhci_ops, |
| 561 | .flags = DLL_PRESENT | DLL_CALIB, |
| 562 | }; |
| 563 | |
| 564 | static const struct am654_driver_data sdhci_am64_4bit_drvdata = { |
| 565 | .ops = &j721e_4bit_sdhci_ops, |
| 566 | .flags = IOMUX_PRESENT, |
| 567 | }; |
| 568 | |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 569 | const struct soc_attr am654_sdhci_soc_attr[] = { |
| 570 | { .family = "AM65X", .revision = "SR1.0", .data = &am654_sr1_drv_data}, |
| 571 | {/* sentinel */} |
| 572 | }; |
| 573 | |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 574 | static int sdhci_am654_get_otap_delay(struct udevice *dev, |
| 575 | struct mmc_config *cfg) |
| 576 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 577 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 578 | int ret; |
| 579 | int i; |
| 580 | |
| 581 | /* ti,otap-del-sel-legacy is mandatory */ |
| 582 | ret = dev_read_u32(dev, "ti,otap-del-sel-legacy", |
| 583 | &plat->otap_del_sel[0]); |
| 584 | if (ret) |
| 585 | return ret; |
| 586 | /* |
| 587 | * Remove the corresponding capability if an otap-del-sel |
| 588 | * value is not found |
| 589 | */ |
Nitin Yadav | 83db1f9 | 2024-04-18 14:00:57 -0500 | [diff] [blame^] | 590 | for (i = MMC_LEGACY; i <= MMC_HS_400; i++) { |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 591 | ret = dev_read_u32(dev, td[i].otap_binding, |
| 592 | &plat->otap_del_sel[i]); |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 593 | if (ret) { |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 594 | dev_dbg(dev, "Couldn't find %s\n", td[i].otap_binding); |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 595 | /* |
| 596 | * Remove the corresponding capability |
| 597 | * if an otap-del-sel value is not found |
| 598 | */ |
| 599 | cfg->host_caps &= ~td[i].capability; |
| 600 | } |
Faiz Abbas | def2a0f | 2021-02-04 15:10:51 +0530 | [diff] [blame] | 601 | |
| 602 | if (td[i].itap_binding) |
| 603 | dev_read_u32(dev, td[i].itap_binding, |
| 604 | &plat->itap_del_sel[i]); |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 610 | static int am654_sdhci_probe(struct udevice *dev) |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 611 | { |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 612 | struct am654_driver_data *drv_data = |
| 613 | (struct am654_driver_data *)dev_get_driver_data(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 614 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 615 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 616 | struct sdhci_host *host = dev_get_priv(dev); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 617 | struct mmc_config *cfg = &plat->cfg; |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 618 | const struct soc_attr *soc; |
| 619 | const struct am654_driver_data *soc_drv_data; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 620 | struct clk clk; |
| 621 | unsigned long clock; |
| 622 | int ret; |
| 623 | |
Faiz Abbas | dc2bcc2 | 2020-01-16 19:42:18 +0530 | [diff] [blame] | 624 | ret = clk_get_by_name(dev, "clk_xin", &clk); |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 625 | if (ret) { |
| 626 | dev_err(dev, "failed to get clock\n"); |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | clock = clk_get_rate(&clk); |
| 631 | if (IS_ERR_VALUE(clock)) { |
| 632 | dev_err(dev, "failed to get rate\n"); |
| 633 | return clock; |
| 634 | } |
| 635 | |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 636 | host->max_clk = clock; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 637 | host->mmc = &plat->mmc; |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 638 | host->mmc->dev = dev; |
Faiz Abbas | 36c8c5c | 2021-02-04 15:10:54 +0530 | [diff] [blame] | 639 | host->ops = drv_data->ops; |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 640 | ret = sdhci_setup_cfg(cfg, host, cfg->f_max, |
| 641 | AM654_SDHCI_MIN_FREQ); |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 642 | if (ret) |
| 643 | return ret; |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 644 | |
Faiz Abbas | c6eb9e7 | 2020-02-26 13:44:33 +0530 | [diff] [blame] | 645 | ret = sdhci_am654_get_otap_delay(dev, cfg); |
| 646 | if (ret) |
| 647 | return ret; |
| 648 | |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 649 | /* Update ops based on SoC revision */ |
| 650 | soc = soc_device_match(am654_sdhci_soc_attr); |
| 651 | if (soc && soc->data) { |
| 652 | soc_drv_data = soc->data; |
| 653 | host->ops = soc_drv_data->ops; |
| 654 | } |
| 655 | |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 656 | host->mmc->priv = host; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 657 | upriv->mmc = host->mmc; |
| 658 | |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 659 | regmap_init_mem_index(dev_ofnode(dev), &plat->base, 1); |
| 660 | |
Faiz Abbas | e4425cb | 2020-02-26 13:44:34 +0530 | [diff] [blame] | 661 | return 0; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 662 | } |
| 663 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 664 | static int am654_sdhci_of_to_plat(struct udevice *dev) |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 665 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 666 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 667 | struct sdhci_host *host = dev_get_priv(dev); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 668 | struct mmc_config *cfg = &plat->cfg; |
| 669 | u32 drv_strength; |
| 670 | int ret; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 671 | |
| 672 | host->name = dev->name; |
Johan Jonker | 8d5d8e0 | 2023-03-13 01:32:04 +0100 | [diff] [blame] | 673 | host->ioaddr = dev_read_addr_ptr(dev); |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 674 | plat->non_removable = dev_read_bool(dev, "non-removable"); |
| 675 | |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 676 | if (plat->flags & DLL_PRESENT) { |
| 677 | ret = dev_read_u32(dev, "ti,trm-icp", &plat->trm_icp); |
| 678 | if (ret) |
| 679 | return ret; |
| 680 | |
| 681 | ret = dev_read_u32(dev, "ti,driver-strength-ohm", |
| 682 | &drv_strength); |
| 683 | if (ret) |
| 684 | return ret; |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 685 | |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 686 | switch (drv_strength) { |
| 687 | case 50: |
| 688 | plat->drv_strength = DRIVER_STRENGTH_50_OHM; |
| 689 | break; |
| 690 | case 33: |
| 691 | plat->drv_strength = DRIVER_STRENGTH_33_OHM; |
| 692 | break; |
| 693 | case 66: |
| 694 | plat->drv_strength = DRIVER_STRENGTH_66_OHM; |
| 695 | break; |
| 696 | case 100: |
| 697 | plat->drv_strength = DRIVER_STRENGTH_100_OHM; |
| 698 | break; |
| 699 | case 40: |
| 700 | plat->drv_strength = DRIVER_STRENGTH_40_OHM; |
| 701 | break; |
| 702 | default: |
| 703 | dev_err(dev, "Invalid driver strength\n"); |
| 704 | return -EINVAL; |
| 705 | } |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 706 | } |
| 707 | |
Aswath Govindraju | 4509fb6 | 2021-05-25 15:08:23 +0530 | [diff] [blame] | 708 | dev_read_u32(dev, "ti,strobe-sel", &plat->strb_sel); |
Faiz Abbas | c73f04e | 2021-02-04 15:10:52 +0530 | [diff] [blame] | 709 | dev_read_u32(dev, "ti,clkbuf-sel", &plat->clkbuf_sel); |
| 710 | |
Faiz Abbas | e9aed58 | 2019-06-11 00:43:38 +0530 | [diff] [blame] | 711 | ret = mmc_of_parse(dev, cfg); |
| 712 | if (ret) |
| 713 | return ret; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 718 | static int am654_sdhci_bind(struct udevice *dev) |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 719 | { |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 720 | struct am654_driver_data *drv_data = |
| 721 | (struct am654_driver_data *)dev_get_driver_data(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 722 | struct am654_sdhci_plat *plat = dev_get_plat(dev); |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 723 | const struct soc_attr *soc; |
| 724 | const struct am654_driver_data *soc_drv_data; |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 725 | |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 726 | plat->flags = drv_data->flags; |
| 727 | |
Faiz Abbas | 2c2fc96 | 2021-02-04 15:10:50 +0530 | [diff] [blame] | 728 | /* Update flags based on SoC revision */ |
| 729 | soc = soc_device_match(am654_sdhci_soc_attr); |
| 730 | if (soc && soc->data) { |
| 731 | soc_drv_data = soc->data; |
| 732 | plat->flags = soc_drv_data->flags; |
| 733 | } |
| 734 | |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 735 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 736 | } |
| 737 | |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 738 | static const struct udevice_id am654_sdhci_ids[] = { |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 739 | { |
| 740 | .compatible = "ti,am654-sdhci-5.1", |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 741 | .data = (ulong)&am654_drv_data, |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 742 | }, |
| 743 | { |
| 744 | .compatible = "ti,j721e-sdhci-8bit", |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 745 | .data = (ulong)&j721e_8bit_drv_data, |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 746 | }, |
| 747 | { |
| 748 | .compatible = "ti,j721e-sdhci-4bit", |
Faiz Abbas | 8cc051e | 2020-01-16 19:42:19 +0530 | [diff] [blame] | 749 | .data = (ulong)&j721e_4bit_drv_data, |
Faiz Abbas | fd8be70 | 2019-06-13 10:29:51 +0530 | [diff] [blame] | 750 | }, |
Dave Gerlach | 057d6af | 2021-04-23 11:27:40 -0500 | [diff] [blame] | 751 | { |
| 752 | .compatible = "ti,am64-sdhci-8bit", |
| 753 | .data = (ulong)&sdhci_am64_8bit_drvdata, |
| 754 | }, |
| 755 | { |
| 756 | .compatible = "ti,am64-sdhci-4bit", |
| 757 | .data = (ulong)&sdhci_am64_4bit_drvdata, |
| 758 | }, |
Aswath Govindraju | 71b9a7b | 2022-05-25 13:38:39 +0530 | [diff] [blame] | 759 | { |
| 760 | .compatible = "ti,am62-sdhci", |
| 761 | .data = (ulong)&sdhci_am64_4bit_drvdata, |
| 762 | }, |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 763 | { } |
| 764 | }; |
| 765 | |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 766 | U_BOOT_DRIVER(am654_sdhci_drv) = { |
| 767 | .name = "am654_sdhci", |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 768 | .id = UCLASS_MMC, |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 769 | .of_match = am654_sdhci_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 770 | .of_to_plat = am654_sdhci_of_to_plat, |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 771 | .ops = &sdhci_ops, |
Faiz Abbas | d8fb309 | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 772 | .bind = am654_sdhci_bind, |
| 773 | .probe = am654_sdhci_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 774 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 775 | .plat_auto = sizeof(struct am654_sdhci_plat), |
Lokesh Vutla | bc9979f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 776 | }; |