xref: /openbmc/linux/drivers/mmc/host/sdhci-s3c.c (revision 0f310a05)
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>
185a0e3ad6STejun Heo #include <linux/slab.h>
190d1bb41aSBen Dooks #include <linux/clk.h>
200d1bb41aSBen Dooks #include <linux/io.h>
2117866e14SMarek Szyprowski #include <linux/gpio.h>
2255156d24SMark Brown #include <linux/module.h>
23d5e9c02cSMark Brown #include <linux/of.h>
24d5e9c02cSMark Brown #include <linux/of_gpio.h>
25d5e9c02cSMark Brown #include <linux/pm.h>
269f4e8151SMark Brown #include <linux/pm_runtime.h>
270d1bb41aSBen Dooks 
280d1bb41aSBen Dooks #include <linux/mmc/host.h>
290d1bb41aSBen Dooks 
300d1bb41aSBen Dooks #include <plat/sdhci.h>
310d1bb41aSBen Dooks #include <plat/regs-sdhci.h>
320d1bb41aSBen Dooks 
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;
600d1bb41aSBen Dooks 
610d1bb41aSBen Dooks 	struct clk		*clk_io;
620d1bb41aSBen Dooks 	struct clk		*clk_bus[MAX_BUS_CLK];
630d1bb41aSBen Dooks };
640d1bb41aSBen Dooks 
653119936aSThomas Abraham /**
663119936aSThomas Abraham  * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
673119936aSThomas Abraham  * @sdhci_quirks: sdhci host specific quirks.
683119936aSThomas Abraham  *
693119936aSThomas Abraham  * Specifies platform specific configuration of sdhci controller.
703119936aSThomas Abraham  * Note: A structure for driver specific platform data is used for future
713119936aSThomas Abraham  * expansion of its usage.
723119936aSThomas Abraham  */
733119936aSThomas Abraham struct sdhci_s3c_drv_data {
743119936aSThomas Abraham 	unsigned int	sdhci_quirks;
753119936aSThomas Abraham };
763119936aSThomas Abraham 
770d1bb41aSBen Dooks static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
780d1bb41aSBen Dooks {
790d1bb41aSBen Dooks 	return sdhci_priv(host);
800d1bb41aSBen Dooks }
810d1bb41aSBen Dooks 
820d1bb41aSBen Dooks /**
830d1bb41aSBen Dooks  * get_curclk - convert ctrl2 register to clock source number
840d1bb41aSBen Dooks  * @ctrl2: Control2 register value.
850d1bb41aSBen Dooks  */
860d1bb41aSBen Dooks static u32 get_curclk(u32 ctrl2)
870d1bb41aSBen Dooks {
880d1bb41aSBen Dooks 	ctrl2 &= S3C_SDHCI_CTRL2_SELBASECLK_MASK;
890d1bb41aSBen Dooks 	ctrl2 >>= S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
900d1bb41aSBen Dooks 
910d1bb41aSBen Dooks 	return ctrl2;
920d1bb41aSBen Dooks }
930d1bb41aSBen Dooks 
940d1bb41aSBen Dooks static void sdhci_s3c_check_sclk(struct sdhci_host *host)
950d1bb41aSBen Dooks {
960d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
970d1bb41aSBen Dooks 	u32 tmp = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
980d1bb41aSBen Dooks 
990d1bb41aSBen Dooks 	if (get_curclk(tmp) != ourhost->cur_clk) {
1000d1bb41aSBen Dooks 		dev_dbg(&ourhost->pdev->dev, "restored ctrl2 clock setting\n");
1010d1bb41aSBen Dooks 
1020d1bb41aSBen Dooks 		tmp &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
1030d1bb41aSBen Dooks 		tmp |= ourhost->cur_clk << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
1047003fecbSJingoo Han 		writel(tmp, host->ioaddr + S3C_SDHCI_CONTROL2);
1050d1bb41aSBen Dooks 	}
1060d1bb41aSBen Dooks }
1070d1bb41aSBen Dooks 
1080d1bb41aSBen Dooks /**
1090d1bb41aSBen Dooks  * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
1100d1bb41aSBen Dooks  * @host: The SDHCI host instance.
1110d1bb41aSBen Dooks  *
1120d1bb41aSBen Dooks  * Callback to return the maximum clock rate acheivable by the controller.
1130d1bb41aSBen Dooks */
1140d1bb41aSBen Dooks static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
1150d1bb41aSBen Dooks {
1160d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
1170d1bb41aSBen Dooks 	struct clk *busclk;
1180d1bb41aSBen Dooks 	unsigned int rate, max;
1190d1bb41aSBen Dooks 	int clk;
1200d1bb41aSBen Dooks 
1210d1bb41aSBen Dooks 	/* note, a reset will reset the clock source */
1220d1bb41aSBen Dooks 
1230d1bb41aSBen Dooks 	sdhci_s3c_check_sclk(host);
1240d1bb41aSBen Dooks 
1250d1bb41aSBen Dooks 	for (max = 0, clk = 0; clk < MAX_BUS_CLK; clk++) {
1260d1bb41aSBen Dooks 		busclk = ourhost->clk_bus[clk];
1270d1bb41aSBen Dooks 		if (!busclk)
1280d1bb41aSBen Dooks 			continue;
1290d1bb41aSBen Dooks 
1300d1bb41aSBen Dooks 		rate = clk_get_rate(busclk);
1310d1bb41aSBen Dooks 		if (rate > max)
1320d1bb41aSBen Dooks 			max = rate;
1330d1bb41aSBen Dooks 	}
1340d1bb41aSBen Dooks 
1350d1bb41aSBen Dooks 	return max;
1360d1bb41aSBen Dooks }
1370d1bb41aSBen Dooks 
1380d1bb41aSBen Dooks /**
1390d1bb41aSBen Dooks  * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
1400d1bb41aSBen Dooks  * @ourhost: Our SDHCI instance.
1410d1bb41aSBen Dooks  * @src: The source clock index.
1420d1bb41aSBen Dooks  * @wanted: The clock frequency wanted.
1430d1bb41aSBen Dooks  */
1440d1bb41aSBen Dooks static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
1450d1bb41aSBen Dooks 					     unsigned int src,
1460d1bb41aSBen Dooks 					     unsigned int wanted)
1470d1bb41aSBen Dooks {
1480d1bb41aSBen Dooks 	unsigned long rate;
1490d1bb41aSBen Dooks 	struct clk *clksrc = ourhost->clk_bus[src];
1500d1bb41aSBen Dooks 	int div;
1510d1bb41aSBen Dooks 
1520d1bb41aSBen Dooks 	if (!clksrc)
1530d1bb41aSBen Dooks 		return UINT_MAX;
1540d1bb41aSBen Dooks 
155253e0a7cSJeongbae Seo 	/*
1563119936aSThomas Abraham 	 * If controller uses a non-standard clock division, find the best clock
1573119936aSThomas Abraham 	 * speed possible with selected clock source and skip the division.
158253e0a7cSJeongbae Seo 	 */
1593119936aSThomas Abraham 	if (ourhost->host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
160253e0a7cSJeongbae Seo 		rate = clk_round_rate(clksrc, wanted);
161253e0a7cSJeongbae Seo 		return wanted - rate;
162253e0a7cSJeongbae Seo 	}
163253e0a7cSJeongbae Seo 
1640d1bb41aSBen Dooks 	rate = clk_get_rate(clksrc);
1650d1bb41aSBen Dooks 
1660d1bb41aSBen Dooks 	for (div = 1; div < 256; div *= 2) {
1670d1bb41aSBen Dooks 		if ((rate / div) <= wanted)
1680d1bb41aSBen Dooks 			break;
1690d1bb41aSBen Dooks 	}
1700d1bb41aSBen Dooks 
1710d1bb41aSBen Dooks 	dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
1720d1bb41aSBen Dooks 		src, rate, wanted, rate / div);
1730d1bb41aSBen Dooks 
1742ad0b249SJingoo Han 	return wanted - (rate / div);
1750d1bb41aSBen Dooks }
1760d1bb41aSBen Dooks 
1770d1bb41aSBen Dooks /**
1780d1bb41aSBen Dooks  * sdhci_s3c_set_clock - callback on clock change
1790d1bb41aSBen Dooks  * @host: The SDHCI host being changed
1800d1bb41aSBen Dooks  * @clock: The clock rate being requested.
1810d1bb41aSBen Dooks  *
1820d1bb41aSBen Dooks  * When the card's clock is going to be changed, look at the new frequency
1830d1bb41aSBen Dooks  * and find the best clock source to go with it.
1840d1bb41aSBen Dooks */
1850d1bb41aSBen Dooks static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
1860d1bb41aSBen Dooks {
1870d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
1880d1bb41aSBen Dooks 	unsigned int best = UINT_MAX;
1890d1bb41aSBen Dooks 	unsigned int delta;
1900d1bb41aSBen Dooks 	int best_src = 0;
1910d1bb41aSBen Dooks 	int src;
1920d1bb41aSBen Dooks 	u32 ctrl;
1930d1bb41aSBen Dooks 
1940d1bb41aSBen Dooks 	/* don't bother if the clock is going off. */
1950d1bb41aSBen Dooks 	if (clock == 0)
1960d1bb41aSBen Dooks 		return;
1970d1bb41aSBen Dooks 
1980d1bb41aSBen Dooks 	for (src = 0; src < MAX_BUS_CLK; src++) {
1990d1bb41aSBen Dooks 		delta = sdhci_s3c_consider_clock(ourhost, src, clock);
2000d1bb41aSBen Dooks 		if (delta < best) {
2010d1bb41aSBen Dooks 			best = delta;
2020d1bb41aSBen Dooks 			best_src = src;
2030d1bb41aSBen Dooks 		}
2040d1bb41aSBen Dooks 	}
2050d1bb41aSBen Dooks 
2060d1bb41aSBen Dooks 	dev_dbg(&ourhost->pdev->dev,
2070d1bb41aSBen Dooks 		"selected source %d, clock %d, delta %d\n",
2080d1bb41aSBen Dooks 		 best_src, clock, best);
2090d1bb41aSBen Dooks 
2100d1bb41aSBen Dooks 	/* select the new clock source */
2110d1bb41aSBen Dooks 	if (ourhost->cur_clk != best_src) {
2120d1bb41aSBen Dooks 		struct clk *clk = ourhost->clk_bus[best_src];
2130d1bb41aSBen Dooks 
2140f310a05SThomas Abraham 		clk_prepare_enable(clk);
2150f310a05SThomas Abraham 		clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
216e684c468SChander Kashyap 
2170d1bb41aSBen Dooks 		/* turn clock off to card before changing clock source */
2180d1bb41aSBen Dooks 		writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
2190d1bb41aSBen Dooks 
2200d1bb41aSBen Dooks 		ourhost->cur_clk = best_src;
2210d1bb41aSBen Dooks 		host->max_clk = clk_get_rate(clk);
2220d1bb41aSBen Dooks 
2230d1bb41aSBen Dooks 		ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
2240d1bb41aSBen Dooks 		ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
2250d1bb41aSBen Dooks 		ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
2260d1bb41aSBen Dooks 		writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
2270d1bb41aSBen Dooks 	}
2280d1bb41aSBen Dooks 
2296fe47179SThomas Abraham 	/* reprogram default hardware configuration */
2306fe47179SThomas Abraham 	writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
2316fe47179SThomas Abraham 		host->ioaddr + S3C64XX_SDHCI_CONTROL4);
2320d1bb41aSBen Dooks 
2336fe47179SThomas Abraham 	ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
2346fe47179SThomas Abraham 	ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
2356fe47179SThomas Abraham 		  S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
2366fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_ENFBCLKRX |
2376fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_DFCNT_NONE |
2386fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
2396fe47179SThomas Abraham 	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
2400d1bb41aSBen Dooks 
2416fe47179SThomas Abraham 	/* reconfigure the controller for new clock rate */
2426fe47179SThomas Abraham 	ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
2436fe47179SThomas Abraham 	if (clock < 25 * 1000000)
2446fe47179SThomas Abraham 		ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
2456fe47179SThomas Abraham 	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
2460d1bb41aSBen Dooks }
2470d1bb41aSBen Dooks 
248ce5f036bSMarek Szyprowski /**
249ce5f036bSMarek Szyprowski  * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
250ce5f036bSMarek Szyprowski  * @host: The SDHCI host being queried
251ce5f036bSMarek Szyprowski  *
252ce5f036bSMarek Szyprowski  * To init mmc host properly a minimal clock value is needed. For high system
253ce5f036bSMarek Szyprowski  * bus clock's values the standard formula gives values out of allowed range.
254ce5f036bSMarek Szyprowski  * The clock still can be set to lower values, if clock source other then
255ce5f036bSMarek Szyprowski  * system bus is selected.
256ce5f036bSMarek Szyprowski */
257ce5f036bSMarek Szyprowski static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
258ce5f036bSMarek Szyprowski {
259ce5f036bSMarek Szyprowski 	struct sdhci_s3c *ourhost = to_s3c(host);
260ce5f036bSMarek Szyprowski 	unsigned int delta, min = UINT_MAX;
261ce5f036bSMarek Szyprowski 	int src;
262ce5f036bSMarek Szyprowski 
263ce5f036bSMarek Szyprowski 	for (src = 0; src < MAX_BUS_CLK; src++) {
264ce5f036bSMarek Szyprowski 		delta = sdhci_s3c_consider_clock(ourhost, src, 0);
265ce5f036bSMarek Szyprowski 		if (delta == UINT_MAX)
266ce5f036bSMarek Szyprowski 			continue;
267ce5f036bSMarek Szyprowski 		/* delta is a negative value in this case */
268ce5f036bSMarek Szyprowski 		if (-delta < min)
269ce5f036bSMarek Szyprowski 			min = -delta;
270ce5f036bSMarek Szyprowski 	}
271ce5f036bSMarek Szyprowski 	return min;
272ce5f036bSMarek Szyprowski }
273ce5f036bSMarek Szyprowski 
274253e0a7cSJeongbae Seo /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
275253e0a7cSJeongbae Seo static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
276253e0a7cSJeongbae Seo {
277253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
278253e0a7cSJeongbae Seo 
279253e0a7cSJeongbae Seo 	return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], UINT_MAX);
280253e0a7cSJeongbae Seo }
281253e0a7cSJeongbae Seo 
282253e0a7cSJeongbae Seo /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
283253e0a7cSJeongbae Seo static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
284253e0a7cSJeongbae Seo {
285253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
286253e0a7cSJeongbae Seo 
287253e0a7cSJeongbae Seo 	/*
288253e0a7cSJeongbae Seo 	 * initial clock can be in the frequency range of
289253e0a7cSJeongbae Seo 	 * 100KHz-400KHz, so we set it as max value.
290253e0a7cSJeongbae Seo 	 */
291253e0a7cSJeongbae Seo 	return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], 400000);
292253e0a7cSJeongbae Seo }
293253e0a7cSJeongbae Seo 
294253e0a7cSJeongbae Seo /* sdhci_cmu_set_clock - callback on clock change.*/
295253e0a7cSJeongbae Seo static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
296253e0a7cSJeongbae Seo {
297253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
2982ad0b249SJingoo Han 	struct device *dev = &ourhost->pdev->dev;
2993119936aSThomas Abraham 	unsigned long timeout;
3003119936aSThomas Abraham 	u16 clk = 0;
301253e0a7cSJeongbae Seo 
302253e0a7cSJeongbae Seo 	/* don't bother if the clock is going off */
303253e0a7cSJeongbae Seo 	if (clock == 0)
304253e0a7cSJeongbae Seo 		return;
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 /**
333548f07d2SJaehoon Chung  * sdhci_s3c_platform_8bit_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.
338548f07d2SJaehoon Chung  * So we add platform_8bit_width() and support 8bit width.
339548f07d2SJaehoon Chung  */
340548f07d2SJaehoon Chung static int sdhci_s3c_platform_8bit_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,
370548f07d2SJaehoon Chung 	.platform_8bit_width	= sdhci_s3c_platform_8bit_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);
37606fe577fSMarek Szyprowski 	unsigned long flags;
37706fe577fSMarek Szyprowski 
37817866e14SMarek Szyprowski 	if (host) {
37906fe577fSMarek Szyprowski 		spin_lock_irqsave(&host->lock, flags);
38017866e14SMarek Szyprowski 		if (state) {
38117866e14SMarek Szyprowski 			dev_dbg(&dev->dev, "card inserted.\n");
38217866e14SMarek Szyprowski 			host->flags &= ~SDHCI_DEVICE_DEAD;
38317866e14SMarek Szyprowski 			host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
38417866e14SMarek Szyprowski 		} else {
38517866e14SMarek Szyprowski 			dev_dbg(&dev->dev, "card removed.\n");
38617866e14SMarek Szyprowski 			host->flags |= SDHCI_DEVICE_DEAD;
38717866e14SMarek Szyprowski 			host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
38817866e14SMarek Szyprowski 		}
389f522886eSKyungmin Park 		tasklet_schedule(&host->card_tasklet);
39006fe577fSMarek Szyprowski 		spin_unlock_irqrestore(&host->lock, flags);
39117866e14SMarek Szyprowski 	}
39217866e14SMarek Szyprowski }
39317866e14SMarek Szyprowski 
39417866e14SMarek Szyprowski static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
39517866e14SMarek Szyprowski {
39617866e14SMarek Szyprowski 	struct sdhci_s3c *sc = dev_id;
39717866e14SMarek Szyprowski 	int status = gpio_get_value(sc->ext_cd_gpio);
39817866e14SMarek Szyprowski 	if (sc->pdata->ext_cd_gpio_invert)
39917866e14SMarek Szyprowski 		status = !status;
40017866e14SMarek Szyprowski 	sdhci_s3c_notify_change(sc->pdev, status);
40117866e14SMarek Szyprowski 	return IRQ_HANDLED;
40217866e14SMarek Szyprowski }
40317866e14SMarek Szyprowski 
40417866e14SMarek Szyprowski static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
40517866e14SMarek Szyprowski {
40617866e14SMarek Szyprowski 	struct s3c_sdhci_platdata *pdata = sc->pdata;
40717866e14SMarek Szyprowski 	struct device *dev = &sc->pdev->dev;
40817866e14SMarek Szyprowski 
40917866e14SMarek Szyprowski 	if (gpio_request(pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
41017866e14SMarek Szyprowski 		sc->ext_cd_gpio = pdata->ext_cd_gpio;
41117866e14SMarek Szyprowski 		sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
41217866e14SMarek Szyprowski 		if (sc->ext_cd_irq &&
41317866e14SMarek Szyprowski 		    request_threaded_irq(sc->ext_cd_irq, NULL,
41417866e14SMarek Szyprowski 					 sdhci_s3c_gpio_card_detect_thread,
4152ad0b249SJingoo Han 					 IRQF_TRIGGER_RISING |
4162ad0b249SJingoo Han 					 IRQF_TRIGGER_FALLING |
4172ad0b249SJingoo Han 					 IRQF_ONESHOT,
41817866e14SMarek Szyprowski 					 dev_name(dev), sc) == 0) {
41917866e14SMarek Szyprowski 			int status = gpio_get_value(sc->ext_cd_gpio);
42017866e14SMarek Szyprowski 			if (pdata->ext_cd_gpio_invert)
42117866e14SMarek Szyprowski 				status = !status;
42217866e14SMarek Szyprowski 			sdhci_s3c_notify_change(sc->pdev, status);
42317866e14SMarek Szyprowski 		} else {
42417866e14SMarek Szyprowski 			dev_warn(dev, "cannot request irq for card detect\n");
42517866e14SMarek Szyprowski 			sc->ext_cd_irq = 0;
42617866e14SMarek Szyprowski 		}
42717866e14SMarek Szyprowski 	} else {
42817866e14SMarek Szyprowski 		dev_err(dev, "cannot request gpio for card detect\n");
42917866e14SMarek Szyprowski 	}
43017866e14SMarek Szyprowski }
43117866e14SMarek Szyprowski 
432cd1b00ebSThomas Abraham #ifdef CONFIG_OF
433cd1b00ebSThomas Abraham static int __devinit sdhci_s3c_parse_dt(struct device *dev,
434cd1b00ebSThomas Abraham 		struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
435cd1b00ebSThomas Abraham {
436cd1b00ebSThomas Abraham 	struct device_node *node = dev->of_node;
437cd1b00ebSThomas Abraham 	struct sdhci_s3c *ourhost = to_s3c(host);
438cd1b00ebSThomas Abraham 	u32 max_width;
439cd1b00ebSThomas Abraham 	int gpio, cnt, ret;
440cd1b00ebSThomas Abraham 
441cd1b00ebSThomas Abraham 	/* if the bus-width property is not specified, assume width as 1 */
442cd1b00ebSThomas Abraham 	if (of_property_read_u32(node, "bus-width", &max_width))
443cd1b00ebSThomas Abraham 		max_width = 1;
444cd1b00ebSThomas Abraham 	pdata->max_width = max_width;
445cd1b00ebSThomas Abraham 
446cd1b00ebSThomas Abraham 	ourhost->gpios = devm_kzalloc(dev, NUM_GPIOS(pdata->max_width) *
447cd1b00ebSThomas Abraham 				sizeof(int), GFP_KERNEL);
448cd1b00ebSThomas Abraham 	if (!ourhost->gpios)
449cd1b00ebSThomas Abraham 		return -ENOMEM;
450cd1b00ebSThomas Abraham 
451cd1b00ebSThomas Abraham 	/* get the card detection method */
452cd1b00ebSThomas Abraham 	if (of_get_property(node, "broken-cd", 0)) {
453cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_NONE;
454cd1b00ebSThomas Abraham 		goto setup_bus;
455cd1b00ebSThomas Abraham 	}
456cd1b00ebSThomas Abraham 
457cd1b00ebSThomas Abraham 	if (of_get_property(node, "non-removable", 0)) {
458cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
459cd1b00ebSThomas Abraham 		goto setup_bus;
460cd1b00ebSThomas Abraham 	}
461cd1b00ebSThomas Abraham 
462cd1b00ebSThomas Abraham 	gpio = of_get_named_gpio(node, "cd-gpios", 0);
463cd1b00ebSThomas Abraham 	if (gpio_is_valid(gpio)) {
464cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_GPIO;
465cd1b00ebSThomas Abraham 		goto found_cd;
466cd1b00ebSThomas Abraham 	} else if (gpio != -ENOENT) {
467cd1b00ebSThomas Abraham 		dev_err(dev, "invalid card detect gpio specified\n");
468cd1b00ebSThomas Abraham 		return -EINVAL;
469cd1b00ebSThomas Abraham 	}
470cd1b00ebSThomas Abraham 
471cd1b00ebSThomas Abraham 	gpio = of_get_named_gpio(node, "samsung,cd-pinmux-gpio", 0);
472cd1b00ebSThomas Abraham 	if (gpio_is_valid(gpio)) {
473cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
474cd1b00ebSThomas Abraham 		goto found_cd;
475cd1b00ebSThomas Abraham 	} else if (gpio != -ENOENT) {
476cd1b00ebSThomas Abraham 		dev_err(dev, "invalid card detect gpio specified\n");
477cd1b00ebSThomas Abraham 		return -EINVAL;
478cd1b00ebSThomas Abraham 	}
479cd1b00ebSThomas Abraham 
480cd1b00ebSThomas Abraham 	dev_info(dev, "assuming no card detect line available\n");
481cd1b00ebSThomas Abraham 	pdata->cd_type = S3C_SDHCI_CD_NONE;
482cd1b00ebSThomas Abraham 
483cd1b00ebSThomas Abraham  found_cd:
484cd1b00ebSThomas Abraham 	if (pdata->cd_type == S3C_SDHCI_CD_GPIO) {
485cd1b00ebSThomas Abraham 		pdata->ext_cd_gpio = gpio;
486cd1b00ebSThomas Abraham 		ourhost->ext_cd_gpio = -1;
487cd1b00ebSThomas Abraham 		if (of_get_property(node, "cd-inverted", NULL))
488cd1b00ebSThomas Abraham 			pdata->ext_cd_gpio_invert = 1;
489cd1b00ebSThomas Abraham 	} else if (pdata->cd_type == S3C_SDHCI_CD_INTERNAL) {
490cd1b00ebSThomas Abraham 		ret = gpio_request(gpio, "sdhci-cd");
491cd1b00ebSThomas Abraham 		if (ret) {
492cd1b00ebSThomas Abraham 			dev_err(dev, "card detect gpio request failed\n");
493cd1b00ebSThomas Abraham 			return -EINVAL;
494cd1b00ebSThomas Abraham 		}
495cd1b00ebSThomas Abraham 		ourhost->ext_cd_gpio = gpio;
496cd1b00ebSThomas Abraham 	}
497cd1b00ebSThomas Abraham 
498cd1b00ebSThomas Abraham  setup_bus:
499cd1b00ebSThomas Abraham 	/* get the gpios for command, clock and data lines */
500cd1b00ebSThomas Abraham 	for (cnt = 0; cnt < NUM_GPIOS(pdata->max_width); cnt++) {
501cd1b00ebSThomas Abraham 		gpio = of_get_gpio(node, cnt);
502cd1b00ebSThomas Abraham 		if (!gpio_is_valid(gpio)) {
503cd1b00ebSThomas Abraham 			dev_err(dev, "invalid gpio[%d]\n", cnt);
504cd1b00ebSThomas Abraham 			goto err_free_dt_cd_gpio;
505cd1b00ebSThomas Abraham 		}
506cd1b00ebSThomas Abraham 		ourhost->gpios[cnt] = gpio;
507cd1b00ebSThomas Abraham 	}
508cd1b00ebSThomas Abraham 
509cd1b00ebSThomas Abraham 	for (cnt = 0; cnt < NUM_GPIOS(pdata->max_width); cnt++) {
510cd1b00ebSThomas Abraham 		ret = gpio_request(ourhost->gpios[cnt], "sdhci-gpio");
511cd1b00ebSThomas Abraham 		if (ret) {
512cd1b00ebSThomas Abraham 			dev_err(dev, "gpio[%d] request failed\n", cnt);
513cd1b00ebSThomas Abraham 			goto err_free_dt_gpios;
514cd1b00ebSThomas Abraham 		}
515cd1b00ebSThomas Abraham 	}
516cd1b00ebSThomas Abraham 
517cd1b00ebSThomas Abraham 	return 0;
518cd1b00ebSThomas Abraham 
519cd1b00ebSThomas Abraham  err_free_dt_gpios:
520cd1b00ebSThomas Abraham 	while (--cnt >= 0)
521cd1b00ebSThomas Abraham 		gpio_free(ourhost->gpios[cnt]);
522cd1b00ebSThomas Abraham  err_free_dt_cd_gpio:
523cd1b00ebSThomas Abraham 	if (pdata->cd_type == S3C_SDHCI_CD_INTERNAL)
524cd1b00ebSThomas Abraham 		gpio_free(ourhost->ext_cd_gpio);
525cd1b00ebSThomas Abraham 	return -EINVAL;
526cd1b00ebSThomas Abraham }
527cd1b00ebSThomas Abraham #else
528cd1b00ebSThomas Abraham static int __devinit sdhci_s3c_parse_dt(struct device *dev,
529cd1b00ebSThomas Abraham 		struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
530cd1b00ebSThomas Abraham {
531cd1b00ebSThomas Abraham 	return -EINVAL;
532cd1b00ebSThomas Abraham }
533cd1b00ebSThomas Abraham #endif
534cd1b00ebSThomas Abraham 
535cd1b00ebSThomas Abraham static const struct of_device_id sdhci_s3c_dt_match[];
536cd1b00ebSThomas Abraham 
5373119936aSThomas Abraham static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
5383119936aSThomas Abraham 			struct platform_device *pdev)
5393119936aSThomas Abraham {
540cd1b00ebSThomas Abraham #ifdef CONFIG_OF
541cd1b00ebSThomas Abraham 	if (pdev->dev.of_node) {
542cd1b00ebSThomas Abraham 		const struct of_device_id *match;
543cd1b00ebSThomas Abraham 		match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
544cd1b00ebSThomas Abraham 		return (struct sdhci_s3c_drv_data *)match->data;
545cd1b00ebSThomas Abraham 	}
546cd1b00ebSThomas Abraham #endif
5473119936aSThomas Abraham 	return (struct sdhci_s3c_drv_data *)
5483119936aSThomas Abraham 			platform_get_device_id(pdev)->driver_data;
5493119936aSThomas Abraham }
5503119936aSThomas Abraham 
5510d1bb41aSBen Dooks static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
5520d1bb41aSBen Dooks {
5531d4dc338SThomas Abraham 	struct s3c_sdhci_platdata *pdata;
5543119936aSThomas Abraham 	struct sdhci_s3c_drv_data *drv_data;
5550d1bb41aSBen Dooks 	struct device *dev = &pdev->dev;
5560d1bb41aSBen Dooks 	struct sdhci_host *host;
5570d1bb41aSBen Dooks 	struct sdhci_s3c *sc;
5580d1bb41aSBen Dooks 	struct resource *res;
5590d1bb41aSBen Dooks 	int ret, irq, ptr, clks;
5600d1bb41aSBen Dooks 
561cd1b00ebSThomas Abraham 	if (!pdev->dev.platform_data && !pdev->dev.of_node) {
5620d1bb41aSBen Dooks 		dev_err(dev, "no device data specified\n");
5630d1bb41aSBen Dooks 		return -ENOENT;
5640d1bb41aSBen Dooks 	}
5650d1bb41aSBen Dooks 
5660d1bb41aSBen Dooks 	irq = platform_get_irq(pdev, 0);
5670d1bb41aSBen Dooks 	if (irq < 0) {
5680d1bb41aSBen Dooks 		dev_err(dev, "no irq specified\n");
5690d1bb41aSBen Dooks 		return irq;
5700d1bb41aSBen Dooks 	}
5710d1bb41aSBen Dooks 
5720d1bb41aSBen Dooks 	host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
5730d1bb41aSBen Dooks 	if (IS_ERR(host)) {
5740d1bb41aSBen Dooks 		dev_err(dev, "sdhci_alloc_host() failed\n");
5750d1bb41aSBen Dooks 		return PTR_ERR(host);
5760d1bb41aSBen Dooks 	}
577cd1b00ebSThomas Abraham 	sc = sdhci_priv(host);
5780d1bb41aSBen Dooks 
5791d4dc338SThomas Abraham 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
5801d4dc338SThomas Abraham 	if (!pdata) {
5811d4dc338SThomas Abraham 		ret = -ENOMEM;
582cd1b00ebSThomas Abraham 		goto err_pdata;
5831d4dc338SThomas Abraham 	}
584cd1b00ebSThomas Abraham 
585cd1b00ebSThomas Abraham 	if (pdev->dev.of_node) {
586cd1b00ebSThomas Abraham 		ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
587cd1b00ebSThomas Abraham 		if (ret)
588cd1b00ebSThomas Abraham 			goto err_pdata;
589cd1b00ebSThomas Abraham 	} else {
5901d4dc338SThomas Abraham 		memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
591cd1b00ebSThomas Abraham 		sc->ext_cd_gpio = -1; /* invalid gpio number */
592cd1b00ebSThomas Abraham 	}
5931d4dc338SThomas Abraham 
5943119936aSThomas Abraham 	drv_data = sdhci_s3c_get_driver_data(pdev);
5950d1bb41aSBen Dooks 
5960d1bb41aSBen Dooks 	sc->host = host;
5970d1bb41aSBen Dooks 	sc->pdev = pdev;
5980d1bb41aSBen Dooks 	sc->pdata = pdata;
5990d1bb41aSBen Dooks 
6000d1bb41aSBen Dooks 	platform_set_drvdata(pdev, host);
6010d1bb41aSBen Dooks 
6020d1bb41aSBen Dooks 	sc->clk_io = clk_get(dev, "hsmmc");
6030d1bb41aSBen Dooks 	if (IS_ERR(sc->clk_io)) {
6040d1bb41aSBen Dooks 		dev_err(dev, "failed to get io clock\n");
6050d1bb41aSBen Dooks 		ret = PTR_ERR(sc->clk_io);
6060d1bb41aSBen Dooks 		goto err_io_clk;
6070d1bb41aSBen Dooks 	}
6080d1bb41aSBen Dooks 
6090d1bb41aSBen Dooks 	/* enable the local io clock and keep it running for the moment. */
6100f310a05SThomas Abraham 	clk_prepare_enable(sc->clk_io);
6110d1bb41aSBen Dooks 
6120d1bb41aSBen Dooks 	for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
6130d1bb41aSBen Dooks 		struct clk *clk;
6144346b6d9SRajeshwari Shinde 		char name[14];
6150d1bb41aSBen Dooks 
6164346b6d9SRajeshwari Shinde 		snprintf(name, 14, "mmc_busclk.%d", ptr);
6170d1bb41aSBen Dooks 		clk = clk_get(dev, name);
6182ad0b249SJingoo Han 		if (IS_ERR(clk))
6190d1bb41aSBen Dooks 			continue;
6200d1bb41aSBen Dooks 
6210d1bb41aSBen Dooks 		clks++;
6220d1bb41aSBen Dooks 		sc->clk_bus[ptr] = clk;
623253e0a7cSJeongbae Seo 
624253e0a7cSJeongbae Seo 		/*
625253e0a7cSJeongbae Seo 		 * save current clock index to know which clock bus
626253e0a7cSJeongbae Seo 		 * is used later in overriding functions.
627253e0a7cSJeongbae Seo 		 */
628253e0a7cSJeongbae Seo 		sc->cur_clk = ptr;
629253e0a7cSJeongbae Seo 
6300d1bb41aSBen Dooks 		dev_info(dev, "clock source %d: %s (%ld Hz)\n",
6310d1bb41aSBen Dooks 			 ptr, name, clk_get_rate(clk));
6320d1bb41aSBen Dooks 	}
6330d1bb41aSBen Dooks 
6340d1bb41aSBen Dooks 	if (clks == 0) {
6350d1bb41aSBen Dooks 		dev_err(dev, "failed to find any bus clocks\n");
6360d1bb41aSBen Dooks 		ret = -ENOENT;
6370d1bb41aSBen Dooks 		goto err_no_busclks;
6380d1bb41aSBen Dooks 	}
6390d1bb41aSBen Dooks 
6402abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME
6410f310a05SThomas Abraham 	clk_prepare_enable(sc->clk_bus[sc->cur_clk]);
6422abeb5c5SChander Kashyap #endif
643e684c468SChander Kashyap 
6449bda6da7SJulia Lawall 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
6459bda6da7SJulia Lawall 	host->ioaddr = devm_request_and_ioremap(&pdev->dev, res);
6460d1bb41aSBen Dooks 	if (!host->ioaddr) {
6470d1bb41aSBen Dooks 		dev_err(dev, "failed to map registers\n");
6480d1bb41aSBen Dooks 		ret = -ENXIO;
6490d1bb41aSBen Dooks 		goto err_req_regs;
6500d1bb41aSBen Dooks 	}
6510d1bb41aSBen Dooks 
6520d1bb41aSBen Dooks 	/* Ensure we have minimal gpio selected CMD/CLK/Detect */
6530d1bb41aSBen Dooks 	if (pdata->cfg_gpio)
6540d1bb41aSBen Dooks 		pdata->cfg_gpio(pdev, pdata->max_width);
6550d1bb41aSBen Dooks 
6560d1bb41aSBen Dooks 	host->hw_name = "samsung-hsmmc";
6570d1bb41aSBen Dooks 	host->ops = &sdhci_s3c_ops;
6580d1bb41aSBen Dooks 	host->quirks = 0;
6590d1bb41aSBen Dooks 	host->irq = irq;
6600d1bb41aSBen Dooks 
6610d1bb41aSBen Dooks 	/* Setup quirks for the controller */
662b2e75effSThomas Abraham 	host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
663a1d56460SMarek Szyprowski 	host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
6643119936aSThomas Abraham 	if (drv_data)
6653119936aSThomas Abraham 		host->quirks |= drv_data->sdhci_quirks;
6660d1bb41aSBen Dooks 
6670d1bb41aSBen Dooks #ifndef CONFIG_MMC_SDHCI_S3C_DMA
6680d1bb41aSBen Dooks 
6690d1bb41aSBen Dooks 	/* we currently see overruns on errors, so disable the SDMA
6700d1bb41aSBen Dooks 	 * support as well. */
6710d1bb41aSBen Dooks 	host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
6720d1bb41aSBen Dooks 
6730d1bb41aSBen Dooks #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
6740d1bb41aSBen Dooks 
6750d1bb41aSBen Dooks 	/* It seems we do not get an DATA transfer complete on non-busy
6760d1bb41aSBen Dooks 	 * transfers, not sure if this is a problem with this specific
6770d1bb41aSBen Dooks 	 * SDHCI block, or a missing configuration that needs to be set. */
6780d1bb41aSBen Dooks 	host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
6790d1bb41aSBen Dooks 
680732f0e31SKyungmin Park 	/* This host supports the Auto CMD12 */
681732f0e31SKyungmin Park 	host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
682732f0e31SKyungmin Park 
6837199e2b6SJaehoon Chung 	/* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
6847199e2b6SJaehoon Chung 	host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
6857199e2b6SJaehoon Chung 
68617866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
68717866e14SMarek Szyprowski 	    pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
68817866e14SMarek Szyprowski 		host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
68917866e14SMarek Szyprowski 
69017866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
69117866e14SMarek Szyprowski 		host->mmc->caps = MMC_CAP_NONREMOVABLE;
69217866e14SMarek Szyprowski 
6930d22c770SThomas Abraham 	switch (pdata->max_width) {
6940d22c770SThomas Abraham 	case 8:
6950d22c770SThomas Abraham 		host->mmc->caps |= MMC_CAP_8_BIT_DATA;
6960d22c770SThomas Abraham 	case 4:
6970d22c770SThomas Abraham 		host->mmc->caps |= MMC_CAP_4_BIT_DATA;
6980d22c770SThomas Abraham 		break;
6990d22c770SThomas Abraham 	}
7000d22c770SThomas Abraham 
701fa1773ccSSangwook Lee 	if (pdata->pm_caps)
702fa1773ccSSangwook Lee 		host->mmc->pm_caps |= pdata->pm_caps;
703fa1773ccSSangwook Lee 
7040d1bb41aSBen Dooks 	host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
7050d1bb41aSBen Dooks 			 SDHCI_QUIRK_32BIT_DMA_SIZE);
7060d1bb41aSBen Dooks 
7073fe42e07SHyuk Lee 	/* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
7083fe42e07SHyuk Lee 	host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
7093fe42e07SHyuk Lee 
710253e0a7cSJeongbae Seo 	/*
711253e0a7cSJeongbae Seo 	 * If controller does not have internal clock divider,
712253e0a7cSJeongbae Seo 	 * we can use overriding functions instead of default.
713253e0a7cSJeongbae Seo 	 */
7143119936aSThomas Abraham 	if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
715253e0a7cSJeongbae Seo 		sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
716253e0a7cSJeongbae Seo 		sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
717253e0a7cSJeongbae Seo 		sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
718253e0a7cSJeongbae Seo 	}
719253e0a7cSJeongbae Seo 
720b3824f2cSJeongbae Seo 	/* It supports additional host capabilities if needed */
721b3824f2cSJeongbae Seo 	if (pdata->host_caps)
722b3824f2cSJeongbae Seo 		host->mmc->caps |= pdata->host_caps;
723b3824f2cSJeongbae Seo 
724c1c4b66dSJaehoon Chung 	if (pdata->host_caps2)
725c1c4b66dSJaehoon Chung 		host->mmc->caps2 |= pdata->host_caps2;
726c1c4b66dSJaehoon Chung 
7279f4e8151SMark Brown 	pm_runtime_enable(&pdev->dev);
7289f4e8151SMark Brown 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
7299f4e8151SMark Brown 	pm_runtime_use_autosuspend(&pdev->dev);
7309f4e8151SMark Brown 	pm_suspend_ignore_children(&pdev->dev, 1);
7319f4e8151SMark Brown 
7320d1bb41aSBen Dooks 	ret = sdhci_add_host(host);
7330d1bb41aSBen Dooks 	if (ret) {
7340d1bb41aSBen Dooks 		dev_err(dev, "sdhci_add_host() failed\n");
7359f4e8151SMark Brown 		pm_runtime_forbid(&pdev->dev);
7369f4e8151SMark Brown 		pm_runtime_get_noresume(&pdev->dev);
7379bda6da7SJulia Lawall 		goto err_req_regs;
7380d1bb41aSBen Dooks 	}
7390d1bb41aSBen Dooks 
74017866e14SMarek Szyprowski 	/* The following two methods of card detection might call
74117866e14SMarek Szyprowski 	   sdhci_s3c_notify_change() immediately, so they can be called
74217866e14SMarek Szyprowski 	   only after sdhci_add_host(). Setup errors are ignored. */
74317866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
74417866e14SMarek Szyprowski 		pdata->ext_cd_init(&sdhci_s3c_notify_change);
74517866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
74617866e14SMarek Szyprowski 	    gpio_is_valid(pdata->ext_cd_gpio))
74717866e14SMarek Szyprowski 		sdhci_s3c_setup_card_detect_gpio(sc);
74817866e14SMarek Szyprowski 
7492abeb5c5SChander Kashyap #ifdef CONFIG_PM_RUNTIME
7500f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_io);
7512abeb5c5SChander Kashyap #endif
7520d1bb41aSBen Dooks 	return 0;
7530d1bb41aSBen Dooks 
7540d1bb41aSBen Dooks  err_req_regs:
7552abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME
7560f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_bus[sc->cur_clk]);
7572abeb5c5SChander Kashyap #endif
7580d1bb41aSBen Dooks 	for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
759326adda5SJaehoon Chung 		if (sc->clk_bus[ptr]) {
7600d1bb41aSBen Dooks 			clk_put(sc->clk_bus[ptr]);
7610d1bb41aSBen Dooks 		}
762326adda5SJaehoon Chung 	}
7630d1bb41aSBen Dooks 
7640d1bb41aSBen Dooks  err_no_busclks:
7650f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_io);
7660d1bb41aSBen Dooks 	clk_put(sc->clk_io);
7670d1bb41aSBen Dooks 
7680d1bb41aSBen Dooks  err_io_clk:
769cd1b00ebSThomas Abraham 	for (ptr = 0; ptr < NUM_GPIOS(sc->pdata->max_width); ptr++)
770cd1b00ebSThomas Abraham 		gpio_free(sc->gpios[ptr]);
771cd1b00ebSThomas Abraham 	if (pdata->cd_type == S3C_SDHCI_CD_INTERNAL)
772cd1b00ebSThomas Abraham 		gpio_free(sc->ext_cd_gpio);
773cd1b00ebSThomas Abraham 
774cd1b00ebSThomas Abraham  err_pdata:
7750d1bb41aSBen Dooks 	sdhci_free_host(host);
7760d1bb41aSBen Dooks 
7770d1bb41aSBen Dooks 	return ret;
7780d1bb41aSBen Dooks }
7790d1bb41aSBen Dooks 
7800d1bb41aSBen Dooks static int __devexit sdhci_s3c_remove(struct platform_device *pdev)
7810d1bb41aSBen Dooks {
7829d51a6b2SMarek Szyprowski 	struct sdhci_host *host =  platform_get_drvdata(pdev);
7839d51a6b2SMarek Szyprowski 	struct sdhci_s3c *sc = sdhci_priv(host);
784cd1b00ebSThomas Abraham 	struct s3c_sdhci_platdata *pdata = sc->pdata;
7859d51a6b2SMarek Szyprowski 	int ptr;
7869d51a6b2SMarek Szyprowski 
78717866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
78817866e14SMarek Szyprowski 		pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
78917866e14SMarek Szyprowski 
79017866e14SMarek Szyprowski 	if (sc->ext_cd_irq)
79117866e14SMarek Szyprowski 		free_irq(sc->ext_cd_irq, sc);
79217866e14SMarek Szyprowski 
79317866e14SMarek Szyprowski 	if (gpio_is_valid(sc->ext_cd_gpio))
79417866e14SMarek Szyprowski 		gpio_free(sc->ext_cd_gpio);
79517866e14SMarek Szyprowski 
7962abeb5c5SChander Kashyap #ifdef CONFIG_PM_RUNTIME
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 
815cd1b00ebSThomas Abraham 	if (pdev->dev.of_node) {
816cd1b00ebSThomas Abraham 		for (ptr = 0; ptr < NUM_GPIOS(sc->pdata->max_width); ptr++)
817cd1b00ebSThomas Abraham 			gpio_free(sc->gpios[ptr]);
818cd1b00ebSThomas Abraham 	}
819cd1b00ebSThomas Abraham 
8209d51a6b2SMarek Szyprowski 	sdhci_free_host(host);
8219d51a6b2SMarek Szyprowski 	platform_set_drvdata(pdev, NULL);
8229d51a6b2SMarek Szyprowski 
8230d1bb41aSBen Dooks 	return 0;
8240d1bb41aSBen Dooks }
8250d1bb41aSBen Dooks 
826d5e9c02cSMark Brown #ifdef CONFIG_PM_SLEEP
82729495aa0SManuel Lauss static int sdhci_s3c_suspend(struct device *dev)
8280d1bb41aSBen Dooks {
82929495aa0SManuel Lauss 	struct sdhci_host *host = dev_get_drvdata(dev);
8300d1bb41aSBen Dooks 
83129495aa0SManuel Lauss 	return sdhci_suspend_host(host);
8320d1bb41aSBen Dooks }
8330d1bb41aSBen Dooks 
83429495aa0SManuel Lauss static int sdhci_s3c_resume(struct device *dev)
8350d1bb41aSBen Dooks {
83629495aa0SManuel Lauss 	struct sdhci_host *host = dev_get_drvdata(dev);
8370d1bb41aSBen Dooks 
83865d13516SWonil Choi 	return sdhci_resume_host(host);
8390d1bb41aSBen Dooks }
840d5e9c02cSMark Brown #endif
8410d1bb41aSBen Dooks 
8429f4e8151SMark Brown #ifdef CONFIG_PM_RUNTIME
8439f4e8151SMark Brown static int sdhci_s3c_runtime_suspend(struct device *dev)
8449f4e8151SMark Brown {
8459f4e8151SMark Brown 	struct sdhci_host *host = dev_get_drvdata(dev);
8462abeb5c5SChander Kashyap 	struct sdhci_s3c *ourhost = to_s3c(host);
8472abeb5c5SChander Kashyap 	struct clk *busclk = ourhost->clk_io;
8482abeb5c5SChander Kashyap 	int ret;
8499f4e8151SMark Brown 
8502abeb5c5SChander Kashyap 	ret = sdhci_runtime_suspend_host(host);
8512abeb5c5SChander Kashyap 
8520f310a05SThomas Abraham 	clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
8530f310a05SThomas Abraham 	clk_disable_unprepare(busclk);
8542abeb5c5SChander Kashyap 	return ret;
8559f4e8151SMark Brown }
8569f4e8151SMark Brown 
8579f4e8151SMark Brown static int sdhci_s3c_runtime_resume(struct device *dev)
8589f4e8151SMark Brown {
8599f4e8151SMark Brown 	struct sdhci_host *host = dev_get_drvdata(dev);
8602abeb5c5SChander Kashyap 	struct sdhci_s3c *ourhost = to_s3c(host);
8612abeb5c5SChander Kashyap 	struct clk *busclk = ourhost->clk_io;
8622abeb5c5SChander Kashyap 	int ret;
8639f4e8151SMark Brown 
8640f310a05SThomas Abraham 	clk_prepare_enable(busclk);
8650f310a05SThomas Abraham 	clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
8662abeb5c5SChander Kashyap 	ret = sdhci_runtime_resume_host(host);
8672abeb5c5SChander Kashyap 	return ret;
8689f4e8151SMark Brown }
8699f4e8151SMark Brown #endif
8709f4e8151SMark Brown 
871d5e9c02cSMark Brown #ifdef CONFIG_PM
87229495aa0SManuel Lauss static const struct dev_pm_ops sdhci_s3c_pmops = {
873d5e9c02cSMark Brown 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
8749f4e8151SMark Brown 	SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
8759f4e8151SMark Brown 			   NULL)
87629495aa0SManuel Lauss };
87729495aa0SManuel Lauss 
87829495aa0SManuel Lauss #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
87929495aa0SManuel Lauss 
8800d1bb41aSBen Dooks #else
88129495aa0SManuel Lauss #define SDHCI_S3C_PMOPS NULL
8820d1bb41aSBen Dooks #endif
8830d1bb41aSBen Dooks 
8843119936aSThomas Abraham #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
8853119936aSThomas Abraham static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
8863119936aSThomas Abraham 	.sdhci_quirks = SDHCI_QUIRK_NONSTANDARD_CLOCK,
8873119936aSThomas Abraham };
8883119936aSThomas Abraham #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
8893119936aSThomas Abraham #else
8903119936aSThomas Abraham #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
8913119936aSThomas Abraham #endif
8923119936aSThomas Abraham 
8933119936aSThomas Abraham static struct platform_device_id sdhci_s3c_driver_ids[] = {
8943119936aSThomas Abraham 	{
8953119936aSThomas Abraham 		.name		= "s3c-sdhci",
8963119936aSThomas Abraham 		.driver_data	= (kernel_ulong_t)NULL,
8973119936aSThomas Abraham 	}, {
8983119936aSThomas Abraham 		.name		= "exynos4-sdhci",
8993119936aSThomas Abraham 		.driver_data	= EXYNOS4_SDHCI_DRV_DATA,
9003119936aSThomas Abraham 	},
9013119936aSThomas Abraham 	{ }
9023119936aSThomas Abraham };
9033119936aSThomas Abraham MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
9043119936aSThomas Abraham 
905cd1b00ebSThomas Abraham #ifdef CONFIG_OF
906cd1b00ebSThomas Abraham static const struct of_device_id sdhci_s3c_dt_match[] = {
907cd1b00ebSThomas Abraham 	{ .compatible = "samsung,s3c6410-sdhci", },
908cd1b00ebSThomas Abraham 	{ .compatible = "samsung,exynos4210-sdhci",
909cd1b00ebSThomas Abraham 		.data = (void *)EXYNOS4_SDHCI_DRV_DATA },
910cd1b00ebSThomas Abraham 	{},
911cd1b00ebSThomas Abraham };
912cd1b00ebSThomas Abraham MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
913cd1b00ebSThomas Abraham #endif
914cd1b00ebSThomas Abraham 
9150d1bb41aSBen Dooks static struct platform_driver sdhci_s3c_driver = {
9160d1bb41aSBen Dooks 	.probe		= sdhci_s3c_probe,
9170d1bb41aSBen Dooks 	.remove		= __devexit_p(sdhci_s3c_remove),
9183119936aSThomas Abraham 	.id_table	= sdhci_s3c_driver_ids,
9190d1bb41aSBen Dooks 	.driver		= {
9200d1bb41aSBen Dooks 		.owner	= THIS_MODULE,
9210d1bb41aSBen Dooks 		.name	= "s3c-sdhci",
922cd1b00ebSThomas Abraham 		.of_match_table = of_match_ptr(sdhci_s3c_dt_match),
92329495aa0SManuel Lauss 		.pm	= SDHCI_S3C_PMOPS,
9240d1bb41aSBen Dooks 	},
9250d1bb41aSBen Dooks };
9260d1bb41aSBen Dooks 
927d1f81a64SAxel Lin module_platform_driver(sdhci_s3c_driver);
9280d1bb41aSBen Dooks 
9290d1bb41aSBen Dooks MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
9300d1bb41aSBen Dooks MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
9310d1bb41aSBen Dooks MODULE_LICENSE("GPL v2");
9320d1bb41aSBen Dooks MODULE_ALIAS("platform:s3c-sdhci");
933