sdhci-of-core.c 5.6 KB

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