sdhci-of.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * OpenFirmware bindings for Secure Digital Host Controller Interface.
  3. *
  4. * Copyright (c) 2007 Freescale Semiconductor, Inc.
  5. * Copyright (c) 2009 MontaVista Software, Inc.
  6. *
  7. * Authors: Xiaobo Xie <X.Xie@freescale.com>
  8. * Anton Vorontsov <avorontsov@ru.mvista.com>
  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 as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/io.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/delay.h>
  20. #include <linux/of.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/mmc/host.h>
  23. #include "sdhci.h"
  24. struct sdhci_of_data {
  25. unsigned int quirks;
  26. struct sdhci_ops ops;
  27. };
  28. struct sdhci_of_host {
  29. unsigned int clock;
  30. u16 xfer_mode_shadow;
  31. };
  32. /*
  33. * Ops and quirks for the Freescale eSDHC controller.
  34. */
  35. #define ESDHC_DMA_SYSCTL 0x40c
  36. #define ESDHC_DMA_SNOOP 0x00000040
  37. #define ESDHC_SYSTEM_CONTROL 0x2c
  38. #define ESDHC_CLOCK_MASK 0x0000fff0
  39. #define ESDHC_PREDIV_SHIFT 8
  40. #define ESDHC_DIVIDER_SHIFT 4
  41. #define ESDHC_CLOCK_PEREN 0x00000004
  42. #define ESDHC_CLOCK_HCKEN 0x00000002
  43. #define ESDHC_CLOCK_IPGEN 0x00000001
  44. static u32 esdhc_readl(struct sdhci_host *host, int reg)
  45. {
  46. return in_be32(host->ioaddr + reg);
  47. }
  48. static u16 esdhc_readw(struct sdhci_host *host, int reg)
  49. {
  50. u16 ret;
  51. if (unlikely(reg == SDHCI_HOST_VERSION))
  52. ret = in_be16(host->ioaddr + reg);
  53. else
  54. ret = in_be16(host->ioaddr + (reg ^ 0x2));
  55. return ret;
  56. }
  57. static u8 esdhc_readb(struct sdhci_host *host, int reg)
  58. {
  59. return in_8(host->ioaddr + (reg ^ 0x3));
  60. }
  61. static void esdhc_writel(struct sdhci_host *host, u32 val, int reg)
  62. {
  63. out_be32(host->ioaddr + reg, val);
  64. }
  65. static void esdhc_writew(struct sdhci_host *host, u16 val, int reg)
  66. {
  67. struct sdhci_of_host *of_host = sdhci_priv(host);
  68. int base = reg & ~0x3;
  69. int shift = (reg & 0x2) * 8;
  70. switch (reg) {
  71. case SDHCI_TRANSFER_MODE:
  72. /*
  73. * Postpone this write, we must do it together with a
  74. * command write that is down below.
  75. */
  76. of_host->xfer_mode_shadow = val;
  77. return;
  78. case SDHCI_COMMAND:
  79. esdhc_writel(host, val << 16 | of_host->xfer_mode_shadow,
  80. SDHCI_TRANSFER_MODE);
  81. return;
  82. case SDHCI_BLOCK_SIZE:
  83. /*
  84. * Two last DMA bits are reserved, and first one is used for
  85. * non-standard blksz of 4096 bytes that we don't support
  86. * yet. So clear the DMA boundary bits.
  87. */
  88. val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
  89. /* fall through */
  90. }
  91. clrsetbits_be32(host->ioaddr + base, 0xffff << shift, val << shift);
  92. }
  93. static void esdhc_writeb(struct sdhci_host *host, u8 val, int reg)
  94. {
  95. int base = reg & ~0x3;
  96. int shift = (reg & 0x3) * 8;
  97. clrsetbits_be32(host->ioaddr + base , 0xff << shift, val << shift);
  98. }
  99. static void esdhc_set_clock(struct sdhci_host *host, unsigned int clock)
  100. {
  101. int div;
  102. int pre_div = 2;
  103. clrbits32(host->ioaddr + ESDHC_SYSTEM_CONTROL, ESDHC_CLOCK_IPGEN |
  104. ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
  105. if (clock == 0)
  106. goto out;
  107. if (host->max_clk / 16 > clock) {
  108. for (; pre_div < 256; pre_div *= 2) {
  109. if (host->max_clk / pre_div < clock * 16)
  110. break;
  111. }
  112. }
  113. for (div = 1; div <= 16; div++) {
  114. if (host->max_clk / (div * pre_div) <= clock)
  115. break;
  116. }
  117. pre_div >>= 1;
  118. setbits32(host->ioaddr + ESDHC_SYSTEM_CONTROL, ESDHC_CLOCK_IPGEN |
  119. ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN |
  120. div << ESDHC_DIVIDER_SHIFT | pre_div << ESDHC_PREDIV_SHIFT);
  121. mdelay(100);
  122. out:
  123. host->clock = clock;
  124. }
  125. static int esdhc_enable_dma(struct sdhci_host *host)
  126. {
  127. setbits32(host->ioaddr + ESDHC_DMA_SYSCTL, ESDHC_DMA_SNOOP);
  128. return 0;
  129. }
  130. static unsigned int esdhc_get_max_clock(struct sdhci_host *host)
  131. {
  132. struct sdhci_of_host *of_host = sdhci_priv(host);
  133. return of_host->clock;
  134. }
  135. static unsigned int esdhc_get_timeout_clock(struct sdhci_host *host)
  136. {
  137. struct sdhci_of_host *of_host = sdhci_priv(host);
  138. return of_host->clock / 1000;
  139. }
  140. static struct sdhci_of_data sdhci_esdhc = {
  141. .quirks = SDHCI_QUIRK_FORCE_BLK_SZ_2048 |
  142. SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  143. SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
  144. SDHCI_QUIRK_NO_BUSY_IRQ |
  145. SDHCI_QUIRK_NONSTANDARD_CLOCK |
  146. SDHCI_QUIRK_PIO_NEEDS_DELAY |
  147. SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET |
  148. SDHCI_QUIRK_NO_CARD_NO_RESET,
  149. .ops = {
  150. .readl = esdhc_readl,
  151. .readw = esdhc_readw,
  152. .readb = esdhc_readb,
  153. .writel = esdhc_writel,
  154. .writew = esdhc_writew,
  155. .writeb = esdhc_writeb,
  156. .set_clock = esdhc_set_clock,
  157. .enable_dma = esdhc_enable_dma,
  158. .get_max_clock = esdhc_get_max_clock,
  159. .get_timeout_clock = esdhc_get_timeout_clock,
  160. },
  161. };
  162. #ifdef CONFIG_PM
  163. static int sdhci_of_suspend(struct of_device *ofdev, pm_message_t state)
  164. {
  165. struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
  166. return mmc_suspend_host(host->mmc, state);
  167. }
  168. static int sdhci_of_resume(struct of_device *ofdev)
  169. {
  170. struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
  171. return mmc_resume_host(host->mmc);
  172. }
  173. #else
  174. #define sdhci_of_suspend NULL
  175. #define sdhci_of_resume NULL
  176. #endif
  177. static int __devinit sdhci_of_probe(struct of_device *ofdev,
  178. const struct of_device_id *match)
  179. {
  180. struct device_node *np = ofdev->node;
  181. struct sdhci_of_data *sdhci_of_data = match->data;
  182. struct sdhci_host *host;
  183. struct sdhci_of_host *of_host;
  184. const u32 *clk;
  185. int size;
  186. int ret;
  187. if (!of_device_is_available(np))
  188. return -ENODEV;
  189. host = sdhci_alloc_host(&ofdev->dev, sizeof(*of_host));
  190. if (!host)
  191. return -ENOMEM;
  192. of_host = sdhci_priv(host);
  193. dev_set_drvdata(&ofdev->dev, host);
  194. host->ioaddr = of_iomap(np, 0);
  195. if (!host->ioaddr) {
  196. ret = -ENOMEM;
  197. goto err_addr_map;
  198. }
  199. host->irq = irq_of_parse_and_map(np, 0);
  200. if (!host->irq) {
  201. ret = -EINVAL;
  202. goto err_no_irq;
  203. }
  204. host->hw_name = dev_name(&ofdev->dev);
  205. if (sdhci_of_data) {
  206. host->quirks = sdhci_of_data->quirks;
  207. host->ops = &sdhci_of_data->ops;
  208. }
  209. if (of_get_property(np, "sdhci,1-bit-only", NULL))
  210. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  211. clk = of_get_property(np, "clock-frequency", &size);
  212. if (clk && size == sizeof(*clk) && *clk)
  213. of_host->clock = *clk;
  214. ret = sdhci_add_host(host);
  215. if (ret)
  216. goto err_add_host;
  217. return 0;
  218. err_add_host:
  219. irq_dispose_mapping(host->irq);
  220. err_no_irq:
  221. iounmap(host->ioaddr);
  222. err_addr_map:
  223. sdhci_free_host(host);
  224. return ret;
  225. }
  226. static int __devexit sdhci_of_remove(struct of_device *ofdev)
  227. {
  228. struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
  229. sdhci_remove_host(host, 0);
  230. sdhci_free_host(host);
  231. irq_dispose_mapping(host->irq);
  232. iounmap(host->ioaddr);
  233. return 0;
  234. }
  235. static const struct of_device_id sdhci_of_match[] = {
  236. { .compatible = "fsl,mpc8379-esdhc", .data = &sdhci_esdhc, },
  237. { .compatible = "fsl,mpc8536-esdhc", .data = &sdhci_esdhc, },
  238. { .compatible = "fsl,esdhc", .data = &sdhci_esdhc, },
  239. { .compatible = "generic-sdhci", },
  240. {},
  241. };
  242. MODULE_DEVICE_TABLE(of, sdhci_of_match);
  243. static struct of_platform_driver sdhci_of_driver = {
  244. .driver.name = "sdhci-of",
  245. .match_table = sdhci_of_match,
  246. .probe = sdhci_of_probe,
  247. .remove = __devexit_p(sdhci_of_remove),
  248. .suspend = sdhci_of_suspend,
  249. .resume = sdhci_of_resume,
  250. };
  251. static int __init sdhci_of_init(void)
  252. {
  253. return of_register_platform_driver(&sdhci_of_driver);
  254. }
  255. module_init(sdhci_of_init);
  256. static void __exit sdhci_of_exit(void)
  257. {
  258. of_unregister_platform_driver(&sdhci_of_driver);
  259. }
  260. module_exit(sdhci_of_exit);
  261. MODULE_DESCRIPTION("Secure Digital Host Controller Interface OF driver");
  262. MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
  263. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  264. MODULE_LICENSE("GPL");