1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
5 * Texas Instruments' K3 SD Host Controller Interface
12 #include <power-domain.h>
15 #include <dm/device_compat.h>
16 #include <linux/bitops.h>
17 #include <linux/err.h>
19 /* CTL_CFG Registers */
20 #define CTL_CFG_2 0x14
22 #define SLOTTYPE_MASK GENMASK(31, 30)
23 #define SLOTTYPE_EMBEDDED BIT(30)
26 #define PHY_CTRL1 0x100
27 #define PHY_CTRL2 0x104
28 #define PHY_CTRL3 0x108
29 #define PHY_CTRL4 0x10C
30 #define PHY_CTRL5 0x110
31 #define PHY_CTRL6 0x114
32 #define PHY_STAT1 0x130
33 #define PHY_STAT2 0x134
35 #define IOMUX_ENABLE_SHIFT 31
36 #define IOMUX_ENABLE_MASK BIT(IOMUX_ENABLE_SHIFT)
37 #define OTAPDLYENA_SHIFT 20
38 #define OTAPDLYENA_MASK BIT(OTAPDLYENA_SHIFT)
39 #define OTAPDLYSEL_SHIFT 12
40 #define OTAPDLYSEL_MASK GENMASK(15, 12)
41 #define STRBSEL_SHIFT 24
42 #define STRBSEL_4BIT_MASK GENMASK(27, 24)
43 #define STRBSEL_8BIT_MASK GENMASK(31, 24)
45 #define SEL50_MASK BIT(SEL50_SHIFT)
46 #define SEL100_SHIFT 9
47 #define SEL100_MASK BIT(SEL100_SHIFT)
48 #define FREQSEL_SHIFT 8
49 #define FREQSEL_MASK GENMASK(10, 8)
50 #define DLL_TRIM_ICP_SHIFT 4
51 #define DLL_TRIM_ICP_MASK GENMASK(7, 4)
52 #define DR_TY_SHIFT 20
53 #define DR_TY_MASK GENMASK(22, 20)
55 #define ENDLL_MASK BIT(ENDLL_SHIFT)
56 #define DLLRDY_SHIFT 0
57 #define DLLRDY_MASK BIT(DLLRDY_SHIFT)
59 #define PDB_MASK BIT(PDB_SHIFT)
60 #define CALDONE_SHIFT 1
61 #define CALDONE_MASK BIT(CALDONE_SHIFT)
62 #define RETRIM_SHIFT 17
63 #define RETRIM_MASK BIT(RETRIM_SHIFT)
65 #define DRIVER_STRENGTH_50_OHM 0x0
66 #define DRIVER_STRENGTH_33_OHM 0x1
67 #define DRIVER_STRENGTH_66_OHM 0x2
68 #define DRIVER_STRENGTH_100_OHM 0x3
69 #define DRIVER_STRENGTH_40_OHM 0x4
71 #define AM654_SDHCI_MIN_FREQ 400000
73 struct am654_sdhci_plat {
74 struct mmc_config cfg;
83 #define DLL_PRESENT (1 << 0)
84 #define IOMUX_PRESENT (1 << 1)
85 #define FREQSEL_2_BIT (1 << 2)
86 #define STRBSEL_4_BIT (1 << 3)
95 static const struct timing_data td[] = {
96 [MMC_LEGACY] = {"ti,otap-del-sel-legacy", 0},
97 [MMC_HS] = {"ti,otap-del-sel-mmc-hs", MMC_CAP(MMC_HS)},
98 [SD_HS] = {"ti,otap-del-sel-sd-hs", MMC_CAP(SD_HS)},
99 [UHS_SDR12] = {"ti,otap-del-sel-sdr12", MMC_CAP(UHS_SDR12)},
100 [UHS_SDR25] = {"ti,otap-del-sel-sdr25", MMC_CAP(UHS_SDR25)},
101 [UHS_SDR50] = {"ti,otap-del-sel-sdr50", MMC_CAP(UHS_SDR50)},
102 [UHS_SDR104] = {"ti,otap-del-sel-sdr104", MMC_CAP(UHS_SDR104)},
103 [UHS_DDR50] = {"ti,otap-del-sel-ddr50", MMC_CAP(UHS_DDR50)},
104 [MMC_DDR_52] = {"ti,otap-del-sel-ddr52", MMC_CAP(MMC_DDR_52)},
105 [MMC_HS_200] = {"ti,otap-del-sel-hs200", MMC_CAP(MMC_HS_200)},
106 [MMC_HS_400] = {"ti,otap-del-sel-hs400", MMC_CAP(MMC_HS_400)},
109 struct am654_driver_data {
110 const struct sdhci_ops *ops;
114 static void am654_sdhci_set_control_reg(struct sdhci_host *host)
116 struct mmc *mmc = (struct mmc *)host->mmc;
119 if (IS_SD(host->mmc) &&
120 mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
121 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
122 reg |= SDHCI_CTRL_VDD_180;
123 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
126 sdhci_set_uhs_timing(host);
129 static int am654_sdhci_set_ios_post(struct sdhci_host *host)
131 struct udevice *dev = host->mmc->dev;
132 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
133 unsigned int speed = host->mmc->clock;
134 int sel50, sel100, freqsel;
139 /* Reset SD Clock Enable */
140 val = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
141 val &= ~SDHCI_CLOCK_CARD_EN;
142 sdhci_writew(host, val, SDHCI_CLOCK_CONTROL);
146 regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK, 0);
148 plat->dll_on = false;
152 sdhci_set_clock(host->mmc, speed);
154 /* switch phy back on */
155 if (speed > AM654_SDHCI_MIN_FREQ) {
156 otap_del_sel = plat->otap_del_sel[host->mmc->selected_mode];
157 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
158 val = (1 << OTAPDLYENA_SHIFT) |
159 (otap_del_sel << OTAPDLYSEL_SHIFT);
161 /* Write to STRBSEL for HS400 speed mode */
162 if (host->mmc->selected_mode == MMC_HS_400) {
163 if (plat->flags & STRBSEL_4_BIT)
164 mask |= STRBSEL_4BIT_MASK;
166 mask |= STRBSEL_8BIT_MASK;
168 val |= plat->strb_sel << STRBSEL_SHIFT;
171 regmap_update_bits(plat->base, PHY_CTRL4, mask, val);
173 if (plat->flags & FREQSEL_2_BIT) {
188 /* Configure PHY DLL frequency */
189 mask = SEL50_MASK | SEL100_MASK;
190 val = (sel50 << SEL50_SHIFT) | (sel100 << SEL100_SHIFT);
191 regmap_update_bits(plat->base, PHY_CTRL5, mask, val);
200 regmap_update_bits(plat->base, PHY_CTRL5, FREQSEL_MASK,
201 freqsel << FREQSEL_SHIFT);
205 regmap_update_bits(plat->base, PHY_CTRL1, ENDLL_MASK,
208 * Poll for DLL ready. Use a one second timeout.
209 * Works in all experiments done so far
211 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1, val,
212 val & DLLRDY_MASK, 1000, 1000000);
222 int am654_sdhci_init(struct am654_sdhci_plat *plat)
228 /* Reset OTAP to default value */
229 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
230 regmap_update_bits(plat->base, PHY_CTRL4, mask, 0x0);
232 if (plat->flags & DLL_PRESENT) {
233 regmap_read(plat->base, PHY_STAT1, &val);
234 if (~val & CALDONE_MASK) {
235 /* Calibrate IO lines */
236 regmap_update_bits(plat->base, PHY_CTRL1, PDB_MASK,
238 ret = regmap_read_poll_timeout(plat->base, PHY_STAT1,
239 val, val & CALDONE_MASK,
245 /* Configure DLL TRIM */
246 mask = DLL_TRIM_ICP_MASK;
247 val = plat->trm_icp << DLL_TRIM_ICP_SHIFT;
249 /* Configure DLL driver strength */
251 val |= plat->drv_strength << DR_TY_SHIFT;
252 regmap_update_bits(plat->base, PHY_CTRL1, mask, val);
255 /* Enable pins by setting IO mux to 0 */
256 if (plat->flags & IOMUX_PRESENT)
257 regmap_update_bits(plat->base, PHY_CTRL1, IOMUX_ENABLE_MASK, 0);
259 /* Set slot type based on SD or eMMC */
260 if (plat->non_removable)
261 ctl_cfg_2 = SLOTTYPE_EMBEDDED;
263 regmap_update_bits(plat->base, CTL_CFG_2, SLOTTYPE_MASK, ctl_cfg_2);
268 #define MAX_SDCD_DEBOUNCE_TIME 2000
269 static int am654_sdhci_deferred_probe(struct sdhci_host *host)
271 struct udevice *dev = host->mmc->dev;
272 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
277 * The controller takes about 1 second to debounce the card detect line
278 * and doesn't let us power on until that time is up. Instead of waiting
279 * for 1 second at every stage, poll on the CARD_PRESENT bit upto a
280 * maximum of 2 seconds to be safe..
282 start = get_timer(0);
284 if (get_timer(start) > MAX_SDCD_DEBOUNCE_TIME)
287 val = mmc_getcd(host->mmc);
290 am654_sdhci_init(plat);
292 return sdhci_probe(dev);
295 const struct sdhci_ops am654_sdhci_ops = {
296 .deferred_probe = am654_sdhci_deferred_probe,
297 .set_ios_post = &am654_sdhci_set_ios_post,
298 .set_control_reg = &am654_sdhci_set_control_reg,
301 const struct am654_driver_data am654_drv_data = {
302 .ops = &am654_sdhci_ops,
303 .flags = IOMUX_PRESENT | FREQSEL_2_BIT | DLL_PRESENT | STRBSEL_4_BIT,
306 const struct am654_driver_data j721e_8bit_drv_data = {
307 .ops = &am654_sdhci_ops,
308 .flags = DLL_PRESENT,
311 static int j721e_4bit_sdhci_set_ios_post(struct sdhci_host *host)
313 struct udevice *dev = host->mmc->dev;
314 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
315 u32 otap_del_sel, mask, val;
317 otap_del_sel = plat->otap_del_sel[host->mmc->selected_mode];
318 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
319 val = (1 << OTAPDLYENA_SHIFT) | (otap_del_sel << OTAPDLYSEL_SHIFT);
320 regmap_update_bits(plat->base, PHY_CTRL4, mask, val);
325 const struct sdhci_ops j721e_4bit_sdhci_ops = {
326 .deferred_probe = am654_sdhci_deferred_probe,
327 .set_ios_post = &j721e_4bit_sdhci_set_ios_post,
330 const struct am654_driver_data j721e_4bit_drv_data = {
331 .ops = &j721e_4bit_sdhci_ops,
332 .flags = IOMUX_PRESENT,
335 static int sdhci_am654_get_otap_delay(struct udevice *dev,
336 struct mmc_config *cfg)
338 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
342 /* ti,otap-del-sel-legacy is mandatory */
343 ret = dev_read_u32(dev, "ti,otap-del-sel-legacy",
344 &plat->otap_del_sel[0]);
348 * Remove the corresponding capability if an otap-del-sel
351 for (i = MMC_HS; i <= MMC_HS_400; i++) {
352 ret = dev_read_u32(dev, td[i].binding, &plat->otap_del_sel[i]);
354 dev_dbg(dev, "Couldn't find %s\n", td[i].binding);
356 * Remove the corresponding capability
357 * if an otap-del-sel value is not found
359 cfg->host_caps &= ~td[i].capability;
366 static int am654_sdhci_probe(struct udevice *dev)
368 struct am654_driver_data *drv_data =
369 (struct am654_driver_data *)dev_get_driver_data(dev);
370 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
371 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
372 struct sdhci_host *host = dev_get_priv(dev);
373 struct mmc_config *cfg = &plat->cfg;
378 ret = clk_get_by_name(dev, "clk_xin", &clk);
380 dev_err(dev, "failed to get clock\n");
384 clock = clk_get_rate(&clk);
385 if (IS_ERR_VALUE(clock)) {
386 dev_err(dev, "failed to get rate\n");
390 host->max_clk = clock;
391 host->mmc = &plat->mmc;
392 host->mmc->dev = dev;
393 ret = sdhci_setup_cfg(cfg, host, cfg->f_max,
394 AM654_SDHCI_MIN_FREQ);
398 ret = sdhci_am654_get_otap_delay(dev, cfg);
402 host->ops = drv_data->ops;
403 host->mmc->priv = host;
404 upriv->mmc = host->mmc;
406 regmap_init_mem_index(dev_ofnode(dev), &plat->base, 1);
411 static int am654_sdhci_ofdata_to_platdata(struct udevice *dev)
413 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
414 struct sdhci_host *host = dev_get_priv(dev);
415 struct mmc_config *cfg = &plat->cfg;
419 host->name = dev->name;
420 host->ioaddr = (void *)dev_read_addr(dev);
421 plat->non_removable = dev_read_bool(dev, "non-removable");
423 if (plat->flags & DLL_PRESENT) {
424 ret = dev_read_u32(dev, "ti,trm-icp", &plat->trm_icp);
428 ret = dev_read_u32(dev, "ti,driver-strength-ohm",
433 switch (drv_strength) {
435 plat->drv_strength = DRIVER_STRENGTH_50_OHM;
438 plat->drv_strength = DRIVER_STRENGTH_33_OHM;
441 plat->drv_strength = DRIVER_STRENGTH_66_OHM;
444 plat->drv_strength = DRIVER_STRENGTH_100_OHM;
447 plat->drv_strength = DRIVER_STRENGTH_40_OHM;
450 dev_err(dev, "Invalid driver strength\n");
455 ret = mmc_of_parse(dev, cfg);
462 static int am654_sdhci_bind(struct udevice *dev)
464 struct am654_driver_data *drv_data =
465 (struct am654_driver_data *)dev_get_driver_data(dev);
466 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
468 plat->flags = drv_data->flags;
470 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
473 static const struct udevice_id am654_sdhci_ids[] = {
475 .compatible = "ti,am654-sdhci-5.1",
476 .data = (ulong)&am654_drv_data,
479 .compatible = "ti,j721e-sdhci-8bit",
480 .data = (ulong)&j721e_8bit_drv_data,
483 .compatible = "ti,j721e-sdhci-4bit",
484 .data = (ulong)&j721e_4bit_drv_data,
489 U_BOOT_DRIVER(am654_sdhci_drv) = {
490 .name = "am654_sdhci",
492 .of_match = am654_sdhci_ids,
493 .ofdata_to_platdata = am654_sdhci_ofdata_to_platdata,
495 .bind = am654_sdhci_bind,
496 .probe = am654_sdhci_probe,
497 .priv_auto_alloc_size = sizeof(struct sdhci_host),
498 .platdata_auto_alloc_size = sizeof(struct am654_sdhci_plat),