1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
9 * Based on sdhci-of-esdhc.c
11 * Copyright (c) 2007 Freescale Semiconductor, Inc.
12 * Copyright (c) 2009 MontaVista Software, Inc.
14 * Authors: Xiaobo Xie <X.Xie@freescale.com>
15 * Anton Vorontsov <avorontsov@ru.mvista.com>
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>
25 #include <linux/firmware/xlnx-zynqmp.h>
28 #include "sdhci-pltfm.h"
30 #define SDHCI_ARASAN_VENDOR_REGISTER 0x78
32 #define SDHCI_ARASAN_ITAPDLY_REGISTER 0xF0F8
33 #define SDHCI_ARASAN_ITAPDLY_SEL_MASK 0xFF
35 #define SDHCI_ARASAN_OTAPDLY_REGISTER 0xF0FC
36 #define SDHCI_ARASAN_OTAPDLY_SEL_MASK 0x3F
38 #define SDHCI_ARASAN_CQE_BASE_ADDR 0x200
39 #define VENDOR_ENHANCED_STROBE BIT(0)
41 #define PHY_CLK_TOO_SLOW_HZ 400000
43 #define SDHCI_ITAPDLY_CHGWIN 0x200
44 #define SDHCI_ITAPDLY_ENABLE 0x100
45 #define SDHCI_OTAPDLY_ENABLE 0x40
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}
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}
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.
60 #define HIWORD_UPDATE(val, mask, shift) \
61 ((val) << (shift) | (mask) << ((shift) + 16))
64 * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
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)
70 struct sdhci_arasan_soc_ctl_field {
77 * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
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
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.
88 struct 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;
96 * struct sdhci_arasan_clk_ops - Clock Operations for Arasan SD controller
98 * @sdcardclk_ops: The output clock related operations
99 * @sampleclk_ops: The sample clock related operations
101 struct sdhci_arasan_clk_ops {
102 const struct clk_ops *sdcardclk_ops;
103 const struct clk_ops *sampleclk_ops;
107 * struct sdhci_arasan_clk_data - Arasan Controller Clock Data.
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
118 struct 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);
130 * struct sdhci_arasan_data - Arasan Controller Data
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.
143 struct sdhci_arasan_data {
144 struct sdhci_host *host;
150 struct sdhci_arasan_clk_data clk_data;
151 const struct sdhci_arasan_clk_ops *clk_ops;
153 struct regmap *soc_ctl_base;
154 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
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)
163 * Some of the Arasan variations might not have timing requirements
164 * met at 25MHz for Default Speed mode, those controllers work at
167 #define SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN BIT(2)
170 struct 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;
176 static 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,
182 static 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,
188 static 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,
194 static 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,
202 * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
204 * @host: The sdhci_host
205 * @fld: The field to write to
206 * @val: The value to write
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.
213 * Return: 0 on success and error value on error
215 static int sdhci_arasan_syscon_write(struct sdhci_host *host,
216 const struct sdhci_arasan_soc_ctl_field *fld,
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;
223 u16 width = fld->width;
224 s16 shift = fld->shift;
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
236 if (sdhci_arasan->soc_ctl_map->hiword_update)
237 ret = regmap_write(soc_ctl_base, reg,
238 HIWORD_UPDATE(val, GENMASK(width, 0),
241 ret = regmap_update_bits(soc_ctl_base, reg,
242 GENMASK(shift + width, shift),
245 /* Yell about (unexpected) regmap errors */
247 pr_warn("%s: Regmap write fail: %d\n",
248 mmc_hostname(host->mmc), ret);
253 static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
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;
260 if (!IS_ERR(sdhci_arasan->phy)) {
261 if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
263 * If PHY off, set clock to max speed and power PHY on.
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
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.
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));
282 sdhci_arasan->is_phy_on = true;
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.
289 } else if (clock > PHY_CLK_TOO_SLOW_HZ) {
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.
299 if (ctrl_phy && sdhci_arasan->is_phy_on) {
300 phy_power_off(sdhci_arasan->phy);
301 sdhci_arasan->is_phy_on = false;
304 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN) {
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.
310 if (clock == DEFAULT_SPEED_MAX_DTR)
311 clock = (DEFAULT_SPEED_MAX_DTR * 19) / 25;
314 /* Set the Input and Output Clock Phase Delays */
315 if (clk_data->set_clk_delays)
316 clk_data->set_clk_delays(host);
318 sdhci_set_clock(host, clock);
320 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE)
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.
331 if (phy_power_on(sdhci_arasan->phy)) {
332 pr_err("%s: Cannot power on phy.\n",
333 mmc_hostname(host->mmc));
337 sdhci_arasan->is_phy_on = true;
341 static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
345 struct sdhci_host *host = mmc_priv(mmc);
347 vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
348 if (ios->enhanced_strobe)
349 vendor |= VENDOR_ENHANCED_STROBE;
351 vendor &= ~VENDOR_ENHANCED_STROBE;
353 sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
356 static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
359 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
360 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
362 sdhci_reset(host, mask);
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);
371 static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
374 switch (ios->signal_voltage) {
375 case MMC_SIGNAL_VOLTAGE_180:
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.
384 case MMC_SIGNAL_VOLTAGE_330:
385 case MMC_SIGNAL_VOLTAGE_120:
386 /* We don't support 3V3 and 1V2 */
393 static 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,
403 static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
408 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
411 cqhci_irq(host->mmc, intmask, cmd_error, data_error);
416 static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
418 sdhci_dumpregs(mmc_priv(mmc));
421 static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
423 struct sdhci_host *host = mmc_priv(mmc);
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);
432 sdhci_cqe_enable(mmc);
435 static 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,
441 static 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,
452 static 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,
459 #ifdef CONFIG_PM_SLEEP
461 * sdhci_arasan_suspend - Suspend method for the driver
462 * @dev: Address of the device structure
464 * Put the device in a low power state.
466 * Return: 0 on success and error value on error
468 static int sdhci_arasan_suspend(struct device *dev)
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);
475 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
476 mmc_retune_needed(host->mmc);
478 if (sdhci_arasan->has_cqe) {
479 ret = cqhci_suspend(host->mmc);
484 ret = sdhci_suspend_host(host);
488 if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
489 ret = phy_power_off(sdhci_arasan->phy);
491 dev_err(dev, "Cannot power off phy.\n");
492 if (sdhci_resume_host(host))
493 dev_err(dev, "Cannot resume host.\n");
497 sdhci_arasan->is_phy_on = false;
500 clk_disable(pltfm_host->clk);
501 clk_disable(sdhci_arasan->clk_ahb);
507 * sdhci_arasan_resume - Resume method for the driver
508 * @dev: Address of the device structure
510 * Resume operation after suspend
512 * Return: 0 on success and error value on error
514 static int sdhci_arasan_resume(struct device *dev)
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);
521 ret = clk_enable(sdhci_arasan->clk_ahb);
523 dev_err(dev, "Cannot enable AHB clock.\n");
527 ret = clk_enable(pltfm_host->clk);
529 dev_err(dev, "Cannot enable SD clock.\n");
533 if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
534 ret = phy_power_on(sdhci_arasan->phy);
536 dev_err(dev, "Cannot power on phy.\n");
539 sdhci_arasan->is_phy_on = true;
542 ret = sdhci_resume_host(host);
544 dev_err(dev, "Cannot resume host.\n");
548 if (sdhci_arasan->has_cqe)
549 return cqhci_resume(host->mmc);
553 #endif /* ! CONFIG_PM_SLEEP */
555 static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
556 sdhci_arasan_resume);
559 * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
561 * @hw: Pointer to the hardware clock structure.
562 * @parent_rate: The parent rate (should be rate of clk_xin).
564 * Return the current actual rate of the SD card clock. This can be used
565 * to communicate with out PHY.
567 * Return: The card clock rate.
569 static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
570 unsigned long parent_rate)
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;
578 return host->mmc->actual_clock;
581 static const struct clk_ops arasan_sdcardclk_ops = {
582 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
586 * sdhci_arasan_sampleclk_recalc_rate - Return the sampling clock rate
588 * @hw: Pointer to the hardware clock structure.
589 * @parent_rate: The parent rate (should be rate of clk_xin).
591 * Return the current actual rate of the sampling clock. This can be used
592 * to communicate with out PHY.
594 * Return: The sample clock rate.
596 static unsigned long sdhci_arasan_sampleclk_recalc_rate(struct clk_hw *hw,
597 unsigned long parent_rate)
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;
605 return host->mmc->actual_clock;
608 static const struct clk_ops arasan_sampleclk_ops = {
609 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
613 * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
615 * @hw: Pointer to the hardware clock structure.
616 * @degrees: The clock phase shift between 0 - 359.
618 * Set the SD Output Clock Tap Delays for Output path
620 * Return: 0 on success and error value on error
622 static int sdhci_zynqmp_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
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;
634 /* This is applicable for SDHCI_SPEC_300 and above */
635 if (host->version < SDHCI_SPEC_300)
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 */
647 case MMC_TIMING_UHS_SDR50:
648 /* For 100MHz clock, 15 Taps are available */
651 case MMC_TIMING_UHS_SDR104:
652 case MMC_TIMING_MMC_HS200:
653 /* For 200MHz clock, 8 Taps are available */
660 tap_delay = (degrees * tap_max) / 360;
662 /* Set the Clock Phase */
663 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_OUTPUT, tap_delay);
665 pr_err("Error setting Output Tap Delay\n");
667 /* Release DLL Reset */
668 zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_RELEASE);
673 static const struct clk_ops zynqmp_sdcardclk_ops = {
674 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
675 .set_phase = sdhci_zynqmp_sdcardclk_set_phase,
679 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
681 * @hw: Pointer to the hardware clock structure.
682 * @degrees: The clock phase shift between 0 - 359.
684 * Set the SD Input Clock Tap Delays for Input path
686 * Return: 0 on success and error value on error
688 static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees)
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;
700 /* This is applicable for SDHCI_SPEC_300 and above */
701 if (host->version < SDHCI_SPEC_300)
704 /* Assert DLL Reset */
705 zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_ASSERT);
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 */
716 case MMC_TIMING_UHS_SDR50:
717 /* For 100MHz clock, 60 Taps are available */
720 case MMC_TIMING_UHS_SDR104:
721 case MMC_TIMING_MMC_HS200:
722 /* For 200MHz clock, 30 Taps are available */
729 tap_delay = (degrees * tap_max) / 360;
731 /* Set the Clock Phase */
732 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_INPUT, tap_delay);
734 pr_err("Error setting Input Tap Delay\n");
739 static const struct clk_ops zynqmp_sampleclk_ops = {
740 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
741 .set_phase = sdhci_zynqmp_sampleclk_set_phase,
745 * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
747 * @hw: Pointer to the hardware clock structure.
748 * @degrees: The clock phase shift between 0 - 359.
750 * Set the SD Output Clock Tap Delays for Output path
752 * Return: 0 on success and error value on error
754 static int sdhci_versal_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
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;
763 /* This is applicable for SDHCI_SPEC_300 and above */
764 if (host->version < SDHCI_SPEC_300)
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 */
776 case MMC_TIMING_UHS_SDR50:
777 /* For 100MHz clock, 15 Taps are available */
780 case MMC_TIMING_UHS_SDR104:
781 case MMC_TIMING_MMC_HS200:
782 /* For 200MHz clock, 8 Taps are available */
789 tap_delay = (degrees * tap_max) / 360;
791 /* Set the Clock Phase */
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;
800 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
806 static const struct clk_ops versal_sdcardclk_ops = {
807 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
808 .set_phase = sdhci_versal_sdcardclk_set_phase,
812 * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
814 * @hw: Pointer to the hardware clock structure.
815 * @degrees: The clock phase shift between 0 - 359.
817 * Set the SD Input Clock Tap Delays for Input path
819 * Return: 0 on success and error value on error
821 static int sdhci_versal_sampleclk_set_phase(struct clk_hw *hw, int degrees)
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;
830 /* This is applicable for SDHCI_SPEC_300 and above */
831 if (host->version < SDHCI_SPEC_300)
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 */
843 case MMC_TIMING_UHS_SDR50:
844 /* For 100MHz clock, 60 Taps are available */
847 case MMC_TIMING_UHS_SDR104:
848 case MMC_TIMING_MMC_HS200:
849 /* For 200MHz clock, 30 Taps are available */
856 tap_delay = (degrees * tap_max) / 360;
858 /* Set the Clock Phase */
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;
869 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
870 regval &= ~SDHCI_ITAPDLY_CHGWIN;
871 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
877 static const struct clk_ops versal_sampleclk_ops = {
878 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
879 .set_phase = sdhci_versal_sampleclk_set_phase,
882 static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 deviceid)
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);
890 /* Issue DLL Reset */
891 zynqmp_pm_sd_dll_reset(deviceid, PM_DLL_RESET_PULSE);
893 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
895 sdhci_enable_clk(host, clk);
898 static int arasan_zynqmp_execute_tuning(struct mmc_host *mmc, u32 opcode)
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 :
909 /* ZynqMP SD controller does not perform auto tuning in DDR50 mode */
910 if (mmc->ios.timing == MMC_TIMING_UHS_DDR50)
913 arasan_zynqmp_dll_reset(host, device_id);
915 err = sdhci_execute_tuning(mmc, opcode);
919 arasan_zynqmp_dll_reset(host, device_id);
925 * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
927 * @host: The sdhci_host
928 * @value: The value to write
930 * The corecfg_clockmultiplier is supposed to contain clock multiplier
931 * value of programmable clock generator.
934 * - Many existing devices don't seem to do this and work fine. To keep
935 * compatibility for old hardware where the device tree doesn't provide a
936 * register map, this function is a noop if a soc_ctl_map hasn't been provided
938 * - The value of corecfg_clockmultiplier should sync with that of corresponding
939 * value reading from sdhci_capability_register. So this function is called
940 * once at probe time and never called again.
942 static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
945 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
946 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
947 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
948 sdhci_arasan->soc_ctl_map;
950 /* Having a map is optional */
954 /* If we have a map, we expect to have a syscon */
955 if (!sdhci_arasan->soc_ctl_base) {
956 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
957 mmc_hostname(host->mmc));
961 sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
965 * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
967 * @host: The sdhci_host
969 * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This
970 * function can be used to make that happen.
973 * - Many existing devices don't seem to do this and work fine. To keep
974 * compatibility for old hardware where the device tree doesn't provide a
975 * register map, this function is a noop if a soc_ctl_map hasn't been provided
977 * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
978 * to achieve lower clock rates. That means that this function is called once
979 * at probe time and never called again.
981 static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
983 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
984 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
985 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
986 sdhci_arasan->soc_ctl_map;
987 u32 mhz = DIV_ROUND_CLOSEST_ULL(clk_get_rate(pltfm_host->clk), 1000000);
989 /* Having a map is optional */
993 /* If we have a map, we expect to have a syscon */
994 if (!sdhci_arasan->soc_ctl_base) {
995 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
996 mmc_hostname(host->mmc));
1000 sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
1003 static void sdhci_arasan_set_clk_delays(struct sdhci_host *host)
1005 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1006 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1007 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1009 clk_set_phase(clk_data->sampleclk,
1010 clk_data->clk_phase_in[host->timing]);
1011 clk_set_phase(clk_data->sdcardclk,
1012 clk_data->clk_phase_out[host->timing]);
1015 static void arasan_dt_read_clk_phase(struct device *dev,
1016 struct sdhci_arasan_clk_data *clk_data,
1017 unsigned int timing, const char *prop)
1019 struct device_node *np = dev->of_node;
1021 u32 clk_phase[2] = {0};
1025 * Read Tap Delay values from DT, if the DT does not contain the
1026 * Tap Values then use the pre-defined values.
1028 ret = of_property_read_variable_u32_array(np, prop, &clk_phase[0],
1031 dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
1032 prop, clk_data->clk_phase_in[timing],
1033 clk_data->clk_phase_out[timing]);
1037 /* The values read are Input and Output Clock Delays in order */
1038 clk_data->clk_phase_in[timing] = clk_phase[0];
1039 clk_data->clk_phase_out[timing] = clk_phase[1];
1043 * arasan_dt_parse_clk_phases - Read Clock Delay values from DT
1045 * @dev: Pointer to our struct device.
1046 * @clk_data: Pointer to the Clock Data structure
1048 * Called at initialization to parse the values of Clock Delays.
1050 static void arasan_dt_parse_clk_phases(struct device *dev,
1051 struct sdhci_arasan_clk_data *clk_data)
1057 * This has been kept as a pointer and is assigned a function here.
1058 * So that different controller variants can assign their own handling
1061 clk_data->set_clk_delays = sdhci_arasan_set_clk_delays;
1063 if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) {
1064 u32 zynqmp_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1066 u32 zynqmp_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1069 of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank);
1070 if (mio_bank == 2) {
1071 zynqmp_oclk_phase[MMC_TIMING_UHS_SDR104] = 90;
1072 zynqmp_oclk_phase[MMC_TIMING_MMC_HS200] = 90;
1075 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1076 clk_data->clk_phase_in[i] = zynqmp_iclk_phase[i];
1077 clk_data->clk_phase_out[i] = zynqmp_oclk_phase[i];
1081 if (of_device_is_compatible(dev->of_node, "xlnx,versal-8.9a")) {
1082 u32 versal_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1084 u32 versal_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1087 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1088 clk_data->clk_phase_in[i] = versal_iclk_phase[i];
1089 clk_data->clk_phase_out[i] = versal_oclk_phase[i];
1093 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY,
1094 "clk-phase-legacy");
1095 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS,
1096 "clk-phase-mmc-hs");
1097 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS,
1099 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12,
1100 "clk-phase-uhs-sdr12");
1101 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25,
1102 "clk-phase-uhs-sdr25");
1103 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50,
1104 "clk-phase-uhs-sdr50");
1105 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104,
1106 "clk-phase-uhs-sdr104");
1107 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50,
1108 "clk-phase-uhs-ddr50");
1109 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52,
1110 "clk-phase-mmc-ddr52");
1111 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200,
1112 "clk-phase-mmc-hs200");
1113 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400,
1114 "clk-phase-mmc-hs400");
1117 static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
1118 .ops = &sdhci_arasan_ops,
1119 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1120 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1121 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1122 SDHCI_QUIRK2_STOP_WITH_TC,
1125 static const struct sdhci_arasan_clk_ops arasan_clk_ops = {
1126 .sdcardclk_ops = &arasan_sdcardclk_ops,
1127 .sampleclk_ops = &arasan_sampleclk_ops,
1130 static struct sdhci_arasan_of_data sdhci_arasan_generic_data = {
1131 .pdata = &sdhci_arasan_pdata,
1132 .clk_ops = &arasan_clk_ops,
1135 static const struct sdhci_pltfm_data sdhci_keembay_emmc_pdata = {
1136 .ops = &sdhci_arasan_cqe_ops,
1137 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1138 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1139 SDHCI_QUIRK_NO_LED |
1140 SDHCI_QUIRK_32BIT_DMA_ADDR |
1141 SDHCI_QUIRK_32BIT_DMA_SIZE |
1142 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1143 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1144 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1145 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
1146 SDHCI_QUIRK2_STOP_WITH_TC |
1147 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1150 static const struct sdhci_pltfm_data sdhci_keembay_sd_pdata = {
1151 .ops = &sdhci_arasan_ops,
1152 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1153 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1154 SDHCI_QUIRK_NO_LED |
1155 SDHCI_QUIRK_32BIT_DMA_ADDR |
1156 SDHCI_QUIRK_32BIT_DMA_SIZE |
1157 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1158 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1159 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1160 SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
1161 SDHCI_QUIRK2_STOP_WITH_TC |
1162 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1165 static const struct sdhci_pltfm_data sdhci_keembay_sdio_pdata = {
1166 .ops = &sdhci_arasan_ops,
1167 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1168 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1169 SDHCI_QUIRK_NO_LED |
1170 SDHCI_QUIRK_32BIT_DMA_ADDR |
1171 SDHCI_QUIRK_32BIT_DMA_SIZE |
1172 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1173 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1174 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1175 SDHCI_QUIRK2_HOST_OFF_CARD_ON |
1176 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1179 static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = {
1180 .soc_ctl_map = &rk3399_soc_ctl_map,
1181 .pdata = &sdhci_arasan_cqe_pdata,
1182 .clk_ops = &arasan_clk_ops,
1185 static struct sdhci_arasan_of_data intel_lgm_emmc_data = {
1186 .soc_ctl_map = &intel_lgm_emmc_soc_ctl_map,
1187 .pdata = &sdhci_arasan_cqe_pdata,
1188 .clk_ops = &arasan_clk_ops,
1191 static struct sdhci_arasan_of_data intel_lgm_sdxc_data = {
1192 .soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map,
1193 .pdata = &sdhci_arasan_cqe_pdata,
1194 .clk_ops = &arasan_clk_ops,
1197 static const struct sdhci_pltfm_data sdhci_arasan_zynqmp_pdata = {
1198 .ops = &sdhci_arasan_ops,
1199 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1200 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1201 SDHCI_QUIRK2_STOP_WITH_TC,
1204 static const struct sdhci_arasan_clk_ops zynqmp_clk_ops = {
1205 .sdcardclk_ops = &zynqmp_sdcardclk_ops,
1206 .sampleclk_ops = &zynqmp_sampleclk_ops,
1209 static struct sdhci_arasan_of_data sdhci_arasan_zynqmp_data = {
1210 .pdata = &sdhci_arasan_zynqmp_pdata,
1211 .clk_ops = &zynqmp_clk_ops,
1214 static const struct sdhci_arasan_clk_ops versal_clk_ops = {
1215 .sdcardclk_ops = &versal_sdcardclk_ops,
1216 .sampleclk_ops = &versal_sampleclk_ops,
1219 static struct sdhci_arasan_of_data sdhci_arasan_versal_data = {
1220 .pdata = &sdhci_arasan_zynqmp_pdata,
1221 .clk_ops = &versal_clk_ops,
1224 static struct sdhci_arasan_of_data intel_keembay_emmc_data = {
1225 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1226 .pdata = &sdhci_keembay_emmc_pdata,
1227 .clk_ops = &arasan_clk_ops,
1230 static struct sdhci_arasan_of_data intel_keembay_sd_data = {
1231 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1232 .pdata = &sdhci_keembay_sd_pdata,
1233 .clk_ops = &arasan_clk_ops,
1236 static struct sdhci_arasan_of_data intel_keembay_sdio_data = {
1237 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1238 .pdata = &sdhci_keembay_sdio_pdata,
1239 .clk_ops = &arasan_clk_ops,
1242 static const struct of_device_id sdhci_arasan_of_match[] = {
1243 /* SoC-specific compatible strings w/ soc_ctl_map */
1245 .compatible = "rockchip,rk3399-sdhci-5.1",
1246 .data = &sdhci_arasan_rk3399_data,
1249 .compatible = "intel,lgm-sdhci-5.1-emmc",
1250 .data = &intel_lgm_emmc_data,
1253 .compatible = "intel,lgm-sdhci-5.1-sdxc",
1254 .data = &intel_lgm_sdxc_data,
1257 .compatible = "intel,keembay-sdhci-5.1-emmc",
1258 .data = &intel_keembay_emmc_data,
1261 .compatible = "intel,keembay-sdhci-5.1-sd",
1262 .data = &intel_keembay_sd_data,
1265 .compatible = "intel,keembay-sdhci-5.1-sdio",
1266 .data = &intel_keembay_sdio_data,
1268 /* Generic compatible below here */
1270 .compatible = "arasan,sdhci-8.9a",
1271 .data = &sdhci_arasan_generic_data,
1274 .compatible = "arasan,sdhci-5.1",
1275 .data = &sdhci_arasan_generic_data,
1278 .compatible = "arasan,sdhci-4.9a",
1279 .data = &sdhci_arasan_generic_data,
1282 .compatible = "xlnx,zynqmp-8.9a",
1283 .data = &sdhci_arasan_zynqmp_data,
1286 .compatible = "xlnx,versal-8.9a",
1287 .data = &sdhci_arasan_versal_data,
1291 MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
1294 * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use
1296 * @sdhci_arasan: Our private data structure.
1297 * @clk_xin: Pointer to the functional clock
1298 * @dev: Pointer to our struct device.
1300 * Some PHY devices need to know what the actual card clock is. In order for
1301 * them to find out, we'll provide a clock through the common clock framework
1302 * for them to query.
1304 * Return: 0 on success and error value on error
1307 sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan,
1308 struct clk *clk_xin,
1311 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1312 struct device_node *np = dev->of_node;
1313 struct clk_init_data sdcardclk_init;
1314 const char *parent_clk_name;
1317 ret = of_property_read_string_index(np, "clock-output-names", 0,
1318 &sdcardclk_init.name);
1320 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1324 parent_clk_name = __clk_get_name(clk_xin);
1325 sdcardclk_init.parent_names = &parent_clk_name;
1326 sdcardclk_init.num_parents = 1;
1327 sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
1328 sdcardclk_init.ops = sdhci_arasan->clk_ops->sdcardclk_ops;
1330 clk_data->sdcardclk_hw.init = &sdcardclk_init;
1331 clk_data->sdcardclk =
1332 devm_clk_register(dev, &clk_data->sdcardclk_hw);
1333 if (IS_ERR(clk_data->sdcardclk))
1334 return PTR_ERR(clk_data->sdcardclk);
1335 clk_data->sdcardclk_hw.init = NULL;
1337 ret = of_clk_add_provider(np, of_clk_src_simple_get,
1338 clk_data->sdcardclk);
1340 dev_err(dev, "Failed to add sdcard clock provider\n");
1346 * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use
1348 * @sdhci_arasan: Our private data structure.
1349 * @clk_xin: Pointer to the functional clock
1350 * @dev: Pointer to our struct device.
1352 * Some PHY devices need to know what the actual card clock is. In order for
1353 * them to find out, we'll provide a clock through the common clock framework
1354 * for them to query.
1356 * Return: 0 on success and error value on error
1359 sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan,
1360 struct clk *clk_xin,
1363 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1364 struct device_node *np = dev->of_node;
1365 struct clk_init_data sampleclk_init;
1366 const char *parent_clk_name;
1369 ret = of_property_read_string_index(np, "clock-output-names", 1,
1370 &sampleclk_init.name);
1372 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1376 parent_clk_name = __clk_get_name(clk_xin);
1377 sampleclk_init.parent_names = &parent_clk_name;
1378 sampleclk_init.num_parents = 1;
1379 sampleclk_init.flags = CLK_GET_RATE_NOCACHE;
1380 sampleclk_init.ops = sdhci_arasan->clk_ops->sampleclk_ops;
1382 clk_data->sampleclk_hw.init = &sampleclk_init;
1383 clk_data->sampleclk =
1384 devm_clk_register(dev, &clk_data->sampleclk_hw);
1385 if (IS_ERR(clk_data->sampleclk))
1386 return PTR_ERR(clk_data->sampleclk);
1387 clk_data->sampleclk_hw.init = NULL;
1389 ret = of_clk_add_provider(np, of_clk_src_simple_get,
1390 clk_data->sampleclk);
1392 dev_err(dev, "Failed to add sample clock provider\n");
1398 * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
1400 * @dev: Pointer to our struct device.
1402 * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
1405 static void sdhci_arasan_unregister_sdclk(struct device *dev)
1407 struct device_node *np = dev->of_node;
1409 if (!of_find_property(np, "#clock-cells", NULL))
1412 of_clk_del_provider(dev->of_node);
1416 * sdhci_arasan_update_support64b - Set SUPPORT_64B (64-bit System Bus Support)
1417 * @host: The sdhci_host
1418 * @value: The value to write
1420 * This should be set based on the System Address Bus.
1421 * 0: the Core supports only 32-bit System Address Bus.
1422 * 1: the Core supports 64-bit System Address Bus.
1425 * For Keem Bay, it is required to clear this bit. Its default value is 1'b1.
1426 * Keem Bay does not support 64-bit access.
1428 static void sdhci_arasan_update_support64b(struct sdhci_host *host, u32 value)
1430 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1431 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1432 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
1434 /* Having a map is optional */
1435 soc_ctl_map = sdhci_arasan->soc_ctl_map;
1439 /* If we have a map, we expect to have a syscon */
1440 if (!sdhci_arasan->soc_ctl_base) {
1441 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1442 mmc_hostname(host->mmc));
1446 sdhci_arasan_syscon_write(host, &soc_ctl_map->support64b, value);
1450 * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use
1452 * @sdhci_arasan: Our private data structure.
1453 * @clk_xin: Pointer to the functional clock
1454 * @dev: Pointer to our struct device.
1456 * Some PHY devices need to know what the actual card clock is. In order for
1457 * them to find out, we'll provide a clock through the common clock framework
1458 * for them to query.
1460 * Note: without seriously re-architecting SDHCI's clock code and testing on
1461 * all platforms, there's no way to create a totally beautiful clock here
1462 * with all clock ops implemented. Instead, we'll just create a clock that can
1463 * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
1464 * framework that we're doing things behind its back. This should be sufficient
1465 * to create nice clean device tree bindings and later (if needed) we can try
1466 * re-architecting SDHCI if we see some benefit to it.
1468 * Return: 0 on success and error value on error
1470 static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
1471 struct clk *clk_xin,
1474 struct device_node *np = dev->of_node;
1478 /* Providing a clock to the PHY is optional; no error if missing */
1479 if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0)
1482 ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev);
1487 ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin,
1490 sdhci_arasan_unregister_sdclk(dev);
1498 static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
1500 struct sdhci_host *host = sdhci_arasan->host;
1501 struct cqhci_host *cq_host;
1505 if (!sdhci_arasan->has_cqe)
1506 return sdhci_add_host(host);
1508 ret = sdhci_setup_host(host);
1512 cq_host = devm_kzalloc(host->mmc->parent,
1513 sizeof(*cq_host), GFP_KERNEL);
1519 cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
1520 cq_host->ops = &sdhci_arasan_cqhci_ops;
1522 dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1524 cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1526 ret = cqhci_init(cq_host, host->mmc, dma64);
1530 ret = __sdhci_add_host(host);
1537 sdhci_cleanup_host(host);
1541 static int sdhci_arasan_probe(struct platform_device *pdev)
1544 struct device_node *node;
1545 struct clk *clk_xin;
1546 struct sdhci_host *host;
1547 struct sdhci_pltfm_host *pltfm_host;
1548 struct device *dev = &pdev->dev;
1549 struct device_node *np = dev->of_node;
1550 struct sdhci_arasan_data *sdhci_arasan;
1551 const struct sdhci_arasan_of_data *data;
1553 data = of_device_get_match_data(dev);
1554 host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan));
1557 return PTR_ERR(host);
1559 pltfm_host = sdhci_priv(host);
1560 sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1561 sdhci_arasan->host = host;
1563 sdhci_arasan->soc_ctl_map = data->soc_ctl_map;
1564 sdhci_arasan->clk_ops = data->clk_ops;
1566 node = of_parse_phandle(np, "arasan,soc-ctl-syscon", 0);
1568 sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
1571 if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
1572 ret = dev_err_probe(dev,
1573 PTR_ERR(sdhci_arasan->soc_ctl_base),
1574 "Can't get syscon\n");
1575 goto err_pltfm_free;
1579 sdhci_get_of_property(pdev);
1581 sdhci_arasan->clk_ahb = devm_clk_get(dev, "clk_ahb");
1582 if (IS_ERR(sdhci_arasan->clk_ahb)) {
1583 ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->clk_ahb),
1584 "clk_ahb clock not found.\n");
1585 goto err_pltfm_free;
1588 clk_xin = devm_clk_get(dev, "clk_xin");
1589 if (IS_ERR(clk_xin)) {
1590 ret = dev_err_probe(dev, PTR_ERR(clk_xin), "clk_xin clock not found.\n");
1591 goto err_pltfm_free;
1594 ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
1596 dev_err(dev, "Unable to enable AHB clock.\n");
1597 goto err_pltfm_free;
1600 /* If clock-frequency property is set, use the provided value */
1601 if (pltfm_host->clock &&
1602 pltfm_host->clock != clk_get_rate(clk_xin)) {
1603 ret = clk_set_rate(clk_xin, pltfm_host->clock);
1605 dev_err(&pdev->dev, "Failed to set SD clock rate\n");
1610 ret = clk_prepare_enable(clk_xin);
1612 dev_err(dev, "Unable to enable SD clock.\n");
1616 if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
1617 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
1619 if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
1620 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
1622 pltfm_host->clk = clk_xin;
1624 if (of_device_is_compatible(np, "rockchip,rk3399-sdhci-5.1"))
1625 sdhci_arasan_update_clockmultiplier(host, 0x0);
1627 if (of_device_is_compatible(np, "intel,keembay-sdhci-5.1-emmc") ||
1628 of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sd") ||
1629 of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sdio")) {
1630 sdhci_arasan_update_clockmultiplier(host, 0x0);
1631 sdhci_arasan_update_support64b(host, 0x0);
1633 host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1636 sdhci_arasan_update_baseclkfreq(host);
1638 ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, dev);
1640 goto clk_disable_all;
1642 if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
1643 host->mmc_host_ops.execute_tuning =
1644 arasan_zynqmp_execute_tuning;
1646 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN;
1647 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
1650 arasan_dt_parse_clk_phases(dev, &sdhci_arasan->clk_data);
1652 ret = mmc_of_parse(host->mmc);
1654 ret = dev_err_probe(dev, ret, "parsing dt failed.\n");
1658 sdhci_arasan->phy = ERR_PTR(-ENODEV);
1659 if (of_device_is_compatible(np, "arasan,sdhci-5.1")) {
1660 sdhci_arasan->phy = devm_phy_get(dev, "phy_arasan");
1661 if (IS_ERR(sdhci_arasan->phy)) {
1662 ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->phy),
1663 "No phy for arasan,sdhci-5.1.\n");
1667 ret = phy_init(sdhci_arasan->phy);
1669 dev_err(dev, "phy_init err.\n");
1673 host->mmc_host_ops.hs400_enhanced_strobe =
1674 sdhci_arasan_hs400_enhanced_strobe;
1675 host->mmc_host_ops.start_signal_voltage_switch =
1676 sdhci_arasan_voltage_switch;
1677 sdhci_arasan->has_cqe = true;
1678 host->mmc->caps2 |= MMC_CAP2_CQE;
1680 if (!of_property_read_bool(np, "disable-cqe-dcmd"))
1681 host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
1684 ret = sdhci_arasan_add_host(sdhci_arasan);
1691 if (!IS_ERR(sdhci_arasan->phy))
1692 phy_exit(sdhci_arasan->phy);
1694 sdhci_arasan_unregister_sdclk(dev);
1696 clk_disable_unprepare(clk_xin);
1698 clk_disable_unprepare(sdhci_arasan->clk_ahb);
1700 sdhci_pltfm_free(pdev);
1704 static int sdhci_arasan_remove(struct platform_device *pdev)
1707 struct sdhci_host *host = platform_get_drvdata(pdev);
1708 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1709 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1710 struct clk *clk_ahb = sdhci_arasan->clk_ahb;
1712 if (!IS_ERR(sdhci_arasan->phy)) {
1713 if (sdhci_arasan->is_phy_on)
1714 phy_power_off(sdhci_arasan->phy);
1715 phy_exit(sdhci_arasan->phy);
1718 sdhci_arasan_unregister_sdclk(&pdev->dev);
1720 ret = sdhci_pltfm_unregister(pdev);
1722 clk_disable_unprepare(clk_ahb);
1727 static struct platform_driver sdhci_arasan_driver = {
1729 .name = "sdhci-arasan",
1730 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1731 .of_match_table = sdhci_arasan_of_match,
1732 .pm = &sdhci_arasan_dev_pm_ops,
1734 .probe = sdhci_arasan_probe,
1735 .remove = sdhci_arasan_remove,
1738 module_platform_driver(sdhci_arasan_driver);
1740 MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
1741 MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
1742 MODULE_LICENSE("GPL");