sdhci-s3c.c 19 KB

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