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.
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,
309 static struct esdhc_soc_data usdhc_imx8qxp_data = {
310 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
311 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
312 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
314 | ESDHC_FLAG_STATE_LOST_IN_LPMODE
315 | ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME,
318 static struct esdhc_soc_data usdhc_imx8mm_data = {
319 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
320 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
321 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
323 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
326 struct pltfm_imx_data {
328 struct pinctrl *pinctrl;
329 struct pinctrl_state *pins_100mhz;
330 struct pinctrl_state *pins_200mhz;
331 const struct esdhc_soc_data *socdata;
332 struct esdhc_platform_data boarddata;
336 unsigned int actual_clock;
338 NO_CMD_PENDING, /* no multiblock command pending */
339 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
340 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
343 struct pm_qos_request pm_qos_req;
346 static const struct of_device_id imx_esdhc_dt_ids[] = {
347 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
348 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
349 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
350 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
351 { .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
352 { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
353 { .compatible = "fsl,imx6sll-usdhc", .data = &usdhc_imx6sll_data, },
354 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
355 { .compatible = "fsl,imx6ull-usdhc", .data = &usdhc_imx6ull_data, },
356 { .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
357 { .compatible = "fsl,imx7ulp-usdhc", .data = &usdhc_imx7ulp_data, },
358 { .compatible = "fsl,imx8qxp-usdhc", .data = &usdhc_imx8qxp_data, },
359 { .compatible = "fsl,imx8mm-usdhc", .data = &usdhc_imx8mm_data, },
360 { .compatible = "nxp,s32g2-usdhc", .data = &usdhc_s32g2_data, },
363 MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
365 static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
367 return data->socdata == &esdhc_imx25_data;
370 static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
372 return data->socdata == &esdhc_imx53_data;
375 static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
377 return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
380 static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
382 void __iomem *base = host->ioaddr + (reg & ~0x3);
383 u32 shift = (reg & 0x3) * 8;
385 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
388 #define DRIVER_NAME "sdhci-esdhc-imx"
389 #define ESDHC_IMX_DUMP(f, x...) \
390 pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
391 static void esdhc_dump_debug_regs(struct sdhci_host *host)
394 char *debug_status[7] = {
397 "trans debug status",
401 "async fifo debug status"
404 ESDHC_IMX_DUMP("========= ESDHC IMX DEBUG STATUS DUMP =========\n");
405 for (i = 0; i < 7; i++) {
406 esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK,
407 ESDHC_DEBUG_SEL_CMD_STATE + i, ESDHC_DEBUG_SEL_REG);
408 ESDHC_IMX_DUMP("%s: 0x%04x\n", debug_status[i],
409 readw(host->ioaddr + ESDHC_DEBUG_SEL_AND_STATUS_REG));
412 esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK, 0, ESDHC_DEBUG_SEL_REG);
416 static inline void esdhc_wait_for_card_clock_gate_off(struct sdhci_host *host)
421 ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, present_state,
422 (present_state & ESDHC_CLOCK_GATE_OFF), 2, 100);
423 if (ret == -ETIMEDOUT)
424 dev_warn(mmc_dev(host->mmc), "%s: card clock still not gate off in 100us!.\n", __func__);
427 /* Enable the auto tuning circuit to check the CMD line and BUS line */
428 static inline void usdhc_auto_tuning_mode_sel(struct sdhci_host *host)
430 u32 buswidth, auto_tune_buswidth;
432 buswidth = USDHC_GET_BUSWIDTH(readl(host->ioaddr + SDHCI_HOST_CONTROL));
435 case ESDHC_CTRL_8BITBUS:
436 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN;
438 case ESDHC_CTRL_4BITBUS:
439 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN;
441 default: /* 1BITBUS */
442 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN;
446 esdhc_clrset_le(host, ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK,
447 auto_tune_buswidth | ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN,
451 static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
453 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
454 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
455 u32 val = readl(host->ioaddr + reg);
457 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
459 /* save the least 20 bits */
460 val = fsl_prss & 0x000FFFFF;
461 /* move dat[0-3] bits */
462 val |= (fsl_prss & 0x0F000000) >> 4;
463 /* move cmd line bit */
464 val |= (fsl_prss & 0x00800000) << 1;
467 if (unlikely(reg == SDHCI_CAPABILITIES)) {
468 /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
469 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
472 /* In FSL esdhc IC module, only bit20 is used to indicate the
473 * ADMA2 capability of esdhc, but this bit is messed up on
474 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
475 * don't actually support ADMA2). So set the BROKEN_ADMA
476 * quirk on MX25/35 platforms.
479 if (val & SDHCI_CAN_DO_ADMA1) {
480 val &= ~SDHCI_CAN_DO_ADMA1;
481 val |= SDHCI_CAN_DO_ADMA2;
485 if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
486 if (esdhc_is_usdhc(imx_data)) {
487 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
488 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
490 /* imx6q/dl does not have cap_1 register, fake one */
491 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
492 | SDHCI_SUPPORT_SDR50
493 | SDHCI_USE_SDR50_TUNING
494 | FIELD_PREP(SDHCI_RETUNING_MODE_MASK,
495 SDHCI_TUNING_MODE_3);
498 * Do not advertise faster UHS modes if there are no
499 * pinctrl states for 100MHz/200MHz.
501 if (IS_ERR_OR_NULL(imx_data->pins_100mhz))
502 val &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50);
503 if (IS_ERR_OR_NULL(imx_data->pins_200mhz))
504 val &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_HS400);
508 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
510 val |= FIELD_PREP(SDHCI_MAX_CURRENT_330_MASK, 0xFF);
511 val |= FIELD_PREP(SDHCI_MAX_CURRENT_300_MASK, 0xFF);
512 val |= FIELD_PREP(SDHCI_MAX_CURRENT_180_MASK, 0xFF);
515 if (unlikely(reg == SDHCI_INT_STATUS)) {
516 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
517 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
518 val |= SDHCI_INT_ADMA_ERROR;
522 * mask off the interrupt we get in response to the manually
525 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
526 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
527 val &= ~SDHCI_INT_RESPONSE;
528 writel(SDHCI_INT_RESPONSE, host->ioaddr +
530 imx_data->multiblock_status = NO_CMD_PENDING;
537 static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
539 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
540 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
543 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
544 reg == SDHCI_INT_STATUS)) {
545 if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
547 * Clear and then set D3CD bit to avoid missing the
548 * card interrupt. This is an eSDHC controller problem
549 * so we need to apply the following workaround: clear
550 * and set D3CD bit will make eSDHC re-sample the card
551 * interrupt. In case a card interrupt was lost,
552 * re-sample it by the following steps.
554 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
555 data &= ~ESDHC_CTRL_D3CD;
556 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
557 data |= ESDHC_CTRL_D3CD;
558 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
561 if (val & SDHCI_INT_ADMA_ERROR) {
562 val &= ~SDHCI_INT_ADMA_ERROR;
563 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
567 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
568 && (reg == SDHCI_INT_STATUS)
569 && (val & SDHCI_INT_DATA_END))) {
571 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
572 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
573 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
575 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
577 /* send a manual CMD12 with RESPTYP=none */
578 data = MMC_STOP_TRANSMISSION << 24 |
579 SDHCI_CMD_ABORTCMD << 16;
580 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
581 imx_data->multiblock_status = WAIT_FOR_INT;
585 writel(val, host->ioaddr + reg);
588 static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
590 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
591 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
595 if (unlikely(reg == SDHCI_HOST_VERSION)) {
597 if (esdhc_is_usdhc(imx_data)) {
599 * The usdhc register returns a wrong host version.
602 return SDHCI_SPEC_300;
606 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
607 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
608 if (val & ESDHC_VENDOR_SPEC_VSELECT)
609 ret |= SDHCI_CTRL_VDD_180;
611 if (esdhc_is_usdhc(imx_data)) {
612 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
613 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
614 else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
615 /* the std tuning bits is in ACMD12_ERR for imx6sl */
616 val = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
619 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
620 ret |= SDHCI_CTRL_EXEC_TUNING;
621 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
622 ret |= SDHCI_CTRL_TUNED_CLK;
624 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
629 if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
630 if (esdhc_is_usdhc(imx_data)) {
631 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
632 ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
634 if (m & ESDHC_MIX_CTRL_AC23EN) {
635 ret &= ~ESDHC_MIX_CTRL_AC23EN;
636 ret |= SDHCI_TRNS_AUTO_CMD23;
639 ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
645 return readw(host->ioaddr + reg);
648 static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
650 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
651 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
655 case SDHCI_CLOCK_CONTROL:
656 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
657 if (val & SDHCI_CLOCK_CARD_EN)
658 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
660 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
661 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
662 if (!(new_val & ESDHC_VENDOR_SPEC_FRC_SDCLK_ON))
663 esdhc_wait_for_card_clock_gate_off(host);
665 case SDHCI_HOST_CONTROL2:
666 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
667 if (val & SDHCI_CTRL_VDD_180)
668 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
670 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
671 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
672 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
673 u32 v = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
674 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
675 if (val & SDHCI_CTRL_TUNED_CLK) {
676 v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
678 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
679 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
680 m &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
683 if (val & SDHCI_CTRL_EXEC_TUNING) {
684 v |= ESDHC_MIX_CTRL_EXE_TUNE;
685 m |= ESDHC_MIX_CTRL_FBCLK_SEL;
686 m |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
687 usdhc_auto_tuning_mode_sel(host);
689 v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
692 writel(v, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
693 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
696 case SDHCI_TRANSFER_MODE:
697 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
698 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
699 && (host->cmd->data->blocks > 1)
700 && (host->cmd->data->flags & MMC_DATA_READ)) {
702 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
703 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
704 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
707 if (esdhc_is_usdhc(imx_data)) {
709 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
711 if (val & SDHCI_TRNS_AUTO_CMD23) {
712 val &= ~SDHCI_TRNS_AUTO_CMD23;
713 val |= ESDHC_MIX_CTRL_AC23EN;
715 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
716 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
718 /* Set watermark levels for PIO access to maximum value
719 * (128 words) to accommodate full 512 bytes buffer.
720 * For DMA access restore the levels to default value.
722 m = readl(host->ioaddr + ESDHC_WTMK_LVL);
723 if (val & SDHCI_TRNS_DMA) {
724 wml = ESDHC_WTMK_LVL_WML_VAL_DEF;
727 wml = ESDHC_WTMK_LVL_WML_VAL_MAX;
730 * Since already disable DMA mode, so also need
731 * to clear the DMASEL. Otherwise, for standard
732 * tuning, when send tuning command, usdhc will
733 * still prefetch the ADMA script from wrong
734 * DMA address, then we will see IOMMU report
735 * some error which show lack of TLB mapping.
737 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
738 ctrl &= ~SDHCI_CTRL_DMA_MASK;
739 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
741 m &= ~(ESDHC_WTMK_LVL_RD_WML_MASK |
742 ESDHC_WTMK_LVL_WR_WML_MASK);
743 m |= (wml << ESDHC_WTMK_LVL_RD_WML_SHIFT) |
744 (wml << ESDHC_WTMK_LVL_WR_WML_SHIFT);
745 writel(m, host->ioaddr + ESDHC_WTMK_LVL);
748 * Postpone this write, we must do it together with a
749 * command write that is down below.
751 imx_data->scratchpad = val;
755 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
756 val |= SDHCI_CMD_ABORTCMD;
758 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
759 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
760 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
762 if (esdhc_is_usdhc(imx_data))
764 host->ioaddr + SDHCI_TRANSFER_MODE);
766 writel(val << 16 | imx_data->scratchpad,
767 host->ioaddr + SDHCI_TRANSFER_MODE);
769 case SDHCI_BLOCK_SIZE:
770 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
773 esdhc_clrset_le(host, 0xffff, val, reg);
776 static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
782 case SDHCI_HOST_CONTROL:
783 val = readl(host->ioaddr + reg);
785 ret = val & SDHCI_CTRL_LED;
786 ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
787 ret |= (val & ESDHC_CTRL_4BITBUS);
788 ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
792 return readb(host->ioaddr + reg);
795 static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
797 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
798 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
803 case SDHCI_POWER_CONTROL:
805 * FSL put some DMA bits here
806 * If your board has a regulator, code should be here
809 case SDHCI_HOST_CONTROL:
810 /* FSL messed up here, so we need to manually compose it. */
811 new_val = val & SDHCI_CTRL_LED;
812 /* ensure the endianness */
813 new_val |= ESDHC_HOST_CONTROL_LE;
814 /* bits 8&9 are reserved on mx25 */
815 if (!is_imx25_esdhc(imx_data)) {
816 /* DMA mode bits are shifted */
817 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
821 * Do not touch buswidth bits here. This is done in
822 * esdhc_pltfm_bus_width.
823 * Do not touch the D3CD bit either which is used for the
824 * SDIO interrupt erratum workaround.
826 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
828 esdhc_clrset_le(host, mask, new_val, reg);
830 case SDHCI_SOFTWARE_RESET:
831 if (val & SDHCI_RESET_DATA)
832 new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
835 esdhc_clrset_le(host, 0xff, val, reg);
837 if (reg == SDHCI_SOFTWARE_RESET) {
838 if (val & SDHCI_RESET_ALL) {
840 * The esdhc has a design violation to SDHC spec which
841 * tells that software reset should not affect card
842 * detection circuit. But esdhc clears its SYSCTL
843 * register bits [0..2] during the software reset. This
844 * will stop those clocks that card detection circuit
845 * relies on. To work around it, we turn the clocks on
846 * back to keep card detection circuit functional.
848 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
850 * The reset on usdhc fails to clear MIX_CTRL register.
851 * Do it manually here.
853 if (esdhc_is_usdhc(imx_data)) {
855 * the tuning bits should be kept during reset
857 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
858 writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
859 host->ioaddr + ESDHC_MIX_CTRL);
860 imx_data->is_ddr = 0;
862 } else if (val & SDHCI_RESET_DATA) {
864 * The eSDHC DAT line software reset clears at least the
865 * data transfer width on i.MX25, so make sure that the
866 * Host Control register is unaffected.
868 esdhc_clrset_le(host, 0xff, new_val,
874 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
876 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
878 return pltfm_host->clock;
881 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
883 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
885 return pltfm_host->clock / 256 / 16;
888 static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
891 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
892 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
893 unsigned int host_clock = pltfm_host->clock;
894 int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
900 if (esdhc_is_usdhc(imx_data)) {
901 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
902 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
903 host->ioaddr + ESDHC_VENDOR_SPEC);
904 esdhc_wait_for_card_clock_gate_off(host);
908 host->mmc->actual_clock = 0;
912 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
913 if (is_imx53_esdhc(imx_data)) {
915 * According to the i.MX53 reference manual, if DLLCTRL[10] can
916 * be set, then the controller is eSDHCv3, else it is eSDHCv2.
918 val = readl(host->ioaddr + ESDHC_DLL_CTRL);
919 writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL);
920 temp = readl(host->ioaddr + ESDHC_DLL_CTRL);
921 writel(val, host->ioaddr + ESDHC_DLL_CTRL);
926 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
927 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
929 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
931 if (imx_data->socdata->flags & ESDHC_FLAG_ERR010450) {
932 unsigned int max_clock;
934 max_clock = imx_data->is_ddr ? 45000000 : 150000000;
936 clock = min(clock, max_clock);
939 while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
943 while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
946 host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
947 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
948 clock, host->mmc->actual_clock);
953 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
954 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
955 | (div << ESDHC_DIVIDER_SHIFT)
956 | (pre_div << ESDHC_PREDIV_SHIFT));
957 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
959 /* need to wait the bit 3 of the PRSSTAT to be set, make sure card clock is stable */
960 ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, temp,
961 (temp & ESDHC_CLOCK_STABLE), 2, 100);
962 if (ret == -ETIMEDOUT)
963 dev_warn(mmc_dev(host->mmc), "card clock still not stable in 100us!.\n");
965 if (esdhc_is_usdhc(imx_data)) {
966 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
967 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
968 host->ioaddr + ESDHC_VENDOR_SPEC);
973 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
975 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
976 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
977 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
979 switch (boarddata->wp_type) {
981 return mmc_gpio_get_ro(host->mmc);
982 case ESDHC_WP_CONTROLLER:
983 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
984 SDHCI_WRITE_PROTECT);
992 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
997 case MMC_BUS_WIDTH_8:
998 ctrl = ESDHC_CTRL_8BITBUS;
1000 case MMC_BUS_WIDTH_4:
1001 ctrl = ESDHC_CTRL_4BITBUS;
1008 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
1009 SDHCI_HOST_CONTROL);
1012 static int usdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
1014 struct sdhci_host *host = mmc_priv(mmc);
1017 * i.MX uSDHC internally already uses a fixed optimized timing for
1018 * DDR50, normally does not require tuning for DDR50 mode.
1020 if (host->timing == MMC_TIMING_UHS_DDR50)
1023 return sdhci_execute_tuning(mmc, opcode);
1026 static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
1032 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
1035 /* IC suggest to reset USDHC before every tuning command */
1036 esdhc_clrset_le(host, 0xff, SDHCI_RESET_ALL, SDHCI_SOFTWARE_RESET);
1037 ret = readb_poll_timeout(host->ioaddr + SDHCI_SOFTWARE_RESET, sw_rst,
1038 !(sw_rst & SDHCI_RESET_ALL), 10, 100);
1039 if (ret == -ETIMEDOUT)
1040 dev_warn(mmc_dev(host->mmc),
1041 "warning! RESET_ALL never complete before sending tuning command\n");
1043 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
1044 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
1045 ESDHC_MIX_CTRL_FBCLK_SEL;
1046 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
1047 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1048 dev_dbg(mmc_dev(host->mmc),
1049 "tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
1050 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
1053 static void esdhc_post_tuning(struct sdhci_host *host)
1057 usdhc_auto_tuning_mode_sel(host);
1059 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
1060 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
1061 reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
1062 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
1065 static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
1067 int min, max, avg, ret;
1069 /* find the mininum delay first which can pass tuning */
1070 min = ESDHC_TUNE_CTRL_MIN;
1071 while (min < ESDHC_TUNE_CTRL_MAX) {
1072 esdhc_prepare_tuning(host, min);
1073 if (!mmc_send_tuning(host->mmc, opcode, NULL))
1075 min += ESDHC_TUNE_CTRL_STEP;
1078 /* find the maxinum delay which can not pass tuning */
1079 max = min + ESDHC_TUNE_CTRL_STEP;
1080 while (max < ESDHC_TUNE_CTRL_MAX) {
1081 esdhc_prepare_tuning(host, max);
1082 if (mmc_send_tuning(host->mmc, opcode, NULL)) {
1083 max -= ESDHC_TUNE_CTRL_STEP;
1086 max += ESDHC_TUNE_CTRL_STEP;
1089 /* use average delay to get the best timing */
1090 avg = (min + max) / 2;
1091 esdhc_prepare_tuning(host, avg);
1092 ret = mmc_send_tuning(host->mmc, opcode, NULL);
1093 esdhc_post_tuning(host);
1095 dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
1096 ret ? "failed" : "passed", avg, ret);
1101 static void esdhc_hs400_enhanced_strobe(struct mmc_host *mmc, struct mmc_ios *ios)
1103 struct sdhci_host *host = mmc_priv(mmc);
1106 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
1107 if (ios->enhanced_strobe)
1108 m |= ESDHC_MIX_CTRL_HS400_ES_EN;
1110 m &= ~ESDHC_MIX_CTRL_HS400_ES_EN;
1111 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1114 static int esdhc_change_pinstate(struct sdhci_host *host,
1117 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1118 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1119 struct pinctrl_state *pinctrl;
1121 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
1123 if (IS_ERR(imx_data->pinctrl) ||
1124 IS_ERR(imx_data->pins_100mhz) ||
1125 IS_ERR(imx_data->pins_200mhz))
1129 case MMC_TIMING_UHS_SDR50:
1130 case MMC_TIMING_UHS_DDR50:
1131 pinctrl = imx_data->pins_100mhz;
1133 case MMC_TIMING_UHS_SDR104:
1134 case MMC_TIMING_MMC_HS200:
1135 case MMC_TIMING_MMC_HS400:
1136 pinctrl = imx_data->pins_200mhz;
1139 /* back to default state for other legacy timing */
1140 return pinctrl_select_default_state(mmc_dev(host->mmc));
1143 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
1147 * For HS400 eMMC, there is a data_strobe line. This signal is generated
1148 * by the device and used for data output and CRC status response output
1149 * in HS400 mode. The frequency of this signal follows the frequency of
1150 * CLK generated by host. The host receives the data which is aligned to the
1151 * edge of data_strobe line. Due to the time delay between CLK line and
1152 * data_strobe line, if the delay time is larger than one clock cycle,
1153 * then CLK and data_strobe line will be misaligned, read error shows up.
1155 static void esdhc_set_strobe_dll(struct sdhci_host *host)
1157 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1158 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1163 /* disable clock before enabling strobe dll */
1164 writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
1165 ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
1166 host->ioaddr + ESDHC_VENDOR_SPEC);
1167 esdhc_wait_for_card_clock_gate_off(host);
1169 /* force a reset on strobe dll */
1170 writel(ESDHC_STROBE_DLL_CTRL_RESET,
1171 host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1172 /* clear the reset bit on strobe dll before any setting */
1173 writel(0, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1176 * enable strobe dll ctrl and adjust the delay target
1177 * for the uSDHC loopback read clock
1179 if (imx_data->boarddata.strobe_dll_delay_target)
1180 strobe_delay = imx_data->boarddata.strobe_dll_delay_target;
1182 strobe_delay = ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT;
1183 v = ESDHC_STROBE_DLL_CTRL_ENABLE |
1184 ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
1185 (strobe_delay << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
1186 writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1188 /* wait max 50us to get the REF/SLV lock */
1189 ret = readl_poll_timeout(host->ioaddr + ESDHC_STROBE_DLL_STATUS, v,
1190 ((v & ESDHC_STROBE_DLL_STS_REF_LOCK) && (v & ESDHC_STROBE_DLL_STS_SLV_LOCK)), 1, 50);
1191 if (ret == -ETIMEDOUT)
1192 dev_warn(mmc_dev(host->mmc),
1193 "warning! HS400 strobe DLL status REF/SLV not lock in 50us, STROBE DLL status is %x!\n", v);
1196 static void esdhc_reset_tuning(struct sdhci_host *host)
1198 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1199 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1203 /* Reset the tuning circuit */
1204 if (esdhc_is_usdhc(imx_data)) {
1205 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1206 ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
1207 ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1208 ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
1209 writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
1210 writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1211 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1212 ctrl = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1213 ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1214 ctrl &= ~ESDHC_MIX_CTRL_EXE_TUNE;
1215 writel(ctrl, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1216 /* Make sure ESDHC_MIX_CTRL_EXE_TUNE cleared */
1217 ret = readl_poll_timeout(host->ioaddr + SDHCI_AUTO_CMD_STATUS,
1218 ctrl, !(ctrl & ESDHC_MIX_CTRL_EXE_TUNE), 1, 50);
1219 if (ret == -ETIMEDOUT)
1220 dev_warn(mmc_dev(host->mmc),
1221 "Warning! clear execute tuning bit failed\n");
1223 * SDHCI_INT_DATA_AVAIL is W1C bit, set this bit will clear the
1224 * usdhc IP internal logic flag execute_tuning_with_clr_buf, which
1225 * will finally make sure the normal data transfer logic correct.
1227 ctrl = readl(host->ioaddr + SDHCI_INT_STATUS);
1228 ctrl |= SDHCI_INT_DATA_AVAIL;
1229 writel(ctrl, host->ioaddr + SDHCI_INT_STATUS);
1234 static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1237 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1238 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1239 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1241 /* disable ddr mode and disable HS400 mode */
1242 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
1243 m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
1244 imx_data->is_ddr = 0;
1247 case MMC_TIMING_UHS_SDR12:
1248 case MMC_TIMING_UHS_SDR25:
1249 case MMC_TIMING_UHS_SDR50:
1250 case MMC_TIMING_UHS_SDR104:
1251 case MMC_TIMING_MMC_HS:
1252 case MMC_TIMING_MMC_HS200:
1253 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1255 case MMC_TIMING_UHS_DDR50:
1256 case MMC_TIMING_MMC_DDR52:
1257 m |= ESDHC_MIX_CTRL_DDREN;
1258 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1259 imx_data->is_ddr = 1;
1260 if (boarddata->delay_line) {
1262 v = boarddata->delay_line <<
1263 ESDHC_DLL_OVERRIDE_VAL_SHIFT |
1264 (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
1265 if (is_imx53_esdhc(imx_data))
1267 writel(v, host->ioaddr + ESDHC_DLL_CTRL);
1270 case MMC_TIMING_MMC_HS400:
1271 m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
1272 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1273 imx_data->is_ddr = 1;
1274 /* update clock after enable DDR for strobe DLL lock */
1275 host->ops->set_clock(host, host->clock);
1276 esdhc_set_strobe_dll(host);
1278 case MMC_TIMING_LEGACY:
1280 esdhc_reset_tuning(host);
1284 esdhc_change_pinstate(host, timing);
1287 static void esdhc_reset(struct sdhci_host *host, u8 mask)
1289 sdhci_reset(host, mask);
1291 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1292 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1295 static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host)
1297 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1298 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1300 /* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
1301 return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
1304 static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
1306 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1307 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1309 /* use maximum timeout counter */
1310 esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
1311 esdhc_is_usdhc(imx_data) ? 0xF : 0xE,
1312 SDHCI_TIMEOUT_CONTROL);
1315 static u32 esdhc_cqhci_irq(struct sdhci_host *host, u32 intmask)
1320 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
1323 cqhci_irq(host->mmc, intmask, cmd_error, data_error);
1328 static struct sdhci_ops sdhci_esdhc_ops = {
1329 .read_l = esdhc_readl_le,
1330 .read_w = esdhc_readw_le,
1331 .read_b = esdhc_readb_le,
1332 .write_l = esdhc_writel_le,
1333 .write_w = esdhc_writew_le,
1334 .write_b = esdhc_writeb_le,
1335 .set_clock = esdhc_pltfm_set_clock,
1336 .get_max_clock = esdhc_pltfm_get_max_clock,
1337 .get_min_clock = esdhc_pltfm_get_min_clock,
1338 .get_max_timeout_count = esdhc_get_max_timeout_count,
1339 .get_ro = esdhc_pltfm_get_ro,
1340 .set_timeout = esdhc_set_timeout,
1341 .set_bus_width = esdhc_pltfm_set_bus_width,
1342 .set_uhs_signaling = esdhc_set_uhs_signaling,
1343 .reset = esdhc_reset,
1344 .irq = esdhc_cqhci_irq,
1345 .dump_vendor_regs = esdhc_dump_debug_regs,
1348 static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
1349 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
1350 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
1351 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
1352 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
1353 .ops = &sdhci_esdhc_ops,
1356 static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
1358 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1359 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1360 struct cqhci_host *cq_host = host->mmc->cqe_private;
1363 if (esdhc_is_usdhc(imx_data)) {
1365 * The imx6q ROM code will change the default watermark
1366 * level setting to something insane. Change it back here.
1368 writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
1371 * ROM code will change the bit burst_length_enable setting
1372 * to zero if this usdhc is chosen to boot system. Change
1373 * it back here, otherwise it will impact the performance a
1374 * lot. This bit is used to enable/disable the burst length
1375 * for the external AHB2AXI bridge. It's useful especially
1376 * for INCR transfer because without burst length indicator,
1377 * the AHB2AXI bridge does not know the burst length in
1378 * advance. And without burst length indicator, AHB INCR
1379 * transfer can only be converted to singles on the AXI side.
1381 writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
1382 | ESDHC_BURST_LEN_EN_INCR,
1383 host->ioaddr + SDHCI_HOST_CONTROL);
1386 * erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
1387 * TO1.1, it's harmless for MX6SL
1389 if (!(imx_data->socdata->flags & ESDHC_FLAG_SKIP_ERR004536)) {
1390 writel(readl(host->ioaddr + 0x6c) & ~BIT(7),
1391 host->ioaddr + 0x6c);
1394 /* disable DLL_CTRL delay line settings */
1395 writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
1398 * For the case of command with busy, if set the bit
1399 * ESDHC_VEND_SPEC2_EN_BUSY_IRQ, USDHC will generate a
1400 * transfer complete interrupt when busy is deasserted.
1401 * When CQHCI use DCMD to send a CMD need R1b respons,
1402 * CQHCI require to set ESDHC_VEND_SPEC2_EN_BUSY_IRQ,
1403 * otherwise DCMD will always meet timeout waiting for
1404 * hardware interrupt issue.
1406 if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1407 tmp = readl(host->ioaddr + ESDHC_VEND_SPEC2);
1408 tmp |= ESDHC_VEND_SPEC2_EN_BUSY_IRQ;
1409 writel(tmp, host->ioaddr + ESDHC_VEND_SPEC2);
1411 host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
1414 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1415 tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1416 tmp |= ESDHC_STD_TUNING_EN |
1417 ESDHC_TUNING_START_TAP_DEFAULT;
1418 if (imx_data->boarddata.tuning_start_tap) {
1419 tmp &= ~ESDHC_TUNING_START_TAP_MASK;
1420 tmp |= imx_data->boarddata.tuning_start_tap;
1423 if (imx_data->boarddata.tuning_step) {
1424 tmp &= ~ESDHC_TUNING_STEP_MASK;
1425 tmp |= imx_data->boarddata.tuning_step
1426 << ESDHC_TUNING_STEP_SHIFT;
1429 /* Disable the CMD CRC check for tuning, if not, need to
1430 * add some delay after every tuning command, because
1431 * hardware standard tuning logic will directly go to next
1432 * step once it detect the CMD CRC error, will not wait for
1433 * the card side to finally send out the tuning data, trigger
1434 * the buffer read ready interrupt immediately. If usdhc send
1435 * the next tuning command some eMMC card will stuck, can't
1436 * response, block the tuning procedure or the first command
1437 * after the whole tuning procedure always can't get any response.
1439 tmp |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
1440 writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1441 } else if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1443 * ESDHC_STD_TUNING_EN may be configed in bootloader
1444 * or ROM code, so clear this bit here to make sure
1445 * the manual tuning can work.
1447 tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1448 tmp &= ~ESDHC_STD_TUNING_EN;
1449 writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1453 * On i.MX8MM, we are running Dual Linux OS, with 1st Linux using SD Card
1454 * as rootfs storage, 2nd Linux using eMMC as rootfs storage. We let the
1455 * the 1st linux configure power/clock for the 2nd Linux.
1457 * When the 2nd Linux is booting into rootfs stage, we let the 1st Linux
1458 * to destroy the 2nd linux, then restart the 2nd linux, we met SDHCI dump.
1459 * After we clear the pending interrupt and halt CQCTL, issue gone.
1462 tmp = cqhci_readl(cq_host, CQHCI_IS);
1463 cqhci_writel(cq_host, tmp, CQHCI_IS);
1464 cqhci_writel(cq_host, CQHCI_HALT, CQHCI_CTL);
1469 static void esdhc_cqe_enable(struct mmc_host *mmc)
1471 struct sdhci_host *host = mmc_priv(mmc);
1472 struct cqhci_host *cq_host = mmc->cqe_private;
1478 * CQE gets stuck if it sees Buffer Read Enable bit set, which can be
1479 * the case after tuning, so ensure the buffer is drained.
1481 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1482 while (reg & SDHCI_DATA_AVAILABLE) {
1483 sdhci_readl(host, SDHCI_BUFFER);
1484 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1486 dev_warn(mmc_dev(host->mmc),
1487 "CQE may get stuck because the Buffer Read Enable bit is set\n");
1494 * Runtime resume will reset the entire host controller, which
1495 * will also clear the DMAEN/BCEN of register ESDHC_MIX_CTRL.
1496 * Here set DMAEN and BCEN when enable CMDQ.
1498 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
1499 if (host->flags & SDHCI_REQ_USE_DMA)
1500 mode |= SDHCI_TRNS_DMA;
1501 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
1502 mode |= SDHCI_TRNS_BLK_CNT_EN;
1503 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
1506 * Though Runtime resume reset the entire host controller,
1507 * but do not impact the CQHCI side, need to clear the
1508 * HALT bit, avoid CQHCI stuck in the first request when
1509 * system resume back.
1511 cqhci_writel(cq_host, 0, CQHCI_CTL);
1512 if (cqhci_readl(cq_host, CQHCI_CTL) && CQHCI_HALT)
1513 dev_err(mmc_dev(host->mmc),
1514 "failed to exit halt state when enable CQE\n");
1517 sdhci_cqe_enable(mmc);
1520 static void esdhc_sdhci_dumpregs(struct mmc_host *mmc)
1522 sdhci_dumpregs(mmc_priv(mmc));
1525 static const struct cqhci_host_ops esdhc_cqhci_ops = {
1526 .enable = esdhc_cqe_enable,
1527 .disable = sdhci_cqe_disable,
1528 .dumpregs = esdhc_sdhci_dumpregs,
1532 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
1533 struct sdhci_host *host,
1534 struct pltfm_imx_data *imx_data)
1536 struct device_node *np = pdev->dev.of_node;
1537 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1540 if (of_get_property(np, "fsl,wp-controller", NULL))
1541 boarddata->wp_type = ESDHC_WP_CONTROLLER;
1544 * If we have this property, then activate WP check.
1545 * Retrieveing and requesting the actual WP GPIO will happen
1546 * in the call to mmc_of_parse().
1548 if (of_property_read_bool(np, "wp-gpios"))
1549 boarddata->wp_type = ESDHC_WP_GPIO;
1551 of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
1552 of_property_read_u32(np, "fsl,tuning-start-tap",
1553 &boarddata->tuning_start_tap);
1555 of_property_read_u32(np, "fsl,strobe-dll-delay-target",
1556 &boarddata->strobe_dll_delay_target);
1557 if (of_find_property(np, "no-1-8-v", NULL))
1558 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1560 if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
1561 boarddata->delay_line = 0;
1563 mmc_of_parse_voltage(host->mmc, &host->ocr_mask);
1565 if (esdhc_is_usdhc(imx_data) && !IS_ERR(imx_data->pinctrl)) {
1566 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1567 ESDHC_PINCTRL_STATE_100MHZ);
1568 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1569 ESDHC_PINCTRL_STATE_200MHZ);
1572 /* call to generic mmc_of_parse to support additional capabilities */
1573 ret = mmc_of_parse(host->mmc);
1577 if (mmc_gpio_get_cd(host->mmc) >= 0)
1578 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1583 static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
1585 struct sdhci_pltfm_host *pltfm_host;
1586 struct sdhci_host *host;
1587 struct cqhci_host *cq_host;
1589 struct pltfm_imx_data *imx_data;
1591 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
1594 return PTR_ERR(host);
1596 pltfm_host = sdhci_priv(host);
1598 imx_data = sdhci_pltfm_priv(pltfm_host);
1600 imx_data->socdata = device_get_match_data(&pdev->dev);
1602 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1603 cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
1605 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1606 if (IS_ERR(imx_data->clk_ipg)) {
1607 err = PTR_ERR(imx_data->clk_ipg);
1611 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1612 if (IS_ERR(imx_data->clk_ahb)) {
1613 err = PTR_ERR(imx_data->clk_ahb);
1617 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
1618 if (IS_ERR(imx_data->clk_per)) {
1619 err = PTR_ERR(imx_data->clk_per);
1623 pltfm_host->clk = imx_data->clk_per;
1624 pltfm_host->clock = clk_get_rate(pltfm_host->clk);
1625 err = clk_prepare_enable(imx_data->clk_per);
1628 err = clk_prepare_enable(imx_data->clk_ipg);
1630 goto disable_per_clk;
1631 err = clk_prepare_enable(imx_data->clk_ahb);
1633 goto disable_ipg_clk;
1635 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
1636 if (IS_ERR(imx_data->pinctrl))
1637 dev_warn(mmc_dev(host->mmc), "could not get pinctrl\n");
1639 if (esdhc_is_usdhc(imx_data)) {
1640 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
1641 host->mmc->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR;
1643 /* GPIO CD can be set as a wakeup source */
1644 host->mmc->caps |= MMC_CAP_CD_WAKE;
1646 if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
1647 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
1649 /* clear tuning bits in case ROM has set it already */
1650 writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
1651 writel(0x0, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1652 writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1655 * Link usdhc specific mmc_host_ops execute_tuning function,
1656 * to replace the standard one in sdhci_ops.
1658 host->mmc_host_ops.execute_tuning = usdhc_execute_tuning;
1661 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
1662 sdhci_esdhc_ops.platform_execute_tuning =
1663 esdhc_executing_tuning;
1665 if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
1666 host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
1668 if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
1669 host->mmc->caps2 |= MMC_CAP2_HS400;
1671 if (imx_data->socdata->flags & ESDHC_FLAG_BROKEN_AUTO_CMD23)
1672 host->quirks2 |= SDHCI_QUIRK2_ACMD23_BROKEN;
1674 if (imx_data->socdata->flags & ESDHC_FLAG_HS400_ES) {
1675 host->mmc->caps2 |= MMC_CAP2_HS400_ES;
1676 host->mmc_host_ops.hs400_enhanced_strobe =
1677 esdhc_hs400_enhanced_strobe;
1680 if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1681 host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
1682 cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL);
1685 goto disable_ahb_clk;
1688 cq_host->mmio = host->ioaddr + ESDHC_CQHCI_ADDR_OFFSET;
1689 cq_host->ops = &esdhc_cqhci_ops;
1691 err = cqhci_init(cq_host, host->mmc, false);
1693 goto disable_ahb_clk;
1696 err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
1698 goto disable_ahb_clk;
1700 sdhci_esdhc_imx_hwinit(host);
1702 err = sdhci_add_host(host);
1704 goto disable_ahb_clk;
1707 * Setup the wakeup capability here, let user to decide
1708 * whether need to enable this wakeup through sysfs interface.
1710 if ((host->mmc->pm_caps & MMC_PM_KEEP_POWER) &&
1711 (host->mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ))
1712 device_set_wakeup_capable(&pdev->dev, true);
1714 pm_runtime_set_active(&pdev->dev);
1715 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1716 pm_runtime_use_autosuspend(&pdev->dev);
1717 pm_suspend_ignore_children(&pdev->dev, 1);
1718 pm_runtime_enable(&pdev->dev);
1723 clk_disable_unprepare(imx_data->clk_ahb);
1725 clk_disable_unprepare(imx_data->clk_ipg);
1727 clk_disable_unprepare(imx_data->clk_per);
1729 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1730 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1731 sdhci_pltfm_free(pdev);
1735 static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
1737 struct sdhci_host *host = platform_get_drvdata(pdev);
1738 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1739 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1742 pm_runtime_get_sync(&pdev->dev);
1743 dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1744 pm_runtime_disable(&pdev->dev);
1745 pm_runtime_put_noidle(&pdev->dev);
1747 sdhci_remove_host(host, dead);
1749 clk_disable_unprepare(imx_data->clk_per);
1750 clk_disable_unprepare(imx_data->clk_ipg);
1751 clk_disable_unprepare(imx_data->clk_ahb);
1753 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1754 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1756 sdhci_pltfm_free(pdev);
1761 #ifdef CONFIG_PM_SLEEP
1762 static int sdhci_esdhc_suspend(struct device *dev)
1764 struct sdhci_host *host = dev_get_drvdata(dev);
1765 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1766 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1769 if (host->mmc->caps2 & MMC_CAP2_CQE) {
1770 ret = cqhci_suspend(host->mmc);
1775 if ((imx_data->socdata->flags & ESDHC_FLAG_STATE_LOST_IN_LPMODE) &&
1776 (host->tuning_mode != SDHCI_TUNING_MODE_1)) {
1777 mmc_retune_timer_stop(host->mmc);
1778 mmc_retune_needed(host->mmc);
1781 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1782 mmc_retune_needed(host->mmc);
1784 ret = sdhci_suspend_host(host);
1788 ret = pinctrl_pm_select_sleep_state(dev);
1792 ret = mmc_gpio_set_cd_wake(host->mmc, true);
1797 static int sdhci_esdhc_resume(struct device *dev)
1799 struct sdhci_host *host = dev_get_drvdata(dev);
1802 ret = pinctrl_pm_select_default_state(dev);
1806 /* re-initialize hw state in case it's lost in low power mode */
1807 sdhci_esdhc_imx_hwinit(host);
1809 ret = sdhci_resume_host(host);
1813 if (host->mmc->caps2 & MMC_CAP2_CQE)
1814 ret = cqhci_resume(host->mmc);
1817 ret = mmc_gpio_set_cd_wake(host->mmc, false);
1824 static int sdhci_esdhc_runtime_suspend(struct device *dev)
1826 struct sdhci_host *host = dev_get_drvdata(dev);
1827 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1828 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1831 if (host->mmc->caps2 & MMC_CAP2_CQE) {
1832 ret = cqhci_suspend(host->mmc);
1837 ret = sdhci_runtime_suspend_host(host);
1841 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1842 mmc_retune_needed(host->mmc);
1844 imx_data->actual_clock = host->mmc->actual_clock;
1845 esdhc_pltfm_set_clock(host, 0);
1846 clk_disable_unprepare(imx_data->clk_per);
1847 clk_disable_unprepare(imx_data->clk_ipg);
1848 clk_disable_unprepare(imx_data->clk_ahb);
1850 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1851 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1856 static int sdhci_esdhc_runtime_resume(struct device *dev)
1858 struct sdhci_host *host = dev_get_drvdata(dev);
1859 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1860 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1863 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1864 cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
1866 if (imx_data->socdata->flags & ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME)
1867 clk_set_rate(imx_data->clk_per, pltfm_host->clock);
1869 err = clk_prepare_enable(imx_data->clk_ahb);
1871 goto remove_pm_qos_request;
1873 err = clk_prepare_enable(imx_data->clk_per);
1875 goto disable_ahb_clk;
1877 err = clk_prepare_enable(imx_data->clk_ipg);
1879 goto disable_per_clk;
1881 esdhc_pltfm_set_clock(host, imx_data->actual_clock);
1883 err = sdhci_runtime_resume_host(host, 0);
1885 goto disable_ipg_clk;
1887 if (host->mmc->caps2 & MMC_CAP2_CQE)
1888 err = cqhci_resume(host->mmc);
1893 clk_disable_unprepare(imx_data->clk_ipg);
1895 clk_disable_unprepare(imx_data->clk_per);
1897 clk_disable_unprepare(imx_data->clk_ahb);
1898 remove_pm_qos_request:
1899 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1900 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1905 static const struct dev_pm_ops sdhci_esdhc_pmops = {
1906 SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
1907 SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
1908 sdhci_esdhc_runtime_resume, NULL)
1911 static struct platform_driver sdhci_esdhc_imx_driver = {
1913 .name = "sdhci-esdhc-imx",
1914 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1915 .of_match_table = imx_esdhc_dt_ids,
1916 .pm = &sdhci_esdhc_pmops,
1918 .probe = sdhci_esdhc_imx_probe,
1919 .remove = sdhci_esdhc_imx_remove,
1922 module_platform_driver(sdhci_esdhc_imx_driver);
1924 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1926 MODULE_LICENSE("GPL v2");