Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Socionext Inc. |
| 4 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 5 | */ |
| 6 | |
Masahiro Yamada | f02cebd | 2017-10-13 21:50:31 +0900 | [diff] [blame] | 7 | #include <linux/bitfield.h> |
Masahiro Yamada | cd7a0d2 | 2018-10-30 20:05:06 +0900 | [diff] [blame] | 8 | #include <linux/bits.h> |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 9 | #include <linux/iopoll.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/mmc/host.h> |
Ulf Hansson | 963836a | 2016-12-30 13:47:21 +0100 | [diff] [blame] | 12 | #include <linux/mmc/mmc.h> |
Piotr Sroka | a89c472 | 2017-03-21 14:33:11 +0000 | [diff] [blame] | 13 | #include <linux/of.h> |
Masahiro Yamada | 18b587b | 2020-03-12 19:42:57 +0900 | [diff] [blame] | 14 | #include <linux/of_device.h> |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 15 | |
| 16 | #include "sdhci-pltfm.h" |
| 17 | |
| 18 | /* HRS - Host Register Set (specific to Cadence) */ |
| 19 | #define SDHCI_CDNS_HRS04 0x10 /* PHY access port */ |
| 20 | #define SDHCI_CDNS_HRS04_ACK BIT(26) |
| 21 | #define SDHCI_CDNS_HRS04_RD BIT(25) |
| 22 | #define SDHCI_CDNS_HRS04_WR BIT(24) |
Masahiro Yamada | f02cebd | 2017-10-13 21:50:31 +0900 | [diff] [blame] | 23 | #define SDHCI_CDNS_HRS04_RDATA GENMASK(23, 16) |
| 24 | #define SDHCI_CDNS_HRS04_WDATA GENMASK(15, 8) |
| 25 | #define SDHCI_CDNS_HRS04_ADDR GENMASK(5, 0) |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 26 | |
| 27 | #define SDHCI_CDNS_HRS06 0x18 /* eMMC control */ |
| 28 | #define SDHCI_CDNS_HRS06_TUNE_UP BIT(15) |
Masahiro Yamada | f02cebd | 2017-10-13 21:50:31 +0900 | [diff] [blame] | 29 | #define SDHCI_CDNS_HRS06_TUNE GENMASK(13, 8) |
| 30 | #define SDHCI_CDNS_HRS06_MODE GENMASK(2, 0) |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 31 | #define SDHCI_CDNS_HRS06_MODE_SD 0x0 |
| 32 | #define SDHCI_CDNS_HRS06_MODE_MMC_SDR 0x2 |
| 33 | #define SDHCI_CDNS_HRS06_MODE_MMC_DDR 0x3 |
| 34 | #define SDHCI_CDNS_HRS06_MODE_MMC_HS200 0x4 |
| 35 | #define SDHCI_CDNS_HRS06_MODE_MMC_HS400 0x5 |
Piotr Sroka | d12990f | 2017-03-06 08:28:41 +0000 | [diff] [blame] | 36 | #define SDHCI_CDNS_HRS06_MODE_MMC_HS400ES 0x6 |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 37 | |
| 38 | /* SRS - Slot Register Set (SDHCI-compatible) */ |
| 39 | #define SDHCI_CDNS_SRS_BASE 0x200 |
| 40 | |
| 41 | /* PHY */ |
| 42 | #define SDHCI_CDNS_PHY_DLY_SD_HS 0x00 |
| 43 | #define SDHCI_CDNS_PHY_DLY_SD_DEFAULT 0x01 |
| 44 | #define SDHCI_CDNS_PHY_DLY_UHS_SDR12 0x02 |
| 45 | #define SDHCI_CDNS_PHY_DLY_UHS_SDR25 0x03 |
| 46 | #define SDHCI_CDNS_PHY_DLY_UHS_SDR50 0x04 |
| 47 | #define SDHCI_CDNS_PHY_DLY_UHS_DDR50 0x05 |
| 48 | #define SDHCI_CDNS_PHY_DLY_EMMC_LEGACY 0x06 |
| 49 | #define SDHCI_CDNS_PHY_DLY_EMMC_SDR 0x07 |
| 50 | #define SDHCI_CDNS_PHY_DLY_EMMC_DDR 0x08 |
Piotr Sroka | a89c472 | 2017-03-21 14:33:11 +0000 | [diff] [blame] | 51 | #define SDHCI_CDNS_PHY_DLY_SDCLK 0x0b |
| 52 | #define SDHCI_CDNS_PHY_DLY_HSMMC 0x0c |
| 53 | #define SDHCI_CDNS_PHY_DLY_STROBE 0x0d |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * The tuned val register is 6 bit-wide, but not the whole of the range is |
| 57 | * available. The range 0-42 seems to be available (then 43 wraps around to 0) |
| 58 | * but I am not quite sure if it is official. Use only 0 to 39 for safety. |
| 59 | */ |
| 60 | #define SDHCI_CDNS_MAX_TUNING_LOOP 40 |
| 61 | |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 62 | struct sdhci_cdns_phy_param { |
| 63 | u8 addr; |
| 64 | u8 data; |
| 65 | }; |
| 66 | |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 67 | struct sdhci_cdns_priv { |
| 68 | void __iomem *hrs_addr; |
Piotr Sroka | d12990f | 2017-03-06 08:28:41 +0000 | [diff] [blame] | 69 | bool enhanced_strobe; |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 70 | unsigned int nr_phy_params; |
Gustavo A. R. Silva | 1a91a36a | 2020-02-26 16:31:25 -0600 | [diff] [blame] | 71 | struct sdhci_cdns_phy_param phy_params[]; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 72 | }; |
| 73 | |
Piotr Sroka | a89c472 | 2017-03-21 14:33:11 +0000 | [diff] [blame] | 74 | struct sdhci_cdns_phy_cfg { |
| 75 | const char *property; |
| 76 | u8 addr; |
| 77 | }; |
| 78 | |
| 79 | static const struct sdhci_cdns_phy_cfg sdhci_cdns_phy_cfgs[] = { |
| 80 | { "cdns,phy-input-delay-sd-highspeed", SDHCI_CDNS_PHY_DLY_SD_HS, }, |
| 81 | { "cdns,phy-input-delay-legacy", SDHCI_CDNS_PHY_DLY_SD_DEFAULT, }, |
| 82 | { "cdns,phy-input-delay-sd-uhs-sdr12", SDHCI_CDNS_PHY_DLY_UHS_SDR12, }, |
| 83 | { "cdns,phy-input-delay-sd-uhs-sdr25", SDHCI_CDNS_PHY_DLY_UHS_SDR25, }, |
| 84 | { "cdns,phy-input-delay-sd-uhs-sdr50", SDHCI_CDNS_PHY_DLY_UHS_SDR50, }, |
| 85 | { "cdns,phy-input-delay-sd-uhs-ddr50", SDHCI_CDNS_PHY_DLY_UHS_DDR50, }, |
| 86 | { "cdns,phy-input-delay-mmc-highspeed", SDHCI_CDNS_PHY_DLY_EMMC_SDR, }, |
| 87 | { "cdns,phy-input-delay-mmc-ddr", SDHCI_CDNS_PHY_DLY_EMMC_DDR, }, |
| 88 | { "cdns,phy-dll-delay-sdclk", SDHCI_CDNS_PHY_DLY_SDCLK, }, |
| 89 | { "cdns,phy-dll-delay-sdclk-hsmmc", SDHCI_CDNS_PHY_DLY_HSMMC, }, |
| 90 | { "cdns,phy-dll-delay-strobe", SDHCI_CDNS_PHY_DLY_STROBE, }, |
| 91 | }; |
| 92 | |
Piotr Sroka | a0f8243 | 2017-03-21 14:32:16 +0000 | [diff] [blame] | 93 | static int sdhci_cdns_write_phy_reg(struct sdhci_cdns_priv *priv, |
| 94 | u8 addr, u8 data) |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 95 | { |
| 96 | void __iomem *reg = priv->hrs_addr + SDHCI_CDNS_HRS04; |
| 97 | u32 tmp; |
Piotr Sroka | a0f8243 | 2017-03-21 14:32:16 +0000 | [diff] [blame] | 98 | int ret; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 99 | |
Masahiro Yamada | f02cebd | 2017-10-13 21:50:31 +0900 | [diff] [blame] | 100 | tmp = FIELD_PREP(SDHCI_CDNS_HRS04_WDATA, data) | |
| 101 | FIELD_PREP(SDHCI_CDNS_HRS04_ADDR, addr); |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 102 | writel(tmp, reg); |
| 103 | |
| 104 | tmp |= SDHCI_CDNS_HRS04_WR; |
| 105 | writel(tmp, reg); |
| 106 | |
Piotr Sroka | a0f8243 | 2017-03-21 14:32:16 +0000 | [diff] [blame] | 107 | ret = readl_poll_timeout(reg, tmp, tmp & SDHCI_CDNS_HRS04_ACK, 0, 10); |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 111 | tmp &= ~SDHCI_CDNS_HRS04_WR; |
| 112 | writel(tmp, reg); |
Piotr Sroka | a0f8243 | 2017-03-21 14:32:16 +0000 | [diff] [blame] | 113 | |
| 114 | return 0; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 115 | } |
| 116 | |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 117 | static unsigned int sdhci_cdns_phy_param_count(struct device_node *np) |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 118 | { |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 119 | unsigned int count = 0; |
| 120 | int i; |
| 121 | |
| 122 | for (i = 0; i < ARRAY_SIZE(sdhci_cdns_phy_cfgs); i++) |
| 123 | if (of_property_read_bool(np, sdhci_cdns_phy_cfgs[i].property)) |
| 124 | count++; |
| 125 | |
| 126 | return count; |
| 127 | } |
| 128 | |
| 129 | static void sdhci_cdns_phy_param_parse(struct device_node *np, |
| 130 | struct sdhci_cdns_priv *priv) |
| 131 | { |
| 132 | struct sdhci_cdns_phy_param *p = priv->phy_params; |
Piotr Sroka | a89c472 | 2017-03-21 14:33:11 +0000 | [diff] [blame] | 133 | u32 val; |
| 134 | int ret, i; |
| 135 | |
| 136 | for (i = 0; i < ARRAY_SIZE(sdhci_cdns_phy_cfgs); i++) { |
| 137 | ret = of_property_read_u32(np, sdhci_cdns_phy_cfgs[i].property, |
| 138 | &val); |
| 139 | if (ret) |
| 140 | continue; |
| 141 | |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 142 | p->addr = sdhci_cdns_phy_cfgs[i].addr; |
| 143 | p->data = val; |
| 144 | p++; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static int sdhci_cdns_phy_init(struct sdhci_cdns_priv *priv) |
| 149 | { |
| 150 | int ret, i; |
| 151 | |
| 152 | for (i = 0; i < priv->nr_phy_params; i++) { |
| 153 | ret = sdhci_cdns_write_phy_reg(priv, priv->phy_params[i].addr, |
| 154 | priv->phy_params[i].data); |
Piotr Sroka | a89c472 | 2017-03-21 14:33:11 +0000 | [diff] [blame] | 155 | if (ret) |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | return 0; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 160 | } |
| 161 | |
Masahiro Yamada | 1d45a3f | 2020-01-21 19:58:58 +0900 | [diff] [blame] | 162 | static void *sdhci_cdns_priv(struct sdhci_host *host) |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 163 | { |
| 164 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 165 | |
| 166 | return sdhci_pltfm_priv(pltfm_host); |
| 167 | } |
| 168 | |
| 169 | static unsigned int sdhci_cdns_get_timeout_clock(struct sdhci_host *host) |
| 170 | { |
| 171 | /* |
| 172 | * Cadence's spec says the Timeout Clock Frequency is the same as the |
Shawn Lin | 8cc3528 | 2017-03-24 15:50:12 +0800 | [diff] [blame] | 173 | * Base Clock Frequency. |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 174 | */ |
Shawn Lin | 8cc3528 | 2017-03-24 15:50:12 +0800 | [diff] [blame] | 175 | return host->max_clk; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 176 | } |
| 177 | |
Piotr Sroka | d12990f | 2017-03-06 08:28:41 +0000 | [diff] [blame] | 178 | static void sdhci_cdns_set_emmc_mode(struct sdhci_cdns_priv *priv, u32 mode) |
| 179 | { |
| 180 | u32 tmp; |
| 181 | |
| 182 | /* The speed mode for eMMC is selected by HRS06 register */ |
| 183 | tmp = readl(priv->hrs_addr + SDHCI_CDNS_HRS06); |
Masahiro Yamada | f02cebd | 2017-10-13 21:50:31 +0900 | [diff] [blame] | 184 | tmp &= ~SDHCI_CDNS_HRS06_MODE; |
| 185 | tmp |= FIELD_PREP(SDHCI_CDNS_HRS06_MODE, mode); |
Piotr Sroka | d12990f | 2017-03-06 08:28:41 +0000 | [diff] [blame] | 186 | writel(tmp, priv->hrs_addr + SDHCI_CDNS_HRS06); |
| 187 | } |
| 188 | |
| 189 | static u32 sdhci_cdns_get_emmc_mode(struct sdhci_cdns_priv *priv) |
| 190 | { |
| 191 | u32 tmp; |
| 192 | |
| 193 | tmp = readl(priv->hrs_addr + SDHCI_CDNS_HRS06); |
Masahiro Yamada | f02cebd | 2017-10-13 21:50:31 +0900 | [diff] [blame] | 194 | return FIELD_GET(SDHCI_CDNS_HRS06_MODE, tmp); |
Piotr Sroka | d12990f | 2017-03-06 08:28:41 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 197 | static void sdhci_cdns_set_uhs_signaling(struct sdhci_host *host, |
| 198 | unsigned int timing) |
| 199 | { |
| 200 | struct sdhci_cdns_priv *priv = sdhci_cdns_priv(host); |
Piotr Sroka | d12990f | 2017-03-06 08:28:41 +0000 | [diff] [blame] | 201 | u32 mode; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 202 | |
| 203 | switch (timing) { |
| 204 | case MMC_TIMING_MMC_HS: |
| 205 | mode = SDHCI_CDNS_HRS06_MODE_MMC_SDR; |
| 206 | break; |
| 207 | case MMC_TIMING_MMC_DDR52: |
| 208 | mode = SDHCI_CDNS_HRS06_MODE_MMC_DDR; |
| 209 | break; |
| 210 | case MMC_TIMING_MMC_HS200: |
| 211 | mode = SDHCI_CDNS_HRS06_MODE_MMC_HS200; |
| 212 | break; |
| 213 | case MMC_TIMING_MMC_HS400: |
Piotr Sroka | d12990f | 2017-03-06 08:28:41 +0000 | [diff] [blame] | 214 | if (priv->enhanced_strobe) |
| 215 | mode = SDHCI_CDNS_HRS06_MODE_MMC_HS400ES; |
| 216 | else |
| 217 | mode = SDHCI_CDNS_HRS06_MODE_MMC_HS400; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 218 | break; |
| 219 | default: |
| 220 | mode = SDHCI_CDNS_HRS06_MODE_SD; |
| 221 | break; |
| 222 | } |
| 223 | |
Piotr Sroka | d12990f | 2017-03-06 08:28:41 +0000 | [diff] [blame] | 224 | sdhci_cdns_set_emmc_mode(priv, mode); |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 225 | |
| 226 | /* For SD, fall back to the default handler */ |
| 227 | if (mode == SDHCI_CDNS_HRS06_MODE_SD) |
| 228 | sdhci_set_uhs_signaling(host, timing); |
| 229 | } |
| 230 | |
| 231 | static const struct sdhci_ops sdhci_cdns_ops = { |
| 232 | .set_clock = sdhci_set_clock, |
| 233 | .get_timeout_clock = sdhci_cdns_get_timeout_clock, |
| 234 | .set_bus_width = sdhci_set_bus_width, |
| 235 | .reset = sdhci_reset, |
| 236 | .set_uhs_signaling = sdhci_cdns_set_uhs_signaling, |
| 237 | }; |
| 238 | |
Masahiro Yamada | 18b587b | 2020-03-12 19:42:57 +0900 | [diff] [blame] | 239 | static const struct sdhci_pltfm_data sdhci_cdns_uniphier_pltfm_data = { |
| 240 | .ops = &sdhci_cdns_ops, |
| 241 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, |
| 242 | }; |
| 243 | |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 244 | static const struct sdhci_pltfm_data sdhci_cdns_pltfm_data = { |
| 245 | .ops = &sdhci_cdns_ops, |
| 246 | }; |
| 247 | |
| 248 | static int sdhci_cdns_set_tune_val(struct sdhci_host *host, unsigned int val) |
| 249 | { |
| 250 | struct sdhci_cdns_priv *priv = sdhci_cdns_priv(host); |
| 251 | void __iomem *reg = priv->hrs_addr + SDHCI_CDNS_HRS06; |
| 252 | u32 tmp; |
Masahiro Yamada | ef6b756 | 2018-03-27 18:29:53 +0900 | [diff] [blame] | 253 | int i, ret; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 254 | |
Masahiro Yamada | f02cebd | 2017-10-13 21:50:31 +0900 | [diff] [blame] | 255 | if (WARN_ON(!FIELD_FIT(SDHCI_CDNS_HRS06_TUNE, val))) |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 256 | return -EINVAL; |
| 257 | |
| 258 | tmp = readl(reg); |
Masahiro Yamada | f02cebd | 2017-10-13 21:50:31 +0900 | [diff] [blame] | 259 | tmp &= ~SDHCI_CDNS_HRS06_TUNE; |
| 260 | tmp |= FIELD_PREP(SDHCI_CDNS_HRS06_TUNE, val); |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 261 | |
Masahiro Yamada | ef6b756 | 2018-03-27 18:29:53 +0900 | [diff] [blame] | 262 | /* |
| 263 | * Workaround for IP errata: |
| 264 | * The IP6116 SD/eMMC PHY design has a timing issue on receive data |
| 265 | * path. Send tune request twice. |
| 266 | */ |
| 267 | for (i = 0; i < 2; i++) { |
| 268 | tmp |= SDHCI_CDNS_HRS06_TUNE_UP; |
| 269 | writel(tmp, reg); |
| 270 | |
| 271 | ret = readl_poll_timeout(reg, tmp, |
| 272 | !(tmp & SDHCI_CDNS_HRS06_TUNE_UP), |
| 273 | 0, 1); |
Gustavo A. R. Silva | 3f1109d | 2018-04-19 10:59:58 -0500 | [diff] [blame] | 274 | if (ret) |
| 275 | return ret; |
Masahiro Yamada | ef6b756 | 2018-03-27 18:29:53 +0900 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | return 0; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static int sdhci_cdns_execute_tuning(struct mmc_host *mmc, u32 opcode) |
| 282 | { |
| 283 | struct sdhci_host *host = mmc_priv(mmc); |
| 284 | int cur_streak = 0; |
| 285 | int max_streak = 0; |
| 286 | int end_of_streak = 0; |
| 287 | int i; |
| 288 | |
| 289 | /* |
| 290 | * This handler only implements the eMMC tuning that is specific to |
| 291 | * this controller. Fall back to the standard method for SD timing. |
| 292 | */ |
| 293 | if (host->timing != MMC_TIMING_MMC_HS200) |
| 294 | return sdhci_execute_tuning(mmc, opcode); |
| 295 | |
| 296 | if (WARN_ON(opcode != MMC_SEND_TUNING_BLOCK_HS200)) |
| 297 | return -EINVAL; |
| 298 | |
| 299 | for (i = 0; i < SDHCI_CDNS_MAX_TUNING_LOOP; i++) { |
| 300 | if (sdhci_cdns_set_tune_val(host, i) || |
| 301 | mmc_send_tuning(host->mmc, opcode, NULL)) { /* bad */ |
| 302 | cur_streak = 0; |
| 303 | } else { /* good */ |
| 304 | cur_streak++; |
| 305 | if (cur_streak > max_streak) { |
| 306 | max_streak = cur_streak; |
| 307 | end_of_streak = i; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if (!max_streak) { |
| 313 | dev_err(mmc_dev(host->mmc), "no tuning point found\n"); |
| 314 | return -EIO; |
| 315 | } |
| 316 | |
| 317 | return sdhci_cdns_set_tune_val(host, end_of_streak - max_streak / 2); |
| 318 | } |
| 319 | |
Piotr Sroka | d12990f | 2017-03-06 08:28:41 +0000 | [diff] [blame] | 320 | static void sdhci_cdns_hs400_enhanced_strobe(struct mmc_host *mmc, |
| 321 | struct mmc_ios *ios) |
| 322 | { |
| 323 | struct sdhci_host *host = mmc_priv(mmc); |
| 324 | struct sdhci_cdns_priv *priv = sdhci_cdns_priv(host); |
| 325 | u32 mode; |
| 326 | |
| 327 | priv->enhanced_strobe = ios->enhanced_strobe; |
| 328 | |
| 329 | mode = sdhci_cdns_get_emmc_mode(priv); |
| 330 | |
| 331 | if (mode == SDHCI_CDNS_HRS06_MODE_MMC_HS400 && ios->enhanced_strobe) |
| 332 | sdhci_cdns_set_emmc_mode(priv, |
| 333 | SDHCI_CDNS_HRS06_MODE_MMC_HS400ES); |
| 334 | |
| 335 | if (mode == SDHCI_CDNS_HRS06_MODE_MMC_HS400ES && !ios->enhanced_strobe) |
| 336 | sdhci_cdns_set_emmc_mode(priv, |
| 337 | SDHCI_CDNS_HRS06_MODE_MMC_HS400); |
| 338 | } |
| 339 | |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 340 | static int sdhci_cdns_probe(struct platform_device *pdev) |
| 341 | { |
| 342 | struct sdhci_host *host; |
Masahiro Yamada | 18b587b | 2020-03-12 19:42:57 +0900 | [diff] [blame] | 343 | const struct sdhci_pltfm_data *data; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 344 | struct sdhci_pltfm_host *pltfm_host; |
| 345 | struct sdhci_cdns_priv *priv; |
| 346 | struct clk *clk; |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 347 | unsigned int nr_phy_params; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 348 | int ret; |
Piotr Sroka | a89c472 | 2017-03-21 14:33:11 +0000 | [diff] [blame] | 349 | struct device *dev = &pdev->dev; |
Masahiro Yamada | 12a632e | 2019-08-29 19:49:28 +0900 | [diff] [blame] | 350 | static const u16 version = SDHCI_SPEC_400 << SDHCI_SPEC_VER_SHIFT; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 351 | |
Piotr Sroka | edf9857 | 2017-03-21 14:33:26 +0000 | [diff] [blame] | 352 | clk = devm_clk_get(dev, NULL); |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 353 | if (IS_ERR(clk)) |
| 354 | return PTR_ERR(clk); |
| 355 | |
| 356 | ret = clk_prepare_enable(clk); |
| 357 | if (ret) |
| 358 | return ret; |
| 359 | |
Masahiro Yamada | 18b587b | 2020-03-12 19:42:57 +0900 | [diff] [blame] | 360 | data = of_device_get_match_data(dev); |
| 361 | if (!data) |
| 362 | data = &sdhci_cdns_pltfm_data; |
| 363 | |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 364 | nr_phy_params = sdhci_cdns_phy_param_count(dev->of_node); |
Masahiro Yamada | 18b587b | 2020-03-12 19:42:57 +0900 | [diff] [blame] | 365 | host = sdhci_pltfm_init(pdev, data, |
Gustavo A. R. Silva | 159a8b4 | 2019-08-08 11:53:01 -0500 | [diff] [blame] | 366 | struct_size(priv, phy_params, nr_phy_params)); |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 367 | if (IS_ERR(host)) { |
| 368 | ret = PTR_ERR(host); |
| 369 | goto disable_clk; |
| 370 | } |
| 371 | |
| 372 | pltfm_host = sdhci_priv(host); |
| 373 | pltfm_host->clk = clk; |
| 374 | |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 375 | priv = sdhci_pltfm_priv(pltfm_host); |
| 376 | priv->nr_phy_params = nr_phy_params; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 377 | priv->hrs_addr = host->ioaddr; |
Piotr Sroka | d12990f | 2017-03-06 08:28:41 +0000 | [diff] [blame] | 378 | priv->enhanced_strobe = false; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 379 | host->ioaddr += SDHCI_CDNS_SRS_BASE; |
| 380 | host->mmc_host_ops.execute_tuning = sdhci_cdns_execute_tuning; |
Piotr Sroka | d12990f | 2017-03-06 08:28:41 +0000 | [diff] [blame] | 381 | host->mmc_host_ops.hs400_enhanced_strobe = |
| 382 | sdhci_cdns_hs400_enhanced_strobe; |
Masahiro Yamada | e73a3896 | 2019-08-29 19:49:26 +0900 | [diff] [blame] | 383 | sdhci_enable_v4_mode(host); |
Masahiro Yamada | 12a632e | 2019-08-29 19:49:28 +0900 | [diff] [blame] | 384 | __sdhci_read_caps(host, &version, NULL, NULL); |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 385 | |
Piotr Sroka | 861183f | 2017-04-11 12:13:38 +0100 | [diff] [blame] | 386 | sdhci_get_of_property(pdev); |
| 387 | |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 388 | ret = mmc_of_parse(host->mmc); |
| 389 | if (ret) |
| 390 | goto free; |
| 391 | |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 392 | sdhci_cdns_phy_param_parse(dev->of_node, priv); |
| 393 | |
| 394 | ret = sdhci_cdns_phy_init(priv); |
Piotr Sroka | a89c472 | 2017-03-21 14:33:11 +0000 | [diff] [blame] | 395 | if (ret) |
| 396 | goto free; |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 397 | |
| 398 | ret = sdhci_add_host(host); |
| 399 | if (ret) |
| 400 | goto free; |
| 401 | |
| 402 | return 0; |
| 403 | free: |
| 404 | sdhci_pltfm_free(pdev); |
| 405 | disable_clk: |
| 406 | clk_disable_unprepare(clk); |
| 407 | |
| 408 | return ret; |
| 409 | } |
| 410 | |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 411 | #ifdef CONFIG_PM_SLEEP |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 412 | static int sdhci_cdns_resume(struct device *dev) |
| 413 | { |
| 414 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 415 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 416 | struct sdhci_cdns_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 417 | int ret; |
| 418 | |
| 419 | ret = clk_prepare_enable(pltfm_host->clk); |
| 420 | if (ret) |
| 421 | return ret; |
| 422 | |
| 423 | ret = sdhci_cdns_phy_init(priv); |
| 424 | if (ret) |
| 425 | goto disable_clk; |
| 426 | |
| 427 | ret = sdhci_resume_host(host); |
| 428 | if (ret) |
| 429 | goto disable_clk; |
| 430 | |
| 431 | return 0; |
| 432 | |
| 433 | disable_clk: |
| 434 | clk_disable_unprepare(pltfm_host->clk); |
| 435 | |
| 436 | return ret; |
| 437 | } |
| 438 | #endif |
| 439 | |
| 440 | static const struct dev_pm_ops sdhci_cdns_pm_ops = { |
Masahiro Yamada | 83a7b32 | 2017-08-23 13:15:03 +0900 | [diff] [blame] | 441 | SET_SYSTEM_SLEEP_PM_OPS(sdhci_pltfm_suspend, sdhci_cdns_resume) |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 442 | }; |
| 443 | |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 444 | static const struct of_device_id sdhci_cdns_match[] = { |
Masahiro Yamada | 18b587b | 2020-03-12 19:42:57 +0900 | [diff] [blame] | 445 | { |
| 446 | .compatible = "socionext,uniphier-sd4hc", |
| 447 | .data = &sdhci_cdns_uniphier_pltfm_data, |
| 448 | }, |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 449 | { .compatible = "cdns,sd4hc" }, |
| 450 | { /* sentinel */ } |
| 451 | }; |
| 452 | MODULE_DEVICE_TABLE(of, sdhci_cdns_match); |
| 453 | |
| 454 | static struct platform_driver sdhci_cdns_driver = { |
| 455 | .driver = { |
| 456 | .name = "sdhci-cdns", |
Masahiro Yamada | a232a8f | 2017-08-23 13:15:00 +0900 | [diff] [blame] | 457 | .pm = &sdhci_cdns_pm_ops, |
Masahiro Yamada | ff6af28 | 2016-12-08 21:50:55 +0900 | [diff] [blame] | 458 | .of_match_table = sdhci_cdns_match, |
| 459 | }, |
| 460 | .probe = sdhci_cdns_probe, |
| 461 | .remove = sdhci_pltfm_unregister, |
| 462 | }; |
| 463 | module_platform_driver(sdhci_cdns_driver); |
| 464 | |
| 465 | MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>"); |
| 466 | MODULE_DESCRIPTION("Cadence SD/SDIO/eMMC Host Controller Driver"); |
| 467 | MODULE_LICENSE("GPL"); |