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