Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Socionext Inc. |
| 4 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 7 | #include <dm.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 8 | #include <asm/global_data.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <dm/device_compat.h> |
Masahiro Yamada | 053ceeb | 2017-12-30 02:00:09 +0900 | [diff] [blame] | 10 | #include <linux/bitfield.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 12 | #include <linux/bug.h> |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 13 | #include <linux/io.h> |
Masahiro Yamada | 89b9391 | 2017-05-09 15:52:04 +0900 | [diff] [blame] | 14 | #include <linux/iopoll.h> |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 15 | #include <linux/sizes.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 16 | #include <linux/libfdt.h> |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 17 | #include <mmc.h> |
| 18 | #include <sdhci.h> |
Kuan Lim Lee | 93986cb | 2023-11-28 14:38:30 +0800 | [diff] [blame] | 19 | #include "sdhci-cadence.h" |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 20 | |
Masahiro Yamada | 89b9391 | 2017-05-09 15:52:04 +0900 | [diff] [blame] | 21 | struct sdhci_cdns_phy_cfg { |
| 22 | const char *property; |
| 23 | u8 addr; |
| 24 | }; |
| 25 | |
| 26 | static const struct sdhci_cdns_phy_cfg sdhci_cdns_phy_cfgs[] = { |
| 27 | { "cdns,phy-input-delay-sd-highspeed", SDHCI_CDNS_PHY_DLY_SD_HS, }, |
| 28 | { "cdns,phy-input-delay-legacy", SDHCI_CDNS_PHY_DLY_SD_DEFAULT, }, |
| 29 | { "cdns,phy-input-delay-sd-uhs-sdr12", SDHCI_CDNS_PHY_DLY_UHS_SDR12, }, |
| 30 | { "cdns,phy-input-delay-sd-uhs-sdr25", SDHCI_CDNS_PHY_DLY_UHS_SDR25, }, |
| 31 | { "cdns,phy-input-delay-sd-uhs-sdr50", SDHCI_CDNS_PHY_DLY_UHS_SDR50, }, |
| 32 | { "cdns,phy-input-delay-sd-uhs-ddr50", SDHCI_CDNS_PHY_DLY_UHS_DDR50, }, |
| 33 | { "cdns,phy-input-delay-mmc-highspeed", SDHCI_CDNS_PHY_DLY_EMMC_SDR, }, |
| 34 | { "cdns,phy-input-delay-mmc-ddr", SDHCI_CDNS_PHY_DLY_EMMC_DDR, }, |
| 35 | { "cdns,phy-dll-delay-sdclk", SDHCI_CDNS_PHY_DLY_SDCLK, }, |
| 36 | { "cdns,phy-dll-delay-sdclk-hsmmc", SDHCI_CDNS_PHY_DLY_HSMMC, }, |
| 37 | { "cdns,phy-dll-delay-strobe", SDHCI_CDNS_PHY_DLY_STROBE, }, |
| 38 | }; |
| 39 | |
| 40 | static int sdhci_cdns_write_phy_reg(struct sdhci_cdns_plat *plat, |
| 41 | u8 addr, u8 data) |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 42 | { |
| 43 | void __iomem *reg = plat->hrs_addr + SDHCI_CDNS_HRS04; |
| 44 | u32 tmp; |
Masahiro Yamada | 89b9391 | 2017-05-09 15:52:04 +0900 | [diff] [blame] | 45 | int ret; |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 46 | |
Masahiro Yamada | 053ceeb | 2017-12-30 02:00:09 +0900 | [diff] [blame] | 47 | tmp = FIELD_PREP(SDHCI_CDNS_HRS04_WDATA, data) | |
| 48 | FIELD_PREP(SDHCI_CDNS_HRS04_ADDR, addr); |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 49 | writel(tmp, reg); |
| 50 | |
| 51 | tmp |= SDHCI_CDNS_HRS04_WR; |
| 52 | writel(tmp, reg); |
| 53 | |
Masahiro Yamada | 89b9391 | 2017-05-09 15:52:04 +0900 | [diff] [blame] | 54 | ret = readl_poll_timeout(reg, tmp, tmp & SDHCI_CDNS_HRS04_ACK, 10); |
| 55 | if (ret) |
| 56 | return ret; |
| 57 | |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 58 | tmp &= ~SDHCI_CDNS_HRS04_WR; |
| 59 | writel(tmp, reg); |
Masahiro Yamada | 89b9391 | 2017-05-09 15:52:04 +0900 | [diff] [blame] | 60 | |
| 61 | return 0; |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 62 | } |
| 63 | |
Masahiro Yamada | 89b9391 | 2017-05-09 15:52:04 +0900 | [diff] [blame] | 64 | static int sdhci_cdns_phy_init(struct sdhci_cdns_plat *plat, |
| 65 | const void *fdt, int nodeoffset) |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 66 | { |
Masahiro Yamada | 959e907 | 2017-06-22 17:58:09 +0900 | [diff] [blame] | 67 | const fdt32_t *prop; |
Masahiro Yamada | 89b9391 | 2017-05-09 15:52:04 +0900 | [diff] [blame] | 68 | int ret, i; |
| 69 | |
| 70 | for (i = 0; i < ARRAY_SIZE(sdhci_cdns_phy_cfgs); i++) { |
| 71 | prop = fdt_getprop(fdt, nodeoffset, |
| 72 | sdhci_cdns_phy_cfgs[i].property, NULL); |
| 73 | if (!prop) |
| 74 | continue; |
| 75 | |
| 76 | ret = sdhci_cdns_write_phy_reg(plat, |
| 77 | sdhci_cdns_phy_cfgs[i].addr, |
| 78 | fdt32_to_cpu(*prop)); |
| 79 | if (ret) |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | return 0; |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 84 | } |
| 85 | |
Masahiro Yamada | a1c702b | 2017-09-28 21:13:10 +0900 | [diff] [blame] | 86 | static void sdhci_cdns_set_control_reg(struct sdhci_host *host) |
| 87 | { |
| 88 | struct mmc *mmc = host->mmc; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 89 | struct sdhci_cdns_plat *plat = dev_get_plat(mmc->dev); |
Masahiro Yamada | a1c702b | 2017-09-28 21:13:10 +0900 | [diff] [blame] | 90 | unsigned int clock = mmc->clock; |
| 91 | u32 mode, tmp; |
| 92 | |
| 93 | /* |
| 94 | * REVISIT: |
| 95 | * The mode should be decided by MMC_TIMING_* like Linux, but |
| 96 | * U-Boot does not support timing. Use the clock frequency instead. |
| 97 | */ |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 98 | if (clock <= 26000000) { |
Masahiro Yamada | a1c702b | 2017-09-28 21:13:10 +0900 | [diff] [blame] | 99 | mode = SDHCI_CDNS_HRS06_MODE_SD; /* use this for Legacy */ |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 100 | } else if (clock <= 52000000) { |
Masahiro Yamada | a1c702b | 2017-09-28 21:13:10 +0900 | [diff] [blame] | 101 | if (mmc->ddr_mode) |
| 102 | mode = SDHCI_CDNS_HRS06_MODE_MMC_DDR; |
| 103 | else |
| 104 | mode = SDHCI_CDNS_HRS06_MODE_MMC_SDR; |
| 105 | } else { |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 106 | if (mmc->ddr_mode) |
| 107 | mode = SDHCI_CDNS_HRS06_MODE_MMC_HS400; |
| 108 | else |
| 109 | mode = SDHCI_CDNS_HRS06_MODE_MMC_HS200; |
Masahiro Yamada | a1c702b | 2017-09-28 21:13:10 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | tmp = readl(plat->hrs_addr + SDHCI_CDNS_HRS06); |
Masahiro Yamada | 053ceeb | 2017-12-30 02:00:09 +0900 | [diff] [blame] | 113 | tmp &= ~SDHCI_CDNS_HRS06_MODE; |
| 114 | tmp |= FIELD_PREP(SDHCI_CDNS_HRS06_MODE, mode); |
Masahiro Yamada | a1c702b | 2017-09-28 21:13:10 +0900 | [diff] [blame] | 115 | writel(tmp, plat->hrs_addr + SDHCI_CDNS_HRS06); |
Kuan Lim Lee | 93986cb | 2023-11-28 14:38:30 +0800 | [diff] [blame] | 116 | |
| 117 | if (device_is_compatible(mmc->dev, "cdns,sd6hc")) |
| 118 | sdhci_cdns6_phy_adj(mmc->dev, plat, mode); |
Masahiro Yamada | a1c702b | 2017-09-28 21:13:10 +0900 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static const struct sdhci_ops sdhci_cdns_ops = { |
| 122 | .set_control_reg = sdhci_cdns_set_control_reg, |
| 123 | }; |
| 124 | |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 125 | static int sdhci_cdns_set_tune_val(struct sdhci_cdns_plat *plat, |
| 126 | unsigned int val) |
| 127 | { |
| 128 | void __iomem *reg = plat->hrs_addr + SDHCI_CDNS_HRS06; |
| 129 | u32 tmp; |
Masahiro Yamada | 55f7b00 | 2020-01-21 18:42:05 +0900 | [diff] [blame] | 130 | int i, ret; |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 131 | |
Kuan Lim Lee | 93986cb | 2023-11-28 14:38:30 +0800 | [diff] [blame] | 132 | if (device_is_compatible(plat->mmc.dev, "cdns,sd6hc")) |
| 133 | return sdhci_cdns6_set_tune_val(plat, val); |
| 134 | |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 135 | if (WARN_ON(!FIELD_FIT(SDHCI_CDNS_HRS06_TUNE, val))) |
| 136 | return -EINVAL; |
| 137 | |
| 138 | tmp = readl(reg); |
| 139 | tmp &= ~SDHCI_CDNS_HRS06_TUNE; |
| 140 | tmp |= FIELD_PREP(SDHCI_CDNS_HRS06_TUNE, val); |
Masahiro Yamada | 55f7b00 | 2020-01-21 18:42:05 +0900 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Workaround for IP errata: |
| 144 | * The IP6116 SD/eMMC PHY design has a timing issue on receive data |
| 145 | * path. Send tune request twice. |
| 146 | */ |
| 147 | for (i = 0; i < 2; i++) { |
| 148 | tmp |= SDHCI_CDNS_HRS06_TUNE_UP; |
| 149 | writel(tmp, reg); |
| 150 | |
| 151 | ret = readl_poll_timeout(reg, tmp, |
| 152 | !(tmp & SDHCI_CDNS_HRS06_TUNE_UP), 1); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | } |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 156 | |
Masahiro Yamada | 55f7b00 | 2020-01-21 18:42:05 +0900 | [diff] [blame] | 157 | return 0; |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static int __maybe_unused sdhci_cdns_execute_tuning(struct udevice *dev, |
| 161 | unsigned int opcode) |
| 162 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 163 | struct sdhci_cdns_plat *plat = dev_get_plat(dev); |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 164 | struct mmc *mmc = &plat->mmc; |
| 165 | int cur_streak = 0; |
| 166 | int max_streak = 0; |
| 167 | int end_of_streak = 0; |
| 168 | int i; |
| 169 | |
| 170 | /* |
| 171 | * This handler only implements the eMMC tuning that is specific to |
| 172 | * this controller. The tuning for SD timing should be handled by the |
| 173 | * SDHCI core. |
| 174 | */ |
| 175 | if (!IS_MMC(mmc)) |
| 176 | return -ENOTSUPP; |
| 177 | |
| 178 | if (WARN_ON(opcode != MMC_CMD_SEND_TUNING_BLOCK_HS200)) |
| 179 | return -EINVAL; |
| 180 | |
| 181 | for (i = 0; i < SDHCI_CDNS_MAX_TUNING_LOOP; i++) { |
| 182 | if (sdhci_cdns_set_tune_val(plat, i) || |
Marek Vasut | dad81fb | 2024-02-20 09:36:23 +0100 | [diff] [blame] | 183 | mmc_send_tuning(mmc, opcode)) { /* bad */ |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 184 | cur_streak = 0; |
| 185 | } else { /* good */ |
| 186 | cur_streak++; |
| 187 | if (cur_streak > max_streak) { |
| 188 | max_streak = cur_streak; |
| 189 | end_of_streak = i; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (!max_streak) { |
| 195 | dev_err(dev, "no tuning point found\n"); |
| 196 | return -EIO; |
| 197 | } |
| 198 | |
| 199 | return sdhci_cdns_set_tune_val(plat, end_of_streak - max_streak / 2); |
| 200 | } |
| 201 | |
| 202 | static struct dm_mmc_ops sdhci_cdns_mmc_ops; |
| 203 | |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 204 | static int sdhci_cdns_bind(struct udevice *dev) |
| 205 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 206 | struct sdhci_cdns_plat *plat = dev_get_plat(dev); |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 207 | |
| 208 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 209 | } |
| 210 | |
| 211 | static int sdhci_cdns_probe(struct udevice *dev) |
| 212 | { |
Masahiro Yamada | 89b9391 | 2017-05-09 15:52:04 +0900 | [diff] [blame] | 213 | DECLARE_GLOBAL_DATA_PTR; |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 214 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 215 | struct sdhci_cdns_plat *plat = dev_get_plat(dev); |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 216 | struct sdhci_host *host = dev_get_priv(dev); |
| 217 | fdt_addr_t base; |
| 218 | int ret; |
| 219 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 220 | base = dev_read_addr(dev); |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 221 | if (base == FDT_ADDR_T_NONE) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | plat->hrs_addr = devm_ioremap(dev, base, SZ_1K); |
| 225 | if (!plat->hrs_addr) |
| 226 | return -ENOMEM; |
| 227 | |
| 228 | host->name = dev->name; |
| 229 | host->ioaddr = plat->hrs_addr + SDHCI_CDNS_SRS_BASE; |
Masahiro Yamada | a1c702b | 2017-09-28 21:13:10 +0900 | [diff] [blame] | 230 | host->ops = &sdhci_cdns_ops; |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 231 | host->quirks |= SDHCI_QUIRK_WAIT_SEND_CMD; |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 232 | sdhci_cdns_mmc_ops = sdhci_ops; |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 233 | #if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING) |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 234 | sdhci_cdns_mmc_ops.execute_tuning = sdhci_cdns_execute_tuning; |
| 235 | #endif |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 236 | |
Masahiro Yamada | d80ca27 | 2017-12-30 02:00:10 +0900 | [diff] [blame] | 237 | ret = mmc_of_parse(dev, &plat->cfg); |
| 238 | if (ret) |
| 239 | return ret; |
| 240 | |
Kuan Lim Lee | 93986cb | 2023-11-28 14:38:30 +0800 | [diff] [blame] | 241 | if (device_is_compatible(dev, "cdns,sd6hc")) |
| 242 | ret = sdhci_cdns6_phy_init(dev, plat); |
| 243 | else |
| 244 | ret = sdhci_cdns_phy_init(plat, gd->fdt_blob, dev_of_offset(dev)); |
Masahiro Yamada | 89b9391 | 2017-05-09 15:52:04 +0900 | [diff] [blame] | 245 | if (ret) |
| 246 | return ret; |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 247 | |
Peng Fan | 41ec1c1 | 2019-08-06 02:48:02 +0000 | [diff] [blame] | 248 | host->mmc = &plat->mmc; |
| 249 | host->mmc->dev = dev; |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 250 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0); |
| 251 | if (ret) |
| 252 | return ret; |
| 253 | |
| 254 | upriv->mmc = &plat->mmc; |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 255 | host->mmc->priv = host; |
| 256 | |
| 257 | return sdhci_probe(dev); |
| 258 | } |
| 259 | |
| 260 | static const struct udevice_id sdhci_cdns_match[] = { |
| 261 | { .compatible = "socionext,uniphier-sd4hc" }, |
| 262 | { .compatible = "cdns,sd4hc" }, |
Kuan Lim Lee | 93986cb | 2023-11-28 14:38:30 +0800 | [diff] [blame] | 263 | { .compatible = "cdns,sd6hc" }, |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 264 | { /* sentinel */ } |
| 265 | }; |
| 266 | |
| 267 | U_BOOT_DRIVER(sdhci_cdns) = { |
| 268 | .name = "sdhci-cdns", |
| 269 | .id = UCLASS_MMC, |
| 270 | .of_match = sdhci_cdns_match, |
| 271 | .bind = sdhci_cdns_bind, |
| 272 | .probe = sdhci_cdns_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 273 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 274 | .plat_auto = sizeof(struct sdhci_cdns_plat), |
Masahiro Yamada | 1a0cb67 | 2018-01-12 18:10:38 +0900 | [diff] [blame] | 275 | .ops = &sdhci_cdns_mmc_ops, |
Masahiro Yamada | dcbc443 | 2016-12-30 22:41:46 +0900 | [diff] [blame] | 276 | }; |