sdhci-of-core.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. const struct of_device_id *match)
  107. {
  108. struct device_node *np = ofdev->dev.of_node;
  109. struct sdhci_of_data *sdhci_of_data = match->data;
  110. struct sdhci_host *host;
  111. struct sdhci_of_host *of_host;
  112. const __be32 *clk;
  113. int size;
  114. int ret;
  115. if (!of_device_is_available(np))
  116. return -ENODEV;
  117. host = sdhci_alloc_host(&ofdev->dev, sizeof(*of_host));
  118. if (IS_ERR(host))
  119. return -ENOMEM;
  120. of_host = sdhci_priv(host);
  121. dev_set_drvdata(&ofdev->dev, host);
  122. host->ioaddr = of_iomap(np, 0);
  123. if (!host->ioaddr) {
  124. ret = -ENOMEM;
  125. goto err_addr_map;
  126. }
  127. host->irq = irq_of_parse_and_map(np, 0);
  128. if (!host->irq) {
  129. ret = -EINVAL;
  130. goto err_no_irq;
  131. }
  132. host->hw_name = dev_name(&ofdev->dev);
  133. if (sdhci_of_data) {
  134. host->quirks = sdhci_of_data->quirks;
  135. host->ops = &sdhci_of_data->ops;
  136. }
  137. if (of_get_property(np, "sdhci,auto-cmd12", NULL))
  138. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  139. if (of_get_property(np, "sdhci,1-bit-only", NULL))
  140. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  141. if (sdhci_of_wp_inverted(np))
  142. host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
  143. clk = of_get_property(np, "clock-frequency", &size);
  144. if (clk && size == sizeof(*clk) && *clk)
  145. of_host->clock = be32_to_cpup(clk);
  146. ret = sdhci_add_host(host);
  147. if (ret)
  148. goto err_add_host;
  149. return 0;
  150. err_add_host:
  151. irq_dispose_mapping(host->irq);
  152. err_no_irq:
  153. iounmap(host->ioaddr);
  154. err_addr_map:
  155. sdhci_free_host(host);
  156. return ret;
  157. }
  158. static int __devexit sdhci_of_remove(struct platform_device *ofdev)
  159. {
  160. struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
  161. sdhci_remove_host(host, 0);
  162. sdhci_free_host(host);
  163. irq_dispose_mapping(host->irq);
  164. iounmap(host->ioaddr);
  165. return 0;
  166. }
  167. static const struct of_device_id sdhci_of_match[] = {
  168. #ifdef CONFIG_MMC_SDHCI_OF_ESDHC
  169. { .compatible = "fsl,mpc8379-esdhc", .data = &sdhci_esdhc, },
  170. { .compatible = "fsl,mpc8536-esdhc", .data = &sdhci_esdhc, },
  171. { .compatible = "fsl,esdhc", .data = &sdhci_esdhc, },
  172. #endif
  173. #ifdef CONFIG_MMC_SDHCI_OF_HLWD
  174. { .compatible = "nintendo,hollywood-sdhci", .data = &sdhci_hlwd, },
  175. #endif
  176. { .compatible = "generic-sdhci", },
  177. {},
  178. };
  179. MODULE_DEVICE_TABLE(of, sdhci_of_match);
  180. static struct of_platform_driver sdhci_of_driver = {
  181. .driver = {
  182. .name = "sdhci-of",
  183. .owner = THIS_MODULE,
  184. .of_match_table = sdhci_of_match,
  185. },
  186. .probe = sdhci_of_probe,
  187. .remove = __devexit_p(sdhci_of_remove),
  188. .suspend = sdhci_of_suspend,
  189. .resume = sdhci_of_resume,
  190. };
  191. static int __init sdhci_of_init(void)
  192. {
  193. return of_register_platform_driver(&sdhci_of_driver);
  194. }
  195. module_init(sdhci_of_init);
  196. static void __exit sdhci_of_exit(void)
  197. {
  198. of_unregister_platform_driver(&sdhci_of_driver);
  199. }
  200. module_exit(sdhci_of_exit);
  201. MODULE_DESCRIPTION("Secure Digital Host Controller Interface OF driver");
  202. MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
  203. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  204. MODULE_LICENSE("GPL");