sdhci-pltfm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * sdhci-pltfm.c Support for SDHCI platform devices
  3. * Copyright (c) 2009 Intel Corporation
  4. *
  5. * Copyright (c) 2007 Freescale Semiconductor, Inc.
  6. * Copyright (c) 2009 MontaVista Software, Inc.
  7. *
  8. * Authors: Xiaobo Xie <X.Xie@freescale.com>
  9. * Anton Vorontsov <avorontsov@ru.mvista.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. /* Supports:
  25. * SDHCI platform devices
  26. *
  27. * Inspired by sdhci-pci.c, by Pierre Ossman
  28. */
  29. #include <linux/err.h>
  30. #include <linux/of.h>
  31. #ifdef CONFIG_PPC
  32. #include <asm/machdep.h>
  33. #endif
  34. #include "sdhci-pltfm.h"
  35. static struct sdhci_ops sdhci_pltfm_ops = {
  36. };
  37. #ifdef CONFIG_OF
  38. static bool sdhci_of_wp_inverted(struct device_node *np)
  39. {
  40. if (of_get_property(np, "sdhci,wp-inverted", NULL))
  41. return true;
  42. /* Old device trees don't have the wp-inverted property. */
  43. #ifdef CONFIG_PPC
  44. return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
  45. #else
  46. return false;
  47. #endif /* CONFIG_PPC */
  48. }
  49. void sdhci_get_of_property(struct platform_device *pdev)
  50. {
  51. struct device_node *np = pdev->dev.of_node;
  52. struct sdhci_host *host = platform_get_drvdata(pdev);
  53. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  54. const __be32 *clk;
  55. int size;
  56. if (of_device_is_available(np)) {
  57. if (of_get_property(np, "sdhci,auto-cmd12", NULL))
  58. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  59. if (of_get_property(np, "sdhci,1-bit-only", NULL))
  60. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  61. if (sdhci_of_wp_inverted(np))
  62. host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
  63. clk = of_get_property(np, "clock-frequency", &size);
  64. if (clk && size == sizeof(*clk) && *clk)
  65. pltfm_host->clock = be32_to_cpup(clk);
  66. }
  67. }
  68. #else
  69. void sdhci_get_of_property(struct platform_device *pdev) {}
  70. #endif /* CONFIG_OF */
  71. EXPORT_SYMBOL_GPL(sdhci_get_of_property);
  72. struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev,
  73. struct sdhci_pltfm_data *pdata)
  74. {
  75. struct sdhci_host *host;
  76. struct sdhci_pltfm_host *pltfm_host;
  77. struct device_node *np = pdev->dev.of_node;
  78. struct resource *iomem;
  79. int ret;
  80. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  81. if (!iomem) {
  82. ret = -ENOMEM;
  83. goto err;
  84. }
  85. if (resource_size(iomem) < 0x100)
  86. dev_err(&pdev->dev, "Invalid iomem size!\n");
  87. /* Some PCI-based MFD need the parent here */
  88. if (pdev->dev.parent != &platform_bus && !np)
  89. host = sdhci_alloc_host(pdev->dev.parent, sizeof(*pltfm_host));
  90. else
  91. host = sdhci_alloc_host(&pdev->dev, sizeof(*pltfm_host));
  92. if (IS_ERR(host)) {
  93. ret = PTR_ERR(host);
  94. goto err;
  95. }
  96. pltfm_host = sdhci_priv(host);
  97. host->hw_name = dev_name(&pdev->dev);
  98. if (pdata && pdata->ops)
  99. host->ops = pdata->ops;
  100. else
  101. host->ops = &sdhci_pltfm_ops;
  102. if (pdata)
  103. host->quirks = pdata->quirks;
  104. host->irq = platform_get_irq(pdev, 0);
  105. if (!request_mem_region(iomem->start, resource_size(iomem),
  106. mmc_hostname(host->mmc))) {
  107. dev_err(&pdev->dev, "cannot request region\n");
  108. ret = -EBUSY;
  109. goto err_request;
  110. }
  111. host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  112. if (!host->ioaddr) {
  113. dev_err(&pdev->dev, "failed to remap registers\n");
  114. ret = -ENOMEM;
  115. goto err_remap;
  116. }
  117. platform_set_drvdata(pdev, host);
  118. return host;
  119. err_remap:
  120. release_mem_region(iomem->start, resource_size(iomem));
  121. err_request:
  122. sdhci_free_host(host);
  123. err:
  124. dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
  125. return ERR_PTR(ret);
  126. }
  127. EXPORT_SYMBOL_GPL(sdhci_pltfm_init);
  128. void sdhci_pltfm_free(struct platform_device *pdev)
  129. {
  130. struct sdhci_host *host = platform_get_drvdata(pdev);
  131. struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  132. iounmap(host->ioaddr);
  133. release_mem_region(iomem->start, resource_size(iomem));
  134. sdhci_free_host(host);
  135. platform_set_drvdata(pdev, NULL);
  136. }
  137. EXPORT_SYMBOL_GPL(sdhci_pltfm_free);
  138. int sdhci_pltfm_register(struct platform_device *pdev,
  139. struct sdhci_pltfm_data *pdata)
  140. {
  141. struct sdhci_host *host;
  142. int ret = 0;
  143. host = sdhci_pltfm_init(pdev, pdata);
  144. if (IS_ERR(host))
  145. return PTR_ERR(host);
  146. sdhci_get_of_property(pdev);
  147. ret = sdhci_add_host(host);
  148. if (ret)
  149. sdhci_pltfm_free(pdev);
  150. return ret;
  151. }
  152. EXPORT_SYMBOL_GPL(sdhci_pltfm_register);
  153. int sdhci_pltfm_unregister(struct platform_device *pdev)
  154. {
  155. struct sdhci_host *host = platform_get_drvdata(pdev);
  156. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  157. sdhci_remove_host(host, dead);
  158. sdhci_pltfm_free(pdev);
  159. return 0;
  160. }
  161. EXPORT_SYMBOL_GPL(sdhci_pltfm_unregister);
  162. #ifdef CONFIG_PM
  163. int sdhci_pltfm_suspend(struct platform_device *dev, pm_message_t state)
  164. {
  165. struct sdhci_host *host = platform_get_drvdata(dev);
  166. return sdhci_suspend_host(host, state);
  167. }
  168. EXPORT_SYMBOL_GPL(sdhci_pltfm_suspend);
  169. int sdhci_pltfm_resume(struct platform_device *dev)
  170. {
  171. struct sdhci_host *host = platform_get_drvdata(dev);
  172. return sdhci_resume_host(host);
  173. }
  174. EXPORT_SYMBOL_GPL(sdhci_pltfm_resume);
  175. #endif /* CONFIG_PM */
  176. static int __init sdhci_pltfm_drv_init(void)
  177. {
  178. pr_info("sdhci-pltfm: SDHCI platform and OF driver helper\n");
  179. return 0;
  180. }
  181. module_init(sdhci_pltfm_drv_init);
  182. static void __exit sdhci_pltfm_drv_exit(void)
  183. {
  184. }
  185. module_exit(sdhci_pltfm_drv_exit);
  186. MODULE_DESCRIPTION("SDHCI platform and OF driver helper");
  187. MODULE_AUTHOR("Intel Corporation");
  188. MODULE_LICENSE("GPL v2");