sdhci-pltfm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * sdhci-pltfm.c Support for SDHCI platform devices
  3. * Copyright (c) 2009 Intel Corporation
  4. *
  5. * Copyright (c) 2007, 2011 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. of_get_property(np, "wp-inverted", NULL))
  43. return true;
  44. /* Old device trees don't have the wp-inverted property. */
  45. #ifdef CONFIG_PPC
  46. return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
  47. #else
  48. return false;
  49. #endif /* CONFIG_PPC */
  50. }
  51. void sdhci_get_of_property(struct platform_device *pdev)
  52. {
  53. struct device_node *np = pdev->dev.of_node;
  54. struct sdhci_host *host = platform_get_drvdata(pdev);
  55. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  56. const __be32 *clk;
  57. u32 bus_width;
  58. int size;
  59. if (of_device_is_available(np)) {
  60. if (of_get_property(np, "sdhci,auto-cmd12", NULL))
  61. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  62. if (of_get_property(np, "sdhci,1-bit-only", NULL) ||
  63. (of_property_read_u32(np, "bus-width", &bus_width) == 0 &&
  64. bus_width == 1))
  65. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  66. if (sdhci_of_wp_inverted(np))
  67. host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
  68. if (of_get_property(np, "broken-cd", NULL))
  69. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  70. if (of_get_property(np, "no-1-8-v", NULL))
  71. host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  72. if (of_device_is_compatible(np, "fsl,p2020-rev1-esdhc"))
  73. host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
  74. if (of_device_is_compatible(np, "fsl,p2020-esdhc") ||
  75. of_device_is_compatible(np, "fsl,p1010-esdhc") ||
  76. of_device_is_compatible(np, "fsl,mpc8536-esdhc"))
  77. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  78. clk = of_get_property(np, "clock-frequency", &size);
  79. if (clk && size == sizeof(*clk) && *clk)
  80. pltfm_host->clock = be32_to_cpup(clk);
  81. if (of_find_property(np, "keep-power-in-suspend", NULL))
  82. host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
  83. if (of_find_property(np, "enable-sdio-wakeup", NULL))
  84. host->mmc->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
  85. }
  86. }
  87. #else
  88. void sdhci_get_of_property(struct platform_device *pdev) {}
  89. #endif /* CONFIG_OF */
  90. EXPORT_SYMBOL_GPL(sdhci_get_of_property);
  91. struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev,
  92. struct sdhci_pltfm_data *pdata)
  93. {
  94. struct sdhci_host *host;
  95. struct sdhci_pltfm_host *pltfm_host;
  96. struct device_node *np = pdev->dev.of_node;
  97. struct resource *iomem;
  98. int ret;
  99. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  100. if (!iomem) {
  101. ret = -ENOMEM;
  102. goto err;
  103. }
  104. if (resource_size(iomem) < 0x100)
  105. dev_err(&pdev->dev, "Invalid iomem size!\n");
  106. /* Some PCI-based MFD need the parent here */
  107. if (pdev->dev.parent != &platform_bus && !np)
  108. host = sdhci_alloc_host(pdev->dev.parent, sizeof(*pltfm_host));
  109. else
  110. host = sdhci_alloc_host(&pdev->dev, sizeof(*pltfm_host));
  111. if (IS_ERR(host)) {
  112. ret = PTR_ERR(host);
  113. goto err;
  114. }
  115. pltfm_host = sdhci_priv(host);
  116. host->hw_name = dev_name(&pdev->dev);
  117. if (pdata && pdata->ops)
  118. host->ops = pdata->ops;
  119. else
  120. host->ops = &sdhci_pltfm_ops;
  121. if (pdata)
  122. host->quirks = pdata->quirks;
  123. host->irq = platform_get_irq(pdev, 0);
  124. if (!request_mem_region(iomem->start, resource_size(iomem),
  125. mmc_hostname(host->mmc))) {
  126. dev_err(&pdev->dev, "cannot request region\n");
  127. ret = -EBUSY;
  128. goto err_request;
  129. }
  130. host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  131. if (!host->ioaddr) {
  132. dev_err(&pdev->dev, "failed to remap registers\n");
  133. ret = -ENOMEM;
  134. goto err_remap;
  135. }
  136. /*
  137. * Some platforms need to probe the controller to be able to
  138. * determine which caps should be used.
  139. */
  140. if (host->ops && host->ops->platform_init)
  141. host->ops->platform_init(host);
  142. platform_set_drvdata(pdev, host);
  143. return host;
  144. err_remap:
  145. release_mem_region(iomem->start, resource_size(iomem));
  146. err_request:
  147. sdhci_free_host(host);
  148. err:
  149. dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
  150. return ERR_PTR(ret);
  151. }
  152. EXPORT_SYMBOL_GPL(sdhci_pltfm_init);
  153. void sdhci_pltfm_free(struct platform_device *pdev)
  154. {
  155. struct sdhci_host *host = platform_get_drvdata(pdev);
  156. struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  157. iounmap(host->ioaddr);
  158. release_mem_region(iomem->start, resource_size(iomem));
  159. sdhci_free_host(host);
  160. platform_set_drvdata(pdev, NULL);
  161. }
  162. EXPORT_SYMBOL_GPL(sdhci_pltfm_free);
  163. int sdhci_pltfm_register(struct platform_device *pdev,
  164. struct sdhci_pltfm_data *pdata)
  165. {
  166. struct sdhci_host *host;
  167. int ret = 0;
  168. host = sdhci_pltfm_init(pdev, pdata);
  169. if (IS_ERR(host))
  170. return PTR_ERR(host);
  171. sdhci_get_of_property(pdev);
  172. ret = sdhci_add_host(host);
  173. if (ret)
  174. sdhci_pltfm_free(pdev);
  175. return ret;
  176. }
  177. EXPORT_SYMBOL_GPL(sdhci_pltfm_register);
  178. int sdhci_pltfm_unregister(struct platform_device *pdev)
  179. {
  180. struct sdhci_host *host = platform_get_drvdata(pdev);
  181. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  182. sdhci_remove_host(host, dead);
  183. sdhci_pltfm_free(pdev);
  184. return 0;
  185. }
  186. EXPORT_SYMBOL_GPL(sdhci_pltfm_unregister);
  187. #ifdef CONFIG_PM
  188. static int sdhci_pltfm_suspend(struct device *dev)
  189. {
  190. struct sdhci_host *host = dev_get_drvdata(dev);
  191. return sdhci_suspend_host(host);
  192. }
  193. static int sdhci_pltfm_resume(struct device *dev)
  194. {
  195. struct sdhci_host *host = dev_get_drvdata(dev);
  196. return sdhci_resume_host(host);
  197. }
  198. const struct dev_pm_ops sdhci_pltfm_pmops = {
  199. .suspend = sdhci_pltfm_suspend,
  200. .resume = sdhci_pltfm_resume,
  201. };
  202. EXPORT_SYMBOL_GPL(sdhci_pltfm_pmops);
  203. #endif /* CONFIG_PM */
  204. static int __init sdhci_pltfm_drv_init(void)
  205. {
  206. pr_info("sdhci-pltfm: SDHCI platform and OF driver helper\n");
  207. return 0;
  208. }
  209. module_init(sdhci_pltfm_drv_init);
  210. static void __exit sdhci_pltfm_drv_exit(void)
  211. {
  212. }
  213. module_exit(sdhci_pltfm_drv_exit);
  214. MODULE_DESCRIPTION("SDHCI platform and OF driver helper");
  215. MODULE_AUTHOR("Intel Corporation");
  216. MODULE_LICENSE("GPL v2");