sdhci-s3c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. unsigned long timeout;
  246. u16 clk = 0;
  247. /* don't bother if the clock is going off */
  248. if (clock == 0)
  249. return;
  250. sdhci_s3c_set_clock(host, clock);
  251. clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
  252. host->clock = clock;
  253. clk = SDHCI_CLOCK_INT_EN;
  254. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  255. /* Wait max 20 ms */
  256. timeout = 20;
  257. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  258. & SDHCI_CLOCK_INT_STABLE)) {
  259. if (timeout == 0) {
  260. printk(KERN_ERR "%s: Internal clock never "
  261. "stabilised.\n", mmc_hostname(host->mmc));
  262. return;
  263. }
  264. timeout--;
  265. mdelay(1);
  266. }
  267. clk |= SDHCI_CLOCK_CARD_EN;
  268. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  269. }
  270. /**
  271. * sdhci_s3c_platform_8bit_width - support 8bit buswidth
  272. * @host: The SDHCI host being queried
  273. * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
  274. *
  275. * We have 8-bit width support but is not a v3 controller.
  276. * So we add platform_8bit_width() and support 8bit width.
  277. */
  278. static int sdhci_s3c_platform_8bit_width(struct sdhci_host *host, int width)
  279. {
  280. u8 ctrl;
  281. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  282. switch (width) {
  283. case MMC_BUS_WIDTH_8:
  284. ctrl |= SDHCI_CTRL_8BITBUS;
  285. ctrl &= ~SDHCI_CTRL_4BITBUS;
  286. break;
  287. case MMC_BUS_WIDTH_4:
  288. ctrl |= SDHCI_CTRL_4BITBUS;
  289. ctrl &= ~SDHCI_CTRL_8BITBUS;
  290. break;
  291. default:
  292. ctrl &= ~SDHCI_CTRL_4BITBUS;
  293. ctrl &= ~SDHCI_CTRL_8BITBUS;
  294. break;
  295. }
  296. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  297. return 0;
  298. }
  299. static struct sdhci_ops sdhci_s3c_ops = {
  300. .get_max_clock = sdhci_s3c_get_max_clk,
  301. .set_clock = sdhci_s3c_set_clock,
  302. .get_min_clock = sdhci_s3c_get_min_clock,
  303. .platform_8bit_width = sdhci_s3c_platform_8bit_width,
  304. };
  305. static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
  306. {
  307. struct sdhci_host *host = platform_get_drvdata(dev);
  308. unsigned long flags;
  309. if (host) {
  310. spin_lock_irqsave(&host->lock, flags);
  311. if (state) {
  312. dev_dbg(&dev->dev, "card inserted.\n");
  313. host->flags &= ~SDHCI_DEVICE_DEAD;
  314. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  315. } else {
  316. dev_dbg(&dev->dev, "card removed.\n");
  317. host->flags |= SDHCI_DEVICE_DEAD;
  318. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  319. }
  320. tasklet_schedule(&host->card_tasklet);
  321. spin_unlock_irqrestore(&host->lock, flags);
  322. }
  323. }
  324. static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
  325. {
  326. struct sdhci_s3c *sc = dev_id;
  327. int status = gpio_get_value(sc->ext_cd_gpio);
  328. if (sc->pdata->ext_cd_gpio_invert)
  329. status = !status;
  330. sdhci_s3c_notify_change(sc->pdev, status);
  331. return IRQ_HANDLED;
  332. }
  333. static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
  334. {
  335. struct s3c_sdhci_platdata *pdata = sc->pdata;
  336. struct device *dev = &sc->pdev->dev;
  337. if (gpio_request(pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
  338. sc->ext_cd_gpio = pdata->ext_cd_gpio;
  339. sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
  340. if (sc->ext_cd_irq &&
  341. request_threaded_irq(sc->ext_cd_irq, NULL,
  342. sdhci_s3c_gpio_card_detect_thread,
  343. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  344. dev_name(dev), sc) == 0) {
  345. int status = gpio_get_value(sc->ext_cd_gpio);
  346. if (pdata->ext_cd_gpio_invert)
  347. status = !status;
  348. sdhci_s3c_notify_change(sc->pdev, status);
  349. } else {
  350. dev_warn(dev, "cannot request irq for card detect\n");
  351. sc->ext_cd_irq = 0;
  352. }
  353. } else {
  354. dev_err(dev, "cannot request gpio for card detect\n");
  355. }
  356. }
  357. static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
  358. struct platform_device *pdev)
  359. {
  360. return (struct sdhci_s3c_drv_data *)
  361. platform_get_device_id(pdev)->driver_data;
  362. }
  363. static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
  364. {
  365. struct s3c_sdhci_platdata *pdata;
  366. struct sdhci_s3c_drv_data *drv_data;
  367. struct device *dev = &pdev->dev;
  368. struct sdhci_host *host;
  369. struct sdhci_s3c *sc;
  370. struct resource *res;
  371. int ret, irq, ptr, clks;
  372. if (!pdev->dev.platform_data) {
  373. dev_err(dev, "no device data specified\n");
  374. return -ENOENT;
  375. }
  376. irq = platform_get_irq(pdev, 0);
  377. if (irq < 0) {
  378. dev_err(dev, "no irq specified\n");
  379. return irq;
  380. }
  381. host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
  382. if (IS_ERR(host)) {
  383. dev_err(dev, "sdhci_alloc_host() failed\n");
  384. return PTR_ERR(host);
  385. }
  386. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  387. if (!pdata) {
  388. ret = -ENOMEM;
  389. goto err_io_clk;
  390. }
  391. memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
  392. drv_data = sdhci_s3c_get_driver_data(pdev);
  393. sc = sdhci_priv(host);
  394. sc->host = host;
  395. sc->pdev = pdev;
  396. sc->pdata = pdata;
  397. sc->ext_cd_gpio = -1; /* invalid gpio number */
  398. platform_set_drvdata(pdev, host);
  399. sc->clk_io = clk_get(dev, "hsmmc");
  400. if (IS_ERR(sc->clk_io)) {
  401. dev_err(dev, "failed to get io clock\n");
  402. ret = PTR_ERR(sc->clk_io);
  403. goto err_io_clk;
  404. }
  405. /* enable the local io clock and keep it running for the moment. */
  406. clk_enable(sc->clk_io);
  407. for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  408. struct clk *clk;
  409. char name[14];
  410. snprintf(name, 14, "mmc_busclk.%d", ptr);
  411. clk = clk_get(dev, name);
  412. if (IS_ERR(clk)) {
  413. continue;
  414. }
  415. clks++;
  416. sc->clk_bus[ptr] = clk;
  417. /*
  418. * save current clock index to know which clock bus
  419. * is used later in overriding functions.
  420. */
  421. sc->cur_clk = ptr;
  422. clk_enable(clk);
  423. dev_info(dev, "clock source %d: %s (%ld Hz)\n",
  424. ptr, name, clk_get_rate(clk));
  425. }
  426. if (clks == 0) {
  427. dev_err(dev, "failed to find any bus clocks\n");
  428. ret = -ENOENT;
  429. goto err_no_busclks;
  430. }
  431. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  432. host->ioaddr = devm_request_and_ioremap(&pdev->dev, res);
  433. if (!host->ioaddr) {
  434. dev_err(dev, "failed to map registers\n");
  435. ret = -ENXIO;
  436. goto err_req_regs;
  437. }
  438. /* Ensure we have minimal gpio selected CMD/CLK/Detect */
  439. if (pdata->cfg_gpio)
  440. pdata->cfg_gpio(pdev, pdata->max_width);
  441. host->hw_name = "samsung-hsmmc";
  442. host->ops = &sdhci_s3c_ops;
  443. host->quirks = 0;
  444. host->irq = irq;
  445. /* Setup quirks for the controller */
  446. host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
  447. host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
  448. if (drv_data)
  449. host->quirks |= drv_data->sdhci_quirks;
  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 (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK) {
  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. ret = sdhci_add_host(host);
  500. if (ret) {
  501. dev_err(dev, "sdhci_add_host() failed\n");
  502. pm_runtime_forbid(&pdev->dev);
  503. pm_runtime_get_noresume(&pdev->dev);
  504. goto err_req_regs;
  505. }
  506. /* The following two methods of card detection might call
  507. sdhci_s3c_notify_change() immediately, so they can be called
  508. only after sdhci_add_host(). Setup errors are ignored. */
  509. if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
  510. pdata->ext_cd_init(&sdhci_s3c_notify_change);
  511. if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
  512. gpio_is_valid(pdata->ext_cd_gpio))
  513. sdhci_s3c_setup_card_detect_gpio(sc);
  514. return 0;
  515. err_req_regs:
  516. for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  517. if (sc->clk_bus[ptr]) {
  518. clk_disable(sc->clk_bus[ptr]);
  519. clk_put(sc->clk_bus[ptr]);
  520. }
  521. }
  522. err_no_busclks:
  523. clk_disable(sc->clk_io);
  524. clk_put(sc->clk_io);
  525. err_io_clk:
  526. sdhci_free_host(host);
  527. return ret;
  528. }
  529. static int __devexit sdhci_s3c_remove(struct platform_device *pdev)
  530. {
  531. struct s3c_sdhci_platdata *pdata = pdev->dev.platform_data;
  532. struct sdhci_host *host = platform_get_drvdata(pdev);
  533. struct sdhci_s3c *sc = sdhci_priv(host);
  534. int ptr;
  535. if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
  536. pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
  537. if (sc->ext_cd_irq)
  538. free_irq(sc->ext_cd_irq, sc);
  539. if (gpio_is_valid(sc->ext_cd_gpio))
  540. gpio_free(sc->ext_cd_gpio);
  541. sdhci_remove_host(host, 1);
  542. pm_runtime_disable(&pdev->dev);
  543. for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  544. if (sc->clk_bus[ptr]) {
  545. clk_disable(sc->clk_bus[ptr]);
  546. clk_put(sc->clk_bus[ptr]);
  547. }
  548. }
  549. clk_disable(sc->clk_io);
  550. clk_put(sc->clk_io);
  551. sdhci_free_host(host);
  552. platform_set_drvdata(pdev, NULL);
  553. return 0;
  554. }
  555. #ifdef CONFIG_PM_SLEEP
  556. static int sdhci_s3c_suspend(struct device *dev)
  557. {
  558. struct sdhci_host *host = dev_get_drvdata(dev);
  559. return sdhci_suspend_host(host);
  560. }
  561. static int sdhci_s3c_resume(struct device *dev)
  562. {
  563. struct sdhci_host *host = dev_get_drvdata(dev);
  564. return sdhci_resume_host(host);
  565. }
  566. #endif
  567. #ifdef CONFIG_PM_RUNTIME
  568. static int sdhci_s3c_runtime_suspend(struct device *dev)
  569. {
  570. struct sdhci_host *host = dev_get_drvdata(dev);
  571. return sdhci_runtime_suspend_host(host);
  572. }
  573. static int sdhci_s3c_runtime_resume(struct device *dev)
  574. {
  575. struct sdhci_host *host = dev_get_drvdata(dev);
  576. return sdhci_runtime_resume_host(host);
  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. static const struct dev_pm_ops sdhci_s3c_pmops = {
  587. .suspend = sdhci_s3c_suspend,
  588. .resume = sdhci_s3c_resume,
  589. };
  590. #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
  591. #else
  592. #define SDHCI_S3C_PMOPS NULL
  593. #endif
  594. #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
  595. static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
  596. .sdhci_quirks = SDHCI_QUIRK_NONSTANDARD_CLOCK,
  597. };
  598. #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
  599. #else
  600. #define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
  601. #endif
  602. static struct platform_device_id sdhci_s3c_driver_ids[] = {
  603. {
  604. .name = "s3c-sdhci",
  605. .driver_data = (kernel_ulong_t)NULL,
  606. }, {
  607. .name = "exynos4-sdhci",
  608. .driver_data = EXYNOS4_SDHCI_DRV_DATA,
  609. },
  610. { }
  611. };
  612. MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);
  613. static struct platform_driver sdhci_s3c_driver = {
  614. .probe = sdhci_s3c_probe,
  615. .remove = __devexit_p(sdhci_s3c_remove),
  616. .id_table = sdhci_s3c_driver_ids,
  617. .driver = {
  618. .owner = THIS_MODULE,
  619. .name = "s3c-sdhci",
  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");