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/slab.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/sdhci-pltfm.h>
22 #include <linux/mmc/mmc.h>
23 #include <linux/mmc/card.h>
24 #include <linux/mmc/sdio.h>
25 #include <linux/mmc/sd.h>
26 #include <linux/scatterlist.h>
27 #include <mach/hardware.h>
28 #include <mach/esdhc.h>
30 #include "sdhci-pltfm.h"
31 #include "sdhci-esdhc.h"
33 /* VENDOR SPEC register */
34 #define SDHCI_VENDOR_SPEC 0xC0
35 #define SDHCI_VENDOR_SPEC_SDIO_QUIRK 0x00000002
37 #define SDHCI_MIX_CTRL_AC23EN (1 << 7)
38 #define SDHCI_MIX_CTRL_EXE_TUNE (1 << 22)
39 #define SDHCI_MIX_CTRL_SMPCLK_SEL (1 << 23)
40 #define SDHCI_MIX_CTRL_AUTO_TUNE (1 << 24)
41 #define SDHCI_MIX_CTRL_FBCLK_SEL (1 << 25)
43 #define SDHCI_SYS_CTRL 0x2C
44 #define SDHCI_SYS_CTRL_RSTA_LSH 24
46 #define SDHCI_DLL_CTRL 0x60
47 #define SDHCI_DLL_OVERRIDE_OFFSET 0x9
48 #define SDHCI_DLL_OVERRIDE_EN_OFFSET 0x8
50 #define SDHCI_TUNE_CTRL_STATUS 0x68
51 #define SDHCI_TUNE_CTRL_STEP 0x1
52 #define SDHCI_TUNE_CTRL_MIN 0x0
53 #define SDHCI_TUNE_CTRL_MAX ((1 << 7) - 1)
55 #define SDHCI_VENDOR_SPEC_VSELECT (1 << 1)
56 #define SDHCI_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
58 #define SDHCI_PRESENT_STATE_CLSL (1 << 23)
59 #define SDHCI_PRESENT_STATE_DLSL_L4 (0xF << 24)
60 #define SDHCI_PRESENT_STATE_DLSL_H4 (0xF << 28)
62 #define ESDHC_FLAG_GPIO_FOR_CD_WP (1 << 0)
64 #define SDHCI_CTRL_D3CD 0x08
66 #define SDHCI_PROT_CTRL_DMASEL_MASK (3 << 8)
67 #define SDHCI_PROT_CTRL_DTW (3 << 1)
68 #define SDHCI_PROT_CTRL_8BIT (2 << 1)
69 #define SDHCI_PROT_CTRL_4BIT (1 << 1)
70 #define SDHCI_PROT_CTRL_1BIT (0 << 1)
71 #define SDHCI_PROT_CTRL_LCTL (1 << 0)
73 /* SDHCI_FSL_SVN_300 only for mx6q and mx6dl */
74 #define SDHCI_FSL_SVN_300 0x3
76 #define SDHCI_TUNE_CTRL 0xCC
77 #define SDHCI_TUNE_CTRL_STD_TUNING_EN (1 << 24)
79 #define SDHCI_HOST_CAP_UHS_MODE_MASK 0x7
81 #define SDHCI_TUNING_BLOCK_PATTERN_LEN 64
83 * There is an INT DMA ERR mis-match between eSDHC and STD SDHC SPEC:
84 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
85 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
86 * Define this macro DMA error INT for fsl eSDHC
88 #define SDHCI_INT_VENDOR_SPEC_DMA_ERR 0x10000000
91 * The CMDTYPE of the CMD register (offset 0xE) should be set to
92 * "11" when the STOP CMD12 is issued on imx53 to abort one
93 * open ended multi-blk IO. Otherwise the TC INT wouldn't
95 * In exact block transfer, the controller doesn't complete the
96 * operations automatically as required at the end of the
97 * transfer and remains on hold if the abort command is not sent.
98 * As a result, the TC flag is not asserted and SW received timeout
99 * exeception. Bit1 of Vendor Spec registor is used to fix it.
101 #define ESDHC_FLAG_MULTIBLK_NO_INT (1 << 1)
103 static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val);
104 static void esdhc_post_tuning(struct sdhci_host *host);
106 struct pltfm_imx_data {
109 /* uhs mode for sdhc host control2 */
110 unsigned char uhs_mode;
113 static void request_done(struct mmc_request *mrq)
115 complete(&mrq->completion);
118 static int esdhc_send_tuning_cmd(struct sdhci_host *host)
120 struct mmc_command cmd = {0};
121 struct mmc_request mrq = {0};
122 struct mmc_data data = {0};
123 struct scatterlist sg;
124 char tuning_pattern[SDHCI_TUNING_BLOCK_PATTERN_LEN];
126 cmd.opcode = MMC_SEND_TUNING_BLOCK;
128 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
130 data.blksz = SDHCI_TUNING_BLOCK_PATTERN_LEN;
132 data.flags = MMC_DATA_READ;
136 sg_init_one(&sg, tuning_pattern, sizeof(tuning_pattern));
141 mrq.data->mrq = &mrq;
142 mrq.cmd->data = mrq.data;
144 mrq.done = request_done;
146 init_completion(&(mrq.completion));
147 sdhci_request(host->mmc, &mrq);
148 wait_for_completion(&(mrq.completion));
158 static int esdhc_execute_tuning(struct sdhci_host *host)
162 min = host->tuning_min;
163 while (min < host->tuning_max) {
164 esdhc_prepare_tuning(host, min);
165 if (!esdhc_send_tuning_cmd(host))
167 min += host->tuning_step;
170 max = min + host->tuning_step;
171 while (max < host->tuning_max) {
172 esdhc_prepare_tuning(host, max);
173 if (esdhc_send_tuning_cmd(host)) {
174 max -= host->tuning_step;
177 max += host->tuning_step;
180 avg = (min + max) / 2;
181 esdhc_prepare_tuning(host, avg);
182 esdhc_send_tuning_cmd(host);
183 esdhc_post_tuning(host);
187 static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
189 void __iomem *base = host->ioaddr + (reg & ~0x3);
190 u32 shift = (reg & 0x3) * 8;
192 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
195 static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
197 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
198 struct pltfm_imx_data *imx_data = pltfm_host->priv;
199 struct esdhc_platform_data *boarddata
200 = host->mmc->parent->platform_data;
202 /* fake CARD_PRESENT flag on mx25/35 */
203 u32 val = readl(host->ioaddr + reg);
206 * mx6q: SDHCI_PRESENT_STATE bit 16, CINST is not functional on SD3.
207 * So move the section up, and check GPIO for card presence again in
208 * the following block.
210 if (reg == SDHCI_PRESENT_STATE && cpu_is_mx6()) {
211 u32 fsl_prss = readl(host->ioaddr + SDHCI_PRESENT_STATE);
212 /* save the least 20 bits */
213 val |= fsl_prss & 0x000FFFFF;
214 /* move dat[0-3] line signal bits */
215 val |= (fsl_prss & 0x0F000000) >> 4;
216 /* move cmd line signal bits */
217 val |= (fsl_prss & 0x00800000) << 1;
220 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
221 if (boarddata && boarddata->always_present)
222 val |= SDHCI_CARD_PRESENT;
223 else if (imx_data->flags & ESDHC_FLAG_GPIO_FOR_CD_WP) {
224 if (boarddata && gpio_is_valid(boarddata->cd_gpio)
225 && gpio_get_value(boarddata->cd_gpio))
226 /* no card, if a valid gpio says so */
227 val &= ~SDHCI_CARD_PRESENT;
229 /* in all other cases assume card is present */
230 val |= SDHCI_CARD_PRESENT;
234 if (reg == SDHCI_INT_STATUS && cpu_is_mx6()
235 && mx6q_revision() == IMX_CHIP_REVISION_1_0) {
237 * on mx6q TO1.0, there is low possibility that
238 * DATA END interrupt comes ealier than DMA
239 * END interrupt which is conflict with standard
240 * host controller spec. In this case, read the
241 * status register again will workaround this issue.
243 if ((val & SDHCI_INT_DATA_END) && \
244 !(val & SDHCI_INT_DMA_END))
245 val = readl(host->ioaddr + reg);
246 } else if (reg == SDHCI_CAPABILITIES_1
247 && (cpu_is_mx6q() || cpu_is_mx6dl())) {
249 * on mx6q, no cap_1 available, fake one.
251 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104 | \
253 } else if (reg == SDHCI_CAPABILITIES_1 && cpu_is_mx6sl()) {
254 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
255 } else if (reg == SDHCI_MAX_CURRENT && cpu_is_mx6()) {
257 * on mx6q, no max current available, fake one.
260 val |= 0xFF << SDHCI_MAX_CURRENT_330_SHIFT;
261 val |= 0xFF << SDHCI_MAX_CURRENT_300_SHIFT;
262 val |= 0xFF << SDHCI_MAX_CURRENT_180_SHIFT;
265 if (unlikely(reg == SDHCI_CAPABILITIES)) {
266 /* In FSL esdhc IC module, only bit20 is used to indicate the
267 * ADMA2 capability of esdhc, but this bit is messed up on
268 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
269 * don't actually support ADMA2). So set the BROKEN_ADMA
270 * uirk on MX25/35 platforms.
273 if (val & SDHCI_CAN_DO_ADMA1) {
274 val &= ~SDHCI_CAN_DO_ADMA1;
275 val |= SDHCI_CAN_DO_ADMA2;
279 if (unlikely(reg == SDHCI_INT_STATUS)) {
280 if (val & SDHCI_INT_VENDOR_SPEC_DMA_ERR) {
281 val &= ~SDHCI_INT_VENDOR_SPEC_DMA_ERR;
282 val |= SDHCI_INT_ADMA_ERROR;
289 static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
291 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
292 struct pltfm_imx_data *imx_data = pltfm_host->priv;
295 if (unlikely((reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE))) {
296 if (imx_data->flags & ESDHC_FLAG_GPIO_FOR_CD_WP)
298 * these interrupts won't work with a custom
299 * card_detect gpio (only applied to mx25/35)
301 val &= ~(SDHCI_INT_CARD_REMOVE | \
302 SDHCI_INT_CARD_INSERT);
304 if (!(val & SDHCI_INT_CARD_INT) && cpu_is_mx6()
305 && mx6q_revision() == IMX_CHIP_REVISION_1_0)
307 * write 1 to clear card interrupt status bit
308 * (only applied to mx6q TO1.0)
309 * uSDHC used for mx6q has such problem which is
310 * not consistant with standard host controller
312 * eSDHC used for mx25/35/50/51/53 does not have
315 writel(SDHCI_INT_CARD_INT, \
316 host->ioaddr + SDHCI_INT_STATUS);
318 if (val & SDHCI_INT_CARD_INT && !cpu_is_mx6()) {
320 * clear D3CD bit and set D3CD bit to avoid
321 * losing card interrupt
322 * (applied to all processors except mx6q)
323 * eSDHC controller used for mx25/35/50/51/53
324 * has such issue, so that we need to do following
325 * operation to avoid losing card interrupt.
326 * uSDCH controller used for mx6q and after won't
329 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
330 data &= ~SDHCI_CTRL_D3CD;
331 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
332 data |= SDHCI_CTRL_D3CD;
333 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
337 if (unlikely((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
338 && (reg == SDHCI_INT_STATUS)
339 && (val & SDHCI_INT_DATA_END))) {
341 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
342 v &= ~SDHCI_VENDOR_SPEC_SDIO_QUIRK;
343 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
346 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE)) {
347 if (val & SDHCI_INT_ADMA_ERROR) {
348 val &= ~SDHCI_INT_ADMA_ERROR;
349 val |= SDHCI_INT_VENDOR_SPEC_DMA_ERR;
353 writel(val, host->ioaddr + reg);
356 static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
360 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
361 struct pltfm_imx_data *imx_data = pltfm_host->priv;
364 case SDHCI_HOST_VERSION:
366 val = readl(host->ioaddr + reg);
367 if ((cpu_is_mx6q() || cpu_is_mx6dl()) &&
368 ((val & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT)
369 == SDHCI_FSL_SVN_300) {
370 val &= ~SDHCI_SPEC_VER_MASK;
371 val |= SDHCI_SPEC_300;
375 case SDHCI_HOST_CONTROL2:
377 /* collect bit info from several regs */
378 val = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
379 ret |= (val & SDHCI_VENDOR_SPEC_VSELECT)
380 ? SDHCI_CTRL_VDD_180 : 0;
382 if (cpu_is_mx6sl()) {
383 val = readl(host->ioaddr + SDHCI_ACMD12_ERR);
384 ret |= (val & SDHCI_MIX_CTRL_EXE_TUNE)
385 ? SDHCI_CTRL_EXEC_TUNING : 0;
386 ret |= (val & SDHCI_MIX_CTRL_SMPCLK_SEL)
387 ? SDHCI_CTRL_TUNED_CLK : 0;
389 val = readl(host->ioaddr + SDHCI_MIX_CTRL);
390 ret |= (val & SDHCI_MIX_CTRL_EXE_TUNE)
391 ? SDHCI_CTRL_EXEC_TUNING : 0;
392 ret |= (val & SDHCI_MIX_CTRL_SMPCLK_SEL)
393 ? SDHCI_CTRL_TUNED_CLK : 0;
395 ret |= SDHCI_CTRL_UHS_MASK & imx_data->uhs_mode;
396 /* no preset enable available */
397 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
402 return readw(host->ioaddr + reg);
405 static void esdhc_post_tuning(struct sdhci_host *host)
407 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
408 struct pltfm_imx_data *imx_data = pltfm_host->priv;
411 imx_data->scratchpad &= ~SDHCI_MIX_CTRL_EXE_TUNE;
412 reg = readl(host->ioaddr + SDHCI_MIX_CTRL);
413 reg &= ~SDHCI_MIX_CTRL_EXE_TUNE;
414 writel(reg, host->ioaddr + SDHCI_MIX_CTRL);
417 static void esdhc_reset(struct sdhci_host *host)
419 unsigned long timeout;
422 reg = readl(host->ioaddr + SDHCI_SYS_CTRL);
423 reg |= 1 << SDHCI_SYS_CTRL_RSTA_LSH;
424 writel(reg, host->ioaddr + SDHCI_SYS_CTRL);
429 /* hw clears the bit when it's done */
430 while (readl(host->ioaddr + SDHCI_SYS_CTRL)
431 & (1 << SDHCI_SYS_CTRL_RSTA_LSH)) {
433 printk(KERN_ERR "%s: Reset never completed.\n",
434 mmc_hostname(host->mmc));
442 static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
449 reg = readl(host->ioaddr + SDHCI_MIX_CTRL);
450 reg |= SDHCI_MIX_CTRL_EXE_TUNE | \
451 SDHCI_MIX_CTRL_SMPCLK_SEL | \
452 SDHCI_MIX_CTRL_FBCLK_SEL;
453 writel(reg, host->ioaddr + SDHCI_MIX_CTRL);
454 writel((val << 8), host->ioaddr + SDHCI_TUNE_CTRL_STATUS);
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 esdhc_platform_data *boarddata =
462 host->mmc->parent->platform_data;
463 struct pltfm_imx_data *imx_data = pltfm_host->priv;
467 case SDHCI_CLOCK_CONTROL:
468 orig_reg = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
469 if (val & SDHCI_CLOCK_CARD_EN) {
470 writel(orig_reg | SDHCI_VENDOR_SPEC_FRC_SDCLK_ON, \
471 host->ioaddr + SDHCI_VENDOR_SPEC);
473 writel(orig_reg & ~SDHCI_VENDOR_SPEC_FRC_SDCLK_ON, \
474 host->ioaddr + SDHCI_VENDOR_SPEC);
478 case SDHCI_HOST_CONTROL2:
479 orig_reg = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
480 if (val & SDHCI_CTRL_VDD_180) {
481 orig_reg |= SDHCI_VENDOR_SPEC_VSELECT;
482 writel(orig_reg, host->ioaddr + SDHCI_VENDOR_SPEC);
484 orig_reg &= ~SDHCI_VENDOR_SPEC_VSELECT;
485 writel(orig_reg, host->ioaddr + SDHCI_VENDOR_SPEC);
489 * FSL sdhc controls bus and signal voltage via one bit
490 * VSELECT in VENDOR_SPEC, which has been set in
491 * SDHCI_POWER_CONTROL. So we skip the SDHCI_CTRL_VDD_180
494 * ignore exec_tuning flag written to SDHCI_HOST_CONTROL2,
495 * tuning will be handled differently for FSL SDHC ip.
497 if (cpu_is_mx6sl()) {
498 orig_reg = readl(host->ioaddr + SDHCI_TUNE_CTRL);
499 if (val & SDHCI_CTRL_EXEC_TUNING) {
500 orig_reg |= SDHCI_TUNE_CTRL_STD_TUNING_EN;
502 host->ioaddr + SDHCI_TUNE_CTRL);
505 readl(host->ioaddr + SDHCI_ACMD12_ERR);
506 orig_reg |= SDHCI_MIX_CTRL_EXE_TUNE;
508 host->ioaddr + SDHCI_ACMD12_ERR);
511 orig_reg = readl(host->ioaddr + SDHCI_MIX_CTRL);
512 orig_reg &= ~SDHCI_MIX_CTRL_SMPCLK_SEL;
513 orig_reg |= (val & SDHCI_CTRL_TUNED_CLK)
514 ? SDHCI_MIX_CTRL_SMPCLK_SEL : 0;
517 if (val & SDHCI_CTRL_UHS_DDR50) {
518 orig_reg |= SDHCI_MIX_CTRL_DDREN;
519 imx_data->scratchpad |= SDHCI_MIX_CTRL_DDREN;
521 orig_reg &= ~SDHCI_MIX_CTRL_DDREN;
522 imx_data->scratchpad &= ~SDHCI_MIX_CTRL_DDREN;
524 writel(orig_reg, host->ioaddr + SDHCI_MIX_CTRL);
526 /* set clock frequency again */
527 esdhc_set_clock(host, host->clock);
528 imx_data->uhs_mode = val & SDHCI_CTRL_UHS_MASK;
530 /* delay line setting */
531 if (!boarddata->delay_line)
534 if (val & SDHCI_CTRL_UHS_DDR50)
535 writel((boarddata->delay_line \
536 << SDHCI_DLL_OVERRIDE_OFFSET) \
537 | (1 << SDHCI_DLL_OVERRIDE_EN_OFFSET),
538 host->ioaddr + SDHCI_DLL_CTRL);
540 writel(0, host->ioaddr + SDHCI_DLL_CTRL);
543 case SDHCI_TRANSFER_MODE:
545 * Postpone this write, we must do it together with a
546 * command write that is down below.
548 if ((imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
549 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
550 && (host->cmd->data->blocks > 1)
551 && (host->cmd->data->flags & MMC_DATA_READ)) {
553 v = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
554 v |= SDHCI_VENDOR_SPEC_SDIO_QUIRK;
555 writel(v, host->ioaddr + SDHCI_VENDOR_SPEC);
557 imx_data->scratchpad = val;
559 if (val & SDHCI_TRNS_AUTO_CMD23)
560 imx_data->scratchpad |= SDHCI_MIX_CTRL_AC23EN;
564 if ((host->cmd->opcode == MMC_STOP_TRANSMISSION ||
565 host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
566 (imx_data->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
567 val |= SDHCI_CMD_ABORTCMD;
569 writel(0x08800880, host->ioaddr + SDHCI_CAPABILITIES_1);
571 imx_data->scratchpad |= \
572 (readl(host->ioaddr + SDHCI_MIX_CTRL) & \
573 (SDHCI_MIX_CTRL_EXE_TUNE | \
574 SDHCI_MIX_CTRL_SMPCLK_SEL | \
575 SDHCI_MIX_CTRL_AUTO_TUNE | \
576 SDHCI_MIX_CTRL_FBCLK_SEL | \
577 SDHCI_MIX_CTRL_DDREN));
579 writel(imx_data->scratchpad,
580 host->ioaddr + SDHCI_MIX_CTRL);
583 host->ioaddr + SDHCI_TRANSFER_MODE);
585 writel(val << 16 | imx_data->scratchpad,
586 host->ioaddr + SDHCI_TRANSFER_MODE);
589 case SDHCI_BLOCK_SIZE:
590 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
593 esdhc_clrset_le(host, 0xffff, val, reg);
596 static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
603 case SDHCI_POWER_CONTROL:
604 reg_val = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
605 ret |= reg_val & SDHCI_VENDOR_SPEC_VSELECT
606 ? SDHCI_POWER_180 : SDHCI_POWER_300;
607 /* could not power off */
608 ret |= SDHCI_POWER_ON;
610 case SDHCI_HOST_CONTROL:
611 reg_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
612 if (reg_val & SDHCI_PROT_CTRL_LCTL)
613 ret |= SDHCI_CTRL_LED;
615 ret &= ~SDHCI_CTRL_LED;
617 ret |= (reg_val & SDHCI_PROT_CTRL_DMASEL_MASK) >> 5;
618 if (SDHCI_PROT_CTRL_8BIT == (reg_val & SDHCI_PROT_CTRL_DTW)) {
619 ret &= ~SDHCI_CTRL_4BITBUS;
620 ret |= SDHCI_CTRL_8BITBUS;
621 } else if (SDHCI_PROT_CTRL_4BIT
622 == (reg_val & SDHCI_PROT_CTRL_DTW)) {
623 ret &= ~SDHCI_CTRL_8BITBUS;
624 ret |= SDHCI_CTRL_4BITBUS;
625 } else if (SDHCI_PROT_CTRL_1BIT
626 == (reg_val & SDHCI_PROT_CTRL_DTW))
627 ret &= ~(SDHCI_CTRL_8BITBUS | SDHCI_CTRL_4BITBUS);
629 case SDHCI_SOFTWARE_RESET:
630 reg_val = readl(host->ioaddr + SDHCI_CLOCK_CONTROL);
633 case SDHCI_RESPONSE + 3:
634 reg_val = readl(host->ioaddr + SDHCI_RESPONSE);
637 case SDHCI_RESPONSE + 7:
638 reg_val = readl(host->ioaddr + SDHCI_RESPONSE + 4);
641 case SDHCI_RESPONSE + 11:
642 reg_val = readl(host->ioaddr + SDHCI_RESPONSE + 8);
652 static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
657 case SDHCI_POWER_CONTROL:
659 * FSL put some DMA bits here
660 * If your board has a regulator, code should be here
662 if (val == (SDHCI_POWER_ON | SDHCI_POWER_180)) {
666 reg = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
667 writel(reg & ~SDHCI_VENDOR_SPEC_FRC_SDCLK_ON, \
668 host->ioaddr + SDHCI_VENDOR_SPEC);
671 reg = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
672 reg |= SDHCI_VENDOR_SPEC_VSELECT;
673 writel(reg, host->ioaddr + SDHCI_VENDOR_SPEC);
678 reg = readl(host->ioaddr + SDHCI_VENDOR_SPEC);
679 reg &= ~SDHCI_VENDOR_SPEC_VSELECT;
680 writel(reg, host->ioaddr + SDHCI_VENDOR_SPEC);
683 case SDHCI_HOST_CONTROL:
684 /* FSL messed up here, so we can just keep those three */
685 new_val = val & (SDHCI_CTRL_LED);
686 if (val & SDHCI_CTRL_8BITBUS) {
687 new_val |= SDHCI_PROT_CTRL_8BIT;
688 new_val &= ~SDHCI_PROT_CTRL_4BIT;
689 } else if (val & SDHCI_CTRL_4BITBUS) {
690 new_val &= ~SDHCI_PROT_CTRL_8BIT;
691 new_val |= SDHCI_PROT_CTRL_4BIT;
693 /* ensure the endianess */
694 new_val |= ESDHC_HOST_CONTROL_LE;
695 /* DMA mode bits are shifted */
696 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
698 esdhc_clrset_le(host, 0xffff, new_val, reg);
701 esdhc_clrset_le(host, 0xff, val, reg);
704 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
706 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
708 return clk_get_rate(pltfm_host->clk);
711 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
713 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
715 return clk_get_rate(pltfm_host->clk) / 256 / 16;
718 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
720 struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
722 if (boarddata && gpio_is_valid(boarddata->wp_gpio))
723 return gpio_get_value(boarddata->wp_gpio);
728 static int plt_8bit_width(struct sdhci_host *host, int width)
730 u32 reg = readl(host->ioaddr + SDHCI_HOST_CONTROL);
732 reg &= ~SDHCI_PROT_CTRL_DTW;
734 if (width == MMC_BUS_WIDTH_8)
735 reg |= SDHCI_PROT_CTRL_8BIT;
736 else if (width == MMC_BUS_WIDTH_4)
737 reg |= SDHCI_PROT_CTRL_4BIT;
739 writel(reg, host->ioaddr + SDHCI_HOST_CONTROL);
743 static void plt_clk_ctrl(struct sdhci_host *host, bool enable)
745 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
748 clk_enable(pltfm_host->clk);
749 host->clk_status = true;
751 clk_disable(pltfm_host->clk);
752 host->clk_status = false;
756 static struct sdhci_ops sdhci_esdhc_ops = {
757 .read_l = esdhc_readl_le,
758 .read_w = esdhc_readw_le,
759 .read_b = esdhc_readb_le,
760 .write_l = esdhc_writel_le,
761 .write_w = esdhc_writew_le,
762 .write_b = esdhc_writeb_le,
763 .set_clock = esdhc_set_clock,
764 .get_max_clock = esdhc_pltfm_get_max_clock,
765 .get_min_clock = esdhc_pltfm_get_min_clock,
766 .platform_8bit_width = plt_8bit_width,
767 .platform_clk_ctrl = plt_clk_ctrl,
770 static irqreturn_t cd_irq(int irq, void *data)
772 struct sdhci_host *sdhost = (struct sdhci_host *)data;
773 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(sdhost);
774 struct pltfm_imx_data *imx_data = pltfm_host->priv;
776 writel(0, sdhost->ioaddr + SDHCI_MIX_CTRL);
777 writel(0, sdhost->ioaddr + SDHCI_TUNE_CTRL_STATUS);
780 imx_data->scratchpad &= ~SDHCI_MIX_CTRL_DDREN;
781 imx_data->scratchpad &= ~SDHCI_MIX_CTRL_FBCLK_SEL;
782 imx_data->scratchpad &= ~SDHCI_MIX_CTRL_SMPCLK_SEL;
788 tasklet_schedule(&sdhost->card_tasklet);
792 static int esdhc_pltfm_init(struct sdhci_host *host, struct sdhci_pltfm_data *pdata)
794 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
795 struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
798 struct pltfm_imx_data *imx_data;
801 clk = clk_get(mmc_dev(host->mmc), NULL);
803 dev_err(mmc_dev(host->mmc), "clk err\n");
807 pltfm_host->clk = clk;
809 imx_data = kzalloc(sizeof(struct pltfm_imx_data), GFP_KERNEL);
811 clk_disable(pltfm_host->clk);
812 clk_put(pltfm_host->clk);
815 pltfm_host->priv = imx_data;
817 host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
819 if (cpu_is_mx25() || cpu_is_mx35())
820 /* Fix errata ENGcm07207 present on i.MX25 and i.MX35 */
821 host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK
822 | SDHCI_QUIRK_BROKEN_ADMA;
825 * on mx6dl TO 1.1, ADMA can work when ahb bus frequency is low,
828 if (mx6dl_revision() >= IMX_CHIP_REVISION_1_1)
829 host->quirks &= ~SDHCI_QUIRK_BROKEN_ADMA;
832 host->quirks2 |= SDHCI_QUIRK_BROKEN_AUTO_CMD23,
834 /* write_protect can't be routed to controller, use gpio */
835 sdhci_esdhc_ops.get_ro = esdhc_pltfm_get_ro;
837 if (!(cpu_is_mx25() || cpu_is_mx35() || cpu_is_mx51() || cpu_is_mx6()))
838 imx_data->flags |= ESDHC_FLAG_MULTIBLK_NO_INT;
840 host->ocr_avail_sd = MMC_VDD_29_30 | MMC_VDD_30_31 | \
841 MMC_VDD_32_33 | MMC_VDD_33_34;
842 host->ocr_avail_mmc = MMC_VDD_29_30 | MMC_VDD_30_31 | \
843 MMC_VDD_32_33 | MMC_VDD_33_34;
844 host->ocr_avail_sdio = MMC_VDD_29_30 | MMC_VDD_30_31 | \
845 MMC_VDD_32_33 | MMC_VDD_33_34;
847 if (cpu_is_mx6q() || cpu_is_mx6dl())
848 sdhci_esdhc_ops.platform_execute_tuning = esdhc_execute_tuning;
850 if (boarddata->support_18v) {
851 host->ocr_avail_sd |= MMC_VDD_165_195;
852 host->ocr_avail_mmc |= MMC_VDD_165_195;
853 host->ocr_avail_sdio |= MMC_VDD_165_195;
856 if (boarddata->support_8bit)
857 host->mmc->caps |= MMC_CAP_8_BIT_DATA;
858 if (boarddata->keep_power_at_suspend)
859 host->mmc->pm_caps |= (MMC_PM_KEEP_POWER | \
860 MMC_PM_WAKE_SDIO_IRQ);
861 if (boarddata->cd_type == ESDHC_CD_PERMANENT)
862 host->mmc->caps |= MMC_CAP_NONREMOVABLE;
863 else if (boarddata->cd_type == ESDHC_CD_CONTROLLER
864 || boarddata->cd_type == ESDHC_CD_NONE
865 || boarddata->cd_type == ESDHC_CD_GPIO)
866 host->mmc->caps &= ~MMC_CAP_NONREMOVABLE;
868 host->mmc->caps |= MMC_CAP_1_8V_DDR;
869 host->tuning_min = SDHCI_TUNE_CTRL_MIN;
870 host->tuning_max = SDHCI_TUNE_CTRL_MAX;
871 host->tuning_step = SDHCI_TUNE_CTRL_STEP;
872 host->clk_mgr_en = true;
875 reg = readl(host->ioaddr + SDHCI_MIX_CTRL);
876 reg &= ~SDHCI_MIX_CTRL_DDREN;
877 writel(reg, host->ioaddr + SDHCI_MIX_CTRL);
880 /* Device is always present, e.x, populated emmc device */
881 if (boarddata->always_present) {
882 /* remove BROKEN_CD to disable card polling */
883 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
884 /* if it is always present, invalidate cd_gpio */
885 boarddata->cd_gpio = -EINVAL;
886 if (host->clk_mgr_en)
887 clk_disable(pltfm_host->clk);
891 if (gpio_is_valid(boarddata->wp_gpio)) {
892 err = gpio_request_one(boarddata->wp_gpio, GPIOF_IN, "ESDHC_WP");
894 dev_err(mmc_dev(host->mmc),
895 "failed to request write-protect pin: %d!\n",
899 dev_warn(mmc_dev(host->mmc),
900 "no write-protect pin available!\n");
903 if (gpio_is_valid(boarddata->cd_gpio)) {
904 err = gpio_request_one(boarddata->cd_gpio, GPIOF_IN, "ESDHC_CD");
906 dev_err(mmc_dev(host->mmc),
907 "failed to request card-detect pin: %d!\n",
909 goto no_card_detect_pin;
912 dev_warn(mmc_dev(host->mmc),
913 "no card-detect pin available!\n");
914 goto no_card_detect_pin;
917 err = request_irq(gpio_to_irq(boarddata->cd_gpio), cd_irq,
918 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
919 mmc_hostname(host->mmc), host);
921 dev_warn(mmc_dev(host->mmc), "request irq error\n");
922 goto no_card_detect_irq;
925 imx_data->flags |= ESDHC_FLAG_GPIO_FOR_CD_WP;
926 /* Now we have a working card_detect again */
927 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
930 #ifdef CONFIG_PM_RUNTIME
931 host->mmc->caps |= MMC_CAP_POWER_OFF_CARD;
934 if (host->clk_mgr_en)
935 clk_disable(pltfm_host->clk);
939 gpio_free(boarddata->cd_gpio);
942 if (host->clk_mgr_en)
943 clk_disable(pltfm_host->clk);
947 static void esdhc_pltfm_exit(struct sdhci_host *host)
949 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
950 struct esdhc_platform_data *boarddata = host->mmc->parent->platform_data;
951 struct pltfm_imx_data *imx_data = pltfm_host->priv;
953 if (boarddata && gpio_is_valid(boarddata->wp_gpio))
954 gpio_free(boarddata->wp_gpio);
956 if (boarddata && gpio_is_valid(boarddata->cd_gpio)) {
957 gpio_free(boarddata->cd_gpio);
959 if (!(host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION))
960 free_irq(gpio_to_irq(boarddata->cd_gpio), host);
963 if (!host->clk_mgr_en)
964 clk_disable(pltfm_host->clk);
965 clk_put(pltfm_host->clk);
969 struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
970 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
971 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
972 | SDHCI_QUIRK_BROKEN_CARD_DETECTION
973 | SDHCI_QUIRK_BROKEN_ADMA
974 | SDHCI_QUIRK_NO_HISPD_BIT,
975 /* ADMA has issues. Might be fixable */
976 .ops = &sdhci_esdhc_ops,
977 .init = esdhc_pltfm_init,
978 .exit = esdhc_pltfm_exit,