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
31 #define SDHCI_ARASAN_CQE_BASE_ADDR 0x200
32 #define VENDOR_ENHANCED_STROBE BIT(0)
34 #define PHY_CLK_TOO_SLOW_HZ 400000
36 /* Default settings for ZynqMP Clock Phases */
37 #define ZYNQMP_ICLK_PHASE {0, 63, 63, 0, 63, 0, 0, 183, 54, 0, 0}
38 #define ZYNQMP_OCLK_PHASE {0, 72, 60, 0, 60, 72, 135, 48, 72, 135, 0}
41 * On some SoCs the syscon area has a feature where the upper 16-bits of
42 * each 32-bit register act as a write mask for the lower 16-bits. This allows
43 * atomic updates of the register without locking. This macro is used on SoCs
44 * that have that feature.
46 #define HIWORD_UPDATE(val, mask, shift) \
47 ((val) << (shift) | (mask) << ((shift) + 16))
50 * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
52 * @reg: Offset within the syscon of the register containing this field
53 * @width: Number of bits for this field
54 * @shift: Bit offset within @reg of this field (or -1 if not avail)
56 struct sdhci_arasan_soc_ctl_field {
63 * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
65 * It's up to the licensee of the Arsan IP block to make these available
66 * somewhere if needed. Presumably these will be scattered somewhere that's
67 * accessible via the syscon API.
69 * @baseclkfreq: Where to find corecfg_baseclkfreq
70 * @clockmultiplier: Where to find corecfg_clockmultiplier
71 * @hiword_update: If true, use HIWORD_UPDATE to access the syscon
73 struct sdhci_arasan_soc_ctl_map {
74 struct sdhci_arasan_soc_ctl_field baseclkfreq;
75 struct sdhci_arasan_soc_ctl_field clockmultiplier;
80 * struct sdhci_arasan_clk_data
81 * @sdcardclk_hw: Struct for the clock we might provide to a PHY.
82 * @sdcardclk: Pointer to normal 'struct clock' for sdcardclk_hw.
83 * @sampleclk_hw: Struct for the clock we might provide to a PHY.
84 * @sampleclk: Pointer to normal 'struct clock' for sampleclk_hw.
85 * @clk_phase_in: Array of Input Clock Phase Delays for all speed modes
86 * @clk_phase_out: Array of Output Clock Phase Delays for all speed modes
87 * @set_clk_delays: Function pointer for setting Clock Delays
88 * @clk_of_data: Platform specific runtime clock data storage pointer
90 struct sdhci_arasan_clk_data {
91 struct clk_hw sdcardclk_hw;
92 struct clk *sdcardclk;
93 struct clk_hw sampleclk_hw;
94 struct clk *sampleclk;
95 int clk_phase_in[MMC_TIMING_MMC_HS400 + 1];
96 int clk_phase_out[MMC_TIMING_MMC_HS400 + 1];
97 void (*set_clk_delays)(struct sdhci_host *host);
102 * struct sdhci_arasan_data
103 * @host: Pointer to the main SDHCI host structure.
104 * @clk_ahb: Pointer to the AHB clock
105 * @phy: Pointer to the generic phy
106 * @is_phy_on: True if the PHY is on; false if not.
107 * @clk_data: Struct for the Arasan Controller Clock Data.
108 * @soc_ctl_base: Pointer to regmap for syscon for soc_ctl registers.
109 * @soc_ctl_map: Map to get offsets into soc_ctl registers.
111 struct sdhci_arasan_data {
112 struct sdhci_host *host;
118 struct sdhci_arasan_clk_data clk_data;
120 struct regmap *soc_ctl_base;
121 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
122 unsigned int quirks; /* Arasan deviations from spec */
124 /* Controller does not have CD wired and will not function normally without */
125 #define SDHCI_ARASAN_QUIRK_FORCE_CDTEST BIT(0)
126 /* Controller immediately reports SDHCI_CLOCK_INT_STABLE after enabling the
127 * internal clock even when the clock isn't stable */
128 #define SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE BIT(1)
131 struct sdhci_arasan_of_data {
132 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
133 const struct sdhci_pltfm_data *pdata;
136 static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
137 .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
138 .clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
139 .hiword_update = true,
142 static const struct sdhci_arasan_soc_ctl_map intel_lgm_emmc_soc_ctl_map = {
143 .baseclkfreq = { .reg = 0xa0, .width = 8, .shift = 2 },
144 .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
145 .hiword_update = false,
148 static const struct sdhci_arasan_soc_ctl_map intel_lgm_sdxc_soc_ctl_map = {
149 .baseclkfreq = { .reg = 0x80, .width = 8, .shift = 2 },
150 .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
151 .hiword_update = false,
155 * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
157 * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
158 * Note that if a field is specified as not available (shift < 0) then
159 * this function will silently return an error code. It will be noisy
160 * and print errors for any other (unexpected) errors.
162 * @host: The sdhci_host
163 * @fld: The field to write to
164 * @val: The value to write
166 static int sdhci_arasan_syscon_write(struct sdhci_host *host,
167 const struct sdhci_arasan_soc_ctl_field *fld,
170 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
171 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
172 struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
174 u16 width = fld->width;
175 s16 shift = fld->shift;
179 * Silently return errors for shift < 0 so caller doesn't have
180 * to check for fields which are optional. For fields that
181 * are required then caller needs to do something special
187 if (sdhci_arasan->soc_ctl_map->hiword_update)
188 ret = regmap_write(soc_ctl_base, reg,
189 HIWORD_UPDATE(val, GENMASK(width, 0),
192 ret = regmap_update_bits(soc_ctl_base, reg,
193 GENMASK(shift + width, shift),
196 /* Yell about (unexpected) regmap errors */
198 pr_warn("%s: Regmap write fail: %d\n",
199 mmc_hostname(host->mmc), ret);
204 static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
206 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
207 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
208 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
209 bool ctrl_phy = false;
211 if (!IS_ERR(sdhci_arasan->phy)) {
212 if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
214 * If PHY off, set clock to max speed and power PHY on.
216 * Although PHY docs apparently suggest power cycling
217 * when changing the clock the PHY doesn't like to be
218 * powered on while at low speeds like those used in ID
219 * mode. Even worse is powering the PHY on while the
222 * To workaround the PHY limitations, the best we can
223 * do is to power it on at a faster speed and then slam
224 * through low speeds without power cycling.
226 sdhci_set_clock(host, host->max_clk);
227 phy_power_on(sdhci_arasan->phy);
228 sdhci_arasan->is_phy_on = true;
231 * We'll now fall through to the below case with
232 * ctrl_phy = false (so we won't turn off/on). The
233 * sdhci_set_clock() will set the real clock.
235 } else if (clock > PHY_CLK_TOO_SLOW_HZ) {
237 * At higher clock speeds the PHY is fine being power
238 * cycled and docs say you _should_ power cycle when
239 * changing clock speeds.
245 if (ctrl_phy && sdhci_arasan->is_phy_on) {
246 phy_power_off(sdhci_arasan->phy);
247 sdhci_arasan->is_phy_on = false;
250 /* Set the Input and Output Clock Phase Delays */
251 if (clk_data->set_clk_delays)
252 clk_data->set_clk_delays(host);
254 sdhci_set_clock(host, clock);
256 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE)
258 * Some controllers immediately report SDHCI_CLOCK_INT_STABLE
259 * after enabling the clock even though the clock is not
260 * stable. Trying to use a clock without waiting here results
261 * in EILSEQ while detecting some older/slower cards. The
262 * chosen delay is the maximum delay from sdhci_set_clock.
267 phy_power_on(sdhci_arasan->phy);
268 sdhci_arasan->is_phy_on = true;
272 static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
276 struct sdhci_host *host = mmc_priv(mmc);
278 vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
279 if (ios->enhanced_strobe)
280 vendor |= VENDOR_ENHANCED_STROBE;
282 vendor &= ~VENDOR_ENHANCED_STROBE;
284 sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
287 static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
290 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
291 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
293 sdhci_reset(host, mask);
295 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
296 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
297 ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
298 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
302 static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
305 switch (ios->signal_voltage) {
306 case MMC_SIGNAL_VOLTAGE_180:
308 * Plese don't switch to 1V8 as arasan,5.1 doesn't
309 * actually refer to this setting to indicate the
310 * signal voltage and the state machine will be broken
311 * actually if we force to enable 1V8. That's something
312 * like broken quirk but we could work around here.
315 case MMC_SIGNAL_VOLTAGE_330:
316 case MMC_SIGNAL_VOLTAGE_120:
317 /* We don't support 3V3 and 1V2 */
324 static const struct sdhci_ops sdhci_arasan_ops = {
325 .set_clock = sdhci_arasan_set_clock,
326 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
327 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
328 .set_bus_width = sdhci_set_bus_width,
329 .reset = sdhci_arasan_reset,
330 .set_uhs_signaling = sdhci_set_uhs_signaling,
331 .set_power = sdhci_set_power_and_bus_voltage,
334 static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
335 .ops = &sdhci_arasan_ops,
336 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
337 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
338 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
339 SDHCI_QUIRK2_STOP_WITH_TC,
342 static struct sdhci_arasan_of_data sdhci_arasan_data = {
343 .pdata = &sdhci_arasan_pdata,
346 static const struct sdhci_pltfm_data sdhci_arasan_zynqmp_pdata = {
347 .ops = &sdhci_arasan_ops,
348 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
349 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
350 SDHCI_QUIRK2_STOP_WITH_TC,
353 static struct sdhci_arasan_of_data sdhci_arasan_zynqmp_data = {
354 .pdata = &sdhci_arasan_zynqmp_pdata,
357 static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
362 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
365 cqhci_irq(host->mmc, intmask, cmd_error, data_error);
370 static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
372 sdhci_dumpregs(mmc_priv(mmc));
375 static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
377 struct sdhci_host *host = mmc_priv(mmc);
380 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
381 while (reg & SDHCI_DATA_AVAILABLE) {
382 sdhci_readl(host, SDHCI_BUFFER);
383 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
386 sdhci_cqe_enable(mmc);
389 static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
390 .enable = sdhci_arasan_cqe_enable,
391 .disable = sdhci_cqe_disable,
392 .dumpregs = sdhci_arasan_dumpregs,
395 static const struct sdhci_ops sdhci_arasan_cqe_ops = {
396 .set_clock = sdhci_arasan_set_clock,
397 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
398 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
399 .set_bus_width = sdhci_set_bus_width,
400 .reset = sdhci_arasan_reset,
401 .set_uhs_signaling = sdhci_set_uhs_signaling,
402 .set_power = sdhci_set_power_and_bus_voltage,
403 .irq = sdhci_arasan_cqhci_irq,
406 static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
407 .ops = &sdhci_arasan_cqe_ops,
408 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
409 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
410 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
413 static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = {
414 .soc_ctl_map = &rk3399_soc_ctl_map,
415 .pdata = &sdhci_arasan_cqe_pdata,
418 static struct sdhci_arasan_of_data intel_lgm_emmc_data = {
419 .soc_ctl_map = &intel_lgm_emmc_soc_ctl_map,
420 .pdata = &sdhci_arasan_cqe_pdata,
423 static struct sdhci_arasan_of_data intel_lgm_sdxc_data = {
424 .soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map,
425 .pdata = &sdhci_arasan_cqe_pdata,
428 #ifdef CONFIG_PM_SLEEP
430 * sdhci_arasan_suspend - Suspend method for the driver
431 * @dev: Address of the device structure
432 * Returns 0 on success and error value on error
434 * Put the device in a low power state.
436 static int sdhci_arasan_suspend(struct device *dev)
438 struct sdhci_host *host = dev_get_drvdata(dev);
439 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
440 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
443 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
444 mmc_retune_needed(host->mmc);
446 if (sdhci_arasan->has_cqe) {
447 ret = cqhci_suspend(host->mmc);
452 ret = sdhci_suspend_host(host);
456 if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
457 ret = phy_power_off(sdhci_arasan->phy);
459 dev_err(dev, "Cannot power off phy.\n");
460 sdhci_resume_host(host);
463 sdhci_arasan->is_phy_on = false;
466 clk_disable(pltfm_host->clk);
467 clk_disable(sdhci_arasan->clk_ahb);
473 * sdhci_arasan_resume - Resume method for the driver
474 * @dev: Address of the device structure
475 * Returns 0 on success and error value on error
477 * Resume operation after suspend
479 static int sdhci_arasan_resume(struct device *dev)
481 struct sdhci_host *host = dev_get_drvdata(dev);
482 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
483 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
486 ret = clk_enable(sdhci_arasan->clk_ahb);
488 dev_err(dev, "Cannot enable AHB clock.\n");
492 ret = clk_enable(pltfm_host->clk);
494 dev_err(dev, "Cannot enable SD clock.\n");
498 if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
499 ret = phy_power_on(sdhci_arasan->phy);
501 dev_err(dev, "Cannot power on phy.\n");
504 sdhci_arasan->is_phy_on = true;
507 ret = sdhci_resume_host(host);
509 dev_err(dev, "Cannot resume host.\n");
513 if (sdhci_arasan->has_cqe)
514 return cqhci_resume(host->mmc);
518 #endif /* ! CONFIG_PM_SLEEP */
520 static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
521 sdhci_arasan_resume);
523 static const struct of_device_id sdhci_arasan_of_match[] = {
524 /* SoC-specific compatible strings w/ soc_ctl_map */
526 .compatible = "rockchip,rk3399-sdhci-5.1",
527 .data = &sdhci_arasan_rk3399_data,
530 .compatible = "intel,lgm-sdhci-5.1-emmc",
531 .data = &intel_lgm_emmc_data,
534 .compatible = "intel,lgm-sdhci-5.1-sdxc",
535 .data = &intel_lgm_sdxc_data,
537 /* Generic compatible below here */
539 .compatible = "arasan,sdhci-8.9a",
540 .data = &sdhci_arasan_data,
543 .compatible = "arasan,sdhci-5.1",
544 .data = &sdhci_arasan_data,
547 .compatible = "arasan,sdhci-4.9a",
548 .data = &sdhci_arasan_data,
551 .compatible = "xlnx,zynqmp-8.9a",
552 .data = &sdhci_arasan_zynqmp_data,
556 MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
559 * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
561 * Return the current actual rate of the SD card clock. This can be used
562 * to communicate with out PHY.
564 * @hw: Pointer to the hardware clock structure.
565 * @parent_rate The parent rate (should be rate of clk_xin).
566 * Returns the card clock rate.
568 static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
569 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 * Return the current actual rate of the sampling clock. This can be used
589 * to communicate with out PHY.
591 * @hw: Pointer to the hardware clock structure.
592 * @parent_rate The parent rate (should be rate of clk_xin).
593 * Returns the sample clock rate.
595 static unsigned long sdhci_arasan_sampleclk_recalc_rate(struct clk_hw *hw,
596 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 * Set the SD Output Clock Tap Delays for Output path
617 * @hw: Pointer to the hardware clock structure.
618 * @degrees The clock phase shift between 0 - 359.
619 * Return: 0 on success and error value on error
621 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;
635 * This is applicable for SDHCI_SPEC_300 and above
636 * ZynqMP does not set phase for <=25MHz clock.
637 * If degrees is zero, no need to do anything.
639 if (host->version < SDHCI_SPEC_300 ||
640 host->timing == MMC_TIMING_LEGACY ||
641 host->timing == MMC_TIMING_UHS_SDR12 || !degrees)
644 switch (host->timing) {
645 case MMC_TIMING_MMC_HS:
646 case MMC_TIMING_SD_HS:
647 case MMC_TIMING_UHS_SDR25:
648 case MMC_TIMING_UHS_DDR50:
649 case MMC_TIMING_MMC_DDR52:
650 /* For 50MHz clock, 30 Taps are available */
653 case MMC_TIMING_UHS_SDR50:
654 /* For 100MHz clock, 15 Taps are available */
657 case MMC_TIMING_UHS_SDR104:
658 case MMC_TIMING_MMC_HS200:
659 /* For 200MHz clock, 8 Taps are available */
665 tap_delay = (degrees * tap_max) / 360;
667 /* Set the Clock Phase */
668 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_OUTPUT, tap_delay);
670 pr_err("Error setting Output Tap Delay\n");
675 static const struct clk_ops zynqmp_sdcardclk_ops = {
676 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
677 .set_phase = sdhci_zynqmp_sdcardclk_set_phase,
681 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
683 * Set the SD Input Clock Tap Delays for Input path
685 * @hw: Pointer to the hardware clock structure.
686 * @degrees The clock phase shift between 0 - 359.
687 * Return: 0 on success and error value on error
689 static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees)
692 struct sdhci_arasan_clk_data *clk_data =
693 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
694 struct sdhci_arasan_data *sdhci_arasan =
695 container_of(clk_data, struct sdhci_arasan_data, clk_data);
696 struct sdhci_host *host = sdhci_arasan->host;
697 const char *clk_name = clk_hw_get_name(hw);
698 u32 node_id = !strcmp(clk_name, "clk_in_sd0") ? NODE_SD_0 : NODE_SD_1;
699 u8 tap_delay, tap_max = 0;
703 * This is applicable for SDHCI_SPEC_300 and above
704 * ZynqMP does not set phase for <=25MHz clock.
705 * If degrees is zero, no need to do anything.
707 if (host->version < SDHCI_SPEC_300 ||
708 host->timing == MMC_TIMING_LEGACY ||
709 host->timing == MMC_TIMING_UHS_SDR12 || !degrees)
712 switch (host->timing) {
713 case MMC_TIMING_MMC_HS:
714 case MMC_TIMING_SD_HS:
715 case MMC_TIMING_UHS_SDR25:
716 case MMC_TIMING_UHS_DDR50:
717 case MMC_TIMING_MMC_DDR52:
718 /* For 50MHz clock, 120 Taps are available */
721 case MMC_TIMING_UHS_SDR50:
722 /* For 100MHz clock, 60 Taps are available */
725 case MMC_TIMING_UHS_SDR104:
726 case MMC_TIMING_MMC_HS200:
727 /* For 200MHz clock, 30 Taps are available */
733 tap_delay = (degrees * tap_max) / 360;
735 /* Set the Clock Phase */
736 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_INPUT, tap_delay);
738 pr_err("Error setting Input Tap Delay\n");
743 static const struct clk_ops zynqmp_sampleclk_ops = {
744 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
745 .set_phase = sdhci_zynqmp_sampleclk_set_phase,
748 static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 deviceid)
752 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
753 clk &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN);
754 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
756 /* Issue DLL Reset */
757 zynqmp_pm_sd_dll_reset(deviceid, PM_DLL_RESET_PULSE);
759 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
761 sdhci_enable_clk(host, clk);
764 static int arasan_zynqmp_execute_tuning(struct mmc_host *mmc, u32 opcode)
766 struct sdhci_host *host = mmc_priv(mmc);
767 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
768 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
769 struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw;
770 const char *clk_name = clk_hw_get_name(hw);
771 u32 device_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 :
775 arasan_zynqmp_dll_reset(host, device_id);
777 err = sdhci_execute_tuning(mmc, opcode);
781 arasan_zynqmp_dll_reset(host, device_id);
787 * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
789 * The corecfg_clockmultiplier is supposed to contain clock multiplier
790 * value of programmable clock generator.
793 * - Many existing devices don't seem to do this and work fine. To keep
794 * compatibility for old hardware where the device tree doesn't provide a
795 * register map, this function is a noop if a soc_ctl_map hasn't been provided
797 * - The value of corecfg_clockmultiplier should sync with that of corresponding
798 * value reading from sdhci_capability_register. So this function is called
799 * once at probe time and never called again.
801 * @host: The sdhci_host
803 static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
806 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
807 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
808 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
809 sdhci_arasan->soc_ctl_map;
811 /* Having a map is optional */
815 /* If we have a map, we expect to have a syscon */
816 if (!sdhci_arasan->soc_ctl_base) {
817 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
818 mmc_hostname(host->mmc));
822 sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
826 * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
828 * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This
829 * function can be used to make that happen.
832 * - Many existing devices don't seem to do this and work fine. To keep
833 * compatibility for old hardware where the device tree doesn't provide a
834 * register map, this function is a noop if a soc_ctl_map hasn't been provided
836 * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
837 * to achieve lower clock rates. That means that this function is called once
838 * at probe time and never called again.
840 * @host: The sdhci_host
842 static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
844 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
845 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
846 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
847 sdhci_arasan->soc_ctl_map;
848 u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000);
850 /* Having a map is optional */
854 /* If we have a map, we expect to have a syscon */
855 if (!sdhci_arasan->soc_ctl_base) {
856 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
857 mmc_hostname(host->mmc));
861 sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
864 static void sdhci_arasan_set_clk_delays(struct sdhci_host *host)
866 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
867 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
868 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
870 clk_set_phase(clk_data->sampleclk,
871 clk_data->clk_phase_in[host->timing]);
872 clk_set_phase(clk_data->sdcardclk,
873 clk_data->clk_phase_out[host->timing]);
876 static void arasan_dt_read_clk_phase(struct device *dev,
877 struct sdhci_arasan_clk_data *clk_data,
878 unsigned int timing, const char *prop)
880 struct device_node *np = dev->of_node;
882 int clk_phase[2] = {0};
885 * Read Tap Delay values from DT, if the DT does not contain the
886 * Tap Values then use the pre-defined values.
888 if (of_property_read_variable_u32_array(np, prop, &clk_phase[0],
890 dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
891 prop, clk_data->clk_phase_in[timing],
892 clk_data->clk_phase_out[timing]);
896 /* The values read are Input and Output Clock Delays in order */
897 clk_data->clk_phase_in[timing] = clk_phase[0];
898 clk_data->clk_phase_out[timing] = clk_phase[1];
902 * arasan_dt_parse_clk_phases - Read Clock Delay values from DT
904 * Called at initialization to parse the values of Clock Delays.
906 * @dev: Pointer to our struct device.
907 * @clk_data: Pointer to the Clock Data structure
909 static void arasan_dt_parse_clk_phases(struct device *dev,
910 struct sdhci_arasan_clk_data *clk_data)
912 int *iclk_phase, *oclk_phase;
917 * This has been kept as a pointer and is assigned a function here.
918 * So that different controller variants can assign their own handling
921 clk_data->set_clk_delays = sdhci_arasan_set_clk_delays;
923 if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) {
924 iclk_phase = (int [MMC_TIMING_MMC_HS400 + 1]) ZYNQMP_ICLK_PHASE;
925 oclk_phase = (int [MMC_TIMING_MMC_HS400 + 1]) ZYNQMP_OCLK_PHASE;
927 of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank);
929 oclk_phase[MMC_TIMING_UHS_SDR104] = 90;
930 oclk_phase[MMC_TIMING_MMC_HS200] = 90;
933 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
934 clk_data->clk_phase_in[i] = iclk_phase[i];
935 clk_data->clk_phase_out[i] = oclk_phase[i];
939 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY,
941 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS,
943 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS,
945 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12,
946 "clk-phase-uhs-sdr12");
947 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25,
948 "clk-phase-uhs-sdr25");
949 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50,
950 "clk-phase-uhs-sdr50");
951 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104,
952 "clk-phase-uhs-sdr104");
953 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50,
954 "clk-phase-uhs-ddr50");
955 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52,
956 "clk-phase-mmc-ddr52");
957 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200,
958 "clk-phase-mmc-hs200");
959 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400,
960 "clk-phase-mmc-hs400");
964 * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use
966 * Some PHY devices need to know what the actual card clock is. In order for
967 * them to find out, we'll provide a clock through the common clock framework
970 * @sdhci_arasan: Our private data structure.
971 * @clk_xin: Pointer to the functional clock
972 * @dev: Pointer to our struct device.
973 * Returns 0 on success and error value on error
976 sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan,
980 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
981 struct device_node *np = dev->of_node;
982 struct clk_init_data sdcardclk_init;
983 const char *parent_clk_name;
986 ret = of_property_read_string_index(np, "clock-output-names", 0,
987 &sdcardclk_init.name);
989 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
993 parent_clk_name = __clk_get_name(clk_xin);
994 sdcardclk_init.parent_names = &parent_clk_name;
995 sdcardclk_init.num_parents = 1;
996 sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
997 if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a"))
998 sdcardclk_init.ops = &zynqmp_sdcardclk_ops;
1000 sdcardclk_init.ops = &arasan_sdcardclk_ops;
1002 clk_data->sdcardclk_hw.init = &sdcardclk_init;
1003 clk_data->sdcardclk =
1004 devm_clk_register(dev, &clk_data->sdcardclk_hw);
1005 clk_data->sdcardclk_hw.init = NULL;
1007 ret = of_clk_add_provider(np, of_clk_src_simple_get,
1008 clk_data->sdcardclk);
1010 dev_err(dev, "Failed to add sdcard clock provider\n");
1016 * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use
1018 * Some PHY devices need to know what the actual card clock is. In order for
1019 * them to find out, we'll provide a clock through the common clock framework
1020 * for them to query.
1022 * @sdhci_arasan: Our private data structure.
1023 * @clk_xin: Pointer to the functional clock
1024 * @dev: Pointer to our struct device.
1025 * Returns 0 on success and error value on error
1028 sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan,
1029 struct clk *clk_xin,
1032 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1033 struct device_node *np = dev->of_node;
1034 struct clk_init_data sampleclk_init;
1035 const char *parent_clk_name;
1038 ret = of_property_read_string_index(np, "clock-output-names", 1,
1039 &sampleclk_init.name);
1041 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1045 parent_clk_name = __clk_get_name(clk_xin);
1046 sampleclk_init.parent_names = &parent_clk_name;
1047 sampleclk_init.num_parents = 1;
1048 sampleclk_init.flags = CLK_GET_RATE_NOCACHE;
1049 if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a"))
1050 sampleclk_init.ops = &zynqmp_sampleclk_ops;
1052 sampleclk_init.ops = &arasan_sampleclk_ops;
1054 clk_data->sampleclk_hw.init = &sampleclk_init;
1055 clk_data->sampleclk =
1056 devm_clk_register(dev, &clk_data->sampleclk_hw);
1057 clk_data->sampleclk_hw.init = NULL;
1059 ret = of_clk_add_provider(np, of_clk_src_simple_get,
1060 clk_data->sampleclk);
1062 dev_err(dev, "Failed to add sample clock provider\n");
1068 * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
1070 * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
1073 * @dev: Pointer to our struct device.
1075 static void sdhci_arasan_unregister_sdclk(struct device *dev)
1077 struct device_node *np = dev->of_node;
1079 if (!of_find_property(np, "#clock-cells", NULL))
1082 of_clk_del_provider(dev->of_node);
1086 * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use
1088 * Some PHY devices need to know what the actual card clock is. In order for
1089 * them to find out, we'll provide a clock through the common clock framework
1090 * for them to query.
1092 * Note: without seriously re-architecting SDHCI's clock code and testing on
1093 * all platforms, there's no way to create a totally beautiful clock here
1094 * with all clock ops implemented. Instead, we'll just create a clock that can
1095 * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
1096 * framework that we're doing things behind its back. This should be sufficient
1097 * to create nice clean device tree bindings and later (if needed) we can try
1098 * re-architecting SDHCI if we see some benefit to it.
1100 * @sdhci_arasan: Our private data structure.
1101 * @clk_xin: Pointer to the functional clock
1102 * @dev: Pointer to our struct device.
1103 * Returns 0 on success and error value on error
1105 static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
1106 struct clk *clk_xin,
1109 struct device_node *np = dev->of_node;
1113 /* Providing a clock to the PHY is optional; no error if missing */
1114 if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0)
1117 ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev);
1122 ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin,
1125 sdhci_arasan_unregister_sdclk(dev);
1133 static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
1135 struct sdhci_host *host = sdhci_arasan->host;
1136 struct cqhci_host *cq_host;
1140 if (!sdhci_arasan->has_cqe)
1141 return sdhci_add_host(host);
1143 ret = sdhci_setup_host(host);
1147 cq_host = devm_kzalloc(host->mmc->parent,
1148 sizeof(*cq_host), GFP_KERNEL);
1154 cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
1155 cq_host->ops = &sdhci_arasan_cqhci_ops;
1157 dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1159 cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1161 ret = cqhci_init(cq_host, host->mmc, dma64);
1165 ret = __sdhci_add_host(host);
1172 sdhci_cleanup_host(host);
1176 static int sdhci_arasan_probe(struct platform_device *pdev)
1179 const struct of_device_id *match;
1180 struct device_node *node;
1181 struct clk *clk_xin;
1182 struct sdhci_host *host;
1183 struct sdhci_pltfm_host *pltfm_host;
1184 struct sdhci_arasan_data *sdhci_arasan;
1185 struct device_node *np = pdev->dev.of_node;
1186 const struct sdhci_arasan_of_data *data;
1188 match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node);
1190 host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan));
1193 return PTR_ERR(host);
1195 pltfm_host = sdhci_priv(host);
1196 sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1197 sdhci_arasan->host = host;
1199 sdhci_arasan->soc_ctl_map = data->soc_ctl_map;
1201 node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0);
1203 sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
1206 if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
1207 ret = PTR_ERR(sdhci_arasan->soc_ctl_base);
1208 if (ret != -EPROBE_DEFER)
1209 dev_err(&pdev->dev, "Can't get syscon: %d\n",
1211 goto err_pltfm_free;
1215 sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb");
1216 if (IS_ERR(sdhci_arasan->clk_ahb)) {
1217 dev_err(&pdev->dev, "clk_ahb clock not found.\n");
1218 ret = PTR_ERR(sdhci_arasan->clk_ahb);
1219 goto err_pltfm_free;
1222 clk_xin = devm_clk_get(&pdev->dev, "clk_xin");
1223 if (IS_ERR(clk_xin)) {
1224 dev_err(&pdev->dev, "clk_xin clock not found.\n");
1225 ret = PTR_ERR(clk_xin);
1226 goto err_pltfm_free;
1229 ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
1231 dev_err(&pdev->dev, "Unable to enable AHB clock.\n");
1232 goto err_pltfm_free;
1235 ret = clk_prepare_enable(clk_xin);
1237 dev_err(&pdev->dev, "Unable to enable SD clock.\n");
1241 sdhci_get_of_property(pdev);
1243 if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
1244 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
1246 if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
1247 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
1249 pltfm_host->clk = clk_xin;
1251 if (of_device_is_compatible(pdev->dev.of_node,
1252 "rockchip,rk3399-sdhci-5.1"))
1253 sdhci_arasan_update_clockmultiplier(host, 0x0);
1255 sdhci_arasan_update_baseclkfreq(host);
1257 ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev);
1259 goto clk_disable_all;
1261 if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
1262 host->mmc_host_ops.execute_tuning =
1263 arasan_zynqmp_execute_tuning;
1266 arasan_dt_parse_clk_phases(&pdev->dev, &sdhci_arasan->clk_data);
1268 ret = mmc_of_parse(host->mmc);
1270 if (ret != -EPROBE_DEFER)
1271 dev_err(&pdev->dev, "parsing dt failed (%d)\n", ret);
1275 sdhci_arasan->phy = ERR_PTR(-ENODEV);
1276 if (of_device_is_compatible(pdev->dev.of_node,
1277 "arasan,sdhci-5.1")) {
1278 sdhci_arasan->phy = devm_phy_get(&pdev->dev,
1280 if (IS_ERR(sdhci_arasan->phy)) {
1281 ret = PTR_ERR(sdhci_arasan->phy);
1282 dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n");
1286 ret = phy_init(sdhci_arasan->phy);
1288 dev_err(&pdev->dev, "phy_init err.\n");
1292 host->mmc_host_ops.hs400_enhanced_strobe =
1293 sdhci_arasan_hs400_enhanced_strobe;
1294 host->mmc_host_ops.start_signal_voltage_switch =
1295 sdhci_arasan_voltage_switch;
1296 sdhci_arasan->has_cqe = true;
1297 host->mmc->caps2 |= MMC_CAP2_CQE;
1299 if (!of_property_read_bool(np, "disable-cqe-dcmd"))
1300 host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
1303 ret = sdhci_arasan_add_host(sdhci_arasan);
1310 if (!IS_ERR(sdhci_arasan->phy))
1311 phy_exit(sdhci_arasan->phy);
1313 sdhci_arasan_unregister_sdclk(&pdev->dev);
1315 clk_disable_unprepare(clk_xin);
1317 clk_disable_unprepare(sdhci_arasan->clk_ahb);
1319 sdhci_pltfm_free(pdev);
1323 static int sdhci_arasan_remove(struct platform_device *pdev)
1326 struct sdhci_host *host = platform_get_drvdata(pdev);
1327 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1328 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1329 struct clk *clk_ahb = sdhci_arasan->clk_ahb;
1331 if (!IS_ERR(sdhci_arasan->phy)) {
1332 if (sdhci_arasan->is_phy_on)
1333 phy_power_off(sdhci_arasan->phy);
1334 phy_exit(sdhci_arasan->phy);
1337 sdhci_arasan_unregister_sdclk(&pdev->dev);
1339 ret = sdhci_pltfm_unregister(pdev);
1341 clk_disable_unprepare(clk_ahb);
1346 static struct platform_driver sdhci_arasan_driver = {
1348 .name = "sdhci-arasan",
1349 .of_match_table = sdhci_arasan_of_match,
1350 .pm = &sdhci_arasan_dev_pm_ops,
1352 .probe = sdhci_arasan_probe,
1353 .remove = sdhci_arasan_remove,
1356 module_platform_driver(sdhci_arasan_driver);
1358 MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
1359 MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
1360 MODULE_LICENSE("GPL");