sdhci-of.c 7.2 KB

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