sdhci-pltfm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * sdhci-pltfm.c Support for SDHCI platform devices
  3. * Copyright (c) 2009 Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* Supports:
  19. * SDHCI platform devices
  20. *
  21. * Inspired by sdhci-pci.c, by Pierre Ossman
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/highmem.h>
  25. #include <linux/mod_devicetable.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/mmc/host.h>
  28. #include <linux/io.h>
  29. #include <linux/mmc/sdhci-pltfm.h>
  30. #include "sdhci.h"
  31. #include "sdhci-pltfm.h"
  32. /*****************************************************************************\
  33. * *
  34. * SDHCI core callbacks *
  35. * *
  36. \*****************************************************************************/
  37. static struct sdhci_ops sdhci_pltfm_ops = {
  38. };
  39. /*****************************************************************************\
  40. * *
  41. * Device probing/removal *
  42. * *
  43. \*****************************************************************************/
  44. static int __devinit sdhci_pltfm_probe(struct platform_device *pdev)
  45. {
  46. const struct platform_device_id *platid = platform_get_device_id(pdev);
  47. struct sdhci_pltfm_data *pdata;
  48. struct sdhci_host *host;
  49. struct sdhci_pltfm_host *pltfm_host;
  50. struct resource *iomem;
  51. int ret;
  52. if (platid && platid->driver_data)
  53. pdata = (void *)platid->driver_data;
  54. else
  55. pdata = pdev->dev.platform_data;
  56. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  57. if (!iomem) {
  58. ret = -ENOMEM;
  59. goto err;
  60. }
  61. if (resource_size(iomem) < 0x100)
  62. dev_err(&pdev->dev, "Invalid iomem size. You may "
  63. "experience problems.\n");
  64. /* Some PCI-based MFD need the parent here */
  65. if (pdev->dev.parent != &platform_bus)
  66. host = sdhci_alloc_host(pdev->dev.parent, sizeof(*pltfm_host));
  67. else
  68. host = sdhci_alloc_host(&pdev->dev, sizeof(*pltfm_host));
  69. if (IS_ERR(host)) {
  70. ret = PTR_ERR(host);
  71. goto err;
  72. }
  73. pltfm_host = sdhci_priv(host);
  74. host->hw_name = "platform";
  75. if (pdata && pdata->ops)
  76. host->ops = pdata->ops;
  77. else
  78. host->ops = &sdhci_pltfm_ops;
  79. if (pdata)
  80. host->quirks = pdata->quirks;
  81. host->irq = platform_get_irq(pdev, 0);
  82. if (!request_mem_region(iomem->start, resource_size(iomem),
  83. mmc_hostname(host->mmc))) {
  84. dev_err(&pdev->dev, "cannot request region\n");
  85. ret = -EBUSY;
  86. goto err_request;
  87. }
  88. host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  89. if (!host->ioaddr) {
  90. dev_err(&pdev->dev, "failed to remap registers\n");
  91. ret = -ENOMEM;
  92. goto err_remap;
  93. }
  94. if (pdata && pdata->init) {
  95. ret = pdata->init(host, pdata);
  96. if (ret)
  97. goto err_plat_init;
  98. }
  99. ret = sdhci_add_host(host);
  100. if (ret)
  101. goto err_add_host;
  102. platform_set_drvdata(pdev, host);
  103. return 0;
  104. err_add_host:
  105. if (pdata && pdata->exit)
  106. pdata->exit(host);
  107. err_plat_init:
  108. iounmap(host->ioaddr);
  109. err_remap:
  110. release_mem_region(iomem->start, resource_size(iomem));
  111. err_request:
  112. sdhci_free_host(host);
  113. err:
  114. printk(KERN_ERR"Probing of sdhci-pltfm failed: %d\n", ret);
  115. return ret;
  116. }
  117. static int __devexit sdhci_pltfm_remove(struct platform_device *pdev)
  118. {
  119. struct sdhci_pltfm_data *pdata = pdev->dev.platform_data;
  120. struct sdhci_host *host = platform_get_drvdata(pdev);
  121. struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  122. int dead;
  123. u32 scratch;
  124. dead = 0;
  125. scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  126. if (scratch == (u32)-1)
  127. dead = 1;
  128. sdhci_remove_host(host, dead);
  129. if (pdata && pdata->exit)
  130. pdata->exit(host);
  131. iounmap(host->ioaddr);
  132. release_mem_region(iomem->start, resource_size(iomem));
  133. sdhci_free_host(host);
  134. platform_set_drvdata(pdev, NULL);
  135. return 0;
  136. }
  137. static const struct platform_device_id sdhci_pltfm_ids[] = {
  138. { "sdhci", },
  139. #ifdef CONFIG_MMC_SDHCI_CNS3XXX
  140. { "sdhci-cns3xxx", (kernel_ulong_t)&sdhci_cns3xxx_pdata },
  141. #endif
  142. #ifdef CONFIG_MMC_SDHCI_ESDHC_IMX
  143. { "sdhci-esdhc-imx", (kernel_ulong_t)&sdhci_esdhc_imx_pdata },
  144. #endif
  145. #ifdef CONFIG_MMC_SDHCI_DOVE
  146. { "sdhci-dove", (kernel_ulong_t)&sdhci_dove_pdata },
  147. #endif
  148. #ifdef CONFIG_MMC_SDHCI_TEGRA
  149. { "sdhci-tegra", (kernel_ulong_t)&sdhci_tegra_pdata },
  150. #endif
  151. { },
  152. };
  153. MODULE_DEVICE_TABLE(platform, sdhci_pltfm_ids);
  154. #ifdef CONFIG_PM
  155. static int sdhci_pltfm_suspend(struct platform_device *dev, pm_message_t state)
  156. {
  157. struct sdhci_host *host = platform_get_drvdata(dev);
  158. return sdhci_suspend_host(host, state);
  159. }
  160. static int sdhci_pltfm_resume(struct platform_device *dev)
  161. {
  162. struct sdhci_host *host = platform_get_drvdata(dev);
  163. return sdhci_resume_host(host);
  164. }
  165. #else
  166. #define sdhci_pltfm_suspend NULL
  167. #define sdhci_pltfm_resume NULL
  168. #endif /* CONFIG_PM */
  169. static struct platform_driver sdhci_pltfm_driver = {
  170. .driver = {
  171. .name = "sdhci",
  172. .owner = THIS_MODULE,
  173. },
  174. .probe = sdhci_pltfm_probe,
  175. .remove = __devexit_p(sdhci_pltfm_remove),
  176. .id_table = sdhci_pltfm_ids,
  177. .suspend = sdhci_pltfm_suspend,
  178. .resume = sdhci_pltfm_resume,
  179. };
  180. /*****************************************************************************\
  181. * *
  182. * Driver init/exit *
  183. * *
  184. \*****************************************************************************/
  185. static int __init sdhci_drv_init(void)
  186. {
  187. return platform_driver_register(&sdhci_pltfm_driver);
  188. }
  189. static void __exit sdhci_drv_exit(void)
  190. {
  191. platform_driver_unregister(&sdhci_pltfm_driver);
  192. }
  193. module_init(sdhci_drv_init);
  194. module_exit(sdhci_drv_exit);
  195. MODULE_DESCRIPTION("Secure Digital Host Controller Interface platform driver");
  196. MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
  197. MODULE_LICENSE("GPL v2");