Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Arasan Secure Digital Host Controller Interface.
0004  * Copyright (C) 2011 - 2012 Michal Simek <monstr@monstr.eu>
0005  * Copyright (c) 2012 Wind River Systems, Inc.
0006  * Copyright (C) 2013 Pengutronix e.K.
0007  * Copyright (C) 2013 Xilinx Inc.
0008  *
0009  * Based on sdhci-of-esdhc.c
0010  *
0011  * Copyright (c) 2007 Freescale Semiconductor, Inc.
0012  * Copyright (c) 2009 MontaVista Software, Inc.
0013  *
0014  * Authors: Xiaobo Xie <X.Xie@freescale.com>
0015  *      Anton Vorontsov <avorontsov@ru.mvista.com>
0016  */
0017 
0018 #include <linux/clk-provider.h>
0019 #include <linux/mfd/syscon.h>
0020 #include <linux/module.h>
0021 #include <linux/of_device.h>
0022 #include <linux/phy/phy.h>
0023 #include <linux/regmap.h>
0024 #include <linux/of.h>
0025 #include <linux/firmware/xlnx-zynqmp.h>
0026 
0027 #include "cqhci.h"
0028 #include "sdhci-pltfm.h"
0029 
0030 #define SDHCI_ARASAN_VENDOR_REGISTER    0x78
0031 
0032 #define SDHCI_ARASAN_ITAPDLY_REGISTER   0xF0F8
0033 #define SDHCI_ARASAN_ITAPDLY_SEL_MASK   0xFF
0034 
0035 #define SDHCI_ARASAN_OTAPDLY_REGISTER   0xF0FC
0036 #define SDHCI_ARASAN_OTAPDLY_SEL_MASK   0x3F
0037 
0038 #define SDHCI_ARASAN_CQE_BASE_ADDR  0x200
0039 #define VENDOR_ENHANCED_STROBE      BIT(0)
0040 
0041 #define PHY_CLK_TOO_SLOW_HZ     400000
0042 
0043 #define SDHCI_ITAPDLY_CHGWIN        0x200
0044 #define SDHCI_ITAPDLY_ENABLE        0x100
0045 #define SDHCI_OTAPDLY_ENABLE        0x40
0046 
0047 /* Default settings for ZynqMP Clock Phases */
0048 #define ZYNQMP_ICLK_PHASE {0, 63, 63, 0, 63,  0,   0, 183, 54,  0, 0}
0049 #define ZYNQMP_OCLK_PHASE {0, 72, 60, 0, 60, 72, 135, 48, 72, 135, 0}
0050 
0051 #define VERSAL_ICLK_PHASE {0, 132, 132, 0, 132, 0, 0, 162, 90, 0, 0}
0052 #define VERSAL_OCLK_PHASE {0,  60, 48, 0, 48, 72, 90, 36, 60, 90, 0}
0053 
0054 /*
0055  * On some SoCs the syscon area has a feature where the upper 16-bits of
0056  * each 32-bit register act as a write mask for the lower 16-bits.  This allows
0057  * atomic updates of the register without locking.  This macro is used on SoCs
0058  * that have that feature.
0059  */
0060 #define HIWORD_UPDATE(val, mask, shift) \
0061         ((val) << (shift) | (mask) << ((shift) + 16))
0062 
0063 /**
0064  * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
0065  *
0066  * @reg:    Offset within the syscon of the register containing this field
0067  * @width:  Number of bits for this field
0068  * @shift:  Bit offset within @reg of this field (or -1 if not avail)
0069  */
0070 struct sdhci_arasan_soc_ctl_field {
0071     u32 reg;
0072     u16 width;
0073     s16 shift;
0074 };
0075 
0076 /**
0077  * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
0078  *
0079  * @baseclkfreq:    Where to find corecfg_baseclkfreq
0080  * @clockmultiplier:    Where to find corecfg_clockmultiplier
0081  * @support64b:     Where to find SUPPORT64B bit
0082  * @hiword_update:  If true, use HIWORD_UPDATE to access the syscon
0083  *
0084  * It's up to the licensee of the Arsan IP block to make these available
0085  * somewhere if needed.  Presumably these will be scattered somewhere that's
0086  * accessible via the syscon API.
0087  */
0088 struct sdhci_arasan_soc_ctl_map {
0089     struct sdhci_arasan_soc_ctl_field   baseclkfreq;
0090     struct sdhci_arasan_soc_ctl_field   clockmultiplier;
0091     struct sdhci_arasan_soc_ctl_field   support64b;
0092     bool                    hiword_update;
0093 };
0094 
0095 /**
0096  * struct sdhci_arasan_clk_ops - Clock Operations for Arasan SD controller
0097  *
0098  * @sdcardclk_ops:  The output clock related operations
0099  * @sampleclk_ops:  The sample clock related operations
0100  */
0101 struct sdhci_arasan_clk_ops {
0102     const struct clk_ops *sdcardclk_ops;
0103     const struct clk_ops *sampleclk_ops;
0104 };
0105 
0106 /**
0107  * struct sdhci_arasan_clk_data - Arasan Controller Clock Data.
0108  *
0109  * @sdcardclk_hw:   Struct for the clock we might provide to a PHY.
0110  * @sdcardclk:      Pointer to normal 'struct clock' for sdcardclk_hw.
0111  * @sampleclk_hw:   Struct for the clock we might provide to a PHY.
0112  * @sampleclk:      Pointer to normal 'struct clock' for sampleclk_hw.
0113  * @clk_phase_in:   Array of Input Clock Phase Delays for all speed modes
0114  * @clk_phase_out:  Array of Output Clock Phase Delays for all speed modes
0115  * @set_clk_delays: Function pointer for setting Clock Delays
0116  * @clk_of_data:    Platform specific runtime clock data storage pointer
0117  */
0118 struct sdhci_arasan_clk_data {
0119     struct clk_hw   sdcardclk_hw;
0120     struct clk      *sdcardclk;
0121     struct clk_hw   sampleclk_hw;
0122     struct clk      *sampleclk;
0123     int     clk_phase_in[MMC_TIMING_MMC_HS400 + 1];
0124     int     clk_phase_out[MMC_TIMING_MMC_HS400 + 1];
0125     void        (*set_clk_delays)(struct sdhci_host *host);
0126     void        *clk_of_data;
0127 };
0128 
0129 /**
0130  * struct sdhci_arasan_data - Arasan Controller Data
0131  *
0132  * @host:       Pointer to the main SDHCI host structure.
0133  * @clk_ahb:        Pointer to the AHB clock
0134  * @phy:        Pointer to the generic phy
0135  * @is_phy_on:      True if the PHY is on; false if not.
0136  * @has_cqe:        True if controller has command queuing engine.
0137  * @clk_data:       Struct for the Arasan Controller Clock Data.
0138  * @clk_ops:        Struct for the Arasan Controller Clock Operations.
0139  * @soc_ctl_base:   Pointer to regmap for syscon for soc_ctl registers.
0140  * @soc_ctl_map:    Map to get offsets into soc_ctl registers.
0141  * @quirks:     Arasan deviations from spec.
0142  */
0143 struct sdhci_arasan_data {
0144     struct sdhci_host *host;
0145     struct clk  *clk_ahb;
0146     struct phy  *phy;
0147     bool        is_phy_on;
0148 
0149     bool        has_cqe;
0150     struct sdhci_arasan_clk_data clk_data;
0151     const struct sdhci_arasan_clk_ops *clk_ops;
0152 
0153     struct regmap   *soc_ctl_base;
0154     const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
0155     unsigned int    quirks;
0156 
0157 /* Controller does not have CD wired and will not function normally without */
0158 #define SDHCI_ARASAN_QUIRK_FORCE_CDTEST BIT(0)
0159 /* Controller immediately reports SDHCI_CLOCK_INT_STABLE after enabling the
0160  * internal clock even when the clock isn't stable */
0161 #define SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE BIT(1)
0162 /*
0163  * Some of the Arasan variations might not have timing requirements
0164  * met at 25MHz for Default Speed mode, those controllers work at
0165  * 19MHz instead
0166  */
0167 #define SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN BIT(2)
0168 };
0169 
0170 struct sdhci_arasan_of_data {
0171     const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
0172     const struct sdhci_pltfm_data *pdata;
0173     const struct sdhci_arasan_clk_ops *clk_ops;
0174 };
0175 
0176 static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
0177     .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
0178     .clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
0179     .hiword_update = true,
0180 };
0181 
0182 static const struct sdhci_arasan_soc_ctl_map intel_lgm_emmc_soc_ctl_map = {
0183     .baseclkfreq = { .reg = 0xa0, .width = 8, .shift = 2 },
0184     .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
0185     .hiword_update = false,
0186 };
0187 
0188 static const struct sdhci_arasan_soc_ctl_map intel_lgm_sdxc_soc_ctl_map = {
0189     .baseclkfreq = { .reg = 0x80, .width = 8, .shift = 2 },
0190     .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
0191     .hiword_update = false,
0192 };
0193 
0194 static const struct sdhci_arasan_soc_ctl_map thunderbay_soc_ctl_map = {
0195     .baseclkfreq = { .reg = 0x0, .width = 8, .shift = 14 },
0196     .clockmultiplier = { .reg = 0x4, .width = 8, .shift = 14 },
0197     .support64b = { .reg = 0x4, .width = 1, .shift = 24 },
0198     .hiword_update = false,
0199 };
0200 
0201 static const struct sdhci_arasan_soc_ctl_map intel_keembay_soc_ctl_map = {
0202     .baseclkfreq = { .reg = 0x0, .width = 8, .shift = 14 },
0203     .clockmultiplier = { .reg = 0x4, .width = 8, .shift = 14 },
0204     .support64b = { .reg = 0x4, .width = 1, .shift = 24 },
0205     .hiword_update = false,
0206 };
0207 
0208 /**
0209  * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
0210  *
0211  * @host:   The sdhci_host
0212  * @fld:    The field to write to
0213  * @val:    The value to write
0214  *
0215  * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
0216  * Note that if a field is specified as not available (shift < 0) then
0217  * this function will silently return an error code.  It will be noisy
0218  * and print errors for any other (unexpected) errors.
0219  *
0220  * Return: 0 on success and error value on error
0221  */
0222 static int sdhci_arasan_syscon_write(struct sdhci_host *host,
0223                    const struct sdhci_arasan_soc_ctl_field *fld,
0224                    u32 val)
0225 {
0226     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0227     struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
0228     struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
0229     u32 reg = fld->reg;
0230     u16 width = fld->width;
0231     s16 shift = fld->shift;
0232     int ret;
0233 
0234     /*
0235      * Silently return errors for shift < 0 so caller doesn't have
0236      * to check for fields which are optional.  For fields that
0237      * are required then caller needs to do something special
0238      * anyway.
0239      */
0240     if (shift < 0)
0241         return -EINVAL;
0242 
0243     if (sdhci_arasan->soc_ctl_map->hiword_update)
0244         ret = regmap_write(soc_ctl_base, reg,
0245                    HIWORD_UPDATE(val, GENMASK(width, 0),
0246                          shift));
0247     else
0248         ret = regmap_update_bits(soc_ctl_base, reg,
0249                      GENMASK(shift + width, shift),
0250                      val << shift);
0251 
0252     /* Yell about (unexpected) regmap errors */
0253     if (ret)
0254         pr_warn("%s: Regmap write fail: %d\n",
0255              mmc_hostname(host->mmc), ret);
0256 
0257     return ret;
0258 }
0259 
0260 static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
0261 {
0262     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0263     struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
0264     struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
0265     bool ctrl_phy = false;
0266 
0267     if (!IS_ERR(sdhci_arasan->phy)) {
0268         if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
0269             /*
0270              * If PHY off, set clock to max speed and power PHY on.
0271              *
0272              * Although PHY docs apparently suggest power cycling
0273              * when changing the clock the PHY doesn't like to be
0274              * powered on while at low speeds like those used in ID
0275              * mode.  Even worse is powering the PHY on while the
0276              * clock is off.
0277              *
0278              * To workaround the PHY limitations, the best we can
0279              * do is to power it on at a faster speed and then slam
0280              * through low speeds without power cycling.
0281              */
0282             sdhci_set_clock(host, host->max_clk);
0283             if (phy_power_on(sdhci_arasan->phy)) {
0284                 pr_err("%s: Cannot power on phy.\n",
0285                        mmc_hostname(host->mmc));
0286                 return;
0287             }
0288 
0289             sdhci_arasan->is_phy_on = true;
0290 
0291             /*
0292              * We'll now fall through to the below case with
0293              * ctrl_phy = false (so we won't turn off/on).  The
0294              * sdhci_set_clock() will set the real clock.
0295              */
0296         } else if (clock > PHY_CLK_TOO_SLOW_HZ) {
0297             /*
0298              * At higher clock speeds the PHY is fine being power
0299              * cycled and docs say you _should_ power cycle when
0300              * changing clock speeds.
0301              */
0302             ctrl_phy = true;
0303         }
0304     }
0305 
0306     if (ctrl_phy && sdhci_arasan->is_phy_on) {
0307         phy_power_off(sdhci_arasan->phy);
0308         sdhci_arasan->is_phy_on = false;
0309     }
0310 
0311     if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN) {
0312         /*
0313          * Some of the Arasan variations might not have timing
0314          * requirements met at 25MHz for Default Speed mode,
0315          * those controllers work at 19MHz instead.
0316          */
0317         if (clock == DEFAULT_SPEED_MAX_DTR)
0318             clock = (DEFAULT_SPEED_MAX_DTR * 19) / 25;
0319     }
0320 
0321     /* Set the Input and Output Clock Phase Delays */
0322     if (clk_data->set_clk_delays)
0323         clk_data->set_clk_delays(host);
0324 
0325     sdhci_set_clock(host, clock);
0326 
0327     if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE)
0328         /*
0329          * Some controllers immediately report SDHCI_CLOCK_INT_STABLE
0330          * after enabling the clock even though the clock is not
0331          * stable. Trying to use a clock without waiting here results
0332          * in EILSEQ while detecting some older/slower cards. The
0333          * chosen delay is the maximum delay from sdhci_set_clock.
0334          */
0335         msleep(20);
0336 
0337     if (ctrl_phy) {
0338         if (phy_power_on(sdhci_arasan->phy)) {
0339             pr_err("%s: Cannot power on phy.\n",
0340                    mmc_hostname(host->mmc));
0341             return;
0342         }
0343 
0344         sdhci_arasan->is_phy_on = true;
0345     }
0346 }
0347 
0348 static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
0349                     struct mmc_ios *ios)
0350 {
0351     u32 vendor;
0352     struct sdhci_host *host = mmc_priv(mmc);
0353 
0354     vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
0355     if (ios->enhanced_strobe)
0356         vendor |= VENDOR_ENHANCED_STROBE;
0357     else
0358         vendor &= ~VENDOR_ENHANCED_STROBE;
0359 
0360     sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
0361 }
0362 
0363 static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
0364 {
0365     u8 ctrl;
0366     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0367     struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
0368 
0369     sdhci_reset(host, mask);
0370 
0371     if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
0372         ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
0373         ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
0374         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
0375     }
0376 }
0377 
0378 static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
0379                        struct mmc_ios *ios)
0380 {
0381     switch (ios->signal_voltage) {
0382     case MMC_SIGNAL_VOLTAGE_180:
0383         /*
0384          * Plese don't switch to 1V8 as arasan,5.1 doesn't
0385          * actually refer to this setting to indicate the
0386          * signal voltage and the state machine will be broken
0387          * actually if we force to enable 1V8. That's something
0388          * like broken quirk but we could work around here.
0389          */
0390         return 0;
0391     case MMC_SIGNAL_VOLTAGE_330:
0392     case MMC_SIGNAL_VOLTAGE_120:
0393         /* We don't support 3V3 and 1V2 */
0394         break;
0395     }
0396 
0397     return -EINVAL;
0398 }
0399 
0400 static const struct sdhci_ops sdhci_arasan_ops = {
0401     .set_clock = sdhci_arasan_set_clock,
0402     .get_max_clock = sdhci_pltfm_clk_get_max_clock,
0403     .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
0404     .set_bus_width = sdhci_set_bus_width,
0405     .reset = sdhci_arasan_reset,
0406     .set_uhs_signaling = sdhci_set_uhs_signaling,
0407     .set_power = sdhci_set_power_and_bus_voltage,
0408 };
0409 
0410 static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
0411 {
0412     int cmd_error = 0;
0413     int data_error = 0;
0414 
0415     if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
0416         return intmask;
0417 
0418     cqhci_irq(host->mmc, intmask, cmd_error, data_error);
0419 
0420     return 0;
0421 }
0422 
0423 static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
0424 {
0425     sdhci_dumpregs(mmc_priv(mmc));
0426 }
0427 
0428 static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
0429 {
0430     struct sdhci_host *host = mmc_priv(mmc);
0431     u32 reg;
0432 
0433     reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
0434     while (reg & SDHCI_DATA_AVAILABLE) {
0435         sdhci_readl(host, SDHCI_BUFFER);
0436         reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
0437     }
0438 
0439     sdhci_cqe_enable(mmc);
0440 }
0441 
0442 static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
0443     .enable         = sdhci_arasan_cqe_enable,
0444     .disable        = sdhci_cqe_disable,
0445     .dumpregs       = sdhci_arasan_dumpregs,
0446 };
0447 
0448 static const struct sdhci_ops sdhci_arasan_cqe_ops = {
0449     .set_clock = sdhci_arasan_set_clock,
0450     .get_max_clock = sdhci_pltfm_clk_get_max_clock,
0451     .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
0452     .set_bus_width = sdhci_set_bus_width,
0453     .reset = sdhci_arasan_reset,
0454     .set_uhs_signaling = sdhci_set_uhs_signaling,
0455     .set_power = sdhci_set_power_and_bus_voltage,
0456     .irq = sdhci_arasan_cqhci_irq,
0457 };
0458 
0459 static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
0460     .ops = &sdhci_arasan_cqe_ops,
0461     .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
0462     .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
0463             SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
0464 };
0465 
0466 static const struct sdhci_pltfm_data sdhci_arasan_thunderbay_pdata = {
0467     .ops = &sdhci_arasan_cqe_ops,
0468     .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
0469     .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
0470         SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
0471         SDHCI_QUIRK2_STOP_WITH_TC |
0472         SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
0473 };
0474 
0475 #ifdef CONFIG_PM_SLEEP
0476 /**
0477  * sdhci_arasan_suspend - Suspend method for the driver
0478  * @dev:    Address of the device structure
0479  *
0480  * Put the device in a low power state.
0481  *
0482  * Return: 0 on success and error value on error
0483  */
0484 static int sdhci_arasan_suspend(struct device *dev)
0485 {
0486     struct sdhci_host *host = dev_get_drvdata(dev);
0487     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0488     struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
0489     int ret;
0490 
0491     if (host->tuning_mode != SDHCI_TUNING_MODE_3)
0492         mmc_retune_needed(host->mmc);
0493 
0494     if (sdhci_arasan->has_cqe) {
0495         ret = cqhci_suspend(host->mmc);
0496         if (ret)
0497             return ret;
0498     }
0499 
0500     ret = sdhci_suspend_host(host);
0501     if (ret)
0502         return ret;
0503 
0504     if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
0505         ret = phy_power_off(sdhci_arasan->phy);
0506         if (ret) {
0507             dev_err(dev, "Cannot power off phy.\n");
0508             if (sdhci_resume_host(host))
0509                 dev_err(dev, "Cannot resume host.\n");
0510 
0511             return ret;
0512         }
0513         sdhci_arasan->is_phy_on = false;
0514     }
0515 
0516     clk_disable(pltfm_host->clk);
0517     clk_disable(sdhci_arasan->clk_ahb);
0518 
0519     return 0;
0520 }
0521 
0522 /**
0523  * sdhci_arasan_resume - Resume method for the driver
0524  * @dev:    Address of the device structure
0525  *
0526  * Resume operation after suspend
0527  *
0528  * Return: 0 on success and error value on error
0529  */
0530 static int sdhci_arasan_resume(struct device *dev)
0531 {
0532     struct sdhci_host *host = dev_get_drvdata(dev);
0533     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0534     struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
0535     int ret;
0536 
0537     ret = clk_enable(sdhci_arasan->clk_ahb);
0538     if (ret) {
0539         dev_err(dev, "Cannot enable AHB clock.\n");
0540         return ret;
0541     }
0542 
0543     ret = clk_enable(pltfm_host->clk);
0544     if (ret) {
0545         dev_err(dev, "Cannot enable SD clock.\n");
0546         return ret;
0547     }
0548 
0549     if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
0550         ret = phy_power_on(sdhci_arasan->phy);
0551         if (ret) {
0552             dev_err(dev, "Cannot power on phy.\n");
0553             return ret;
0554         }
0555         sdhci_arasan->is_phy_on = true;
0556     }
0557 
0558     ret = sdhci_resume_host(host);
0559     if (ret) {
0560         dev_err(dev, "Cannot resume host.\n");
0561         return ret;
0562     }
0563 
0564     if (sdhci_arasan->has_cqe)
0565         return cqhci_resume(host->mmc);
0566 
0567     return 0;
0568 }
0569 #endif /* ! CONFIG_PM_SLEEP */
0570 
0571 static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
0572              sdhci_arasan_resume);
0573 
0574 /**
0575  * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
0576  *
0577  * @hw:         Pointer to the hardware clock structure.
0578  * @parent_rate:        The parent rate (should be rate of clk_xin).
0579  *
0580  * Return the current actual rate of the SD card clock.  This can be used
0581  * to communicate with out PHY.
0582  *
0583  * Return: The card clock rate.
0584  */
0585 static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
0586                               unsigned long parent_rate)
0587 {
0588     struct sdhci_arasan_clk_data *clk_data =
0589         container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
0590     struct sdhci_arasan_data *sdhci_arasan =
0591         container_of(clk_data, struct sdhci_arasan_data, clk_data);
0592     struct sdhci_host *host = sdhci_arasan->host;
0593 
0594     return host->mmc->actual_clock;
0595 }
0596 
0597 static const struct clk_ops arasan_sdcardclk_ops = {
0598     .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
0599 };
0600 
0601 /**
0602  * sdhci_arasan_sampleclk_recalc_rate - Return the sampling clock rate
0603  *
0604  * @hw:         Pointer to the hardware clock structure.
0605  * @parent_rate:        The parent rate (should be rate of clk_xin).
0606  *
0607  * Return the current actual rate of the sampling clock.  This can be used
0608  * to communicate with out PHY.
0609  *
0610  * Return: The sample clock rate.
0611  */
0612 static unsigned long sdhci_arasan_sampleclk_recalc_rate(struct clk_hw *hw,
0613                               unsigned long parent_rate)
0614 {
0615     struct sdhci_arasan_clk_data *clk_data =
0616         container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
0617     struct sdhci_arasan_data *sdhci_arasan =
0618         container_of(clk_data, struct sdhci_arasan_data, clk_data);
0619     struct sdhci_host *host = sdhci_arasan->host;
0620 
0621     return host->mmc->actual_clock;
0622 }
0623 
0624 static const struct clk_ops arasan_sampleclk_ops = {
0625     .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
0626 };
0627 
0628 /**
0629  * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
0630  *
0631  * @hw:         Pointer to the hardware clock structure.
0632  * @degrees:        The clock phase shift between 0 - 359.
0633  *
0634  * Set the SD Output Clock Tap Delays for Output path
0635  *
0636  * Return: 0 on success and error value on error
0637  */
0638 static int sdhci_zynqmp_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
0639 {
0640     struct sdhci_arasan_clk_data *clk_data =
0641         container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
0642     struct sdhci_arasan_data *sdhci_arasan =
0643         container_of(clk_data, struct sdhci_arasan_data, clk_data);
0644     struct sdhci_host *host = sdhci_arasan->host;
0645     const char *clk_name = clk_hw_get_name(hw);
0646     u32 node_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : NODE_SD_1;
0647     u8 tap_delay, tap_max = 0;
0648     int ret;
0649 
0650     /* This is applicable for SDHCI_SPEC_300 and above */
0651     if (host->version < SDHCI_SPEC_300)
0652         return 0;
0653 
0654     switch (host->timing) {
0655     case MMC_TIMING_MMC_HS:
0656     case MMC_TIMING_SD_HS:
0657     case MMC_TIMING_UHS_SDR25:
0658     case MMC_TIMING_UHS_DDR50:
0659     case MMC_TIMING_MMC_DDR52:
0660         /* For 50MHz clock, 30 Taps are available */
0661         tap_max = 30;
0662         break;
0663     case MMC_TIMING_UHS_SDR50:
0664         /* For 100MHz clock, 15 Taps are available */
0665         tap_max = 15;
0666         break;
0667     case MMC_TIMING_UHS_SDR104:
0668     case MMC_TIMING_MMC_HS200:
0669         /* For 200MHz clock, 8 Taps are available */
0670         tap_max = 8;
0671         break;
0672     default:
0673         break;
0674     }
0675 
0676     tap_delay = (degrees * tap_max) / 360;
0677 
0678     /* Set the Clock Phase */
0679     ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_OUTPUT, tap_delay);
0680     if (ret)
0681         pr_err("Error setting Output Tap Delay\n");
0682 
0683     /* Release DLL Reset */
0684     zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_RELEASE);
0685 
0686     return ret;
0687 }
0688 
0689 static const struct clk_ops zynqmp_sdcardclk_ops = {
0690     .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
0691     .set_phase = sdhci_zynqmp_sdcardclk_set_phase,
0692 };
0693 
0694 /**
0695  * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
0696  *
0697  * @hw:         Pointer to the hardware clock structure.
0698  * @degrees:        The clock phase shift between 0 - 359.
0699  *
0700  * Set the SD Input Clock Tap Delays for Input path
0701  *
0702  * Return: 0 on success and error value on error
0703  */
0704 static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees)
0705 {
0706     struct sdhci_arasan_clk_data *clk_data =
0707         container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
0708     struct sdhci_arasan_data *sdhci_arasan =
0709         container_of(clk_data, struct sdhci_arasan_data, clk_data);
0710     struct sdhci_host *host = sdhci_arasan->host;
0711     const char *clk_name = clk_hw_get_name(hw);
0712     u32 node_id = !strcmp(clk_name, "clk_in_sd0") ? NODE_SD_0 : NODE_SD_1;
0713     u8 tap_delay, tap_max = 0;
0714     int ret;
0715 
0716     /* This is applicable for SDHCI_SPEC_300 and above */
0717     if (host->version < SDHCI_SPEC_300)
0718         return 0;
0719 
0720     /* Assert DLL Reset */
0721     zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_ASSERT);
0722 
0723     switch (host->timing) {
0724     case MMC_TIMING_MMC_HS:
0725     case MMC_TIMING_SD_HS:
0726     case MMC_TIMING_UHS_SDR25:
0727     case MMC_TIMING_UHS_DDR50:
0728     case MMC_TIMING_MMC_DDR52:
0729         /* For 50MHz clock, 120 Taps are available */
0730         tap_max = 120;
0731         break;
0732     case MMC_TIMING_UHS_SDR50:
0733         /* For 100MHz clock, 60 Taps are available */
0734         tap_max = 60;
0735         break;
0736     case MMC_TIMING_UHS_SDR104:
0737     case MMC_TIMING_MMC_HS200:
0738         /* For 200MHz clock, 30 Taps are available */
0739         tap_max = 30;
0740         break;
0741     default:
0742         break;
0743     }
0744 
0745     tap_delay = (degrees * tap_max) / 360;
0746 
0747     /* Set the Clock Phase */
0748     ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_INPUT, tap_delay);
0749     if (ret)
0750         pr_err("Error setting Input Tap Delay\n");
0751 
0752     return ret;
0753 }
0754 
0755 static const struct clk_ops zynqmp_sampleclk_ops = {
0756     .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
0757     .set_phase = sdhci_zynqmp_sampleclk_set_phase,
0758 };
0759 
0760 /**
0761  * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
0762  *
0763  * @hw:         Pointer to the hardware clock structure.
0764  * @degrees:        The clock phase shift between 0 - 359.
0765  *
0766  * Set the SD Output Clock Tap Delays for Output path
0767  *
0768  * Return: 0 on success and error value on error
0769  */
0770 static int sdhci_versal_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
0771 {
0772     struct sdhci_arasan_clk_data *clk_data =
0773         container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
0774     struct sdhci_arasan_data *sdhci_arasan =
0775         container_of(clk_data, struct sdhci_arasan_data, clk_data);
0776     struct sdhci_host *host = sdhci_arasan->host;
0777     u8 tap_delay, tap_max = 0;
0778 
0779     /* This is applicable for SDHCI_SPEC_300 and above */
0780     if (host->version < SDHCI_SPEC_300)
0781         return 0;
0782 
0783     switch (host->timing) {
0784     case MMC_TIMING_MMC_HS:
0785     case MMC_TIMING_SD_HS:
0786     case MMC_TIMING_UHS_SDR25:
0787     case MMC_TIMING_UHS_DDR50:
0788     case MMC_TIMING_MMC_DDR52:
0789         /* For 50MHz clock, 30 Taps are available */
0790         tap_max = 30;
0791         break;
0792     case MMC_TIMING_UHS_SDR50:
0793         /* For 100MHz clock, 15 Taps are available */
0794         tap_max = 15;
0795         break;
0796     case MMC_TIMING_UHS_SDR104:
0797     case MMC_TIMING_MMC_HS200:
0798         /* For 200MHz clock, 8 Taps are available */
0799         tap_max = 8;
0800         break;
0801     default:
0802         break;
0803     }
0804 
0805     tap_delay = (degrees * tap_max) / 360;
0806 
0807     /* Set the Clock Phase */
0808     if (tap_delay) {
0809         u32 regval;
0810 
0811         regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
0812         regval |= SDHCI_OTAPDLY_ENABLE;
0813         sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
0814         regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK;
0815         regval |= tap_delay;
0816         sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
0817     }
0818 
0819     return 0;
0820 }
0821 
0822 static const struct clk_ops versal_sdcardclk_ops = {
0823     .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
0824     .set_phase = sdhci_versal_sdcardclk_set_phase,
0825 };
0826 
0827 /**
0828  * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
0829  *
0830  * @hw:         Pointer to the hardware clock structure.
0831  * @degrees:        The clock phase shift between 0 - 359.
0832  *
0833  * Set the SD Input Clock Tap Delays for Input path
0834  *
0835  * Return: 0 on success and error value on error
0836  */
0837 static int sdhci_versal_sampleclk_set_phase(struct clk_hw *hw, int degrees)
0838 {
0839     struct sdhci_arasan_clk_data *clk_data =
0840         container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
0841     struct sdhci_arasan_data *sdhci_arasan =
0842         container_of(clk_data, struct sdhci_arasan_data, clk_data);
0843     struct sdhci_host *host = sdhci_arasan->host;
0844     u8 tap_delay, tap_max = 0;
0845 
0846     /* This is applicable for SDHCI_SPEC_300 and above */
0847     if (host->version < SDHCI_SPEC_300)
0848         return 0;
0849 
0850     switch (host->timing) {
0851     case MMC_TIMING_MMC_HS:
0852     case MMC_TIMING_SD_HS:
0853     case MMC_TIMING_UHS_SDR25:
0854     case MMC_TIMING_UHS_DDR50:
0855     case MMC_TIMING_MMC_DDR52:
0856         /* For 50MHz clock, 120 Taps are available */
0857         tap_max = 120;
0858         break;
0859     case MMC_TIMING_UHS_SDR50:
0860         /* For 100MHz clock, 60 Taps are available */
0861         tap_max = 60;
0862         break;
0863     case MMC_TIMING_UHS_SDR104:
0864     case MMC_TIMING_MMC_HS200:
0865         /* For 200MHz clock, 30 Taps are available */
0866         tap_max = 30;
0867         break;
0868     default:
0869         break;
0870     }
0871 
0872     tap_delay = (degrees * tap_max) / 360;
0873 
0874     /* Set the Clock Phase */
0875     if (tap_delay) {
0876         u32 regval;
0877 
0878         regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
0879         regval |= SDHCI_ITAPDLY_CHGWIN;
0880         sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
0881         regval |= SDHCI_ITAPDLY_ENABLE;
0882         sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
0883         regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK;
0884         regval |= tap_delay;
0885         sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
0886         regval &= ~SDHCI_ITAPDLY_CHGWIN;
0887         sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
0888     }
0889 
0890     return 0;
0891 }
0892 
0893 static const struct clk_ops versal_sampleclk_ops = {
0894     .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
0895     .set_phase = sdhci_versal_sampleclk_set_phase,
0896 };
0897 
0898 static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 deviceid)
0899 {
0900     u16 clk;
0901 
0902     clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
0903     clk &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN);
0904     sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
0905 
0906     /* Issue DLL Reset */
0907     zynqmp_pm_sd_dll_reset(deviceid, PM_DLL_RESET_PULSE);
0908 
0909     clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
0910 
0911     sdhci_enable_clk(host, clk);
0912 }
0913 
0914 static int arasan_zynqmp_execute_tuning(struct mmc_host *mmc, u32 opcode)
0915 {
0916     struct sdhci_host *host = mmc_priv(mmc);
0917     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0918     struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
0919     struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw;
0920     const char *clk_name = clk_hw_get_name(hw);
0921     u32 device_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 :
0922                                NODE_SD_1;
0923     int err;
0924 
0925     /* ZynqMP SD controller does not perform auto tuning in DDR50 mode */
0926     if (mmc->ios.timing == MMC_TIMING_UHS_DDR50)
0927         return 0;
0928 
0929     arasan_zynqmp_dll_reset(host, device_id);
0930 
0931     err = sdhci_execute_tuning(mmc, opcode);
0932     if (err)
0933         return err;
0934 
0935     arasan_zynqmp_dll_reset(host, device_id);
0936 
0937     return 0;
0938 }
0939 
0940 /**
0941  * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
0942  *
0943  * @host:       The sdhci_host
0944  * @value:      The value to write
0945  *
0946  * The corecfg_clockmultiplier is supposed to contain clock multiplier
0947  * value of programmable clock generator.
0948  *
0949  * NOTES:
0950  * - Many existing devices don't seem to do this and work fine.  To keep
0951  *   compatibility for old hardware where the device tree doesn't provide a
0952  *   register map, this function is a noop if a soc_ctl_map hasn't been provided
0953  *   for this platform.
0954  * - The value of corecfg_clockmultiplier should sync with that of corresponding
0955  *   value reading from sdhci_capability_register. So this function is called
0956  *   once at probe time and never called again.
0957  */
0958 static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
0959                         u32 value)
0960 {
0961     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0962     struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
0963     const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
0964         sdhci_arasan->soc_ctl_map;
0965 
0966     /* Having a map is optional */
0967     if (!soc_ctl_map)
0968         return;
0969 
0970     /* If we have a map, we expect to have a syscon */
0971     if (!sdhci_arasan->soc_ctl_base) {
0972         pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
0973             mmc_hostname(host->mmc));
0974         return;
0975     }
0976 
0977     sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
0978 }
0979 
0980 /**
0981  * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
0982  *
0983  * @host:       The sdhci_host
0984  *
0985  * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin.  This
0986  * function can be used to make that happen.
0987  *
0988  * NOTES:
0989  * - Many existing devices don't seem to do this and work fine.  To keep
0990  *   compatibility for old hardware where the device tree doesn't provide a
0991  *   register map, this function is a noop if a soc_ctl_map hasn't been provided
0992  *   for this platform.
0993  * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
0994  *   to achieve lower clock rates.  That means that this function is called once
0995  *   at probe time and never called again.
0996  */
0997 static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
0998 {
0999     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1000     struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1001     const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
1002         sdhci_arasan->soc_ctl_map;
1003     u32 mhz = DIV_ROUND_CLOSEST_ULL(clk_get_rate(pltfm_host->clk), 1000000);
1004 
1005     /* Having a map is optional */
1006     if (!soc_ctl_map)
1007         return;
1008 
1009     /* If we have a map, we expect to have a syscon */
1010     if (!sdhci_arasan->soc_ctl_base) {
1011         pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1012             mmc_hostname(host->mmc));
1013         return;
1014     }
1015 
1016     sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
1017 }
1018 
1019 static void sdhci_arasan_set_clk_delays(struct sdhci_host *host)
1020 {
1021     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1022     struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1023     struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1024 
1025     clk_set_phase(clk_data->sampleclk,
1026               clk_data->clk_phase_in[host->timing]);
1027     clk_set_phase(clk_data->sdcardclk,
1028               clk_data->clk_phase_out[host->timing]);
1029 }
1030 
1031 static void arasan_dt_read_clk_phase(struct device *dev,
1032                      struct sdhci_arasan_clk_data *clk_data,
1033                      unsigned int timing, const char *prop)
1034 {
1035     struct device_node *np = dev->of_node;
1036 
1037     u32 clk_phase[2] = {0};
1038     int ret;
1039 
1040     /*
1041      * Read Tap Delay values from DT, if the DT does not contain the
1042      * Tap Values then use the pre-defined values.
1043      */
1044     ret = of_property_read_variable_u32_array(np, prop, &clk_phase[0],
1045                           2, 0);
1046     if (ret < 0) {
1047         dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
1048             prop, clk_data->clk_phase_in[timing],
1049             clk_data->clk_phase_out[timing]);
1050         return;
1051     }
1052 
1053     /* The values read are Input and Output Clock Delays in order */
1054     clk_data->clk_phase_in[timing] = clk_phase[0];
1055     clk_data->clk_phase_out[timing] = clk_phase[1];
1056 }
1057 
1058 /**
1059  * arasan_dt_parse_clk_phases - Read Clock Delay values from DT
1060  *
1061  * @dev:        Pointer to our struct device.
1062  * @clk_data:       Pointer to the Clock Data structure
1063  *
1064  * Called at initialization to parse the values of Clock Delays.
1065  */
1066 static void arasan_dt_parse_clk_phases(struct device *dev,
1067                        struct sdhci_arasan_clk_data *clk_data)
1068 {
1069     u32 mio_bank = 0;
1070     int i;
1071 
1072     /*
1073      * This has been kept as a pointer and is assigned a function here.
1074      * So that different controller variants can assign their own handling
1075      * function.
1076      */
1077     clk_data->set_clk_delays = sdhci_arasan_set_clk_delays;
1078 
1079     if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) {
1080         u32 zynqmp_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1081             ZYNQMP_ICLK_PHASE;
1082         u32 zynqmp_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1083             ZYNQMP_OCLK_PHASE;
1084 
1085         of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank);
1086         if (mio_bank == 2) {
1087             zynqmp_oclk_phase[MMC_TIMING_UHS_SDR104] = 90;
1088             zynqmp_oclk_phase[MMC_TIMING_MMC_HS200] = 90;
1089         }
1090 
1091         for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1092             clk_data->clk_phase_in[i] = zynqmp_iclk_phase[i];
1093             clk_data->clk_phase_out[i] = zynqmp_oclk_phase[i];
1094         }
1095     }
1096 
1097     if (of_device_is_compatible(dev->of_node, "xlnx,versal-8.9a")) {
1098         u32 versal_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1099             VERSAL_ICLK_PHASE;
1100         u32 versal_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1101             VERSAL_OCLK_PHASE;
1102 
1103         for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1104             clk_data->clk_phase_in[i] = versal_iclk_phase[i];
1105             clk_data->clk_phase_out[i] = versal_oclk_phase[i];
1106         }
1107     }
1108 
1109     arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY,
1110                  "clk-phase-legacy");
1111     arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS,
1112                  "clk-phase-mmc-hs");
1113     arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS,
1114                  "clk-phase-sd-hs");
1115     arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12,
1116                  "clk-phase-uhs-sdr12");
1117     arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25,
1118                  "clk-phase-uhs-sdr25");
1119     arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50,
1120                  "clk-phase-uhs-sdr50");
1121     arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104,
1122                  "clk-phase-uhs-sdr104");
1123     arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50,
1124                  "clk-phase-uhs-ddr50");
1125     arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52,
1126                  "clk-phase-mmc-ddr52");
1127     arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200,
1128                  "clk-phase-mmc-hs200");
1129     arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400,
1130                  "clk-phase-mmc-hs400");
1131 }
1132 
1133 static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
1134     .ops = &sdhci_arasan_ops,
1135     .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1136     .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1137             SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1138             SDHCI_QUIRK2_STOP_WITH_TC,
1139 };
1140 
1141 static const struct sdhci_arasan_clk_ops arasan_clk_ops = {
1142     .sdcardclk_ops = &arasan_sdcardclk_ops,
1143     .sampleclk_ops = &arasan_sampleclk_ops,
1144 };
1145 
1146 static struct sdhci_arasan_of_data sdhci_arasan_generic_data = {
1147     .pdata = &sdhci_arasan_pdata,
1148     .clk_ops = &arasan_clk_ops,
1149 };
1150 
1151 static const struct sdhci_arasan_of_data sdhci_arasan_thunderbay_data = {
1152     .soc_ctl_map = &thunderbay_soc_ctl_map,
1153     .pdata = &sdhci_arasan_thunderbay_pdata,
1154     .clk_ops = &arasan_clk_ops,
1155 };
1156 
1157 static const struct sdhci_pltfm_data sdhci_keembay_emmc_pdata = {
1158     .ops = &sdhci_arasan_cqe_ops,
1159     .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1160         SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1161         SDHCI_QUIRK_NO_LED |
1162         SDHCI_QUIRK_32BIT_DMA_ADDR |
1163         SDHCI_QUIRK_32BIT_DMA_SIZE |
1164         SDHCI_QUIRK_32BIT_ADMA_SIZE,
1165     .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1166         SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1167         SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
1168         SDHCI_QUIRK2_STOP_WITH_TC |
1169         SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1170 };
1171 
1172 static const struct sdhci_pltfm_data sdhci_keembay_sd_pdata = {
1173     .ops = &sdhci_arasan_ops,
1174     .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1175         SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1176         SDHCI_QUIRK_NO_LED |
1177         SDHCI_QUIRK_32BIT_DMA_ADDR |
1178         SDHCI_QUIRK_32BIT_DMA_SIZE |
1179         SDHCI_QUIRK_32BIT_ADMA_SIZE,
1180     .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1181         SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1182         SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
1183         SDHCI_QUIRK2_STOP_WITH_TC |
1184         SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1185 };
1186 
1187 static const struct sdhci_pltfm_data sdhci_keembay_sdio_pdata = {
1188     .ops = &sdhci_arasan_ops,
1189     .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1190         SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1191         SDHCI_QUIRK_NO_LED |
1192         SDHCI_QUIRK_32BIT_DMA_ADDR |
1193         SDHCI_QUIRK_32BIT_DMA_SIZE |
1194         SDHCI_QUIRK_32BIT_ADMA_SIZE,
1195     .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1196         SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1197         SDHCI_QUIRK2_HOST_OFF_CARD_ON |
1198         SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1199 };
1200 
1201 static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = {
1202     .soc_ctl_map = &rk3399_soc_ctl_map,
1203     .pdata = &sdhci_arasan_cqe_pdata,
1204     .clk_ops = &arasan_clk_ops,
1205 };
1206 
1207 static struct sdhci_arasan_of_data intel_lgm_emmc_data = {
1208     .soc_ctl_map = &intel_lgm_emmc_soc_ctl_map,
1209     .pdata = &sdhci_arasan_cqe_pdata,
1210     .clk_ops = &arasan_clk_ops,
1211 };
1212 
1213 static struct sdhci_arasan_of_data intel_lgm_sdxc_data = {
1214     .soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map,
1215     .pdata = &sdhci_arasan_cqe_pdata,
1216     .clk_ops = &arasan_clk_ops,
1217 };
1218 
1219 static const struct sdhci_pltfm_data sdhci_arasan_zynqmp_pdata = {
1220     .ops = &sdhci_arasan_ops,
1221     .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1222             SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1223             SDHCI_QUIRK2_STOP_WITH_TC,
1224 };
1225 
1226 static const struct sdhci_arasan_clk_ops zynqmp_clk_ops = {
1227     .sdcardclk_ops = &zynqmp_sdcardclk_ops,
1228     .sampleclk_ops = &zynqmp_sampleclk_ops,
1229 };
1230 
1231 static struct sdhci_arasan_of_data sdhci_arasan_zynqmp_data = {
1232     .pdata = &sdhci_arasan_zynqmp_pdata,
1233     .clk_ops = &zynqmp_clk_ops,
1234 };
1235 
1236 static const struct sdhci_arasan_clk_ops versal_clk_ops = {
1237     .sdcardclk_ops = &versal_sdcardclk_ops,
1238     .sampleclk_ops = &versal_sampleclk_ops,
1239 };
1240 
1241 static struct sdhci_arasan_of_data sdhci_arasan_versal_data = {
1242     .pdata = &sdhci_arasan_zynqmp_pdata,
1243     .clk_ops = &versal_clk_ops,
1244 };
1245 
1246 static struct sdhci_arasan_of_data intel_keembay_emmc_data = {
1247     .soc_ctl_map = &intel_keembay_soc_ctl_map,
1248     .pdata = &sdhci_keembay_emmc_pdata,
1249     .clk_ops = &arasan_clk_ops,
1250 };
1251 
1252 static struct sdhci_arasan_of_data intel_keembay_sd_data = {
1253     .soc_ctl_map = &intel_keembay_soc_ctl_map,
1254     .pdata = &sdhci_keembay_sd_pdata,
1255     .clk_ops = &arasan_clk_ops,
1256 };
1257 
1258 static struct sdhci_arasan_of_data intel_keembay_sdio_data = {
1259     .soc_ctl_map = &intel_keembay_soc_ctl_map,
1260     .pdata = &sdhci_keembay_sdio_pdata,
1261     .clk_ops = &arasan_clk_ops,
1262 };
1263 
1264 static const struct of_device_id sdhci_arasan_of_match[] = {
1265     /* SoC-specific compatible strings w/ soc_ctl_map */
1266     {
1267         .compatible = "rockchip,rk3399-sdhci-5.1",
1268         .data = &sdhci_arasan_rk3399_data,
1269     },
1270     {
1271         .compatible = "intel,lgm-sdhci-5.1-emmc",
1272         .data = &intel_lgm_emmc_data,
1273     },
1274     {
1275         .compatible = "intel,lgm-sdhci-5.1-sdxc",
1276         .data = &intel_lgm_sdxc_data,
1277     },
1278     {
1279         .compatible = "intel,keembay-sdhci-5.1-emmc",
1280         .data = &intel_keembay_emmc_data,
1281     },
1282     {
1283         .compatible = "intel,keembay-sdhci-5.1-sd",
1284         .data = &intel_keembay_sd_data,
1285     },
1286     {
1287         .compatible = "intel,keembay-sdhci-5.1-sdio",
1288         .data = &intel_keembay_sdio_data,
1289     },
1290     {
1291         .compatible = "intel,thunderbay-sdhci-5.1",
1292         .data = &sdhci_arasan_thunderbay_data,
1293     },
1294     /* Generic compatible below here */
1295     {
1296         .compatible = "arasan,sdhci-8.9a",
1297         .data = &sdhci_arasan_generic_data,
1298     },
1299     {
1300         .compatible = "arasan,sdhci-5.1",
1301         .data = &sdhci_arasan_generic_data,
1302     },
1303     {
1304         .compatible = "arasan,sdhci-4.9a",
1305         .data = &sdhci_arasan_generic_data,
1306     },
1307     {
1308         .compatible = "xlnx,zynqmp-8.9a",
1309         .data = &sdhci_arasan_zynqmp_data,
1310     },
1311     {
1312         .compatible = "xlnx,versal-8.9a",
1313         .data = &sdhci_arasan_versal_data,
1314     },
1315     { /* sentinel */ }
1316 };
1317 MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
1318 
1319 /**
1320  * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use
1321  *
1322  * @sdhci_arasan:   Our private data structure.
1323  * @clk_xin:        Pointer to the functional clock
1324  * @dev:        Pointer to our struct device.
1325  *
1326  * Some PHY devices need to know what the actual card clock is.  In order for
1327  * them to find out, we'll provide a clock through the common clock framework
1328  * for them to query.
1329  *
1330  * Return: 0 on success and error value on error
1331  */
1332 static int
1333 sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan,
1334                 struct clk *clk_xin,
1335                 struct device *dev)
1336 {
1337     struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1338     struct device_node *np = dev->of_node;
1339     struct clk_init_data sdcardclk_init;
1340     const char *parent_clk_name;
1341     int ret;
1342 
1343     ret = of_property_read_string_index(np, "clock-output-names", 0,
1344                         &sdcardclk_init.name);
1345     if (ret) {
1346         dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1347         return ret;
1348     }
1349 
1350     parent_clk_name = __clk_get_name(clk_xin);
1351     sdcardclk_init.parent_names = &parent_clk_name;
1352     sdcardclk_init.num_parents = 1;
1353     sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
1354     sdcardclk_init.ops = sdhci_arasan->clk_ops->sdcardclk_ops;
1355 
1356     clk_data->sdcardclk_hw.init = &sdcardclk_init;
1357     clk_data->sdcardclk =
1358         devm_clk_register(dev, &clk_data->sdcardclk_hw);
1359     if (IS_ERR(clk_data->sdcardclk))
1360         return PTR_ERR(clk_data->sdcardclk);
1361     clk_data->sdcardclk_hw.init = NULL;
1362 
1363     ret = of_clk_add_provider(np, of_clk_src_simple_get,
1364                   clk_data->sdcardclk);
1365     if (ret)
1366         dev_err(dev, "Failed to add sdcard clock provider\n");
1367 
1368     return ret;
1369 }
1370 
1371 /**
1372  * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use
1373  *
1374  * @sdhci_arasan:   Our private data structure.
1375  * @clk_xin:        Pointer to the functional clock
1376  * @dev:        Pointer to our struct device.
1377  *
1378  * Some PHY devices need to know what the actual card clock is.  In order for
1379  * them to find out, we'll provide a clock through the common clock framework
1380  * for them to query.
1381  *
1382  * Return: 0 on success and error value on error
1383  */
1384 static int
1385 sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan,
1386                 struct clk *clk_xin,
1387                 struct device *dev)
1388 {
1389     struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1390     struct device_node *np = dev->of_node;
1391     struct clk_init_data sampleclk_init;
1392     const char *parent_clk_name;
1393     int ret;
1394 
1395     ret = of_property_read_string_index(np, "clock-output-names", 1,
1396                         &sampleclk_init.name);
1397     if (ret) {
1398         dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1399         return ret;
1400     }
1401 
1402     parent_clk_name = __clk_get_name(clk_xin);
1403     sampleclk_init.parent_names = &parent_clk_name;
1404     sampleclk_init.num_parents = 1;
1405     sampleclk_init.flags = CLK_GET_RATE_NOCACHE;
1406     sampleclk_init.ops = sdhci_arasan->clk_ops->sampleclk_ops;
1407 
1408     clk_data->sampleclk_hw.init = &sampleclk_init;
1409     clk_data->sampleclk =
1410         devm_clk_register(dev, &clk_data->sampleclk_hw);
1411     if (IS_ERR(clk_data->sampleclk))
1412         return PTR_ERR(clk_data->sampleclk);
1413     clk_data->sampleclk_hw.init = NULL;
1414 
1415     ret = of_clk_add_provider(np, of_clk_src_simple_get,
1416                   clk_data->sampleclk);
1417     if (ret)
1418         dev_err(dev, "Failed to add sample clock provider\n");
1419 
1420     return ret;
1421 }
1422 
1423 /**
1424  * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
1425  *
1426  * @dev:        Pointer to our struct device.
1427  *
1428  * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
1429  * returned success.
1430  */
1431 static void sdhci_arasan_unregister_sdclk(struct device *dev)
1432 {
1433     struct device_node *np = dev->of_node;
1434 
1435     if (!of_find_property(np, "#clock-cells", NULL))
1436         return;
1437 
1438     of_clk_del_provider(dev->of_node);
1439 }
1440 
1441 /**
1442  * sdhci_arasan_update_support64b - Set SUPPORT_64B (64-bit System Bus Support)
1443  * @host:       The sdhci_host
1444  * @value:      The value to write
1445  *
1446  * This should be set based on the System Address Bus.
1447  * 0: the Core supports only 32-bit System Address Bus.
1448  * 1: the Core supports 64-bit System Address Bus.
1449  *
1450  * NOTE:
1451  * For Keem Bay, it is required to clear this bit. Its default value is 1'b1.
1452  * Keem Bay does not support 64-bit access.
1453  */
1454 static void sdhci_arasan_update_support64b(struct sdhci_host *host, u32 value)
1455 {
1456     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1457     struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1458     const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
1459 
1460     /* Having a map is optional */
1461     soc_ctl_map = sdhci_arasan->soc_ctl_map;
1462     if (!soc_ctl_map)
1463         return;
1464 
1465     /* If we have a map, we expect to have a syscon */
1466     if (!sdhci_arasan->soc_ctl_base) {
1467         pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1468             mmc_hostname(host->mmc));
1469         return;
1470     }
1471 
1472     sdhci_arasan_syscon_write(host, &soc_ctl_map->support64b, value);
1473 }
1474 
1475 /**
1476  * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use
1477  *
1478  * @sdhci_arasan:   Our private data structure.
1479  * @clk_xin:        Pointer to the functional clock
1480  * @dev:        Pointer to our struct device.
1481  *
1482  * Some PHY devices need to know what the actual card clock is.  In order for
1483  * them to find out, we'll provide a clock through the common clock framework
1484  * for them to query.
1485  *
1486  * Note: without seriously re-architecting SDHCI's clock code and testing on
1487  * all platforms, there's no way to create a totally beautiful clock here
1488  * with all clock ops implemented.  Instead, we'll just create a clock that can
1489  * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
1490  * framework that we're doing things behind its back.  This should be sufficient
1491  * to create nice clean device tree bindings and later (if needed) we can try
1492  * re-architecting SDHCI if we see some benefit to it.
1493  *
1494  * Return: 0 on success and error value on error
1495  */
1496 static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
1497                        struct clk *clk_xin,
1498                        struct device *dev)
1499 {
1500     struct device_node *np = dev->of_node;
1501     u32 num_clks = 0;
1502     int ret;
1503 
1504     /* Providing a clock to the PHY is optional; no error if missing */
1505     if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0)
1506         return 0;
1507 
1508     ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev);
1509     if (ret)
1510         return ret;
1511 
1512     if (num_clks) {
1513         ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin,
1514                               dev);
1515         if (ret) {
1516             sdhci_arasan_unregister_sdclk(dev);
1517             return ret;
1518         }
1519     }
1520 
1521     return 0;
1522 }
1523 
1524 static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
1525 {
1526     struct sdhci_host *host = sdhci_arasan->host;
1527     struct cqhci_host *cq_host;
1528     bool dma64;
1529     int ret;
1530 
1531     if (!sdhci_arasan->has_cqe)
1532         return sdhci_add_host(host);
1533 
1534     ret = sdhci_setup_host(host);
1535     if (ret)
1536         return ret;
1537 
1538     cq_host = devm_kzalloc(host->mmc->parent,
1539                    sizeof(*cq_host), GFP_KERNEL);
1540     if (!cq_host) {
1541         ret = -ENOMEM;
1542         goto cleanup;
1543     }
1544 
1545     cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
1546     cq_host->ops = &sdhci_arasan_cqhci_ops;
1547 
1548     dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1549     if (dma64)
1550         cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1551 
1552     ret = cqhci_init(cq_host, host->mmc, dma64);
1553     if (ret)
1554         goto cleanup;
1555 
1556     ret = __sdhci_add_host(host);
1557     if (ret)
1558         goto cleanup;
1559 
1560     return 0;
1561 
1562 cleanup:
1563     sdhci_cleanup_host(host);
1564     return ret;
1565 }
1566 
1567 static int sdhci_arasan_probe(struct platform_device *pdev)
1568 {
1569     int ret;
1570     struct device_node *node;
1571     struct clk *clk_xin;
1572     struct sdhci_host *host;
1573     struct sdhci_pltfm_host *pltfm_host;
1574     struct device *dev = &pdev->dev;
1575     struct device_node *np = dev->of_node;
1576     struct sdhci_arasan_data *sdhci_arasan;
1577     const struct sdhci_arasan_of_data *data;
1578 
1579     data = of_device_get_match_data(dev);
1580     if (!data)
1581         return -EINVAL;
1582 
1583     host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan));
1584 
1585     if (IS_ERR(host))
1586         return PTR_ERR(host);
1587 
1588     pltfm_host = sdhci_priv(host);
1589     sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1590     sdhci_arasan->host = host;
1591 
1592     sdhci_arasan->soc_ctl_map = data->soc_ctl_map;
1593     sdhci_arasan->clk_ops = data->clk_ops;
1594 
1595     node = of_parse_phandle(np, "arasan,soc-ctl-syscon", 0);
1596     if (node) {
1597         sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
1598         of_node_put(node);
1599 
1600         if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
1601             ret = dev_err_probe(dev,
1602                         PTR_ERR(sdhci_arasan->soc_ctl_base),
1603                         "Can't get syscon\n");
1604             goto err_pltfm_free;
1605         }
1606     }
1607 
1608     sdhci_get_of_property(pdev);
1609 
1610     sdhci_arasan->clk_ahb = devm_clk_get(dev, "clk_ahb");
1611     if (IS_ERR(sdhci_arasan->clk_ahb)) {
1612         ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->clk_ahb),
1613                     "clk_ahb clock not found.\n");
1614         goto err_pltfm_free;
1615     }
1616 
1617     clk_xin = devm_clk_get(dev, "clk_xin");
1618     if (IS_ERR(clk_xin)) {
1619         ret = dev_err_probe(dev, PTR_ERR(clk_xin), "clk_xin clock not found.\n");
1620         goto err_pltfm_free;
1621     }
1622 
1623     ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
1624     if (ret) {
1625         dev_err(dev, "Unable to enable AHB clock.\n");
1626         goto err_pltfm_free;
1627     }
1628 
1629     /* If clock-frequency property is set, use the provided value */
1630     if (pltfm_host->clock &&
1631         pltfm_host->clock != clk_get_rate(clk_xin)) {
1632         ret = clk_set_rate(clk_xin, pltfm_host->clock);
1633         if (ret) {
1634             dev_err(&pdev->dev, "Failed to set SD clock rate\n");
1635             goto clk_dis_ahb;
1636         }
1637     }
1638 
1639     ret = clk_prepare_enable(clk_xin);
1640     if (ret) {
1641         dev_err(dev, "Unable to enable SD clock.\n");
1642         goto clk_dis_ahb;
1643     }
1644 
1645     if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
1646         sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
1647 
1648     if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
1649         sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
1650 
1651     pltfm_host->clk = clk_xin;
1652 
1653     if (of_device_is_compatible(np, "rockchip,rk3399-sdhci-5.1"))
1654         sdhci_arasan_update_clockmultiplier(host, 0x0);
1655 
1656     if (of_device_is_compatible(np, "intel,keembay-sdhci-5.1-emmc") ||
1657         of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sd") ||
1658         of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sdio") ||
1659         of_device_is_compatible(np, "intel,thunderbay-sdhci-5.1")) {
1660         sdhci_arasan_update_clockmultiplier(host, 0x0);
1661         sdhci_arasan_update_support64b(host, 0x0);
1662 
1663         host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1664     }
1665 
1666     sdhci_arasan_update_baseclkfreq(host);
1667 
1668     ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, dev);
1669     if (ret)
1670         goto clk_disable_all;
1671 
1672     if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
1673         host->mmc_host_ops.execute_tuning =
1674             arasan_zynqmp_execute_tuning;
1675 
1676         sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN;
1677         host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
1678     }
1679 
1680     arasan_dt_parse_clk_phases(dev, &sdhci_arasan->clk_data);
1681 
1682     ret = mmc_of_parse(host->mmc);
1683     if (ret) {
1684         ret = dev_err_probe(dev, ret, "parsing dt failed.\n");
1685         goto unreg_clk;
1686     }
1687 
1688     sdhci_arasan->phy = ERR_PTR(-ENODEV);
1689     if (of_device_is_compatible(np, "arasan,sdhci-5.1")) {
1690         sdhci_arasan->phy = devm_phy_get(dev, "phy_arasan");
1691         if (IS_ERR(sdhci_arasan->phy)) {
1692             ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->phy),
1693                         "No phy for arasan,sdhci-5.1.\n");
1694             goto unreg_clk;
1695         }
1696 
1697         ret = phy_init(sdhci_arasan->phy);
1698         if (ret < 0) {
1699             dev_err(dev, "phy_init err.\n");
1700             goto unreg_clk;
1701         }
1702 
1703         host->mmc_host_ops.hs400_enhanced_strobe =
1704                     sdhci_arasan_hs400_enhanced_strobe;
1705         host->mmc_host_ops.start_signal_voltage_switch =
1706                     sdhci_arasan_voltage_switch;
1707         sdhci_arasan->has_cqe = true;
1708         host->mmc->caps2 |= MMC_CAP2_CQE;
1709 
1710         if (!of_property_read_bool(np, "disable-cqe-dcmd"))
1711             host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
1712     }
1713 
1714     ret = sdhci_arasan_add_host(sdhci_arasan);
1715     if (ret)
1716         goto err_add_host;
1717 
1718     return 0;
1719 
1720 err_add_host:
1721     if (!IS_ERR(sdhci_arasan->phy))
1722         phy_exit(sdhci_arasan->phy);
1723 unreg_clk:
1724     sdhci_arasan_unregister_sdclk(dev);
1725 clk_disable_all:
1726     clk_disable_unprepare(clk_xin);
1727 clk_dis_ahb:
1728     clk_disable_unprepare(sdhci_arasan->clk_ahb);
1729 err_pltfm_free:
1730     sdhci_pltfm_free(pdev);
1731     return ret;
1732 }
1733 
1734 static int sdhci_arasan_remove(struct platform_device *pdev)
1735 {
1736     struct sdhci_host *host = platform_get_drvdata(pdev);
1737     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1738     struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1739     struct clk *clk_ahb = sdhci_arasan->clk_ahb;
1740 
1741     if (!IS_ERR(sdhci_arasan->phy)) {
1742         if (sdhci_arasan->is_phy_on)
1743             phy_power_off(sdhci_arasan->phy);
1744         phy_exit(sdhci_arasan->phy);
1745     }
1746 
1747     sdhci_arasan_unregister_sdclk(&pdev->dev);
1748 
1749     sdhci_pltfm_unregister(pdev);
1750 
1751     clk_disable_unprepare(clk_ahb);
1752 
1753     return 0;
1754 }
1755 
1756 static struct platform_driver sdhci_arasan_driver = {
1757     .driver = {
1758         .name = "sdhci-arasan",
1759         .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1760         .of_match_table = sdhci_arasan_of_match,
1761         .pm = &sdhci_arasan_dev_pm_ops,
1762     },
1763     .probe = sdhci_arasan_probe,
1764     .remove = sdhci_arasan_remove,
1765 };
1766 
1767 module_platform_driver(sdhci_arasan_driver);
1768 
1769 MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
1770 MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
1771 MODULE_LICENSE("GPL");