xref: /openbmc/linux/drivers/mmc/host/sdhci-s3c.c (revision 7ef2a5e2)
10d1bb41aSBen Dooks /* linux/drivers/mmc/host/sdhci-s3c.c
20d1bb41aSBen Dooks  *
30d1bb41aSBen Dooks  * Copyright 2008 Openmoko Inc.
40d1bb41aSBen Dooks  * Copyright 2008 Simtec Electronics
50d1bb41aSBen Dooks  *      Ben Dooks <ben@simtec.co.uk>
60d1bb41aSBen Dooks  *      http://armlinux.simtec.co.uk/
70d1bb41aSBen Dooks  *
80d1bb41aSBen Dooks  * SDHCI (HSMMC) support for Samsung SoC
90d1bb41aSBen Dooks  *
100d1bb41aSBen Dooks  * This program is free software; you can redistribute it and/or modify
110d1bb41aSBen Dooks  * it under the terms of the GNU General Public License version 2 as
120d1bb41aSBen Dooks  * published by the Free Software Foundation.
130d1bb41aSBen Dooks  */
140d1bb41aSBen Dooks 
150d1bb41aSBen Dooks #include <linux/delay.h>
160d1bb41aSBen Dooks #include <linux/dma-mapping.h>
170d1bb41aSBen Dooks #include <linux/platform_device.h>
18cc014f3eSArnd Bergmann #include <linux/platform_data/mmc-sdhci-s3c.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
200d1bb41aSBen Dooks #include <linux/clk.h>
210d1bb41aSBen Dooks #include <linux/io.h>
2217866e14SMarek Szyprowski #include <linux/gpio.h>
2355156d24SMark Brown #include <linux/module.h>
24d5e9c02cSMark Brown #include <linux/of.h>
25d5e9c02cSMark Brown #include <linux/of_gpio.h>
26d5e9c02cSMark Brown #include <linux/pm.h>
279f4e8151SMark Brown #include <linux/pm_runtime.h>
280d1bb41aSBen Dooks 
290d1bb41aSBen Dooks #include <linux/mmc/host.h>
300d1bb41aSBen Dooks 
31cc014f3eSArnd Bergmann #include "sdhci-s3c-regs.h"
320d1bb41aSBen Dooks #include "sdhci.h"
330d1bb41aSBen Dooks 
340d1bb41aSBen Dooks #define MAX_BUS_CLK	(4)
350d1bb41aSBen Dooks 
36cd1b00ebSThomas Abraham /* Number of gpio's used is max data bus width + command and clock lines */
37cd1b00ebSThomas Abraham #define NUM_GPIOS(x)	(x + 2)
38cd1b00ebSThomas Abraham 
390d1bb41aSBen Dooks /**
400d1bb41aSBen Dooks  * struct sdhci_s3c - S3C SDHCI instance
410d1bb41aSBen Dooks  * @host: The SDHCI host created
420d1bb41aSBen Dooks  * @pdev: The platform device we where created from.
430d1bb41aSBen Dooks  * @ioarea: The resource created when we claimed the IO area.
440d1bb41aSBen Dooks  * @pdata: The platform data for this controller.
450d1bb41aSBen Dooks  * @cur_clk: The index of the current bus clock.
460d1bb41aSBen Dooks  * @clk_io: The clock for the internal bus interface.
470d1bb41aSBen Dooks  * @clk_bus: The clocks that are available for the SD/MMC bus clock.
480d1bb41aSBen Dooks  */
490d1bb41aSBen Dooks struct sdhci_s3c {
500d1bb41aSBen Dooks 	struct sdhci_host	*host;
510d1bb41aSBen Dooks 	struct platform_device	*pdev;
520d1bb41aSBen Dooks 	struct resource		*ioarea;
530d1bb41aSBen Dooks 	struct s3c_sdhci_platdata *pdata;
540d1bb41aSBen Dooks 	unsigned int		cur_clk;
5517866e14SMarek Szyprowski 	int			ext_cd_irq;
5617866e14SMarek Szyprowski 	int			ext_cd_gpio;
570d1bb41aSBen Dooks 
580d1bb41aSBen Dooks 	struct clk		*clk_io;
590d1bb41aSBen Dooks 	struct clk		*clk_bus[MAX_BUS_CLK];
600d1bb41aSBen Dooks };
610d1bb41aSBen Dooks 
623119936aSThomas Abraham /**
633119936aSThomas Abraham  * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
643119936aSThomas Abraham  * @sdhci_quirks: sdhci host specific quirks.
653119936aSThomas Abraham  *
663119936aSThomas Abraham  * Specifies platform specific configuration of sdhci controller.
673119936aSThomas Abraham  * Note: A structure for driver specific platform data is used for future
683119936aSThomas Abraham  * expansion of its usage.
693119936aSThomas Abraham  */
703119936aSThomas Abraham struct sdhci_s3c_drv_data {
713119936aSThomas Abraham 	unsigned int	sdhci_quirks;
723119936aSThomas Abraham };
733119936aSThomas Abraham 
740d1bb41aSBen Dooks static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
750d1bb41aSBen Dooks {
760d1bb41aSBen Dooks 	return sdhci_priv(host);
770d1bb41aSBen Dooks }
780d1bb41aSBen Dooks 
790d1bb41aSBen Dooks /**
800d1bb41aSBen Dooks  * get_curclk - convert ctrl2 register to clock source number
810d1bb41aSBen Dooks  * @ctrl2: Control2 register value.
820d1bb41aSBen Dooks  */
830d1bb41aSBen Dooks static u32 get_curclk(u32 ctrl2)
840d1bb41aSBen Dooks {
850d1bb41aSBen Dooks 	ctrl2 &= S3C_SDHCI_CTRL2_SELBASECLK_MASK;
860d1bb41aSBen Dooks 	ctrl2 >>= S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
870d1bb41aSBen Dooks 
880d1bb41aSBen Dooks 	return ctrl2;
890d1bb41aSBen Dooks }
900d1bb41aSBen Dooks 
910d1bb41aSBen Dooks static void sdhci_s3c_check_sclk(struct sdhci_host *host)
920d1bb41aSBen Dooks {
930d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
940d1bb41aSBen Dooks 	u32 tmp = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
950d1bb41aSBen Dooks 
960d1bb41aSBen Dooks 	if (get_curclk(tmp) != ourhost->cur_clk) {
970d1bb41aSBen Dooks 		dev_dbg(&ourhost->pdev->dev, "restored ctrl2 clock setting\n");
980d1bb41aSBen Dooks 
990d1bb41aSBen Dooks 		tmp &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
1000d1bb41aSBen Dooks 		tmp |= ourhost->cur_clk << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
1017003fecbSJingoo Han 		writel(tmp, host->ioaddr + S3C_SDHCI_CONTROL2);
1020d1bb41aSBen Dooks 	}
1030d1bb41aSBen Dooks }
1040d1bb41aSBen Dooks 
1050d1bb41aSBen Dooks /**
1060d1bb41aSBen Dooks  * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
1070d1bb41aSBen Dooks  * @host: The SDHCI host instance.
1080d1bb41aSBen Dooks  *
1090d1bb41aSBen Dooks  * Callback to return the maximum clock rate acheivable by the controller.
1100d1bb41aSBen Dooks */
1110d1bb41aSBen Dooks static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
1120d1bb41aSBen Dooks {
1130d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
1140d1bb41aSBen Dooks 	struct clk *busclk;
1150d1bb41aSBen Dooks 	unsigned int rate, max;
1160d1bb41aSBen Dooks 	int clk;
1170d1bb41aSBen Dooks 
1180d1bb41aSBen Dooks 	/* note, a reset will reset the clock source */
1190d1bb41aSBen Dooks 
1200d1bb41aSBen Dooks 	sdhci_s3c_check_sclk(host);
1210d1bb41aSBen Dooks 
1220d1bb41aSBen Dooks 	for (max = 0, clk = 0; clk < MAX_BUS_CLK; clk++) {
1230d1bb41aSBen Dooks 		busclk = ourhost->clk_bus[clk];
1240d1bb41aSBen Dooks 		if (!busclk)
1250d1bb41aSBen Dooks 			continue;
1260d1bb41aSBen Dooks 
1270d1bb41aSBen Dooks 		rate = clk_get_rate(busclk);
1280d1bb41aSBen Dooks 		if (rate > max)
1290d1bb41aSBen Dooks 			max = rate;
1300d1bb41aSBen Dooks 	}
1310d1bb41aSBen Dooks 
1320d1bb41aSBen Dooks 	return max;
1330d1bb41aSBen Dooks }
1340d1bb41aSBen Dooks 
1350d1bb41aSBen Dooks /**
1360d1bb41aSBen Dooks  * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
1370d1bb41aSBen Dooks  * @ourhost: Our SDHCI instance.
1380d1bb41aSBen Dooks  * @src: The source clock index.
1390d1bb41aSBen Dooks  * @wanted: The clock frequency wanted.
1400d1bb41aSBen Dooks  */
1410d1bb41aSBen Dooks static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
1420d1bb41aSBen Dooks 					     unsigned int src,
1430d1bb41aSBen Dooks 					     unsigned int wanted)
1440d1bb41aSBen Dooks {
1450d1bb41aSBen Dooks 	unsigned long rate;
1460d1bb41aSBen Dooks 	struct clk *clksrc = ourhost->clk_bus[src];
1470d1bb41aSBen Dooks 	int div;
1480d1bb41aSBen Dooks 
1490d1bb41aSBen Dooks 	if (!clksrc)
1500d1bb41aSBen Dooks 		return UINT_MAX;
1510d1bb41aSBen Dooks 
152253e0a7cSJeongbae Seo 	/*
1533119936aSThomas Abraham 	 * If controller uses a non-standard clock division, find the best clock
1543119936aSThomas Abraham 	 * speed possible with selected clock source and skip the division.
155253e0a7cSJeongbae Seo 	 */
1563119936aSThomas Abraham 	if (ourhost->host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
157253e0a7cSJeongbae Seo 		rate = clk_round_rate(clksrc, wanted);
158253e0a7cSJeongbae Seo 		return wanted - rate;
159253e0a7cSJeongbae Seo 	}
160253e0a7cSJeongbae Seo 
1610d1bb41aSBen Dooks 	rate = clk_get_rate(clksrc);
1620d1bb41aSBen Dooks 
1630d1bb41aSBen Dooks 	for (div = 1; div < 256; div *= 2) {
1640d1bb41aSBen Dooks 		if ((rate / div) <= wanted)
1650d1bb41aSBen Dooks 			break;
1660d1bb41aSBen Dooks 	}
1670d1bb41aSBen Dooks 
1680d1bb41aSBen Dooks 	dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
1690d1bb41aSBen Dooks 		src, rate, wanted, rate / div);
1700d1bb41aSBen Dooks 
1712ad0b249SJingoo Han 	return wanted - (rate / div);
1720d1bb41aSBen Dooks }
1730d1bb41aSBen Dooks 
1740d1bb41aSBen Dooks /**
1750d1bb41aSBen Dooks  * sdhci_s3c_set_clock - callback on clock change
1760d1bb41aSBen Dooks  * @host: The SDHCI host being changed
1770d1bb41aSBen Dooks  * @clock: The clock rate being requested.
1780d1bb41aSBen Dooks  *
1790d1bb41aSBen Dooks  * When the card's clock is going to be changed, look at the new frequency
1800d1bb41aSBen Dooks  * and find the best clock source to go with it.
1810d1bb41aSBen Dooks */
1820d1bb41aSBen Dooks static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
1830d1bb41aSBen Dooks {
1840d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
1850d1bb41aSBen Dooks 	unsigned int best = UINT_MAX;
1860d1bb41aSBen Dooks 	unsigned int delta;
1870d1bb41aSBen Dooks 	int best_src = 0;
1880d1bb41aSBen Dooks 	int src;
1890d1bb41aSBen Dooks 	u32 ctrl;
1900d1bb41aSBen Dooks 
1910d1bb41aSBen Dooks 	/* don't bother if the clock is going off. */
1920d1bb41aSBen Dooks 	if (clock == 0)
1930d1bb41aSBen Dooks 		return;
1940d1bb41aSBen Dooks 
1950d1bb41aSBen Dooks 	for (src = 0; src < MAX_BUS_CLK; src++) {
1960d1bb41aSBen Dooks 		delta = sdhci_s3c_consider_clock(ourhost, src, clock);
1970d1bb41aSBen Dooks 		if (delta < best) {
1980d1bb41aSBen Dooks 			best = delta;
1990d1bb41aSBen Dooks 			best_src = src;
2000d1bb41aSBen Dooks 		}
2010d1bb41aSBen Dooks 	}
2020d1bb41aSBen Dooks 
2030d1bb41aSBen Dooks 	dev_dbg(&ourhost->pdev->dev,
2040d1bb41aSBen Dooks 		"selected source %d, clock %d, delta %d\n",
2050d1bb41aSBen Dooks 		 best_src, clock, best);
2060d1bb41aSBen Dooks 
2070d1bb41aSBen Dooks 	/* select the new clock source */
2080d1bb41aSBen Dooks 	if (ourhost->cur_clk != best_src) {
2090d1bb41aSBen Dooks 		struct clk *clk = ourhost->clk_bus[best_src];
2100d1bb41aSBen Dooks 
2110f310a05SThomas Abraham 		clk_prepare_enable(clk);
2120f310a05SThomas Abraham 		clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
213e684c468SChander Kashyap 
2140d1bb41aSBen Dooks 		/* turn clock off to card before changing clock source */
2150d1bb41aSBen Dooks 		writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
2160d1bb41aSBen Dooks 
2170d1bb41aSBen Dooks 		ourhost->cur_clk = best_src;
2180d1bb41aSBen Dooks 		host->max_clk = clk_get_rate(clk);
2190d1bb41aSBen Dooks 
2200d1bb41aSBen Dooks 		ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
2210d1bb41aSBen Dooks 		ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
2220d1bb41aSBen Dooks 		ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
2230d1bb41aSBen Dooks 		writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
2240d1bb41aSBen Dooks 	}
2250d1bb41aSBen Dooks 
2266fe47179SThomas Abraham 	/* reprogram default hardware configuration */
2276fe47179SThomas Abraham 	writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
2286fe47179SThomas Abraham 		host->ioaddr + S3C64XX_SDHCI_CONTROL4);
2290d1bb41aSBen Dooks 
2306fe47179SThomas Abraham 	ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
2316fe47179SThomas Abraham 	ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
2326fe47179SThomas Abraham 		  S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
2336fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_ENFBCLKRX |
2346fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_DFCNT_NONE |
2356fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
2366fe47179SThomas Abraham 	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
2370d1bb41aSBen Dooks 
2386fe47179SThomas Abraham 	/* reconfigure the controller for new clock rate */
2396fe47179SThomas Abraham 	ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
2406fe47179SThomas Abraham 	if (clock < 25 * 1000000)
2416fe47179SThomas Abraham 		ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
2426fe47179SThomas Abraham 	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
2430d1bb41aSBen Dooks }
2440d1bb41aSBen Dooks 
245ce5f036bSMarek Szyprowski /**
246ce5f036bSMarek Szyprowski  * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
247ce5f036bSMarek Szyprowski  * @host: The SDHCI host being queried
248ce5f036bSMarek Szyprowski  *
249ce5f036bSMarek Szyprowski  * To init mmc host properly a minimal clock value is needed. For high system
250ce5f036bSMarek Szyprowski  * bus clock's values the standard formula gives values out of allowed range.
251ce5f036bSMarek Szyprowski  * The clock still can be set to lower values, if clock source other then
252ce5f036bSMarek Szyprowski  * system bus is selected.
253ce5f036bSMarek Szyprowski */
254ce5f036bSMarek Szyprowski static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
255ce5f036bSMarek Szyprowski {
256ce5f036bSMarek Szyprowski 	struct sdhci_s3c *ourhost = to_s3c(host);
257ce5f036bSMarek Szyprowski 	unsigned int delta, min = UINT_MAX;
258ce5f036bSMarek Szyprowski 	int src;
259ce5f036bSMarek Szyprowski 
260ce5f036bSMarek Szyprowski 	for (src = 0; src < MAX_BUS_CLK; src++) {
261ce5f036bSMarek Szyprowski 		delta = sdhci_s3c_consider_clock(ourhost, src, 0);
262ce5f036bSMarek Szyprowski 		if (delta == UINT_MAX)
263ce5f036bSMarek Szyprowski 			continue;
264ce5f036bSMarek Szyprowski 		/* delta is a negative value in this case */
265ce5f036bSMarek Szyprowski 		if (-delta < min)
266ce5f036bSMarek Szyprowski 			min = -delta;
267ce5f036bSMarek Szyprowski 	}
268ce5f036bSMarek Szyprowski 	return min;
269ce5f036bSMarek Szyprowski }
270ce5f036bSMarek Szyprowski 
271253e0a7cSJeongbae Seo /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
272253e0a7cSJeongbae Seo static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
273253e0a7cSJeongbae Seo {
274253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
275253e0a7cSJeongbae Seo 
276253e0a7cSJeongbae Seo 	return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], UINT_MAX);
277253e0a7cSJeongbae Seo }
278253e0a7cSJeongbae Seo 
279253e0a7cSJeongbae Seo /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
280253e0a7cSJeongbae Seo static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
281253e0a7cSJeongbae Seo {
282253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
283253e0a7cSJeongbae Seo 
284253e0a7cSJeongbae Seo 	/*
285253e0a7cSJeongbae Seo 	 * initial clock can be in the frequency range of
286253e0a7cSJeongbae Seo 	 * 100KHz-400KHz, so we set it as max value.
287253e0a7cSJeongbae Seo 	 */
288253e0a7cSJeongbae Seo 	return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], 400000);
289253e0a7cSJeongbae Seo }
290253e0a7cSJeongbae Seo 
291253e0a7cSJeongbae Seo /* sdhci_cmu_set_clock - callback on clock change.*/
292253e0a7cSJeongbae Seo static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
293253e0a7cSJeongbae Seo {
294253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
2952ad0b249SJingoo Han 	struct device *dev = &ourhost->pdev->dev;
2963119936aSThomas Abraham 	unsigned long timeout;
2973119936aSThomas Abraham 	u16 clk = 0;
298253e0a7cSJeongbae Seo 
2997ef2a5e2SJaehoon Chung 	/* If the clock is going off, set to 0 at clock control register */
3007ef2a5e2SJaehoon Chung 	if (clock == 0) {
3017ef2a5e2SJaehoon Chung 		sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
3027ef2a5e2SJaehoon Chung 		host->clock = clock;
303253e0a7cSJeongbae Seo 		return;
3047ef2a5e2SJaehoon Chung 	}
305253e0a7cSJeongbae Seo 
306253e0a7cSJeongbae Seo 	sdhci_s3c_set_clock(host, clock);
307253e0a7cSJeongbae Seo 
308253e0a7cSJeongbae Seo 	clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
309253e0a7cSJeongbae Seo 
310253e0a7cSJeongbae Seo 	host->clock = clock;
3113119936aSThomas Abraham 
3123119936aSThomas Abraham 	clk = SDHCI_CLOCK_INT_EN;
3133119936aSThomas Abraham 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
3143119936aSThomas Abraham 
3153119936aSThomas Abraham 	/* Wait max 20 ms */
3163119936aSThomas Abraham 	timeout = 20;
3173119936aSThomas Abraham 	while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
3183119936aSThomas Abraham 		& SDHCI_CLOCK_INT_STABLE)) {
3193119936aSThomas Abraham 		if (timeout == 0) {
3202ad0b249SJingoo Han 			dev_err(dev, "%s: Internal clock never stabilised.\n",
3212ad0b249SJingoo Han 				mmc_hostname(host->mmc));
3223119936aSThomas Abraham 			return;
3233119936aSThomas Abraham 		}
3243119936aSThomas Abraham 		timeout--;
3253119936aSThomas Abraham 		mdelay(1);
3263119936aSThomas Abraham 	}
3273119936aSThomas Abraham 
3283119936aSThomas Abraham 	clk |= SDHCI_CLOCK_CARD_EN;
3293119936aSThomas Abraham 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
330253e0a7cSJeongbae Seo }
331253e0a7cSJeongbae Seo 
332548f07d2SJaehoon Chung /**
3337bc088d3SSascha Hauer  * sdhci_s3c_platform_bus_width - support 8bit buswidth
334548f07d2SJaehoon Chung  * @host: The SDHCI host being queried
335548f07d2SJaehoon Chung  * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
336548f07d2SJaehoon Chung  *
337548f07d2SJaehoon Chung  * We have 8-bit width support but is not a v3 controller.
3387bc088d3SSascha Hauer  * So we add platform_bus_width() and support 8bit width.
339548f07d2SJaehoon Chung  */
3407bc088d3SSascha Hauer static int sdhci_s3c_platform_bus_width(struct sdhci_host *host, int width)
341548f07d2SJaehoon Chung {
342548f07d2SJaehoon Chung 	u8 ctrl;
343548f07d2SJaehoon Chung 
344548f07d2SJaehoon Chung 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
345548f07d2SJaehoon Chung 
346548f07d2SJaehoon Chung 	switch (width) {
347548f07d2SJaehoon Chung 	case MMC_BUS_WIDTH_8:
348548f07d2SJaehoon Chung 		ctrl |= SDHCI_CTRL_8BITBUS;
349548f07d2SJaehoon Chung 		ctrl &= ~SDHCI_CTRL_4BITBUS;
350548f07d2SJaehoon Chung 		break;
351548f07d2SJaehoon Chung 	case MMC_BUS_WIDTH_4:
352548f07d2SJaehoon Chung 		ctrl |= SDHCI_CTRL_4BITBUS;
353548f07d2SJaehoon Chung 		ctrl &= ~SDHCI_CTRL_8BITBUS;
354548f07d2SJaehoon Chung 		break;
355548f07d2SJaehoon Chung 	default:
35649bb1e61SGirish K S 		ctrl &= ~SDHCI_CTRL_4BITBUS;
35749bb1e61SGirish K S 		ctrl &= ~SDHCI_CTRL_8BITBUS;
358548f07d2SJaehoon Chung 		break;
359548f07d2SJaehoon Chung 	}
360548f07d2SJaehoon Chung 
361548f07d2SJaehoon Chung 	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
362548f07d2SJaehoon Chung 
363548f07d2SJaehoon Chung 	return 0;
364548f07d2SJaehoon Chung }
365548f07d2SJaehoon Chung 
3660d1bb41aSBen Dooks static struct sdhci_ops sdhci_s3c_ops = {
3670d1bb41aSBen Dooks 	.get_max_clock		= sdhci_s3c_get_max_clk,
3680d1bb41aSBen Dooks 	.set_clock		= sdhci_s3c_set_clock,
369ce5f036bSMarek Szyprowski 	.get_min_clock		= sdhci_s3c_get_min_clock,
3707bc088d3SSascha Hauer 	.platform_bus_width	= sdhci_s3c_platform_bus_width,
3710d1bb41aSBen Dooks };
3720d1bb41aSBen Dooks 
37317866e14SMarek Szyprowski static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
37417866e14SMarek Szyprowski {
37517866e14SMarek Szyprowski 	struct sdhci_host *host = platform_get_drvdata(dev);
3764577f77bSSachin Kamat #ifdef CONFIG_PM_RUNTIME
377fe007c02SHeiko Stübner 	struct sdhci_s3c *sc = sdhci_priv(host);
3784577f77bSSachin Kamat #endif
37906fe577fSMarek Szyprowski 	unsigned long flags;
38006fe577fSMarek Szyprowski 
38117866e14SMarek Szyprowski 	if (host) {
38206fe577fSMarek Szyprowski 		spin_lock_irqsave(&host->lock, flags);
38317866e14SMarek Szyprowski 		if (state) {
38417866e14SMarek Szyprowski 			dev_dbg(&dev->dev, "card inserted.\n");
385fe007c02SHeiko Stübner #ifdef CONFIG_PM_RUNTIME
386fe007c02SHeiko Stübner 			clk_prepare_enable(sc->clk_io);
387fe007c02SHeiko Stübner #endif
38817866e14SMarek Szyprowski 			host->flags &= ~SDHCI_DEVICE_DEAD;
38917866e14SMarek Szyprowski 			host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
39017866e14SMarek Szyprowski 		} else {
39117866e14SMarek Szyprowski 			dev_dbg(&dev->dev, "card removed.\n");
39217866e14SMarek Szyprowski 			host->flags |= SDHCI_DEVICE_DEAD;
39317866e14SMarek Szyprowski 			host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
394fe007c02SHeiko Stübner #ifdef CONFIG_PM_RUNTIME
395fe007c02SHeiko Stübner 			clk_disable_unprepare(sc->clk_io);
396fe007c02SHeiko Stübner #endif
39717866e14SMarek Szyprowski 		}
398f522886eSKyungmin Park 		tasklet_schedule(&host->card_tasklet);
39906fe577fSMarek Szyprowski 		spin_unlock_irqrestore(&host->lock, flags);
40017866e14SMarek Szyprowski 	}
40117866e14SMarek Szyprowski }
40217866e14SMarek Szyprowski 
40317866e14SMarek Szyprowski static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
40417866e14SMarek Szyprowski {
40517866e14SMarek Szyprowski 	struct sdhci_s3c *sc = dev_id;
40617866e14SMarek Szyprowski 	int status = gpio_get_value(sc->ext_cd_gpio);
40717866e14SMarek Szyprowski 	if (sc->pdata->ext_cd_gpio_invert)
40817866e14SMarek Szyprowski 		status = !status;
40917866e14SMarek Szyprowski 	sdhci_s3c_notify_change(sc->pdev, status);
41017866e14SMarek Szyprowski 	return IRQ_HANDLED;
41117866e14SMarek Szyprowski }
41217866e14SMarek Szyprowski 
41317866e14SMarek Szyprowski static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
41417866e14SMarek Szyprowski {
41517866e14SMarek Szyprowski 	struct s3c_sdhci_platdata *pdata = sc->pdata;
41617866e14SMarek Szyprowski 	struct device *dev = &sc->pdev->dev;
41717866e14SMarek Szyprowski 
418b1b8fea9STomasz Figa 	if (devm_gpio_request(dev, pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
41917866e14SMarek Szyprowski 		sc->ext_cd_gpio = pdata->ext_cd_gpio;
42017866e14SMarek Szyprowski 		sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
42117866e14SMarek Szyprowski 		if (sc->ext_cd_irq &&
42217866e14SMarek Szyprowski 		    request_threaded_irq(sc->ext_cd_irq, NULL,
42317866e14SMarek Szyprowski 					 sdhci_s3c_gpio_card_detect_thread,
4242ad0b249SJingoo Han 					 IRQF_TRIGGER_RISING |
4252ad0b249SJingoo Han 					 IRQF_TRIGGER_FALLING |
4262ad0b249SJingoo Han 					 IRQF_ONESHOT,
42717866e14SMarek Szyprowski 					 dev_name(dev), sc) == 0) {
42817866e14SMarek Szyprowski 			int status = gpio_get_value(sc->ext_cd_gpio);
42917866e14SMarek Szyprowski 			if (pdata->ext_cd_gpio_invert)
43017866e14SMarek Szyprowski 				status = !status;
43117866e14SMarek Szyprowski 			sdhci_s3c_notify_change(sc->pdev, status);
43217866e14SMarek Szyprowski 		} else {
43317866e14SMarek Szyprowski 			dev_warn(dev, "cannot request irq for card detect\n");
43417866e14SMarek Szyprowski 			sc->ext_cd_irq = 0;
43517866e14SMarek Szyprowski 		}
43617866e14SMarek Szyprowski 	} else {
43717866e14SMarek Szyprowski 		dev_err(dev, "cannot request gpio for card detect\n");
43817866e14SMarek Szyprowski 	}
43917866e14SMarek Szyprowski }
44017866e14SMarek Szyprowski 
441cd1b00ebSThomas Abraham #ifdef CONFIG_OF
442c3be1efdSBill Pemberton static int sdhci_s3c_parse_dt(struct device *dev,
443cd1b00ebSThomas Abraham 		struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
444cd1b00ebSThomas Abraham {
445cd1b00ebSThomas Abraham 	struct device_node *node = dev->of_node;
446cd1b00ebSThomas Abraham 	struct sdhci_s3c *ourhost = to_s3c(host);
447cd1b00ebSThomas Abraham 	u32 max_width;
448e19499aeSThomas Abraham 	int gpio;
449cd1b00ebSThomas Abraham 
450cd1b00ebSThomas Abraham 	/* if the bus-width property is not specified, assume width as 1 */
451cd1b00ebSThomas Abraham 	if (of_property_read_u32(node, "bus-width", &max_width))
452cd1b00ebSThomas Abraham 		max_width = 1;
453cd1b00ebSThomas Abraham 	pdata->max_width = max_width;
454cd1b00ebSThomas Abraham 
455cd1b00ebSThomas Abraham 	/* get the card detection method */
456ab5023efSTushar Behera 	if (of_get_property(node, "broken-cd", NULL)) {
457cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_NONE;
458e19499aeSThomas Abraham 		return 0;
459cd1b00ebSThomas Abraham 	}
460cd1b00ebSThomas Abraham 
461ab5023efSTushar Behera 	if (of_get_property(node, "non-removable", NULL)) {
462cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
463e19499aeSThomas Abraham 		return 0;
464cd1b00ebSThomas Abraham 	}
465cd1b00ebSThomas Abraham 
466cd1b00ebSThomas Abraham 	gpio = of_get_named_gpio(node, "cd-gpios", 0);
467cd1b00ebSThomas Abraham 	if (gpio_is_valid(gpio)) {
468cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_GPIO;
469e19499aeSThomas Abraham 		pdata->ext_cd_gpio = gpio;
470e19499aeSThomas Abraham 		ourhost->ext_cd_gpio = -1;
471e19499aeSThomas Abraham 		if (of_get_property(node, "cd-inverted", NULL))
472e19499aeSThomas Abraham 			pdata->ext_cd_gpio_invert = 1;
473e19499aeSThomas Abraham 		return 0;
474cd1b00ebSThomas Abraham 	} else if (gpio != -ENOENT) {
475cd1b00ebSThomas Abraham 		dev_err(dev, "invalid card detect gpio specified\n");
476cd1b00ebSThomas Abraham 		return -EINVAL;
477cd1b00ebSThomas Abraham 	}
478cd1b00ebSThomas Abraham 
479b96efccbSTomasz Figa 	/* assuming internal card detect that will be configured by pinctrl */
480b96efccbSTomasz Figa 	pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
481cd1b00ebSThomas Abraham 	return 0;
482cd1b00ebSThomas Abraham }
483cd1b00ebSThomas Abraham #else
484c3be1efdSBill Pemberton static int sdhci_s3c_parse_dt(struct device *dev,
485cd1b00ebSThomas Abraham 		struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
486cd1b00ebSThomas Abraham {
487cd1b00ebSThomas Abraham 	return -EINVAL;
488cd1b00ebSThomas Abraham }
489cd1b00ebSThomas Abraham #endif
490cd1b00ebSThomas Abraham 
491cd1b00ebSThomas Abraham static const struct of_device_id sdhci_s3c_dt_match[];
492cd1b00ebSThomas Abraham 
4933119936aSThomas Abraham static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
4943119936aSThomas Abraham 			struct platform_device *pdev)
4953119936aSThomas Abraham {
496cd1b00ebSThomas Abraham #ifdef CONFIG_OF
497cd1b00ebSThomas Abraham 	if (pdev->dev.of_node) {
498cd1b00ebSThomas Abraham 		const struct of_device_id *match;
499cd1b00ebSThomas Abraham 		match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
500cd1b00ebSThomas Abraham 		return (struct sdhci_s3c_drv_data *)match->data;
501cd1b00ebSThomas Abraham 	}
502cd1b00ebSThomas Abraham #endif
5033119936aSThomas Abraham 	return (struct sdhci_s3c_drv_data *)
5043119936aSThomas Abraham 			platform_get_device_id(pdev)->driver_data;
5053119936aSThomas Abraham }
5063119936aSThomas Abraham 
507c3be1efdSBill Pemberton static int sdhci_s3c_probe(struct platform_device *pdev)
5080d1bb41aSBen Dooks {
5091d4dc338SThomas Abraham 	struct s3c_sdhci_platdata *pdata;
5103119936aSThomas Abraham 	struct sdhci_s3c_drv_data *drv_data;
5110d1bb41aSBen Dooks 	struct device *dev = &pdev->dev;
5120d1bb41aSBen Dooks 	struct sdhci_host *host;
5130d1bb41aSBen Dooks 	struct sdhci_s3c *sc;
5140d1bb41aSBen Dooks 	struct resource *res;
5150d1bb41aSBen Dooks 	int ret, irq, ptr, clks;
5160d1bb41aSBen Dooks 
517cd1b00ebSThomas Abraham 	if (!pdev->dev.platform_data && !pdev->dev.of_node) {
5180d1bb41aSBen Dooks 		dev_err(dev, "no device data specified\n");
5190d1bb41aSBen Dooks 		return -ENOENT;
5200d1bb41aSBen Dooks 	}
5210d1bb41aSBen Dooks 
5220d1bb41aSBen Dooks 	irq = platform_get_irq(pdev, 0);
5230d1bb41aSBen Dooks 	if (irq < 0) {
5240d1bb41aSBen Dooks 		dev_err(dev, "no irq specified\n");
5250d1bb41aSBen Dooks 		return irq;
5260d1bb41aSBen Dooks 	}
5270d1bb41aSBen Dooks 
5280d1bb41aSBen Dooks 	host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
5290d1bb41aSBen Dooks 	if (IS_ERR(host)) {
5300d1bb41aSBen Dooks 		dev_err(dev, "sdhci_alloc_host() failed\n");
5310d1bb41aSBen Dooks 		return PTR_ERR(host);
5320d1bb41aSBen Dooks 	}
533cd1b00ebSThomas Abraham 	sc = sdhci_priv(host);
5340d1bb41aSBen Dooks 
5351d4dc338SThomas Abraham 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
5361d4dc338SThomas Abraham 	if (!pdata) {
5371d4dc338SThomas Abraham 		ret = -ENOMEM;
538b1b8fea9STomasz Figa 		goto err_pdata_io_clk;
5391d4dc338SThomas Abraham 	}
540cd1b00ebSThomas Abraham 
541cd1b00ebSThomas Abraham 	if (pdev->dev.of_node) {
542cd1b00ebSThomas Abraham 		ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
543cd1b00ebSThomas Abraham 		if (ret)
544b1b8fea9STomasz Figa 			goto err_pdata_io_clk;
545cd1b00ebSThomas Abraham 	} else {
5461d4dc338SThomas Abraham 		memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
547cd1b00ebSThomas Abraham 		sc->ext_cd_gpio = -1; /* invalid gpio number */
548cd1b00ebSThomas Abraham 	}
5491d4dc338SThomas Abraham 
5503119936aSThomas Abraham 	drv_data = sdhci_s3c_get_driver_data(pdev);
5510d1bb41aSBen Dooks 
5520d1bb41aSBen Dooks 	sc->host = host;
5530d1bb41aSBen Dooks 	sc->pdev = pdev;
5540d1bb41aSBen Dooks 	sc->pdata = pdata;
5550d1bb41aSBen Dooks 
5560d1bb41aSBen Dooks 	platform_set_drvdata(pdev, host);
5570d1bb41aSBen Dooks 
5583aaf7ba7SJingoo Han 	sc->clk_io = devm_clk_get(dev, "hsmmc");
5590d1bb41aSBen Dooks 	if (IS_ERR(sc->clk_io)) {
5600d1bb41aSBen Dooks 		dev_err(dev, "failed to get io clock\n");
5610d1bb41aSBen Dooks 		ret = PTR_ERR(sc->clk_io);
562b1b8fea9STomasz Figa 		goto err_pdata_io_clk;
5630d1bb41aSBen Dooks 	}
5640d1bb41aSBen Dooks 
5650d1bb41aSBen Dooks 	/* enable the local io clock and keep it running for the moment. */
5660f310a05SThomas Abraham 	clk_prepare_enable(sc->clk_io);
5670d1bb41aSBen Dooks 
5680d1bb41aSBen Dooks 	for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
5690d1bb41aSBen Dooks 		struct clk *clk;
5704346b6d9SRajeshwari Shinde 		char name[14];
5710d1bb41aSBen Dooks 
5724346b6d9SRajeshwari Shinde 		snprintf(name, 14, "mmc_busclk.%d", ptr);
5733aaf7ba7SJingoo Han 		clk = devm_clk_get(dev, name);
5742ad0b249SJingoo Han 		if (IS_ERR(clk))
5750d1bb41aSBen Dooks 			continue;
5760d1bb41aSBen Dooks 
5770d1bb41aSBen Dooks 		clks++;
5780d1bb41aSBen Dooks 		sc->clk_bus[ptr] = clk;
579253e0a7cSJeongbae Seo 
580253e0a7cSJeongbae Seo 		/*
581253e0a7cSJeongbae Seo 		 * save current clock index to know which clock bus
582253e0a7cSJeongbae Seo 		 * is used later in overriding functions.
583253e0a7cSJeongbae Seo 		 */
584253e0a7cSJeongbae Seo 		sc->cur_clk = ptr;
585253e0a7cSJeongbae Seo 
5860d1bb41aSBen Dooks 		dev_info(dev, "clock source %d: %s (%ld Hz)\n",
5870d1bb41aSBen Dooks 			 ptr, name, clk_get_rate(clk));
5880d1bb41aSBen Dooks 	}
5890d1bb41aSBen Dooks 
5900d1bb41aSBen Dooks 	if (clks == 0) {
5910d1bb41aSBen Dooks 		dev_err(dev, "failed to find any bus clocks\n");
5920d1bb41aSBen Dooks 		ret = -ENOENT;
5930d1bb41aSBen Dooks 		goto err_no_busclks;
5940d1bb41aSBen Dooks 	}
5950d1bb41aSBen Dooks 
5962abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME
5970f310a05SThomas Abraham 	clk_prepare_enable(sc->clk_bus[sc->cur_clk]);
5982abeb5c5SChander Kashyap #endif
599e684c468SChander Kashyap 
6009bda6da7SJulia Lawall 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
601a3e2cd7fSThierry Reding 	host->ioaddr = devm_ioremap_resource(&pdev->dev, res);
602a3e2cd7fSThierry Reding 	if (IS_ERR(host->ioaddr)) {
603a3e2cd7fSThierry Reding 		ret = PTR_ERR(host->ioaddr);
6040d1bb41aSBen Dooks 		goto err_req_regs;
6050d1bb41aSBen Dooks 	}
6060d1bb41aSBen Dooks 
6070d1bb41aSBen Dooks 	/* Ensure we have minimal gpio selected CMD/CLK/Detect */
6080d1bb41aSBen Dooks 	if (pdata->cfg_gpio)
6090d1bb41aSBen Dooks 		pdata->cfg_gpio(pdev, pdata->max_width);
6100d1bb41aSBen Dooks 
6110d1bb41aSBen Dooks 	host->hw_name = "samsung-hsmmc";
6120d1bb41aSBen Dooks 	host->ops = &sdhci_s3c_ops;
6130d1bb41aSBen Dooks 	host->quirks = 0;
6140d1bb41aSBen Dooks 	host->irq = irq;
6150d1bb41aSBen Dooks 
6160d1bb41aSBen Dooks 	/* Setup quirks for the controller */
617b2e75effSThomas Abraham 	host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
618a1d56460SMarek Szyprowski 	host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
6193119936aSThomas Abraham 	if (drv_data)
6203119936aSThomas Abraham 		host->quirks |= drv_data->sdhci_quirks;
6210d1bb41aSBen Dooks 
6220d1bb41aSBen Dooks #ifndef CONFIG_MMC_SDHCI_S3C_DMA
6230d1bb41aSBen Dooks 
6240d1bb41aSBen Dooks 	/* we currently see overruns on errors, so disable the SDMA
6250d1bb41aSBen Dooks 	 * support as well. */
6260d1bb41aSBen Dooks 	host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
6270d1bb41aSBen Dooks 
6280d1bb41aSBen Dooks #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
6290d1bb41aSBen Dooks 
6300d1bb41aSBen Dooks 	/* It seems we do not get an DATA transfer complete on non-busy
6310d1bb41aSBen Dooks 	 * transfers, not sure if this is a problem with this specific
6320d1bb41aSBen Dooks 	 * SDHCI block, or a missing configuration that needs to be set. */
6330d1bb41aSBen Dooks 	host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
6340d1bb41aSBen Dooks 
635732f0e31SKyungmin Park 	/* This host supports the Auto CMD12 */
636732f0e31SKyungmin Park 	host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
637732f0e31SKyungmin Park 
6387199e2b6SJaehoon Chung 	/* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
6397199e2b6SJaehoon Chung 	host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
6407199e2b6SJaehoon Chung 
64117866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
64217866e14SMarek Szyprowski 	    pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
64317866e14SMarek Szyprowski 		host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
64417866e14SMarek Szyprowski 
64517866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
64617866e14SMarek Szyprowski 		host->mmc->caps = MMC_CAP_NONREMOVABLE;
64717866e14SMarek Szyprowski 
6480d22c770SThomas Abraham 	switch (pdata->max_width) {
6490d22c770SThomas Abraham 	case 8:
6500d22c770SThomas Abraham 		host->mmc->caps |= MMC_CAP_8_BIT_DATA;
6510d22c770SThomas Abraham 	case 4:
6520d22c770SThomas Abraham 		host->mmc->caps |= MMC_CAP_4_BIT_DATA;
6530d22c770SThomas Abraham 		break;
6540d22c770SThomas Abraham 	}
6550d22c770SThomas Abraham 
656fa1773ccSSangwook Lee 	if (pdata->pm_caps)
657fa1773ccSSangwook Lee 		host->mmc->pm_caps |= pdata->pm_caps;
658fa1773ccSSangwook Lee 
6590d1bb41aSBen Dooks 	host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
6600d1bb41aSBen Dooks 			 SDHCI_QUIRK_32BIT_DMA_SIZE);
6610d1bb41aSBen Dooks 
6623fe42e07SHyuk Lee 	/* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
6633fe42e07SHyuk Lee 	host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
6643fe42e07SHyuk Lee 
665253e0a7cSJeongbae Seo 	/*
666253e0a7cSJeongbae Seo 	 * If controller does not have internal clock divider,
667253e0a7cSJeongbae Seo 	 * we can use overriding functions instead of default.
668253e0a7cSJeongbae Seo 	 */
6693119936aSThomas Abraham 	if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
670253e0a7cSJeongbae Seo 		sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
671253e0a7cSJeongbae Seo 		sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
672253e0a7cSJeongbae Seo 		sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
673253e0a7cSJeongbae Seo 	}
674253e0a7cSJeongbae Seo 
675b3824f2cSJeongbae Seo 	/* It supports additional host capabilities if needed */
676b3824f2cSJeongbae Seo 	if (pdata->host_caps)
677b3824f2cSJeongbae Seo 		host->mmc->caps |= pdata->host_caps;
678b3824f2cSJeongbae Seo 
679c1c4b66dSJaehoon Chung 	if (pdata->host_caps2)
680c1c4b66dSJaehoon Chung 		host->mmc->caps2 |= pdata->host_caps2;
681c1c4b66dSJaehoon Chung 
6829f4e8151SMark Brown 	pm_runtime_enable(&pdev->dev);
6839f4e8151SMark Brown 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
6849f4e8151SMark Brown 	pm_runtime_use_autosuspend(&pdev->dev);
6859f4e8151SMark Brown 	pm_suspend_ignore_children(&pdev->dev, 1);
6869f4e8151SMark Brown 
6870d1bb41aSBen Dooks 	ret = sdhci_add_host(host);
6880d1bb41aSBen Dooks 	if (ret) {
6890d1bb41aSBen Dooks 		dev_err(dev, "sdhci_add_host() failed\n");
6909f4e8151SMark Brown 		pm_runtime_forbid(&pdev->dev);
6919f4e8151SMark Brown 		pm_runtime_get_noresume(&pdev->dev);
6929bda6da7SJulia Lawall 		goto err_req_regs;
6930d1bb41aSBen Dooks 	}
6940d1bb41aSBen Dooks 
69517866e14SMarek Szyprowski 	/* The following two methods of card detection might call
69617866e14SMarek Szyprowski 	   sdhci_s3c_notify_change() immediately, so they can be called
69717866e14SMarek Szyprowski 	   only after sdhci_add_host(). Setup errors are ignored. */
69817866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
69917866e14SMarek Szyprowski 		pdata->ext_cd_init(&sdhci_s3c_notify_change);
70017866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
70117866e14SMarek Szyprowski 	    gpio_is_valid(pdata->ext_cd_gpio))
70217866e14SMarek Szyprowski 		sdhci_s3c_setup_card_detect_gpio(sc);
70317866e14SMarek Szyprowski 
7042abeb5c5SChander Kashyap #ifdef CONFIG_PM_RUNTIME
7050aa55c23SSeungwon Jeon 	if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
7060f310a05SThomas Abraham 		clk_disable_unprepare(sc->clk_io);
7072abeb5c5SChander Kashyap #endif
7080d1bb41aSBen Dooks 	return 0;
7090d1bb41aSBen Dooks 
7100d1bb41aSBen Dooks  err_req_regs:
7112abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME
7120f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_bus[sc->cur_clk]);
7132abeb5c5SChander Kashyap #endif
7140d1bb41aSBen Dooks 
7150d1bb41aSBen Dooks  err_no_busclks:
7160f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_io);
7170d1bb41aSBen Dooks 
718b1b8fea9STomasz Figa  err_pdata_io_clk:
7190d1bb41aSBen Dooks 	sdhci_free_host(host);
7200d1bb41aSBen Dooks 
7210d1bb41aSBen Dooks 	return ret;
7220d1bb41aSBen Dooks }
7230d1bb41aSBen Dooks 
7246e0ee714SBill Pemberton static int sdhci_s3c_remove(struct platform_device *pdev)
7250d1bb41aSBen Dooks {
7269d51a6b2SMarek Szyprowski 	struct sdhci_host *host =  platform_get_drvdata(pdev);
7279d51a6b2SMarek Szyprowski 	struct sdhci_s3c *sc = sdhci_priv(host);
728cd1b00ebSThomas Abraham 	struct s3c_sdhci_platdata *pdata = sc->pdata;
7299d51a6b2SMarek Szyprowski 
73017866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
73117866e14SMarek Szyprowski 		pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
73217866e14SMarek Szyprowski 
73317866e14SMarek Szyprowski 	if (sc->ext_cd_irq)
73417866e14SMarek Szyprowski 		free_irq(sc->ext_cd_irq, sc);
73517866e14SMarek Szyprowski 
7362abeb5c5SChander Kashyap #ifdef CONFIG_PM_RUNTIME
7370aa55c23SSeungwon Jeon 	if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
7380f310a05SThomas Abraham 		clk_prepare_enable(sc->clk_io);
7392abeb5c5SChander Kashyap #endif
7409d51a6b2SMarek Szyprowski 	sdhci_remove_host(host, 1);
7419d51a6b2SMarek Szyprowski 
742387a8cbdSChander Kashyap 	pm_runtime_dont_use_autosuspend(&pdev->dev);
7439f4e8151SMark Brown 	pm_runtime_disable(&pdev->dev);
7449f4e8151SMark Brown 
7452abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME
7460f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_bus[sc->cur_clk]);
7472abeb5c5SChander Kashyap #endif
7480f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_io);
7499d51a6b2SMarek Szyprowski 
7509d51a6b2SMarek Szyprowski 	sdhci_free_host(host);
7519d51a6b2SMarek Szyprowski 
7520d1bb41aSBen Dooks 	return 0;
7530d1bb41aSBen Dooks }
7540d1bb41aSBen Dooks 
755d5e9c02cSMark Brown #ifdef CONFIG_PM_SLEEP
75629495aa0SManuel Lauss static int sdhci_s3c_suspend(struct device *dev)
7570d1bb41aSBen Dooks {
75829495aa0SManuel Lauss 	struct sdhci_host *host = dev_get_drvdata(dev);
7590d1bb41aSBen Dooks 
76029495aa0SManuel Lauss 	return sdhci_suspend_host(host);
7610d1bb41aSBen Dooks }
7620d1bb41aSBen Dooks 
76329495aa0SManuel Lauss static int sdhci_s3c_resume(struct device *dev)
7640d1bb41aSBen Dooks {
76529495aa0SManuel Lauss 	struct sdhci_host *host = dev_get_drvdata(dev);
7660d1bb41aSBen Dooks 
76765d13516SWonil Choi 	return sdhci_resume_host(host);
7680d1bb41aSBen Dooks }
769d5e9c02cSMark Brown #endif
7700d1bb41aSBen Dooks 
7719f4e8151SMark Brown #ifdef CONFIG_PM_RUNTIME
7729f4e8151SMark Brown static int sdhci_s3c_runtime_suspend(struct device *dev)
7739f4e8151SMark Brown {
7749f4e8151SMark Brown 	struct sdhci_host *host = dev_get_drvdata(dev);
7752abeb5c5SChander Kashyap 	struct sdhci_s3c *ourhost = to_s3c(host);
7762abeb5c5SChander Kashyap 	struct clk *busclk = ourhost->clk_io;
7772abeb5c5SChander Kashyap 	int ret;
7789f4e8151SMark Brown 
7792abeb5c5SChander Kashyap 	ret = sdhci_runtime_suspend_host(host);
7802abeb5c5SChander Kashyap 
7810f310a05SThomas Abraham 	clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
7820f310a05SThomas Abraham 	clk_disable_unprepare(busclk);
7832abeb5c5SChander Kashyap 	return ret;
7849f4e8151SMark Brown }
7859f4e8151SMark Brown 
7869f4e8151SMark Brown static int sdhci_s3c_runtime_resume(struct device *dev)
7879f4e8151SMark Brown {
7889f4e8151SMark Brown 	struct sdhci_host *host = dev_get_drvdata(dev);
7892abeb5c5SChander Kashyap 	struct sdhci_s3c *ourhost = to_s3c(host);
7902abeb5c5SChander Kashyap 	struct clk *busclk = ourhost->clk_io;
7912abeb5c5SChander Kashyap 	int ret;
7929f4e8151SMark Brown 
7930f310a05SThomas Abraham 	clk_prepare_enable(busclk);
7940f310a05SThomas Abraham 	clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
7952abeb5c5SChander Kashyap 	ret = sdhci_runtime_resume_host(host);
7962abeb5c5SChander Kashyap 	return ret;
7979f4e8151SMark Brown }
7989f4e8151SMark Brown #endif
7999f4e8151SMark Brown 
800d5e9c02cSMark Brown #ifdef CONFIG_PM
80129495aa0SManuel Lauss static const struct dev_pm_ops sdhci_s3c_pmops = {
802d5e9c02cSMark Brown 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
8039f4e8151SMark Brown 	SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
8049f4e8151SMark Brown 			   NULL)
80529495aa0SManuel Lauss };
80629495aa0SManuel Lauss 
80729495aa0SManuel Lauss #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
80829495aa0SManuel Lauss 
8090d1bb41aSBen Dooks #else
81029495aa0SManuel Lauss #define SDHCI_S3C_PMOPS NULL
8110d1bb41aSBen Dooks #endif
8120d1bb41aSBen Dooks 
8133119936aSThomas Abraham #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
8143119936aSThomas Abraham static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
8153119936aSThomas Abraham 	.sdhci_quirks = SDHCI_QUIRK_NONSTANDARD_CLOCK,
8163119936aSThomas Abraham };
8173119936aSThomas Abraham #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
8183119936aSThomas Abraham #else
8193119936aSThomas Abraham #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
8203119936aSThomas Abraham #endif
8213119936aSThomas Abraham 
8223119936aSThomas Abraham static struct platform_device_id sdhci_s3c_driver_ids[] = {
8233119936aSThomas Abraham 	{
8243119936aSThomas Abraham 		.name		= "s3c-sdhci",
8253119936aSThomas Abraham 		.driver_data	= (kernel_ulong_t)NULL,
8263119936aSThomas Abraham 	}, {
8273119936aSThomas Abraham 		.name		= "exynos4-sdhci",
8283119936aSThomas Abraham 		.driver_data	= EXYNOS4_SDHCI_DRV_DATA,
8293119936aSThomas Abraham 	},
8303119936aSThomas Abraham 	{ }
8313119936aSThomas Abraham };
8323119936aSThomas Abraham MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
8333119936aSThomas Abraham 
834cd1b00ebSThomas Abraham #ifdef CONFIG_OF
835cd1b00ebSThomas Abraham static const struct of_device_id sdhci_s3c_dt_match[] = {
836cd1b00ebSThomas Abraham 	{ .compatible = "samsung,s3c6410-sdhci", },
837cd1b00ebSThomas Abraham 	{ .compatible = "samsung,exynos4210-sdhci",
838cd1b00ebSThomas Abraham 		.data = (void *)EXYNOS4_SDHCI_DRV_DATA },
839cd1b00ebSThomas Abraham 	{},
840cd1b00ebSThomas Abraham };
841cd1b00ebSThomas Abraham MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
842cd1b00ebSThomas Abraham #endif
843cd1b00ebSThomas Abraham 
8440d1bb41aSBen Dooks static struct platform_driver sdhci_s3c_driver = {
8450d1bb41aSBen Dooks 	.probe		= sdhci_s3c_probe,
8460433c143SBill Pemberton 	.remove		= sdhci_s3c_remove,
8473119936aSThomas Abraham 	.id_table	= sdhci_s3c_driver_ids,
8480d1bb41aSBen Dooks 	.driver		= {
8490d1bb41aSBen Dooks 		.owner	= THIS_MODULE,
8500d1bb41aSBen Dooks 		.name	= "s3c-sdhci",
851cd1b00ebSThomas Abraham 		.of_match_table = of_match_ptr(sdhci_s3c_dt_match),
85229495aa0SManuel Lauss 		.pm	= SDHCI_S3C_PMOPS,
8530d1bb41aSBen Dooks 	},
8540d1bb41aSBen Dooks };
8550d1bb41aSBen Dooks 
856d1f81a64SAxel Lin module_platform_driver(sdhci_s3c_driver);
8570d1bb41aSBen Dooks 
8580d1bb41aSBen Dooks MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
8590d1bb41aSBen Dooks MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
8600d1bb41aSBen Dooks MODULE_LICENSE("GPL v2");
8610d1bb41aSBen Dooks MODULE_ALIAS("platform:s3c-sdhci");
862