sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / mmc / host / sdhci-cadence.c
blob4b0ecb981842248b1cc4ea8edaf35dc4bac753e1
1 /*
2 * Copyright (C) 2016 Socionext Inc.
3 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/bitops.h>
17 #include <linux/iopoll.h>
18 #include <linux/module.h>
19 #include <linux/mmc/host.h>
21 #include "sdhci-pltfm.h"
23 /* HRS - Host Register Set (specific to Cadence) */
24 #define SDHCI_CDNS_HRS04 0x10 /* PHY access port */
25 #define SDHCI_CDNS_HRS04_ACK BIT(26)
26 #define SDHCI_CDNS_HRS04_RD BIT(25)
27 #define SDHCI_CDNS_HRS04_WR BIT(24)
28 #define SDHCI_CDNS_HRS04_RDATA_SHIFT 12
29 #define SDHCI_CDNS_HRS04_WDATA_SHIFT 8
30 #define SDHCI_CDNS_HRS04_ADDR_SHIFT 0
32 #define SDHCI_CDNS_HRS06 0x18 /* eMMC control */
33 #define SDHCI_CDNS_HRS06_TUNE_UP BIT(15)
34 #define SDHCI_CDNS_HRS06_TUNE_SHIFT 8
35 #define SDHCI_CDNS_HRS06_TUNE_MASK 0x3f
36 #define SDHCI_CDNS_HRS06_MODE_MASK 0x7
37 #define SDHCI_CDNS_HRS06_MODE_SD 0x0
38 #define SDHCI_CDNS_HRS06_MODE_MMC_SDR 0x2
39 #define SDHCI_CDNS_HRS06_MODE_MMC_DDR 0x3
40 #define SDHCI_CDNS_HRS06_MODE_MMC_HS200 0x4
41 #define SDHCI_CDNS_HRS06_MODE_MMC_HS400 0x5
43 /* SRS - Slot Register Set (SDHCI-compatible) */
44 #define SDHCI_CDNS_SRS_BASE 0x200
46 /* PHY */
47 #define SDHCI_CDNS_PHY_DLY_SD_HS 0x00
48 #define SDHCI_CDNS_PHY_DLY_SD_DEFAULT 0x01
49 #define SDHCI_CDNS_PHY_DLY_UHS_SDR12 0x02
50 #define SDHCI_CDNS_PHY_DLY_UHS_SDR25 0x03
51 #define SDHCI_CDNS_PHY_DLY_UHS_SDR50 0x04
52 #define SDHCI_CDNS_PHY_DLY_UHS_DDR50 0x05
53 #define SDHCI_CDNS_PHY_DLY_EMMC_LEGACY 0x06
54 #define SDHCI_CDNS_PHY_DLY_EMMC_SDR 0x07
55 #define SDHCI_CDNS_PHY_DLY_EMMC_DDR 0x08
58 * The tuned val register is 6 bit-wide, but not the whole of the range is
59 * available. The range 0-42 seems to be available (then 43 wraps around to 0)
60 * but I am not quite sure if it is official. Use only 0 to 39 for safety.
62 #define SDHCI_CDNS_MAX_TUNING_LOOP 40
64 struct sdhci_cdns_priv {
65 void __iomem *hrs_addr;
68 static void sdhci_cdns_write_phy_reg(struct sdhci_cdns_priv *priv,
69 u8 addr, u8 data)
71 void __iomem *reg = priv->hrs_addr + SDHCI_CDNS_HRS04;
72 u32 tmp;
74 tmp = (data << SDHCI_CDNS_HRS04_WDATA_SHIFT) |
75 (addr << SDHCI_CDNS_HRS04_ADDR_SHIFT);
76 writel(tmp, reg);
78 tmp |= SDHCI_CDNS_HRS04_WR;
79 writel(tmp, reg);
81 tmp &= ~SDHCI_CDNS_HRS04_WR;
82 writel(tmp, reg);
85 static void sdhci_cdns_phy_init(struct sdhci_cdns_priv *priv)
87 sdhci_cdns_write_phy_reg(priv, SDHCI_CDNS_PHY_DLY_SD_HS, 4);
88 sdhci_cdns_write_phy_reg(priv, SDHCI_CDNS_PHY_DLY_SD_DEFAULT, 4);
89 sdhci_cdns_write_phy_reg(priv, SDHCI_CDNS_PHY_DLY_EMMC_LEGACY, 9);
90 sdhci_cdns_write_phy_reg(priv, SDHCI_CDNS_PHY_DLY_EMMC_SDR, 2);
91 sdhci_cdns_write_phy_reg(priv, SDHCI_CDNS_PHY_DLY_EMMC_DDR, 3);
94 static inline void *sdhci_cdns_priv(struct sdhci_host *host)
96 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
98 return sdhci_pltfm_priv(pltfm_host);
101 static unsigned int sdhci_cdns_get_timeout_clock(struct sdhci_host *host)
104 * Cadence's spec says the Timeout Clock Frequency is the same as the
105 * Base Clock Frequency. Divide it by 1000 to return a value in kHz.
107 return host->max_clk / 1000;
110 static void sdhci_cdns_set_uhs_signaling(struct sdhci_host *host,
111 unsigned int timing)
113 struct sdhci_cdns_priv *priv = sdhci_cdns_priv(host);
114 u32 mode, tmp;
116 switch (timing) {
117 case MMC_TIMING_MMC_HS:
118 mode = SDHCI_CDNS_HRS06_MODE_MMC_SDR;
119 break;
120 case MMC_TIMING_MMC_DDR52:
121 mode = SDHCI_CDNS_HRS06_MODE_MMC_DDR;
122 break;
123 case MMC_TIMING_MMC_HS200:
124 mode = SDHCI_CDNS_HRS06_MODE_MMC_HS200;
125 break;
126 case MMC_TIMING_MMC_HS400:
127 mode = SDHCI_CDNS_HRS06_MODE_MMC_HS400;
128 break;
129 default:
130 mode = SDHCI_CDNS_HRS06_MODE_SD;
131 break;
134 /* The speed mode for eMMC is selected by HRS06 register */
135 tmp = readl(priv->hrs_addr + SDHCI_CDNS_HRS06);
136 tmp &= ~SDHCI_CDNS_HRS06_MODE_MASK;
137 tmp |= mode;
138 writel(tmp, priv->hrs_addr + SDHCI_CDNS_HRS06);
140 /* For SD, fall back to the default handler */
141 if (mode == SDHCI_CDNS_HRS06_MODE_SD)
142 sdhci_set_uhs_signaling(host, timing);
145 static const struct sdhci_ops sdhci_cdns_ops = {
146 .set_clock = sdhci_set_clock,
147 .get_timeout_clock = sdhci_cdns_get_timeout_clock,
148 .set_bus_width = sdhci_set_bus_width,
149 .reset = sdhci_reset,
150 .set_uhs_signaling = sdhci_cdns_set_uhs_signaling,
153 static const struct sdhci_pltfm_data sdhci_cdns_pltfm_data = {
154 .ops = &sdhci_cdns_ops,
157 static int sdhci_cdns_set_tune_val(struct sdhci_host *host, unsigned int val)
159 struct sdhci_cdns_priv *priv = sdhci_cdns_priv(host);
160 void __iomem *reg = priv->hrs_addr + SDHCI_CDNS_HRS06;
161 u32 tmp;
163 if (WARN_ON(val > SDHCI_CDNS_HRS06_TUNE_MASK))
164 return -EINVAL;
166 tmp = readl(reg);
167 tmp &= ~(SDHCI_CDNS_HRS06_TUNE_MASK << SDHCI_CDNS_HRS06_TUNE_SHIFT);
168 tmp |= val << SDHCI_CDNS_HRS06_TUNE_SHIFT;
169 tmp |= SDHCI_CDNS_HRS06_TUNE_UP;
170 writel(tmp, reg);
172 return readl_poll_timeout(reg, tmp, !(tmp & SDHCI_CDNS_HRS06_TUNE_UP),
173 0, 1);
176 static int sdhci_cdns_execute_tuning(struct mmc_host *mmc, u32 opcode)
178 struct sdhci_host *host = mmc_priv(mmc);
179 int cur_streak = 0;
180 int max_streak = 0;
181 int end_of_streak = 0;
182 int i;
185 * This handler only implements the eMMC tuning that is specific to
186 * this controller. Fall back to the standard method for SD timing.
188 if (host->timing != MMC_TIMING_MMC_HS200)
189 return sdhci_execute_tuning(mmc, opcode);
191 if (WARN_ON(opcode != MMC_SEND_TUNING_BLOCK_HS200))
192 return -EINVAL;
194 for (i = 0; i < SDHCI_CDNS_MAX_TUNING_LOOP; i++) {
195 if (sdhci_cdns_set_tune_val(host, i) ||
196 mmc_send_tuning(host->mmc, opcode, NULL)) { /* bad */
197 cur_streak = 0;
198 } else { /* good */
199 cur_streak++;
200 if (cur_streak > max_streak) {
201 max_streak = cur_streak;
202 end_of_streak = i;
207 if (!max_streak) {
208 dev_err(mmc_dev(host->mmc), "no tuning point found\n");
209 return -EIO;
212 return sdhci_cdns_set_tune_val(host, end_of_streak - max_streak / 2);
215 static int sdhci_cdns_probe(struct platform_device *pdev)
217 struct sdhci_host *host;
218 struct sdhci_pltfm_host *pltfm_host;
219 struct sdhci_cdns_priv *priv;
220 struct clk *clk;
221 int ret;
223 clk = devm_clk_get(&pdev->dev, NULL);
224 if (IS_ERR(clk))
225 return PTR_ERR(clk);
227 ret = clk_prepare_enable(clk);
228 if (ret)
229 return ret;
231 host = sdhci_pltfm_init(pdev, &sdhci_cdns_pltfm_data, sizeof(*priv));
232 if (IS_ERR(host)) {
233 ret = PTR_ERR(host);
234 goto disable_clk;
237 pltfm_host = sdhci_priv(host);
238 pltfm_host->clk = clk;
240 priv = sdhci_cdns_priv(host);
241 priv->hrs_addr = host->ioaddr;
242 host->ioaddr += SDHCI_CDNS_SRS_BASE;
243 host->mmc_host_ops.execute_tuning = sdhci_cdns_execute_tuning;
245 ret = mmc_of_parse(host->mmc);
246 if (ret)
247 goto free;
249 sdhci_cdns_phy_init(priv);
251 ret = sdhci_add_host(host);
252 if (ret)
253 goto free;
255 return 0;
256 free:
257 sdhci_pltfm_free(pdev);
258 disable_clk:
259 clk_disable_unprepare(clk);
261 return ret;
264 static const struct of_device_id sdhci_cdns_match[] = {
265 { .compatible = "socionext,uniphier-sd4hc" },
266 { .compatible = "cdns,sd4hc" },
267 { /* sentinel */ }
269 MODULE_DEVICE_TABLE(of, sdhci_cdns_match);
271 static struct platform_driver sdhci_cdns_driver = {
272 .driver = {
273 .name = "sdhci-cdns",
274 .pm = &sdhci_pltfm_pmops,
275 .of_match_table = sdhci_cdns_match,
277 .probe = sdhci_cdns_probe,
278 .remove = sdhci_pltfm_unregister,
280 module_platform_driver(sdhci_cdns_driver);
282 MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
283 MODULE_DESCRIPTION("Cadence SD/SDIO/eMMC Host Controller Driver");
284 MODULE_LICENSE("GPL");