1 // SPDX-License-Identifier: GPL-2.0
3 // Secure Digital Host Controller
5 // Copyright (C) 2018 Spreadtrum, Inc.
6 // Author: Chunyan Zhang <chunyan.zhang@unisoc.com>
8 #include <linux/delay.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/highmem.h>
11 #include <linux/iopoll.h>
12 #include <linux/mmc/host.h>
13 #include <linux/mmc/mmc.h>
14 #include <linux/module.h>
16 #include <linux/of_gpio.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/slab.h>
23 #include "sdhci-pltfm.h"
26 /* SDHCI_ARGUMENT2 register high 16bit */
27 #define SDHCI_SPRD_ARG2_STUFF GENMASK(31, 16)
29 #define SDHCI_SPRD_REG_32_DLL_CFG 0x200
30 #define SDHCI_SPRD_DLL_ALL_CPST_EN (BIT(18) | BIT(24) | BIT(25) | BIT(26) | BIT(27))
31 #define SDHCI_SPRD_DLL_EN BIT(21)
32 #define SDHCI_SPRD_DLL_SEARCH_MODE BIT(16)
33 #define SDHCI_SPRD_DLL_INIT_COUNT 0xc00
34 #define SDHCI_SPRD_DLL_PHASE_INTERNAL 0x3
36 #define SDHCI_SPRD_REG_32_DLL_DLY 0x204
38 #define SDHCI_SPRD_REG_32_DLL_DLY_OFFSET 0x208
39 #define SDHCIBSPRD_IT_WR_DLY_INV BIT(5)
40 #define SDHCI_SPRD_BIT_CMD_DLY_INV BIT(13)
41 #define SDHCI_SPRD_BIT_POSRD_DLY_INV BIT(21)
42 #define SDHCI_SPRD_BIT_NEGRD_DLY_INV BIT(29)
44 #define SDHCI_SPRD_REG_32_DLL_STS0 0x210
45 #define SDHCI_SPRD_DLL_LOCKED BIT(18)
47 #define SDHCI_SPRD_REG_32_BUSY_POSI 0x250
48 #define SDHCI_SPRD_BIT_OUTR_CLK_AUTO_EN BIT(25)
49 #define SDHCI_SPRD_BIT_INNR_CLK_AUTO_EN BIT(24)
51 #define SDHCI_SPRD_REG_DEBOUNCE 0x28C
52 #define SDHCI_SPRD_BIT_DLL_BAK BIT(0)
53 #define SDHCI_SPRD_BIT_DLL_VAL BIT(1)
55 #define SDHCI_SPRD_INT_SIGNAL_MASK 0x1B7F410B
57 /* SDHCI_HOST_CONTROL2 */
58 #define SDHCI_SPRD_CTRL_HS200 0x0005
59 #define SDHCI_SPRD_CTRL_HS400 0x0006
60 #define SDHCI_SPRD_CTRL_HS400ES 0x0007
63 * According to the standard specification, BIT(3) of SDHCI_SOFTWARE_RESET is
64 * reserved, and only used on Spreadtrum's design, the hardware cannot work
65 * if this bit is cleared.
69 #define SDHCI_HW_RESET_CARD BIT(3)
71 #define SDHCI_SPRD_MAX_CUR 0xFFFFFF
72 #define SDHCI_SPRD_CLK_MAX_DIV 1023
74 #define SDHCI_SPRD_CLK_DEF_RATE 26000000
75 #define SDHCI_SPRD_PHY_DLL_CLK 52000000
77 #define SDHCI_SPRD_MAX_RANGE 0xff
78 #define SDHCI_SPRD_CMD_DLY_MASK GENMASK(15, 8)
79 #define SDHCI_SPRD_POSRD_DLY_MASK GENMASK(23, 16)
80 #define SDHCI_SPRD_CPST_EN GENMASK(27, 24)
82 struct sdhci_sprd_host {
85 struct clk *clk_enable;
86 struct clk *clk_2x_enable;
87 struct pinctrl *pinctrl;
88 struct pinctrl_state *pins_uhs;
89 struct pinctrl_state *pins_default;
91 int flags; /* backup of host attribute */
92 u32 phy_delay[MMC_TIMING_MMC_HS400 + 2];
95 enum sdhci_sprd_tuning_type {
96 SDHCI_SPRD_TUNING_SD_HS_CMD,
97 SDHCI_SPRD_TUNING_SD_HS_DATA,
100 struct sdhci_sprd_phy_cfg {
101 const char *property;
105 static const struct sdhci_sprd_phy_cfg sdhci_sprd_phy_cfgs[] = {
106 { "sprd,phy-delay-legacy", MMC_TIMING_LEGACY, },
107 { "sprd,phy-delay-sd-highspeed", MMC_TIMING_SD_HS, },
108 { "sprd,phy-delay-sd-uhs-sdr50", MMC_TIMING_UHS_SDR50, },
109 { "sprd,phy-delay-sd-uhs-sdr104", MMC_TIMING_UHS_SDR104, },
110 { "sprd,phy-delay-mmc-highspeed", MMC_TIMING_MMC_HS, },
111 { "sprd,phy-delay-mmc-ddr52", MMC_TIMING_MMC_DDR52, },
112 { "sprd,phy-delay-mmc-hs200", MMC_TIMING_MMC_HS200, },
113 { "sprd,phy-delay-mmc-hs400", MMC_TIMING_MMC_HS400, },
114 { "sprd,phy-delay-mmc-hs400es", MMC_TIMING_MMC_HS400 + 1, },
117 #define TO_SPRD_HOST(host) sdhci_pltfm_priv(sdhci_priv(host))
119 static void sdhci_sprd_init_config(struct sdhci_host *host)
123 /* set dll backup mode */
124 val = sdhci_readl(host, SDHCI_SPRD_REG_DEBOUNCE);
125 val |= SDHCI_SPRD_BIT_DLL_BAK | SDHCI_SPRD_BIT_DLL_VAL;
126 sdhci_writel(host, val, SDHCI_SPRD_REG_DEBOUNCE);
129 static inline u32 sdhci_sprd_readl(struct sdhci_host *host, int reg)
131 if (unlikely(reg == SDHCI_MAX_CURRENT))
132 return SDHCI_SPRD_MAX_CUR;
134 return readl_relaxed(host->ioaddr + reg);
137 static inline void sdhci_sprd_writel(struct sdhci_host *host, u32 val, int reg)
139 /* SDHCI_MAX_CURRENT is reserved on Spreadtrum's platform */
140 if (unlikely(reg == SDHCI_MAX_CURRENT))
143 if (unlikely(reg == SDHCI_SIGNAL_ENABLE || reg == SDHCI_INT_ENABLE))
144 val = val & SDHCI_SPRD_INT_SIGNAL_MASK;
146 writel_relaxed(val, host->ioaddr + reg);
149 static inline void sdhci_sprd_writew(struct sdhci_host *host, u16 val, int reg)
151 /* SDHCI_BLOCK_COUNT is Read Only on Spreadtrum's platform */
152 if (unlikely(reg == SDHCI_BLOCK_COUNT))
155 writew_relaxed(val, host->ioaddr + reg);
158 static inline void sdhci_sprd_writeb(struct sdhci_host *host, u8 val, int reg)
161 * Since BIT(3) of SDHCI_SOFTWARE_RESET is reserved according to the
162 * standard specification, sdhci_reset() write this register directly
163 * without checking other reserved bits, that will clear BIT(3) which
164 * is defined as hardware reset on Spreadtrum's platform and clearing
165 * it by mistake will lead the card not work. So here we need to work
168 if (unlikely(reg == SDHCI_SOFTWARE_RESET)) {
169 if (readb_relaxed(host->ioaddr + reg) & SDHCI_HW_RESET_CARD)
170 val |= SDHCI_HW_RESET_CARD;
173 writeb_relaxed(val, host->ioaddr + reg);
176 static inline void sdhci_sprd_sd_clk_off(struct sdhci_host *host)
178 u16 ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
180 ctrl &= ~SDHCI_CLOCK_CARD_EN;
181 sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
184 static inline void sdhci_sprd_sd_clk_on(struct sdhci_host *host)
188 ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
189 ctrl |= SDHCI_CLOCK_CARD_EN;
190 sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
194 sdhci_sprd_set_dll_invert(struct sdhci_host *host, u32 mask, bool en)
198 dll_dly_offset = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_DLY_OFFSET);
200 dll_dly_offset |= mask;
202 dll_dly_offset &= ~mask;
203 sdhci_writel(host, dll_dly_offset, SDHCI_SPRD_REG_32_DLL_DLY_OFFSET);
206 static inline u32 sdhci_sprd_calc_div(u32 base_clk, u32 clk)
210 /* select 2x clock source */
211 if (base_clk <= clk * 2)
214 div = (u32) (base_clk / (clk * 2));
216 if ((base_clk / div) > (clk * 2))
224 if (div > SDHCI_SPRD_CLK_MAX_DIV)
225 div = SDHCI_SPRD_CLK_MAX_DIV;
230 static inline void _sdhci_sprd_set_clock(struct sdhci_host *host,
233 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
236 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
238 div = sdhci_sprd_calc_div(sprd_host->base_rate, clk);
239 div = ((div & 0x300) >> 2) | ((div & 0xFF) << 8);
240 sdhci_enable_clk(host, div);
242 val = sdhci_readl(host, SDHCI_SPRD_REG_32_BUSY_POSI);
243 mask = SDHCI_SPRD_BIT_OUTR_CLK_AUTO_EN | SDHCI_SPRD_BIT_INNR_CLK_AUTO_EN;
244 /* Enable CLK_AUTO when the clock is greater than 400K. */
246 if (mask != (val & mask)) {
248 sdhci_writel(host, val, SDHCI_SPRD_REG_32_BUSY_POSI);
253 sdhci_writel(host, val, SDHCI_SPRD_REG_32_BUSY_POSI);
258 static void sdhci_sprd_enable_phy_dll(struct sdhci_host *host)
262 tmp = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG);
263 tmp &= ~(SDHCI_SPRD_DLL_EN | SDHCI_SPRD_DLL_ALL_CPST_EN);
264 sdhci_writel(host, tmp, SDHCI_SPRD_REG_32_DLL_CFG);
266 usleep_range(1000, 1250);
268 tmp = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG);
269 tmp |= SDHCI_SPRD_DLL_ALL_CPST_EN | SDHCI_SPRD_DLL_SEARCH_MODE |
270 SDHCI_SPRD_DLL_INIT_COUNT | SDHCI_SPRD_DLL_PHASE_INTERNAL;
271 sdhci_writel(host, tmp, SDHCI_SPRD_REG_32_DLL_CFG);
273 usleep_range(1000, 1250);
275 tmp = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG);
276 tmp |= SDHCI_SPRD_DLL_EN;
277 sdhci_writel(host, tmp, SDHCI_SPRD_REG_32_DLL_CFG);
279 usleep_range(1000, 1250);
281 if (read_poll_timeout(sdhci_readl, tmp, (tmp & SDHCI_SPRD_DLL_LOCKED),
282 2000, USEC_PER_SEC, false, host, SDHCI_SPRD_REG_32_DLL_STS0)) {
283 pr_err("%s: DLL locked fail!\n", mmc_hostname(host->mmc));
284 pr_info("%s: DLL_STS0 : 0x%x, DLL_CFG : 0x%x\n",
285 mmc_hostname(host->mmc),
286 sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_STS0),
287 sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG));
291 static void sdhci_sprd_set_clock(struct sdhci_host *host, unsigned int clock)
293 bool en = false, clk_changed = false;
296 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
297 } else if (clock != host->clock) {
298 sdhci_sprd_sd_clk_off(host);
299 _sdhci_sprd_set_clock(host, clock);
303 sdhci_sprd_set_dll_invert(host, SDHCI_SPRD_BIT_CMD_DLY_INV |
304 SDHCI_SPRD_BIT_POSRD_DLY_INV, en);
307 _sdhci_sprd_set_clock(host, clock);
311 * According to the Spreadtrum SD host specification, when we changed
312 * the clock to be more than 52M, we should enable the PHY DLL which
313 * is used to track the clock frequency to make the clock work more
314 * stable. Otherwise deviation may occur of the higher clock.
316 if (clk_changed && clock > SDHCI_SPRD_PHY_DLL_CLK)
317 sdhci_sprd_enable_phy_dll(host);
320 static unsigned int sdhci_sprd_get_max_clock(struct sdhci_host *host)
322 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
324 return clk_round_rate(sprd_host->clk_sdio, ULONG_MAX);
327 static unsigned int sdhci_sprd_get_min_clock(struct sdhci_host *host)
332 static void sdhci_sprd_set_uhs_signaling(struct sdhci_host *host,
335 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
336 struct mmc_host *mmc = host->mmc;
337 u32 *p = sprd_host->phy_delay;
340 if (timing == host->timing)
343 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
344 /* Select Bus Speed Mode for host */
345 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
347 case MMC_TIMING_UHS_SDR12:
348 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
350 case MMC_TIMING_MMC_HS:
351 case MMC_TIMING_SD_HS:
352 case MMC_TIMING_UHS_SDR25:
353 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
355 case MMC_TIMING_UHS_SDR50:
356 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
358 case MMC_TIMING_UHS_SDR104:
359 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
361 case MMC_TIMING_UHS_DDR50:
362 case MMC_TIMING_MMC_DDR52:
363 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
365 case MMC_TIMING_MMC_HS200:
366 ctrl_2 |= SDHCI_SPRD_CTRL_HS200;
368 case MMC_TIMING_MMC_HS400:
369 ctrl_2 |= SDHCI_SPRD_CTRL_HS400;
375 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
377 if (!mmc->ios.enhanced_strobe)
378 sdhci_writel(host, p[timing], SDHCI_SPRD_REG_32_DLL_DLY);
381 static void sdhci_sprd_hw_reset(struct sdhci_host *host)
386 * Note: don't use sdhci_writeb() API here since it is redirected to
387 * sdhci_sprd_writeb() in which we have a workaround for
388 * SDHCI_SOFTWARE_RESET which would make bit SDHCI_HW_RESET_CARD can
391 val = readb_relaxed(host->ioaddr + SDHCI_SOFTWARE_RESET);
392 val &= ~SDHCI_HW_RESET_CARD;
393 writeb_relaxed(val, host->ioaddr + SDHCI_SOFTWARE_RESET);
395 usleep_range(10, 20);
397 val |= SDHCI_HW_RESET_CARD;
398 writeb_relaxed(val, host->ioaddr + SDHCI_SOFTWARE_RESET);
399 usleep_range(300, 500);
402 static unsigned int sdhci_sprd_get_max_timeout_count(struct sdhci_host *host)
404 /* The Spredtrum controller actual maximum timeout count is 1 << 31 */
408 static unsigned int sdhci_sprd_get_ro(struct sdhci_host *host)
413 static void sdhci_sprd_request_done(struct sdhci_host *host,
414 struct mmc_request *mrq)
416 /* Validate if the request was from software queue firstly. */
417 if (mmc_hsq_finalize_request(host->mmc, mrq))
420 mmc_request_done(host->mmc, mrq);
423 static void sdhci_sprd_set_power(struct sdhci_host *host, unsigned char mode,
426 struct mmc_host *mmc = host->mmc;
430 mmc_regulator_set_ocr(host->mmc, mmc->supply.vmmc, 0);
432 mmc_regulator_disable_vqmmc(mmc);
435 mmc_regulator_enable_vqmmc(mmc);
438 mmc_regulator_set_ocr(host->mmc, mmc->supply.vmmc, vdd);
443 static struct sdhci_ops sdhci_sprd_ops = {
444 .read_l = sdhci_sprd_readl,
445 .write_l = sdhci_sprd_writel,
446 .write_w = sdhci_sprd_writew,
447 .write_b = sdhci_sprd_writeb,
448 .set_clock = sdhci_sprd_set_clock,
449 .set_power = sdhci_sprd_set_power,
450 .get_max_clock = sdhci_sprd_get_max_clock,
451 .get_min_clock = sdhci_sprd_get_min_clock,
452 .set_bus_width = sdhci_set_bus_width,
453 .reset = sdhci_reset,
454 .set_uhs_signaling = sdhci_sprd_set_uhs_signaling,
455 .hw_reset = sdhci_sprd_hw_reset,
456 .get_max_timeout_count = sdhci_sprd_get_max_timeout_count,
457 .get_ro = sdhci_sprd_get_ro,
458 .request_done = sdhci_sprd_request_done,
461 static void sdhci_sprd_check_auto_cmd23(struct mmc_host *mmc,
462 struct mmc_request *mrq)
464 struct sdhci_host *host = mmc_priv(mmc);
465 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
467 host->flags |= sprd_host->flags & SDHCI_AUTO_CMD23;
470 * From version 4.10 onward, ARGUMENT2 register is also as 32-bit
471 * block count register which doesn't support stuff bits of
472 * CMD23 argument on Spreadtrum's sd host controller.
474 if (host->version >= SDHCI_SPEC_410 &&
475 mrq->sbc && (mrq->sbc->arg & SDHCI_SPRD_ARG2_STUFF) &&
476 (host->flags & SDHCI_AUTO_CMD23))
477 host->flags &= ~SDHCI_AUTO_CMD23;
480 static void sdhci_sprd_request(struct mmc_host *mmc, struct mmc_request *mrq)
482 sdhci_sprd_check_auto_cmd23(mmc, mrq);
484 sdhci_request(mmc, mrq);
487 static int sdhci_sprd_request_atomic(struct mmc_host *mmc,
488 struct mmc_request *mrq)
490 sdhci_sprd_check_auto_cmd23(mmc, mrq);
492 return sdhci_request_atomic(mmc, mrq);
495 static int sdhci_sprd_voltage_switch(struct mmc_host *mmc, struct mmc_ios *ios)
497 struct sdhci_host *host = mmc_priv(mmc);
498 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
501 if (!IS_ERR(mmc->supply.vqmmc)) {
502 ret = mmc_regulator_set_vqmmc(mmc, ios);
504 pr_err("%s: Switching signalling voltage failed\n",
510 if (IS_ERR(sprd_host->pinctrl))
513 switch (ios->signal_voltage) {
514 case MMC_SIGNAL_VOLTAGE_180:
515 ret = pinctrl_select_state(sprd_host->pinctrl,
516 sprd_host->pins_uhs);
518 pr_err("%s: failed to select uhs pin state\n",
526 case MMC_SIGNAL_VOLTAGE_330:
527 ret = pinctrl_select_state(sprd_host->pinctrl,
528 sprd_host->pins_default);
530 pr_err("%s: failed to select default pin state\n",
537 /* Wait for 300 ~ 500 us for pin state stable */
538 usleep_range(300, 500);
541 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
546 static void sdhci_sprd_hs400_enhanced_strobe(struct mmc_host *mmc,
549 struct sdhci_host *host = mmc_priv(mmc);
550 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
551 u32 *p = sprd_host->phy_delay;
554 if (!ios->enhanced_strobe)
557 sdhci_sprd_sd_clk_off(host);
559 /* Set HS400 enhanced strobe mode */
560 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
561 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
562 ctrl_2 |= SDHCI_SPRD_CTRL_HS400ES;
563 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
565 sdhci_sprd_sd_clk_on(host);
567 /* Set the PHY DLL delay value for HS400 enhanced strobe mode */
568 sdhci_writel(host, p[MMC_TIMING_MMC_HS400 + 1],
569 SDHCI_SPRD_REG_32_DLL_DLY);
572 static int mmc_send_tuning_cmd(struct mmc_card *card)
574 return mmc_send_status(card, NULL);
577 static int mmc_send_tuning_data(struct mmc_card *card)
582 status = kmalloc(64, GFP_KERNEL);
586 ret = mmc_sd_switch(card, 0, 0, 0, status);
593 static int sdhci_sprd_get_best_clk_sample(struct mmc_host *mmc, u8 *value)
595 int range_end = SDHCI_SPRD_MAX_RANGE;
596 int range_length = 0;
597 int middle_range = 0;
601 for (i = 0; i <= SDHCI_SPRD_MAX_RANGE; i++) {
603 pr_debug("%s: tuning ok: %d\n", mmc_hostname(mmc), i);
606 pr_debug("%s: tuning fail: %d\n", mmc_hostname(mmc), i);
607 if (range_length < count) {
608 range_length = count;
618 if (count > range_length) {
619 range_length = count;
623 middle_range = range_end - (range_length - 1) / 2;
628 static int sdhci_sprd_tuning(struct mmc_host *mmc, struct mmc_card *card,
629 enum sdhci_sprd_tuning_type type)
631 struct sdhci_host *host = mmc_priv(mmc);
632 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
633 u32 *p = sprd_host->phy_delay;
634 u32 dll_cfg, dll_dly;
640 value = kmalloc(SDHCI_SPRD_MAX_RANGE + 1, GFP_KERNEL);
644 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
646 dll_cfg = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG);
647 dll_cfg &= ~SDHCI_SPRD_CPST_EN;
648 sdhci_writel(host, dll_cfg, SDHCI_SPRD_REG_32_DLL_CFG);
650 dll_dly = p[mmc->ios.timing];
652 for (i = 0; i <= SDHCI_SPRD_MAX_RANGE; i++) {
653 if (type == SDHCI_SPRD_TUNING_SD_HS_CMD) {
654 dll_dly &= ~SDHCI_SPRD_CMD_DLY_MASK;
655 dll_dly |= ((i << 8) & SDHCI_SPRD_CMD_DLY_MASK);
657 dll_dly &= ~SDHCI_SPRD_POSRD_DLY_MASK;
658 dll_dly |= ((i << 16) & SDHCI_SPRD_POSRD_DLY_MASK);
661 sdhci_writel(host, dll_dly, SDHCI_SPRD_REG_32_DLL_DLY);
663 if (type == SDHCI_SPRD_TUNING_SD_HS_CMD)
664 value[i] = !mmc_send_tuning_cmd(card);
666 value[i] = !mmc_send_tuning_data(card);
669 best_clk_sample = sdhci_sprd_get_best_clk_sample(mmc, value);
670 if (best_clk_sample < 0) {
671 dev_err(mmc_dev(host->mmc), "all tuning phase fail!\n");
672 err = best_clk_sample;
676 if (type == SDHCI_SPRD_TUNING_SD_HS_CMD) {
677 p[mmc->ios.timing] &= ~SDHCI_SPRD_CMD_DLY_MASK;
678 p[mmc->ios.timing] |= ((best_clk_sample << 8) & SDHCI_SPRD_CMD_DLY_MASK);
680 p[mmc->ios.timing] &= ~(SDHCI_SPRD_POSRD_DLY_MASK);
681 p[mmc->ios.timing] |= ((best_clk_sample << 16) & SDHCI_SPRD_POSRD_DLY_MASK);
684 pr_debug("%s: the best clk sample %d, delay value 0x%08x\n",
685 mmc_hostname(host->mmc), best_clk_sample, p[mmc->ios.timing]);
688 sdhci_writel(host, p[mmc->ios.timing], SDHCI_SPRD_REG_32_DLL_DLY);
695 static int sdhci_sprd_prepare_sd_hs_cmd_tuning(struct mmc_host *mmc, struct mmc_card *card)
697 return sdhci_sprd_tuning(mmc, card, SDHCI_SPRD_TUNING_SD_HS_CMD);
700 static int sdhci_sprd_execute_sd_hs_data_tuning(struct mmc_host *mmc, struct mmc_card *card)
702 return sdhci_sprd_tuning(mmc, card, SDHCI_SPRD_TUNING_SD_HS_DATA);
705 static void sdhci_sprd_phy_param_parse(struct sdhci_sprd_host *sprd_host,
706 struct device_node *np)
708 u32 *p = sprd_host->phy_delay;
712 for (i = 0; i < ARRAY_SIZE(sdhci_sprd_phy_cfgs); i++) {
713 ret = of_property_read_u32_array(np,
714 sdhci_sprd_phy_cfgs[i].property, val, 4);
718 index = sdhci_sprd_phy_cfgs[i].timing;
719 p[index] = val[0] | (val[1] << 8) | (val[2] << 16) | (val[3] << 24);
723 static const struct sdhci_pltfm_data sdhci_sprd_pdata = {
724 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
725 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK,
726 .quirks2 = SDHCI_QUIRK2_BROKEN_HS200 |
727 SDHCI_QUIRK2_USE_32BIT_BLK_CNT |
728 SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
729 .ops = &sdhci_sprd_ops,
732 static int sdhci_sprd_probe(struct platform_device *pdev)
734 struct sdhci_host *host;
735 struct sdhci_sprd_host *sprd_host;
740 host = sdhci_pltfm_init(pdev, &sdhci_sprd_pdata, sizeof(*sprd_host));
742 return PTR_ERR(host);
744 host->dma_mask = DMA_BIT_MASK(64);
745 pdev->dev.dma_mask = &host->dma_mask;
746 host->mmc_host_ops.request = sdhci_sprd_request;
747 host->mmc_host_ops.hs400_enhanced_strobe =
748 sdhci_sprd_hs400_enhanced_strobe;
749 host->mmc_host_ops.prepare_sd_hs_tuning =
750 sdhci_sprd_prepare_sd_hs_cmd_tuning;
751 host->mmc_host_ops.execute_sd_hs_tuning =
752 sdhci_sprd_execute_sd_hs_data_tuning;
755 * We can not use the standard ops to change and detect the voltage
756 * signal for Spreadtrum SD host controller, since our voltage regulator
757 * for I/O is fixed in hardware, that means we do not need control
758 * the standard SD host controller to change the I/O voltage.
760 host->mmc_host_ops.start_signal_voltage_switch =
761 sdhci_sprd_voltage_switch;
763 host->mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
764 MMC_CAP_WAIT_WHILE_BUSY;
766 ret = mmc_of_parse(host->mmc);
770 if (!mmc_card_is_removable(host->mmc))
771 host->mmc_host_ops.request_atomic = sdhci_sprd_request_atomic;
773 host->always_defer_done = true;
775 sprd_host = TO_SPRD_HOST(host);
776 sdhci_sprd_phy_param_parse(sprd_host, pdev->dev.of_node);
778 sprd_host->pinctrl = devm_pinctrl_get(&pdev->dev);
779 if (!IS_ERR(sprd_host->pinctrl)) {
780 sprd_host->pins_uhs =
781 pinctrl_lookup_state(sprd_host->pinctrl, "state_uhs");
782 if (IS_ERR(sprd_host->pins_uhs)) {
783 ret = PTR_ERR(sprd_host->pins_uhs);
787 sprd_host->pins_default =
788 pinctrl_lookup_state(sprd_host->pinctrl, "default");
789 if (IS_ERR(sprd_host->pins_default)) {
790 ret = PTR_ERR(sprd_host->pins_default);
795 clk = devm_clk_get(&pdev->dev, "sdio");
800 sprd_host->clk_sdio = clk;
801 sprd_host->base_rate = clk_get_rate(sprd_host->clk_sdio);
802 if (!sprd_host->base_rate)
803 sprd_host->base_rate = SDHCI_SPRD_CLK_DEF_RATE;
805 clk = devm_clk_get(&pdev->dev, "enable");
810 sprd_host->clk_enable = clk;
812 clk = devm_clk_get(&pdev->dev, "2x_enable");
814 sprd_host->clk_2x_enable = clk;
816 ret = clk_prepare_enable(sprd_host->clk_sdio);
820 ret = clk_prepare_enable(sprd_host->clk_enable);
824 ret = clk_prepare_enable(sprd_host->clk_2x_enable);
828 sdhci_sprd_init_config(host);
829 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
830 sprd_host->version = ((host->version & SDHCI_VENDOR_VER_MASK) >>
831 SDHCI_VENDOR_VER_SHIFT);
833 pm_runtime_get_noresume(&pdev->dev);
834 pm_runtime_set_active(&pdev->dev);
835 pm_runtime_enable(&pdev->dev);
836 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
837 pm_runtime_use_autosuspend(&pdev->dev);
838 pm_suspend_ignore_children(&pdev->dev, 1);
840 sdhci_enable_v4_mode(host);
843 * Supply the existing CAPS, but clear the UHS-I modes. This
844 * will allow these modes to be specified only by device
845 * tree properties through mmc_of_parse().
847 sdhci_read_caps(host);
848 host->caps1 &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
849 SDHCI_SUPPORT_DDR50);
851 ret = mmc_regulator_get_supply(host->mmc);
853 goto pm_runtime_disable;
855 ret = sdhci_setup_host(host);
857 goto pm_runtime_disable;
859 sprd_host->flags = host->flags;
861 hsq = devm_kzalloc(&pdev->dev, sizeof(*hsq), GFP_KERNEL);
864 goto err_cleanup_host;
867 ret = mmc_hsq_init(hsq, host->mmc);
869 goto err_cleanup_host;
871 ret = __sdhci_add_host(host);
873 goto err_cleanup_host;
875 pm_runtime_mark_last_busy(&pdev->dev);
876 pm_runtime_put_autosuspend(&pdev->dev);
881 sdhci_cleanup_host(host);
884 pm_runtime_put_noidle(&pdev->dev);
885 pm_runtime_disable(&pdev->dev);
886 pm_runtime_set_suspended(&pdev->dev);
888 clk_disable_unprepare(sprd_host->clk_2x_enable);
891 clk_disable_unprepare(sprd_host->clk_enable);
894 clk_disable_unprepare(sprd_host->clk_sdio);
897 sdhci_pltfm_free(pdev);
901 static void sdhci_sprd_remove(struct platform_device *pdev)
903 struct sdhci_host *host = platform_get_drvdata(pdev);
904 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
906 sdhci_remove_host(host, 0);
908 clk_disable_unprepare(sprd_host->clk_sdio);
909 clk_disable_unprepare(sprd_host->clk_enable);
910 clk_disable_unprepare(sprd_host->clk_2x_enable);
912 sdhci_pltfm_free(pdev);
915 static const struct of_device_id sdhci_sprd_of_match[] = {
916 { .compatible = "sprd,sdhci-r11", },
919 MODULE_DEVICE_TABLE(of, sdhci_sprd_of_match);
922 static int sdhci_sprd_runtime_suspend(struct device *dev)
924 struct sdhci_host *host = dev_get_drvdata(dev);
925 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
927 mmc_hsq_suspend(host->mmc);
928 sdhci_runtime_suspend_host(host);
930 clk_disable_unprepare(sprd_host->clk_sdio);
931 clk_disable_unprepare(sprd_host->clk_enable);
932 clk_disable_unprepare(sprd_host->clk_2x_enable);
937 static int sdhci_sprd_runtime_resume(struct device *dev)
939 struct sdhci_host *host = dev_get_drvdata(dev);
940 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
943 ret = clk_prepare_enable(sprd_host->clk_2x_enable);
947 ret = clk_prepare_enable(sprd_host->clk_enable);
951 ret = clk_prepare_enable(sprd_host->clk_sdio);
955 sdhci_runtime_resume_host(host, 1);
956 mmc_hsq_resume(host->mmc);
961 clk_disable_unprepare(sprd_host->clk_enable);
964 clk_disable_unprepare(sprd_host->clk_2x_enable);
970 static const struct dev_pm_ops sdhci_sprd_pm_ops = {
971 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
972 pm_runtime_force_resume)
973 SET_RUNTIME_PM_OPS(sdhci_sprd_runtime_suspend,
974 sdhci_sprd_runtime_resume, NULL)
977 static struct platform_driver sdhci_sprd_driver = {
978 .probe = sdhci_sprd_probe,
979 .remove_new = sdhci_sprd_remove,
981 .name = "sdhci_sprd_r11",
982 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
983 .of_match_table = sdhci_sprd_of_match,
984 .pm = &sdhci_sprd_pm_ops,
987 module_platform_driver(sdhci_sprd_driver);
989 MODULE_DESCRIPTION("Spreadtrum sdio host controller r11 driver");
990 MODULE_LICENSE("GPL v2");
991 MODULE_ALIAS("platform:sdhci-sprd-r11");