sdhci-pltfm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sdhci-pltfm.c Support for SDHCI platform devices
  4. * Copyright (c) 2009 Intel Corporation
  5. *
  6. * Copyright (c) 2007, 2011 Freescale Semiconductor, Inc.
  7. * Copyright (c) 2009 MontaVista Software, Inc.
  8. *
  9. * Authors: Xiaobo Xie <X.Xie@freescale.com>
  10. * Anton Vorontsov <avorontsov@ru.mvista.com>
  11. */
  12. /* Supports:
  13. * SDHCI platform devices
  14. *
  15. * Inspired by sdhci-pci.c, by Pierre Ossman
  16. */
  17. #include <linux/err.h>
  18. #include <linux/module.h>
  19. #include <linux/property.h>
  20. #include <linux/of.h>
  21. #ifdef CONFIG_PPC
  22. #include <asm/machdep.h>
  23. #endif
  24. #include "sdhci-pltfm.h"
  25. unsigned int sdhci_pltfm_clk_get_max_clock(struct sdhci_host *host)
  26. {
  27. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  28. return clk_get_rate(pltfm_host->clk);
  29. }
  30. EXPORT_SYMBOL_GPL(sdhci_pltfm_clk_get_max_clock);
  31. static const struct sdhci_ops sdhci_pltfm_ops = {
  32. .set_clock = sdhci_set_clock,
  33. .set_bus_width = sdhci_set_bus_width,
  34. .reset = sdhci_reset,
  35. .set_uhs_signaling = sdhci_set_uhs_signaling,
  36. };
  37. static bool sdhci_wp_inverted(struct device *dev)
  38. {
  39. if (device_property_present(dev, "sdhci,wp-inverted") ||
  40. device_property_present(dev, "wp-inverted"))
  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. #ifdef CONFIG_OF
  50. static void sdhci_get_compatibility(struct platform_device *pdev)
  51. {
  52. struct sdhci_host *host = platform_get_drvdata(pdev);
  53. struct device_node *np = pdev->dev.of_node;
  54. if (!np)
  55. return;
  56. if (of_device_is_compatible(np, "fsl,p2020-rev1-esdhc"))
  57. host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
  58. if (of_device_is_compatible(np, "fsl,p2020-esdhc") ||
  59. of_device_is_compatible(np, "fsl,p1010-esdhc") ||
  60. of_device_is_compatible(np, "fsl,t4240-esdhc") ||
  61. of_device_is_compatible(np, "fsl,mpc8536-esdhc"))
  62. host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
  63. }
  64. #else
  65. void sdhci_get_compatibility(struct platform_device *pdev) {}
  66. #endif /* CONFIG_OF */
  67. void sdhci_get_property(struct platform_device *pdev)
  68. {
  69. struct device *dev = &pdev->dev;
  70. struct sdhci_host *host = platform_get_drvdata(pdev);
  71. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  72. u32 bus_width;
  73. if (device_property_present(dev, "sdhci,auto-cmd12"))
  74. host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
  75. if (device_property_present(dev, "sdhci,1-bit-only") ||
  76. (device_property_read_u32(dev, "bus-width", &bus_width) == 0 &&
  77. bus_width == 1))
  78. host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
  79. if (sdhci_wp_inverted(dev))
  80. host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
  81. if (device_property_present(dev, "broken-cd"))
  82. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  83. if (device_property_present(dev, "no-1-8-v"))
  84. host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
  85. sdhci_get_compatibility(pdev);
  86. device_property_read_u32(dev, "clock-frequency", &pltfm_host->clock);
  87. if (device_property_present(dev, "keep-power-in-suspend"))
  88. host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
  89. if (device_property_read_bool(dev, "wakeup-source") ||
  90. device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
  91. host->mmc->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
  92. }
  93. EXPORT_SYMBOL_GPL(sdhci_get_property);
  94. struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev,
  95. const struct sdhci_pltfm_data *pdata,
  96. size_t priv_size)
  97. {
  98. struct sdhci_host *host;
  99. void __iomem *ioaddr;
  100. int irq, ret;
  101. ioaddr = devm_platform_ioremap_resource(pdev, 0);
  102. if (IS_ERR(ioaddr)) {
  103. ret = PTR_ERR(ioaddr);
  104. goto err;
  105. }
  106. irq = platform_get_irq(pdev, 0);
  107. if (irq < 0) {
  108. ret = irq;
  109. goto err;
  110. }
  111. host = sdhci_alloc_host(&pdev->dev,
  112. sizeof(struct sdhci_pltfm_host) + priv_size);
  113. if (IS_ERR(host)) {
  114. ret = PTR_ERR(host);
  115. goto err;
  116. }
  117. host->ioaddr = ioaddr;
  118. host->irq = irq;
  119. host->hw_name = dev_name(&pdev->dev);
  120. if (pdata && pdata->ops)
  121. host->ops = pdata->ops;
  122. else
  123. host->ops = &sdhci_pltfm_ops;
  124. if (pdata) {
  125. host->quirks = pdata->quirks;
  126. host->quirks2 = pdata->quirks2;
  127. }
  128. platform_set_drvdata(pdev, host);
  129. return host;
  130. err:
  131. dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
  132. return ERR_PTR(ret);
  133. }
  134. EXPORT_SYMBOL_GPL(sdhci_pltfm_init);
  135. void sdhci_pltfm_free(struct platform_device *pdev)
  136. {
  137. struct sdhci_host *host = platform_get_drvdata(pdev);
  138. sdhci_free_host(host);
  139. }
  140. EXPORT_SYMBOL_GPL(sdhci_pltfm_free);
  141. int sdhci_pltfm_register(struct platform_device *pdev,
  142. const struct sdhci_pltfm_data *pdata,
  143. size_t priv_size)
  144. {
  145. struct sdhci_host *host;
  146. int ret = 0;
  147. host = sdhci_pltfm_init(pdev, pdata, priv_size);
  148. if (IS_ERR(host))
  149. return PTR_ERR(host);
  150. sdhci_get_property(pdev);
  151. ret = sdhci_add_host(host);
  152. if (ret)
  153. sdhci_pltfm_free(pdev);
  154. return ret;
  155. }
  156. EXPORT_SYMBOL_GPL(sdhci_pltfm_register);
  157. int sdhci_pltfm_unregister(struct platform_device *pdev)
  158. {
  159. struct sdhci_host *host = platform_get_drvdata(pdev);
  160. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  161. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  162. sdhci_remove_host(host, dead);
  163. clk_disable_unprepare(pltfm_host->clk);
  164. sdhci_pltfm_free(pdev);
  165. return 0;
  166. }
  167. EXPORT_SYMBOL_GPL(sdhci_pltfm_unregister);
  168. #ifdef CONFIG_PM_SLEEP
  169. int sdhci_pltfm_suspend(struct device *dev)
  170. {
  171. struct sdhci_host *host = dev_get_drvdata(dev);
  172. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  173. int ret;
  174. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  175. mmc_retune_needed(host->mmc);
  176. ret = sdhci_suspend_host(host);
  177. if (ret)
  178. return ret;
  179. clk_disable_unprepare(pltfm_host->clk);
  180. return 0;
  181. }
  182. EXPORT_SYMBOL_GPL(sdhci_pltfm_suspend);
  183. int sdhci_pltfm_resume(struct device *dev)
  184. {
  185. struct sdhci_host *host = dev_get_drvdata(dev);
  186. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  187. int ret;
  188. ret = clk_prepare_enable(pltfm_host->clk);
  189. if (ret)
  190. return ret;
  191. ret = sdhci_resume_host(host);
  192. if (ret)
  193. clk_disable_unprepare(pltfm_host->clk);
  194. return ret;
  195. }
  196. EXPORT_SYMBOL_GPL(sdhci_pltfm_resume);
  197. #endif
  198. const struct dev_pm_ops sdhci_pltfm_pmops = {
  199. SET_SYSTEM_SLEEP_PM_OPS(sdhci_pltfm_suspend, sdhci_pltfm_resume)
  200. };
  201. EXPORT_SYMBOL_GPL(sdhci_pltfm_pmops);
  202. static int __init sdhci_pltfm_drv_init(void)
  203. {
  204. pr_info("sdhci-pltfm: SDHCI platform and OF driver helper\n");
  205. return 0;
  206. }
  207. module_init(sdhci_pltfm_drv_init);
  208. static void __exit sdhci_pltfm_drv_exit(void)
  209. {
  210. }
  211. module_exit(sdhci_pltfm_drv_exit);
  212. MODULE_DESCRIPTION("SDHCI platform and OF driver helper");
  213. MODULE_AUTHOR("Intel Corporation");
  214. MODULE_LICENSE("GPL v2");