sdhci-pltfm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/property.h>
  32. #include <linux/of.h>
  33. #ifdef CONFIG_PPC
  34. #include <asm/machdep.h>
  35. #endif
  36. #include "sdhci-pltfm.h"
  37. unsigned int sdhci_pltfm_clk_get_max_clock(struct sdhci_host *host)
  38. {
  39. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  40. return clk_get_rate(pltfm_host->clk);
  41. }
  42. EXPORT_SYMBOL_GPL(sdhci_pltfm_clk_get_max_clock);
  43. static const struct sdhci_ops sdhci_pltfm_ops = {
  44. .set_clock = sdhci_set_clock,
  45. .set_bus_width = sdhci_set_bus_width,
  46. .reset = sdhci_reset,
  47. .set_uhs_signaling = sdhci_set_uhs_signaling,
  48. };
  49. static bool sdhci_wp_inverted(struct device *dev)
  50. {
  51. if (device_property_present(dev, "sdhci,wp-inverted") ||
  52. device_property_present(dev, "wp-inverted"))
  53. return true;
  54. /* Old device trees don't have the wp-inverted property. */
  55. #ifdef CONFIG_PPC
  56. return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
  57. #else
  58. return false;
  59. #endif /* CONFIG_PPC */
  60. }
  61. #ifdef CONFIG_OF
  62. static void sdhci_get_compatibility(struct platform_device *pdev)
  63. {
  64. struct sdhci_host *host = platform_get_drvdata(pdev);
  65. struct device_node *np = pdev->dev.of_node;
  66. if (!np)
  67. return;
  68. if (of_device_is_compatible(np, "fsl,p2020-rev1-esdhc"))
  69. host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
  70. if (of_device_is_compatible(np, "fsl,p2020-esdhc") ||
  71. of_device_is_compatible(np, "fsl,p1010-esdhc") ||
  72. of_device_is_compatible(np, "fsl,t4240-esdhc") ||
  73. of_device_is_compatible(np, "fsl,mpc8536-esdhc"))
  74. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  75. }
  76. #else
  77. void sdhci_get_compatibility(struct platform_device *pdev) {}
  78. #endif /* CONFIG_OF */
  79. void sdhci_get_property(struct platform_device *pdev)
  80. {
  81. struct device *dev = &pdev->dev;
  82. struct sdhci_host *host = platform_get_drvdata(pdev);
  83. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  84. u32 bus_width;
  85. if (device_property_present(dev, "sdhci,auto-cmd12"))
  86. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  87. if (device_property_present(dev, "sdhci,1-bit-only") ||
  88. (device_property_read_u32(dev, "bus-width", &bus_width) == 0 &&
  89. bus_width == 1))
  90. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  91. if (sdhci_wp_inverted(dev))
  92. host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
  93. if (device_property_present(dev, "broken-cd"))
  94. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  95. if (device_property_present(dev, "no-1-8-v"))
  96. host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  97. sdhci_get_compatibility(pdev);
  98. device_property_read_u32(dev, "clock-frequency", &pltfm_host->clock);
  99. if (device_property_present(dev, "keep-power-in-suspend"))
  100. host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
  101. if (device_property_read_bool(dev, "wakeup-source") ||
  102. device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
  103. host->mmc->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
  104. }
  105. EXPORT_SYMBOL_GPL(sdhci_get_property);
  106. struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev,
  107. const struct sdhci_pltfm_data *pdata,
  108. size_t priv_size)
  109. {
  110. struct sdhci_host *host;
  111. struct resource *iomem;
  112. void __iomem *ioaddr;
  113. int irq, ret;
  114. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  115. ioaddr = devm_ioremap_resource(&pdev->dev, iomem);
  116. if (IS_ERR(ioaddr)) {
  117. ret = PTR_ERR(ioaddr);
  118. goto err;
  119. }
  120. irq = platform_get_irq(pdev, 0);
  121. if (irq < 0) {
  122. dev_err(&pdev->dev, "failed to get IRQ number\n");
  123. ret = irq;
  124. goto err;
  125. }
  126. host = sdhci_alloc_host(&pdev->dev,
  127. sizeof(struct sdhci_pltfm_host) + priv_size);
  128. if (IS_ERR(host)) {
  129. ret = PTR_ERR(host);
  130. goto err;
  131. }
  132. host->ioaddr = ioaddr;
  133. host->irq = irq;
  134. host->hw_name = dev_name(&pdev->dev);
  135. if (pdata && pdata->ops)
  136. host->ops = pdata->ops;
  137. else
  138. host->ops = &sdhci_pltfm_ops;
  139. if (pdata) {
  140. host->quirks = pdata->quirks;
  141. host->quirks2 = pdata->quirks2;
  142. }
  143. platform_set_drvdata(pdev, host);
  144. return host;
  145. err:
  146. dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
  147. return ERR_PTR(ret);
  148. }
  149. EXPORT_SYMBOL_GPL(sdhci_pltfm_init);
  150. void sdhci_pltfm_free(struct platform_device *pdev)
  151. {
  152. struct sdhci_host *host = platform_get_drvdata(pdev);
  153. sdhci_free_host(host);
  154. }
  155. EXPORT_SYMBOL_GPL(sdhci_pltfm_free);
  156. int sdhci_pltfm_register(struct platform_device *pdev,
  157. const struct sdhci_pltfm_data *pdata,
  158. size_t priv_size)
  159. {
  160. struct sdhci_host *host;
  161. int ret = 0;
  162. host = sdhci_pltfm_init(pdev, pdata, priv_size);
  163. if (IS_ERR(host))
  164. return PTR_ERR(host);
  165. sdhci_get_property(pdev);
  166. ret = sdhci_add_host(host);
  167. if (ret)
  168. sdhci_pltfm_free(pdev);
  169. return ret;
  170. }
  171. EXPORT_SYMBOL_GPL(sdhci_pltfm_register);
  172. int sdhci_pltfm_unregister(struct platform_device *pdev)
  173. {
  174. struct sdhci_host *host = platform_get_drvdata(pdev);
  175. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  176. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  177. sdhci_remove_host(host, dead);
  178. clk_disable_unprepare(pltfm_host->clk);
  179. sdhci_pltfm_free(pdev);
  180. return 0;
  181. }
  182. EXPORT_SYMBOL_GPL(sdhci_pltfm_unregister);
  183. #ifdef CONFIG_PM_SLEEP
  184. int sdhci_pltfm_suspend(struct device *dev)
  185. {
  186. struct sdhci_host *host = dev_get_drvdata(dev);
  187. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  188. int ret;
  189. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  190. mmc_retune_needed(host->mmc);
  191. ret = sdhci_suspend_host(host);
  192. if (ret)
  193. return ret;
  194. clk_disable_unprepare(pltfm_host->clk);
  195. return 0;
  196. }
  197. EXPORT_SYMBOL_GPL(sdhci_pltfm_suspend);
  198. int sdhci_pltfm_resume(struct device *dev)
  199. {
  200. struct sdhci_host *host = dev_get_drvdata(dev);
  201. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  202. int ret;
  203. ret = clk_prepare_enable(pltfm_host->clk);
  204. if (ret)
  205. return ret;
  206. ret = sdhci_resume_host(host);
  207. if (ret)
  208. clk_disable_unprepare(pltfm_host->clk);
  209. return ret;
  210. }
  211. EXPORT_SYMBOL_GPL(sdhci_pltfm_resume);
  212. #endif
  213. const struct dev_pm_ops sdhci_pltfm_pmops = {
  214. SET_SYSTEM_SLEEP_PM_OPS(sdhci_pltfm_suspend, sdhci_pltfm_resume)
  215. };
  216. EXPORT_SYMBOL_GPL(sdhci_pltfm_pmops);
  217. static int __init sdhci_pltfm_drv_init(void)
  218. {
  219. pr_info("sdhci-pltfm: SDHCI platform and OF driver helper\n");
  220. return 0;
  221. }
  222. module_init(sdhci_pltfm_drv_init);
  223. static void __exit sdhci_pltfm_drv_exit(void)
  224. {
  225. }
  226. module_exit(sdhci_pltfm_drv_exit);
  227. MODULE_DESCRIPTION("SDHCI platform and OF driver helper");
  228. MODULE_AUTHOR("Intel Corporation");
  229. MODULE_LICENSE("GPL v2");