2 * Freescale eSDHC i.MX controller driver for the platform bus.
4 * derived from the OF-version.
6 * Copyright (c) 2010 Pengutronix e.K.
7 * Author: Wolfram Sang <w.sang@pengutronix.de>
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_CTRL_D3CD 0x08
35 /* VENDOR SPEC register */
36 #define ESDHC_VENDOR_SPEC 0xc0
37 #define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
38 #define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
39 #define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
40 #define ESDHC_WTMK_LVL 0x44
41 #define ESDHC_MIX_CTRL 0x48
42 #define ESDHC_MIX_CTRL_DDREN (1 << 3)
43 #define ESDHC_MIX_CTRL_AC23EN (1 << 7)
44 #define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
45 #define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
46 #define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
47 /* Bits 3 and 6 are not SDHCI standard definitions */
48 #define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
50 #define ESDHC_MIX_CTRL_TUNING_MASK 0x03c00000
52 /* dll control register */
53 #define ESDHC_DLL_CTRL 0x60
54 #define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9
55 #define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
57 /* dll control register */
58 #define ESDHC_DLL_CTRL 0x60
59 #define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9
60 #define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
62 /* tune control register */
63 #define ESDHC_TUNE_CTRL_STATUS 0x68
64 #define ESDHC_TUNE_CTRL_STEP 1
65 #define ESDHC_TUNE_CTRL_MIN 0
66 #define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
68 #define ESDHC_TUNING_CTRL 0xcc
69 #define ESDHC_STD_TUNING_EN (1 << 24)
70 /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
71 #define ESDHC_TUNING_START_TAP 0x1
73 #define ESDHC_TUNING_BLOCK_PATTERN_LEN 64
76 #define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
77 #define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
80 * Our interpretation of the SDHCI_HOST_CONTROL register
82 #define ESDHC_CTRL_4BITBUS (0x1 << 1)
83 #define ESDHC_CTRL_8BITBUS (0x2 << 1)
84 #define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
87 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
88 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
89 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
90 * Define this macro DMA error INT for fsl eSDHC
92 #define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
95 * The CMDTYPE of the CMD register (offset 0xE) should be set to
96 * "11" when the STOP CMD12 is issued on imx53 to abort one
97 * open ended multi-blk IO. Otherwise the TC INT wouldn't
99 * In exact block transfer, the controller doesn't complete the
100 * operations automatically as required at the end of the
101 * transfer and remains on hold if the abort command is not sent.
102 * As a result, the TC flag is not asserted and SW received timeout
103 * exeception. Bit1 of Vendor Spec registor is used to fix it.
105 #define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
107 * The flag enables the workaround for ESDHC errata ENGcm07207 which
108 * affects i.MX25 and i.MX35.
110 #define ESDHC_FLAG_ENGCM07207 BIT(2)
112 * The flag tells that the ESDHC controller is an USDHC block that is
113 * integrated on the i.MX6 series.
115 #define ESDHC_FLAG_USDHC BIT(3)
116 /* The IP supports manual tuning process */
117 #define ESDHC_FLAG_MAN_TUNING BIT(4)
118 /* The IP supports standard tuning process */
119 #define ESDHC_FLAG_STD_TUNING BIT(5)
120 /* The IP has SDHCI_CAPABILITIES_1 register */
121 #define ESDHC_FLAG_HAVE_CAP1 BIT(6)
123 struct esdhc_soc_data {
127 static struct esdhc_soc_data esdhc_imx25_data = {
128 .flags = ESDHC_FLAG_ENGCM07207,
131 static struct esdhc_soc_data esdhc_imx35_data = {
132 .flags = ESDHC_FLAG_ENGCM07207,
135 static struct esdhc_soc_data esdhc_imx51_data = {
139 static struct esdhc_soc_data esdhc_imx53_data = {
140 .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
143 static struct esdhc_soc_data usdhc_imx6q_data = {
144 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
147 static struct esdhc_soc_data usdhc_imx6sl_data = {
148 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
149 | ESDHC_FLAG_HAVE_CAP1,
152 struct pltfm_imx_data {
154 struct pinctrl *pinctrl;
155 struct pinctrl_state *pins_default;
156 struct pinctrl_state *pins_100mhz;
157 struct pinctrl_state *pins_200mhz;
158 const struct esdhc_soc_data *socdata;
159 struct esdhc_platform_data boarddata;
164 NO_CMD_PENDING, /* no multiblock command pending*/
165 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
166 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
171 static struct platform_device_id imx_esdhc_devtype[] = {
173 .name = "sdhci-esdhc-imx25",
174 .driver_data = (kernel_ulong_t) &esdhc_imx25_data,
176 .name = "sdhci-esdhc-imx35",
177 .driver_data = (kernel_ulong_t) &esdhc_imx35_data,
179 .name = "sdhci-esdhc-imx51",
180 .driver_data = (kernel_ulong_t) &esdhc_imx51_data,
185 MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
187 static const struct of_device_id imx_esdhc_dt_ids[] = {
188 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
189 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
190 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
191 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
192 { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
193 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
196 MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
198 static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
200 return data->socdata == &esdhc_imx25_data;
203 static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
205 return data->socdata == &esdhc_imx53_data;
208 static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
210 return data->socdata == &usdhc_imx6q_data;
213 static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
215 return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
218 static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
220 void __iomem *base = host->ioaddr + (reg & ~0x3);
221 u32 shift = (reg & 0x3) * 8;
223 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
226 static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
228 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
229 struct pltfm_imx_data *imx_data = pltfm_host->priv;
230 u32 val = readl(host->ioaddr + reg);
232 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
234 /* save the least 20 bits */
235 val = fsl_prss & 0x000FFFFF;
236 /* move dat[0-3] bits */
237 val |= (fsl_prss & 0x0F000000) >> 4;
238 /* move cmd line bit */
239 val |= (fsl_prss & 0x00800000) << 1;
242 if (unlikely(reg == SDHCI_CAPABILITIES)) {
243 /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
244 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
247 /* In FSL esdhc IC module, only bit20 is used to indicate the
248 * ADMA2 capability of esdhc, but this bit is messed up on
249 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
250 * don't actually support ADMA2). So set the BROKEN_ADMA
251 * uirk on MX25/35 platforms.
254 if (val & SDHCI_CAN_DO_ADMA1) {
255 val &= ~SDHCI_CAN_DO_ADMA1;
256 val |= SDHCI_CAN_DO_ADMA2;
260 if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
261 if (esdhc_is_usdhc(imx_data)) {
262 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
263 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
265 /* imx6q/dl does not have cap_1 register, fake one */
266 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
267 | SDHCI_SUPPORT_SDR50
268 | SDHCI_USE_SDR50_TUNING;
272 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
274 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
275 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
276 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
279 if (unlikely(reg == SDHCI_INT_STATUS)) {
280 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
281 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
282 val |= SDHCI_INT_ADMA_ERROR;
286 * mask off the interrupt we get in response to the manually
289 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
290 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
291 val &= ~SDHCI_INT_RESPONSE;
292 writel(SDHCI_INT_RESPONSE, host->ioaddr +
294 imx_data->multiblock_status = NO_CMD_PENDING;
301 static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
303 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
304 struct pltfm_imx_data *imx_data = pltfm_host->priv;
307 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
308 if (val & SDHCI_INT_CARD_INT) {
310 * Clear and then set D3CD bit to avoid missing the
311 * card interrupt. This is a eSDHC controller problem
312 * so we need to apply the following workaround: clear
313 * and set D3CD bit will make eSDHC re-sample the card
314 * interrupt. In case a card interrupt was lost,
315 * re-sample it by the following steps.
317 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
318 data &= ~ESDHC_CTRL_D3CD;
319 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
320 data |= ESDHC_CTRL_D3CD;
321 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
325 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
326 && (reg == SDHCI_INT_STATUS)
327 && (val & SDHCI_INT_DATA_END))) {
329 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
330 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
331 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
333 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
335 /* send a manual CMD12 with RESPTYP=none */
336 data = MMC_STOP_TRANSMISSION << 24 |
337 SDHCI_CMD_ABORTCMD << 16;
338 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
339 imx_data->multiblock_status = WAIT_FOR_INT;
343 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
344 if (val & SDHCI_INT_ADMA_ERROR) {
345 val &= ~SDHCI_INT_ADMA_ERROR;
346 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
350 writel(val, host->ioaddr + reg);
353 static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
355 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
356 struct pltfm_imx_data *imx_data = pltfm_host->priv;
360 if (unlikely(reg == SDHCI_HOST_VERSION)) {
362 if (esdhc_is_usdhc(imx_data)) {
364 * The usdhc register returns a wrong host version.
367 return SDHCI_SPEC_300;
371 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
372 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
373 if (val & ESDHC_VENDOR_SPEC_VSELECT)
374 ret |= SDHCI_CTRL_VDD_180;
376 if (esdhc_is_usdhc(imx_data)) {
377 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
378 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
379 else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
380 /* the std tuning bits is in ACMD12_ERR for imx6sl */
381 val = readl(host->ioaddr + SDHCI_ACMD12_ERR);
384 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
385 ret |= SDHCI_CTRL_EXEC_TUNING;
386 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
387 ret |= SDHCI_CTRL_TUNED_CLK;
389 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
394 if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
395 if (esdhc_is_usdhc(imx_data)) {
396 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
397 ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
399 if (m & ESDHC_MIX_CTRL_AC23EN) {
400 ret &= ~ESDHC_MIX_CTRL_AC23EN;
401 ret |= SDHCI_TRNS_AUTO_CMD23;
404 ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
410 if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
411 if (esdhc_is_usdhc(imx_data)) {
412 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
413 ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
415 if (m & ESDHC_MIX_CTRL_AC23EN) {
416 ret &= ~ESDHC_MIX_CTRL_AC23EN;
417 ret |= SDHCI_TRNS_AUTO_CMD23;
420 ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
426 return readw(host->ioaddr + reg);
429 static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
431 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
432 struct pltfm_imx_data *imx_data = pltfm_host->priv;
436 case SDHCI_CLOCK_CONTROL:
437 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
438 if (val & SDHCI_CLOCK_CARD_EN)
439 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
441 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
442 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
444 case SDHCI_HOST_CONTROL2:
445 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
446 if (val & SDHCI_CTRL_VDD_180)
447 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
449 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
450 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
451 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
452 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
453 if (val & SDHCI_CTRL_TUNED_CLK)
454 new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
456 new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
457 writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
458 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
459 u32 v = readl(host->ioaddr + SDHCI_ACMD12_ERR);
460 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
461 if (val & SDHCI_CTRL_TUNED_CLK) {
462 v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
464 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
465 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
468 if (val & SDHCI_CTRL_EXEC_TUNING) {
469 v |= ESDHC_MIX_CTRL_EXE_TUNE;
470 m |= ESDHC_MIX_CTRL_FBCLK_SEL;
472 v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
475 writel(v, host->ioaddr + SDHCI_ACMD12_ERR);
476 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
479 case SDHCI_TRANSFER_MODE:
480 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
481 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
482 && (host->cmd->data->blocks > 1)
483 && (host->cmd->data->flags & MMC_DATA_READ)) {
485 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
486 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
487 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
490 if (esdhc_is_usdhc(imx_data)) {
491 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
493 if (val & SDHCI_TRNS_AUTO_CMD23) {
494 val &= ~SDHCI_TRNS_AUTO_CMD23;
495 val |= ESDHC_MIX_CTRL_AC23EN;
497 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
498 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
501 * Postpone this write, we must do it together with a
502 * command write that is down below.
504 imx_data->scratchpad = val;
508 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
509 val |= SDHCI_CMD_ABORTCMD;
511 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
512 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
513 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
515 if (esdhc_is_usdhc(imx_data))
517 host->ioaddr + SDHCI_TRANSFER_MODE);
519 writel(val << 16 | imx_data->scratchpad,
520 host->ioaddr + SDHCI_TRANSFER_MODE);
522 case SDHCI_BLOCK_SIZE:
523 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
526 esdhc_clrset_le(host, 0xffff, val, reg);
529 static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
531 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
532 struct pltfm_imx_data *imx_data = pltfm_host->priv;
537 case SDHCI_POWER_CONTROL:
539 * FSL put some DMA bits here
540 * If your board has a regulator, code should be here
543 case SDHCI_HOST_CONTROL:
544 /* FSL messed up here, so we need to manually compose it. */
545 new_val = val & SDHCI_CTRL_LED;
546 /* ensure the endianness */
547 new_val |= ESDHC_HOST_CONTROL_LE;
548 /* bits 8&9 are reserved on mx25 */
549 if (!is_imx25_esdhc(imx_data)) {
550 /* DMA mode bits are shifted */
551 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
555 * Do not touch buswidth bits here. This is done in
556 * esdhc_pltfm_bus_width.
557 * Do not touch the D3CD bit either which is used for the
558 * SDIO interrupt errata workaround.
560 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
562 esdhc_clrset_le(host, mask, new_val, reg);
565 esdhc_clrset_le(host, 0xff, val, reg);
568 * The esdhc has a design violation to SDHC spec which tells
569 * that software reset should not affect card detection circuit.
570 * But esdhc clears its SYSCTL register bits [0..2] during the
571 * software reset. This will stop those clocks that card detection
572 * circuit relies on. To work around it, we turn the clocks on back
573 * to keep card detection circuit functional.
575 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
576 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
578 * The reset on usdhc fails to clear MIX_CTRL register.
579 * Do it manually here.
581 if (esdhc_is_usdhc(imx_data)) {
582 /* the tuning bits should be kept during reset */
583 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
584 writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
585 host->ioaddr + ESDHC_MIX_CTRL);
586 imx_data->is_ddr = 0;
591 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
593 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
594 struct pltfm_imx_data *imx_data = pltfm_host->priv;
595 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
597 if (boarddata->f_max && (boarddata->f_max < pltfm_host->clock))
598 return boarddata->f_max;
600 return pltfm_host->clock;
603 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
605 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
607 return pltfm_host->clock / 256 / 16;
610 static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
613 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
614 struct pltfm_imx_data *imx_data = pltfm_host->priv;
615 unsigned int host_clock = pltfm_host->clock;
621 host->mmc->actual_clock = 0;
623 if (esdhc_is_usdhc(imx_data)) {
624 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
625 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
626 host->ioaddr + ESDHC_VENDOR_SPEC);
631 if (esdhc_is_usdhc(imx_data) && !imx_data->is_ddr)
634 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
635 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
637 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
639 while (host_clock / pre_div / 16 > clock && pre_div < 256)
642 while (host_clock / pre_div / div > clock && div < 16)
645 host->mmc->actual_clock = host_clock / pre_div / div;
646 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
647 clock, host->mmc->actual_clock);
649 if (imx_data->is_ddr)
655 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
656 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
657 | (div << ESDHC_DIVIDER_SHIFT)
658 | (pre_div << ESDHC_PREDIV_SHIFT));
659 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
661 if (esdhc_is_usdhc(imx_data)) {
662 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
663 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
664 host->ioaddr + ESDHC_VENDOR_SPEC);
670 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
672 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
673 struct pltfm_imx_data *imx_data = pltfm_host->priv;
674 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
676 switch (boarddata->wp_type) {
678 return mmc_gpio_get_ro(host->mmc);
679 case ESDHC_WP_CONTROLLER:
680 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
681 SDHCI_WRITE_PROTECT);
689 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
694 case MMC_BUS_WIDTH_8:
695 ctrl = ESDHC_CTRL_8BITBUS;
697 case MMC_BUS_WIDTH_4:
698 ctrl = ESDHC_CTRL_4BITBUS;
705 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
709 static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
713 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
716 /* This is balanced by the runtime put in sdhci_tasklet_finish */
717 pm_runtime_get_sync(host->mmc->parent);
718 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
719 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
720 ESDHC_MIX_CTRL_FBCLK_SEL;
721 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
722 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
723 dev_dbg(mmc_dev(host->mmc),
724 "tunning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
725 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
728 static void esdhc_request_done(struct mmc_request *mrq)
730 complete(&mrq->completion);
733 static int esdhc_send_tuning_cmd(struct sdhci_host *host, u32 opcode,
734 struct scatterlist *sg)
736 struct mmc_command cmd = {0};
737 struct mmc_request mrq = {NULL};
738 struct mmc_data data = {0};
742 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
744 data.blksz = ESDHC_TUNING_BLOCK_PATTERN_LEN;
746 data.flags = MMC_DATA_READ;
753 mrq.data->mrq = &mrq;
754 mrq.cmd->data = mrq.data;
756 mrq.done = esdhc_request_done;
757 init_completion(&(mrq.completion));
759 spin_lock_irq(&host->lock);
762 sdhci_send_command(host, mrq.cmd);
764 spin_unlock_irq(&host->lock);
766 wait_for_completion(&mrq.completion);
776 static void esdhc_post_tuning(struct sdhci_host *host)
780 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
781 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
782 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
785 static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
787 struct scatterlist sg;
788 char *tuning_pattern;
789 int min, max, avg, ret;
791 tuning_pattern = kmalloc(ESDHC_TUNING_BLOCK_PATTERN_LEN, GFP_KERNEL);
795 sg_init_one(&sg, tuning_pattern, ESDHC_TUNING_BLOCK_PATTERN_LEN);
797 /* find the mininum delay first which can pass tuning */
798 min = ESDHC_TUNE_CTRL_MIN;
799 while (min < ESDHC_TUNE_CTRL_MAX) {
800 esdhc_prepare_tuning(host, min);
801 if (!esdhc_send_tuning_cmd(host, opcode, &sg))
803 min += ESDHC_TUNE_CTRL_STEP;
806 /* find the maxinum delay which can not pass tuning */
807 max = min + ESDHC_TUNE_CTRL_STEP;
808 while (max < ESDHC_TUNE_CTRL_MAX) {
809 esdhc_prepare_tuning(host, max);
810 if (esdhc_send_tuning_cmd(host, opcode, &sg)) {
811 max -= ESDHC_TUNE_CTRL_STEP;
814 max += ESDHC_TUNE_CTRL_STEP;
817 /* use average delay to get the best timing */
818 avg = (min + max) / 2;
819 esdhc_prepare_tuning(host, avg);
820 ret = esdhc_send_tuning_cmd(host, opcode, &sg);
821 esdhc_post_tuning(host);
823 kfree(tuning_pattern);
825 dev_dbg(mmc_dev(host->mmc), "tunning %s at 0x%x ret %d\n",
826 ret ? "failed" : "passed", avg, ret);
831 static int esdhc_change_pinstate(struct sdhci_host *host,
834 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
835 struct pltfm_imx_data *imx_data = pltfm_host->priv;
836 struct pinctrl_state *pinctrl;
838 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
840 if (IS_ERR(imx_data->pinctrl) ||
841 IS_ERR(imx_data->pins_default) ||
842 IS_ERR(imx_data->pins_100mhz) ||
843 IS_ERR(imx_data->pins_200mhz))
847 case MMC_TIMING_UHS_SDR50:
848 pinctrl = imx_data->pins_100mhz;
850 case MMC_TIMING_UHS_SDR104:
851 case MMC_TIMING_MMC_HS200:
852 pinctrl = imx_data->pins_200mhz;
855 /* back to default state for other legacy timing */
856 pinctrl = imx_data->pins_default;
859 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
862 static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
864 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
865 struct pltfm_imx_data *imx_data = pltfm_host->priv;
866 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
869 case MMC_TIMING_UHS_SDR12:
870 case MMC_TIMING_UHS_SDR25:
871 case MMC_TIMING_UHS_SDR50:
872 case MMC_TIMING_UHS_SDR104:
873 case MMC_TIMING_MMC_HS200:
875 case MMC_TIMING_UHS_DDR50:
876 case MMC_TIMING_MMC_DDR52:
877 writel(readl(host->ioaddr + ESDHC_MIX_CTRL) |
878 ESDHC_MIX_CTRL_DDREN,
879 host->ioaddr + ESDHC_MIX_CTRL);
880 imx_data->is_ddr = 1;
881 if (boarddata->delay_line) {
883 v = boarddata->delay_line <<
884 ESDHC_DLL_OVERRIDE_VAL_SHIFT |
885 (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
886 if (is_imx53_esdhc(imx_data))
888 writel(v, host->ioaddr + ESDHC_DLL_CTRL);
893 esdhc_change_pinstate(host, timing);
896 static void esdhc_reset(struct sdhci_host *host, u8 mask)
898 sdhci_reset(host, mask);
900 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
901 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
904 static struct sdhci_ops sdhci_esdhc_ops = {
905 .read_l = esdhc_readl_le,
906 .read_w = esdhc_readw_le,
907 .write_l = esdhc_writel_le,
908 .write_w = esdhc_writew_le,
909 .write_b = esdhc_writeb_le,
910 .set_clock = esdhc_pltfm_set_clock,
911 .get_max_clock = esdhc_pltfm_get_max_clock,
912 .get_min_clock = esdhc_pltfm_get_min_clock,
913 .get_ro = esdhc_pltfm_get_ro,
914 .set_bus_width = esdhc_pltfm_set_bus_width,
915 .set_uhs_signaling = esdhc_set_uhs_signaling,
916 .reset = esdhc_reset,
919 static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
920 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
921 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
922 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
923 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
924 .ops = &sdhci_esdhc_ops,
929 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
930 struct esdhc_platform_data *boarddata)
932 struct device_node *np = pdev->dev.of_node;
937 if (of_get_property(np, "non-removable", NULL))
938 boarddata->cd_type = ESDHC_CD_PERMANENT;
940 if (of_get_property(np, "fsl,cd-controller", NULL))
941 boarddata->cd_type = ESDHC_CD_CONTROLLER;
943 if (of_get_property(np, "fsl,wp-controller", NULL))
944 boarddata->wp_type = ESDHC_WP_CONTROLLER;
946 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
947 if (gpio_is_valid(boarddata->cd_gpio))
948 boarddata->cd_type = ESDHC_CD_GPIO;
950 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
951 if (gpio_is_valid(boarddata->wp_gpio))
952 boarddata->wp_type = ESDHC_WP_GPIO;
954 of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
956 of_property_read_u32(np, "max-frequency", &boarddata->f_max);
958 if (of_find_property(np, "no-1-8-v", NULL))
959 boarddata->support_vsel = false;
961 boarddata->support_vsel = true;
963 if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
964 boarddata->delay_line = 0;
970 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
971 struct esdhc_platform_data *boarddata)
977 static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
979 const struct of_device_id *of_id =
980 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
981 struct sdhci_pltfm_host *pltfm_host;
982 struct sdhci_host *host;
983 struct esdhc_platform_data *boarddata;
985 struct pltfm_imx_data *imx_data;
987 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
989 return PTR_ERR(host);
991 pltfm_host = sdhci_priv(host);
993 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
999 imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
1000 pdev->id_entry->driver_data;
1001 pltfm_host->priv = imx_data;
1003 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1004 if (IS_ERR(imx_data->clk_ipg)) {
1005 err = PTR_ERR(imx_data->clk_ipg);
1009 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1010 if (IS_ERR(imx_data->clk_ahb)) {
1011 err = PTR_ERR(imx_data->clk_ahb);
1015 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
1016 if (IS_ERR(imx_data->clk_per)) {
1017 err = PTR_ERR(imx_data->clk_per);
1021 pltfm_host->clk = imx_data->clk_per;
1022 pltfm_host->clock = clk_get_rate(pltfm_host->clk);
1023 clk_prepare_enable(imx_data->clk_per);
1024 clk_prepare_enable(imx_data->clk_ipg);
1025 clk_prepare_enable(imx_data->clk_ahb);
1027 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
1028 if (IS_ERR(imx_data->pinctrl)) {
1029 err = PTR_ERR(imx_data->pinctrl);
1033 imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
1034 PINCTRL_STATE_DEFAULT);
1035 if (IS_ERR(imx_data->pins_default)) {
1036 err = PTR_ERR(imx_data->pins_default);
1037 dev_err(mmc_dev(host->mmc), "could not get default state\n");
1041 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
1043 if (imx_data->socdata->flags & ESDHC_FLAG_ENGCM07207)
1044 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
1045 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
1046 | SDHCI_QUIRK_BROKEN_ADMA;
1049 * The imx6q ROM code will change the default watermark level setting
1050 * to something insane. Change it back here.
1052 if (esdhc_is_usdhc(imx_data)) {
1053 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
1054 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
1055 host->mmc->caps |= MMC_CAP_1_8V_DDR;
1058 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
1059 sdhci_esdhc_ops.platform_execute_tuning =
1060 esdhc_executing_tuning;
1062 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
1063 writel(readl(host->ioaddr + ESDHC_TUNING_CTRL) |
1064 ESDHC_STD_TUNING_EN | ESDHC_TUNING_START_TAP,
1065 host->ioaddr + ESDHC_TUNING_CTRL);
1067 boarddata = &imx_data->boarddata;
1068 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
1069 if (!host->mmc->parent->platform_data) {
1070 dev_err(mmc_dev(host->mmc), "no board data!\n");
1074 imx_data->boarddata = *((struct esdhc_platform_data *)
1075 host->mmc->parent->platform_data);
1079 if (boarddata->wp_type == ESDHC_WP_GPIO) {
1080 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
1082 dev_err(mmc_dev(host->mmc),
1083 "failed to request write-protect gpio!\n");
1086 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
1090 switch (boarddata->cd_type) {
1092 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
1094 dev_err(mmc_dev(host->mmc),
1095 "failed to request card-detect gpio!\n");
1100 case ESDHC_CD_CONTROLLER:
1101 /* we have a working card_detect back */
1102 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1105 case ESDHC_CD_PERMANENT:
1106 host->mmc->caps |= MMC_CAP_NONREMOVABLE;
1113 switch (boarddata->max_bus_width) {
1115 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
1118 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
1122 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
1126 /* sdr50 and sdr104 needs work on 1.8v signal voltage */
1127 if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data)) {
1128 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1129 ESDHC_PINCTRL_STATE_100MHZ);
1130 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1131 ESDHC_PINCTRL_STATE_200MHZ);
1132 if (IS_ERR(imx_data->pins_100mhz) ||
1133 IS_ERR(imx_data->pins_200mhz)) {
1134 dev_warn(mmc_dev(host->mmc),
1135 "could not get ultra high speed state, work on normal mode\n");
1136 /* fall back to not support uhs by specify no 1.8v quirk */
1137 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1140 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1143 err = sdhci_add_host(host);
1147 pm_runtime_set_active(&pdev->dev);
1148 pm_runtime_enable(&pdev->dev);
1149 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1150 pm_runtime_use_autosuspend(&pdev->dev);
1151 pm_suspend_ignore_children(&pdev->dev, 1);
1156 clk_disable_unprepare(imx_data->clk_per);
1157 clk_disable_unprepare(imx_data->clk_ipg);
1158 clk_disable_unprepare(imx_data->clk_ahb);
1160 sdhci_pltfm_free(pdev);
1164 static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
1166 struct sdhci_host *host = platform_get_drvdata(pdev);
1167 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1168 struct pltfm_imx_data *imx_data = pltfm_host->priv;
1169 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1171 sdhci_remove_host(host, dead);
1173 pm_runtime_dont_use_autosuspend(&pdev->dev);
1174 pm_runtime_disable(&pdev->dev);
1176 if (!IS_ENABLED(CONFIG_PM_RUNTIME)) {
1177 clk_disable_unprepare(imx_data->clk_per);
1178 clk_disable_unprepare(imx_data->clk_ipg);
1179 clk_disable_unprepare(imx_data->clk_ahb);
1182 sdhci_pltfm_free(pdev);
1187 #ifdef CONFIG_PM_RUNTIME
1188 static int sdhci_esdhc_runtime_suspend(struct device *dev)
1190 struct sdhci_host *host = dev_get_drvdata(dev);
1191 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1192 struct pltfm_imx_data *imx_data = pltfm_host->priv;
1195 ret = sdhci_runtime_suspend_host(host);
1197 if (!sdhci_sdio_irq_enabled(host)) {
1198 clk_disable_unprepare(imx_data->clk_per);
1199 clk_disable_unprepare(imx_data->clk_ipg);
1201 clk_disable_unprepare(imx_data->clk_ahb);
1206 static int sdhci_esdhc_runtime_resume(struct device *dev)
1208 struct sdhci_host *host = dev_get_drvdata(dev);
1209 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1210 struct pltfm_imx_data *imx_data = pltfm_host->priv;
1212 if (!sdhci_sdio_irq_enabled(host)) {
1213 clk_prepare_enable(imx_data->clk_per);
1214 clk_prepare_enable(imx_data->clk_ipg);
1216 clk_prepare_enable(imx_data->clk_ahb);
1218 return sdhci_runtime_resume_host(host);
1222 static const struct dev_pm_ops sdhci_esdhc_pmops = {
1223 SET_SYSTEM_SLEEP_PM_OPS(sdhci_pltfm_suspend, sdhci_pltfm_resume)
1224 SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
1225 sdhci_esdhc_runtime_resume, NULL)
1228 static struct platform_driver sdhci_esdhc_imx_driver = {
1230 .name = "sdhci-esdhc-imx",
1231 .owner = THIS_MODULE,
1232 .of_match_table = imx_esdhc_dt_ids,
1233 .pm = &sdhci_esdhc_pmops,
1235 .id_table = imx_esdhc_devtype,
1236 .probe = sdhci_esdhc_imx_probe,
1237 .remove = sdhci_esdhc_imx_remove,
1240 module_platform_driver(sdhci_esdhc_imx_driver);
1242 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1243 MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
1244 MODULE_LICENSE("GPL v2");