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_OTAPDLY_REGISTER 0xF0FC
35 #define SDHCI_ARASAN_CQE_BASE_ADDR 0x200
36 #define VENDOR_ENHANCED_STROBE BIT(0)
38 #define PHY_CLK_TOO_SLOW_HZ 400000
40 #define SDHCI_ITAPDLY_CHGWIN 0x200
41 #define SDHCI_ITAPDLY_ENABLE 0x100
42 #define SDHCI_OTAPDLY_ENABLE 0x40
44 /* Default settings for ZynqMP Clock Phases */
45 #define ZYNQMP_ICLK_PHASE {0, 63, 63, 0, 63, 0, 0, 183, 54, 0, 0}
46 #define ZYNQMP_OCLK_PHASE {0, 72, 60, 0, 60, 72, 135, 48, 72, 135, 0}
48 #define VERSAL_ICLK_PHASE {0, 132, 132, 0, 132, 0, 0, 162, 90, 0, 0}
49 #define VERSAL_OCLK_PHASE {0, 60, 48, 0, 48, 72, 90, 36, 60, 90, 0}
52 * On some SoCs the syscon area has a feature where the upper 16-bits of
53 * each 32-bit register act as a write mask for the lower 16-bits. This allows
54 * atomic updates of the register without locking. This macro is used on SoCs
55 * that have that feature.
57 #define HIWORD_UPDATE(val, mask, shift) \
58 ((val) << (shift) | (mask) << ((shift) + 16))
61 * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
63 * @reg: Offset within the syscon of the register containing this field
64 * @width: Number of bits for this field
65 * @shift: Bit offset within @reg of this field (or -1 if not avail)
67 struct sdhci_arasan_soc_ctl_field {
74 * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
76 * @baseclkfreq: Where to find corecfg_baseclkfreq
77 * @clockmultiplier: Where to find corecfg_clockmultiplier
78 * @support64b: Where to find SUPPORT64B bit
79 * @hiword_update: If true, use HIWORD_UPDATE to access the syscon
81 * It's up to the licensee of the Arsan IP block to make these available
82 * somewhere if needed. Presumably these will be scattered somewhere that's
83 * accessible via the syscon API.
85 struct sdhci_arasan_soc_ctl_map {
86 struct sdhci_arasan_soc_ctl_field baseclkfreq;
87 struct sdhci_arasan_soc_ctl_field clockmultiplier;
88 struct sdhci_arasan_soc_ctl_field support64b;
93 * struct sdhci_arasan_clk_ops - Clock Operations for Arasan SD controller
95 * @sdcardclk_ops: The output clock related operations
96 * @sampleclk_ops: The sample clock related operations
98 struct sdhci_arasan_clk_ops {
99 const struct clk_ops *sdcardclk_ops;
100 const struct clk_ops *sampleclk_ops;
104 * struct sdhci_arasan_clk_data - Arasan Controller Clock Data.
106 * @sdcardclk_hw: Struct for the clock we might provide to a PHY.
107 * @sdcardclk: Pointer to normal 'struct clock' for sdcardclk_hw.
108 * @sampleclk_hw: Struct for the clock we might provide to a PHY.
109 * @sampleclk: Pointer to normal 'struct clock' for sampleclk_hw.
110 * @clk_phase_in: Array of Input Clock Phase Delays for all speed modes
111 * @clk_phase_out: Array of Output Clock Phase Delays for all speed modes
112 * @set_clk_delays: Function pointer for setting Clock Delays
113 * @clk_of_data: Platform specific runtime clock data storage pointer
115 struct sdhci_arasan_clk_data {
116 struct clk_hw sdcardclk_hw;
117 struct clk *sdcardclk;
118 struct clk_hw sampleclk_hw;
119 struct clk *sampleclk;
120 int clk_phase_in[MMC_TIMING_MMC_HS400 + 1];
121 int clk_phase_out[MMC_TIMING_MMC_HS400 + 1];
122 void (*set_clk_delays)(struct sdhci_host *host);
127 * struct sdhci_arasan_data - Arasan Controller Data
129 * @host: Pointer to the main SDHCI host structure.
130 * @clk_ahb: Pointer to the AHB clock
131 * @phy: Pointer to the generic phy
132 * @is_phy_on: True if the PHY is on; false if not.
133 * @has_cqe: True if controller has command queuing engine.
134 * @clk_data: Struct for the Arasan Controller Clock Data.
135 * @clk_ops: Struct for the Arasan Controller Clock Operations.
136 * @soc_ctl_base: Pointer to regmap for syscon for soc_ctl registers.
137 * @soc_ctl_map: Map to get offsets into soc_ctl registers.
138 * @quirks: Arasan deviations from spec.
140 struct sdhci_arasan_data {
141 struct sdhci_host *host;
147 struct sdhci_arasan_clk_data clk_data;
148 const struct sdhci_arasan_clk_ops *clk_ops;
150 struct regmap *soc_ctl_base;
151 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
154 /* Controller does not have CD wired and will not function normally without */
155 #define SDHCI_ARASAN_QUIRK_FORCE_CDTEST BIT(0)
156 /* Controller immediately reports SDHCI_CLOCK_INT_STABLE after enabling the
157 * internal clock even when the clock isn't stable */
158 #define SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE BIT(1)
161 struct sdhci_arasan_of_data {
162 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
163 const struct sdhci_pltfm_data *pdata;
164 const struct sdhci_arasan_clk_ops *clk_ops;
167 static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
168 .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
169 .clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
170 .hiword_update = true,
173 static const struct sdhci_arasan_soc_ctl_map intel_lgm_emmc_soc_ctl_map = {
174 .baseclkfreq = { .reg = 0xa0, .width = 8, .shift = 2 },
175 .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
176 .hiword_update = false,
179 static const struct sdhci_arasan_soc_ctl_map intel_lgm_sdxc_soc_ctl_map = {
180 .baseclkfreq = { .reg = 0x80, .width = 8, .shift = 2 },
181 .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
182 .hiword_update = false,
185 static const struct sdhci_arasan_soc_ctl_map intel_keembay_soc_ctl_map = {
186 .baseclkfreq = { .reg = 0x0, .width = 8, .shift = 14 },
187 .clockmultiplier = { .reg = 0x4, .width = 8, .shift = 14 },
188 .support64b = { .reg = 0x4, .width = 1, .shift = 24 },
189 .hiword_update = false,
193 * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
195 * @host: The sdhci_host
196 * @fld: The field to write to
197 * @val: The value to write
199 * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
200 * Note that if a field is specified as not available (shift < 0) then
201 * this function will silently return an error code. It will be noisy
202 * and print errors for any other (unexpected) errors.
204 * Return: 0 on success and error value on error
206 static int sdhci_arasan_syscon_write(struct sdhci_host *host,
207 const struct sdhci_arasan_soc_ctl_field *fld,
210 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
211 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
212 struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
214 u16 width = fld->width;
215 s16 shift = fld->shift;
219 * Silently return errors for shift < 0 so caller doesn't have
220 * to check for fields which are optional. For fields that
221 * are required then caller needs to do something special
227 if (sdhci_arasan->soc_ctl_map->hiword_update)
228 ret = regmap_write(soc_ctl_base, reg,
229 HIWORD_UPDATE(val, GENMASK(width, 0),
232 ret = regmap_update_bits(soc_ctl_base, reg,
233 GENMASK(shift + width, shift),
236 /* Yell about (unexpected) regmap errors */
238 pr_warn("%s: Regmap write fail: %d\n",
239 mmc_hostname(host->mmc), ret);
244 static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
246 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
247 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
248 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
249 bool ctrl_phy = false;
251 if (!IS_ERR(sdhci_arasan->phy)) {
252 if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
254 * If PHY off, set clock to max speed and power PHY on.
256 * Although PHY docs apparently suggest power cycling
257 * when changing the clock the PHY doesn't like to be
258 * powered on while at low speeds like those used in ID
259 * mode. Even worse is powering the PHY on while the
262 * To workaround the PHY limitations, the best we can
263 * do is to power it on at a faster speed and then slam
264 * through low speeds without power cycling.
266 sdhci_set_clock(host, host->max_clk);
267 phy_power_on(sdhci_arasan->phy);
268 sdhci_arasan->is_phy_on = true;
271 * We'll now fall through to the below case with
272 * ctrl_phy = false (so we won't turn off/on). The
273 * sdhci_set_clock() will set the real clock.
275 } else if (clock > PHY_CLK_TOO_SLOW_HZ) {
277 * At higher clock speeds the PHY is fine being power
278 * cycled and docs say you _should_ power cycle when
279 * changing clock speeds.
285 if (ctrl_phy && sdhci_arasan->is_phy_on) {
286 phy_power_off(sdhci_arasan->phy);
287 sdhci_arasan->is_phy_on = false;
290 /* Set the Input and Output Clock Phase Delays */
291 if (clk_data->set_clk_delays)
292 clk_data->set_clk_delays(host);
294 sdhci_set_clock(host, clock);
296 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE)
298 * Some controllers immediately report SDHCI_CLOCK_INT_STABLE
299 * after enabling the clock even though the clock is not
300 * stable. Trying to use a clock without waiting here results
301 * in EILSEQ while detecting some older/slower cards. The
302 * chosen delay is the maximum delay from sdhci_set_clock.
307 phy_power_on(sdhci_arasan->phy);
308 sdhci_arasan->is_phy_on = true;
312 static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
316 struct sdhci_host *host = mmc_priv(mmc);
318 vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
319 if (ios->enhanced_strobe)
320 vendor |= VENDOR_ENHANCED_STROBE;
322 vendor &= ~VENDOR_ENHANCED_STROBE;
324 sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
327 static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
330 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
331 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
333 sdhci_reset(host, mask);
335 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
336 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
337 ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
338 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
342 static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
345 switch (ios->signal_voltage) {
346 case MMC_SIGNAL_VOLTAGE_180:
348 * Plese don't switch to 1V8 as arasan,5.1 doesn't
349 * actually refer to this setting to indicate the
350 * signal voltage and the state machine will be broken
351 * actually if we force to enable 1V8. That's something
352 * like broken quirk but we could work around here.
355 case MMC_SIGNAL_VOLTAGE_330:
356 case MMC_SIGNAL_VOLTAGE_120:
357 /* We don't support 3V3 and 1V2 */
364 static const struct sdhci_ops sdhci_arasan_ops = {
365 .set_clock = sdhci_arasan_set_clock,
366 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
367 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
368 .set_bus_width = sdhci_set_bus_width,
369 .reset = sdhci_arasan_reset,
370 .set_uhs_signaling = sdhci_set_uhs_signaling,
371 .set_power = sdhci_set_power_and_bus_voltage,
374 static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
379 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
382 cqhci_irq(host->mmc, intmask, cmd_error, data_error);
387 static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
389 sdhci_dumpregs(mmc_priv(mmc));
392 static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
394 struct sdhci_host *host = mmc_priv(mmc);
397 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
398 while (reg & SDHCI_DATA_AVAILABLE) {
399 sdhci_readl(host, SDHCI_BUFFER);
400 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
403 sdhci_cqe_enable(mmc);
406 static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
407 .enable = sdhci_arasan_cqe_enable,
408 .disable = sdhci_cqe_disable,
409 .dumpregs = sdhci_arasan_dumpregs,
412 static const struct sdhci_ops sdhci_arasan_cqe_ops = {
413 .set_clock = sdhci_arasan_set_clock,
414 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
415 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
416 .set_bus_width = sdhci_set_bus_width,
417 .reset = sdhci_arasan_reset,
418 .set_uhs_signaling = sdhci_set_uhs_signaling,
419 .set_power = sdhci_set_power_and_bus_voltage,
420 .irq = sdhci_arasan_cqhci_irq,
423 static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
424 .ops = &sdhci_arasan_cqe_ops,
425 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
426 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
427 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
430 #ifdef CONFIG_PM_SLEEP
432 * sdhci_arasan_suspend - Suspend method for the driver
433 * @dev: Address of the device structure
435 * Put the device in a low power state.
437 * Return: 0 on success and error value on error
439 static int sdhci_arasan_suspend(struct device *dev)
441 struct sdhci_host *host = dev_get_drvdata(dev);
442 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
443 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
446 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
447 mmc_retune_needed(host->mmc);
449 if (sdhci_arasan->has_cqe) {
450 ret = cqhci_suspend(host->mmc);
455 ret = sdhci_suspend_host(host);
459 if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
460 ret = phy_power_off(sdhci_arasan->phy);
462 dev_err(dev, "Cannot power off phy.\n");
463 sdhci_resume_host(host);
466 sdhci_arasan->is_phy_on = false;
469 clk_disable(pltfm_host->clk);
470 clk_disable(sdhci_arasan->clk_ahb);
476 * sdhci_arasan_resume - Resume method for the driver
477 * @dev: Address of the device structure
479 * Resume operation after suspend
481 * Return: 0 on success and error value on error
483 static int sdhci_arasan_resume(struct device *dev)
485 struct sdhci_host *host = dev_get_drvdata(dev);
486 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
487 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
490 ret = clk_enable(sdhci_arasan->clk_ahb);
492 dev_err(dev, "Cannot enable AHB clock.\n");
496 ret = clk_enable(pltfm_host->clk);
498 dev_err(dev, "Cannot enable SD clock.\n");
502 if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
503 ret = phy_power_on(sdhci_arasan->phy);
505 dev_err(dev, "Cannot power on phy.\n");
508 sdhci_arasan->is_phy_on = true;
511 ret = sdhci_resume_host(host);
513 dev_err(dev, "Cannot resume host.\n");
517 if (sdhci_arasan->has_cqe)
518 return cqhci_resume(host->mmc);
522 #endif /* ! CONFIG_PM_SLEEP */
524 static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
525 sdhci_arasan_resume);
528 * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
530 * @hw: Pointer to the hardware clock structure.
531 * @parent_rate: The parent rate (should be rate of clk_xin).
533 * Return the current actual rate of the SD card clock. This can be used
534 * to communicate with out PHY.
536 * Return: The card clock rate.
538 static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
539 unsigned long parent_rate)
541 struct sdhci_arasan_clk_data *clk_data =
542 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
543 struct sdhci_arasan_data *sdhci_arasan =
544 container_of(clk_data, struct sdhci_arasan_data, clk_data);
545 struct sdhci_host *host = sdhci_arasan->host;
547 return host->mmc->actual_clock;
550 static const struct clk_ops arasan_sdcardclk_ops = {
551 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
555 * sdhci_arasan_sampleclk_recalc_rate - Return the sampling clock rate
557 * @hw: Pointer to the hardware clock structure.
558 * @parent_rate: The parent rate (should be rate of clk_xin).
560 * Return the current actual rate of the sampling clock. This can be used
561 * to communicate with out PHY.
563 * Return: The sample clock rate.
565 static unsigned long sdhci_arasan_sampleclk_recalc_rate(struct clk_hw *hw,
566 unsigned long parent_rate)
568 struct sdhci_arasan_clk_data *clk_data =
569 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
570 struct sdhci_arasan_data *sdhci_arasan =
571 container_of(clk_data, struct sdhci_arasan_data, clk_data);
572 struct sdhci_host *host = sdhci_arasan->host;
574 return host->mmc->actual_clock;
577 static const struct clk_ops arasan_sampleclk_ops = {
578 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
582 * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
584 * @hw: Pointer to the hardware clock structure.
585 * @degrees: The clock phase shift between 0 - 359.
587 * Set the SD Output Clock Tap Delays for Output path
589 * Return: 0 on success and error value on error
591 static int sdhci_zynqmp_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
593 struct sdhci_arasan_clk_data *clk_data =
594 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
595 struct sdhci_arasan_data *sdhci_arasan =
596 container_of(clk_data, struct sdhci_arasan_data, clk_data);
597 struct sdhci_host *host = sdhci_arasan->host;
598 const char *clk_name = clk_hw_get_name(hw);
599 u32 node_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : NODE_SD_1;
600 u8 tap_delay, tap_max = 0;
604 * This is applicable for SDHCI_SPEC_300 and above
605 * ZynqMP does not set phase for <=25MHz clock.
606 * If degrees is zero, no need to do anything.
608 if (host->version < SDHCI_SPEC_300 ||
609 host->timing == MMC_TIMING_LEGACY ||
610 host->timing == MMC_TIMING_UHS_SDR12 || !degrees)
613 switch (host->timing) {
614 case MMC_TIMING_MMC_HS:
615 case MMC_TIMING_SD_HS:
616 case MMC_TIMING_UHS_SDR25:
617 case MMC_TIMING_UHS_DDR50:
618 case MMC_TIMING_MMC_DDR52:
619 /* For 50MHz clock, 30 Taps are available */
622 case MMC_TIMING_UHS_SDR50:
623 /* For 100MHz clock, 15 Taps are available */
626 case MMC_TIMING_UHS_SDR104:
627 case MMC_TIMING_MMC_HS200:
628 /* For 200MHz clock, 8 Taps are available */
634 tap_delay = (degrees * tap_max) / 360;
636 /* Set the Clock Phase */
637 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_OUTPUT, tap_delay);
639 pr_err("Error setting Output Tap Delay\n");
644 static const struct clk_ops zynqmp_sdcardclk_ops = {
645 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
646 .set_phase = sdhci_zynqmp_sdcardclk_set_phase,
650 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
652 * @hw: Pointer to the hardware clock structure.
653 * @degrees: The clock phase shift between 0 - 359.
655 * Set the SD Input Clock Tap Delays for Input path
657 * Return: 0 on success and error value on error
659 static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees)
661 struct sdhci_arasan_clk_data *clk_data =
662 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
663 struct sdhci_arasan_data *sdhci_arasan =
664 container_of(clk_data, struct sdhci_arasan_data, clk_data);
665 struct sdhci_host *host = sdhci_arasan->host;
666 const char *clk_name = clk_hw_get_name(hw);
667 u32 node_id = !strcmp(clk_name, "clk_in_sd0") ? NODE_SD_0 : NODE_SD_1;
668 u8 tap_delay, tap_max = 0;
672 * This is applicable for SDHCI_SPEC_300 and above
673 * ZynqMP does not set phase for <=25MHz clock.
674 * If degrees is zero, no need to do anything.
676 if (host->version < SDHCI_SPEC_300 ||
677 host->timing == MMC_TIMING_LEGACY ||
678 host->timing == MMC_TIMING_UHS_SDR12 || !degrees)
681 switch (host->timing) {
682 case MMC_TIMING_MMC_HS:
683 case MMC_TIMING_SD_HS:
684 case MMC_TIMING_UHS_SDR25:
685 case MMC_TIMING_UHS_DDR50:
686 case MMC_TIMING_MMC_DDR52:
687 /* For 50MHz clock, 120 Taps are available */
690 case MMC_TIMING_UHS_SDR50:
691 /* For 100MHz clock, 60 Taps are available */
694 case MMC_TIMING_UHS_SDR104:
695 case MMC_TIMING_MMC_HS200:
696 /* For 200MHz clock, 30 Taps are available */
702 tap_delay = (degrees * tap_max) / 360;
704 /* Set the Clock Phase */
705 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_INPUT, tap_delay);
707 pr_err("Error setting Input Tap Delay\n");
712 static const struct clk_ops zynqmp_sampleclk_ops = {
713 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
714 .set_phase = sdhci_zynqmp_sampleclk_set_phase,
718 * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
720 * @hw: Pointer to the hardware clock structure.
721 * @degrees: The clock phase shift between 0 - 359.
723 * Set the SD Output Clock Tap Delays for Output path
725 * Return: 0 on success and error value on error
727 static int sdhci_versal_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
729 struct sdhci_arasan_clk_data *clk_data =
730 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
731 struct sdhci_arasan_data *sdhci_arasan =
732 container_of(clk_data, struct sdhci_arasan_data, clk_data);
733 struct sdhci_host *host = sdhci_arasan->host;
734 u8 tap_delay, tap_max = 0;
737 * This is applicable for SDHCI_SPEC_300 and above
738 * Versal does not set phase for <=25MHz clock.
739 * If degrees is zero, no need to do anything.
741 if (host->version < SDHCI_SPEC_300 ||
742 host->timing == MMC_TIMING_LEGACY ||
743 host->timing == MMC_TIMING_UHS_SDR12 || !degrees)
746 switch (host->timing) {
747 case MMC_TIMING_MMC_HS:
748 case MMC_TIMING_SD_HS:
749 case MMC_TIMING_UHS_SDR25:
750 case MMC_TIMING_UHS_DDR50:
751 case MMC_TIMING_MMC_DDR52:
752 /* For 50MHz clock, 30 Taps are available */
755 case MMC_TIMING_UHS_SDR50:
756 /* For 100MHz clock, 15 Taps are available */
759 case MMC_TIMING_UHS_SDR104:
760 case MMC_TIMING_MMC_HS200:
761 /* For 200MHz clock, 8 Taps are available */
767 tap_delay = (degrees * tap_max) / 360;
769 /* Set the Clock Phase */
773 regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
774 regval |= SDHCI_OTAPDLY_ENABLE;
775 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
777 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
783 static const struct clk_ops versal_sdcardclk_ops = {
784 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
785 .set_phase = sdhci_versal_sdcardclk_set_phase,
789 * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
791 * @hw: Pointer to the hardware clock structure.
792 * @degrees: The clock phase shift between 0 - 359.
794 * Set the SD Input Clock Tap Delays for Input path
796 * Return: 0 on success and error value on error
798 static int sdhci_versal_sampleclk_set_phase(struct clk_hw *hw, int degrees)
800 struct sdhci_arasan_clk_data *clk_data =
801 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
802 struct sdhci_arasan_data *sdhci_arasan =
803 container_of(clk_data, struct sdhci_arasan_data, clk_data);
804 struct sdhci_host *host = sdhci_arasan->host;
805 u8 tap_delay, tap_max = 0;
808 * This is applicable for SDHCI_SPEC_300 and above
809 * Versal does not set phase for <=25MHz clock.
810 * If degrees is zero, no need to do anything.
812 if (host->version < SDHCI_SPEC_300 ||
813 host->timing == MMC_TIMING_LEGACY ||
814 host->timing == MMC_TIMING_UHS_SDR12 || !degrees)
817 switch (host->timing) {
818 case MMC_TIMING_MMC_HS:
819 case MMC_TIMING_SD_HS:
820 case MMC_TIMING_UHS_SDR25:
821 case MMC_TIMING_UHS_DDR50:
822 case MMC_TIMING_MMC_DDR52:
823 /* For 50MHz clock, 120 Taps are available */
826 case MMC_TIMING_UHS_SDR50:
827 /* For 100MHz clock, 60 Taps are available */
830 case MMC_TIMING_UHS_SDR104:
831 case MMC_TIMING_MMC_HS200:
832 /* For 200MHz clock, 30 Taps are available */
838 tap_delay = (degrees * tap_max) / 360;
840 /* Set the Clock Phase */
844 regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
845 regval |= SDHCI_ITAPDLY_CHGWIN;
846 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
847 regval |= SDHCI_ITAPDLY_ENABLE;
848 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
850 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
851 regval &= ~SDHCI_ITAPDLY_CHGWIN;
852 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
858 static const struct clk_ops versal_sampleclk_ops = {
859 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
860 .set_phase = sdhci_versal_sampleclk_set_phase,
863 static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 deviceid)
867 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
868 clk &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN);
869 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
871 /* Issue DLL Reset */
872 zynqmp_pm_sd_dll_reset(deviceid, PM_DLL_RESET_PULSE);
874 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
876 sdhci_enable_clk(host, clk);
879 static int arasan_zynqmp_execute_tuning(struct mmc_host *mmc, u32 opcode)
881 struct sdhci_host *host = mmc_priv(mmc);
882 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
883 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
884 struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw;
885 const char *clk_name = clk_hw_get_name(hw);
886 u32 device_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 :
890 arasan_zynqmp_dll_reset(host, device_id);
892 err = sdhci_execute_tuning(mmc, opcode);
896 arasan_zynqmp_dll_reset(host, device_id);
902 * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
904 * @host: The sdhci_host
905 * @value: The value to write
907 * The corecfg_clockmultiplier is supposed to contain clock multiplier
908 * value of programmable clock generator.
911 * - Many existing devices don't seem to do this and work fine. To keep
912 * compatibility for old hardware where the device tree doesn't provide a
913 * register map, this function is a noop if a soc_ctl_map hasn't been provided
915 * - The value of corecfg_clockmultiplier should sync with that of corresponding
916 * value reading from sdhci_capability_register. So this function is called
917 * once at probe time and never called again.
919 static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
922 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
923 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
924 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
925 sdhci_arasan->soc_ctl_map;
927 /* Having a map is optional */
931 /* If we have a map, we expect to have a syscon */
932 if (!sdhci_arasan->soc_ctl_base) {
933 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
934 mmc_hostname(host->mmc));
938 sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
942 * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
944 * @host: The sdhci_host
946 * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This
947 * function can be used to make that happen.
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 * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
955 * to achieve lower clock rates. That means that this function is called once
956 * at probe time and never called again.
958 static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
960 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
961 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
962 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
963 sdhci_arasan->soc_ctl_map;
964 u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000);
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->baseclkfreq, mhz);
980 static void sdhci_arasan_set_clk_delays(struct sdhci_host *host)
982 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
983 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
984 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
986 clk_set_phase(clk_data->sampleclk,
987 clk_data->clk_phase_in[host->timing]);
988 clk_set_phase(clk_data->sdcardclk,
989 clk_data->clk_phase_out[host->timing]);
992 static void arasan_dt_read_clk_phase(struct device *dev,
993 struct sdhci_arasan_clk_data *clk_data,
994 unsigned int timing, const char *prop)
996 struct device_node *np = dev->of_node;
998 int clk_phase[2] = {0};
1001 * Read Tap Delay values from DT, if the DT does not contain the
1002 * Tap Values then use the pre-defined values.
1004 if (of_property_read_variable_u32_array(np, prop, &clk_phase[0],
1006 dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
1007 prop, clk_data->clk_phase_in[timing],
1008 clk_data->clk_phase_out[timing]);
1012 /* The values read are Input and Output Clock Delays in order */
1013 clk_data->clk_phase_in[timing] = clk_phase[0];
1014 clk_data->clk_phase_out[timing] = clk_phase[1];
1018 * arasan_dt_parse_clk_phases - Read Clock Delay values from DT
1020 * @dev: Pointer to our struct device.
1021 * @clk_data: Pointer to the Clock Data structure
1023 * Called at initialization to parse the values of Clock Delays.
1025 static void arasan_dt_parse_clk_phases(struct device *dev,
1026 struct sdhci_arasan_clk_data *clk_data)
1028 int *iclk_phase, *oclk_phase;
1033 * This has been kept as a pointer and is assigned a function here.
1034 * So that different controller variants can assign their own handling
1037 clk_data->set_clk_delays = sdhci_arasan_set_clk_delays;
1039 if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) {
1040 iclk_phase = (int [MMC_TIMING_MMC_HS400 + 1]) ZYNQMP_ICLK_PHASE;
1041 oclk_phase = (int [MMC_TIMING_MMC_HS400 + 1]) ZYNQMP_OCLK_PHASE;
1043 of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank);
1044 if (mio_bank == 2) {
1045 oclk_phase[MMC_TIMING_UHS_SDR104] = 90;
1046 oclk_phase[MMC_TIMING_MMC_HS200] = 90;
1049 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1050 clk_data->clk_phase_in[i] = iclk_phase[i];
1051 clk_data->clk_phase_out[i] = oclk_phase[i];
1055 if (of_device_is_compatible(dev->of_node, "xlnx,versal-8.9a")) {
1056 iclk_phase = (int [MMC_TIMING_MMC_HS400 + 1]) VERSAL_ICLK_PHASE;
1057 oclk_phase = (int [MMC_TIMING_MMC_HS400 + 1]) VERSAL_OCLK_PHASE;
1059 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1060 clk_data->clk_phase_in[i] = iclk_phase[i];
1061 clk_data->clk_phase_out[i] = oclk_phase[i];
1065 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY,
1066 "clk-phase-legacy");
1067 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS,
1068 "clk-phase-mmc-hs");
1069 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS,
1071 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12,
1072 "clk-phase-uhs-sdr12");
1073 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25,
1074 "clk-phase-uhs-sdr25");
1075 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50,
1076 "clk-phase-uhs-sdr50");
1077 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104,
1078 "clk-phase-uhs-sdr104");
1079 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50,
1080 "clk-phase-uhs-ddr50");
1081 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52,
1082 "clk-phase-mmc-ddr52");
1083 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200,
1084 "clk-phase-mmc-hs200");
1085 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400,
1086 "clk-phase-mmc-hs400");
1089 static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
1090 .ops = &sdhci_arasan_ops,
1091 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1092 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1093 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1094 SDHCI_QUIRK2_STOP_WITH_TC,
1097 static const struct sdhci_arasan_clk_ops arasan_clk_ops = {
1098 .sdcardclk_ops = &arasan_sdcardclk_ops,
1099 .sampleclk_ops = &arasan_sampleclk_ops,
1102 static struct sdhci_arasan_of_data sdhci_arasan_generic_data = {
1103 .pdata = &sdhci_arasan_pdata,
1104 .clk_ops = &arasan_clk_ops,
1107 static const struct sdhci_pltfm_data sdhci_keembay_emmc_pdata = {
1108 .ops = &sdhci_arasan_cqe_ops,
1109 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1110 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1111 SDHCI_QUIRK_NO_LED |
1112 SDHCI_QUIRK_32BIT_DMA_ADDR |
1113 SDHCI_QUIRK_32BIT_DMA_SIZE |
1114 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1115 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1116 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1117 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
1118 SDHCI_QUIRK2_STOP_WITH_TC |
1119 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1122 static const struct sdhci_pltfm_data sdhci_keembay_sd_pdata = {
1123 .ops = &sdhci_arasan_ops,
1124 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1125 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1126 SDHCI_QUIRK_NO_LED |
1127 SDHCI_QUIRK_32BIT_DMA_ADDR |
1128 SDHCI_QUIRK_32BIT_DMA_SIZE |
1129 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1130 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1131 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1132 SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
1133 SDHCI_QUIRK2_STOP_WITH_TC |
1134 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1137 static const struct sdhci_pltfm_data sdhci_keembay_sdio_pdata = {
1138 .ops = &sdhci_arasan_ops,
1139 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1140 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1141 SDHCI_QUIRK_NO_LED |
1142 SDHCI_QUIRK_32BIT_DMA_ADDR |
1143 SDHCI_QUIRK_32BIT_DMA_SIZE |
1144 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1145 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1146 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1147 SDHCI_QUIRK2_HOST_OFF_CARD_ON |
1148 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1151 static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = {
1152 .soc_ctl_map = &rk3399_soc_ctl_map,
1153 .pdata = &sdhci_arasan_cqe_pdata,
1154 .clk_ops = &arasan_clk_ops,
1157 static struct sdhci_arasan_of_data intel_lgm_emmc_data = {
1158 .soc_ctl_map = &intel_lgm_emmc_soc_ctl_map,
1159 .pdata = &sdhci_arasan_cqe_pdata,
1160 .clk_ops = &arasan_clk_ops,
1163 static struct sdhci_arasan_of_data intel_lgm_sdxc_data = {
1164 .soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map,
1165 .pdata = &sdhci_arasan_cqe_pdata,
1166 .clk_ops = &arasan_clk_ops,
1169 static const struct sdhci_pltfm_data sdhci_arasan_zynqmp_pdata = {
1170 .ops = &sdhci_arasan_ops,
1171 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1172 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1173 SDHCI_QUIRK2_STOP_WITH_TC,
1176 static const struct sdhci_arasan_clk_ops zynqmp_clk_ops = {
1177 .sdcardclk_ops = &zynqmp_sdcardclk_ops,
1178 .sampleclk_ops = &zynqmp_sampleclk_ops,
1181 static struct sdhci_arasan_of_data sdhci_arasan_zynqmp_data = {
1182 .pdata = &sdhci_arasan_zynqmp_pdata,
1183 .clk_ops = &zynqmp_clk_ops,
1186 static const struct sdhci_arasan_clk_ops versal_clk_ops = {
1187 .sdcardclk_ops = &versal_sdcardclk_ops,
1188 .sampleclk_ops = &versal_sampleclk_ops,
1191 static struct sdhci_arasan_of_data sdhci_arasan_versal_data = {
1192 .pdata = &sdhci_arasan_zynqmp_pdata,
1193 .clk_ops = &versal_clk_ops,
1196 static struct sdhci_arasan_of_data intel_keembay_emmc_data = {
1197 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1198 .pdata = &sdhci_keembay_emmc_pdata,
1201 static struct sdhci_arasan_of_data intel_keembay_sd_data = {
1202 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1203 .pdata = &sdhci_keembay_sd_pdata,
1206 static struct sdhci_arasan_of_data intel_keembay_sdio_data = {
1207 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1208 .pdata = &sdhci_keembay_sdio_pdata,
1211 static const struct of_device_id sdhci_arasan_of_match[] = {
1212 /* SoC-specific compatible strings w/ soc_ctl_map */
1214 .compatible = "rockchip,rk3399-sdhci-5.1",
1215 .data = &sdhci_arasan_rk3399_data,
1218 .compatible = "intel,lgm-sdhci-5.1-emmc",
1219 .data = &intel_lgm_emmc_data,
1222 .compatible = "intel,lgm-sdhci-5.1-sdxc",
1223 .data = &intel_lgm_sdxc_data,
1226 .compatible = "intel,keembay-sdhci-5.1-emmc",
1227 .data = &intel_keembay_emmc_data,
1230 .compatible = "intel,keembay-sdhci-5.1-sd",
1231 .data = &intel_keembay_sd_data,
1234 .compatible = "intel,keembay-sdhci-5.1-sdio",
1235 .data = &intel_keembay_sdio_data,
1237 /* Generic compatible below here */
1239 .compatible = "arasan,sdhci-8.9a",
1240 .data = &sdhci_arasan_generic_data,
1243 .compatible = "arasan,sdhci-5.1",
1244 .data = &sdhci_arasan_generic_data,
1247 .compatible = "arasan,sdhci-4.9a",
1248 .data = &sdhci_arasan_generic_data,
1251 .compatible = "xlnx,zynqmp-8.9a",
1252 .data = &sdhci_arasan_zynqmp_data,
1255 .compatible = "xlnx,versal-8.9a",
1256 .data = &sdhci_arasan_versal_data,
1260 MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
1263 * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use
1265 * @sdhci_arasan: Our private data structure.
1266 * @clk_xin: Pointer to the functional clock
1267 * @dev: Pointer to our struct device.
1269 * Some PHY devices need to know what the actual card clock is. In order for
1270 * them to find out, we'll provide a clock through the common clock framework
1271 * for them to query.
1273 * Return: 0 on success and error value on error
1276 sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan,
1277 struct clk *clk_xin,
1280 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1281 struct device_node *np = dev->of_node;
1282 struct clk_init_data sdcardclk_init;
1283 const char *parent_clk_name;
1286 ret = of_property_read_string_index(np, "clock-output-names", 0,
1287 &sdcardclk_init.name);
1289 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1293 parent_clk_name = __clk_get_name(clk_xin);
1294 sdcardclk_init.parent_names = &parent_clk_name;
1295 sdcardclk_init.num_parents = 1;
1296 sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
1297 sdcardclk_init.ops = sdhci_arasan->clk_ops->sdcardclk_ops;
1299 clk_data->sdcardclk_hw.init = &sdcardclk_init;
1300 clk_data->sdcardclk =
1301 devm_clk_register(dev, &clk_data->sdcardclk_hw);
1302 clk_data->sdcardclk_hw.init = NULL;
1304 ret = of_clk_add_provider(np, of_clk_src_simple_get,
1305 clk_data->sdcardclk);
1307 dev_err(dev, "Failed to add sdcard clock provider\n");
1313 * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use
1315 * @sdhci_arasan: Our private data structure.
1316 * @clk_xin: Pointer to the functional clock
1317 * @dev: Pointer to our struct device.
1319 * Some PHY devices need to know what the actual card clock is. In order for
1320 * them to find out, we'll provide a clock through the common clock framework
1321 * for them to query.
1323 * Return: 0 on success and error value on error
1326 sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan,
1327 struct clk *clk_xin,
1330 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1331 struct device_node *np = dev->of_node;
1332 struct clk_init_data sampleclk_init;
1333 const char *parent_clk_name;
1336 ret = of_property_read_string_index(np, "clock-output-names", 1,
1337 &sampleclk_init.name);
1339 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1343 parent_clk_name = __clk_get_name(clk_xin);
1344 sampleclk_init.parent_names = &parent_clk_name;
1345 sampleclk_init.num_parents = 1;
1346 sampleclk_init.flags = CLK_GET_RATE_NOCACHE;
1347 sampleclk_init.ops = sdhci_arasan->clk_ops->sampleclk_ops;
1349 clk_data->sampleclk_hw.init = &sampleclk_init;
1350 clk_data->sampleclk =
1351 devm_clk_register(dev, &clk_data->sampleclk_hw);
1352 clk_data->sampleclk_hw.init = NULL;
1354 ret = of_clk_add_provider(np, of_clk_src_simple_get,
1355 clk_data->sampleclk);
1357 dev_err(dev, "Failed to add sample clock provider\n");
1363 * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
1365 * @dev: Pointer to our struct device.
1367 * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
1370 static void sdhci_arasan_unregister_sdclk(struct device *dev)
1372 struct device_node *np = dev->of_node;
1374 if (!of_find_property(np, "#clock-cells", NULL))
1377 of_clk_del_provider(dev->of_node);
1381 * sdhci_arasan_update_support64b - Set SUPPORT_64B (64-bit System Bus Support)
1383 * This should be set based on the System Address Bus.
1384 * 0: the Core supports only 32-bit System Address Bus.
1385 * 1: the Core supports 64-bit System Address Bus.
1388 * - For Keem Bay, it is required to clear this bit. Its default value is 1'b1.
1389 * Keem Bay does not support 64-bit access.
1391 * @host The sdhci_host
1393 static void sdhci_arasan_update_support64b(struct sdhci_host *host, u32 value)
1395 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1396 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1397 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
1398 sdhci_arasan->soc_ctl_map;
1400 /* Having a map is optional */
1404 /* If we have a map, we expect to have a syscon */
1405 if (!sdhci_arasan->soc_ctl_base) {
1406 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1407 mmc_hostname(host->mmc));
1411 sdhci_arasan_syscon_write(host, &soc_ctl_map->support64b, value);
1415 * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use
1417 * @sdhci_arasan: Our private data structure.
1418 * @clk_xin: Pointer to the functional clock
1419 * @dev: Pointer to our struct device.
1421 * Some PHY devices need to know what the actual card clock is. In order for
1422 * them to find out, we'll provide a clock through the common clock framework
1423 * for them to query.
1425 * Note: without seriously re-architecting SDHCI's clock code and testing on
1426 * all platforms, there's no way to create a totally beautiful clock here
1427 * with all clock ops implemented. Instead, we'll just create a clock that can
1428 * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
1429 * framework that we're doing things behind its back. This should be sufficient
1430 * to create nice clean device tree bindings and later (if needed) we can try
1431 * re-architecting SDHCI if we see some benefit to it.
1433 * Return: 0 on success and error value on error
1435 static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
1436 struct clk *clk_xin,
1439 struct device_node *np = dev->of_node;
1443 /* Providing a clock to the PHY is optional; no error if missing */
1444 if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0)
1447 ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev);
1452 ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin,
1455 sdhci_arasan_unregister_sdclk(dev);
1463 static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
1465 struct sdhci_host *host = sdhci_arasan->host;
1466 struct cqhci_host *cq_host;
1470 if (!sdhci_arasan->has_cqe)
1471 return sdhci_add_host(host);
1473 ret = sdhci_setup_host(host);
1477 cq_host = devm_kzalloc(host->mmc->parent,
1478 sizeof(*cq_host), GFP_KERNEL);
1484 cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
1485 cq_host->ops = &sdhci_arasan_cqhci_ops;
1487 dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1489 cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1491 ret = cqhci_init(cq_host, host->mmc, dma64);
1495 ret = __sdhci_add_host(host);
1502 sdhci_cleanup_host(host);
1506 static int sdhci_arasan_probe(struct platform_device *pdev)
1509 const struct of_device_id *match;
1510 struct device_node *node;
1511 struct clk *clk_xin;
1512 struct sdhci_host *host;
1513 struct sdhci_pltfm_host *pltfm_host;
1514 struct sdhci_arasan_data *sdhci_arasan;
1515 struct device_node *np = pdev->dev.of_node;
1516 const struct sdhci_arasan_of_data *data;
1518 match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node);
1520 host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan));
1523 return PTR_ERR(host);
1525 pltfm_host = sdhci_priv(host);
1526 sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1527 sdhci_arasan->host = host;
1529 sdhci_arasan->soc_ctl_map = data->soc_ctl_map;
1530 sdhci_arasan->clk_ops = data->clk_ops;
1532 node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0);
1534 sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
1537 if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
1538 ret = PTR_ERR(sdhci_arasan->soc_ctl_base);
1539 if (ret != -EPROBE_DEFER)
1540 dev_err(&pdev->dev, "Can't get syscon: %d\n",
1542 goto err_pltfm_free;
1546 sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb");
1547 if (IS_ERR(sdhci_arasan->clk_ahb)) {
1548 dev_err(&pdev->dev, "clk_ahb clock not found.\n");
1549 ret = PTR_ERR(sdhci_arasan->clk_ahb);
1550 goto err_pltfm_free;
1553 clk_xin = devm_clk_get(&pdev->dev, "clk_xin");
1554 if (IS_ERR(clk_xin)) {
1555 dev_err(&pdev->dev, "clk_xin clock not found.\n");
1556 ret = PTR_ERR(clk_xin);
1557 goto err_pltfm_free;
1560 ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
1562 dev_err(&pdev->dev, "Unable to enable AHB clock.\n");
1563 goto err_pltfm_free;
1566 ret = clk_prepare_enable(clk_xin);
1568 dev_err(&pdev->dev, "Unable to enable SD clock.\n");
1572 sdhci_get_of_property(pdev);
1574 if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
1575 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
1577 if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
1578 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
1580 pltfm_host->clk = clk_xin;
1582 if (of_device_is_compatible(pdev->dev.of_node,
1583 "rockchip,rk3399-sdhci-5.1"))
1584 sdhci_arasan_update_clockmultiplier(host, 0x0);
1586 if (of_device_is_compatible(np, "intel,keembay-sdhci-5.1-emmc") ||
1587 of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sd") ||
1588 of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sdio")) {
1589 sdhci_arasan_update_clockmultiplier(host, 0x0);
1590 sdhci_arasan_update_support64b(host, 0x0);
1592 host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1595 sdhci_arasan_update_baseclkfreq(host);
1597 ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev);
1599 goto clk_disable_all;
1601 if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
1602 host->mmc_host_ops.execute_tuning =
1603 arasan_zynqmp_execute_tuning;
1606 arasan_dt_parse_clk_phases(&pdev->dev, &sdhci_arasan->clk_data);
1608 ret = mmc_of_parse(host->mmc);
1610 if (ret != -EPROBE_DEFER)
1611 dev_err(&pdev->dev, "parsing dt failed (%d)\n", ret);
1615 sdhci_arasan->phy = ERR_PTR(-ENODEV);
1616 if (of_device_is_compatible(pdev->dev.of_node,
1617 "arasan,sdhci-5.1")) {
1618 sdhci_arasan->phy = devm_phy_get(&pdev->dev,
1620 if (IS_ERR(sdhci_arasan->phy)) {
1621 ret = PTR_ERR(sdhci_arasan->phy);
1622 dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n");
1626 ret = phy_init(sdhci_arasan->phy);
1628 dev_err(&pdev->dev, "phy_init err.\n");
1632 host->mmc_host_ops.hs400_enhanced_strobe =
1633 sdhci_arasan_hs400_enhanced_strobe;
1634 host->mmc_host_ops.start_signal_voltage_switch =
1635 sdhci_arasan_voltage_switch;
1636 sdhci_arasan->has_cqe = true;
1637 host->mmc->caps2 |= MMC_CAP2_CQE;
1639 if (!of_property_read_bool(np, "disable-cqe-dcmd"))
1640 host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
1643 ret = sdhci_arasan_add_host(sdhci_arasan);
1650 if (!IS_ERR(sdhci_arasan->phy))
1651 phy_exit(sdhci_arasan->phy);
1653 sdhci_arasan_unregister_sdclk(&pdev->dev);
1655 clk_disable_unprepare(clk_xin);
1657 clk_disable_unprepare(sdhci_arasan->clk_ahb);
1659 sdhci_pltfm_free(pdev);
1663 static int sdhci_arasan_remove(struct platform_device *pdev)
1666 struct sdhci_host *host = platform_get_drvdata(pdev);
1667 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1668 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1669 struct clk *clk_ahb = sdhci_arasan->clk_ahb;
1671 if (!IS_ERR(sdhci_arasan->phy)) {
1672 if (sdhci_arasan->is_phy_on)
1673 phy_power_off(sdhci_arasan->phy);
1674 phy_exit(sdhci_arasan->phy);
1677 sdhci_arasan_unregister_sdclk(&pdev->dev);
1679 ret = sdhci_pltfm_unregister(pdev);
1681 clk_disable_unprepare(clk_ahb);
1686 static struct platform_driver sdhci_arasan_driver = {
1688 .name = "sdhci-arasan",
1689 .of_match_table = sdhci_arasan_of_match,
1690 .pm = &sdhci_arasan_dev_pm_ops,
1692 .probe = sdhci_arasan_probe,
1693 .remove = sdhci_arasan_remove,
1696 module_platform_driver(sdhci_arasan_driver);
1698 MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
1699 MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
1700 MODULE_LICENSE("GPL");