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 */
659 tap_delay = (degrees * tap_max) / 360;
661 /* Set the Clock Phase */
662 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_OUTPUT, tap_delay);
664 pr_err("Error setting Output Tap Delay\n");
666 /* Release DLL Reset */
667 zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_RELEASE);
672 static const struct clk_ops zynqmp_sdcardclk_ops = {
673 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
674 .set_phase = sdhci_zynqmp_sdcardclk_set_phase,
678 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
680 * @hw: Pointer to the hardware clock structure.
681 * @degrees: The clock phase shift between 0 - 359.
683 * Set the SD Input Clock Tap Delays for Input path
685 * Return: 0 on success and error value on error
687 static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees)
689 struct sdhci_arasan_clk_data *clk_data =
690 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
691 struct sdhci_arasan_data *sdhci_arasan =
692 container_of(clk_data, struct sdhci_arasan_data, clk_data);
693 struct sdhci_host *host = sdhci_arasan->host;
694 const char *clk_name = clk_hw_get_name(hw);
695 u32 node_id = !strcmp(clk_name, "clk_in_sd0") ? NODE_SD_0 : NODE_SD_1;
696 u8 tap_delay, tap_max = 0;
699 /* This is applicable for SDHCI_SPEC_300 and above */
700 if (host->version < SDHCI_SPEC_300)
703 /* Assert DLL Reset */
704 zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_ASSERT);
706 switch (host->timing) {
707 case MMC_TIMING_MMC_HS:
708 case MMC_TIMING_SD_HS:
709 case MMC_TIMING_UHS_SDR25:
710 case MMC_TIMING_UHS_DDR50:
711 case MMC_TIMING_MMC_DDR52:
712 /* For 50MHz clock, 120 Taps are available */
715 case MMC_TIMING_UHS_SDR50:
716 /* For 100MHz clock, 60 Taps are available */
719 case MMC_TIMING_UHS_SDR104:
720 case MMC_TIMING_MMC_HS200:
721 /* For 200MHz clock, 30 Taps are available */
727 tap_delay = (degrees * tap_max) / 360;
729 /* Set the Clock Phase */
730 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_INPUT, tap_delay);
732 pr_err("Error setting Input Tap Delay\n");
737 static const struct clk_ops zynqmp_sampleclk_ops = {
738 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
739 .set_phase = sdhci_zynqmp_sampleclk_set_phase,
743 * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
745 * @hw: Pointer to the hardware clock structure.
746 * @degrees: The clock phase shift between 0 - 359.
748 * Set the SD Output Clock Tap Delays for Output path
750 * Return: 0 on success and error value on error
752 static int sdhci_versal_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
754 struct sdhci_arasan_clk_data *clk_data =
755 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
756 struct sdhci_arasan_data *sdhci_arasan =
757 container_of(clk_data, struct sdhci_arasan_data, clk_data);
758 struct sdhci_host *host = sdhci_arasan->host;
759 u8 tap_delay, tap_max = 0;
761 /* This is applicable for SDHCI_SPEC_300 and above */
762 if (host->version < SDHCI_SPEC_300)
765 switch (host->timing) {
766 case MMC_TIMING_MMC_HS:
767 case MMC_TIMING_SD_HS:
768 case MMC_TIMING_UHS_SDR25:
769 case MMC_TIMING_UHS_DDR50:
770 case MMC_TIMING_MMC_DDR52:
771 /* For 50MHz clock, 30 Taps are available */
774 case MMC_TIMING_UHS_SDR50:
775 /* For 100MHz clock, 15 Taps are available */
778 case MMC_TIMING_UHS_SDR104:
779 case MMC_TIMING_MMC_HS200:
780 /* For 200MHz clock, 8 Taps are available */
786 tap_delay = (degrees * tap_max) / 360;
788 /* Set the Clock Phase */
792 regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
793 regval |= SDHCI_OTAPDLY_ENABLE;
794 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
795 regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK;
797 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
803 static const struct clk_ops versal_sdcardclk_ops = {
804 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
805 .set_phase = sdhci_versal_sdcardclk_set_phase,
809 * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
811 * @hw: Pointer to the hardware clock structure.
812 * @degrees: The clock phase shift between 0 - 359.
814 * Set the SD Input Clock Tap Delays for Input path
816 * Return: 0 on success and error value on error
818 static int sdhci_versal_sampleclk_set_phase(struct clk_hw *hw, int degrees)
820 struct sdhci_arasan_clk_data *clk_data =
821 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
822 struct sdhci_arasan_data *sdhci_arasan =
823 container_of(clk_data, struct sdhci_arasan_data, clk_data);
824 struct sdhci_host *host = sdhci_arasan->host;
825 u8 tap_delay, tap_max = 0;
827 /* This is applicable for SDHCI_SPEC_300 and above */
828 if (host->version < SDHCI_SPEC_300)
831 switch (host->timing) {
832 case MMC_TIMING_MMC_HS:
833 case MMC_TIMING_SD_HS:
834 case MMC_TIMING_UHS_SDR25:
835 case MMC_TIMING_UHS_DDR50:
836 case MMC_TIMING_MMC_DDR52:
837 /* For 50MHz clock, 120 Taps are available */
840 case MMC_TIMING_UHS_SDR50:
841 /* For 100MHz clock, 60 Taps are available */
844 case MMC_TIMING_UHS_SDR104:
845 case MMC_TIMING_MMC_HS200:
846 /* For 200MHz clock, 30 Taps are available */
852 tap_delay = (degrees * tap_max) / 360;
854 /* Set the Clock Phase */
858 regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
859 regval |= SDHCI_ITAPDLY_CHGWIN;
860 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
861 regval |= SDHCI_ITAPDLY_ENABLE;
862 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
863 regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK;
865 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
866 regval &= ~SDHCI_ITAPDLY_CHGWIN;
867 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
873 static const struct clk_ops versal_sampleclk_ops = {
874 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
875 .set_phase = sdhci_versal_sampleclk_set_phase,
878 static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 deviceid)
882 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
883 clk &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN);
884 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
886 /* Issue DLL Reset */
887 zynqmp_pm_sd_dll_reset(deviceid, PM_DLL_RESET_PULSE);
889 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
891 sdhci_enable_clk(host, clk);
894 static int arasan_zynqmp_execute_tuning(struct mmc_host *mmc, u32 opcode)
896 struct sdhci_host *host = mmc_priv(mmc);
897 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
898 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
899 struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw;
900 const char *clk_name = clk_hw_get_name(hw);
901 u32 device_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 :
905 arasan_zynqmp_dll_reset(host, device_id);
907 err = sdhci_execute_tuning(mmc, opcode);
911 arasan_zynqmp_dll_reset(host, device_id);
917 * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
919 * @host: The sdhci_host
920 * @value: The value to write
922 * The corecfg_clockmultiplier is supposed to contain clock multiplier
923 * value of programmable clock generator.
926 * - Many existing devices don't seem to do this and work fine. To keep
927 * compatibility for old hardware where the device tree doesn't provide a
928 * register map, this function is a noop if a soc_ctl_map hasn't been provided
930 * - The value of corecfg_clockmultiplier should sync with that of corresponding
931 * value reading from sdhci_capability_register. So this function is called
932 * once at probe time and never called again.
934 static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
937 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
938 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
939 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
940 sdhci_arasan->soc_ctl_map;
942 /* Having a map is optional */
946 /* If we have a map, we expect to have a syscon */
947 if (!sdhci_arasan->soc_ctl_base) {
948 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
949 mmc_hostname(host->mmc));
953 sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
957 * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
959 * @host: The sdhci_host
961 * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This
962 * function can be used to make that happen.
965 * - Many existing devices don't seem to do this and work fine. To keep
966 * compatibility for old hardware where the device tree doesn't provide a
967 * register map, this function is a noop if a soc_ctl_map hasn't been provided
969 * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
970 * to achieve lower clock rates. That means that this function is called once
971 * at probe time and never called again.
973 static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
975 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
976 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
977 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
978 sdhci_arasan->soc_ctl_map;
979 u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000);
981 /* Having a map is optional */
985 /* If we have a map, we expect to have a syscon */
986 if (!sdhci_arasan->soc_ctl_base) {
987 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
988 mmc_hostname(host->mmc));
992 sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
995 static void sdhci_arasan_set_clk_delays(struct sdhci_host *host)
997 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
998 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
999 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1001 clk_set_phase(clk_data->sampleclk,
1002 clk_data->clk_phase_in[host->timing]);
1003 clk_set_phase(clk_data->sdcardclk,
1004 clk_data->clk_phase_out[host->timing]);
1007 static void arasan_dt_read_clk_phase(struct device *dev,
1008 struct sdhci_arasan_clk_data *clk_data,
1009 unsigned int timing, const char *prop)
1011 struct device_node *np = dev->of_node;
1013 int clk_phase[2] = {0};
1016 * Read Tap Delay values from DT, if the DT does not contain the
1017 * Tap Values then use the pre-defined values.
1019 if (of_property_read_variable_u32_array(np, prop, &clk_phase[0],
1021 dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
1022 prop, clk_data->clk_phase_in[timing],
1023 clk_data->clk_phase_out[timing]);
1027 /* The values read are Input and Output Clock Delays in order */
1028 clk_data->clk_phase_in[timing] = clk_phase[0];
1029 clk_data->clk_phase_out[timing] = clk_phase[1];
1033 * arasan_dt_parse_clk_phases - Read Clock Delay values from DT
1035 * @dev: Pointer to our struct device.
1036 * @clk_data: Pointer to the Clock Data structure
1038 * Called at initialization to parse the values of Clock Delays.
1040 static void arasan_dt_parse_clk_phases(struct device *dev,
1041 struct sdhci_arasan_clk_data *clk_data)
1047 * This has been kept as a pointer and is assigned a function here.
1048 * So that different controller variants can assign their own handling
1051 clk_data->set_clk_delays = sdhci_arasan_set_clk_delays;
1053 if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) {
1054 u32 zynqmp_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1056 u32 zynqmp_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1059 of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank);
1060 if (mio_bank == 2) {
1061 zynqmp_oclk_phase[MMC_TIMING_UHS_SDR104] = 90;
1062 zynqmp_oclk_phase[MMC_TIMING_MMC_HS200] = 90;
1065 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1066 clk_data->clk_phase_in[i] = zynqmp_iclk_phase[i];
1067 clk_data->clk_phase_out[i] = zynqmp_oclk_phase[i];
1071 if (of_device_is_compatible(dev->of_node, "xlnx,versal-8.9a")) {
1072 u32 versal_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1074 u32 versal_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1077 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1078 clk_data->clk_phase_in[i] = versal_iclk_phase[i];
1079 clk_data->clk_phase_out[i] = versal_oclk_phase[i];
1083 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY,
1084 "clk-phase-legacy");
1085 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS,
1086 "clk-phase-mmc-hs");
1087 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS,
1089 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12,
1090 "clk-phase-uhs-sdr12");
1091 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25,
1092 "clk-phase-uhs-sdr25");
1093 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50,
1094 "clk-phase-uhs-sdr50");
1095 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104,
1096 "clk-phase-uhs-sdr104");
1097 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50,
1098 "clk-phase-uhs-ddr50");
1099 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52,
1100 "clk-phase-mmc-ddr52");
1101 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200,
1102 "clk-phase-mmc-hs200");
1103 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400,
1104 "clk-phase-mmc-hs400");
1107 static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
1108 .ops = &sdhci_arasan_ops,
1109 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1110 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1111 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1112 SDHCI_QUIRK2_STOP_WITH_TC,
1115 static const struct sdhci_arasan_clk_ops arasan_clk_ops = {
1116 .sdcardclk_ops = &arasan_sdcardclk_ops,
1117 .sampleclk_ops = &arasan_sampleclk_ops,
1120 static struct sdhci_arasan_of_data sdhci_arasan_generic_data = {
1121 .pdata = &sdhci_arasan_pdata,
1122 .clk_ops = &arasan_clk_ops,
1125 static const struct sdhci_pltfm_data sdhci_keembay_emmc_pdata = {
1126 .ops = &sdhci_arasan_cqe_ops,
1127 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1128 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1129 SDHCI_QUIRK_NO_LED |
1130 SDHCI_QUIRK_32BIT_DMA_ADDR |
1131 SDHCI_QUIRK_32BIT_DMA_SIZE |
1132 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1133 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1134 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1135 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
1136 SDHCI_QUIRK2_STOP_WITH_TC |
1137 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1140 static const struct sdhci_pltfm_data sdhci_keembay_sd_pdata = {
1141 .ops = &sdhci_arasan_ops,
1142 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1143 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1144 SDHCI_QUIRK_NO_LED |
1145 SDHCI_QUIRK_32BIT_DMA_ADDR |
1146 SDHCI_QUIRK_32BIT_DMA_SIZE |
1147 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1148 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1149 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1150 SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
1151 SDHCI_QUIRK2_STOP_WITH_TC |
1152 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1155 static const struct sdhci_pltfm_data sdhci_keembay_sdio_pdata = {
1156 .ops = &sdhci_arasan_ops,
1157 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1158 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1159 SDHCI_QUIRK_NO_LED |
1160 SDHCI_QUIRK_32BIT_DMA_ADDR |
1161 SDHCI_QUIRK_32BIT_DMA_SIZE |
1162 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1163 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1164 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1165 SDHCI_QUIRK2_HOST_OFF_CARD_ON |
1166 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1169 static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = {
1170 .soc_ctl_map = &rk3399_soc_ctl_map,
1171 .pdata = &sdhci_arasan_cqe_pdata,
1172 .clk_ops = &arasan_clk_ops,
1175 static struct sdhci_arasan_of_data intel_lgm_emmc_data = {
1176 .soc_ctl_map = &intel_lgm_emmc_soc_ctl_map,
1177 .pdata = &sdhci_arasan_cqe_pdata,
1178 .clk_ops = &arasan_clk_ops,
1181 static struct sdhci_arasan_of_data intel_lgm_sdxc_data = {
1182 .soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map,
1183 .pdata = &sdhci_arasan_cqe_pdata,
1184 .clk_ops = &arasan_clk_ops,
1187 static const struct sdhci_pltfm_data sdhci_arasan_zynqmp_pdata = {
1188 .ops = &sdhci_arasan_ops,
1189 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1190 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1191 SDHCI_QUIRK2_STOP_WITH_TC,
1194 static const struct sdhci_arasan_clk_ops zynqmp_clk_ops = {
1195 .sdcardclk_ops = &zynqmp_sdcardclk_ops,
1196 .sampleclk_ops = &zynqmp_sampleclk_ops,
1199 static struct sdhci_arasan_of_data sdhci_arasan_zynqmp_data = {
1200 .pdata = &sdhci_arasan_zynqmp_pdata,
1201 .clk_ops = &zynqmp_clk_ops,
1204 static const struct sdhci_arasan_clk_ops versal_clk_ops = {
1205 .sdcardclk_ops = &versal_sdcardclk_ops,
1206 .sampleclk_ops = &versal_sampleclk_ops,
1209 static struct sdhci_arasan_of_data sdhci_arasan_versal_data = {
1210 .pdata = &sdhci_arasan_zynqmp_pdata,
1211 .clk_ops = &versal_clk_ops,
1214 static struct sdhci_arasan_of_data intel_keembay_emmc_data = {
1215 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1216 .pdata = &sdhci_keembay_emmc_pdata,
1217 .clk_ops = &arasan_clk_ops,
1220 static struct sdhci_arasan_of_data intel_keembay_sd_data = {
1221 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1222 .pdata = &sdhci_keembay_sd_pdata,
1223 .clk_ops = &arasan_clk_ops,
1226 static struct sdhci_arasan_of_data intel_keembay_sdio_data = {
1227 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1228 .pdata = &sdhci_keembay_sdio_pdata,
1229 .clk_ops = &arasan_clk_ops,
1232 static const struct of_device_id sdhci_arasan_of_match[] = {
1233 /* SoC-specific compatible strings w/ soc_ctl_map */
1235 .compatible = "rockchip,rk3399-sdhci-5.1",
1236 .data = &sdhci_arasan_rk3399_data,
1239 .compatible = "intel,lgm-sdhci-5.1-emmc",
1240 .data = &intel_lgm_emmc_data,
1243 .compatible = "intel,lgm-sdhci-5.1-sdxc",
1244 .data = &intel_lgm_sdxc_data,
1247 .compatible = "intel,keembay-sdhci-5.1-emmc",
1248 .data = &intel_keembay_emmc_data,
1251 .compatible = "intel,keembay-sdhci-5.1-sd",
1252 .data = &intel_keembay_sd_data,
1255 .compatible = "intel,keembay-sdhci-5.1-sdio",
1256 .data = &intel_keembay_sdio_data,
1258 /* Generic compatible below here */
1260 .compatible = "arasan,sdhci-8.9a",
1261 .data = &sdhci_arasan_generic_data,
1264 .compatible = "arasan,sdhci-5.1",
1265 .data = &sdhci_arasan_generic_data,
1268 .compatible = "arasan,sdhci-4.9a",
1269 .data = &sdhci_arasan_generic_data,
1272 .compatible = "xlnx,zynqmp-8.9a",
1273 .data = &sdhci_arasan_zynqmp_data,
1276 .compatible = "xlnx,versal-8.9a",
1277 .data = &sdhci_arasan_versal_data,
1281 MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
1284 * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use
1286 * @sdhci_arasan: Our private data structure.
1287 * @clk_xin: Pointer to the functional clock
1288 * @dev: Pointer to our struct device.
1290 * Some PHY devices need to know what the actual card clock is. In order for
1291 * them to find out, we'll provide a clock through the common clock framework
1292 * for them to query.
1294 * Return: 0 on success and error value on error
1297 sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan,
1298 struct clk *clk_xin,
1301 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1302 struct device_node *np = dev->of_node;
1303 struct clk_init_data sdcardclk_init;
1304 const char *parent_clk_name;
1307 ret = of_property_read_string_index(np, "clock-output-names", 0,
1308 &sdcardclk_init.name);
1310 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1314 parent_clk_name = __clk_get_name(clk_xin);
1315 sdcardclk_init.parent_names = &parent_clk_name;
1316 sdcardclk_init.num_parents = 1;
1317 sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
1318 sdcardclk_init.ops = sdhci_arasan->clk_ops->sdcardclk_ops;
1320 clk_data->sdcardclk_hw.init = &sdcardclk_init;
1321 clk_data->sdcardclk =
1322 devm_clk_register(dev, &clk_data->sdcardclk_hw);
1323 if (IS_ERR(clk_data->sdcardclk))
1324 return PTR_ERR(clk_data->sdcardclk);
1325 clk_data->sdcardclk_hw.init = NULL;
1327 ret = of_clk_add_provider(np, of_clk_src_simple_get,
1328 clk_data->sdcardclk);
1330 dev_err(dev, "Failed to add sdcard clock provider\n");
1336 * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use
1338 * @sdhci_arasan: Our private data structure.
1339 * @clk_xin: Pointer to the functional clock
1340 * @dev: Pointer to our struct device.
1342 * Some PHY devices need to know what the actual card clock is. In order for
1343 * them to find out, we'll provide a clock through the common clock framework
1344 * for them to query.
1346 * Return: 0 on success and error value on error
1349 sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan,
1350 struct clk *clk_xin,
1353 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1354 struct device_node *np = dev->of_node;
1355 struct clk_init_data sampleclk_init;
1356 const char *parent_clk_name;
1359 ret = of_property_read_string_index(np, "clock-output-names", 1,
1360 &sampleclk_init.name);
1362 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1366 parent_clk_name = __clk_get_name(clk_xin);
1367 sampleclk_init.parent_names = &parent_clk_name;
1368 sampleclk_init.num_parents = 1;
1369 sampleclk_init.flags = CLK_GET_RATE_NOCACHE;
1370 sampleclk_init.ops = sdhci_arasan->clk_ops->sampleclk_ops;
1372 clk_data->sampleclk_hw.init = &sampleclk_init;
1373 clk_data->sampleclk =
1374 devm_clk_register(dev, &clk_data->sampleclk_hw);
1375 if (IS_ERR(clk_data->sampleclk))
1376 return PTR_ERR(clk_data->sampleclk);
1377 clk_data->sampleclk_hw.init = NULL;
1379 ret = of_clk_add_provider(np, of_clk_src_simple_get,
1380 clk_data->sampleclk);
1382 dev_err(dev, "Failed to add sample clock provider\n");
1388 * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
1390 * @dev: Pointer to our struct device.
1392 * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
1395 static void sdhci_arasan_unregister_sdclk(struct device *dev)
1397 struct device_node *np = dev->of_node;
1399 if (!of_find_property(np, "#clock-cells", NULL))
1402 of_clk_del_provider(dev->of_node);
1406 * sdhci_arasan_update_support64b - Set SUPPORT_64B (64-bit System Bus Support)
1408 * This should be set based on the System Address Bus.
1409 * 0: the Core supports only 32-bit System Address Bus.
1410 * 1: the Core supports 64-bit System Address Bus.
1413 * - For Keem Bay, it is required to clear this bit. Its default value is 1'b1.
1414 * Keem Bay does not support 64-bit access.
1416 * @host: The sdhci_host
1417 * @value: The value to write
1419 static void sdhci_arasan_update_support64b(struct sdhci_host *host, u32 value)
1421 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1422 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1423 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
1424 sdhci_arasan->soc_ctl_map;
1426 /* Having a map is optional */
1430 /* If we have a map, we expect to have a syscon */
1431 if (!sdhci_arasan->soc_ctl_base) {
1432 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1433 mmc_hostname(host->mmc));
1437 sdhci_arasan_syscon_write(host, &soc_ctl_map->support64b, value);
1441 * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use
1443 * @sdhci_arasan: Our private data structure.
1444 * @clk_xin: Pointer to the functional clock
1445 * @dev: Pointer to our struct device.
1447 * Some PHY devices need to know what the actual card clock is. In order for
1448 * them to find out, we'll provide a clock through the common clock framework
1449 * for them to query.
1451 * Note: without seriously re-architecting SDHCI's clock code and testing on
1452 * all platforms, there's no way to create a totally beautiful clock here
1453 * with all clock ops implemented. Instead, we'll just create a clock that can
1454 * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
1455 * framework that we're doing things behind its back. This should be sufficient
1456 * to create nice clean device tree bindings and later (if needed) we can try
1457 * re-architecting SDHCI if we see some benefit to it.
1459 * Return: 0 on success and error value on error
1461 static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
1462 struct clk *clk_xin,
1465 struct device_node *np = dev->of_node;
1469 /* Providing a clock to the PHY is optional; no error if missing */
1470 if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0)
1473 ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev);
1478 ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin,
1481 sdhci_arasan_unregister_sdclk(dev);
1489 static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
1491 struct sdhci_host *host = sdhci_arasan->host;
1492 struct cqhci_host *cq_host;
1496 if (!sdhci_arasan->has_cqe)
1497 return sdhci_add_host(host);
1499 ret = sdhci_setup_host(host);
1503 cq_host = devm_kzalloc(host->mmc->parent,
1504 sizeof(*cq_host), GFP_KERNEL);
1510 cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
1511 cq_host->ops = &sdhci_arasan_cqhci_ops;
1513 dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1515 cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1517 ret = cqhci_init(cq_host, host->mmc, dma64);
1521 ret = __sdhci_add_host(host);
1528 sdhci_cleanup_host(host);
1532 static int sdhci_arasan_probe(struct platform_device *pdev)
1535 const struct of_device_id *match;
1536 struct device_node *node;
1537 struct clk *clk_xin;
1538 struct sdhci_host *host;
1539 struct sdhci_pltfm_host *pltfm_host;
1540 struct sdhci_arasan_data *sdhci_arasan;
1541 struct device_node *np = pdev->dev.of_node;
1542 const struct sdhci_arasan_of_data *data;
1544 match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node);
1546 host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan));
1549 return PTR_ERR(host);
1551 pltfm_host = sdhci_priv(host);
1552 sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1553 sdhci_arasan->host = host;
1555 sdhci_arasan->soc_ctl_map = data->soc_ctl_map;
1556 sdhci_arasan->clk_ops = data->clk_ops;
1558 node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0);
1560 sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
1563 if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
1564 ret = dev_err_probe(&pdev->dev,
1565 PTR_ERR(sdhci_arasan->soc_ctl_base),
1566 "Can't get syscon\n");
1567 goto err_pltfm_free;
1571 sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb");
1572 if (IS_ERR(sdhci_arasan->clk_ahb)) {
1573 dev_err(&pdev->dev, "clk_ahb clock not found.\n");
1574 ret = PTR_ERR(sdhci_arasan->clk_ahb);
1575 goto err_pltfm_free;
1578 clk_xin = devm_clk_get(&pdev->dev, "clk_xin");
1579 if (IS_ERR(clk_xin)) {
1580 dev_err(&pdev->dev, "clk_xin clock not found.\n");
1581 ret = PTR_ERR(clk_xin);
1582 goto err_pltfm_free;
1585 ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
1587 dev_err(&pdev->dev, "Unable to enable AHB clock.\n");
1588 goto err_pltfm_free;
1591 ret = clk_prepare_enable(clk_xin);
1593 dev_err(&pdev->dev, "Unable to enable SD clock.\n");
1597 sdhci_get_of_property(pdev);
1599 if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
1600 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
1602 if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
1603 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
1605 pltfm_host->clk = clk_xin;
1607 if (of_device_is_compatible(pdev->dev.of_node,
1608 "rockchip,rk3399-sdhci-5.1"))
1609 sdhci_arasan_update_clockmultiplier(host, 0x0);
1611 if (of_device_is_compatible(np, "intel,keembay-sdhci-5.1-emmc") ||
1612 of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sd") ||
1613 of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sdio")) {
1614 sdhci_arasan_update_clockmultiplier(host, 0x0);
1615 sdhci_arasan_update_support64b(host, 0x0);
1617 host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1620 sdhci_arasan_update_baseclkfreq(host);
1622 ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev);
1624 goto clk_disable_all;
1626 if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
1627 host->mmc_host_ops.execute_tuning =
1628 arasan_zynqmp_execute_tuning;
1630 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN;
1633 arasan_dt_parse_clk_phases(&pdev->dev, &sdhci_arasan->clk_data);
1635 ret = mmc_of_parse(host->mmc);
1637 if (ret != -EPROBE_DEFER)
1638 dev_err(&pdev->dev, "parsing dt failed (%d)\n", ret);
1642 sdhci_arasan->phy = ERR_PTR(-ENODEV);
1643 if (of_device_is_compatible(pdev->dev.of_node,
1644 "arasan,sdhci-5.1")) {
1645 sdhci_arasan->phy = devm_phy_get(&pdev->dev,
1647 if (IS_ERR(sdhci_arasan->phy)) {
1648 ret = PTR_ERR(sdhci_arasan->phy);
1649 dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n");
1653 ret = phy_init(sdhci_arasan->phy);
1655 dev_err(&pdev->dev, "phy_init err.\n");
1659 host->mmc_host_ops.hs400_enhanced_strobe =
1660 sdhci_arasan_hs400_enhanced_strobe;
1661 host->mmc_host_ops.start_signal_voltage_switch =
1662 sdhci_arasan_voltage_switch;
1663 sdhci_arasan->has_cqe = true;
1664 host->mmc->caps2 |= MMC_CAP2_CQE;
1666 if (!of_property_read_bool(np, "disable-cqe-dcmd"))
1667 host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
1670 ret = sdhci_arasan_add_host(sdhci_arasan);
1677 if (!IS_ERR(sdhci_arasan->phy))
1678 phy_exit(sdhci_arasan->phy);
1680 sdhci_arasan_unregister_sdclk(&pdev->dev);
1682 clk_disable_unprepare(clk_xin);
1684 clk_disable_unprepare(sdhci_arasan->clk_ahb);
1686 sdhci_pltfm_free(pdev);
1690 static int sdhci_arasan_remove(struct platform_device *pdev)
1693 struct sdhci_host *host = platform_get_drvdata(pdev);
1694 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1695 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1696 struct clk *clk_ahb = sdhci_arasan->clk_ahb;
1698 if (!IS_ERR(sdhci_arasan->phy)) {
1699 if (sdhci_arasan->is_phy_on)
1700 phy_power_off(sdhci_arasan->phy);
1701 phy_exit(sdhci_arasan->phy);
1704 sdhci_arasan_unregister_sdclk(&pdev->dev);
1706 ret = sdhci_pltfm_unregister(pdev);
1708 clk_disable_unprepare(clk_ahb);
1713 static struct platform_driver sdhci_arasan_driver = {
1715 .name = "sdhci-arasan",
1716 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1717 .of_match_table = sdhci_arasan_of_match,
1718 .pm = &sdhci_arasan_dev_pm_ops,
1720 .probe = sdhci_arasan_probe,
1721 .remove = sdhci_arasan_remove,
1724 module_platform_driver(sdhci_arasan_driver);
1726 MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
1727 MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
1728 MODULE_LICENSE("GPL");