sdhci-s3c.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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/clk.h>
  18. #include <linux/io.h>
  19. #include <linux/mmc/host.h>
  20. #include <plat/sdhci.h>
  21. #include <plat/regs-sdhci.h>
  22. #include "sdhci.h"
  23. #define MAX_BUS_CLK (4)
  24. /**
  25. * struct sdhci_s3c - S3C SDHCI instance
  26. * @host: The SDHCI host created
  27. * @pdev: The platform device we where created from.
  28. * @ioarea: The resource created when we claimed the IO area.
  29. * @pdata: The platform data for this controller.
  30. * @cur_clk: The index of the current bus clock.
  31. * @clk_io: The clock for the internal bus interface.
  32. * @clk_bus: The clocks that are available for the SD/MMC bus clock.
  33. */
  34. struct sdhci_s3c {
  35. struct sdhci_host *host;
  36. struct platform_device *pdev;
  37. struct resource *ioarea;
  38. struct s3c_sdhci_platdata *pdata;
  39. unsigned int cur_clk;
  40. struct clk *clk_io;
  41. struct clk *clk_bus[MAX_BUS_CLK];
  42. };
  43. static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
  44. {
  45. return sdhci_priv(host);
  46. }
  47. /**
  48. * get_curclk - convert ctrl2 register to clock source number
  49. * @ctrl2: Control2 register value.
  50. */
  51. static u32 get_curclk(u32 ctrl2)
  52. {
  53. ctrl2 &= S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  54. ctrl2 >>= S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  55. return ctrl2;
  56. }
  57. static void sdhci_s3c_check_sclk(struct sdhci_host *host)
  58. {
  59. struct sdhci_s3c *ourhost = to_s3c(host);
  60. u32 tmp = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  61. if (get_curclk(tmp) != ourhost->cur_clk) {
  62. dev_dbg(&ourhost->pdev->dev, "restored ctrl2 clock setting\n");
  63. tmp &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  64. tmp |= ourhost->cur_clk << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  65. writel(tmp, host->ioaddr + 0x80);
  66. }
  67. }
  68. /**
  69. * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
  70. * @host: The SDHCI host instance.
  71. *
  72. * Callback to return the maximum clock rate acheivable by the controller.
  73. */
  74. static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
  75. {
  76. struct sdhci_s3c *ourhost = to_s3c(host);
  77. struct clk *busclk;
  78. unsigned int rate, max;
  79. int clk;
  80. /* note, a reset will reset the clock source */
  81. sdhci_s3c_check_sclk(host);
  82. for (max = 0, clk = 0; clk < MAX_BUS_CLK; clk++) {
  83. busclk = ourhost->clk_bus[clk];
  84. if (!busclk)
  85. continue;
  86. rate = clk_get_rate(busclk);
  87. if (rate > max)
  88. max = rate;
  89. }
  90. return max;
  91. }
  92. static unsigned int sdhci_s3c_get_timeout_clk(struct sdhci_host *host)
  93. {
  94. return sdhci_s3c_get_max_clk(host) / 1000000;
  95. }
  96. /**
  97. * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
  98. * @ourhost: Our SDHCI instance.
  99. * @src: The source clock index.
  100. * @wanted: The clock frequency wanted.
  101. */
  102. static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
  103. unsigned int src,
  104. unsigned int wanted)
  105. {
  106. unsigned long rate;
  107. struct clk *clksrc = ourhost->clk_bus[src];
  108. int div;
  109. if (!clksrc)
  110. return UINT_MAX;
  111. rate = clk_get_rate(clksrc);
  112. for (div = 1; div < 256; div *= 2) {
  113. if ((rate / div) <= wanted)
  114. break;
  115. }
  116. dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
  117. src, rate, wanted, rate / div);
  118. return (wanted - (rate / div));
  119. }
  120. /**
  121. * sdhci_s3c_set_clock - callback on clock change
  122. * @host: The SDHCI host being changed
  123. * @clock: The clock rate being requested.
  124. *
  125. * When the card's clock is going to be changed, look at the new frequency
  126. * and find the best clock source to go with it.
  127. */
  128. static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
  129. {
  130. struct sdhci_s3c *ourhost = to_s3c(host);
  131. unsigned int best = UINT_MAX;
  132. unsigned int delta;
  133. int best_src = 0;
  134. int src;
  135. u32 ctrl;
  136. /* don't bother if the clock is going off. */
  137. if (clock == 0)
  138. return;
  139. for (src = 0; src < MAX_BUS_CLK; src++) {
  140. delta = sdhci_s3c_consider_clock(ourhost, src, clock);
  141. if (delta < best) {
  142. best = delta;
  143. best_src = src;
  144. }
  145. }
  146. dev_dbg(&ourhost->pdev->dev,
  147. "selected source %d, clock %d, delta %d\n",
  148. best_src, clock, best);
  149. /* select the new clock source */
  150. if (ourhost->cur_clk != best_src) {
  151. struct clk *clk = ourhost->clk_bus[best_src];
  152. /* turn clock off to card before changing clock source */
  153. writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
  154. ourhost->cur_clk = best_src;
  155. host->max_clk = clk_get_rate(clk);
  156. host->timeout_clk = sdhci_s3c_get_timeout_clk(host);
  157. ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
  158. ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
  159. ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
  160. writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
  161. }
  162. /* reconfigure the hardware for new clock rate */
  163. {
  164. struct mmc_ios ios;
  165. ios.clock = clock;
  166. if (ourhost->pdata->cfg_card)
  167. (ourhost->pdata->cfg_card)(ourhost->pdev, host->ioaddr,
  168. &ios, NULL);
  169. }
  170. }
  171. static struct sdhci_ops sdhci_s3c_ops = {
  172. .get_max_clock = sdhci_s3c_get_max_clk,
  173. .get_timeout_clock = sdhci_s3c_get_timeout_clk,
  174. .set_clock = sdhci_s3c_set_clock,
  175. };
  176. static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
  177. {
  178. struct s3c_sdhci_platdata *pdata = pdev->dev.platform_data;
  179. struct device *dev = &pdev->dev;
  180. struct sdhci_host *host;
  181. struct sdhci_s3c *sc;
  182. struct resource *res;
  183. int ret, irq, ptr, clks;
  184. if (!pdata) {
  185. dev_err(dev, "no device data specified\n");
  186. return -ENOENT;
  187. }
  188. irq = platform_get_irq(pdev, 0);
  189. if (irq < 0) {
  190. dev_err(dev, "no irq specified\n");
  191. return irq;
  192. }
  193. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  194. if (!res) {
  195. dev_err(dev, "no memory specified\n");
  196. return -ENOENT;
  197. }
  198. host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
  199. if (IS_ERR(host)) {
  200. dev_err(dev, "sdhci_alloc_host() failed\n");
  201. return PTR_ERR(host);
  202. }
  203. sc = sdhci_priv(host);
  204. sc->host = host;
  205. sc->pdev = pdev;
  206. sc->pdata = pdata;
  207. platform_set_drvdata(pdev, host);
  208. sc->clk_io = clk_get(dev, "hsmmc");
  209. if (IS_ERR(sc->clk_io)) {
  210. dev_err(dev, "failed to get io clock\n");
  211. ret = PTR_ERR(sc->clk_io);
  212. goto err_io_clk;
  213. }
  214. /* enable the local io clock and keep it running for the moment. */
  215. clk_enable(sc->clk_io);
  216. for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  217. struct clk *clk;
  218. char *name = pdata->clocks[ptr];
  219. if (name == NULL)
  220. continue;
  221. clk = clk_get(dev, name);
  222. if (IS_ERR(clk)) {
  223. dev_err(dev, "failed to get clock %s\n", name);
  224. continue;
  225. }
  226. clks++;
  227. sc->clk_bus[ptr] = clk;
  228. clk_enable(clk);
  229. dev_info(dev, "clock source %d: %s (%ld Hz)\n",
  230. ptr, name, clk_get_rate(clk));
  231. }
  232. if (clks == 0) {
  233. dev_err(dev, "failed to find any bus clocks\n");
  234. ret = -ENOENT;
  235. goto err_no_busclks;
  236. }
  237. sc->ioarea = request_mem_region(res->start, resource_size(res),
  238. mmc_hostname(host->mmc));
  239. if (!sc->ioarea) {
  240. dev_err(dev, "failed to reserve register area\n");
  241. ret = -ENXIO;
  242. goto err_req_regs;
  243. }
  244. host->ioaddr = ioremap_nocache(res->start, resource_size(res));
  245. if (!host->ioaddr) {
  246. dev_err(dev, "failed to map registers\n");
  247. ret = -ENXIO;
  248. goto err_req_regs;
  249. }
  250. /* Ensure we have minimal gpio selected CMD/CLK/Detect */
  251. if (pdata->cfg_gpio)
  252. pdata->cfg_gpio(pdev, pdata->max_width);
  253. host->hw_name = "samsung-hsmmc";
  254. host->ops = &sdhci_s3c_ops;
  255. host->quirks = 0;
  256. host->irq = irq;
  257. /* Setup quirks for the controller */
  258. /* Currently with ADMA enabled we are getting some length
  259. * interrupts that are not being dealt with, do disable
  260. * ADMA until this is sorted out. */
  261. host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
  262. host->quirks |= SDHCI_QUIRK_32BIT_ADMA_SIZE;
  263. #ifndef CONFIG_MMC_SDHCI_S3C_DMA
  264. /* we currently see overruns on errors, so disable the SDMA
  265. * support as well. */
  266. host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
  267. /* PIO currently has problems with multi-block IO */
  268. host->quirks |= SDHCI_QUIRK_NO_MULTIBLOCK;
  269. #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
  270. /* It seems we do not get an DATA transfer complete on non-busy
  271. * transfers, not sure if this is a problem with this specific
  272. * SDHCI block, or a missing configuration that needs to be set. */
  273. host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
  274. host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
  275. SDHCI_QUIRK_32BIT_DMA_SIZE);
  276. ret = sdhci_add_host(host);
  277. if (ret) {
  278. dev_err(dev, "sdhci_add_host() failed\n");
  279. goto err_add_host;
  280. }
  281. return 0;
  282. err_add_host:
  283. release_resource(sc->ioarea);
  284. kfree(sc->ioarea);
  285. err_req_regs:
  286. for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
  287. clk_disable(sc->clk_bus[ptr]);
  288. clk_put(sc->clk_bus[ptr]);
  289. }
  290. err_no_busclks:
  291. clk_disable(sc->clk_io);
  292. clk_put(sc->clk_io);
  293. err_io_clk:
  294. sdhci_free_host(host);
  295. return ret;
  296. }
  297. static int __devexit sdhci_s3c_remove(struct platform_device *pdev)
  298. {
  299. return 0;
  300. }
  301. #ifdef CONFIG_PM
  302. static int sdhci_s3c_suspend(struct platform_device *dev, pm_message_t pm)
  303. {
  304. struct sdhci_host *host = platform_get_drvdata(dev);
  305. sdhci_suspend_host(host, pm);
  306. return 0;
  307. }
  308. static int sdhci_s3c_resume(struct platform_device *dev)
  309. {
  310. struct sdhci_host *host = platform_get_drvdata(dev);
  311. sdhci_resume_host(host);
  312. return 0;
  313. }
  314. #else
  315. #define sdhci_s3c_suspend NULL
  316. #define sdhci_s3c_resume NULL
  317. #endif
  318. static struct platform_driver sdhci_s3c_driver = {
  319. .probe = sdhci_s3c_probe,
  320. .remove = __devexit_p(sdhci_s3c_remove),
  321. .suspend = sdhci_s3c_suspend,
  322. .resume = sdhci_s3c_resume,
  323. .driver = {
  324. .owner = THIS_MODULE,
  325. .name = "s3c-sdhci",
  326. },
  327. };
  328. static int __init sdhci_s3c_init(void)
  329. {
  330. return platform_driver_register(&sdhci_s3c_driver);
  331. }
  332. static void __exit sdhci_s3c_exit(void)
  333. {
  334. platform_driver_unregister(&sdhci_s3c_driver);
  335. }
  336. module_init(sdhci_s3c_init);
  337. module_exit(sdhci_s3c_exit);
  338. MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
  339. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  340. MODULE_LICENSE("GPL v2");
  341. MODULE_ALIAS("platform:s3c-sdhci");