sdhci-s3c.c 10.0 KB

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