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/platform_device.h>
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/pm_runtime.h>
28 #include "sdhci-cqhci.h"
29 #include "sdhci-pltfm.h"
30 #include "sdhci-esdhc.h"
33 #define ESDHC_SYS_CTRL_DTOCV_MASK GENMASK(19, 16)
34 #define ESDHC_SYS_CTRL_IPP_RST_N BIT(23)
35 #define ESDHC_CTRL_D3CD 0x08
36 #define ESDHC_BURST_LEN_EN_INCR (1 << 27)
37 /* VENDOR SPEC register */
38 #define ESDHC_VENDOR_SPEC 0xc0
39 #define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
40 #define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
41 #define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
42 #define ESDHC_DEBUG_SEL_AND_STATUS_REG 0xc2
43 #define ESDHC_DEBUG_SEL_REG 0xc3
44 #define ESDHC_DEBUG_SEL_MASK 0xf
45 #define ESDHC_DEBUG_SEL_CMD_STATE 1
46 #define ESDHC_DEBUG_SEL_DATA_STATE 2
47 #define ESDHC_DEBUG_SEL_TRANS_STATE 3
48 #define ESDHC_DEBUG_SEL_DMA_STATE 4
49 #define ESDHC_DEBUG_SEL_ADMA_STATE 5
50 #define ESDHC_DEBUG_SEL_FIFO_STATE 6
51 #define ESDHC_DEBUG_SEL_ASYNC_FIFO_STATE 7
52 #define ESDHC_WTMK_LVL 0x44
53 #define ESDHC_WTMK_DEFAULT_VAL 0x10401040
54 #define ESDHC_WTMK_LVL_RD_WML_MASK 0x000000FF
55 #define ESDHC_WTMK_LVL_RD_WML_SHIFT 0
56 #define ESDHC_WTMK_LVL_WR_WML_MASK 0x00FF0000
57 #define ESDHC_WTMK_LVL_WR_WML_SHIFT 16
58 #define ESDHC_WTMK_LVL_WML_VAL_DEF 64
59 #define ESDHC_WTMK_LVL_WML_VAL_MAX 128
60 #define ESDHC_MIX_CTRL 0x48
61 #define ESDHC_MIX_CTRL_DDREN (1 << 3)
62 #define ESDHC_MIX_CTRL_AC23EN (1 << 7)
63 #define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
64 #define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
65 #define ESDHC_MIX_CTRL_AUTO_TUNE_EN (1 << 24)
66 #define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
67 #define ESDHC_MIX_CTRL_HS400_EN (1 << 26)
68 #define ESDHC_MIX_CTRL_HS400_ES_EN (1 << 27)
69 /* Bits 3 and 6 are not SDHCI standard definitions */
70 #define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
72 #define ESDHC_MIX_CTRL_TUNING_MASK 0x03c00000
74 /* dll control register */
75 #define ESDHC_DLL_CTRL 0x60
76 #define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9
77 #define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
79 /* tune control register */
80 #define ESDHC_TUNE_CTRL_STATUS 0x68
81 #define ESDHC_TUNE_CTRL_STEP 1
82 #define ESDHC_TUNE_CTRL_MIN 0
83 #define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
85 /* strobe dll register */
86 #define ESDHC_STROBE_DLL_CTRL 0x70
87 #define ESDHC_STROBE_DLL_CTRL_ENABLE (1 << 0)
88 #define ESDHC_STROBE_DLL_CTRL_RESET (1 << 1)
89 #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT 0x7
90 #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT 3
91 #define ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT (4 << 20)
93 #define ESDHC_STROBE_DLL_STATUS 0x74
94 #define ESDHC_STROBE_DLL_STS_REF_LOCK (1 << 1)
95 #define ESDHC_STROBE_DLL_STS_SLV_LOCK 0x1
97 #define ESDHC_VEND_SPEC2 0xc8
98 #define ESDHC_VEND_SPEC2_EN_BUSY_IRQ (1 << 8)
99 #define ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN (1 << 4)
100 #define ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN (0 << 4)
101 #define ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN (2 << 4)
102 #define ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN (1 << 6)
103 #define ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK (7 << 4)
105 #define ESDHC_TUNING_CTRL 0xcc
106 #define ESDHC_STD_TUNING_EN (1 << 24)
107 /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
108 #define ESDHC_TUNING_START_TAP_DEFAULT 0x1
109 #define ESDHC_TUNING_START_TAP_MASK 0x7f
110 #define ESDHC_TUNING_CMD_CRC_CHECK_DISABLE (1 << 7)
111 #define ESDHC_TUNING_STEP_DEFAULT 0x1
112 #define ESDHC_TUNING_STEP_MASK 0x00070000
113 #define ESDHC_TUNING_STEP_SHIFT 16
116 #define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
117 #define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
120 * Our interpretation of the SDHCI_HOST_CONTROL register
122 #define ESDHC_CTRL_4BITBUS (0x1 << 1)
123 #define ESDHC_CTRL_8BITBUS (0x2 << 1)
124 #define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
125 #define USDHC_GET_BUSWIDTH(c) (c & ESDHC_CTRL_BUSWIDTH_MASK)
128 * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC:
129 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
130 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
131 * Define this macro DMA error INT for fsl eSDHC
133 #define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
135 /* the address offset of CQHCI */
136 #define ESDHC_CQHCI_ADDR_OFFSET 0x100
139 * The CMDTYPE of the CMD register (offset 0xE) should be set to
140 * "11" when the STOP CMD12 is issued on imx53 to abort one
141 * open ended multi-blk IO. Otherwise the TC INT wouldn't
143 * In exact block transfer, the controller doesn't complete the
144 * operations automatically as required at the end of the
145 * transfer and remains on hold if the abort command is not sent.
146 * As a result, the TC flag is not asserted and SW received timeout
147 * exception. Bit1 of Vendor Spec register is used to fix it.
149 #define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
151 * The flag tells that the ESDHC controller is an USDHC block that is
152 * integrated on the i.MX6 series.
154 #define ESDHC_FLAG_USDHC BIT(3)
155 /* The IP supports manual tuning process */
156 #define ESDHC_FLAG_MAN_TUNING BIT(4)
157 /* The IP supports standard tuning process */
158 #define ESDHC_FLAG_STD_TUNING BIT(5)
159 /* The IP has SDHCI_CAPABILITIES_1 register */
160 #define ESDHC_FLAG_HAVE_CAP1 BIT(6)
162 * The IP has erratum ERR004536
163 * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow,
164 * when reading data from the card
165 * This flag is also set for i.MX25 and i.MX35 in order to get
166 * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits).
168 #define ESDHC_FLAG_ERR004536 BIT(7)
169 /* The IP supports HS200 mode */
170 #define ESDHC_FLAG_HS200 BIT(8)
171 /* The IP supports HS400 mode */
172 #define ESDHC_FLAG_HS400 BIT(9)
174 * The IP has errata ERR010450
175 * uSDHC: At 1.8V due to the I/O timing limit, for SDR mode, SD card
176 * clock can't exceed 150MHz, for DDR mode, SD card clock can't exceed 45MHz.
178 #define ESDHC_FLAG_ERR010450 BIT(10)
179 /* The IP supports HS400ES mode */
180 #define ESDHC_FLAG_HS400_ES BIT(11)
181 /* The IP has Host Controller Interface for Command Queuing */
182 #define ESDHC_FLAG_CQHCI BIT(12)
183 /* need request pmqos during low power */
184 #define ESDHC_FLAG_PMQOS BIT(13)
185 /* The IP state got lost in low power mode */
186 #define ESDHC_FLAG_STATE_LOST_IN_LPMODE BIT(14)
187 /* The IP lost clock rate in PM_RUNTIME */
188 #define ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME BIT(15)
190 * The IP do not support the ACMD23 feature completely when use ADMA mode.
191 * In ADMA mode, it only use the 16 bit block count of the register 0x4
192 * (BLOCK_ATT) as the CMD23's argument for ACMD23 mode, which means it will
193 * ignore the upper 16 bit of the CMD23's argument. This will block the reliable
194 * write operation in RPMB, because RPMB reliable write need to set the bit31
195 * of the CMD23's argument.
196 * imx6qpdl/imx6sx/imx6sl/imx7d has this limitation only for ADMA mode, SDMA
197 * do not has this limitation. so when these SoC use ADMA mode, it need to
198 * disable the ACMD23 feature.
200 #define ESDHC_FLAG_BROKEN_AUTO_CMD23 BIT(16)
202 /* ERR004536 is not applicable for the IP */
203 #define ESDHC_FLAG_SKIP_ERR004536 BIT(17)
205 /* The IP does not have GPIO CD wake capabilities */
206 #define ESDHC_FLAG_SKIP_CD_WAKE BIT(18)
209 ESDHC_WP_NONE, /* no WP, neither controller nor gpio */
210 ESDHC_WP_CONTROLLER, /* mmc controller internal WP */
211 ESDHC_WP_GPIO, /* external gpio pin for WP */
215 ESDHC_CD_NONE, /* no CD, neither controller nor gpio */
216 ESDHC_CD_CONTROLLER, /* mmc controller internal CD */
217 ESDHC_CD_GPIO, /* external gpio pin for CD */
218 ESDHC_CD_PERMANENT, /* no CD, card permanently wired to host */
222 * struct esdhc_platform_data - platform data for esdhc on i.MX
224 * ESDHC_WP(CD)_CONTROLLER type is not available on i.MX25/35.
226 * @wp_type: type of write_protect method (see wp_types enum above)
227 * @cd_type: type of card_detect method (see cd_types enum above)
230 struct esdhc_platform_data {
231 enum wp_types wp_type;
232 enum cd_types cd_type;
234 unsigned int delay_line;
235 unsigned int tuning_step; /* The delay cell steps in tuning procedure */
236 unsigned int tuning_start_tap; /* The start delay cell point in tuning procedure */
237 unsigned int strobe_dll_delay_target; /* The delay cell for strobe pad (read clock) */
240 struct esdhc_soc_data {
245 static const struct esdhc_soc_data esdhc_imx25_data = {
246 .flags = ESDHC_FLAG_ERR004536,
249 static const struct esdhc_soc_data esdhc_imx35_data = {
250 .flags = ESDHC_FLAG_ERR004536,
253 static const struct esdhc_soc_data esdhc_imx51_data = {
257 static const struct esdhc_soc_data esdhc_imx53_data = {
258 .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
261 static const struct esdhc_soc_data usdhc_imx6q_data = {
262 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING
263 | ESDHC_FLAG_BROKEN_AUTO_CMD23,
266 static const struct esdhc_soc_data usdhc_imx6sl_data = {
267 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
268 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536
270 | ESDHC_FLAG_BROKEN_AUTO_CMD23,
273 static const struct esdhc_soc_data usdhc_imx6sll_data = {
274 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
275 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
277 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
280 static const struct esdhc_soc_data usdhc_imx6sx_data = {
281 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
282 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
283 | ESDHC_FLAG_STATE_LOST_IN_LPMODE
284 | ESDHC_FLAG_BROKEN_AUTO_CMD23,
287 static const struct esdhc_soc_data usdhc_imx6ull_data = {
288 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
289 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
290 | ESDHC_FLAG_ERR010450
291 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
294 static const struct esdhc_soc_data usdhc_imx7d_data = {
295 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
296 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
298 | ESDHC_FLAG_STATE_LOST_IN_LPMODE
299 | ESDHC_FLAG_BROKEN_AUTO_CMD23,
302 static struct esdhc_soc_data usdhc_s32g2_data = {
303 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING
304 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
305 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
306 | ESDHC_FLAG_SKIP_ERR004536 | ESDHC_FLAG_SKIP_CD_WAKE,
307 .quirks = SDHCI_QUIRK_NO_LED,
310 static struct esdhc_soc_data usdhc_imx7ulp_data = {
311 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
312 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
313 | ESDHC_FLAG_PMQOS | ESDHC_FLAG_HS400
314 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
315 .quirks = SDHCI_QUIRK_NO_LED,
317 static struct esdhc_soc_data usdhc_imxrt1050_data = {
318 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
319 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200,
320 .quirks = SDHCI_QUIRK_NO_LED,
323 static struct esdhc_soc_data usdhc_imx8qxp_data = {
324 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
325 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
326 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
327 | ESDHC_FLAG_STATE_LOST_IN_LPMODE
328 | ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME,
329 .quirks = SDHCI_QUIRK_NO_LED,
332 static struct esdhc_soc_data usdhc_imx8mm_data = {
333 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
334 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
335 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
336 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
337 .quirks = SDHCI_QUIRK_NO_LED,
340 struct pltfm_imx_data {
342 struct pinctrl *pinctrl;
343 struct pinctrl_state *pins_100mhz;
344 struct pinctrl_state *pins_200mhz;
345 const struct esdhc_soc_data *socdata;
346 struct esdhc_platform_data boarddata;
350 unsigned int actual_clock;
353 * USDHC has one limition, require the SDIO device a different
354 * register setting. Driver has to recognize card type during
355 * the card init, but at this stage, mmc_host->card is not
356 * available. So involve this field to save the card type
357 * during card init through usdhc_init_card().
359 unsigned int init_card_type;
362 NO_CMD_PENDING, /* no multiblock command pending */
363 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
364 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
367 struct pm_qos_request pm_qos_req;
370 static const struct of_device_id imx_esdhc_dt_ids[] = {
371 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
372 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
373 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
374 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
375 { .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
376 { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
377 { .compatible = "fsl,imx6sll-usdhc", .data = &usdhc_imx6sll_data, },
378 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
379 { .compatible = "fsl,imx6ull-usdhc", .data = &usdhc_imx6ull_data, },
380 { .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
381 { .compatible = "fsl,imx7ulp-usdhc", .data = &usdhc_imx7ulp_data, },
382 { .compatible = "fsl,imx8qxp-usdhc", .data = &usdhc_imx8qxp_data, },
383 { .compatible = "fsl,imx8mm-usdhc", .data = &usdhc_imx8mm_data, },
384 { .compatible = "fsl,imxrt1050-usdhc", .data = &usdhc_imxrt1050_data, },
385 { .compatible = "nxp,s32g2-usdhc", .data = &usdhc_s32g2_data, },
388 MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
390 static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
392 return data->socdata == &esdhc_imx25_data;
395 static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
397 return data->socdata == &esdhc_imx53_data;
400 static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
402 return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
405 static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
407 void __iomem *base = host->ioaddr + (reg & ~0x3);
408 u32 shift = (reg & 0x3) * 8;
410 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
413 #define DRIVER_NAME "sdhci-esdhc-imx"
414 #define ESDHC_IMX_DUMP(f, x...) \
415 pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
416 static void esdhc_dump_debug_regs(struct sdhci_host *host)
419 char *debug_status[7] = {
422 "trans debug status",
426 "async fifo debug status"
429 ESDHC_IMX_DUMP("========= ESDHC IMX DEBUG STATUS DUMP =========\n");
430 for (i = 0; i < 7; i++) {
431 esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK,
432 ESDHC_DEBUG_SEL_CMD_STATE + i, ESDHC_DEBUG_SEL_REG);
433 ESDHC_IMX_DUMP("%s: 0x%04x\n", debug_status[i],
434 readw(host->ioaddr + ESDHC_DEBUG_SEL_AND_STATUS_REG));
437 esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK, 0, ESDHC_DEBUG_SEL_REG);
441 static inline void esdhc_wait_for_card_clock_gate_off(struct sdhci_host *host)
446 ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, present_state,
447 (present_state & ESDHC_CLOCK_GATE_OFF), 2, 100);
448 if (ret == -ETIMEDOUT)
449 dev_warn(mmc_dev(host->mmc), "%s: card clock still not gate off in 100us!.\n", __func__);
452 /* Enable the auto tuning circuit to check the CMD line and BUS line */
453 static inline void usdhc_auto_tuning_mode_sel_and_en(struct sdhci_host *host)
455 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
456 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
457 u32 buswidth, auto_tune_buswidth;
460 buswidth = USDHC_GET_BUSWIDTH(readl(host->ioaddr + SDHCI_HOST_CONTROL));
463 case ESDHC_CTRL_8BITBUS:
464 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN;
466 case ESDHC_CTRL_4BITBUS:
467 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN;
469 default: /* 1BITBUS */
470 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN;
475 * For USDHC, auto tuning circuit can not handle the async sdio
476 * device interrupt correctly. When sdio device use 4 data lines,
477 * async sdio interrupt will use the shared DAT[1], if enable auto
478 * tuning circuit check these 4 data lines, include the DAT[1],
479 * this circuit will detect this interrupt, take this as a data on
480 * DAT[1], and adjust the delay cell wrongly.
481 * This is the hardware design limitation, to avoid this, for sdio
482 * device, config the auto tuning circuit only check DAT[0] and CMD
485 if (imx_data->init_card_type == MMC_TYPE_SDIO)
486 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN;
488 esdhc_clrset_le(host, ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK,
489 auto_tune_buswidth | ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN,
492 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
493 reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
494 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
497 static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
499 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
500 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
501 u32 val = readl(host->ioaddr + reg);
503 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
505 /* save the least 20 bits */
506 val = fsl_prss & 0x000FFFFF;
507 /* move dat[0-3] bits */
508 val |= (fsl_prss & 0x0F000000) >> 4;
509 /* move cmd line bit */
510 val |= (fsl_prss & 0x00800000) << 1;
513 if (unlikely(reg == SDHCI_CAPABILITIES)) {
514 /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
515 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
518 /* In FSL esdhc IC module, only bit20 is used to indicate the
519 * ADMA2 capability of esdhc, but this bit is messed up on
520 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
521 * don't actually support ADMA2). So set the BROKEN_ADMA
522 * quirk on MX25/35 platforms.
525 if (val & SDHCI_CAN_DO_ADMA1) {
526 val &= ~SDHCI_CAN_DO_ADMA1;
527 val |= SDHCI_CAN_DO_ADMA2;
531 if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
532 if (esdhc_is_usdhc(imx_data)) {
533 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
534 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
536 /* imx6q/dl does not have cap_1 register, fake one */
537 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
538 | SDHCI_SUPPORT_SDR50
539 | SDHCI_USE_SDR50_TUNING
540 | FIELD_PREP(SDHCI_RETUNING_MODE_MASK,
541 SDHCI_TUNING_MODE_3);
544 * Do not advertise faster UHS modes if there are no
545 * pinctrl states for 100MHz/200MHz.
547 if (IS_ERR_OR_NULL(imx_data->pins_100mhz))
548 val &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50);
549 if (IS_ERR_OR_NULL(imx_data->pins_200mhz))
550 val &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_HS400);
554 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
556 val |= FIELD_PREP(SDHCI_MAX_CURRENT_330_MASK, 0xFF);
557 val |= FIELD_PREP(SDHCI_MAX_CURRENT_300_MASK, 0xFF);
558 val |= FIELD_PREP(SDHCI_MAX_CURRENT_180_MASK, 0xFF);
561 if (unlikely(reg == SDHCI_INT_STATUS)) {
562 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
563 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
564 val |= SDHCI_INT_ADMA_ERROR;
568 * mask off the interrupt we get in response to the manually
571 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
572 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
573 val &= ~SDHCI_INT_RESPONSE;
574 writel(SDHCI_INT_RESPONSE, host->ioaddr +
576 imx_data->multiblock_status = NO_CMD_PENDING;
583 static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
585 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
586 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
589 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
590 reg == SDHCI_INT_STATUS)) {
591 if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
593 * Clear and then set D3CD bit to avoid missing the
594 * card interrupt. This is an eSDHC controller problem
595 * so we need to apply the following workaround: clear
596 * and set D3CD bit will make eSDHC re-sample the card
597 * interrupt. In case a card interrupt was lost,
598 * re-sample it by the following steps.
600 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
601 data &= ~ESDHC_CTRL_D3CD;
602 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
603 data |= ESDHC_CTRL_D3CD;
604 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
607 if (val & SDHCI_INT_ADMA_ERROR) {
608 val &= ~SDHCI_INT_ADMA_ERROR;
609 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
613 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
614 && (reg == SDHCI_INT_STATUS)
615 && (val & SDHCI_INT_DATA_END))) {
617 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
618 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
619 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
621 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
623 /* send a manual CMD12 with RESPTYP=none */
624 data = MMC_STOP_TRANSMISSION << 24 |
625 SDHCI_CMD_ABORTCMD << 16;
626 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
627 imx_data->multiblock_status = WAIT_FOR_INT;
631 writel(val, host->ioaddr + reg);
634 static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
636 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
637 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
641 if (unlikely(reg == SDHCI_HOST_VERSION)) {
643 if (esdhc_is_usdhc(imx_data)) {
645 * The usdhc register returns a wrong host version.
648 return SDHCI_SPEC_300;
652 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
653 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
654 if (val & ESDHC_VENDOR_SPEC_VSELECT)
655 ret |= SDHCI_CTRL_VDD_180;
657 if (esdhc_is_usdhc(imx_data)) {
658 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
659 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
660 else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
661 /* the std tuning bits is in ACMD12_ERR for imx6sl */
662 val = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
665 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
666 ret |= SDHCI_CTRL_EXEC_TUNING;
667 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
668 ret |= SDHCI_CTRL_TUNED_CLK;
670 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
675 if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
676 if (esdhc_is_usdhc(imx_data)) {
677 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
678 ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
680 if (m & ESDHC_MIX_CTRL_AC23EN) {
681 ret &= ~ESDHC_MIX_CTRL_AC23EN;
682 ret |= SDHCI_TRNS_AUTO_CMD23;
685 ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
691 return readw(host->ioaddr + reg);
694 static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
696 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
697 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
701 case SDHCI_CLOCK_CONTROL:
702 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
703 if (val & SDHCI_CLOCK_CARD_EN)
704 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
706 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
707 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
708 if (!(new_val & ESDHC_VENDOR_SPEC_FRC_SDCLK_ON))
709 esdhc_wait_for_card_clock_gate_off(host);
711 case SDHCI_HOST_CONTROL2:
712 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
713 if (val & SDHCI_CTRL_VDD_180)
714 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
716 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
717 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
718 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
719 u32 v = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
720 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
721 if (val & SDHCI_CTRL_TUNED_CLK) {
722 v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
724 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
725 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
728 if (val & SDHCI_CTRL_EXEC_TUNING) {
729 v |= ESDHC_MIX_CTRL_EXE_TUNE;
730 m |= ESDHC_MIX_CTRL_FBCLK_SEL;
732 v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
735 writel(v, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
736 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
739 case SDHCI_TRANSFER_MODE:
740 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
741 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
742 && (host->cmd->data->blocks > 1)
743 && (host->cmd->data->flags & MMC_DATA_READ)) {
745 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
746 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
747 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
750 if (esdhc_is_usdhc(imx_data)) {
752 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
754 if (val & SDHCI_TRNS_AUTO_CMD23) {
755 val &= ~SDHCI_TRNS_AUTO_CMD23;
756 val |= ESDHC_MIX_CTRL_AC23EN;
758 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
759 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
761 /* Set watermark levels for PIO access to maximum value
762 * (128 words) to accommodate full 512 bytes buffer.
763 * For DMA access restore the levels to default value.
765 m = readl(host->ioaddr + ESDHC_WTMK_LVL);
766 if (val & SDHCI_TRNS_DMA) {
767 wml = ESDHC_WTMK_LVL_WML_VAL_DEF;
770 wml = ESDHC_WTMK_LVL_WML_VAL_MAX;
773 * Since already disable DMA mode, so also need
774 * to clear the DMASEL. Otherwise, for standard
775 * tuning, when send tuning command, usdhc will
776 * still prefetch the ADMA script from wrong
777 * DMA address, then we will see IOMMU report
778 * some error which show lack of TLB mapping.
780 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
781 ctrl &= ~SDHCI_CTRL_DMA_MASK;
782 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
784 m &= ~(ESDHC_WTMK_LVL_RD_WML_MASK |
785 ESDHC_WTMK_LVL_WR_WML_MASK);
786 m |= (wml << ESDHC_WTMK_LVL_RD_WML_SHIFT) |
787 (wml << ESDHC_WTMK_LVL_WR_WML_SHIFT);
788 writel(m, host->ioaddr + ESDHC_WTMK_LVL);
791 * Postpone this write, we must do it together with a
792 * command write that is down below.
794 imx_data->scratchpad = val;
798 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
799 val |= SDHCI_CMD_ABORTCMD;
801 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
802 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
803 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
805 if (esdhc_is_usdhc(imx_data))
807 host->ioaddr + SDHCI_TRANSFER_MODE);
809 writel(val << 16 | imx_data->scratchpad,
810 host->ioaddr + SDHCI_TRANSFER_MODE);
812 case SDHCI_BLOCK_SIZE:
813 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
816 esdhc_clrset_le(host, 0xffff, val, reg);
819 static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
825 case SDHCI_HOST_CONTROL:
826 val = readl(host->ioaddr + reg);
828 ret = val & SDHCI_CTRL_LED;
829 ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
830 ret |= (val & ESDHC_CTRL_4BITBUS);
831 ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
835 return readb(host->ioaddr + reg);
838 static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
840 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
841 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
846 case SDHCI_POWER_CONTROL:
848 * FSL put some DMA bits here
849 * If your board has a regulator, code should be here
852 case SDHCI_HOST_CONTROL:
853 /* FSL messed up here, so we need to manually compose it. */
854 new_val = val & SDHCI_CTRL_LED;
855 /* ensure the endianness */
856 new_val |= ESDHC_HOST_CONTROL_LE;
857 /* bits 8&9 are reserved on mx25 */
858 if (!is_imx25_esdhc(imx_data)) {
859 /* DMA mode bits are shifted */
860 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
864 * Do not touch buswidth bits here. This is done in
865 * esdhc_pltfm_bus_width.
866 * Do not touch the D3CD bit either which is used for the
867 * SDIO interrupt erratum workaround.
869 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
871 esdhc_clrset_le(host, mask, new_val, reg);
873 case SDHCI_SOFTWARE_RESET:
874 if (val & SDHCI_RESET_DATA)
875 new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
878 esdhc_clrset_le(host, 0xff, val, reg);
880 if (reg == SDHCI_SOFTWARE_RESET) {
881 if (val & SDHCI_RESET_ALL) {
883 * The esdhc has a design violation to SDHC spec which
884 * tells that software reset should not affect card
885 * detection circuit. But esdhc clears its SYSCTL
886 * register bits [0..2] during the software reset. This
887 * will stop those clocks that card detection circuit
888 * relies on. To work around it, we turn the clocks on
889 * back to keep card detection circuit functional.
891 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
893 * The reset on usdhc fails to clear MIX_CTRL register.
894 * Do it manually here.
896 if (esdhc_is_usdhc(imx_data)) {
898 * the tuning bits should be kept during reset
900 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
901 writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
902 host->ioaddr + ESDHC_MIX_CTRL);
903 imx_data->is_ddr = 0;
905 } else if (val & SDHCI_RESET_DATA) {
907 * The eSDHC DAT line software reset clears at least the
908 * data transfer width on i.MX25, so make sure that the
909 * Host Control register is unaffected.
911 esdhc_clrset_le(host, 0xff, new_val,
917 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
919 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
921 return pltfm_host->clock;
924 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
926 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
928 return pltfm_host->clock / 256 / 16;
931 static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
934 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
935 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
936 unsigned int host_clock = pltfm_host->clock;
937 int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
943 if (esdhc_is_usdhc(imx_data)) {
944 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
945 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
946 host->ioaddr + ESDHC_VENDOR_SPEC);
947 esdhc_wait_for_card_clock_gate_off(host);
951 host->mmc->actual_clock = 0;
955 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
956 if (is_imx53_esdhc(imx_data)) {
958 * According to the i.MX53 reference manual, if DLLCTRL[10] can
959 * be set, then the controller is eSDHCv3, else it is eSDHCv2.
961 val = readl(host->ioaddr + ESDHC_DLL_CTRL);
962 writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL);
963 temp = readl(host->ioaddr + ESDHC_DLL_CTRL);
964 writel(val, host->ioaddr + ESDHC_DLL_CTRL);
969 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
970 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
972 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
974 if ((imx_data->socdata->flags & ESDHC_FLAG_ERR010450) &&
975 (!(host->quirks2 & SDHCI_QUIRK2_NO_1_8_V))) {
976 unsigned int max_clock;
978 max_clock = imx_data->is_ddr ? 45000000 : 150000000;
980 clock = min(clock, max_clock);
983 while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
987 while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
990 host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
991 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
992 clock, host->mmc->actual_clock);
997 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
998 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
999 | (div << ESDHC_DIVIDER_SHIFT)
1000 | (pre_div << ESDHC_PREDIV_SHIFT));
1001 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
1003 /* need to wait the bit 3 of the PRSSTAT to be set, make sure card clock is stable */
1004 ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, temp,
1005 (temp & ESDHC_CLOCK_STABLE), 2, 100);
1006 if (ret == -ETIMEDOUT)
1007 dev_warn(mmc_dev(host->mmc), "card clock still not stable in 100us!.\n");
1009 if (esdhc_is_usdhc(imx_data)) {
1010 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
1011 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
1012 host->ioaddr + ESDHC_VENDOR_SPEC);
1017 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
1019 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1020 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1021 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1023 switch (boarddata->wp_type) {
1025 return mmc_gpio_get_ro(host->mmc);
1026 case ESDHC_WP_CONTROLLER:
1027 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
1028 SDHCI_WRITE_PROTECT);
1036 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
1041 case MMC_BUS_WIDTH_8:
1042 ctrl = ESDHC_CTRL_8BITBUS;
1044 case MMC_BUS_WIDTH_4:
1045 ctrl = ESDHC_CTRL_4BITBUS;
1052 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
1053 SDHCI_HOST_CONTROL);
1056 static void esdhc_reset_tuning(struct sdhci_host *host)
1058 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1059 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1063 /* Reset the tuning circuit */
1064 if (esdhc_is_usdhc(imx_data)) {
1065 ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
1066 ctrl &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
1067 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1068 ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1069 ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
1070 writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
1071 writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1072 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1073 writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
1074 ctrl = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1075 ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1076 ctrl &= ~ESDHC_MIX_CTRL_EXE_TUNE;
1077 writel(ctrl, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1078 /* Make sure ESDHC_MIX_CTRL_EXE_TUNE cleared */
1079 ret = readl_poll_timeout(host->ioaddr + SDHCI_AUTO_CMD_STATUS,
1080 ctrl, !(ctrl & ESDHC_MIX_CTRL_EXE_TUNE), 1, 50);
1081 if (ret == -ETIMEDOUT)
1082 dev_warn(mmc_dev(host->mmc),
1083 "Warning! clear execute tuning bit failed\n");
1085 * SDHCI_INT_DATA_AVAIL is W1C bit, set this bit will clear the
1086 * usdhc IP internal logic flag execute_tuning_with_clr_buf, which
1087 * will finally make sure the normal data transfer logic correct.
1089 ctrl = readl(host->ioaddr + SDHCI_INT_STATUS);
1090 ctrl |= SDHCI_INT_DATA_AVAIL;
1091 writel(ctrl, host->ioaddr + SDHCI_INT_STATUS);
1096 static void usdhc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1098 struct sdhci_host *host = mmc_priv(mmc);
1099 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1100 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1102 imx_data->init_card_type = card->type;
1105 static int usdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
1107 struct sdhci_host *host = mmc_priv(mmc);
1111 * i.MX uSDHC internally already uses a fixed optimized timing for
1112 * DDR50, normally does not require tuning for DDR50 mode.
1114 if (host->timing == MMC_TIMING_UHS_DDR50)
1118 * Reset tuning circuit logic. If not, the previous tuning result
1119 * will impact current tuning, make current tuning can't set the
1120 * correct delay cell.
1122 esdhc_reset_tuning(host);
1123 err = sdhci_execute_tuning(mmc, opcode);
1124 /* If tuning done, enable auto tuning */
1125 if (!err && !host->tuning_err)
1126 usdhc_auto_tuning_mode_sel_and_en(host);
1131 static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
1137 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
1140 /* IC suggest to reset USDHC before every tuning command */
1141 esdhc_clrset_le(host, 0xff, SDHCI_RESET_ALL, SDHCI_SOFTWARE_RESET);
1142 ret = readb_poll_timeout(host->ioaddr + SDHCI_SOFTWARE_RESET, sw_rst,
1143 !(sw_rst & SDHCI_RESET_ALL), 10, 100);
1144 if (ret == -ETIMEDOUT)
1145 dev_warn(mmc_dev(host->mmc),
1146 "warning! RESET_ALL never complete before sending tuning command\n");
1148 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
1149 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
1150 ESDHC_MIX_CTRL_FBCLK_SEL;
1151 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
1152 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1153 dev_dbg(mmc_dev(host->mmc),
1154 "tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
1155 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
1158 static void esdhc_post_tuning(struct sdhci_host *host)
1162 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
1163 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
1164 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
1168 * find the largest pass window, and use the average delay of this
1169 * largest window to get the best timing.
1171 static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
1173 int min, max, avg, ret;
1174 int win_length, target_min, target_max, target_win_length;
1176 min = ESDHC_TUNE_CTRL_MIN;
1177 max = ESDHC_TUNE_CTRL_MIN;
1178 target_win_length = 0;
1179 while (max < ESDHC_TUNE_CTRL_MAX) {
1180 /* find the mininum delay first which can pass tuning */
1181 while (min < ESDHC_TUNE_CTRL_MAX) {
1182 esdhc_prepare_tuning(host, min);
1183 if (!mmc_send_tuning(host->mmc, opcode, NULL))
1185 min += ESDHC_TUNE_CTRL_STEP;
1188 /* find the maxinum delay which can not pass tuning */
1189 max = min + ESDHC_TUNE_CTRL_STEP;
1190 while (max < ESDHC_TUNE_CTRL_MAX) {
1191 esdhc_prepare_tuning(host, max);
1192 if (mmc_send_tuning(host->mmc, opcode, NULL)) {
1193 max -= ESDHC_TUNE_CTRL_STEP;
1196 max += ESDHC_TUNE_CTRL_STEP;
1199 win_length = max - min + 1;
1200 /* get the largest pass window */
1201 if (win_length > target_win_length) {
1202 target_win_length = win_length;
1207 /* continue to find the next pass window */
1208 min = max + ESDHC_TUNE_CTRL_STEP;
1211 /* use average delay to get the best timing */
1212 avg = (target_min + target_max) / 2;
1213 esdhc_prepare_tuning(host, avg);
1214 ret = mmc_send_tuning(host->mmc, opcode, NULL);
1215 esdhc_post_tuning(host);
1217 dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
1218 ret ? "failed" : "passed", avg, ret);
1223 static void esdhc_hs400_enhanced_strobe(struct mmc_host *mmc, struct mmc_ios *ios)
1225 struct sdhci_host *host = mmc_priv(mmc);
1228 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
1229 if (ios->enhanced_strobe)
1230 m |= ESDHC_MIX_CTRL_HS400_ES_EN;
1232 m &= ~ESDHC_MIX_CTRL_HS400_ES_EN;
1233 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1236 static int esdhc_change_pinstate(struct sdhci_host *host,
1239 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1240 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1241 struct pinctrl_state *pinctrl;
1243 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
1245 if (IS_ERR(imx_data->pinctrl) ||
1246 IS_ERR(imx_data->pins_100mhz) ||
1247 IS_ERR(imx_data->pins_200mhz))
1251 case MMC_TIMING_UHS_SDR50:
1252 case MMC_TIMING_UHS_DDR50:
1253 pinctrl = imx_data->pins_100mhz;
1255 case MMC_TIMING_UHS_SDR104:
1256 case MMC_TIMING_MMC_HS200:
1257 case MMC_TIMING_MMC_HS400:
1258 pinctrl = imx_data->pins_200mhz;
1261 /* back to default state for other legacy timing */
1262 return pinctrl_select_default_state(mmc_dev(host->mmc));
1265 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
1269 * For HS400 eMMC, there is a data_strobe line. This signal is generated
1270 * by the device and used for data output and CRC status response output
1271 * in HS400 mode. The frequency of this signal follows the frequency of
1272 * CLK generated by host. The host receives the data which is aligned to the
1273 * edge of data_strobe line. Due to the time delay between CLK line and
1274 * data_strobe line, if the delay time is larger than one clock cycle,
1275 * then CLK and data_strobe line will be misaligned, read error shows up.
1277 static void esdhc_set_strobe_dll(struct sdhci_host *host)
1279 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1280 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1285 /* disable clock before enabling strobe dll */
1286 writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
1287 ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
1288 host->ioaddr + ESDHC_VENDOR_SPEC);
1289 esdhc_wait_for_card_clock_gate_off(host);
1291 /* force a reset on strobe dll */
1292 writel(ESDHC_STROBE_DLL_CTRL_RESET,
1293 host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1294 /* clear the reset bit on strobe dll before any setting */
1295 writel(0, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1298 * enable strobe dll ctrl and adjust the delay target
1299 * for the uSDHC loopback read clock
1301 if (imx_data->boarddata.strobe_dll_delay_target)
1302 strobe_delay = imx_data->boarddata.strobe_dll_delay_target;
1304 strobe_delay = ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT;
1305 v = ESDHC_STROBE_DLL_CTRL_ENABLE |
1306 ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
1307 (strobe_delay << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
1308 writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1310 /* wait max 50us to get the REF/SLV lock */
1311 ret = readl_poll_timeout(host->ioaddr + ESDHC_STROBE_DLL_STATUS, v,
1312 ((v & ESDHC_STROBE_DLL_STS_REF_LOCK) && (v & ESDHC_STROBE_DLL_STS_SLV_LOCK)), 1, 50);
1313 if (ret == -ETIMEDOUT)
1314 dev_warn(mmc_dev(host->mmc),
1315 "warning! HS400 strobe DLL status REF/SLV not lock in 50us, STROBE DLL status is %x!\n", v);
1318 static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1321 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1322 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1323 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1325 /* disable ddr mode and disable HS400 mode */
1326 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
1327 m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
1328 imx_data->is_ddr = 0;
1331 case MMC_TIMING_UHS_SDR12:
1332 case MMC_TIMING_UHS_SDR25:
1333 case MMC_TIMING_UHS_SDR50:
1334 case MMC_TIMING_UHS_SDR104:
1335 case MMC_TIMING_MMC_HS:
1336 case MMC_TIMING_MMC_HS200:
1337 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1339 case MMC_TIMING_UHS_DDR50:
1340 case MMC_TIMING_MMC_DDR52:
1341 m |= ESDHC_MIX_CTRL_DDREN;
1342 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1343 imx_data->is_ddr = 1;
1344 if (boarddata->delay_line) {
1346 v = boarddata->delay_line <<
1347 ESDHC_DLL_OVERRIDE_VAL_SHIFT |
1348 (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
1349 if (is_imx53_esdhc(imx_data))
1351 writel(v, host->ioaddr + ESDHC_DLL_CTRL);
1354 case MMC_TIMING_MMC_HS400:
1355 m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
1356 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1357 imx_data->is_ddr = 1;
1358 /* update clock after enable DDR for strobe DLL lock */
1359 host->ops->set_clock(host, host->clock);
1360 esdhc_set_strobe_dll(host);
1362 case MMC_TIMING_LEGACY:
1364 esdhc_reset_tuning(host);
1368 esdhc_change_pinstate(host, timing);
1371 static void esdhc_reset(struct sdhci_host *host, u8 mask)
1373 sdhci_and_cqhci_reset(host, mask);
1375 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1376 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1379 static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host)
1381 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1382 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1384 /* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
1385 return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
1388 static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
1390 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1391 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1393 /* use maximum timeout counter */
1394 esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
1395 esdhc_is_usdhc(imx_data) ? 0xF0000 : 0xE0000,
1396 ESDHC_SYSTEM_CONTROL);
1399 static u32 esdhc_cqhci_irq(struct sdhci_host *host, u32 intmask)
1404 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
1407 cqhci_irq(host->mmc, intmask, cmd_error, data_error);
1412 static void esdhc_hw_reset(struct sdhci_host *host)
1414 esdhc_clrset_le(host, ESDHC_SYS_CTRL_IPP_RST_N, 0, ESDHC_SYSTEM_CONTROL);
1415 /* eMMC spec requires minimum 1us, here delay between 1-10us */
1416 usleep_range(1, 10);
1417 esdhc_clrset_le(host, ESDHC_SYS_CTRL_IPP_RST_N,
1418 ESDHC_SYS_CTRL_IPP_RST_N, ESDHC_SYSTEM_CONTROL);
1419 /* eMMC spec requires minimum 200us, here delay between 200-300us */
1420 usleep_range(200, 300);
1423 static struct sdhci_ops sdhci_esdhc_ops = {
1424 .read_l = esdhc_readl_le,
1425 .read_w = esdhc_readw_le,
1426 .read_b = esdhc_readb_le,
1427 .write_l = esdhc_writel_le,
1428 .write_w = esdhc_writew_le,
1429 .write_b = esdhc_writeb_le,
1430 .set_clock = esdhc_pltfm_set_clock,
1431 .get_max_clock = esdhc_pltfm_get_max_clock,
1432 .get_min_clock = esdhc_pltfm_get_min_clock,
1433 .get_max_timeout_count = esdhc_get_max_timeout_count,
1434 .get_ro = esdhc_pltfm_get_ro,
1435 .set_timeout = esdhc_set_timeout,
1436 .set_bus_width = esdhc_pltfm_set_bus_width,
1437 .set_uhs_signaling = esdhc_set_uhs_signaling,
1438 .reset = esdhc_reset,
1439 .irq = esdhc_cqhci_irq,
1440 .dump_vendor_regs = esdhc_dump_debug_regs,
1441 .hw_reset = esdhc_hw_reset,
1444 static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
1445 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
1446 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
1447 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
1448 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
1449 .ops = &sdhci_esdhc_ops,
1452 static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
1454 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1455 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1456 struct cqhci_host *cq_host = host->mmc->cqe_private;
1459 if (esdhc_is_usdhc(imx_data)) {
1461 * The imx6q ROM code will change the default watermark
1462 * level setting to something insane. Change it back here.
1464 writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
1467 * ROM code will change the bit burst_length_enable setting
1468 * to zero if this usdhc is chosen to boot system. Change
1469 * it back here, otherwise it will impact the performance a
1470 * lot. This bit is used to enable/disable the burst length
1471 * for the external AHB2AXI bridge. It's useful especially
1472 * for INCR transfer because without burst length indicator,
1473 * the AHB2AXI bridge does not know the burst length in
1474 * advance. And without burst length indicator, AHB INCR
1475 * transfer can only be converted to singles on the AXI side.
1477 writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
1478 | ESDHC_BURST_LEN_EN_INCR,
1479 host->ioaddr + SDHCI_HOST_CONTROL);
1482 * erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
1483 * TO1.1, it's harmless for MX6SL
1485 if (!(imx_data->socdata->flags & ESDHC_FLAG_SKIP_ERR004536)) {
1486 writel(readl(host->ioaddr + 0x6c) & ~BIT(7),
1487 host->ioaddr + 0x6c);
1490 /* disable DLL_CTRL delay line settings */
1491 writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
1494 * For the case of command with busy, if set the bit
1495 * ESDHC_VEND_SPEC2_EN_BUSY_IRQ, USDHC will generate a
1496 * transfer complete interrupt when busy is deasserted.
1497 * When CQHCI use DCMD to send a CMD need R1b respons,
1498 * CQHCI require to set ESDHC_VEND_SPEC2_EN_BUSY_IRQ,
1499 * otherwise DCMD will always meet timeout waiting for
1500 * hardware interrupt issue.
1502 if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1503 tmp = readl(host->ioaddr + ESDHC_VEND_SPEC2);
1504 tmp |= ESDHC_VEND_SPEC2_EN_BUSY_IRQ;
1505 writel(tmp, host->ioaddr + ESDHC_VEND_SPEC2);
1507 host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
1510 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1511 tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1512 tmp |= ESDHC_STD_TUNING_EN;
1515 * ROM code or bootloader may config the start tap
1516 * and step, unmask them first.
1518 tmp &= ~(ESDHC_TUNING_START_TAP_MASK | ESDHC_TUNING_STEP_MASK);
1519 if (imx_data->boarddata.tuning_start_tap)
1520 tmp |= imx_data->boarddata.tuning_start_tap;
1522 tmp |= ESDHC_TUNING_START_TAP_DEFAULT;
1524 if (imx_data->boarddata.tuning_step) {
1525 tmp |= imx_data->boarddata.tuning_step
1526 << ESDHC_TUNING_STEP_SHIFT;
1528 tmp |= ESDHC_TUNING_STEP_DEFAULT
1529 << ESDHC_TUNING_STEP_SHIFT;
1532 /* Disable the CMD CRC check for tuning, if not, need to
1533 * add some delay after every tuning command, because
1534 * hardware standard tuning logic will directly go to next
1535 * step once it detect the CMD CRC error, will not wait for
1536 * the card side to finally send out the tuning data, trigger
1537 * the buffer read ready interrupt immediately. If usdhc send
1538 * the next tuning command some eMMC card will stuck, can't
1539 * response, block the tuning procedure or the first command
1540 * after the whole tuning procedure always can't get any response.
1542 tmp |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
1543 writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1544 } else if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1546 * ESDHC_STD_TUNING_EN may be configured in bootloader
1547 * or ROM code, so clear this bit here to make sure
1548 * the manual tuning can work.
1550 tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1551 tmp &= ~ESDHC_STD_TUNING_EN;
1552 writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1556 * On i.MX8MM, we are running Dual Linux OS, with 1st Linux using SD Card
1557 * as rootfs storage, 2nd Linux using eMMC as rootfs storage. We let
1558 * the 1st linux configure power/clock for the 2nd Linux.
1560 * When the 2nd Linux is booting into rootfs stage, we let the 1st Linux
1561 * to destroy the 2nd linux, then restart the 2nd linux, we met SDHCI dump.
1562 * After we clear the pending interrupt and halt CQCTL, issue gone.
1565 tmp = cqhci_readl(cq_host, CQHCI_IS);
1566 cqhci_writel(cq_host, tmp, CQHCI_IS);
1567 cqhci_writel(cq_host, CQHCI_HALT, CQHCI_CTL);
1572 static void esdhc_cqe_enable(struct mmc_host *mmc)
1574 struct sdhci_host *host = mmc_priv(mmc);
1575 struct cqhci_host *cq_host = mmc->cqe_private;
1581 * CQE gets stuck if it sees Buffer Read Enable bit set, which can be
1582 * the case after tuning, so ensure the buffer is drained.
1584 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1585 while (reg & SDHCI_DATA_AVAILABLE) {
1586 sdhci_readl(host, SDHCI_BUFFER);
1587 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1589 dev_warn(mmc_dev(host->mmc),
1590 "CQE may get stuck because the Buffer Read Enable bit is set\n");
1597 * Runtime resume will reset the entire host controller, which
1598 * will also clear the DMAEN/BCEN of register ESDHC_MIX_CTRL.
1599 * Here set DMAEN and BCEN when enable CMDQ.
1601 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
1602 if (host->flags & SDHCI_REQ_USE_DMA)
1603 mode |= SDHCI_TRNS_DMA;
1604 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
1605 mode |= SDHCI_TRNS_BLK_CNT_EN;
1606 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
1609 * Though Runtime resume reset the entire host controller,
1610 * but do not impact the CQHCI side, need to clear the
1611 * HALT bit, avoid CQHCI stuck in the first request when
1612 * system resume back.
1614 cqhci_writel(cq_host, 0, CQHCI_CTL);
1615 if (cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_HALT)
1616 dev_err(mmc_dev(host->mmc),
1617 "failed to exit halt state when enable CQE\n");
1620 sdhci_cqe_enable(mmc);
1623 static void esdhc_sdhci_dumpregs(struct mmc_host *mmc)
1625 sdhci_dumpregs(mmc_priv(mmc));
1628 static const struct cqhci_host_ops esdhc_cqhci_ops = {
1629 .enable = esdhc_cqe_enable,
1630 .disable = sdhci_cqe_disable,
1631 .dumpregs = esdhc_sdhci_dumpregs,
1635 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
1636 struct sdhci_host *host,
1637 struct pltfm_imx_data *imx_data)
1639 struct device_node *np = pdev->dev.of_node;
1640 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1643 if (of_property_read_bool(np, "fsl,wp-controller"))
1644 boarddata->wp_type = ESDHC_WP_CONTROLLER;
1647 * If we have this property, then activate WP check.
1648 * Retrieving and requesting the actual WP GPIO will happen
1649 * in the call to mmc_of_parse().
1651 if (of_property_present(np, "wp-gpios"))
1652 boarddata->wp_type = ESDHC_WP_GPIO;
1654 of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
1655 of_property_read_u32(np, "fsl,tuning-start-tap",
1656 &boarddata->tuning_start_tap);
1658 of_property_read_u32(np, "fsl,strobe-dll-delay-target",
1659 &boarddata->strobe_dll_delay_target);
1660 if (of_property_read_bool(np, "no-1-8-v"))
1661 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1663 if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
1664 boarddata->delay_line = 0;
1666 mmc_of_parse_voltage(host->mmc, &host->ocr_mask);
1668 if (esdhc_is_usdhc(imx_data) && !IS_ERR(imx_data->pinctrl)) {
1669 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1670 ESDHC_PINCTRL_STATE_100MHZ);
1671 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1672 ESDHC_PINCTRL_STATE_200MHZ);
1675 /* call to generic mmc_of_parse to support additional capabilities */
1676 ret = mmc_of_parse(host->mmc);
1680 /* HS400/HS400ES require 8 bit bus */
1681 if (!(host->mmc->caps & MMC_CAP_8_BIT_DATA))
1682 host->mmc->caps2 &= ~(MMC_CAP2_HS400 | MMC_CAP2_HS400_ES);
1684 if (mmc_gpio_get_cd(host->mmc) >= 0)
1685 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1690 static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
1692 struct sdhci_pltfm_host *pltfm_host;
1693 struct sdhci_host *host;
1694 struct cqhci_host *cq_host;
1696 struct pltfm_imx_data *imx_data;
1698 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
1701 return PTR_ERR(host);
1703 pltfm_host = sdhci_priv(host);
1705 imx_data = sdhci_pltfm_priv(pltfm_host);
1707 imx_data->socdata = device_get_match_data(&pdev->dev);
1709 host->quirks |= imx_data->socdata->quirks;
1710 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1711 cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
1713 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1714 if (IS_ERR(imx_data->clk_ipg)) {
1715 err = PTR_ERR(imx_data->clk_ipg);
1719 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1720 if (IS_ERR(imx_data->clk_ahb)) {
1721 err = PTR_ERR(imx_data->clk_ahb);
1725 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
1726 if (IS_ERR(imx_data->clk_per)) {
1727 err = PTR_ERR(imx_data->clk_per);
1731 pltfm_host->clk = imx_data->clk_per;
1732 err = clk_prepare_enable(imx_data->clk_per);
1735 err = clk_prepare_enable(imx_data->clk_ipg);
1737 goto disable_per_clk;
1738 err = clk_prepare_enable(imx_data->clk_ahb);
1740 goto disable_ipg_clk;
1742 pltfm_host->clock = clk_get_rate(pltfm_host->clk);
1743 if (!pltfm_host->clock) {
1744 dev_err(mmc_dev(host->mmc), "could not get clk rate\n");
1746 goto disable_ahb_clk;
1749 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
1750 if (IS_ERR(imx_data->pinctrl))
1751 dev_warn(mmc_dev(host->mmc), "could not get pinctrl\n");
1753 if (esdhc_is_usdhc(imx_data)) {
1754 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
1755 host->mmc->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR;
1757 /* GPIO CD can be set as a wakeup source */
1758 if (!(imx_data->socdata->flags & ESDHC_FLAG_SKIP_CD_WAKE))
1759 host->mmc->caps |= MMC_CAP_CD_WAKE;
1761 if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
1762 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
1764 /* clear tuning bits in case ROM has set it already */
1765 writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
1766 writel(0x0, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1767 writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1770 * Link usdhc specific mmc_host_ops execute_tuning function,
1771 * to replace the standard one in sdhci_ops.
1773 host->mmc_host_ops.execute_tuning = usdhc_execute_tuning;
1776 * Link usdhc specific mmc_host_ops init card function,
1777 * to distinguish the card type.
1779 host->mmc_host_ops.init_card = usdhc_init_card;
1782 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
1783 sdhci_esdhc_ops.platform_execute_tuning =
1784 esdhc_executing_tuning;
1786 if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
1787 host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
1789 if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
1790 host->mmc->caps2 |= MMC_CAP2_HS400;
1792 if (imx_data->socdata->flags & ESDHC_FLAG_BROKEN_AUTO_CMD23)
1793 host->quirks2 |= SDHCI_QUIRK2_ACMD23_BROKEN;
1795 if (imx_data->socdata->flags & ESDHC_FLAG_HS400_ES) {
1796 host->mmc->caps2 |= MMC_CAP2_HS400_ES;
1797 host->mmc_host_ops.hs400_enhanced_strobe =
1798 esdhc_hs400_enhanced_strobe;
1801 if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1802 host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
1803 cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL);
1806 goto disable_ahb_clk;
1809 cq_host->mmio = host->ioaddr + ESDHC_CQHCI_ADDR_OFFSET;
1810 cq_host->ops = &esdhc_cqhci_ops;
1812 err = cqhci_init(cq_host, host->mmc, false);
1814 goto disable_ahb_clk;
1817 err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
1819 goto disable_ahb_clk;
1821 sdhci_esdhc_imx_hwinit(host);
1823 err = sdhci_add_host(host);
1825 goto disable_ahb_clk;
1828 * Setup the wakeup capability here, let user to decide
1829 * whether need to enable this wakeup through sysfs interface.
1831 if ((host->mmc->pm_caps & MMC_PM_KEEP_POWER) &&
1832 (host->mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ))
1833 device_set_wakeup_capable(&pdev->dev, true);
1835 pm_runtime_set_active(&pdev->dev);
1836 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1837 pm_runtime_use_autosuspend(&pdev->dev);
1838 pm_suspend_ignore_children(&pdev->dev, 1);
1839 pm_runtime_enable(&pdev->dev);
1844 clk_disable_unprepare(imx_data->clk_ahb);
1846 clk_disable_unprepare(imx_data->clk_ipg);
1848 clk_disable_unprepare(imx_data->clk_per);
1850 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1851 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1852 sdhci_pltfm_free(pdev);
1856 static void sdhci_esdhc_imx_remove(struct platform_device *pdev)
1858 struct sdhci_host *host = platform_get_drvdata(pdev);
1859 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1860 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1863 pm_runtime_get_sync(&pdev->dev);
1864 dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1865 pm_runtime_disable(&pdev->dev);
1866 pm_runtime_put_noidle(&pdev->dev);
1868 sdhci_remove_host(host, dead);
1870 clk_disable_unprepare(imx_data->clk_per);
1871 clk_disable_unprepare(imx_data->clk_ipg);
1872 clk_disable_unprepare(imx_data->clk_ahb);
1874 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1875 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1877 sdhci_pltfm_free(pdev);
1880 #ifdef CONFIG_PM_SLEEP
1881 static int sdhci_esdhc_suspend(struct device *dev)
1883 struct sdhci_host *host = dev_get_drvdata(dev);
1884 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1885 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1888 if (host->mmc->caps2 & MMC_CAP2_CQE) {
1889 ret = cqhci_suspend(host->mmc);
1894 if ((imx_data->socdata->flags & ESDHC_FLAG_STATE_LOST_IN_LPMODE) &&
1895 (host->tuning_mode != SDHCI_TUNING_MODE_1)) {
1896 mmc_retune_timer_stop(host->mmc);
1897 mmc_retune_needed(host->mmc);
1900 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1901 mmc_retune_needed(host->mmc);
1903 ret = sdhci_suspend_host(host);
1907 ret = pinctrl_pm_select_sleep_state(dev);
1911 ret = mmc_gpio_set_cd_wake(host->mmc, true);
1916 static int sdhci_esdhc_resume(struct device *dev)
1918 struct sdhci_host *host = dev_get_drvdata(dev);
1921 ret = pinctrl_pm_select_default_state(dev);
1925 /* re-initialize hw state in case it's lost in low power mode */
1926 sdhci_esdhc_imx_hwinit(host);
1928 ret = sdhci_resume_host(host);
1932 if (host->mmc->caps2 & MMC_CAP2_CQE)
1933 ret = cqhci_resume(host->mmc);
1936 ret = mmc_gpio_set_cd_wake(host->mmc, false);
1943 static int sdhci_esdhc_runtime_suspend(struct device *dev)
1945 struct sdhci_host *host = dev_get_drvdata(dev);
1946 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1947 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1950 if (host->mmc->caps2 & MMC_CAP2_CQE) {
1951 ret = cqhci_suspend(host->mmc);
1956 ret = sdhci_runtime_suspend_host(host);
1960 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1961 mmc_retune_needed(host->mmc);
1963 imx_data->actual_clock = host->mmc->actual_clock;
1964 esdhc_pltfm_set_clock(host, 0);
1965 clk_disable_unprepare(imx_data->clk_per);
1966 clk_disable_unprepare(imx_data->clk_ipg);
1967 clk_disable_unprepare(imx_data->clk_ahb);
1969 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1970 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1975 static int sdhci_esdhc_runtime_resume(struct device *dev)
1977 struct sdhci_host *host = dev_get_drvdata(dev);
1978 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1979 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1982 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1983 cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
1985 if (imx_data->socdata->flags & ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME)
1986 clk_set_rate(imx_data->clk_per, pltfm_host->clock);
1988 err = clk_prepare_enable(imx_data->clk_ahb);
1990 goto remove_pm_qos_request;
1992 err = clk_prepare_enable(imx_data->clk_per);
1994 goto disable_ahb_clk;
1996 err = clk_prepare_enable(imx_data->clk_ipg);
1998 goto disable_per_clk;
2000 esdhc_pltfm_set_clock(host, imx_data->actual_clock);
2002 err = sdhci_runtime_resume_host(host, 0);
2004 goto disable_ipg_clk;
2006 if (host->mmc->caps2 & MMC_CAP2_CQE)
2007 err = cqhci_resume(host->mmc);
2012 clk_disable_unprepare(imx_data->clk_ipg);
2014 clk_disable_unprepare(imx_data->clk_per);
2016 clk_disable_unprepare(imx_data->clk_ahb);
2017 remove_pm_qos_request:
2018 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
2019 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
2024 static const struct dev_pm_ops sdhci_esdhc_pmops = {
2025 SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
2026 SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
2027 sdhci_esdhc_runtime_resume, NULL)
2030 static struct platform_driver sdhci_esdhc_imx_driver = {
2032 .name = "sdhci-esdhc-imx",
2033 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
2034 .of_match_table = imx_esdhc_dt_ids,
2035 .pm = &sdhci_esdhc_pmops,
2037 .probe = sdhci_esdhc_imx_probe,
2038 .remove = sdhci_esdhc_imx_remove,
2041 module_platform_driver(sdhci_esdhc_imx_driver);
2043 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
2045 MODULE_LICENSE("GPL v2");