sdhci-pltfm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/module.h>
  31. #include <linux/of.h>
  32. #ifdef CONFIG_PPC
  33. #include <asm/machdep.h>
  34. #endif
  35. #include "sdhci-pltfm.h"
  36. static struct sdhci_ops sdhci_pltfm_ops = {
  37. };
  38. #ifdef CONFIG_OF
  39. static bool sdhci_of_wp_inverted(struct device_node *np)
  40. {
  41. if (of_get_property(np, "sdhci,wp-inverted", NULL))
  42. return true;
  43. /* Old device trees don't have the wp-inverted property. */
  44. #ifdef CONFIG_PPC
  45. return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
  46. #else
  47. return false;
  48. #endif /* CONFIG_PPC */
  49. }
  50. void sdhci_get_of_property(struct platform_device *pdev)
  51. {
  52. struct device_node *np = pdev->dev.of_node;
  53. struct sdhci_host *host = platform_get_drvdata(pdev);
  54. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  55. const __be32 *clk;
  56. int size;
  57. if (of_device_is_available(np)) {
  58. if (of_get_property(np, "sdhci,auto-cmd12", NULL))
  59. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  60. if (of_get_property(np, "sdhci,1-bit-only", NULL))
  61. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  62. if (sdhci_of_wp_inverted(np))
  63. host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
  64. clk = of_get_property(np, "clock-frequency", &size);
  65. if (clk && size == sizeof(*clk) && *clk)
  66. pltfm_host->clock = be32_to_cpup(clk);
  67. }
  68. }
  69. #else
  70. void sdhci_get_of_property(struct platform_device *pdev) {}
  71. #endif /* CONFIG_OF */
  72. EXPORT_SYMBOL_GPL(sdhci_get_of_property);
  73. struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev,
  74. struct sdhci_pltfm_data *pdata)
  75. {
  76. struct sdhci_host *host;
  77. struct sdhci_pltfm_host *pltfm_host;
  78. struct device_node *np = pdev->dev.of_node;
  79. struct resource *iomem;
  80. int ret;
  81. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  82. if (!iomem) {
  83. ret = -ENOMEM;
  84. goto err;
  85. }
  86. if (resource_size(iomem) < 0x100)
  87. dev_err(&pdev->dev, "Invalid iomem size!\n");
  88. /* Some PCI-based MFD need the parent here */
  89. if (pdev->dev.parent != &platform_bus && !np)
  90. host = sdhci_alloc_host(pdev->dev.parent, sizeof(*pltfm_host));
  91. else
  92. host = sdhci_alloc_host(&pdev->dev, sizeof(*pltfm_host));
  93. if (IS_ERR(host)) {
  94. ret = PTR_ERR(host);
  95. goto err;
  96. }
  97. pltfm_host = sdhci_priv(host);
  98. host->hw_name = dev_name(&pdev->dev);
  99. if (pdata && pdata->ops)
  100. host->ops = pdata->ops;
  101. else
  102. host->ops = &sdhci_pltfm_ops;
  103. if (pdata)
  104. host->quirks = pdata->quirks;
  105. host->irq = platform_get_irq(pdev, 0);
  106. if (!request_mem_region(iomem->start, resource_size(iomem),
  107. mmc_hostname(host->mmc))) {
  108. dev_err(&pdev->dev, "cannot request region\n");
  109. ret = -EBUSY;
  110. goto err_request;
  111. }
  112. host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  113. if (!host->ioaddr) {
  114. dev_err(&pdev->dev, "failed to remap registers\n");
  115. ret = -ENOMEM;
  116. goto err_remap;
  117. }
  118. platform_set_drvdata(pdev, host);
  119. return host;
  120. err_remap:
  121. release_mem_region(iomem->start, resource_size(iomem));
  122. err_request:
  123. sdhci_free_host(host);
  124. err:
  125. dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
  126. return ERR_PTR(ret);
  127. }
  128. EXPORT_SYMBOL_GPL(sdhci_pltfm_init);
  129. void sdhci_pltfm_free(struct platform_device *pdev)
  130. {
  131. struct sdhci_host *host = platform_get_drvdata(pdev);
  132. struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  133. iounmap(host->ioaddr);
  134. release_mem_region(iomem->start, resource_size(iomem));
  135. sdhci_free_host(host);
  136. platform_set_drvdata(pdev, NULL);
  137. }
  138. EXPORT_SYMBOL_GPL(sdhci_pltfm_free);
  139. int sdhci_pltfm_register(struct platform_device *pdev,
  140. struct sdhci_pltfm_data *pdata)
  141. {
  142. struct sdhci_host *host;
  143. int ret = 0;
  144. host = sdhci_pltfm_init(pdev, pdata);
  145. if (IS_ERR(host))
  146. return PTR_ERR(host);
  147. sdhci_get_of_property(pdev);
  148. ret = sdhci_add_host(host);
  149. if (ret)
  150. sdhci_pltfm_free(pdev);
  151. return ret;
  152. }
  153. EXPORT_SYMBOL_GPL(sdhci_pltfm_register);
  154. int sdhci_pltfm_unregister(struct platform_device *pdev)
  155. {
  156. struct sdhci_host *host = platform_get_drvdata(pdev);
  157. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  158. sdhci_remove_host(host, dead);
  159. sdhci_pltfm_free(pdev);
  160. return 0;
  161. }
  162. EXPORT_SYMBOL_GPL(sdhci_pltfm_unregister);
  163. #ifdef CONFIG_PM
  164. int sdhci_pltfm_suspend(struct platform_device *dev, pm_message_t state)
  165. {
  166. struct sdhci_host *host = platform_get_drvdata(dev);
  167. return sdhci_suspend_host(host, state);
  168. }
  169. EXPORT_SYMBOL_GPL(sdhci_pltfm_suspend);
  170. int sdhci_pltfm_resume(struct platform_device *dev)
  171. {
  172. struct sdhci_host *host = platform_get_drvdata(dev);
  173. return sdhci_resume_host(host);
  174. }
  175. EXPORT_SYMBOL_GPL(sdhci_pltfm_resume);
  176. #endif /* CONFIG_PM */
  177. static int __init sdhci_pltfm_drv_init(void)
  178. {
  179. pr_info("sdhci-pltfm: SDHCI platform and OF driver helper\n");
  180. return 0;
  181. }
  182. module_init(sdhci_pltfm_drv_init);
  183. static void __exit sdhci_pltfm_drv_exit(void)
  184. {
  185. }
  186. module_exit(sdhci_pltfm_drv_exit);
  187. MODULE_DESCRIPTION("SDHCI platform and OF driver helper");
  188. MODULE_AUTHOR("Intel Corporation");
  189. MODULE_LICENSE("GPL v2");