1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2010 Marvell International Ltd.
4 * Zhangfei Gao <zhangfei.gao@marvell.com>
5 * Kevin Wang <dwang4@marvell.com>
6 * Jun Nie <njun@marvell.com>
7 * Qiming Wu <wuqm@marvell.com>
8 * Philip Rakity <prakity@marvell.com>
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/mmc/sdio.h>
23 #include <linux/mmc/mmc.h>
24 #include <linux/pinctrl/consumer.h>
27 #include "sdhci-pltfm.h"
29 #define SD_FIFO_PARAM 0xe0
30 #define DIS_PAD_SD_CLK_GATE 0x0400 /* Turn on/off Dynamic SD Clock Gating */
31 #define CLK_GATE_ON 0x0200 /* Disable/enable Clock Gate */
32 #define CLK_GATE_CTL 0x0100 /* Clock Gate Control */
33 #define CLK_GATE_SETTING_BITS (DIS_PAD_SD_CLK_GATE | \
34 CLK_GATE_ON | CLK_GATE_CTL)
36 #define SD_CLOCK_BURST_SIZE_SETUP 0xe6
37 #define SDCLK_SEL_SHIFT 8
38 #define SDCLK_SEL_MASK 0x3
39 #define SDCLK_DELAY_SHIFT 10
40 #define SDCLK_DELAY_MASK 0x3c
42 #define SD_CE_ATA_2 0xea
43 #define MMC_CARD 0x1000
44 #define MMC_WIDTH 0x0100
46 struct sdhci_pxav2_host
{
47 struct mmc_request
*sdio_mrq
;
48 struct pinctrl
*pinctrl
;
49 struct pinctrl_state
*pins_default
;
50 struct pinctrl_state
*pins_cmd_gpio
;
53 static void pxav2_reset(struct sdhci_host
*host
, u8 mask
)
55 struct platform_device
*pdev
= to_platform_device(mmc_dev(host
->mmc
));
56 struct sdhci_pxa_platdata
*pdata
= pdev
->dev
.platform_data
;
58 sdhci_reset(host
, mask
);
60 if (mask
== SDHCI_RESET_ALL
) {
64 * tune timing of read data/command when crc error happen
65 * no performance impact
67 if (pdata
&& pdata
->clk_delay_sel
== 1) {
68 tmp
= readw(host
->ioaddr
+ SD_CLOCK_BURST_SIZE_SETUP
);
70 tmp
&= ~(SDCLK_DELAY_MASK
<< SDCLK_DELAY_SHIFT
);
71 tmp
|= (pdata
->clk_delay_cycles
& SDCLK_DELAY_MASK
)
73 tmp
&= ~(SDCLK_SEL_MASK
<< SDCLK_SEL_SHIFT
);
74 tmp
|= (1 & SDCLK_SEL_MASK
) << SDCLK_SEL_SHIFT
;
76 writew(tmp
, host
->ioaddr
+ SD_CLOCK_BURST_SIZE_SETUP
);
79 if (pdata
&& (pdata
->flags
& PXA_FLAG_ENABLE_CLOCK_GATING
)) {
80 tmp
= readw(host
->ioaddr
+ SD_FIFO_PARAM
);
81 tmp
&= ~CLK_GATE_SETTING_BITS
;
82 writew(tmp
, host
->ioaddr
+ SD_FIFO_PARAM
);
84 tmp
= readw(host
->ioaddr
+ SD_FIFO_PARAM
);
85 tmp
&= ~CLK_GATE_SETTING_BITS
;
86 tmp
|= CLK_GATE_SETTING_BITS
;
87 writew(tmp
, host
->ioaddr
+ SD_FIFO_PARAM
);
92 static u16
pxav1_readw(struct sdhci_host
*host
, int reg
)
94 /* Workaround for data abort exception on SDH2 and SDH4 on PXA168 */
95 if (reg
== SDHCI_HOST_VERSION
)
96 return readl(host
->ioaddr
+ SDHCI_HOST_VERSION
- 2) >> 16;
98 return readw(host
->ioaddr
+ reg
);
101 static u32
pxav1_irq(struct sdhci_host
*host
, u32 intmask
)
103 struct sdhci_pxav2_host
*pxav2_host
= sdhci_pltfm_priv(sdhci_priv(host
));
104 struct mmc_request
*sdio_mrq
;
106 if (pxav2_host
->sdio_mrq
&& (intmask
& SDHCI_INT_CMD_MASK
)) {
107 /* The dummy CMD0 for the SDIO workaround just completed */
108 sdhci_writel(host
, intmask
& SDHCI_INT_CMD_MASK
, SDHCI_INT_STATUS
);
109 intmask
&= ~SDHCI_INT_CMD_MASK
;
111 /* Restore MMC function to CMD pin */
112 if (pxav2_host
->pinctrl
&& pxav2_host
->pins_default
)
113 pinctrl_select_state(pxav2_host
->pinctrl
, pxav2_host
->pins_default
);
115 sdio_mrq
= pxav2_host
->sdio_mrq
;
116 pxav2_host
->sdio_mrq
= NULL
;
117 mmc_request_done(host
->mmc
, sdio_mrq
);
123 static void pxav1_request_done(struct sdhci_host
*host
, struct mmc_request
*mrq
)
126 struct sdhci_pxav2_host
*pxav2_host
;
128 /* If this is an SDIO command, perform errata workaround for silicon bug */
129 if (!mrq
->cmd
->error
&&
130 (mrq
->cmd
->opcode
== SD_IO_RW_DIRECT
||
131 mrq
->cmd
->opcode
== SD_IO_RW_EXTENDED
)) {
132 /* Reset data port */
133 tmp
= readw(host
->ioaddr
+ SDHCI_TIMEOUT_CONTROL
);
135 writew(tmp
, host
->ioaddr
+ SDHCI_TIMEOUT_CONTROL
);
137 /* Clock is now stopped, so restart it by sending a dummy CMD0 */
138 pxav2_host
= sdhci_pltfm_priv(sdhci_priv(host
));
139 pxav2_host
->sdio_mrq
= mrq
;
141 /* Set CMD as high output rather than MMC function while we do CMD0 */
142 if (pxav2_host
->pinctrl
&& pxav2_host
->pins_cmd_gpio
)
143 pinctrl_select_state(pxav2_host
->pinctrl
, pxav2_host
->pins_cmd_gpio
);
145 sdhci_writel(host
, 0, SDHCI_ARGUMENT
);
146 sdhci_writew(host
, 0, SDHCI_TRANSFER_MODE
);
147 sdhci_writew(host
, SDHCI_MAKE_CMD(MMC_GO_IDLE_STATE
, SDHCI_CMD_RESP_NONE
),
150 /* Don't finish this request until the dummy CMD0 finishes */
154 mmc_request_done(host
->mmc
, mrq
);
157 static void pxav2_mmc_set_bus_width(struct sdhci_host
*host
, int width
)
162 ctrl
= readb(host
->ioaddr
+ SDHCI_HOST_CONTROL
);
163 tmp
= readw(host
->ioaddr
+ SD_CE_ATA_2
);
164 if (width
== MMC_BUS_WIDTH_8
) {
165 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
166 tmp
|= MMC_CARD
| MMC_WIDTH
;
168 tmp
&= ~(MMC_CARD
| MMC_WIDTH
);
169 if (width
== MMC_BUS_WIDTH_4
)
170 ctrl
|= SDHCI_CTRL_4BITBUS
;
172 ctrl
&= ~SDHCI_CTRL_4BITBUS
;
174 writew(tmp
, host
->ioaddr
+ SD_CE_ATA_2
);
175 writeb(ctrl
, host
->ioaddr
+ SDHCI_HOST_CONTROL
);
178 struct sdhci_pxa_variant
{
179 const struct sdhci_ops
*ops
;
180 unsigned int extra_quirks
;
183 static const struct sdhci_ops pxav1_sdhci_ops
= {
184 .read_w
= pxav1_readw
,
185 .set_clock
= sdhci_set_clock
,
187 .get_max_clock
= sdhci_pltfm_clk_get_max_clock
,
188 .set_bus_width
= pxav2_mmc_set_bus_width
,
189 .reset
= pxav2_reset
,
190 .set_uhs_signaling
= sdhci_set_uhs_signaling
,
191 .request_done
= pxav1_request_done
,
194 static const struct sdhci_pxa_variant __maybe_unused pxav1_variant
= {
195 .ops
= &pxav1_sdhci_ops
,
196 .extra_quirks
= SDHCI_QUIRK_NO_BUSY_IRQ
| SDHCI_QUIRK_32BIT_DMA_SIZE
,
199 static const struct sdhci_ops pxav2_sdhci_ops
= {
200 .set_clock
= sdhci_set_clock
,
201 .get_max_clock
= sdhci_pltfm_clk_get_max_clock
,
202 .set_bus_width
= pxav2_mmc_set_bus_width
,
203 .reset
= pxav2_reset
,
204 .set_uhs_signaling
= sdhci_set_uhs_signaling
,
207 static const struct sdhci_pxa_variant pxav2_variant
= {
208 .ops
= &pxav2_sdhci_ops
,
212 static const struct of_device_id sdhci_pxav2_of_match
[] = {
213 { .compatible
= "mrvl,pxav1-mmc", .data
= &pxav1_variant
, },
214 { .compatible
= "mrvl,pxav2-mmc", .data
= &pxav2_variant
, },
217 MODULE_DEVICE_TABLE(of
, sdhci_pxav2_of_match
);
219 static struct sdhci_pxa_platdata
*pxav2_get_mmc_pdata(struct device
*dev
)
221 struct sdhci_pxa_platdata
*pdata
;
222 struct device_node
*np
= dev
->of_node
;
224 u32 clk_delay_cycles
;
226 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
230 if (of_property_read_bool(np
, "non-removable"))
231 pdata
->flags
|= PXA_FLAG_CARD_PERMANENT
;
233 of_property_read_u32(np
, "bus-width", &bus_width
);
235 pdata
->flags
|= PXA_FLAG_SD_8_BIT_CAPABLE_SLOT
;
237 of_property_read_u32(np
, "mrvl,clk-delay-cycles", &clk_delay_cycles
);
238 if (clk_delay_cycles
> 0) {
239 pdata
->clk_delay_sel
= 1;
240 pdata
->clk_delay_cycles
= clk_delay_cycles
;
246 static inline struct sdhci_pxa_platdata
*pxav2_get_mmc_pdata(struct device
*dev
)
252 static int sdhci_pxav2_probe(struct platform_device
*pdev
)
254 struct sdhci_pltfm_host
*pltfm_host
;
255 struct sdhci_pxa_platdata
*pdata
= pdev
->dev
.platform_data
;
256 struct sdhci_pxav2_host
*pxav2_host
;
257 struct device
*dev
= &pdev
->dev
;
258 struct sdhci_host
*host
= NULL
;
259 const struct sdhci_pxa_variant
*variant
;
262 struct clk
*clk
, *clk_core
;
264 host
= sdhci_pltfm_init(pdev
, NULL
, sizeof(*pxav2_host
));
266 return PTR_ERR(host
);
268 pltfm_host
= sdhci_priv(host
);
269 pxav2_host
= sdhci_pltfm_priv(pltfm_host
);
271 clk
= devm_clk_get_optional_enabled(dev
, "io");
273 clk
= devm_clk_get_enabled(dev
, NULL
);
276 dev_err_probe(dev
, ret
, "failed to get io clock\n");
279 pltfm_host
->clk
= clk
;
281 clk_core
= devm_clk_get_optional_enabled(dev
, "core");
282 if (IS_ERR(clk_core
)) {
283 ret
= PTR_ERR(clk_core
);
284 dev_err_probe(dev
, ret
, "failed to enable core clock\n");
288 host
->quirks
= SDHCI_QUIRK_BROKEN_ADMA
289 | SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
290 | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN
;
292 variant
= of_device_get_match_data(dev
);
294 pdata
= pxav2_get_mmc_pdata(dev
);
296 variant
= &pxav2_variant
;
299 if (pdata
->flags
& PXA_FLAG_CARD_PERMANENT
) {
301 host
->quirks
|= SDHCI_QUIRK_BROKEN_CARD_DETECTION
;
302 host
->mmc
->caps
|= MMC_CAP_NONREMOVABLE
;
305 /* If slot design supports 8 bit data, indicate this to MMC. */
306 if (pdata
->flags
& PXA_FLAG_SD_8_BIT_CAPABLE_SLOT
)
307 host
->mmc
->caps
|= MMC_CAP_8_BIT_DATA
;
310 host
->quirks
|= pdata
->quirks
;
311 if (pdata
->host_caps
)
312 host
->mmc
->caps
|= pdata
->host_caps
;
314 host
->mmc
->pm_caps
|= pdata
->pm_caps
;
317 host
->quirks
|= variant
->extra_quirks
;
318 host
->ops
= variant
->ops
;
320 /* Set up optional pinctrl for PXA168 SDIO IRQ fix */
321 pxav2_host
->pinctrl
= devm_pinctrl_get(dev
);
322 if (!IS_ERR(pxav2_host
->pinctrl
)) {
323 pxav2_host
->pins_cmd_gpio
= pinctrl_lookup_state(pxav2_host
->pinctrl
,
325 if (IS_ERR(pxav2_host
->pins_cmd_gpio
))
326 pxav2_host
->pins_cmd_gpio
= NULL
;
327 pxav2_host
->pins_default
= pinctrl_lookup_state(pxav2_host
->pinctrl
,
329 if (IS_ERR(pxav2_host
->pins_default
))
330 pxav2_host
->pins_default
= NULL
;
332 pxav2_host
->pinctrl
= NULL
;
335 ret
= sdhci_add_host(host
);
342 sdhci_pltfm_free(pdev
);
346 static struct platform_driver sdhci_pxav2_driver
= {
348 .name
= "sdhci-pxav2",
349 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
350 .of_match_table
= of_match_ptr(sdhci_pxav2_of_match
),
351 .pm
= &sdhci_pltfm_pmops
,
353 .probe
= sdhci_pxav2_probe
,
354 .remove
= sdhci_pltfm_remove
,
357 module_platform_driver(sdhci_pxav2_driver
);
359 MODULE_DESCRIPTION("SDHCI driver for pxav2");
360 MODULE_AUTHOR("Marvell International Ltd.");
361 MODULE_LICENSE("GPL v2");