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