1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2013 - 2015 Xilinx, Inc.
5 * Xilinx Zynq SD Host Controller Interface
12 #include <linux/delay.h>
13 #include "mmc_private.h"
16 #include <dm/device_compat.h>
17 #include <linux/err.h>
18 #include <linux/libfdt.h>
19 #include <asm/types.h>
20 #include <linux/math64.h>
21 #include <asm/cache.h>
24 #include <zynqmp_firmware.h>
26 #define SDHCI_ARASAN_ITAPDLY_REGISTER 0xF0F8
27 #define SDHCI_ARASAN_ITAPDLY_SEL_MASK GENMASK(7, 0)
28 #define SDHCI_ARASAN_OTAPDLY_REGISTER 0xF0FC
29 #define SDHCI_ARASAN_OTAPDLY_SEL_MASK GENMASK(5, 0)
30 #define SDHCI_ITAPDLY_CHGWIN BIT(9)
31 #define SDHCI_ITAPDLY_ENABLE BIT(8)
32 #define SDHCI_OTAPDLY_ENABLE BIT(6)
34 #define SDHCI_TUNING_LOOP_COUNT 40
37 #define SD_DLL_CTRL 0xFF180358
38 #define SD_ITAP_DLY 0xFF180314
39 #define SD_OTAP_DLY 0xFF180318
40 #define SD0_DLL_RST BIT(2)
41 #define SD1_DLL_RST BIT(18)
42 #define SD0_ITAPCHGWIN BIT(9)
43 #define SD1_ITAPCHGWIN BIT(25)
44 #define SD0_ITAPDLYENA BIT(8)
45 #define SD1_ITAPDLYENA BIT(24)
46 #define SD0_ITAPDLYSEL_MASK GENMASK(7, 0)
47 #define SD1_ITAPDLYSEL_MASK GENMASK(23, 16)
48 #define SD0_OTAPDLYSEL_MASK GENMASK(5, 0)
49 #define SD1_OTAPDLYSEL_MASK GENMASK(21, 16)
51 struct arasan_sdhci_clk_data {
52 int clk_phase_in[MMC_TIMING_MMC_HS400 + 1];
53 int clk_phase_out[MMC_TIMING_MMC_HS400 + 1];
56 struct arasan_sdhci_plat {
57 struct mmc_config cfg;
61 struct arasan_sdhci_priv {
62 struct sdhci_host *host;
63 struct arasan_sdhci_clk_data clk_data;
67 struct reset_ctl_bulk resets;
70 /* For Versal platforms zynqmp_mmio_write() won't be available */
71 __weak int zynqmp_mmio_write(const u32 address, const u32 mask, const u32 value)
76 __weak int xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
77 u32 arg3, u32 *ret_payload)
82 __weak int zynqmp_pm_is_function_supported(const u32 api_id, const u32 id)
87 #if defined(CONFIG_ARCH_ZYNQMP) || defined(CONFIG_ARCH_VERSAL)
88 /* Default settings for ZynqMP Clock Phases */
89 static const u32 zynqmp_iclk_phases[] = {0, 63, 63, 0, 63, 0,
91 static const u32 zynqmp_oclk_phases[] = {0, 72, 60, 0, 60, 72,
94 /* Default settings for Versal Clock Phases */
95 static const u32 versal_iclk_phases[] = {0, 132, 132, 0, 132,
97 static const u32 versal_oclk_phases[] = {0, 60, 48, 0, 48, 72,
100 static const u8 mode2timing[] = {
101 [MMC_LEGACY] = MMC_TIMING_LEGACY,
102 [MMC_HS] = MMC_TIMING_MMC_HS,
103 [SD_HS] = MMC_TIMING_SD_HS,
104 [MMC_HS_52] = MMC_TIMING_MMC_HS,
105 [MMC_DDR_52] = MMC_TIMING_MMC_DDR52,
106 [UHS_SDR12] = MMC_TIMING_UHS_SDR12,
107 [UHS_SDR25] = MMC_TIMING_UHS_SDR25,
108 [UHS_SDR50] = MMC_TIMING_UHS_SDR50,
109 [UHS_DDR50] = MMC_TIMING_UHS_DDR50,
110 [UHS_SDR104] = MMC_TIMING_UHS_SDR104,
111 [MMC_HS_200] = MMC_TIMING_MMC_HS200,
114 static inline int arasan_zynqmp_set_in_tapdelay(u32 node_id, u32 itap_delay)
118 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
119 if (node_id == NODE_SD_0) {
120 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPCHGWIN,
125 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPDLYENA,
130 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPDLYSEL_MASK,
135 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPCHGWIN, 0);
139 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPCHGWIN,
144 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPDLYENA,
149 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPDLYSEL_MASK,
154 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPCHGWIN, 0);
158 return xilinx_pm_request(PM_IOCTL, node_id,
159 IOCTL_SET_SD_TAPDELAY,
160 PM_TAPDELAY_INPUT, itap_delay, NULL);
166 static inline int arasan_zynqmp_set_out_tapdelay(u32 node_id, u32 otap_delay)
168 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
169 if (node_id == NODE_SD_0)
170 return zynqmp_mmio_write(SD_OTAP_DLY,
174 return zynqmp_mmio_write(SD_OTAP_DLY, SD1_OTAPDLYSEL_MASK,
177 return xilinx_pm_request(PM_IOCTL, node_id,
178 IOCTL_SET_SD_TAPDELAY,
179 PM_TAPDELAY_OUTPUT, otap_delay, NULL);
183 static inline int zynqmp_dll_reset(u32 node_id, u32 type)
185 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
186 if (node_id == NODE_SD_0)
187 return zynqmp_mmio_write(SD_DLL_CTRL, SD0_DLL_RST,
188 type == PM_DLL_RESET_ASSERT ?
191 return zynqmp_mmio_write(SD_DLL_CTRL, SD1_DLL_RST,
192 type == PM_DLL_RESET_ASSERT ?
195 return xilinx_pm_request(PM_IOCTL, node_id,
196 IOCTL_SD_DLL_RESET, type, 0, NULL);
200 static int arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 node_id)
202 struct mmc *mmc = (struct mmc *)host->mmc;
203 struct udevice *dev = mmc->dev;
204 unsigned long timeout;
208 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
209 clk &= ~(SDHCI_CLOCK_CARD_EN);
210 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
212 /* Issue DLL Reset */
213 ret = zynqmp_dll_reset(node_id, PM_DLL_RESET_ASSERT);
215 dev_err(dev, "dll_reset assert failed with err: %d\n", ret);
219 /* Allow atleast 1ms delay for proper DLL reset */
221 ret = zynqmp_dll_reset(node_id, PM_DLL_RESET_RELEASE);
223 dev_err(dev, "dll_reset release failed with err: %d\n", ret);
229 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
230 & SDHCI_CLOCK_INT_STABLE)) {
232 dev_err(dev, ": Internal clock never stabilised.\n");
239 clk |= SDHCI_CLOCK_CARD_EN;
240 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
245 static int arasan_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
248 struct mmc_data data;
250 struct sdhci_host *host;
251 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
252 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
254 dev_dbg(mmc->dev, "%s\n", __func__);
258 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
259 ctrl |= SDHCI_CTRL_EXEC_TUNING;
260 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
264 arasan_zynqmp_dll_reset(host, priv->node_id);
266 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
267 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
271 cmd.resp_type = MMC_RSP_R1;
276 data.flags = MMC_DATA_READ;
278 if (tuning_loop_counter-- == 0)
281 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200 &&
283 data.blocksize = 128;
285 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
288 sdhci_writew(host, data.blocks, SDHCI_BLOCK_COUNT);
289 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
291 mmc_send_cmd(mmc, &cmd, NULL);
292 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
294 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK)
297 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
299 if (tuning_loop_counter < 0) {
300 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
301 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
304 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
305 printf("%s:Tuning failed\n", __func__);
310 arasan_zynqmp_dll_reset(host, priv->node_id);
312 /* Enable only interrupts served by the SD controller */
313 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
315 /* Mask all sdhci interrupt sources */
316 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
322 * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
324 * @host: Pointer to the sdhci_host structure.
325 * @degrees: The clock phase shift between 0 - 359.
328 * Set the SD Output Clock Tap Delays for Output path
330 static int sdhci_zynqmp_sdcardclk_set_phase(struct sdhci_host *host,
333 struct mmc *mmc = (struct mmc *)host->mmc;
334 struct udevice *dev = mmc->dev;
335 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
336 u8 tap_delay, tap_max = 0;
337 int timing = mode2timing[mmc->selected_mode];
341 * This is applicable for SDHCI_SPEC_300 and above
342 * ZynqMP does not set phase for <=25MHz clock.
343 * If degrees is zero, no need to do anything.
345 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
349 case MMC_TIMING_MMC_HS:
350 case MMC_TIMING_SD_HS:
351 case MMC_TIMING_UHS_SDR25:
352 case MMC_TIMING_UHS_DDR50:
353 case MMC_TIMING_MMC_DDR52:
354 /* For 50MHz clock, 30 Taps are available */
357 case MMC_TIMING_UHS_SDR50:
358 /* For 100MHz clock, 15 Taps are available */
361 case MMC_TIMING_UHS_SDR104:
362 case MMC_TIMING_MMC_HS200:
363 /* For 200MHz clock, 8 Taps are available */
369 tap_delay = (degrees * tap_max) / 360;
371 /* Limit output tap_delay value to 6 bits */
372 tap_delay &= SDHCI_ARASAN_OTAPDLY_SEL_MASK;
374 /* Set the Clock Phase */
375 ret = arasan_zynqmp_set_out_tapdelay(priv->node_id, tap_delay);
377 dev_err(dev, "Error setting output Tap Delay\n");
381 /* Release DLL Reset */
382 ret = zynqmp_dll_reset(priv->node_id, PM_DLL_RESET_RELEASE);
384 dev_err(dev, "dll_reset release failed with err: %d\n", ret);
392 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
394 * @host: Pointer to the sdhci_host structure.
395 * @degrees: The clock phase shift between 0 - 359.
398 * Set the SD Input Clock Tap Delays for Input path
400 static int sdhci_zynqmp_sampleclk_set_phase(struct sdhci_host *host,
403 struct mmc *mmc = (struct mmc *)host->mmc;
404 struct udevice *dev = mmc->dev;
405 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
406 u8 tap_delay, tap_max = 0;
407 int timing = mode2timing[mmc->selected_mode];
411 * This is applicable for SDHCI_SPEC_300 and above
412 * ZynqMP does not set phase for <=25MHz clock.
413 * If degrees is zero, no need to do anything.
415 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
418 /* Assert DLL Reset */
419 ret = zynqmp_dll_reset(priv->node_id, PM_DLL_RESET_ASSERT);
421 dev_err(dev, "dll_reset assert failed with err: %d\n", ret);
426 case MMC_TIMING_MMC_HS:
427 case MMC_TIMING_SD_HS:
428 case MMC_TIMING_UHS_SDR25:
429 case MMC_TIMING_UHS_DDR50:
430 case MMC_TIMING_MMC_DDR52:
431 /* For 50MHz clock, 120 Taps are available */
434 case MMC_TIMING_UHS_SDR50:
435 /* For 100MHz clock, 60 Taps are available */
438 case MMC_TIMING_UHS_SDR104:
439 case MMC_TIMING_MMC_HS200:
440 /* For 200MHz clock, 30 Taps are available */
446 tap_delay = (degrees * tap_max) / 360;
448 /* Limit input tap_delay value to 8 bits */
449 tap_delay &= SDHCI_ARASAN_ITAPDLY_SEL_MASK;
451 ret = arasan_zynqmp_set_in_tapdelay(priv->node_id, tap_delay);
453 dev_err(dev, "Error setting Input Tap Delay\n");
461 * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
463 * @host: Pointer to the sdhci_host structure.
464 * @degrees: The clock phase shift between 0 - 359.
467 * Set the SD Output Clock Tap Delays for Output path
469 static int sdhci_versal_sdcardclk_set_phase(struct sdhci_host *host,
472 struct mmc *mmc = (struct mmc *)host->mmc;
473 u8 tap_delay, tap_max = 0;
474 int timing = mode2timing[mmc->selected_mode];
478 * This is applicable for SDHCI_SPEC_300 and above
479 * Versal does not set phase for <=25MHz clock.
480 * If degrees is zero, no need to do anything.
482 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
486 case MMC_TIMING_MMC_HS:
487 case MMC_TIMING_SD_HS:
488 case MMC_TIMING_UHS_SDR25:
489 case MMC_TIMING_UHS_DDR50:
490 case MMC_TIMING_MMC_DDR52:
491 /* For 50MHz clock, 30 Taps are available */
494 case MMC_TIMING_UHS_SDR50:
495 /* For 100MHz clock, 15 Taps are available */
498 case MMC_TIMING_UHS_SDR104:
499 case MMC_TIMING_MMC_HS200:
500 /* For 200MHz clock, 8 Taps are available */
506 tap_delay = (degrees * tap_max) / 360;
508 /* Limit output tap_delay value to 6 bits */
509 tap_delay &= SDHCI_ARASAN_OTAPDLY_SEL_MASK;
511 /* Set the Clock Phase */
512 regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
513 regval |= SDHCI_OTAPDLY_ENABLE;
514 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
515 regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK;
517 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
523 * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
525 * @host: Pointer to the sdhci_host structure.
526 * @degrees: The clock phase shift between 0 - 359.
529 * Set the SD Input Clock Tap Delays for Input path
531 static int sdhci_versal_sampleclk_set_phase(struct sdhci_host *host,
534 struct mmc *mmc = (struct mmc *)host->mmc;
535 u8 tap_delay, tap_max = 0;
536 int timing = mode2timing[mmc->selected_mode];
540 * This is applicable for SDHCI_SPEC_300 and above
541 * Versal does not set phase for <=25MHz clock.
542 * If degrees is zero, no need to do anything.
544 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
548 case MMC_TIMING_MMC_HS:
549 case MMC_TIMING_SD_HS:
550 case MMC_TIMING_UHS_SDR25:
551 case MMC_TIMING_UHS_DDR50:
552 case MMC_TIMING_MMC_DDR52:
553 /* For 50MHz clock, 120 Taps are available */
556 case MMC_TIMING_UHS_SDR50:
557 /* For 100MHz clock, 60 Taps are available */
560 case MMC_TIMING_UHS_SDR104:
561 case MMC_TIMING_MMC_HS200:
562 /* For 200MHz clock, 30 Taps are available */
568 tap_delay = (degrees * tap_max) / 360;
570 /* Limit input tap_delay value to 8 bits */
571 tap_delay &= SDHCI_ARASAN_ITAPDLY_SEL_MASK;
573 /* Set the Clock Phase */
574 regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
575 regval |= SDHCI_ITAPDLY_CHGWIN;
576 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
577 regval |= SDHCI_ITAPDLY_ENABLE;
578 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
579 regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK;
581 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
582 regval &= ~SDHCI_ITAPDLY_CHGWIN;
583 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
588 static int arasan_sdhci_set_tapdelay(struct sdhci_host *host)
590 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
591 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
592 struct mmc *mmc = (struct mmc *)host->mmc;
593 struct udevice *dev = mmc->dev;
594 u8 timing = mode2timing[mmc->selected_mode];
595 u32 iclk_phase = clk_data->clk_phase_in[timing];
596 u32 oclk_phase = clk_data->clk_phase_out[timing];
599 dev_dbg(dev, "%s, host:%s, mode:%d\n", __func__, host->name, timing);
601 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) &&
602 device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
603 ret = sdhci_zynqmp_sampleclk_set_phase(host, iclk_phase);
607 ret = sdhci_zynqmp_sdcardclk_set_phase(host, oclk_phase);
610 } else if (IS_ENABLED(CONFIG_ARCH_VERSAL) &&
611 device_is_compatible(dev, "xlnx,versal-8.9a")) {
612 ret = sdhci_versal_sampleclk_set_phase(host, iclk_phase);
616 ret = sdhci_versal_sdcardclk_set_phase(host, oclk_phase);
624 static void arasan_dt_read_clk_phase(struct udevice *dev, unsigned char timing,
627 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
628 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
629 u32 clk_phase[2] = {0};
632 * Read Tap Delay values from DT, if the DT does not contain the
633 * Tap Values then use the pre-defined values
635 if (dev_read_u32_array(dev, prop, &clk_phase[0], 2)) {
636 dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
637 prop, clk_data->clk_phase_in[timing],
638 clk_data->clk_phase_out[timing]);
642 /* The values read are Input and Output Clock Delays in order */
643 clk_data->clk_phase_in[timing] = clk_phase[0];
644 clk_data->clk_phase_out[timing] = clk_phase[1];
648 * arasan_dt_parse_clk_phases - Read Tap Delay values from DT
650 * @dev: Pointer to our struct udevice.
652 * Called at initialization to parse the values of Tap Delays.
654 static void arasan_dt_parse_clk_phases(struct udevice *dev)
656 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
657 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
660 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) &&
661 device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
662 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
663 clk_data->clk_phase_in[i] = zynqmp_iclk_phases[i];
664 clk_data->clk_phase_out[i] = zynqmp_oclk_phases[i];
667 if (priv->bank == MMC_BANK2) {
668 clk_data->clk_phase_out[MMC_TIMING_UHS_SDR104] = 90;
669 clk_data->clk_phase_out[MMC_TIMING_MMC_HS200] = 90;
673 if (IS_ENABLED(CONFIG_ARCH_VERSAL) &&
674 device_is_compatible(dev, "xlnx,versal-8.9a")) {
675 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
676 clk_data->clk_phase_in[i] = versal_iclk_phases[i];
677 clk_data->clk_phase_out[i] = versal_oclk_phases[i];
681 arasan_dt_read_clk_phase(dev, MMC_TIMING_LEGACY,
683 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS,
685 arasan_dt_read_clk_phase(dev, MMC_TIMING_SD_HS,
687 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR12,
688 "clk-phase-uhs-sdr12");
689 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR25,
690 "clk-phase-uhs-sdr25");
691 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR50,
692 "clk-phase-uhs-sdr50");
693 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR104,
694 "clk-phase-uhs-sdr104");
695 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_DDR50,
696 "clk-phase-uhs-ddr50");
697 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_DDR52,
698 "clk-phase-mmc-ddr52");
699 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS200,
700 "clk-phase-mmc-hs200");
701 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS400,
702 "clk-phase-mmc-hs400");
705 static const struct sdhci_ops arasan_ops = {
706 .platform_execute_tuning = &arasan_sdhci_execute_tuning,
707 .set_delay = &arasan_sdhci_set_tapdelay,
708 .set_control_reg = &sdhci_set_control_reg,
712 #if defined(CONFIG_ARCH_ZYNQMP)
713 static int sdhci_zynqmp_set_dynamic_config(struct arasan_sdhci_priv *priv,
718 unsigned long clock, mhz;
720 ret = xilinx_pm_request(PM_REQUEST_NODE, priv->node_id,
721 ZYNQMP_PM_CAPABILITY_ACCESS, ZYNQMP_PM_MAX_QOS,
722 ZYNQMP_PM_REQUEST_ACK_NO, NULL);
724 dev_err(dev, "Request node failed for %d\n", priv->node_id);
728 ret = reset_get_bulk(dev, &priv->resets);
729 if (ret == -ENOTSUPP || ret == -ENOENT) {
730 dev_err(dev, "Reset not found\n");
733 dev_err(dev, "Reset failed\n");
737 ret = reset_assert_bulk(&priv->resets);
739 dev_err(dev, "Reset assert failed\n");
743 ret = zynqmp_pm_set_sd_config(priv->node_id, SD_CONFIG_FIXED, 0);
745 dev_err(dev, "SD_CONFIG_FIXED failed\n");
749 ret = zynqmp_pm_set_sd_config(priv->node_id, SD_CONFIG_EMMC_SEL,
750 dev_read_bool(dev, "non-removable"));
752 dev_err(dev, "SD_CONFIG_EMMC_SEL failed\n");
756 ret = clk_get_by_index(dev, 0, &clk);
758 dev_err(dev, "failed to get clock\n");
762 clock = clk_get_rate(&clk);
763 if (IS_ERR_VALUE(clock)) {
764 dev_err(dev, "failed to get rate\n");
768 mhz = DIV64_U64_ROUND_UP(clock, 1000000);
770 if (mhz > 100 && mhz <= 200)
772 else if (mhz > 50 && mhz <= 100)
774 else if (mhz > 25 && mhz <= 50)
779 ret = zynqmp_pm_set_sd_config(priv->node_id, SD_CONFIG_BASECLK, mhz);
781 dev_err(dev, "SD_CONFIG_BASECLK failed\n");
785 ret = zynqmp_pm_set_sd_config(priv->node_id, SD_CONFIG_8BIT,
786 (dev_read_u32_default(dev, "bus-width", 1) == 8));
788 dev_err(dev, "SD_CONFIG_8BIT failed\n");
792 ret = reset_deassert_bulk(&priv->resets);
794 dev_err(dev, "Reset release failed\n");
802 static int arasan_sdhci_probe(struct udevice *dev)
804 struct arasan_sdhci_plat *plat = dev_get_plat(dev);
805 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
806 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
807 struct sdhci_host *host;
814 #if defined(CONFIG_ARCH_ZYNQMP)
815 if (device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
816 ret = zynqmp_pm_is_function_supported(PM_IOCTL,
817 IOCTL_SET_SD_CONFIG);
819 ret = sdhci_zynqmp_set_dynamic_config(priv, dev);
826 ret = clk_get_by_index(dev, 0, &clk);
828 dev_err(dev, "failed to get clock\n");
832 clock = clk_get_rate(&clk);
833 if (IS_ERR_VALUE(clock)) {
834 dev_err(dev, "failed to get rate\n");
838 dev_dbg(dev, "%s: CLK %ld\n", __func__, clock);
840 ret = clk_enable(&clk);
842 dev_err(dev, "failed to enable clock\n");
846 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
847 SDHCI_QUIRK_BROKEN_R1B;
849 #ifdef CONFIG_ZYNQ_HISPD_BROKEN
850 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
854 host->quirks |= SDHCI_QUIRK_NO_1_8_V;
856 plat->cfg.f_max = CONFIG_ZYNQ_SDHCI_MAX_FREQ;
858 ret = mmc_of_parse(dev, &plat->cfg);
862 host->max_clk = clock;
864 host->mmc = &plat->mmc;
865 host->mmc->dev = dev;
866 host->mmc->priv = host;
868 ret = sdhci_setup_cfg(&plat->cfg, host, plat->cfg.f_max,
869 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
872 upriv->mmc = host->mmc;
875 * WORKAROUND: Versal platforms have an issue with card detect state.
876 * Due to this, host controller is switching off voltage to sd card
877 * causing sd card timeout error. Workaround this by adding a wait for
878 * 1000msec till the card detect state gets stable.
880 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) || IS_ENABLED(CONFIG_ARCH_VERSAL)) {
881 u32 timeout = 1000000;
883 while (((sdhci_readl(host, SDHCI_PRESENT_STATE) &
884 SDHCI_CARD_STATE_STABLE) == 0) && timeout) {
889 dev_err(dev, "Sdhci card detect state not stable\n");
894 return sdhci_probe(dev);
897 static int arasan_sdhci_of_to_plat(struct udevice *dev)
899 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
902 priv->host = calloc(1, sizeof(struct sdhci_host));
906 priv->host->name = dev->name;
908 #if defined(CONFIG_ARCH_ZYNQMP) || defined(CONFIG_ARCH_VERSAL)
909 priv->host->ops = &arasan_ops;
910 arasan_dt_parse_clk_phases(dev);
913 priv->host->ioaddr = (void *)dev_read_addr(dev);
914 if (IS_ERR(priv->host->ioaddr))
915 return PTR_ERR(priv->host->ioaddr);
917 priv->bank = dev_read_u32_default(dev, "xlnx,mio-bank", 0);
918 priv->no_1p8 = dev_read_bool(dev, "no-1-8-v");
921 if (!dev_read_u32_array(dev, "power-domains", pm_info, ARRAY_SIZE(pm_info)))
922 priv->node_id = pm_info[1];
927 static int arasan_sdhci_bind(struct udevice *dev)
929 struct arasan_sdhci_plat *plat = dev_get_plat(dev);
931 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
934 static const struct udevice_id arasan_sdhci_ids[] = {
935 { .compatible = "arasan,sdhci-8.9a" },
939 U_BOOT_DRIVER(arasan_sdhci_drv) = {
940 .name = "arasan_sdhci",
942 .of_match = arasan_sdhci_ids,
943 .of_to_plat = arasan_sdhci_of_to_plat,
945 .bind = arasan_sdhci_bind,
946 .probe = arasan_sdhci_probe,
947 .priv_auto = sizeof(struct arasan_sdhci_priv),
948 .plat_auto = sizeof(struct arasan_sdhci_plat),