sdhci-s3c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /* linux/drivers/mmc/host/sdhci-s3c.c
  2. *
  3. * Copyright 2008 Openmoko Inc.
  4. * Copyright 2008 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>
  6. * http://armlinux.simtec.co.uk/
  7. *
  8. * SDHCI (HSMMC) support for Samsung SoC
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <linux/gpio.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/pm.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/mmc/host.h>
  27. #include <plat/sdhci.h>
  28. #include <plat/regs-sdhci.h>
  29. #include "sdhci.h"
  30. #define MAX_BUS_CLK (4)
  31. /**
  32. * struct sdhci_s3c - S3C SDHCI instance
  33. * @host: The SDHCI host created
  34. * @pdev: The platform device we where created from.
  35. * @ioarea: The resource created when we claimed the IO area.
  36. * @pdata: The platform data for this controller.
  37. * @cur_clk: The index of the current bus clock.
  38. * @clk_io: The clock for the internal bus interface.
  39. * @clk_bus: The clocks that are available for the SD/MMC bus clock.
  40. */
  41. struct sdhci_s3c {
  42. struct sdhci_host *host;
  43. struct platform_device *pdev;
  44. struct resource *ioarea;
  45. struct s3c_sdhci_platdata *pdata;
  46. unsigned int cur_clk;
  47. int ext_cd_irq;
  48. int ext_cd_gpio;
  49. struct clk *clk_io;
  50. struct clk *clk_bus[MAX_BUS_CLK];
  51. };
  52. /**
  53. * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
  54. * @sdhci_quirks: sdhci host specific quirks.
  55. *
  56. * Specifies platform specific configuration of sdhci controller.
  57. * Note: A structure for driver specific platform data is used for future
  58. * expansion of its usage.
  59. */
  60. struct sdhci_s3c_drv_data {
  61. unsigned int sdhci_quirks;
  62. };
  63. static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
  64. {
  65. return sdhci_priv(host);
  66. }
  67. /**
  68. * get_curclk - convert ctrl2 register to clock source number
  69. * @ctrl2: Control2 register value.
  70. */
  71. static u32 get_curclk(u32 ctrl2)
  72. {
  73. ctrl2 &= S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  74. ctrl2 >>= S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  75. return ctrl2;
  76. }
  77. static void sdhci_s3c_check_sclk(struct sdhci_host *host)
  78. {
  79. struct sdhci_s3c *ourhost = to_s3c(host);
  80. u32 tmp = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  81. if (get_curclk(tmp) != ourhost->cur_clk) {
  82. dev_dbg(&ourhost->pdev->dev, "restored ctrl2 clock setting\n");
  83. tmp &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  84. tmp |= ourhost->cur_clk << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  85. writel(tmp, host->ioaddr + S3C_SDHCI_CONTROL2);
  86. }
  87. }
  88. /**
  89. * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
  90. * @host: The SDHCI host instance.
  91. *
  92. * Callback to return the maximum clock rate acheivable by the controller.
  93. */
  94. static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
  95. {
  96. struct sdhci_s3c *ourhost = to_s3c(host);
  97. struct clk *busclk;
  98. unsigned int rate, max;
  99. int clk;
  100. /* note, a reset will reset the clock source */
  101. sdhci_s3c_check_sclk(host);
  102. for (max = 0, clk = 0; clk < MAX_BUS_CLK; clk++) {
  103. busclk = ourhost->clk_bus[clk];
  104. if (!busclk)
  105. continue;
  106. rate = clk_get_rate(busclk);
  107. if (rate > max)
  108. max = rate;
  109. }
  110. return max;
  111. }
  112. /**
  113. * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
  114. * @ourhost: Our SDHCI instance.
  115. * @src: The source clock index.
  116. * @wanted: The clock frequency wanted.
  117. */
  118. static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
  119. unsigned int src,
  120. unsigned int wanted)
  121. {
  122. unsigned long rate;
  123. struct clk *clksrc = ourhost->clk_bus[src];
  124. int div;
  125. if (!clksrc)
  126. return UINT_MAX;
  127. /*
  128. * If controller uses a non-standard clock division, find the best clock
  129. * speed possible with selected clock source and skip the division.
  130. */
  131. if (ourhost->host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
  132. rate = clk_round_rate(clksrc, wanted);
  133. return wanted - rate;
  134. }
  135. rate = clk_get_rate(clksrc);
  136. for (div = 1; div < 256; div *= 2) {
  137. if ((rate / div) <= wanted)
  138. break;
  139. }
  140. dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
  141. src, rate, wanted, rate / div);
  142. return wanted - (rate / div);
  143. }
  144. /**
  145. * sdhci_s3c_set_clock - callback on clock change
  146. * @host: The SDHCI host being changed
  147. * @clock: The clock rate being requested.
  148. *
  149. * When the card's clock is going to be changed, look at the new frequency
  150. * and find the best clock source to go with it.
  151. */
  152. static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
  153. {
  154. struct sdhci_s3c *ourhost = to_s3c(host);
  155. unsigned int best = UINT_MAX;
  156. unsigned int delta;
  157. int best_src = 0;
  158. int src;
  159. u32 ctrl;
  160. /* don't bother if the clock is going off. */
  161. if (clock == 0)
  162. return;
  163. for (src = 0; src < MAX_BUS_CLK; src++) {
  164. delta = sdhci_s3c_consider_clock(ourhost, src, clock);
  165. if (delta < best) {
  166. best = delta;
  167. best_src = src;
  168. }
  169. }
  170. dev_dbg(&ourhost->pdev->dev,
  171. "selected source %d, clock %d, delta %d\n",
  172. best_src, clock, best);
  173. /* select the new clock source */
  174. if (ourhost->cur_clk != best_src) {
  175. struct clk *clk = ourhost->clk_bus[best_src];
  176. /* turn clock off to card before changing clock source */
  177. writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
  178. ourhost->cur_clk = best_src;
  179. host->max_clk = clk_get_rate(clk);
  180. ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  181. ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  182. ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  183. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
  184. }
  185. /* reprogram default hardware configuration */
  186. writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
  187. host->ioaddr + S3C64XX_SDHCI_CONTROL4);
  188. ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  189. ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
  190. S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
  191. S3C_SDHCI_CTRL2_ENFBCLKRX |
  192. S3C_SDHCI_CTRL2_DFCNT_NONE |
  193. S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
  194. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
  195. /* reconfigure the controller for new clock rate */
  196. ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
  197. if (clock < 25 * 1000000)
  198. ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
  199. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
  200. }
  201. /**
  202. * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
  203. * @host: The SDHCI host being queried
  204. *
  205. * To init mmc host properly a minimal clock value is needed. For high system
  206. * bus clock's values the standard formula gives values out of allowed range.
  207. * The clock still can be set to lower values, if clock source other then
  208. * system bus is selected.
  209. */
  210. static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
  211. {
  212. struct sdhci_s3c *ourhost = to_s3c(host);
  213. unsigned int delta, min = UINT_MAX;
  214. int src;
  215. for (src = 0; src < MAX_BUS_CLK; src++) {
  216. delta = sdhci_s3c_consider_clock(ourhost, src, 0);
  217. if (delta == UINT_MAX)
  218. continue;
  219. /* delta is a negative value in this case */
  220. if (-delta < min)
  221. min = -delta;
  222. }
  223. return min;
  224. }
  225. /* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
  226. static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
  227. {
  228. struct sdhci_s3c *ourhost = to_s3c(host);
  229. return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], UINT_MAX);
  230. }
  231. /* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
  232. static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
  233. {
  234. struct sdhci_s3c *ourhost = to_s3c(host);
  235. /*
  236. * initial clock can be in the frequency range of
  237. * 100KHz-400KHz, so we set it as max value.
  238. */
  239. return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], 400000);
  240. }
  241. /* sdhci_cmu_set_clock - callback on clock change.*/
  242. static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
  243. {
  244. struct sdhci_s3c *ourhost = to_s3c(host);
  245. struct device *dev = &ourhost->pdev->dev;
  246. unsigned long timeout;
  247. u16 clk = 0;
  248. /* don't bother if the clock is going off */
  249. if (clock == 0)
  250. return;
  251. sdhci_s3c_set_clock(host, clock);
  252. clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
  253. host->clock = clock;
  254. clk = SDHCI_CLOCK_INT_EN;
  255. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  256. /* Wait max 20 ms */
  257. timeout = 20;
  258. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  259. & SDHCI_CLOCK_INT_STABLE)) {
  260. if (timeout == 0) {
  261. dev_err(dev, "%s: Internal clock never stabilised.\n",
  262. mmc_hostname(host->mmc));
  263. return;
  264. }
  265. timeout--;
  266. mdelay(1);
  267. }
  268. clk |= SDHCI_CLOCK_CARD_EN;
  269. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  270. }
  271. /**
  272. * sdhci_s3c_platform_8bit_width - support 8bit buswidth
  273. * @host: The SDHCI host being queried
  274. * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
  275. *
  276. * We have 8-bit width support but is not a v3 controller.
  277. * So we add platform_8bit_width() and support 8bit width.
  278. */
  279. static int sdhci_s3c_platform_8bit_width(struct sdhci_host *host, int width)
  280. {
  281. u8 ctrl;
  282. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  283. switch (width) {
  284. case MMC_BUS_WIDTH_8:
  285. ctrl |= SDHCI_CTRL_8BITBUS;
  286. ctrl &= ~SDHCI_CTRL_4BITBUS;
  287. break;
  288. case MMC_BUS_WIDTH_4:
  289. ctrl |= SDHCI_CTRL_4BITBUS;
  290. ctrl &= ~SDHCI_CTRL_8BITBUS;
  291. break;
  292. default:
  293. ctrl &= ~SDHCI_CTRL_4BITBUS;
  294. ctrl &= ~SDHCI_CTRL_8BITBUS;
  295. break;
  296. }
  297. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  298. return 0;
  299. }
  300. static struct sdhci_ops sdhci_s3c_ops = {
  301. .get_max_clock = sdhci_s3c_get_max_clk,
  302. .set_clock = sdhci_s3c_set_clock,
  303. .get_min_clock = sdhci_s3c_get_min_clock,
  304. .platform_8bit_width = sdhci_s3c_platform_8bit_width,
  305. };
  306. static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
  307. {
  308. struct sdhci_host *host = platform_get_drvdata(dev);
  309. unsigned long flags;
  310. if (host) {
  311. spin_lock_irqsave(&host->lock, flags);
  312. if (state) {
  313. dev_dbg(&dev->dev, "card inserted.\n");
  314. host->flags &= ~SDHCI_DEVICE_DEAD;
  315. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  316. } else {
  317. dev_dbg(&dev->dev, "card removed.\n");
  318. host->flags |= SDHCI_DEVICE_DEAD;
  319. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  320. }
  321. tasklet_schedule(&host->card_tasklet);
  322. spin_unlock_irqrestore(&host->lock, flags);
  323. }
  324. }
  325. static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
  326. {
  327. struct sdhci_s3c *sc = dev_id;
  328. int status = gpio_get_value(sc->ext_cd_gpio);
  329. if (sc->pdata->ext_cd_gpio_invert)
  330. status = !status;
  331. sdhci_s3c_notify_change(sc->pdev, status);
  332. return IRQ_HANDLED;
  333. }
  334. static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
  335. {
  336. struct s3c_sdhci_platdata *pdata = sc->pdata;
  337. struct device *dev = &sc->pdev->dev;
  338. if (gpio_request(pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
  339. sc->ext_cd_gpio = pdata->ext_cd_gpio;
  340. sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
  341. if (sc->ext_cd_irq &&
  342. request_threaded_irq(sc->ext_cd_irq, NULL,
  343. sdhci_s3c_gpio_card_detect_thread,
  344. IRQF_TRIGGER_RISING |
  345. IRQF_TRIGGER_FALLING |
  346. IRQF_ONESHOT,
  347. dev_name(dev), sc) == 0) {
  348. int status = gpio_get_value(sc->ext_cd_gpio);
  349. if (pdata->ext_cd_gpio_invert)
  350. status = !status;
  351. sdhci_s3c_notify_change(sc->pdev, status);
  352. } else {
  353. dev_warn(dev, "cannot request irq for card detect\n");
  354. sc->ext_cd_irq = 0;
  355. }
  356. } else {
  357. dev_err(dev, "cannot request gpio for card detect\n");
  358. }
  359. }
  360. static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
  361. struct platform_device *pdev)
  362. {
  363. return (struct sdhci_s3c_drv_data *)
  364. platform_get_device_id(pdev)->driver_data;
  365. }
  366. static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
  367. {
  368. struct s3c_sdhci_platdata *pdata;
  369. struct sdhci_s3c_drv_data *drv_data;
  370. struct device *dev = &pdev->dev;
  371. struct sdhci_host *host;
  372. struct sdhci_s3c *sc;
  373. struct resource *res;
  374. int ret, irq, ptr, clks;
  375. if (!pdev->dev.platform_data) {
  376. dev_err(dev, "no device data specified\n");
  377. return -ENOENT;
  378. }
  379. irq = platform_get_irq(pdev, 0);
  380. if (irq < 0) {
  381. dev_err(dev, "no irq specified\n");
  382. return irq;
  383. }
  384. host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
  385. if (IS_ERR(host)) {
  386. dev_err(dev, "sdhci_alloc_host() failed\n");
  387. return PTR_ERR(host);
  388. }
  389. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  390. if (!pdata) {
  391. ret = -ENOMEM;
  392. goto err_io_clk;
  393. }
  394. memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
  395. drv_data = sdhci_s3c_get_driver_data(pdev);
  396. sc = sdhci_priv(host);
  397. sc->host = host;
  398. sc->pdev = pdev;
  399. sc->pdata = pdata;
  400. sc->ext_cd_gpio = -1; /* invalid gpio number */
  401. platform_set_drvdata(pdev, host);
  402. sc->clk_io = clk_get(dev, "hsmmc");
  403. if (IS_ERR(sc->clk_io)) {
  404. dev_err(dev, "failed to get io clock\n");
  405. ret = PTR_ERR(sc->clk_io);
  406. goto err_io_clk;
  407. }
  408. /* enable the local io clock and keep it running for the moment. */
  409. clk_enable(sc->clk_io);
  410. for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  411. struct clk *clk;
  412. char name[14];
  413. snprintf(name, 14, "mmc_busclk.%d", ptr);
  414. clk = clk_get(dev, name);
  415. if (IS_ERR(clk))
  416. continue;
  417. clks++;
  418. sc->clk_bus[ptr] = clk;
  419. /*
  420. * save current clock index to know which clock bus
  421. * is used later in overriding functions.
  422. */
  423. sc->cur_clk = ptr;
  424. clk_enable(clk);
  425. dev_info(dev, "clock source %d: %s (%ld Hz)\n",
  426. ptr, name, clk_get_rate(clk));
  427. }
  428. if (clks == 0) {
  429. dev_err(dev, "failed to find any bus clocks\n");
  430. ret = -ENOENT;
  431. goto err_no_busclks;
  432. }
  433. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  434. host->ioaddr = devm_request_and_ioremap(&pdev->dev, res);
  435. if (!host->ioaddr) {
  436. dev_err(dev, "failed to map registers\n");
  437. ret = -ENXIO;
  438. goto err_req_regs;
  439. }
  440. /* Ensure we have minimal gpio selected CMD/CLK/Detect */
  441. if (pdata->cfg_gpio)
  442. pdata->cfg_gpio(pdev, pdata->max_width);
  443. host->hw_name = "samsung-hsmmc";
  444. host->ops = &sdhci_s3c_ops;
  445. host->quirks = 0;
  446. host->irq = irq;
  447. /* Setup quirks for the controller */
  448. host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
  449. host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
  450. if (drv_data)
  451. host->quirks |= drv_data->sdhci_quirks;
  452. #ifndef CONFIG_MMC_SDHCI_S3C_DMA
  453. /* we currently see overruns on errors, so disable the SDMA
  454. * support as well. */
  455. host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
  456. #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
  457. /* It seems we do not get an DATA transfer complete on non-busy
  458. * transfers, not sure if this is a problem with this specific
  459. * SDHCI block, or a missing configuration that needs to be set. */
  460. host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
  461. /* This host supports the Auto CMD12 */
  462. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  463. /* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
  464. host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
  465. if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
  466. pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
  467. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  468. if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
  469. host->mmc->caps = MMC_CAP_NONREMOVABLE;
  470. switch (pdata->max_width) {
  471. case 8:
  472. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  473. case 4:
  474. host->mmc->caps |= MMC_CAP_4_BIT_DATA;
  475. break;
  476. }
  477. if (pdata->pm_caps)
  478. host->mmc->pm_caps |= pdata->pm_caps;
  479. host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
  480. SDHCI_QUIRK_32BIT_DMA_SIZE);
  481. /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
  482. host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
  483. /*
  484. * If controller does not have internal clock divider,
  485. * we can use overriding functions instead of default.
  486. */
  487. if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
  488. sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
  489. sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
  490. sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
  491. }
  492. /* It supports additional host capabilities if needed */
  493. if (pdata->host_caps)
  494. host->mmc->caps |= pdata->host_caps;
  495. if (pdata->host_caps2)
  496. host->mmc->caps2 |= pdata->host_caps2;
  497. pm_runtime_enable(&pdev->dev);
  498. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  499. pm_runtime_use_autosuspend(&pdev->dev);
  500. pm_suspend_ignore_children(&pdev->dev, 1);
  501. ret = sdhci_add_host(host);
  502. if (ret) {
  503. dev_err(dev, "sdhci_add_host() failed\n");
  504. pm_runtime_forbid(&pdev->dev);
  505. pm_runtime_get_noresume(&pdev->dev);
  506. goto err_req_regs;
  507. }
  508. /* The following two methods of card detection might call
  509. sdhci_s3c_notify_change() immediately, so they can be called
  510. only after sdhci_add_host(). Setup errors are ignored. */
  511. if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
  512. pdata->ext_cd_init(&sdhci_s3c_notify_change);
  513. if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
  514. gpio_is_valid(pdata->ext_cd_gpio))
  515. sdhci_s3c_setup_card_detect_gpio(sc);
  516. return 0;
  517. err_req_regs:
  518. for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  519. if (sc->clk_bus[ptr]) {
  520. clk_disable(sc->clk_bus[ptr]);
  521. clk_put(sc->clk_bus[ptr]);
  522. }
  523. }
  524. err_no_busclks:
  525. clk_disable(sc->clk_io);
  526. clk_put(sc->clk_io);
  527. err_io_clk:
  528. sdhci_free_host(host);
  529. return ret;
  530. }
  531. static int __devexit sdhci_s3c_remove(struct platform_device *pdev)
  532. {
  533. struct s3c_sdhci_platdata *pdata = pdev->dev.platform_data;
  534. struct sdhci_host *host = platform_get_drvdata(pdev);
  535. struct sdhci_s3c *sc = sdhci_priv(host);
  536. int ptr;
  537. if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
  538. pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
  539. if (sc->ext_cd_irq)
  540. free_irq(sc->ext_cd_irq, sc);
  541. if (gpio_is_valid(sc->ext_cd_gpio))
  542. gpio_free(sc->ext_cd_gpio);
  543. sdhci_remove_host(host, 1);
  544. pm_runtime_disable(&pdev->dev);
  545. for (ptr = 0; ptr < 3; ptr++) {
  546. if (sc->clk_bus[ptr]) {
  547. clk_disable(sc->clk_bus[ptr]);
  548. clk_put(sc->clk_bus[ptr]);
  549. }
  550. }
  551. clk_disable(sc->clk_io);
  552. clk_put(sc->clk_io);
  553. sdhci_free_host(host);
  554. platform_set_drvdata(pdev, NULL);
  555. return 0;
  556. }
  557. #ifdef CONFIG_PM_SLEEP
  558. static int sdhci_s3c_suspend(struct device *dev)
  559. {
  560. struct sdhci_host *host = dev_get_drvdata(dev);
  561. return sdhci_suspend_host(host);
  562. }
  563. static int sdhci_s3c_resume(struct device *dev)
  564. {
  565. struct sdhci_host *host = dev_get_drvdata(dev);
  566. return sdhci_resume_host(host);
  567. }
  568. #endif
  569. #ifdef CONFIG_PM_RUNTIME
  570. static int sdhci_s3c_runtime_suspend(struct device *dev)
  571. {
  572. struct sdhci_host *host = dev_get_drvdata(dev);
  573. return sdhci_runtime_suspend_host(host);
  574. }
  575. static int sdhci_s3c_runtime_resume(struct device *dev)
  576. {
  577. struct sdhci_host *host = dev_get_drvdata(dev);
  578. return sdhci_runtime_resume_host(host);
  579. }
  580. #endif
  581. #ifdef CONFIG_PM
  582. static const struct dev_pm_ops sdhci_s3c_pmops = {
  583. SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
  584. SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
  585. NULL)
  586. };
  587. #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
  588. #else
  589. #define SDHCI_S3C_PMOPS NULL
  590. #endif
  591. #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
  592. static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
  593. .sdhci_quirks = SDHCI_QUIRK_NONSTANDARD_CLOCK,
  594. };
  595. #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
  596. #else
  597. #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
  598. #endif
  599. static struct platform_device_id sdhci_s3c_driver_ids[] = {
  600. {
  601. .name = "s3c-sdhci",
  602. .driver_data = (kernel_ulong_t)NULL,
  603. }, {
  604. .name = "exynos4-sdhci",
  605. .driver_data = EXYNOS4_SDHCI_DRV_DATA,
  606. },
  607. { }
  608. };
  609. MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
  610. static struct platform_driver sdhci_s3c_driver = {
  611. .probe = sdhci_s3c_probe,
  612. .remove = __devexit_p(sdhci_s3c_remove),
  613. .id_table = sdhci_s3c_driver_ids,
  614. .driver = {
  615. .owner = THIS_MODULE,
  616. .name = "s3c-sdhci",
  617. .pm = SDHCI_S3C_PMOPS,
  618. },
  619. };
  620. module_platform_driver(sdhci_s3c_driver);
  621. MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
  622. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  623. MODULE_LICENSE("GPL v2");
  624. MODULE_ALIAS("platform:s3c-sdhci");