Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Freescale eSDHC i.MX controller driver for the platform bus.
0004  *
0005  * derived from the OF-version.
0006  *
0007  * Copyright (c) 2010 Pengutronix e.K.
0008  *   Author: Wolfram Sang <kernel@pengutronix.de>
0009  */
0010 
0011 #include <linux/bitfield.h>
0012 #include <linux/io.h>
0013 #include <linux/iopoll.h>
0014 #include <linux/delay.h>
0015 #include <linux/err.h>
0016 #include <linux/clk.h>
0017 #include <linux/module.h>
0018 #include <linux/slab.h>
0019 #include <linux/pm_qos.h>
0020 #include <linux/mmc/host.h>
0021 #include <linux/mmc/mmc.h>
0022 #include <linux/mmc/sdio.h>
0023 #include <linux/mmc/slot-gpio.h>
0024 #include <linux/of.h>
0025 #include <linux/of_device.h>
0026 #include <linux/pinctrl/consumer.h>
0027 #include <linux/pm_runtime.h>
0028 #include "sdhci-pltfm.h"
0029 #include "sdhci-esdhc.h"
0030 #include "cqhci.h"
0031 
0032 #define ESDHC_SYS_CTRL_DTOCV_MASK   0x0f
0033 #define ESDHC_CTRL_D3CD         0x08
0034 #define ESDHC_BURST_LEN_EN_INCR     (1 << 27)
0035 /* VENDOR SPEC register */
0036 #define ESDHC_VENDOR_SPEC       0xc0
0037 #define  ESDHC_VENDOR_SPEC_SDIO_QUIRK   (1 << 1)
0038 #define  ESDHC_VENDOR_SPEC_VSELECT  (1 << 1)
0039 #define  ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
0040 #define ESDHC_DEBUG_SEL_AND_STATUS_REG      0xc2
0041 #define ESDHC_DEBUG_SEL_REG         0xc3
0042 #define ESDHC_DEBUG_SEL_MASK            0xf
0043 #define ESDHC_DEBUG_SEL_CMD_STATE       1
0044 #define ESDHC_DEBUG_SEL_DATA_STATE      2
0045 #define ESDHC_DEBUG_SEL_TRANS_STATE     3
0046 #define ESDHC_DEBUG_SEL_DMA_STATE       4
0047 #define ESDHC_DEBUG_SEL_ADMA_STATE      5
0048 #define ESDHC_DEBUG_SEL_FIFO_STATE      6
0049 #define ESDHC_DEBUG_SEL_ASYNC_FIFO_STATE    7
0050 #define ESDHC_WTMK_LVL          0x44
0051 #define  ESDHC_WTMK_DEFAULT_VAL     0x10401040
0052 #define  ESDHC_WTMK_LVL_RD_WML_MASK 0x000000FF
0053 #define  ESDHC_WTMK_LVL_RD_WML_SHIFT    0
0054 #define  ESDHC_WTMK_LVL_WR_WML_MASK 0x00FF0000
0055 #define  ESDHC_WTMK_LVL_WR_WML_SHIFT    16
0056 #define  ESDHC_WTMK_LVL_WML_VAL_DEF 64
0057 #define  ESDHC_WTMK_LVL_WML_VAL_MAX 128
0058 #define ESDHC_MIX_CTRL          0x48
0059 #define  ESDHC_MIX_CTRL_DDREN       (1 << 3)
0060 #define  ESDHC_MIX_CTRL_AC23EN      (1 << 7)
0061 #define  ESDHC_MIX_CTRL_EXE_TUNE    (1 << 22)
0062 #define  ESDHC_MIX_CTRL_SMPCLK_SEL  (1 << 23)
0063 #define  ESDHC_MIX_CTRL_AUTO_TUNE_EN    (1 << 24)
0064 #define  ESDHC_MIX_CTRL_FBCLK_SEL   (1 << 25)
0065 #define  ESDHC_MIX_CTRL_HS400_EN    (1 << 26)
0066 #define  ESDHC_MIX_CTRL_HS400_ES_EN (1 << 27)
0067 /* Bits 3 and 6 are not SDHCI standard definitions */
0068 #define  ESDHC_MIX_CTRL_SDHCI_MASK  0xb7
0069 /* Tuning bits */
0070 #define  ESDHC_MIX_CTRL_TUNING_MASK 0x03c00000
0071 
0072 /* dll control register */
0073 #define ESDHC_DLL_CTRL          0x60
0074 #define ESDHC_DLL_OVERRIDE_VAL_SHIFT    9
0075 #define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
0076 
0077 /* tune control register */
0078 #define ESDHC_TUNE_CTRL_STATUS      0x68
0079 #define  ESDHC_TUNE_CTRL_STEP       1
0080 #define  ESDHC_TUNE_CTRL_MIN        0
0081 #define  ESDHC_TUNE_CTRL_MAX        ((1 << 7) - 1)
0082 
0083 /* strobe dll register */
0084 #define ESDHC_STROBE_DLL_CTRL       0x70
0085 #define ESDHC_STROBE_DLL_CTRL_ENABLE    (1 << 0)
0086 #define ESDHC_STROBE_DLL_CTRL_RESET (1 << 1)
0087 #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT    0x7
0088 #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT  3
0089 #define ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT    (4 << 20)
0090 
0091 #define ESDHC_STROBE_DLL_STATUS     0x74
0092 #define ESDHC_STROBE_DLL_STS_REF_LOCK   (1 << 1)
0093 #define ESDHC_STROBE_DLL_STS_SLV_LOCK   0x1
0094 
0095 #define ESDHC_VEND_SPEC2        0xc8
0096 #define ESDHC_VEND_SPEC2_EN_BUSY_IRQ    (1 << 8)
0097 #define ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN  (1 << 4)
0098 #define ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN  (0 << 4)
0099 #define ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN  (2 << 4)
0100 #define ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN   (1 << 6)
0101 #define ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK    (7 << 4)
0102 
0103 #define ESDHC_TUNING_CTRL       0xcc
0104 #define ESDHC_STD_TUNING_EN     (1 << 24)
0105 /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
0106 #define ESDHC_TUNING_START_TAP_DEFAULT  0x1
0107 #define ESDHC_TUNING_START_TAP_MASK 0x7f
0108 #define ESDHC_TUNING_CMD_CRC_CHECK_DISABLE  (1 << 7)
0109 #define ESDHC_TUNING_STEP_MASK      0x00070000
0110 #define ESDHC_TUNING_STEP_SHIFT     16
0111 
0112 /* pinctrl state */
0113 #define ESDHC_PINCTRL_STATE_100MHZ  "state_100mhz"
0114 #define ESDHC_PINCTRL_STATE_200MHZ  "state_200mhz"
0115 
0116 /*
0117  * Our interpretation of the SDHCI_HOST_CONTROL register
0118  */
0119 #define ESDHC_CTRL_4BITBUS      (0x1 << 1)
0120 #define ESDHC_CTRL_8BITBUS      (0x2 << 1)
0121 #define ESDHC_CTRL_BUSWIDTH_MASK    (0x3 << 1)
0122 #define USDHC_GET_BUSWIDTH(c) (c & ESDHC_CTRL_BUSWIDTH_MASK)
0123 
0124 /*
0125  * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC:
0126  * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
0127  * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
0128  * Define this macro DMA error INT for fsl eSDHC
0129  */
0130 #define ESDHC_INT_VENDOR_SPEC_DMA_ERR   (1 << 28)
0131 
0132 /* the address offset of CQHCI */
0133 #define ESDHC_CQHCI_ADDR_OFFSET     0x100
0134 
0135 /*
0136  * The CMDTYPE of the CMD register (offset 0xE) should be set to
0137  * "11" when the STOP CMD12 is issued on imx53 to abort one
0138  * open ended multi-blk IO. Otherwise the TC INT wouldn't
0139  * be generated.
0140  * In exact block transfer, the controller doesn't complete the
0141  * operations automatically as required at the end of the
0142  * transfer and remains on hold if the abort command is not sent.
0143  * As a result, the TC flag is not asserted and SW received timeout
0144  * exception. Bit1 of Vendor Spec register is used to fix it.
0145  */
0146 #define ESDHC_FLAG_MULTIBLK_NO_INT  BIT(1)
0147 /*
0148  * The flag tells that the ESDHC controller is an USDHC block that is
0149  * integrated on the i.MX6 series.
0150  */
0151 #define ESDHC_FLAG_USDHC        BIT(3)
0152 /* The IP supports manual tuning process */
0153 #define ESDHC_FLAG_MAN_TUNING       BIT(4)
0154 /* The IP supports standard tuning process */
0155 #define ESDHC_FLAG_STD_TUNING       BIT(5)
0156 /* The IP has SDHCI_CAPABILITIES_1 register */
0157 #define ESDHC_FLAG_HAVE_CAP1        BIT(6)
0158 /*
0159  * The IP has erratum ERR004536
0160  * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow,
0161  * when reading data from the card
0162  * This flag is also set for i.MX25 and i.MX35 in order to get
0163  * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits).
0164  */
0165 #define ESDHC_FLAG_ERR004536        BIT(7)
0166 /* The IP supports HS200 mode */
0167 #define ESDHC_FLAG_HS200        BIT(8)
0168 /* The IP supports HS400 mode */
0169 #define ESDHC_FLAG_HS400        BIT(9)
0170 /*
0171  * The IP has errata ERR010450
0172  * uSDHC: Due to the I/O timing limit, for SDR mode, SD card clock can't
0173  * exceed 150MHz, for DDR mode, SD card clock can't exceed 45MHz.
0174  */
0175 #define ESDHC_FLAG_ERR010450        BIT(10)
0176 /* The IP supports HS400ES mode */
0177 #define ESDHC_FLAG_HS400_ES     BIT(11)
0178 /* The IP has Host Controller Interface for Command Queuing */
0179 #define ESDHC_FLAG_CQHCI        BIT(12)
0180 /* need request pmqos during low power */
0181 #define ESDHC_FLAG_PMQOS        BIT(13)
0182 /* The IP state got lost in low power mode */
0183 #define ESDHC_FLAG_STATE_LOST_IN_LPMODE     BIT(14)
0184 /* The IP lost clock rate in PM_RUNTIME */
0185 #define ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME  BIT(15)
0186 /*
0187  * The IP do not support the ACMD23 feature completely when use ADMA mode.
0188  * In ADMA mode, it only use the 16 bit block count of the register 0x4
0189  * (BLOCK_ATT) as the CMD23's argument for ACMD23 mode, which means it will
0190  * ignore the upper 16 bit of the CMD23's argument. This will block the reliable
0191  * write operation in RPMB, because RPMB reliable write need to set the bit31
0192  * of the CMD23's argument.
0193  * imx6qpdl/imx6sx/imx6sl/imx7d has this limitation only for ADMA mode, SDMA
0194  * do not has this limitation. so when these SoC use ADMA mode, it need to
0195  * disable the ACMD23 feature.
0196  */
0197 #define ESDHC_FLAG_BROKEN_AUTO_CMD23    BIT(16)
0198 
0199 /* ERR004536 is not applicable for the IP  */
0200 #define ESDHC_FLAG_SKIP_ERR004536   BIT(17)
0201 
0202 enum wp_types {
0203     ESDHC_WP_NONE,      /* no WP, neither controller nor gpio */
0204     ESDHC_WP_CONTROLLER,    /* mmc controller internal WP */
0205     ESDHC_WP_GPIO,      /* external gpio pin for WP */
0206 };
0207 
0208 enum cd_types {
0209     ESDHC_CD_NONE,      /* no CD, neither controller nor gpio */
0210     ESDHC_CD_CONTROLLER,    /* mmc controller internal CD */
0211     ESDHC_CD_GPIO,      /* external gpio pin for CD */
0212     ESDHC_CD_PERMANENT, /* no CD, card permanently wired to host */
0213 };
0214 
0215 /*
0216  * struct esdhc_platform_data - platform data for esdhc on i.MX
0217  *
0218  * ESDHC_WP(CD)_CONTROLLER type is not available on i.MX25/35.
0219  *
0220  * @wp_type:    type of write_protect method (see wp_types enum above)
0221  * @cd_type:    type of card_detect method (see cd_types enum above)
0222  */
0223 
0224 struct esdhc_platform_data {
0225     enum wp_types wp_type;
0226     enum cd_types cd_type;
0227     int max_bus_width;
0228     unsigned int delay_line;
0229     unsigned int tuning_step;       /* The delay cell steps in tuning procedure */
0230     unsigned int tuning_start_tap;  /* The start delay cell point in tuning procedure */
0231     unsigned int strobe_dll_delay_target;   /* The delay cell for strobe pad (read clock) */
0232 };
0233 
0234 struct esdhc_soc_data {
0235     u32 flags;
0236 };
0237 
0238 static const struct esdhc_soc_data esdhc_imx25_data = {
0239     .flags = ESDHC_FLAG_ERR004536,
0240 };
0241 
0242 static const struct esdhc_soc_data esdhc_imx35_data = {
0243     .flags = ESDHC_FLAG_ERR004536,
0244 };
0245 
0246 static const struct esdhc_soc_data esdhc_imx51_data = {
0247     .flags = 0,
0248 };
0249 
0250 static const struct esdhc_soc_data esdhc_imx53_data = {
0251     .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
0252 };
0253 
0254 static const struct esdhc_soc_data usdhc_imx6q_data = {
0255     .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING
0256             | ESDHC_FLAG_BROKEN_AUTO_CMD23,
0257 };
0258 
0259 static const struct esdhc_soc_data usdhc_imx6sl_data = {
0260     .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
0261             | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536
0262             | ESDHC_FLAG_HS200
0263             | ESDHC_FLAG_BROKEN_AUTO_CMD23,
0264 };
0265 
0266 static const struct esdhc_soc_data usdhc_imx6sll_data = {
0267     .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
0268             | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
0269             | ESDHC_FLAG_HS400
0270             | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
0271 };
0272 
0273 static const struct esdhc_soc_data usdhc_imx6sx_data = {
0274     .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
0275             | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
0276             | ESDHC_FLAG_STATE_LOST_IN_LPMODE
0277             | ESDHC_FLAG_BROKEN_AUTO_CMD23,
0278 };
0279 
0280 static const struct esdhc_soc_data usdhc_imx6ull_data = {
0281     .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
0282             | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
0283             | ESDHC_FLAG_ERR010450
0284             | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
0285 };
0286 
0287 static const struct esdhc_soc_data usdhc_imx7d_data = {
0288     .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
0289             | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
0290             | ESDHC_FLAG_HS400
0291             | ESDHC_FLAG_STATE_LOST_IN_LPMODE
0292             | ESDHC_FLAG_BROKEN_AUTO_CMD23,
0293 };
0294 
0295 static struct esdhc_soc_data usdhc_s32g2_data = {
0296     .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING
0297             | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
0298             | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
0299             | ESDHC_FLAG_SKIP_ERR004536,
0300 };
0301 
0302 static struct esdhc_soc_data usdhc_imx7ulp_data = {
0303     .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
0304             | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
0305             | ESDHC_FLAG_PMQOS | ESDHC_FLAG_HS400
0306             | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
0307 };
0308 static struct esdhc_soc_data usdhc_imxrt1050_data = {
0309     .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_HS200 | ESDHC_FLAG_ERR004536,
0310 };
0311 
0312 static struct esdhc_soc_data usdhc_imx8qxp_data = {
0313     .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
0314             | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
0315             | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
0316             | ESDHC_FLAG_STATE_LOST_IN_LPMODE
0317             | ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME,
0318 };
0319 
0320 static struct esdhc_soc_data usdhc_imx8mm_data = {
0321     .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
0322             | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
0323             | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
0324             | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
0325 };
0326 
0327 struct pltfm_imx_data {
0328     u32 scratchpad;
0329     struct pinctrl *pinctrl;
0330     struct pinctrl_state *pins_100mhz;
0331     struct pinctrl_state *pins_200mhz;
0332     const struct esdhc_soc_data *socdata;
0333     struct esdhc_platform_data boarddata;
0334     struct clk *clk_ipg;
0335     struct clk *clk_ahb;
0336     struct clk *clk_per;
0337     unsigned int actual_clock;
0338     enum {
0339         NO_CMD_PENDING,      /* no multiblock command pending */
0340         MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
0341         WAIT_FOR_INT,        /* sent CMD12, waiting for response INT */
0342     } multiblock_status;
0343     u32 is_ddr;
0344     struct pm_qos_request pm_qos_req;
0345 };
0346 
0347 static const struct of_device_id imx_esdhc_dt_ids[] = {
0348     { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
0349     { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
0350     { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
0351     { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
0352     { .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
0353     { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
0354     { .compatible = "fsl,imx6sll-usdhc", .data = &usdhc_imx6sll_data, },
0355     { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
0356     { .compatible = "fsl,imx6ull-usdhc", .data = &usdhc_imx6ull_data, },
0357     { .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
0358     { .compatible = "fsl,imx7ulp-usdhc", .data = &usdhc_imx7ulp_data, },
0359     { .compatible = "fsl,imx8qxp-usdhc", .data = &usdhc_imx8qxp_data, },
0360     { .compatible = "fsl,imx8mm-usdhc", .data = &usdhc_imx8mm_data, },
0361     { .compatible = "fsl,imxrt1050-usdhc", .data = &usdhc_imxrt1050_data, },
0362     { .compatible = "nxp,s32g2-usdhc", .data = &usdhc_s32g2_data, },
0363     { /* sentinel */ }
0364 };
0365 MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
0366 
0367 static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
0368 {
0369     return data->socdata == &esdhc_imx25_data;
0370 }
0371 
0372 static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
0373 {
0374     return data->socdata == &esdhc_imx53_data;
0375 }
0376 
0377 static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
0378 {
0379     return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
0380 }
0381 
0382 static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
0383 {
0384     void __iomem *base = host->ioaddr + (reg & ~0x3);
0385     u32 shift = (reg & 0x3) * 8;
0386 
0387     writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
0388 }
0389 
0390 #define DRIVER_NAME "sdhci-esdhc-imx"
0391 #define ESDHC_IMX_DUMP(f, x...) \
0392     pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
0393 static void esdhc_dump_debug_regs(struct sdhci_host *host)
0394 {
0395     int i;
0396     char *debug_status[7] = {
0397                  "cmd debug status",
0398                  "data debug status",
0399                  "trans debug status",
0400                  "dma debug status",
0401                  "adma debug status",
0402                  "fifo debug status",
0403                  "async fifo debug status"
0404     };
0405 
0406     ESDHC_IMX_DUMP("========= ESDHC IMX DEBUG STATUS DUMP =========\n");
0407     for (i = 0; i < 7; i++) {
0408         esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK,
0409             ESDHC_DEBUG_SEL_CMD_STATE + i, ESDHC_DEBUG_SEL_REG);
0410         ESDHC_IMX_DUMP("%s:  0x%04x\n", debug_status[i],
0411             readw(host->ioaddr + ESDHC_DEBUG_SEL_AND_STATUS_REG));
0412     }
0413 
0414     esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK, 0, ESDHC_DEBUG_SEL_REG);
0415 
0416 }
0417 
0418 static inline void esdhc_wait_for_card_clock_gate_off(struct sdhci_host *host)
0419 {
0420     u32 present_state;
0421     int ret;
0422 
0423     ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, present_state,
0424                 (present_state & ESDHC_CLOCK_GATE_OFF), 2, 100);
0425     if (ret == -ETIMEDOUT)
0426         dev_warn(mmc_dev(host->mmc), "%s: card clock still not gate off in 100us!.\n", __func__);
0427 }
0428 
0429 /* Enable the auto tuning circuit to check the CMD line and BUS line */
0430 static inline void usdhc_auto_tuning_mode_sel(struct sdhci_host *host)
0431 {
0432     u32 buswidth, auto_tune_buswidth;
0433 
0434     buswidth = USDHC_GET_BUSWIDTH(readl(host->ioaddr + SDHCI_HOST_CONTROL));
0435 
0436     switch (buswidth) {
0437     case ESDHC_CTRL_8BITBUS:
0438         auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN;
0439         break;
0440     case ESDHC_CTRL_4BITBUS:
0441         auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN;
0442         break;
0443     default:    /* 1BITBUS */
0444         auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN;
0445         break;
0446     }
0447 
0448     esdhc_clrset_le(host, ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK,
0449             auto_tune_buswidth | ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN,
0450             ESDHC_VEND_SPEC2);
0451 }
0452 
0453 static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
0454 {
0455     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0456     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
0457     u32 val = readl(host->ioaddr + reg);
0458 
0459     if (unlikely(reg == SDHCI_PRESENT_STATE)) {
0460         u32 fsl_prss = val;
0461         /* save the least 20 bits */
0462         val = fsl_prss & 0x000FFFFF;
0463         /* move dat[0-3] bits */
0464         val |= (fsl_prss & 0x0F000000) >> 4;
0465         /* move cmd line bit */
0466         val |= (fsl_prss & 0x00800000) << 1;
0467     }
0468 
0469     if (unlikely(reg == SDHCI_CAPABILITIES)) {
0470         /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
0471         if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
0472             val &= 0xffff0000;
0473 
0474         /* In FSL esdhc IC module, only bit20 is used to indicate the
0475          * ADMA2 capability of esdhc, but this bit is messed up on
0476          * some SOCs (e.g. on MX25, MX35 this bit is set, but they
0477          * don't actually support ADMA2). So set the BROKEN_ADMA
0478          * quirk on MX25/35 platforms.
0479          */
0480 
0481         if (val & SDHCI_CAN_DO_ADMA1) {
0482             val &= ~SDHCI_CAN_DO_ADMA1;
0483             val |= SDHCI_CAN_DO_ADMA2;
0484         }
0485     }
0486 
0487     if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
0488         if (esdhc_is_usdhc(imx_data)) {
0489             if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
0490                 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
0491             else
0492                 /* imx6q/dl does not have cap_1 register, fake one */
0493                 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
0494                     | SDHCI_SUPPORT_SDR50
0495                     | SDHCI_USE_SDR50_TUNING
0496                     | FIELD_PREP(SDHCI_RETUNING_MODE_MASK,
0497                              SDHCI_TUNING_MODE_3);
0498 
0499             /*
0500              * Do not advertise faster UHS modes if there are no
0501              * pinctrl states for 100MHz/200MHz.
0502              */
0503             if (IS_ERR_OR_NULL(imx_data->pins_100mhz))
0504                 val &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50);
0505             if (IS_ERR_OR_NULL(imx_data->pins_200mhz))
0506                 val &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_HS400);
0507         }
0508     }
0509 
0510     if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
0511         val = 0;
0512         val |= FIELD_PREP(SDHCI_MAX_CURRENT_330_MASK, 0xFF);
0513         val |= FIELD_PREP(SDHCI_MAX_CURRENT_300_MASK, 0xFF);
0514         val |= FIELD_PREP(SDHCI_MAX_CURRENT_180_MASK, 0xFF);
0515     }
0516 
0517     if (unlikely(reg == SDHCI_INT_STATUS)) {
0518         if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
0519             val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
0520             val |= SDHCI_INT_ADMA_ERROR;
0521         }
0522 
0523         /*
0524          * mask off the interrupt we get in response to the manually
0525          * sent CMD12
0526          */
0527         if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
0528             ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
0529             val &= ~SDHCI_INT_RESPONSE;
0530             writel(SDHCI_INT_RESPONSE, host->ioaddr +
0531                            SDHCI_INT_STATUS);
0532             imx_data->multiblock_status = NO_CMD_PENDING;
0533         }
0534     }
0535 
0536     return val;
0537 }
0538 
0539 static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
0540 {
0541     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0542     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
0543     u32 data;
0544 
0545     if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
0546             reg == SDHCI_INT_STATUS)) {
0547         if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
0548             /*
0549              * Clear and then set D3CD bit to avoid missing the
0550              * card interrupt. This is an eSDHC controller problem
0551              * so we need to apply the following workaround: clear
0552              * and set D3CD bit will make eSDHC re-sample the card
0553              * interrupt. In case a card interrupt was lost,
0554              * re-sample it by the following steps.
0555              */
0556             data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
0557             data &= ~ESDHC_CTRL_D3CD;
0558             writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
0559             data |= ESDHC_CTRL_D3CD;
0560             writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
0561         }
0562 
0563         if (val & SDHCI_INT_ADMA_ERROR) {
0564             val &= ~SDHCI_INT_ADMA_ERROR;
0565             val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
0566         }
0567     }
0568 
0569     if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
0570                 && (reg == SDHCI_INT_STATUS)
0571                 && (val & SDHCI_INT_DATA_END))) {
0572             u32 v;
0573             v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
0574             v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
0575             writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
0576 
0577             if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
0578             {
0579                 /* send a manual CMD12 with RESPTYP=none */
0580                 data = MMC_STOP_TRANSMISSION << 24 |
0581                        SDHCI_CMD_ABORTCMD << 16;
0582                 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
0583                 imx_data->multiblock_status = WAIT_FOR_INT;
0584             }
0585     }
0586 
0587     writel(val, host->ioaddr + reg);
0588 }
0589 
0590 static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
0591 {
0592     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0593     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
0594     u16 ret = 0;
0595     u32 val;
0596 
0597     if (unlikely(reg == SDHCI_HOST_VERSION)) {
0598         reg ^= 2;
0599         if (esdhc_is_usdhc(imx_data)) {
0600             /*
0601              * The usdhc register returns a wrong host version.
0602              * Correct it here.
0603              */
0604             return SDHCI_SPEC_300;
0605         }
0606     }
0607 
0608     if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
0609         val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
0610         if (val & ESDHC_VENDOR_SPEC_VSELECT)
0611             ret |= SDHCI_CTRL_VDD_180;
0612 
0613         if (esdhc_is_usdhc(imx_data)) {
0614             if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
0615                 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
0616             else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
0617                 /* the std tuning bits is in ACMD12_ERR for imx6sl */
0618                 val = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
0619         }
0620 
0621         if (val & ESDHC_MIX_CTRL_EXE_TUNE)
0622             ret |= SDHCI_CTRL_EXEC_TUNING;
0623         if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
0624             ret |= SDHCI_CTRL_TUNED_CLK;
0625 
0626         ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
0627 
0628         return ret;
0629     }
0630 
0631     if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
0632         if (esdhc_is_usdhc(imx_data)) {
0633             u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
0634             ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
0635             /* Swap AC23 bit */
0636             if (m & ESDHC_MIX_CTRL_AC23EN) {
0637                 ret &= ~ESDHC_MIX_CTRL_AC23EN;
0638                 ret |= SDHCI_TRNS_AUTO_CMD23;
0639             }
0640         } else {
0641             ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
0642         }
0643 
0644         return ret;
0645     }
0646 
0647     return readw(host->ioaddr + reg);
0648 }
0649 
0650 static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
0651 {
0652     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0653     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
0654     u32 new_val = 0;
0655 
0656     switch (reg) {
0657     case SDHCI_CLOCK_CONTROL:
0658         new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
0659         if (val & SDHCI_CLOCK_CARD_EN)
0660             new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
0661         else
0662             new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
0663         writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
0664         if (!(new_val & ESDHC_VENDOR_SPEC_FRC_SDCLK_ON))
0665             esdhc_wait_for_card_clock_gate_off(host);
0666         return;
0667     case SDHCI_HOST_CONTROL2:
0668         new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
0669         if (val & SDHCI_CTRL_VDD_180)
0670             new_val |= ESDHC_VENDOR_SPEC_VSELECT;
0671         else
0672             new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
0673         writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
0674         if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
0675             u32 v = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
0676             u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
0677             if (val & SDHCI_CTRL_TUNED_CLK) {
0678                 v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
0679             } else {
0680                 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
0681                 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
0682                 m &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
0683             }
0684 
0685             if (val & SDHCI_CTRL_EXEC_TUNING) {
0686                 v |= ESDHC_MIX_CTRL_EXE_TUNE;
0687                 m |= ESDHC_MIX_CTRL_FBCLK_SEL;
0688                 m |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
0689                 usdhc_auto_tuning_mode_sel(host);
0690             } else {
0691                 v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
0692             }
0693 
0694             writel(v, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
0695             writel(m, host->ioaddr + ESDHC_MIX_CTRL);
0696         }
0697         return;
0698     case SDHCI_TRANSFER_MODE:
0699         if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
0700                 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
0701                 && (host->cmd->data->blocks > 1)
0702                 && (host->cmd->data->flags & MMC_DATA_READ)) {
0703             u32 v;
0704             v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
0705             v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
0706             writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
0707         }
0708 
0709         if (esdhc_is_usdhc(imx_data)) {
0710             u32 wml;
0711             u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
0712             /* Swap AC23 bit */
0713             if (val & SDHCI_TRNS_AUTO_CMD23) {
0714                 val &= ~SDHCI_TRNS_AUTO_CMD23;
0715                 val |= ESDHC_MIX_CTRL_AC23EN;
0716             }
0717             m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
0718             writel(m, host->ioaddr + ESDHC_MIX_CTRL);
0719 
0720             /* Set watermark levels for PIO access to maximum value
0721              * (128 words) to accommodate full 512 bytes buffer.
0722              * For DMA access restore the levels to default value.
0723              */
0724             m = readl(host->ioaddr + ESDHC_WTMK_LVL);
0725             if (val & SDHCI_TRNS_DMA) {
0726                 wml = ESDHC_WTMK_LVL_WML_VAL_DEF;
0727             } else {
0728                 u8 ctrl;
0729                 wml = ESDHC_WTMK_LVL_WML_VAL_MAX;
0730 
0731                 /*
0732                  * Since already disable DMA mode, so also need
0733                  * to clear the DMASEL. Otherwise, for standard
0734                  * tuning, when send tuning command, usdhc will
0735                  * still prefetch the ADMA script from wrong
0736                  * DMA address, then we will see IOMMU report
0737                  * some error which show lack of TLB mapping.
0738                  */
0739                 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
0740                 ctrl &= ~SDHCI_CTRL_DMA_MASK;
0741                 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
0742             }
0743             m &= ~(ESDHC_WTMK_LVL_RD_WML_MASK |
0744                    ESDHC_WTMK_LVL_WR_WML_MASK);
0745             m |= (wml << ESDHC_WTMK_LVL_RD_WML_SHIFT) |
0746                  (wml << ESDHC_WTMK_LVL_WR_WML_SHIFT);
0747             writel(m, host->ioaddr + ESDHC_WTMK_LVL);
0748         } else {
0749             /*
0750              * Postpone this write, we must do it together with a
0751              * command write that is down below.
0752              */
0753             imx_data->scratchpad = val;
0754         }
0755         return;
0756     case SDHCI_COMMAND:
0757         if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
0758             val |= SDHCI_CMD_ABORTCMD;
0759 
0760         if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
0761             (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
0762             imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
0763 
0764         if (esdhc_is_usdhc(imx_data))
0765             writel(val << 16,
0766                    host->ioaddr + SDHCI_TRANSFER_MODE);
0767         else
0768             writel(val << 16 | imx_data->scratchpad,
0769                    host->ioaddr + SDHCI_TRANSFER_MODE);
0770         return;
0771     case SDHCI_BLOCK_SIZE:
0772         val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
0773         break;
0774     }
0775     esdhc_clrset_le(host, 0xffff, val, reg);
0776 }
0777 
0778 static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
0779 {
0780     u8 ret;
0781     u32 val;
0782 
0783     switch (reg) {
0784     case SDHCI_HOST_CONTROL:
0785         val = readl(host->ioaddr + reg);
0786 
0787         ret = val & SDHCI_CTRL_LED;
0788         ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
0789         ret |= (val & ESDHC_CTRL_4BITBUS);
0790         ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
0791         return ret;
0792     }
0793 
0794     return readb(host->ioaddr + reg);
0795 }
0796 
0797 static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
0798 {
0799     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0800     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
0801     u32 new_val = 0;
0802     u32 mask;
0803 
0804     switch (reg) {
0805     case SDHCI_POWER_CONTROL:
0806         /*
0807          * FSL put some DMA bits here
0808          * If your board has a regulator, code should be here
0809          */
0810         return;
0811     case SDHCI_HOST_CONTROL:
0812         /* FSL messed up here, so we need to manually compose it. */
0813         new_val = val & SDHCI_CTRL_LED;
0814         /* ensure the endianness */
0815         new_val |= ESDHC_HOST_CONTROL_LE;
0816         /* bits 8&9 are reserved on mx25 */
0817         if (!is_imx25_esdhc(imx_data)) {
0818             /* DMA mode bits are shifted */
0819             new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
0820         }
0821 
0822         /*
0823          * Do not touch buswidth bits here. This is done in
0824          * esdhc_pltfm_bus_width.
0825          * Do not touch the D3CD bit either which is used for the
0826          * SDIO interrupt erratum workaround.
0827          */
0828         mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
0829 
0830         esdhc_clrset_le(host, mask, new_val, reg);
0831         return;
0832     case SDHCI_SOFTWARE_RESET:
0833         if (val & SDHCI_RESET_DATA)
0834             new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
0835         break;
0836     }
0837     esdhc_clrset_le(host, 0xff, val, reg);
0838 
0839     if (reg == SDHCI_SOFTWARE_RESET) {
0840         if (val & SDHCI_RESET_ALL) {
0841             /*
0842              * The esdhc has a design violation to SDHC spec which
0843              * tells that software reset should not affect card
0844              * detection circuit. But esdhc clears its SYSCTL
0845              * register bits [0..2] during the software reset. This
0846              * will stop those clocks that card detection circuit
0847              * relies on. To work around it, we turn the clocks on
0848              * back to keep card detection circuit functional.
0849              */
0850             esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
0851             /*
0852              * The reset on usdhc fails to clear MIX_CTRL register.
0853              * Do it manually here.
0854              */
0855             if (esdhc_is_usdhc(imx_data)) {
0856                 /*
0857                  * the tuning bits should be kept during reset
0858                  */
0859                 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
0860                 writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
0861                         host->ioaddr + ESDHC_MIX_CTRL);
0862                 imx_data->is_ddr = 0;
0863             }
0864         } else if (val & SDHCI_RESET_DATA) {
0865             /*
0866              * The eSDHC DAT line software reset clears at least the
0867              * data transfer width on i.MX25, so make sure that the
0868              * Host Control register is unaffected.
0869              */
0870             esdhc_clrset_le(host, 0xff, new_val,
0871                     SDHCI_HOST_CONTROL);
0872         }
0873     }
0874 }
0875 
0876 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
0877 {
0878     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0879 
0880     return pltfm_host->clock;
0881 }
0882 
0883 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
0884 {
0885     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0886 
0887     return pltfm_host->clock / 256 / 16;
0888 }
0889 
0890 static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
0891                      unsigned int clock)
0892 {
0893     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0894     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
0895     unsigned int host_clock = pltfm_host->clock;
0896     int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
0897     int pre_div = 1;
0898     int div = 1;
0899     int ret;
0900     u32 temp, val;
0901 
0902     if (esdhc_is_usdhc(imx_data)) {
0903         val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
0904         writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
0905             host->ioaddr + ESDHC_VENDOR_SPEC);
0906         esdhc_wait_for_card_clock_gate_off(host);
0907     }
0908 
0909     if (clock == 0) {
0910         host->mmc->actual_clock = 0;
0911         return;
0912     }
0913 
0914     /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
0915     if (is_imx53_esdhc(imx_data)) {
0916         /*
0917          * According to the i.MX53 reference manual, if DLLCTRL[10] can
0918          * be set, then the controller is eSDHCv3, else it is eSDHCv2.
0919          */
0920         val = readl(host->ioaddr + ESDHC_DLL_CTRL);
0921         writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL);
0922         temp = readl(host->ioaddr + ESDHC_DLL_CTRL);
0923         writel(val, host->ioaddr + ESDHC_DLL_CTRL);
0924         if (temp & BIT(10))
0925             pre_div = 2;
0926     }
0927 
0928     temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
0929     temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
0930         | ESDHC_CLOCK_MASK);
0931     sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
0932 
0933     if (imx_data->socdata->flags & ESDHC_FLAG_ERR010450) {
0934         unsigned int max_clock;
0935 
0936         max_clock = imx_data->is_ddr ? 45000000 : 150000000;
0937 
0938         clock = min(clock, max_clock);
0939     }
0940 
0941     while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
0942             pre_div < 256)
0943         pre_div *= 2;
0944 
0945     while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
0946         div++;
0947 
0948     host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
0949     dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
0950         clock, host->mmc->actual_clock);
0951 
0952     pre_div >>= 1;
0953     div--;
0954 
0955     temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
0956     temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
0957         | (div << ESDHC_DIVIDER_SHIFT)
0958         | (pre_div << ESDHC_PREDIV_SHIFT));
0959     sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
0960 
0961     /* need to wait the bit 3 of the PRSSTAT to be set, make sure card clock is stable */
0962     ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, temp,
0963                 (temp & ESDHC_CLOCK_STABLE), 2, 100);
0964     if (ret == -ETIMEDOUT)
0965         dev_warn(mmc_dev(host->mmc), "card clock still not stable in 100us!.\n");
0966 
0967     if (esdhc_is_usdhc(imx_data)) {
0968         val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
0969         writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
0970             host->ioaddr + ESDHC_VENDOR_SPEC);
0971     }
0972 
0973 }
0974 
0975 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
0976 {
0977     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0978     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
0979     struct esdhc_platform_data *boarddata = &imx_data->boarddata;
0980 
0981     switch (boarddata->wp_type) {
0982     case ESDHC_WP_GPIO:
0983         return mmc_gpio_get_ro(host->mmc);
0984     case ESDHC_WP_CONTROLLER:
0985         return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
0986                    SDHCI_WRITE_PROTECT);
0987     case ESDHC_WP_NONE:
0988         break;
0989     }
0990 
0991     return -ENOSYS;
0992 }
0993 
0994 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
0995 {
0996     u32 ctrl;
0997 
0998     switch (width) {
0999     case MMC_BUS_WIDTH_8:
1000         ctrl = ESDHC_CTRL_8BITBUS;
1001         break;
1002     case MMC_BUS_WIDTH_4:
1003         ctrl = ESDHC_CTRL_4BITBUS;
1004         break;
1005     default:
1006         ctrl = 0;
1007         break;
1008     }
1009 
1010     esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
1011             SDHCI_HOST_CONTROL);
1012 }
1013 
1014 static int usdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
1015 {
1016     struct sdhci_host *host = mmc_priv(mmc);
1017 
1018     /*
1019      * i.MX uSDHC internally already uses a fixed optimized timing for
1020      * DDR50, normally does not require tuning for DDR50 mode.
1021      */
1022     if (host->timing == MMC_TIMING_UHS_DDR50)
1023         return 0;
1024 
1025     return sdhci_execute_tuning(mmc, opcode);
1026 }
1027 
1028 static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
1029 {
1030     u32 reg;
1031     u8 sw_rst;
1032     int ret;
1033 
1034     /* FIXME: delay a bit for card to be ready for next tuning due to errors */
1035     mdelay(1);
1036 
1037     /* IC suggest to reset USDHC before every tuning command */
1038     esdhc_clrset_le(host, 0xff, SDHCI_RESET_ALL, SDHCI_SOFTWARE_RESET);
1039     ret = readb_poll_timeout(host->ioaddr + SDHCI_SOFTWARE_RESET, sw_rst,
1040                 !(sw_rst & SDHCI_RESET_ALL), 10, 100);
1041     if (ret == -ETIMEDOUT)
1042         dev_warn(mmc_dev(host->mmc),
1043         "warning! RESET_ALL never complete before sending tuning command\n");
1044 
1045     reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
1046     reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
1047             ESDHC_MIX_CTRL_FBCLK_SEL;
1048     writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
1049     writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1050     dev_dbg(mmc_dev(host->mmc),
1051         "tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
1052             val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
1053 }
1054 
1055 static void esdhc_post_tuning(struct sdhci_host *host)
1056 {
1057     u32 reg;
1058 
1059     usdhc_auto_tuning_mode_sel(host);
1060 
1061     reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
1062     reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
1063     reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
1064     writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
1065 }
1066 
1067 static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
1068 {
1069     int min, max, avg, ret;
1070 
1071     /* find the mininum delay first which can pass tuning */
1072     min = ESDHC_TUNE_CTRL_MIN;
1073     while (min < ESDHC_TUNE_CTRL_MAX) {
1074         esdhc_prepare_tuning(host, min);
1075         if (!mmc_send_tuning(host->mmc, opcode, NULL))
1076             break;
1077         min += ESDHC_TUNE_CTRL_STEP;
1078     }
1079 
1080     /* find the maxinum delay which can not pass tuning */
1081     max = min + ESDHC_TUNE_CTRL_STEP;
1082     while (max < ESDHC_TUNE_CTRL_MAX) {
1083         esdhc_prepare_tuning(host, max);
1084         if (mmc_send_tuning(host->mmc, opcode, NULL)) {
1085             max -= ESDHC_TUNE_CTRL_STEP;
1086             break;
1087         }
1088         max += ESDHC_TUNE_CTRL_STEP;
1089     }
1090 
1091     /* use average delay to get the best timing */
1092     avg = (min + max) / 2;
1093     esdhc_prepare_tuning(host, avg);
1094     ret = mmc_send_tuning(host->mmc, opcode, NULL);
1095     esdhc_post_tuning(host);
1096 
1097     dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
1098         ret ? "failed" : "passed", avg, ret);
1099 
1100     return ret;
1101 }
1102 
1103 static void esdhc_hs400_enhanced_strobe(struct mmc_host *mmc, struct mmc_ios *ios)
1104 {
1105     struct sdhci_host *host = mmc_priv(mmc);
1106     u32 m;
1107 
1108     m = readl(host->ioaddr + ESDHC_MIX_CTRL);
1109     if (ios->enhanced_strobe)
1110         m |= ESDHC_MIX_CTRL_HS400_ES_EN;
1111     else
1112         m &= ~ESDHC_MIX_CTRL_HS400_ES_EN;
1113     writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1114 }
1115 
1116 static int esdhc_change_pinstate(struct sdhci_host *host,
1117                         unsigned int uhs)
1118 {
1119     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1120     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1121     struct pinctrl_state *pinctrl;
1122 
1123     dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
1124 
1125     if (IS_ERR(imx_data->pinctrl) ||
1126         IS_ERR(imx_data->pins_100mhz) ||
1127         IS_ERR(imx_data->pins_200mhz))
1128         return -EINVAL;
1129 
1130     switch (uhs) {
1131     case MMC_TIMING_UHS_SDR50:
1132     case MMC_TIMING_UHS_DDR50:
1133         pinctrl = imx_data->pins_100mhz;
1134         break;
1135     case MMC_TIMING_UHS_SDR104:
1136     case MMC_TIMING_MMC_HS200:
1137     case MMC_TIMING_MMC_HS400:
1138         pinctrl = imx_data->pins_200mhz;
1139         break;
1140     default:
1141         /* back to default state for other legacy timing */
1142         return pinctrl_select_default_state(mmc_dev(host->mmc));
1143     }
1144 
1145     return pinctrl_select_state(imx_data->pinctrl, pinctrl);
1146 }
1147 
1148 /*
1149  * For HS400 eMMC, there is a data_strobe line. This signal is generated
1150  * by the device and used for data output and CRC status response output
1151  * in HS400 mode. The frequency of this signal follows the frequency of
1152  * CLK generated by host. The host receives the data which is aligned to the
1153  * edge of data_strobe line. Due to the time delay between CLK line and
1154  * data_strobe line, if the delay time is larger than one clock cycle,
1155  * then CLK and data_strobe line will be misaligned, read error shows up.
1156  */
1157 static void esdhc_set_strobe_dll(struct sdhci_host *host)
1158 {
1159     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1160     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1161     u32 strobe_delay;
1162     u32 v;
1163     int ret;
1164 
1165     /* disable clock before enabling strobe dll */
1166     writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
1167         ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
1168         host->ioaddr + ESDHC_VENDOR_SPEC);
1169     esdhc_wait_for_card_clock_gate_off(host);
1170 
1171     /* force a reset on strobe dll */
1172     writel(ESDHC_STROBE_DLL_CTRL_RESET,
1173         host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1174     /* clear the reset bit on strobe dll before any setting */
1175     writel(0, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1176 
1177     /*
1178      * enable strobe dll ctrl and adjust the delay target
1179      * for the uSDHC loopback read clock
1180      */
1181     if (imx_data->boarddata.strobe_dll_delay_target)
1182         strobe_delay = imx_data->boarddata.strobe_dll_delay_target;
1183     else
1184         strobe_delay = ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT;
1185     v = ESDHC_STROBE_DLL_CTRL_ENABLE |
1186         ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
1187         (strobe_delay << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
1188     writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1189 
1190     /* wait max 50us to get the REF/SLV lock */
1191     ret = readl_poll_timeout(host->ioaddr + ESDHC_STROBE_DLL_STATUS, v,
1192         ((v & ESDHC_STROBE_DLL_STS_REF_LOCK) && (v & ESDHC_STROBE_DLL_STS_SLV_LOCK)), 1, 50);
1193     if (ret == -ETIMEDOUT)
1194         dev_warn(mmc_dev(host->mmc),
1195         "warning! HS400 strobe DLL status REF/SLV not lock in 50us, STROBE DLL status is %x!\n", v);
1196 }
1197 
1198 static void esdhc_reset_tuning(struct sdhci_host *host)
1199 {
1200     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1201     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1202     u32 ctrl;
1203     int ret;
1204 
1205     /* Reset the tuning circuit */
1206     if (esdhc_is_usdhc(imx_data)) {
1207         if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1208             ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
1209             ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1210             ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
1211             writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
1212             writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1213         } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1214             ctrl = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1215             ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1216             ctrl &= ~ESDHC_MIX_CTRL_EXE_TUNE;
1217             writel(ctrl, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1218             /* Make sure ESDHC_MIX_CTRL_EXE_TUNE cleared */
1219             ret = readl_poll_timeout(host->ioaddr + SDHCI_AUTO_CMD_STATUS,
1220                 ctrl, !(ctrl & ESDHC_MIX_CTRL_EXE_TUNE), 1, 50);
1221             if (ret == -ETIMEDOUT)
1222                 dev_warn(mmc_dev(host->mmc),
1223                  "Warning! clear execute tuning bit failed\n");
1224             /*
1225              * SDHCI_INT_DATA_AVAIL is W1C bit, set this bit will clear the
1226              * usdhc IP internal logic flag execute_tuning_with_clr_buf, which
1227              * will finally make sure the normal data transfer logic correct.
1228              */
1229             ctrl = readl(host->ioaddr + SDHCI_INT_STATUS);
1230             ctrl |= SDHCI_INT_DATA_AVAIL;
1231             writel(ctrl, host->ioaddr + SDHCI_INT_STATUS);
1232         }
1233     }
1234 }
1235 
1236 static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1237 {
1238     u32 m;
1239     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1240     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1241     struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1242 
1243     /* disable ddr mode and disable HS400 mode */
1244     m = readl(host->ioaddr + ESDHC_MIX_CTRL);
1245     m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
1246     imx_data->is_ddr = 0;
1247 
1248     switch (timing) {
1249     case MMC_TIMING_UHS_SDR12:
1250     case MMC_TIMING_UHS_SDR25:
1251     case MMC_TIMING_UHS_SDR50:
1252     case MMC_TIMING_UHS_SDR104:
1253     case MMC_TIMING_MMC_HS:
1254     case MMC_TIMING_MMC_HS200:
1255         writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1256         break;
1257     case MMC_TIMING_UHS_DDR50:
1258     case MMC_TIMING_MMC_DDR52:
1259         m |= ESDHC_MIX_CTRL_DDREN;
1260         writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1261         imx_data->is_ddr = 1;
1262         if (boarddata->delay_line) {
1263             u32 v;
1264             v = boarddata->delay_line <<
1265                 ESDHC_DLL_OVERRIDE_VAL_SHIFT |
1266                 (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
1267             if (is_imx53_esdhc(imx_data))
1268                 v <<= 1;
1269             writel(v, host->ioaddr + ESDHC_DLL_CTRL);
1270         }
1271         break;
1272     case MMC_TIMING_MMC_HS400:
1273         m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
1274         writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1275         imx_data->is_ddr = 1;
1276         /* update clock after enable DDR for strobe DLL lock */
1277         host->ops->set_clock(host, host->clock);
1278         esdhc_set_strobe_dll(host);
1279         break;
1280     case MMC_TIMING_LEGACY:
1281     default:
1282         esdhc_reset_tuning(host);
1283         break;
1284     }
1285 
1286     esdhc_change_pinstate(host, timing);
1287 }
1288 
1289 static void esdhc_reset(struct sdhci_host *host, u8 mask)
1290 {
1291     sdhci_reset(host, mask);
1292 
1293     sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1294     sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1295 }
1296 
1297 static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host)
1298 {
1299     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1300     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1301 
1302     /* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
1303     return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
1304 }
1305 
1306 static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
1307 {
1308     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1309     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1310 
1311     /* use maximum timeout counter */
1312     esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
1313             esdhc_is_usdhc(imx_data) ? 0xF : 0xE,
1314             SDHCI_TIMEOUT_CONTROL);
1315 }
1316 
1317 static u32 esdhc_cqhci_irq(struct sdhci_host *host, u32 intmask)
1318 {
1319     int cmd_error = 0;
1320     int data_error = 0;
1321 
1322     if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
1323         return intmask;
1324 
1325     cqhci_irq(host->mmc, intmask, cmd_error, data_error);
1326 
1327     return 0;
1328 }
1329 
1330 static struct sdhci_ops sdhci_esdhc_ops = {
1331     .read_l = esdhc_readl_le,
1332     .read_w = esdhc_readw_le,
1333     .read_b = esdhc_readb_le,
1334     .write_l = esdhc_writel_le,
1335     .write_w = esdhc_writew_le,
1336     .write_b = esdhc_writeb_le,
1337     .set_clock = esdhc_pltfm_set_clock,
1338     .get_max_clock = esdhc_pltfm_get_max_clock,
1339     .get_min_clock = esdhc_pltfm_get_min_clock,
1340     .get_max_timeout_count = esdhc_get_max_timeout_count,
1341     .get_ro = esdhc_pltfm_get_ro,
1342     .set_timeout = esdhc_set_timeout,
1343     .set_bus_width = esdhc_pltfm_set_bus_width,
1344     .set_uhs_signaling = esdhc_set_uhs_signaling,
1345     .reset = esdhc_reset,
1346     .irq = esdhc_cqhci_irq,
1347     .dump_vendor_regs = esdhc_dump_debug_regs,
1348 };
1349 
1350 static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
1351     .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
1352             | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
1353             | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
1354             | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
1355     .ops = &sdhci_esdhc_ops,
1356 };
1357 
1358 static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
1359 {
1360     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1361     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1362     struct cqhci_host *cq_host = host->mmc->cqe_private;
1363     int tmp;
1364 
1365     if (esdhc_is_usdhc(imx_data)) {
1366         /*
1367          * The imx6q ROM code will change the default watermark
1368          * level setting to something insane.  Change it back here.
1369          */
1370         writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
1371 
1372         /*
1373          * ROM code will change the bit burst_length_enable setting
1374          * to zero if this usdhc is chosen to boot system. Change
1375          * it back here, otherwise it will impact the performance a
1376          * lot. This bit is used to enable/disable the burst length
1377          * for the external AHB2AXI bridge. It's useful especially
1378          * for INCR transfer because without burst length indicator,
1379          * the AHB2AXI bridge does not know the burst length in
1380          * advance. And without burst length indicator, AHB INCR
1381          * transfer can only be converted to singles on the AXI side.
1382          */
1383         writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
1384             | ESDHC_BURST_LEN_EN_INCR,
1385             host->ioaddr + SDHCI_HOST_CONTROL);
1386 
1387         /*
1388          * erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
1389          * TO1.1, it's harmless for MX6SL
1390          */
1391         if (!(imx_data->socdata->flags & ESDHC_FLAG_SKIP_ERR004536)) {
1392             writel(readl(host->ioaddr + 0x6c) & ~BIT(7),
1393                 host->ioaddr + 0x6c);
1394         }
1395 
1396         /* disable DLL_CTRL delay line settings */
1397         writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
1398 
1399         /*
1400          * For the case of command with busy, if set the bit
1401          * ESDHC_VEND_SPEC2_EN_BUSY_IRQ, USDHC will generate a
1402          * transfer complete interrupt when busy is deasserted.
1403          * When CQHCI use DCMD to send a CMD need R1b respons,
1404          * CQHCI require to set ESDHC_VEND_SPEC2_EN_BUSY_IRQ,
1405          * otherwise DCMD will always meet timeout waiting for
1406          * hardware interrupt issue.
1407          */
1408         if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1409             tmp = readl(host->ioaddr + ESDHC_VEND_SPEC2);
1410             tmp |= ESDHC_VEND_SPEC2_EN_BUSY_IRQ;
1411             writel(tmp, host->ioaddr + ESDHC_VEND_SPEC2);
1412 
1413             host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
1414         }
1415 
1416         if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1417             tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1418             tmp |= ESDHC_STD_TUNING_EN |
1419                 ESDHC_TUNING_START_TAP_DEFAULT;
1420             if (imx_data->boarddata.tuning_start_tap) {
1421                 tmp &= ~ESDHC_TUNING_START_TAP_MASK;
1422                 tmp |= imx_data->boarddata.tuning_start_tap;
1423             }
1424 
1425             if (imx_data->boarddata.tuning_step) {
1426                 tmp &= ~ESDHC_TUNING_STEP_MASK;
1427                 tmp |= imx_data->boarddata.tuning_step
1428                     << ESDHC_TUNING_STEP_SHIFT;
1429             }
1430 
1431             /* Disable the CMD CRC check for tuning, if not, need to
1432              * add some delay after every tuning command, because
1433              * hardware standard tuning logic will directly go to next
1434              * step once it detect the CMD CRC error, will not wait for
1435              * the card side to finally send out the tuning data, trigger
1436              * the buffer read ready interrupt immediately. If usdhc send
1437              * the next tuning command some eMMC card will stuck, can't
1438              * response, block the tuning procedure or the first command
1439              * after the whole tuning procedure always can't get any response.
1440              */
1441             tmp |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
1442             writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1443         } else if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1444             /*
1445              * ESDHC_STD_TUNING_EN may be configed in bootloader
1446              * or ROM code, so clear this bit here to make sure
1447              * the manual tuning can work.
1448              */
1449             tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1450             tmp &= ~ESDHC_STD_TUNING_EN;
1451             writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1452         }
1453 
1454         /*
1455          * On i.MX8MM, we are running Dual Linux OS, with 1st Linux using SD Card
1456          * as rootfs storage, 2nd Linux using eMMC as rootfs storage. We let the
1457          * the 1st linux configure power/clock for the 2nd Linux.
1458          *
1459          * When the 2nd Linux is booting into rootfs stage, we let the 1st Linux
1460          * to destroy the 2nd linux, then restart the 2nd linux, we met SDHCI dump.
1461          * After we clear the pending interrupt and halt CQCTL, issue gone.
1462          */
1463         if (cq_host) {
1464             tmp = cqhci_readl(cq_host, CQHCI_IS);
1465             cqhci_writel(cq_host, tmp, CQHCI_IS);
1466             cqhci_writel(cq_host, CQHCI_HALT, CQHCI_CTL);
1467         }
1468     }
1469 }
1470 
1471 static void esdhc_cqe_enable(struct mmc_host *mmc)
1472 {
1473     struct sdhci_host *host = mmc_priv(mmc);
1474     struct cqhci_host *cq_host = mmc->cqe_private;
1475     u32 reg;
1476     u16 mode;
1477     int count = 10;
1478 
1479     /*
1480      * CQE gets stuck if it sees Buffer Read Enable bit set, which can be
1481      * the case after tuning, so ensure the buffer is drained.
1482      */
1483     reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1484     while (reg & SDHCI_DATA_AVAILABLE) {
1485         sdhci_readl(host, SDHCI_BUFFER);
1486         reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1487         if (count-- == 0) {
1488             dev_warn(mmc_dev(host->mmc),
1489                 "CQE may get stuck because the Buffer Read Enable bit is set\n");
1490             break;
1491         }
1492         mdelay(1);
1493     }
1494 
1495     /*
1496      * Runtime resume will reset the entire host controller, which
1497      * will also clear the DMAEN/BCEN of register ESDHC_MIX_CTRL.
1498      * Here set DMAEN and BCEN when enable CMDQ.
1499      */
1500     mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
1501     if (host->flags & SDHCI_REQ_USE_DMA)
1502         mode |= SDHCI_TRNS_DMA;
1503     if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
1504         mode |= SDHCI_TRNS_BLK_CNT_EN;
1505     sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
1506 
1507     /*
1508      * Though Runtime resume reset the entire host controller,
1509      * but do not impact the CQHCI side, need to clear the
1510      * HALT bit, avoid CQHCI stuck in the first request when
1511      * system resume back.
1512      */
1513     cqhci_writel(cq_host, 0, CQHCI_CTL);
1514     if (cqhci_readl(cq_host, CQHCI_CTL) && CQHCI_HALT)
1515         dev_err(mmc_dev(host->mmc),
1516             "failed to exit halt state when enable CQE\n");
1517 
1518 
1519     sdhci_cqe_enable(mmc);
1520 }
1521 
1522 static void esdhc_sdhci_dumpregs(struct mmc_host *mmc)
1523 {
1524     sdhci_dumpregs(mmc_priv(mmc));
1525 }
1526 
1527 static const struct cqhci_host_ops esdhc_cqhci_ops = {
1528     .enable     = esdhc_cqe_enable,
1529     .disable    = sdhci_cqe_disable,
1530     .dumpregs   = esdhc_sdhci_dumpregs,
1531 };
1532 
1533 static int
1534 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
1535              struct sdhci_host *host,
1536              struct pltfm_imx_data *imx_data)
1537 {
1538     struct device_node *np = pdev->dev.of_node;
1539     struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1540     int ret;
1541 
1542     if (of_get_property(np, "fsl,wp-controller", NULL))
1543         boarddata->wp_type = ESDHC_WP_CONTROLLER;
1544 
1545     /*
1546      * If we have this property, then activate WP check.
1547      * Retrieveing and requesting the actual WP GPIO will happen
1548      * in the call to mmc_of_parse().
1549      */
1550     if (of_property_read_bool(np, "wp-gpios"))
1551         boarddata->wp_type = ESDHC_WP_GPIO;
1552 
1553     of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
1554     of_property_read_u32(np, "fsl,tuning-start-tap",
1555                  &boarddata->tuning_start_tap);
1556 
1557     of_property_read_u32(np, "fsl,strobe-dll-delay-target",
1558                 &boarddata->strobe_dll_delay_target);
1559     if (of_find_property(np, "no-1-8-v", NULL))
1560         host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1561 
1562     if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
1563         boarddata->delay_line = 0;
1564 
1565     mmc_of_parse_voltage(host->mmc, &host->ocr_mask);
1566 
1567     if (esdhc_is_usdhc(imx_data) && !IS_ERR(imx_data->pinctrl)) {
1568         imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1569                         ESDHC_PINCTRL_STATE_100MHZ);
1570         imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1571                         ESDHC_PINCTRL_STATE_200MHZ);
1572     }
1573 
1574     /* call to generic mmc_of_parse to support additional capabilities */
1575     ret = mmc_of_parse(host->mmc);
1576     if (ret)
1577         return ret;
1578 
1579     if (mmc_gpio_get_cd(host->mmc) >= 0)
1580         host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1581 
1582     return 0;
1583 }
1584 
1585 static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
1586 {
1587     struct sdhci_pltfm_host *pltfm_host;
1588     struct sdhci_host *host;
1589     struct cqhci_host *cq_host;
1590     int err;
1591     struct pltfm_imx_data *imx_data;
1592 
1593     host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
1594                 sizeof(*imx_data));
1595     if (IS_ERR(host))
1596         return PTR_ERR(host);
1597 
1598     pltfm_host = sdhci_priv(host);
1599 
1600     imx_data = sdhci_pltfm_priv(pltfm_host);
1601 
1602     imx_data->socdata = device_get_match_data(&pdev->dev);
1603 
1604     if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1605         cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
1606 
1607     imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1608     if (IS_ERR(imx_data->clk_ipg)) {
1609         err = PTR_ERR(imx_data->clk_ipg);
1610         goto free_sdhci;
1611     }
1612 
1613     imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1614     if (IS_ERR(imx_data->clk_ahb)) {
1615         err = PTR_ERR(imx_data->clk_ahb);
1616         goto free_sdhci;
1617     }
1618 
1619     imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
1620     if (IS_ERR(imx_data->clk_per)) {
1621         err = PTR_ERR(imx_data->clk_per);
1622         goto free_sdhci;
1623     }
1624 
1625     pltfm_host->clk = imx_data->clk_per;
1626     pltfm_host->clock = clk_get_rate(pltfm_host->clk);
1627     err = clk_prepare_enable(imx_data->clk_per);
1628     if (err)
1629         goto free_sdhci;
1630     err = clk_prepare_enable(imx_data->clk_ipg);
1631     if (err)
1632         goto disable_per_clk;
1633     err = clk_prepare_enable(imx_data->clk_ahb);
1634     if (err)
1635         goto disable_ipg_clk;
1636 
1637     imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
1638     if (IS_ERR(imx_data->pinctrl))
1639         dev_warn(mmc_dev(host->mmc), "could not get pinctrl\n");
1640 
1641     if (esdhc_is_usdhc(imx_data)) {
1642         host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
1643         host->mmc->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR;
1644 
1645         /* GPIO CD can be set as a wakeup source */
1646         host->mmc->caps |= MMC_CAP_CD_WAKE;
1647 
1648         if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
1649             host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
1650 
1651         /* clear tuning bits in case ROM has set it already */
1652         writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
1653         writel(0x0, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1654         writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1655 
1656         /*
1657          * Link usdhc specific mmc_host_ops execute_tuning function,
1658          * to replace the standard one in sdhci_ops.
1659          */
1660         host->mmc_host_ops.execute_tuning = usdhc_execute_tuning;
1661     }
1662 
1663     if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
1664         sdhci_esdhc_ops.platform_execute_tuning =
1665                     esdhc_executing_tuning;
1666 
1667     if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
1668         host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
1669 
1670     if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
1671         host->mmc->caps2 |= MMC_CAP2_HS400;
1672 
1673     if (imx_data->socdata->flags & ESDHC_FLAG_BROKEN_AUTO_CMD23)
1674         host->quirks2 |= SDHCI_QUIRK2_ACMD23_BROKEN;
1675 
1676     if (imx_data->socdata->flags & ESDHC_FLAG_HS400_ES) {
1677         host->mmc->caps2 |= MMC_CAP2_HS400_ES;
1678         host->mmc_host_ops.hs400_enhanced_strobe =
1679                     esdhc_hs400_enhanced_strobe;
1680     }
1681 
1682     if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1683         host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
1684         cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL);
1685         if (!cq_host) {
1686             err = -ENOMEM;
1687             goto disable_ahb_clk;
1688         }
1689 
1690         cq_host->mmio = host->ioaddr + ESDHC_CQHCI_ADDR_OFFSET;
1691         cq_host->ops = &esdhc_cqhci_ops;
1692 
1693         err = cqhci_init(cq_host, host->mmc, false);
1694         if (err)
1695             goto disable_ahb_clk;
1696     }
1697 
1698     err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
1699     if (err)
1700         goto disable_ahb_clk;
1701 
1702     sdhci_esdhc_imx_hwinit(host);
1703 
1704     err = sdhci_add_host(host);
1705     if (err)
1706         goto disable_ahb_clk;
1707 
1708     /*
1709      * Setup the wakeup capability here, let user to decide
1710      * whether need to enable this wakeup through sysfs interface.
1711      */
1712     if ((host->mmc->pm_caps & MMC_PM_KEEP_POWER) &&
1713             (host->mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ))
1714         device_set_wakeup_capable(&pdev->dev, true);
1715 
1716     pm_runtime_set_active(&pdev->dev);
1717     pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1718     pm_runtime_use_autosuspend(&pdev->dev);
1719     pm_suspend_ignore_children(&pdev->dev, 1);
1720     pm_runtime_enable(&pdev->dev);
1721 
1722     return 0;
1723 
1724 disable_ahb_clk:
1725     clk_disable_unprepare(imx_data->clk_ahb);
1726 disable_ipg_clk:
1727     clk_disable_unprepare(imx_data->clk_ipg);
1728 disable_per_clk:
1729     clk_disable_unprepare(imx_data->clk_per);
1730 free_sdhci:
1731     if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1732         cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1733     sdhci_pltfm_free(pdev);
1734     return err;
1735 }
1736 
1737 static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
1738 {
1739     struct sdhci_host *host = platform_get_drvdata(pdev);
1740     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1741     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1742     int dead;
1743 
1744     pm_runtime_get_sync(&pdev->dev);
1745     dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1746     pm_runtime_disable(&pdev->dev);
1747     pm_runtime_put_noidle(&pdev->dev);
1748 
1749     sdhci_remove_host(host, dead);
1750 
1751     clk_disable_unprepare(imx_data->clk_per);
1752     clk_disable_unprepare(imx_data->clk_ipg);
1753     clk_disable_unprepare(imx_data->clk_ahb);
1754 
1755     if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1756         cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1757 
1758     sdhci_pltfm_free(pdev);
1759 
1760     return 0;
1761 }
1762 
1763 #ifdef CONFIG_PM_SLEEP
1764 static int sdhci_esdhc_suspend(struct device *dev)
1765 {
1766     struct sdhci_host *host = dev_get_drvdata(dev);
1767     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1768     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1769     int ret;
1770 
1771     if (host->mmc->caps2 & MMC_CAP2_CQE) {
1772         ret = cqhci_suspend(host->mmc);
1773         if (ret)
1774             return ret;
1775     }
1776 
1777     if ((imx_data->socdata->flags & ESDHC_FLAG_STATE_LOST_IN_LPMODE) &&
1778         (host->tuning_mode != SDHCI_TUNING_MODE_1)) {
1779         mmc_retune_timer_stop(host->mmc);
1780         mmc_retune_needed(host->mmc);
1781     }
1782 
1783     if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1784         mmc_retune_needed(host->mmc);
1785 
1786     ret = sdhci_suspend_host(host);
1787     if (ret)
1788         return ret;
1789 
1790     ret = pinctrl_pm_select_sleep_state(dev);
1791     if (ret)
1792         return ret;
1793 
1794     ret = mmc_gpio_set_cd_wake(host->mmc, true);
1795 
1796     return ret;
1797 }
1798 
1799 static int sdhci_esdhc_resume(struct device *dev)
1800 {
1801     struct sdhci_host *host = dev_get_drvdata(dev);
1802     int ret;
1803 
1804     ret = pinctrl_pm_select_default_state(dev);
1805     if (ret)
1806         return ret;
1807 
1808     /* re-initialize hw state in case it's lost in low power mode */
1809     sdhci_esdhc_imx_hwinit(host);
1810 
1811     ret = sdhci_resume_host(host);
1812     if (ret)
1813         return ret;
1814 
1815     if (host->mmc->caps2 & MMC_CAP2_CQE)
1816         ret = cqhci_resume(host->mmc);
1817 
1818     if (!ret)
1819         ret = mmc_gpio_set_cd_wake(host->mmc, false);
1820 
1821     return ret;
1822 }
1823 #endif
1824 
1825 #ifdef CONFIG_PM
1826 static int sdhci_esdhc_runtime_suspend(struct device *dev)
1827 {
1828     struct sdhci_host *host = dev_get_drvdata(dev);
1829     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1830     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1831     int ret;
1832 
1833     if (host->mmc->caps2 & MMC_CAP2_CQE) {
1834         ret = cqhci_suspend(host->mmc);
1835         if (ret)
1836             return ret;
1837     }
1838 
1839     ret = sdhci_runtime_suspend_host(host);
1840     if (ret)
1841         return ret;
1842 
1843     if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1844         mmc_retune_needed(host->mmc);
1845 
1846     imx_data->actual_clock = host->mmc->actual_clock;
1847     esdhc_pltfm_set_clock(host, 0);
1848     clk_disable_unprepare(imx_data->clk_per);
1849     clk_disable_unprepare(imx_data->clk_ipg);
1850     clk_disable_unprepare(imx_data->clk_ahb);
1851 
1852     if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1853         cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1854 
1855     return ret;
1856 }
1857 
1858 static int sdhci_esdhc_runtime_resume(struct device *dev)
1859 {
1860     struct sdhci_host *host = dev_get_drvdata(dev);
1861     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1862     struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1863     int err;
1864 
1865     if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1866         cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
1867 
1868     if (imx_data->socdata->flags & ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME)
1869         clk_set_rate(imx_data->clk_per, pltfm_host->clock);
1870 
1871     err = clk_prepare_enable(imx_data->clk_ahb);
1872     if (err)
1873         goto remove_pm_qos_request;
1874 
1875     err = clk_prepare_enable(imx_data->clk_per);
1876     if (err)
1877         goto disable_ahb_clk;
1878 
1879     err = clk_prepare_enable(imx_data->clk_ipg);
1880     if (err)
1881         goto disable_per_clk;
1882 
1883     esdhc_pltfm_set_clock(host, imx_data->actual_clock);
1884 
1885     err = sdhci_runtime_resume_host(host, 0);
1886     if (err)
1887         goto disable_ipg_clk;
1888 
1889     if (host->mmc->caps2 & MMC_CAP2_CQE)
1890         err = cqhci_resume(host->mmc);
1891 
1892     return err;
1893 
1894 disable_ipg_clk:
1895     clk_disable_unprepare(imx_data->clk_ipg);
1896 disable_per_clk:
1897     clk_disable_unprepare(imx_data->clk_per);
1898 disable_ahb_clk:
1899     clk_disable_unprepare(imx_data->clk_ahb);
1900 remove_pm_qos_request:
1901     if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1902         cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1903     return err;
1904 }
1905 #endif
1906 
1907 static const struct dev_pm_ops sdhci_esdhc_pmops = {
1908     SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
1909     SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
1910                 sdhci_esdhc_runtime_resume, NULL)
1911 };
1912 
1913 static struct platform_driver sdhci_esdhc_imx_driver = {
1914     .driver     = {
1915         .name   = "sdhci-esdhc-imx",
1916         .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1917         .of_match_table = imx_esdhc_dt_ids,
1918         .pm = &sdhci_esdhc_pmops,
1919     },
1920     .probe      = sdhci_esdhc_imx_probe,
1921     .remove     = sdhci_esdhc_imx_remove,
1922 };
1923 
1924 module_platform_driver(sdhci_esdhc_imx_driver);
1925 
1926 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1927 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
1928 MODULE_LICENSE("GPL v2");