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)
122 /* The IP has errata ERR004536 */
123 #define ESDHC_FLAG_ERR004536 BIT(7)
125 struct esdhc_soc_data {
129 static struct esdhc_soc_data esdhc_imx25_data = {
130 .flags = ESDHC_FLAG_ENGCM07207,
133 static struct esdhc_soc_data esdhc_imx35_data = {
134 .flags = ESDHC_FLAG_ENGCM07207,
137 static struct esdhc_soc_data esdhc_imx51_data = {
141 static struct esdhc_soc_data esdhc_imx53_data = {
142 .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
145 static struct esdhc_soc_data usdhc_imx6q_data = {
146 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING,
149 static struct esdhc_soc_data usdhc_imx6sl_data = {
150 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
151 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536,
154 struct pltfm_imx_data {
156 struct pinctrl *pinctrl;
157 struct pinctrl_state *pins_default;
158 struct pinctrl_state *pins_100mhz;
159 struct pinctrl_state *pins_200mhz;
160 const struct esdhc_soc_data *socdata;
161 struct esdhc_platform_data boarddata;
166 NO_CMD_PENDING, /* no multiblock command pending*/
167 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
168 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
173 static struct platform_device_id imx_esdhc_devtype[] = {
175 .name = "sdhci-esdhc-imx25",
176 .driver_data = (kernel_ulong_t) &esdhc_imx25_data,
178 .name = "sdhci-esdhc-imx35",
179 .driver_data = (kernel_ulong_t) &esdhc_imx35_data,
181 .name = "sdhci-esdhc-imx51",
182 .driver_data = (kernel_ulong_t) &esdhc_imx51_data,
187 MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
189 static const struct of_device_id imx_esdhc_dt_ids[] = {
190 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
191 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
192 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
193 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
194 { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
195 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
198 MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
200 static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
202 return data->socdata == &esdhc_imx25_data;
205 static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
207 return data->socdata == &esdhc_imx53_data;
210 static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
212 return data->socdata == &usdhc_imx6q_data;
215 static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
217 return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
220 static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
222 void __iomem *base = host->ioaddr + (reg & ~0x3);
223 u32 shift = (reg & 0x3) * 8;
225 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
228 static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
230 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
231 struct pltfm_imx_data *imx_data = pltfm_host->priv;
232 u32 val = readl(host->ioaddr + reg);
234 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
236 /* save the least 20 bits */
237 val = fsl_prss & 0x000FFFFF;
238 /* move dat[0-3] bits */
239 val |= (fsl_prss & 0x0F000000) >> 4;
240 /* move cmd line bit */
241 val |= (fsl_prss & 0x00800000) << 1;
244 if (unlikely(reg == SDHCI_CAPABILITIES)) {
245 /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
246 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
249 /* In FSL esdhc IC module, only bit20 is used to indicate the
250 * ADMA2 capability of esdhc, but this bit is messed up on
251 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
252 * don't actually support ADMA2). So set the BROKEN_ADMA
253 * uirk on MX25/35 platforms.
256 if (val & SDHCI_CAN_DO_ADMA1) {
257 val &= ~SDHCI_CAN_DO_ADMA1;
258 val |= SDHCI_CAN_DO_ADMA2;
262 if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
263 if (esdhc_is_usdhc(imx_data)) {
264 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
265 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
267 /* imx6q/dl does not have cap_1 register, fake one */
268 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
269 | SDHCI_SUPPORT_SDR50
270 | SDHCI_USE_SDR50_TUNING;
274 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
276 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
277 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
278 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
281 if (unlikely(reg == SDHCI_INT_STATUS)) {
282 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
283 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
284 val |= SDHCI_INT_ADMA_ERROR;
288 * mask off the interrupt we get in response to the manually
291 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
292 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
293 val &= ~SDHCI_INT_RESPONSE;
294 writel(SDHCI_INT_RESPONSE, host->ioaddr +
296 imx_data->multiblock_status = NO_CMD_PENDING;
303 static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
305 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
306 struct pltfm_imx_data *imx_data = pltfm_host->priv;
309 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
310 if (val & SDHCI_INT_CARD_INT) {
312 * Clear and then set D3CD bit to avoid missing the
313 * card interrupt. This is a eSDHC controller problem
314 * so we need to apply the following workaround: clear
315 * and set D3CD bit will make eSDHC re-sample the card
316 * interrupt. In case a card interrupt was lost,
317 * re-sample it by the following steps.
319 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
320 data &= ~ESDHC_CTRL_D3CD;
321 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
322 data |= ESDHC_CTRL_D3CD;
323 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
327 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
328 && (reg == SDHCI_INT_STATUS)
329 && (val & SDHCI_INT_DATA_END))) {
331 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
332 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
333 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
335 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
337 /* send a manual CMD12 with RESPTYP=none */
338 data = MMC_STOP_TRANSMISSION << 24 |
339 SDHCI_CMD_ABORTCMD << 16;
340 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
341 imx_data->multiblock_status = WAIT_FOR_INT;
345 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
346 if (val & SDHCI_INT_ADMA_ERROR) {
347 val &= ~SDHCI_INT_ADMA_ERROR;
348 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
352 writel(val, host->ioaddr + reg);
355 static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
357 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
358 struct pltfm_imx_data *imx_data = pltfm_host->priv;
362 if (unlikely(reg == SDHCI_HOST_VERSION)) {
364 if (esdhc_is_usdhc(imx_data)) {
366 * The usdhc register returns a wrong host version.
369 return SDHCI_SPEC_300;
373 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
374 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
375 if (val & ESDHC_VENDOR_SPEC_VSELECT)
376 ret |= SDHCI_CTRL_VDD_180;
378 if (esdhc_is_usdhc(imx_data)) {
379 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
380 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
381 else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
382 /* the std tuning bits is in ACMD12_ERR for imx6sl */
383 val = readl(host->ioaddr + SDHCI_ACMD12_ERR);
386 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
387 ret |= SDHCI_CTRL_EXEC_TUNING;
388 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
389 ret |= SDHCI_CTRL_TUNED_CLK;
391 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
396 if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
397 if (esdhc_is_usdhc(imx_data)) {
398 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
399 ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
401 if (m & ESDHC_MIX_CTRL_AC23EN) {
402 ret &= ~ESDHC_MIX_CTRL_AC23EN;
403 ret |= SDHCI_TRNS_AUTO_CMD23;
406 ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
412 if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
413 if (esdhc_is_usdhc(imx_data)) {
414 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
415 ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
417 if (m & ESDHC_MIX_CTRL_AC23EN) {
418 ret &= ~ESDHC_MIX_CTRL_AC23EN;
419 ret |= SDHCI_TRNS_AUTO_CMD23;
422 ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
428 return readw(host->ioaddr + reg);
431 static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
433 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
434 struct pltfm_imx_data *imx_data = pltfm_host->priv;
438 case SDHCI_CLOCK_CONTROL:
439 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
440 if (val & SDHCI_CLOCK_CARD_EN)
441 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
443 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
444 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
446 case SDHCI_HOST_CONTROL2:
447 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
448 if (val & SDHCI_CTRL_VDD_180)
449 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
451 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
452 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
453 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
454 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
455 if (val & SDHCI_CTRL_TUNED_CLK)
456 new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
458 new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
459 writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
460 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
461 u32 v = readl(host->ioaddr + SDHCI_ACMD12_ERR);
462 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
463 if (val & SDHCI_CTRL_TUNED_CLK) {
464 v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
466 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
467 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
470 if (val & SDHCI_CTRL_EXEC_TUNING) {
471 v |= ESDHC_MIX_CTRL_EXE_TUNE;
472 m |= ESDHC_MIX_CTRL_FBCLK_SEL;
474 v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
477 writel(v, host->ioaddr + SDHCI_ACMD12_ERR);
478 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
481 case SDHCI_TRANSFER_MODE:
482 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
483 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
484 && (host->cmd->data->blocks > 1)
485 && (host->cmd->data->flags & MMC_DATA_READ)) {
487 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
488 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
489 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
492 if (esdhc_is_usdhc(imx_data)) {
493 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
495 if (val & SDHCI_TRNS_AUTO_CMD23) {
496 val &= ~SDHCI_TRNS_AUTO_CMD23;
497 val |= ESDHC_MIX_CTRL_AC23EN;
499 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
500 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
503 * Postpone this write, we must do it together with a
504 * command write that is down below.
506 imx_data->scratchpad = val;
510 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
511 val |= SDHCI_CMD_ABORTCMD;
513 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
514 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
515 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
517 if (esdhc_is_usdhc(imx_data))
519 host->ioaddr + SDHCI_TRANSFER_MODE);
521 writel(val << 16 | imx_data->scratchpad,
522 host->ioaddr + SDHCI_TRANSFER_MODE);
524 case SDHCI_BLOCK_SIZE:
525 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
528 esdhc_clrset_le(host, 0xffff, val, reg);
531 static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
533 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
534 struct pltfm_imx_data *imx_data = pltfm_host->priv;
539 case SDHCI_POWER_CONTROL:
541 * FSL put some DMA bits here
542 * If your board has a regulator, code should be here
545 case SDHCI_HOST_CONTROL:
546 /* FSL messed up here, so we need to manually compose it. */
547 new_val = val & SDHCI_CTRL_LED;
548 /* ensure the endianness */
549 new_val |= ESDHC_HOST_CONTROL_LE;
550 /* bits 8&9 are reserved on mx25 */
551 if (!is_imx25_esdhc(imx_data)) {
552 /* DMA mode bits are shifted */
553 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
557 * Do not touch buswidth bits here. This is done in
558 * esdhc_pltfm_bus_width.
559 * Do not touch the D3CD bit either which is used for the
560 * SDIO interrupt errata workaround.
562 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
564 esdhc_clrset_le(host, mask, new_val, reg);
567 esdhc_clrset_le(host, 0xff, val, reg);
570 * The esdhc has a design violation to SDHC spec which tells
571 * that software reset should not affect card detection circuit.
572 * But esdhc clears its SYSCTL register bits [0..2] during the
573 * software reset. This will stop those clocks that card detection
574 * circuit relies on. To work around it, we turn the clocks on back
575 * to keep card detection circuit functional.
577 if ((reg == SDHCI_SOFTWARE_RESET) && (val & 1)) {
578 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
580 * The reset on usdhc fails to clear MIX_CTRL register.
581 * Do it manually here.
583 if (esdhc_is_usdhc(imx_data)) {
584 /* the tuning bits should be kept during reset */
585 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
586 writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
587 host->ioaddr + ESDHC_MIX_CTRL);
588 imx_data->is_ddr = 0;
593 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
595 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
596 struct pltfm_imx_data *imx_data = pltfm_host->priv;
597 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
599 if (boarddata->f_max && (boarddata->f_max < pltfm_host->clock))
600 return boarddata->f_max;
602 return pltfm_host->clock;
605 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
607 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
609 return pltfm_host->clock / 256 / 16;
612 static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
615 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
616 struct pltfm_imx_data *imx_data = pltfm_host->priv;
617 unsigned int host_clock = pltfm_host->clock;
623 host->mmc->actual_clock = 0;
625 if (esdhc_is_usdhc(imx_data)) {
626 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
627 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
628 host->ioaddr + ESDHC_VENDOR_SPEC);
633 if (esdhc_is_usdhc(imx_data) && !imx_data->is_ddr)
636 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
637 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
639 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
641 while (host_clock / pre_div / 16 > clock && pre_div < 256)
644 while (host_clock / pre_div / div > clock && div < 16)
647 host->mmc->actual_clock = host_clock / pre_div / div;
648 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
649 clock, host->mmc->actual_clock);
651 if (imx_data->is_ddr)
657 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
658 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
659 | (div << ESDHC_DIVIDER_SHIFT)
660 | (pre_div << ESDHC_PREDIV_SHIFT));
661 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
663 if (esdhc_is_usdhc(imx_data)) {
664 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
665 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
666 host->ioaddr + ESDHC_VENDOR_SPEC);
672 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
674 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
675 struct pltfm_imx_data *imx_data = pltfm_host->priv;
676 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
678 switch (boarddata->wp_type) {
680 return mmc_gpio_get_ro(host->mmc);
681 case ESDHC_WP_CONTROLLER:
682 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
683 SDHCI_WRITE_PROTECT);
691 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
696 case MMC_BUS_WIDTH_8:
697 ctrl = ESDHC_CTRL_8BITBUS;
699 case MMC_BUS_WIDTH_4:
700 ctrl = ESDHC_CTRL_4BITBUS;
707 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
711 static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
715 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
718 /* This is balanced by the runtime put in sdhci_tasklet_finish */
719 pm_runtime_get_sync(host->mmc->parent);
720 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
721 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
722 ESDHC_MIX_CTRL_FBCLK_SEL;
723 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
724 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
725 dev_dbg(mmc_dev(host->mmc),
726 "tunning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
727 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
730 static void esdhc_request_done(struct mmc_request *mrq)
732 complete(&mrq->completion);
735 static int esdhc_send_tuning_cmd(struct sdhci_host *host, u32 opcode,
736 struct scatterlist *sg)
738 struct mmc_command cmd = {0};
739 struct mmc_request mrq = {NULL};
740 struct mmc_data data = {0};
744 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
746 data.blksz = ESDHC_TUNING_BLOCK_PATTERN_LEN;
748 data.flags = MMC_DATA_READ;
755 mrq.data->mrq = &mrq;
756 mrq.cmd->data = mrq.data;
758 mrq.done = esdhc_request_done;
759 init_completion(&(mrq.completion));
761 spin_lock_irq(&host->lock);
764 sdhci_send_command(host, mrq.cmd);
766 spin_unlock_irq(&host->lock);
768 wait_for_completion(&mrq.completion);
778 static void esdhc_post_tuning(struct sdhci_host *host)
782 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
783 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
784 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
787 static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
789 struct scatterlist sg;
790 char *tuning_pattern;
791 int min, max, avg, ret;
793 tuning_pattern = kmalloc(ESDHC_TUNING_BLOCK_PATTERN_LEN, GFP_KERNEL);
797 sg_init_one(&sg, tuning_pattern, ESDHC_TUNING_BLOCK_PATTERN_LEN);
799 /* find the mininum delay first which can pass tuning */
800 min = ESDHC_TUNE_CTRL_MIN;
801 while (min < ESDHC_TUNE_CTRL_MAX) {
802 esdhc_prepare_tuning(host, min);
803 if (!esdhc_send_tuning_cmd(host, opcode, &sg))
805 min += ESDHC_TUNE_CTRL_STEP;
808 /* find the maxinum delay which can not pass tuning */
809 max = min + ESDHC_TUNE_CTRL_STEP;
810 while (max < ESDHC_TUNE_CTRL_MAX) {
811 esdhc_prepare_tuning(host, max);
812 if (esdhc_send_tuning_cmd(host, opcode, &sg)) {
813 max -= ESDHC_TUNE_CTRL_STEP;
816 max += ESDHC_TUNE_CTRL_STEP;
819 /* use average delay to get the best timing */
820 avg = (min + max) / 2;
821 esdhc_prepare_tuning(host, avg);
822 ret = esdhc_send_tuning_cmd(host, opcode, &sg);
823 esdhc_post_tuning(host);
825 kfree(tuning_pattern);
827 dev_dbg(mmc_dev(host->mmc), "tunning %s at 0x%x ret %d\n",
828 ret ? "failed" : "passed", avg, ret);
833 static int esdhc_change_pinstate(struct sdhci_host *host,
836 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
837 struct pltfm_imx_data *imx_data = pltfm_host->priv;
838 struct pinctrl_state *pinctrl;
840 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
842 if (IS_ERR(imx_data->pinctrl) ||
843 IS_ERR(imx_data->pins_default) ||
844 IS_ERR(imx_data->pins_100mhz) ||
845 IS_ERR(imx_data->pins_200mhz))
849 case MMC_TIMING_UHS_SDR50:
850 pinctrl = imx_data->pins_100mhz;
852 case MMC_TIMING_UHS_SDR104:
853 case MMC_TIMING_MMC_HS200:
854 pinctrl = imx_data->pins_200mhz;
857 /* back to default state for other legacy timing */
858 pinctrl = imx_data->pins_default;
861 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
864 static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
866 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
867 struct pltfm_imx_data *imx_data = pltfm_host->priv;
868 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
871 case MMC_TIMING_UHS_SDR12:
872 case MMC_TIMING_UHS_SDR25:
873 case MMC_TIMING_UHS_SDR50:
874 case MMC_TIMING_UHS_SDR104:
875 case MMC_TIMING_MMC_HS200:
877 case MMC_TIMING_UHS_DDR50:
878 case MMC_TIMING_MMC_DDR52:
879 writel(readl(host->ioaddr + ESDHC_MIX_CTRL) |
880 ESDHC_MIX_CTRL_DDREN,
881 host->ioaddr + ESDHC_MIX_CTRL);
882 imx_data->is_ddr = 1;
883 if (boarddata->delay_line) {
885 v = boarddata->delay_line <<
886 ESDHC_DLL_OVERRIDE_VAL_SHIFT |
887 (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
888 if (is_imx53_esdhc(imx_data))
890 writel(v, host->ioaddr + ESDHC_DLL_CTRL);
895 esdhc_change_pinstate(host, timing);
898 static void esdhc_reset(struct sdhci_host *host, u8 mask)
900 sdhci_reset(host, mask);
902 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
903 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
906 static struct sdhci_ops sdhci_esdhc_ops = {
907 .read_l = esdhc_readl_le,
908 .read_w = esdhc_readw_le,
909 .write_l = esdhc_writel_le,
910 .write_w = esdhc_writew_le,
911 .write_b = esdhc_writeb_le,
912 .set_clock = esdhc_pltfm_set_clock,
913 .get_max_clock = esdhc_pltfm_get_max_clock,
914 .get_min_clock = esdhc_pltfm_get_min_clock,
915 .get_ro = esdhc_pltfm_get_ro,
916 .set_bus_width = esdhc_pltfm_set_bus_width,
917 .set_uhs_signaling = esdhc_set_uhs_signaling,
918 .reset = esdhc_reset,
921 static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
922 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
923 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
924 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
925 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
926 .ops = &sdhci_esdhc_ops,
931 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
932 struct esdhc_platform_data *boarddata)
934 struct device_node *np = pdev->dev.of_node;
939 if (of_get_property(np, "non-removable", NULL))
940 boarddata->cd_type = ESDHC_CD_PERMANENT;
942 if (of_get_property(np, "fsl,cd-controller", NULL))
943 boarddata->cd_type = ESDHC_CD_CONTROLLER;
945 if (of_get_property(np, "fsl,wp-controller", NULL))
946 boarddata->wp_type = ESDHC_WP_CONTROLLER;
948 boarddata->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
949 if (gpio_is_valid(boarddata->cd_gpio))
950 boarddata->cd_type = ESDHC_CD_GPIO;
952 boarddata->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
953 if (gpio_is_valid(boarddata->wp_gpio))
954 boarddata->wp_type = ESDHC_WP_GPIO;
956 of_property_read_u32(np, "bus-width", &boarddata->max_bus_width);
958 of_property_read_u32(np, "max-frequency", &boarddata->f_max);
960 if (of_find_property(np, "no-1-8-v", NULL))
961 boarddata->support_vsel = false;
963 boarddata->support_vsel = true;
965 if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
966 boarddata->delay_line = 0;
972 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
973 struct esdhc_platform_data *boarddata)
979 static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
981 const struct of_device_id *of_id =
982 of_match_device(imx_esdhc_dt_ids, &pdev->dev);
983 struct sdhci_pltfm_host *pltfm_host;
984 struct sdhci_host *host;
985 struct esdhc_platform_data *boarddata;
987 struct pltfm_imx_data *imx_data;
989 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 0);
991 return PTR_ERR(host);
993 pltfm_host = sdhci_priv(host);
995 imx_data = devm_kzalloc(&pdev->dev, sizeof(*imx_data), GFP_KERNEL);
1001 imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
1002 pdev->id_entry->driver_data;
1003 pltfm_host->priv = imx_data;
1005 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1006 if (IS_ERR(imx_data->clk_ipg)) {
1007 err = PTR_ERR(imx_data->clk_ipg);
1011 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1012 if (IS_ERR(imx_data->clk_ahb)) {
1013 err = PTR_ERR(imx_data->clk_ahb);
1017 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
1018 if (IS_ERR(imx_data->clk_per)) {
1019 err = PTR_ERR(imx_data->clk_per);
1023 pltfm_host->clk = imx_data->clk_per;
1024 pltfm_host->clock = clk_get_rate(pltfm_host->clk);
1025 clk_prepare_enable(imx_data->clk_per);
1026 clk_prepare_enable(imx_data->clk_ipg);
1027 clk_prepare_enable(imx_data->clk_ahb);
1029 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
1030 if (IS_ERR(imx_data->pinctrl)) {
1031 err = PTR_ERR(imx_data->pinctrl);
1035 imx_data->pins_default = pinctrl_lookup_state(imx_data->pinctrl,
1036 PINCTRL_STATE_DEFAULT);
1037 if (IS_ERR(imx_data->pins_default)) {
1038 err = PTR_ERR(imx_data->pins_default);
1039 dev_err(mmc_dev(host->mmc), "could not get default state\n");
1043 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
1045 if (imx_data->socdata->flags & ESDHC_FLAG_ENGCM07207)
1046 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
1047 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
1048 | SDHCI_QUIRK_BROKEN_ADMA;
1051 * The imx6q ROM code will change the default watermark level setting
1052 * to something insane. Change it back here.
1054 if (esdhc_is_usdhc(imx_data)) {
1055 writel(0x08100810, host->ioaddr + ESDHC_WTMK_LVL);
1056 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
1057 host->mmc->caps |= MMC_CAP_1_8V_DDR;
1060 * errata ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
1061 * TO1.1, it's harmless for MX6SL
1063 writel(readl(host->ioaddr + 0x6c) | BIT(7), host->ioaddr + 0x6c);
1066 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
1067 sdhci_esdhc_ops.platform_execute_tuning =
1068 esdhc_executing_tuning;
1070 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
1071 writel(readl(host->ioaddr + ESDHC_TUNING_CTRL) |
1072 ESDHC_STD_TUNING_EN | ESDHC_TUNING_START_TAP,
1073 host->ioaddr + ESDHC_TUNING_CTRL);
1075 if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
1076 host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
1078 boarddata = &imx_data->boarddata;
1079 if (sdhci_esdhc_imx_probe_dt(pdev, boarddata) < 0) {
1080 if (!host->mmc->parent->platform_data) {
1081 dev_err(mmc_dev(host->mmc), "no board data!\n");
1085 imx_data->boarddata = *((struct esdhc_platform_data *)
1086 host->mmc->parent->platform_data);
1090 if (boarddata->wp_type == ESDHC_WP_GPIO) {
1091 err = mmc_gpio_request_ro(host->mmc, boarddata->wp_gpio);
1093 dev_err(mmc_dev(host->mmc),
1094 "failed to request write-protect gpio!\n");
1097 host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
1101 switch (boarddata->cd_type) {
1103 err = mmc_gpio_request_cd(host->mmc, boarddata->cd_gpio, 0);
1105 dev_err(mmc_dev(host->mmc),
1106 "failed to request card-detect gpio!\n");
1111 case ESDHC_CD_CONTROLLER:
1112 /* we have a working card_detect back */
1113 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1116 case ESDHC_CD_PERMANENT:
1117 host->mmc->caps |= MMC_CAP_NONREMOVABLE;
1124 switch (boarddata->max_bus_width) {
1126 host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
1129 host->mmc->caps |= MMC_CAP_4_BIT_DATA;
1133 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
1137 /* sdr50 and sdr104 needs work on 1.8v signal voltage */
1138 if ((boarddata->support_vsel) && esdhc_is_usdhc(imx_data)) {
1139 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1140 ESDHC_PINCTRL_STATE_100MHZ);
1141 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1142 ESDHC_PINCTRL_STATE_200MHZ);
1143 if (IS_ERR(imx_data->pins_100mhz) ||
1144 IS_ERR(imx_data->pins_200mhz)) {
1145 dev_warn(mmc_dev(host->mmc),
1146 "could not get ultra high speed state, work on normal mode\n");
1147 /* fall back to not support uhs by specify no 1.8v quirk */
1148 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1151 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1154 err = sdhci_add_host(host);
1158 pm_runtime_set_active(&pdev->dev);
1159 pm_runtime_enable(&pdev->dev);
1160 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1161 pm_runtime_use_autosuspend(&pdev->dev);
1162 pm_suspend_ignore_children(&pdev->dev, 1);
1167 clk_disable_unprepare(imx_data->clk_per);
1168 clk_disable_unprepare(imx_data->clk_ipg);
1169 clk_disable_unprepare(imx_data->clk_ahb);
1171 sdhci_pltfm_free(pdev);
1175 static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
1177 struct sdhci_host *host = platform_get_drvdata(pdev);
1178 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1179 struct pltfm_imx_data *imx_data = pltfm_host->priv;
1180 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1182 sdhci_remove_host(host, dead);
1184 pm_runtime_dont_use_autosuspend(&pdev->dev);
1185 pm_runtime_disable(&pdev->dev);
1187 if (!IS_ENABLED(CONFIG_PM_RUNTIME)) {
1188 clk_disable_unprepare(imx_data->clk_per);
1189 clk_disable_unprepare(imx_data->clk_ipg);
1190 clk_disable_unprepare(imx_data->clk_ahb);
1193 sdhci_pltfm_free(pdev);
1198 #ifdef CONFIG_PM_RUNTIME
1199 static int sdhci_esdhc_runtime_suspend(struct device *dev)
1201 struct sdhci_host *host = dev_get_drvdata(dev);
1202 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1203 struct pltfm_imx_data *imx_data = pltfm_host->priv;
1206 ret = sdhci_runtime_suspend_host(host);
1208 if (!sdhci_sdio_irq_enabled(host)) {
1209 clk_disable_unprepare(imx_data->clk_per);
1210 clk_disable_unprepare(imx_data->clk_ipg);
1212 clk_disable_unprepare(imx_data->clk_ahb);
1217 static int sdhci_esdhc_runtime_resume(struct device *dev)
1219 struct sdhci_host *host = dev_get_drvdata(dev);
1220 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1221 struct pltfm_imx_data *imx_data = pltfm_host->priv;
1223 if (!sdhci_sdio_irq_enabled(host)) {
1224 clk_prepare_enable(imx_data->clk_per);
1225 clk_prepare_enable(imx_data->clk_ipg);
1227 clk_prepare_enable(imx_data->clk_ahb);
1229 return sdhci_runtime_resume_host(host);
1233 static const struct dev_pm_ops sdhci_esdhc_pmops = {
1234 SET_SYSTEM_SLEEP_PM_OPS(sdhci_pltfm_suspend, sdhci_pltfm_resume)
1235 SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
1236 sdhci_esdhc_runtime_resume, NULL)
1239 static struct platform_driver sdhci_esdhc_imx_driver = {
1241 .name = "sdhci-esdhc-imx",
1242 .owner = THIS_MODULE,
1243 .of_match_table = imx_esdhc_dt_ids,
1244 .pm = &sdhci_esdhc_pmops,
1246 .id_table = imx_esdhc_devtype,
1247 .probe = sdhci_esdhc_imx_probe,
1248 .remove = sdhci_esdhc_imx_remove,
1251 module_platform_driver(sdhci_esdhc_imx_driver);
1253 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1254 MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
1255 MODULE_LICENSE("GPL v2");