linux/drivers/mmc/host/sdhci-of-arasan.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Arasan Secure Digital Host Controller Interface.
   4 * Copyright (C) 2011 - 2012 Michal Simek <monstr@monstr.eu>
   5 * Copyright (c) 2012 Wind River Systems, Inc.
   6 * Copyright (C) 2013 Pengutronix e.K.
   7 * Copyright (C) 2013 Xilinx Inc.
   8 *
   9 * Based on sdhci-of-esdhc.c
  10 *
  11 * Copyright (c) 2007 Freescale Semiconductor, Inc.
  12 * Copyright (c) 2009 MontaVista Software, Inc.
  13 *
  14 * Authors: Xiaobo Xie <X.Xie@freescale.com>
  15 *          Anton Vorontsov <avorontsov@ru.mvista.com>
  16 */
  17
  18#include <linux/clk-provider.h>
  19#include <linux/mfd/syscon.h>
  20#include <linux/module.h>
  21#include <linux/of_device.h>
  22#include <linux/phy/phy.h>
  23#include <linux/regmap.h>
  24#include <linux/reset.h>
  25#include <linux/of.h>
  26#include <linux/firmware/xlnx-zynqmp.h>
  27
  28#include "cqhci.h"
  29#include "sdhci-pltfm.h"
  30
  31#define SDHCI_ARASAN_VENDOR_REGISTER    0x78
  32
  33#define SDHCI_ARASAN_ITAPDLY_REGISTER   0xF0F8
  34#define SDHCI_ARASAN_ITAPDLY_SEL_MASK   0xFF
  35
  36#define SDHCI_ARASAN_OTAPDLY_REGISTER   0xF0FC
  37#define SDHCI_ARASAN_OTAPDLY_SEL_MASK   0x3F
  38
  39#define SDHCI_ARASAN_CQE_BASE_ADDR      0x200
  40#define VENDOR_ENHANCED_STROBE          BIT(0)
  41
  42#define PHY_CLK_TOO_SLOW_HZ             400000
  43
  44#define SDHCI_ITAPDLY_CHGWIN            0x200
  45#define SDHCI_ITAPDLY_ENABLE            0x100
  46#define SDHCI_OTAPDLY_ENABLE            0x40
  47
  48/* Default settings for ZynqMP Clock Phases */
  49#define ZYNQMP_ICLK_PHASE {0, 63, 63, 0, 63,  0,   0, 183, 54,  0, 0}
  50#define ZYNQMP_OCLK_PHASE {0, 72, 60, 0, 60, 72, 135, 48, 72, 135, 0}
  51
  52#define VERSAL_ICLK_PHASE {0, 132, 132, 0, 132, 0, 0, 162, 90, 0, 0}
  53#define VERSAL_OCLK_PHASE {0,  60, 48, 0, 48, 72, 90, 36, 60, 90, 0}
  54
  55/*
  56 * On some SoCs the syscon area has a feature where the upper 16-bits of
  57 * each 32-bit register act as a write mask for the lower 16-bits.  This allows
  58 * atomic updates of the register without locking.  This macro is used on SoCs
  59 * that have that feature.
  60 */
  61#define HIWORD_UPDATE(val, mask, shift) \
  62                ((val) << (shift) | (mask) << ((shift) + 16))
  63
  64/**
  65 * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
  66 *
  67 * @reg:        Offset within the syscon of the register containing this field
  68 * @width:      Number of bits for this field
  69 * @shift:      Bit offset within @reg of this field (or -1 if not avail)
  70 */
  71struct sdhci_arasan_soc_ctl_field {
  72        u32 reg;
  73        u16 width;
  74        s16 shift;
  75};
  76
  77/**
  78 * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
  79 *
  80 * @baseclkfreq:        Where to find corecfg_baseclkfreq
  81 * @clockmultiplier:    Where to find corecfg_clockmultiplier
  82 * @support64b:         Where to find SUPPORT64B bit
  83 * @hiword_update:      If true, use HIWORD_UPDATE to access the syscon
  84 *
  85 * It's up to the licensee of the Arsan IP block to make these available
  86 * somewhere if needed.  Presumably these will be scattered somewhere that's
  87 * accessible via the syscon API.
  88 */
  89struct sdhci_arasan_soc_ctl_map {
  90        struct sdhci_arasan_soc_ctl_field       baseclkfreq;
  91        struct sdhci_arasan_soc_ctl_field       clockmultiplier;
  92        struct sdhci_arasan_soc_ctl_field       support64b;
  93        bool                                    hiword_update;
  94};
  95
  96/**
  97 * struct sdhci_arasan_clk_ops - Clock Operations for Arasan SD controller
  98 *
  99 * @sdcardclk_ops:      The output clock related operations
 100 * @sampleclk_ops:      The sample clock related operations
 101 */
 102struct sdhci_arasan_clk_ops {
 103        const struct clk_ops *sdcardclk_ops;
 104        const struct clk_ops *sampleclk_ops;
 105};
 106
 107/**
 108 * struct sdhci_arasan_clk_data - Arasan Controller Clock Data.
 109 *
 110 * @sdcardclk_hw:       Struct for the clock we might provide to a PHY.
 111 * @sdcardclk:          Pointer to normal 'struct clock' for sdcardclk_hw.
 112 * @sampleclk_hw:       Struct for the clock we might provide to a PHY.
 113 * @sampleclk:          Pointer to normal 'struct clock' for sampleclk_hw.
 114 * @clk_phase_in:       Array of Input Clock Phase Delays for all speed modes
 115 * @clk_phase_out:      Array of Output Clock Phase Delays for all speed modes
 116 * @set_clk_delays:     Function pointer for setting Clock Delays
 117 * @clk_of_data:        Platform specific runtime clock data storage pointer
 118 */
 119struct sdhci_arasan_clk_data {
 120        struct clk_hw   sdcardclk_hw;
 121        struct clk      *sdcardclk;
 122        struct clk_hw   sampleclk_hw;
 123        struct clk      *sampleclk;
 124        int             clk_phase_in[MMC_TIMING_MMC_HS400 + 1];
 125        int             clk_phase_out[MMC_TIMING_MMC_HS400 + 1];
 126        void            (*set_clk_delays)(struct sdhci_host *host);
 127        void            *clk_of_data;
 128};
 129
 130/**
 131 * struct sdhci_arasan_data - Arasan Controller Data
 132 *
 133 * @host:               Pointer to the main SDHCI host structure.
 134 * @clk_ahb:            Pointer to the AHB clock
 135 * @phy:                Pointer to the generic phy
 136 * @is_phy_on:          True if the PHY is on; false if not.
 137 * @has_cqe:            True if controller has command queuing engine.
 138 * @clk_data:           Struct for the Arasan Controller Clock Data.
 139 * @clk_ops:            Struct for the Arasan Controller Clock Operations.
 140 * @soc_ctl_base:       Pointer to regmap for syscon for soc_ctl registers.
 141 * @soc_ctl_map:        Map to get offsets into soc_ctl registers.
 142 * @quirks:             Arasan deviations from spec.
 143 */
 144struct sdhci_arasan_data {
 145        struct sdhci_host *host;
 146        struct clk      *clk_ahb;
 147        struct phy      *phy;
 148        bool            is_phy_on;
 149
 150        bool            has_cqe;
 151        struct sdhci_arasan_clk_data clk_data;
 152        const struct sdhci_arasan_clk_ops *clk_ops;
 153
 154        struct regmap   *soc_ctl_base;
 155        const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
 156        unsigned int    quirks;
 157
 158/* Controller does not have CD wired and will not function normally without */
 159#define SDHCI_ARASAN_QUIRK_FORCE_CDTEST BIT(0)
 160/* Controller immediately reports SDHCI_CLOCK_INT_STABLE after enabling the
 161 * internal clock even when the clock isn't stable */
 162#define SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE BIT(1)
 163/*
 164 * Some of the Arasan variations might not have timing requirements
 165 * met at 25MHz for Default Speed mode, those controllers work at
 166 * 19MHz instead
 167 */
 168#define SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN BIT(2)
 169};
 170
 171struct sdhci_arasan_of_data {
 172        const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
 173        const struct sdhci_pltfm_data *pdata;
 174        const struct sdhci_arasan_clk_ops *clk_ops;
 175};
 176
 177static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
 178        .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
 179        .clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
 180        .hiword_update = true,
 181};
 182
 183static const struct sdhci_arasan_soc_ctl_map intel_lgm_emmc_soc_ctl_map = {
 184        .baseclkfreq = { .reg = 0xa0, .width = 8, .shift = 2 },
 185        .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
 186        .hiword_update = false,
 187};
 188
 189static const struct sdhci_arasan_soc_ctl_map intel_lgm_sdxc_soc_ctl_map = {
 190        .baseclkfreq = { .reg = 0x80, .width = 8, .shift = 2 },
 191        .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
 192        .hiword_update = false,
 193};
 194
 195static const struct sdhci_arasan_soc_ctl_map intel_keembay_soc_ctl_map = {
 196        .baseclkfreq = { .reg = 0x0, .width = 8, .shift = 14 },
 197        .clockmultiplier = { .reg = 0x4, .width = 8, .shift = 14 },
 198        .support64b = { .reg = 0x4, .width = 1, .shift = 24 },
 199        .hiword_update = false,
 200};
 201
 202/**
 203 * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
 204 *
 205 * @host:       The sdhci_host
 206 * @fld:        The field to write to
 207 * @val:        The value to write
 208 *
 209 * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
 210 * Note that if a field is specified as not available (shift < 0) then
 211 * this function will silently return an error code.  It will be noisy
 212 * and print errors for any other (unexpected) errors.
 213 *
 214 * Return: 0 on success and error value on error
 215 */
 216static int sdhci_arasan_syscon_write(struct sdhci_host *host,
 217                                   const struct sdhci_arasan_soc_ctl_field *fld,
 218                                   u32 val)
 219{
 220        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 221        struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
 222        struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
 223        u32 reg = fld->reg;
 224        u16 width = fld->width;
 225        s16 shift = fld->shift;
 226        int ret;
 227
 228        /*
 229         * Silently return errors for shift < 0 so caller doesn't have
 230         * to check for fields which are optional.  For fields that
 231         * are required then caller needs to do something special
 232         * anyway.
 233         */
 234        if (shift < 0)
 235                return -EINVAL;
 236
 237        if (sdhci_arasan->soc_ctl_map->hiword_update)
 238                ret = regmap_write(soc_ctl_base, reg,
 239                                   HIWORD_UPDATE(val, GENMASK(width, 0),
 240                                                 shift));
 241        else
 242                ret = regmap_update_bits(soc_ctl_base, reg,
 243                                         GENMASK(shift + width, shift),
 244                                         val << shift);
 245
 246        /* Yell about (unexpected) regmap errors */
 247        if (ret)
 248                pr_warn("%s: Regmap write fail: %d\n",
 249                         mmc_hostname(host->mmc), ret);
 250
 251        return ret;
 252}
 253
 254static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
 255{
 256        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 257        struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
 258        struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
 259        bool ctrl_phy = false;
 260
 261        if (!IS_ERR(sdhci_arasan->phy)) {
 262                if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
 263                        /*
 264                         * If PHY off, set clock to max speed and power PHY on.
 265                         *
 266                         * Although PHY docs apparently suggest power cycling
 267                         * when changing the clock the PHY doesn't like to be
 268                         * powered on while at low speeds like those used in ID
 269                         * mode.  Even worse is powering the PHY on while the
 270                         * clock is off.
 271                         *
 272                         * To workaround the PHY limitations, the best we can
 273                         * do is to power it on at a faster speed and then slam
 274                         * through low speeds without power cycling.
 275                         */
 276                        sdhci_set_clock(host, host->max_clk);
 277                        if (phy_power_on(sdhci_arasan->phy)) {
 278                                pr_err("%s: Cannot power on phy.\n",
 279                                       mmc_hostname(host->mmc));
 280                                return;
 281                        }
 282
 283                        sdhci_arasan->is_phy_on = true;
 284
 285                        /*
 286                         * We'll now fall through to the below case with
 287                         * ctrl_phy = false (so we won't turn off/on).  The
 288                         * sdhci_set_clock() will set the real clock.
 289                         */
 290                } else if (clock > PHY_CLK_TOO_SLOW_HZ) {
 291                        /*
 292                         * At higher clock speeds the PHY is fine being power
 293                         * cycled and docs say you _should_ power cycle when
 294                         * changing clock speeds.
 295                         */
 296                        ctrl_phy = true;
 297                }
 298        }
 299
 300        if (ctrl_phy && sdhci_arasan->is_phy_on) {
 301                phy_power_off(sdhci_arasan->phy);
 302                sdhci_arasan->is_phy_on = false;
 303        }
 304
 305        if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN) {
 306                /*
 307                 * Some of the Arasan variations might not have timing
 308                 * requirements met at 25MHz for Default Speed mode,
 309                 * those controllers work at 19MHz instead.
 310                 */
 311                if (clock == DEFAULT_SPEED_MAX_DTR)
 312                        clock = (DEFAULT_SPEED_MAX_DTR * 19) / 25;
 313        }
 314
 315        /* Set the Input and Output Clock Phase Delays */
 316        if (clk_data->set_clk_delays)
 317                clk_data->set_clk_delays(host);
 318
 319        sdhci_set_clock(host, clock);
 320
 321        if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE)
 322                /*
 323                 * Some controllers immediately report SDHCI_CLOCK_INT_STABLE
 324                 * after enabling the clock even though the clock is not
 325                 * stable. Trying to use a clock without waiting here results
 326                 * in EILSEQ while detecting some older/slower cards. The
 327                 * chosen delay is the maximum delay from sdhci_set_clock.
 328                 */
 329                msleep(20);
 330
 331        if (ctrl_phy) {
 332                if (phy_power_on(sdhci_arasan->phy)) {
 333                        pr_err("%s: Cannot power on phy.\n",
 334                               mmc_hostname(host->mmc));
 335                        return;
 336                }
 337
 338                sdhci_arasan->is_phy_on = true;
 339        }
 340}
 341
 342static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
 343                                        struct mmc_ios *ios)
 344{
 345        u32 vendor;
 346        struct sdhci_host *host = mmc_priv(mmc);
 347
 348        vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
 349        if (ios->enhanced_strobe)
 350                vendor |= VENDOR_ENHANCED_STROBE;
 351        else
 352                vendor &= ~VENDOR_ENHANCED_STROBE;
 353
 354        sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
 355}
 356
 357static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
 358{
 359        u8 ctrl;
 360        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 361        struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
 362
 363        sdhci_reset(host, mask);
 364
 365        if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
 366                ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
 367                ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
 368                sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
 369        }
 370}
 371
 372static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
 373                                       struct mmc_ios *ios)
 374{
 375        switch (ios->signal_voltage) {
 376        case MMC_SIGNAL_VOLTAGE_180:
 377                /*
 378                 * Plese don't switch to 1V8 as arasan,5.1 doesn't
 379                 * actually refer to this setting to indicate the
 380                 * signal voltage and the state machine will be broken
 381                 * actually if we force to enable 1V8. That's something
 382                 * like broken quirk but we could work around here.
 383                 */
 384                return 0;
 385        case MMC_SIGNAL_VOLTAGE_330:
 386        case MMC_SIGNAL_VOLTAGE_120:
 387                /* We don't support 3V3 and 1V2 */
 388                break;
 389        }
 390
 391        return -EINVAL;
 392}
 393
 394static const struct sdhci_ops sdhci_arasan_ops = {
 395        .set_clock = sdhci_arasan_set_clock,
 396        .get_max_clock = sdhci_pltfm_clk_get_max_clock,
 397        .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
 398        .set_bus_width = sdhci_set_bus_width,
 399        .reset = sdhci_arasan_reset,
 400        .set_uhs_signaling = sdhci_set_uhs_signaling,
 401        .set_power = sdhci_set_power_and_bus_voltage,
 402};
 403
 404static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
 405{
 406        int cmd_error = 0;
 407        int data_error = 0;
 408
 409        if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
 410                return intmask;
 411
 412        cqhci_irq(host->mmc, intmask, cmd_error, data_error);
 413
 414        return 0;
 415}
 416
 417static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
 418{
 419        sdhci_dumpregs(mmc_priv(mmc));
 420}
 421
 422static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
 423{
 424        struct sdhci_host *host = mmc_priv(mmc);
 425        u32 reg;
 426
 427        reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
 428        while (reg & SDHCI_DATA_AVAILABLE) {
 429                sdhci_readl(host, SDHCI_BUFFER);
 430                reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
 431        }
 432
 433        sdhci_cqe_enable(mmc);
 434}
 435
 436static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
 437        .enable         = sdhci_arasan_cqe_enable,
 438        .disable        = sdhci_cqe_disable,
 439        .dumpregs       = sdhci_arasan_dumpregs,
 440};
 441
 442static const struct sdhci_ops sdhci_arasan_cqe_ops = {
 443        .set_clock = sdhci_arasan_set_clock,
 444        .get_max_clock = sdhci_pltfm_clk_get_max_clock,
 445        .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
 446        .set_bus_width = sdhci_set_bus_width,
 447        .reset = sdhci_arasan_reset,
 448        .set_uhs_signaling = sdhci_set_uhs_signaling,
 449        .set_power = sdhci_set_power_and_bus_voltage,
 450        .irq = sdhci_arasan_cqhci_irq,
 451};
 452
 453static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
 454        .ops = &sdhci_arasan_cqe_ops,
 455        .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
 456        .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
 457                        SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
 458};
 459
 460#ifdef CONFIG_PM_SLEEP
 461/**
 462 * sdhci_arasan_suspend - Suspend method for the driver
 463 * @dev:        Address of the device structure
 464 *
 465 * Put the device in a low power state.
 466 *
 467 * Return: 0 on success and error value on error
 468 */
 469static int sdhci_arasan_suspend(struct device *dev)
 470{
 471        struct sdhci_host *host = dev_get_drvdata(dev);
 472        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 473        struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
 474        int ret;
 475
 476        if (host->tuning_mode != SDHCI_TUNING_MODE_3)
 477                mmc_retune_needed(host->mmc);
 478
 479        if (sdhci_arasan->has_cqe) {
 480                ret = cqhci_suspend(host->mmc);
 481                if (ret)
 482                        return ret;
 483        }
 484
 485        ret = sdhci_suspend_host(host);
 486        if (ret)
 487                return ret;
 488
 489        if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
 490                ret = phy_power_off(sdhci_arasan->phy);
 491                if (ret) {
 492                        dev_err(dev, "Cannot power off phy.\n");
 493                        if (sdhci_resume_host(host))
 494                                dev_err(dev, "Cannot resume host.\n");
 495
 496                        return ret;
 497                }
 498                sdhci_arasan->is_phy_on = false;
 499        }
 500
 501        clk_disable(pltfm_host->clk);
 502        clk_disable(sdhci_arasan->clk_ahb);
 503
 504        return 0;
 505}
 506
 507/**
 508 * sdhci_arasan_resume - Resume method for the driver
 509 * @dev:        Address of the device structure
 510 *
 511 * Resume operation after suspend
 512 *
 513 * Return: 0 on success and error value on error
 514 */
 515static int sdhci_arasan_resume(struct device *dev)
 516{
 517        struct sdhci_host *host = dev_get_drvdata(dev);
 518        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 519        struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
 520        int ret;
 521
 522        ret = clk_enable(sdhci_arasan->clk_ahb);
 523        if (ret) {
 524                dev_err(dev, "Cannot enable AHB clock.\n");
 525                return ret;
 526        }
 527
 528        ret = clk_enable(pltfm_host->clk);
 529        if (ret) {
 530                dev_err(dev, "Cannot enable SD clock.\n");
 531                return ret;
 532        }
 533
 534        if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
 535                ret = phy_power_on(sdhci_arasan->phy);
 536                if (ret) {
 537                        dev_err(dev, "Cannot power on phy.\n");
 538                        return ret;
 539                }
 540                sdhci_arasan->is_phy_on = true;
 541        }
 542
 543        ret = sdhci_resume_host(host);
 544        if (ret) {
 545                dev_err(dev, "Cannot resume host.\n");
 546                return ret;
 547        }
 548
 549        if (sdhci_arasan->has_cqe)
 550                return cqhci_resume(host->mmc);
 551
 552        return 0;
 553}
 554#endif /* ! CONFIG_PM_SLEEP */
 555
 556static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
 557                         sdhci_arasan_resume);
 558
 559/**
 560 * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
 561 *
 562 * @hw:                 Pointer to the hardware clock structure.
 563 * @parent_rate:                The parent rate (should be rate of clk_xin).
 564 *
 565 * Return the current actual rate of the SD card clock.  This can be used
 566 * to communicate with out PHY.
 567 *
 568 * Return: The card clock rate.
 569 */
 570static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
 571                                                      unsigned long parent_rate)
 572{
 573        struct sdhci_arasan_clk_data *clk_data =
 574                container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
 575        struct sdhci_arasan_data *sdhci_arasan =
 576                container_of(clk_data, struct sdhci_arasan_data, clk_data);
 577        struct sdhci_host *host = sdhci_arasan->host;
 578
 579        return host->mmc->actual_clock;
 580}
 581
 582static const struct clk_ops arasan_sdcardclk_ops = {
 583        .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
 584};
 585
 586/**
 587 * sdhci_arasan_sampleclk_recalc_rate - Return the sampling clock rate
 588 *
 589 * @hw:                 Pointer to the hardware clock structure.
 590 * @parent_rate:                The parent rate (should be rate of clk_xin).
 591 *
 592 * Return the current actual rate of the sampling clock.  This can be used
 593 * to communicate with out PHY.
 594 *
 595 * Return: The sample clock rate.
 596 */
 597static unsigned long sdhci_arasan_sampleclk_recalc_rate(struct clk_hw *hw,
 598                                                      unsigned long parent_rate)
 599{
 600        struct sdhci_arasan_clk_data *clk_data =
 601                container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
 602        struct sdhci_arasan_data *sdhci_arasan =
 603                container_of(clk_data, struct sdhci_arasan_data, clk_data);
 604        struct sdhci_host *host = sdhci_arasan->host;
 605
 606        return host->mmc->actual_clock;
 607}
 608
 609static const struct clk_ops arasan_sampleclk_ops = {
 610        .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
 611};
 612
 613/**
 614 * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
 615 *
 616 * @hw:                 Pointer to the hardware clock structure.
 617 * @degrees:            The clock phase shift between 0 - 359.
 618 *
 619 * Set the SD Output Clock Tap Delays for Output path
 620 *
 621 * Return: 0 on success and error value on error
 622 */
 623static int sdhci_zynqmp_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
 624{
 625        struct sdhci_arasan_clk_data *clk_data =
 626                container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
 627        struct sdhci_arasan_data *sdhci_arasan =
 628                container_of(clk_data, struct sdhci_arasan_data, clk_data);
 629        struct sdhci_host *host = sdhci_arasan->host;
 630        const char *clk_name = clk_hw_get_name(hw);
 631        u32 node_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : NODE_SD_1;
 632        u8 tap_delay, tap_max = 0;
 633        int ret;
 634
 635        /* This is applicable for SDHCI_SPEC_300 and above */
 636        if (host->version < SDHCI_SPEC_300)
 637                return 0;
 638
 639        switch (host->timing) {
 640        case MMC_TIMING_MMC_HS:
 641        case MMC_TIMING_SD_HS:
 642        case MMC_TIMING_UHS_SDR25:
 643        case MMC_TIMING_UHS_DDR50:
 644        case MMC_TIMING_MMC_DDR52:
 645                /* For 50MHz clock, 30 Taps are available */
 646                tap_max = 30;
 647                break;
 648        case MMC_TIMING_UHS_SDR50:
 649                /* For 100MHz clock, 15 Taps are available */
 650                tap_max = 15;
 651                break;
 652        case MMC_TIMING_UHS_SDR104:
 653        case MMC_TIMING_MMC_HS200:
 654                /* For 200MHz clock, 8 Taps are available */
 655                tap_max = 8;
 656                break;
 657        default:
 658                break;
 659        }
 660
 661        tap_delay = (degrees * tap_max) / 360;
 662
 663        /* Set the Clock Phase */
 664        ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_OUTPUT, tap_delay);
 665        if (ret)
 666                pr_err("Error setting Output Tap Delay\n");
 667
 668        /* Release DLL Reset */
 669        zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_RELEASE);
 670
 671        return ret;
 672}
 673
 674static const struct clk_ops zynqmp_sdcardclk_ops = {
 675        .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
 676        .set_phase = sdhci_zynqmp_sdcardclk_set_phase,
 677};
 678
 679/**
 680 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
 681 *
 682 * @hw:                 Pointer to the hardware clock structure.
 683 * @degrees:            The clock phase shift between 0 - 359.
 684 *
 685 * Set the SD Input Clock Tap Delays for Input path
 686 *
 687 * Return: 0 on success and error value on error
 688 */
 689static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees)
 690{
 691        struct sdhci_arasan_clk_data *clk_data =
 692                container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
 693        struct sdhci_arasan_data *sdhci_arasan =
 694                container_of(clk_data, struct sdhci_arasan_data, clk_data);
 695        struct sdhci_host *host = sdhci_arasan->host;
 696        const char *clk_name = clk_hw_get_name(hw);
 697        u32 node_id = !strcmp(clk_name, "clk_in_sd0") ? NODE_SD_0 : NODE_SD_1;
 698        u8 tap_delay, tap_max = 0;
 699        int ret;
 700
 701        /* This is applicable for SDHCI_SPEC_300 and above */
 702        if (host->version < SDHCI_SPEC_300)
 703                return 0;
 704
 705        /* Assert DLL Reset */
 706        zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_ASSERT);
 707
 708        switch (host->timing) {
 709        case MMC_TIMING_MMC_HS:
 710        case MMC_TIMING_SD_HS:
 711        case MMC_TIMING_UHS_SDR25:
 712        case MMC_TIMING_UHS_DDR50:
 713        case MMC_TIMING_MMC_DDR52:
 714                /* For 50MHz clock, 120 Taps are available */
 715                tap_max = 120;
 716                break;
 717        case MMC_TIMING_UHS_SDR50:
 718                /* For 100MHz clock, 60 Taps are available */
 719                tap_max = 60;
 720                break;
 721        case MMC_TIMING_UHS_SDR104:
 722        case MMC_TIMING_MMC_HS200:
 723                /* For 200MHz clock, 30 Taps are available */
 724                tap_max = 30;
 725                break;
 726        default:
 727                break;
 728        }
 729
 730        tap_delay = (degrees * tap_max) / 360;
 731
 732        /* Set the Clock Phase */
 733        ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_INPUT, tap_delay);
 734        if (ret)
 735                pr_err("Error setting Input Tap Delay\n");
 736
 737        return ret;
 738}
 739
 740static const struct clk_ops zynqmp_sampleclk_ops = {
 741        .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
 742        .set_phase = sdhci_zynqmp_sampleclk_set_phase,
 743};
 744
 745/**
 746 * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
 747 *
 748 * @hw:                 Pointer to the hardware clock structure.
 749 * @degrees:            The clock phase shift between 0 - 359.
 750 *
 751 * Set the SD Output Clock Tap Delays for Output path
 752 *
 753 * Return: 0 on success and error value on error
 754 */
 755static int sdhci_versal_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
 756{
 757        struct sdhci_arasan_clk_data *clk_data =
 758                container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
 759        struct sdhci_arasan_data *sdhci_arasan =
 760                container_of(clk_data, struct sdhci_arasan_data, clk_data);
 761        struct sdhci_host *host = sdhci_arasan->host;
 762        u8 tap_delay, tap_max = 0;
 763
 764        /* This is applicable for SDHCI_SPEC_300 and above */
 765        if (host->version < SDHCI_SPEC_300)
 766                return 0;
 767
 768        switch (host->timing) {
 769        case MMC_TIMING_MMC_HS:
 770        case MMC_TIMING_SD_HS:
 771        case MMC_TIMING_UHS_SDR25:
 772        case MMC_TIMING_UHS_DDR50:
 773        case MMC_TIMING_MMC_DDR52:
 774                /* For 50MHz clock, 30 Taps are available */
 775                tap_max = 30;
 776                break;
 777        case MMC_TIMING_UHS_SDR50:
 778                /* For 100MHz clock, 15 Taps are available */
 779                tap_max = 15;
 780                break;
 781        case MMC_TIMING_UHS_SDR104:
 782        case MMC_TIMING_MMC_HS200:
 783                /* For 200MHz clock, 8 Taps are available */
 784                tap_max = 8;
 785                break;
 786        default:
 787                break;
 788        }
 789
 790        tap_delay = (degrees * tap_max) / 360;
 791
 792        /* Set the Clock Phase */
 793        if (tap_delay) {
 794                u32 regval;
 795
 796                regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
 797                regval |= SDHCI_OTAPDLY_ENABLE;
 798                sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
 799                regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK;
 800                regval |= tap_delay;
 801                sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
 802        }
 803
 804        return 0;
 805}
 806
 807static const struct clk_ops versal_sdcardclk_ops = {
 808        .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
 809        .set_phase = sdhci_versal_sdcardclk_set_phase,
 810};
 811
 812/**
 813 * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
 814 *
 815 * @hw:                 Pointer to the hardware clock structure.
 816 * @degrees:            The clock phase shift between 0 - 359.
 817 *
 818 * Set the SD Input Clock Tap Delays for Input path
 819 *
 820 * Return: 0 on success and error value on error
 821 */
 822static int sdhci_versal_sampleclk_set_phase(struct clk_hw *hw, int degrees)
 823{
 824        struct sdhci_arasan_clk_data *clk_data =
 825                container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
 826        struct sdhci_arasan_data *sdhci_arasan =
 827                container_of(clk_data, struct sdhci_arasan_data, clk_data);
 828        struct sdhci_host *host = sdhci_arasan->host;
 829        u8 tap_delay, tap_max = 0;
 830
 831        /* This is applicable for SDHCI_SPEC_300 and above */
 832        if (host->version < SDHCI_SPEC_300)
 833                return 0;
 834
 835        switch (host->timing) {
 836        case MMC_TIMING_MMC_HS:
 837        case MMC_TIMING_SD_HS:
 838        case MMC_TIMING_UHS_SDR25:
 839        case MMC_TIMING_UHS_DDR50:
 840        case MMC_TIMING_MMC_DDR52:
 841                /* For 50MHz clock, 120 Taps are available */
 842                tap_max = 120;
 843                break;
 844        case MMC_TIMING_UHS_SDR50:
 845                /* For 100MHz clock, 60 Taps are available */
 846                tap_max = 60;
 847                break;
 848        case MMC_TIMING_UHS_SDR104:
 849        case MMC_TIMING_MMC_HS200:
 850                /* For 200MHz clock, 30 Taps are available */
 851                tap_max = 30;
 852                break;
 853        default:
 854                break;
 855        }
 856
 857        tap_delay = (degrees * tap_max) / 360;
 858
 859        /* Set the Clock Phase */
 860        if (tap_delay) {
 861                u32 regval;
 862
 863                regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
 864                regval |= SDHCI_ITAPDLY_CHGWIN;
 865                sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
 866                regval |= SDHCI_ITAPDLY_ENABLE;
 867                sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
 868                regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK;
 869                regval |= tap_delay;
 870                sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
 871                regval &= ~SDHCI_ITAPDLY_CHGWIN;
 872                sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
 873        }
 874
 875        return 0;
 876}
 877
 878static const struct clk_ops versal_sampleclk_ops = {
 879        .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
 880        .set_phase = sdhci_versal_sampleclk_set_phase,
 881};
 882
 883static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 deviceid)
 884{
 885        u16 clk;
 886
 887        clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
 888        clk &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN);
 889        sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
 890
 891        /* Issue DLL Reset */
 892        zynqmp_pm_sd_dll_reset(deviceid, PM_DLL_RESET_PULSE);
 893
 894        clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
 895
 896        sdhci_enable_clk(host, clk);
 897}
 898
 899static int arasan_zynqmp_execute_tuning(struct mmc_host *mmc, u32 opcode)
 900{
 901        struct sdhci_host *host = mmc_priv(mmc);
 902        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 903        struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
 904        struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw;
 905        const char *clk_name = clk_hw_get_name(hw);
 906        u32 device_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 :
 907                                                           NODE_SD_1;
 908        int err;
 909
 910        /* ZynqMP SD controller does not perform auto tuning in DDR50 mode */
 911        if (mmc->ios.timing == MMC_TIMING_UHS_DDR50)
 912                return 0;
 913
 914        arasan_zynqmp_dll_reset(host, device_id);
 915
 916        err = sdhci_execute_tuning(mmc, opcode);
 917        if (err)
 918                return err;
 919
 920        arasan_zynqmp_dll_reset(host, device_id);
 921
 922        return 0;
 923}
 924
 925/**
 926 * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
 927 *
 928 * @host:               The sdhci_host
 929 * @value:              The value to write
 930 *
 931 * The corecfg_clockmultiplier is supposed to contain clock multiplier
 932 * value of programmable clock generator.
 933 *
 934 * NOTES:
 935 * - Many existing devices don't seem to do this and work fine.  To keep
 936 *   compatibility for old hardware where the device tree doesn't provide a
 937 *   register map, this function is a noop if a soc_ctl_map hasn't been provided
 938 *   for this platform.
 939 * - The value of corecfg_clockmultiplier should sync with that of corresponding
 940 *   value reading from sdhci_capability_register. So this function is called
 941 *   once at probe time and never called again.
 942 */
 943static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
 944                                                u32 value)
 945{
 946        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 947        struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
 948        const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
 949                sdhci_arasan->soc_ctl_map;
 950
 951        /* Having a map is optional */
 952        if (!soc_ctl_map)
 953                return;
 954
 955        /* If we have a map, we expect to have a syscon */
 956        if (!sdhci_arasan->soc_ctl_base) {
 957                pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
 958                        mmc_hostname(host->mmc));
 959                return;
 960        }
 961
 962        sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
 963}
 964
 965/**
 966 * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
 967 *
 968 * @host:               The sdhci_host
 969 *
 970 * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin.  This
 971 * function can be used to make that happen.
 972 *
 973 * NOTES:
 974 * - Many existing devices don't seem to do this and work fine.  To keep
 975 *   compatibility for old hardware where the device tree doesn't provide a
 976 *   register map, this function is a noop if a soc_ctl_map hasn't been provided
 977 *   for this platform.
 978 * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
 979 *   to achieve lower clock rates.  That means that this function is called once
 980 *   at probe time and never called again.
 981 */
 982static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
 983{
 984        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 985        struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
 986        const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
 987                sdhci_arasan->soc_ctl_map;
 988        u32 mhz = DIV_ROUND_CLOSEST_ULL(clk_get_rate(pltfm_host->clk), 1000000);
 989
 990        /* Having a map is optional */
 991        if (!soc_ctl_map)
 992                return;
 993
 994        /* If we have a map, we expect to have a syscon */
 995        if (!sdhci_arasan->soc_ctl_base) {
 996                pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
 997                        mmc_hostname(host->mmc));
 998                return;
 999        }
1000
1001        sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
1002}
1003
1004static void sdhci_arasan_set_clk_delays(struct sdhci_host *host)
1005{
1006        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1007        struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1008        struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1009
1010        clk_set_phase(clk_data->sampleclk,
1011                      clk_data->clk_phase_in[host->timing]);
1012        clk_set_phase(clk_data->sdcardclk,
1013                      clk_data->clk_phase_out[host->timing]);
1014}
1015
1016static void arasan_dt_read_clk_phase(struct device *dev,
1017                                     struct sdhci_arasan_clk_data *clk_data,
1018                                     unsigned int timing, const char *prop)
1019{
1020        struct device_node *np = dev->of_node;
1021
1022        u32 clk_phase[2] = {0};
1023        int ret;
1024
1025        /*
1026         * Read Tap Delay values from DT, if the DT does not contain the
1027         * Tap Values then use the pre-defined values.
1028         */
1029        ret = of_property_read_variable_u32_array(np, prop, &clk_phase[0],
1030                                                  2, 0);
1031        if (ret < 0) {
1032                dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
1033                        prop, clk_data->clk_phase_in[timing],
1034                        clk_data->clk_phase_out[timing]);
1035                return;
1036        }
1037
1038        /* The values read are Input and Output Clock Delays in order */
1039        clk_data->clk_phase_in[timing] = clk_phase[0];
1040        clk_data->clk_phase_out[timing] = clk_phase[1];
1041}
1042
1043/**
1044 * arasan_dt_parse_clk_phases - Read Clock Delay values from DT
1045 *
1046 * @dev:                Pointer to our struct device.
1047 * @clk_data:           Pointer to the Clock Data structure
1048 *
1049 * Called at initialization to parse the values of Clock Delays.
1050 */
1051static void arasan_dt_parse_clk_phases(struct device *dev,
1052                                       struct sdhci_arasan_clk_data *clk_data)
1053{
1054        u32 mio_bank = 0;
1055        int i;
1056
1057        /*
1058         * This has been kept as a pointer and is assigned a function here.
1059         * So that different controller variants can assign their own handling
1060         * function.
1061         */
1062        clk_data->set_clk_delays = sdhci_arasan_set_clk_delays;
1063
1064        if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) {
1065                u32 zynqmp_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1066                        ZYNQMP_ICLK_PHASE;
1067                u32 zynqmp_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1068                        ZYNQMP_OCLK_PHASE;
1069
1070                of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank);
1071                if (mio_bank == 2) {
1072                        zynqmp_oclk_phase[MMC_TIMING_UHS_SDR104] = 90;
1073                        zynqmp_oclk_phase[MMC_TIMING_MMC_HS200] = 90;
1074                }
1075
1076                for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1077                        clk_data->clk_phase_in[i] = zynqmp_iclk_phase[i];
1078                        clk_data->clk_phase_out[i] = zynqmp_oclk_phase[i];
1079                }
1080        }
1081
1082        if (of_device_is_compatible(dev->of_node, "xlnx,versal-8.9a")) {
1083                u32 versal_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1084                        VERSAL_ICLK_PHASE;
1085                u32 versal_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1086                        VERSAL_OCLK_PHASE;
1087
1088                for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1089                        clk_data->clk_phase_in[i] = versal_iclk_phase[i];
1090                        clk_data->clk_phase_out[i] = versal_oclk_phase[i];
1091                }
1092        }
1093
1094        arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY,
1095                                 "clk-phase-legacy");
1096        arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS,
1097                                 "clk-phase-mmc-hs");
1098        arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS,
1099                                 "clk-phase-sd-hs");
1100        arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12,
1101                                 "clk-phase-uhs-sdr12");
1102        arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25,
1103                                 "clk-phase-uhs-sdr25");
1104        arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50,
1105                                 "clk-phase-uhs-sdr50");
1106        arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104,
1107                                 "clk-phase-uhs-sdr104");
1108        arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50,
1109                                 "clk-phase-uhs-ddr50");
1110        arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52,
1111                                 "clk-phase-mmc-ddr52");
1112        arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200,
1113                                 "clk-phase-mmc-hs200");
1114        arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400,
1115                                 "clk-phase-mmc-hs400");
1116}
1117
1118static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
1119        .ops = &sdhci_arasan_ops,
1120        .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1121        .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1122                        SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1123                        SDHCI_QUIRK2_STOP_WITH_TC,
1124};
1125
1126static const struct sdhci_arasan_clk_ops arasan_clk_ops = {
1127        .sdcardclk_ops = &arasan_sdcardclk_ops,
1128        .sampleclk_ops = &arasan_sampleclk_ops,
1129};
1130
1131static struct sdhci_arasan_of_data sdhci_arasan_generic_data = {
1132        .pdata = &sdhci_arasan_pdata,
1133        .clk_ops = &arasan_clk_ops,
1134};
1135
1136static const struct sdhci_pltfm_data sdhci_keembay_emmc_pdata = {
1137        .ops = &sdhci_arasan_cqe_ops,
1138        .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1139                SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1140                SDHCI_QUIRK_NO_LED |
1141                SDHCI_QUIRK_32BIT_DMA_ADDR |
1142                SDHCI_QUIRK_32BIT_DMA_SIZE |
1143                SDHCI_QUIRK_32BIT_ADMA_SIZE,
1144        .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1145                SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1146                SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
1147                SDHCI_QUIRK2_STOP_WITH_TC |
1148                SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1149};
1150
1151static const struct sdhci_pltfm_data sdhci_keembay_sd_pdata = {
1152        .ops = &sdhci_arasan_ops,
1153        .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1154                SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1155                SDHCI_QUIRK_NO_LED |
1156                SDHCI_QUIRK_32BIT_DMA_ADDR |
1157                SDHCI_QUIRK_32BIT_DMA_SIZE |
1158                SDHCI_QUIRK_32BIT_ADMA_SIZE,
1159        .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1160                SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1161                SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
1162                SDHCI_QUIRK2_STOP_WITH_TC |
1163                SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1164};
1165
1166static const struct sdhci_pltfm_data sdhci_keembay_sdio_pdata = {
1167        .ops = &sdhci_arasan_ops,
1168        .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1169                SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1170                SDHCI_QUIRK_NO_LED |
1171                SDHCI_QUIRK_32BIT_DMA_ADDR |
1172                SDHCI_QUIRK_32BIT_DMA_SIZE |
1173                SDHCI_QUIRK_32BIT_ADMA_SIZE,
1174        .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1175                SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1176                SDHCI_QUIRK2_HOST_OFF_CARD_ON |
1177                SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1178};
1179
1180static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = {
1181        .soc_ctl_map = &rk3399_soc_ctl_map,
1182        .pdata = &sdhci_arasan_cqe_pdata,
1183        .clk_ops = &arasan_clk_ops,
1184};
1185
1186static struct sdhci_arasan_of_data intel_lgm_emmc_data = {
1187        .soc_ctl_map = &intel_lgm_emmc_soc_ctl_map,
1188        .pdata = &sdhci_arasan_cqe_pdata,
1189        .clk_ops = &arasan_clk_ops,
1190};
1191
1192static struct sdhci_arasan_of_data intel_lgm_sdxc_data = {
1193        .soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map,
1194        .pdata = &sdhci_arasan_cqe_pdata,
1195        .clk_ops = &arasan_clk_ops,
1196};
1197
1198static const struct sdhci_pltfm_data sdhci_arasan_zynqmp_pdata = {
1199        .ops = &sdhci_arasan_ops,
1200        .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1201                        SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1202                        SDHCI_QUIRK2_STOP_WITH_TC,
1203};
1204
1205static const struct sdhci_arasan_clk_ops zynqmp_clk_ops = {
1206        .sdcardclk_ops = &zynqmp_sdcardclk_ops,
1207        .sampleclk_ops = &zynqmp_sampleclk_ops,
1208};
1209
1210static struct sdhci_arasan_of_data sdhci_arasan_zynqmp_data = {
1211        .pdata = &sdhci_arasan_zynqmp_pdata,
1212        .clk_ops = &zynqmp_clk_ops,
1213};
1214
1215static const struct sdhci_arasan_clk_ops versal_clk_ops = {
1216        .sdcardclk_ops = &versal_sdcardclk_ops,
1217        .sampleclk_ops = &versal_sampleclk_ops,
1218};
1219
1220static struct sdhci_arasan_of_data sdhci_arasan_versal_data = {
1221        .pdata = &sdhci_arasan_zynqmp_pdata,
1222        .clk_ops = &versal_clk_ops,
1223};
1224
1225static struct sdhci_arasan_of_data intel_keembay_emmc_data = {
1226        .soc_ctl_map = &intel_keembay_soc_ctl_map,
1227        .pdata = &sdhci_keembay_emmc_pdata,
1228        .clk_ops = &arasan_clk_ops,
1229};
1230
1231static struct sdhci_arasan_of_data intel_keembay_sd_data = {
1232        .soc_ctl_map = &intel_keembay_soc_ctl_map,
1233        .pdata = &sdhci_keembay_sd_pdata,
1234        .clk_ops = &arasan_clk_ops,
1235};
1236
1237static struct sdhci_arasan_of_data intel_keembay_sdio_data = {
1238        .soc_ctl_map = &intel_keembay_soc_ctl_map,
1239        .pdata = &sdhci_keembay_sdio_pdata,
1240        .clk_ops = &arasan_clk_ops,
1241};
1242
1243static const struct of_device_id sdhci_arasan_of_match[] = {
1244        /* SoC-specific compatible strings w/ soc_ctl_map */
1245        {
1246                .compatible = "rockchip,rk3399-sdhci-5.1",
1247                .data = &sdhci_arasan_rk3399_data,
1248        },
1249        {
1250                .compatible = "intel,lgm-sdhci-5.1-emmc",
1251                .data = &intel_lgm_emmc_data,
1252        },
1253        {
1254                .compatible = "intel,lgm-sdhci-5.1-sdxc",
1255                .data = &intel_lgm_sdxc_data,
1256        },
1257        {
1258                .compatible = "intel,keembay-sdhci-5.1-emmc",
1259                .data = &intel_keembay_emmc_data,
1260        },
1261        {
1262                .compatible = "intel,keembay-sdhci-5.1-sd",
1263                .data = &intel_keembay_sd_data,
1264        },
1265        {
1266                .compatible = "intel,keembay-sdhci-5.1-sdio",
1267                .data = &intel_keembay_sdio_data,
1268        },
1269        /* Generic compatible below here */
1270        {
1271                .compatible = "arasan,sdhci-8.9a",
1272                .data = &sdhci_arasan_generic_data,
1273        },
1274        {
1275                .compatible = "arasan,sdhci-5.1",
1276                .data = &sdhci_arasan_generic_data,
1277        },
1278        {
1279                .compatible = "arasan,sdhci-4.9a",
1280                .data = &sdhci_arasan_generic_data,
1281        },
1282        {
1283                .compatible = "xlnx,zynqmp-8.9a",
1284                .data = &sdhci_arasan_zynqmp_data,
1285        },
1286        {
1287                .compatible = "xlnx,versal-8.9a",
1288                .data = &sdhci_arasan_versal_data,
1289        },
1290        { /* sentinel */ }
1291};
1292MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
1293
1294/**
1295 * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use
1296 *
1297 * @sdhci_arasan:       Our private data structure.
1298 * @clk_xin:            Pointer to the functional clock
1299 * @dev:                Pointer to our struct device.
1300 *
1301 * Some PHY devices need to know what the actual card clock is.  In order for
1302 * them to find out, we'll provide a clock through the common clock framework
1303 * for them to query.
1304 *
1305 * Return: 0 on success and error value on error
1306 */
1307static int
1308sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan,
1309                                struct clk *clk_xin,
1310                                struct device *dev)
1311{
1312        struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1313        struct device_node *np = dev->of_node;
1314        struct clk_init_data sdcardclk_init;
1315        const char *parent_clk_name;
1316        int ret;
1317
1318        ret = of_property_read_string_index(np, "clock-output-names", 0,
1319                                            &sdcardclk_init.name);
1320        if (ret) {
1321                dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1322                return ret;
1323        }
1324
1325        parent_clk_name = __clk_get_name(clk_xin);
1326        sdcardclk_init.parent_names = &parent_clk_name;
1327        sdcardclk_init.num_parents = 1;
1328        sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
1329        sdcardclk_init.ops = sdhci_arasan->clk_ops->sdcardclk_ops;
1330
1331        clk_data->sdcardclk_hw.init = &sdcardclk_init;
1332        clk_data->sdcardclk =
1333                devm_clk_register(dev, &clk_data->sdcardclk_hw);
1334        if (IS_ERR(clk_data->sdcardclk))
1335                return PTR_ERR(clk_data->sdcardclk);
1336        clk_data->sdcardclk_hw.init = NULL;
1337
1338        ret = of_clk_add_provider(np, of_clk_src_simple_get,
1339                                  clk_data->sdcardclk);
1340        if (ret)
1341                dev_err(dev, "Failed to add sdcard clock provider\n");
1342
1343        return ret;
1344}
1345
1346/**
1347 * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use
1348 *
1349 * @sdhci_arasan:       Our private data structure.
1350 * @clk_xin:            Pointer to the functional clock
1351 * @dev:                Pointer to our struct device.
1352 *
1353 * Some PHY devices need to know what the actual card clock is.  In order for
1354 * them to find out, we'll provide a clock through the common clock framework
1355 * for them to query.
1356 *
1357 * Return: 0 on success and error value on error
1358 */
1359static int
1360sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan,
1361                                struct clk *clk_xin,
1362                                struct device *dev)
1363{
1364        struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1365        struct device_node *np = dev->of_node;
1366        struct clk_init_data sampleclk_init;
1367        const char *parent_clk_name;
1368        int ret;
1369
1370        ret = of_property_read_string_index(np, "clock-output-names", 1,
1371                                            &sampleclk_init.name);
1372        if (ret) {
1373                dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1374                return ret;
1375        }
1376
1377        parent_clk_name = __clk_get_name(clk_xin);
1378        sampleclk_init.parent_names = &parent_clk_name;
1379        sampleclk_init.num_parents = 1;
1380        sampleclk_init.flags = CLK_GET_RATE_NOCACHE;
1381        sampleclk_init.ops = sdhci_arasan->clk_ops->sampleclk_ops;
1382
1383        clk_data->sampleclk_hw.init = &sampleclk_init;
1384        clk_data->sampleclk =
1385                devm_clk_register(dev, &clk_data->sampleclk_hw);
1386        if (IS_ERR(clk_data->sampleclk))
1387                return PTR_ERR(clk_data->sampleclk);
1388        clk_data->sampleclk_hw.init = NULL;
1389
1390        ret = of_clk_add_provider(np, of_clk_src_simple_get,
1391                                  clk_data->sampleclk);
1392        if (ret)
1393                dev_err(dev, "Failed to add sample clock provider\n");
1394
1395        return ret;
1396}
1397
1398/**
1399 * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
1400 *
1401 * @dev:                Pointer to our struct device.
1402 *
1403 * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
1404 * returned success.
1405 */
1406static void sdhci_arasan_unregister_sdclk(struct device *dev)
1407{
1408        struct device_node *np = dev->of_node;
1409
1410        if (!of_find_property(np, "#clock-cells", NULL))
1411                return;
1412
1413        of_clk_del_provider(dev->of_node);
1414}
1415
1416/**
1417 * sdhci_arasan_update_support64b - Set SUPPORT_64B (64-bit System Bus Support)
1418 * @host:               The sdhci_host
1419 * @value:              The value to write
1420 *
1421 * This should be set based on the System Address Bus.
1422 * 0: the Core supports only 32-bit System Address Bus.
1423 * 1: the Core supports 64-bit System Address Bus.
1424 *
1425 * NOTE:
1426 * For Keem Bay, it is required to clear this bit. Its default value is 1'b1.
1427 * Keem Bay does not support 64-bit access.
1428 */
1429static void sdhci_arasan_update_support64b(struct sdhci_host *host, u32 value)
1430{
1431        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1432        struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1433        const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
1434
1435        /* Having a map is optional */
1436        soc_ctl_map = sdhci_arasan->soc_ctl_map;
1437        if (!soc_ctl_map)
1438                return;
1439
1440        /* If we have a map, we expect to have a syscon */
1441        if (!sdhci_arasan->soc_ctl_base) {
1442                pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1443                        mmc_hostname(host->mmc));
1444                return;
1445        }
1446
1447        sdhci_arasan_syscon_write(host, &soc_ctl_map->support64b, value);
1448}
1449
1450/**
1451 * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use
1452 *
1453 * @sdhci_arasan:       Our private data structure.
1454 * @clk_xin:            Pointer to the functional clock
1455 * @dev:                Pointer to our struct device.
1456 *
1457 * Some PHY devices need to know what the actual card clock is.  In order for
1458 * them to find out, we'll provide a clock through the common clock framework
1459 * for them to query.
1460 *
1461 * Note: without seriously re-architecting SDHCI's clock code and testing on
1462 * all platforms, there's no way to create a totally beautiful clock here
1463 * with all clock ops implemented.  Instead, we'll just create a clock that can
1464 * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
1465 * framework that we're doing things behind its back.  This should be sufficient
1466 * to create nice clean device tree bindings and later (if needed) we can try
1467 * re-architecting SDHCI if we see some benefit to it.
1468 *
1469 * Return: 0 on success and error value on error
1470 */
1471static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
1472                                       struct clk *clk_xin,
1473                                       struct device *dev)
1474{
1475        struct device_node *np = dev->of_node;
1476        u32 num_clks = 0;
1477        int ret;
1478
1479        /* Providing a clock to the PHY is optional; no error if missing */
1480        if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0)
1481                return 0;
1482
1483        ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev);
1484        if (ret)
1485                return ret;
1486
1487        if (num_clks) {
1488                ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin,
1489                                                      dev);
1490                if (ret) {
1491                        sdhci_arasan_unregister_sdclk(dev);
1492                        return ret;
1493                }
1494        }
1495
1496        return 0;
1497}
1498
1499static int sdhci_zynqmp_set_dynamic_config(struct device *dev,
1500                                           struct sdhci_arasan_data *sdhci_arasan)
1501{
1502        struct sdhci_host *host = sdhci_arasan->host;
1503        struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw;
1504        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1505        const char *clk_name = clk_hw_get_name(hw);
1506        u32 node_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : NODE_SD_1;
1507        struct reset_control *rstc;
1508        u32 mhz;
1509        int ret;
1510
1511        /* Obtain SDHC reset control */
1512        rstc = devm_reset_control_get_optional_exclusive(dev, NULL);
1513        if (IS_ERR(rstc)) {
1514                dev_err(dev, "Cannot get SDHC reset.\n");
1515                return PTR_ERR(rstc);
1516        }
1517
1518        reset_control_assert(rstc);
1519
1520        ret = zynqmp_pm_set_sd_config(node_id, SD_CONFIG_FIXED, 0);
1521        if (ret)
1522                return ret;
1523
1524        ret = zynqmp_pm_set_sd_config(node_id, SD_CONFIG_EMMC_SEL,
1525                                      !!(host->mmc->caps & MMC_CAP_NONREMOVABLE));
1526        if (ret)
1527                return ret;
1528
1529        mhz = DIV_ROUND_CLOSEST_ULL(clk_get_rate(pltfm_host->clk), 1000000);
1530        if (mhz > 100 && mhz <= 200)
1531                mhz = 200;
1532        else if (mhz > 50 && mhz <= 100)
1533                mhz = 100;
1534        else if (mhz > 25 && mhz <= 50)
1535                mhz = 50;
1536        else
1537                mhz = 25;
1538
1539        ret = zynqmp_pm_set_sd_config(node_id, SD_CONFIG_BASECLK, mhz);
1540        if (ret)
1541                return ret;
1542
1543        ret = zynqmp_pm_set_sd_config(node_id, SD_CONFIG_8BIT,
1544                                      !!(host->mmc->caps & MMC_CAP_8_BIT_DATA));
1545        if (ret)
1546                return ret;
1547
1548        reset_control_deassert(rstc);
1549
1550        /*
1551         * Workaround: In case of eMMC, to reach the card stable state
1552         * it is taking 1msec delay after deassert the controller reset.
1553         */
1554        usleep_range(1000, 1500);
1555
1556        return 0;
1557}
1558
1559static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
1560{
1561        struct sdhci_host *host = sdhci_arasan->host;
1562        struct cqhci_host *cq_host;
1563        bool dma64;
1564        int ret;
1565
1566        if (!sdhci_arasan->has_cqe)
1567                return sdhci_add_host(host);
1568
1569        ret = sdhci_setup_host(host);
1570        if (ret)
1571                return ret;
1572
1573        cq_host = devm_kzalloc(host->mmc->parent,
1574                               sizeof(*cq_host), GFP_KERNEL);
1575        if (!cq_host) {
1576                ret = -ENOMEM;
1577                goto cleanup;
1578        }
1579
1580        cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
1581        cq_host->ops = &sdhci_arasan_cqhci_ops;
1582
1583        dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1584        if (dma64)
1585                cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1586
1587        ret = cqhci_init(cq_host, host->mmc, dma64);
1588        if (ret)
1589                goto cleanup;
1590
1591        ret = __sdhci_add_host(host);
1592        if (ret)
1593                goto cleanup;
1594
1595        return 0;
1596
1597cleanup:
1598        sdhci_cleanup_host(host);
1599        return ret;
1600}
1601
1602static int sdhci_arasan_probe(struct platform_device *pdev)
1603{
1604        int ret;
1605        struct device_node *node;
1606        struct clk *clk_xin;
1607        struct sdhci_host *host;
1608        struct sdhci_pltfm_host *pltfm_host;
1609        struct device *dev = &pdev->dev;
1610        struct device_node *np = dev->of_node;
1611        struct sdhci_arasan_data *sdhci_arasan;
1612        const struct sdhci_arasan_of_data *data;
1613
1614        data = of_device_get_match_data(dev);
1615        host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan));
1616
1617        if (IS_ERR(host))
1618                return PTR_ERR(host);
1619
1620        pltfm_host = sdhci_priv(host);
1621        sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1622        sdhci_arasan->host = host;
1623
1624        sdhci_arasan->soc_ctl_map = data->soc_ctl_map;
1625        sdhci_arasan->clk_ops = data->clk_ops;
1626
1627        node = of_parse_phandle(np, "arasan,soc-ctl-syscon", 0);
1628        if (node) {
1629                sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
1630                of_node_put(node);
1631
1632                if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
1633                        ret = dev_err_probe(dev,
1634                                            PTR_ERR(sdhci_arasan->soc_ctl_base),
1635                                            "Can't get syscon\n");
1636                        goto err_pltfm_free;
1637                }
1638        }
1639
1640        sdhci_get_of_property(pdev);
1641
1642        sdhci_arasan->clk_ahb = devm_clk_get(dev, "clk_ahb");
1643        if (IS_ERR(sdhci_arasan->clk_ahb)) {
1644                ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->clk_ahb),
1645                                    "clk_ahb clock not found.\n");
1646                goto err_pltfm_free;
1647        }
1648
1649        clk_xin = devm_clk_get(dev, "clk_xin");
1650        if (IS_ERR(clk_xin)) {
1651                ret = dev_err_probe(dev, PTR_ERR(clk_xin), "clk_xin clock not found.\n");
1652                goto err_pltfm_free;
1653        }
1654
1655        ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
1656        if (ret) {
1657                dev_err(dev, "Unable to enable AHB clock.\n");
1658                goto err_pltfm_free;
1659        }
1660
1661        /* If clock-frequency property is set, use the provided value */
1662        if (pltfm_host->clock &&
1663            pltfm_host->clock != clk_get_rate(clk_xin)) {
1664                ret = clk_set_rate(clk_xin, pltfm_host->clock);
1665                if (ret) {
1666                        dev_err(&pdev->dev, "Failed to set SD clock rate\n");
1667                        goto clk_dis_ahb;
1668                }
1669        }
1670
1671        ret = clk_prepare_enable(clk_xin);
1672        if (ret) {
1673                dev_err(dev, "Unable to enable SD clock.\n");
1674                goto clk_dis_ahb;
1675        }
1676
1677        if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
1678                sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
1679
1680        if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
1681                sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
1682
1683        pltfm_host->clk = clk_xin;
1684
1685        if (of_device_is_compatible(np, "rockchip,rk3399-sdhci-5.1"))
1686                sdhci_arasan_update_clockmultiplier(host, 0x0);
1687
1688        if (of_device_is_compatible(np, "intel,keembay-sdhci-5.1-emmc") ||
1689            of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sd") ||
1690            of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sdio")) {
1691                sdhci_arasan_update_clockmultiplier(host, 0x0);
1692                sdhci_arasan_update_support64b(host, 0x0);
1693
1694                host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1695        }
1696
1697        sdhci_arasan_update_baseclkfreq(host);
1698
1699        ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, dev);
1700        if (ret)
1701                goto clk_disable_all;
1702
1703        if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
1704                host->mmc_host_ops.execute_tuning =
1705                        arasan_zynqmp_execute_tuning;
1706
1707                sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN;
1708                host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
1709        }
1710
1711        arasan_dt_parse_clk_phases(dev, &sdhci_arasan->clk_data);
1712
1713        ret = mmc_of_parse(host->mmc);
1714        if (ret) {
1715                ret = dev_err_probe(dev, ret, "parsing dt failed.\n");
1716                goto unreg_clk;
1717        }
1718
1719        if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
1720                ret = zynqmp_pm_is_function_supported(PM_IOCTL, IOCTL_SET_SD_CONFIG);
1721                if (!ret) {
1722                        ret = sdhci_zynqmp_set_dynamic_config(dev, sdhci_arasan);
1723                        if (ret)
1724                                goto unreg_clk;
1725                }
1726        }
1727
1728        sdhci_arasan->phy = ERR_PTR(-ENODEV);
1729        if (of_device_is_compatible(np, "arasan,sdhci-5.1")) {
1730                sdhci_arasan->phy = devm_phy_get(dev, "phy_arasan");
1731                if (IS_ERR(sdhci_arasan->phy)) {
1732                        ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->phy),
1733                                            "No phy for arasan,sdhci-5.1.\n");
1734                        goto unreg_clk;
1735                }
1736
1737                ret = phy_init(sdhci_arasan->phy);
1738                if (ret < 0) {
1739                        dev_err(dev, "phy_init err.\n");
1740                        goto unreg_clk;
1741                }
1742
1743                host->mmc_host_ops.hs400_enhanced_strobe =
1744                                        sdhci_arasan_hs400_enhanced_strobe;
1745                host->mmc_host_ops.start_signal_voltage_switch =
1746                                        sdhci_arasan_voltage_switch;
1747                sdhci_arasan->has_cqe = true;
1748                host->mmc->caps2 |= MMC_CAP2_CQE;
1749
1750                if (!of_property_read_bool(np, "disable-cqe-dcmd"))
1751                        host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
1752        }
1753
1754        ret = sdhci_arasan_add_host(sdhci_arasan);
1755        if (ret)
1756                goto err_add_host;
1757
1758        return 0;
1759
1760err_add_host:
1761        if (!IS_ERR(sdhci_arasan->phy))
1762                phy_exit(sdhci_arasan->phy);
1763unreg_clk:
1764        sdhci_arasan_unregister_sdclk(dev);
1765clk_disable_all:
1766        clk_disable_unprepare(clk_xin);
1767clk_dis_ahb:
1768        clk_disable_unprepare(sdhci_arasan->clk_ahb);
1769err_pltfm_free:
1770        sdhci_pltfm_free(pdev);
1771        return ret;
1772}
1773
1774static int sdhci_arasan_remove(struct platform_device *pdev)
1775{
1776        int ret;
1777        struct sdhci_host *host = platform_get_drvdata(pdev);
1778        struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1779        struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1780        struct clk *clk_ahb = sdhci_arasan->clk_ahb;
1781
1782        if (!IS_ERR(sdhci_arasan->phy)) {
1783                if (sdhci_arasan->is_phy_on)
1784                        phy_power_off(sdhci_arasan->phy);
1785                phy_exit(sdhci_arasan->phy);
1786        }
1787
1788        sdhci_arasan_unregister_sdclk(&pdev->dev);
1789
1790        ret = sdhci_pltfm_unregister(pdev);
1791
1792        clk_disable_unprepare(clk_ahb);
1793
1794        return ret;
1795}
1796
1797static struct platform_driver sdhci_arasan_driver = {
1798        .driver = {
1799                .name = "sdhci-arasan",
1800                .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1801                .of_match_table = sdhci_arasan_of_match,
1802                .pm = &sdhci_arasan_dev_pm_ops,
1803        },
1804        .probe = sdhci_arasan_probe,
1805        .remove = sdhci_arasan_remove,
1806};
1807
1808module_platform_driver(sdhci_arasan_driver);
1809
1810MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
1811MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
1812MODULE_LICENSE("GPL");
1813