1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Arasan Secure Digital Host Controller Interface.
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.
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 thunderbay_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,
201 static const struct sdhci_arasan_soc_ctl_map intel_keembay_soc_ctl_map = {
202 .baseclkfreq = { .reg = 0x0, .width = 8, .shift = 14 },
203 .clockmultiplier = { .reg = 0x4, .width = 8, .shift = 14 },
204 .support64b = { .reg = 0x4, .width = 1, .shift = 24 },
205 .hiword_update = false,
209 * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
211 * @host: The sdhci_host
212 * @fld: The field to write to
213 * @val: The value to write
215 * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
216 * Note that if a field is specified as not available (shift < 0) then
217 * this function will silently return an error code. It will be noisy
218 * and print errors for any other (unexpected) errors.
220 * Return: 0 on success and error value on error
222 static int sdhci_arasan_syscon_write(struct sdhci_host *host,
223 const struct sdhci_arasan_soc_ctl_field *fld,
226 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
227 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
228 struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
230 u16 width = fld->width;
231 s16 shift = fld->shift;
235 * Silently return errors for shift < 0 so caller doesn't have
236 * to check for fields which are optional. For fields that
237 * are required then caller needs to do something special
243 if (sdhci_arasan->soc_ctl_map->hiword_update)
244 ret = regmap_write(soc_ctl_base, reg,
245 HIWORD_UPDATE(val, GENMASK(width, 0),
248 ret = regmap_update_bits(soc_ctl_base, reg,
249 GENMASK(shift + width, shift),
252 /* Yell about (unexpected) regmap errors */
254 pr_warn("%s: Regmap write fail: %d\n",
255 mmc_hostname(host->mmc), ret);
260 static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
262 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
263 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
264 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
265 bool ctrl_phy = false;
267 if (!IS_ERR(sdhci_arasan->phy)) {
268 if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
270 * If PHY off, set clock to max speed and power PHY on.
272 * Although PHY docs apparently suggest power cycling
273 * when changing the clock the PHY doesn't like to be
274 * powered on while at low speeds like those used in ID
275 * mode. Even worse is powering the PHY on while the
278 * To workaround the PHY limitations, the best we can
279 * do is to power it on at a faster speed and then slam
280 * through low speeds without power cycling.
282 sdhci_set_clock(host, host->max_clk);
283 if (phy_power_on(sdhci_arasan->phy)) {
284 pr_err("%s: Cannot power on phy.\n",
285 mmc_hostname(host->mmc));
289 sdhci_arasan->is_phy_on = true;
292 * We'll now fall through to the below case with
293 * ctrl_phy = false (so we won't turn off/on). The
294 * sdhci_set_clock() will set the real clock.
296 } else if (clock > PHY_CLK_TOO_SLOW_HZ) {
298 * At higher clock speeds the PHY is fine being power
299 * cycled and docs say you _should_ power cycle when
300 * changing clock speeds.
306 if (ctrl_phy && sdhci_arasan->is_phy_on) {
307 phy_power_off(sdhci_arasan->phy);
308 sdhci_arasan->is_phy_on = false;
311 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN) {
313 * Some of the Arasan variations might not have timing
314 * requirements met at 25MHz for Default Speed mode,
315 * those controllers work at 19MHz instead.
317 if (clock == DEFAULT_SPEED_MAX_DTR)
318 clock = (DEFAULT_SPEED_MAX_DTR * 19) / 25;
321 /* Set the Input and Output Clock Phase Delays */
322 if (clk_data->set_clk_delays)
323 clk_data->set_clk_delays(host);
325 sdhci_set_clock(host, clock);
327 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE)
329 * Some controllers immediately report SDHCI_CLOCK_INT_STABLE
330 * after enabling the clock even though the clock is not
331 * stable. Trying to use a clock without waiting here results
332 * in EILSEQ while detecting some older/slower cards. The
333 * chosen delay is the maximum delay from sdhci_set_clock.
338 if (phy_power_on(sdhci_arasan->phy)) {
339 pr_err("%s: Cannot power on phy.\n",
340 mmc_hostname(host->mmc));
344 sdhci_arasan->is_phy_on = true;
348 static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
352 struct sdhci_host *host = mmc_priv(mmc);
354 vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
355 if (ios->enhanced_strobe)
356 vendor |= VENDOR_ENHANCED_STROBE;
358 vendor &= ~VENDOR_ENHANCED_STROBE;
360 sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
363 static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
366 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
367 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
369 sdhci_reset(host, mask);
371 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
372 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
373 ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
374 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
378 static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
381 switch (ios->signal_voltage) {
382 case MMC_SIGNAL_VOLTAGE_180:
384 * Plese don't switch to 1V8 as arasan,5.1 doesn't
385 * actually refer to this setting to indicate the
386 * signal voltage and the state machine will be broken
387 * actually if we force to enable 1V8. That's something
388 * like broken quirk but we could work around here.
391 case MMC_SIGNAL_VOLTAGE_330:
392 case MMC_SIGNAL_VOLTAGE_120:
393 /* We don't support 3V3 and 1V2 */
400 static const struct sdhci_ops sdhci_arasan_ops = {
401 .set_clock = sdhci_arasan_set_clock,
402 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
403 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
404 .set_bus_width = sdhci_set_bus_width,
405 .reset = sdhci_arasan_reset,
406 .set_uhs_signaling = sdhci_set_uhs_signaling,
407 .set_power = sdhci_set_power_and_bus_voltage,
410 static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
415 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
418 cqhci_irq(host->mmc, intmask, cmd_error, data_error);
423 static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
425 sdhci_dumpregs(mmc_priv(mmc));
428 static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
430 struct sdhci_host *host = mmc_priv(mmc);
433 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
434 while (reg & SDHCI_DATA_AVAILABLE) {
435 sdhci_readl(host, SDHCI_BUFFER);
436 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
439 sdhci_cqe_enable(mmc);
442 static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
443 .enable = sdhci_arasan_cqe_enable,
444 .disable = sdhci_cqe_disable,
445 .dumpregs = sdhci_arasan_dumpregs,
448 static const struct sdhci_ops sdhci_arasan_cqe_ops = {
449 .set_clock = sdhci_arasan_set_clock,
450 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
451 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
452 .set_bus_width = sdhci_set_bus_width,
453 .reset = sdhci_arasan_reset,
454 .set_uhs_signaling = sdhci_set_uhs_signaling,
455 .set_power = sdhci_set_power_and_bus_voltage,
456 .irq = sdhci_arasan_cqhci_irq,
459 static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
460 .ops = &sdhci_arasan_cqe_ops,
461 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
462 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
463 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
466 static const struct sdhci_pltfm_data sdhci_arasan_thunderbay_pdata = {
467 .ops = &sdhci_arasan_cqe_ops,
468 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
469 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
470 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
471 SDHCI_QUIRK2_STOP_WITH_TC |
472 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
475 #ifdef CONFIG_PM_SLEEP
477 * sdhci_arasan_suspend - Suspend method for the driver
478 * @dev: Address of the device structure
480 * Put the device in a low power state.
482 * Return: 0 on success and error value on error
484 static int sdhci_arasan_suspend(struct device *dev)
486 struct sdhci_host *host = dev_get_drvdata(dev);
487 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
488 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
491 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
492 mmc_retune_needed(host->mmc);
494 if (sdhci_arasan->has_cqe) {
495 ret = cqhci_suspend(host->mmc);
500 ret = sdhci_suspend_host(host);
504 if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
505 ret = phy_power_off(sdhci_arasan->phy);
507 dev_err(dev, "Cannot power off phy.\n");
508 if (sdhci_resume_host(host))
509 dev_err(dev, "Cannot resume host.\n");
513 sdhci_arasan->is_phy_on = false;
516 clk_disable(pltfm_host->clk);
517 clk_disable(sdhci_arasan->clk_ahb);
523 * sdhci_arasan_resume - Resume method for the driver
524 * @dev: Address of the device structure
526 * Resume operation after suspend
528 * Return: 0 on success and error value on error
530 static int sdhci_arasan_resume(struct device *dev)
532 struct sdhci_host *host = dev_get_drvdata(dev);
533 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
534 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
537 ret = clk_enable(sdhci_arasan->clk_ahb);
539 dev_err(dev, "Cannot enable AHB clock.\n");
543 ret = clk_enable(pltfm_host->clk);
545 dev_err(dev, "Cannot enable SD clock.\n");
549 if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
550 ret = phy_power_on(sdhci_arasan->phy);
552 dev_err(dev, "Cannot power on phy.\n");
555 sdhci_arasan->is_phy_on = true;
558 ret = sdhci_resume_host(host);
560 dev_err(dev, "Cannot resume host.\n");
564 if (sdhci_arasan->has_cqe)
565 return cqhci_resume(host->mmc);
569 #endif /* ! CONFIG_PM_SLEEP */
571 static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
572 sdhci_arasan_resume);
575 * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
577 * @hw: Pointer to the hardware clock structure.
578 * @parent_rate: The parent rate (should be rate of clk_xin).
580 * Return the current actual rate of the SD card clock. This can be used
581 * to communicate with out PHY.
583 * Return: The card clock rate.
585 static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
586 unsigned long parent_rate)
588 struct sdhci_arasan_clk_data *clk_data =
589 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
590 struct sdhci_arasan_data *sdhci_arasan =
591 container_of(clk_data, struct sdhci_arasan_data, clk_data);
592 struct sdhci_host *host = sdhci_arasan->host;
594 return host->mmc->actual_clock;
597 static const struct clk_ops arasan_sdcardclk_ops = {
598 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
602 * sdhci_arasan_sampleclk_recalc_rate - Return the sampling clock rate
604 * @hw: Pointer to the hardware clock structure.
605 * @parent_rate: The parent rate (should be rate of clk_xin).
607 * Return the current actual rate of the sampling clock. This can be used
608 * to communicate with out PHY.
610 * Return: The sample clock rate.
612 static unsigned long sdhci_arasan_sampleclk_recalc_rate(struct clk_hw *hw,
613 unsigned long parent_rate)
615 struct sdhci_arasan_clk_data *clk_data =
616 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
617 struct sdhci_arasan_data *sdhci_arasan =
618 container_of(clk_data, struct sdhci_arasan_data, clk_data);
619 struct sdhci_host *host = sdhci_arasan->host;
621 return host->mmc->actual_clock;
624 static const struct clk_ops arasan_sampleclk_ops = {
625 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
629 * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
631 * @hw: Pointer to the hardware clock structure.
632 * @degrees: The clock phase shift between 0 - 359.
634 * Set the SD Output Clock Tap Delays for Output path
636 * Return: 0 on success and error value on error
638 static int sdhci_zynqmp_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
640 struct sdhci_arasan_clk_data *clk_data =
641 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
642 struct sdhci_arasan_data *sdhci_arasan =
643 container_of(clk_data, struct sdhci_arasan_data, clk_data);
644 struct sdhci_host *host = sdhci_arasan->host;
645 const char *clk_name = clk_hw_get_name(hw);
646 u32 node_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : NODE_SD_1;
647 u8 tap_delay, tap_max = 0;
650 /* This is applicable for SDHCI_SPEC_300 and above */
651 if (host->version < SDHCI_SPEC_300)
654 switch (host->timing) {
655 case MMC_TIMING_MMC_HS:
656 case MMC_TIMING_SD_HS:
657 case MMC_TIMING_UHS_SDR25:
658 case MMC_TIMING_UHS_DDR50:
659 case MMC_TIMING_MMC_DDR52:
660 /* For 50MHz clock, 30 Taps are available */
663 case MMC_TIMING_UHS_SDR50:
664 /* For 100MHz clock, 15 Taps are available */
667 case MMC_TIMING_UHS_SDR104:
668 case MMC_TIMING_MMC_HS200:
669 /* For 200MHz clock, 8 Taps are available */
676 tap_delay = (degrees * tap_max) / 360;
678 /* Set the Clock Phase */
679 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_OUTPUT, tap_delay);
681 pr_err("Error setting Output Tap Delay\n");
683 /* Release DLL Reset */
684 zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_RELEASE);
689 static const struct clk_ops zynqmp_sdcardclk_ops = {
690 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
691 .set_phase = sdhci_zynqmp_sdcardclk_set_phase,
695 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
697 * @hw: Pointer to the hardware clock structure.
698 * @degrees: The clock phase shift between 0 - 359.
700 * Set the SD Input Clock Tap Delays for Input path
702 * Return: 0 on success and error value on error
704 static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees)
706 struct sdhci_arasan_clk_data *clk_data =
707 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
708 struct sdhci_arasan_data *sdhci_arasan =
709 container_of(clk_data, struct sdhci_arasan_data, clk_data);
710 struct sdhci_host *host = sdhci_arasan->host;
711 const char *clk_name = clk_hw_get_name(hw);
712 u32 node_id = !strcmp(clk_name, "clk_in_sd0") ? NODE_SD_0 : NODE_SD_1;
713 u8 tap_delay, tap_max = 0;
716 /* This is applicable for SDHCI_SPEC_300 and above */
717 if (host->version < SDHCI_SPEC_300)
720 /* Assert DLL Reset */
721 zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_ASSERT);
723 switch (host->timing) {
724 case MMC_TIMING_MMC_HS:
725 case MMC_TIMING_SD_HS:
726 case MMC_TIMING_UHS_SDR25:
727 case MMC_TIMING_UHS_DDR50:
728 case MMC_TIMING_MMC_DDR52:
729 /* For 50MHz clock, 120 Taps are available */
732 case MMC_TIMING_UHS_SDR50:
733 /* For 100MHz clock, 60 Taps are available */
736 case MMC_TIMING_UHS_SDR104:
737 case MMC_TIMING_MMC_HS200:
738 /* For 200MHz clock, 30 Taps are available */
745 tap_delay = (degrees * tap_max) / 360;
747 /* Set the Clock Phase */
748 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_INPUT, tap_delay);
750 pr_err("Error setting Input Tap Delay\n");
755 static const struct clk_ops zynqmp_sampleclk_ops = {
756 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
757 .set_phase = sdhci_zynqmp_sampleclk_set_phase,
761 * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
763 * @hw: Pointer to the hardware clock structure.
764 * @degrees: The clock phase shift between 0 - 359.
766 * Set the SD Output Clock Tap Delays for Output path
768 * Return: 0 on success and error value on error
770 static int sdhci_versal_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
772 struct sdhci_arasan_clk_data *clk_data =
773 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
774 struct sdhci_arasan_data *sdhci_arasan =
775 container_of(clk_data, struct sdhci_arasan_data, clk_data);
776 struct sdhci_host *host = sdhci_arasan->host;
777 u8 tap_delay, tap_max = 0;
779 /* This is applicable for SDHCI_SPEC_300 and above */
780 if (host->version < SDHCI_SPEC_300)
783 switch (host->timing) {
784 case MMC_TIMING_MMC_HS:
785 case MMC_TIMING_SD_HS:
786 case MMC_TIMING_UHS_SDR25:
787 case MMC_TIMING_UHS_DDR50:
788 case MMC_TIMING_MMC_DDR52:
789 /* For 50MHz clock, 30 Taps are available */
792 case MMC_TIMING_UHS_SDR50:
793 /* For 100MHz clock, 15 Taps are available */
796 case MMC_TIMING_UHS_SDR104:
797 case MMC_TIMING_MMC_HS200:
798 /* For 200MHz clock, 8 Taps are available */
805 tap_delay = (degrees * tap_max) / 360;
807 /* Set the Clock Phase */
811 regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
812 regval |= SDHCI_OTAPDLY_ENABLE;
813 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
814 regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK;
816 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
822 static const struct clk_ops versal_sdcardclk_ops = {
823 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
824 .set_phase = sdhci_versal_sdcardclk_set_phase,
828 * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
830 * @hw: Pointer to the hardware clock structure.
831 * @degrees: The clock phase shift between 0 - 359.
833 * Set the SD Input Clock Tap Delays for Input path
835 * Return: 0 on success and error value on error
837 static int sdhci_versal_sampleclk_set_phase(struct clk_hw *hw, int degrees)
839 struct sdhci_arasan_clk_data *clk_data =
840 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
841 struct sdhci_arasan_data *sdhci_arasan =
842 container_of(clk_data, struct sdhci_arasan_data, clk_data);
843 struct sdhci_host *host = sdhci_arasan->host;
844 u8 tap_delay, tap_max = 0;
846 /* This is applicable for SDHCI_SPEC_300 and above */
847 if (host->version < SDHCI_SPEC_300)
850 switch (host->timing) {
851 case MMC_TIMING_MMC_HS:
852 case MMC_TIMING_SD_HS:
853 case MMC_TIMING_UHS_SDR25:
854 case MMC_TIMING_UHS_DDR50:
855 case MMC_TIMING_MMC_DDR52:
856 /* For 50MHz clock, 120 Taps are available */
859 case MMC_TIMING_UHS_SDR50:
860 /* For 100MHz clock, 60 Taps are available */
863 case MMC_TIMING_UHS_SDR104:
864 case MMC_TIMING_MMC_HS200:
865 /* For 200MHz clock, 30 Taps are available */
872 tap_delay = (degrees * tap_max) / 360;
874 /* Set the Clock Phase */
878 regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
879 regval |= SDHCI_ITAPDLY_CHGWIN;
880 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
881 regval |= SDHCI_ITAPDLY_ENABLE;
882 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
883 regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK;
885 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
886 regval &= ~SDHCI_ITAPDLY_CHGWIN;
887 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
893 static const struct clk_ops versal_sampleclk_ops = {
894 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
895 .set_phase = sdhci_versal_sampleclk_set_phase,
898 static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 deviceid)
902 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
903 clk &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN);
904 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
906 /* Issue DLL Reset */
907 zynqmp_pm_sd_dll_reset(deviceid, PM_DLL_RESET_PULSE);
909 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
911 sdhci_enable_clk(host, clk);
914 static int arasan_zynqmp_execute_tuning(struct mmc_host *mmc, u32 opcode)
916 struct sdhci_host *host = mmc_priv(mmc);
917 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
918 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
919 struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw;
920 const char *clk_name = clk_hw_get_name(hw);
921 u32 device_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 :
925 /* ZynqMP SD controller does not perform auto tuning in DDR50 mode */
926 if (mmc->ios.timing == MMC_TIMING_UHS_DDR50)
929 arasan_zynqmp_dll_reset(host, device_id);
931 err = sdhci_execute_tuning(mmc, opcode);
935 arasan_zynqmp_dll_reset(host, device_id);
941 * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
943 * @host: The sdhci_host
944 * @value: The value to write
946 * The corecfg_clockmultiplier is supposed to contain clock multiplier
947 * value of programmable clock generator.
950 * - Many existing devices don't seem to do this and work fine. To keep
951 * compatibility for old hardware where the device tree doesn't provide a
952 * register map, this function is a noop if a soc_ctl_map hasn't been provided
954 * - The value of corecfg_clockmultiplier should sync with that of corresponding
955 * value reading from sdhci_capability_register. So this function is called
956 * once at probe time and never called again.
958 static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
961 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
962 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
963 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
964 sdhci_arasan->soc_ctl_map;
966 /* Having a map is optional */
970 /* If we have a map, we expect to have a syscon */
971 if (!sdhci_arasan->soc_ctl_base) {
972 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
973 mmc_hostname(host->mmc));
977 sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
981 * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
983 * @host: The sdhci_host
985 * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This
986 * function can be used to make that happen.
989 * - Many existing devices don't seem to do this and work fine. To keep
990 * compatibility for old hardware where the device tree doesn't provide a
991 * register map, this function is a noop if a soc_ctl_map hasn't been provided
993 * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
994 * to achieve lower clock rates. That means that this function is called once
995 * at probe time and never called again.
997 static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
999 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1000 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1001 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
1002 sdhci_arasan->soc_ctl_map;
1003 u32 mhz = DIV_ROUND_CLOSEST_ULL(clk_get_rate(pltfm_host->clk), 1000000);
1005 /* Having a map is optional */
1009 /* If we have a map, we expect to have a syscon */
1010 if (!sdhci_arasan->soc_ctl_base) {
1011 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1012 mmc_hostname(host->mmc));
1016 sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
1019 static void sdhci_arasan_set_clk_delays(struct sdhci_host *host)
1021 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1022 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1023 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1025 clk_set_phase(clk_data->sampleclk,
1026 clk_data->clk_phase_in[host->timing]);
1027 clk_set_phase(clk_data->sdcardclk,
1028 clk_data->clk_phase_out[host->timing]);
1031 static void arasan_dt_read_clk_phase(struct device *dev,
1032 struct sdhci_arasan_clk_data *clk_data,
1033 unsigned int timing, const char *prop)
1035 struct device_node *np = dev->of_node;
1037 u32 clk_phase[2] = {0};
1041 * Read Tap Delay values from DT, if the DT does not contain the
1042 * Tap Values then use the pre-defined values.
1044 ret = of_property_read_variable_u32_array(np, prop, &clk_phase[0],
1047 dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
1048 prop, clk_data->clk_phase_in[timing],
1049 clk_data->clk_phase_out[timing]);
1053 /* The values read are Input and Output Clock Delays in order */
1054 clk_data->clk_phase_in[timing] = clk_phase[0];
1055 clk_data->clk_phase_out[timing] = clk_phase[1];
1059 * arasan_dt_parse_clk_phases - Read Clock Delay values from DT
1061 * @dev: Pointer to our struct device.
1062 * @clk_data: Pointer to the Clock Data structure
1064 * Called at initialization to parse the values of Clock Delays.
1066 static void arasan_dt_parse_clk_phases(struct device *dev,
1067 struct sdhci_arasan_clk_data *clk_data)
1073 * This has been kept as a pointer and is assigned a function here.
1074 * So that different controller variants can assign their own handling
1077 clk_data->set_clk_delays = sdhci_arasan_set_clk_delays;
1079 if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) {
1080 u32 zynqmp_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1082 u32 zynqmp_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1085 of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank);
1086 if (mio_bank == 2) {
1087 zynqmp_oclk_phase[MMC_TIMING_UHS_SDR104] = 90;
1088 zynqmp_oclk_phase[MMC_TIMING_MMC_HS200] = 90;
1091 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1092 clk_data->clk_phase_in[i] = zynqmp_iclk_phase[i];
1093 clk_data->clk_phase_out[i] = zynqmp_oclk_phase[i];
1097 if (of_device_is_compatible(dev->of_node, "xlnx,versal-8.9a")) {
1098 u32 versal_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1100 u32 versal_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1103 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1104 clk_data->clk_phase_in[i] = versal_iclk_phase[i];
1105 clk_data->clk_phase_out[i] = versal_oclk_phase[i];
1109 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY,
1110 "clk-phase-legacy");
1111 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS,
1112 "clk-phase-mmc-hs");
1113 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS,
1115 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12,
1116 "clk-phase-uhs-sdr12");
1117 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25,
1118 "clk-phase-uhs-sdr25");
1119 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50,
1120 "clk-phase-uhs-sdr50");
1121 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104,
1122 "clk-phase-uhs-sdr104");
1123 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50,
1124 "clk-phase-uhs-ddr50");
1125 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52,
1126 "clk-phase-mmc-ddr52");
1127 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200,
1128 "clk-phase-mmc-hs200");
1129 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400,
1130 "clk-phase-mmc-hs400");
1133 static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
1134 .ops = &sdhci_arasan_ops,
1135 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1136 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1137 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1138 SDHCI_QUIRK2_STOP_WITH_TC,
1141 static const struct sdhci_arasan_clk_ops arasan_clk_ops = {
1142 .sdcardclk_ops = &arasan_sdcardclk_ops,
1143 .sampleclk_ops = &arasan_sampleclk_ops,
1146 static struct sdhci_arasan_of_data sdhci_arasan_generic_data = {
1147 .pdata = &sdhci_arasan_pdata,
1148 .clk_ops = &arasan_clk_ops,
1151 static const struct sdhci_arasan_of_data sdhci_arasan_thunderbay_data = {
1152 .soc_ctl_map = &thunderbay_soc_ctl_map,
1153 .pdata = &sdhci_arasan_thunderbay_pdata,
1154 .clk_ops = &arasan_clk_ops,
1157 static const struct sdhci_pltfm_data sdhci_keembay_emmc_pdata = {
1158 .ops = &sdhci_arasan_cqe_ops,
1159 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1160 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1161 SDHCI_QUIRK_NO_LED |
1162 SDHCI_QUIRK_32BIT_DMA_ADDR |
1163 SDHCI_QUIRK_32BIT_DMA_SIZE |
1164 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1165 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1166 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1167 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
1168 SDHCI_QUIRK2_STOP_WITH_TC |
1169 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1172 static const struct sdhci_pltfm_data sdhci_keembay_sd_pdata = {
1173 .ops = &sdhci_arasan_ops,
1174 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1175 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1176 SDHCI_QUIRK_NO_LED |
1177 SDHCI_QUIRK_32BIT_DMA_ADDR |
1178 SDHCI_QUIRK_32BIT_DMA_SIZE |
1179 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1180 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1181 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1182 SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
1183 SDHCI_QUIRK2_STOP_WITH_TC |
1184 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1187 static const struct sdhci_pltfm_data sdhci_keembay_sdio_pdata = {
1188 .ops = &sdhci_arasan_ops,
1189 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1190 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1191 SDHCI_QUIRK_NO_LED |
1192 SDHCI_QUIRK_32BIT_DMA_ADDR |
1193 SDHCI_QUIRK_32BIT_DMA_SIZE |
1194 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1195 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1196 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1197 SDHCI_QUIRK2_HOST_OFF_CARD_ON |
1198 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1201 static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = {
1202 .soc_ctl_map = &rk3399_soc_ctl_map,
1203 .pdata = &sdhci_arasan_cqe_pdata,
1204 .clk_ops = &arasan_clk_ops,
1207 static struct sdhci_arasan_of_data intel_lgm_emmc_data = {
1208 .soc_ctl_map = &intel_lgm_emmc_soc_ctl_map,
1209 .pdata = &sdhci_arasan_cqe_pdata,
1210 .clk_ops = &arasan_clk_ops,
1213 static struct sdhci_arasan_of_data intel_lgm_sdxc_data = {
1214 .soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map,
1215 .pdata = &sdhci_arasan_cqe_pdata,
1216 .clk_ops = &arasan_clk_ops,
1219 static const struct sdhci_pltfm_data sdhci_arasan_zynqmp_pdata = {
1220 .ops = &sdhci_arasan_ops,
1221 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1222 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1223 SDHCI_QUIRK2_STOP_WITH_TC,
1226 static const struct sdhci_arasan_clk_ops zynqmp_clk_ops = {
1227 .sdcardclk_ops = &zynqmp_sdcardclk_ops,
1228 .sampleclk_ops = &zynqmp_sampleclk_ops,
1231 static struct sdhci_arasan_of_data sdhci_arasan_zynqmp_data = {
1232 .pdata = &sdhci_arasan_zynqmp_pdata,
1233 .clk_ops = &zynqmp_clk_ops,
1236 static const struct sdhci_arasan_clk_ops versal_clk_ops = {
1237 .sdcardclk_ops = &versal_sdcardclk_ops,
1238 .sampleclk_ops = &versal_sampleclk_ops,
1241 static struct sdhci_arasan_of_data sdhci_arasan_versal_data = {
1242 .pdata = &sdhci_arasan_zynqmp_pdata,
1243 .clk_ops = &versal_clk_ops,
1246 static struct sdhci_arasan_of_data intel_keembay_emmc_data = {
1247 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1248 .pdata = &sdhci_keembay_emmc_pdata,
1249 .clk_ops = &arasan_clk_ops,
1252 static struct sdhci_arasan_of_data intel_keembay_sd_data = {
1253 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1254 .pdata = &sdhci_keembay_sd_pdata,
1255 .clk_ops = &arasan_clk_ops,
1258 static struct sdhci_arasan_of_data intel_keembay_sdio_data = {
1259 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1260 .pdata = &sdhci_keembay_sdio_pdata,
1261 .clk_ops = &arasan_clk_ops,
1264 static const struct of_device_id sdhci_arasan_of_match[] = {
1265 /* SoC-specific compatible strings w/ soc_ctl_map */
1267 .compatible = "rockchip,rk3399-sdhci-5.1",
1268 .data = &sdhci_arasan_rk3399_data,
1271 .compatible = "intel,lgm-sdhci-5.1-emmc",
1272 .data = &intel_lgm_emmc_data,
1275 .compatible = "intel,lgm-sdhci-5.1-sdxc",
1276 .data = &intel_lgm_sdxc_data,
1279 .compatible = "intel,keembay-sdhci-5.1-emmc",
1280 .data = &intel_keembay_emmc_data,
1283 .compatible = "intel,keembay-sdhci-5.1-sd",
1284 .data = &intel_keembay_sd_data,
1287 .compatible = "intel,keembay-sdhci-5.1-sdio",
1288 .data = &intel_keembay_sdio_data,
1291 .compatible = "intel,thunderbay-sdhci-5.1",
1292 .data = &sdhci_arasan_thunderbay_data,
1294 /* Generic compatible below here */
1296 .compatible = "arasan,sdhci-8.9a",
1297 .data = &sdhci_arasan_generic_data,
1300 .compatible = "arasan,sdhci-5.1",
1301 .data = &sdhci_arasan_generic_data,
1304 .compatible = "arasan,sdhci-4.9a",
1305 .data = &sdhci_arasan_generic_data,
1308 .compatible = "xlnx,zynqmp-8.9a",
1309 .data = &sdhci_arasan_zynqmp_data,
1312 .compatible = "xlnx,versal-8.9a",
1313 .data = &sdhci_arasan_versal_data,
1317 MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
1320 * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use
1322 * @sdhci_arasan: Our private data structure.
1323 * @clk_xin: Pointer to the functional clock
1324 * @dev: Pointer to our struct device.
1326 * Some PHY devices need to know what the actual card clock is. In order for
1327 * them to find out, we'll provide a clock through the common clock framework
1328 * for them to query.
1330 * Return: 0 on success and error value on error
1333 sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan,
1334 struct clk *clk_xin,
1337 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1338 struct device_node *np = dev->of_node;
1339 struct clk_init_data sdcardclk_init;
1340 const char *parent_clk_name;
1343 ret = of_property_read_string_index(np, "clock-output-names", 0,
1344 &sdcardclk_init.name);
1346 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1350 parent_clk_name = __clk_get_name(clk_xin);
1351 sdcardclk_init.parent_names = &parent_clk_name;
1352 sdcardclk_init.num_parents = 1;
1353 sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
1354 sdcardclk_init.ops = sdhci_arasan->clk_ops->sdcardclk_ops;
1356 clk_data->sdcardclk_hw.init = &sdcardclk_init;
1357 clk_data->sdcardclk =
1358 devm_clk_register(dev, &clk_data->sdcardclk_hw);
1359 if (IS_ERR(clk_data->sdcardclk))
1360 return PTR_ERR(clk_data->sdcardclk);
1361 clk_data->sdcardclk_hw.init = NULL;
1363 ret = of_clk_add_provider(np, of_clk_src_simple_get,
1364 clk_data->sdcardclk);
1366 dev_err(dev, "Failed to add sdcard clock provider\n");
1372 * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use
1374 * @sdhci_arasan: Our private data structure.
1375 * @clk_xin: Pointer to the functional clock
1376 * @dev: Pointer to our struct device.
1378 * Some PHY devices need to know what the actual card clock is. In order for
1379 * them to find out, we'll provide a clock through the common clock framework
1380 * for them to query.
1382 * Return: 0 on success and error value on error
1385 sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan,
1386 struct clk *clk_xin,
1389 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1390 struct device_node *np = dev->of_node;
1391 struct clk_init_data sampleclk_init;
1392 const char *parent_clk_name;
1395 ret = of_property_read_string_index(np, "clock-output-names", 1,
1396 &sampleclk_init.name);
1398 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1402 parent_clk_name = __clk_get_name(clk_xin);
1403 sampleclk_init.parent_names = &parent_clk_name;
1404 sampleclk_init.num_parents = 1;
1405 sampleclk_init.flags = CLK_GET_RATE_NOCACHE;
1406 sampleclk_init.ops = sdhci_arasan->clk_ops->sampleclk_ops;
1408 clk_data->sampleclk_hw.init = &sampleclk_init;
1409 clk_data->sampleclk =
1410 devm_clk_register(dev, &clk_data->sampleclk_hw);
1411 if (IS_ERR(clk_data->sampleclk))
1412 return PTR_ERR(clk_data->sampleclk);
1413 clk_data->sampleclk_hw.init = NULL;
1415 ret = of_clk_add_provider(np, of_clk_src_simple_get,
1416 clk_data->sampleclk);
1418 dev_err(dev, "Failed to add sample clock provider\n");
1424 * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
1426 * @dev: Pointer to our struct device.
1428 * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
1431 static void sdhci_arasan_unregister_sdclk(struct device *dev)
1433 struct device_node *np = dev->of_node;
1435 if (!of_find_property(np, "#clock-cells", NULL))
1438 of_clk_del_provider(dev->of_node);
1442 * sdhci_arasan_update_support64b - Set SUPPORT_64B (64-bit System Bus Support)
1443 * @host: The sdhci_host
1444 * @value: The value to write
1446 * This should be set based on the System Address Bus.
1447 * 0: the Core supports only 32-bit System Address Bus.
1448 * 1: the Core supports 64-bit System Address Bus.
1451 * For Keem Bay, it is required to clear this bit. Its default value is 1'b1.
1452 * Keem Bay does not support 64-bit access.
1454 static void sdhci_arasan_update_support64b(struct sdhci_host *host, u32 value)
1456 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1457 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1458 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
1460 /* Having a map is optional */
1461 soc_ctl_map = sdhci_arasan->soc_ctl_map;
1465 /* If we have a map, we expect to have a syscon */
1466 if (!sdhci_arasan->soc_ctl_base) {
1467 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1468 mmc_hostname(host->mmc));
1472 sdhci_arasan_syscon_write(host, &soc_ctl_map->support64b, value);
1476 * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use
1478 * @sdhci_arasan: Our private data structure.
1479 * @clk_xin: Pointer to the functional clock
1480 * @dev: Pointer to our struct device.
1482 * Some PHY devices need to know what the actual card clock is. In order for
1483 * them to find out, we'll provide a clock through the common clock framework
1484 * for them to query.
1486 * Note: without seriously re-architecting SDHCI's clock code and testing on
1487 * all platforms, there's no way to create a totally beautiful clock here
1488 * with all clock ops implemented. Instead, we'll just create a clock that can
1489 * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
1490 * framework that we're doing things behind its back. This should be sufficient
1491 * to create nice clean device tree bindings and later (if needed) we can try
1492 * re-architecting SDHCI if we see some benefit to it.
1494 * Return: 0 on success and error value on error
1496 static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
1497 struct clk *clk_xin,
1500 struct device_node *np = dev->of_node;
1504 /* Providing a clock to the PHY is optional; no error if missing */
1505 if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0)
1508 ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev);
1513 ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin,
1516 sdhci_arasan_unregister_sdclk(dev);
1524 static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
1526 struct sdhci_host *host = sdhci_arasan->host;
1527 struct cqhci_host *cq_host;
1531 if (!sdhci_arasan->has_cqe)
1532 return sdhci_add_host(host);
1534 ret = sdhci_setup_host(host);
1538 cq_host = devm_kzalloc(host->mmc->parent,
1539 sizeof(*cq_host), GFP_KERNEL);
1545 cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
1546 cq_host->ops = &sdhci_arasan_cqhci_ops;
1548 dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1550 cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1552 ret = cqhci_init(cq_host, host->mmc, dma64);
1556 ret = __sdhci_add_host(host);
1563 sdhci_cleanup_host(host);
1567 static int sdhci_arasan_probe(struct platform_device *pdev)
1570 struct device_node *node;
1571 struct clk *clk_xin;
1572 struct sdhci_host *host;
1573 struct sdhci_pltfm_host *pltfm_host;
1574 struct device *dev = &pdev->dev;
1575 struct device_node *np = dev->of_node;
1576 struct sdhci_arasan_data *sdhci_arasan;
1577 const struct sdhci_arasan_of_data *data;
1579 data = of_device_get_match_data(dev);
1583 host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan));
1586 return PTR_ERR(host);
1588 pltfm_host = sdhci_priv(host);
1589 sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1590 sdhci_arasan->host = host;
1592 sdhci_arasan->soc_ctl_map = data->soc_ctl_map;
1593 sdhci_arasan->clk_ops = data->clk_ops;
1595 node = of_parse_phandle(np, "arasan,soc-ctl-syscon", 0);
1597 sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
1600 if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
1601 ret = dev_err_probe(dev,
1602 PTR_ERR(sdhci_arasan->soc_ctl_base),
1603 "Can't get syscon\n");
1604 goto err_pltfm_free;
1608 sdhci_get_of_property(pdev);
1610 sdhci_arasan->clk_ahb = devm_clk_get(dev, "clk_ahb");
1611 if (IS_ERR(sdhci_arasan->clk_ahb)) {
1612 ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->clk_ahb),
1613 "clk_ahb clock not found.\n");
1614 goto err_pltfm_free;
1617 clk_xin = devm_clk_get(dev, "clk_xin");
1618 if (IS_ERR(clk_xin)) {
1619 ret = dev_err_probe(dev, PTR_ERR(clk_xin), "clk_xin clock not found.\n");
1620 goto err_pltfm_free;
1623 ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
1625 dev_err(dev, "Unable to enable AHB clock.\n");
1626 goto err_pltfm_free;
1629 /* If clock-frequency property is set, use the provided value */
1630 if (pltfm_host->clock &&
1631 pltfm_host->clock != clk_get_rate(clk_xin)) {
1632 ret = clk_set_rate(clk_xin, pltfm_host->clock);
1634 dev_err(&pdev->dev, "Failed to set SD clock rate\n");
1639 ret = clk_prepare_enable(clk_xin);
1641 dev_err(dev, "Unable to enable SD clock.\n");
1645 if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
1646 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
1648 if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
1649 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
1651 pltfm_host->clk = clk_xin;
1653 if (of_device_is_compatible(np, "rockchip,rk3399-sdhci-5.1"))
1654 sdhci_arasan_update_clockmultiplier(host, 0x0);
1656 if (of_device_is_compatible(np, "intel,keembay-sdhci-5.1-emmc") ||
1657 of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sd") ||
1658 of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sdio") ||
1659 of_device_is_compatible(np, "intel,thunderbay-sdhci-5.1")) {
1660 sdhci_arasan_update_clockmultiplier(host, 0x0);
1661 sdhci_arasan_update_support64b(host, 0x0);
1663 host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1666 sdhci_arasan_update_baseclkfreq(host);
1668 ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, dev);
1670 goto clk_disable_all;
1672 if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
1673 host->mmc_host_ops.execute_tuning =
1674 arasan_zynqmp_execute_tuning;
1676 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN;
1677 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
1680 arasan_dt_parse_clk_phases(dev, &sdhci_arasan->clk_data);
1682 ret = mmc_of_parse(host->mmc);
1684 ret = dev_err_probe(dev, ret, "parsing dt failed.\n");
1688 sdhci_arasan->phy = ERR_PTR(-ENODEV);
1689 if (of_device_is_compatible(np, "arasan,sdhci-5.1")) {
1690 sdhci_arasan->phy = devm_phy_get(dev, "phy_arasan");
1691 if (IS_ERR(sdhci_arasan->phy)) {
1692 ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->phy),
1693 "No phy for arasan,sdhci-5.1.\n");
1697 ret = phy_init(sdhci_arasan->phy);
1699 dev_err(dev, "phy_init err.\n");
1703 host->mmc_host_ops.hs400_enhanced_strobe =
1704 sdhci_arasan_hs400_enhanced_strobe;
1705 host->mmc_host_ops.start_signal_voltage_switch =
1706 sdhci_arasan_voltage_switch;
1707 sdhci_arasan->has_cqe = true;
1708 host->mmc->caps2 |= MMC_CAP2_CQE;
1710 if (!of_property_read_bool(np, "disable-cqe-dcmd"))
1711 host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
1714 ret = sdhci_arasan_add_host(sdhci_arasan);
1721 if (!IS_ERR(sdhci_arasan->phy))
1722 phy_exit(sdhci_arasan->phy);
1724 sdhci_arasan_unregister_sdclk(dev);
1726 clk_disable_unprepare(clk_xin);
1728 clk_disable_unprepare(sdhci_arasan->clk_ahb);
1730 sdhci_pltfm_free(pdev);
1734 static int sdhci_arasan_remove(struct platform_device *pdev)
1736 struct sdhci_host *host = platform_get_drvdata(pdev);
1737 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1738 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1739 struct clk *clk_ahb = sdhci_arasan->clk_ahb;
1741 if (!IS_ERR(sdhci_arasan->phy)) {
1742 if (sdhci_arasan->is_phy_on)
1743 phy_power_off(sdhci_arasan->phy);
1744 phy_exit(sdhci_arasan->phy);
1747 sdhci_arasan_unregister_sdclk(&pdev->dev);
1749 sdhci_pltfm_unregister(pdev);
1751 clk_disable_unprepare(clk_ahb);
1756 static struct platform_driver sdhci_arasan_driver = {
1758 .name = "sdhci-arasan",
1759 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1760 .of_match_table = sdhci_arasan_of_match,
1761 .pm = &sdhci_arasan_dev_pm_ops,
1763 .probe = sdhci_arasan_probe,
1764 .remove = sdhci_arasan_remove,
1767 module_platform_driver(sdhci_arasan_driver);
1769 MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
1771 MODULE_LICENSE("GPL");