xref: /openbmc/linux/drivers/mmc/host/sdhci-s3c.c (revision 222a13c5)
10d1bb41aSBen Dooks /* linux/drivers/mmc/host/sdhci-s3c.c
20d1bb41aSBen Dooks  *
30d1bb41aSBen Dooks  * Copyright 2008 Openmoko Inc.
40d1bb41aSBen Dooks  * Copyright 2008 Simtec Electronics
50d1bb41aSBen Dooks  *      Ben Dooks <ben@simtec.co.uk>
60d1bb41aSBen Dooks  *      http://armlinux.simtec.co.uk/
70d1bb41aSBen Dooks  *
80d1bb41aSBen Dooks  * SDHCI (HSMMC) support for Samsung SoC
90d1bb41aSBen Dooks  *
100d1bb41aSBen Dooks  * This program is free software; you can redistribute it and/or modify
110d1bb41aSBen Dooks  * it under the terms of the GNU General Public License version 2 as
120d1bb41aSBen Dooks  * published by the Free Software Foundation.
130d1bb41aSBen Dooks  */
140d1bb41aSBen Dooks 
150d1bb41aSBen Dooks #include <linux/delay.h>
160d1bb41aSBen Dooks #include <linux/dma-mapping.h>
170d1bb41aSBen Dooks #include <linux/platform_device.h>
18cc014f3eSArnd Bergmann #include <linux/platform_data/mmc-sdhci-s3c.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
200d1bb41aSBen Dooks #include <linux/clk.h>
210d1bb41aSBen Dooks #include <linux/io.h>
2217866e14SMarek Szyprowski #include <linux/gpio.h>
2355156d24SMark Brown #include <linux/module.h>
24d5e9c02cSMark Brown #include <linux/of.h>
25d5e9c02cSMark Brown #include <linux/of_gpio.h>
26d5e9c02cSMark Brown #include <linux/pm.h>
279f4e8151SMark Brown #include <linux/pm_runtime.h>
280d1bb41aSBen Dooks 
290d1bb41aSBen Dooks #include <linux/mmc/host.h>
300d1bb41aSBen Dooks 
31cc014f3eSArnd Bergmann #include "sdhci-s3c-regs.h"
320d1bb41aSBen Dooks #include "sdhci.h"
330d1bb41aSBen Dooks 
340d1bb41aSBen Dooks #define MAX_BUS_CLK	(4)
350d1bb41aSBen Dooks 
36cd1b00ebSThomas Abraham /* Number of gpio's used is max data bus width + command and clock lines */
37cd1b00ebSThomas Abraham #define NUM_GPIOS(x)	(x + 2)
38cd1b00ebSThomas Abraham 
390d1bb41aSBen Dooks /**
400d1bb41aSBen Dooks  * struct sdhci_s3c - S3C SDHCI instance
410d1bb41aSBen Dooks  * @host: The SDHCI host created
420d1bb41aSBen Dooks  * @pdev: The platform device we where created from.
430d1bb41aSBen Dooks  * @ioarea: The resource created when we claimed the IO area.
440d1bb41aSBen Dooks  * @pdata: The platform data for this controller.
450d1bb41aSBen Dooks  * @cur_clk: The index of the current bus clock.
460d1bb41aSBen Dooks  * @clk_io: The clock for the internal bus interface.
470d1bb41aSBen Dooks  * @clk_bus: The clocks that are available for the SD/MMC bus clock.
480d1bb41aSBen Dooks  */
490d1bb41aSBen Dooks struct sdhci_s3c {
500d1bb41aSBen Dooks 	struct sdhci_host	*host;
510d1bb41aSBen Dooks 	struct platform_device	*pdev;
520d1bb41aSBen Dooks 	struct resource		*ioarea;
530d1bb41aSBen Dooks 	struct s3c_sdhci_platdata *pdata;
540d1bb41aSBen Dooks 	unsigned int		cur_clk;
5517866e14SMarek Szyprowski 	int			ext_cd_irq;
5617866e14SMarek Szyprowski 	int			ext_cd_gpio;
570d1bb41aSBen Dooks 
580d1bb41aSBen Dooks 	struct clk		*clk_io;
590d1bb41aSBen Dooks 	struct clk		*clk_bus[MAX_BUS_CLK];
606eb28bdcSTomasz Figa 	unsigned long		clk_rates[MAX_BUS_CLK];
610d1bb41aSBen Dooks };
620d1bb41aSBen Dooks 
633119936aSThomas Abraham /**
643119936aSThomas Abraham  * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
653119936aSThomas Abraham  * @sdhci_quirks: sdhci host specific quirks.
663119936aSThomas Abraham  *
673119936aSThomas Abraham  * Specifies platform specific configuration of sdhci controller.
683119936aSThomas Abraham  * Note: A structure for driver specific platform data is used for future
693119936aSThomas Abraham  * expansion of its usage.
703119936aSThomas Abraham  */
713119936aSThomas Abraham struct sdhci_s3c_drv_data {
723119936aSThomas Abraham 	unsigned int	sdhci_quirks;
733119936aSThomas Abraham };
743119936aSThomas Abraham 
750d1bb41aSBen Dooks static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
760d1bb41aSBen Dooks {
770d1bb41aSBen Dooks 	return sdhci_priv(host);
780d1bb41aSBen Dooks }
790d1bb41aSBen Dooks 
800d1bb41aSBen Dooks /**
810d1bb41aSBen Dooks  * get_curclk - convert ctrl2 register to clock source number
820d1bb41aSBen Dooks  * @ctrl2: Control2 register value.
830d1bb41aSBen Dooks  */
840d1bb41aSBen Dooks static u32 get_curclk(u32 ctrl2)
850d1bb41aSBen Dooks {
860d1bb41aSBen Dooks 	ctrl2 &= S3C_SDHCI_CTRL2_SELBASECLK_MASK;
870d1bb41aSBen Dooks 	ctrl2 >>= S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
880d1bb41aSBen Dooks 
890d1bb41aSBen Dooks 	return ctrl2;
900d1bb41aSBen Dooks }
910d1bb41aSBen Dooks 
920d1bb41aSBen Dooks static void sdhci_s3c_check_sclk(struct sdhci_host *host)
930d1bb41aSBen Dooks {
940d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
950d1bb41aSBen Dooks 	u32 tmp = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
960d1bb41aSBen Dooks 
970d1bb41aSBen Dooks 	if (get_curclk(tmp) != ourhost->cur_clk) {
980d1bb41aSBen Dooks 		dev_dbg(&ourhost->pdev->dev, "restored ctrl2 clock setting\n");
990d1bb41aSBen Dooks 
1000d1bb41aSBen Dooks 		tmp &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
1010d1bb41aSBen Dooks 		tmp |= ourhost->cur_clk << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
1027003fecbSJingoo Han 		writel(tmp, host->ioaddr + S3C_SDHCI_CONTROL2);
1030d1bb41aSBen Dooks 	}
1040d1bb41aSBen Dooks }
1050d1bb41aSBen Dooks 
1060d1bb41aSBen Dooks /**
1070d1bb41aSBen Dooks  * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
1080d1bb41aSBen Dooks  * @host: The SDHCI host instance.
1090d1bb41aSBen Dooks  *
1100d1bb41aSBen Dooks  * Callback to return the maximum clock rate acheivable by the controller.
1110d1bb41aSBen Dooks */
1120d1bb41aSBen Dooks static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
1130d1bb41aSBen Dooks {
1140d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
115222a13c5STomasz Figa 	unsigned long rate, max = 0;
116222a13c5STomasz Figa 	int src;
1170d1bb41aSBen Dooks 
1180d1bb41aSBen Dooks 	/* note, a reset will reset the clock source */
1190d1bb41aSBen Dooks 
1200d1bb41aSBen Dooks 	sdhci_s3c_check_sclk(host);
1210d1bb41aSBen Dooks 
1220d1bb41aSBen Dooks 
123222a13c5STomasz Figa 	for (src = 0; src < MAX_BUS_CLK; src++) {
124222a13c5STomasz Figa 		rate = ourhost->clk_rates[src];
1250d1bb41aSBen Dooks 		if (rate > max)
1260d1bb41aSBen Dooks 			max = rate;
1270d1bb41aSBen Dooks 	}
1280d1bb41aSBen Dooks 
1290d1bb41aSBen Dooks 	return max;
1300d1bb41aSBen Dooks }
1310d1bb41aSBen Dooks 
1320d1bb41aSBen Dooks /**
1330d1bb41aSBen Dooks  * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
1340d1bb41aSBen Dooks  * @ourhost: Our SDHCI instance.
1350d1bb41aSBen Dooks  * @src: The source clock index.
1360d1bb41aSBen Dooks  * @wanted: The clock frequency wanted.
1370d1bb41aSBen Dooks  */
1380d1bb41aSBen Dooks static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
1390d1bb41aSBen Dooks 					     unsigned int src,
1400d1bb41aSBen Dooks 					     unsigned int wanted)
1410d1bb41aSBen Dooks {
1420d1bb41aSBen Dooks 	unsigned long rate;
1430d1bb41aSBen Dooks 	struct clk *clksrc = ourhost->clk_bus[src];
1448880a4a5STomasz Figa 	int shift;
1450d1bb41aSBen Dooks 
1468f4b78d9STomasz Figa 	if (IS_ERR(clksrc))
1470d1bb41aSBen Dooks 		return UINT_MAX;
1480d1bb41aSBen Dooks 
149253e0a7cSJeongbae Seo 	/*
1503119936aSThomas Abraham 	 * If controller uses a non-standard clock division, find the best clock
1513119936aSThomas Abraham 	 * speed possible with selected clock source and skip the division.
152253e0a7cSJeongbae Seo 	 */
1533119936aSThomas Abraham 	if (ourhost->host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
154253e0a7cSJeongbae Seo 		rate = clk_round_rate(clksrc, wanted);
155253e0a7cSJeongbae Seo 		return wanted - rate;
156253e0a7cSJeongbae Seo 	}
157253e0a7cSJeongbae Seo 
1586eb28bdcSTomasz Figa 	rate = ourhost->clk_rates[src];
1590d1bb41aSBen Dooks 
1608880a4a5STomasz Figa 	for (shift = 0; shift < 8; ++shift) {
1618880a4a5STomasz Figa 		if ((rate >> shift) <= wanted)
1620d1bb41aSBen Dooks 			break;
1630d1bb41aSBen Dooks 	}
1640d1bb41aSBen Dooks 
1650d1bb41aSBen Dooks 	dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
1668880a4a5STomasz Figa 		src, rate, wanted, rate >> shift);
1670d1bb41aSBen Dooks 
1688880a4a5STomasz Figa 	return wanted - (rate >> shift);
1690d1bb41aSBen Dooks }
1700d1bb41aSBen Dooks 
1710d1bb41aSBen Dooks /**
1720d1bb41aSBen Dooks  * sdhci_s3c_set_clock - callback on clock change
1730d1bb41aSBen Dooks  * @host: The SDHCI host being changed
1740d1bb41aSBen Dooks  * @clock: The clock rate being requested.
1750d1bb41aSBen Dooks  *
1760d1bb41aSBen Dooks  * When the card's clock is going to be changed, look at the new frequency
1770d1bb41aSBen Dooks  * and find the best clock source to go with it.
1780d1bb41aSBen Dooks */
1790d1bb41aSBen Dooks static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
1800d1bb41aSBen Dooks {
1810d1bb41aSBen Dooks 	struct sdhci_s3c *ourhost = to_s3c(host);
1820d1bb41aSBen Dooks 	unsigned int best = UINT_MAX;
1830d1bb41aSBen Dooks 	unsigned int delta;
1840d1bb41aSBen Dooks 	int best_src = 0;
1850d1bb41aSBen Dooks 	int src;
1860d1bb41aSBen Dooks 	u32 ctrl;
1870d1bb41aSBen Dooks 
1880d1bb41aSBen Dooks 	/* don't bother if the clock is going off. */
1890d1bb41aSBen Dooks 	if (clock == 0)
1900d1bb41aSBen Dooks 		return;
1910d1bb41aSBen Dooks 
1920d1bb41aSBen Dooks 	for (src = 0; src < MAX_BUS_CLK; src++) {
1930d1bb41aSBen Dooks 		delta = sdhci_s3c_consider_clock(ourhost, src, clock);
1940d1bb41aSBen Dooks 		if (delta < best) {
1950d1bb41aSBen Dooks 			best = delta;
1960d1bb41aSBen Dooks 			best_src = src;
1970d1bb41aSBen Dooks 		}
1980d1bb41aSBen Dooks 	}
1990d1bb41aSBen Dooks 
2000d1bb41aSBen Dooks 	dev_dbg(&ourhost->pdev->dev,
2010d1bb41aSBen Dooks 		"selected source %d, clock %d, delta %d\n",
2020d1bb41aSBen Dooks 		 best_src, clock, best);
2030d1bb41aSBen Dooks 
2040d1bb41aSBen Dooks 	/* select the new clock source */
2050d1bb41aSBen Dooks 	if (ourhost->cur_clk != best_src) {
2060d1bb41aSBen Dooks 		struct clk *clk = ourhost->clk_bus[best_src];
2070d1bb41aSBen Dooks 
2080f310a05SThomas Abraham 		clk_prepare_enable(clk);
2090f310a05SThomas Abraham 		clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
210e684c468SChander Kashyap 
2110d1bb41aSBen Dooks 		/* turn clock off to card before changing clock source */
2120d1bb41aSBen Dooks 		writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
2130d1bb41aSBen Dooks 
2140d1bb41aSBen Dooks 		ourhost->cur_clk = best_src;
2156eb28bdcSTomasz Figa 		host->max_clk = ourhost->clk_rates[best_src];
2160d1bb41aSBen Dooks 
2170d1bb41aSBen Dooks 		ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
2180d1bb41aSBen Dooks 		ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
2190d1bb41aSBen Dooks 		ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
2200d1bb41aSBen Dooks 		writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
2210d1bb41aSBen Dooks 	}
2220d1bb41aSBen Dooks 
2236fe47179SThomas Abraham 	/* reprogram default hardware configuration */
2246fe47179SThomas Abraham 	writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
2256fe47179SThomas Abraham 		host->ioaddr + S3C64XX_SDHCI_CONTROL4);
2260d1bb41aSBen Dooks 
2276fe47179SThomas Abraham 	ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
2286fe47179SThomas Abraham 	ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
2296fe47179SThomas Abraham 		  S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
2306fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_ENFBCLKRX |
2316fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_DFCNT_NONE |
2326fe47179SThomas Abraham 		  S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
2336fe47179SThomas Abraham 	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
2340d1bb41aSBen Dooks 
2356fe47179SThomas Abraham 	/* reconfigure the controller for new clock rate */
2366fe47179SThomas Abraham 	ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
2376fe47179SThomas Abraham 	if (clock < 25 * 1000000)
2386fe47179SThomas Abraham 		ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
2396fe47179SThomas Abraham 	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
2400d1bb41aSBen Dooks }
2410d1bb41aSBen Dooks 
242ce5f036bSMarek Szyprowski /**
243ce5f036bSMarek Szyprowski  * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
244ce5f036bSMarek Szyprowski  * @host: The SDHCI host being queried
245ce5f036bSMarek Szyprowski  *
246ce5f036bSMarek Szyprowski  * To init mmc host properly a minimal clock value is needed. For high system
247ce5f036bSMarek Szyprowski  * bus clock's values the standard formula gives values out of allowed range.
248ce5f036bSMarek Szyprowski  * The clock still can be set to lower values, if clock source other then
249ce5f036bSMarek Szyprowski  * system bus is selected.
250ce5f036bSMarek Szyprowski */
251ce5f036bSMarek Szyprowski static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
252ce5f036bSMarek Szyprowski {
253ce5f036bSMarek Szyprowski 	struct sdhci_s3c *ourhost = to_s3c(host);
254222a13c5STomasz Figa 	unsigned long rate, min = ULONG_MAX;
255ce5f036bSMarek Szyprowski 	int src;
256ce5f036bSMarek Szyprowski 
257ce5f036bSMarek Szyprowski 	for (src = 0; src < MAX_BUS_CLK; src++) {
258222a13c5STomasz Figa 		rate = ourhost->clk_rates[src] / 256;
259222a13c5STomasz Figa 		if (!rate)
260ce5f036bSMarek Szyprowski 			continue;
261222a13c5STomasz Figa 		if (rate < min)
262222a13c5STomasz Figa 			min = rate;
263ce5f036bSMarek Szyprowski 	}
264222a13c5STomasz Figa 
265ce5f036bSMarek Szyprowski 	return min;
266ce5f036bSMarek Szyprowski }
267ce5f036bSMarek Szyprowski 
268253e0a7cSJeongbae Seo /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
269253e0a7cSJeongbae Seo static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
270253e0a7cSJeongbae Seo {
271253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
272222a13c5STomasz Figa 	unsigned long rate, max = 0;
273222a13c5STomasz Figa 	int src;
274253e0a7cSJeongbae Seo 
275222a13c5STomasz Figa 	for (src = 0; src < MAX_BUS_CLK; src++) {
276222a13c5STomasz Figa 		struct clk *clk;
277222a13c5STomasz Figa 
278222a13c5STomasz Figa 		clk = ourhost->clk_bus[src];
279222a13c5STomasz Figa 		if (IS_ERR(clk))
280222a13c5STomasz Figa 			continue;
281222a13c5STomasz Figa 
282222a13c5STomasz Figa 		rate = clk_round_rate(clk, ULONG_MAX);
283222a13c5STomasz Figa 		if (rate > max)
284222a13c5STomasz Figa 			max = rate;
285222a13c5STomasz Figa 	}
286222a13c5STomasz Figa 
287222a13c5STomasz Figa 	return max;
288253e0a7cSJeongbae Seo }
289253e0a7cSJeongbae Seo 
290253e0a7cSJeongbae Seo /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
291253e0a7cSJeongbae Seo static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
292253e0a7cSJeongbae Seo {
293253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
294222a13c5STomasz Figa 	unsigned long rate, min = ULONG_MAX;
295222a13c5STomasz Figa 	int src;
296253e0a7cSJeongbae Seo 
297222a13c5STomasz Figa 	for (src = 0; src < MAX_BUS_CLK; src++) {
298222a13c5STomasz Figa 		struct clk *clk;
299222a13c5STomasz Figa 
300222a13c5STomasz Figa 		clk = ourhost->clk_bus[src];
301222a13c5STomasz Figa 		if (IS_ERR(clk))
302222a13c5STomasz Figa 			continue;
303222a13c5STomasz Figa 
304222a13c5STomasz Figa 		rate = clk_round_rate(clk, 0);
305222a13c5STomasz Figa 		if (rate < min)
306222a13c5STomasz Figa 			min = rate;
307222a13c5STomasz Figa 	}
308222a13c5STomasz Figa 
309222a13c5STomasz Figa 	return min;
310253e0a7cSJeongbae Seo }
311253e0a7cSJeongbae Seo 
312253e0a7cSJeongbae Seo /* sdhci_cmu_set_clock - callback on clock change.*/
313253e0a7cSJeongbae Seo static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
314253e0a7cSJeongbae Seo {
315253e0a7cSJeongbae Seo 	struct sdhci_s3c *ourhost = to_s3c(host);
3162ad0b249SJingoo Han 	struct device *dev = &ourhost->pdev->dev;
3173119936aSThomas Abraham 	unsigned long timeout;
3183119936aSThomas Abraham 	u16 clk = 0;
319253e0a7cSJeongbae Seo 
3207ef2a5e2SJaehoon Chung 	/* If the clock is going off, set to 0 at clock control register */
3217ef2a5e2SJaehoon Chung 	if (clock == 0) {
3227ef2a5e2SJaehoon Chung 		sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
3237ef2a5e2SJaehoon Chung 		host->clock = clock;
324253e0a7cSJeongbae Seo 		return;
3257ef2a5e2SJaehoon Chung 	}
326253e0a7cSJeongbae Seo 
327253e0a7cSJeongbae Seo 	sdhci_s3c_set_clock(host, clock);
328253e0a7cSJeongbae Seo 
329253e0a7cSJeongbae Seo 	clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
330253e0a7cSJeongbae Seo 
331253e0a7cSJeongbae Seo 	host->clock = clock;
3323119936aSThomas Abraham 
3333119936aSThomas Abraham 	clk = SDHCI_CLOCK_INT_EN;
3343119936aSThomas Abraham 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
3353119936aSThomas Abraham 
3363119936aSThomas Abraham 	/* Wait max 20 ms */
3373119936aSThomas Abraham 	timeout = 20;
3383119936aSThomas Abraham 	while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
3393119936aSThomas Abraham 		& SDHCI_CLOCK_INT_STABLE)) {
3403119936aSThomas Abraham 		if (timeout == 0) {
3412ad0b249SJingoo Han 			dev_err(dev, "%s: Internal clock never stabilised.\n",
3422ad0b249SJingoo Han 				mmc_hostname(host->mmc));
3433119936aSThomas Abraham 			return;
3443119936aSThomas Abraham 		}
3453119936aSThomas Abraham 		timeout--;
3463119936aSThomas Abraham 		mdelay(1);
3473119936aSThomas Abraham 	}
3483119936aSThomas Abraham 
3493119936aSThomas Abraham 	clk |= SDHCI_CLOCK_CARD_EN;
3503119936aSThomas Abraham 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
351253e0a7cSJeongbae Seo }
352253e0a7cSJeongbae Seo 
353548f07d2SJaehoon Chung /**
3547bc088d3SSascha Hauer  * sdhci_s3c_platform_bus_width - support 8bit buswidth
355548f07d2SJaehoon Chung  * @host: The SDHCI host being queried
356548f07d2SJaehoon Chung  * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
357548f07d2SJaehoon Chung  *
358548f07d2SJaehoon Chung  * We have 8-bit width support but is not a v3 controller.
3597bc088d3SSascha Hauer  * So we add platform_bus_width() and support 8bit width.
360548f07d2SJaehoon Chung  */
3617bc088d3SSascha Hauer static int sdhci_s3c_platform_bus_width(struct sdhci_host *host, int width)
362548f07d2SJaehoon Chung {
363548f07d2SJaehoon Chung 	u8 ctrl;
364548f07d2SJaehoon Chung 
365548f07d2SJaehoon Chung 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
366548f07d2SJaehoon Chung 
367548f07d2SJaehoon Chung 	switch (width) {
368548f07d2SJaehoon Chung 	case MMC_BUS_WIDTH_8:
369548f07d2SJaehoon Chung 		ctrl |= SDHCI_CTRL_8BITBUS;
370548f07d2SJaehoon Chung 		ctrl &= ~SDHCI_CTRL_4BITBUS;
371548f07d2SJaehoon Chung 		break;
372548f07d2SJaehoon Chung 	case MMC_BUS_WIDTH_4:
373548f07d2SJaehoon Chung 		ctrl |= SDHCI_CTRL_4BITBUS;
374548f07d2SJaehoon Chung 		ctrl &= ~SDHCI_CTRL_8BITBUS;
375548f07d2SJaehoon Chung 		break;
376548f07d2SJaehoon Chung 	default:
37749bb1e61SGirish K S 		ctrl &= ~SDHCI_CTRL_4BITBUS;
37849bb1e61SGirish K S 		ctrl &= ~SDHCI_CTRL_8BITBUS;
379548f07d2SJaehoon Chung 		break;
380548f07d2SJaehoon Chung 	}
381548f07d2SJaehoon Chung 
382548f07d2SJaehoon Chung 	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
383548f07d2SJaehoon Chung 
384548f07d2SJaehoon Chung 	return 0;
385548f07d2SJaehoon Chung }
386548f07d2SJaehoon Chung 
3870d1bb41aSBen Dooks static struct sdhci_ops sdhci_s3c_ops = {
3880d1bb41aSBen Dooks 	.get_max_clock		= sdhci_s3c_get_max_clk,
3890d1bb41aSBen Dooks 	.set_clock		= sdhci_s3c_set_clock,
390ce5f036bSMarek Szyprowski 	.get_min_clock		= sdhci_s3c_get_min_clock,
3917bc088d3SSascha Hauer 	.platform_bus_width	= sdhci_s3c_platform_bus_width,
3920d1bb41aSBen Dooks };
3930d1bb41aSBen Dooks 
39417866e14SMarek Szyprowski static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
39517866e14SMarek Szyprowski {
39617866e14SMarek Szyprowski 	struct sdhci_host *host = platform_get_drvdata(dev);
3974577f77bSSachin Kamat #ifdef CONFIG_PM_RUNTIME
398fe007c02SHeiko Stübner 	struct sdhci_s3c *sc = sdhci_priv(host);
3994577f77bSSachin Kamat #endif
40006fe577fSMarek Szyprowski 	unsigned long flags;
40106fe577fSMarek Szyprowski 
40217866e14SMarek Szyprowski 	if (host) {
40306fe577fSMarek Szyprowski 		spin_lock_irqsave(&host->lock, flags);
40417866e14SMarek Szyprowski 		if (state) {
40517866e14SMarek Szyprowski 			dev_dbg(&dev->dev, "card inserted.\n");
406fe007c02SHeiko Stübner #ifdef CONFIG_PM_RUNTIME
407fe007c02SHeiko Stübner 			clk_prepare_enable(sc->clk_io);
408fe007c02SHeiko Stübner #endif
40917866e14SMarek Szyprowski 			host->flags &= ~SDHCI_DEVICE_DEAD;
41017866e14SMarek Szyprowski 			host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
41117866e14SMarek Szyprowski 		} else {
41217866e14SMarek Szyprowski 			dev_dbg(&dev->dev, "card removed.\n");
41317866e14SMarek Szyprowski 			host->flags |= SDHCI_DEVICE_DEAD;
41417866e14SMarek Szyprowski 			host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
415fe007c02SHeiko Stübner #ifdef CONFIG_PM_RUNTIME
416fe007c02SHeiko Stübner 			clk_disable_unprepare(sc->clk_io);
417fe007c02SHeiko Stübner #endif
41817866e14SMarek Szyprowski 		}
419f522886eSKyungmin Park 		tasklet_schedule(&host->card_tasklet);
42006fe577fSMarek Szyprowski 		spin_unlock_irqrestore(&host->lock, flags);
42117866e14SMarek Szyprowski 	}
42217866e14SMarek Szyprowski }
42317866e14SMarek Szyprowski 
42417866e14SMarek Szyprowski static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
42517866e14SMarek Szyprowski {
42617866e14SMarek Szyprowski 	struct sdhci_s3c *sc = dev_id;
42717866e14SMarek Szyprowski 	int status = gpio_get_value(sc->ext_cd_gpio);
42817866e14SMarek Szyprowski 	if (sc->pdata->ext_cd_gpio_invert)
42917866e14SMarek Szyprowski 		status = !status;
43017866e14SMarek Szyprowski 	sdhci_s3c_notify_change(sc->pdev, status);
43117866e14SMarek Szyprowski 	return IRQ_HANDLED;
43217866e14SMarek Szyprowski }
43317866e14SMarek Szyprowski 
43417866e14SMarek Szyprowski static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
43517866e14SMarek Szyprowski {
43617866e14SMarek Szyprowski 	struct s3c_sdhci_platdata *pdata = sc->pdata;
43717866e14SMarek Szyprowski 	struct device *dev = &sc->pdev->dev;
43817866e14SMarek Szyprowski 
439b1b8fea9STomasz Figa 	if (devm_gpio_request(dev, pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
44017866e14SMarek Szyprowski 		sc->ext_cd_gpio = pdata->ext_cd_gpio;
44117866e14SMarek Szyprowski 		sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
44217866e14SMarek Szyprowski 		if (sc->ext_cd_irq &&
44317866e14SMarek Szyprowski 		    request_threaded_irq(sc->ext_cd_irq, NULL,
44417866e14SMarek Szyprowski 					 sdhci_s3c_gpio_card_detect_thread,
4452ad0b249SJingoo Han 					 IRQF_TRIGGER_RISING |
4462ad0b249SJingoo Han 					 IRQF_TRIGGER_FALLING |
4472ad0b249SJingoo Han 					 IRQF_ONESHOT,
44817866e14SMarek Szyprowski 					 dev_name(dev), sc) == 0) {
44917866e14SMarek Szyprowski 			int status = gpio_get_value(sc->ext_cd_gpio);
45017866e14SMarek Szyprowski 			if (pdata->ext_cd_gpio_invert)
45117866e14SMarek Szyprowski 				status = !status;
45217866e14SMarek Szyprowski 			sdhci_s3c_notify_change(sc->pdev, status);
45317866e14SMarek Szyprowski 		} else {
45417866e14SMarek Szyprowski 			dev_warn(dev, "cannot request irq for card detect\n");
45517866e14SMarek Szyprowski 			sc->ext_cd_irq = 0;
45617866e14SMarek Szyprowski 		}
45717866e14SMarek Szyprowski 	} else {
45817866e14SMarek Szyprowski 		dev_err(dev, "cannot request gpio for card detect\n");
45917866e14SMarek Szyprowski 	}
46017866e14SMarek Szyprowski }
46117866e14SMarek Szyprowski 
462cd1b00ebSThomas Abraham #ifdef CONFIG_OF
463c3be1efdSBill Pemberton static int sdhci_s3c_parse_dt(struct device *dev,
464cd1b00ebSThomas Abraham 		struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
465cd1b00ebSThomas Abraham {
466cd1b00ebSThomas Abraham 	struct device_node *node = dev->of_node;
467cd1b00ebSThomas Abraham 	struct sdhci_s3c *ourhost = to_s3c(host);
468cd1b00ebSThomas Abraham 	u32 max_width;
469e19499aeSThomas Abraham 	int gpio;
470cd1b00ebSThomas Abraham 
471cd1b00ebSThomas Abraham 	/* if the bus-width property is not specified, assume width as 1 */
472cd1b00ebSThomas Abraham 	if (of_property_read_u32(node, "bus-width", &max_width))
473cd1b00ebSThomas Abraham 		max_width = 1;
474cd1b00ebSThomas Abraham 	pdata->max_width = max_width;
475cd1b00ebSThomas Abraham 
476cd1b00ebSThomas Abraham 	/* get the card detection method */
477ab5023efSTushar Behera 	if (of_get_property(node, "broken-cd", NULL)) {
478cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_NONE;
479e19499aeSThomas Abraham 		return 0;
480cd1b00ebSThomas Abraham 	}
481cd1b00ebSThomas Abraham 
482ab5023efSTushar Behera 	if (of_get_property(node, "non-removable", NULL)) {
483cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
484e19499aeSThomas Abraham 		return 0;
485cd1b00ebSThomas Abraham 	}
486cd1b00ebSThomas Abraham 
487cd1b00ebSThomas Abraham 	gpio = of_get_named_gpio(node, "cd-gpios", 0);
488cd1b00ebSThomas Abraham 	if (gpio_is_valid(gpio)) {
489cd1b00ebSThomas Abraham 		pdata->cd_type = S3C_SDHCI_CD_GPIO;
490e19499aeSThomas Abraham 		pdata->ext_cd_gpio = gpio;
491e19499aeSThomas Abraham 		ourhost->ext_cd_gpio = -1;
492e19499aeSThomas Abraham 		if (of_get_property(node, "cd-inverted", NULL))
493e19499aeSThomas Abraham 			pdata->ext_cd_gpio_invert = 1;
494e19499aeSThomas Abraham 		return 0;
495cd1b00ebSThomas Abraham 	} else if (gpio != -ENOENT) {
496cd1b00ebSThomas Abraham 		dev_err(dev, "invalid card detect gpio specified\n");
497cd1b00ebSThomas Abraham 		return -EINVAL;
498cd1b00ebSThomas Abraham 	}
499cd1b00ebSThomas Abraham 
500b96efccbSTomasz Figa 	/* assuming internal card detect that will be configured by pinctrl */
501b96efccbSTomasz Figa 	pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
502cd1b00ebSThomas Abraham 	return 0;
503cd1b00ebSThomas Abraham }
504cd1b00ebSThomas Abraham #else
505c3be1efdSBill Pemberton static int sdhci_s3c_parse_dt(struct device *dev,
506cd1b00ebSThomas Abraham 		struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
507cd1b00ebSThomas Abraham {
508cd1b00ebSThomas Abraham 	return -EINVAL;
509cd1b00ebSThomas Abraham }
510cd1b00ebSThomas Abraham #endif
511cd1b00ebSThomas Abraham 
512cd1b00ebSThomas Abraham static const struct of_device_id sdhci_s3c_dt_match[];
513cd1b00ebSThomas Abraham 
5143119936aSThomas Abraham static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
5153119936aSThomas Abraham 			struct platform_device *pdev)
5163119936aSThomas Abraham {
517cd1b00ebSThomas Abraham #ifdef CONFIG_OF
518cd1b00ebSThomas Abraham 	if (pdev->dev.of_node) {
519cd1b00ebSThomas Abraham 		const struct of_device_id *match;
520cd1b00ebSThomas Abraham 		match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
521cd1b00ebSThomas Abraham 		return (struct sdhci_s3c_drv_data *)match->data;
522cd1b00ebSThomas Abraham 	}
523cd1b00ebSThomas Abraham #endif
5243119936aSThomas Abraham 	return (struct sdhci_s3c_drv_data *)
5253119936aSThomas Abraham 			platform_get_device_id(pdev)->driver_data;
5263119936aSThomas Abraham }
5273119936aSThomas Abraham 
528c3be1efdSBill Pemberton static int sdhci_s3c_probe(struct platform_device *pdev)
5290d1bb41aSBen Dooks {
5301d4dc338SThomas Abraham 	struct s3c_sdhci_platdata *pdata;
5313119936aSThomas Abraham 	struct sdhci_s3c_drv_data *drv_data;
5320d1bb41aSBen Dooks 	struct device *dev = &pdev->dev;
5330d1bb41aSBen Dooks 	struct sdhci_host *host;
5340d1bb41aSBen Dooks 	struct sdhci_s3c *sc;
5350d1bb41aSBen Dooks 	struct resource *res;
5360d1bb41aSBen Dooks 	int ret, irq, ptr, clks;
5370d1bb41aSBen Dooks 
538cd1b00ebSThomas Abraham 	if (!pdev->dev.platform_data && !pdev->dev.of_node) {
5390d1bb41aSBen Dooks 		dev_err(dev, "no device data specified\n");
5400d1bb41aSBen Dooks 		return -ENOENT;
5410d1bb41aSBen Dooks 	}
5420d1bb41aSBen Dooks 
5430d1bb41aSBen Dooks 	irq = platform_get_irq(pdev, 0);
5440d1bb41aSBen Dooks 	if (irq < 0) {
5450d1bb41aSBen Dooks 		dev_err(dev, "no irq specified\n");
5460d1bb41aSBen Dooks 		return irq;
5470d1bb41aSBen Dooks 	}
5480d1bb41aSBen Dooks 
5490d1bb41aSBen Dooks 	host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
5500d1bb41aSBen Dooks 	if (IS_ERR(host)) {
5510d1bb41aSBen Dooks 		dev_err(dev, "sdhci_alloc_host() failed\n");
5520d1bb41aSBen Dooks 		return PTR_ERR(host);
5530d1bb41aSBen Dooks 	}
554cd1b00ebSThomas Abraham 	sc = sdhci_priv(host);
5550d1bb41aSBen Dooks 
5561d4dc338SThomas Abraham 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
5571d4dc338SThomas Abraham 	if (!pdata) {
5581d4dc338SThomas Abraham 		ret = -ENOMEM;
559b1b8fea9STomasz Figa 		goto err_pdata_io_clk;
5601d4dc338SThomas Abraham 	}
561cd1b00ebSThomas Abraham 
562cd1b00ebSThomas Abraham 	if (pdev->dev.of_node) {
563cd1b00ebSThomas Abraham 		ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
564cd1b00ebSThomas Abraham 		if (ret)
565b1b8fea9STomasz Figa 			goto err_pdata_io_clk;
566cd1b00ebSThomas Abraham 	} else {
5671d4dc338SThomas Abraham 		memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
568cd1b00ebSThomas Abraham 		sc->ext_cd_gpio = -1; /* invalid gpio number */
569cd1b00ebSThomas Abraham 	}
5701d4dc338SThomas Abraham 
5713119936aSThomas Abraham 	drv_data = sdhci_s3c_get_driver_data(pdev);
5720d1bb41aSBen Dooks 
5730d1bb41aSBen Dooks 	sc->host = host;
5740d1bb41aSBen Dooks 	sc->pdev = pdev;
5750d1bb41aSBen Dooks 	sc->pdata = pdata;
5760d1bb41aSBen Dooks 
5770d1bb41aSBen Dooks 	platform_set_drvdata(pdev, host);
5780d1bb41aSBen Dooks 
5793aaf7ba7SJingoo Han 	sc->clk_io = devm_clk_get(dev, "hsmmc");
5800d1bb41aSBen Dooks 	if (IS_ERR(sc->clk_io)) {
5810d1bb41aSBen Dooks 		dev_err(dev, "failed to get io clock\n");
5820d1bb41aSBen Dooks 		ret = PTR_ERR(sc->clk_io);
583b1b8fea9STomasz Figa 		goto err_pdata_io_clk;
5840d1bb41aSBen Dooks 	}
5850d1bb41aSBen Dooks 
5860d1bb41aSBen Dooks 	/* enable the local io clock and keep it running for the moment. */
5870f310a05SThomas Abraham 	clk_prepare_enable(sc->clk_io);
5880d1bb41aSBen Dooks 
5890d1bb41aSBen Dooks 	for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
5904346b6d9SRajeshwari Shinde 		char name[14];
5910d1bb41aSBen Dooks 
5924346b6d9SRajeshwari Shinde 		snprintf(name, 14, "mmc_busclk.%d", ptr);
5938f4b78d9STomasz Figa 		sc->clk_bus[ptr] = devm_clk_get(dev, name);
5948f4b78d9STomasz Figa 		if (IS_ERR(sc->clk_bus[ptr]))
5950d1bb41aSBen Dooks 			continue;
5960d1bb41aSBen Dooks 
5970d1bb41aSBen Dooks 		clks++;
598253e0a7cSJeongbae Seo 
599253e0a7cSJeongbae Seo 		/*
600253e0a7cSJeongbae Seo 		 * save current clock index to know which clock bus
601253e0a7cSJeongbae Seo 		 * is used later in overriding functions.
602253e0a7cSJeongbae Seo 		 */
603253e0a7cSJeongbae Seo 		sc->cur_clk = ptr;
604253e0a7cSJeongbae Seo 
6056eb28bdcSTomasz Figa 		sc->clk_rates[ptr] = clk_get_rate(sc->clk_bus[ptr]);
6066eb28bdcSTomasz Figa 
6070d1bb41aSBen Dooks 		dev_info(dev, "clock source %d: %s (%ld Hz)\n",
6086eb28bdcSTomasz Figa 				ptr, name, sc->clk_rates[ptr]);
6090d1bb41aSBen Dooks 	}
6100d1bb41aSBen Dooks 
6110d1bb41aSBen Dooks 	if (clks == 0) {
6120d1bb41aSBen Dooks 		dev_err(dev, "failed to find any bus clocks\n");
6130d1bb41aSBen Dooks 		ret = -ENOENT;
6140d1bb41aSBen Dooks 		goto err_no_busclks;
6150d1bb41aSBen Dooks 	}
6160d1bb41aSBen Dooks 
6172abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME
6180f310a05SThomas Abraham 	clk_prepare_enable(sc->clk_bus[sc->cur_clk]);
6192abeb5c5SChander Kashyap #endif
620e684c468SChander Kashyap 
6219bda6da7SJulia Lawall 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
622a3e2cd7fSThierry Reding 	host->ioaddr = devm_ioremap_resource(&pdev->dev, res);
623a3e2cd7fSThierry Reding 	if (IS_ERR(host->ioaddr)) {
624a3e2cd7fSThierry Reding 		ret = PTR_ERR(host->ioaddr);
6250d1bb41aSBen Dooks 		goto err_req_regs;
6260d1bb41aSBen Dooks 	}
6270d1bb41aSBen Dooks 
6280d1bb41aSBen Dooks 	/* Ensure we have minimal gpio selected CMD/CLK/Detect */
6290d1bb41aSBen Dooks 	if (pdata->cfg_gpio)
6300d1bb41aSBen Dooks 		pdata->cfg_gpio(pdev, pdata->max_width);
6310d1bb41aSBen Dooks 
6320d1bb41aSBen Dooks 	host->hw_name = "samsung-hsmmc";
6330d1bb41aSBen Dooks 	host->ops = &sdhci_s3c_ops;
6340d1bb41aSBen Dooks 	host->quirks = 0;
635285e244fSJaehoon Chung 	host->quirks2 = 0;
6360d1bb41aSBen Dooks 	host->irq = irq;
6370d1bb41aSBen Dooks 
6380d1bb41aSBen Dooks 	/* Setup quirks for the controller */
639b2e75effSThomas Abraham 	host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
640a1d56460SMarek Szyprowski 	host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
6413119936aSThomas Abraham 	if (drv_data)
6423119936aSThomas Abraham 		host->quirks |= drv_data->sdhci_quirks;
6430d1bb41aSBen Dooks 
6440d1bb41aSBen Dooks #ifndef CONFIG_MMC_SDHCI_S3C_DMA
6450d1bb41aSBen Dooks 
6460d1bb41aSBen Dooks 	/* we currently see overruns on errors, so disable the SDMA
6470d1bb41aSBen Dooks 	 * support as well. */
6480d1bb41aSBen Dooks 	host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
6490d1bb41aSBen Dooks 
6500d1bb41aSBen Dooks #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
6510d1bb41aSBen Dooks 
6520d1bb41aSBen Dooks 	/* It seems we do not get an DATA transfer complete on non-busy
6530d1bb41aSBen Dooks 	 * transfers, not sure if this is a problem with this specific
6540d1bb41aSBen Dooks 	 * SDHCI block, or a missing configuration that needs to be set. */
6550d1bb41aSBen Dooks 	host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
6560d1bb41aSBen Dooks 
657732f0e31SKyungmin Park 	/* This host supports the Auto CMD12 */
658732f0e31SKyungmin Park 	host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
659732f0e31SKyungmin Park 
6607199e2b6SJaehoon Chung 	/* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
6617199e2b6SJaehoon Chung 	host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
6627199e2b6SJaehoon Chung 
66317866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
66417866e14SMarek Szyprowski 	    pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
66517866e14SMarek Szyprowski 		host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
66617866e14SMarek Szyprowski 
66717866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
66817866e14SMarek Szyprowski 		host->mmc->caps = MMC_CAP_NONREMOVABLE;
66917866e14SMarek Szyprowski 
6700d22c770SThomas Abraham 	switch (pdata->max_width) {
6710d22c770SThomas Abraham 	case 8:
6720d22c770SThomas Abraham 		host->mmc->caps |= MMC_CAP_8_BIT_DATA;
6730d22c770SThomas Abraham 	case 4:
6740d22c770SThomas Abraham 		host->mmc->caps |= MMC_CAP_4_BIT_DATA;
6750d22c770SThomas Abraham 		break;
6760d22c770SThomas Abraham 	}
6770d22c770SThomas Abraham 
678fa1773ccSSangwook Lee 	if (pdata->pm_caps)
679fa1773ccSSangwook Lee 		host->mmc->pm_caps |= pdata->pm_caps;
680fa1773ccSSangwook Lee 
6810d1bb41aSBen Dooks 	host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
6820d1bb41aSBen Dooks 			 SDHCI_QUIRK_32BIT_DMA_SIZE);
6830d1bb41aSBen Dooks 
6843fe42e07SHyuk Lee 	/* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
6853fe42e07SHyuk Lee 	host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
6863fe42e07SHyuk Lee 
687253e0a7cSJeongbae Seo 	/*
688253e0a7cSJeongbae Seo 	 * If controller does not have internal clock divider,
689253e0a7cSJeongbae Seo 	 * we can use overriding functions instead of default.
690253e0a7cSJeongbae Seo 	 */
6913119936aSThomas Abraham 	if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
692253e0a7cSJeongbae Seo 		sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
693253e0a7cSJeongbae Seo 		sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
694253e0a7cSJeongbae Seo 		sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
695253e0a7cSJeongbae Seo 	}
696253e0a7cSJeongbae Seo 
697b3824f2cSJeongbae Seo 	/* It supports additional host capabilities if needed */
698b3824f2cSJeongbae Seo 	if (pdata->host_caps)
699b3824f2cSJeongbae Seo 		host->mmc->caps |= pdata->host_caps;
700b3824f2cSJeongbae Seo 
701c1c4b66dSJaehoon Chung 	if (pdata->host_caps2)
702c1c4b66dSJaehoon Chung 		host->mmc->caps2 |= pdata->host_caps2;
703c1c4b66dSJaehoon Chung 
7049f4e8151SMark Brown 	pm_runtime_enable(&pdev->dev);
7059f4e8151SMark Brown 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
7069f4e8151SMark Brown 	pm_runtime_use_autosuspend(&pdev->dev);
7079f4e8151SMark Brown 	pm_suspend_ignore_children(&pdev->dev, 1);
7089f4e8151SMark Brown 
7090d1bb41aSBen Dooks 	ret = sdhci_add_host(host);
7100d1bb41aSBen Dooks 	if (ret) {
7110d1bb41aSBen Dooks 		dev_err(dev, "sdhci_add_host() failed\n");
7129f4e8151SMark Brown 		pm_runtime_forbid(&pdev->dev);
7139f4e8151SMark Brown 		pm_runtime_get_noresume(&pdev->dev);
7149bda6da7SJulia Lawall 		goto err_req_regs;
7150d1bb41aSBen Dooks 	}
7160d1bb41aSBen Dooks 
71717866e14SMarek Szyprowski 	/* The following two methods of card detection might call
71817866e14SMarek Szyprowski 	   sdhci_s3c_notify_change() immediately, so they can be called
71917866e14SMarek Szyprowski 	   only after sdhci_add_host(). Setup errors are ignored. */
72017866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
72117866e14SMarek Szyprowski 		pdata->ext_cd_init(&sdhci_s3c_notify_change);
72217866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
72317866e14SMarek Szyprowski 	    gpio_is_valid(pdata->ext_cd_gpio))
72417866e14SMarek Szyprowski 		sdhci_s3c_setup_card_detect_gpio(sc);
72517866e14SMarek Szyprowski 
7262abeb5c5SChander Kashyap #ifdef CONFIG_PM_RUNTIME
7270aa55c23SSeungwon Jeon 	if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
7280f310a05SThomas Abraham 		clk_disable_unprepare(sc->clk_io);
7292abeb5c5SChander Kashyap #endif
7300d1bb41aSBen Dooks 	return 0;
7310d1bb41aSBen Dooks 
7320d1bb41aSBen Dooks  err_req_regs:
7332abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME
7340f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_bus[sc->cur_clk]);
7352abeb5c5SChander Kashyap #endif
7360d1bb41aSBen Dooks 
7370d1bb41aSBen Dooks  err_no_busclks:
7380f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_io);
7390d1bb41aSBen Dooks 
740b1b8fea9STomasz Figa  err_pdata_io_clk:
7410d1bb41aSBen Dooks 	sdhci_free_host(host);
7420d1bb41aSBen Dooks 
7430d1bb41aSBen Dooks 	return ret;
7440d1bb41aSBen Dooks }
7450d1bb41aSBen Dooks 
7466e0ee714SBill Pemberton static int sdhci_s3c_remove(struct platform_device *pdev)
7470d1bb41aSBen Dooks {
7489d51a6b2SMarek Szyprowski 	struct sdhci_host *host =  platform_get_drvdata(pdev);
7499d51a6b2SMarek Szyprowski 	struct sdhci_s3c *sc = sdhci_priv(host);
750cd1b00ebSThomas Abraham 	struct s3c_sdhci_platdata *pdata = sc->pdata;
7519d51a6b2SMarek Szyprowski 
75217866e14SMarek Szyprowski 	if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
75317866e14SMarek Szyprowski 		pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
75417866e14SMarek Szyprowski 
75517866e14SMarek Szyprowski 	if (sc->ext_cd_irq)
75617866e14SMarek Szyprowski 		free_irq(sc->ext_cd_irq, sc);
75717866e14SMarek Szyprowski 
7582abeb5c5SChander Kashyap #ifdef CONFIG_PM_RUNTIME
7590aa55c23SSeungwon Jeon 	if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
7600f310a05SThomas Abraham 		clk_prepare_enable(sc->clk_io);
7612abeb5c5SChander Kashyap #endif
7629d51a6b2SMarek Szyprowski 	sdhci_remove_host(host, 1);
7639d51a6b2SMarek Szyprowski 
764387a8cbdSChander Kashyap 	pm_runtime_dont_use_autosuspend(&pdev->dev);
7659f4e8151SMark Brown 	pm_runtime_disable(&pdev->dev);
7669f4e8151SMark Brown 
7672abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME
7680f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_bus[sc->cur_clk]);
7692abeb5c5SChander Kashyap #endif
7700f310a05SThomas Abraham 	clk_disable_unprepare(sc->clk_io);
7719d51a6b2SMarek Szyprowski 
7729d51a6b2SMarek Szyprowski 	sdhci_free_host(host);
7739d51a6b2SMarek Szyprowski 
7740d1bb41aSBen Dooks 	return 0;
7750d1bb41aSBen Dooks }
7760d1bb41aSBen Dooks 
777d5e9c02cSMark Brown #ifdef CONFIG_PM_SLEEP
77829495aa0SManuel Lauss static int sdhci_s3c_suspend(struct device *dev)
7790d1bb41aSBen Dooks {
78029495aa0SManuel Lauss 	struct sdhci_host *host = dev_get_drvdata(dev);
7810d1bb41aSBen Dooks 
78229495aa0SManuel Lauss 	return sdhci_suspend_host(host);
7830d1bb41aSBen Dooks }
7840d1bb41aSBen Dooks 
78529495aa0SManuel Lauss static int sdhci_s3c_resume(struct device *dev)
7860d1bb41aSBen Dooks {
78729495aa0SManuel Lauss 	struct sdhci_host *host = dev_get_drvdata(dev);
7880d1bb41aSBen Dooks 
78965d13516SWonil Choi 	return sdhci_resume_host(host);
7900d1bb41aSBen Dooks }
791d5e9c02cSMark Brown #endif
7920d1bb41aSBen Dooks 
7939f4e8151SMark Brown #ifdef CONFIG_PM_RUNTIME
7949f4e8151SMark Brown static int sdhci_s3c_runtime_suspend(struct device *dev)
7959f4e8151SMark Brown {
7969f4e8151SMark Brown 	struct sdhci_host *host = dev_get_drvdata(dev);
7972abeb5c5SChander Kashyap 	struct sdhci_s3c *ourhost = to_s3c(host);
7982abeb5c5SChander Kashyap 	struct clk *busclk = ourhost->clk_io;
7992abeb5c5SChander Kashyap 	int ret;
8009f4e8151SMark Brown 
8012abeb5c5SChander Kashyap 	ret = sdhci_runtime_suspend_host(host);
8022abeb5c5SChander Kashyap 
8030f310a05SThomas Abraham 	clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
8040f310a05SThomas Abraham 	clk_disable_unprepare(busclk);
8052abeb5c5SChander Kashyap 	return ret;
8069f4e8151SMark Brown }
8079f4e8151SMark Brown 
8089f4e8151SMark Brown static int sdhci_s3c_runtime_resume(struct device *dev)
8099f4e8151SMark Brown {
8109f4e8151SMark Brown 	struct sdhci_host *host = dev_get_drvdata(dev);
8112abeb5c5SChander Kashyap 	struct sdhci_s3c *ourhost = to_s3c(host);
8122abeb5c5SChander Kashyap 	struct clk *busclk = ourhost->clk_io;
8132abeb5c5SChander Kashyap 	int ret;
8149f4e8151SMark Brown 
8150f310a05SThomas Abraham 	clk_prepare_enable(busclk);
8160f310a05SThomas Abraham 	clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
8172abeb5c5SChander Kashyap 	ret = sdhci_runtime_resume_host(host);
8182abeb5c5SChander Kashyap 	return ret;
8199f4e8151SMark Brown }
8209f4e8151SMark Brown #endif
8219f4e8151SMark Brown 
822d5e9c02cSMark Brown #ifdef CONFIG_PM
82329495aa0SManuel Lauss static const struct dev_pm_ops sdhci_s3c_pmops = {
824d5e9c02cSMark Brown 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
8259f4e8151SMark Brown 	SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
8269f4e8151SMark Brown 			   NULL)
82729495aa0SManuel Lauss };
82829495aa0SManuel Lauss 
82929495aa0SManuel Lauss #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
83029495aa0SManuel Lauss 
8310d1bb41aSBen Dooks #else
83229495aa0SManuel Lauss #define SDHCI_S3C_PMOPS NULL
8330d1bb41aSBen Dooks #endif
8340d1bb41aSBen Dooks 
8353119936aSThomas Abraham #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
8363119936aSThomas Abraham static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
8373119936aSThomas Abraham 	.sdhci_quirks = SDHCI_QUIRK_NONSTANDARD_CLOCK,
8383119936aSThomas Abraham };
8393119936aSThomas Abraham #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
8403119936aSThomas Abraham #else
8413119936aSThomas Abraham #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
8423119936aSThomas Abraham #endif
8433119936aSThomas Abraham 
8443119936aSThomas Abraham static struct platform_device_id sdhci_s3c_driver_ids[] = {
8453119936aSThomas Abraham 	{
8463119936aSThomas Abraham 		.name		= "s3c-sdhci",
8473119936aSThomas Abraham 		.driver_data	= (kernel_ulong_t)NULL,
8483119936aSThomas Abraham 	}, {
8493119936aSThomas Abraham 		.name		= "exynos4-sdhci",
8503119936aSThomas Abraham 		.driver_data	= EXYNOS4_SDHCI_DRV_DATA,
8513119936aSThomas Abraham 	},
8523119936aSThomas Abraham 	{ }
8533119936aSThomas Abraham };
8543119936aSThomas Abraham MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
8553119936aSThomas Abraham 
856cd1b00ebSThomas Abraham #ifdef CONFIG_OF
857cd1b00ebSThomas Abraham static const struct of_device_id sdhci_s3c_dt_match[] = {
858cd1b00ebSThomas Abraham 	{ .compatible = "samsung,s3c6410-sdhci", },
859cd1b00ebSThomas Abraham 	{ .compatible = "samsung,exynos4210-sdhci",
860cd1b00ebSThomas Abraham 		.data = (void *)EXYNOS4_SDHCI_DRV_DATA },
861cd1b00ebSThomas Abraham 	{},
862cd1b00ebSThomas Abraham };
863cd1b00ebSThomas Abraham MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
864cd1b00ebSThomas Abraham #endif
865cd1b00ebSThomas Abraham 
8660d1bb41aSBen Dooks static struct platform_driver sdhci_s3c_driver = {
8670d1bb41aSBen Dooks 	.probe		= sdhci_s3c_probe,
8680433c143SBill Pemberton 	.remove		= sdhci_s3c_remove,
8693119936aSThomas Abraham 	.id_table	= sdhci_s3c_driver_ids,
8700d1bb41aSBen Dooks 	.driver		= {
8710d1bb41aSBen Dooks 		.owner	= THIS_MODULE,
8720d1bb41aSBen Dooks 		.name	= "s3c-sdhci",
873cd1b00ebSThomas Abraham 		.of_match_table = of_match_ptr(sdhci_s3c_dt_match),
87429495aa0SManuel Lauss 		.pm	= SDHCI_S3C_PMOPS,
8750d1bb41aSBen Dooks 	},
8760d1bb41aSBen Dooks };
8770d1bb41aSBen Dooks 
878d1f81a64SAxel Lin module_platform_driver(sdhci_s3c_driver);
8790d1bb41aSBen Dooks 
8800d1bb41aSBen Dooks MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
8810d1bb41aSBen Dooks MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
8820d1bb41aSBen Dooks MODULE_LICENSE("GPL v2");
8830d1bb41aSBen Dooks MODULE_ALIAS("platform:s3c-sdhci");
884