sdhci-s3c.c 19 KB

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