sdhci-pltfm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/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. struct sdhci_pltfm_data *pdata = pdev->dev.platform_data;
  47. const struct platform_device_id *platid = platform_get_device_id(pdev);
  48. struct sdhci_host *host;
  49. struct resource *iomem;
  50. int ret;
  51. if (!pdata && platid && platid->driver_data)
  52. pdata = (void *)platid->driver_data;
  53. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  54. if (!iomem) {
  55. ret = -ENOMEM;
  56. goto err;
  57. }
  58. if (resource_size(iomem) < 0x100)
  59. dev_err(&pdev->dev, "Invalid iomem size. You may "
  60. "experience problems.\n");
  61. if (pdev->dev.parent)
  62. host = sdhci_alloc_host(pdev->dev.parent, 0);
  63. else
  64. host = sdhci_alloc_host(&pdev->dev, 0);
  65. if (IS_ERR(host)) {
  66. ret = PTR_ERR(host);
  67. goto err;
  68. }
  69. host->hw_name = "platform";
  70. if (pdata && pdata->ops)
  71. host->ops = pdata->ops;
  72. else
  73. host->ops = &sdhci_pltfm_ops;
  74. if (pdata)
  75. host->quirks = pdata->quirks;
  76. host->irq = platform_get_irq(pdev, 0);
  77. if (!request_mem_region(iomem->start, resource_size(iomem),
  78. mmc_hostname(host->mmc))) {
  79. dev_err(&pdev->dev, "cannot request region\n");
  80. ret = -EBUSY;
  81. goto err_request;
  82. }
  83. host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  84. if (!host->ioaddr) {
  85. dev_err(&pdev->dev, "failed to remap registers\n");
  86. ret = -ENOMEM;
  87. goto err_remap;
  88. }
  89. if (pdata && pdata->init) {
  90. ret = pdata->init(host);
  91. if (ret)
  92. goto err_plat_init;
  93. }
  94. ret = sdhci_add_host(host);
  95. if (ret)
  96. goto err_add_host;
  97. platform_set_drvdata(pdev, host);
  98. return 0;
  99. err_add_host:
  100. if (pdata && pdata->exit)
  101. pdata->exit(host);
  102. err_plat_init:
  103. iounmap(host->ioaddr);
  104. err_remap:
  105. release_mem_region(iomem->start, resource_size(iomem));
  106. err_request:
  107. sdhci_free_host(host);
  108. err:
  109. printk(KERN_ERR"Probing of sdhci-pltfm failed: %d\n", ret);
  110. return ret;
  111. }
  112. static int __devexit sdhci_pltfm_remove(struct platform_device *pdev)
  113. {
  114. struct sdhci_pltfm_data *pdata = pdev->dev.platform_data;
  115. struct sdhci_host *host = platform_get_drvdata(pdev);
  116. struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  117. int dead;
  118. u32 scratch;
  119. dead = 0;
  120. scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  121. if (scratch == (u32)-1)
  122. dead = 1;
  123. sdhci_remove_host(host, dead);
  124. if (pdata && pdata->exit)
  125. pdata->exit(host);
  126. iounmap(host->ioaddr);
  127. release_mem_region(iomem->start, resource_size(iomem));
  128. sdhci_free_host(host);
  129. platform_set_drvdata(pdev, NULL);
  130. return 0;
  131. }
  132. static const struct platform_device_id sdhci_pltfm_ids[] = {
  133. { "sdhci", },
  134. #ifdef CONFIG_MMC_SDHCI_CNS3XXX
  135. { "sdhci-cns3xxx", (kernel_ulong_t)&sdhci_cns3xxx_pdata },
  136. #endif
  137. { },
  138. };
  139. MODULE_DEVICE_TABLE(platform, sdhci_pltfm_ids);
  140. static struct platform_driver sdhci_pltfm_driver = {
  141. .driver = {
  142. .name = "sdhci",
  143. .owner = THIS_MODULE,
  144. },
  145. .probe = sdhci_pltfm_probe,
  146. .remove = __devexit_p(sdhci_pltfm_remove),
  147. .id_table = sdhci_pltfm_ids,
  148. };
  149. /*****************************************************************************\
  150. * *
  151. * Driver init/exit *
  152. * *
  153. \*****************************************************************************/
  154. static int __init sdhci_drv_init(void)
  155. {
  156. return platform_driver_register(&sdhci_pltfm_driver);
  157. }
  158. static void __exit sdhci_drv_exit(void)
  159. {
  160. platform_driver_unregister(&sdhci_pltfm_driver);
  161. }
  162. module_init(sdhci_drv_init);
  163. module_exit(sdhci_drv_exit);
  164. MODULE_DESCRIPTION("Secure Digital Host Controller Interface platform driver");
  165. MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
  166. MODULE_LICENSE("GPL v2");