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