1 // SPDX-License-Identifier: GPL-2.0
3 * Freescale eSDHC i.MX controller driver for the platform bus.
5 * derived from the OF-version.
7 * Copyright (c) 2010 Pengutronix e.K.
8 * Author: Wolfram Sang <kernel@pengutronix.de>
11 #include <linux/bitfield.h>
13 #include <linux/iopoll.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/clk.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/pm_qos.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/mmc.h>
22 #include <linux/mmc/sdio.h>
23 #include <linux/mmc/slot-gpio.h>
25 #include <linux/of_device.h>
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/pm_runtime.h>
28 #include "sdhci-pltfm.h"
29 #include "sdhci-esdhc.h"
32 #define ESDHC_SYS_CTRL_DTOCV_MASK 0x0f
33 #define ESDHC_CTRL_D3CD 0x08
34 #define ESDHC_BURST_LEN_EN_INCR (1 << 27)
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_DEBUG_SEL_AND_STATUS_REG 0xc2
41 #define ESDHC_DEBUG_SEL_REG 0xc3
42 #define ESDHC_DEBUG_SEL_MASK 0xf
43 #define ESDHC_DEBUG_SEL_CMD_STATE 1
44 #define ESDHC_DEBUG_SEL_DATA_STATE 2
45 #define ESDHC_DEBUG_SEL_TRANS_STATE 3
46 #define ESDHC_DEBUG_SEL_DMA_STATE 4
47 #define ESDHC_DEBUG_SEL_ADMA_STATE 5
48 #define ESDHC_DEBUG_SEL_FIFO_STATE 6
49 #define ESDHC_DEBUG_SEL_ASYNC_FIFO_STATE 7
50 #define ESDHC_WTMK_LVL 0x44
51 #define ESDHC_WTMK_DEFAULT_VAL 0x10401040
52 #define ESDHC_WTMK_LVL_RD_WML_MASK 0x000000FF
53 #define ESDHC_WTMK_LVL_RD_WML_SHIFT 0
54 #define ESDHC_WTMK_LVL_WR_WML_MASK 0x00FF0000
55 #define ESDHC_WTMK_LVL_WR_WML_SHIFT 16
56 #define ESDHC_WTMK_LVL_WML_VAL_DEF 64
57 #define ESDHC_WTMK_LVL_WML_VAL_MAX 128
58 #define ESDHC_MIX_CTRL 0x48
59 #define ESDHC_MIX_CTRL_DDREN (1 << 3)
60 #define ESDHC_MIX_CTRL_AC23EN (1 << 7)
61 #define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
62 #define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
63 #define ESDHC_MIX_CTRL_AUTO_TUNE_EN (1 << 24)
64 #define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
65 #define ESDHC_MIX_CTRL_HS400_EN (1 << 26)
66 #define ESDHC_MIX_CTRL_HS400_ES_EN (1 << 27)
67 /* Bits 3 and 6 are not SDHCI standard definitions */
68 #define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
70 #define ESDHC_MIX_CTRL_TUNING_MASK 0x03c00000
72 /* dll control register */
73 #define ESDHC_DLL_CTRL 0x60
74 #define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9
75 #define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
77 /* tune control register */
78 #define ESDHC_TUNE_CTRL_STATUS 0x68
79 #define ESDHC_TUNE_CTRL_STEP 1
80 #define ESDHC_TUNE_CTRL_MIN 0
81 #define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
83 /* strobe dll register */
84 #define ESDHC_STROBE_DLL_CTRL 0x70
85 #define ESDHC_STROBE_DLL_CTRL_ENABLE (1 << 0)
86 #define ESDHC_STROBE_DLL_CTRL_RESET (1 << 1)
87 #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT 0x7
88 #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT 3
89 #define ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT (4 << 20)
91 #define ESDHC_STROBE_DLL_STATUS 0x74
92 #define ESDHC_STROBE_DLL_STS_REF_LOCK (1 << 1)
93 #define ESDHC_STROBE_DLL_STS_SLV_LOCK 0x1
95 #define ESDHC_VEND_SPEC2 0xc8
96 #define ESDHC_VEND_SPEC2_EN_BUSY_IRQ (1 << 8)
97 #define ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN (1 << 4)
98 #define ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN (0 << 4)
99 #define ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN (2 << 4)
100 #define ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN (1 << 6)
101 #define ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK (7 << 4)
103 #define ESDHC_TUNING_CTRL 0xcc
104 #define ESDHC_STD_TUNING_EN (1 << 24)
105 /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
106 #define ESDHC_TUNING_START_TAP_DEFAULT 0x1
107 #define ESDHC_TUNING_START_TAP_MASK 0x7f
108 #define ESDHC_TUNING_CMD_CRC_CHECK_DISABLE (1 << 7)
109 #define ESDHC_TUNING_STEP_MASK 0x00070000
110 #define ESDHC_TUNING_STEP_SHIFT 16
113 #define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
114 #define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
117 * Our interpretation of the SDHCI_HOST_CONTROL register
119 #define ESDHC_CTRL_4BITBUS (0x1 << 1)
120 #define ESDHC_CTRL_8BITBUS (0x2 << 1)
121 #define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
122 #define USDHC_GET_BUSWIDTH(c) (c & ESDHC_CTRL_BUSWIDTH_MASK)
125 * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC:
126 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
127 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
128 * Define this macro DMA error INT for fsl eSDHC
130 #define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
132 /* the address offset of CQHCI */
133 #define ESDHC_CQHCI_ADDR_OFFSET 0x100
136 * The CMDTYPE of the CMD register (offset 0xE) should be set to
137 * "11" when the STOP CMD12 is issued on imx53 to abort one
138 * open ended multi-blk IO. Otherwise the TC INT wouldn't
140 * In exact block transfer, the controller doesn't complete the
141 * operations automatically as required at the end of the
142 * transfer and remains on hold if the abort command is not sent.
143 * As a result, the TC flag is not asserted and SW received timeout
144 * exception. Bit1 of Vendor Spec register is used to fix it.
146 #define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
148 * The flag tells that the ESDHC controller is an USDHC block that is
149 * integrated on the i.MX6 series.
151 #define ESDHC_FLAG_USDHC BIT(3)
152 /* The IP supports manual tuning process */
153 #define ESDHC_FLAG_MAN_TUNING BIT(4)
154 /* The IP supports standard tuning process */
155 #define ESDHC_FLAG_STD_TUNING BIT(5)
156 /* The IP has SDHCI_CAPABILITIES_1 register */
157 #define ESDHC_FLAG_HAVE_CAP1 BIT(6)
159 * The IP has erratum ERR004536
160 * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow,
161 * when reading data from the card
162 * This flag is also set for i.MX25 and i.MX35 in order to get
163 * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits).
165 #define ESDHC_FLAG_ERR004536 BIT(7)
166 /* The IP supports HS200 mode */
167 #define ESDHC_FLAG_HS200 BIT(8)
168 /* The IP supports HS400 mode */
169 #define ESDHC_FLAG_HS400 BIT(9)
171 * The IP has errata ERR010450
172 * uSDHC: Due to the I/O timing limit, for SDR mode, SD card clock can't
173 * exceed 150MHz, for DDR mode, SD card clock can't exceed 45MHz.
175 #define ESDHC_FLAG_ERR010450 BIT(10)
176 /* The IP supports HS400ES mode */
177 #define ESDHC_FLAG_HS400_ES BIT(11)
178 /* The IP has Host Controller Interface for Command Queuing */
179 #define ESDHC_FLAG_CQHCI BIT(12)
180 /* need request pmqos during low power */
181 #define ESDHC_FLAG_PMQOS BIT(13)
182 /* The IP state got lost in low power mode */
183 #define ESDHC_FLAG_STATE_LOST_IN_LPMODE BIT(14)
184 /* The IP lost clock rate in PM_RUNTIME */
185 #define ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME BIT(15)
187 * The IP do not support the ACMD23 feature completely when use ADMA mode.
188 * In ADMA mode, it only use the 16 bit block count of the register 0x4
189 * (BLOCK_ATT) as the CMD23's argument for ACMD23 mode, which means it will
190 * ignore the upper 16 bit of the CMD23's argument. This will block the reliable
191 * write operation in RPMB, because RPMB reliable write need to set the bit31
192 * of the CMD23's argument.
193 * imx6qpdl/imx6sx/imx6sl/imx7d has this limitation only for ADMA mode, SDMA
194 * do not has this limitation. so when these SoC use ADMA mode, it need to
195 * disable the ACMD23 feature.
197 #define ESDHC_FLAG_BROKEN_AUTO_CMD23 BIT(16)
199 /* ERR004536 is not applicable for the IP */
200 #define ESDHC_FLAG_SKIP_ERR004536 BIT(17)
203 ESDHC_WP_NONE, /* no WP, neither controller nor gpio */
204 ESDHC_WP_CONTROLLER, /* mmc controller internal WP */
205 ESDHC_WP_GPIO, /* external gpio pin for WP */
209 ESDHC_CD_NONE, /* no CD, neither controller nor gpio */
210 ESDHC_CD_CONTROLLER, /* mmc controller internal CD */
211 ESDHC_CD_GPIO, /* external gpio pin for CD */
212 ESDHC_CD_PERMANENT, /* no CD, card permanently wired to host */
216 * struct esdhc_platform_data - platform data for esdhc on i.MX
218 * ESDHC_WP(CD)_CONTROLLER type is not available on i.MX25/35.
220 * @wp_type: type of write_protect method (see wp_types enum above)
221 * @cd_type: type of card_detect method (see cd_types enum above)
224 struct esdhc_platform_data {
225 enum wp_types wp_type;
226 enum cd_types cd_type;
228 unsigned int delay_line;
229 unsigned int tuning_step; /* The delay cell steps in tuning procedure */
230 unsigned int tuning_start_tap; /* The start delay cell point in tuning procedure */
231 unsigned int strobe_dll_delay_target; /* The delay cell for strobe pad (read clock) */
234 struct esdhc_soc_data {
238 static const struct esdhc_soc_data esdhc_imx25_data = {
239 .flags = ESDHC_FLAG_ERR004536,
242 static const struct esdhc_soc_data esdhc_imx35_data = {
243 .flags = ESDHC_FLAG_ERR004536,
246 static const struct esdhc_soc_data esdhc_imx51_data = {
250 static const struct esdhc_soc_data esdhc_imx53_data = {
251 .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
254 static const struct esdhc_soc_data usdhc_imx6q_data = {
255 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING
256 | ESDHC_FLAG_BROKEN_AUTO_CMD23,
259 static const struct esdhc_soc_data usdhc_imx6sl_data = {
260 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
261 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536
263 | ESDHC_FLAG_BROKEN_AUTO_CMD23,
266 static const struct esdhc_soc_data usdhc_imx6sll_data = {
267 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
268 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
270 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
273 static const struct esdhc_soc_data usdhc_imx6sx_data = {
274 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
275 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
276 | ESDHC_FLAG_STATE_LOST_IN_LPMODE
277 | ESDHC_FLAG_BROKEN_AUTO_CMD23,
280 static const struct esdhc_soc_data usdhc_imx6ull_data = {
281 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
282 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
283 | ESDHC_FLAG_ERR010450
284 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
287 static const struct esdhc_soc_data usdhc_imx7d_data = {
288 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
289 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
291 | ESDHC_FLAG_STATE_LOST_IN_LPMODE
292 | ESDHC_FLAG_BROKEN_AUTO_CMD23,
295 static struct esdhc_soc_data usdhc_s32g2_data = {
296 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING
297 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
298 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
299 | ESDHC_FLAG_SKIP_ERR004536,
302 static struct esdhc_soc_data usdhc_imx7ulp_data = {
303 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
304 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
305 | ESDHC_FLAG_PMQOS | ESDHC_FLAG_HS400
306 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
308 static struct esdhc_soc_data usdhc_imxrt1050_data = {
309 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_HS200 | ESDHC_FLAG_ERR004536,
312 static struct esdhc_soc_data usdhc_imx8qxp_data = {
313 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
314 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
315 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
316 | ESDHC_FLAG_STATE_LOST_IN_LPMODE
317 | ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME,
320 static struct esdhc_soc_data usdhc_imx8mm_data = {
321 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
322 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
323 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
324 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
327 struct pltfm_imx_data {
329 struct pinctrl *pinctrl;
330 struct pinctrl_state *pins_100mhz;
331 struct pinctrl_state *pins_200mhz;
332 const struct esdhc_soc_data *socdata;
333 struct esdhc_platform_data boarddata;
337 unsigned int actual_clock;
339 NO_CMD_PENDING, /* no multiblock command pending */
340 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
341 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
344 struct pm_qos_request pm_qos_req;
347 static const struct of_device_id imx_esdhc_dt_ids[] = {
348 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
349 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
350 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
351 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
352 { .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
353 { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
354 { .compatible = "fsl,imx6sll-usdhc", .data = &usdhc_imx6sll_data, },
355 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
356 { .compatible = "fsl,imx6ull-usdhc", .data = &usdhc_imx6ull_data, },
357 { .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
358 { .compatible = "fsl,imx7ulp-usdhc", .data = &usdhc_imx7ulp_data, },
359 { .compatible = "fsl,imx8qxp-usdhc", .data = &usdhc_imx8qxp_data, },
360 { .compatible = "fsl,imx8mm-usdhc", .data = &usdhc_imx8mm_data, },
361 { .compatible = "fsl,imxrt1050-usdhc", .data = &usdhc_imxrt1050_data, },
362 { .compatible = "nxp,s32g2-usdhc", .data = &usdhc_s32g2_data, },
365 MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
367 static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
369 return data->socdata == &esdhc_imx25_data;
372 static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
374 return data->socdata == &esdhc_imx53_data;
377 static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
379 return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
382 static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
384 void __iomem *base = host->ioaddr + (reg & ~0x3);
385 u32 shift = (reg & 0x3) * 8;
387 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
390 #define DRIVER_NAME "sdhci-esdhc-imx"
391 #define ESDHC_IMX_DUMP(f, x...) \
392 pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
393 static void esdhc_dump_debug_regs(struct sdhci_host *host)
396 char *debug_status[7] = {
399 "trans debug status",
403 "async fifo debug status"
406 ESDHC_IMX_DUMP("========= ESDHC IMX DEBUG STATUS DUMP =========\n");
407 for (i = 0; i < 7; i++) {
408 esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK,
409 ESDHC_DEBUG_SEL_CMD_STATE + i, ESDHC_DEBUG_SEL_REG);
410 ESDHC_IMX_DUMP("%s: 0x%04x\n", debug_status[i],
411 readw(host->ioaddr + ESDHC_DEBUG_SEL_AND_STATUS_REG));
414 esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK, 0, ESDHC_DEBUG_SEL_REG);
418 static inline void esdhc_wait_for_card_clock_gate_off(struct sdhci_host *host)
423 ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, present_state,
424 (present_state & ESDHC_CLOCK_GATE_OFF), 2, 100);
425 if (ret == -ETIMEDOUT)
426 dev_warn(mmc_dev(host->mmc), "%s: card clock still not gate off in 100us!.\n", __func__);
429 /* Enable the auto tuning circuit to check the CMD line and BUS line */
430 static inline void usdhc_auto_tuning_mode_sel(struct sdhci_host *host)
432 u32 buswidth, auto_tune_buswidth;
434 buswidth = USDHC_GET_BUSWIDTH(readl(host->ioaddr + SDHCI_HOST_CONTROL));
437 case ESDHC_CTRL_8BITBUS:
438 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN;
440 case ESDHC_CTRL_4BITBUS:
441 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN;
443 default: /* 1BITBUS */
444 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN;
448 esdhc_clrset_le(host, ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK,
449 auto_tune_buswidth | ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN,
453 static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
455 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
456 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
457 u32 val = readl(host->ioaddr + reg);
459 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
461 /* save the least 20 bits */
462 val = fsl_prss & 0x000FFFFF;
463 /* move dat[0-3] bits */
464 val |= (fsl_prss & 0x0F000000) >> 4;
465 /* move cmd line bit */
466 val |= (fsl_prss & 0x00800000) << 1;
469 if (unlikely(reg == SDHCI_CAPABILITIES)) {
470 /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
471 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
474 /* In FSL esdhc IC module, only bit20 is used to indicate the
475 * ADMA2 capability of esdhc, but this bit is messed up on
476 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
477 * don't actually support ADMA2). So set the BROKEN_ADMA
478 * quirk on MX25/35 platforms.
481 if (val & SDHCI_CAN_DO_ADMA1) {
482 val &= ~SDHCI_CAN_DO_ADMA1;
483 val |= SDHCI_CAN_DO_ADMA2;
487 if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
488 if (esdhc_is_usdhc(imx_data)) {
489 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
490 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
492 /* imx6q/dl does not have cap_1 register, fake one */
493 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
494 | SDHCI_SUPPORT_SDR50
495 | SDHCI_USE_SDR50_TUNING
496 | FIELD_PREP(SDHCI_RETUNING_MODE_MASK,
497 SDHCI_TUNING_MODE_3);
500 * Do not advertise faster UHS modes if there are no
501 * pinctrl states for 100MHz/200MHz.
503 if (IS_ERR_OR_NULL(imx_data->pins_100mhz))
504 val &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50);
505 if (IS_ERR_OR_NULL(imx_data->pins_200mhz))
506 val &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_HS400);
510 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
512 val |= FIELD_PREP(SDHCI_MAX_CURRENT_330_MASK, 0xFF);
513 val |= FIELD_PREP(SDHCI_MAX_CURRENT_300_MASK, 0xFF);
514 val |= FIELD_PREP(SDHCI_MAX_CURRENT_180_MASK, 0xFF);
517 if (unlikely(reg == SDHCI_INT_STATUS)) {
518 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
519 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
520 val |= SDHCI_INT_ADMA_ERROR;
524 * mask off the interrupt we get in response to the manually
527 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
528 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
529 val &= ~SDHCI_INT_RESPONSE;
530 writel(SDHCI_INT_RESPONSE, host->ioaddr +
532 imx_data->multiblock_status = NO_CMD_PENDING;
539 static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
541 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
542 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
545 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
546 reg == SDHCI_INT_STATUS)) {
547 if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
549 * Clear and then set D3CD bit to avoid missing the
550 * card interrupt. This is an eSDHC controller problem
551 * so we need to apply the following workaround: clear
552 * and set D3CD bit will make eSDHC re-sample the card
553 * interrupt. In case a card interrupt was lost,
554 * re-sample it by the following steps.
556 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
557 data &= ~ESDHC_CTRL_D3CD;
558 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
559 data |= ESDHC_CTRL_D3CD;
560 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
563 if (val & SDHCI_INT_ADMA_ERROR) {
564 val &= ~SDHCI_INT_ADMA_ERROR;
565 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
569 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
570 && (reg == SDHCI_INT_STATUS)
571 && (val & SDHCI_INT_DATA_END))) {
573 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
574 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
575 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
577 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
579 /* send a manual CMD12 with RESPTYP=none */
580 data = MMC_STOP_TRANSMISSION << 24 |
581 SDHCI_CMD_ABORTCMD << 16;
582 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
583 imx_data->multiblock_status = WAIT_FOR_INT;
587 writel(val, host->ioaddr + reg);
590 static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
592 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
593 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
597 if (unlikely(reg == SDHCI_HOST_VERSION)) {
599 if (esdhc_is_usdhc(imx_data)) {
601 * The usdhc register returns a wrong host version.
604 return SDHCI_SPEC_300;
608 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
609 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
610 if (val & ESDHC_VENDOR_SPEC_VSELECT)
611 ret |= SDHCI_CTRL_VDD_180;
613 if (esdhc_is_usdhc(imx_data)) {
614 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
615 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
616 else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
617 /* the std tuning bits is in ACMD12_ERR for imx6sl */
618 val = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
621 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
622 ret |= SDHCI_CTRL_EXEC_TUNING;
623 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
624 ret |= SDHCI_CTRL_TUNED_CLK;
626 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
631 if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
632 if (esdhc_is_usdhc(imx_data)) {
633 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
634 ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
636 if (m & ESDHC_MIX_CTRL_AC23EN) {
637 ret &= ~ESDHC_MIX_CTRL_AC23EN;
638 ret |= SDHCI_TRNS_AUTO_CMD23;
641 ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
647 return readw(host->ioaddr + reg);
650 static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
652 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
653 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
657 case SDHCI_CLOCK_CONTROL:
658 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
659 if (val & SDHCI_CLOCK_CARD_EN)
660 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
662 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
663 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
664 if (!(new_val & ESDHC_VENDOR_SPEC_FRC_SDCLK_ON))
665 esdhc_wait_for_card_clock_gate_off(host);
667 case SDHCI_HOST_CONTROL2:
668 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
669 if (val & SDHCI_CTRL_VDD_180)
670 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
672 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
673 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
674 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
675 u32 v = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
676 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
677 if (val & SDHCI_CTRL_TUNED_CLK) {
678 v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
680 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
681 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
682 m &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
685 if (val & SDHCI_CTRL_EXEC_TUNING) {
686 v |= ESDHC_MIX_CTRL_EXE_TUNE;
687 m |= ESDHC_MIX_CTRL_FBCLK_SEL;
688 m |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
689 usdhc_auto_tuning_mode_sel(host);
691 v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
694 writel(v, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
695 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
698 case SDHCI_TRANSFER_MODE:
699 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
700 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
701 && (host->cmd->data->blocks > 1)
702 && (host->cmd->data->flags & MMC_DATA_READ)) {
704 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
705 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
706 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
709 if (esdhc_is_usdhc(imx_data)) {
711 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
713 if (val & SDHCI_TRNS_AUTO_CMD23) {
714 val &= ~SDHCI_TRNS_AUTO_CMD23;
715 val |= ESDHC_MIX_CTRL_AC23EN;
717 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
718 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
720 /* Set watermark levels for PIO access to maximum value
721 * (128 words) to accommodate full 512 bytes buffer.
722 * For DMA access restore the levels to default value.
724 m = readl(host->ioaddr + ESDHC_WTMK_LVL);
725 if (val & SDHCI_TRNS_DMA) {
726 wml = ESDHC_WTMK_LVL_WML_VAL_DEF;
729 wml = ESDHC_WTMK_LVL_WML_VAL_MAX;
732 * Since already disable DMA mode, so also need
733 * to clear the DMASEL. Otherwise, for standard
734 * tuning, when send tuning command, usdhc will
735 * still prefetch the ADMA script from wrong
736 * DMA address, then we will see IOMMU report
737 * some error which show lack of TLB mapping.
739 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
740 ctrl &= ~SDHCI_CTRL_DMA_MASK;
741 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
743 m &= ~(ESDHC_WTMK_LVL_RD_WML_MASK |
744 ESDHC_WTMK_LVL_WR_WML_MASK);
745 m |= (wml << ESDHC_WTMK_LVL_RD_WML_SHIFT) |
746 (wml << ESDHC_WTMK_LVL_WR_WML_SHIFT);
747 writel(m, host->ioaddr + ESDHC_WTMK_LVL);
750 * Postpone this write, we must do it together with a
751 * command write that is down below.
753 imx_data->scratchpad = val;
757 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
758 val |= SDHCI_CMD_ABORTCMD;
760 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
761 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
762 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
764 if (esdhc_is_usdhc(imx_data))
766 host->ioaddr + SDHCI_TRANSFER_MODE);
768 writel(val << 16 | imx_data->scratchpad,
769 host->ioaddr + SDHCI_TRANSFER_MODE);
771 case SDHCI_BLOCK_SIZE:
772 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
775 esdhc_clrset_le(host, 0xffff, val, reg);
778 static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
784 case SDHCI_HOST_CONTROL:
785 val = readl(host->ioaddr + reg);
787 ret = val & SDHCI_CTRL_LED;
788 ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
789 ret |= (val & ESDHC_CTRL_4BITBUS);
790 ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
794 return readb(host->ioaddr + reg);
797 static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
799 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
800 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
805 case SDHCI_POWER_CONTROL:
807 * FSL put some DMA bits here
808 * If your board has a regulator, code should be here
811 case SDHCI_HOST_CONTROL:
812 /* FSL messed up here, so we need to manually compose it. */
813 new_val = val & SDHCI_CTRL_LED;
814 /* ensure the endianness */
815 new_val |= ESDHC_HOST_CONTROL_LE;
816 /* bits 8&9 are reserved on mx25 */
817 if (!is_imx25_esdhc(imx_data)) {
818 /* DMA mode bits are shifted */
819 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
823 * Do not touch buswidth bits here. This is done in
824 * esdhc_pltfm_bus_width.
825 * Do not touch the D3CD bit either which is used for the
826 * SDIO interrupt erratum workaround.
828 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
830 esdhc_clrset_le(host, mask, new_val, reg);
832 case SDHCI_SOFTWARE_RESET:
833 if (val & SDHCI_RESET_DATA)
834 new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
837 esdhc_clrset_le(host, 0xff, val, reg);
839 if (reg == SDHCI_SOFTWARE_RESET) {
840 if (val & SDHCI_RESET_ALL) {
842 * The esdhc has a design violation to SDHC spec which
843 * tells that software reset should not affect card
844 * detection circuit. But esdhc clears its SYSCTL
845 * register bits [0..2] during the software reset. This
846 * will stop those clocks that card detection circuit
847 * relies on. To work around it, we turn the clocks on
848 * back to keep card detection circuit functional.
850 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
852 * The reset on usdhc fails to clear MIX_CTRL register.
853 * Do it manually here.
855 if (esdhc_is_usdhc(imx_data)) {
857 * the tuning bits should be kept during reset
859 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
860 writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
861 host->ioaddr + ESDHC_MIX_CTRL);
862 imx_data->is_ddr = 0;
864 } else if (val & SDHCI_RESET_DATA) {
866 * The eSDHC DAT line software reset clears at least the
867 * data transfer width on i.MX25, so make sure that the
868 * Host Control register is unaffected.
870 esdhc_clrset_le(host, 0xff, new_val,
876 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
878 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
880 return pltfm_host->clock;
883 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
885 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
887 return pltfm_host->clock / 256 / 16;
890 static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
893 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
894 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
895 unsigned int host_clock = pltfm_host->clock;
896 int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
902 if (esdhc_is_usdhc(imx_data)) {
903 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
904 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
905 host->ioaddr + ESDHC_VENDOR_SPEC);
906 esdhc_wait_for_card_clock_gate_off(host);
910 host->mmc->actual_clock = 0;
914 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
915 if (is_imx53_esdhc(imx_data)) {
917 * According to the i.MX53 reference manual, if DLLCTRL[10] can
918 * be set, then the controller is eSDHCv3, else it is eSDHCv2.
920 val = readl(host->ioaddr + ESDHC_DLL_CTRL);
921 writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL);
922 temp = readl(host->ioaddr + ESDHC_DLL_CTRL);
923 writel(val, host->ioaddr + ESDHC_DLL_CTRL);
928 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
929 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
931 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
933 if (imx_data->socdata->flags & ESDHC_FLAG_ERR010450) {
934 unsigned int max_clock;
936 max_clock = imx_data->is_ddr ? 45000000 : 150000000;
938 clock = min(clock, max_clock);
941 while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
945 while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
948 host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
949 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
950 clock, host->mmc->actual_clock);
955 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
956 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
957 | (div << ESDHC_DIVIDER_SHIFT)
958 | (pre_div << ESDHC_PREDIV_SHIFT));
959 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
961 /* need to wait the bit 3 of the PRSSTAT to be set, make sure card clock is stable */
962 ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, temp,
963 (temp & ESDHC_CLOCK_STABLE), 2, 100);
964 if (ret == -ETIMEDOUT)
965 dev_warn(mmc_dev(host->mmc), "card clock still not stable in 100us!.\n");
967 if (esdhc_is_usdhc(imx_data)) {
968 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
969 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
970 host->ioaddr + ESDHC_VENDOR_SPEC);
975 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
977 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
978 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
979 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
981 switch (boarddata->wp_type) {
983 return mmc_gpio_get_ro(host->mmc);
984 case ESDHC_WP_CONTROLLER:
985 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
986 SDHCI_WRITE_PROTECT);
994 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
999 case MMC_BUS_WIDTH_8:
1000 ctrl = ESDHC_CTRL_8BITBUS;
1002 case MMC_BUS_WIDTH_4:
1003 ctrl = ESDHC_CTRL_4BITBUS;
1010 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
1011 SDHCI_HOST_CONTROL);
1014 static int usdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
1016 struct sdhci_host *host = mmc_priv(mmc);
1019 * i.MX uSDHC internally already uses a fixed optimized timing for
1020 * DDR50, normally does not require tuning for DDR50 mode.
1022 if (host->timing == MMC_TIMING_UHS_DDR50)
1025 return sdhci_execute_tuning(mmc, opcode);
1028 static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
1034 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
1037 /* IC suggest to reset USDHC before every tuning command */
1038 esdhc_clrset_le(host, 0xff, SDHCI_RESET_ALL, SDHCI_SOFTWARE_RESET);
1039 ret = readb_poll_timeout(host->ioaddr + SDHCI_SOFTWARE_RESET, sw_rst,
1040 !(sw_rst & SDHCI_RESET_ALL), 10, 100);
1041 if (ret == -ETIMEDOUT)
1042 dev_warn(mmc_dev(host->mmc),
1043 "warning! RESET_ALL never complete before sending tuning command\n");
1045 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
1046 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
1047 ESDHC_MIX_CTRL_FBCLK_SEL;
1048 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
1049 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1050 dev_dbg(mmc_dev(host->mmc),
1051 "tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
1052 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
1055 static void esdhc_post_tuning(struct sdhci_host *host)
1059 usdhc_auto_tuning_mode_sel(host);
1061 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
1062 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
1063 reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
1064 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
1067 static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
1069 int min, max, avg, ret;
1071 /* find the mininum delay first which can pass tuning */
1072 min = ESDHC_TUNE_CTRL_MIN;
1073 while (min < ESDHC_TUNE_CTRL_MAX) {
1074 esdhc_prepare_tuning(host, min);
1075 if (!mmc_send_tuning(host->mmc, opcode, NULL))
1077 min += ESDHC_TUNE_CTRL_STEP;
1080 /* find the maxinum delay which can not pass tuning */
1081 max = min + ESDHC_TUNE_CTRL_STEP;
1082 while (max < ESDHC_TUNE_CTRL_MAX) {
1083 esdhc_prepare_tuning(host, max);
1084 if (mmc_send_tuning(host->mmc, opcode, NULL)) {
1085 max -= ESDHC_TUNE_CTRL_STEP;
1088 max += ESDHC_TUNE_CTRL_STEP;
1091 /* use average delay to get the best timing */
1092 avg = (min + max) / 2;
1093 esdhc_prepare_tuning(host, avg);
1094 ret = mmc_send_tuning(host->mmc, opcode, NULL);
1095 esdhc_post_tuning(host);
1097 dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
1098 ret ? "failed" : "passed", avg, ret);
1103 static void esdhc_hs400_enhanced_strobe(struct mmc_host *mmc, struct mmc_ios *ios)
1105 struct sdhci_host *host = mmc_priv(mmc);
1108 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
1109 if (ios->enhanced_strobe)
1110 m |= ESDHC_MIX_CTRL_HS400_ES_EN;
1112 m &= ~ESDHC_MIX_CTRL_HS400_ES_EN;
1113 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1116 static int esdhc_change_pinstate(struct sdhci_host *host,
1119 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1120 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1121 struct pinctrl_state *pinctrl;
1123 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
1125 if (IS_ERR(imx_data->pinctrl) ||
1126 IS_ERR(imx_data->pins_100mhz) ||
1127 IS_ERR(imx_data->pins_200mhz))
1131 case MMC_TIMING_UHS_SDR50:
1132 case MMC_TIMING_UHS_DDR50:
1133 pinctrl = imx_data->pins_100mhz;
1135 case MMC_TIMING_UHS_SDR104:
1136 case MMC_TIMING_MMC_HS200:
1137 case MMC_TIMING_MMC_HS400:
1138 pinctrl = imx_data->pins_200mhz;
1141 /* back to default state for other legacy timing */
1142 return pinctrl_select_default_state(mmc_dev(host->mmc));
1145 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
1149 * For HS400 eMMC, there is a data_strobe line. This signal is generated
1150 * by the device and used for data output and CRC status response output
1151 * in HS400 mode. The frequency of this signal follows the frequency of
1152 * CLK generated by host. The host receives the data which is aligned to the
1153 * edge of data_strobe line. Due to the time delay between CLK line and
1154 * data_strobe line, if the delay time is larger than one clock cycle,
1155 * then CLK and data_strobe line will be misaligned, read error shows up.
1157 static void esdhc_set_strobe_dll(struct sdhci_host *host)
1159 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1160 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1165 /* disable clock before enabling strobe dll */
1166 writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
1167 ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
1168 host->ioaddr + ESDHC_VENDOR_SPEC);
1169 esdhc_wait_for_card_clock_gate_off(host);
1171 /* force a reset on strobe dll */
1172 writel(ESDHC_STROBE_DLL_CTRL_RESET,
1173 host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1174 /* clear the reset bit on strobe dll before any setting */
1175 writel(0, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1178 * enable strobe dll ctrl and adjust the delay target
1179 * for the uSDHC loopback read clock
1181 if (imx_data->boarddata.strobe_dll_delay_target)
1182 strobe_delay = imx_data->boarddata.strobe_dll_delay_target;
1184 strobe_delay = ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT;
1185 v = ESDHC_STROBE_DLL_CTRL_ENABLE |
1186 ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
1187 (strobe_delay << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
1188 writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1190 /* wait max 50us to get the REF/SLV lock */
1191 ret = readl_poll_timeout(host->ioaddr + ESDHC_STROBE_DLL_STATUS, v,
1192 ((v & ESDHC_STROBE_DLL_STS_REF_LOCK) && (v & ESDHC_STROBE_DLL_STS_SLV_LOCK)), 1, 50);
1193 if (ret == -ETIMEDOUT)
1194 dev_warn(mmc_dev(host->mmc),
1195 "warning! HS400 strobe DLL status REF/SLV not lock in 50us, STROBE DLL status is %x!\n", v);
1198 static void esdhc_reset_tuning(struct sdhci_host *host)
1200 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1201 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1205 /* Reset the tuning circuit */
1206 if (esdhc_is_usdhc(imx_data)) {
1207 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1208 ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
1209 ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1210 ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
1211 writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
1212 writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1213 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1214 ctrl = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1215 ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1216 ctrl &= ~ESDHC_MIX_CTRL_EXE_TUNE;
1217 writel(ctrl, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1218 /* Make sure ESDHC_MIX_CTRL_EXE_TUNE cleared */
1219 ret = readl_poll_timeout(host->ioaddr + SDHCI_AUTO_CMD_STATUS,
1220 ctrl, !(ctrl & ESDHC_MIX_CTRL_EXE_TUNE), 1, 50);
1221 if (ret == -ETIMEDOUT)
1222 dev_warn(mmc_dev(host->mmc),
1223 "Warning! clear execute tuning bit failed\n");
1225 * SDHCI_INT_DATA_AVAIL is W1C bit, set this bit will clear the
1226 * usdhc IP internal logic flag execute_tuning_with_clr_buf, which
1227 * will finally make sure the normal data transfer logic correct.
1229 ctrl = readl(host->ioaddr + SDHCI_INT_STATUS);
1230 ctrl |= SDHCI_INT_DATA_AVAIL;
1231 writel(ctrl, host->ioaddr + SDHCI_INT_STATUS);
1236 static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1239 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1240 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1241 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1243 /* disable ddr mode and disable HS400 mode */
1244 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
1245 m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
1246 imx_data->is_ddr = 0;
1249 case MMC_TIMING_UHS_SDR12:
1250 case MMC_TIMING_UHS_SDR25:
1251 case MMC_TIMING_UHS_SDR50:
1252 case MMC_TIMING_UHS_SDR104:
1253 case MMC_TIMING_MMC_HS:
1254 case MMC_TIMING_MMC_HS200:
1255 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1257 case MMC_TIMING_UHS_DDR50:
1258 case MMC_TIMING_MMC_DDR52:
1259 m |= ESDHC_MIX_CTRL_DDREN;
1260 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1261 imx_data->is_ddr = 1;
1262 if (boarddata->delay_line) {
1264 v = boarddata->delay_line <<
1265 ESDHC_DLL_OVERRIDE_VAL_SHIFT |
1266 (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
1267 if (is_imx53_esdhc(imx_data))
1269 writel(v, host->ioaddr + ESDHC_DLL_CTRL);
1272 case MMC_TIMING_MMC_HS400:
1273 m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
1274 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1275 imx_data->is_ddr = 1;
1276 /* update clock after enable DDR for strobe DLL lock */
1277 host->ops->set_clock(host, host->clock);
1278 esdhc_set_strobe_dll(host);
1280 case MMC_TIMING_LEGACY:
1282 esdhc_reset_tuning(host);
1286 esdhc_change_pinstate(host, timing);
1289 static void esdhc_reset(struct sdhci_host *host, u8 mask)
1291 sdhci_reset(host, mask);
1293 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1294 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1297 static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host)
1299 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1300 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1302 /* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
1303 return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
1306 static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
1308 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1309 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1311 /* use maximum timeout counter */
1312 esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
1313 esdhc_is_usdhc(imx_data) ? 0xF : 0xE,
1314 SDHCI_TIMEOUT_CONTROL);
1317 static u32 esdhc_cqhci_irq(struct sdhci_host *host, u32 intmask)
1322 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
1325 cqhci_irq(host->mmc, intmask, cmd_error, data_error);
1330 static struct sdhci_ops sdhci_esdhc_ops = {
1331 .read_l = esdhc_readl_le,
1332 .read_w = esdhc_readw_le,
1333 .read_b = esdhc_readb_le,
1334 .write_l = esdhc_writel_le,
1335 .write_w = esdhc_writew_le,
1336 .write_b = esdhc_writeb_le,
1337 .set_clock = esdhc_pltfm_set_clock,
1338 .get_max_clock = esdhc_pltfm_get_max_clock,
1339 .get_min_clock = esdhc_pltfm_get_min_clock,
1340 .get_max_timeout_count = esdhc_get_max_timeout_count,
1341 .get_ro = esdhc_pltfm_get_ro,
1342 .set_timeout = esdhc_set_timeout,
1343 .set_bus_width = esdhc_pltfm_set_bus_width,
1344 .set_uhs_signaling = esdhc_set_uhs_signaling,
1345 .reset = esdhc_reset,
1346 .irq = esdhc_cqhci_irq,
1347 .dump_vendor_regs = esdhc_dump_debug_regs,
1350 static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
1351 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
1352 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
1353 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
1354 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
1355 .ops = &sdhci_esdhc_ops,
1358 static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
1360 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1361 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1362 struct cqhci_host *cq_host = host->mmc->cqe_private;
1365 if (esdhc_is_usdhc(imx_data)) {
1367 * The imx6q ROM code will change the default watermark
1368 * level setting to something insane. Change it back here.
1370 writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
1373 * ROM code will change the bit burst_length_enable setting
1374 * to zero if this usdhc is chosen to boot system. Change
1375 * it back here, otherwise it will impact the performance a
1376 * lot. This bit is used to enable/disable the burst length
1377 * for the external AHB2AXI bridge. It's useful especially
1378 * for INCR transfer because without burst length indicator,
1379 * the AHB2AXI bridge does not know the burst length in
1380 * advance. And without burst length indicator, AHB INCR
1381 * transfer can only be converted to singles on the AXI side.
1383 writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
1384 | ESDHC_BURST_LEN_EN_INCR,
1385 host->ioaddr + SDHCI_HOST_CONTROL);
1388 * erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
1389 * TO1.1, it's harmless for MX6SL
1391 if (!(imx_data->socdata->flags & ESDHC_FLAG_SKIP_ERR004536)) {
1392 writel(readl(host->ioaddr + 0x6c) & ~BIT(7),
1393 host->ioaddr + 0x6c);
1396 /* disable DLL_CTRL delay line settings */
1397 writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
1400 * For the case of command with busy, if set the bit
1401 * ESDHC_VEND_SPEC2_EN_BUSY_IRQ, USDHC will generate a
1402 * transfer complete interrupt when busy is deasserted.
1403 * When CQHCI use DCMD to send a CMD need R1b respons,
1404 * CQHCI require to set ESDHC_VEND_SPEC2_EN_BUSY_IRQ,
1405 * otherwise DCMD will always meet timeout waiting for
1406 * hardware interrupt issue.
1408 if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1409 tmp = readl(host->ioaddr + ESDHC_VEND_SPEC2);
1410 tmp |= ESDHC_VEND_SPEC2_EN_BUSY_IRQ;
1411 writel(tmp, host->ioaddr + ESDHC_VEND_SPEC2);
1413 host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
1416 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1417 tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1418 tmp |= ESDHC_STD_TUNING_EN |
1419 ESDHC_TUNING_START_TAP_DEFAULT;
1420 if (imx_data->boarddata.tuning_start_tap) {
1421 tmp &= ~ESDHC_TUNING_START_TAP_MASK;
1422 tmp |= imx_data->boarddata.tuning_start_tap;
1425 if (imx_data->boarddata.tuning_step) {
1426 tmp &= ~ESDHC_TUNING_STEP_MASK;
1427 tmp |= imx_data->boarddata.tuning_step
1428 << ESDHC_TUNING_STEP_SHIFT;
1431 /* Disable the CMD CRC check for tuning, if not, need to
1432 * add some delay after every tuning command, because
1433 * hardware standard tuning logic will directly go to next
1434 * step once it detect the CMD CRC error, will not wait for
1435 * the card side to finally send out the tuning data, trigger
1436 * the buffer read ready interrupt immediately. If usdhc send
1437 * the next tuning command some eMMC card will stuck, can't
1438 * response, block the tuning procedure or the first command
1439 * after the whole tuning procedure always can't get any response.
1441 tmp |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
1442 writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1443 } else if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1445 * ESDHC_STD_TUNING_EN may be configed in bootloader
1446 * or ROM code, so clear this bit here to make sure
1447 * the manual tuning can work.
1449 tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1450 tmp &= ~ESDHC_STD_TUNING_EN;
1451 writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1455 * On i.MX8MM, we are running Dual Linux OS, with 1st Linux using SD Card
1456 * as rootfs storage, 2nd Linux using eMMC as rootfs storage. We let the
1457 * the 1st linux configure power/clock for the 2nd Linux.
1459 * When the 2nd Linux is booting into rootfs stage, we let the 1st Linux
1460 * to destroy the 2nd linux, then restart the 2nd linux, we met SDHCI dump.
1461 * After we clear the pending interrupt and halt CQCTL, issue gone.
1464 tmp = cqhci_readl(cq_host, CQHCI_IS);
1465 cqhci_writel(cq_host, tmp, CQHCI_IS);
1466 cqhci_writel(cq_host, CQHCI_HALT, CQHCI_CTL);
1471 static void esdhc_cqe_enable(struct mmc_host *mmc)
1473 struct sdhci_host *host = mmc_priv(mmc);
1474 struct cqhci_host *cq_host = mmc->cqe_private;
1480 * CQE gets stuck if it sees Buffer Read Enable bit set, which can be
1481 * the case after tuning, so ensure the buffer is drained.
1483 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1484 while (reg & SDHCI_DATA_AVAILABLE) {
1485 sdhci_readl(host, SDHCI_BUFFER);
1486 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1488 dev_warn(mmc_dev(host->mmc),
1489 "CQE may get stuck because the Buffer Read Enable bit is set\n");
1496 * Runtime resume will reset the entire host controller, which
1497 * will also clear the DMAEN/BCEN of register ESDHC_MIX_CTRL.
1498 * Here set DMAEN and BCEN when enable CMDQ.
1500 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
1501 if (host->flags & SDHCI_REQ_USE_DMA)
1502 mode |= SDHCI_TRNS_DMA;
1503 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
1504 mode |= SDHCI_TRNS_BLK_CNT_EN;
1505 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
1508 * Though Runtime resume reset the entire host controller,
1509 * but do not impact the CQHCI side, need to clear the
1510 * HALT bit, avoid CQHCI stuck in the first request when
1511 * system resume back.
1513 cqhci_writel(cq_host, 0, CQHCI_CTL);
1514 if (cqhci_readl(cq_host, CQHCI_CTL) && CQHCI_HALT)
1515 dev_err(mmc_dev(host->mmc),
1516 "failed to exit halt state when enable CQE\n");
1519 sdhci_cqe_enable(mmc);
1522 static void esdhc_sdhci_dumpregs(struct mmc_host *mmc)
1524 sdhci_dumpregs(mmc_priv(mmc));
1527 static const struct cqhci_host_ops esdhc_cqhci_ops = {
1528 .enable = esdhc_cqe_enable,
1529 .disable = sdhci_cqe_disable,
1530 .dumpregs = esdhc_sdhci_dumpregs,
1534 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
1535 struct sdhci_host *host,
1536 struct pltfm_imx_data *imx_data)
1538 struct device_node *np = pdev->dev.of_node;
1539 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1542 if (of_get_property(np, "fsl,wp-controller", NULL))
1543 boarddata->wp_type = ESDHC_WP_CONTROLLER;
1546 * If we have this property, then activate WP check.
1547 * Retrieveing and requesting the actual WP GPIO will happen
1548 * in the call to mmc_of_parse().
1550 if (of_property_read_bool(np, "wp-gpios"))
1551 boarddata->wp_type = ESDHC_WP_GPIO;
1553 of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
1554 of_property_read_u32(np, "fsl,tuning-start-tap",
1555 &boarddata->tuning_start_tap);
1557 of_property_read_u32(np, "fsl,strobe-dll-delay-target",
1558 &boarddata->strobe_dll_delay_target);
1559 if (of_find_property(np, "no-1-8-v", NULL))
1560 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1562 if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
1563 boarddata->delay_line = 0;
1565 mmc_of_parse_voltage(host->mmc, &host->ocr_mask);
1567 if (esdhc_is_usdhc(imx_data) && !IS_ERR(imx_data->pinctrl)) {
1568 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1569 ESDHC_PINCTRL_STATE_100MHZ);
1570 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1571 ESDHC_PINCTRL_STATE_200MHZ);
1574 /* call to generic mmc_of_parse to support additional capabilities */
1575 ret = mmc_of_parse(host->mmc);
1579 if (mmc_gpio_get_cd(host->mmc) >= 0)
1580 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1585 static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
1587 struct sdhci_pltfm_host *pltfm_host;
1588 struct sdhci_host *host;
1589 struct cqhci_host *cq_host;
1591 struct pltfm_imx_data *imx_data;
1593 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
1596 return PTR_ERR(host);
1598 pltfm_host = sdhci_priv(host);
1600 imx_data = sdhci_pltfm_priv(pltfm_host);
1602 imx_data->socdata = device_get_match_data(&pdev->dev);
1604 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1605 cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
1607 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1608 if (IS_ERR(imx_data->clk_ipg)) {
1609 err = PTR_ERR(imx_data->clk_ipg);
1613 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1614 if (IS_ERR(imx_data->clk_ahb)) {
1615 err = PTR_ERR(imx_data->clk_ahb);
1619 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
1620 if (IS_ERR(imx_data->clk_per)) {
1621 err = PTR_ERR(imx_data->clk_per);
1625 pltfm_host->clk = imx_data->clk_per;
1626 pltfm_host->clock = clk_get_rate(pltfm_host->clk);
1627 err = clk_prepare_enable(imx_data->clk_per);
1630 err = clk_prepare_enable(imx_data->clk_ipg);
1632 goto disable_per_clk;
1633 err = clk_prepare_enable(imx_data->clk_ahb);
1635 goto disable_ipg_clk;
1637 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
1638 if (IS_ERR(imx_data->pinctrl))
1639 dev_warn(mmc_dev(host->mmc), "could not get pinctrl\n");
1641 if (esdhc_is_usdhc(imx_data)) {
1642 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
1643 host->mmc->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR;
1645 /* GPIO CD can be set as a wakeup source */
1646 host->mmc->caps |= MMC_CAP_CD_WAKE;
1648 if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
1649 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
1651 /* clear tuning bits in case ROM has set it already */
1652 writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
1653 writel(0x0, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1654 writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1657 * Link usdhc specific mmc_host_ops execute_tuning function,
1658 * to replace the standard one in sdhci_ops.
1660 host->mmc_host_ops.execute_tuning = usdhc_execute_tuning;
1663 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
1664 sdhci_esdhc_ops.platform_execute_tuning =
1665 esdhc_executing_tuning;
1667 if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
1668 host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
1670 if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
1671 host->mmc->caps2 |= MMC_CAP2_HS400;
1673 if (imx_data->socdata->flags & ESDHC_FLAG_BROKEN_AUTO_CMD23)
1674 host->quirks2 |= SDHCI_QUIRK2_ACMD23_BROKEN;
1676 if (imx_data->socdata->flags & ESDHC_FLAG_HS400_ES) {
1677 host->mmc->caps2 |= MMC_CAP2_HS400_ES;
1678 host->mmc_host_ops.hs400_enhanced_strobe =
1679 esdhc_hs400_enhanced_strobe;
1682 if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1683 host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
1684 cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL);
1687 goto disable_ahb_clk;
1690 cq_host->mmio = host->ioaddr + ESDHC_CQHCI_ADDR_OFFSET;
1691 cq_host->ops = &esdhc_cqhci_ops;
1693 err = cqhci_init(cq_host, host->mmc, false);
1695 goto disable_ahb_clk;
1698 err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
1700 goto disable_ahb_clk;
1702 sdhci_esdhc_imx_hwinit(host);
1704 err = sdhci_add_host(host);
1706 goto disable_ahb_clk;
1709 * Setup the wakeup capability here, let user to decide
1710 * whether need to enable this wakeup through sysfs interface.
1712 if ((host->mmc->pm_caps & MMC_PM_KEEP_POWER) &&
1713 (host->mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ))
1714 device_set_wakeup_capable(&pdev->dev, true);
1716 pm_runtime_set_active(&pdev->dev);
1717 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1718 pm_runtime_use_autosuspend(&pdev->dev);
1719 pm_suspend_ignore_children(&pdev->dev, 1);
1720 pm_runtime_enable(&pdev->dev);
1725 clk_disable_unprepare(imx_data->clk_ahb);
1727 clk_disable_unprepare(imx_data->clk_ipg);
1729 clk_disable_unprepare(imx_data->clk_per);
1731 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1732 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1733 sdhci_pltfm_free(pdev);
1737 static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
1739 struct sdhci_host *host = platform_get_drvdata(pdev);
1740 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1741 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1744 pm_runtime_get_sync(&pdev->dev);
1745 dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1746 pm_runtime_disable(&pdev->dev);
1747 pm_runtime_put_noidle(&pdev->dev);
1749 sdhci_remove_host(host, dead);
1751 clk_disable_unprepare(imx_data->clk_per);
1752 clk_disable_unprepare(imx_data->clk_ipg);
1753 clk_disable_unprepare(imx_data->clk_ahb);
1755 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1756 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1758 sdhci_pltfm_free(pdev);
1763 #ifdef CONFIG_PM_SLEEP
1764 static int sdhci_esdhc_suspend(struct device *dev)
1766 struct sdhci_host *host = dev_get_drvdata(dev);
1767 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1768 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1771 if (host->mmc->caps2 & MMC_CAP2_CQE) {
1772 ret = cqhci_suspend(host->mmc);
1777 if ((imx_data->socdata->flags & ESDHC_FLAG_STATE_LOST_IN_LPMODE) &&
1778 (host->tuning_mode != SDHCI_TUNING_MODE_1)) {
1779 mmc_retune_timer_stop(host->mmc);
1780 mmc_retune_needed(host->mmc);
1783 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1784 mmc_retune_needed(host->mmc);
1786 ret = sdhci_suspend_host(host);
1790 ret = pinctrl_pm_select_sleep_state(dev);
1794 ret = mmc_gpio_set_cd_wake(host->mmc, true);
1799 static int sdhci_esdhc_resume(struct device *dev)
1801 struct sdhci_host *host = dev_get_drvdata(dev);
1804 ret = pinctrl_pm_select_default_state(dev);
1808 /* re-initialize hw state in case it's lost in low power mode */
1809 sdhci_esdhc_imx_hwinit(host);
1811 ret = sdhci_resume_host(host);
1815 if (host->mmc->caps2 & MMC_CAP2_CQE)
1816 ret = cqhci_resume(host->mmc);
1819 ret = mmc_gpio_set_cd_wake(host->mmc, false);
1826 static int sdhci_esdhc_runtime_suspend(struct device *dev)
1828 struct sdhci_host *host = dev_get_drvdata(dev);
1829 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1830 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1833 if (host->mmc->caps2 & MMC_CAP2_CQE) {
1834 ret = cqhci_suspend(host->mmc);
1839 ret = sdhci_runtime_suspend_host(host);
1843 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1844 mmc_retune_needed(host->mmc);
1846 imx_data->actual_clock = host->mmc->actual_clock;
1847 esdhc_pltfm_set_clock(host, 0);
1848 clk_disable_unprepare(imx_data->clk_per);
1849 clk_disable_unprepare(imx_data->clk_ipg);
1850 clk_disable_unprepare(imx_data->clk_ahb);
1852 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1853 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1858 static int sdhci_esdhc_runtime_resume(struct device *dev)
1860 struct sdhci_host *host = dev_get_drvdata(dev);
1861 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1862 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1865 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1866 cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
1868 if (imx_data->socdata->flags & ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME)
1869 clk_set_rate(imx_data->clk_per, pltfm_host->clock);
1871 err = clk_prepare_enable(imx_data->clk_ahb);
1873 goto remove_pm_qos_request;
1875 err = clk_prepare_enable(imx_data->clk_per);
1877 goto disable_ahb_clk;
1879 err = clk_prepare_enable(imx_data->clk_ipg);
1881 goto disable_per_clk;
1883 esdhc_pltfm_set_clock(host, imx_data->actual_clock);
1885 err = sdhci_runtime_resume_host(host, 0);
1887 goto disable_ipg_clk;
1889 if (host->mmc->caps2 & MMC_CAP2_CQE)
1890 err = cqhci_resume(host->mmc);
1895 clk_disable_unprepare(imx_data->clk_ipg);
1897 clk_disable_unprepare(imx_data->clk_per);
1899 clk_disable_unprepare(imx_data->clk_ahb);
1900 remove_pm_qos_request:
1901 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1902 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1907 static const struct dev_pm_ops sdhci_esdhc_pmops = {
1908 SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
1909 SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
1910 sdhci_esdhc_runtime_resume, NULL)
1913 static struct platform_driver sdhci_esdhc_imx_driver = {
1915 .name = "sdhci-esdhc-imx",
1916 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1917 .of_match_table = imx_esdhc_dt_ids,
1918 .pm = &sdhci_esdhc_pmops,
1920 .probe = sdhci_esdhc_imx_probe,
1921 .remove = sdhci_esdhc_imx_remove,
1924 module_platform_driver(sdhci_esdhc_imx_driver);
1926 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1927 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
1928 MODULE_LICENSE("GPL v2");