Linux Audio

Check our new training course

Embedded Linux training

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