xref: /openbmc/linux/drivers/mmc/host/sdhci-s3c.c (revision cc014f3e)
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>
28b96efccbSTomasz Figa #include <linux/pinctrl/consumer.h>
290d1bb41aSBen Dooks 
300d1bb41aSBen Dooks #include <linux/mmc/host.h>
310d1bb41aSBen Dooks 
32cc014f3eSArnd Bergmann #include "sdhci-s3c-regs.h"
330d1bb41aSBen Dooks #include "sdhci.h"
340d1bb41aSBen Dooks 
350d1bb41aSBen Dooks #define MAX_BUS_CLK	(4)
360d1bb41aSBen Dooks 
37cd1b00ebSThomas Abraham /* Number of gpio's used is max data bus width + command and clock lines */
38cd1b00ebSThomas Abraham #define NUM_GPIOS(x)	(x + 2)
39cd1b00ebSThomas Abraham 
400d1bb41aSBen Dooks /**
410d1bb41aSBen Dooks  * struct sdhci_s3c - S3C SDHCI instance
420d1bb41aSBen Dooks  * @host: The SDHCI host created
430d1bb41aSBen Dooks  * @pdev: The platform device we where created from.
440d1bb41aSBen Dooks  * @ioarea: The resource created when we claimed the IO area.
450d1bb41aSBen Dooks  * @pdata: The platform data for this controller.
460d1bb41aSBen Dooks  * @cur_clk: The index of the current bus clock.
47cd1b00ebSThomas Abraham  * @gpios: List of gpio numbers parsed from device tree.
480d1bb41aSBen Dooks  * @clk_io: The clock for the internal bus interface.
490d1bb41aSBen Dooks  * @clk_bus: The clocks that are available for the SD/MMC bus clock.
500d1bb41aSBen Dooks  */
510d1bb41aSBen Dooks struct sdhci_s3c {
520d1bb41aSBen Dooks 	struct sdhci_host	*host;
530d1bb41aSBen Dooks 	struct platform_device	*pdev;
540d1bb41aSBen Dooks 	struct resource		*ioarea;
550d1bb41aSBen Dooks 	struct s3c_sdhci_platdata *pdata;
560d1bb41aSBen Dooks 	unsigned int		cur_clk;
5717866e14SMarek Szyprowski 	int			ext_cd_irq;
5817866e14SMarek Szyprowski 	int			ext_cd_gpio;
59cd1b00ebSThomas Abraham 	int			*gpios;
60b96efccbSTomasz Figa 	struct pinctrl          *pctrl;
610d1bb41aSBen Dooks 
620d1bb41aSBen Dooks 	struct clk		*clk_io;
630d1bb41aSBen Dooks 	struct clk		*clk_bus[MAX_BUS_CLK];
640d1bb41aSBen Dooks };
650d1bb41aSBen Dooks 
663119936aSThomas Abraham /**
673119936aSThomas Abraham  * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
683119936aSThomas Abraham  * @sdhci_quirks: sdhci host specific quirks.
693119936aSThomas Abraham  *
703119936aSThomas Abraham  * Specifies platform specific configuration of sdhci controller.
713119936aSThomas Abraham  * Note: A structure for driver specific platform data is used for future
723119936aSThomas Abraham  * expansion of its usage.
733119936aSThomas Abraham  */
743119936aSThomas Abraham struct sdhci_s3c_drv_data {
753119936aSThomas Abraham 	unsigned int	sdhci_quirks;
763119936aSThomas Abraham };
773119936aSThomas Abraham 
780d1bb41aSBen Dooks static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
790d1bb41aSBen Dooks {
800d1bb41aSBen Dooks 	return sdhci_priv(host);
810d1bb41aSBen Dooks }
820d1bb41aSBen Dooks 
830d1bb41aSBen Dooks /**
840d1bb41aSBen Dooks  * get_curclk - convert ctrl2 register to clock source number
850d1bb41aSBen Dooks  * @ctrl2: Control2 register value.
860d1bb41aSBen Dooks  */
870d1bb41aSBen Dooks static u32 get_curclk(u32 ctrl2)
880d1bb41aSBen Dooks {
890d1bb41aSBen Dooks 	ctrl2 &= S3C_SDHCI_CTRL2_SELBASECLK_MASK;
900d1bb41aSBen Dooks 	ctrl2 >>= S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
910d1bb41aSBen Dooks 
920d1bb41aSBen Dooks 	return ctrl2;
930d1bb41aSBen Dooks }
940d1bb41aSBen Dooks 
950d1bb41aSBen Dooks static void sdhci_s3c_check_sclk(struct sdhci_host *host)
960d1bb41aSBen Dooks {
970d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
980d1bb41aSBen Dooks 	u32 tmp = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
990d1bb41aSBen Dooks 
1000d1bb41aSBen Dooks 	if (get_curclk(tmp) != ourhost->cur_clk) {
1010d1bb41aSBen Dooks 		dev_dbg(&ourhost->pdev->dev, "restored ctrl2 clock setting\n");
1020d1bb41aSBen Dooks 
1030d1bb41aSBen Dooks 		tmp &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
1040d1bb41aSBen Dooks 		tmp |= ourhost->cur_clk << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
1057003fecbSJingoo Han 		writel(tmp, host->ioaddr + S3C_SDHCI_CONTROL2);
1060d1bb41aSBen Dooks 	}
1070d1bb41aSBen Dooks }
1080d1bb41aSBen Dooks 
1090d1bb41aSBen Dooks /**
1100d1bb41aSBen Dooks  * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
1110d1bb41aSBen Dooks  * @host: The SDHCI host instance.
1120d1bb41aSBen Dooks  *
1130d1bb41aSBen Dooks  * Callback to return the maximum clock rate acheivable by the controller.
1140d1bb41aSBen Dooks */
1150d1bb41aSBen Dooks static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
1160d1bb41aSBen Dooks {
1170d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
1180d1bb41aSBen Dooks 	struct clk *busclk;
1190d1bb41aSBen Dooks 	unsigned int rate, max;
1200d1bb41aSBen Dooks 	int clk;
1210d1bb41aSBen Dooks 
1220d1bb41aSBen Dooks 	/* note, a reset will reset the clock source */
1230d1bb41aSBen Dooks 
1240d1bb41aSBen Dooks 	sdhci_s3c_check_sclk(host);
1250d1bb41aSBen Dooks 
1260d1bb41aSBen Dooks 	for (max = 0, clk = 0; clk < MAX_BUS_CLK; clk++) {
1270d1bb41aSBen Dooks 		busclk = ourhost->clk_bus[clk];
1280d1bb41aSBen Dooks 		if (!busclk)
1290d1bb41aSBen Dooks 			continue;
1300d1bb41aSBen Dooks 
1310d1bb41aSBen Dooks 		rate = clk_get_rate(busclk);
1320d1bb41aSBen Dooks 		if (rate > max)
1330d1bb41aSBen Dooks 			max = rate;
1340d1bb41aSBen Dooks 	}
1350d1bb41aSBen Dooks 
1360d1bb41aSBen Dooks 	return max;
1370d1bb41aSBen Dooks }
1380d1bb41aSBen Dooks 
1390d1bb41aSBen Dooks /**
1400d1bb41aSBen Dooks  * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
1410d1bb41aSBen Dooks  * @ourhost: Our SDHCI instance.
1420d1bb41aSBen Dooks  * @src: The source clock index.
1430d1bb41aSBen Dooks  * @wanted: The clock frequency wanted.
1440d1bb41aSBen Dooks  */
1450d1bb41aSBen Dooks static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
1460d1bb41aSBen Dooks 					     unsigned int src,
1470d1bb41aSBen Dooks 					     unsigned int wanted)
1480d1bb41aSBen Dooks {
1490d1bb41aSBen Dooks 	unsigned long rate;
1500d1bb41aSBen Dooks 	struct clk *clksrc = ourhost->clk_bus[src];
1510d1bb41aSBen Dooks 	int div;
1520d1bb41aSBen Dooks 
1530d1bb41aSBen Dooks 	if (!clksrc)
1540d1bb41aSBen Dooks 		return UINT_MAX;
1550d1bb41aSBen Dooks 
156253e0a7cSJeongbae Seo 	/*
1573119936aSThomas Abraham 	 * If controller uses a non-standard clock division, find the best clock
1583119936aSThomas Abraham 	 * speed possible with selected clock source and skip the division.
159253e0a7cSJeongbae Seo 	 */
1603119936aSThomas Abraham 	if (ourhost->host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
161253e0a7cSJeongbae Seo 		rate = clk_round_rate(clksrc, wanted);
162253e0a7cSJeongbae Seo 		return wanted - rate;
163253e0a7cSJeongbae Seo 	}
164253e0a7cSJeongbae Seo 
1650d1bb41aSBen Dooks 	rate = clk_get_rate(clksrc);
1660d1bb41aSBen Dooks 
1670d1bb41aSBen Dooks 	for (div = 1; div < 256; div *= 2) {
1680d1bb41aSBen Dooks 		if ((rate / div) <= wanted)
1690d1bb41aSBen Dooks 			break;
1700d1bb41aSBen Dooks 	}
1710d1bb41aSBen Dooks 
1720d1bb41aSBen Dooks 	dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
1730d1bb41aSBen Dooks 		src, rate, wanted, rate / div);
1740d1bb41aSBen Dooks 
1752ad0b249SJingoo Han 	return wanted - (rate / div);
1760d1bb41aSBen Dooks }
1770d1bb41aSBen Dooks 
1780d1bb41aSBen Dooks /**
1790d1bb41aSBen Dooks  * sdhci_s3c_set_clock - callback on clock change
1800d1bb41aSBen Dooks  * @host: The SDHCI host being changed
1810d1bb41aSBen Dooks  * @clock: The clock rate being requested.
1820d1bb41aSBen Dooks  *
1830d1bb41aSBen Dooks  * When the card's clock is going to be changed, look at the new frequency
1840d1bb41aSBen Dooks  * and find the best clock source to go with it.
1850d1bb41aSBen Dooks */
1860d1bb41aSBen Dooks static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
1870d1bb41aSBen Dooks {
1880d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
1890d1bb41aSBen Dooks 	unsigned int best = UINT_MAX;
1900d1bb41aSBen Dooks 	unsigned int delta;
1910d1bb41aSBen Dooks 	int best_src = 0;
1920d1bb41aSBen Dooks 	int src;
1930d1bb41aSBen Dooks 	u32 ctrl;
1940d1bb41aSBen Dooks 
1950d1bb41aSBen Dooks 	/* don't bother if the clock is going off. */
1960d1bb41aSBen Dooks 	if (clock == 0)
1970d1bb41aSBen Dooks 		return;
1980d1bb41aSBen Dooks 
1990d1bb41aSBen Dooks 	for (src = 0; src < MAX_BUS_CLK; src++) {
2000d1bb41aSBen Dooks 		delta = sdhci_s3c_consider_clock(ourhost, src, clock);
2010d1bb41aSBen Dooks 		if (delta < best) {
2020d1bb41aSBen Dooks 			best = delta;
2030d1bb41aSBen Dooks 			best_src = src;
2040d1bb41aSBen Dooks 		}
2050d1bb41aSBen Dooks 	}
2060d1bb41aSBen Dooks 
2070d1bb41aSBen Dooks 	dev_dbg(&ourhost->pdev->dev,
2080d1bb41aSBen Dooks 		"selected source %d, clock %d, delta %d\n",
2090d1bb41aSBen Dooks 		 best_src, clock, best);
2100d1bb41aSBen Dooks 
2110d1bb41aSBen Dooks 	/* select the new clock source */
2120d1bb41aSBen Dooks 	if (ourhost->cur_clk != best_src) {
2130d1bb41aSBen Dooks 		struct clk *clk = ourhost->clk_bus[best_src];
2140d1bb41aSBen Dooks 
2150f310a05SThomas Abraham 		clk_prepare_enable(clk);
2160f310a05SThomas Abraham 		clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
217e684c468SChander Kashyap 
2180d1bb41aSBen Dooks 		/* turn clock off to card before changing clock source */
2190d1bb41aSBen Dooks 		writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
2200d1bb41aSBen Dooks 
2210d1bb41aSBen Dooks 		ourhost->cur_clk = best_src;
2220d1bb41aSBen Dooks 		host->max_clk = clk_get_rate(clk);
2230d1bb41aSBen Dooks 
2240d1bb41aSBen Dooks 		ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
2250d1bb41aSBen Dooks 		ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
2260d1bb41aSBen Dooks 		ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
2270d1bb41aSBen Dooks 		writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
2280d1bb41aSBen Dooks 	}
2290d1bb41aSBen Dooks 
2306fe47179SThomas Abraham 	/* reprogram default hardware configuration */
2316fe47179SThomas Abraham 	writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
2326fe47179SThomas Abraham 		host->ioaddr + S3C64XX_SDHCI_CONTROL4);
2330d1bb41aSBen Dooks 
2346fe47179SThomas Abraham 	ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
2356fe47179SThomas Abraham 	ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
2366fe47179SThomas Abraham 		  S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
2376fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_ENFBCLKRX |
2386fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_DFCNT_NONE |
2396fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
2406fe47179SThomas Abraham 	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
2410d1bb41aSBen Dooks 
2426fe47179SThomas Abraham 	/* reconfigure the controller for new clock rate */
2436fe47179SThomas Abraham 	ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
2446fe47179SThomas Abraham 	if (clock < 25 * 1000000)
2456fe47179SThomas Abraham 		ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
2466fe47179SThomas Abraham 	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
2470d1bb41aSBen Dooks }
2480d1bb41aSBen Dooks 
249ce5f036bSMarek Szyprowski /**
250ce5f036bSMarek Szyprowski  * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
251ce5f036bSMarek Szyprowski  * @host: The SDHCI host being queried
252ce5f036bSMarek Szyprowski  *
253ce5f036bSMarek Szyprowski  * To init mmc host properly a minimal clock value is needed. For high system
254ce5f036bSMarek Szyprowski  * bus clock's values the standard formula gives values out of allowed range.
255ce5f036bSMarek Szyprowski  * The clock still can be set to lower values, if clock source other then
256ce5f036bSMarek Szyprowski  * system bus is selected.
257ce5f036bSMarek Szyprowski */
258ce5f036bSMarek Szyprowski static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
259ce5f036bSMarek Szyprowski {
260ce5f036bSMarek Szyprowski 	struct sdhci_s3c *ourhost = to_s3c(host);
261ce5f036bSMarek Szyprowski 	unsigned int delta, min = UINT_MAX;
262ce5f036bSMarek Szyprowski 	int src;
263ce5f036bSMarek Szyprowski 
264ce5f036bSMarek Szyprowski 	for (src = 0; src < MAX_BUS_CLK; src++) {
265ce5f036bSMarek Szyprowski 		delta = sdhci_s3c_consider_clock(ourhost, src, 0);
266ce5f036bSMarek Szyprowski 		if (delta == UINT_MAX)
267ce5f036bSMarek Szyprowski 			continue;
268ce5f036bSMarek Szyprowski 		/* delta is a negative value in this case */
269ce5f036bSMarek Szyprowski 		if (-delta < min)
270ce5f036bSMarek Szyprowski 			min = -delta;
271ce5f036bSMarek Szyprowski 	}
272ce5f036bSMarek Szyprowski 	return min;
273ce5f036bSMarek Szyprowski }
274ce5f036bSMarek Szyprowski 
275253e0a7cSJeongbae Seo /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
276253e0a7cSJeongbae Seo static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
277253e0a7cSJeongbae Seo {
278253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
279253e0a7cSJeongbae Seo 
280253e0a7cSJeongbae Seo 	return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], UINT_MAX);
281253e0a7cSJeongbae Seo }
282253e0a7cSJeongbae Seo 
283253e0a7cSJeongbae Seo /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
284253e0a7cSJeongbae Seo static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
285253e0a7cSJeongbae Seo {
286253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
287253e0a7cSJeongbae Seo 
288253e0a7cSJeongbae Seo 	/*
289253e0a7cSJeongbae Seo 	 * initial clock can be in the frequency range of
290253e0a7cSJeongbae Seo 	 * 100KHz-400KHz, so we set it as max value.
291253e0a7cSJeongbae Seo 	 */
292253e0a7cSJeongbae Seo 	return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], 400000);
293253e0a7cSJeongbae Seo }
294253e0a7cSJeongbae Seo 
295253e0a7cSJeongbae Seo /* sdhci_cmu_set_clock - callback on clock change.*/
296253e0a7cSJeongbae Seo static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
297253e0a7cSJeongbae Seo {
298253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
2992ad0b249SJingoo Han 	struct device *dev = &ourhost->pdev->dev;
3003119936aSThomas Abraham 	unsigned long timeout;
3013119936aSThomas Abraham 	u16 clk = 0;
302253e0a7cSJeongbae Seo 
303253e0a7cSJeongbae Seo 	/* don't bother if the clock is going off */
304253e0a7cSJeongbae Seo 	if (clock == 0)
305253e0a7cSJeongbae Seo 		return;
306253e0a7cSJeongbae Seo 
307253e0a7cSJeongbae Seo 	sdhci_s3c_set_clock(host, clock);
308253e0a7cSJeongbae Seo 
309253e0a7cSJeongbae Seo 	clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
310253e0a7cSJeongbae Seo 
311253e0a7cSJeongbae Seo 	host->clock = clock;
3123119936aSThomas Abraham 
3133119936aSThomas Abraham 	clk = SDHCI_CLOCK_INT_EN;
3143119936aSThomas Abraham 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
3153119936aSThomas Abraham 
3163119936aSThomas Abraham 	/* Wait max 20 ms */
3173119936aSThomas Abraham 	timeout = 20;
3183119936aSThomas Abraham 	while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
3193119936aSThomas Abraham 		& SDHCI_CLOCK_INT_STABLE)) {
3203119936aSThomas Abraham 		if (timeout == 0) {
3212ad0b249SJingoo Han 			dev_err(dev, "%s: Internal clock never stabilised.\n",
3222ad0b249SJingoo Han 				mmc_hostname(host->mmc));
3233119936aSThomas Abraham 			return;
3243119936aSThomas Abraham 		}
3253119936aSThomas Abraham 		timeout--;
3263119936aSThomas Abraham 		mdelay(1);
3273119936aSThomas Abraham 	}
3283119936aSThomas Abraham 
3293119936aSThomas Abraham 	clk |= SDHCI_CLOCK_CARD_EN;
3303119936aSThomas Abraham 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
331253e0a7cSJeongbae Seo }
332253e0a7cSJeongbae Seo 
333548f07d2SJaehoon Chung /**
3347bc088d3SSascha Hauer  * sdhci_s3c_platform_bus_width - support 8bit buswidth
335548f07d2SJaehoon Chung  * @host: The SDHCI host being queried
336548f07d2SJaehoon Chung  * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
337548f07d2SJaehoon Chung  *
338548f07d2SJaehoon Chung  * We have 8-bit width support but is not a v3 controller.
3397bc088d3SSascha Hauer  * So we add platform_bus_width() and support 8bit width.
340548f07d2SJaehoon Chung  */
3417bc088d3SSascha Hauer static int sdhci_s3c_platform_bus_width(struct sdhci_host *host, int width)
342548f07d2SJaehoon Chung {
343548f07d2SJaehoon Chung 	u8 ctrl;
344548f07d2SJaehoon Chung 
345548f07d2SJaehoon Chung 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
346548f07d2SJaehoon Chung 
347548f07d2SJaehoon Chung 	switch (width) {
348548f07d2SJaehoon Chung 	case MMC_BUS_WIDTH_8:
349548f07d2SJaehoon Chung 		ctrl |= SDHCI_CTRL_8BITBUS;
350548f07d2SJaehoon Chung 		ctrl &= ~SDHCI_CTRL_4BITBUS;
351548f07d2SJaehoon Chung 		break;
352548f07d2SJaehoon Chung 	case MMC_BUS_WIDTH_4:
353548f07d2SJaehoon Chung 		ctrl |= SDHCI_CTRL_4BITBUS;
354548f07d2SJaehoon Chung 		ctrl &= ~SDHCI_CTRL_8BITBUS;
355548f07d2SJaehoon Chung 		break;
356548f07d2SJaehoon Chung 	default:
35749bb1e61SGirish K S 		ctrl &= ~SDHCI_CTRL_4BITBUS;
35849bb1e61SGirish K S 		ctrl &= ~SDHCI_CTRL_8BITBUS;
359548f07d2SJaehoon Chung 		break;
360548f07d2SJaehoon Chung 	}
361548f07d2SJaehoon Chung 
362548f07d2SJaehoon Chung 	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
363548f07d2SJaehoon Chung 
364548f07d2SJaehoon Chung 	return 0;
365548f07d2SJaehoon Chung }
366548f07d2SJaehoon Chung 
3670d1bb41aSBen Dooks static struct sdhci_ops sdhci_s3c_ops = {
3680d1bb41aSBen Dooks 	.get_max_clock		= sdhci_s3c_get_max_clk,
3690d1bb41aSBen Dooks 	.set_clock		= sdhci_s3c_set_clock,
370ce5f036bSMarek Szyprowski 	.get_min_clock		= sdhci_s3c_get_min_clock,
3717bc088d3SSascha Hauer 	.platform_bus_width	= sdhci_s3c_platform_bus_width,
3720d1bb41aSBen Dooks };
3730d1bb41aSBen Dooks 
37417866e14SMarek Szyprowski static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
37517866e14SMarek Szyprowski {
37617866e14SMarek Szyprowski 	struct sdhci_host *host = platform_get_drvdata(dev);
3774577f77bSSachin Kamat #ifdef CONFIG_PM_RUNTIME
378fe007c02SHeiko Stübner 	struct sdhci_s3c *sc = sdhci_priv(host);
3794577f77bSSachin Kamat #endif
38006fe577fSMarek Szyprowski 	unsigned long flags;
38106fe577fSMarek Szyprowski 
38217866e14SMarek Szyprowski 	if (host) {
38306fe577fSMarek Szyprowski 		spin_lock_irqsave(&host->lock, flags);
38417866e14SMarek Szyprowski 		if (state) {
38517866e14SMarek Szyprowski 			dev_dbg(&dev->dev, "card inserted.\n");
386fe007c02SHeiko Stübner #ifdef CONFIG_PM_RUNTIME
387fe007c02SHeiko Stübner 			clk_prepare_enable(sc->clk_io);
388fe007c02SHeiko Stübner #endif
38917866e14SMarek Szyprowski 			host->flags &= ~SDHCI_DEVICE_DEAD;
39017866e14SMarek Szyprowski 			host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
39117866e14SMarek Szyprowski 		} else {
39217866e14SMarek Szyprowski 			dev_dbg(&dev->dev, "card removed.\n");
39317866e14SMarek Szyprowski 			host->flags |= SDHCI_DEVICE_DEAD;
39417866e14SMarek Szyprowski 			host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
395fe007c02SHeiko Stübner #ifdef CONFIG_PM_RUNTIME
396fe007c02SHeiko Stübner 			clk_disable_unprepare(sc->clk_io);
397fe007c02SHeiko Stübner #endif
39817866e14SMarek Szyprowski 		}
399f522886eSKyungmin Park 		tasklet_schedule(&host->card_tasklet);
40006fe577fSMarek Szyprowski 		spin_unlock_irqrestore(&host->lock, flags);
40117866e14SMarek Szyprowski 	}
40217866e14SMarek Szyprowski }
40317866e14SMarek Szyprowski 
40417866e14SMarek Szyprowski static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
40517866e14SMarek Szyprowski {
40617866e14SMarek Szyprowski 	struct sdhci_s3c *sc = dev_id;
40717866e14SMarek Szyprowski 	int status = gpio_get_value(sc->ext_cd_gpio);
40817866e14SMarek Szyprowski 	if (sc->pdata->ext_cd_gpio_invert)
40917866e14SMarek Szyprowski 		status = !status;
41017866e14SMarek Szyprowski 	sdhci_s3c_notify_change(sc->pdev, status);
41117866e14SMarek Szyprowski 	return IRQ_HANDLED;
41217866e14SMarek Szyprowski }
41317866e14SMarek Szyprowski 
41417866e14SMarek Szyprowski static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
41517866e14SMarek Szyprowski {
41617866e14SMarek Szyprowski 	struct s3c_sdhci_platdata *pdata = sc->pdata;
41717866e14SMarek Szyprowski 	struct device *dev = &sc->pdev->dev;
41817866e14SMarek Szyprowski 
419b1b8fea9STomasz Figa 	if (devm_gpio_request(dev, pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
42017866e14SMarek Szyprowski 		sc->ext_cd_gpio = pdata->ext_cd_gpio;
42117866e14SMarek Szyprowski 		sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
42217866e14SMarek Szyprowski 		if (sc->ext_cd_irq &&
42317866e14SMarek Szyprowski 		    request_threaded_irq(sc->ext_cd_irq, NULL,
42417866e14SMarek Szyprowski 					 sdhci_s3c_gpio_card_detect_thread,
4252ad0b249SJingoo Han 					 IRQF_TRIGGER_RISING |
4262ad0b249SJingoo Han 					 IRQF_TRIGGER_FALLING |
4272ad0b249SJingoo Han 					 IRQF_ONESHOT,
42817866e14SMarek Szyprowski 					 dev_name(dev), sc) == 0) {
42917866e14SMarek Szyprowski 			int status = gpio_get_value(sc->ext_cd_gpio);
43017866e14SMarek Szyprowski 			if (pdata->ext_cd_gpio_invert)
43117866e14SMarek Szyprowski 				status = !status;
43217866e14SMarek Szyprowski 			sdhci_s3c_notify_change(sc->pdev, status);
43317866e14SMarek Szyprowski 		} else {
43417866e14SMarek Szyprowski 			dev_warn(dev, "cannot request irq for card detect\n");
43517866e14SMarek Szyprowski 			sc->ext_cd_irq = 0;
43617866e14SMarek Szyprowski 		}
43717866e14SMarek Szyprowski 	} else {
43817866e14SMarek Szyprowski 		dev_err(dev, "cannot request gpio for card detect\n");
43917866e14SMarek Szyprowski 	}
44017866e14SMarek Szyprowski }
44117866e14SMarek Szyprowski 
442cd1b00ebSThomas Abraham #ifdef CONFIG_OF
443c3be1efdSBill Pemberton static int sdhci_s3c_parse_dt(struct device *dev,
444cd1b00ebSThomas Abraham 		struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
445cd1b00ebSThomas Abraham {
446cd1b00ebSThomas Abraham 	struct device_node *node = dev->of_node;
447cd1b00ebSThomas Abraham 	struct sdhci_s3c *ourhost = to_s3c(host);
448cd1b00ebSThomas Abraham 	u32 max_width;
449cd1b00ebSThomas Abraham 	int gpio, cnt, ret;
450cd1b00ebSThomas Abraham 
451cd1b00ebSThomas Abraham 	/* if the bus-width property is not specified, assume width as 1 */
452cd1b00ebSThomas Abraham 	if (of_property_read_u32(node, "bus-width", &max_width))
453cd1b00ebSThomas Abraham 		max_width = 1;
454cd1b00ebSThomas Abraham 	pdata->max_width = max_width;
455cd1b00ebSThomas Abraham 
456cd1b00ebSThomas Abraham 	ourhost->gpios = devm_kzalloc(dev, NUM_GPIOS(pdata->max_width) *
457cd1b00ebSThomas Abraham 				sizeof(int), GFP_KERNEL);
458cd1b00ebSThomas Abraham 	if (!ourhost->gpios)
459cd1b00ebSThomas Abraham 		return -ENOMEM;
460cd1b00ebSThomas Abraham 
461cd1b00ebSThomas Abraham 	/* get the card detection method */
462ab5023efSTushar Behera 	if (of_get_property(node, "broken-cd", NULL)) {
463cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_NONE;
464cd1b00ebSThomas Abraham 		goto setup_bus;
465cd1b00ebSThomas Abraham 	}
466cd1b00ebSThomas Abraham 
467ab5023efSTushar Behera 	if (of_get_property(node, "non-removable", NULL)) {
468cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
469cd1b00ebSThomas Abraham 		goto setup_bus;
470cd1b00ebSThomas Abraham 	}
471cd1b00ebSThomas Abraham 
472cd1b00ebSThomas Abraham 	gpio = of_get_named_gpio(node, "cd-gpios", 0);
473cd1b00ebSThomas Abraham 	if (gpio_is_valid(gpio)) {
474cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_GPIO;
475cd1b00ebSThomas Abraham 		goto found_cd;
476cd1b00ebSThomas Abraham 	} else if (gpio != -ENOENT) {
477cd1b00ebSThomas Abraham 		dev_err(dev, "invalid card detect gpio specified\n");
478cd1b00ebSThomas Abraham 		return -EINVAL;
479cd1b00ebSThomas Abraham 	}
480cd1b00ebSThomas Abraham 
481cd1b00ebSThomas Abraham 	gpio = of_get_named_gpio(node, "samsung,cd-pinmux-gpio", 0);
482cd1b00ebSThomas Abraham 	if (gpio_is_valid(gpio)) {
483cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
484cd1b00ebSThomas Abraham 		goto found_cd;
485cd1b00ebSThomas Abraham 	} else if (gpio != -ENOENT) {
486cd1b00ebSThomas Abraham 		dev_err(dev, "invalid card detect gpio specified\n");
487cd1b00ebSThomas Abraham 		return -EINVAL;
488cd1b00ebSThomas Abraham 	}
489cd1b00ebSThomas Abraham 
490b96efccbSTomasz Figa 	/* assuming internal card detect that will be configured by pinctrl */
491b96efccbSTomasz Figa 	pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
492b96efccbSTomasz Figa 	goto setup_bus;
493cd1b00ebSThomas Abraham 
494cd1b00ebSThomas Abraham  found_cd:
495cd1b00ebSThomas Abraham 	if (pdata->cd_type == S3C_SDHCI_CD_GPIO) {
496cd1b00ebSThomas Abraham 		pdata->ext_cd_gpio = gpio;
497cd1b00ebSThomas Abraham 		ourhost->ext_cd_gpio = -1;
498cd1b00ebSThomas Abraham 		if (of_get_property(node, "cd-inverted", NULL))
499cd1b00ebSThomas Abraham 			pdata->ext_cd_gpio_invert = 1;
500cd1b00ebSThomas Abraham 	} else if (pdata->cd_type == S3C_SDHCI_CD_INTERNAL) {
501b1b8fea9STomasz Figa 		ret = devm_gpio_request(dev, gpio, "sdhci-cd");
502cd1b00ebSThomas Abraham 		if (ret) {
503cd1b00ebSThomas Abraham 			dev_err(dev, "card detect gpio request failed\n");
504cd1b00ebSThomas Abraham 			return -EINVAL;
505cd1b00ebSThomas Abraham 		}
506cd1b00ebSThomas Abraham 		ourhost->ext_cd_gpio = gpio;
507cd1b00ebSThomas Abraham 	}
508cd1b00ebSThomas Abraham 
509cd1b00ebSThomas Abraham  setup_bus:
510b96efccbSTomasz Figa 	if (!IS_ERR(ourhost->pctrl))
511b96efccbSTomasz Figa 		return 0;
512b96efccbSTomasz Figa 
513cd1b00ebSThomas Abraham 	/* get the gpios for command, clock and data lines */
514cd1b00ebSThomas Abraham 	for (cnt = 0; cnt < NUM_GPIOS(pdata->max_width); cnt++) {
515cd1b00ebSThomas Abraham 		gpio = of_get_gpio(node, cnt);
516cd1b00ebSThomas Abraham 		if (!gpio_is_valid(gpio)) {
517cd1b00ebSThomas Abraham 			dev_err(dev, "invalid gpio[%d]\n", cnt);
518b1b8fea9STomasz Figa 			return -EINVAL;
519cd1b00ebSThomas Abraham 		}
520cd1b00ebSThomas Abraham 		ourhost->gpios[cnt] = gpio;
521cd1b00ebSThomas Abraham 	}
522cd1b00ebSThomas Abraham 
523cd1b00ebSThomas Abraham 	for (cnt = 0; cnt < NUM_GPIOS(pdata->max_width); cnt++) {
524b1b8fea9STomasz Figa 		ret = devm_gpio_request(dev, ourhost->gpios[cnt], "sdhci-gpio");
525cd1b00ebSThomas Abraham 		if (ret) {
526cd1b00ebSThomas Abraham 			dev_err(dev, "gpio[%d] request failed\n", cnt);
527b1b8fea9STomasz Figa 			return -EINVAL;
528cd1b00ebSThomas Abraham 		}
529cd1b00ebSThomas Abraham 	}
530cd1b00ebSThomas Abraham 
531cd1b00ebSThomas Abraham 	return 0;
532cd1b00ebSThomas Abraham }
533cd1b00ebSThomas Abraham #else
534c3be1efdSBill Pemberton static int sdhci_s3c_parse_dt(struct device *dev,
535cd1b00ebSThomas Abraham 		struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
536cd1b00ebSThomas Abraham {
537cd1b00ebSThomas Abraham 	return -EINVAL;
538cd1b00ebSThomas Abraham }
539cd1b00ebSThomas Abraham #endif
540cd1b00ebSThomas Abraham 
541cd1b00ebSThomas Abraham static const struct of_device_id sdhci_s3c_dt_match[];
542cd1b00ebSThomas Abraham 
5433119936aSThomas Abraham static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
5443119936aSThomas Abraham 			struct platform_device *pdev)
5453119936aSThomas Abraham {
546cd1b00ebSThomas Abraham #ifdef CONFIG_OF
547cd1b00ebSThomas Abraham 	if (pdev->dev.of_node) {
548cd1b00ebSThomas Abraham 		const struct of_device_id *match;
549cd1b00ebSThomas Abraham 		match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
550cd1b00ebSThomas Abraham 		return (struct sdhci_s3c_drv_data *)match->data;
551cd1b00ebSThomas Abraham 	}
552cd1b00ebSThomas Abraham #endif
5533119936aSThomas Abraham 	return (struct sdhci_s3c_drv_data *)
5543119936aSThomas Abraham 			platform_get_device_id(pdev)->driver_data;
5553119936aSThomas Abraham }
5563119936aSThomas Abraham 
557c3be1efdSBill Pemberton static int sdhci_s3c_probe(struct platform_device *pdev)
5580d1bb41aSBen Dooks {
5591d4dc338SThomas Abraham 	struct s3c_sdhci_platdata *pdata;
5603119936aSThomas Abraham 	struct sdhci_s3c_drv_data *drv_data;
5610d1bb41aSBen Dooks 	struct device *dev = &pdev->dev;
5620d1bb41aSBen Dooks 	struct sdhci_host *host;
5630d1bb41aSBen Dooks 	struct sdhci_s3c *sc;
5640d1bb41aSBen Dooks 	struct resource *res;
5650d1bb41aSBen Dooks 	int ret, irq, ptr, clks;
5660d1bb41aSBen Dooks 
567cd1b00ebSThomas Abraham 	if (!pdev->dev.platform_data && !pdev->dev.of_node) {
5680d1bb41aSBen Dooks 		dev_err(dev, "no device data specified\n");
5690d1bb41aSBen Dooks 		return -ENOENT;
5700d1bb41aSBen Dooks 	}
5710d1bb41aSBen Dooks 
5720d1bb41aSBen Dooks 	irq = platform_get_irq(pdev, 0);
5730d1bb41aSBen Dooks 	if (irq < 0) {
5740d1bb41aSBen Dooks 		dev_err(dev, "no irq specified\n");
5750d1bb41aSBen Dooks 		return irq;
5760d1bb41aSBen Dooks 	}
5770d1bb41aSBen Dooks 
5780d1bb41aSBen Dooks 	host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
5790d1bb41aSBen Dooks 	if (IS_ERR(host)) {
5800d1bb41aSBen Dooks 		dev_err(dev, "sdhci_alloc_host() failed\n");
5810d1bb41aSBen Dooks 		return PTR_ERR(host);
5820d1bb41aSBen Dooks 	}
583cd1b00ebSThomas Abraham 	sc = sdhci_priv(host);
5840d1bb41aSBen Dooks 
5851d4dc338SThomas Abraham 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
5861d4dc338SThomas Abraham 	if (!pdata) {
5871d4dc338SThomas Abraham 		ret = -ENOMEM;
588b1b8fea9STomasz Figa 		goto err_pdata_io_clk;
5891d4dc338SThomas Abraham 	}
590cd1b00ebSThomas Abraham 
591b96efccbSTomasz Figa 	sc->pctrl = devm_pinctrl_get_select_default(&pdev->dev);
592b96efccbSTomasz Figa 
593cd1b00ebSThomas Abraham 	if (pdev->dev.of_node) {
594cd1b00ebSThomas Abraham 		ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
595cd1b00ebSThomas Abraham 		if (ret)
596b1b8fea9STomasz Figa 			goto err_pdata_io_clk;
597cd1b00ebSThomas Abraham 	} else {
5981d4dc338SThomas Abraham 		memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
599cd1b00ebSThomas Abraham 		sc->ext_cd_gpio = -1; /* invalid gpio number */
600cd1b00ebSThomas Abraham 	}
6011d4dc338SThomas Abraham 
6023119936aSThomas Abraham 	drv_data = sdhci_s3c_get_driver_data(pdev);
6030d1bb41aSBen Dooks 
6040d1bb41aSBen Dooks 	sc->host = host;
6050d1bb41aSBen Dooks 	sc->pdev = pdev;
6060d1bb41aSBen Dooks 	sc->pdata = pdata;
6070d1bb41aSBen Dooks 
6080d1bb41aSBen Dooks 	platform_set_drvdata(pdev, host);
6090d1bb41aSBen Dooks 
6100d1bb41aSBen Dooks 	sc->clk_io = clk_get(dev, "hsmmc");
6110d1bb41aSBen Dooks 	if (IS_ERR(sc->clk_io)) {
6120d1bb41aSBen Dooks 		dev_err(dev, "failed to get io clock\n");
6130d1bb41aSBen Dooks 		ret = PTR_ERR(sc->clk_io);
614b1b8fea9STomasz Figa 		goto err_pdata_io_clk;
6150d1bb41aSBen Dooks 	}
6160d1bb41aSBen Dooks 
6170d1bb41aSBen Dooks 	/* enable the local io clock and keep it running for the moment. */
6180f310a05SThomas Abraham 	clk_prepare_enable(sc->clk_io);
6190d1bb41aSBen Dooks 
6200d1bb41aSBen Dooks 	for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
6210d1bb41aSBen Dooks 		struct clk *clk;
6224346b6d9SRajeshwari Shinde 		char name[14];
6230d1bb41aSBen Dooks 
6244346b6d9SRajeshwari Shinde 		snprintf(name, 14, "mmc_busclk.%d", ptr);
6250d1bb41aSBen Dooks 		clk = clk_get(dev, name);
6262ad0b249SJingoo Han 		if (IS_ERR(clk))
6270d1bb41aSBen Dooks 			continue;
6280d1bb41aSBen Dooks 
6290d1bb41aSBen Dooks 		clks++;
6300d1bb41aSBen Dooks 		sc->clk_bus[ptr] = clk;
631253e0a7cSJeongbae Seo 
632253e0a7cSJeongbae Seo 		/*
633253e0a7cSJeongbae Seo 		 * save current clock index to know which clock bus
634253e0a7cSJeongbae Seo 		 * is used later in overriding functions.
635253e0a7cSJeongbae Seo 		 */
636253e0a7cSJeongbae Seo 		sc->cur_clk = ptr;
637253e0a7cSJeongbae Seo 
6380d1bb41aSBen Dooks 		dev_info(dev, "clock source %d: %s (%ld Hz)\n",
6390d1bb41aSBen Dooks 			 ptr, name, clk_get_rate(clk));
6400d1bb41aSBen Dooks 	}
6410d1bb41aSBen Dooks 
6420d1bb41aSBen Dooks 	if (clks == 0) {
6430d1bb41aSBen Dooks 		dev_err(dev, "failed to find any bus clocks\n");
6440d1bb41aSBen Dooks 		ret = -ENOENT;
6450d1bb41aSBen Dooks 		goto err_no_busclks;
6460d1bb41aSBen Dooks 	}
6470d1bb41aSBen Dooks 
6482abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME
6490f310a05SThomas Abraham 	clk_prepare_enable(sc->clk_bus[sc->cur_clk]);
6502abeb5c5SChander Kashyap #endif
651e684c468SChander Kashyap 
6529bda6da7SJulia Lawall 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
653a3e2cd7fSThierry Reding 	host->ioaddr = devm_ioremap_resource(&pdev->dev, res);
654a3e2cd7fSThierry Reding 	if (IS_ERR(host->ioaddr)) {
655a3e2cd7fSThierry Reding 		ret = PTR_ERR(host->ioaddr);
6560d1bb41aSBen Dooks 		goto err_req_regs;
6570d1bb41aSBen Dooks 	}
6580d1bb41aSBen Dooks 
6590d1bb41aSBen Dooks 	/* Ensure we have minimal gpio selected CMD/CLK/Detect */
6600d1bb41aSBen Dooks 	if (pdata->cfg_gpio)
6610d1bb41aSBen Dooks 		pdata->cfg_gpio(pdev, pdata->max_width);
6620d1bb41aSBen Dooks 
6630d1bb41aSBen Dooks 	host->hw_name = "samsung-hsmmc";
6640d1bb41aSBen Dooks 	host->ops = &sdhci_s3c_ops;
6650d1bb41aSBen Dooks 	host->quirks = 0;
6660d1bb41aSBen Dooks 	host->irq = irq;
6670d1bb41aSBen Dooks 
6680d1bb41aSBen Dooks 	/* Setup quirks for the controller */
669b2e75effSThomas Abraham 	host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
670a1d56460SMarek Szyprowski 	host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
6713119936aSThomas Abraham 	if (drv_data)
6723119936aSThomas Abraham 		host->quirks |= drv_data->sdhci_quirks;
6730d1bb41aSBen Dooks 
6740d1bb41aSBen Dooks #ifndef CONFIG_MMC_SDHCI_S3C_DMA
6750d1bb41aSBen Dooks 
6760d1bb41aSBen Dooks 	/* we currently see overruns on errors, so disable the SDMA
6770d1bb41aSBen Dooks 	 * support as well. */
6780d1bb41aSBen Dooks 	host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
6790d1bb41aSBen Dooks 
6800d1bb41aSBen Dooks #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
6810d1bb41aSBen Dooks 
6820d1bb41aSBen Dooks 	/* It seems we do not get an DATA transfer complete on non-busy
6830d1bb41aSBen Dooks 	 * transfers, not sure if this is a problem with this specific
6840d1bb41aSBen Dooks 	 * SDHCI block, or a missing configuration that needs to be set. */
6850d1bb41aSBen Dooks 	host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
6860d1bb41aSBen Dooks 
687732f0e31SKyungmin Park 	/* This host supports the Auto CMD12 */
688732f0e31SKyungmin Park 	host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
689732f0e31SKyungmin Park 
6907199e2b6SJaehoon Chung 	/* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
6917199e2b6SJaehoon Chung 	host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
6927199e2b6SJaehoon Chung 
69317866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
69417866e14SMarek Szyprowski 	    pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
69517866e14SMarek Szyprowski 		host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
69617866e14SMarek Szyprowski 
69717866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
69817866e14SMarek Szyprowski 		host->mmc->caps = MMC_CAP_NONREMOVABLE;
69917866e14SMarek Szyprowski 
7000d22c770SThomas Abraham 	switch (pdata->max_width) {
7010d22c770SThomas Abraham 	case 8:
7020d22c770SThomas Abraham 		host->mmc->caps |= MMC_CAP_8_BIT_DATA;
7030d22c770SThomas Abraham 	case 4:
7040d22c770SThomas Abraham 		host->mmc->caps |= MMC_CAP_4_BIT_DATA;
7050d22c770SThomas Abraham 		break;
7060d22c770SThomas Abraham 	}
7070d22c770SThomas Abraham 
708fa1773ccSSangwook Lee 	if (pdata->pm_caps)
709fa1773ccSSangwook Lee 		host->mmc->pm_caps |= pdata->pm_caps;
710fa1773ccSSangwook Lee 
7110d1bb41aSBen Dooks 	host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
7120d1bb41aSBen Dooks 			 SDHCI_QUIRK_32BIT_DMA_SIZE);
7130d1bb41aSBen Dooks 
7143fe42e07SHyuk Lee 	/* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
7153fe42e07SHyuk Lee 	host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
7163fe42e07SHyuk Lee 
717253e0a7cSJeongbae Seo 	/*
718253e0a7cSJeongbae Seo 	 * If controller does not have internal clock divider,
719253e0a7cSJeongbae Seo 	 * we can use overriding functions instead of default.
720253e0a7cSJeongbae Seo 	 */
7213119936aSThomas Abraham 	if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
722253e0a7cSJeongbae Seo 		sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
723253e0a7cSJeongbae Seo 		sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
724253e0a7cSJeongbae Seo 		sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
725253e0a7cSJeongbae Seo 	}
726253e0a7cSJeongbae Seo 
727b3824f2cSJeongbae Seo 	/* It supports additional host capabilities if needed */
728b3824f2cSJeongbae Seo 	if (pdata->host_caps)
729b3824f2cSJeongbae Seo 		host->mmc->caps |= pdata->host_caps;
730b3824f2cSJeongbae Seo 
731c1c4b66dSJaehoon Chung 	if (pdata->host_caps2)
732c1c4b66dSJaehoon Chung 		host->mmc->caps2 |= pdata->host_caps2;
733c1c4b66dSJaehoon Chung 
7349f4e8151SMark Brown 	pm_runtime_enable(&pdev->dev);
7359f4e8151SMark Brown 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
7369f4e8151SMark Brown 	pm_runtime_use_autosuspend(&pdev->dev);
7379f4e8151SMark Brown 	pm_suspend_ignore_children(&pdev->dev, 1);
7389f4e8151SMark Brown 
7390d1bb41aSBen Dooks 	ret = sdhci_add_host(host);
7400d1bb41aSBen Dooks 	if (ret) {
7410d1bb41aSBen Dooks 		dev_err(dev, "sdhci_add_host() failed\n");
7429f4e8151SMark Brown 		pm_runtime_forbid(&pdev->dev);
7439f4e8151SMark Brown 		pm_runtime_get_noresume(&pdev->dev);
7449bda6da7SJulia Lawall 		goto err_req_regs;
7450d1bb41aSBen Dooks 	}
7460d1bb41aSBen Dooks 
74717866e14SMarek Szyprowski 	/* The following two methods of card detection might call
74817866e14SMarek Szyprowski 	   sdhci_s3c_notify_change() immediately, so they can be called
74917866e14SMarek Szyprowski 	   only after sdhci_add_host(). Setup errors are ignored. */
75017866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
75117866e14SMarek Szyprowski 		pdata->ext_cd_init(&sdhci_s3c_notify_change);
75217866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
75317866e14SMarek Szyprowski 	    gpio_is_valid(pdata->ext_cd_gpio))
75417866e14SMarek Szyprowski 		sdhci_s3c_setup_card_detect_gpio(sc);
75517866e14SMarek Szyprowski 
7562abeb5c5SChander Kashyap #ifdef CONFIG_PM_RUNTIME
7570aa55c23SSeungwon Jeon 	if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
7580f310a05SThomas Abraham 		clk_disable_unprepare(sc->clk_io);
7592abeb5c5SChander Kashyap #endif
7600d1bb41aSBen Dooks 	return 0;
7610d1bb41aSBen Dooks 
7620d1bb41aSBen Dooks  err_req_regs:
7632abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME
7640f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_bus[sc->cur_clk]);
7652abeb5c5SChander Kashyap #endif
7660d1bb41aSBen Dooks 	for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
767326adda5SJaehoon Chung 		if (sc->clk_bus[ptr]) {
7680d1bb41aSBen Dooks 			clk_put(sc->clk_bus[ptr]);
7690d1bb41aSBen Dooks 		}
770326adda5SJaehoon Chung 	}
7710d1bb41aSBen Dooks 
7720d1bb41aSBen Dooks  err_no_busclks:
7730f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_io);
7740d1bb41aSBen Dooks 	clk_put(sc->clk_io);
7750d1bb41aSBen Dooks 
776b1b8fea9STomasz Figa  err_pdata_io_clk:
7770d1bb41aSBen Dooks 	sdhci_free_host(host);
7780d1bb41aSBen Dooks 
7790d1bb41aSBen Dooks 	return ret;
7800d1bb41aSBen Dooks }
7810d1bb41aSBen Dooks 
7826e0ee714SBill Pemberton static int sdhci_s3c_remove(struct platform_device *pdev)
7830d1bb41aSBen Dooks {
7849d51a6b2SMarek Szyprowski 	struct sdhci_host *host =  platform_get_drvdata(pdev);
7859d51a6b2SMarek Szyprowski 	struct sdhci_s3c *sc = sdhci_priv(host);
786cd1b00ebSThomas Abraham 	struct s3c_sdhci_platdata *pdata = sc->pdata;
7879d51a6b2SMarek Szyprowski 	int ptr;
7889d51a6b2SMarek Szyprowski 
78917866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
79017866e14SMarek Szyprowski 		pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
79117866e14SMarek Szyprowski 
79217866e14SMarek Szyprowski 	if (sc->ext_cd_irq)
79317866e14SMarek Szyprowski 		free_irq(sc->ext_cd_irq, sc);
79417866e14SMarek Szyprowski 
7952abeb5c5SChander Kashyap #ifdef CONFIG_PM_RUNTIME
7960aa55c23SSeungwon Jeon 	if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
7970f310a05SThomas Abraham 		clk_prepare_enable(sc->clk_io);
7982abeb5c5SChander Kashyap #endif
7999d51a6b2SMarek Szyprowski 	sdhci_remove_host(host, 1);
8009d51a6b2SMarek Szyprowski 
801387a8cbdSChander Kashyap 	pm_runtime_dont_use_autosuspend(&pdev->dev);
8029f4e8151SMark Brown 	pm_runtime_disable(&pdev->dev);
8039f4e8151SMark Brown 
8042abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME
8050f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_bus[sc->cur_clk]);
8062abeb5c5SChander Kashyap #endif
8075feb54a1SJaehoon Chung 	for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
8089320f7cbSMarek Szyprowski 		if (sc->clk_bus[ptr]) {
8099d51a6b2SMarek Szyprowski 			clk_put(sc->clk_bus[ptr]);
8109d51a6b2SMarek Szyprowski 		}
8119320f7cbSMarek Szyprowski 	}
8120f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_io);
8139d51a6b2SMarek Szyprowski 	clk_put(sc->clk_io);
8149d51a6b2SMarek Szyprowski 
8159d51a6b2SMarek Szyprowski 	sdhci_free_host(host);
8169d51a6b2SMarek Szyprowski 	platform_set_drvdata(pdev, NULL);
8179d51a6b2SMarek Szyprowski 
8180d1bb41aSBen Dooks 	return 0;
8190d1bb41aSBen Dooks }
8200d1bb41aSBen Dooks 
821d5e9c02cSMark Brown #ifdef CONFIG_PM_SLEEP
82229495aa0SManuel Lauss static int sdhci_s3c_suspend(struct device *dev)
8230d1bb41aSBen Dooks {
82429495aa0SManuel Lauss 	struct sdhci_host *host = dev_get_drvdata(dev);
8250d1bb41aSBen Dooks 
82629495aa0SManuel Lauss 	return sdhci_suspend_host(host);
8270d1bb41aSBen Dooks }
8280d1bb41aSBen Dooks 
82929495aa0SManuel Lauss static int sdhci_s3c_resume(struct device *dev)
8300d1bb41aSBen Dooks {
83129495aa0SManuel Lauss 	struct sdhci_host *host = dev_get_drvdata(dev);
8320d1bb41aSBen Dooks 
83365d13516SWonil Choi 	return sdhci_resume_host(host);
8340d1bb41aSBen Dooks }
835d5e9c02cSMark Brown #endif
8360d1bb41aSBen Dooks 
8379f4e8151SMark Brown #ifdef CONFIG_PM_RUNTIME
8389f4e8151SMark Brown static int sdhci_s3c_runtime_suspend(struct device *dev)
8399f4e8151SMark Brown {
8409f4e8151SMark Brown 	struct sdhci_host *host = dev_get_drvdata(dev);
8412abeb5c5SChander Kashyap 	struct sdhci_s3c *ourhost = to_s3c(host);
8422abeb5c5SChander Kashyap 	struct clk *busclk = ourhost->clk_io;
8432abeb5c5SChander Kashyap 	int ret;
8449f4e8151SMark Brown 
8452abeb5c5SChander Kashyap 	ret = sdhci_runtime_suspend_host(host);
8462abeb5c5SChander Kashyap 
8470f310a05SThomas Abraham 	clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
8480f310a05SThomas Abraham 	clk_disable_unprepare(busclk);
8492abeb5c5SChander Kashyap 	return ret;
8509f4e8151SMark Brown }
8519f4e8151SMark Brown 
8529f4e8151SMark Brown static int sdhci_s3c_runtime_resume(struct device *dev)
8539f4e8151SMark Brown {
8549f4e8151SMark Brown 	struct sdhci_host *host = dev_get_drvdata(dev);
8552abeb5c5SChander Kashyap 	struct sdhci_s3c *ourhost = to_s3c(host);
8562abeb5c5SChander Kashyap 	struct clk *busclk = ourhost->clk_io;
8572abeb5c5SChander Kashyap 	int ret;
8589f4e8151SMark Brown 
8590f310a05SThomas Abraham 	clk_prepare_enable(busclk);
8600f310a05SThomas Abraham 	clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
8612abeb5c5SChander Kashyap 	ret = sdhci_runtime_resume_host(host);
8622abeb5c5SChander Kashyap 	return ret;
8639f4e8151SMark Brown }
8649f4e8151SMark Brown #endif
8659f4e8151SMark Brown 
866d5e9c02cSMark Brown #ifdef CONFIG_PM
86729495aa0SManuel Lauss static const struct dev_pm_ops sdhci_s3c_pmops = {
868d5e9c02cSMark Brown 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
8699f4e8151SMark Brown 	SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
8709f4e8151SMark Brown 			   NULL)
87129495aa0SManuel Lauss };
87229495aa0SManuel Lauss 
87329495aa0SManuel Lauss #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
87429495aa0SManuel Lauss 
8750d1bb41aSBen Dooks #else
87629495aa0SManuel Lauss #define SDHCI_S3C_PMOPS NULL
8770d1bb41aSBen Dooks #endif
8780d1bb41aSBen Dooks 
8793119936aSThomas Abraham #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
8803119936aSThomas Abraham static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
8813119936aSThomas Abraham 	.sdhci_quirks = SDHCI_QUIRK_NONSTANDARD_CLOCK,
8823119936aSThomas Abraham };
8833119936aSThomas Abraham #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
8843119936aSThomas Abraham #else
8853119936aSThomas Abraham #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
8863119936aSThomas Abraham #endif
8873119936aSThomas Abraham 
8883119936aSThomas Abraham static struct platform_device_id sdhci_s3c_driver_ids[] = {
8893119936aSThomas Abraham 	{
8903119936aSThomas Abraham 		.name		= "s3c-sdhci",
8913119936aSThomas Abraham 		.driver_data	= (kernel_ulong_t)NULL,
8923119936aSThomas Abraham 	}, {
8933119936aSThomas Abraham 		.name		= "exynos4-sdhci",
8943119936aSThomas Abraham 		.driver_data	= EXYNOS4_SDHCI_DRV_DATA,
8953119936aSThomas Abraham 	},
8963119936aSThomas Abraham 	{ }
8973119936aSThomas Abraham };
8983119936aSThomas Abraham MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
8993119936aSThomas Abraham 
900cd1b00ebSThomas Abraham #ifdef CONFIG_OF
901cd1b00ebSThomas Abraham static const struct of_device_id sdhci_s3c_dt_match[] = {
902cd1b00ebSThomas Abraham 	{ .compatible = "samsung,s3c6410-sdhci", },
903cd1b00ebSThomas Abraham 	{ .compatible = "samsung,exynos4210-sdhci",
904cd1b00ebSThomas Abraham 		.data = (void *)EXYNOS4_SDHCI_DRV_DATA },
905cd1b00ebSThomas Abraham 	{},
906cd1b00ebSThomas Abraham };
907cd1b00ebSThomas Abraham MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
908cd1b00ebSThomas Abraham #endif
909cd1b00ebSThomas Abraham 
9100d1bb41aSBen Dooks static struct platform_driver sdhci_s3c_driver = {
9110d1bb41aSBen Dooks 	.probe		= sdhci_s3c_probe,
9120433c143SBill Pemberton 	.remove		= sdhci_s3c_remove,
9133119936aSThomas Abraham 	.id_table	= sdhci_s3c_driver_ids,
9140d1bb41aSBen Dooks 	.driver		= {
9150d1bb41aSBen Dooks 		.owner	= THIS_MODULE,
9160d1bb41aSBen Dooks 		.name	= "s3c-sdhci",
917cd1b00ebSThomas Abraham 		.of_match_table = of_match_ptr(sdhci_s3c_dt_match),
91829495aa0SManuel Lauss 		.pm	= SDHCI_S3C_PMOPS,
9190d1bb41aSBen Dooks 	},
9200d1bb41aSBen Dooks };
9210d1bb41aSBen Dooks 
922d1f81a64SAxel Lin module_platform_driver(sdhci_s3c_driver);
9230d1bb41aSBen Dooks 
9240d1bb41aSBen Dooks MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
9250d1bb41aSBen Dooks MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
9260d1bb41aSBen Dooks MODULE_LICENSE("GPL v2");
9270d1bb41aSBen Dooks MODULE_ALIAS("platform:s3c-sdhci");
928