1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2010 Marvell International Ltd.
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/clk.h>
15 #include <linux/module.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/host.h>
19 #include <linux/platform_data/pxa_sdhci.h>
20 #include <linux/slab.h>
22 #include <linux/of_device.h>
23 #include <linux/mmc/sdio.h>
24 #include <linux/mmc/mmc.h>
25 #include <linux/pinctrl/consumer.h>
28 #include "sdhci-pltfm.h"
30 #define SD_FIFO_PARAM 0xe0
31 #define DIS_PAD_SD_CLK_GATE 0x0400 /* Turn on/off Dynamic SD Clock Gating */
32 #define CLK_GATE_ON 0x0200 /* Disable/enable Clock Gate */
33 #define CLK_GATE_CTL 0x0100 /* Clock Gate Control */
34 #define CLK_GATE_SETTING_BITS (DIS_PAD_SD_CLK_GATE | \
35 CLK_GATE_ON | CLK_GATE_CTL)
37 #define SD_CLOCK_BURST_SIZE_SETUP 0xe6
38 #define SDCLK_SEL_SHIFT 8
39 #define SDCLK_SEL_MASK 0x3
40 #define SDCLK_DELAY_SHIFT 10
41 #define SDCLK_DELAY_MASK 0x3c
43 #define SD_CE_ATA_2 0xea
44 #define MMC_CARD 0x1000
45 #define MMC_WIDTH 0x0100
47 struct sdhci_pxav2_host {
48 struct mmc_request *sdio_mrq;
49 struct pinctrl *pinctrl;
50 struct pinctrl_state *pins_default;
51 struct pinctrl_state *pins_cmd_gpio;
54 static void pxav2_reset(struct sdhci_host *host, u8 mask)
56 struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
57 struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
59 sdhci_reset(host, mask);
61 if (mask == SDHCI_RESET_ALL) {
65 * tune timing of read data/command when crc error happen
66 * no performance impact
68 if (pdata && pdata->clk_delay_sel == 1) {
69 tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
71 tmp &= ~(SDCLK_DELAY_MASK << SDCLK_DELAY_SHIFT);
72 tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK)
74 tmp &= ~(SDCLK_SEL_MASK << SDCLK_SEL_SHIFT);
75 tmp |= (1 & SDCLK_SEL_MASK) << SDCLK_SEL_SHIFT;
77 writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
80 if (pdata && (pdata->flags & PXA_FLAG_ENABLE_CLOCK_GATING)) {
81 tmp = readw(host->ioaddr + SD_FIFO_PARAM);
82 tmp &= ~CLK_GATE_SETTING_BITS;
83 writew(tmp, host->ioaddr + SD_FIFO_PARAM);
85 tmp = readw(host->ioaddr + SD_FIFO_PARAM);
86 tmp &= ~CLK_GATE_SETTING_BITS;
87 tmp |= CLK_GATE_SETTING_BITS;
88 writew(tmp, host->ioaddr + SD_FIFO_PARAM);
93 static u16 pxav1_readw(struct sdhci_host *host, int reg)
95 /* Workaround for data abort exception on SDH2 and SDH4 on PXA168 */
96 if (reg == SDHCI_HOST_VERSION)
97 return readl(host->ioaddr + SDHCI_HOST_VERSION - 2) >> 16;
99 return readw(host->ioaddr + reg);
102 static u32 pxav1_irq(struct sdhci_host *host, u32 intmask)
104 struct sdhci_pxav2_host *pxav2_host = sdhci_pltfm_priv(sdhci_priv(host));
105 struct mmc_request *sdio_mrq;
107 if (pxav2_host->sdio_mrq && (intmask & SDHCI_INT_CMD_MASK)) {
108 /* The dummy CMD0 for the SDIO workaround just completed */
109 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK, SDHCI_INT_STATUS);
110 intmask &= ~SDHCI_INT_CMD_MASK;
112 /* Restore MMC function to CMD pin */
113 if (pxav2_host->pinctrl && pxav2_host->pins_default)
114 pinctrl_select_state(pxav2_host->pinctrl, pxav2_host->pins_default);
116 sdio_mrq = pxav2_host->sdio_mrq;
117 pxav2_host->sdio_mrq = NULL;
118 mmc_request_done(host->mmc, sdio_mrq);
124 static void pxav1_request_done(struct sdhci_host *host, struct mmc_request *mrq)
127 struct sdhci_pxav2_host *pxav2_host;
129 /* If this is an SDIO command, perform errata workaround for silicon bug */
130 if (mrq->cmd && !mrq->cmd->error &&
131 (mrq->cmd->opcode == SD_IO_RW_DIRECT ||
132 mrq->cmd->opcode == SD_IO_RW_EXTENDED)) {
133 /* Reset data port */
134 tmp = readw(host->ioaddr + SDHCI_TIMEOUT_CONTROL);
136 writew(tmp, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
138 /* Clock is now stopped, so restart it by sending a dummy CMD0 */
139 pxav2_host = sdhci_pltfm_priv(sdhci_priv(host));
140 pxav2_host->sdio_mrq = mrq;
142 /* Set CMD as high output rather than MMC function while we do CMD0 */
143 if (pxav2_host->pinctrl && pxav2_host->pins_cmd_gpio)
144 pinctrl_select_state(pxav2_host->pinctrl, pxav2_host->pins_cmd_gpio);
146 sdhci_writel(host, 0, SDHCI_ARGUMENT);
147 sdhci_writew(host, 0, SDHCI_TRANSFER_MODE);
148 sdhci_writew(host, SDHCI_MAKE_CMD(MMC_GO_IDLE_STATE, SDHCI_CMD_RESP_NONE),
151 /* Don't finish this request until the dummy CMD0 finishes */
155 mmc_request_done(host->mmc, mrq);
158 static void pxav2_mmc_set_bus_width(struct sdhci_host *host, int width)
163 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
164 tmp = readw(host->ioaddr + SD_CE_ATA_2);
165 if (width == MMC_BUS_WIDTH_8) {
166 ctrl &= ~SDHCI_CTRL_4BITBUS;
167 tmp |= MMC_CARD | MMC_WIDTH;
169 tmp &= ~(MMC_CARD | MMC_WIDTH);
170 if (width == MMC_BUS_WIDTH_4)
171 ctrl |= SDHCI_CTRL_4BITBUS;
173 ctrl &= ~SDHCI_CTRL_4BITBUS;
175 writew(tmp, host->ioaddr + SD_CE_ATA_2);
176 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
179 struct sdhci_pxa_variant {
180 const struct sdhci_ops *ops;
181 unsigned int extra_quirks;
184 static const struct sdhci_ops pxav1_sdhci_ops = {
185 .read_w = pxav1_readw,
186 .set_clock = sdhci_set_clock,
188 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
189 .set_bus_width = pxav2_mmc_set_bus_width,
190 .reset = pxav2_reset,
191 .set_uhs_signaling = sdhci_set_uhs_signaling,
192 .request_done = pxav1_request_done,
195 static const struct sdhci_pxa_variant __maybe_unused pxav1_variant = {
196 .ops = &pxav1_sdhci_ops,
197 .extra_quirks = SDHCI_QUIRK_NO_BUSY_IRQ | SDHCI_QUIRK_32BIT_DMA_SIZE,
200 static const struct sdhci_ops pxav2_sdhci_ops = {
201 .set_clock = sdhci_set_clock,
202 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
203 .set_bus_width = pxav2_mmc_set_bus_width,
204 .reset = pxav2_reset,
205 .set_uhs_signaling = sdhci_set_uhs_signaling,
208 static const struct sdhci_pxa_variant pxav2_variant = {
209 .ops = &pxav2_sdhci_ops,
213 static const struct of_device_id sdhci_pxav2_of_match[] = {
214 { .compatible = "mrvl,pxav1-mmc", .data = &pxav1_variant, },
215 { .compatible = "mrvl,pxav2-mmc", .data = &pxav2_variant, },
218 MODULE_DEVICE_TABLE(of, sdhci_pxav2_of_match);
220 static struct sdhci_pxa_platdata *pxav2_get_mmc_pdata(struct device *dev)
222 struct sdhci_pxa_platdata *pdata;
223 struct device_node *np = dev->of_node;
225 u32 clk_delay_cycles;
227 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
231 if (of_property_read_bool(np, "non-removable"))
232 pdata->flags |= PXA_FLAG_CARD_PERMANENT;
234 of_property_read_u32(np, "bus-width", &bus_width);
236 pdata->flags |= PXA_FLAG_SD_8_BIT_CAPABLE_SLOT;
238 of_property_read_u32(np, "mrvl,clk-delay-cycles", &clk_delay_cycles);
239 if (clk_delay_cycles > 0) {
240 pdata->clk_delay_sel = 1;
241 pdata->clk_delay_cycles = clk_delay_cycles;
247 static inline struct sdhci_pxa_platdata *pxav2_get_mmc_pdata(struct device *dev)
253 static int sdhci_pxav2_probe(struct platform_device *pdev)
255 struct sdhci_pltfm_host *pltfm_host;
256 struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
257 struct sdhci_pxav2_host *pxav2_host;
258 struct device *dev = &pdev->dev;
259 struct sdhci_host *host = NULL;
260 const struct sdhci_pxa_variant *variant;
263 struct clk *clk, *clk_core;
265 host = sdhci_pltfm_init(pdev, NULL, sizeof(*pxav2_host));
267 return PTR_ERR(host);
269 pltfm_host = sdhci_priv(host);
270 pxav2_host = sdhci_pltfm_priv(pltfm_host);
272 clk = devm_clk_get(dev, "io");
273 if (IS_ERR(clk) && PTR_ERR(clk) != -EPROBE_DEFER)
274 clk = devm_clk_get(dev, NULL);
277 dev_err_probe(dev, ret, "failed to get io clock\n");
280 pltfm_host->clk = clk;
281 ret = clk_prepare_enable(clk);
283 dev_err(dev, "failed to enable io clock\n");
287 clk_core = devm_clk_get_optional_enabled(dev, "core");
288 if (IS_ERR(clk_core)) {
289 ret = PTR_ERR(clk_core);
290 dev_err_probe(dev, ret, "failed to enable core clock\n");
294 host->quirks = SDHCI_QUIRK_BROKEN_ADMA
295 | SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
296 | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
298 variant = of_device_get_match_data(dev);
300 pdata = pxav2_get_mmc_pdata(dev);
302 variant = &pxav2_variant;
305 if (pdata->flags & PXA_FLAG_CARD_PERMANENT) {
307 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
308 host->mmc->caps |= MMC_CAP_NONREMOVABLE;
311 /* If slot design supports 8 bit data, indicate this to MMC. */
312 if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
313 host->mmc->caps |= MMC_CAP_8_BIT_DATA;
316 host->quirks |= pdata->quirks;
317 if (pdata->host_caps)
318 host->mmc->caps |= pdata->host_caps;
320 host->mmc->pm_caps |= pdata->pm_caps;
323 host->quirks |= variant->extra_quirks;
324 host->ops = variant->ops;
326 /* Set up optional pinctrl for PXA168 SDIO IRQ fix */
327 pxav2_host->pinctrl = devm_pinctrl_get(dev);
328 if (!IS_ERR(pxav2_host->pinctrl)) {
329 pxav2_host->pins_cmd_gpio = pinctrl_lookup_state(pxav2_host->pinctrl,
331 if (IS_ERR(pxav2_host->pins_cmd_gpio))
332 pxav2_host->pins_cmd_gpio = NULL;
333 pxav2_host->pins_default = pinctrl_lookup_state(pxav2_host->pinctrl,
335 if (IS_ERR(pxav2_host->pins_default))
336 pxav2_host->pins_default = NULL;
338 pxav2_host->pinctrl = NULL;
341 ret = sdhci_add_host(host);
348 clk_disable_unprepare(clk);
350 sdhci_pltfm_free(pdev);
354 static struct platform_driver sdhci_pxav2_driver = {
356 .name = "sdhci-pxav2",
357 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
358 .of_match_table = of_match_ptr(sdhci_pxav2_of_match),
359 .pm = &sdhci_pltfm_pmops,
361 .probe = sdhci_pxav2_probe,
362 .remove = sdhci_pltfm_unregister,
365 module_platform_driver(sdhci_pxav2_driver);
367 MODULE_DESCRIPTION("SDHCI driver for pxav2");
368 MODULE_AUTHOR("Marvell International Ltd.");
369 MODULE_LICENSE("GPL v2");