Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* linux/drivers/mmc/host/sdhci-s3c.c
0003  *
0004  * Copyright 2008 Openmoko Inc.
0005  * Copyright 2008 Simtec Electronics
0006  *      Ben Dooks <ben@simtec.co.uk>
0007  *      http://armlinux.simtec.co.uk/
0008  *
0009  * SDHCI (HSMMC) support for Samsung SoC
0010  */
0011 
0012 #include <linux/spinlock.h>
0013 #include <linux/delay.h>
0014 #include <linux/dma-mapping.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/platform_data/mmc-sdhci-s3c.h>
0017 #include <linux/slab.h>
0018 #include <linux/clk.h>
0019 #include <linux/io.h>
0020 #include <linux/gpio.h>
0021 #include <linux/module.h>
0022 #include <linux/of.h>
0023 #include <linux/of_device.h>
0024 #include <linux/of_gpio.h>
0025 #include <linux/pm.h>
0026 #include <linux/pm_runtime.h>
0027 
0028 #include <linux/mmc/host.h>
0029 
0030 #include "sdhci.h"
0031 
0032 #define MAX_BUS_CLK (4)
0033 
0034 #define S3C_SDHCI_CONTROL2          (0x80)
0035 #define S3C_SDHCI_CONTROL3          (0x84)
0036 #define S3C64XX_SDHCI_CONTROL4          (0x8C)
0037 
0038 #define S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR   BIT(31)
0039 #define S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK     BIT(30)
0040 #define S3C_SDHCI_CTRL2_CDINVRXD3       BIT(29)
0041 #define S3C_SDHCI_CTRL2_SLCARDOUT       BIT(28)
0042 
0043 #define S3C_SDHCI_CTRL2_FLTCLKSEL_MASK      (0xf << 24)
0044 #define S3C_SDHCI_CTRL2_FLTCLKSEL_SHIFT     (24)
0045 #define S3C_SDHCI_CTRL2_FLTCLKSEL(_x)       ((_x) << 24)
0046 
0047 #define S3C_SDHCI_CTRL2_LVLDAT_MASK     (0xff << 16)
0048 #define S3C_SDHCI_CTRL2_LVLDAT_SHIFT        (16)
0049 #define S3C_SDHCI_CTRL2_LVLDAT(_x)      ((_x) << 16)
0050 
0051 #define S3C_SDHCI_CTRL2_ENFBCLKTX       BIT(15)
0052 #define S3C_SDHCI_CTRL2_ENFBCLKRX       BIT(14)
0053 #define S3C_SDHCI_CTRL2_SDCDSEL         BIT(13)
0054 #define S3C_SDHCI_CTRL2_SDSIGPC         BIT(12)
0055 #define S3C_SDHCI_CTRL2_ENBUSYCHKTXSTART    BIT(11)
0056 
0057 #define S3C_SDHCI_CTRL2_DFCNT_MASK      (0x3 << 9)
0058 #define S3C_SDHCI_CTRL2_DFCNT_SHIFT     (9)
0059 #define S3C_SDHCI_CTRL2_DFCNT_NONE      (0x0 << 9)
0060 #define S3C_SDHCI_CTRL2_DFCNT_4SDCLK        (0x1 << 9)
0061 #define S3C_SDHCI_CTRL2_DFCNT_16SDCLK       (0x2 << 9)
0062 #define S3C_SDHCI_CTRL2_DFCNT_64SDCLK       (0x3 << 9)
0063 
0064 #define S3C_SDHCI_CTRL2_ENCLKOUTHOLD        BIT(8)
0065 #define S3C_SDHCI_CTRL2_RWAITMODE       BIT(7)
0066 #define S3C_SDHCI_CTRL2_DISBUFRD        BIT(6)
0067 
0068 #define S3C_SDHCI_CTRL2_SELBASECLK_MASK     (0x3 << 4)
0069 #define S3C_SDHCI_CTRL2_SELBASECLK_SHIFT    (4)
0070 #define S3C_SDHCI_CTRL2_PWRSYNC         BIT(3)
0071 #define S3C_SDHCI_CTRL2_ENCLKOUTMSKCON      BIT(1)
0072 #define S3C_SDHCI_CTRL2_HWINITFIN       BIT(0)
0073 
0074 #define S3C_SDHCI_CTRL3_FCSEL3          BIT(31)
0075 #define S3C_SDHCI_CTRL3_FCSEL2          BIT(23)
0076 #define S3C_SDHCI_CTRL3_FCSEL1          BIT(15)
0077 #define S3C_SDHCI_CTRL3_FCSEL0          BIT(7)
0078 
0079 #define S3C_SDHCI_CTRL3_FIA3_MASK       (0x7f << 24)
0080 #define S3C_SDHCI_CTRL3_FIA3_SHIFT      (24)
0081 #define S3C_SDHCI_CTRL3_FIA3(_x)        ((_x) << 24)
0082 
0083 #define S3C_SDHCI_CTRL3_FIA2_MASK       (0x7f << 16)
0084 #define S3C_SDHCI_CTRL3_FIA2_SHIFT      (16)
0085 #define S3C_SDHCI_CTRL3_FIA2(_x)        ((_x) << 16)
0086 
0087 #define S3C_SDHCI_CTRL3_FIA1_MASK       (0x7f << 8)
0088 #define S3C_SDHCI_CTRL3_FIA1_SHIFT      (8)
0089 #define S3C_SDHCI_CTRL3_FIA1(_x)        ((_x) << 8)
0090 
0091 #define S3C_SDHCI_CTRL3_FIA0_MASK       (0x7f << 0)
0092 #define S3C_SDHCI_CTRL3_FIA0_SHIFT      (0)
0093 #define S3C_SDHCI_CTRL3_FIA0(_x)        ((_x) << 0)
0094 
0095 #define S3C64XX_SDHCI_CONTROL4_DRIVE_MASK   (0x3 << 16)
0096 #define S3C64XX_SDHCI_CONTROL4_DRIVE_SHIFT  (16)
0097 #define S3C64XX_SDHCI_CONTROL4_DRIVE_2mA    (0x0 << 16)
0098 #define S3C64XX_SDHCI_CONTROL4_DRIVE_4mA    (0x1 << 16)
0099 #define S3C64XX_SDHCI_CONTROL4_DRIVE_7mA    (0x2 << 16)
0100 #define S3C64XX_SDHCI_CONTROL4_DRIVE_9mA    (0x3 << 16)
0101 
0102 #define S3C64XX_SDHCI_CONTROL4_BUSY     (1)
0103 
0104 /**
0105  * struct sdhci_s3c - S3C SDHCI instance
0106  * @host: The SDHCI host created
0107  * @pdev: The platform device we where created from.
0108  * @ioarea: The resource created when we claimed the IO area.
0109  * @pdata: The platform data for this controller.
0110  * @cur_clk: The index of the current bus clock.
0111  * @ext_cd_irq: External card detect interrupt.
0112  * @clk_io: The clock for the internal bus interface.
0113  * @clk_rates: Clock frequencies.
0114  * @clk_bus: The clocks that are available for the SD/MMC bus clock.
0115  * @no_divider: No or non-standard internal clock divider.
0116  */
0117 struct sdhci_s3c {
0118     struct sdhci_host   *host;
0119     struct platform_device  *pdev;
0120     struct resource     *ioarea;
0121     struct s3c_sdhci_platdata *pdata;
0122     int         cur_clk;
0123     int         ext_cd_irq;
0124 
0125     struct clk      *clk_io;
0126     struct clk      *clk_bus[MAX_BUS_CLK];
0127     unsigned long       clk_rates[MAX_BUS_CLK];
0128 
0129     bool            no_divider;
0130 };
0131 
0132 /**
0133  * struct sdhci_s3c_drv_data - S3C SDHCI platform specific driver data
0134  * @sdhci_quirks: sdhci host specific quirks.
0135  * @no_divider: no or non-standard internal clock divider.
0136  *
0137  * Specifies platform specific configuration of sdhci controller.
0138  * Note: A structure for driver specific platform data is used for future
0139  * expansion of its usage.
0140  */
0141 struct sdhci_s3c_drv_data {
0142     unsigned int    sdhci_quirks;
0143     bool        no_divider;
0144 };
0145 
0146 static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
0147 {
0148     return sdhci_priv(host);
0149 }
0150 
0151 /**
0152  * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
0153  * @host: The SDHCI host instance.
0154  *
0155  * Callback to return the maximum clock rate acheivable by the controller.
0156 */
0157 static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
0158 {
0159     struct sdhci_s3c *ourhost = to_s3c(host);
0160     unsigned long rate, max = 0;
0161     int src;
0162 
0163     for (src = 0; src < MAX_BUS_CLK; src++) {
0164         rate = ourhost->clk_rates[src];
0165         if (rate > max)
0166             max = rate;
0167     }
0168 
0169     return max;
0170 }
0171 
0172 /**
0173  * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
0174  * @ourhost: Our SDHCI instance.
0175  * @src: The source clock index.
0176  * @wanted: The clock frequency wanted.
0177  */
0178 static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
0179                          unsigned int src,
0180                          unsigned int wanted)
0181 {
0182     unsigned long rate;
0183     struct clk *clksrc = ourhost->clk_bus[src];
0184     int shift;
0185 
0186     if (IS_ERR(clksrc))
0187         return UINT_MAX;
0188 
0189     /*
0190      * If controller uses a non-standard clock division, find the best clock
0191      * speed possible with selected clock source and skip the division.
0192      */
0193     if (ourhost->no_divider) {
0194         rate = clk_round_rate(clksrc, wanted);
0195         return wanted - rate;
0196     }
0197 
0198     rate = ourhost->clk_rates[src];
0199 
0200     for (shift = 0; shift <= 8; ++shift) {
0201         if ((rate >> shift) <= wanted)
0202             break;
0203     }
0204 
0205     if (shift > 8) {
0206         dev_dbg(&ourhost->pdev->dev,
0207             "clk %d: rate %ld, min rate %lu > wanted %u\n",
0208             src, rate, rate / 256, wanted);
0209         return UINT_MAX;
0210     }
0211 
0212     dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
0213         src, rate, wanted, rate >> shift);
0214 
0215     return wanted - (rate >> shift);
0216 }
0217 
0218 /**
0219  * sdhci_s3c_set_clock - callback on clock change
0220  * @host: The SDHCI host being changed
0221  * @clock: The clock rate being requested.
0222  *
0223  * When the card's clock is going to be changed, look at the new frequency
0224  * and find the best clock source to go with it.
0225 */
0226 static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
0227 {
0228     struct sdhci_s3c *ourhost = to_s3c(host);
0229     unsigned int best = UINT_MAX;
0230     unsigned int delta;
0231     int best_src = 0;
0232     int src;
0233     u32 ctrl;
0234 
0235     host->mmc->actual_clock = 0;
0236 
0237     /* don't bother if the clock is going off. */
0238     if (clock == 0) {
0239         sdhci_set_clock(host, clock);
0240         return;
0241     }
0242 
0243     for (src = 0; src < MAX_BUS_CLK; src++) {
0244         delta = sdhci_s3c_consider_clock(ourhost, src, clock);
0245         if (delta < best) {
0246             best = delta;
0247             best_src = src;
0248         }
0249     }
0250 
0251     dev_dbg(&ourhost->pdev->dev,
0252         "selected source %d, clock %d, delta %d\n",
0253          best_src, clock, best);
0254 
0255     /* select the new clock source */
0256     if (ourhost->cur_clk != best_src) {
0257         struct clk *clk = ourhost->clk_bus[best_src];
0258 
0259         clk_prepare_enable(clk);
0260         if (ourhost->cur_clk >= 0)
0261             clk_disable_unprepare(
0262                     ourhost->clk_bus[ourhost->cur_clk]);
0263 
0264         ourhost->cur_clk = best_src;
0265         host->max_clk = ourhost->clk_rates[best_src];
0266     }
0267 
0268     /* turn clock off to card before changing clock source */
0269     writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
0270 
0271     ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
0272     ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
0273     ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
0274     writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
0275 
0276     /* reprogram default hardware configuration */
0277     writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
0278         host->ioaddr + S3C64XX_SDHCI_CONTROL4);
0279 
0280     ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
0281     ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
0282           S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
0283           S3C_SDHCI_CTRL2_ENFBCLKRX |
0284           S3C_SDHCI_CTRL2_DFCNT_NONE |
0285           S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
0286     writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
0287 
0288     /* reconfigure the controller for new clock rate */
0289     ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
0290     if (clock < 25 * 1000000)
0291         ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
0292     writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
0293 
0294     sdhci_set_clock(host, clock);
0295 }
0296 
0297 /**
0298  * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
0299  * @host: The SDHCI host being queried
0300  *
0301  * To init mmc host properly a minimal clock value is needed. For high system
0302  * bus clock's values the standard formula gives values out of allowed range.
0303  * The clock still can be set to lower values, if clock source other then
0304  * system bus is selected.
0305 */
0306 static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
0307 {
0308     struct sdhci_s3c *ourhost = to_s3c(host);
0309     unsigned long rate, min = ULONG_MAX;
0310     int src;
0311 
0312     for (src = 0; src < MAX_BUS_CLK; src++) {
0313         rate = ourhost->clk_rates[src] / 256;
0314         if (!rate)
0315             continue;
0316         if (rate < min)
0317             min = rate;
0318     }
0319 
0320     return min;
0321 }
0322 
0323 /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
0324 static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
0325 {
0326     struct sdhci_s3c *ourhost = to_s3c(host);
0327     unsigned long rate, max = 0;
0328     int src;
0329 
0330     for (src = 0; src < MAX_BUS_CLK; src++) {
0331         struct clk *clk;
0332 
0333         clk = ourhost->clk_bus[src];
0334         if (IS_ERR(clk))
0335             continue;
0336 
0337         rate = clk_round_rate(clk, ULONG_MAX);
0338         if (rate > max)
0339             max = rate;
0340     }
0341 
0342     return max;
0343 }
0344 
0345 /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
0346 static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
0347 {
0348     struct sdhci_s3c *ourhost = to_s3c(host);
0349     unsigned long rate, min = ULONG_MAX;
0350     int src;
0351 
0352     for (src = 0; src < MAX_BUS_CLK; src++) {
0353         struct clk *clk;
0354 
0355         clk = ourhost->clk_bus[src];
0356         if (IS_ERR(clk))
0357             continue;
0358 
0359         rate = clk_round_rate(clk, 0);
0360         if (rate < min)
0361             min = rate;
0362     }
0363 
0364     return min;
0365 }
0366 
0367 /* sdhci_cmu_set_clock - callback on clock change.*/
0368 static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
0369 {
0370     struct sdhci_s3c *ourhost = to_s3c(host);
0371     struct device *dev = &ourhost->pdev->dev;
0372     unsigned long timeout;
0373     u16 clk = 0;
0374     int ret;
0375 
0376     host->mmc->actual_clock = 0;
0377 
0378     /* If the clock is going off, set to 0 at clock control register */
0379     if (clock == 0) {
0380         sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
0381         return;
0382     }
0383 
0384     sdhci_s3c_set_clock(host, clock);
0385 
0386     /* Reset SD Clock Enable */
0387     clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
0388     clk &= ~SDHCI_CLOCK_CARD_EN;
0389     sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
0390 
0391     ret = clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
0392     if (ret != 0) {
0393         dev_err(dev, "%s: failed to set clock rate %uHz\n",
0394             mmc_hostname(host->mmc), clock);
0395         return;
0396     }
0397 
0398     clk = SDHCI_CLOCK_INT_EN;
0399     sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
0400 
0401     /* Wait max 20 ms */
0402     timeout = 20;
0403     while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
0404         & SDHCI_CLOCK_INT_STABLE)) {
0405         if (timeout == 0) {
0406             dev_err(dev, "%s: Internal clock never stabilised.\n",
0407                 mmc_hostname(host->mmc));
0408             return;
0409         }
0410         timeout--;
0411         mdelay(1);
0412     }
0413 
0414     clk |= SDHCI_CLOCK_CARD_EN;
0415     sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
0416 }
0417 
0418 static struct sdhci_ops sdhci_s3c_ops = {
0419     .get_max_clock      = sdhci_s3c_get_max_clk,
0420     .set_clock      = sdhci_s3c_set_clock,
0421     .get_min_clock      = sdhci_s3c_get_min_clock,
0422     .set_bus_width      = sdhci_set_bus_width,
0423     .reset          = sdhci_reset,
0424     .set_uhs_signaling  = sdhci_set_uhs_signaling,
0425 };
0426 
0427 #ifdef CONFIG_OF
0428 static int sdhci_s3c_parse_dt(struct device *dev,
0429         struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
0430 {
0431     struct device_node *node = dev->of_node;
0432     u32 max_width;
0433 
0434     /* if the bus-width property is not specified, assume width as 1 */
0435     if (of_property_read_u32(node, "bus-width", &max_width))
0436         max_width = 1;
0437     pdata->max_width = max_width;
0438 
0439     /* get the card detection method */
0440     if (of_get_property(node, "broken-cd", NULL)) {
0441         pdata->cd_type = S3C_SDHCI_CD_NONE;
0442         return 0;
0443     }
0444 
0445     if (of_get_property(node, "non-removable", NULL)) {
0446         pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
0447         return 0;
0448     }
0449 
0450     if (of_get_named_gpio(node, "cd-gpios", 0))
0451         return 0;
0452 
0453     /* assuming internal card detect that will be configured by pinctrl */
0454     pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
0455     return 0;
0456 }
0457 #else
0458 static int sdhci_s3c_parse_dt(struct device *dev,
0459         struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
0460 {
0461     return -EINVAL;
0462 }
0463 #endif
0464 
0465 static inline const struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
0466             struct platform_device *pdev)
0467 {
0468 #ifdef CONFIG_OF
0469     if (pdev->dev.of_node)
0470         return of_device_get_match_data(&pdev->dev);
0471 #endif
0472     return (const struct sdhci_s3c_drv_data *)
0473             platform_get_device_id(pdev)->driver_data;
0474 }
0475 
0476 static int sdhci_s3c_probe(struct platform_device *pdev)
0477 {
0478     struct s3c_sdhci_platdata *pdata;
0479     const struct sdhci_s3c_drv_data *drv_data;
0480     struct device *dev = &pdev->dev;
0481     struct sdhci_host *host;
0482     struct sdhci_s3c *sc;
0483     int ret, irq, ptr, clks;
0484 
0485     if (!pdev->dev.platform_data && !pdev->dev.of_node) {
0486         dev_err(dev, "no device data specified\n");
0487         return -ENOENT;
0488     }
0489 
0490     irq = platform_get_irq(pdev, 0);
0491     if (irq < 0)
0492         return irq;
0493 
0494     host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
0495     if (IS_ERR(host)) {
0496         dev_err(dev, "sdhci_alloc_host() failed\n");
0497         return PTR_ERR(host);
0498     }
0499     sc = sdhci_priv(host);
0500 
0501     pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
0502     if (!pdata) {
0503         ret = -ENOMEM;
0504         goto err_pdata_io_clk;
0505     }
0506 
0507     if (pdev->dev.of_node) {
0508         ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
0509         if (ret)
0510             goto err_pdata_io_clk;
0511     } else {
0512         memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
0513     }
0514 
0515     drv_data = sdhci_s3c_get_driver_data(pdev);
0516 
0517     sc->host = host;
0518     sc->pdev = pdev;
0519     sc->pdata = pdata;
0520     sc->cur_clk = -1;
0521 
0522     platform_set_drvdata(pdev, host);
0523 
0524     sc->clk_io = devm_clk_get(dev, "hsmmc");
0525     if (IS_ERR(sc->clk_io)) {
0526         dev_err(dev, "failed to get io clock\n");
0527         ret = PTR_ERR(sc->clk_io);
0528         goto err_pdata_io_clk;
0529     }
0530 
0531     /* enable the local io clock and keep it running for the moment. */
0532     clk_prepare_enable(sc->clk_io);
0533 
0534     for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
0535         char name[14];
0536 
0537         snprintf(name, 14, "mmc_busclk.%d", ptr);
0538         sc->clk_bus[ptr] = devm_clk_get(dev, name);
0539         if (IS_ERR(sc->clk_bus[ptr]))
0540             continue;
0541 
0542         clks++;
0543         sc->clk_rates[ptr] = clk_get_rate(sc->clk_bus[ptr]);
0544 
0545         dev_info(dev, "clock source %d: %s (%ld Hz)\n",
0546                 ptr, name, sc->clk_rates[ptr]);
0547     }
0548 
0549     if (clks == 0) {
0550         dev_err(dev, "failed to find any bus clocks\n");
0551         ret = -ENOENT;
0552         goto err_no_busclks;
0553     }
0554 
0555     host->ioaddr = devm_platform_ioremap_resource(pdev, 0);
0556     if (IS_ERR(host->ioaddr)) {
0557         ret = PTR_ERR(host->ioaddr);
0558         goto err_req_regs;
0559     }
0560 
0561     /* Ensure we have minimal gpio selected CMD/CLK/Detect */
0562     if (pdata->cfg_gpio)
0563         pdata->cfg_gpio(pdev, pdata->max_width);
0564 
0565     host->hw_name = "samsung-hsmmc";
0566     host->ops = &sdhci_s3c_ops;
0567     host->quirks = 0;
0568     host->quirks2 = 0;
0569     host->irq = irq;
0570 
0571     /* Setup quirks for the controller */
0572     host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
0573     host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
0574     if (drv_data) {
0575         host->quirks |= drv_data->sdhci_quirks;
0576         sc->no_divider = drv_data->no_divider;
0577     }
0578 
0579 #ifndef CONFIG_MMC_SDHCI_S3C_DMA
0580 
0581     /* we currently see overruns on errors, so disable the SDMA
0582      * support as well. */
0583     host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
0584 
0585 #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
0586 
0587     /* It seems we do not get an DATA transfer complete on non-busy
0588      * transfers, not sure if this is a problem with this specific
0589      * SDHCI block, or a missing configuration that needs to be set. */
0590     host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
0591 
0592     /* This host supports the Auto CMD12 */
0593     host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
0594 
0595     /* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
0596     host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
0597 
0598     if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
0599         pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
0600         host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
0601 
0602     if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
0603         host->mmc->caps = MMC_CAP_NONREMOVABLE;
0604 
0605     switch (pdata->max_width) {
0606     case 8:
0607         host->mmc->caps |= MMC_CAP_8_BIT_DATA;
0608         fallthrough;
0609     case 4:
0610         host->mmc->caps |= MMC_CAP_4_BIT_DATA;
0611         break;
0612     }
0613 
0614     if (pdata->pm_caps)
0615         host->mmc->pm_caps |= pdata->pm_caps;
0616 
0617     host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
0618              SDHCI_QUIRK_32BIT_DMA_SIZE);
0619 
0620     /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
0621     host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
0622 
0623     /*
0624      * If controller does not have internal clock divider,
0625      * we can use overriding functions instead of default.
0626      */
0627     if (sc->no_divider) {
0628         sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
0629         sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
0630         sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
0631     }
0632 
0633     /* It supports additional host capabilities if needed */
0634     if (pdata->host_caps)
0635         host->mmc->caps |= pdata->host_caps;
0636 
0637     if (pdata->host_caps2)
0638         host->mmc->caps2 |= pdata->host_caps2;
0639 
0640     pm_runtime_enable(&pdev->dev);
0641     pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
0642     pm_runtime_use_autosuspend(&pdev->dev);
0643     pm_suspend_ignore_children(&pdev->dev, 1);
0644 
0645     ret = mmc_of_parse(host->mmc);
0646     if (ret)
0647         goto err_req_regs;
0648 
0649     ret = sdhci_add_host(host);
0650     if (ret)
0651         goto err_req_regs;
0652 
0653 #ifdef CONFIG_PM
0654     if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
0655         clk_disable_unprepare(sc->clk_io);
0656 #endif
0657     return 0;
0658 
0659  err_req_regs:
0660     pm_runtime_disable(&pdev->dev);
0661 
0662  err_no_busclks:
0663     clk_disable_unprepare(sc->clk_io);
0664 
0665  err_pdata_io_clk:
0666     sdhci_free_host(host);
0667 
0668     return ret;
0669 }
0670 
0671 static int sdhci_s3c_remove(struct platform_device *pdev)
0672 {
0673     struct sdhci_host *host =  platform_get_drvdata(pdev);
0674     struct sdhci_s3c *sc = sdhci_priv(host);
0675 
0676     if (sc->ext_cd_irq)
0677         free_irq(sc->ext_cd_irq, sc);
0678 
0679 #ifdef CONFIG_PM
0680     if (sc->pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
0681         clk_prepare_enable(sc->clk_io);
0682 #endif
0683     sdhci_remove_host(host, 1);
0684 
0685     pm_runtime_dont_use_autosuspend(&pdev->dev);
0686     pm_runtime_disable(&pdev->dev);
0687 
0688     clk_disable_unprepare(sc->clk_io);
0689 
0690     sdhci_free_host(host);
0691 
0692     return 0;
0693 }
0694 
0695 #ifdef CONFIG_PM_SLEEP
0696 static int sdhci_s3c_suspend(struct device *dev)
0697 {
0698     struct sdhci_host *host = dev_get_drvdata(dev);
0699 
0700     if (host->tuning_mode != SDHCI_TUNING_MODE_3)
0701         mmc_retune_needed(host->mmc);
0702 
0703     return sdhci_suspend_host(host);
0704 }
0705 
0706 static int sdhci_s3c_resume(struct device *dev)
0707 {
0708     struct sdhci_host *host = dev_get_drvdata(dev);
0709 
0710     return sdhci_resume_host(host);
0711 }
0712 #endif
0713 
0714 #ifdef CONFIG_PM
0715 static int sdhci_s3c_runtime_suspend(struct device *dev)
0716 {
0717     struct sdhci_host *host = dev_get_drvdata(dev);
0718     struct sdhci_s3c *ourhost = to_s3c(host);
0719     struct clk *busclk = ourhost->clk_io;
0720     int ret;
0721 
0722     ret = sdhci_runtime_suspend_host(host);
0723 
0724     if (host->tuning_mode != SDHCI_TUNING_MODE_3)
0725         mmc_retune_needed(host->mmc);
0726 
0727     if (ourhost->cur_clk >= 0)
0728         clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
0729     clk_disable_unprepare(busclk);
0730     return ret;
0731 }
0732 
0733 static int sdhci_s3c_runtime_resume(struct device *dev)
0734 {
0735     struct sdhci_host *host = dev_get_drvdata(dev);
0736     struct sdhci_s3c *ourhost = to_s3c(host);
0737     struct clk *busclk = ourhost->clk_io;
0738     int ret;
0739 
0740     clk_prepare_enable(busclk);
0741     if (ourhost->cur_clk >= 0)
0742         clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
0743     ret = sdhci_runtime_resume_host(host, 0);
0744     return ret;
0745 }
0746 #endif
0747 
0748 static const struct dev_pm_ops sdhci_s3c_pmops = {
0749     SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
0750     SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
0751                NULL)
0752 };
0753 
0754 static const struct platform_device_id sdhci_s3c_driver_ids[] = {
0755     {
0756         .name       = "s3c-sdhci",
0757         .driver_data    = (kernel_ulong_t)NULL,
0758     },
0759     { }
0760 };
0761 MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
0762 
0763 #ifdef CONFIG_OF
0764 static const struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
0765     .no_divider = true,
0766 };
0767 
0768 static const struct of_device_id sdhci_s3c_dt_match[] = {
0769     { .compatible = "samsung,s3c6410-sdhci", },
0770     { .compatible = "samsung,exynos4210-sdhci",
0771         .data = &exynos4_sdhci_drv_data },
0772     {},
0773 };
0774 MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
0775 #endif
0776 
0777 static struct platform_driver sdhci_s3c_driver = {
0778     .probe      = sdhci_s3c_probe,
0779     .remove     = sdhci_s3c_remove,
0780     .id_table   = sdhci_s3c_driver_ids,
0781     .driver     = {
0782         .name   = "s3c-sdhci",
0783         .probe_type = PROBE_PREFER_ASYNCHRONOUS,
0784         .of_match_table = of_match_ptr(sdhci_s3c_dt_match),
0785         .pm = &sdhci_s3c_pmops,
0786     },
0787 };
0788 
0789 module_platform_driver(sdhci_s3c_driver);
0790 
0791 MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
0792 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
0793 MODULE_LICENSE("GPL v2");