sdhci-of-core.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/err.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/delay.h>
  21. #include <linux/of.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/mmc/host.h>
  26. #ifdef CONFIG_PPC
  27. #include <asm/machdep.h>
  28. #endif
  29. #include "sdhci-of.h"
  30. #include "sdhci.h"
  31. #ifdef CONFIG_MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER
  32. /*
  33. * These accessors are designed for big endian hosts doing I/O to
  34. * little endian controllers incorporating a 32-bit hardware byte swapper.
  35. */
  36. u32 sdhci_be32bs_readl(struct sdhci_host *host, int reg)
  37. {
  38. return in_be32(host->ioaddr + reg);
  39. }
  40. u16 sdhci_be32bs_readw(struct sdhci_host *host, int reg)
  41. {
  42. return in_be16(host->ioaddr + (reg ^ 0x2));
  43. }
  44. u8 sdhci_be32bs_readb(struct sdhci_host *host, int reg)
  45. {
  46. return in_8(host->ioaddr + (reg ^ 0x3));
  47. }
  48. void sdhci_be32bs_writel(struct sdhci_host *host, u32 val, int reg)
  49. {
  50. out_be32(host->ioaddr + reg, val);
  51. }
  52. void sdhci_be32bs_writew(struct sdhci_host *host, u16 val, int reg)
  53. {
  54. struct sdhci_of_host *of_host = sdhci_priv(host);
  55. int base = reg & ~0x3;
  56. int shift = (reg & 0x2) * 8;
  57. switch (reg) {
  58. case SDHCI_TRANSFER_MODE:
  59. /*
  60. * Postpone this write, we must do it together with a
  61. * command write that is down below.
  62. */
  63. of_host->xfer_mode_shadow = val;
  64. return;
  65. case SDHCI_COMMAND:
  66. sdhci_be32bs_writel(host, val << 16 | of_host->xfer_mode_shadow,
  67. SDHCI_TRANSFER_MODE);
  68. return;
  69. }
  70. clrsetbits_be32(host->ioaddr + base, 0xffff << shift, val << shift);
  71. }
  72. void sdhci_be32bs_writeb(struct sdhci_host *host, u8 val, int reg)
  73. {
  74. int base = reg & ~0x3;
  75. int shift = (reg & 0x3) * 8;
  76. clrsetbits_be32(host->ioaddr + base , 0xff << shift, val << shift);
  77. }
  78. #endif /* CONFIG_MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER */
  79. #ifdef CONFIG_PM
  80. static int sdhci_of_suspend(struct platform_device *ofdev, pm_message_t state)
  81. {
  82. struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
  83. return mmc_suspend_host(host->mmc);
  84. }
  85. static int sdhci_of_resume(struct platform_device *ofdev)
  86. {
  87. struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
  88. return mmc_resume_host(host->mmc);
  89. }
  90. #else
  91. #define sdhci_of_suspend NULL
  92. #define sdhci_of_resume NULL
  93. #endif
  94. static bool __devinit sdhci_of_wp_inverted(struct device_node *np)
  95. {
  96. if (of_get_property(np, "sdhci,wp-inverted", NULL))
  97. return true;
  98. /* Old device trees don't have the wp-inverted property. */
  99. #ifdef CONFIG_PPC
  100. return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
  101. #else
  102. return false;
  103. #endif
  104. }
  105. static int __devinit sdhci_of_probe(struct platform_device *ofdev)
  106. {
  107. struct device_node *np = ofdev->dev.of_node;
  108. struct sdhci_of_data *sdhci_of_data;
  109. struct sdhci_host *host;
  110. struct sdhci_of_host *of_host;
  111. const __be32 *clk;
  112. int size;
  113. int ret;
  114. if (!ofdev->dev.of_match)
  115. return -EINVAL;
  116. sdhci_of_data = ofdev->dev.of_match->data;
  117. if (!of_device_is_available(np))
  118. return -ENODEV;
  119. host = sdhci_alloc_host(&ofdev->dev, sizeof(*of_host));
  120. if (IS_ERR(host))
  121. return -ENOMEM;
  122. of_host = sdhci_priv(host);
  123. dev_set_drvdata(&ofdev->dev, host);
  124. host->ioaddr = of_iomap(np, 0);
  125. if (!host->ioaddr) {
  126. ret = -ENOMEM;
  127. goto err_addr_map;
  128. }
  129. host->irq = irq_of_parse_and_map(np, 0);
  130. if (!host->irq) {
  131. ret = -EINVAL;
  132. goto err_no_irq;
  133. }
  134. host->hw_name = dev_name(&ofdev->dev);
  135. if (sdhci_of_data) {
  136. host->quirks = sdhci_of_data->quirks;
  137. host->ops = &sdhci_of_data->ops;
  138. }
  139. if (of_get_property(np, "sdhci,auto-cmd12", NULL))
  140. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  141. if (of_get_property(np, "sdhci,1-bit-only", NULL))
  142. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  143. if (sdhci_of_wp_inverted(np))
  144. host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
  145. clk = of_get_property(np, "clock-frequency", &size);
  146. if (clk && size == sizeof(*clk) && *clk)
  147. of_host->clock = be32_to_cpup(clk);
  148. ret = sdhci_add_host(host);
  149. if (ret)
  150. goto err_add_host;
  151. return 0;
  152. err_add_host:
  153. irq_dispose_mapping(host->irq);
  154. err_no_irq:
  155. iounmap(host->ioaddr);
  156. err_addr_map:
  157. sdhci_free_host(host);
  158. return ret;
  159. }
  160. static int __devexit sdhci_of_remove(struct platform_device *ofdev)
  161. {
  162. struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
  163. sdhci_remove_host(host, 0);
  164. sdhci_free_host(host);
  165. irq_dispose_mapping(host->irq);
  166. iounmap(host->ioaddr);
  167. return 0;
  168. }
  169. static const struct of_device_id sdhci_of_match[] = {
  170. #ifdef CONFIG_MMC_SDHCI_OF_ESDHC
  171. { .compatible = "fsl,mpc8379-esdhc", .data = &sdhci_esdhc, },
  172. { .compatible = "fsl,mpc8536-esdhc", .data = &sdhci_esdhc, },
  173. { .compatible = "fsl,esdhc", .data = &sdhci_esdhc, },
  174. #endif
  175. #ifdef CONFIG_MMC_SDHCI_OF_HLWD
  176. { .compatible = "nintendo,hollywood-sdhci", .data = &sdhci_hlwd, },
  177. #endif
  178. { .compatible = "generic-sdhci", },
  179. {},
  180. };
  181. MODULE_DEVICE_TABLE(of, sdhci_of_match);
  182. static struct platform_driver sdhci_of_driver = {
  183. .driver = {
  184. .name = "sdhci-of",
  185. .owner = THIS_MODULE,
  186. .of_match_table = sdhci_of_match,
  187. },
  188. .probe = sdhci_of_probe,
  189. .remove = __devexit_p(sdhci_of_remove),
  190. .suspend = sdhci_of_suspend,
  191. .resume = sdhci_of_resume,
  192. };
  193. static int __init sdhci_of_init(void)
  194. {
  195. return platform_driver_register(&sdhci_of_driver);
  196. }
  197. module_init(sdhci_of_init);
  198. static void __exit sdhci_of_exit(void)
  199. {
  200. platform_driver_unregister(&sdhci_of_driver);
  201. }
  202. module_exit(sdhci_of_exit);
  203. MODULE_DESCRIPTION("Secure Digital Host Controller Interface OF driver");
  204. MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
  205. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  206. MODULE_LICENSE("GPL");