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); 376fe007c02SHeiko Stübner struct sdhci_s3c *sc = sdhci_priv(host); 37706fe577fSMarek Szyprowski unsigned long flags; 37806fe577fSMarek Szyprowski 37917866e14SMarek Szyprowski if (host) { 38006fe577fSMarek Szyprowski spin_lock_irqsave(&host->lock, flags); 38117866e14SMarek Szyprowski if (state) { 38217866e14SMarek Szyprowski dev_dbg(&dev->dev, "card inserted.\n"); 383fe007c02SHeiko Stübner #ifdef CONFIG_PM_RUNTIME 384fe007c02SHeiko Stübner clk_prepare_enable(sc->clk_io); 385fe007c02SHeiko Stübner #endif 38617866e14SMarek Szyprowski host->flags &= ~SDHCI_DEVICE_DEAD; 38717866e14SMarek Szyprowski host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION; 38817866e14SMarek Szyprowski } else { 38917866e14SMarek Szyprowski dev_dbg(&dev->dev, "card removed.\n"); 39017866e14SMarek Szyprowski host->flags |= SDHCI_DEVICE_DEAD; 39117866e14SMarek Szyprowski host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; 392fe007c02SHeiko Stübner #ifdef CONFIG_PM_RUNTIME 393fe007c02SHeiko Stübner clk_disable_unprepare(sc->clk_io); 394fe007c02SHeiko Stübner #endif 39517866e14SMarek Szyprowski } 396f522886eSKyungmin Park tasklet_schedule(&host->card_tasklet); 39706fe577fSMarek Szyprowski spin_unlock_irqrestore(&host->lock, flags); 39817866e14SMarek Szyprowski } 39917866e14SMarek Szyprowski } 40017866e14SMarek Szyprowski 40117866e14SMarek Szyprowski static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id) 40217866e14SMarek Szyprowski { 40317866e14SMarek Szyprowski struct sdhci_s3c *sc = dev_id; 40417866e14SMarek Szyprowski int status = gpio_get_value(sc->ext_cd_gpio); 40517866e14SMarek Szyprowski if (sc->pdata->ext_cd_gpio_invert) 40617866e14SMarek Szyprowski status = !status; 40717866e14SMarek Szyprowski sdhci_s3c_notify_change(sc->pdev, status); 40817866e14SMarek Szyprowski return IRQ_HANDLED; 40917866e14SMarek Szyprowski } 41017866e14SMarek Szyprowski 41117866e14SMarek Szyprowski static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc) 41217866e14SMarek Szyprowski { 41317866e14SMarek Szyprowski struct s3c_sdhci_platdata *pdata = sc->pdata; 41417866e14SMarek Szyprowski struct device *dev = &sc->pdev->dev; 41517866e14SMarek Szyprowski 416b1b8fea9STomasz Figa if (devm_gpio_request(dev, pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) { 41717866e14SMarek Szyprowski sc->ext_cd_gpio = pdata->ext_cd_gpio; 41817866e14SMarek Szyprowski sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio); 41917866e14SMarek Szyprowski if (sc->ext_cd_irq && 42017866e14SMarek Szyprowski request_threaded_irq(sc->ext_cd_irq, NULL, 42117866e14SMarek Szyprowski sdhci_s3c_gpio_card_detect_thread, 4222ad0b249SJingoo Han IRQF_TRIGGER_RISING | 4232ad0b249SJingoo Han IRQF_TRIGGER_FALLING | 4242ad0b249SJingoo Han IRQF_ONESHOT, 42517866e14SMarek Szyprowski dev_name(dev), sc) == 0) { 42617866e14SMarek Szyprowski int status = gpio_get_value(sc->ext_cd_gpio); 42717866e14SMarek Szyprowski if (pdata->ext_cd_gpio_invert) 42817866e14SMarek Szyprowski status = !status; 42917866e14SMarek Szyprowski sdhci_s3c_notify_change(sc->pdev, status); 43017866e14SMarek Szyprowski } else { 43117866e14SMarek Szyprowski dev_warn(dev, "cannot request irq for card detect\n"); 43217866e14SMarek Szyprowski sc->ext_cd_irq = 0; 43317866e14SMarek Szyprowski } 43417866e14SMarek Szyprowski } else { 43517866e14SMarek Szyprowski dev_err(dev, "cannot request gpio for card detect\n"); 43617866e14SMarek Szyprowski } 43717866e14SMarek Szyprowski } 43817866e14SMarek Szyprowski 439cd1b00ebSThomas Abraham #ifdef CONFIG_OF 440cd1b00ebSThomas Abraham static int __devinit sdhci_s3c_parse_dt(struct device *dev, 441cd1b00ebSThomas Abraham struct sdhci_host *host, struct s3c_sdhci_platdata *pdata) 442cd1b00ebSThomas Abraham { 443cd1b00ebSThomas Abraham struct device_node *node = dev->of_node; 444cd1b00ebSThomas Abraham struct sdhci_s3c *ourhost = to_s3c(host); 445cd1b00ebSThomas Abraham u32 max_width; 446cd1b00ebSThomas Abraham int gpio, cnt, ret; 447cd1b00ebSThomas Abraham 448cd1b00ebSThomas Abraham /* if the bus-width property is not specified, assume width as 1 */ 449cd1b00ebSThomas Abraham if (of_property_read_u32(node, "bus-width", &max_width)) 450cd1b00ebSThomas Abraham max_width = 1; 451cd1b00ebSThomas Abraham pdata->max_width = max_width; 452cd1b00ebSThomas Abraham 453cd1b00ebSThomas Abraham ourhost->gpios = devm_kzalloc(dev, NUM_GPIOS(pdata->max_width) * 454cd1b00ebSThomas Abraham sizeof(int), GFP_KERNEL); 455cd1b00ebSThomas Abraham if (!ourhost->gpios) 456cd1b00ebSThomas Abraham return -ENOMEM; 457cd1b00ebSThomas Abraham 458cd1b00ebSThomas Abraham /* get the card detection method */ 459ab5023efSTushar Behera if (of_get_property(node, "broken-cd", NULL)) { 460cd1b00ebSThomas Abraham pdata->cd_type = S3C_SDHCI_CD_NONE; 461cd1b00ebSThomas Abraham goto setup_bus; 462cd1b00ebSThomas Abraham } 463cd1b00ebSThomas Abraham 464ab5023efSTushar Behera if (of_get_property(node, "non-removable", NULL)) { 465cd1b00ebSThomas Abraham pdata->cd_type = S3C_SDHCI_CD_PERMANENT; 466cd1b00ebSThomas Abraham goto setup_bus; 467cd1b00ebSThomas Abraham } 468cd1b00ebSThomas Abraham 469cd1b00ebSThomas Abraham gpio = of_get_named_gpio(node, "cd-gpios", 0); 470cd1b00ebSThomas Abraham if (gpio_is_valid(gpio)) { 471cd1b00ebSThomas Abraham pdata->cd_type = S3C_SDHCI_CD_GPIO; 472cd1b00ebSThomas Abraham goto found_cd; 473cd1b00ebSThomas Abraham } else if (gpio != -ENOENT) { 474cd1b00ebSThomas Abraham dev_err(dev, "invalid card detect gpio specified\n"); 475cd1b00ebSThomas Abraham return -EINVAL; 476cd1b00ebSThomas Abraham } 477cd1b00ebSThomas Abraham 478cd1b00ebSThomas Abraham gpio = of_get_named_gpio(node, "samsung,cd-pinmux-gpio", 0); 479cd1b00ebSThomas Abraham if (gpio_is_valid(gpio)) { 480cd1b00ebSThomas Abraham pdata->cd_type = S3C_SDHCI_CD_INTERNAL; 481cd1b00ebSThomas Abraham goto found_cd; 482cd1b00ebSThomas Abraham } else if (gpio != -ENOENT) { 483cd1b00ebSThomas Abraham dev_err(dev, "invalid card detect gpio specified\n"); 484cd1b00ebSThomas Abraham return -EINVAL; 485cd1b00ebSThomas Abraham } 486cd1b00ebSThomas Abraham 487cd1b00ebSThomas Abraham dev_info(dev, "assuming no card detect line available\n"); 488cd1b00ebSThomas Abraham pdata->cd_type = S3C_SDHCI_CD_NONE; 489cd1b00ebSThomas Abraham 490cd1b00ebSThomas Abraham found_cd: 491cd1b00ebSThomas Abraham if (pdata->cd_type == S3C_SDHCI_CD_GPIO) { 492cd1b00ebSThomas Abraham pdata->ext_cd_gpio = gpio; 493cd1b00ebSThomas Abraham ourhost->ext_cd_gpio = -1; 494cd1b00ebSThomas Abraham if (of_get_property(node, "cd-inverted", NULL)) 495cd1b00ebSThomas Abraham pdata->ext_cd_gpio_invert = 1; 496cd1b00ebSThomas Abraham } else if (pdata->cd_type == S3C_SDHCI_CD_INTERNAL) { 497b1b8fea9STomasz Figa ret = devm_gpio_request(dev, gpio, "sdhci-cd"); 498cd1b00ebSThomas Abraham if (ret) { 499cd1b00ebSThomas Abraham dev_err(dev, "card detect gpio request failed\n"); 500cd1b00ebSThomas Abraham return -EINVAL; 501cd1b00ebSThomas Abraham } 502cd1b00ebSThomas Abraham ourhost->ext_cd_gpio = gpio; 503cd1b00ebSThomas Abraham } 504cd1b00ebSThomas Abraham 505cd1b00ebSThomas Abraham setup_bus: 506cd1b00ebSThomas Abraham /* get the gpios for command, clock and data lines */ 507cd1b00ebSThomas Abraham for (cnt = 0; cnt < NUM_GPIOS(pdata->max_width); cnt++) { 508cd1b00ebSThomas Abraham gpio = of_get_gpio(node, cnt); 509cd1b00ebSThomas Abraham if (!gpio_is_valid(gpio)) { 510cd1b00ebSThomas Abraham dev_err(dev, "invalid gpio[%d]\n", cnt); 511b1b8fea9STomasz Figa return -EINVAL; 512cd1b00ebSThomas Abraham } 513cd1b00ebSThomas Abraham ourhost->gpios[cnt] = gpio; 514cd1b00ebSThomas Abraham } 515cd1b00ebSThomas Abraham 516cd1b00ebSThomas Abraham for (cnt = 0; cnt < NUM_GPIOS(pdata->max_width); cnt++) { 517b1b8fea9STomasz Figa ret = devm_gpio_request(dev, ourhost->gpios[cnt], "sdhci-gpio"); 518cd1b00ebSThomas Abraham if (ret) { 519cd1b00ebSThomas Abraham dev_err(dev, "gpio[%d] request failed\n", cnt); 520b1b8fea9STomasz Figa return -EINVAL; 521cd1b00ebSThomas Abraham } 522cd1b00ebSThomas Abraham } 523cd1b00ebSThomas Abraham 524cd1b00ebSThomas Abraham return 0; 525cd1b00ebSThomas Abraham } 526cd1b00ebSThomas Abraham #else 527cd1b00ebSThomas Abraham static int __devinit sdhci_s3c_parse_dt(struct device *dev, 528cd1b00ebSThomas Abraham struct sdhci_host *host, struct s3c_sdhci_platdata *pdata) 529cd1b00ebSThomas Abraham { 530cd1b00ebSThomas Abraham return -EINVAL; 531cd1b00ebSThomas Abraham } 532cd1b00ebSThomas Abraham #endif 533cd1b00ebSThomas Abraham 534cd1b00ebSThomas Abraham static const struct of_device_id sdhci_s3c_dt_match[]; 535cd1b00ebSThomas Abraham 5363119936aSThomas Abraham static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data( 5373119936aSThomas Abraham struct platform_device *pdev) 5383119936aSThomas Abraham { 539cd1b00ebSThomas Abraham #ifdef CONFIG_OF 540cd1b00ebSThomas Abraham if (pdev->dev.of_node) { 541cd1b00ebSThomas Abraham const struct of_device_id *match; 542cd1b00ebSThomas Abraham match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node); 543cd1b00ebSThomas Abraham return (struct sdhci_s3c_drv_data *)match->data; 544cd1b00ebSThomas Abraham } 545cd1b00ebSThomas Abraham #endif 5463119936aSThomas Abraham return (struct sdhci_s3c_drv_data *) 5473119936aSThomas Abraham platform_get_device_id(pdev)->driver_data; 5483119936aSThomas Abraham } 5493119936aSThomas Abraham 5500d1bb41aSBen Dooks static int __devinit sdhci_s3c_probe(struct platform_device *pdev) 5510d1bb41aSBen Dooks { 5521d4dc338SThomas Abraham struct s3c_sdhci_platdata *pdata; 5533119936aSThomas Abraham struct sdhci_s3c_drv_data *drv_data; 5540d1bb41aSBen Dooks struct device *dev = &pdev->dev; 5550d1bb41aSBen Dooks struct sdhci_host *host; 5560d1bb41aSBen Dooks struct sdhci_s3c *sc; 5570d1bb41aSBen Dooks struct resource *res; 5580d1bb41aSBen Dooks int ret, irq, ptr, clks; 5590d1bb41aSBen Dooks 560cd1b00ebSThomas Abraham if (!pdev->dev.platform_data && !pdev->dev.of_node) { 5610d1bb41aSBen Dooks dev_err(dev, "no device data specified\n"); 5620d1bb41aSBen Dooks return -ENOENT; 5630d1bb41aSBen Dooks } 5640d1bb41aSBen Dooks 5650d1bb41aSBen Dooks irq = platform_get_irq(pdev, 0); 5660d1bb41aSBen Dooks if (irq < 0) { 5670d1bb41aSBen Dooks dev_err(dev, "no irq specified\n"); 5680d1bb41aSBen Dooks return irq; 5690d1bb41aSBen Dooks } 5700d1bb41aSBen Dooks 5710d1bb41aSBen Dooks host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c)); 5720d1bb41aSBen Dooks if (IS_ERR(host)) { 5730d1bb41aSBen Dooks dev_err(dev, "sdhci_alloc_host() failed\n"); 5740d1bb41aSBen Dooks return PTR_ERR(host); 5750d1bb41aSBen Dooks } 576cd1b00ebSThomas Abraham sc = sdhci_priv(host); 5770d1bb41aSBen Dooks 5781d4dc338SThomas Abraham pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 5791d4dc338SThomas Abraham if (!pdata) { 5801d4dc338SThomas Abraham ret = -ENOMEM; 581b1b8fea9STomasz Figa goto err_pdata_io_clk; 5821d4dc338SThomas Abraham } 583cd1b00ebSThomas Abraham 584cd1b00ebSThomas Abraham if (pdev->dev.of_node) { 585cd1b00ebSThomas Abraham ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata); 586cd1b00ebSThomas Abraham if (ret) 587b1b8fea9STomasz Figa goto err_pdata_io_clk; 588cd1b00ebSThomas Abraham } else { 5891d4dc338SThomas Abraham memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata)); 590cd1b00ebSThomas Abraham sc->ext_cd_gpio = -1; /* invalid gpio number */ 591cd1b00ebSThomas Abraham } 5921d4dc338SThomas Abraham 5933119936aSThomas Abraham drv_data = sdhci_s3c_get_driver_data(pdev); 5940d1bb41aSBen Dooks 5950d1bb41aSBen Dooks sc->host = host; 5960d1bb41aSBen Dooks sc->pdev = pdev; 5970d1bb41aSBen Dooks sc->pdata = pdata; 5980d1bb41aSBen Dooks 5990d1bb41aSBen Dooks platform_set_drvdata(pdev, host); 6000d1bb41aSBen Dooks 6010d1bb41aSBen Dooks sc->clk_io = clk_get(dev, "hsmmc"); 6020d1bb41aSBen Dooks if (IS_ERR(sc->clk_io)) { 6030d1bb41aSBen Dooks dev_err(dev, "failed to get io clock\n"); 6040d1bb41aSBen Dooks ret = PTR_ERR(sc->clk_io); 605b1b8fea9STomasz Figa goto err_pdata_io_clk; 6060d1bb41aSBen Dooks } 6070d1bb41aSBen Dooks 6080d1bb41aSBen Dooks /* enable the local io clock and keep it running for the moment. */ 6090f310a05SThomas Abraham clk_prepare_enable(sc->clk_io); 6100d1bb41aSBen Dooks 6110d1bb41aSBen Dooks for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) { 6120d1bb41aSBen Dooks struct clk *clk; 6134346b6d9SRajeshwari Shinde char name[14]; 6140d1bb41aSBen Dooks 6154346b6d9SRajeshwari Shinde snprintf(name, 14, "mmc_busclk.%d", ptr); 6160d1bb41aSBen Dooks clk = clk_get(dev, name); 6172ad0b249SJingoo Han if (IS_ERR(clk)) 6180d1bb41aSBen Dooks continue; 6190d1bb41aSBen Dooks 6200d1bb41aSBen Dooks clks++; 6210d1bb41aSBen Dooks sc->clk_bus[ptr] = clk; 622253e0a7cSJeongbae Seo 623253e0a7cSJeongbae Seo /* 624253e0a7cSJeongbae Seo * save current clock index to know which clock bus 625253e0a7cSJeongbae Seo * is used later in overriding functions. 626253e0a7cSJeongbae Seo */ 627253e0a7cSJeongbae Seo sc->cur_clk = ptr; 628253e0a7cSJeongbae Seo 6290d1bb41aSBen Dooks dev_info(dev, "clock source %d: %s (%ld Hz)\n", 6300d1bb41aSBen Dooks ptr, name, clk_get_rate(clk)); 6310d1bb41aSBen Dooks } 6320d1bb41aSBen Dooks 6330d1bb41aSBen Dooks if (clks == 0) { 6340d1bb41aSBen Dooks dev_err(dev, "failed to find any bus clocks\n"); 6350d1bb41aSBen Dooks ret = -ENOENT; 6360d1bb41aSBen Dooks goto err_no_busclks; 6370d1bb41aSBen Dooks } 6380d1bb41aSBen Dooks 6392abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME 6400f310a05SThomas Abraham clk_prepare_enable(sc->clk_bus[sc->cur_clk]); 6412abeb5c5SChander Kashyap #endif 642e684c468SChander Kashyap 6439bda6da7SJulia Lawall res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 6449bda6da7SJulia Lawall host->ioaddr = devm_request_and_ioremap(&pdev->dev, res); 6450d1bb41aSBen Dooks if (!host->ioaddr) { 6460d1bb41aSBen Dooks dev_err(dev, "failed to map registers\n"); 6470d1bb41aSBen Dooks ret = -ENXIO; 6480d1bb41aSBen Dooks goto err_req_regs; 6490d1bb41aSBen Dooks } 6500d1bb41aSBen Dooks 6510d1bb41aSBen Dooks /* Ensure we have minimal gpio selected CMD/CLK/Detect */ 6520d1bb41aSBen Dooks if (pdata->cfg_gpio) 6530d1bb41aSBen Dooks pdata->cfg_gpio(pdev, pdata->max_width); 6540d1bb41aSBen Dooks 6550d1bb41aSBen Dooks host->hw_name = "samsung-hsmmc"; 6560d1bb41aSBen Dooks host->ops = &sdhci_s3c_ops; 6570d1bb41aSBen Dooks host->quirks = 0; 6580d1bb41aSBen Dooks host->irq = irq; 6590d1bb41aSBen Dooks 6600d1bb41aSBen Dooks /* Setup quirks for the controller */ 661b2e75effSThomas Abraham host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC; 662a1d56460SMarek Szyprowski host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT; 6633119936aSThomas Abraham if (drv_data) 6643119936aSThomas Abraham host->quirks |= drv_data->sdhci_quirks; 6650d1bb41aSBen Dooks 6660d1bb41aSBen Dooks #ifndef CONFIG_MMC_SDHCI_S3C_DMA 6670d1bb41aSBen Dooks 6680d1bb41aSBen Dooks /* we currently see overruns on errors, so disable the SDMA 6690d1bb41aSBen Dooks * support as well. */ 6700d1bb41aSBen Dooks host->quirks |= SDHCI_QUIRK_BROKEN_DMA; 6710d1bb41aSBen Dooks 6720d1bb41aSBen Dooks #endif /* CONFIG_MMC_SDHCI_S3C_DMA */ 6730d1bb41aSBen Dooks 6740d1bb41aSBen Dooks /* It seems we do not get an DATA transfer complete on non-busy 6750d1bb41aSBen Dooks * transfers, not sure if this is a problem with this specific 6760d1bb41aSBen Dooks * SDHCI block, or a missing configuration that needs to be set. */ 6770d1bb41aSBen Dooks host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ; 6780d1bb41aSBen Dooks 679732f0e31SKyungmin Park /* This host supports the Auto CMD12 */ 680732f0e31SKyungmin Park host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12; 681732f0e31SKyungmin Park 6827199e2b6SJaehoon Chung /* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */ 6837199e2b6SJaehoon Chung host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC; 6847199e2b6SJaehoon Chung 68517866e14SMarek Szyprowski if (pdata->cd_type == S3C_SDHCI_CD_NONE || 68617866e14SMarek Szyprowski pdata->cd_type == S3C_SDHCI_CD_PERMANENT) 68717866e14SMarek Szyprowski host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION; 68817866e14SMarek Szyprowski 68917866e14SMarek Szyprowski if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT) 69017866e14SMarek Szyprowski host->mmc->caps = MMC_CAP_NONREMOVABLE; 69117866e14SMarek Szyprowski 6920d22c770SThomas Abraham switch (pdata->max_width) { 6930d22c770SThomas Abraham case 8: 6940d22c770SThomas Abraham host->mmc->caps |= MMC_CAP_8_BIT_DATA; 6950d22c770SThomas Abraham case 4: 6960d22c770SThomas Abraham host->mmc->caps |= MMC_CAP_4_BIT_DATA; 6970d22c770SThomas Abraham break; 6980d22c770SThomas Abraham } 6990d22c770SThomas Abraham 700fa1773ccSSangwook Lee if (pdata->pm_caps) 701fa1773ccSSangwook Lee host->mmc->pm_caps |= pdata->pm_caps; 702fa1773ccSSangwook Lee 7030d1bb41aSBen Dooks host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR | 7040d1bb41aSBen Dooks SDHCI_QUIRK_32BIT_DMA_SIZE); 7050d1bb41aSBen Dooks 7063fe42e07SHyuk Lee /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */ 7073fe42e07SHyuk Lee host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK; 7083fe42e07SHyuk Lee 709253e0a7cSJeongbae Seo /* 710253e0a7cSJeongbae Seo * If controller does not have internal clock divider, 711253e0a7cSJeongbae Seo * we can use overriding functions instead of default. 712253e0a7cSJeongbae Seo */ 7133119936aSThomas Abraham if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) { 714253e0a7cSJeongbae Seo sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock; 715253e0a7cSJeongbae Seo sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock; 716253e0a7cSJeongbae Seo sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock; 717253e0a7cSJeongbae Seo } 718253e0a7cSJeongbae Seo 719b3824f2cSJeongbae Seo /* It supports additional host capabilities if needed */ 720b3824f2cSJeongbae Seo if (pdata->host_caps) 721b3824f2cSJeongbae Seo host->mmc->caps |= pdata->host_caps; 722b3824f2cSJeongbae Seo 723c1c4b66dSJaehoon Chung if (pdata->host_caps2) 724c1c4b66dSJaehoon Chung host->mmc->caps2 |= pdata->host_caps2; 725c1c4b66dSJaehoon Chung 7269f4e8151SMark Brown pm_runtime_enable(&pdev->dev); 7279f4e8151SMark Brown pm_runtime_set_autosuspend_delay(&pdev->dev, 50); 7289f4e8151SMark Brown pm_runtime_use_autosuspend(&pdev->dev); 7299f4e8151SMark Brown pm_suspend_ignore_children(&pdev->dev, 1); 7309f4e8151SMark Brown 7310d1bb41aSBen Dooks ret = sdhci_add_host(host); 7320d1bb41aSBen Dooks if (ret) { 7330d1bb41aSBen Dooks dev_err(dev, "sdhci_add_host() failed\n"); 7349f4e8151SMark Brown pm_runtime_forbid(&pdev->dev); 7359f4e8151SMark Brown pm_runtime_get_noresume(&pdev->dev); 7369bda6da7SJulia Lawall goto err_req_regs; 7370d1bb41aSBen Dooks } 7380d1bb41aSBen Dooks 73917866e14SMarek Szyprowski /* The following two methods of card detection might call 74017866e14SMarek Szyprowski sdhci_s3c_notify_change() immediately, so they can be called 74117866e14SMarek Szyprowski only after sdhci_add_host(). Setup errors are ignored. */ 74217866e14SMarek Szyprowski if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init) 74317866e14SMarek Szyprowski pdata->ext_cd_init(&sdhci_s3c_notify_change); 74417866e14SMarek Szyprowski if (pdata->cd_type == S3C_SDHCI_CD_GPIO && 74517866e14SMarek Szyprowski gpio_is_valid(pdata->ext_cd_gpio)) 74617866e14SMarek Szyprowski sdhci_s3c_setup_card_detect_gpio(sc); 74717866e14SMarek Szyprowski 7482abeb5c5SChander Kashyap #ifdef CONFIG_PM_RUNTIME 7490aa55c23SSeungwon Jeon if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL) 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 768b1b8fea9STomasz Figa err_pdata_io_clk: 7690d1bb41aSBen Dooks sdhci_free_host(host); 7700d1bb41aSBen Dooks 7710d1bb41aSBen Dooks return ret; 7720d1bb41aSBen Dooks } 7730d1bb41aSBen Dooks 7740d1bb41aSBen Dooks static int __devexit sdhci_s3c_remove(struct platform_device *pdev) 7750d1bb41aSBen Dooks { 7769d51a6b2SMarek Szyprowski struct sdhci_host *host = platform_get_drvdata(pdev); 7779d51a6b2SMarek Szyprowski struct sdhci_s3c *sc = sdhci_priv(host); 778cd1b00ebSThomas Abraham struct s3c_sdhci_platdata *pdata = sc->pdata; 7799d51a6b2SMarek Szyprowski int ptr; 7809d51a6b2SMarek Szyprowski 78117866e14SMarek Szyprowski if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup) 78217866e14SMarek Szyprowski pdata->ext_cd_cleanup(&sdhci_s3c_notify_change); 78317866e14SMarek Szyprowski 78417866e14SMarek Szyprowski if (sc->ext_cd_irq) 78517866e14SMarek Szyprowski free_irq(sc->ext_cd_irq, sc); 78617866e14SMarek Szyprowski 7872abeb5c5SChander Kashyap #ifdef CONFIG_PM_RUNTIME 7880aa55c23SSeungwon Jeon if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL) 7890f310a05SThomas Abraham clk_prepare_enable(sc->clk_io); 7902abeb5c5SChander Kashyap #endif 7919d51a6b2SMarek Szyprowski sdhci_remove_host(host, 1); 7929d51a6b2SMarek Szyprowski 793387a8cbdSChander Kashyap pm_runtime_dont_use_autosuspend(&pdev->dev); 7949f4e8151SMark Brown pm_runtime_disable(&pdev->dev); 7959f4e8151SMark Brown 7962abeb5c5SChander Kashyap #ifndef CONFIG_PM_RUNTIME 7970f310a05SThomas Abraham clk_disable_unprepare(sc->clk_bus[sc->cur_clk]); 7982abeb5c5SChander Kashyap #endif 7995feb54a1SJaehoon Chung for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) { 8009320f7cbSMarek Szyprowski if (sc->clk_bus[ptr]) { 8019d51a6b2SMarek Szyprowski clk_put(sc->clk_bus[ptr]); 8029d51a6b2SMarek Szyprowski } 8039320f7cbSMarek Szyprowski } 8040f310a05SThomas Abraham clk_disable_unprepare(sc->clk_io); 8059d51a6b2SMarek Szyprowski clk_put(sc->clk_io); 8069d51a6b2SMarek Szyprowski 8079d51a6b2SMarek Szyprowski sdhci_free_host(host); 8089d51a6b2SMarek Szyprowski platform_set_drvdata(pdev, NULL); 8099d51a6b2SMarek Szyprowski 8100d1bb41aSBen Dooks return 0; 8110d1bb41aSBen Dooks } 8120d1bb41aSBen Dooks 813d5e9c02cSMark Brown #ifdef CONFIG_PM_SLEEP 81429495aa0SManuel Lauss static int sdhci_s3c_suspend(struct device *dev) 8150d1bb41aSBen Dooks { 81629495aa0SManuel Lauss struct sdhci_host *host = dev_get_drvdata(dev); 8170d1bb41aSBen Dooks 81829495aa0SManuel Lauss return sdhci_suspend_host(host); 8190d1bb41aSBen Dooks } 8200d1bb41aSBen Dooks 82129495aa0SManuel Lauss static int sdhci_s3c_resume(struct device *dev) 8220d1bb41aSBen Dooks { 82329495aa0SManuel Lauss struct sdhci_host *host = dev_get_drvdata(dev); 8240d1bb41aSBen Dooks 82565d13516SWonil Choi return sdhci_resume_host(host); 8260d1bb41aSBen Dooks } 827d5e9c02cSMark Brown #endif 8280d1bb41aSBen Dooks 8299f4e8151SMark Brown #ifdef CONFIG_PM_RUNTIME 8309f4e8151SMark Brown static int sdhci_s3c_runtime_suspend(struct device *dev) 8319f4e8151SMark Brown { 8329f4e8151SMark Brown struct sdhci_host *host = dev_get_drvdata(dev); 8332abeb5c5SChander Kashyap struct sdhci_s3c *ourhost = to_s3c(host); 8342abeb5c5SChander Kashyap struct clk *busclk = ourhost->clk_io; 8352abeb5c5SChander Kashyap int ret; 8369f4e8151SMark Brown 8372abeb5c5SChander Kashyap ret = sdhci_runtime_suspend_host(host); 8382abeb5c5SChander Kashyap 8390f310a05SThomas Abraham clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]); 8400f310a05SThomas Abraham clk_disable_unprepare(busclk); 8412abeb5c5SChander Kashyap return ret; 8429f4e8151SMark Brown } 8439f4e8151SMark Brown 8449f4e8151SMark Brown static int sdhci_s3c_runtime_resume(struct device *dev) 8459f4e8151SMark Brown { 8469f4e8151SMark Brown struct sdhci_host *host = dev_get_drvdata(dev); 8472abeb5c5SChander Kashyap struct sdhci_s3c *ourhost = to_s3c(host); 8482abeb5c5SChander Kashyap struct clk *busclk = ourhost->clk_io; 8492abeb5c5SChander Kashyap int ret; 8509f4e8151SMark Brown 8510f310a05SThomas Abraham clk_prepare_enable(busclk); 8520f310a05SThomas Abraham clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]); 8532abeb5c5SChander Kashyap ret = sdhci_runtime_resume_host(host); 8542abeb5c5SChander Kashyap return ret; 8559f4e8151SMark Brown } 8569f4e8151SMark Brown #endif 8579f4e8151SMark Brown 858d5e9c02cSMark Brown #ifdef CONFIG_PM 85929495aa0SManuel Lauss static const struct dev_pm_ops sdhci_s3c_pmops = { 860d5e9c02cSMark Brown SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume) 8619f4e8151SMark Brown SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume, 8629f4e8151SMark Brown NULL) 86329495aa0SManuel Lauss }; 86429495aa0SManuel Lauss 86529495aa0SManuel Lauss #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops) 86629495aa0SManuel Lauss 8670d1bb41aSBen Dooks #else 86829495aa0SManuel Lauss #define SDHCI_S3C_PMOPS NULL 8690d1bb41aSBen Dooks #endif 8700d1bb41aSBen Dooks 8713119936aSThomas Abraham #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212) 8723119936aSThomas Abraham static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = { 8733119936aSThomas Abraham .sdhci_quirks = SDHCI_QUIRK_NONSTANDARD_CLOCK, 8743119936aSThomas Abraham }; 8753119936aSThomas Abraham #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data) 8763119936aSThomas Abraham #else 8773119936aSThomas Abraham #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL) 8783119936aSThomas Abraham #endif 8793119936aSThomas Abraham 8803119936aSThomas Abraham static struct platform_device_id sdhci_s3c_driver_ids[] = { 8813119936aSThomas Abraham { 8823119936aSThomas Abraham .name = "s3c-sdhci", 8833119936aSThomas Abraham .driver_data = (kernel_ulong_t)NULL, 8843119936aSThomas Abraham }, { 8853119936aSThomas Abraham .name = "exynos4-sdhci", 8863119936aSThomas Abraham .driver_data = EXYNOS4_SDHCI_DRV_DATA, 8873119936aSThomas Abraham }, 8883119936aSThomas Abraham { } 8893119936aSThomas Abraham }; 8903119936aSThomas Abraham MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids); 8913119936aSThomas Abraham 892cd1b00ebSThomas Abraham #ifdef CONFIG_OF 893cd1b00ebSThomas Abraham static const struct of_device_id sdhci_s3c_dt_match[] = { 894cd1b00ebSThomas Abraham { .compatible = "samsung,s3c6410-sdhci", }, 895cd1b00ebSThomas Abraham { .compatible = "samsung,exynos4210-sdhci", 896cd1b00ebSThomas Abraham .data = (void *)EXYNOS4_SDHCI_DRV_DATA }, 897cd1b00ebSThomas Abraham {}, 898cd1b00ebSThomas Abraham }; 899cd1b00ebSThomas Abraham MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match); 900cd1b00ebSThomas Abraham #endif 901cd1b00ebSThomas Abraham 9020d1bb41aSBen Dooks static struct platform_driver sdhci_s3c_driver = { 9030d1bb41aSBen Dooks .probe = sdhci_s3c_probe, 9040d1bb41aSBen Dooks .remove = __devexit_p(sdhci_s3c_remove), 9053119936aSThomas Abraham .id_table = sdhci_s3c_driver_ids, 9060d1bb41aSBen Dooks .driver = { 9070d1bb41aSBen Dooks .owner = THIS_MODULE, 9080d1bb41aSBen Dooks .name = "s3c-sdhci", 909cd1b00ebSThomas Abraham .of_match_table = of_match_ptr(sdhci_s3c_dt_match), 91029495aa0SManuel Lauss .pm = SDHCI_S3C_PMOPS, 9110d1bb41aSBen Dooks }, 9120d1bb41aSBen Dooks }; 9130d1bb41aSBen Dooks 914d1f81a64SAxel Lin module_platform_driver(sdhci_s3c_driver); 9150d1bb41aSBen Dooks 9160d1bb41aSBen Dooks MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue"); 9170d1bb41aSBen Dooks MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>"); 9180d1bb41aSBen Dooks MODULE_LICENSE("GPL v2"); 9190d1bb41aSBen Dooks MODULE_ALIAS("platform:s3c-sdhci"); 920