xref: /openbmc/linux/drivers/mmc/host/sdhci-s3c.c (revision 03231f9b)
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 
360d1bb41aSBen Dooks /**
370d1bb41aSBen Dooks  * struct sdhci_s3c - S3C SDHCI instance
380d1bb41aSBen Dooks  * @host: The SDHCI host created
390d1bb41aSBen Dooks  * @pdev: The platform device we where created from.
400d1bb41aSBen Dooks  * @ioarea: The resource created when we claimed the IO area.
410d1bb41aSBen Dooks  * @pdata: The platform data for this controller.
420d1bb41aSBen Dooks  * @cur_clk: The index of the current bus clock.
430d1bb41aSBen Dooks  * @clk_io: The clock for the internal bus interface.
440d1bb41aSBen Dooks  * @clk_bus: The clocks that are available for the SD/MMC bus clock.
450d1bb41aSBen Dooks  */
460d1bb41aSBen Dooks struct sdhci_s3c {
470d1bb41aSBen Dooks 	struct sdhci_host	*host;
480d1bb41aSBen Dooks 	struct platform_device	*pdev;
490d1bb41aSBen Dooks 	struct resource		*ioarea;
500d1bb41aSBen Dooks 	struct s3c_sdhci_platdata *pdata;
513ac147faSTomasz Figa 	int			cur_clk;
5217866e14SMarek Szyprowski 	int			ext_cd_irq;
5317866e14SMarek Szyprowski 	int			ext_cd_gpio;
540d1bb41aSBen Dooks 
550d1bb41aSBen Dooks 	struct clk		*clk_io;
560d1bb41aSBen Dooks 	struct clk		*clk_bus[MAX_BUS_CLK];
576eb28bdcSTomasz Figa 	unsigned long		clk_rates[MAX_BUS_CLK];
580d1bb41aSBen Dooks };
590d1bb41aSBen Dooks 
603119936aSThomas Abraham /**
613119936aSThomas Abraham  * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
623119936aSThomas Abraham  * @sdhci_quirks: sdhci host specific quirks.
633119936aSThomas Abraham  *
643119936aSThomas Abraham  * Specifies platform specific configuration of sdhci controller.
653119936aSThomas Abraham  * Note: A structure for driver specific platform data is used for future
663119936aSThomas Abraham  * expansion of its usage.
673119936aSThomas Abraham  */
683119936aSThomas Abraham struct sdhci_s3c_drv_data {
693119936aSThomas Abraham 	unsigned int	sdhci_quirks;
703119936aSThomas Abraham };
713119936aSThomas Abraham 
720d1bb41aSBen Dooks static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
730d1bb41aSBen Dooks {
740d1bb41aSBen Dooks 	return sdhci_priv(host);
750d1bb41aSBen Dooks }
760d1bb41aSBen Dooks 
770d1bb41aSBen Dooks /**
780d1bb41aSBen Dooks  * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
790d1bb41aSBen Dooks  * @host: The SDHCI host instance.
800d1bb41aSBen Dooks  *
810d1bb41aSBen Dooks  * Callback to return the maximum clock rate acheivable by the controller.
820d1bb41aSBen Dooks */
830d1bb41aSBen Dooks static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
840d1bb41aSBen Dooks {
850d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
86222a13c5STomasz Figa 	unsigned long rate, max = 0;
87222a13c5STomasz Figa 	int src;
880d1bb41aSBen Dooks 
89222a13c5STomasz Figa 	for (src = 0; src < MAX_BUS_CLK; src++) {
90222a13c5STomasz Figa 		rate = ourhost->clk_rates[src];
910d1bb41aSBen Dooks 		if (rate > max)
920d1bb41aSBen Dooks 			max = rate;
930d1bb41aSBen Dooks 	}
940d1bb41aSBen Dooks 
950d1bb41aSBen Dooks 	return max;
960d1bb41aSBen Dooks }
970d1bb41aSBen Dooks 
980d1bb41aSBen Dooks /**
990d1bb41aSBen Dooks  * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
1000d1bb41aSBen Dooks  * @ourhost: Our SDHCI instance.
1010d1bb41aSBen Dooks  * @src: The source clock index.
1020d1bb41aSBen Dooks  * @wanted: The clock frequency wanted.
1030d1bb41aSBen Dooks  */
1040d1bb41aSBen Dooks static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
1050d1bb41aSBen Dooks 					     unsigned int src,
1060d1bb41aSBen Dooks 					     unsigned int wanted)
1070d1bb41aSBen Dooks {
1080d1bb41aSBen Dooks 	unsigned long rate;
1090d1bb41aSBen Dooks 	struct clk *clksrc = ourhost->clk_bus[src];
1108880a4a5STomasz Figa 	int shift;
1110d1bb41aSBen Dooks 
1128f4b78d9STomasz Figa 	if (IS_ERR(clksrc))
1130d1bb41aSBen Dooks 		return UINT_MAX;
1140d1bb41aSBen Dooks 
115253e0a7cSJeongbae Seo 	/*
1163119936aSThomas Abraham 	 * If controller uses a non-standard clock division, find the best clock
1173119936aSThomas Abraham 	 * speed possible with selected clock source and skip the division.
118253e0a7cSJeongbae Seo 	 */
1193119936aSThomas Abraham 	if (ourhost->host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
120253e0a7cSJeongbae Seo 		rate = clk_round_rate(clksrc, wanted);
121253e0a7cSJeongbae Seo 		return wanted - rate;
122253e0a7cSJeongbae Seo 	}
123253e0a7cSJeongbae Seo 
1246eb28bdcSTomasz Figa 	rate = ourhost->clk_rates[src];
1250d1bb41aSBen Dooks 
12622003000STomasz Figa 	for (shift = 0; shift <= 8; ++shift) {
1278880a4a5STomasz Figa 		if ((rate >> shift) <= wanted)
1280d1bb41aSBen Dooks 			break;
1290d1bb41aSBen Dooks 	}
1300d1bb41aSBen Dooks 
13122003000STomasz Figa 	if (shift > 8) {
13222003000STomasz Figa 		dev_dbg(&ourhost->pdev->dev,
13322003000STomasz Figa 			"clk %d: rate %ld, min rate %lu > wanted %u\n",
13422003000STomasz Figa 			src, rate, rate / 256, wanted);
13522003000STomasz Figa 		return UINT_MAX;
13622003000STomasz Figa 	}
13722003000STomasz Figa 
1380d1bb41aSBen Dooks 	dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
1398880a4a5STomasz Figa 		src, rate, wanted, rate >> shift);
1400d1bb41aSBen Dooks 
1418880a4a5STomasz Figa 	return wanted - (rate >> shift);
1420d1bb41aSBen Dooks }
1430d1bb41aSBen Dooks 
1440d1bb41aSBen Dooks /**
1450d1bb41aSBen Dooks  * sdhci_s3c_set_clock - callback on clock change
1460d1bb41aSBen Dooks  * @host: The SDHCI host being changed
1470d1bb41aSBen Dooks  * @clock: The clock rate being requested.
1480d1bb41aSBen Dooks  *
1490d1bb41aSBen Dooks  * When the card's clock is going to be changed, look at the new frequency
1500d1bb41aSBen Dooks  * and find the best clock source to go with it.
1510d1bb41aSBen Dooks */
1520d1bb41aSBen Dooks static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
1530d1bb41aSBen Dooks {
1540d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
1550d1bb41aSBen Dooks 	unsigned int best = UINT_MAX;
1560d1bb41aSBen Dooks 	unsigned int delta;
1570d1bb41aSBen Dooks 	int best_src = 0;
1580d1bb41aSBen Dooks 	int src;
1590d1bb41aSBen Dooks 	u32 ctrl;
1600d1bb41aSBen Dooks 
1610d1bb41aSBen Dooks 	/* don't bother if the clock is going off. */
1620d1bb41aSBen Dooks 	if (clock == 0)
1630d1bb41aSBen Dooks 		return;
1640d1bb41aSBen Dooks 
1650d1bb41aSBen Dooks 	for (src = 0; src < MAX_BUS_CLK; src++) {
1660d1bb41aSBen Dooks 		delta = sdhci_s3c_consider_clock(ourhost, src, clock);
1670d1bb41aSBen Dooks 		if (delta < best) {
1680d1bb41aSBen Dooks 			best = delta;
1690d1bb41aSBen Dooks 			best_src = src;
1700d1bb41aSBen Dooks 		}
1710d1bb41aSBen Dooks 	}
1720d1bb41aSBen Dooks 
1730d1bb41aSBen Dooks 	dev_dbg(&ourhost->pdev->dev,
1740d1bb41aSBen Dooks 		"selected source %d, clock %d, delta %d\n",
1750d1bb41aSBen Dooks 		 best_src, clock, best);
1760d1bb41aSBen Dooks 
1770d1bb41aSBen Dooks 	/* select the new clock source */
1780d1bb41aSBen Dooks 	if (ourhost->cur_clk != best_src) {
1790d1bb41aSBen Dooks 		struct clk *clk = ourhost->clk_bus[best_src];
1800d1bb41aSBen Dooks 
1810f310a05SThomas Abraham 		clk_prepare_enable(clk);
1823ac147faSTomasz Figa 		if (ourhost->cur_clk >= 0)
1833ac147faSTomasz Figa 			clk_disable_unprepare(
1843ac147faSTomasz Figa 					ourhost->clk_bus[ourhost->cur_clk]);
1850d1bb41aSBen Dooks 
1860d1bb41aSBen Dooks 		ourhost->cur_clk = best_src;
1876eb28bdcSTomasz Figa 		host->max_clk = ourhost->clk_rates[best_src];
1883ac147faSTomasz Figa 	}
1893ac147faSTomasz Figa 
1903ac147faSTomasz Figa 	/* turn clock off to card before changing clock source */
1913ac147faSTomasz Figa 	writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
1920d1bb41aSBen Dooks 
1930d1bb41aSBen Dooks 	ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
1940d1bb41aSBen Dooks 	ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
1950d1bb41aSBen Dooks 	ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
1960d1bb41aSBen Dooks 	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
1970d1bb41aSBen Dooks 
1986fe47179SThomas Abraham 	/* reprogram default hardware configuration */
1996fe47179SThomas Abraham 	writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
2006fe47179SThomas Abraham 		host->ioaddr + S3C64XX_SDHCI_CONTROL4);
2010d1bb41aSBen Dooks 
2026fe47179SThomas Abraham 	ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
2036fe47179SThomas Abraham 	ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
2046fe47179SThomas Abraham 		  S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
2056fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_ENFBCLKRX |
2066fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_DFCNT_NONE |
2076fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
2086fe47179SThomas Abraham 	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
2090d1bb41aSBen Dooks 
2106fe47179SThomas Abraham 	/* reconfigure the controller for new clock rate */
2116fe47179SThomas Abraham 	ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
2126fe47179SThomas Abraham 	if (clock < 25 * 1000000)
2136fe47179SThomas Abraham 		ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
2146fe47179SThomas Abraham 	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
2150d1bb41aSBen Dooks }
2160d1bb41aSBen Dooks 
217ce5f036bSMarek Szyprowski /**
218ce5f036bSMarek Szyprowski  * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
219ce5f036bSMarek Szyprowski  * @host: The SDHCI host being queried
220ce5f036bSMarek Szyprowski  *
221ce5f036bSMarek Szyprowski  * To init mmc host properly a minimal clock value is needed. For high system
222ce5f036bSMarek Szyprowski  * bus clock's values the standard formula gives values out of allowed range.
223ce5f036bSMarek Szyprowski  * The clock still can be set to lower values, if clock source other then
224ce5f036bSMarek Szyprowski  * system bus is selected.
225ce5f036bSMarek Szyprowski */
226ce5f036bSMarek Szyprowski static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
227ce5f036bSMarek Szyprowski {
228ce5f036bSMarek Szyprowski 	struct sdhci_s3c *ourhost = to_s3c(host);
229222a13c5STomasz Figa 	unsigned long rate, min = ULONG_MAX;
230ce5f036bSMarek Szyprowski 	int src;
231ce5f036bSMarek Szyprowski 
232ce5f036bSMarek Szyprowski 	for (src = 0; src < MAX_BUS_CLK; src++) {
233222a13c5STomasz Figa 		rate = ourhost->clk_rates[src] / 256;
234222a13c5STomasz Figa 		if (!rate)
235ce5f036bSMarek Szyprowski 			continue;
236222a13c5STomasz Figa 		if (rate < min)
237222a13c5STomasz Figa 			min = rate;
238ce5f036bSMarek Szyprowski 	}
239222a13c5STomasz Figa 
240ce5f036bSMarek Szyprowski 	return min;
241ce5f036bSMarek Szyprowski }
242ce5f036bSMarek Szyprowski 
243253e0a7cSJeongbae Seo /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
244253e0a7cSJeongbae Seo static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
245253e0a7cSJeongbae Seo {
246253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
247222a13c5STomasz Figa 	unsigned long rate, max = 0;
248222a13c5STomasz Figa 	int src;
249253e0a7cSJeongbae Seo 
250222a13c5STomasz Figa 	for (src = 0; src < MAX_BUS_CLK; src++) {
251222a13c5STomasz Figa 		struct clk *clk;
252222a13c5STomasz Figa 
253222a13c5STomasz Figa 		clk = ourhost->clk_bus[src];
254222a13c5STomasz Figa 		if (IS_ERR(clk))
255222a13c5STomasz Figa 			continue;
256222a13c5STomasz Figa 
257222a13c5STomasz Figa 		rate = clk_round_rate(clk, ULONG_MAX);
258222a13c5STomasz Figa 		if (rate > max)
259222a13c5STomasz Figa 			max = rate;
260222a13c5STomasz Figa 	}
261222a13c5STomasz Figa 
262222a13c5STomasz Figa 	return max;
263253e0a7cSJeongbae Seo }
264253e0a7cSJeongbae Seo 
265253e0a7cSJeongbae Seo /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
266253e0a7cSJeongbae Seo static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
267253e0a7cSJeongbae Seo {
268253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
269222a13c5STomasz Figa 	unsigned long rate, min = ULONG_MAX;
270222a13c5STomasz Figa 	int src;
271253e0a7cSJeongbae Seo 
272222a13c5STomasz Figa 	for (src = 0; src < MAX_BUS_CLK; src++) {
273222a13c5STomasz Figa 		struct clk *clk;
274222a13c5STomasz Figa 
275222a13c5STomasz Figa 		clk = ourhost->clk_bus[src];
276222a13c5STomasz Figa 		if (IS_ERR(clk))
277222a13c5STomasz Figa 			continue;
278222a13c5STomasz Figa 
279222a13c5STomasz Figa 		rate = clk_round_rate(clk, 0);
280222a13c5STomasz Figa 		if (rate < min)
281222a13c5STomasz Figa 			min = rate;
282222a13c5STomasz Figa 	}
283222a13c5STomasz Figa 
284222a13c5STomasz Figa 	return min;
285253e0a7cSJeongbae Seo }
286253e0a7cSJeongbae Seo 
287253e0a7cSJeongbae Seo /* sdhci_cmu_set_clock - callback on clock change.*/
288253e0a7cSJeongbae Seo static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
289253e0a7cSJeongbae Seo {
290253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
2912ad0b249SJingoo Han 	struct device *dev = &ourhost->pdev->dev;
2923119936aSThomas Abraham 	unsigned long timeout;
2933119936aSThomas Abraham 	u16 clk = 0;
294253e0a7cSJeongbae Seo 
2957ef2a5e2SJaehoon Chung 	/* If the clock is going off, set to 0 at clock control register */
2967ef2a5e2SJaehoon Chung 	if (clock == 0) {
2977ef2a5e2SJaehoon Chung 		sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2987ef2a5e2SJaehoon Chung 		host->clock = clock;
299253e0a7cSJeongbae Seo 		return;
3007ef2a5e2SJaehoon Chung 	}
301253e0a7cSJeongbae Seo 
302253e0a7cSJeongbae Seo 	sdhci_s3c_set_clock(host, clock);
303253e0a7cSJeongbae Seo 
304253e0a7cSJeongbae Seo 	clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
305253e0a7cSJeongbae Seo 
306253e0a7cSJeongbae Seo 	host->clock = clock;
3073119936aSThomas Abraham 
3083119936aSThomas Abraham 	clk = SDHCI_CLOCK_INT_EN;
3093119936aSThomas Abraham 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
3103119936aSThomas Abraham 
3113119936aSThomas Abraham 	/* Wait max 20 ms */
3123119936aSThomas Abraham 	timeout = 20;
3133119936aSThomas Abraham 	while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
3143119936aSThomas Abraham 		& SDHCI_CLOCK_INT_STABLE)) {
3153119936aSThomas Abraham 		if (timeout == 0) {
3162ad0b249SJingoo Han 			dev_err(dev, "%s: Internal clock never stabilised.\n",
3172ad0b249SJingoo Han 				mmc_hostname(host->mmc));
3183119936aSThomas Abraham 			return;
3193119936aSThomas Abraham 		}
3203119936aSThomas Abraham 		timeout--;
3213119936aSThomas Abraham 		mdelay(1);
3223119936aSThomas Abraham 	}
3233119936aSThomas Abraham 
3243119936aSThomas Abraham 	clk |= SDHCI_CLOCK_CARD_EN;
3253119936aSThomas Abraham 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
326253e0a7cSJeongbae Seo }
327253e0a7cSJeongbae Seo 
328548f07d2SJaehoon Chung /**
3292317f56cSRussell King  * sdhci_s3c_set_bus_width - support 8bit buswidth
330548f07d2SJaehoon Chung  * @host: The SDHCI host being queried
331548f07d2SJaehoon Chung  * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
332548f07d2SJaehoon Chung  *
333548f07d2SJaehoon Chung  * We have 8-bit width support but is not a v3 controller.
3347bc088d3SSascha Hauer  * So we add platform_bus_width() and support 8bit width.
335548f07d2SJaehoon Chung  */
3362317f56cSRussell King static void sdhci_s3c_set_bus_width(struct sdhci_host *host, int width)
337548f07d2SJaehoon Chung {
338548f07d2SJaehoon Chung 	u8 ctrl;
339548f07d2SJaehoon Chung 
340548f07d2SJaehoon Chung 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
341548f07d2SJaehoon Chung 
342548f07d2SJaehoon Chung 	switch (width) {
343548f07d2SJaehoon Chung 	case MMC_BUS_WIDTH_8:
344548f07d2SJaehoon Chung 		ctrl |= SDHCI_CTRL_8BITBUS;
345548f07d2SJaehoon Chung 		ctrl &= ~SDHCI_CTRL_4BITBUS;
346548f07d2SJaehoon Chung 		break;
347548f07d2SJaehoon Chung 	case MMC_BUS_WIDTH_4:
348548f07d2SJaehoon Chung 		ctrl |= SDHCI_CTRL_4BITBUS;
349548f07d2SJaehoon Chung 		ctrl &= ~SDHCI_CTRL_8BITBUS;
350548f07d2SJaehoon Chung 		break;
351548f07d2SJaehoon Chung 	default:
35249bb1e61SGirish K S 		ctrl &= ~SDHCI_CTRL_4BITBUS;
35349bb1e61SGirish K S 		ctrl &= ~SDHCI_CTRL_8BITBUS;
354548f07d2SJaehoon Chung 		break;
355548f07d2SJaehoon Chung 	}
356548f07d2SJaehoon Chung 
357548f07d2SJaehoon Chung 	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
358548f07d2SJaehoon Chung }
359548f07d2SJaehoon Chung 
3600d1bb41aSBen Dooks static struct sdhci_ops sdhci_s3c_ops = {
3610d1bb41aSBen Dooks 	.get_max_clock		= sdhci_s3c_get_max_clk,
3620d1bb41aSBen Dooks 	.set_clock		= sdhci_s3c_set_clock,
363ce5f036bSMarek Szyprowski 	.get_min_clock		= sdhci_s3c_get_min_clock,
3642317f56cSRussell King 	.set_bus_width		= sdhci_s3c_set_bus_width,
36503231f9bSRussell King 	.reset			= sdhci_reset,
3660d1bb41aSBen Dooks };
3670d1bb41aSBen Dooks 
36817866e14SMarek Szyprowski static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
36917866e14SMarek Szyprowski {
37017866e14SMarek Szyprowski 	struct sdhci_host *host = platform_get_drvdata(dev);
3714577f77bSSachin Kamat #ifdef CONFIG_PM_RUNTIME
372fe007c02SHeiko Stübner 	struct sdhci_s3c *sc = sdhci_priv(host);
3734577f77bSSachin Kamat #endif
37406fe577fSMarek Szyprowski 	unsigned long flags;
37506fe577fSMarek Szyprowski 
37617866e14SMarek Szyprowski 	if (host) {
37706fe577fSMarek Szyprowski 		spin_lock_irqsave(&host->lock, flags);
37817866e14SMarek Szyprowski 		if (state) {
37917866e14SMarek Szyprowski 			dev_dbg(&dev->dev, "card inserted.\n");
380fe007c02SHeiko Stübner #ifdef CONFIG_PM_RUNTIME
381fe007c02SHeiko Stübner 			clk_prepare_enable(sc->clk_io);
382fe007c02SHeiko Stübner #endif
38317866e14SMarek Szyprowski 			host->flags &= ~SDHCI_DEVICE_DEAD;
38417866e14SMarek Szyprowski 			host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
38517866e14SMarek Szyprowski 		} else {
38617866e14SMarek Szyprowski 			dev_dbg(&dev->dev, "card removed.\n");
38717866e14SMarek Szyprowski 			host->flags |= SDHCI_DEVICE_DEAD;
38817866e14SMarek Szyprowski 			host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
389fe007c02SHeiko Stübner #ifdef CONFIG_PM_RUNTIME
390fe007c02SHeiko Stübner 			clk_disable_unprepare(sc->clk_io);
391fe007c02SHeiko Stübner #endif
39217866e14SMarek Szyprowski 		}
393f522886eSKyungmin Park 		tasklet_schedule(&host->card_tasklet);
39406fe577fSMarek Szyprowski 		spin_unlock_irqrestore(&host->lock, flags);
39517866e14SMarek Szyprowski 	}
39617866e14SMarek Szyprowski }
39717866e14SMarek Szyprowski 
39817866e14SMarek Szyprowski static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
39917866e14SMarek Szyprowski {
40017866e14SMarek Szyprowski 	struct sdhci_s3c *sc = dev_id;
40117866e14SMarek Szyprowski 	int status = gpio_get_value(sc->ext_cd_gpio);
40217866e14SMarek Szyprowski 	if (sc->pdata->ext_cd_gpio_invert)
40317866e14SMarek Szyprowski 		status = !status;
40417866e14SMarek Szyprowski 	sdhci_s3c_notify_change(sc->pdev, status);
40517866e14SMarek Szyprowski 	return IRQ_HANDLED;
40617866e14SMarek Szyprowski }
40717866e14SMarek Szyprowski 
40817866e14SMarek Szyprowski static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
40917866e14SMarek Szyprowski {
41017866e14SMarek Szyprowski 	struct s3c_sdhci_platdata *pdata = sc->pdata;
41117866e14SMarek Szyprowski 	struct device *dev = &sc->pdev->dev;
41217866e14SMarek Szyprowski 
413b1b8fea9STomasz Figa 	if (devm_gpio_request(dev, pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
41417866e14SMarek Szyprowski 		sc->ext_cd_gpio = pdata->ext_cd_gpio;
41517866e14SMarek Szyprowski 		sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
41617866e14SMarek Szyprowski 		if (sc->ext_cd_irq &&
41717866e14SMarek Szyprowski 		    request_threaded_irq(sc->ext_cd_irq, NULL,
41817866e14SMarek Szyprowski 					 sdhci_s3c_gpio_card_detect_thread,
4192ad0b249SJingoo Han 					 IRQF_TRIGGER_RISING |
4202ad0b249SJingoo Han 					 IRQF_TRIGGER_FALLING |
4212ad0b249SJingoo Han 					 IRQF_ONESHOT,
42217866e14SMarek Szyprowski 					 dev_name(dev), sc) == 0) {
42317866e14SMarek Szyprowski 			int status = gpio_get_value(sc->ext_cd_gpio);
42417866e14SMarek Szyprowski 			if (pdata->ext_cd_gpio_invert)
42517866e14SMarek Szyprowski 				status = !status;
42617866e14SMarek Szyprowski 			sdhci_s3c_notify_change(sc->pdev, status);
42717866e14SMarek Szyprowski 		} else {
42817866e14SMarek Szyprowski 			dev_warn(dev, "cannot request irq for card detect\n");
42917866e14SMarek Szyprowski 			sc->ext_cd_irq = 0;
43017866e14SMarek Szyprowski 		}
43117866e14SMarek Szyprowski 	} else {
43217866e14SMarek Szyprowski 		dev_err(dev, "cannot request gpio for card detect\n");
43317866e14SMarek Szyprowski 	}
43417866e14SMarek Szyprowski }
43517866e14SMarek Szyprowski 
436cd1b00ebSThomas Abraham #ifdef CONFIG_OF
437c3be1efdSBill Pemberton static int sdhci_s3c_parse_dt(struct device *dev,
438cd1b00ebSThomas Abraham 		struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
439cd1b00ebSThomas Abraham {
440cd1b00ebSThomas Abraham 	struct device_node *node = dev->of_node;
441cd1b00ebSThomas Abraham 	struct sdhci_s3c *ourhost = to_s3c(host);
442cd1b00ebSThomas Abraham 	u32 max_width;
443e19499aeSThomas Abraham 	int gpio;
444cd1b00ebSThomas Abraham 
445cd1b00ebSThomas Abraham 	/* if the bus-width property is not specified, assume width as 1 */
446cd1b00ebSThomas Abraham 	if (of_property_read_u32(node, "bus-width", &max_width))
447cd1b00ebSThomas Abraham 		max_width = 1;
448cd1b00ebSThomas Abraham 	pdata->max_width = max_width;
449cd1b00ebSThomas Abraham 
450cd1b00ebSThomas Abraham 	/* get the card detection method */
451ab5023efSTushar Behera 	if (of_get_property(node, "broken-cd", NULL)) {
452cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_NONE;
453e19499aeSThomas Abraham 		return 0;
454cd1b00ebSThomas Abraham 	}
455cd1b00ebSThomas Abraham 
456ab5023efSTushar Behera 	if (of_get_property(node, "non-removable", NULL)) {
457cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
458e19499aeSThomas Abraham 		return 0;
459cd1b00ebSThomas Abraham 	}
460cd1b00ebSThomas Abraham 
461cd1b00ebSThomas Abraham 	gpio = of_get_named_gpio(node, "cd-gpios", 0);
462cd1b00ebSThomas Abraham 	if (gpio_is_valid(gpio)) {
463cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_GPIO;
464e19499aeSThomas Abraham 		pdata->ext_cd_gpio = gpio;
465e19499aeSThomas Abraham 		ourhost->ext_cd_gpio = -1;
466e19499aeSThomas Abraham 		if (of_get_property(node, "cd-inverted", NULL))
467e19499aeSThomas Abraham 			pdata->ext_cd_gpio_invert = 1;
468e19499aeSThomas Abraham 		return 0;
469cd1b00ebSThomas Abraham 	} else if (gpio != -ENOENT) {
470cd1b00ebSThomas Abraham 		dev_err(dev, "invalid card detect gpio specified\n");
471cd1b00ebSThomas Abraham 		return -EINVAL;
472cd1b00ebSThomas Abraham 	}
473cd1b00ebSThomas Abraham 
474b96efccbSTomasz Figa 	/* assuming internal card detect that will be configured by pinctrl */
475b96efccbSTomasz Figa 	pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
476cd1b00ebSThomas Abraham 	return 0;
477cd1b00ebSThomas Abraham }
478cd1b00ebSThomas Abraham #else
479c3be1efdSBill Pemberton static int sdhci_s3c_parse_dt(struct device *dev,
480cd1b00ebSThomas Abraham 		struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
481cd1b00ebSThomas Abraham {
482cd1b00ebSThomas Abraham 	return -EINVAL;
483cd1b00ebSThomas Abraham }
484cd1b00ebSThomas Abraham #endif
485cd1b00ebSThomas Abraham 
486cd1b00ebSThomas Abraham static const struct of_device_id sdhci_s3c_dt_match[];
487cd1b00ebSThomas Abraham 
4883119936aSThomas Abraham static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
4893119936aSThomas Abraham 			struct platform_device *pdev)
4903119936aSThomas Abraham {
491cd1b00ebSThomas Abraham #ifdef CONFIG_OF
492cd1b00ebSThomas Abraham 	if (pdev->dev.of_node) {
493cd1b00ebSThomas Abraham 		const struct of_device_id *match;
494cd1b00ebSThomas Abraham 		match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
495cd1b00ebSThomas Abraham 		return (struct sdhci_s3c_drv_data *)match->data;
496cd1b00ebSThomas Abraham 	}
497cd1b00ebSThomas Abraham #endif
4983119936aSThomas Abraham 	return (struct sdhci_s3c_drv_data *)
4993119936aSThomas Abraham 			platform_get_device_id(pdev)->driver_data;
5003119936aSThomas Abraham }
5013119936aSThomas Abraham 
502c3be1efdSBill Pemberton static int sdhci_s3c_probe(struct platform_device *pdev)
5030d1bb41aSBen Dooks {
5041d4dc338SThomas Abraham 	struct s3c_sdhci_platdata *pdata;
5053119936aSThomas Abraham 	struct sdhci_s3c_drv_data *drv_data;
5060d1bb41aSBen Dooks 	struct device *dev = &pdev->dev;
5070d1bb41aSBen Dooks 	struct sdhci_host *host;
5080d1bb41aSBen Dooks 	struct sdhci_s3c *sc;
5090d1bb41aSBen Dooks 	struct resource *res;
5100d1bb41aSBen Dooks 	int ret, irq, ptr, clks;
5110d1bb41aSBen Dooks 
512cd1b00ebSThomas Abraham 	if (!pdev->dev.platform_data && !pdev->dev.of_node) {
5130d1bb41aSBen Dooks 		dev_err(dev, "no device data specified\n");
5140d1bb41aSBen Dooks 		return -ENOENT;
5150d1bb41aSBen Dooks 	}
5160d1bb41aSBen Dooks 
5170d1bb41aSBen Dooks 	irq = platform_get_irq(pdev, 0);
5180d1bb41aSBen Dooks 	if (irq < 0) {
5190d1bb41aSBen Dooks 		dev_err(dev, "no irq specified\n");
5200d1bb41aSBen Dooks 		return irq;
5210d1bb41aSBen Dooks 	}
5220d1bb41aSBen Dooks 
5230d1bb41aSBen Dooks 	host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
5240d1bb41aSBen Dooks 	if (IS_ERR(host)) {
5250d1bb41aSBen Dooks 		dev_err(dev, "sdhci_alloc_host() failed\n");
5260d1bb41aSBen Dooks 		return PTR_ERR(host);
5270d1bb41aSBen Dooks 	}
528cd1b00ebSThomas Abraham 	sc = sdhci_priv(host);
5290d1bb41aSBen Dooks 
5301d4dc338SThomas Abraham 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
5311d4dc338SThomas Abraham 	if (!pdata) {
5321d4dc338SThomas Abraham 		ret = -ENOMEM;
533b1b8fea9STomasz Figa 		goto err_pdata_io_clk;
5341d4dc338SThomas Abraham 	}
535cd1b00ebSThomas Abraham 
536cd1b00ebSThomas Abraham 	if (pdev->dev.of_node) {
537cd1b00ebSThomas Abraham 		ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
538cd1b00ebSThomas Abraham 		if (ret)
539b1b8fea9STomasz Figa 			goto err_pdata_io_clk;
540cd1b00ebSThomas Abraham 	} else {
5411d4dc338SThomas Abraham 		memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
542cd1b00ebSThomas Abraham 		sc->ext_cd_gpio = -1; /* invalid gpio number */
543cd1b00ebSThomas Abraham 	}
5441d4dc338SThomas Abraham 
5453119936aSThomas Abraham 	drv_data = sdhci_s3c_get_driver_data(pdev);
5460d1bb41aSBen Dooks 
5470d1bb41aSBen Dooks 	sc->host = host;
5480d1bb41aSBen Dooks 	sc->pdev = pdev;
5490d1bb41aSBen Dooks 	sc->pdata = pdata;
5503ac147faSTomasz Figa 	sc->cur_clk = -1;
5510d1bb41aSBen Dooks 
5520d1bb41aSBen Dooks 	platform_set_drvdata(pdev, host);
5530d1bb41aSBen Dooks 
5543aaf7ba7SJingoo Han 	sc->clk_io = devm_clk_get(dev, "hsmmc");
5550d1bb41aSBen Dooks 	if (IS_ERR(sc->clk_io)) {
5560d1bb41aSBen Dooks 		dev_err(dev, "failed to get io clock\n");
5570d1bb41aSBen Dooks 		ret = PTR_ERR(sc->clk_io);
558b1b8fea9STomasz Figa 		goto err_pdata_io_clk;
5590d1bb41aSBen Dooks 	}
5600d1bb41aSBen Dooks 
5610d1bb41aSBen Dooks 	/* enable the local io clock and keep it running for the moment. */
5620f310a05SThomas Abraham 	clk_prepare_enable(sc->clk_io);
5630d1bb41aSBen Dooks 
5640d1bb41aSBen Dooks 	for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
5654346b6d9SRajeshwari Shinde 		char name[14];
5660d1bb41aSBen Dooks 
5674346b6d9SRajeshwari Shinde 		snprintf(name, 14, "mmc_busclk.%d", ptr);
5688f4b78d9STomasz Figa 		sc->clk_bus[ptr] = devm_clk_get(dev, name);
5698f4b78d9STomasz Figa 		if (IS_ERR(sc->clk_bus[ptr]))
5700d1bb41aSBen Dooks 			continue;
5710d1bb41aSBen Dooks 
5720d1bb41aSBen Dooks 		clks++;
5736eb28bdcSTomasz Figa 		sc->clk_rates[ptr] = clk_get_rate(sc->clk_bus[ptr]);
5746eb28bdcSTomasz Figa 
5750d1bb41aSBen Dooks 		dev_info(dev, "clock source %d: %s (%ld Hz)\n",
5766eb28bdcSTomasz Figa 				ptr, name, sc->clk_rates[ptr]);
5770d1bb41aSBen Dooks 	}
5780d1bb41aSBen Dooks 
5790d1bb41aSBen Dooks 	if (clks == 0) {
5800d1bb41aSBen Dooks 		dev_err(dev, "failed to find any bus clocks\n");
5810d1bb41aSBen Dooks 		ret = -ENOENT;
5820d1bb41aSBen Dooks 		goto err_no_busclks;
5830d1bb41aSBen Dooks 	}
5840d1bb41aSBen Dooks 
5859bda6da7SJulia Lawall 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
586a3e2cd7fSThierry Reding 	host->ioaddr = devm_ioremap_resource(&pdev->dev, res);
587a3e2cd7fSThierry Reding 	if (IS_ERR(host->ioaddr)) {
588a3e2cd7fSThierry Reding 		ret = PTR_ERR(host->ioaddr);
5890d1bb41aSBen Dooks 		goto err_req_regs;
5900d1bb41aSBen Dooks 	}
5910d1bb41aSBen Dooks 
5920d1bb41aSBen Dooks 	/* Ensure we have minimal gpio selected CMD/CLK/Detect */
5930d1bb41aSBen Dooks 	if (pdata->cfg_gpio)
5940d1bb41aSBen Dooks 		pdata->cfg_gpio(pdev, pdata->max_width);
5950d1bb41aSBen Dooks 
5960d1bb41aSBen Dooks 	host->hw_name = "samsung-hsmmc";
5970d1bb41aSBen Dooks 	host->ops = &sdhci_s3c_ops;
5980d1bb41aSBen Dooks 	host->quirks = 0;
599285e244fSJaehoon Chung 	host->quirks2 = 0;
6000d1bb41aSBen Dooks 	host->irq = irq;
6010d1bb41aSBen Dooks 
6020d1bb41aSBen Dooks 	/* Setup quirks for the controller */
603b2e75effSThomas Abraham 	host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
604a1d56460SMarek Szyprowski 	host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
6053119936aSThomas Abraham 	if (drv_data)
6063119936aSThomas Abraham 		host->quirks |= drv_data->sdhci_quirks;
6070d1bb41aSBen Dooks 
6080d1bb41aSBen Dooks #ifndef CONFIG_MMC_SDHCI_S3C_DMA
6090d1bb41aSBen Dooks 
6100d1bb41aSBen Dooks 	/* we currently see overruns on errors, so disable the SDMA
6110d1bb41aSBen Dooks 	 * support as well. */
6120d1bb41aSBen Dooks 	host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
6130d1bb41aSBen Dooks 
6140d1bb41aSBen Dooks #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
6150d1bb41aSBen Dooks 
6160d1bb41aSBen Dooks 	/* It seems we do not get an DATA transfer complete on non-busy
6170d1bb41aSBen Dooks 	 * transfers, not sure if this is a problem with this specific
6180d1bb41aSBen Dooks 	 * SDHCI block, or a missing configuration that needs to be set. */
6190d1bb41aSBen Dooks 	host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
6200d1bb41aSBen Dooks 
621732f0e31SKyungmin Park 	/* This host supports the Auto CMD12 */
622732f0e31SKyungmin Park 	host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
623732f0e31SKyungmin Park 
6247199e2b6SJaehoon Chung 	/* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
6257199e2b6SJaehoon Chung 	host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
6267199e2b6SJaehoon Chung 
62717866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
62817866e14SMarek Szyprowski 	    pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
62917866e14SMarek Szyprowski 		host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
63017866e14SMarek Szyprowski 
63117866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
63217866e14SMarek Szyprowski 		host->mmc->caps = MMC_CAP_NONREMOVABLE;
63317866e14SMarek Szyprowski 
6340d22c770SThomas Abraham 	switch (pdata->max_width) {
6350d22c770SThomas Abraham 	case 8:
6360d22c770SThomas Abraham 		host->mmc->caps |= MMC_CAP_8_BIT_DATA;
6370d22c770SThomas Abraham 	case 4:
6380d22c770SThomas Abraham 		host->mmc->caps |= MMC_CAP_4_BIT_DATA;
6390d22c770SThomas Abraham 		break;
6400d22c770SThomas Abraham 	}
6410d22c770SThomas Abraham 
642fa1773ccSSangwook Lee 	if (pdata->pm_caps)
643fa1773ccSSangwook Lee 		host->mmc->pm_caps |= pdata->pm_caps;
644fa1773ccSSangwook Lee 
6450d1bb41aSBen Dooks 	host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
6460d1bb41aSBen Dooks 			 SDHCI_QUIRK_32BIT_DMA_SIZE);
6470d1bb41aSBen Dooks 
6483fe42e07SHyuk Lee 	/* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
6493fe42e07SHyuk Lee 	host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
6503fe42e07SHyuk Lee 
651253e0a7cSJeongbae Seo 	/*
652253e0a7cSJeongbae Seo 	 * If controller does not have internal clock divider,
653253e0a7cSJeongbae Seo 	 * we can use overriding functions instead of default.
654253e0a7cSJeongbae Seo 	 */
6553119936aSThomas Abraham 	if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
656253e0a7cSJeongbae Seo 		sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
657253e0a7cSJeongbae Seo 		sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
658253e0a7cSJeongbae Seo 		sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
659253e0a7cSJeongbae Seo 	}
660253e0a7cSJeongbae Seo 
661b3824f2cSJeongbae Seo 	/* It supports additional host capabilities if needed */
662b3824f2cSJeongbae Seo 	if (pdata->host_caps)
663b3824f2cSJeongbae Seo 		host->mmc->caps |= pdata->host_caps;
664b3824f2cSJeongbae Seo 
665c1c4b66dSJaehoon Chung 	if (pdata->host_caps2)
666c1c4b66dSJaehoon Chung 		host->mmc->caps2 |= pdata->host_caps2;
667c1c4b66dSJaehoon Chung 
6689f4e8151SMark Brown 	pm_runtime_enable(&pdev->dev);
6699f4e8151SMark Brown 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
6709f4e8151SMark Brown 	pm_runtime_use_autosuspend(&pdev->dev);
6719f4e8151SMark Brown 	pm_suspend_ignore_children(&pdev->dev, 1);
6729f4e8151SMark Brown 
6730d1bb41aSBen Dooks 	ret = sdhci_add_host(host);
6740d1bb41aSBen Dooks 	if (ret) {
6750d1bb41aSBen Dooks 		dev_err(dev, "sdhci_add_host() failed\n");
6769f4e8151SMark Brown 		pm_runtime_forbid(&pdev->dev);
6779f4e8151SMark Brown 		pm_runtime_get_noresume(&pdev->dev);
6789bda6da7SJulia Lawall 		goto err_req_regs;
6790d1bb41aSBen Dooks 	}
6800d1bb41aSBen Dooks 
68117866e14SMarek Szyprowski 	/* The following two methods of card detection might call
68217866e14SMarek Szyprowski 	   sdhci_s3c_notify_change() immediately, so they can be called
68317866e14SMarek Szyprowski 	   only after sdhci_add_host(). Setup errors are ignored. */
68417866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
68517866e14SMarek Szyprowski 		pdata->ext_cd_init(&sdhci_s3c_notify_change);
68617866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
68717866e14SMarek Szyprowski 	    gpio_is_valid(pdata->ext_cd_gpio))
68817866e14SMarek Szyprowski 		sdhci_s3c_setup_card_detect_gpio(sc);
68917866e14SMarek Szyprowski 
6902abeb5c5SChander Kashyap #ifdef CONFIG_PM_RUNTIME
6910aa55c23SSeungwon Jeon 	if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
6920f310a05SThomas Abraham 		clk_disable_unprepare(sc->clk_io);
6932abeb5c5SChander Kashyap #endif
6940d1bb41aSBen Dooks 	return 0;
6950d1bb41aSBen Dooks 
6960d1bb41aSBen Dooks  err_req_regs:
6970d1bb41aSBen Dooks  err_no_busclks:
6980f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_io);
6990d1bb41aSBen Dooks 
700b1b8fea9STomasz Figa  err_pdata_io_clk:
7010d1bb41aSBen Dooks 	sdhci_free_host(host);
7020d1bb41aSBen Dooks 
7030d1bb41aSBen Dooks 	return ret;
7040d1bb41aSBen Dooks }
7050d1bb41aSBen Dooks 
7066e0ee714SBill Pemberton static int sdhci_s3c_remove(struct platform_device *pdev)
7070d1bb41aSBen Dooks {
7089d51a6b2SMarek Szyprowski 	struct sdhci_host *host =  platform_get_drvdata(pdev);
7099d51a6b2SMarek Szyprowski 	struct sdhci_s3c *sc = sdhci_priv(host);
710cd1b00ebSThomas Abraham 	struct s3c_sdhci_platdata *pdata = sc->pdata;
7119d51a6b2SMarek Szyprowski 
71217866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
71317866e14SMarek Szyprowski 		pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
71417866e14SMarek Szyprowski 
71517866e14SMarek Szyprowski 	if (sc->ext_cd_irq)
71617866e14SMarek Szyprowski 		free_irq(sc->ext_cd_irq, sc);
71717866e14SMarek Szyprowski 
7182abeb5c5SChander Kashyap #ifdef CONFIG_PM_RUNTIME
7190aa55c23SSeungwon Jeon 	if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
7200f310a05SThomas Abraham 		clk_prepare_enable(sc->clk_io);
7212abeb5c5SChander Kashyap #endif
7229d51a6b2SMarek Szyprowski 	sdhci_remove_host(host, 1);
7239d51a6b2SMarek Szyprowski 
724387a8cbdSChander Kashyap 	pm_runtime_dont_use_autosuspend(&pdev->dev);
7259f4e8151SMark Brown 	pm_runtime_disable(&pdev->dev);
7269f4e8151SMark Brown 
7270f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_io);
7289d51a6b2SMarek Szyprowski 
7299d51a6b2SMarek Szyprowski 	sdhci_free_host(host);
7309d51a6b2SMarek Szyprowski 
7310d1bb41aSBen Dooks 	return 0;
7320d1bb41aSBen Dooks }
7330d1bb41aSBen Dooks 
734d5e9c02cSMark Brown #ifdef CONFIG_PM_SLEEP
73529495aa0SManuel Lauss static int sdhci_s3c_suspend(struct device *dev)
7360d1bb41aSBen Dooks {
73729495aa0SManuel Lauss 	struct sdhci_host *host = dev_get_drvdata(dev);
7380d1bb41aSBen Dooks 
73929495aa0SManuel Lauss 	return sdhci_suspend_host(host);
7400d1bb41aSBen Dooks }
7410d1bb41aSBen Dooks 
74229495aa0SManuel Lauss static int sdhci_s3c_resume(struct device *dev)
7430d1bb41aSBen Dooks {
74429495aa0SManuel Lauss 	struct sdhci_host *host = dev_get_drvdata(dev);
7450d1bb41aSBen Dooks 
74665d13516SWonil Choi 	return sdhci_resume_host(host);
7470d1bb41aSBen Dooks }
748d5e9c02cSMark Brown #endif
7490d1bb41aSBen Dooks 
7509f4e8151SMark Brown #ifdef CONFIG_PM_RUNTIME
7519f4e8151SMark Brown static int sdhci_s3c_runtime_suspend(struct device *dev)
7529f4e8151SMark Brown {
7539f4e8151SMark Brown 	struct sdhci_host *host = dev_get_drvdata(dev);
7542abeb5c5SChander Kashyap 	struct sdhci_s3c *ourhost = to_s3c(host);
7552abeb5c5SChander Kashyap 	struct clk *busclk = ourhost->clk_io;
7562abeb5c5SChander Kashyap 	int ret;
7579f4e8151SMark Brown 
7582abeb5c5SChander Kashyap 	ret = sdhci_runtime_suspend_host(host);
7592abeb5c5SChander Kashyap 
7603ac147faSTomasz Figa 	if (ourhost->cur_clk >= 0)
7610f310a05SThomas Abraham 		clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
7620f310a05SThomas Abraham 	clk_disable_unprepare(busclk);
7632abeb5c5SChander Kashyap 	return ret;
7649f4e8151SMark Brown }
7659f4e8151SMark Brown 
7669f4e8151SMark Brown static int sdhci_s3c_runtime_resume(struct device *dev)
7679f4e8151SMark Brown {
7689f4e8151SMark Brown 	struct sdhci_host *host = dev_get_drvdata(dev);
7692abeb5c5SChander Kashyap 	struct sdhci_s3c *ourhost = to_s3c(host);
7702abeb5c5SChander Kashyap 	struct clk *busclk = ourhost->clk_io;
7712abeb5c5SChander Kashyap 	int ret;
7729f4e8151SMark Brown 
7730f310a05SThomas Abraham 	clk_prepare_enable(busclk);
7743ac147faSTomasz Figa 	if (ourhost->cur_clk >= 0)
7750f310a05SThomas Abraham 		clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
7762abeb5c5SChander Kashyap 	ret = sdhci_runtime_resume_host(host);
7772abeb5c5SChander Kashyap 	return ret;
7789f4e8151SMark Brown }
7799f4e8151SMark Brown #endif
7809f4e8151SMark Brown 
781d5e9c02cSMark Brown #ifdef CONFIG_PM
78229495aa0SManuel Lauss static const struct dev_pm_ops sdhci_s3c_pmops = {
783d5e9c02cSMark Brown 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
7849f4e8151SMark Brown 	SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
7859f4e8151SMark Brown 			   NULL)
78629495aa0SManuel Lauss };
78729495aa0SManuel Lauss 
78829495aa0SManuel Lauss #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
78929495aa0SManuel Lauss 
7900d1bb41aSBen Dooks #else
79129495aa0SManuel Lauss #define SDHCI_S3C_PMOPS NULL
7920d1bb41aSBen Dooks #endif
7930d1bb41aSBen Dooks 
7943119936aSThomas Abraham #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
7953119936aSThomas Abraham static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
7963119936aSThomas Abraham 	.sdhci_quirks = SDHCI_QUIRK_NONSTANDARD_CLOCK,
7973119936aSThomas Abraham };
7983119936aSThomas Abraham #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
7993119936aSThomas Abraham #else
8003119936aSThomas Abraham #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
8013119936aSThomas Abraham #endif
8023119936aSThomas Abraham 
8033119936aSThomas Abraham static struct platform_device_id sdhci_s3c_driver_ids[] = {
8043119936aSThomas Abraham 	{
8053119936aSThomas Abraham 		.name		= "s3c-sdhci",
8063119936aSThomas Abraham 		.driver_data	= (kernel_ulong_t)NULL,
8073119936aSThomas Abraham 	}, {
8083119936aSThomas Abraham 		.name		= "exynos4-sdhci",
8093119936aSThomas Abraham 		.driver_data	= EXYNOS4_SDHCI_DRV_DATA,
8103119936aSThomas Abraham 	},
8113119936aSThomas Abraham 	{ }
8123119936aSThomas Abraham };
8133119936aSThomas Abraham MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
8143119936aSThomas Abraham 
815cd1b00ebSThomas Abraham #ifdef CONFIG_OF
816cd1b00ebSThomas Abraham static const struct of_device_id sdhci_s3c_dt_match[] = {
817cd1b00ebSThomas Abraham 	{ .compatible = "samsung,s3c6410-sdhci", },
818cd1b00ebSThomas Abraham 	{ .compatible = "samsung,exynos4210-sdhci",
819cd1b00ebSThomas Abraham 		.data = (void *)EXYNOS4_SDHCI_DRV_DATA },
820cd1b00ebSThomas Abraham 	{},
821cd1b00ebSThomas Abraham };
822cd1b00ebSThomas Abraham MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
823cd1b00ebSThomas Abraham #endif
824cd1b00ebSThomas Abraham 
8250d1bb41aSBen Dooks static struct platform_driver sdhci_s3c_driver = {
8260d1bb41aSBen Dooks 	.probe		= sdhci_s3c_probe,
8270433c143SBill Pemberton 	.remove		= sdhci_s3c_remove,
8283119936aSThomas Abraham 	.id_table	= sdhci_s3c_driver_ids,
8290d1bb41aSBen Dooks 	.driver		= {
8300d1bb41aSBen Dooks 		.owner	= THIS_MODULE,
8310d1bb41aSBen Dooks 		.name	= "s3c-sdhci",
832cd1b00ebSThomas Abraham 		.of_match_table = of_match_ptr(sdhci_s3c_dt_match),
83329495aa0SManuel Lauss 		.pm	= SDHCI_S3C_PMOPS,
8340d1bb41aSBen Dooks 	},
8350d1bb41aSBen Dooks };
8360d1bb41aSBen Dooks 
837d1f81a64SAxel Lin module_platform_driver(sdhci_s3c_driver);
8380d1bb41aSBen Dooks 
8390d1bb41aSBen Dooks MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
8400d1bb41aSBen Dooks MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
8410d1bb41aSBen Dooks MODULE_LICENSE("GPL v2");
8420d1bb41aSBen Dooks MODULE_ALIAS("platform:s3c-sdhci");
843