sdhci-s3c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. host->mmc->actual_clock = 0;
  256. /* If the clock is going off, set to 0 at clock control register */
  257. if (clock == 0) {
  258. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  259. return;
  260. }
  261. sdhci_s3c_set_clock(host, clock);
  262. clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
  263. clk = SDHCI_CLOCK_INT_EN;
  264. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  265. /* Wait max 20 ms */
  266. timeout = 20;
  267. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  268. & SDHCI_CLOCK_INT_STABLE)) {
  269. if (timeout == 0) {
  270. dev_err(dev, "%s: Internal clock never stabilised.\n",
  271. mmc_hostname(host->mmc));
  272. return;
  273. }
  274. timeout--;
  275. mdelay(1);
  276. }
  277. clk |= SDHCI_CLOCK_CARD_EN;
  278. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  279. }
  280. /**
  281. * sdhci_s3c_set_bus_width - support 8bit buswidth
  282. * @host: The SDHCI host being queried
  283. * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
  284. *
  285. * We have 8-bit width support but is not a v3 controller.
  286. * So we add platform_bus_width() and support 8bit width.
  287. */
  288. static void sdhci_s3c_set_bus_width(struct sdhci_host *host, int width)
  289. {
  290. u8 ctrl;
  291. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  292. switch (width) {
  293. case MMC_BUS_WIDTH_8:
  294. ctrl |= SDHCI_CTRL_8BITBUS;
  295. ctrl &= ~SDHCI_CTRL_4BITBUS;
  296. break;
  297. case MMC_BUS_WIDTH_4:
  298. ctrl |= SDHCI_CTRL_4BITBUS;
  299. ctrl &= ~SDHCI_CTRL_8BITBUS;
  300. break;
  301. default:
  302. ctrl &= ~SDHCI_CTRL_4BITBUS;
  303. ctrl &= ~SDHCI_CTRL_8BITBUS;
  304. break;
  305. }
  306. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  307. }
  308. static struct sdhci_ops sdhci_s3c_ops = {
  309. .get_max_clock = sdhci_s3c_get_max_clk,
  310. .set_clock = sdhci_s3c_set_clock,
  311. .get_min_clock = sdhci_s3c_get_min_clock,
  312. .set_bus_width = sdhci_s3c_set_bus_width,
  313. .reset = sdhci_reset,
  314. .set_uhs_signaling = sdhci_set_uhs_signaling,
  315. };
  316. #ifdef CONFIG_OF
  317. static int sdhci_s3c_parse_dt(struct device *dev,
  318. struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
  319. {
  320. struct device_node *node = dev->of_node;
  321. u32 max_width;
  322. /* if the bus-width property is not specified, assume width as 1 */
  323. if (of_property_read_u32(node, "bus-width", &max_width))
  324. max_width = 1;
  325. pdata->max_width = max_width;
  326. /* get the card detection method */
  327. if (of_get_property(node, "broken-cd", NULL)) {
  328. pdata->cd_type = S3C_SDHCI_CD_NONE;
  329. return 0;
  330. }
  331. if (of_get_property(node, "non-removable", NULL)) {
  332. pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
  333. return 0;
  334. }
  335. if (of_get_named_gpio(node, "cd-gpios", 0))
  336. return 0;
  337. /* assuming internal card detect that will be configured by pinctrl */
  338. pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
  339. return 0;
  340. }
  341. #else
  342. static int sdhci_s3c_parse_dt(struct device *dev,
  343. struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
  344. {
  345. return -EINVAL;
  346. }
  347. #endif
  348. static const struct of_device_id sdhci_s3c_dt_match[];
  349. static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
  350. struct platform_device *pdev)
  351. {
  352. #ifdef CONFIG_OF
  353. if (pdev->dev.of_node) {
  354. const struct of_device_id *match;
  355. match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
  356. return (struct sdhci_s3c_drv_data *)match->data;
  357. }
  358. #endif
  359. return (struct sdhci_s3c_drv_data *)
  360. platform_get_device_id(pdev)->driver_data;
  361. }
  362. static int sdhci_s3c_probe(struct platform_device *pdev)
  363. {
  364. struct s3c_sdhci_platdata *pdata;
  365. struct sdhci_s3c_drv_data *drv_data;
  366. struct device *dev = &pdev->dev;
  367. struct sdhci_host *host;
  368. struct sdhci_s3c *sc;
  369. struct resource *res;
  370. int ret, irq, ptr, clks;
  371. if (!pdev->dev.platform_data && !pdev->dev.of_node) {
  372. dev_err(dev, "no device data specified\n");
  373. return -ENOENT;
  374. }
  375. irq = platform_get_irq(pdev, 0);
  376. if (irq < 0) {
  377. dev_err(dev, "no irq specified\n");
  378. return irq;
  379. }
  380. host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
  381. if (IS_ERR(host)) {
  382. dev_err(dev, "sdhci_alloc_host() failed\n");
  383. return PTR_ERR(host);
  384. }
  385. sc = sdhci_priv(host);
  386. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  387. if (!pdata) {
  388. ret = -ENOMEM;
  389. goto err_pdata_io_clk;
  390. }
  391. if (pdev->dev.of_node) {
  392. ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
  393. if (ret)
  394. goto err_pdata_io_clk;
  395. } else {
  396. memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
  397. sc->ext_cd_gpio = -1; /* invalid gpio number */
  398. }
  399. drv_data = sdhci_s3c_get_driver_data(pdev);
  400. sc->host = host;
  401. sc->pdev = pdev;
  402. sc->pdata = pdata;
  403. sc->cur_clk = -1;
  404. platform_set_drvdata(pdev, host);
  405. sc->clk_io = devm_clk_get(dev, "hsmmc");
  406. if (IS_ERR(sc->clk_io)) {
  407. dev_err(dev, "failed to get io clock\n");
  408. ret = PTR_ERR(sc->clk_io);
  409. goto err_pdata_io_clk;
  410. }
  411. /* enable the local io clock and keep it running for the moment. */
  412. clk_prepare_enable(sc->clk_io);
  413. for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  414. char name[14];
  415. snprintf(name, 14, "mmc_busclk.%d", ptr);
  416. sc->clk_bus[ptr] = devm_clk_get(dev, name);
  417. if (IS_ERR(sc->clk_bus[ptr]))
  418. continue;
  419. clks++;
  420. sc->clk_rates[ptr] = clk_get_rate(sc->clk_bus[ptr]);
  421. dev_info(dev, "clock source %d: %s (%ld Hz)\n",
  422. ptr, name, sc->clk_rates[ptr]);
  423. }
  424. if (clks == 0) {
  425. dev_err(dev, "failed to find any bus clocks\n");
  426. ret = -ENOENT;
  427. goto err_no_busclks;
  428. }
  429. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  430. host->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  431. if (IS_ERR(host->ioaddr)) {
  432. ret = PTR_ERR(host->ioaddr);
  433. goto err_req_regs;
  434. }
  435. /* Ensure we have minimal gpio selected CMD/CLK/Detect */
  436. if (pdata->cfg_gpio)
  437. pdata->cfg_gpio(pdev, pdata->max_width);
  438. host->hw_name = "samsung-hsmmc";
  439. host->ops = &sdhci_s3c_ops;
  440. host->quirks = 0;
  441. host->quirks2 = 0;
  442. host->irq = irq;
  443. /* Setup quirks for the controller */
  444. host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
  445. host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
  446. if (drv_data) {
  447. host->quirks |= drv_data->sdhci_quirks;
  448. sc->no_divider = drv_data->no_divider;
  449. }
  450. #ifndef CONFIG_MMC_SDHCI_S3C_DMA
  451. /* we currently see overruns on errors, so disable the SDMA
  452. * support as well. */
  453. host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
  454. #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
  455. /* It seems we do not get an DATA transfer complete on non-busy
  456. * transfers, not sure if this is a problem with this specific
  457. * SDHCI block, or a missing configuration that needs to be set. */
  458. host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
  459. /* This host supports the Auto CMD12 */
  460. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  461. /* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
  462. host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
  463. if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
  464. pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
  465. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  466. if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
  467. host->mmc->caps = MMC_CAP_NONREMOVABLE;
  468. switch (pdata->max_width) {
  469. case 8:
  470. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  471. case 4:
  472. host->mmc->caps |= MMC_CAP_4_BIT_DATA;
  473. break;
  474. }
  475. if (pdata->pm_caps)
  476. host->mmc->pm_caps |= pdata->pm_caps;
  477. host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
  478. SDHCI_QUIRK_32BIT_DMA_SIZE);
  479. /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
  480. host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
  481. /*
  482. * If controller does not have internal clock divider,
  483. * we can use overriding functions instead of default.
  484. */
  485. if (sc->no_divider) {
  486. sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
  487. sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
  488. sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
  489. }
  490. /* It supports additional host capabilities if needed */
  491. if (pdata->host_caps)
  492. host->mmc->caps |= pdata->host_caps;
  493. if (pdata->host_caps2)
  494. host->mmc->caps2 |= pdata->host_caps2;
  495. pm_runtime_enable(&pdev->dev);
  496. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  497. pm_runtime_use_autosuspend(&pdev->dev);
  498. pm_suspend_ignore_children(&pdev->dev, 1);
  499. mmc_of_parse(host->mmc);
  500. ret = sdhci_add_host(host);
  501. if (ret) {
  502. dev_err(dev, "sdhci_add_host() failed\n");
  503. pm_runtime_forbid(&pdev->dev);
  504. pm_runtime_get_noresume(&pdev->dev);
  505. goto err_req_regs;
  506. }
  507. #ifdef CONFIG_PM_RUNTIME
  508. if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
  509. clk_disable_unprepare(sc->clk_io);
  510. #endif
  511. return 0;
  512. err_req_regs:
  513. err_no_busclks:
  514. clk_disable_unprepare(sc->clk_io);
  515. err_pdata_io_clk:
  516. sdhci_free_host(host);
  517. return ret;
  518. }
  519. static int sdhci_s3c_remove(struct platform_device *pdev)
  520. {
  521. struct sdhci_host *host = platform_get_drvdata(pdev);
  522. struct sdhci_s3c *sc = sdhci_priv(host);
  523. if (sc->ext_cd_irq)
  524. free_irq(sc->ext_cd_irq, sc);
  525. #ifdef CONFIG_PM_RUNTIME
  526. if (sc->pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
  527. clk_prepare_enable(sc->clk_io);
  528. #endif
  529. sdhci_remove_host(host, 1);
  530. pm_runtime_dont_use_autosuspend(&pdev->dev);
  531. pm_runtime_disable(&pdev->dev);
  532. clk_disable_unprepare(sc->clk_io);
  533. sdhci_free_host(host);
  534. return 0;
  535. }
  536. #ifdef CONFIG_PM_SLEEP
  537. static int sdhci_s3c_suspend(struct device *dev)
  538. {
  539. struct sdhci_host *host = dev_get_drvdata(dev);
  540. return sdhci_suspend_host(host);
  541. }
  542. static int sdhci_s3c_resume(struct device *dev)
  543. {
  544. struct sdhci_host *host = dev_get_drvdata(dev);
  545. return sdhci_resume_host(host);
  546. }
  547. #endif
  548. #ifdef CONFIG_PM_RUNTIME
  549. static int sdhci_s3c_runtime_suspend(struct device *dev)
  550. {
  551. struct sdhci_host *host = dev_get_drvdata(dev);
  552. struct sdhci_s3c *ourhost = to_s3c(host);
  553. struct clk *busclk = ourhost->clk_io;
  554. int ret;
  555. ret = sdhci_runtime_suspend_host(host);
  556. if (ourhost->cur_clk >= 0)
  557. clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
  558. clk_disable_unprepare(busclk);
  559. return ret;
  560. }
  561. static int sdhci_s3c_runtime_resume(struct device *dev)
  562. {
  563. struct sdhci_host *host = dev_get_drvdata(dev);
  564. struct sdhci_s3c *ourhost = to_s3c(host);
  565. struct clk *busclk = ourhost->clk_io;
  566. int ret;
  567. clk_prepare_enable(busclk);
  568. if (ourhost->cur_clk >= 0)
  569. clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
  570. ret = sdhci_runtime_resume_host(host);
  571. return ret;
  572. }
  573. #endif
  574. #ifdef CONFIG_PM
  575. static const struct dev_pm_ops sdhci_s3c_pmops = {
  576. SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
  577. SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
  578. NULL)
  579. };
  580. #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
  581. #else
  582. #define SDHCI_S3C_PMOPS NULL
  583. #endif
  584. #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
  585. static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
  586. .no_divider = true,
  587. };
  588. #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
  589. #else
  590. #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
  591. #endif
  592. static struct platform_device_id sdhci_s3c_driver_ids[] = {
  593. {
  594. .name = "s3c-sdhci",
  595. .driver_data = (kernel_ulong_t)NULL,
  596. }, {
  597. .name = "exynos4-sdhci",
  598. .driver_data = EXYNOS4_SDHCI_DRV_DATA,
  599. },
  600. { }
  601. };
  602. MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
  603. #ifdef CONFIG_OF
  604. static const struct of_device_id sdhci_s3c_dt_match[] = {
  605. { .compatible = "samsung,s3c6410-sdhci", },
  606. { .compatible = "samsung,exynos4210-sdhci",
  607. .data = (void *)EXYNOS4_SDHCI_DRV_DATA },
  608. {},
  609. };
  610. MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
  611. #endif
  612. static struct platform_driver sdhci_s3c_driver = {
  613. .probe = sdhci_s3c_probe,
  614. .remove = sdhci_s3c_remove,
  615. .id_table = sdhci_s3c_driver_ids,
  616. .driver = {
  617. .owner = THIS_MODULE,
  618. .name = "s3c-sdhci",
  619. .of_match_table = of_match_ptr(sdhci_s3c_dt_match),
  620. .pm = SDHCI_S3C_PMOPS,
  621. },
  622. };
  623. module_platform_driver(sdhci_s3c_driver);
  624. MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
  625. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  626. MODULE_LICENSE("GPL v2");
  627. MODULE_ALIAS("platform:s3c-sdhci");