2 * Freescale eSDHC i.MX controller driver for the platform bus.
4 * derived from the OF-version.
6 * Copyright (c) 2010 Pengutronix e.K.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License.
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/clk.h>
18 #include <linux/gpio.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/mmc.h>
23 #include <linux/mmc/sdio.h>
24 #include <linux/mmc/slot-gpio.h>
26 #include <linux/of_device.h>
27 #include <linux/of_gpio.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/platform_data/mmc-esdhc-imx.h>
30 #include <linux/pm_runtime.h>
31 #include "sdhci-pltfm.h"
32 #include "sdhci-esdhc.h"
34 #define ESDHC_SYS_CTRL_DTOCV_MASK 0x0f
35 #define ESDHC_CTRL_D3CD 0x08
36 #define ESDHC_BURST_LEN_EN_INCR (1 << 27)
37 /* VENDOR SPEC register */
38 #define ESDHC_VENDOR_SPEC 0xc0
39 #define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
40 #define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
41 #define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
42 #define ESDHC_WTMK_LVL 0x44
43 #define ESDHC_WTMK_DEFAULT_VAL 0x10401040
44 #define ESDHC_WTMK_LVL_RD_WML_MASK 0x000000FF
45 #define ESDHC_WTMK_LVL_RD_WML_SHIFT 0
46 #define ESDHC_WTMK_LVL_WR_WML_MASK 0x00FF0000
47 #define ESDHC_WTMK_LVL_WR_WML_SHIFT 16
48 #define ESDHC_WTMK_LVL_WML_VAL_DEF 64
49 #define ESDHC_WTMK_LVL_WML_VAL_MAX 128
50 #define ESDHC_MIX_CTRL 0x48
51 #define ESDHC_MIX_CTRL_DDREN (1 << 3)
52 #define ESDHC_MIX_CTRL_AC23EN (1 << 7)
53 #define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
54 #define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
55 #define ESDHC_MIX_CTRL_AUTO_TUNE_EN (1 << 24)
56 #define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
57 #define ESDHC_MIX_CTRL_HS400_EN (1 << 26)
58 /* Bits 3 and 6 are not SDHCI standard definitions */
59 #define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
61 #define ESDHC_MIX_CTRL_TUNING_MASK 0x03c00000
63 /* dll control register */
64 #define ESDHC_DLL_CTRL 0x60
65 #define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9
66 #define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
68 /* tune control register */
69 #define ESDHC_TUNE_CTRL_STATUS 0x68
70 #define ESDHC_TUNE_CTRL_STEP 1
71 #define ESDHC_TUNE_CTRL_MIN 0
72 #define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
74 /* strobe dll register */
75 #define ESDHC_STROBE_DLL_CTRL 0x70
76 #define ESDHC_STROBE_DLL_CTRL_ENABLE (1 << 0)
77 #define ESDHC_STROBE_DLL_CTRL_RESET (1 << 1)
78 #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT 3
80 #define ESDHC_STROBE_DLL_STATUS 0x74
81 #define ESDHC_STROBE_DLL_STS_REF_LOCK (1 << 1)
82 #define ESDHC_STROBE_DLL_STS_SLV_LOCK 0x1
84 #define ESDHC_TUNING_CTRL 0xcc
85 #define ESDHC_STD_TUNING_EN (1 << 24)
86 /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
87 #define ESDHC_TUNING_START_TAP_DEFAULT 0x1
88 #define ESDHC_TUNING_START_TAP_MASK 0xff
89 #define ESDHC_TUNING_STEP_MASK 0x00070000
90 #define ESDHC_TUNING_STEP_SHIFT 16
93 #define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
94 #define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
97 * Our interpretation of the SDHCI_HOST_CONTROL register
99 #define ESDHC_CTRL_4BITBUS (0x1 << 1)
100 #define ESDHC_CTRL_8BITBUS (0x2 << 1)
101 #define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
104 * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC:
105 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
106 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
107 * Define this macro DMA error INT for fsl eSDHC
109 #define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
112 * The CMDTYPE of the CMD register (offset 0xE) should be set to
113 * "11" when the STOP CMD12 is issued on imx53 to abort one
114 * open ended multi-blk IO. Otherwise the TC INT wouldn't
116 * In exact block transfer, the controller doesn't complete the
117 * operations automatically as required at the end of the
118 * transfer and remains on hold if the abort command is not sent.
119 * As a result, the TC flag is not asserted and SW received timeout
120 * exception. Bit1 of Vendor Spec register is used to fix it.
122 #define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
124 * The flag tells that the ESDHC controller is an USDHC block that is
125 * integrated on the i.MX6 series.
127 #define ESDHC_FLAG_USDHC BIT(3)
128 /* The IP supports manual tuning process */
129 #define ESDHC_FLAG_MAN_TUNING BIT(4)
130 /* The IP supports standard tuning process */
131 #define ESDHC_FLAG_STD_TUNING BIT(5)
132 /* The IP has SDHCI_CAPABILITIES_1 register */
133 #define ESDHC_FLAG_HAVE_CAP1 BIT(6)
135 * The IP has erratum ERR004536
136 * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow,
137 * when reading data from the card
138 * This flag is also set for i.MX25 and i.MX35 in order to get
139 * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits).
141 #define ESDHC_FLAG_ERR004536 BIT(7)
142 /* The IP supports HS200 mode */
143 #define ESDHC_FLAG_HS200 BIT(8)
144 /* The IP supports HS400 mode */
145 #define ESDHC_FLAG_HS400 BIT(9)
147 /* A clock frequency higher than this rate requires strobe dll control */
148 #define ESDHC_STROBE_DLL_CLK_FREQ 100000000
150 struct esdhc_soc_data {
154 static struct esdhc_soc_data esdhc_imx25_data = {
155 .flags = ESDHC_FLAG_ERR004536,
158 static struct esdhc_soc_data esdhc_imx35_data = {
159 .flags = ESDHC_FLAG_ERR004536,
162 static struct esdhc_soc_data esdhc_imx51_data = {
166 static struct esdhc_soc_data esdhc_imx53_data = {
167 .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
170 static struct esdhc_soc_data usdhc_imx6q_data = {
171 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
174 static struct esdhc_soc_data usdhc_imx6sl_data = {
175 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
176 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536
180 static struct esdhc_soc_data usdhc_imx6sx_data = {
181 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
182 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200,
185 static struct esdhc_soc_data usdhc_imx7d_data = {
186 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
187 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
191 struct pltfm_imx_data {
193 struct pinctrl *pinctrl;
194 struct pinctrl_state *pins_default;
195 struct pinctrl_state *pins_100mhz;
196 struct pinctrl_state *pins_200mhz;
197 const struct esdhc_soc_data *socdata;
198 struct esdhc_platform_data boarddata;
202 unsigned int actual_clock;
204 NO_CMD_PENDING, /* no multiblock command pending */
205 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
206 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
211 static const struct platform_device_id imx_esdhc_devtype[] = {
213 .name = "sdhci-esdhc-imx25",
214 .driver_data = (kernel_ulong_t) &esdhc_imx25_data,
216 .name = "sdhci-esdhc-imx35",
217 .driver_data = (kernel_ulong_t) &esdhc_imx35_data,
219 .name = "sdhci-esdhc-imx51",
220 .driver_data = (kernel_ulong_t) &esdhc_imx51_data,
225 MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
227 static const struct of_device_id imx_esdhc_dt_ids[] = {
228 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
229 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
230 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
231 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
232 { .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
233 { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
234 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
235 { .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
238 MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
240 static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
242 return data->socdata == &esdhc_imx25_data;
245 static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
247 return data->socdata == &esdhc_imx53_data;
250 static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
252 return data->socdata == &usdhc_imx6q_data;
255 static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
257 return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
260 static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
262 void __iomem *base = host->ioaddr + (reg & ~0x3);
263 u32 shift = (reg & 0x3) * 8;
265 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
268 static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
270 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
271 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
272 u32 val = readl(host->ioaddr + reg);
274 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
276 /* save the least 20 bits */
277 val = fsl_prss & 0x000FFFFF;
278 /* move dat[0-3] bits */
279 val |= (fsl_prss & 0x0F000000) >> 4;
280 /* move cmd line bit */
281 val |= (fsl_prss & 0x00800000) << 1;
284 if (unlikely(reg == SDHCI_CAPABILITIES)) {
285 /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
286 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
289 /* In FSL esdhc IC module, only bit20 is used to indicate the
290 * ADMA2 capability of esdhc, but this bit is messed up on
291 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
292 * don't actually support ADMA2). So set the BROKEN_ADMA
293 * quirk on MX25/35 platforms.
296 if (val & SDHCI_CAN_DO_ADMA1) {
297 val &= ~SDHCI_CAN_DO_ADMA1;
298 val |= SDHCI_CAN_DO_ADMA2;
302 if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
303 if (esdhc_is_usdhc(imx_data)) {
304 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
305 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
307 /* imx6q/dl does not have cap_1 register, fake one */
308 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
309 | SDHCI_SUPPORT_SDR50
310 | SDHCI_USE_SDR50_TUNING
311 | (SDHCI_TUNING_MODE_3 << SDHCI_RETUNING_MODE_SHIFT);
313 if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
314 val |= SDHCI_SUPPORT_HS400;
318 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
320 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
321 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
322 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
325 if (unlikely(reg == SDHCI_INT_STATUS)) {
326 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
327 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
328 val |= SDHCI_INT_ADMA_ERROR;
332 * mask off the interrupt we get in response to the manually
335 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
336 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
337 val &= ~SDHCI_INT_RESPONSE;
338 writel(SDHCI_INT_RESPONSE, host->ioaddr +
340 imx_data->multiblock_status = NO_CMD_PENDING;
347 static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
349 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
350 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
353 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
354 reg == SDHCI_INT_STATUS)) {
355 if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
357 * Clear and then set D3CD bit to avoid missing the
358 * card interrupt. This is an eSDHC controller problem
359 * so we need to apply the following workaround: clear
360 * and set D3CD bit will make eSDHC re-sample the card
361 * interrupt. In case a card interrupt was lost,
362 * re-sample it by the following steps.
364 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
365 data &= ~ESDHC_CTRL_D3CD;
366 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
367 data |= ESDHC_CTRL_D3CD;
368 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
371 if (val & SDHCI_INT_ADMA_ERROR) {
372 val &= ~SDHCI_INT_ADMA_ERROR;
373 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
377 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
378 && (reg == SDHCI_INT_STATUS)
379 && (val & SDHCI_INT_DATA_END))) {
381 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
382 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
383 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
385 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
387 /* send a manual CMD12 with RESPTYP=none */
388 data = MMC_STOP_TRANSMISSION << 24 |
389 SDHCI_CMD_ABORTCMD << 16;
390 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
391 imx_data->multiblock_status = WAIT_FOR_INT;
395 writel(val, host->ioaddr + reg);
398 static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
400 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
401 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
405 if (unlikely(reg == SDHCI_HOST_VERSION)) {
407 if (esdhc_is_usdhc(imx_data)) {
409 * The usdhc register returns a wrong host version.
412 return SDHCI_SPEC_300;
416 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
417 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
418 if (val & ESDHC_VENDOR_SPEC_VSELECT)
419 ret |= SDHCI_CTRL_VDD_180;
421 if (esdhc_is_usdhc(imx_data)) {
422 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
423 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
424 else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
425 /* the std tuning bits is in ACMD12_ERR for imx6sl */
426 val = readl(host->ioaddr + SDHCI_ACMD12_ERR);
429 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
430 ret |= SDHCI_CTRL_EXEC_TUNING;
431 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
432 ret |= SDHCI_CTRL_TUNED_CLK;
434 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
439 if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
440 if (esdhc_is_usdhc(imx_data)) {
441 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
442 ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
444 if (m & ESDHC_MIX_CTRL_AC23EN) {
445 ret &= ~ESDHC_MIX_CTRL_AC23EN;
446 ret |= SDHCI_TRNS_AUTO_CMD23;
449 ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
455 return readw(host->ioaddr + reg);
458 static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
460 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
461 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
465 case SDHCI_CLOCK_CONTROL:
466 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
467 if (val & SDHCI_CLOCK_CARD_EN)
468 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
470 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
471 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
473 case SDHCI_HOST_CONTROL2:
474 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
475 if (val & SDHCI_CTRL_VDD_180)
476 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
478 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
479 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
480 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
481 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
482 if (val & SDHCI_CTRL_TUNED_CLK) {
483 new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
484 new_val |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
486 new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
487 new_val &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
489 writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
490 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
491 u32 v = readl(host->ioaddr + SDHCI_ACMD12_ERR);
492 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
493 if (val & SDHCI_CTRL_TUNED_CLK) {
494 v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
496 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
497 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
498 m &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
501 if (val & SDHCI_CTRL_EXEC_TUNING) {
502 v |= ESDHC_MIX_CTRL_EXE_TUNE;
503 m |= ESDHC_MIX_CTRL_FBCLK_SEL;
504 m |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
506 v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
509 writel(v, host->ioaddr + SDHCI_ACMD12_ERR);
510 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
513 case SDHCI_TRANSFER_MODE:
514 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
515 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
516 && (host->cmd->data->blocks > 1)
517 && (host->cmd->data->flags & MMC_DATA_READ)) {
519 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
520 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
521 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
524 if (esdhc_is_usdhc(imx_data)) {
526 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
528 if (val & SDHCI_TRNS_AUTO_CMD23) {
529 val &= ~SDHCI_TRNS_AUTO_CMD23;
530 val |= ESDHC_MIX_CTRL_AC23EN;
532 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
533 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
535 /* Set watermark levels for PIO access to maximum value
536 * (128 words) to accommodate full 512 bytes buffer.
537 * For DMA access restore the levels to default value.
539 m = readl(host->ioaddr + ESDHC_WTMK_LVL);
540 if (val & SDHCI_TRNS_DMA)
541 wml = ESDHC_WTMK_LVL_WML_VAL_DEF;
543 wml = ESDHC_WTMK_LVL_WML_VAL_MAX;
544 m &= ~(ESDHC_WTMK_LVL_RD_WML_MASK |
545 ESDHC_WTMK_LVL_WR_WML_MASK);
546 m |= (wml << ESDHC_WTMK_LVL_RD_WML_SHIFT) |
547 (wml << ESDHC_WTMK_LVL_WR_WML_SHIFT);
548 writel(m, host->ioaddr + ESDHC_WTMK_LVL);
551 * Postpone this write, we must do it together with a
552 * command write that is down below.
554 imx_data->scratchpad = val;
558 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
559 val |= SDHCI_CMD_ABORTCMD;
561 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
562 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
563 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
565 if (esdhc_is_usdhc(imx_data))
567 host->ioaddr + SDHCI_TRANSFER_MODE);
569 writel(val << 16 | imx_data->scratchpad,
570 host->ioaddr + SDHCI_TRANSFER_MODE);
572 case SDHCI_BLOCK_SIZE:
573 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
576 esdhc_clrset_le(host, 0xffff, val, reg);
579 static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
585 case SDHCI_HOST_CONTROL:
586 val = readl(host->ioaddr + reg);
588 ret = val & SDHCI_CTRL_LED;
589 ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
590 ret |= (val & ESDHC_CTRL_4BITBUS);
591 ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
595 return readb(host->ioaddr + reg);
598 static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
600 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
601 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
606 case SDHCI_POWER_CONTROL:
608 * FSL put some DMA bits here
609 * If your board has a regulator, code should be here
612 case SDHCI_HOST_CONTROL:
613 /* FSL messed up here, so we need to manually compose it. */
614 new_val = val & SDHCI_CTRL_LED;
615 /* ensure the endianness */
616 new_val |= ESDHC_HOST_CONTROL_LE;
617 /* bits 8&9 are reserved on mx25 */
618 if (!is_imx25_esdhc(imx_data)) {
619 /* DMA mode bits are shifted */
620 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
624 * Do not touch buswidth bits here. This is done in
625 * esdhc_pltfm_bus_width.
626 * Do not touch the D3CD bit either which is used for the
627 * SDIO interrupt erratum workaround.
629 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
631 esdhc_clrset_le(host, mask, new_val, reg);
633 case SDHCI_SOFTWARE_RESET:
634 if (val & SDHCI_RESET_DATA)
635 new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
638 esdhc_clrset_le(host, 0xff, val, reg);
640 if (reg == SDHCI_SOFTWARE_RESET) {
641 if (val & SDHCI_RESET_ALL) {
643 * The esdhc has a design violation to SDHC spec which
644 * tells that software reset should not affect card
645 * detection circuit. But esdhc clears its SYSCTL
646 * register bits [0..2] during the software reset. This
647 * will stop those clocks that card detection circuit
648 * relies on. To work around it, we turn the clocks on
649 * back to keep card detection circuit functional.
651 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
653 * The reset on usdhc fails to clear MIX_CTRL register.
654 * Do it manually here.
656 if (esdhc_is_usdhc(imx_data)) {
658 * the tuning bits should be kept during reset
660 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
661 writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
662 host->ioaddr + ESDHC_MIX_CTRL);
663 imx_data->is_ddr = 0;
665 } else if (val & SDHCI_RESET_DATA) {
667 * The eSDHC DAT line software reset clears at least the
668 * data transfer width on i.MX25, so make sure that the
669 * Host Control register is unaffected.
671 esdhc_clrset_le(host, 0xff, new_val,
677 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
679 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
681 return pltfm_host->clock;
684 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
686 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
688 return pltfm_host->clock / 256 / 16;
691 static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
694 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
695 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
696 unsigned int host_clock = pltfm_host->clock;
697 int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
703 host->mmc->actual_clock = 0;
705 if (esdhc_is_usdhc(imx_data)) {
706 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
707 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
708 host->ioaddr + ESDHC_VENDOR_SPEC);
713 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
714 if (is_imx53_esdhc(imx_data)) {
716 * According to the i.MX53 reference manual, if DLLCTRL[10] can
717 * be set, then the controller is eSDHCv3, else it is eSDHCv2.
719 val = readl(host->ioaddr + ESDHC_DLL_CTRL);
720 writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL);
721 temp = readl(host->ioaddr + ESDHC_DLL_CTRL);
722 writel(val, host->ioaddr + ESDHC_DLL_CTRL);
727 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
728 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
730 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
732 while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
736 while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
739 host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
740 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
741 clock, host->mmc->actual_clock);
746 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
747 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
748 | (div << ESDHC_DIVIDER_SHIFT)
749 | (pre_div << ESDHC_PREDIV_SHIFT));
750 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
752 if (esdhc_is_usdhc(imx_data)) {
753 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
754 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
755 host->ioaddr + ESDHC_VENDOR_SPEC);
761 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
763 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
764 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
765 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
767 switch (boarddata->wp_type) {
769 return mmc_gpio_get_ro(host->mmc);
770 case ESDHC_WP_CONTROLLER:
771 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
772 SDHCI_WRITE_PROTECT);
780 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
785 case MMC_BUS_WIDTH_8:
786 ctrl = ESDHC_CTRL_8BITBUS;
788 case MMC_BUS_WIDTH_4:
789 ctrl = ESDHC_CTRL_4BITBUS;
796 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
800 static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
804 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
807 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
808 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
809 ESDHC_MIX_CTRL_FBCLK_SEL;
810 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
811 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
812 dev_dbg(mmc_dev(host->mmc),
813 "tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
814 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
817 static void esdhc_post_tuning(struct sdhci_host *host)
821 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
822 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
823 reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
824 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
827 static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
829 int min, max, avg, ret;
831 /* find the mininum delay first which can pass tuning */
832 min = ESDHC_TUNE_CTRL_MIN;
833 while (min < ESDHC_TUNE_CTRL_MAX) {
834 esdhc_prepare_tuning(host, min);
835 if (!mmc_send_tuning(host->mmc, opcode, NULL))
837 min += ESDHC_TUNE_CTRL_STEP;
840 /* find the maxinum delay which can not pass tuning */
841 max = min + ESDHC_TUNE_CTRL_STEP;
842 while (max < ESDHC_TUNE_CTRL_MAX) {
843 esdhc_prepare_tuning(host, max);
844 if (mmc_send_tuning(host->mmc, opcode, NULL)) {
845 max -= ESDHC_TUNE_CTRL_STEP;
848 max += ESDHC_TUNE_CTRL_STEP;
851 /* use average delay to get the best timing */
852 avg = (min + max) / 2;
853 esdhc_prepare_tuning(host, avg);
854 ret = mmc_send_tuning(host->mmc, opcode, NULL);
855 esdhc_post_tuning(host);
857 dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
858 ret ? "failed" : "passed", avg, ret);
863 static int esdhc_change_pinstate(struct sdhci_host *host,
866 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
867 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
868 struct pinctrl_state *pinctrl;
870 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
872 if (IS_ERR(imx_data->pinctrl) ||
873 IS_ERR(imx_data->pins_default) ||
874 IS_ERR(imx_data->pins_100mhz) ||
875 IS_ERR(imx_data->pins_200mhz))
879 case MMC_TIMING_UHS_SDR50:
880 case MMC_TIMING_UHS_DDR50:
881 pinctrl = imx_data->pins_100mhz;
883 case MMC_TIMING_UHS_SDR104:
884 case MMC_TIMING_MMC_HS200:
885 case MMC_TIMING_MMC_HS400:
886 pinctrl = imx_data->pins_200mhz;
889 /* back to default state for other legacy timing */
890 pinctrl = imx_data->pins_default;
893 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
897 * For HS400 eMMC, there is a data_strobe line. This signal is generated
898 * by the device and used for data output and CRC status response output
899 * in HS400 mode. The frequency of this signal follows the frequency of
900 * CLK generated by host. The host receives the data which is aligned to the
901 * edge of data_strobe line. Due to the time delay between CLK line and
902 * data_strobe line, if the delay time is larger than one clock cycle,
903 * then CLK and data_strobe line will be misaligned, read error shows up.
904 * So when the CLK is higher than 100MHz, each clock cycle is short enough,
905 * host should configure the delay target.
907 static void esdhc_set_strobe_dll(struct sdhci_host *host)
911 if (host->mmc->actual_clock > ESDHC_STROBE_DLL_CLK_FREQ) {
912 /* disable clock before enabling strobe dll */
913 writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
914 ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
915 host->ioaddr + ESDHC_VENDOR_SPEC);
917 /* force a reset on strobe dll */
918 writel(ESDHC_STROBE_DLL_CTRL_RESET,
919 host->ioaddr + ESDHC_STROBE_DLL_CTRL);
921 * enable strobe dll ctrl and adjust the delay target
922 * for the uSDHC loopback read clock
924 v = ESDHC_STROBE_DLL_CTRL_ENABLE |
925 (7 << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
926 writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
927 /* wait 1us to make sure strobe dll status register stable */
929 v = readl(host->ioaddr + ESDHC_STROBE_DLL_STATUS);
930 if (!(v & ESDHC_STROBE_DLL_STS_REF_LOCK))
931 dev_warn(mmc_dev(host->mmc),
932 "warning! HS400 strobe DLL status REF not lock!\n");
933 if (!(v & ESDHC_STROBE_DLL_STS_SLV_LOCK))
934 dev_warn(mmc_dev(host->mmc),
935 "warning! HS400 strobe DLL status SLV not lock!\n");
939 static void esdhc_reset_tuning(struct sdhci_host *host)
941 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
942 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
945 /* Reset the tuning circuit */
946 if (esdhc_is_usdhc(imx_data)) {
947 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
948 ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
949 ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
950 ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
951 writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
952 writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
953 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
954 ctrl = readl(host->ioaddr + SDHCI_ACMD12_ERR);
955 ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
956 writel(ctrl, host->ioaddr + SDHCI_ACMD12_ERR);
961 static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
964 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
965 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
966 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
968 /* disable ddr mode and disable HS400 mode */
969 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
970 m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
971 imx_data->is_ddr = 0;
974 case MMC_TIMING_UHS_SDR12:
975 case MMC_TIMING_UHS_SDR25:
976 case MMC_TIMING_UHS_SDR50:
977 case MMC_TIMING_UHS_SDR104:
978 case MMC_TIMING_MMC_HS200:
979 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
981 case MMC_TIMING_UHS_DDR50:
982 case MMC_TIMING_MMC_DDR52:
983 m |= ESDHC_MIX_CTRL_DDREN;
984 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
985 imx_data->is_ddr = 1;
986 if (boarddata->delay_line) {
988 v = boarddata->delay_line <<
989 ESDHC_DLL_OVERRIDE_VAL_SHIFT |
990 (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
991 if (is_imx53_esdhc(imx_data))
993 writel(v, host->ioaddr + ESDHC_DLL_CTRL);
996 case MMC_TIMING_MMC_HS400:
997 m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
998 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
999 imx_data->is_ddr = 1;
1000 /* update clock after enable DDR for strobe DLL lock */
1001 host->ops->set_clock(host, host->clock);
1002 esdhc_set_strobe_dll(host);
1004 case MMC_TIMING_LEGACY:
1006 esdhc_reset_tuning(host);
1010 esdhc_change_pinstate(host, timing);
1013 static void esdhc_reset(struct sdhci_host *host, u8 mask)
1015 sdhci_reset(host, mask);
1017 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1018 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1021 static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host)
1023 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1024 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1026 /* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
1027 return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
1030 static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
1032 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1033 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1035 /* use maximum timeout counter */
1036 esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
1037 esdhc_is_usdhc(imx_data) ? 0xF : 0xE,
1038 SDHCI_TIMEOUT_CONTROL);
1041 static struct sdhci_ops sdhci_esdhc_ops = {
1042 .read_l = esdhc_readl_le,
1043 .read_w = esdhc_readw_le,
1044 .read_b = esdhc_readb_le,
1045 .write_l = esdhc_writel_le,
1046 .write_w = esdhc_writew_le,
1047 .write_b = esdhc_writeb_le,
1048 .set_clock = esdhc_pltfm_set_clock,
1049 .get_max_clock = esdhc_pltfm_get_max_clock,
1050 .get_min_clock = esdhc_pltfm_get_min_clock,
1051 .get_max_timeout_count = esdhc_get_max_timeout_count,
1052 .get_ro = esdhc_pltfm_get_ro,
1053 .set_timeout = esdhc_set_timeout,
1054 .set_bus_width = esdhc_pltfm_set_bus_width,
1055 .set_uhs_signaling = esdhc_set_uhs_signaling,
1056 .reset = esdhc_reset,
1059 static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
1060 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
1061 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
1062 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
1063 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
1064 .ops = &sdhci_esdhc_ops,
1067 static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
1069 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1070 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1073 if (esdhc_is_usdhc(imx_data)) {
1075 * The imx6q ROM code will change the default watermark
1076 * level setting to something insane. Change it back here.
1078 writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
1081 * ROM code will change the bit burst_length_enable setting
1082 * to zero if this usdhc is chosen to boot system. Change
1083 * it back here, otherwise it will impact the performance a
1084 * lot. This bit is used to enable/disable the burst length
1085 * for the external AHB2AXI bridge. It's useful especially
1086 * for INCR transfer because without burst length indicator,
1087 * the AHB2AXI bridge does not know the burst length in
1088 * advance. And without burst length indicator, AHB INCR
1089 * transfer can only be converted to singles on the AXI side.
1091 writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
1092 | ESDHC_BURST_LEN_EN_INCR,
1093 host->ioaddr + SDHCI_HOST_CONTROL);
1095 * erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
1096 * TO1.1, it's harmless for MX6SL
1098 writel(readl(host->ioaddr + 0x6c) | BIT(7),
1099 host->ioaddr + 0x6c);
1101 /* disable DLL_CTRL delay line settings */
1102 writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
1104 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1105 tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1106 tmp |= ESDHC_STD_TUNING_EN |
1107 ESDHC_TUNING_START_TAP_DEFAULT;
1108 if (imx_data->boarddata.tuning_start_tap) {
1109 tmp &= ~ESDHC_TUNING_START_TAP_MASK;
1110 tmp |= imx_data->boarddata.tuning_start_tap;
1113 if (imx_data->boarddata.tuning_step) {
1114 tmp &= ~ESDHC_TUNING_STEP_MASK;
1115 tmp |= imx_data->boarddata.tuning_step
1116 << ESDHC_TUNING_STEP_SHIFT;
1118 writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1125 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
1126 struct sdhci_host *host,
1127 struct pltfm_imx_data *imx_data)
1129 struct device_node *np = pdev->dev.of_node;
1130 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1133 if (of_get_property(np, "fsl,wp-controller", NULL))
1134 boarddata->wp_type = ESDHC_WP_CONTROLLER;
1136 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1137 if (gpio_is_valid(boarddata->wp_gpio))
1138 boarddata->wp_type = ESDHC_WP_GPIO;
1140 of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
1141 of_property_read_u32(np, "fsl,tuning-start-tap",
1142 &boarddata->tuning_start_tap);
1144 if (of_find_property(np, "no-1-8-v", NULL))
1145 boarddata->support_vsel = false;
1147 boarddata->support_vsel = true;
1149 if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
1150 boarddata->delay_line = 0;
1152 mmc_of_parse_voltage(np, &host->ocr_mask);
1154 /* sdr50 and sdr104 need work on 1.8v signal voltage */
1155 if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data) &&
1156 !IS_ERR(imx_data->pins_default)) {
1157 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1158 ESDHC_PINCTRL_STATE_100MHZ);
1159 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1160 ESDHC_PINCTRL_STATE_200MHZ);
1161 if (IS_ERR(imx_data->pins_100mhz) ||
1162 IS_ERR(imx_data->pins_200mhz)) {
1163 dev_warn(mmc_dev(host->mmc),
1164 "could not get ultra high speed state, work on normal mode\n");
1166 * fall back to not supporting uhs by specifying no
1169 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1172 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1175 /* call to generic mmc_of_parse to support additional capabilities */
1176 ret = mmc_of_parse(host->mmc);
1180 if (mmc_gpio_get_cd(host->mmc) >= 0)
1181 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1187 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
1188 struct sdhci_host *host,
1189 struct pltfm_imx_data *imx_data)
1195 static int sdhci_esdhc_imx_probe_nondt(struct platform_device *pdev,
1196 struct sdhci_host *host,
1197 struct pltfm_imx_data *imx_data)
1199 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1202 if (!host->mmc->parent->platform_data) {
1203 dev_err(mmc_dev(host->mmc), "no board data!\n");
1207 imx_data->boarddata = *((struct esdhc_platform_data *)
1208 host->mmc->parent->platform_data);
1210 if (boarddata->wp_type == ESDHC_WP_GPIO) {
1211 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
1213 dev_err(mmc_dev(host->mmc),
1214 "failed to request write-protect gpio!\n");
1217 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
1221 switch (boarddata->cd_type) {
1223 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
1225 dev_err(mmc_dev(host->mmc),
1226 "failed to request card-detect gpio!\n");
1231 case ESDHC_CD_CONTROLLER:
1232 /* we have a working card_detect back */
1233 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1236 case ESDHC_CD_PERMANENT:
1237 host->mmc->caps |= MMC_CAP_NONREMOVABLE;
1244 switch (boarddata->max_bus_width) {
1246 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
1249 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
1253 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
1260 static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
1262 const struct of_device_id *of_id =
1263 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
1264 struct sdhci_pltfm_host *pltfm_host;
1265 struct sdhci_host *host;
1267 struct pltfm_imx_data *imx_data;
1269 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
1272 return PTR_ERR(host);
1274 pltfm_host = sdhci_priv(host);
1276 imx_data = sdhci_pltfm_priv(pltfm_host);
1278 imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
1279 pdev->id_entry->driver_data;
1281 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1282 if (IS_ERR(imx_data->clk_ipg)) {
1283 err = PTR_ERR(imx_data->clk_ipg);
1287 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1288 if (IS_ERR(imx_data->clk_ahb)) {
1289 err = PTR_ERR(imx_data->clk_ahb);
1293 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
1294 if (IS_ERR(imx_data->clk_per)) {
1295 err = PTR_ERR(imx_data->clk_per);
1299 pltfm_host->clk = imx_data->clk_per;
1300 pltfm_host->clock = clk_get_rate(pltfm_host->clk);
1301 err = clk_prepare_enable(imx_data->clk_per);
1304 err = clk_prepare_enable(imx_data->clk_ipg);
1306 goto disable_per_clk;
1307 err = clk_prepare_enable(imx_data->clk_ahb);
1309 goto disable_ipg_clk;
1311 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
1312 if (IS_ERR(imx_data->pinctrl)) {
1313 err = PTR_ERR(imx_data->pinctrl);
1314 goto disable_ahb_clk;
1317 imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
1318 PINCTRL_STATE_DEFAULT);
1319 if (IS_ERR(imx_data->pins_default))
1320 dev_warn(mmc_dev(host->mmc), "could not get default state\n");
1322 if (esdhc_is_usdhc(imx_data)) {
1323 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
1324 host->mmc->caps |= MMC_CAP_1_8V_DDR;
1325 if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
1326 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
1328 /* clear tuning bits in case ROM has set it already */
1329 writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
1330 writel(0x0, host->ioaddr + SDHCI_ACMD12_ERR);
1331 writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1334 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
1335 sdhci_esdhc_ops.platform_execute_tuning =
1336 esdhc_executing_tuning;
1338 if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
1339 host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
1341 if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
1342 host->quirks2 |= SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400;
1345 err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
1347 err = sdhci_esdhc_imx_probe_nondt(pdev, host, imx_data);
1349 goto disable_ahb_clk;
1351 sdhci_esdhc_imx_hwinit(host);
1353 err = sdhci_add_host(host);
1355 goto disable_ahb_clk;
1357 pm_runtime_set_active(&pdev->dev);
1358 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1359 pm_runtime_use_autosuspend(&pdev->dev);
1360 pm_suspend_ignore_children(&pdev->dev, 1);
1361 pm_runtime_enable(&pdev->dev);
1366 clk_disable_unprepare(imx_data->clk_ahb);
1368 clk_disable_unprepare(imx_data->clk_ipg);
1370 clk_disable_unprepare(imx_data->clk_per);
1372 sdhci_pltfm_free(pdev);
1376 static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
1378 struct sdhci_host *host = platform_get_drvdata(pdev);
1379 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1380 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1381 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1383 pm_runtime_get_sync(&pdev->dev);
1384 pm_runtime_disable(&pdev->dev);
1385 pm_runtime_put_noidle(&pdev->dev);
1387 sdhci_remove_host(host, dead);
1389 clk_disable_unprepare(imx_data->clk_per);
1390 clk_disable_unprepare(imx_data->clk_ipg);
1391 clk_disable_unprepare(imx_data->clk_ahb);
1393 sdhci_pltfm_free(pdev);
1398 #ifdef CONFIG_PM_SLEEP
1399 static int sdhci_esdhc_suspend(struct device *dev)
1401 struct sdhci_host *host = dev_get_drvdata(dev);
1403 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1404 mmc_retune_needed(host->mmc);
1406 return sdhci_suspend_host(host);
1409 static int sdhci_esdhc_resume(struct device *dev)
1411 struct sdhci_host *host = dev_get_drvdata(dev);
1413 /* re-initialize hw state in case it's lost in low power mode */
1414 sdhci_esdhc_imx_hwinit(host);
1416 return sdhci_resume_host(host);
1421 static int sdhci_esdhc_runtime_suspend(struct device *dev)
1423 struct sdhci_host *host = dev_get_drvdata(dev);
1424 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1425 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1428 ret = sdhci_runtime_suspend_host(host);
1432 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1433 mmc_retune_needed(host->mmc);
1435 if (!sdhci_sdio_irq_enabled(host)) {
1436 imx_data->actual_clock = host->mmc->actual_clock;
1437 esdhc_pltfm_set_clock(host, 0);
1438 clk_disable_unprepare(imx_data->clk_per);
1439 clk_disable_unprepare(imx_data->clk_ipg);
1441 clk_disable_unprepare(imx_data->clk_ahb);
1446 static int sdhci_esdhc_runtime_resume(struct device *dev)
1448 struct sdhci_host *host = dev_get_drvdata(dev);
1449 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1450 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1453 err = clk_prepare_enable(imx_data->clk_ahb);
1457 if (!sdhci_sdio_irq_enabled(host)) {
1458 err = clk_prepare_enable(imx_data->clk_per);
1460 goto disable_ahb_clk;
1461 err = clk_prepare_enable(imx_data->clk_ipg);
1463 goto disable_per_clk;
1464 esdhc_pltfm_set_clock(host, imx_data->actual_clock);
1467 err = sdhci_runtime_resume_host(host);
1469 goto disable_ipg_clk;
1474 if (!sdhci_sdio_irq_enabled(host))
1475 clk_disable_unprepare(imx_data->clk_ipg);
1477 if (!sdhci_sdio_irq_enabled(host))
1478 clk_disable_unprepare(imx_data->clk_per);
1480 clk_disable_unprepare(imx_data->clk_ahb);
1485 static const struct dev_pm_ops sdhci_esdhc_pmops = {
1486 SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
1487 SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
1488 sdhci_esdhc_runtime_resume, NULL)
1491 static struct platform_driver sdhci_esdhc_imx_driver = {
1493 .name = "sdhci-esdhc-imx",
1494 .of_match_table = imx_esdhc_dt_ids,
1495 .pm = &sdhci_esdhc_pmops,
1497 .id_table = imx_esdhc_devtype,
1498 .probe = sdhci_esdhc_imx_probe,
1499 .remove = sdhci_esdhc_imx_remove,
1502 module_platform_driver(sdhci_esdhc_imx_driver);
1504 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1506 MODULE_LICENSE("GPL v2");