linux/drivers/mmc/host/sdhci-esdhc-imx.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Freescale eSDHC i.MX controller driver for the platform bus.
   4 *
   5 * derived from the OF-version.
   6 *
   7 * Copyright (c) 2010 Pengutronix e.K.
   8 *   Author: Wolfram Sang <kernel@pengutronix.de>
   9 */
  10
  11#include <linux/bitfield.h>
  12#include <linux/io.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>
  24#include <linux/of.h>
  25#include <linux/of_device.h>
  26#include <linux/pinctrl/consumer.h>
  27#include <linux/platform_data/mmc-esdhc-imx.h>
  28#include <linux/pm_runtime.h>
  29#include "sdhci-pltfm.h"
  30#include "sdhci-esdhc.h"
  31#include "cqhci.h"
  32
  33#define ESDHC_SYS_CTRL_DTOCV_MASK       0x0f
  34#define ESDHC_CTRL_D3CD                 0x08
  35#define ESDHC_BURST_LEN_EN_INCR         (1 << 27)
  36/* VENDOR SPEC register */
  37#define ESDHC_VENDOR_SPEC               0xc0
  38#define  ESDHC_VENDOR_SPEC_SDIO_QUIRK   (1 << 1)
  39#define  ESDHC_VENDOR_SPEC_VSELECT      (1 << 1)
  40#define  ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
  41#define ESDHC_WTMK_LVL                  0x44
  42#define  ESDHC_WTMK_DEFAULT_VAL         0x10401040
  43#define  ESDHC_WTMK_LVL_RD_WML_MASK     0x000000FF
  44#define  ESDHC_WTMK_LVL_RD_WML_SHIFT    0
  45#define  ESDHC_WTMK_LVL_WR_WML_MASK     0x00FF0000
  46#define  ESDHC_WTMK_LVL_WR_WML_SHIFT    16
  47#define  ESDHC_WTMK_LVL_WML_VAL_DEF     64
  48#define  ESDHC_WTMK_LVL_WML_VAL_MAX     128
  49#define ESDHC_MIX_CTRL                  0x48
  50#define  ESDHC_MIX_CTRL_DDREN           (1 << 3)
  51#define  ESDHC_MIX_CTRL_AC23EN          (1 << 7)
  52#define  ESDHC_MIX_CTRL_EXE_TUNE        (1 << 22)
  53#define  ESDHC_MIX_CTRL_SMPCLK_SEL      (1 << 23)
  54#define  ESDHC_MIX_CTRL_AUTO_TUNE_EN    (1 << 24)
  55#define  ESDHC_MIX_CTRL_FBCLK_SEL       (1 << 25)
  56#define  ESDHC_MIX_CTRL_HS400_EN        (1 << 26)
  57#define  ESDHC_MIX_CTRL_HS400_ES_EN     (1 << 27)
  58/* Bits 3 and 6 are not SDHCI standard definitions */
  59#define  ESDHC_MIX_CTRL_SDHCI_MASK      0xb7
  60/* Tuning bits */
  61#define  ESDHC_MIX_CTRL_TUNING_MASK     0x03c00000
  62
  63/* dll control register */
  64#define ESDHC_DLL_CTRL                  0x60
  65#define ESDHC_DLL_OVERRIDE_VAL_SHIFT    9
  66#define ESDHC_DLL_OVERRIDE_EN_SHIFT     8
  67
  68/* tune control register */
  69#define ESDHC_TUNE_CTRL_STATUS          0x68
  70#define  ESDHC_TUNE_CTRL_STEP           1
  71#define  ESDHC_TUNE_CTRL_MIN            0
  72#define  ESDHC_TUNE_CTRL_MAX            ((1 << 7) - 1)
  73
  74/* strobe dll register */
  75#define ESDHC_STROBE_DLL_CTRL           0x70
  76#define ESDHC_STROBE_DLL_CTRL_ENABLE    (1 << 0)
  77#define ESDHC_STROBE_DLL_CTRL_RESET     (1 << 1)
  78#define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT    0x7
  79#define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT      3
  80#define ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT    (4 << 20)
  81
  82#define ESDHC_STROBE_DLL_STATUS         0x74
  83#define ESDHC_STROBE_DLL_STS_REF_LOCK   (1 << 1)
  84#define ESDHC_STROBE_DLL_STS_SLV_LOCK   0x1
  85
  86#define ESDHC_VEND_SPEC2                0xc8
  87#define ESDHC_VEND_SPEC2_EN_BUSY_IRQ    (1 << 8)
  88
  89#define ESDHC_TUNING_CTRL               0xcc
  90#define ESDHC_STD_TUNING_EN             (1 << 24)
  91/* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
  92#define ESDHC_TUNING_START_TAP_DEFAULT  0x1
  93#define ESDHC_TUNING_START_TAP_MASK     0x7f
  94#define ESDHC_TUNING_CMD_CRC_CHECK_DISABLE      (1 << 7)
  95#define ESDHC_TUNING_STEP_MASK          0x00070000
  96#define ESDHC_TUNING_STEP_SHIFT         16
  97
  98/* pinctrl state */
  99#define ESDHC_PINCTRL_STATE_100MHZ      "state_100mhz"
 100#define ESDHC_PINCTRL_STATE_200MHZ      "state_200mhz"
 101
 102/*
 103 * Our interpretation of the SDHCI_HOST_CONTROL register
 104 */
 105#define ESDHC_CTRL_4BITBUS              (0x1 << 1)
 106#define ESDHC_CTRL_8BITBUS              (0x2 << 1)
 107#define ESDHC_CTRL_BUSWIDTH_MASK        (0x3 << 1)
 108
 109/*
 110 * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC:
 111 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
 112 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
 113 * Define this macro DMA error INT for fsl eSDHC
 114 */
 115#define ESDHC_INT_VENDOR_SPEC_DMA_ERR   (1 << 28)
 116
 117/* the address offset of CQHCI */
 118#define ESDHC_CQHCI_ADDR_OFFSET         0x100
 119
 120/*
 121 * The CMDTYPE of the CMD register (offset 0xE) should be set to
 122 * "11" when the STOP CMD12 is issued on imx53 to abort one
 123 * open ended multi-blk IO. Otherwise the TC INT wouldn't
 124 * be generated.
 125 * In exact block transfer, the controller doesn't complete the
 126 * operations automatically as required at the end of the
 127 * transfer and remains on hold if the abort command is not sent.
 128 * As a result, the TC flag is not asserted and SW received timeout
 129 * exception. Bit1 of Vendor Spec register is used to fix it.
 130 */
 131#define ESDHC_FLAG_MULTIBLK_NO_INT      BIT(1)
 132/*
 133 * The flag tells that the ESDHC controller is an USDHC block that is
 134 * integrated on the i.MX6 series.
 135 */
 136#define ESDHC_FLAG_USDHC                BIT(3)
 137/* The IP supports manual tuning process */
 138#define ESDHC_FLAG_MAN_TUNING           BIT(4)
 139/* The IP supports standard tuning process */
 140#define ESDHC_FLAG_STD_TUNING           BIT(5)
 141/* The IP has SDHCI_CAPABILITIES_1 register */
 142#define ESDHC_FLAG_HAVE_CAP1            BIT(6)
 143/*
 144 * The IP has erratum ERR004536
 145 * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow,
 146 * when reading data from the card
 147 * This flag is also set for i.MX25 and i.MX35 in order to get
 148 * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits).
 149 */
 150#define ESDHC_FLAG_ERR004536            BIT(7)
 151/* The IP supports HS200 mode */
 152#define ESDHC_FLAG_HS200                BIT(8)
 153/* The IP supports HS400 mode */
 154#define ESDHC_FLAG_HS400                BIT(9)
 155/*
 156 * The IP has errata ERR010450
 157 * uSDHC: Due to the I/O timing limit, for SDR mode, SD card clock can't
 158 * exceed 150MHz, for DDR mode, SD card clock can't exceed 45MHz.
 159 */
 160#define ESDHC_FLAG_ERR010450            BIT(10)
 161/* The IP supports HS400ES mode */
 162#define ESDHC_FLAG_HS400_ES             BIT(11)
 163/* The IP has Host Controller Interface for Command Queuing */
 164#define ESDHC_FLAG_CQHCI                BIT(12)
 165/* need request pmqos during low power */
 166#define ESDHC_FLAG_PMQOS                BIT(13)
 167/* The IP state got lost in low power mode */
 168#define ESDHC_FLAG_STATE_LOST_IN_LPMODE         BIT(14)
 169/* The IP lost clock rate in PM_RUNTIME */
 170#define ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME  BIT(15)
 171/*
 172 * The IP do not support the ACMD23 feature completely when use ADMA mode.
 173 * In ADMA mode, it only use the 16 bit block count of the register 0x4
 174 * (BLOCK_ATT) as the CMD23's argument for ACMD23 mode, which means it will
 175 * ignore the upper 16 bit of the CMD23's argument. This will block the reliable
 176 * write operation in RPMB, because RPMB reliable write need to set the bit31
 177 * of the CMD23's argument.
 178 * imx6qpdl/imx6sx/imx6sl/imx7d has this limitation only for ADMA mode, SDMA
 179 * do not has this limitation. so when these SoC use ADMA mode, it need to
 180 * disable the ACMD23 feature.
 181 */
 182#define ESDHC_FLAG_BROKEN_AUTO_CMD23    BIT(16)
 183
 184struct esdhc_soc_data {
 185        u32 flags;
 186};
 187
 188static const struct esdhc_soc_data esdhc_imx25_data = {
 189        .flags = ESDHC_FLAG_ERR004536,
 190};
 191
 192static const struct esdhc_soc_data esdhc_imx35_data = {
 193        .flags = ESDHC_FLAG_ERR004536,
 194};
 195
 196static const struct esdhc_soc_data esdhc_imx51_data = {
 197        .flags = 0,
 198};
 199
 200static const struct esdhc_soc_data esdhc_imx53_data = {
 201        .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
 202};
 203
 204static const struct esdhc_soc_data usdhc_imx6q_data = {
 205        .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING
 206                        | ESDHC_FLAG_BROKEN_AUTO_CMD23,
 207};
 208
 209static const struct esdhc_soc_data usdhc_imx6sl_data = {
 210        .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
 211                        | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536
 212                        | ESDHC_FLAG_HS200
 213                        | ESDHC_FLAG_BROKEN_AUTO_CMD23,
 214};
 215
 216static const struct esdhc_soc_data usdhc_imx6sll_data = {
 217        .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
 218                        | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
 219                        | ESDHC_FLAG_HS400
 220                        | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
 221};
 222
 223static const struct esdhc_soc_data usdhc_imx6sx_data = {
 224        .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
 225                        | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
 226                        | ESDHC_FLAG_STATE_LOST_IN_LPMODE
 227                        | ESDHC_FLAG_BROKEN_AUTO_CMD23,
 228};
 229
 230static const struct esdhc_soc_data usdhc_imx6ull_data = {
 231        .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
 232                        | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
 233                        | ESDHC_FLAG_ERR010450
 234                        | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
 235};
 236
 237static const struct esdhc_soc_data usdhc_imx7d_data = {
 238        .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
 239                        | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
 240                        | ESDHC_FLAG_HS400
 241                        | ESDHC_FLAG_STATE_LOST_IN_LPMODE
 242                        | ESDHC_FLAG_BROKEN_AUTO_CMD23,
 243};
 244
 245static struct esdhc_soc_data usdhc_imx7ulp_data = {
 246        .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
 247                        | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
 248                        | ESDHC_FLAG_PMQOS | ESDHC_FLAG_HS400
 249                        | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
 250};
 251
 252static struct esdhc_soc_data usdhc_imx8qxp_data = {
 253        .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
 254                        | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
 255                        | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
 256                        | ESDHC_FLAG_CQHCI
 257                        | ESDHC_FLAG_STATE_LOST_IN_LPMODE
 258                        | ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME,
 259};
 260
 261static struct esdhc_soc_data usdhc_imx8mm_data = {
 262        .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
 263                        | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
 264                        | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
 265                        | ESDHC_FLAG_CQHCI
 266                        | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
 267};
 268
 269struct pltfm_imx_data {
 270        u32 scratchpad;
 271        struct pinctrl *pinctrl;
 272        struct pinctrl_state *pins_100mhz;
 273        struct pinctrl_state *pins_200mhz;
 274        const struct esdhc_soc_data *socdata;
 275        struct esdhc_platform_data boarddata;
 276        struct clk *clk_ipg;
 277        struct clk *clk_ahb;
 278        struct clk *clk_per;
 279        unsigned int actual_clock;
 280        enum {
 281                NO_CMD_PENDING,      /* no multiblock command pending */
 282                MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
 283                WAIT_FOR_INT,        /* sent CMD12, waiting for response INT */
 284        } multiblock_status;
 285        u32 is_ddr;
 286        struct pm_qos_request pm_qos_req;
 287};
 288
 289static const struct platform_device_id imx_esdhc_devtype[] = {
 290        {
 291                .name = "sdhci-esdhc-imx25",
 292                .driver_data = (kernel_ulong_t) &esdhc_imx25_data,
 293        }, {
 294                .name = "sdhci-esdhc-imx35",
 295                .driver_data = (kernel_ulong_t) &esdhc_imx35_data,
 296        }, {
 297                .name = "sdhci-esdhc-imx51",
 298                .driver_data = (kernel_ulong_t) &esdhc_imx51_data,
 299        }, {
 300                /* sentinel */
 301        }
 302};
 303MODULE_DEVICE_TABLE(platform, imx_esdhc_devtype);
 304
 305static const struct of_device_id imx_esdhc_dt_ids[] = {
 306        { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
 307        { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
 308        { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
 309        { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
 310        { .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
 311        { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
 312        { .compatible = "fsl,imx6sll-usdhc", .data = &usdhc_imx6sll_data, },
 313        { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
 314        { .compatible = "fsl,imx6ull-usdhc", .data = &usdhc_imx6ull_data, },
 315        { .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
 316        { .compatible = "fsl,imx7ulp-usdhc", .data = &usdhc_imx7ulp_data, },
 317        { .compatible = "fsl,imx8qxp-usdhc", .data = &usdhc_imx8qxp_data, },
 318        { .compatible = "fsl,imx8mm-usdhc", .data = &usdhc_imx8mm_data, },
 319        { /* sentinel */ }
 320};
 321MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
 322
 323static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
 324{
 325        return data->socdata == &esdhc_imx25_data;
 326}
 327
 328static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
 329{
 330        return data->socdata == &esdhc_imx53_data;
 331}
 332
 333static inline int is_imx6q_usdhc(struct pltfm_imx_data *data)
 334{
 335        return data->socdata == &usdhc_imx6q_data;
 336}
 337
 338static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
 339{
 340        return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
 341}
 342
 343static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
 344{
 345        void __iomem *base = host->ioaddr + (reg & ~0x3);
 346        u32 shift = (reg & 0x3) * 8;
 347
 348        writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
 349}
 350
 351static inline void esdhc_wait_for_card_clock_gate_off(struct sdhci_host *host)
 352{
 353        u32 present_state;
 354        int ret;
 355
 356        ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, present_state,
 357                                (present_state & ESDHC_CLOCK_GATE_OFF), 2, 100);
 358        if (ret == -ETIMEDOUT)
 359                dev_warn(mmc_dev(host->mmc), "%s: card clock still not gate off in 100us!.\n", __func__);
 360}
 361
 362static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
 363{
 364        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 365        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
 366        u32 val = readl(host->ioaddr + reg);
 367
 368        if (unlikely(reg == SDHCI_PRESENT_STATE)) {
 369                u32 fsl_prss = val;
 370                /* save the least 20 bits */
 371                val = fsl_prss & 0x000FFFFF;
 372                /* move dat[0-3] bits */
 373                val |= (fsl_prss & 0x0F000000) >> 4;
 374                /* move cmd line bit */
 375                val |= (fsl_prss & 0x00800000) << 1;
 376        }
 377
 378        if (unlikely(reg == SDHCI_CAPABILITIES)) {
 379                /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
 380                if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
 381                        val &= 0xffff0000;
 382
 383                /* In FSL esdhc IC module, only bit20 is used to indicate the
 384                 * ADMA2 capability of esdhc, but this bit is messed up on
 385                 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
 386                 * don't actually support ADMA2). So set the BROKEN_ADMA
 387                 * quirk on MX25/35 platforms.
 388                 */
 389
 390                if (val & SDHCI_CAN_DO_ADMA1) {
 391                        val &= ~SDHCI_CAN_DO_ADMA1;
 392                        val |= SDHCI_CAN_DO_ADMA2;
 393                }
 394        }
 395
 396        if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
 397                if (esdhc_is_usdhc(imx_data)) {
 398                        if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
 399                                val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
 400                        else
 401                                /* imx6q/dl does not have cap_1 register, fake one */
 402                                val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
 403                                        | SDHCI_SUPPORT_SDR50
 404                                        | SDHCI_USE_SDR50_TUNING
 405                                        | FIELD_PREP(SDHCI_RETUNING_MODE_MASK,
 406                                                     SDHCI_TUNING_MODE_3);
 407
 408                        if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
 409                                val |= SDHCI_SUPPORT_HS400;
 410
 411                        /*
 412                         * Do not advertise faster UHS modes if there are no
 413                         * pinctrl states for 100MHz/200MHz.
 414                         */
 415                        if (IS_ERR_OR_NULL(imx_data->pins_100mhz) ||
 416                            IS_ERR_OR_NULL(imx_data->pins_200mhz))
 417                                val &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50
 418                                         | SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_HS400);
 419                }
 420        }
 421
 422        if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
 423                val = 0;
 424                val |= FIELD_PREP(SDHCI_MAX_CURRENT_330_MASK, 0xFF);
 425                val |= FIELD_PREP(SDHCI_MAX_CURRENT_300_MASK, 0xFF);
 426                val |= FIELD_PREP(SDHCI_MAX_CURRENT_180_MASK, 0xFF);
 427        }
 428
 429        if (unlikely(reg == SDHCI_INT_STATUS)) {
 430                if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
 431                        val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
 432                        val |= SDHCI_INT_ADMA_ERROR;
 433                }
 434
 435                /*
 436                 * mask off the interrupt we get in response to the manually
 437                 * sent CMD12
 438                 */
 439                if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
 440                    ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
 441                        val &= ~SDHCI_INT_RESPONSE;
 442                        writel(SDHCI_INT_RESPONSE, host->ioaddr +
 443                                                   SDHCI_INT_STATUS);
 444                        imx_data->multiblock_status = NO_CMD_PENDING;
 445                }
 446        }
 447
 448        return val;
 449}
 450
 451static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
 452{
 453        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 454        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
 455        u32 data;
 456
 457        if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
 458                        reg == SDHCI_INT_STATUS)) {
 459                if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
 460                        /*
 461                         * Clear and then set D3CD bit to avoid missing the
 462                         * card interrupt. This is an eSDHC controller problem
 463                         * so we need to apply the following workaround: clear
 464                         * and set D3CD bit will make eSDHC re-sample the card
 465                         * interrupt. In case a card interrupt was lost,
 466                         * re-sample it by the following steps.
 467                         */
 468                        data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
 469                        data &= ~ESDHC_CTRL_D3CD;
 470                        writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
 471                        data |= ESDHC_CTRL_D3CD;
 472                        writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
 473                }
 474
 475                if (val & SDHCI_INT_ADMA_ERROR) {
 476                        val &= ~SDHCI_INT_ADMA_ERROR;
 477                        val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
 478                }
 479        }
 480
 481        if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
 482                                && (reg == SDHCI_INT_STATUS)
 483                                && (val & SDHCI_INT_DATA_END))) {
 484                        u32 v;
 485                        v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
 486                        v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
 487                        writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
 488
 489                        if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
 490                        {
 491                                /* send a manual CMD12 with RESPTYP=none */
 492                                data = MMC_STOP_TRANSMISSION << 24 |
 493                                       SDHCI_CMD_ABORTCMD << 16;
 494                                writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
 495                                imx_data->multiblock_status = WAIT_FOR_INT;
 496                        }
 497        }
 498
 499        writel(val, host->ioaddr + reg);
 500}
 501
 502static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
 503{
 504        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 505        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
 506        u16 ret = 0;
 507        u32 val;
 508
 509        if (unlikely(reg == SDHCI_HOST_VERSION)) {
 510                reg ^= 2;
 511                if (esdhc_is_usdhc(imx_data)) {
 512                        /*
 513                         * The usdhc register returns a wrong host version.
 514                         * Correct it here.
 515                         */
 516                        return SDHCI_SPEC_300;
 517                }
 518        }
 519
 520        if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
 521                val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
 522                if (val & ESDHC_VENDOR_SPEC_VSELECT)
 523                        ret |= SDHCI_CTRL_VDD_180;
 524
 525                if (esdhc_is_usdhc(imx_data)) {
 526                        if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
 527                                val = readl(host->ioaddr + ESDHC_MIX_CTRL);
 528                        else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
 529                                /* the std tuning bits is in ACMD12_ERR for imx6sl */
 530                                val = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
 531                }
 532
 533                if (val & ESDHC_MIX_CTRL_EXE_TUNE)
 534                        ret |= SDHCI_CTRL_EXEC_TUNING;
 535                if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
 536                        ret |= SDHCI_CTRL_TUNED_CLK;
 537
 538                ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
 539
 540                return ret;
 541        }
 542
 543        if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
 544                if (esdhc_is_usdhc(imx_data)) {
 545                        u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
 546                        ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
 547                        /* Swap AC23 bit */
 548                        if (m & ESDHC_MIX_CTRL_AC23EN) {
 549                                ret &= ~ESDHC_MIX_CTRL_AC23EN;
 550                                ret |= SDHCI_TRNS_AUTO_CMD23;
 551                        }
 552                } else {
 553                        ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
 554                }
 555
 556                return ret;
 557        }
 558
 559        return readw(host->ioaddr + reg);
 560}
 561
 562static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
 563{
 564        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 565        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
 566        u32 new_val = 0;
 567
 568        switch (reg) {
 569        case SDHCI_CLOCK_CONTROL:
 570                new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
 571                if (val & SDHCI_CLOCK_CARD_EN)
 572                        new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
 573                else
 574                        new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
 575                writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
 576                if (!(new_val & ESDHC_VENDOR_SPEC_FRC_SDCLK_ON))
 577                        esdhc_wait_for_card_clock_gate_off(host);
 578                return;
 579        case SDHCI_HOST_CONTROL2:
 580                new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
 581                if (val & SDHCI_CTRL_VDD_180)
 582                        new_val |= ESDHC_VENDOR_SPEC_VSELECT;
 583                else
 584                        new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
 585                writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
 586                if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
 587                        new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
 588                        if (val & SDHCI_CTRL_TUNED_CLK) {
 589                                new_val |= ESDHC_MIX_CTRL_SMPCLK_SEL;
 590                                new_val |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
 591                        } else {
 592                                new_val &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
 593                                new_val &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
 594                        }
 595                        writel(new_val , host->ioaddr + ESDHC_MIX_CTRL);
 596                } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
 597                        u32 v = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
 598                        u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
 599                        if (val & SDHCI_CTRL_TUNED_CLK) {
 600                                v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
 601                        } else {
 602                                v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
 603                                m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
 604                                m &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
 605                        }
 606
 607                        if (val & SDHCI_CTRL_EXEC_TUNING) {
 608                                v |= ESDHC_MIX_CTRL_EXE_TUNE;
 609                                m |= ESDHC_MIX_CTRL_FBCLK_SEL;
 610                                m |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
 611                        } else {
 612                                v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
 613                        }
 614
 615                        writel(v, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
 616                        writel(m, host->ioaddr + ESDHC_MIX_CTRL);
 617                }
 618                return;
 619        case SDHCI_TRANSFER_MODE:
 620                if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
 621                                && (host->cmd->opcode == SD_IO_RW_EXTENDED)
 622                                && (host->cmd->data->blocks > 1)
 623                                && (host->cmd->data->flags & MMC_DATA_READ)) {
 624                        u32 v;
 625                        v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
 626                        v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
 627                        writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
 628                }
 629
 630                if (esdhc_is_usdhc(imx_data)) {
 631                        u32 wml;
 632                        u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
 633                        /* Swap AC23 bit */
 634                        if (val & SDHCI_TRNS_AUTO_CMD23) {
 635                                val &= ~SDHCI_TRNS_AUTO_CMD23;
 636                                val |= ESDHC_MIX_CTRL_AC23EN;
 637                        }
 638                        m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
 639                        writel(m, host->ioaddr + ESDHC_MIX_CTRL);
 640
 641                        /* Set watermark levels for PIO access to maximum value
 642                         * (128 words) to accommodate full 512 bytes buffer.
 643                         * For DMA access restore the levels to default value.
 644                         */
 645                        m = readl(host->ioaddr + ESDHC_WTMK_LVL);
 646                        if (val & SDHCI_TRNS_DMA) {
 647                                wml = ESDHC_WTMK_LVL_WML_VAL_DEF;
 648                        } else {
 649                                u8 ctrl;
 650                                wml = ESDHC_WTMK_LVL_WML_VAL_MAX;
 651
 652                                /*
 653                                 * Since already disable DMA mode, so also need
 654                                 * to clear the DMASEL. Otherwise, for standard
 655                                 * tuning, when send tuning command, usdhc will
 656                                 * still prefetch the ADMA script from wrong
 657                                 * DMA address, then we will see IOMMU report
 658                                 * some error which show lack of TLB mapping.
 659                                 */
 660                                ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
 661                                ctrl &= ~SDHCI_CTRL_DMA_MASK;
 662                                sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
 663                        }
 664                        m &= ~(ESDHC_WTMK_LVL_RD_WML_MASK |
 665                               ESDHC_WTMK_LVL_WR_WML_MASK);
 666                        m |= (wml << ESDHC_WTMK_LVL_RD_WML_SHIFT) |
 667                             (wml << ESDHC_WTMK_LVL_WR_WML_SHIFT);
 668                        writel(m, host->ioaddr + ESDHC_WTMK_LVL);
 669                } else {
 670                        /*
 671                         * Postpone this write, we must do it together with a
 672                         * command write that is down below.
 673                         */
 674                        imx_data->scratchpad = val;
 675                }
 676                return;
 677        case SDHCI_COMMAND:
 678                if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
 679                        val |= SDHCI_CMD_ABORTCMD;
 680
 681                if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
 682                    (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
 683                        imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
 684
 685                if (esdhc_is_usdhc(imx_data))
 686                        writel(val << 16,
 687                               host->ioaddr + SDHCI_TRANSFER_MODE);
 688                else
 689                        writel(val << 16 | imx_data->scratchpad,
 690                               host->ioaddr + SDHCI_TRANSFER_MODE);
 691                return;
 692        case SDHCI_BLOCK_SIZE:
 693                val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
 694                break;
 695        }
 696        esdhc_clrset_le(host, 0xffff, val, reg);
 697}
 698
 699static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
 700{
 701        u8 ret;
 702        u32 val;
 703
 704        switch (reg) {
 705        case SDHCI_HOST_CONTROL:
 706                val = readl(host->ioaddr + reg);
 707
 708                ret = val & SDHCI_CTRL_LED;
 709                ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
 710                ret |= (val & ESDHC_CTRL_4BITBUS);
 711                ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
 712                return ret;
 713        }
 714
 715        return readb(host->ioaddr + reg);
 716}
 717
 718static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
 719{
 720        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 721        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
 722        u32 new_val = 0;
 723        u32 mask;
 724
 725        switch (reg) {
 726        case SDHCI_POWER_CONTROL:
 727                /*
 728                 * FSL put some DMA bits here
 729                 * If your board has a regulator, code should be here
 730                 */
 731                return;
 732        case SDHCI_HOST_CONTROL:
 733                /* FSL messed up here, so we need to manually compose it. */
 734                new_val = val & SDHCI_CTRL_LED;
 735                /* ensure the endianness */
 736                new_val |= ESDHC_HOST_CONTROL_LE;
 737                /* bits 8&9 are reserved on mx25 */
 738                if (!is_imx25_esdhc(imx_data)) {
 739                        /* DMA mode bits are shifted */
 740                        new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
 741                }
 742
 743                /*
 744                 * Do not touch buswidth bits here. This is done in
 745                 * esdhc_pltfm_bus_width.
 746                 * Do not touch the D3CD bit either which is used for the
 747                 * SDIO interrupt erratum workaround.
 748                 */
 749                mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
 750
 751                esdhc_clrset_le(host, mask, new_val, reg);
 752                return;
 753        case SDHCI_SOFTWARE_RESET:
 754                if (val & SDHCI_RESET_DATA)
 755                        new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
 756                break;
 757        }
 758        esdhc_clrset_le(host, 0xff, val, reg);
 759
 760        if (reg == SDHCI_SOFTWARE_RESET) {
 761                if (val & SDHCI_RESET_ALL) {
 762                        /*
 763                         * The esdhc has a design violation to SDHC spec which
 764                         * tells that software reset should not affect card
 765                         * detection circuit. But esdhc clears its SYSCTL
 766                         * register bits [0..2] during the software reset. This
 767                         * will stop those clocks that card detection circuit
 768                         * relies on. To work around it, we turn the clocks on
 769                         * back to keep card detection circuit functional.
 770                         */
 771                        esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
 772                        /*
 773                         * The reset on usdhc fails to clear MIX_CTRL register.
 774                         * Do it manually here.
 775                         */
 776                        if (esdhc_is_usdhc(imx_data)) {
 777                                /*
 778                                 * the tuning bits should be kept during reset
 779                                 */
 780                                new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
 781                                writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
 782                                                host->ioaddr + ESDHC_MIX_CTRL);
 783                                imx_data->is_ddr = 0;
 784                        }
 785                } else if (val & SDHCI_RESET_DATA) {
 786                        /*
 787                         * The eSDHC DAT line software reset clears at least the
 788                         * data transfer width on i.MX25, so make sure that the
 789                         * Host Control register is unaffected.
 790                         */
 791                        esdhc_clrset_le(host, 0xff, new_val,
 792                                        SDHCI_HOST_CONTROL);
 793                }
 794        }
 795}
 796
 797static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
 798{
 799        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 800
 801        return pltfm_host->clock;
 802}
 803
 804static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
 805{
 806        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 807
 808        return pltfm_host->clock / 256 / 16;
 809}
 810
 811static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
 812                                         unsigned int clock)
 813{
 814        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 815        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
 816        unsigned int host_clock = pltfm_host->clock;
 817        int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
 818        int pre_div = 1;
 819        int div = 1;
 820        int ret;
 821        u32 temp, val;
 822
 823        if (esdhc_is_usdhc(imx_data)) {
 824                val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
 825                writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
 826                        host->ioaddr + ESDHC_VENDOR_SPEC);
 827                esdhc_wait_for_card_clock_gate_off(host);
 828        }
 829
 830        if (clock == 0) {
 831                host->mmc->actual_clock = 0;
 832                return;
 833        }
 834
 835        /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
 836        if (is_imx53_esdhc(imx_data)) {
 837                /*
 838                 * According to the i.MX53 reference manual, if DLLCTRL[10] can
 839                 * be set, then the controller is eSDHCv3, else it is eSDHCv2.
 840                 */
 841                val = readl(host->ioaddr + ESDHC_DLL_CTRL);
 842                writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL);
 843                temp = readl(host->ioaddr + ESDHC_DLL_CTRL);
 844                writel(val, host->ioaddr + ESDHC_DLL_CTRL);
 845                if (temp & BIT(10))
 846                        pre_div = 2;
 847        }
 848
 849        temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
 850        temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
 851                | ESDHC_CLOCK_MASK);
 852        sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
 853
 854        if (imx_data->socdata->flags & ESDHC_FLAG_ERR010450) {
 855                unsigned int max_clock;
 856
 857                max_clock = imx_data->is_ddr ? 45000000 : 150000000;
 858
 859                clock = min(clock, max_clock);
 860        }
 861
 862        while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
 863                        pre_div < 256)
 864                pre_div *= 2;
 865
 866        while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
 867                div++;
 868
 869        host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
 870        dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
 871                clock, host->mmc->actual_clock);
 872
 873        pre_div >>= 1;
 874        div--;
 875
 876        temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
 877        temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
 878                | (div << ESDHC_DIVIDER_SHIFT)
 879                | (pre_div << ESDHC_PREDIV_SHIFT));
 880        sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
 881
 882        /* need to wait the bit 3 of the PRSSTAT to be set, make sure card clock is stable */
 883        ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, temp,
 884                                (temp & ESDHC_CLOCK_STABLE), 2, 100);
 885        if (ret == -ETIMEDOUT)
 886                dev_warn(mmc_dev(host->mmc), "card clock still not stable in 100us!.\n");
 887
 888        if (esdhc_is_usdhc(imx_data)) {
 889                val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
 890                writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
 891                        host->ioaddr + ESDHC_VENDOR_SPEC);
 892        }
 893
 894}
 895
 896static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
 897{
 898        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 899        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
 900        struct esdhc_platform_data *boarddata = &imx_data->boarddata;
 901
 902        switch (boarddata->wp_type) {
 903        case ESDHC_WP_GPIO:
 904                return mmc_gpio_get_ro(host->mmc);
 905        case ESDHC_WP_CONTROLLER:
 906                return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
 907                               SDHCI_WRITE_PROTECT);
 908        case ESDHC_WP_NONE:
 909                break;
 910        }
 911
 912        return -ENOSYS;
 913}
 914
 915static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
 916{
 917        u32 ctrl;
 918
 919        switch (width) {
 920        case MMC_BUS_WIDTH_8:
 921                ctrl = ESDHC_CTRL_8BITBUS;
 922                break;
 923        case MMC_BUS_WIDTH_4:
 924                ctrl = ESDHC_CTRL_4BITBUS;
 925                break;
 926        default:
 927                ctrl = 0;
 928                break;
 929        }
 930
 931        esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
 932                        SDHCI_HOST_CONTROL);
 933}
 934
 935static int usdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
 936{
 937        struct sdhci_host *host = mmc_priv(mmc);
 938
 939        /*
 940         * i.MX uSDHC internally already uses a fixed optimized timing for
 941         * DDR50, normally does not require tuning for DDR50 mode.
 942         */
 943        if (host->timing == MMC_TIMING_UHS_DDR50)
 944                return 0;
 945
 946        return sdhci_execute_tuning(mmc, opcode);
 947}
 948
 949static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
 950{
 951        u32 reg;
 952
 953        /* FIXME: delay a bit for card to be ready for next tuning due to errors */
 954        mdelay(1);
 955
 956        reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
 957        reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
 958                        ESDHC_MIX_CTRL_FBCLK_SEL;
 959        writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
 960        writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
 961        dev_dbg(mmc_dev(host->mmc),
 962                "tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
 963                        val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
 964}
 965
 966static void esdhc_post_tuning(struct sdhci_host *host)
 967{
 968        u32 reg;
 969
 970        reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
 971        reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
 972        reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
 973        writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
 974}
 975
 976static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
 977{
 978        int min, max, avg, ret;
 979
 980        /* find the mininum delay first which can pass tuning */
 981        min = ESDHC_TUNE_CTRL_MIN;
 982        while (min < ESDHC_TUNE_CTRL_MAX) {
 983                esdhc_prepare_tuning(host, min);
 984                if (!mmc_send_tuning(host->mmc, opcode, NULL))
 985                        break;
 986                min += ESDHC_TUNE_CTRL_STEP;
 987        }
 988
 989        /* find the maxinum delay which can not pass tuning */
 990        max = min + ESDHC_TUNE_CTRL_STEP;
 991        while (max < ESDHC_TUNE_CTRL_MAX) {
 992                esdhc_prepare_tuning(host, max);
 993                if (mmc_send_tuning(host->mmc, opcode, NULL)) {
 994                        max -= ESDHC_TUNE_CTRL_STEP;
 995                        break;
 996                }
 997                max += ESDHC_TUNE_CTRL_STEP;
 998        }
 999
1000        /* use average delay to get the best timing */
1001        avg = (min + max) / 2;
1002        esdhc_prepare_tuning(host, avg);
1003        ret = mmc_send_tuning(host->mmc, opcode, NULL);
1004        esdhc_post_tuning(host);
1005
1006        dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
1007                ret ? "failed" : "passed", avg, ret);
1008
1009        return ret;
1010}
1011
1012static void esdhc_hs400_enhanced_strobe(struct mmc_host *mmc, struct mmc_ios *ios)
1013{
1014        struct sdhci_host *host = mmc_priv(mmc);
1015        u32 m;
1016
1017        m = readl(host->ioaddr + ESDHC_MIX_CTRL);
1018        if (ios->enhanced_strobe)
1019                m |= ESDHC_MIX_CTRL_HS400_ES_EN;
1020        else
1021                m &= ~ESDHC_MIX_CTRL_HS400_ES_EN;
1022        writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1023}
1024
1025static int esdhc_change_pinstate(struct sdhci_host *host,
1026                                                unsigned int uhs)
1027{
1028        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1029        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1030        struct pinctrl_state *pinctrl;
1031
1032        dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
1033
1034        if (IS_ERR(imx_data->pinctrl) ||
1035                IS_ERR(imx_data->pins_100mhz) ||
1036                IS_ERR(imx_data->pins_200mhz))
1037                return -EINVAL;
1038
1039        switch (uhs) {
1040        case MMC_TIMING_UHS_SDR50:
1041        case MMC_TIMING_UHS_DDR50:
1042                pinctrl = imx_data->pins_100mhz;
1043                break;
1044        case MMC_TIMING_UHS_SDR104:
1045        case MMC_TIMING_MMC_HS200:
1046        case MMC_TIMING_MMC_HS400:
1047                pinctrl = imx_data->pins_200mhz;
1048                break;
1049        default:
1050                /* back to default state for other legacy timing */
1051                return pinctrl_select_default_state(mmc_dev(host->mmc));
1052        }
1053
1054        return pinctrl_select_state(imx_data->pinctrl, pinctrl);
1055}
1056
1057/*
1058 * For HS400 eMMC, there is a data_strobe line. This signal is generated
1059 * by the device and used for data output and CRC status response output
1060 * in HS400 mode. The frequency of this signal follows the frequency of
1061 * CLK generated by host. The host receives the data which is aligned to the
1062 * edge of data_strobe line. Due to the time delay between CLK line and
1063 * data_strobe line, if the delay time is larger than one clock cycle,
1064 * then CLK and data_strobe line will be misaligned, read error shows up.
1065 */
1066static void esdhc_set_strobe_dll(struct sdhci_host *host)
1067{
1068        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1069        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1070        u32 strobe_delay;
1071        u32 v;
1072        int ret;
1073
1074        /* disable clock before enabling strobe dll */
1075        writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
1076                ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
1077                host->ioaddr + ESDHC_VENDOR_SPEC);
1078        esdhc_wait_for_card_clock_gate_off(host);
1079
1080        /* force a reset on strobe dll */
1081        writel(ESDHC_STROBE_DLL_CTRL_RESET,
1082                host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1083        /* clear the reset bit on strobe dll before any setting */
1084        writel(0, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1085
1086        /*
1087         * enable strobe dll ctrl and adjust the delay target
1088         * for the uSDHC loopback read clock
1089         */
1090        if (imx_data->boarddata.strobe_dll_delay_target)
1091                strobe_delay = imx_data->boarddata.strobe_dll_delay_target;
1092        else
1093                strobe_delay = ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT;
1094        v = ESDHC_STROBE_DLL_CTRL_ENABLE |
1095                ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
1096                (strobe_delay << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
1097        writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1098
1099        /* wait max 50us to get the REF/SLV lock */
1100        ret = readl_poll_timeout(host->ioaddr + ESDHC_STROBE_DLL_STATUS, v,
1101                ((v & ESDHC_STROBE_DLL_STS_REF_LOCK) && (v & ESDHC_STROBE_DLL_STS_SLV_LOCK)), 1, 50);
1102        if (ret == -ETIMEDOUT)
1103                dev_warn(mmc_dev(host->mmc),
1104                "warning! HS400 strobe DLL status REF/SLV not lock in 50us, STROBE DLL status is %x!\n", v);
1105}
1106
1107static void esdhc_reset_tuning(struct sdhci_host *host)
1108{
1109        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1110        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1111        u32 ctrl;
1112
1113        /* Reset the tuning circuit */
1114        if (esdhc_is_usdhc(imx_data)) {
1115                if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1116                        ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
1117                        ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1118                        ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
1119                        writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
1120                        writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1121                } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1122                        ctrl = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1123                        ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1124                        writel(ctrl, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1125                }
1126        }
1127}
1128
1129static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1130{
1131        u32 m;
1132        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1133        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1134        struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1135
1136        /* disable ddr mode and disable HS400 mode */
1137        m = readl(host->ioaddr + ESDHC_MIX_CTRL);
1138        m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
1139        imx_data->is_ddr = 0;
1140
1141        switch (timing) {
1142        case MMC_TIMING_UHS_SDR12:
1143        case MMC_TIMING_UHS_SDR25:
1144        case MMC_TIMING_UHS_SDR50:
1145        case MMC_TIMING_UHS_SDR104:
1146        case MMC_TIMING_MMC_HS:
1147        case MMC_TIMING_MMC_HS200:
1148                writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1149                break;
1150        case MMC_TIMING_UHS_DDR50:
1151        case MMC_TIMING_MMC_DDR52:
1152                m |= ESDHC_MIX_CTRL_DDREN;
1153                writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1154                imx_data->is_ddr = 1;
1155                if (boarddata->delay_line) {
1156                        u32 v;
1157                        v = boarddata->delay_line <<
1158                                ESDHC_DLL_OVERRIDE_VAL_SHIFT |
1159                                (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
1160                        if (is_imx53_esdhc(imx_data))
1161                                v <<= 1;
1162                        writel(v, host->ioaddr + ESDHC_DLL_CTRL);
1163                }
1164                break;
1165        case MMC_TIMING_MMC_HS400:
1166                m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
1167                writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1168                imx_data->is_ddr = 1;
1169                /* update clock after enable DDR for strobe DLL lock */
1170                host->ops->set_clock(host, host->clock);
1171                esdhc_set_strobe_dll(host);
1172                break;
1173        case MMC_TIMING_LEGACY:
1174        default:
1175                esdhc_reset_tuning(host);
1176                break;
1177        }
1178
1179        esdhc_change_pinstate(host, timing);
1180}
1181
1182static void esdhc_reset(struct sdhci_host *host, u8 mask)
1183{
1184        sdhci_reset(host, mask);
1185
1186        sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1187        sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1188}
1189
1190static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host)
1191{
1192        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1193        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1194
1195        /* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
1196        return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
1197}
1198
1199static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
1200{
1201        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1202        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1203
1204        /* use maximum timeout counter */
1205        esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
1206                        esdhc_is_usdhc(imx_data) ? 0xF : 0xE,
1207                        SDHCI_TIMEOUT_CONTROL);
1208}
1209
1210static u32 esdhc_cqhci_irq(struct sdhci_host *host, u32 intmask)
1211{
1212        int cmd_error = 0;
1213        int data_error = 0;
1214
1215        if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
1216                return intmask;
1217
1218        cqhci_irq(host->mmc, intmask, cmd_error, data_error);
1219
1220        return 0;
1221}
1222
1223static struct sdhci_ops sdhci_esdhc_ops = {
1224        .read_l = esdhc_readl_le,
1225        .read_w = esdhc_readw_le,
1226        .read_b = esdhc_readb_le,
1227        .write_l = esdhc_writel_le,
1228        .write_w = esdhc_writew_le,
1229        .write_b = esdhc_writeb_le,
1230        .set_clock = esdhc_pltfm_set_clock,
1231        .get_max_clock = esdhc_pltfm_get_max_clock,
1232        .get_min_clock = esdhc_pltfm_get_min_clock,
1233        .get_max_timeout_count = esdhc_get_max_timeout_count,
1234        .get_ro = esdhc_pltfm_get_ro,
1235        .set_timeout = esdhc_set_timeout,
1236        .set_bus_width = esdhc_pltfm_set_bus_width,
1237        .set_uhs_signaling = esdhc_set_uhs_signaling,
1238        .reset = esdhc_reset,
1239        .irq = esdhc_cqhci_irq,
1240};
1241
1242static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
1243        .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
1244                        | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
1245                        | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
1246                        | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
1247        .ops = &sdhci_esdhc_ops,
1248};
1249
1250static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
1251{
1252        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1253        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1254        struct cqhci_host *cq_host = host->mmc->cqe_private;
1255        int tmp;
1256
1257        if (esdhc_is_usdhc(imx_data)) {
1258                /*
1259                 * The imx6q ROM code will change the default watermark
1260                 * level setting to something insane.  Change it back here.
1261                 */
1262                writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
1263
1264                /*
1265                 * ROM code will change the bit burst_length_enable setting
1266                 * to zero if this usdhc is chosen to boot system. Change
1267                 * it back here, otherwise it will impact the performance a
1268                 * lot. This bit is used to enable/disable the burst length
1269                 * for the external AHB2AXI bridge. It's useful especially
1270                 * for INCR transfer because without burst length indicator,
1271                 * the AHB2AXI bridge does not know the burst length in
1272                 * advance. And without burst length indicator, AHB INCR
1273                 * transfer can only be converted to singles on the AXI side.
1274                 */
1275                writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
1276                        | ESDHC_BURST_LEN_EN_INCR,
1277                        host->ioaddr + SDHCI_HOST_CONTROL);
1278
1279                /*
1280                 * erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
1281                 * TO1.1, it's harmless for MX6SL
1282                 */
1283                writel(readl(host->ioaddr + 0x6c) & ~BIT(7),
1284                        host->ioaddr + 0x6c);
1285
1286                /* disable DLL_CTRL delay line settings */
1287                writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
1288
1289                /*
1290                 * For the case of command with busy, if set the bit
1291                 * ESDHC_VEND_SPEC2_EN_BUSY_IRQ, USDHC will generate a
1292                 * transfer complete interrupt when busy is deasserted.
1293                 * When CQHCI use DCMD to send a CMD need R1b respons,
1294                 * CQHCI require to set ESDHC_VEND_SPEC2_EN_BUSY_IRQ,
1295                 * otherwise DCMD will always meet timeout waiting for
1296                 * hardware interrupt issue.
1297                 */
1298                if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1299                        tmp = readl(host->ioaddr + ESDHC_VEND_SPEC2);
1300                        tmp |= ESDHC_VEND_SPEC2_EN_BUSY_IRQ;
1301                        writel(tmp, host->ioaddr + ESDHC_VEND_SPEC2);
1302
1303                        host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
1304                }
1305
1306                if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1307                        tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1308                        tmp |= ESDHC_STD_TUNING_EN |
1309                                ESDHC_TUNING_START_TAP_DEFAULT;
1310                        if (imx_data->boarddata.tuning_start_tap) {
1311                                tmp &= ~ESDHC_TUNING_START_TAP_MASK;
1312                                tmp |= imx_data->boarddata.tuning_start_tap;
1313                        }
1314
1315                        if (imx_data->boarddata.tuning_step) {
1316                                tmp &= ~ESDHC_TUNING_STEP_MASK;
1317                                tmp |= imx_data->boarddata.tuning_step
1318                                        << ESDHC_TUNING_STEP_SHIFT;
1319                        }
1320
1321                        /* Disable the CMD CRC check for tuning, if not, need to
1322                         * add some delay after every tuning command, because
1323                         * hardware standard tuning logic will directly go to next
1324                         * step once it detect the CMD CRC error, will not wait for
1325                         * the card side to finally send out the tuning data, trigger
1326                         * the buffer read ready interrupt immediately. If usdhc send
1327                         * the next tuning command some eMMC card will stuck, can't
1328                         * response, block the tuning procedure or the first command
1329                         * after the whole tuning procedure always can't get any response.
1330                         */
1331                         tmp |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
1332                        writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1333                } else if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1334                        /*
1335                         * ESDHC_STD_TUNING_EN may be configed in bootloader
1336                         * or ROM code, so clear this bit here to make sure
1337                         * the manual tuning can work.
1338                         */
1339                        tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1340                        tmp &= ~ESDHC_STD_TUNING_EN;
1341                        writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1342                }
1343
1344                /*
1345                 * On i.MX8MM, we are running Dual Linux OS, with 1st Linux using SD Card
1346                 * as rootfs storage, 2nd Linux using eMMC as rootfs storage. We let the
1347                 * the 1st linux configure power/clock for the 2nd Linux.
1348                 *
1349                 * When the 2nd Linux is booting into rootfs stage, we let the 1st Linux
1350                 * to destroy the 2nd linux, then restart the 2nd linux, we met SDHCI dump.
1351                 * After we clear the pending interrupt and halt CQCTL, issue gone.
1352                 */
1353                if (cq_host) {
1354                        tmp = cqhci_readl(cq_host, CQHCI_IS);
1355                        cqhci_writel(cq_host, tmp, CQHCI_IS);
1356                        cqhci_writel(cq_host, CQHCI_HALT, CQHCI_CTL);
1357                }
1358        }
1359}
1360
1361static void esdhc_cqe_enable(struct mmc_host *mmc)
1362{
1363        struct sdhci_host *host = mmc_priv(mmc);
1364        struct cqhci_host *cq_host = mmc->cqe_private;
1365        u32 reg;
1366        u16 mode;
1367        int count = 10;
1368
1369        /*
1370         * CQE gets stuck if it sees Buffer Read Enable bit set, which can be
1371         * the case after tuning, so ensure the buffer is drained.
1372         */
1373        reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1374        while (reg & SDHCI_DATA_AVAILABLE) {
1375                sdhci_readl(host, SDHCI_BUFFER);
1376                reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1377                if (count-- == 0) {
1378                        dev_warn(mmc_dev(host->mmc),
1379                                "CQE may get stuck because the Buffer Read Enable bit is set\n");
1380                        break;
1381                }
1382                mdelay(1);
1383        }
1384
1385        /*
1386         * Runtime resume will reset the entire host controller, which
1387         * will also clear the DMAEN/BCEN of register ESDHC_MIX_CTRL.
1388         * Here set DMAEN and BCEN when enable CMDQ.
1389         */
1390        mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
1391        if (host->flags & SDHCI_REQ_USE_DMA)
1392                mode |= SDHCI_TRNS_DMA;
1393        if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
1394                mode |= SDHCI_TRNS_BLK_CNT_EN;
1395        sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
1396
1397        /*
1398         * Though Runtime resume reset the entire host controller,
1399         * but do not impact the CQHCI side, need to clear the
1400         * HALT bit, avoid CQHCI stuck in the first request when
1401         * system resume back.
1402         */
1403        cqhci_writel(cq_host, 0, CQHCI_CTL);
1404        if (cqhci_readl(cq_host, CQHCI_CTL) && CQHCI_HALT)
1405                dev_err(mmc_dev(host->mmc),
1406                        "failed to exit halt state when enable CQE\n");
1407
1408
1409        sdhci_cqe_enable(mmc);
1410}
1411
1412static void esdhc_sdhci_dumpregs(struct mmc_host *mmc)
1413{
1414        sdhci_dumpregs(mmc_priv(mmc));
1415}
1416
1417static const struct cqhci_host_ops esdhc_cqhci_ops = {
1418        .enable         = esdhc_cqe_enable,
1419        .disable        = sdhci_cqe_disable,
1420        .dumpregs       = esdhc_sdhci_dumpregs,
1421};
1422
1423#ifdef CONFIG_OF
1424static int
1425sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
1426                         struct sdhci_host *host,
1427                         struct pltfm_imx_data *imx_data)
1428{
1429        struct device_node *np = pdev->dev.of_node;
1430        struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1431        int ret;
1432
1433        if (of_get_property(np, "fsl,wp-controller", NULL))
1434                boarddata->wp_type = ESDHC_WP_CONTROLLER;
1435
1436        /*
1437         * If we have this property, then activate WP check.
1438         * Retrieveing and requesting the actual WP GPIO will happen
1439         * in the call to mmc_of_parse().
1440         */
1441        if (of_property_read_bool(np, "wp-gpios"))
1442                boarddata->wp_type = ESDHC_WP_GPIO;
1443
1444        of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
1445        of_property_read_u32(np, "fsl,tuning-start-tap",
1446                             &boarddata->tuning_start_tap);
1447
1448        of_property_read_u32(np, "fsl,strobe-dll-delay-target",
1449                                &boarddata->strobe_dll_delay_target);
1450        if (of_find_property(np, "no-1-8-v", NULL))
1451                host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1452
1453        if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
1454                boarddata->delay_line = 0;
1455
1456        mmc_of_parse_voltage(np, &host->ocr_mask);
1457
1458        if (esdhc_is_usdhc(imx_data)) {
1459                imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1460                                                ESDHC_PINCTRL_STATE_100MHZ);
1461                imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1462                                                ESDHC_PINCTRL_STATE_200MHZ);
1463        }
1464
1465        /* call to generic mmc_of_parse to support additional capabilities */
1466        ret = mmc_of_parse(host->mmc);
1467        if (ret)
1468                return ret;
1469
1470        if (mmc_gpio_get_cd(host->mmc) >= 0)
1471                host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1472
1473        return 0;
1474}
1475#else
1476static inline int
1477sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
1478                         struct sdhci_host *host,
1479                         struct pltfm_imx_data *imx_data)
1480{
1481        return -ENODEV;
1482}
1483#endif
1484
1485static int sdhci_esdhc_imx_probe_nondt(struct platform_device *pdev,
1486                         struct sdhci_host *host,
1487                         struct pltfm_imx_data *imx_data)
1488{
1489        struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1490        int err;
1491
1492        if (!host->mmc->parent->platform_data) {
1493                dev_err(mmc_dev(host->mmc), "no board data!\n");
1494                return -EINVAL;
1495        }
1496
1497        imx_data->boarddata = *((struct esdhc_platform_data *)
1498                                host->mmc->parent->platform_data);
1499        /* write_protect */
1500        if (boarddata->wp_type == ESDHC_WP_GPIO) {
1501                host->mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
1502
1503                err = mmc_gpiod_request_ro(host->mmc, "wp", 0, 0);
1504                if (err) {
1505                        dev_err(mmc_dev(host->mmc),
1506                                "failed to request write-protect gpio!\n");
1507                        return err;
1508                }
1509        }
1510
1511        /* card_detect */
1512        switch (boarddata->cd_type) {
1513        case ESDHC_CD_GPIO:
1514                err = mmc_gpiod_request_cd(host->mmc, "cd", 0, false, 0);
1515                if (err) {
1516                        dev_err(mmc_dev(host->mmc),
1517                                "failed to request card-detect gpio!\n");
1518                        return err;
1519                }
1520                /* fall through */
1521
1522        case ESDHC_CD_CONTROLLER:
1523                /* we have a working card_detect back */
1524                host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1525                break;
1526
1527        case ESDHC_CD_PERMANENT:
1528                host->mmc->caps |= MMC_CAP_NONREMOVABLE;
1529                break;
1530
1531        case ESDHC_CD_NONE:
1532                break;
1533        }
1534
1535        switch (boarddata->max_bus_width) {
1536        case 8:
1537                host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
1538                break;
1539        case 4:
1540                host->mmc->caps |= MMC_CAP_4_BIT_DATA;
1541                break;
1542        case 1:
1543        default:
1544                host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
1545                break;
1546        }
1547
1548        return 0;
1549}
1550
1551static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
1552{
1553        const struct of_device_id *of_id =
1554                        of_match_device(imx_esdhc_dt_ids, &pdev->dev);
1555        struct sdhci_pltfm_host *pltfm_host;
1556        struct sdhci_host *host;
1557        struct cqhci_host *cq_host;
1558        int err;
1559        struct pltfm_imx_data *imx_data;
1560
1561        host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
1562                                sizeof(*imx_data));
1563        if (IS_ERR(host))
1564                return PTR_ERR(host);
1565
1566        pltfm_host = sdhci_priv(host);
1567
1568        imx_data = sdhci_pltfm_priv(pltfm_host);
1569
1570        imx_data->socdata = of_id ? of_id->data : (struct esdhc_soc_data *)
1571                                                  pdev->id_entry->driver_data;
1572
1573        if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1574                cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
1575
1576        imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1577        if (IS_ERR(imx_data->clk_ipg)) {
1578                err = PTR_ERR(imx_data->clk_ipg);
1579                goto free_sdhci;
1580        }
1581
1582        imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1583        if (IS_ERR(imx_data->clk_ahb)) {
1584                err = PTR_ERR(imx_data->clk_ahb);
1585                goto free_sdhci;
1586        }
1587
1588        imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
1589        if (IS_ERR(imx_data->clk_per)) {
1590                err = PTR_ERR(imx_data->clk_per);
1591                goto free_sdhci;
1592        }
1593
1594        pltfm_host->clk = imx_data->clk_per;
1595        pltfm_host->clock = clk_get_rate(pltfm_host->clk);
1596        err = clk_prepare_enable(imx_data->clk_per);
1597        if (err)
1598                goto free_sdhci;
1599        err = clk_prepare_enable(imx_data->clk_ipg);
1600        if (err)
1601                goto disable_per_clk;
1602        err = clk_prepare_enable(imx_data->clk_ahb);
1603        if (err)
1604                goto disable_ipg_clk;
1605
1606        imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
1607        if (IS_ERR(imx_data->pinctrl)) {
1608                err = PTR_ERR(imx_data->pinctrl);
1609                dev_warn(mmc_dev(host->mmc), "could not get pinctrl\n");
1610        }
1611
1612        if (esdhc_is_usdhc(imx_data)) {
1613                host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
1614                host->mmc->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR;
1615
1616                /* GPIO CD can be set as a wakeup source */
1617                host->mmc->caps |= MMC_CAP_CD_WAKE;
1618
1619                if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
1620                        host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
1621
1622                /* clear tuning bits in case ROM has set it already */
1623                writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
1624                writel(0x0, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1625                writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1626
1627                /*
1628                 * Link usdhc specific mmc_host_ops execute_tuning function,
1629                 * to replace the standard one in sdhci_ops.
1630                 */
1631                host->mmc_host_ops.execute_tuning = usdhc_execute_tuning;
1632        }
1633
1634        if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
1635                sdhci_esdhc_ops.platform_execute_tuning =
1636                                        esdhc_executing_tuning;
1637
1638        if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
1639                host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
1640
1641        if (imx_data->socdata->flags & ESDHC_FLAG_HS400)
1642                host->quirks2 |= SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400;
1643
1644        if (imx_data->socdata->flags & ESDHC_FLAG_BROKEN_AUTO_CMD23)
1645                host->quirks2 |= SDHCI_QUIRK2_ACMD23_BROKEN;
1646
1647        if (imx_data->socdata->flags & ESDHC_FLAG_HS400_ES) {
1648                host->mmc->caps2 |= MMC_CAP2_HS400_ES;
1649                host->mmc_host_ops.hs400_enhanced_strobe =
1650                                        esdhc_hs400_enhanced_strobe;
1651        }
1652
1653        if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1654                host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
1655                cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL);
1656                if (!cq_host) {
1657                        err = -ENOMEM;
1658                        goto disable_ahb_clk;
1659                }
1660
1661                cq_host->mmio = host->ioaddr + ESDHC_CQHCI_ADDR_OFFSET;
1662                cq_host->ops = &esdhc_cqhci_ops;
1663
1664                err = cqhci_init(cq_host, host->mmc, false);
1665                if (err)
1666                        goto disable_ahb_clk;
1667        }
1668
1669        if (of_id)
1670                err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
1671        else
1672                err = sdhci_esdhc_imx_probe_nondt(pdev, host, imx_data);
1673        if (err)
1674                goto disable_ahb_clk;
1675
1676        sdhci_esdhc_imx_hwinit(host);
1677
1678        err = sdhci_add_host(host);
1679        if (err)
1680                goto disable_ahb_clk;
1681
1682        pm_runtime_set_active(&pdev->dev);
1683        pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1684        pm_runtime_use_autosuspend(&pdev->dev);
1685        pm_suspend_ignore_children(&pdev->dev, 1);
1686        pm_runtime_enable(&pdev->dev);
1687
1688        return 0;
1689
1690disable_ahb_clk:
1691        clk_disable_unprepare(imx_data->clk_ahb);
1692disable_ipg_clk:
1693        clk_disable_unprepare(imx_data->clk_ipg);
1694disable_per_clk:
1695        clk_disable_unprepare(imx_data->clk_per);
1696free_sdhci:
1697        if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1698                cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1699        sdhci_pltfm_free(pdev);
1700        return err;
1701}
1702
1703static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
1704{
1705        struct sdhci_host *host = platform_get_drvdata(pdev);
1706        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1707        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1708        int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1709
1710        pm_runtime_get_sync(&pdev->dev);
1711        pm_runtime_disable(&pdev->dev);
1712        pm_runtime_put_noidle(&pdev->dev);
1713
1714        sdhci_remove_host(host, dead);
1715
1716        clk_disable_unprepare(imx_data->clk_per);
1717        clk_disable_unprepare(imx_data->clk_ipg);
1718        clk_disable_unprepare(imx_data->clk_ahb);
1719
1720        if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1721                cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1722
1723        sdhci_pltfm_free(pdev);
1724
1725        return 0;
1726}
1727
1728#ifdef CONFIG_PM_SLEEP
1729static int sdhci_esdhc_suspend(struct device *dev)
1730{
1731        struct sdhci_host *host = dev_get_drvdata(dev);
1732        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1733        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1734        int ret;
1735
1736        if (host->mmc->caps2 & MMC_CAP2_CQE) {
1737                ret = cqhci_suspend(host->mmc);
1738                if (ret)
1739                        return ret;
1740        }
1741
1742        if ((imx_data->socdata->flags & ESDHC_FLAG_STATE_LOST_IN_LPMODE) &&
1743                (host->tuning_mode != SDHCI_TUNING_MODE_1)) {
1744                mmc_retune_timer_stop(host->mmc);
1745                mmc_retune_needed(host->mmc);
1746        }
1747
1748        if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1749                mmc_retune_needed(host->mmc);
1750
1751        ret = sdhci_suspend_host(host);
1752        if (ret)
1753                return ret;
1754
1755        ret = pinctrl_pm_select_sleep_state(dev);
1756        if (ret)
1757                return ret;
1758
1759        ret = mmc_gpio_set_cd_wake(host->mmc, true);
1760
1761        return ret;
1762}
1763
1764static int sdhci_esdhc_resume(struct device *dev)
1765{
1766        struct sdhci_host *host = dev_get_drvdata(dev);
1767        int ret;
1768
1769        ret = pinctrl_pm_select_default_state(dev);
1770        if (ret)
1771                return ret;
1772
1773        /* re-initialize hw state in case it's lost in low power mode */
1774        sdhci_esdhc_imx_hwinit(host);
1775
1776        ret = sdhci_resume_host(host);
1777        if (ret)
1778                return ret;
1779
1780        if (host->mmc->caps2 & MMC_CAP2_CQE)
1781                ret = cqhci_resume(host->mmc);
1782
1783        if (!ret)
1784                ret = mmc_gpio_set_cd_wake(host->mmc, false);
1785
1786        return ret;
1787}
1788#endif
1789
1790#ifdef CONFIG_PM
1791static int sdhci_esdhc_runtime_suspend(struct device *dev)
1792{
1793        struct sdhci_host *host = dev_get_drvdata(dev);
1794        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1795        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1796        int ret;
1797
1798        if (host->mmc->caps2 & MMC_CAP2_CQE) {
1799                ret = cqhci_suspend(host->mmc);
1800                if (ret)
1801                        return ret;
1802        }
1803
1804        ret = sdhci_runtime_suspend_host(host);
1805        if (ret)
1806                return ret;
1807
1808        if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1809                mmc_retune_needed(host->mmc);
1810
1811        imx_data->actual_clock = host->mmc->actual_clock;
1812        esdhc_pltfm_set_clock(host, 0);
1813        clk_disable_unprepare(imx_data->clk_per);
1814        clk_disable_unprepare(imx_data->clk_ipg);
1815        clk_disable_unprepare(imx_data->clk_ahb);
1816
1817        if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1818                cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1819
1820        return ret;
1821}
1822
1823static int sdhci_esdhc_runtime_resume(struct device *dev)
1824{
1825        struct sdhci_host *host = dev_get_drvdata(dev);
1826        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1827        struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1828        int err;
1829
1830        if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1831                cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
1832
1833        if (imx_data->socdata->flags & ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME)
1834                clk_set_rate(imx_data->clk_per, pltfm_host->clock);
1835
1836        err = clk_prepare_enable(imx_data->clk_ahb);
1837        if (err)
1838                goto remove_pm_qos_request;
1839
1840        err = clk_prepare_enable(imx_data->clk_per);
1841        if (err)
1842                goto disable_ahb_clk;
1843
1844        err = clk_prepare_enable(imx_data->clk_ipg);
1845        if (err)
1846                goto disable_per_clk;
1847
1848        esdhc_pltfm_set_clock(host, imx_data->actual_clock);
1849
1850        err = sdhci_runtime_resume_host(host, 0);
1851        if (err)
1852                goto disable_ipg_clk;
1853
1854        if (host->mmc->caps2 & MMC_CAP2_CQE)
1855                err = cqhci_resume(host->mmc);
1856
1857        return err;
1858
1859disable_ipg_clk:
1860        clk_disable_unprepare(imx_data->clk_ipg);
1861disable_per_clk:
1862        clk_disable_unprepare(imx_data->clk_per);
1863disable_ahb_clk:
1864        clk_disable_unprepare(imx_data->clk_ahb);
1865remove_pm_qos_request:
1866        if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1867                cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1868        return err;
1869}
1870#endif
1871
1872static const struct dev_pm_ops sdhci_esdhc_pmops = {
1873        SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
1874        SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
1875                                sdhci_esdhc_runtime_resume, NULL)
1876};
1877
1878static struct platform_driver sdhci_esdhc_imx_driver = {
1879        .driver         = {
1880                .name   = "sdhci-esdhc-imx",
1881                .of_match_table = imx_esdhc_dt_ids,
1882                .pm     = &sdhci_esdhc_pmops,
1883        },
1884        .id_table       = imx_esdhc_devtype,
1885        .probe          = sdhci_esdhc_imx_probe,
1886        .remove         = sdhci_esdhc_imx_remove,
1887};
1888
1889module_platform_driver(sdhci_esdhc_imx_driver);
1890
1891MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1892MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
1893MODULE_LICENSE("GPL v2");
1894