sdhci-pltfm.c 7.1 KB

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