sdhci-of.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_min_clock(struct sdhci_host *host)
  136. {
  137. struct sdhci_of_host *of_host = sdhci_priv(host);
  138. return of_host->clock / 256 / 16;
  139. }
  140. static unsigned int esdhc_get_timeout_clock(struct sdhci_host *host)
  141. {
  142. struct sdhci_of_host *of_host = sdhci_priv(host);
  143. return of_host->clock / 1000;
  144. }
  145. static struct sdhci_of_data sdhci_esdhc = {
  146. .quirks = SDHCI_QUIRK_FORCE_BLK_SZ_2048 |
  147. SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  148. SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
  149. SDHCI_QUIRK_NO_BUSY_IRQ |
  150. SDHCI_QUIRK_NONSTANDARD_CLOCK |
  151. SDHCI_QUIRK_PIO_NEEDS_DELAY |
  152. SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET |
  153. SDHCI_QUIRK_NO_CARD_NO_RESET,
  154. .ops = {
  155. .readl = esdhc_readl,
  156. .readw = esdhc_readw,
  157. .readb = esdhc_readb,
  158. .writel = esdhc_writel,
  159. .writew = esdhc_writew,
  160. .writeb = esdhc_writeb,
  161. .set_clock = esdhc_set_clock,
  162. .enable_dma = esdhc_enable_dma,
  163. .get_max_clock = esdhc_get_max_clock,
  164. .get_min_clock = esdhc_get_min_clock,
  165. .get_timeout_clock = esdhc_get_timeout_clock,
  166. },
  167. };
  168. #ifdef CONFIG_PM
  169. static int sdhci_of_suspend(struct of_device *ofdev, pm_message_t state)
  170. {
  171. struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
  172. return mmc_suspend_host(host->mmc, state);
  173. }
  174. static int sdhci_of_resume(struct of_device *ofdev)
  175. {
  176. struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
  177. return mmc_resume_host(host->mmc);
  178. }
  179. #else
  180. #define sdhci_of_suspend NULL
  181. #define sdhci_of_resume NULL
  182. #endif
  183. static int __devinit sdhci_of_probe(struct of_device *ofdev,
  184. const struct of_device_id *match)
  185. {
  186. struct device_node *np = ofdev->node;
  187. struct sdhci_of_data *sdhci_of_data = match->data;
  188. struct sdhci_host *host;
  189. struct sdhci_of_host *of_host;
  190. const u32 *clk;
  191. int size;
  192. int ret;
  193. if (!of_device_is_available(np))
  194. return -ENODEV;
  195. host = sdhci_alloc_host(&ofdev->dev, sizeof(*of_host));
  196. if (IS_ERR(host))
  197. return -ENOMEM;
  198. of_host = sdhci_priv(host);
  199. dev_set_drvdata(&ofdev->dev, host);
  200. host->ioaddr = of_iomap(np, 0);
  201. if (!host->ioaddr) {
  202. ret = -ENOMEM;
  203. goto err_addr_map;
  204. }
  205. host->irq = irq_of_parse_and_map(np, 0);
  206. if (!host->irq) {
  207. ret = -EINVAL;
  208. goto err_no_irq;
  209. }
  210. host->hw_name = dev_name(&ofdev->dev);
  211. if (sdhci_of_data) {
  212. host->quirks = sdhci_of_data->quirks;
  213. host->ops = &sdhci_of_data->ops;
  214. }
  215. if (of_get_property(np, "sdhci,1-bit-only", NULL))
  216. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  217. clk = of_get_property(np, "clock-frequency", &size);
  218. if (clk && size == sizeof(*clk) && *clk)
  219. of_host->clock = *clk;
  220. ret = sdhci_add_host(host);
  221. if (ret)
  222. goto err_add_host;
  223. return 0;
  224. err_add_host:
  225. irq_dispose_mapping(host->irq);
  226. err_no_irq:
  227. iounmap(host->ioaddr);
  228. err_addr_map:
  229. sdhci_free_host(host);
  230. return ret;
  231. }
  232. static int __devexit sdhci_of_remove(struct of_device *ofdev)
  233. {
  234. struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
  235. sdhci_remove_host(host, 0);
  236. sdhci_free_host(host);
  237. irq_dispose_mapping(host->irq);
  238. iounmap(host->ioaddr);
  239. return 0;
  240. }
  241. static const struct of_device_id sdhci_of_match[] = {
  242. { .compatible = "fsl,mpc8379-esdhc", .data = &sdhci_esdhc, },
  243. { .compatible = "fsl,mpc8536-esdhc", .data = &sdhci_esdhc, },
  244. { .compatible = "fsl,esdhc", .data = &sdhci_esdhc, },
  245. { .compatible = "generic-sdhci", },
  246. {},
  247. };
  248. MODULE_DEVICE_TABLE(of, sdhci_of_match);
  249. static struct of_platform_driver sdhci_of_driver = {
  250. .driver.name = "sdhci-of",
  251. .match_table = sdhci_of_match,
  252. .probe = sdhci_of_probe,
  253. .remove = __devexit_p(sdhci_of_remove),
  254. .suspend = sdhci_of_suspend,
  255. .resume = sdhci_of_resume,
  256. };
  257. static int __init sdhci_of_init(void)
  258. {
  259. return of_register_platform_driver(&sdhci_of_driver);
  260. }
  261. module_init(sdhci_of_init);
  262. static void __exit sdhci_of_exit(void)
  263. {
  264. of_unregister_platform_driver(&sdhci_of_driver);
  265. }
  266. module_exit(sdhci_of_exit);
  267. MODULE_DESCRIPTION("Secure Digital Host Controller Interface OF driver");
  268. MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
  269. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  270. MODULE_LICENSE("GPL");